1

Heyo. I'm trying to get the rasters from a list (about 32 rasters) and then sum them up with CellStatistics in arcpy. I'm using Python 2.7.8 and ArcGIS 10.3.1

I got it to work the first time around but then I added a new process and then the CellStatistics failed for some reason.

The code I have is:

import arcpy
import arcpy.sa
from arcpy.sa import *
from arcpy import env
import os

#Set Environment
arcpy.CheckOutExtension("Spatial")
arcpy.OverwriteOutput = True
env.extent = "MINOF"
env.cellSize = "MINOF"
Coordsystem = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]"

#Enter Input Data
parentdir = r'Z:\Analysis\Dave\WindRiver\P37R30\1999'
fmaskdir = parentdir + "\\" + "SavedOutputs" + "\\" + "Fmask" + "\\" + "Reclassified"
output = r'G:\trash\cloudsum\1999' #output folder
scratch = r'G:\trash\cloudsum\1999\scratch' #scratchfolder
year = 1999

fmaskfolder = fmaskdir
env.workspace = fmaskdir
fmaskrasters = arcpy.ListRasters()
fmaskrasters.sort()
print fmaskrasters

number = len(fmaskrasters)
print str(number)

env.workspace = scratch
outras = scratch + "\\" + "sum" + str(year) + ".tif"
outSUM = CellStatistics([fmaskrasters],"SUM") #the script fails here!
arcpy.CopyRaster_management(outSUM,outras,"","","","","","32_BIT_FLOAT")

factor = float(number)
finras = output + "\\" + "SUM" + str(year) + ".tif"
percentage = outSUM / factor
arcpy.CopyRaster_management(percentage,finras,"","","","","","32_BIT_FLOAT")
print finras

The error I am getting says:

[u'1999003_Fmask_reclass.tif', u'1999019_Fmask_reclass.tif', u'1999035_Fmask_reclass.tif', u'1999051_Fmask_reclass.tif', u'1999083_Fmask_reclass.tif', u'1999131_Fmask_reclass.tif', u'1999147_Fmask_reclass.tif', u'1999163_Fmask_reclass.tif', u'1999179_Fmask_reclass.tif', u'1999187_Fmask_reclass.tif', u'1999195_Fmask_reclass.tif', u'1999203_Fmask_reclass.tif', u'1999211_Fmask_reclass.tif', u'1999219_Fmask_reclass.tif', u'1999227_Fmask_reclass.tif', u'1999235_Fmask_reclass.tif', u'1999243_Fmask_reclass.tif', u'1999251_Fmask_reclass.tif', u'1999259_Fmask_reclass.tif', u'1999267_Fmask_reclass.tif', u'1999275_Fmask_reclass.tif', u'1999283_Fmask_reclass.tif', u'1999291_Fmask_reclass.tif', u'1999299_Fmask_reclass.tif', u'1999307_Fmask_reclass.tif', u'1999315_Fmask_reclass.tif', u'1999323_Fmask_reclass.tif', u'1999331_Fmask_reclass.tif', u'1999339_Fmask_reclass.tif', u'1999347_Fmask_reclass.tif', u'1999355_Fmask_reclass.tif', u'1999363_Fmask_reclass.tif'] 32

Traceback (most recent call last):

File "C:\Users\mschauer\Desktop\ETScripts\bscripts\clouds_sum.py", line 35, in <module>

outSUM = CellStatistics([fmaskrasters],"SUM")

File "C:\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Functions.py", line 2967, in CellStatistics
ignore_nodata)

File "C:\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)

File "C:\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Functions.py", line 2963, in Wrapper
[function] + Utils.flattenLists(in_rasters_or_constants))

RuntimeError: ERROR 000732: Input Raster: Dataset 1999003_Fmask_reclass.tif does not exist or is not supported

What am I doing wrong?

1

fmaskrasters holds the names of your rasters, which are in fmaskdir, but then you change the working directory to scratch. You want fmaskrasters to hold the full paths to your rasters. The easiest way is to change your line:

fmaskrasters = arcpy.ListRasters()

to:

fmaskrasters = [os.path.join(fmaskdir, i) for i in arcpy.ListRasters()]
  • Got it and it worked! Thanks! that must be why it worked the first time, I had the workspace as fmaskdir at first and it created a temp file there which I didn't want, so I changed the workspace to scratch and it failed. but your trick worked! – MattS Dec 7 '15 at 16:47
  • @MattS, In that case, you could just set the scratch workspace at the top of your script, and then leave the regular workspace the same throughout: arcpy.env.scratchWorkspace = scratch – Tom Dec 7 '15 at 16:48

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.