Error Parsing Argument for Add Message when running scripts again or sequentially

2859
3
04-01-2011 05:55 PM
KarlZimmer
New Contributor II
Hello all,

I've built a series of scripts that run great in Pythonwin, but I've recently ran into an error after bringing them in as ArcGIS tools. All the scripts/tools work fine the first time when I run them, but for some reason I am getting an attribute error when I either run the script again or run the next script in the analysis sequence. All my scripts have the same structure so I suspect once I solve this issue for one I'll solve it for them all.  The exact error reads:

Executing: 4ReclassifySnowto12 J:\MODIS_Test\SnowCloudFill\HEB_SNOW_GEO_RUN6_NibbleFill J:\Scratch J:\MODIS_Test\SnowCloudFill\HEB_SNOW_GEO_RUN7_ReclassSnow1
  Start Time: Fri Apr 01 19:18:54 2011
  Running script 4ReclassifySnowto12...
  <type 'exceptions.AttributeError'>: Object: Error in parsing arguments for AddMessage
  Failed to execute (4ReclassifySnowto12).
  End Time: Fri Apr 01 19:18:54 2011 (Elapsed Time: 0.00 seconds)
 
If I click cancel and try running the script again it works fine. It just seems to fail every second time I try to run the script, but I can't figure out what Argument is not parsing correctly.  Any help would be great. Attached is a sample script code:

# Con_sample.py
# Description: Performs a conditional if/else evaluation on each cell of an input raster.
# Requirements: None
# Author: ESRI
# Date: 12/01/03

# Import system modules
import sys, string, os, arcgisscripting,time,datetime

# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

# Load required toolboxes...
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")
##gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

# Check out Spatial Analyst extension license
gp.CheckOutExtension("spatial")
print " got extensions"

gp.OverWriteOutput = 1

start = time.clock()
print "Time starting"
gp.AddMessage ("Time Starting")

# workspace containing raw MODIS rasters
##gp.workspace = r"J:\GEOG683\SnowCloudFill\HEB_SNOW_GEO_RUN4"
gp.workspace = sys.argv[1]
print "Workspace with Original rasters is = " + gp.workspace
gp.AddMessage("Workspace with Original rasters is = " + gp.workspace)

# Set location of Scratch Directory
gp.scratchworkspace = r"J:\Scratch"
gp.scratchworkspace = sys.argv[2]
print "Scratch Workspace = " + gp.scratchworkspace
gp.AddMessage("Scratch Workspace = " + gp.scratchworkspace)

##outputFolder = r"J:\GEOG683\SnowCloudFill\HEB_SNOW_GEO_RUN6_NibbleFill"
outputFolder = sys.argv[3]
print "Output Directory for Nibble Filled Raster will be" + outputFolder
gp.AddMessage("Output Directory for Nibble Filled Raster will be " + outputFolder)

##RasterDirectory = r"J:\GEOG683\SnowCloudFill\HEB_SNOW_GEO_RUN5_ReclassNodata"
RasterDirectory = sys.argv[4]
print "Directory with Raster Mask for Nibble Filled Raster will be" + RasterDirectory
gp.AddMessage("Directory with Raster Mask for Nibble Filled Raster will be" + RasterDirectory)

##OutputDirACC4 = r"J:\GEOG683\SnowCloudFill\HEB_SNOW_GEO_ACC_4"
OutputDirACC4 = sys.argv[5]
print "Output Directory for Assumption Raster 4 will be" + OutputDirACC4
gp.AddMessage("Output Directory for Assumption Raster 4 will be = " + OutputDirACC4)

try:
  
    # Start raster list of all rasters in the original raster workspace   
    rsList = gp.ListRasters("*")
    # begin looping through rasters
    ##rs = rsList.Next()
    for rs in rsList:
       # Creat the name for the output raster including output directory location
        ##InRaster = rs
        InRaster = gp.workspace + "\\" + rs
        print "Input Raster = " + InRaster

        #outputFolder = "J:/GEOG683/SnowCloudFill/HEB_SNOW_GEO_RUN6_FocalFill"
        ##outputFolder = r"J:\GEOG683\SnowCloudFill\HEB_SNOW_GEO_RUN6_NibbleFill"
        OutRaster = outputFolder + "\Nib" + rs    
        
        ##RasterDirectory = r"J:\GEOG683\SnowCloudFill\HEB_SNOW_GEO_RUN5_ReclassNodata"
        InRasterMask = RasterDirectory + "\\RclND_" + rs
        print "InRaster Mask = " + InRasterMask
           
        print "Output Raster = "+ OutRaster
        print " ......................."

        # Process: FocalStatistics
        #InExpression = "con("+ gp.workspace + "\\" + InRaster + " == 50, focalmajority(" + gp.workspace + "\\" + rs + ", Rectangle, 3,3),"+ gp.workspace + "\\" + InRaster + ")"
        #print InExpression

        ##gp.Nibble_sa(Fill50AfterOnly_Montana_Canada_Maximum_Snow_Extent_A2000D065_tif, tests_tif, Nibble_Fill51, "DATA_ONLY")
        gp.Nibble_sa(InRaster, InRasterMask, OutRaster, "DATA_ONLY")
        print "Nibble Fill Succuess"
        ##rs = rsList.Next()

        AccuracyOutput4 = OutputDirACC4 + "\\" + "Acc4" + rs
        print "ACC4 " + AccuracyOutput4
        gp.AddMessage ("ACC4 " + AccuracyOutput4)
        print "Acc input " + InRaster
        gp.AddMessage("Acc input " + InRaster)
        print "Acc output " + OutRaster
        gp.AddMessage("Acc output " + OutRaster)
        
        Accuracyexpression4 = "con("+ InRaster +" == " + OutRaster + ",0, 4 )"
        print "Accuracy4 expression = "+ Accuracyexpression4
        gp.AddMessage ("Accuracy4 expression = "+ Accuracyexpression4)
        
        gp.SingleOutputMapAlgebra_sa(Accuracyexpression4, AccuracyOutput4)
        print "Accuracy 4 is completed"
        gp.AddMessage("Accuracy 4 is completed")

   
# Error checker           
except:
    print " Does not work"
    gp.getmessage()
    
# Stop Time Clock
end = time.clock()
print "Processing time:", (end-start)/60, "minutes"
gp.AddMessage("Processing time:", (end-start)/60, "minutes")   
Thanks in Advance
Tags (2)
0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus
gp.AddMessage("Processing time:", (end-start)/60, "minutes")
this line is wrong since it read
gp.AddMessage("Processing time:" + str( (end-start)/60.0 ) + " minutes")
the reason that you didn't get an error in Pythonwin is because, it simply ignored it...there may be other errors but remember
print "a", "b", "c"
needs to be translate to
gp.AddMessage("a" + "b" + "c")
until ESRI makes AddMessage more Pythonic
0 Kudos
KarlZimmer
New Contributor II
Great Catch Dan! That seems to have solved the problem and would explain why its happening on all my scripts as I copied and pasted that line to each of them! Thanks again for the help!

Cheers,
Karl
0 Kudos
DanPatterson_Retired
MVP Emeritus
No problem Karl
I also wanted to point out that the line

(end-start)/60.0 )

ensures that integer division doesn't occur.  Consider the following code snippet which demonstrates the importance of ensuring that at least one of the inputs is a floating point number when performing division IF you want a floating point result.
>>> start = 0
>>> end = 10
>>> result = (end - start) / 60
>>> print result
0
>>> result_good = (end - start) / 60.0
>>> print result_good
0.166666666667
>>> 
0 Kudos