model builder raster calculator converted to python script

338
3
06-26-2011 04:50 AM
AliceDeschamps1
New Contributor
I exported this from a working model builder tool and working on modifying to make it work in python.  In the end I would like to be able the automatically rename the output => input + ST_DEV + enh.tif .  

I am fairly new to Python so I am not there yet!  Python does not seam to recognize the map algebra from arcpy.gp.RasterCalculator_sa that was exported from Model Builder.   Therefore I decided to convert this part to straight python map algebra but I keep on getting errors for the code under # Process: Raster Calculator (1)

"RuntimeError: ERROR 000732: Input Raster: Dataset inRaster does not exist or is not supported."   This does not make sense to me since the inRaster is recognised in the previous 3 GetRasterProperties statements since the print statements are ouputting the right values?


Any suggestions on how to make this work??

# ---------------------------------------------------------------------------
# copy and past this at promt: 15May2011_HH_set_zero_8bit.tif 3 test.tif
# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *
import sys, string, os
arcpy.env.overwriteOutput = True

# Set Geoprocessing environments
arcpy.env.workspace = "C:/Alice"

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Script arguments
inRaster = arcpy.GetParameterAsText(0)
No_STDEV = arcpy.GetParameterAsText(1)
Output_Enhanced_8bit_SAR_Image = arcpy.GetParameterAsText(2)

# Process: Get Raster Properties (2)
imageMin=arcpy.GetRasterProperties_management(inRaster, "MINIMUM")
print "Image MIN value=", imageMin

# Process: Get Raster Properties (3)
imageMean=arcpy.GetRasterProperties_management(inRaster, "MEAN")
print "Image MEAN value=", imageMean

# Process: Get Raster Properties
imageSTDEV=arcpy.GetRasterProperties_management(inRaster, "STD")
print "Image STDEV value=", imageSTDEV

# Process: Raster Calculator (1)
enhImage=((Raster("inRaster") - imageMin) / ((imageMean + No_STDEV * imageSTDEV) - imageMin)) * 255

# Process: Raster Calculator (2)
outCon= Con(enhImage, enhImage, 255, "VALUE < 255")

# Process: Copy Raster
arcpy.CopyRaster_management(outCon, Output_Enhanced_8bit_SAR_Image, "", "", "", "NONE", "NONE", "8_BIT_UNSIGNED")
Tags (2)
0 Kudos
3 Replies
AliceDeschamps1
New Contributor
Fixed the error mentioned above with the code below.  Of course, I get a new error for same line ;(   Either the syntax or that .save?

# Process: linear stretch with right tail trim
enhImage = ((Raster(inRaster) - imageMin) / ((imageMean + No_STDEV * imageSTDEV) - imageMin)) * 255
enhImage.save("enhImage")
0 Kudos
LoganPugh
Occasional Contributor III
Take care not to quote variable names as quoting indicates it's a string, not a variable.

Also, not a big deal since there is no indentation in this script, but it makes it easier to read when you use the CODE tags (#) icon on the formatting toolbar above the message body box on your code to preserve indentation, which is significant in Python.
0 Kudos
AliceDeschamps1
New Contributor
Yes, thanks.  Its slowly coming back to me.   Now the new errors seam to be caused by a mix of integers, floats & string in my map algebra expression.  My inRaster & imageMin are integers while my imageMean and imageSTDEV are floats but some of these variables are coming in as stings too.  It a matter of getting the right conversions going now and I think is should work!
0 Kudos