Hi, I am very new to python and am coming across an issue that I haven't been able to work out on my own.
I have 9 rasters that I need to sum. Nothing fancy, just a simple sum. I have this code that I used in another script (found on some blog somewhere) and it worked without any issue whatsoever:
rasters = arcpy.ListRasters('cf_*')
i = 0
for ras in rasters:
if i == 0:
sumras = arcpy.Raster(ras)
i += 1
else:
sumras = sumras + ras
i += 1
sumras.save(outRasterCFS)
However, in my current script it fails with:
Traceback (most recent call last):
File "<module1>", line 32, in <module>
File "<module1>", line 25, in Cut
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 4329, in Plus
in_raster_or_constant2)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 4326, in Wrapper
["Plus", in_raster_or_constant1, in_raster_or_constant2])
RuntimeError: ERROR 999998: Unexpected Error.
When researching how to resolve this, I found another post somewhere that suggested cellstatistics might be better, so:
sumras = CellStatistics(rasters,"SUM","DATA")
sumras.save(outRasterCFS)
But it fails with:
Traceback (most recent call last):
File "<module1>", line 32, in <module>
File "<module1>", line 16, in Cut
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 3149, in CellStatistics
ignore_nodata)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 3145, in Wrapper
[function] + Utils.flattenLists(in_rasters_or_constants))
RuntimeError: ERROR 999998: Unexpected Error.
Usually, when I run into problems that aren't very clear, I run it manually from ArcMap and can usually figure out what is going wrong in my python, but when I run CellStatistics from ArcMap, it fails as well:
ERROR 010092: Invalid output extent.
I really have no idea where to go from here. I can't find anything online that points me in a helpful direction...
Any thoughts or suggestions? I would greatly appreciate it!
Here is the entire script:
#Set environment
import arcpy
from arcpy import env
from arcpy.sa import *
wrkpath = r'E:\GIS_Data\GEO5419\MyWork'
arcpy.env.workspace = wrkpath
arcpy.env.overwriteOutput = True
def Cut():
arcpy.CheckOutExtension("Spatial")
topRaster = 'Site_DEM'
outRasterCFS = wrkpath+'\CFSum'
rasters = arcpy.ListRasters('cf_*')
sumras = CellStatistics(rasters,"SUM","DATA")
sumras.save(outRasterCFS)
## i = 0
## for ras in rasters:
## if i == 0:
## sumras = arcpy.Raster(ras)
## i += 1
## else:
## sumras = sumras + ras
## i += 1
## sumras.save(outRasterCFS)
arcpy.CheckInExtension("Spatial")
if __name__ == '__main__':
Cut()