laff
laff

Reputation: 1

Extract every value in a 3D (time, lat, lon) array and create a list or 1D array

Forgive me if this is simple but I am new to python. I have daily wind speed data with one data point for every latitude(180) and longitude(360) and time(6624) which is a 3D array with numpy.shape (time, lat, lon). I am trying to extract every wind speed and put it into a new array or list so that I can plot a histogram or a probability density function. Is there a way in python to extract each of these values?

Upvotes: 0

Views: 935

Answers (2)

B. M.
B. M.

Reputation: 18658

Your data are huge, so you must first have global approach.

As a toy example :

from pylab import *

wind = rand(662,18,36)
means = wind.mean(axis=0)
subplot(121)
hist(means.ravel(),100)  
subplot(122)
imshow(means)
colorbar()
show()

enter image description here

Then you can decide which area you will refine.

Upvotes: 1

ahed87
ahed87

Reputation: 1360

so if you do wind_speedjja.shape you get (6624, 180, 360)?

This is not an efficient answer, more written for being illustrative with a nested loop.

all_wsp = np.array([])
mtx = wind_speed.shape
for idx_lat in range(mtx[1]):
    for idx_long in range(mtx[2]):
        lat_long_wsp = wind_speed[:, idx_lat, idx_long]
        # do a plot on lat_long_wsp, or your histogram
        all_wsp = np.concatenate((all_wsp, lat_long_wsp))
        # all_wsp will be all single values in a flattened array

If you are just after the flattened array, do flat_wsp = windspeed.flatten().

Upvotes: 1

Related Questions