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
      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

This is a visual of the two gdbs

  • Do you have enough space on disk? What happens if you limit the output filename to have fewer characters (e.g. 10)? Commented Aug 26, 2019 at 20:12
  • Hey Marcelo thanks for helping. Yes I have 300 gb of free space and when I change the character count to 10, It still fails after the 5th raster is done
    – seak23
    Commented Aug 26, 2019 at 20:42

1 Answer 1


ArcGIS Help description of the error is the following:

This is an operating system error and can indicate different error conditions. These errors are not specifically documented in the ArcGIS Desktop Help system. However, in most cases, the error description provided with the error code can be used to look up additional information on the operating system.

Taking this into account I would try the following things:

  • Take a look at the Name restrictions for raster names and make sure all the rasters have valid names. From your previous posts I see you were naming the output ras_calc_con. Maybe it's not the case in your new script but better make sure you are not using the word con. You can see one of the restrictions is:

The word con should not be used.

  • This answer suggests installing a patch to fix this bug. From the patch's page:

Issues Addressed with this patch

BUG-000109772 - The Spatial Analyst tools intermittently throw the 999998 error.

BUG-000108977 - The Zonal Statistics tool produces incorrect statistics when an 8 bit value raster is used as input.

  • Add a try/except clause to find whether the error is happening always the 6th time you execute the Con tool or if it only occurs to certain rasters. Your loop could look something like this:
for i, (cvr_ras, lss_ras) in enumerate(zip(cvr_rasters, lss_rasters), 1):
    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:])
    except RuntimeError:
        print('Tool failed in loop: {} while executing rasters {} and {}'.format(i, cvr_ras, lss_ras))
  • Thanks for the insight. There's no output with "con" in the name. That patch seems to be for 10.5 and i'm running on 10.6. I added the try/except clause and included the error in the OP
    – seak23
    Commented Aug 27, 2019 at 16:11
  • Taking a look at your edit, it seems that the tool is failing when cvr_ras and lss_ras do not coincide (e.g. 070W and 090W). Do you have the same amount of rasters in each GDB and do the names match exactly in the last relevant characters? Commented Aug 27, 2019 at 16:19
  • Yes there are the exact same numbers of rasters. I even deleted them and included new ones to make sure everything is exactly the same. I included a pic in the OP. Is there someway to force the rasters to join based on the last 8 characters, instead of based on their order in the gdb?
    – seak23
    Commented Aug 27, 2019 at 16:53
  • If the roots don't change (i.e. they are always Hansen_GFC2015_treecover2000 and Hansen_GFC2015_lossyear) you can sort the lists just to make sure. You can add the following lines before the loop and try: cvr_rasters.sort() and lss_rasters.sort(). This will sort the lists alphabetically and in place, so you don't need to assign the result to a variable. Try this and print the lists before the loop to compare them. You don't need to run the whole process. Commented Aug 27, 2019 at 17:02
  • I printed the lists and they seem to match up just fine. Sorting the lists seemed to have worked, but ArcMap crashes after the 3rd raster pair. Every time.
    – seak23
    Commented Aug 27, 2019 at 17:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.