Iteration of 2 datasets (raster calculator)

1512
4
04-26-2017 05:16 PM
Fra_AngelicoViray
New Contributor

Good day!

I am having a problem setting up a model builder. I have 2 raster datasets (qx,qy) that are needed to be iterated as input for the raster calculation tool. The number of files of qx and qy is 243 each. The expression for the raster calculator is as follows:

SquareRoot(Square("%qx%")+Square("%qy%"))   with terms:

qx1 and qy1
qx2 and qy2

.... qxn and qyn.

My initial setup is as follows (since you can only use 1 iterator in a model): 

For this trial, I only used 5 rasters both for qx and qy. The model actually ran the calculation but the terms used were not correct. Please see below:

qy1 and qx5

qy2 and qx5

qy3 and qx5

qy4 and qx5

qy5 and qx5

Qy (iteration in the main model) was correctly iterated but not in the case of Qx. I hope you can help me with this problem. Thank you!

0 Kudos
4 Replies
curtvprice
MVP Esteemed Contributor

This is how loops work. If you want to operate on pairs you need to only do one iterator on qx and somehow create the path to qy from qx. No nesting needed.

In python scripts you can easily loop over a list of 2 item lists, but this isn't easily accomplished in ModelBuilder.

0 Kudos
curtvprice
MVP Esteemed Contributor

I just had a thought. Maybe the For iterator is what you need here, and use Calculate Value to generate the paths to the input rasters based on the index number. 

0 Kudos
Fra_AngelicoViray
New Contributor

Thank you very much Curtis Price. Using python scripts can be my backup plan but I am not familiar with that. Do you have sample code for this kind of scenario. I am leaning to your 2nd advice for the "for" iterator. Can you show me how? Thank you again.

0 Kudos
curtvprice
MVP Esteemed Contributor

Here is a model that uses the For tool in this way. You can get this to work by modifying the Calculate Value tools to construct a full file path, and set the outputs to type Raster Dataset. Then use the model elements grid1 and grid2 in the Map Algebra tool. I have connected the Value element to the tools as a precondition to ensure both Calculate Value tools run on every iteration.

model tool for example

Here's what the messages look like when this model is run:

Executing (For): IterateCount 1 3 1
Start Time: Thu Apr 27 14:44:33 2017
Succeeded at Thu Apr 27 14:44:33 2017 (Elapsed Time: 0.00 seconds)
Executing (Calculate Value): CalculateValue r"qx1" # Variant
Start Time: Thu Apr 27 14:44:34 2017
Value = qx1
Succeeded at Thu Apr 27 14:44:34 2017 (Elapsed Time: 0.08 seconds)
Executing (Calculate Value (2)): CalculateValue r"qy1" # Variant
Start Time: Thu Apr 27 14:44:34 2017
Value = qy1
Succeeded at Thu Apr 27 14:44:34 2017 (Elapsed Time: 0.08 seconds)
Executing (For): IterateCount 1 3 1
Start Time: Thu Apr 27 14:44:34 2017
Succeeded at Thu Apr 27 14:44:34 2017 (Elapsed Time: 0.00 seconds)
Executing (Calculate Value): CalculateValue r"qx2" # Variant
Start Time: Thu Apr 27 14:44:34 2017
Value = qx2
Succeeded at Thu Apr 27 14:44:34 2017 (Elapsed Time: 0.08 seconds)
Executing (Calculate Value (2)): CalculateValue r"qy2" # Variant
Start Time: Thu Apr 27 14:44:34 2017
Value = qy2
Succeeded at Thu Apr 27 14:44:34 2017 (Elapsed Time: 0.08 seconds)
Executing (For): IterateCount 1 3 1
Start Time: Thu Apr 27 14:44:34 2017
Succeeded at Thu Apr 27 14:44:34 2017 (Elapsed Time: 0.00 seconds)
Executing (Calculate Value): CalculateValue r"qx3" # Variant
Start Time: Thu Apr 27 14:44:34 2017
Value = qx3
Succeeded at Thu Apr 27 14:44:34 2017 (Elapsed Time: 0.08 seconds)
Executing (Calculate Value (2)): CalculateValue r"qy3" # Variant
Start Time: Thu Apr 27 14:44:34 2017
Value = qy3
Succeeded at Thu Apr 27 14:44:34 2017 (Elapsed Time: 0.08 seconds)
Executing (For): IterateCount 1 3 1
Start Time: Thu Apr 27 14:44:34 2017
Succeeded at Thu Apr 27 14:44:34 2017 (Elapsed Time: 0.00 seconds)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos