CELL STATISTICS with LIST RASTERS

1880
7
Jump to solution
04-23-2018 03:07 AM
anTonialcaraz
Occasional Contributor II

Hi,

I'm trying to run Mean annual cell statistics for groups of monthly rasters.

The output I'm getting is the same as the last raster in the list ("camp_t_srf_sep" in this example) so i assume the script is not actually iterating through all rasters.

Any help will be greatly appreciate it.

# Example of list of rasters:
 # camp_t_srf_apr
 # camp_t_srf_aug
 # camp_t_srf_dec
 # camp_t_srf_feb
 # camp_t_srf_jan
 # camp_t_srf_jul
 # camp_t_srf_jun
 # camp_t_srf_mar
 # camp_t_srf_may
 # camp_t_srf_nov
 # camp_t_srf_oct
 # camp_t_srf_sep

import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
env.overwriteOutput = True
env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0)

env.workspace = arcpy.GetParameterAsText(0)
StageAge = arcpy.GetParameterAsText(1)


rasterlist = arcpy.ListRasters(StageAge + "_*")
for raster in rasterlist:
    output_mean = CellStatistics(raster, "MEAN", "DATA")
    output_mean.save(env.workspace + "\\" + raster[:11] + "ann")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

You can use your for loop to build the list... just don't have cellstatistics in the loop

View solution in original post

7 Replies
anTonialcaraz
Occasional Contributor II

just to add that I'm using 10.2.1 at the moment.

many thanks

0 Kudos
DanPatterson_Retired
MVP Emeritus

Throw a print statement in... just to make sure you are getting the correct names, it will silently do nothing otherwise

And also.. Cell statistics expects a list/stack of rasters, not one at a time

http://desktop.arcgis.com/en/arcmap/latest/tools/spatial-analyst-toolbox/cell-statistics.htm

anTonialcaraz
Occasional Contributor II

Thank you Dan.

yes, the print statement gives me the list of rasters I want to calculate the mean from.

I thought I was giving the script the list of rasters with the variable rasterlist. I don't quite understand why it only processes the last raster in the list.

0 Kudos
DanPatterson_Retired
MVP Emeritus

because you are cycling through each raster one at a time.

for raster in rasterlist:

give it the whole list... examine the syntax in the code sample in the link I provided

0 Kudos
anTonialcaraz
Occasional Contributor II

I know it works like as below. Just thought there would be a more elegant and economic way to put it using ListRasters

rasterlist5 = [StageAge + "_t_srf_apr", StageAge + "_t_srf_aug", StageAge + "_t_srf_dec", StageAge + "_t_srf_feb", StageAge + "_t_srf_jan", StageAge + "_t_srf_jul", StageAge + "_t_srf_jun", \
 StageAge + "_t_srf_mar", StageAge + "_t_srf_may", StageAge + "_t_srf_nov", StageAge + "_t_srf_oct", StageAge + "_t_srf_sep"]

output_mean = CellStatistics(rasterlist5, "MEAN", "DATA")

output_mean.save(OUTWorkspace + "\\" + raster[:11] + "ann")‍‍‍‍
0 Kudos
DanPatterson_Retired
MVP Emeritus

You can use your for loop to build the list... just don't have cellstatistics in the loop

anTonialcaraz
Occasional Contributor II

I see what you mean. Thanks so much Dan.

rasterlist5 = arcpy.ListRasters(StageAge + "_*")

for raster in rasterlist5:
    print raster

output_mean = CellStatistics(rasterlist5, "MEAN", "DATA")
output_mean.save(env.workspace + "\\" + raster[:11] + "ann")‍‍‍‍‍
0 Kudos