# 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)
rasterObj = arcpy.Raster(r"C:\temp\myraster.img") rasterObj.hasRAT
is there anything to be done about it
My data structure is solid with ESRI ArcGIS. All alpha starting characters, no spaces
# Import modules
import arcpy
# Get parameters
indir = arcpy.GetParameterAsText(0)
zonefile = arcpy.GetParameterAsText(1)
outdir = arcpy.GetParameterAsText(5)
outfile = arcpy.GetParameterAsText(6)
# Set global environments and make scratch
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True
scratchdir = arcpy.CreateFolder_management("C:\\TEMP","ZoneScratch")
scratchdir = str(scratchdir)
arcpy.env.scratchWorkspace = scratchdir
scratchgdb = arcpy.env.scratchGDB
scratchgdb = str(scratchgdb)
# Check the input data for the presence of the zone field in zone file
# and that rasters are present in the raster folder
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")
# Create individual feature classes for every feature in zonefile
# Allows zonal statistics to be computed for overlapping zones
zonedesc = arcpy.Describe(zonefile)
zonepath = zonedesc.catalogPath
arcpy.env.workspace = zonepath
cursor = arcpy.da.SearchCursor(zonefile,["UID"])
for row in cursor:
    rowuid = row[0]
    newfc ="scratchfc" + str(rowuid)
    whereclause = "\"UID\" = " + str(rowuid)
    featureclass = arcpy.FeatureClassToFeatureClass_conversion(zonefile,scratchgdb,newfc,whereclause)
del row
del cursor
# Create list of path names for these individual feature classes
# Allows access from a different workspace
arcpy.env.workspace = scratchgdb
features = arcpy.ListFeatureClasses("*")
featurepaths = []
for feature in features:
    desc = arcpy.Describe(feature)
    featurepaths.append(desc.catalogPath)
# Set variables and environment for zonal statistics and add field routines
arcpy.AddMessage("Setting variables and environment for ZoneStats/AddField")
zonefield = "UID"
rzonefield = "VALUE"
outtable = ""
arcpy.env.workspace = indir
arcpy.env.snapRaster = rasters[0]
arcpy.env.cellSize = rasters[0]
spatialRef = arcpy.Describe(rasters[0]).spatialReference
arcpy.env.outputCoordinateSystem = spatialRef
# Zonal Statistics
for feature in featurepaths:
    index = 0
    fname = feature.split("\\")[-1]
    zraster = scratchdir + "\\" + fname
    arcpy.FeatureToRaster_conversion(feature,zonefield,zraster)
    arcpy.AddMessage("Feature To Raster done, about to Calculate Statistics!")
    arcpy.CalculateStatistics_management(zraster)
    arcpy.AddMessage("Calculating Statistics Done")
    for raster in rasters:
        arcpy.AddMessage("Got into the raster in rasters loop!")
        outtable = scratchdir + "\\" + fname +"_" + str(index) + ".dbf"
        arcpy.sa.ZonalStatisticsAsTable(zraster,rzonefield,raster,outtable,"DATA","ALL")
        arcpy.AddMessage("Zonal statistics have been calculated for zone: " + fname + " and raster: " + raster)
        index = index + 1
					
				
			
			
				
			
			
				
			
			
			
			
			
			
		i=1 for i in range(50):
polyLyr=arcpy.MakeFeatureLayer_management(polygonFC, outputLyr, "\"UID\"={}".format(str(i)))
Just an update:
I went ahead with hard-coded python run out of PyScripter due to time constraints. Another issue that came up was that many of my polygons were significantly smaller than my raster cell size. I created a script to resample my rasters to a 1m resolution and that has helped as well.
I also got some suggestions from Jon Bodamer (ESRI) at the User Conference. His suggestions were list comprehensions and function definitions.
When (if!) I have the time to refine this script and get it into a script tool I'll definitely incorporate the last two suggestions as well. This was definitely a good crash course in scripting and problem solving. Thanks folks!
