Last week I asked Looping through two raster folders to perform raster calculation? about a script to loop through two geodatabases and perform a raster calculation by matching the two rasters up based on their coordinates. This is my post for clarification:
"I have two geodatabases with rasters containing information about "treecover" and "loss". The file names are slightly different but can be matched based on the last 8 characters (the coordinates of each raster granule).See image
I can perform this simple conditional statement using either CON or RASTER CALCULATOR to retrieve all "Loss" data situated on pixels which have a value for "treecover" greater than or equal to 50, with 9999 being assigned as the output value if the statement is FALSE. The output raster is exactly what I want. I just need to do it for 137 pairs of rasters
RASTER CALC STATEMENT: con("treecover raster" >= 50, "Loss raster", 9999)"
The script so far looks like this:
import arcpy, os, math
... arcpy.CheckOutExtension("Spatial")
... from arcpy.sa import *
... CVR = r"F:\Work\Aquifers_recalc\Final_recalc\Continent_Data\TEST\Treecover_TEST.gdb"
... LSS = r"F:\Work\Aquifers_recalc\Final_recalc\Continent_Data\TEST\Lossyear_TEST.gdb"
... # get first set of rasters
... arcpy.env.workspace = CVR
... cvr_rasters = arcpy.ListRasters()
... cvr_rasters = [os.path.join(CVR, r) for r in cvr_rasters]
...
... # get second set of rasters
... arcpy.env.workspace = LSS
... lss_rasters = arcpy.ListRasters()
... lss_rasters = [os.path.join(LSS, r) for r in lss_rasters]
...
... arcpy.env.workspace = r"F:\Work\Aquifers_recalc\Final_recalc\Continent_Data\TEST"
... for cvr_ras, lss_ras in zip(cvr_rasters, lss_rasters):
... r1 = arcpy.sa.Raster(cvr_ras)
... r2 = arcpy.sa.Raster(lss_ras)
... result = Con(r1 >=50, r2, 999)
... out_name = "loss_{}.tif".format(cvr_ras[-8:])
... result.save(out_name)
This script worked for two geodatabases with 4 rasters in each, but when i tried it with 9 rasters in each it failed on the 6th with this error. I will need to do this for two geodatabases with 137 rasters in each.
Runtime error
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\sa\Functions.py", line 269, in Con
where_clause)
File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\sa\Functions.py", line 256, in Wrapper
["IfThenElse", in_conditional_raster, in_true_raster_or_constant, in_false_raster_or_constant])
RuntimeError: ERROR 999998: Unexpected Error.
UPDATE: I deleted the pair of rasters causing the issue and the script still fails. It seems like it always fails after 5 pairs of rastes, without completing the 6th.
UPDATE:By adding the loop I recieved this error, but I got 6 rasters this time:
Tool failed in loop: 6 while executing rasters F:\Work\Aquifers_recalc\Final_recalc\Continent_Data\TEST\Treecover_TEST.gdb\Hansen_GFC2015_treecover2000_00N_070W and F:\Work\Aquifers_recalc\Final_recalc\Continent_Data\TEST\Lossyear_TEST.gdb\Hansen_GFC2015_lossyear_00N_090W
Tool failed in loop: 8 while executing rasters F:\Work\Aquifers_recalc\Final_recalc\Continent_Data\TEST\Treecover_TEST.gdb\Hansen_GFC2015_treecover2000_00N_090W and F:\Work\Aquifers_recalc\Final_recalc\Continent_Data\TEST\Lossyear_TEST.gdb\Hansen_GFC2015_lossyear_00N_070W
>>>