简体   繁体   中英

How to store months from NETcdf time dimension (0 corresponding to jan)?

I have a 3d NETcdf dataset and am trying to work with the precipitation variable. I would like to plot the monthly averages of it over 60 years, but am having difficulty producing a plot that is consistent with previous averaging calculations I did not involving for loops. Here is how I store the values for the january, february and take the mean:

janNES = np.empty_like(conprecip[0:720,6:-11,95:141])

for i in range(0,720,12):
     janNES[i]=(NESprecip[i,6:-11,95:141])          

mjanNES=(np.mean(janNES, axis=0))

febNES = np.empty_like(conprecip[0:720,6:-11,95:141])

for i in range(1,720,12):
     febNES[i]=(NESprecip[i,6:-11,95:141]) 

mfebNES=(np.mean(febNES, axis=0))

#values that are plotted, monthly 60 year averages
np.mean(mjanNES-mjan)
np.mean(mfebNES-mfeb)

Where 0-11 is jan to dec, so I store values from the other months similarly. I believe there is something wrong with the way I am storing the data as its producing weird results after I subtract the control(mjan and mfeb, store using this method) and take the mean again. Thank you for reading and helping.

The size of the first dimension of your janNES array is 720, but when you loop over time with:

for i in range(0,720,12):
    janNES[i]=...

You are only filling every 12th ( 0,12,24,... ) item, but in the end take the mean over the entire array. You probably want to create your array as:

janNES = np.empty_like(conprecip[0:720:12,6:-11,95:141])

And then loop over it with eg:

for ii,i in enumerate(range(0,720,12)):
    janNES[ii]=(NESprecip[i,6:-11,95:141])

ii now runs from 0,1,2,..,59 while i runs from 0,12,24,..,708 .


By the way, you don't need the for loops for this, Numpy can select the data directly by slicing the original array:

janNES = NESprecip[0:720:12, 6:-11, 95:141]
febNES = NESprecip[1:720:12, 6:-11, 95:141]

And you can even calculate the mean directly from this:

mjanNES = NESprecip[0:720:12, 6:-11, 95:141].mean(axis=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM