[Numpy-discussion] numpy sum each month and then numpy mean of all months
questions anon
questions.anon at gmail.com
Mon Mar 24 02:39:50 EDT 2014
Hello all,
I have netcdf files that contain hourly rainfall data. Each netcdf file
includes one months worth of hours and I have 10 years worth of data. I
would like to calculate the sum of each month and then the mean of these
summed months across all of the years.
I have no problem firstly calculating the sum of each month but then I come
up with a mask-size error when I try to calculate the mean of those months.
So somehow I am not combining my summed months correctly?
Below is the code I am using (focusing on just january at this stage) and
below that is the error.
Any feedback will be greatly appreciated.
from netCDF4 import Dataset
import numpy as N
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import os
shapefile1="/Users/REGIONS"
OutputFolder=r"/Users/rainmonthlysummarystats/"
fileforlatlon=Dataset("/Users/WRFsample/IDV71000_VIC_T_SFC.nc", 'r+',
'NETCDF4')
LAT=fileforlatlon.variables['latitude'][:]
LON=fileforlatlon.variables['longitude'][:]
def summaryplots(variable):
if variable=='RAIN':
ncvariablename='PCP_SFC'
MainFolder=r"/Data/WRFmonthly/"
ticks=[0, 25, 50, 75, 100, 125, 150, 175, 200]
cmap=plt.cm.jet
Jan="01"
monthseason="Jan"
summonthlyrain_all=[]
all_variabledata=[]
for (path, dirs, files) in os.walk(MainFolder):
if os.path.basename(path)==Jan:
for ncfile in files:
fileext=ncvariablename+'.nc'
if ncfile.endswith(fileext):
print "dealing with ncfiles:", path+ncfile
ncfile=os.path.join(path,ncfile)
ncfile=Dataset(ncfile, 'r+', 'NETCDF4')
variable=ncfile.variables[ncvariablename][:,:,:]
ncfile.close()
all_variabledata.append(variable) #combine all data from the chosen
variable to make one array for analyses
big_array=N.ma.concatenate(all_variabledata)
SUM=big_array.sum(axis=0)
summonthlyrain_all.append(SUM)
del all_variabledata[:]
big_array_sumrain=N.ma.concatenate(summonthlyrain_all)
MEAN=big_array_sumrain.mean(axis=0)
#plot output summary stats
map = Basemap(projection='merc',llcrnrlat=-40,urcrnrlat=-33,
llcrnrlon=139.0,urcrnrlon=151.0,lat_ts=0,resolution='i')
map.drawcoastlines()
map.drawstates()
map.readshapefile(shapefile1, 'DSE_REGIONS')
x,y=map(*N.meshgrid(LON,LAT))
plottitle=ncvariablename+'_mean_'+monthseason
plt.title(plottitle)
CS = map.contourf(x,y,MEAN, ticks, cmap=cmap)
l,b,w,h =0.1,0.1,0.8,0.8
cax = plt.axes([l+w+0.025, b, 0.025, h])
plt.colorbar(CS,cax=cax, drawedges=True)
plt.savefig((os.path.join(OutputFolder, plottitle+'.png')))
plt.show()
plt.close()
summaryplots('RAIN')
MaskError Traceback (most recent call last)
/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/IPython/utils/py3compat.pyc
in execfile(fname, *where)
202 else:
203 filename = fname
--> 204 __builtin__.execfile(filename, *where)
/Users/slburns/Dropbox/Python_code/WRFoutputs/plot_variable_sum_monthlyrain_percentiles_test.py
in <module>()
71
72
---> 73 summaryplots('RAIN')
74
75
/Users/slburns/Dropbox/Python_code/WRFoutputs/plot_variable_sum_monthlyrain_percentiles_test.py
in summaryplots(variable)
62 plottitle=ncvariablename+'_mean_'+monthseason
63 plt.title(plottitle)
---> 64 CS = map.contourf(x,y,MEAN, ticks, cmap=cmap)
65 l,b,w,h =0.1,0.1,0.8,0.8
66 cax = plt.axes([l+w+0.025, b, 0.025, h])
/Users/slburns/Applications/User/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc
in with_transform(self, x, y, data, *args, **kwargs)
519 # convert lat/lon coords to map projection coords.
520 x, y = self(x,y)
--> 521 return plotfunc(self,x,y,data,*args,**kwargs)
522 return with_transform
523
/Users/slburns/Applications/User/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc
in contourf(self, x, y, data, *args, **kwargs)
3670 # combine with data mask.
3671 mask = np.logical_or(ma.getmaskarray(data),xymask)
-> 3672 data = ma.masked_array(data,mask=mask)
3673 CS = ax.contourf(x,y,data,*args,**kwargs)
3674 except:
/Users/slburns/Applications/User/lib/python2.7/site-packages/numpy/ma/core.pyc
in __new__(cls, data, mask, dtype, copy, subok, ndmin, fill_value,
keep_mask, hard_mask, shrink, **options)
2708 msg = "Mask and data not compatible: data size is %i, " + 2709 "mask
size is %i."
-> 2710 raise MaskError(msg % (nd, nm))
2711 copy = True
2712 # Set the mask to the new value
MaskError: Mask and data not compatible: data size is 193, mask size is
20458.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140324/359e38e5/attachment.html>
More information about the NumPy-Discussion
mailing list