Select to view content in your preferred language

Concatenating user input into output path/filename

2443
2
09-07-2011 02:19 PM
BrianCohen
Deactivated User
I'm running a script that takes three pieces of user input, uses those as weights for a raster calcluation, and then saves the output raster layer.

I'd like to use those input values as part of the filename for the output raster, but, unfortunately, I''m not sure how (I copied most of this script from other places).

I'm pretty sure I need to set the input values as a string (which I have in a comment line), but how do I use those in the final line where I save the output file? Thanks for any help!

Script:
# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *


#set workspace
arcpy.env.workspace = "C:/GISTemp"


#assign variables to raster layers
reduced_west = Raster("reduced_west")
offsite_west = Raster("offsite_west")
frag_west = Raster("frag_west")


# Set the parameters from user input
RecoverInput = arcpy.GetParameter(0)
FragInput = arcpy.GetParameter(1)
OffsiteInput = arcpy.GetParameter(2)


# Convert input to percent
recoverweight = RecoverInput / 100
fragweight = FragInput / 100
offsiteweight = OffsiteInput / 100

# Check out Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Calculate ouput layer
WeightedOutput = ((reduced_west * recoverweight) + (offsite_west * offsiteweight) + (frag_west * fragweight)) / 3

# outpath = "C:/GISTemp/ModelOutput/Out_"
# outname = str(RecoverInput) + "_" + str(FragInput) + "_" + str(OffsiteInput)

WeightedOutput.save("C:/GISTemp/ModelOutput/Out_2")
Tags (2)
0 Kudos
2 Replies
TonyPagani
Emerging Contributor
You are correct that the argument passed to the save function needs to be a string.

Here is an example of how to create a path and name for your output.

outpath = "C:/GISTemp/ModelOutput/Out"
outname = �??myRaster�?�
WeightedOutput.save(outpath + os.sep + outname)


os.sep is the operating system path separator. You will need to import os in you script to us os.sep.
0 Kudos
BrianCohen
Deactivated User
Thanks for the response Tony. Interestingly, the problem turned out to be that the input values from the user were set as Double, which added a "." to the output path. So making them integers (using the int() function) got rid of that issue and the script worked with just OutRaster.save(pathname + rastername), without need for the os.sep.

Thanks again for the reply!
0 Kudos