I'm using these codes to generate Cell-Statistics, I have data in four different folders but right now I'm using two folders just for the testing but I couldn't get the result. I'm a beginner level of Python user.
Can you help me out with this code?
I'm getting this kind of error again and again:
import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension ("Spatial")
u'CheckedOut'
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r'I:\Test_Delete\AQUA_D'
inws = arcpy.env.workspace
inws1 = r'I:\Test_Delete\AQUA_N'
outws = r'I:\Test_Delete\OUTPUT'
rasters = arcpy.ListRasters()
for r in rasters:
prefix = os.path.splitext(r)[0]
suffix = os.path.splitext(r)[1]
prefix1 = prefix.rstrip("Aqua_D")
raster2 = os.path.join(inws1, prefix1 + "Aqua_N" + suffix)
print raster2
outraster = os.path.join(outws, prefix1 + "mean" + suffix)
print outraster
outCellStatistics = CellStatistics([arcpy.sa.Raster(r), arcpy.sa.Raster(raster2)], "MEAN")
outCellStatistics.save(outraster)
I:\Test_Delete\AQUA_N\clip1_A2002185Aqua_N.tif
I:\Test_Delete\OUTPUT\clip1_A2002185mean.tif
Runtime error
Traceback (most recent call last):
File "<string>", line 19, in <module>
RuntimeError: ERROR 000732: Input Raster: Dataset I:\Test_Delete\AQUA_N\clip1_A2002185Aqua_N.tif does not exist or is not supported
1 Answer 1
prefix1 = prefix.rstrip("Aqua_D")
is just asking for a failure, python is a case sensitive language; it seems to me you want to swap "Aqua_D"
with "Aqua_N"
so perhaps instead of rstrip
and concatenation try raster2 = os.path.join(inws1, r.lower().replace("aqua_d","aqua_n"))
then check that both rasters are valid with arcpy.Exists, case isn't important to the file system so using the lower case of the input string won't hurt.
Perhaps the biggest problem is that your file paths don't match your screen shots, the paths you have printed are:
I:\Test_Delete\AQUA_N\clip1_A2002185Aqua_N.tif
I:\Test_Delete\OUTPUT\clip1_A2002185mean.tif
But your screen shot of the Aqua_D folder shows file names like I:\Test_Delete\AQUA_N\clip1_A2002185.tif
, I can only assume that the files are similarly named in the Aqua_N folder.. which makes all the path/string modifications redundant:
import arcpy
from arcpy.sa import *
# just a personal thing but I prefer paths set before environs
# when your code gets bigger and more complex it's easy enough
# to find them right at the top and modify the values
inws = r'I:\Test_Delete\AQUA_D' # Please consider changing
inws1 = r'I:\Test_Delete\AQUA_N' # the case of your variable names
outws = r'I:\Test_Delete\OUTPUT' # to sentence or camel case
if arcpy.CheckExtension('spatial') != 'Available':
arcpy.AddError('No spatial analyst extension available')
sys.exit(-1) # quick bail out with a failure return code if spatial analyst license not available
# now set your environs using the hard paths
arcpy.CheckOutExtension ("Spatial")
arcpy.env.overwriteOutput = True
arcpy.env.workspace = inws
#rasters = arcpy.ListRasters() # redundant line
for r in arcpy.ListRasters():
# set both prefix and suffix at the same time as splitext always returns a tuple of two elements
prefix, suffix = os.path.splitext(r)
raster2 = os.path.join(inws1, r) # assuming the name of the tiff in Aqua_D is the same as in Aqua_N
print raster2
outraster = os.path.join(outws, prefix + "mean" + suffix)
print outraster
if arcpy.Exists(r) and arcpy.Exists(raster2):
outCellStatistics = CellStatistics([r, raster2], "MEAN")
outCellStatistics.save(outraster)
else:
# no failure continuation with a warning if the two inputs don't exist
arcpy.AddWarning('I could not find both {} and {} to process'.format(r,raster2))
arcpy.CheckInExtension('spatial') # be nice, give up the extension when you're done
-
@MichealStimson Thank You so much, sir, for this explanation, I'll try this and let you know...Raza– Raza2020年07月22日 13:56:17 +00:00Commented Jul 22, 2020 at 13:56
Explore related questions
See similar questions with these tags.
I:\Test_Delete\AQUA_N\clip1_A2002185Aqua_N.tif
in Aqua_N folder?[arcpy.sa.Raster(r), arcpy.sa.Raster(raster2)]
with[r, raster2]
in CellStatistics. Accouring to the help section inputs should be raster layers (not raster objects)