2

I have data in an array.
The first column is time. Second, latitude, third longitude, fourth precipitation
Sample:

2 70 100 5.6 
2 70 110 5.9 
2 80 100 6.2 
2 80 110 5.0 
3 70 100 2.3 
3 70 110 1.1 
3 80 100 0.0 
3 80 110 7.9 

I would like to convert this into an array where the y axis is longitude, the z axis is latitude, and the x axis is time.
Precipitation amounts will be located at each 3d grid point.

For instance, in the following image:

enter image description here
The sizes of the bubbles represent different precipitation amounts (ignore the colors)

How can I use python to do this?

So far I have:

import numpy as np<br>
a=open('time.dat') #original file
b=open('three.dat','w+')
dif=np.fromfile(a) 
tim=dif[:,[0]] 
lat=dif[:,[1]] 
lon=dif[:,[2]] 
pre=dif[:,[3]]
c=np.empty(780,360,720)

780 time steps, 360 latitudes, 720 longitudes

user3756366
  • 37
  • 2
  • 9

2 Answers2

2

So you want a 2 dimensional array with the inner dimension containing all of the data, and the outer dimension ordered by lon, lat, time.

You can read in the file as a array of values, convert to a 2d array to group them into each 4 tuple. Then translate the column order of the inner array. Next sort the outer dimension on the inner dimension.

>>> data = np.array([2, 70, 100, 5.6, 2, 70, 110, 5.9, 2, 80, 100, 6.2, 2, 80, 110, 5.0, 3, 70, 100, 2.3, 3, 70, 110, 1.1, 3, 80, 100, 0.0, 3, 80, 110, 7.9])
>>> data2 = data.reshape((8, 4))
>>> data2
array([[   2. ,   70. ,  100. ,    5.6],
       [   2. ,   70. ,  110. ,    5.9],
       [   2. ,   80. ,  100. ,    6.2],
       [   2. ,   80. ,  110. ,    5. ],
       [   3. ,   70. ,  100. ,    2.3],
       [   3. ,   70. ,  110. ,    1.1],
       [   3. ,   80. ,  100. ,    0. ],
       [   3. ,   80. ,  110. ,    7.9]])
>>> data2 = data2[:,[1,2,0,3]]
>>> data2
array([[  70. ,  100. ,    2. ,    5.6],
       [  70. ,  110. ,    2. ,    5.9],
       [  80. ,  100. ,    2. ,    6.2],
       [  80. ,  110. ,    2. ,    5. ],
       [  70. ,  100. ,    3. ,    2.3],
       [  70. ,  110. ,    3. ,    1.1],
       [  80. ,  100. ,    3. ,    0. ],
       [  80. ,  110. ,    3. ,    7.9]])

The goofiness with view and sort described here

Community
  • 1
  • 1
cs_alumnus
  • 1,579
  • 16
  • 24
0

You can't use the numpy reshape for a simple reason : you have data duplicity in your original array (time and positions) and not in the result you want. Before and after a reshape the number of elements must be the same.

You have to do a loop to read your initial array and fill your new array.

Hope it helped

caymard
  • 158
  • 2
  • 10
  • So, I would do a nested for-loop? How do I fill in my new array? – user3756366 Jun 19 '14 at 15:11
  • Assuming you know how many time points, longitudes, latitudes you have, you can access to you precipitations datas for any time/latitude/longitude. For each time you fill the "mini-matrix" and go through next time. Sorry don't have time to write you a bit of code – caymard Jun 19 '14 at 15:16
  • So Far I have ` import numpy as np a=open('time.dat') #original file b=open('three.dat','w+') dif=np.fromfile(a) tim=dif[:,[0]] lat=dif[:,[1]] lon=dif[:,[2]] pre=dif[:,[3]] ` – user3756366 Jun 19 '14 at 15:22