[GIS] Error 999998 when saving raster dataset after using any raster algebra functions in ArcPy

arcpyerror-999998

I'm getting the following error without any documentation whenever I attempt to save a raster dataset after having used any algebraic functions (with any other raster or constant):

Traceback (most recent call last):

File "C:\Python27\ArcGIS10.6\Lib\site-packages\pythonwin\pywin\framework\interact.py", line 575, in OnEditExecClipboard
exec o in main.dict
File "", line 95, in
RuntimeError: ERROR 999998: Unexpected Error.

I've tried to minimize memory utilization by going as far as breaking it down into as many small pieces as I can and deleting the variables after using them. I've also tried using Plus(,) instead of +, Times(,) instead of *, taking things outside of loops and doing them individually, and nothing. I can save the output of any other function as long as there was never any algebra involved in the leadup Any suggestions? Here's one version of my code:

import arcpy as arc
arc.CheckOutExtension('Spatial')
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:\Users\hp\Documents\Work\EE_Model"
arc.env.outputCoordinateSystem = arcpy.SpatialReference("WGS 1984 UTM Zone 014N")
arcpy.env.cellSize = "MINOF"

# Set up Soil Raster
...

# Create Soil sub-Rasters

arc.conversion.PolygonToRaster("Soil_Cut.shp","N_G1","N_G1.tif",cellsize = 200.0)
arc.conversion.PolygonToRaster("Soil_Cut.shp","N_G2","N_G2.tif",cellsize = 200.0)
arc.conversion.PolygonToRaster("Soil_Cut.shp","N_G3","N_G3.tif",cellsize = 200.0)

print "Soils Converted"

N_G1 = arc.sa.Int("N_G1.tif")
arc.sa.EucAllocation(N_G1).save("N_G1.tif")
N_G1 = "N_G1.tif"

N_G2 = arc.sa.Int("N_G2.tif")
arc.sa.EucAllocation(N_G2).save("N_G2.tif")
N_G2 = "N_G2.tif"

N_G3 = arc.sa.Int("N_G3.tif")
arc.sa.EucAllocation(N_G3).save("N_G3.tif")
N_G3 = "N_G3.tif"

print "Soils Interpolated"

arc.sa.Times(N_G1,0.05).save("N_G1.tif")

EDIT: I should also note that trying to call a method like .minimum or .maximum results in this error:

Traceback (most recent call last):

File "C:\Python27\ArcGIS10.6\Lib\site-packages\pythonwin\pywin\framework\interact.py", line 575, in OnEditExecClipboard
exec o in main.dict
File "", line 22, in
RuntimeError: ERROR 999998: Unexpected Error.

Best Answer

This is a known bug, and the ArcGIS 10.5.1 (Desktop, Engine, Server) Spatial Analyst Zonal Statistics Tool Patch is available to resolve it:

Esri recommends installing this patch to prevent execution error 999998 in deeply nested map algebra expression with large data or incorrect output with 8 bit input raster from Zonal Statistics.