[SciPy-User] numpy array append

questions anon questions.anon at gmail.com
Tue Aug 16 18:50:35 EDT 2011


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()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110817/e39c3aa0/attachment.html>


More information about the SciPy-User mailing list