AnsweredAssumed Answered

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.?

 

 

Thanks in advance,

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
fmaskrasters = arcpy.ListRasters()
fmaskrasters.sort()


if year % 4 > 0 and month == 9:
     fmaskrasters1 = fmaskrasters[243:249]
     fmaskrasters2 = fmaskrasters[249:255]
     fmaskrasters3 = fmaskrasters[255:261]
     fmaskrasters4 = fmaskrasters[261:267]
     fmaskrasters5 = fmaskrasters[267:273]
elif year % 4 == 0 and month == 9:
     fmaskrasters1 = fmaskrasters[244:250]
     fmaskrasters2 = fmaskrasters[250:256]
     fmaskrasters3 = fmaskrasters[256:262]
     fmaskrasters4 = fmaskrasters[262:268]
     fmaskrasters5 = fmaskrasters[268:274]
elif year % 4 > 0 and month == 10:
     fmaskrasters1 = fmaskrasters[273:279]
     fmaskrasters2 = fmaskrasters[279:285]
     fmaskrasters3 = fmaskrasters[285:291]
     fmaskrasters4 = fmaskrasters[291:297]
     fmaskrasters5 = fmaskrasters[297:304]
elif year % 4 == 0 and month == 10:
     fmaskrasters1 = fmaskrasters[274:280]
     fmaskrasters2 = fmaskrasters[280:286]
     fmaskrasters3 = fmaskrasters[286:292]
     fmaskrasters4 = fmaskrasters[292:298]
     fmaskrasters5 = fmaskrasters[298:305]
elif year % 4 > 0 and month == 11:
     fmaskrasters1 = fmaskrasters[304:310]
     fmaskrasters2 = fmaskrasters[310:316]
     fmaskrasters3 = fmaskrasters[316:322]
     fmaskrasters4 = fmaskrasters[322:328]
     fmaskrasters5 = fmaskrasters[328:334]
elif year % 4 == 0 and month == 11:
     fmaskrasters1 = fmaskrasters[305:311]
     fmaskrasters2 = fmaskrasters[311:317]
     fmaskrasters3 = fmaskrasters[317:323]
     fmaskrasters4 = fmaskrasters[323:329]
     fmaskrasters5 = fmaskrasters[329:335]
else:
     print("Not configured in the script!")


number = len(fmaskrasters)

# 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")

Outcomes