Error: 000732 executing Con in loop

557
1
09-03-2019 07:46 AM
ForrestFollett1
New Contributor II

I'm working on automating a workflow and I'm encountering an error inside a for loop.  On the second iteration, Con fails to access the target raster, giving the 000732 error.  I've created the following reprex to illustrate my workflow and the error I'm experiencing:

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

path = r"C:\GIS\CMP\test"
outName = 'test.gdb'
riskMax = {}

arcpy.env.workspace = path
arcpy.env.addOutputsToMap = 0
print("Creating test.tif")
test = arcpy.CreateRandomRaster_management(path, 'test.tif', 'NORMAL 3.0', '0 0 500 500', 50)

print("Creating file geodatabase")
arcpy.CreateFileGDB_management(path, outName)

gdb = os.path.join(path, outName)
arcpy.env.workspace = gdb

for i in range(10):
 name = 'rand' + str(i)
 print("Creating " + name)
 arcpy.CreateRandomRaster_management(gdb, name, 'NORMAL 3.0', '0 0 500 500', 50)

try:
 for ras in arcpy.ListRasters():
 print('Running Con')
 riskProd = Con(Raster(ras) > 0, test)
 print('Storing max value')
 riskMax[ras] = riskProd.maximum
 outName = os.path.join(gdb, "{0}_{1}".format('out', ras))
 print("Saving " + outName)
 riskProd.save(outName)
except Exception:
 e = sys.exc_info()[1]
 print(e.args[0])
 arcpy.AddError(e.args[0])

Running this script in the python window or (after adding some code to handle license check-in/out) externally, you should see the error 000732 when trying to execute Con in the second iteration of that for loop.  It is odd to me that it makes it through the first iteration successfully, but the second time around is a problem, given that the inputs are all identical in regard to parameters.

I've identified two workarounds that are undesirable for my implementation of this script, but which may inform diagnosing the above error.

First, setting arcpy.env.addOutputsToMap = 1 at the beginning of the script causes the whole script to execute successfully (when run in the ArcPro python window).  So for some reason, drawing the raster object allows the Con tool to work properly.  Related to this, I noticed that in several, but not all cases, when I was running this code line by line, if I ran the BuildPyramids tool on some of the rasters that were to be iterated over, those that had had pyramids built would not cause an error with the Con tool, and it would iterate until it hit a raster that I had not built pyramids for.

The other workaround I've noticed is that after the Con() call, if I save the entire riskProd raster object to my riskMax dictionary instead of just the .maximum property, this also prevents the 000732 error.  This one is even more confusing to me as it happens after the Con() call that should be giving the error.  I could just use this method and access the .maximum property from the riskMax dict when I need it later, but this significantly inflates the memory usage, and the real rasters I'm using are much larger than the ones in this example code.

Does anyone have any insight at what's triggering this error?  Are these workarounds somehow altering the Raster object in a way that prevents the error, and is there a way to do that intentionally and efficiently?

0 Kudos
1 Reply
KateDouthat1
New Contributor

Did you ever find a solution to this? I am encountering a similar issue in which my loop succeeds the first iteration but fails the second iteration.

0 Kudos