Finding an Average Rainfall Data

977
11
05-02-2012 11:46 AM
RPatarasuk
New Contributor II
Hi All,

I have about 350 files of PRISM rainfall data to process. I have e.g. precip from 1970 - 2010. and each year has 13 files.

ppt_1970.01.tif
ppt_1970.02.tif
ppt_1970.03.tif
ppt_1970.04.tif
ppt_1970.05.tif
ppt_1970.06.tif
ppt_1970.07.tif
ppt_1970.08.tif
ppt_1970.09.tif
ppt_1970.10.tif
ppt_1970.11.tif
ppt_1970.12.tif
ppt_1970.14.tif
.
.
.
ppt_2010.11.tif
ppt_2010.12.tif
ppt_2010.14.tif

I try to write a python code to calculate a long term average. Here is the code that I have so far. I have only tried to play around with 5-10 files and the program only returns values for the last file (i.e. it doesn't add up the values of previous file and find the average). Also, I would like to exclude "*.14" from the calculation.

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Input data source
arcpy.env.workspace = "S:/Work/Risa/Trial & Error/input"
arcpy.env.overwriteOutput = True

# Set output folder
OutputFolder = "S:/Work/Risa/Trial & Error/output/"

# Loop through a list of files in the workspace
rasterFiles = arcpy.ListRasters()

# Local variables:
for filename in rasterFiles:
    print("Processing: " + filename)
    inRaster = arcpy.env.workspace + "/" + filename
    outRaster = OutputFolder + "/" + "AvgPrecip.tif"
   
    #***need to process from all of the files except the ones that have "*.14"

    # Process: Cell Statistics
    arcpy.gp.CellStatistics_sa(inRaster, outRaster, "MEAN", "DATA")

   
                                  
print "***DONE!!!"


Any help would be very appreciated. Thank you very much.

Risa
Tags (2)
0 Kudos
11 Replies
DarrenWiens2
MVP Honored Contributor
Haha, now that I look at it "*.14.tif" wouldn't work even if it used wildcards - there's an extra '.'

So, touché.
0 Kudos
RPatarasuk
New Contributor II
Dear philmorefield & dkwiens,

It works now. Thank you very much for your help.

if not filename.endswith("14.tif") -- this one works

if not filename.endswith("*.14.tif") -- this one, Phil is right, keeps looking for the "*" so the program keeps running non stop.
0 Kudos