[SciPy-User] numpy array append
questions anon
questions.anon@gmail....
Tue Aug 16 19:42:59 CDT 2011
Thanks Tim, that worked although I did run into a problem with different the
sizes of each file.
Each netcdf file contains a month of hourly data and some of those months
are 31 days and some are 28 or 30. Is there a way to get this to work?
here is the error I receive:
*Traceback (most recent call last):*
* File "d:\documents and settings\SLBurns\Work\My
Dropbox\Python_code\calculate_the_mean_across_multiple_netcdf_files_in_multiple_folders.py",
line 39, in <module>*
* Mean=N.mean(big_array, axis=0)*
* File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line
2374, in mean*
* return mean(axis, dtype, out)*
*ValueError: operands could not be broadcast together with shapes
(31,106,193) (28,106,193)*
from netCDF4 import Dataset
import numpy as N
import os
MainFolder=r"E:/temp_samples/"
all_TSFC=[]
for (path, dirs, files) in os.walk(MainFolder):
for dir in dirs:
print dir
path=path+'/'
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,:,:]
all_TSFC.append(TSFC)
big_array=N.array(all_TSFC)
Mean=N.mean(big_array, axis=0)
print "the mean for three months at 3pm is", Mean
On Wed, Aug 17, 2011 at 9:13 AM, Tim Supinie <tsupinie@gmail.com> wrote:
> 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
>>
>>
>
> _______________________________________________
> 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/20110817/df6a8bee/attachment-0001.html
More information about the SciPy-User
mailing list