I am running Arc 10.5 on Windows 10 and am having issues with the Spatial Analyst tool ZonalStatisticsAsTable that appears to crash my python script for no reason. I have used this script before and it ran fine for many other inputs but for some reason when I use it on this particular set of inputs, it randomly crashes. Basically what I am trying to do is calculate a "MEDIAN" while using a mask. All inputs are integer grids, with projection defined the same, and attribute tables constructed for each input raster. For this particular set of inputs ( using a mask which is denoted by my list, 81 and 82 which is a variable to call the raster: arcpy.env.mask = r"U:/NAWQA/GIS/FlowDist/nlcd11"+id) my script will randomly crash. It will run and process the first 4 inputs and then crash. I can manually do the operation on these sets of inputs in ArcMap with no problem but I cannot get myscript to run properly in Pyscripter. I am including my script below.

import os, sys, arcpy
from arcpy import env
from arcpy import sa
from arcpy.sa import *
from datetime import datetime
wks = r"U:/NAWQA/GIS/FlowDist/Distances"
env.workspace = wks
startTime = datetime.now()
# Check out Spatial Analyst extension license
arcpy.env.overwriteOutput = True
env.qualifiedFieldNames = False

# Set the Geoprocessing environment...
arcpy.scratchWorkspace = "U:/NAWQA/GIS/FlowDist/Distances"
arcpy.env.geographicTransformations = ""
arcpy.env.outputCoordinateSystem =                 

arcpy.env.pyramid = "NONE"
NHD_dir = "Q:/WORK/NHDPlusV2"
list = ['81' '82']
for id in list:
    tv = "tv"
    tvStat = "tvStat"
    tmpTable2 = "temp2.dbf"
    #Local variables...

    inputs = {'CA':['18'],'CO':['14','15'],'GB':['16'],'GL':['04'],'MA':    ['02'],'MS':['05','06','07','08','10L','10U','11'],'NE':['01'],'PN':['17'],\
    for regions in inputs.keys():
        for hydro in inputs[regions]:
            print 'Region: ' + regions + ' Production unit: ' + hydro
            inZoneData = "%s/NHDPlus%s/NHDPlus%s/NHDPlusCatchment/cat"%    (NHD_dir,regions,hydro)
            print inZoneData
            zoneField = "VALUE"
            print zoneField
        valGrid = r"Q:/GIS/Flow/National_Flow_Dist_Down/flwdist"
        print valGrid
        outTable = "temp0.dbf"
        print outTable
        arcpy.env.cellSize = inZoneData
        arcpy.env.Extent = inZoneData
        arcpy.env.mask = r"U:/NAWQA/GIS/FlowDist/nlcd11"+id
        outTable1 = "U:/NAWQA/GIS/FlowDist/Distances/D"+id+"/Median_DIST_PU%s.dbf"%(hydro)
        outTable1 = os.path.join(wks,outTable1)
        catShape= "%s/NHDPlus%s/NHDPlus%s/NHDPlusCatchment/catchment.shp"%(NHD_dir,regions,hydro)
        print catShape
        print outTable1
        outZSaT = ZonalStatisticsAsTable(inZoneData, zoneField, valGrid, outTable, "DATA", "MEDIAN")
# join catchment shape attributes to stats and write out
        #arcpy.AddIndex_management(tvStat, "VALUE","IDX")
        arcpy.AddJoin_management (tv, "GRIDCODE", tvStat, "VALUE")
        print catShape
# Set up desired fields and order for output
        arcpy.CreateTable_management(os.path.dirname(outTable1), os.path.basename(outTable1))
        arcpy.AddField_management(outTable1,"MEDIAN","DOUBLE") # statistic value
        arcpy.AddField_management(outTable1,"DIST"+id,"DOUBLE") # raster name #statistic value
# delete dummy field added by CreateTable
# Copy matching fields to the output table
# tag non-matches with the zonal stat = -9999
        arcpy.CalculateField_management(tv,'COMID', '!FEATUREID!', "PYTHON_9.3")
        arcpy.CalculateField_management(tv,'DIST'+id, '!MEDIAN!', "PYTHON_9.3")
        arcpy.CalculateField_management(tv,'basin_area', '!AREASQKM! * 1000000', "PYTHON_9.3")
        arcpy.CalculateField_management(tv,'nodata_are', '!basin_area! - !AREA!', "PYTHON_9.3")
        arcpy.CalculateField_management(tv,'NODATA', '100 * !nodata_are! / !basin_area!', "PYTHON_9.3")
        where = "AREA = 0" # if output is GDB use "AREA IS NULL"
        arcpy.CalculateField_management(tv,"DIST"+id,"-9999", "PYTHON_9.3")
  • What are the precise symptoms of your "crash". – PolyGeo Sep 14 '17 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.