nrwilson14

Batch Zonal Statistics Script - Error 010423

Discussion created by nrwilson14 on May 19, 2014
Latest reply on Jul 31, 2014 by rzufelt
I created a script to batch process Zonal Statistics as Table with user inputs, option to add a field, and a single appended table as an output.

It runs fine with some data. But with other data it throws a ERROR 010423: N:\Gabions\NDVI\35_38_May_June\t_t324 does not have valid statistics as required by the operation
When I modify the code to run from PyScripter it works fine and doesn't throw the error. (Modifications: hard-code the inputs, change AddMessage/AddWarning to print.) I haven't been able to find much information on this error within a python setting.

I tried adding Calculate Statistics just before the Zonal Statistics as Table but nothing changed...
arcpy.CalculateStatistics_management(raster,"","","","OVERWRITE",zonefile)

I'm pretty new to scripting. Any help?

# Import modules
import arcpy
import os
import shutil

# Get parameters
indir = arcpy.GetParameterAsText(0)
zonefile = arcpy.GetParameterAsText(1)
addfield = arcpy.GetParameterAsText(2)
startcharin = arcpy.GetParameterAsText(3)
endcharin = arcpy.GetParameterAsText(4)
outdir = arcpy.GetParameterAsText(5)
outfile = arcpy.GetParameterAsText(6)

# Set variables for zonal statistics and add field routines
zonefield = "UID"
startchar = int(startcharin) - 1
endchar = int(endcharin) - 1
outtable = ""
year = ""

# Set global environments and make scratch
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True
scratchdir = "C:\\TEMP\\Scratch\\ZoneStatsBatch"
if not os.path.exists(scratchdir):
    os.makedirs(scratchdir)

# Check the input data
fieldlist = arcpy.ListFields(zonefile,"*","ALL")
fieldnames = []
for field in fieldlist:
    fieldnames.append(field.name)
if not "UID" in fieldnames:
    arcpy.AddWarning(zonefield + " field does not exist in " + zonefile)
arcpy.env.workspace = indir
rasters = arcpy.ListRasters("*")
if not rasters:
    arcpy.AddWarning(indir +" contains no rasters \nZonal statistics failed")

# Zonal Statistics
index = 0
for raster in rasters:
    outtable = scratchdir + "\\scratchouttable" + str(index) + ".dbf"
    arcpy.sa.ZonalStatisticsAsTable(zonefile,zonefield,raster,outtable,"DATA","ALL")
    arcpy.AddMessage("Zonal statistics have been calculated for raster: " + raster)
# Add Field and Calculate Field if addfield boolean = True
    if addfield:
        try:
            year = raster[startchar:endchar]
            arcpy.AddField_management(outtable, "Year", "TEXT", "", "", 4)
            arcpy.CalculateField_management(outtable, "Year", year, "PYTHON")
            arcpy.AddMessage ("Year field (" + year + ") has been calculated for raster: " + raster)
        except:
            arcpy.AddWarning("Add year field failed")
    index = index + 1

# Set local variables and environment for Append
arcpy.env.workspace = scratchdir
outfiledbf = outfile +".dbf"
template = outtable
config_keyword = ""
outfullpath = outdir + "\\" + outfiledbf
schemaType ="NO_TEST"
fieldMappings = ""
subtype = ""

# Append
try:
    tablelist = arcpy.ListTables("*")
    arcpy.CreateTable_management(outdir, outfiledbf, template, config_keyword)
    arcpy.Append_management(tablelist, outfullpath, schemaType, fieldMappings, subtype)
    arcpy.AddMessage("Batch zonal statistics output table located at: " + outdir + "\\" + outfiledbf)
except:
    arcpy.AddWarning("Table append failed")

# Clean up your mess! (delete scratch)
try:
    shutil.rmtree(scratchdir,ignore_errors = "TRUE", onerror = "NONE")
    arcpy.AddMessage("Scratch workspace cleared")
except:
    arcpy.AddWarning("Scratch workspace not cleared: " + scratchdir)

Outcomes