[SciPy-User] numpy array append
Tim Supinie
tsupinie@gmail....
Tue Aug 16 18:13:49 CDT 2011
Ah, TSFC is being overwritten every time you go through the "for ncfile in
files" loop. So if you want to keep around all of them, you should declare
a list before that loop called all_TSFC or something. Then instead of
saying
TSFC = ncfile...
you would say
all_TSFC.append(ncfile...)
After that you could remove the second loop (for i in TSFC) and simply say
# Convert all_TSFC to a numpy array. Not sure what happens
# if some lists in all_TSFC are of different sizes, so you
# should probably make sure they're all the same size.
big_array = N.array(all_TSFC)
# Take the mean of big_array along axis 0 (returns a
# 1-dimensional numpy array the size of one of the lists in
# all_TSFC).
Mean = N.mean(big_array, axis=0)
Hope that helps.
Tim
On Tue, Aug 16, 2011 at 5:50 PM, questions anon <questions.anon@gmail.com>wrote:
> I would like to loop through a bunch of netcdf files in separate folders
> and select a particular time and then calculate the mean and plot this. I
> have been told to use append and make the selected times into a big array
> and then use numpy.mean but I can't seem to get the numpy array to work. The
> loop keeps calculating over the top of the last entry, if that makes sense?
>
> from netCDF4 import Dataset
> import matplotlib.pyplot as plt
> import numpy as N
> from mpl_toolkits.basemap import Basemap
> import os
>
> MainFolder=r"E:/temp_samples/"
> for (path, dirs, files) in os.walk(MainFolder):
> for dir in dirs:
> print dir
> for ncfile in files:
> if ncfile[-3:]=='.nc':
> ncfile=os.path.join(path,ncfile)
> ncfile=Dataset(ncfile, 'r+', 'NETCDF4')
> TSFC=ncfile.variables['T_SFC'][4::24]
> LAT=ncfile.variables['latitude'][:]
> LON=ncfile.variables['longitude'][:]
> TIME=ncfile.variables['time'][:]
> fillvalue=ncfile.variables['T_SFC']._FillValue
> ncfile.close()
>
> #calculate summary stats
> big_array=[]
> for i in TSFC:
> big_array.append(i)
> big_array=N.array(big_array)
> Mean=N.mean(big_array, 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()
> x,y=map(*N.meshgrid(LON,LAT))
> plt.title('Total Mean at 3pm')
> ticks=[-5,0,5,10,15,20,25,30,35,40,45,50]
> CS = map.contourf(x,y,Mean,ticks, cmap=plt.cm.jet)
> 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.show()
>
>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20110816/e3341547/attachment.html
More information about the SciPy-User
mailing list