arcpy.sa save random ERROR 999998: Unexpected Error

1794
8
Jump to solution
06-16-2018 10:00 PM
IanMCDONALD
New Contributor III

I am working on a pilot for a system that will eventually loop over a number of raster files to produce a single output one.  If I have two input rasters, the routine works reliably.  If I have more than three, it always fails.  If I try three after a failure, it fails, but it can be made to work by first resorting to just two inputs and then adding the third.  This is my (anonymised) code:

import arcpy
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")
arcpy.env.extent = "MAXOF"
arcpy.env.overwriteOutput = True
arcpy.env.qualifiedFieldNames = False
arcpy.env.workspace = "C:\aaaa"

inRasDir = bbb\\"
outRasDir = ccc\\"

terms = {}
terms = {
     'first'  : ['0.025', '*', '+', ],
     'second' : ['0.025', '*', '+', ],
     'third'  : ['0.025', '*', '+', ],
     'fourth'  : ['0.15',  '*', '-']
    }

def coeff(lyr):
    if terms[lyr][2] == '+':
        c = float(terms[lyr][0])
    else:
        c = -float(terms[lyr][0])
    return c;

outRas = ((
             (coeff('first')  * Raster(inRasDir + "first"))
           + (coeff('second') * Raster(inRasDir + "second"))
           + (coeff('third')  * Raster(inRasDir + "third"))
#           + (coeff('fourth')  * Raster(inRasDir + "fourth"))
            )
#           * 'fifth'
           )

outRas.save(outRasDir + "finalRas")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The error on the last line is:

    outRas.save(outRasDir + "finalRas")
RuntimeError: ERROR 999998: Unexpected Error.

I also tried using CellStatistics but that also failed on the save statement.

Any comments would be gratefully received.

Regards,

Ian

0 Kudos
1 Solution

Accepted Solutions
IanMCDONALD
New Contributor III

Yes, I was beginning to think memory management might be a problem.  I have taken your advice to simplify my test system.  It is now just a simple loop summing all the rasters.  The erratic behaviour appears to have been resolved by using a 64-bit version of Python.

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

It is hard to tell with your anon-stuff but this certainly doesn't work... 

You need to use raw formatting for paths or os.join

"C:\aaaa" + "finalRas"
'C:\x07aaafinalRas'  # ---- not good

"C:\aaaa\" + "finalRas"
'C:\x07aaafinalRas'
  File "<ipython-input-11-8b220ffe2f0d>", line 1
    "C:\aaaa\" + "finalRas"
                         ^
SyntaxError: invalid syntax

# This works
# ---- you need raw formatting or use forward slashes in the pathe
r"C:\aaaa" + r"\finalRas"
Out[12]: 'C:\\aaaa\\finalRas'
0 Kudos
IanMCDONALD
New Contributor III

Hi Dan. Thanks for your prompt response.

It was working, as far as the path definition was concerned (see the '\\' at the end of the inRasDir and outRasDir definitions). It was generating an output raster in the directory I expected and I could view it in ArcMap.

I have taken your advice and tried both the raw formatting and forward slashes but the outcome is the same. I can produce an output raster from up to three inputs but not four. If it fails with four, and I immediately try with three, it fails. If I wait for about a minute, without altering the code, it succeeds with three.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I suspect system resources then... 

but simplify things

import arcpy

a = np.random.randint(0,10, size=(3,3))*0.1

b = a*2

c = a*np.random.rand()

d = a*np.random.rand()

func = a + 1.0 + b * 2 - c + d/2

func
Out[14]: 
array([[3.24388419, 5.03899154, 2.34633051],
       [3.24388419, 5.03899154, 4.5902147 ],
       [4.5902147 , 3.69266102, 1.44877684]])

Now, replace a, b, c, d with

RasterToNumPyArray—Help | ArcGIS Desktop 

RasterToNumPyArray (in_raster, {lower_left_corner}, {ncols}, {nrows}, {nodata_to_value})

and func with

NumPyArrayToRaster—Help | ArcGIS Desktop 

NumPyArrayToRaster (in_array, {lower_left_corner}, {x_cell_size}, {y_cell_size}, {value_to_nodata})

Equivalent functions exist in PRO.

I have tons of NumPy examples on my blog

0 Kudos
IanMCDONALD
New Contributor III

Yes, I was beginning to think memory management might be a problem.  I have taken your advice to simplify my test system.  It is now just a simple loop summing all the rasters.  The erratic behaviour appears to have been resolved by using a 64-bit version of Python.

IanMCDONALD
New Contributor III

This problem was resolved for 32-bit implementations by ArcMap 10.6.1.

0 Kudos
curtvprice
MVP Esteemed Contributor

This sounds like the issue patched in 10.5.1 (I was one of the complainers)

ArcGIS 10.5.1 (Desktop, Engine, Server) Spatial Analyst Zonal Statistics Tool Patch 

0 Kudos
IanMCDONALD
New Contributor III

Yes, I only read that recently.  Apparently, the fix didn't make it into 10.6 which is were I came in!

0 Kudos
DanPatterson_Retired
MVP Emeritus