# [Tutor] numpy.mean across multiple netcdf files

questions anon questions.anon at gmail.com
Thu Aug 11 03:57:02 CEST 2011

```I have many ncfiles each containing one month of hourly temperature data.
I have worked out how to loop through a number of ncfiles and calculate the
mean for each file at a particular time and even plot this and output as a
*.png.
What I am unsure of is how to calculate the mean at a particular time for
all files.
Do I somehow output the mean for each folder and then calculate the mean of
all the means? Or is there some way to loop through and calculate it
directly?
Below is the code I have working that calculates the mean for each file. Any
help will be greatly appreciated!!

from netCDF4 import Dataset
import numpy as N

MainFolder=r"D:/temp_samples/"
print MainFolder
for (path, dirs, files) in os.walk(MainFolder):
for dir in dirs:
print dir
path=path+'/'

for ncfile in files:
if ncfile[-3:]=='.nc':
print "dealing with ncfiles:", ncfile
ncfile=os.path.join(path,ncfile)
ncfile=Dataset(ncfile, 'r+', 'NETCDF4')
TSFC=ncfile.variables['T_SFC'][4::24,:,:] #this chooses a
particular time for each day for the whole month
LAT=ncfile.variables['latitude'][:]
LON=ncfile.variables['longitude'][:]
TIME=ncfile.variables['time'][:]
fillvalue=ncfile.variables['T_SFC']._FillValue
ncfile.close()

#calculate summary stats
TSFCmean=N.mean(TSFC, axis=0)
print "The TSFCmean array is:", TSFCmean

# there is a lot more code that follows this to plot the mean for each
ncfile but I have cut that out as I think it is irrelevant to my question
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110811/c3d61683/attachment.html>
```