rastersCount = 360 counter = 0 dek = 1 while counter < rastersCount: for i in range(counter,(counter+10)): print i counter += 10 dek += 1
Hi Luke, thanks. This code is much simpler and very much readable.
Thanks again for the help.
-Leo
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 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,
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
if arcpy.CheckExtension("Spatial") == "Available": arcpy.CheckOutExtension("Spatial")
Hi m.gasior,I tried to implement the python script that you suggested but I get an error when I ran it in in PythonWin and in ArcGIS 10. I have 366 rasters in TIFF format and I want to calculate the mean values every 10 days. I tried to modify the script but get an error. This is the modified script that I am trying to use. Could you help me revise my script?Thanks,-Leoimport arcpy, os, sys from arcpy import env from arcpy.sa import * env.workspace = "E:\PCP_TRMM_1998-2013" rasters = arcpy.ListRasters("*", "tif") rastersCount = len(rasters) counter = 0 weekDek = 1 while counter < rastersCount: dekadRasters = [] for i in range(counter,(counter+10)): dekRasters.append(rasters) outCellStatistics = CellStatistics(dekRasters, "MEAN", "NODATA") outRasterName = "dekad_"+ str(weekDek) outCellStatistics.save("E:\PCP_TRMM_1998-2013\Dekads" + outRasterName ) counter += 10 weekDek += 1
import arcpy, os, sys from arcpy import env from arcpy.sa import * env.workspace = "E:\PCP_TRMM_1998-2013" rasters = arcpy.ListRasters("*", "tif") rastersCount = len(rasters) counter = 0 weekDek = 1 while counter < rastersCount: dekadRasters = [] for i in range(counter,(counter+10)): dekRasters.append(rasters) outCellStatistics = CellStatistics(dekRasters, "MEAN", "NODATA") outRasterName = "dekad_"+ str(weekDek) outCellStatistics.save("E:\PCP_TRMM_1998-2013\Dekads" + outRasterName ) counter += 10 weekDek += 1
try: # Create list of rasters rasters = arcpy.ListRasters("*", "tif") rastersCount = len(rasters) counter = 0 weekNum = 1 while counter < rastersCount: #collect 7 consecutive rasters weekRasters = [] for i in range(counter,(counter+7)): weekRasters.append(rasters) # Execute CellStatistics (one can use list of rasters directly here) outCellStatistics = CellStatistics(weekRasters, "MEAN", "NODATA") # Save the output outRasterName = "week_"+ str(weekNum) outCellStatistics.save("C:/tmp/PrecipRasters/" + outRasterName ) counter += 7 weekNum += 1
# Import system modules import arcpy, os, sys from arcpy import env from arcpy.sa import * # Set workspace env.workspace = "D:/dailyrainfall/subset" # Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial") # Create list of rasters rasters = arcpy.ListRasters("*", "tif") # Create Weekly rainfall raster try: index = 70 for index in rasters: count = 0 while count <=7: # Set local variables inRaster0 = rasters.Next() count += 1 inRaster1 = rasters.Next() count += 1 inRaster2 = rasters.Next() count += 1 inRaster3 = rasters.Next() count += 1 inRaster4 = rasters.Next() count += 1 inRaster5 = rasters.Next() count += 1 inRaster6 = rasters.Next() count += 1 index -= 1 # Execute CellStatistics outCellStatistics = CellStatistics([inRaster0, inRaster1, inRaster2, inRaster3, inRaster4, inRaster5, inRaster6], "SUM", "NODATA") output = outCellStatistics.name # Save the output outCellStatistics.save("D:/dailyrainfall/subset/result/" + output + ".tif") except: # If an error occurred while running a tool, then print the messages. print arcpy.GetMessages()
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.