# 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)
Might want to put some print/addmessage statements in there and look at the parameters that are being passed.
I say this as the last time I had something working fine in stand alone, it would not function properly in a script tool.
Basically if I had something like this:
intext = arcpy.GetParameterAsText(0) # from text input box in tool
and I enter "test text" (without the quotes of course)
>>>print intext
"test text"
However, in stand alone if I set:
>>>intext = "test text"
>>>print intext
test text
To match the same format in stand alone, I need to do something similar to:
>>>intext = '"' + "test text" + '"'
>>>print intext
"test text"
as you can see, when it gets passed as a parameter as text, the variable value includes the double quotes. If you just set a variable equal to a text value, the double quotes are not there.
Just a thought as the first time I ran into this it took me a while to realize the two outputs were different (one had quotes).
R_