Hi,
I am trying to calculate the average using multiple rasters using years from 1981 to 2016. For example, average for week 1 for the month of 9 from years 1981 to 2016 is as follows:
Normal_Week_1_1981_2016_9.tif =average [Week_1_Sum1981_9.tif
.
.
Week_1_Sum2016_9.tif]
My following code outputs all the tif files at the "out" path but at the end it gives me the error below. What I am doing wrong here.? Just noticed the output raster values are also wrong.
Any help is appreciated.
Thanks in advance.
arcpy.CheckOutExtension("Spatial")
arcpy.OverwriteOutput = True
years = [1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016]
out = r'H:\PRISM_800m_weekly_sum\Normal_out'
path = r'H:\PRISM_800m_weekly_sum_Normal'
lstFiles = []
for root, dirs, files in os.walk(path):
arcpy.env.workspace = root
files = arcpy.ListRasters("*", "TIF")
for file in files:
(filename, extension) = os.path.splitext(file)
if(extension == ".tif"):
lstFiles.append(os.path.join(root, file))
print (lstFiles)
new_list1=[]
new_list2=[]
new_list3=[]
new_list4=[]
new_list5=[]
months=[9,10,11]
for month in months:
for file in lstFiles:
if fnmatch.fnmatch(file, "H:\\PRISM_800m_weekly_sum_Normal" + "*" + "\\" + "Week_1" + "_*_" + str(month) + ".tif"):
new_list1.append(file)
new_list1.sort()
print (new_list1)
env.workspace = out
finras1 = out + "\\" + "Normal_Week_1" + "_1981_2016" + "_" + str(month) + ".tif"
calc1 = arcpy.sa.CellStatistics(new_list1, statistics_type = "MEAN")
arcpy.CopyRaster_management(calc1,finras1,"","","","","","32_BIT_FLOAT")
for file in lstFiles:
if fnmatch.fnmatch(file, "H:\\PRISM_800m_weekly_sum_Normal" + "*" + "\\" + "Week_2" + "_*_" + str(month) + ".tif"):
new_list2.append(file)
new_list2.sort()
print (new_list2)
env.workspace = out
finras2 = out + "\\" + "Normal_Week_2" + "_1981_2016" + "_" + str(month) + ".tif"
calc2 = arcpy.sa.CellStatistics(new_list2, statistics_type = "MEAN")
arcpy.CopyRaster_management(calc2,finras2,"","","","","","32_BIT_FLOAT")
for file in lstFiles:
if fnmatch.fnmatch(file, "H:\\PRISM_800m_weekly_sum_Normal" + "*" + "\\" + "Week_3" + "_*_" + str(month) + ".tif"):
new_list3.append(file)
new_list3.sort()
print (new_list3)
env.workspace = out
finras3 = out + "\\" + "Normal_Week_3" + "_1981_2016" + "_" + str(month) + ".tif"
calc3 = arcpy.sa.CellStatistics(new_list3, statistics_type = "MEAN")
arcpy.CopyRaster_management(calc3,finras3,"","","","","","32_BIT_FLOAT")
for file in lstFiles:
if fnmatch.fnmatch(file, "H:\\PRISM_800m_weekly_sum_Normal" + "*" + "\\" + "Week_4" + "_*_" + str(month) + ".tif"):
new_list4.append(file)
new_list4.sort()
print (new_list4)
env.workspace = out
finras4 = out + "\\" + "Normal_Week_4" + "_1981_2016" + "_" + str(month) + ".tif"
calc4 = arcpy.sa.CellStatistics(new_list4, statistics_type = "MEAN")
arcpy.CopyRaster_management(calc4,finras4,"","","","","","32_BIT_FLOAT")
for file in lstFiles:
if fnmatch.fnmatch(file, "H:\\PRISM_800m_weekly_sum_Normal" + "*" + "\\" + "Week_5" + "_*_" + str(month) + ".tif"):
new_list5.append(file)
new_list5.sort()
print (new_list5)
env.workspace = out
finras5 = out + "\\" + "Normal_Week_5" + "_1981_2016" + "_" + str(month) + ".tif"
calc5 = arcpy.sa.CellStatistics(new_list5, statistics_type = "MEAN")
arcpy.CopyRaster_management(calc5,finras5,"","","","","","32_BIT_FLOAT")