Solved! Go to Solution.
rastersCount = 360 counter = 0 dek = 1 while counter < rastersCount: for i in range(counter,(counter+10)): print i counter += 10 dek += 1
Hi Clint,
It's been a while since my last inquiry and apologies if I have not answered your last reply. the code that you suggested is perfectly running. I tested it using 100 rasters.
However, I want to expand the code a little bit. I have a total of 366 rasters representing day of year. My objective is how to run the script that it will compute every 10 days from raster 1 to raster 360, but on the last step, it will only compute for the remaining 6 rasters. I know this can be done but I cannot imagine how to proceed.
Any help will be much appreciated. In the meantime I will try to research for some examples in the internet.
Thanks,
-Leo
import arcpy, os, sys
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")
arcpy.env.extent = "MAXOF"
# workspace
env.workspace = 'path/to/input/rasters'
rasters = arcpy.ListRasters()
out_ws = 'path/to/output/directory'
rastersCount = len(rasters)
counter = 0
dek = 1
# compute dekad rasters
while counter < rastersCount:
dekad = []
for i in range(counter,(counter+10)):
print i
dekad.append(rasters)
outCellStatistics = CellStatistics(dekad, "MEAN", "NODATA")
outRasterName = out_ws + 'tmax_dek_{:03d}.tif'.format (dek)
#outRasterName = "00" + str(dek) + ".tif"
outCellStatistics.save(outRasterName)
counter += 10
dek += 1
Another way of looping is to use the range function with a step of 7 and list slice notation:
rasterList = arcpy.ListRasters("*", "tif")
rasterCount = len(rasterList)
#loop from 0 to rasterCount incrementing by 7 each iteration
for i in range(0, rasterCount, 7):
# Slice 7 elements from the list
outCellStatistics = CellStatistics(rasterList, "MEAN", "NODATA")
outCellStatistics.save("C:/sapyexamples/output/week{}".format(i/7)) #e.g. week0 is first week. For week1 as first, use i/7+1
Hi Luke, thanks. This code is much simpler and very much readable.
Thanks again for the help.
-Leo