I have a folder with Day 1 Min, Day 1 Max and Day 2 Min, Day 2 Max...Day X Min, Day X Max rasters in it. I want to loop through this folder and use cell statistics on them to sum each day's min/max values (i.e. Day 1 Min + Day 1 Max).

I tried the following and got no results. Any pointers? How do I ensure that Cell Stats is summing the same days and not across multiple days?

MainFolder = r"C:\Users\uma.bhandaram\Desktop\2015\RasterCon2015"
for (path, dirs, files) in os.walk(MainFolder):
    for dir in dirs:
        env.workspace = os.path.join(path, dir)
        listRstrs = arcpy.ListRasters("Con.tif")
        for raster in listRstrs:
            rasterlist = []
            outCellStats = CellStatistics(rasterlist, "SUM", "DATA")
            outCellStats.save(os.path.join(path, dir) + "Check.tif")

If I understand your question and more importantly the folder structure you are looping over correctly. You need to remove your actual call to CellStatistics to outside the loop where you are appending your rasters in.

There are a few other issues with this code per documentation here the input list to arcpy.CellStatistics needs to be of Layer, Raster, or Constant type. You were providing a list of paths to the raster. You need to convert the paths being looped over from listRstrs to arcpy.Raster and then add them to rasterlist. Further, you should not provide 'Con.tif' to the function arcpy.ListRasters this will only match rasters with that name. Once env.workspace is defined call arcpy.ListRaster and that will return all of the rasters within env.workspace

The corrected code should look something like this:

    env.workspace = os.path.join(path, dir)
    listRstrs = arcpy.ListRasters('*')
    rasterlist = []
    for path_to_raster in listRstrs:
        raster_dataset = arcpy.Raster(path_to_raster)            
    outCellStats = CellStatistics(rasterlist, "SUM", "DATA")
    outCellStats.save(os.path.join(path, dir) + "Check.tif")

The way you were doing it was running CellStatistics each time a raster path, not arcpy.Raster as required, was added to rasterlist. So once for each raster path as well as for each raster path combination. However because you were providing the wildcard of 'Con.tif' to arcpy.ListRasters only one raster path, that of Con.tif was being looped over. By doing it how I have shown in the snippet above you are populating your list with the relevant arcpy.raster objects and then running CellStatistics on the fully populated list.

  • If I change the output path to save to: outCellStats.save(os.path.join(path, dir + "Check.tif"), at that line I get the following error: SyntaxError: invalid syntax (Script3.py, line 66) Failed to execute (Script3). – Uma Nov 30 '16 at 23:48
  • well I would go with what you have then. I ran a quick test and was receiving the error I noted. I will remove that from my answer. Hopefully you are able to get everything running with the other change I suggested :-) – GeoSharp Nov 30 '16 at 23:49
  • The folder structure is Daily Min, Daily Max, RasterCon inside the 2015 folder. The inputs to RasterCon are reclassified rasters from Daily Min and Daily Max. Within RasterCon, I want matching days to be summed using Cell Statistics. Moving CellStatistics to outside the loop didn't work; there is still no output. Any other ideas? – Uma Nov 30 '16 at 23:52
  • @Uma please see the updated answer. I changed the format for clarity. Let me know if that works out for you. – GeoSharp Dec 1 '16 at 3:26
  • 1
    You can simplify the code as rasterlist = [arcpy.Raster(r) for r in arcpy.ListRasters('*')] +1 for explanations – Paul Dec 1 '16 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.