looping through SQL, speed

Discussion created by aldescha_9 on May 18, 2010
I built this model that creates a raster water mask from a SAR image.   It take the image, filter it and then extracts a raster water mask (0,1) based on a specific threshold.  A processing mask can be used as an option to limit the processing to an area of interest.   The raster water mask is then filtered using a mode filter to remove small isolated pixels.   The model runs as a tool and prompts the user for an input/ ouput filename, processing mask (optional) and sql expression (default given).  See exported model to script below...

1) I exported the model to a script and  would like to add a looping functionality based on the sql expression.  The expression is as follows, "Value > 0 AND Value < 10".   For example, upon input of a theshold of 10 I would like to produce a mask with a threshold of 9 (-1) and 11 (+1).  Therefore that script would be producing 3 raster mask every time it ran.  Ideally it would also name the ouput files automatically based on the threshold.    Any advice on how to do this??
Note:  As a temporary solution I used the batching functionality in the model.  It works well but I think it leaves a bit too much room for errors.

2) The first image filter is very slow (Focal statistics, 3x3 median).  Test were ran with the toolbox tool being the slowest  (24min), desktop command line (7 min) and arc command line being much faster (don't have the numbers).   Any explanation for this difference is speed and any suggestion on how to speed it up?    Do script tools run faster than model tools?

3) While I am at it, I have another question.   Is there any functionality in Model builder to linearly scale an image from 16bit to 8 bit?   I did not find a function for this but maybe it does exist?  Otherwise I would need to find the min and max from and image and somehow use a formula to scale the image.   This will be my next challenge!

I am new to model builder and scripting with Python.   Thanks for any advice on these issues.

# ---------------------------------------------------------------------------
# Created on: Tue May 18 2010 01:29:25 PM
#   (generated by ArcGIS/ModelBuilder)
# Usage: ExtractWaterMask <Input_8bit_scaled_SAR_image> <Processing_Mask> <Water_Threshold> <Output_8bit_water_mask>
# ---------------------------------------------------------------------------

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

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

# Check out any necessary licenses

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

# Set the Geoprocessing environment...
gp.mask = ""

# Script arguments...
Input_8bit_scaled_SAR_image = sys.argv[1]

Processing_Mask = sys.argv[2]

Water_Threshold = sys.argv[3]
if Water_Threshold == '#':
Water_Threshold = "Value > 0 AND Value < 11" # provide a default value if unspecified

Output_8bit_water_mask = sys.argv[4]
if Output_8bit_water_mask == '#':
Output_8bit_water_mask = "D:\\EMERG\\test\\Water_Mask" # provide a default value if unspecified

# Local variables...
Con_focalst_1 = "D:\\EMERG\\test\\Con_filter_31"
Input_true_raster_or_constant_value__3_ = "1"
Input_false_raster_or_constant_value__3_ = "0"
filter_scale1__2_ = "D:\\EMERG\\R1_21mars10\\mosaic\\filter_scale1"
filter_scale1__3_ = "D:\\EMERG\\R1_21mars10\\mosaic\\filter_scale1"
filter_33 = "D:\\EMERG\\test\\filter_33"

# Process: Focal Statistics (3x3 median)...
gp.FocalStatistics_sa(Input_8bit_scaled_SAR_image, filter_33, "Rectangle 3 3 CELL", "MEDIAN", "DATA")

# Process: Con...
tempEnvironment0 = gp.mask
gp.mask = ""
gp.Con_sa(filter_33, Input_true_raster_or_constant_value__3_, Con_focalst_1, Input_false_raster_or_constant_value__3_, Water_Threshold)
gp.mask = tempEnvironment0

# Process: Focal Statistics (5x5 Mode)...
gp.FocalStatistics_sa(Con_focalst_1, Output_8bit_water_mask, "Rectangle 5 5 CELL", "MAJORITY", "DATA")