Using derived variables in the Raster Calculator tool?

Discussion created by curtvprice Champion on Mar 18, 2012
Latest reply on May 13, 2014 by curtvprice
This is a cross-post from the [post=181728]Geoprocessing forum:[/post]

Hello, I have constructed a model that uses an OLS regression towards the end.  I would like the values for two of the coefficients to be used as part of a raster calculator equation that rebuilds the regression and outputs to a raster.  I have entered the equation that I want to use in the Raster Calculator tool, but cannot figure out how to make the two coefficients from the OLS regression become part of the equation (they are not listed as options under the Layers and Variables section of Raster Calculator).  Part of the problem is that the model is designed to have those two coefficients be different every time the model is run.  Is there any way that I can have the two coefficients become part of the equation?  I have attached a screen shot of the model for clarity.  Any help is greatly appreciated...

There is some missing information here.... I'm assuming your "variables" output from the script tool is a string right? Say it's a space delimited text with two numbers: "234.2 -12.4".

Add two Calculate Value tools to your model (right click / model tools/ calculate value) and set "variables" as a precondition to both so they will run after your OLS script.

The expressions (one for each Calculate Value) would look like this, using the default output type (Variant) would be fine. Note the correct use of quotes and "%" characters in Calculate Value, this is important and took me a while to figure out. (The % substitution happens before any python interpretation happens, the quotes are part of Python syntax designating the values between the quotes compose a text string.)

"%variables%".split()[0]  # python sees "234.2 -12.4".split()[0] (== "234.2")
"%variables%".split()[1]  # == "-12.4"

(BTW, if you use path variables in Calculate Value you must use the syntax r"%input dataset%" to make sure the Windows path back-slashes are interpreted correctly by Python.)

ModelBuilder will give them default names "output_value" and "output_value (2)". Rename them to "regval1" and "regval2".

At this point you can precondition these two on a second calculate value expression, when does python string manipulation to create your map algebra expression, or you can enter the values %regval1% and %regval2% directly into the Map Algebra tool. I'm not sure which will work better in this situation, though I like the second approach better as you can connect raster inputs into the tool which makes for a prettier ModelBuilder presentation.