Thanks...I have adopted that and as you said it is a lot neater! Though I
need to keep the pixel count for a weighting in another piece of code so
have amended your logic slightly.

#!/usr/bin/env python


import sys, os, glob
import numpy as np

def averageEightDays(filenamesList, numrows, numcols, year):
        doy = 122
        nodatavalue =  -999.0
        
        for fchunk in filenamesList:
                # fill with nodata values, in case there are less than 8 days
                data8days = np.zeros((8, numrows, numcols), dtype=np.float32) * 
-999.0 
                pixCount = np.zeros((numrows, numcols), dtype=np.int)
                avg8days = np.zeros((numrows, numcols), dtype=np.float32)
                for day in xrange(len(fchunk)):
                        f = fchunk[day]
                        data8days[day] = np.fromfile(f, 
dtype=np.float32).reshape(numrows,
numcols)
                        avg8days = np.where(data8days[day] > nodatavalue,
avg8days+data8days[day], avg8days)
                        pixCount = np.where(data8days[day] > nodatavalue, 
pixCount+1, pixCount)
                
                avg8days = np.where(pixCount > 0, avg8days / 
np.float32(pixCount), -999.0)
                doy += 8
                print year,':',doy
                
                outfile = "lst_8day1030am_" + str(year) + str(doy) + ".gra"
                write_outputs(outfile, avg8days)
                outfile = "pixelcount_8day1030am_" + str(year) + str(doy) + 
".gra"
                write_outputs(outfile, pixCount)

def write_outputs(outfile, data):
        opath = "/users/eow/mgdk/research/HOFF_plots/LST/tmp"
        
        try:
                of = open(os.path.join(opath, outfile), 'wb')
        except IOError:
                print "Cannot open outfile for write", outfile
                sys.exit(1)
        
        # empty stuff
        data.tofile(of)
        of.close()
        
        
if __name__ == "__main__":

        numrows = 332
        numcols = 667
        path = "/users/eow/mgdk/research/HOFF_plots/LST/gridded_03/"
        
        files = 'lst_scr_2006*.gra'
        filenames = glob.glob(os.path.join(path, files))
        filenames.sort()
        filenamesList = [filenames[n:n+8] for n in xrange(0, len(filenames), 8)]
        averageEightDays(filenamesList, numrows, numcols, year=2006)
        
        files = 'lst_scr_2007*.gra'
        filenames = glob.glob(os.path.join(path, files))
        filenames.sort()
        filenamesList = [filenames[n:n+8] for n in xrange(0, len(filenames), 8)]
        averageEightDays(filenamesList, numrows, numcols, year=2007)

-- 
View this message in context: 
http://old.nabble.com/Help-making-better-use-of-numpy-array-functions-tp26503657p26528176.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to