Personal tools
You are here: Home CDAT Tutorials Getting Started with CDAT Scripting Loops Averaging over multiple files
Document Actions

Averaging over multiple files

by zopeadmin last modified 2008-01-09 14:35

In this example we will loop over multiple files print compute the average over all these files

import cdms, MV
import sys

# Creates string for path to data
pth=sys.prefix+'/sample_data/'

# Creates list containg path to files
files = [ pth+'u_2000.nc',
pth+'u_2001.nc',
pth+'u_2002.nc',
        ]

# Now LOOP through the files
for file in files:
# Open a file
f=cdms.open(file)

# Get the 'u' variable
    u=f('u')

    if file == files[0]: # First file
    avg=MV.sum(u,0) # Compute the time sum
        n=u.shape[0] # Number of month in this file
   
else: # Another file
        avg=avg+MV.sum(u,0) # Compute the time sum and add it to our final variable
n=n+u.shape[0]
f.close()


# Now divide by the total number of  times
avg=avg/n

Powered by Plone