I looked further at the output of the zonal statistics tool for my 2 zones. According to the output statistics table, the means and standard deviations of the two zone are both 1 and 0, respectively. Obviously, this is not correct. Could there be some issues with the noData parts?Here is the code for the function I wrote:
import arcpy, os, random
from arcpy import env, mapping
from arcpy.sa import *
def calcZonalHistograms(polygonFile, zoneField, inRaster, outDir = None, outTbls=None, cleanup=1):
if outTbls:
outHistTbl, outStatTbl = outTbls
else:
outHistTbl = os.path.join(os.path.dirname(inRaster), os.path.basename(inRaster).split(".")[0]+"_zoneHist_tbl.dbf")
outStatTbl = os.path.join(os.path.dirname(inRaster), os.path.basename(inRaster).split(".")[0]+"_zoneStat_tbl.dbf")
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Find frequencies for histogram. This will use 256 bins of equal intervals spanning the range of values in the area. (?)
ZonalHistogram(polygonFile, zoneField, inRaster, outHistTbl)
# Execute ZonalStatisticsAsTable
outZSaT = ZonalStatisticsAsTable(polygonFile, zoneField, inRaster, outStatTbl, "NODATA")
# Find bin sizes and values
# Put histogram frequencies in a dictionary; key word equal to field name (Poly_Area)
fieldList = arcpy.ListFields(outHistTbl)
info_dict = {}
binSizes = {}
for field in fieldList:
if field.name == "OID": pass
else:
info_dict[field.name] = []
rows = arcpy.SearchCursor(outHistTbl)
i = 0
for row in rows:
for field in fieldList:
if field.name != "OID": info_dict[field.name].append(row.getValue(field.name))
i+=1
# Find bin Size from outStatTbl
rows = arcpy.SearchCursor(outStatTbl)
for row in rows:
binSizes[row.getValue("POLY_AREA")] = [row.getValue("MIN"), (float(row.getValue("MAX"))-float(row.getValue("MIN")))/i]
# Export to txt for use in other programs (excel, etc...)
if not outDir: outDir = os.path.dirname(inRaster)
for key in info_dict.keys():
exportHistogramToTxt(binSizes[key][1], binSizes[key][0], info_dict[key], key, os.path.join(outDir, key+"_hist.txt"))
Again, thanks for the help!