Select to view content in your preferred language

Issues bringing stand-alone python script in as a Script Tool

1089
1
09-06-2012 01:47 PM
DanSlayback
Deactivated User
I'm trying to bring a script that runs perfectly well in command-line python into ArcMap as a Script Tool, and am having a couple of intransigent problems:

When run as a Script Tool, after the first run in the ArcMap session, there are often (but not always) errors regarding read-only status of data source etc, eg:
...
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 484, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 999999: Error executing function.
Workspace or data source is read only.
Workspace or data source is read only.
Workspace or data source is read only.
Workspace or data source is read only.
Workspace or data source is read only.
ERROR 010302: Unable to create the output raster: C:\Users\Dan Slayback\Documents\ArcGIS\Default.gdb\SetNull_Idw_1
ERROR 010067: Error in executing grid expression.
Failed to execute (SetNull).

This never happens on the first run in the ArcMap session - sometimes you can run it twice, but rarely if ever 3 times. So I suspect it has to do with something being locked. I've tried lots of variations on deleting intermediate objects, but this has not consistently helped.

More problematic is the fact that even when it runs without complaint as a Script Tool in ArcMap, the SetNull part is not working. Whereas, when run from a command prompt, it does. The guts of the code are as follows:

    import arcpy, string
    from arcpy.sa import Idw, RadiusFixed, SetNull

    arcpy.env.overwriteOutput = True

    spRef = r"E:Code\WGS_1984_UTM_Zone_36N.prj"
    pointlayer = "PointLayer"   
    arcpy.MakeXYEventLayer_management(csvfile, "X", "Y", pointlayer, spRef, 'Median')

    arcpy.CheckOutExtension("Spatial")

    # Execute IDW
    searchRadius = RadiusFixed(1,"")
    outIDW = Idw("PointLayer", 'Median', 0.2, 2, searchRadius)

   # Convert values < -1000 to NoData
    outSetNull = SetNull(outIDW, outIDW, "Value < -1000")

    # Save to tiff file
    outSetNull.save(outTiff)



Finally, this used to run both as a Script Tool (and from the dos prompt), without error, before I added the SetNull line, in an attempt to clean up some very low values that are coming out outside the perimeter of the data. (another problem - Idw does not seem to be doing the right thing here, but figured i could get around it with SetNull).

I'd greatly appreciate any suggestions on (1) what I should be deleting at the end of this in order to unlock any relevant files/workspaces for the next run, and whether to use 'del' or arcpy.Delete_management, and (2) why SetNull may not be working when run as a Script Tool.
Tags (2)
0 Kudos
1 Reply
GeorgeNewbury
Frequent Contributor
Not sure if what you put in there accurately reflects your code but:

- Your projection file is probably missing a backslash? Should it be:
  spRef = r"E:\Code\WGS_1984_UTM_Zone_36N.prj"

- In the documentation for 'MakeXYEventLayer' it shows the spatial reference should be a spatial reference object. So maybe you need to create the spatial reference. spRef = arcpy.SpatialReference(r"E:\Code\WGS_1984_UTM_Zone_36N.prj")

- In the examples of SetNull, they have the word 'value' capitalized, ie. 'VALUE'. That could be causing a problem, see:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z00000007000000.htm
0 Kudos