Raster Iteration and Raster Calculator Model

2683
4
05-16-2014 07:17 AM
MelissaSlater
New Contributor III
I have 2 raster datasets and I am trying to iterate through a "simple" raster calculation (Precipitation Grid + Residual Grid) for several hundred rasters.
I have created one Iterator model for the Residuals and used that as a sub model embedded in the main model which has an iterator for the Precipitation and the raster calculation. I've set parameters in my sub model so that I can call it in my main model and set the output name using inline variables to verify that the the model runs a one to one raster calculation but every way I've tried this it only adds the last raster in my sub-model to each iteration of the main model. I've tired a singular workspace using wild cards and variations of sub-model iterations all with no luck. I've also tried taking it back a step to a previous model where I created the precipitation grids and tried to embed an iterator sub-model there that adds the residual grid but also with no luck. I've attached my models but perhaps I am trying to go about this the wrong way...

HOW can I perform a one to one raster calculation for several hundred rasters?

Please, any help would be appreciated.
Thanks,
Melissa
0 Kudos
4 Replies
AmyKlug
Regular Contributor
0 Kudos
by Anonymous User
Not applicable
Original User: mslater06

I have tried Collect Value and that does not work either; only the last file in my sub-model is used in the raster calculation.
I am guessing that this is a limitation in Arc and Model Builder does not have the ability to run a sub-model iterator within a raster calculation iteration model...
0 Kudos
curtvprice
MVP Esteemed Contributor
What exactly are you trying to do -

p1 + r1
p1 + r2
p1 + r3
...
p2 + r1
p2 + r2
...


or:

p1 + r1
p2 + r2
p3 + r3

If it's the second, a much more straightforward method to do this is to iterate on one set (say, the precip folder you showed) and use the Calculate Value tool to generate the pathname of the other one. Here's an example expression:

[INDENT]
expression: r_path(r"%P_raster%")

code block:
import os
import arcpy
def r_path(praster):
  rfolder = r"C:\myfolder\rrasters"
  pname = os.path.basename(praster)  # P_srg10.tif
  pname = os.path.splitext(pname)[0] # P_srg10
  rname = pname.replace("P_","R_") + "_ols.tif" # R_srg10_ols.tif
  return os.path.join(rfolder,  rname) # "C:\myfolder\rrasters\R_rgs10_ols.tif"


type: Raster Dataset
[/INDENT]


Set the output of the Calculate Value to type Raster Dataset and then then both raster variables will be available in the Raster Calculator.

For your output name, you probably want to do another Calculate Value to create a name for your output raster that isn't so long:

[INDENT]
expression: get_number(r"%P_raster%")

code block:
import os
import arcpy
def get_number(praster):
  pname = os.path.basename(praster)
  pname = os.path.splitext(pname)[0] # "P_srg10"
  pnum = pname.replace("P_srg","") # "10"
  return pnum


type: String
[/INDENT]


Use the output raster name PR%value%.tif for the output from the Raster Calculator. Make sure this second calculate value is a precondition to the Raster Calculator so the name will be ready when the Raster Calculator runs.

You could simplify the above functions by incorporating the model builder tool Parse Path and perhaps using the raster name output of the iterator instead of the full path.
0 Kudos
by Anonymous User
Not applicable
Original User: alscatS

I am having a similar issue - I have a folder of raster files that needs to be recoded all the same way - then sum the output files.

I am trying to use iterate to recode all the raster files rather than have a model that has several input files. What I need to do is sum the output files..."step2a_recode_px.img" (see pic) there are actually several of these files and by using the %NAME% in the output it worked great.  I now need to sum those recoded files in the same model? As mentioned above is this a limitation - do I really need a separate model?
0 Kudos