1

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

These are the folders

Data in one of the folder, (i.e. Aqua_D)

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 18, 2020 at 5:22
6
  • 1
    Can you verify that you really have I:\Test_Delete\AQUA_N\clip1_A2002185Aqua_N.tif in Aqua_N folder? Commented Jul 18, 2020 at 9:23
  • Try replacing [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) Commented Jul 18, 2020 at 10:08
  • @BERA Sir, After editing my code I'm getting another Runtime Error Commented Jul 19, 2020 at 3:35
  • @fatih_dur Sir, there is no such kind of file that has been created. Commented Jul 19, 2020 at 4:05
  • That’s might be the problem. CellStatistics tool expects existing rasters to do the MEAN calculation. Commented Jul 19, 2020 at 4:47

1 Answer 1

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
answered Jul 20, 2020 at 3:53
1
  • @MichealStimson Thank You so much, sir, for this explanation, I'll try this and let you know... Commented Jul 22, 2020 at 13:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.