Loop over rasters to calculate mean values for weekly

Question asked by dljayasekera on Aug 10, 2017
Latest reply on Aug 13, 2017 by xander_bakker

Hi,

I have multiple rasters named as follows for each year. But, I need to calculate mean for weekly ONLY for 09, 10 and 11 months based on the following week definition.

Week 1: Day 01, 02, 03, 04, 05, 06

Week 2: Day 07, 08, 09, 10, 11, 12

Week 3: Day 13, 14, 15, 16, 17, 18

Week 4: Day 19, 20, 21, 22, 23, 24

Week 5: Day 25, 26, 27, 28, 29, 30, 31

Can anybody suggest me a way to process months 9, 10 and 11 in one go for the following script.?

import arcpy
import arcpy.sa
from arcpy.sa import *
from arcpy import env
import os

arcpy.CheckOutExtension("Spatial")
arcpy.OverwriteOutput = True

arcpy.env.workspace = r'H:\Test_ONLY'
scratch = r'H:\Test_ONLY\scratch'

year = 1998
month = 01

if year % 4 > 0 and month == 9:
elif year % 4 == 0 and month == 9:
elif year % 4 > 0 and month == 10:
elif year % 4 == 0 and month == 10:
elif year % 4 > 0 and month == 11:
elif year % 4 == 0 and month == 11:
else:
print("Not configured in the script!")

# Week 1
finras1 = scratch + "\\" + "Week_1_Avg" + str(year) + "_" + str(month) + ".tif"
calc1 = arcpy.sa.CellStatistics([fmaskrasters1], statistics_type = "MEAN")
arcpy.CopyRaster_management(calc1,finras1,"","","","","","32_BIT_FLOAT")

# Week 2
finras2 = scratch + "\\" + "Week_2_Avg" + str(year) + "_" + str(month) + ".tif"
calc2 = arcpy.sa.CellStatistics([fmaskrasters2], statistics_type = "MEAN")
arcpy.CopyRaster_management(calc2,finras2,"","","","","","32_BIT_FLOAT")

# Week 3
finras3 = scratch + "\\" + "Week_3_Avg" + str(year) + "_" + str(month) + ".tif"
calc3 = arcpy.sa.CellStatistics([fmaskrasters3], statistics_type = "MEAN")
arcpy.CopyRaster_management(calc3,finras3,"","","","","","32_BIT_FLOAT")

# Week 4
finras4 = scratch + "\\" + "Week_4_Avg" + str(year) + "_" + str(month) + ".tif"
calc4 = arcpy.sa.CellStatistics([fmaskrasters4], statistics_type = "MEAN")
arcpy.CopyRaster_management(calc4,finras4,"","","","","","32_BIT_FLOAT")

# Week 5
finras5 = scratch + "\\" + "Week_5_Avg" + str(year) + "_" + str(month) + ".tif"
calc5 = arcpy.sa.CellStatistics([fmaskrasters5], statistics_type = "MEAN")
arcpy.CopyRaster_management(calc5,finras5,"","","","","","32_BIT_FLOAT")