[GRASS-user] how to add a loop in the graphical modeler

Hi everyone,

I’m trying the start getting familiar with the graphical modeler as I have a lot of raster operations that I need to automate.
I have 456 raster layers that I (1) want to interpolate using v.surf.bspline, and then (2) export to six different folders, 76 rasters for each.

I have designed the workflow in the graphical modeler using just one layer to see that everything works the way that it should, and it does!
However, I can’t for the life of me figure out how to set up a loop so that it can iterate through all the remaining layers. I’m trying my best to
read the manual, but since all of this is new to me, I can’t figure out (1) how to add several raster layers. Whenever I use the “add data to model” I can
only add one layer from the drop down list. I also don’t know if I need two loops, one for interpolation and then one for the raster export.

I have attached a screenshot that outlines my model. I hope you’ll be able to guide me in the right direction!

Best,
Victor

Hello.
I’m Javier García Prieto from México.

I do exactly the same as you intend to do years ago.

There are 2 ways to do what you want frim my perspective.

1 If you are working in Linux do a bash script to import every raster to grass, then execute your model from that script.

https://thedbadmin.com/how-to-loop-in-bash-linux-shell-script-automation/

https://grasswiki.osgeo.org/wiki/Importing_data#:~:text=Create%20a%20new%20location%20for,raster%20data%20(internally%20using%20GDAL)

2 Use Python to do the same in a way that the script is system agnostic. Once you have aquired your maps in grass use your model as described above.

Greetings from Mexico.

Enviado desde mi LG de Telcel.

------ Mensaje original------
Desde: Victor Lundström via grass-user
Fecha: sáb., 6 ene. 2024 4:17
Para: grass-user;
Cc:
Asunto:[GRASS-user] how to add a loop in the graphical modeler

Hi everyone,

I’m trying the start getting familiar with the graphical modeler as I have a lot of raster operations that I need to automate.
I have 456 raster layers that I (1) want to interpolate using v.surf.bspline, and then (2) export to six different folders, 76 rasters for each.

I have designed the workflow in the graphical modeler using just one layer to see that everything works the way that it should, and it does!
However, I can’t for the life of me figure out how to set up a loop so that it can iterate through all the remaining layers. I’m trying my best to
read the manual, but since all of this is new to me, I can’t figure out (1) how to add several raster layers. Whenever I use the “add data to model” I can
only add one layer from the drop down list. I also don’t know if I need two loops, one for interpolation and then one for the raster export.

I have attached a screenshot that outlines my model. I hope you’ll be able to guide me in the right direction!

Best,
Victor

Hi Victor,

You can do it as Javier said. However, if you really wish to do everything in the modeler, you can do so, but the loops in the modeler are optimized for data already in GRASS and thus it would be sort of clumsy.

You define your models using a variable, for example %map as I did for my r.in.gdal:

Screenshot from 2024-01-06 19-32-44.png

Then you can define a loop using the “Add loop/series to model” button (or Model → Add loop / series). If the maps were already imported, you could automatically loop through them based on a pattern. As there are outside, you would need to write all of them manually (or copy from some list of yours) which is pretty annoying but possible (see the field “Condition” in the example below):

Screenshot from 2024-01-06 19-33-46.png

That’s it, just choose all your modules to be included in the loop. However, I would recommend also another way that’s easy even if you don’t feel Pythonist enough. As you already have your model defined, you could use the “Python editor” tab and you already have your Python script that needs just minimal changes. So if my model consisting of r.in.gdal and r.out.gdal produced this Python script:

import sys
import os
import atexit

from grass.script import parser, run_command

def cleanup():
pass

def main(options, flags):
run_command("r.in.gdal",
input="input_raster",
output="imported_raster",
memory=300,
offset=0,
num_digits=0)

run_command("r.out.gdal",
input="imported_raster",
output="raster_output",
format="GTiff",
overviews=0)

return 0

if __name__ == "__main__":
options, flags = parser()
atexit.register(cleanup)
sys.exit(main(options, flags))

I would just need to change it to the following. Please note the extra import glob, for loop, indentation change, and variables used in r.in.gdal[input] and r.out.gdal[output]. Naturally, you could also change the intermediate map names based on the file_path variable if you wish so:

import sys
import os
import atexit
import glob

from grass.script import parser, run_command

def cleanup():
pass

def main(options, flags):
for file_path in glob.glob('your_data_path/*'):
run_command("r.in.gdal",
input=file_path,
output="imported_raster",
memory=300,
offset=0,
num_digits=0)

run_command("r.out.gdal",
input="imported_raster",
output=os.path.join("your_output_dir", os.path.basename(file_path)),
format="GTiff",
overviews=0)

return 0

if __name__ == "__main__":
options, flags = parser()
atexit.register(cleanup)
sys.exit(main(options, flags))

Cheers.

so 6. 1. 2024 v 11:17 odesílatel Victor Lundström via grass-user <grass-user@lists.osgeo.org> napsal:

Hi everyone,

I’m trying the start getting familiar with the graphical modeler as I have a lot of raster operations that I need to automate.
I have 456 raster layers that I (1) want to interpolate using v.surf.bspline, and then (2) export to six different folders, 76 rasters for each.

I have designed the workflow in the graphical modeler using just one layer to see that everything works the way that it should, and it does!
However, I can’t for the life of me figure out how to set up a loop so that it can iterate through all the remaining layers. I’m trying my best to
read the manual, but since all of this is new to me, I can’t figure out (1) how to add several raster layers. Whenever I use the “add data to model” I can
only add one layer from the drop down list. I also don’t know if I need two loops, one for interpolation and then one for the raster export.

I have attached a screenshot that outlines my model. I hope you’ll be able to guide me in the right direction!

Best,
Victor