I want to crop a raster based on another one, inside a loop. I created a mask (MASK
) and then cropped the old raster (old
), creating a new raster (new
).
r.mask vector=EU@PERMANENT #my mask
r.mapcalc "new = old"
For a single raster, it works. However, I have many old rasters, and I want to create a loop. I tried this:
for i in `cat /myPath/myRasters.txt | tr -d '\r'`
do
echo SET REGION
g.region vector=EU@PERMANENT
echo CREATE A MASK
r.mask --o vector=EU@PERMANENT
echo CROP $i RASTER
r.mapcalc "$i'_new' = $i'_old'"
r.mask -r
done
I can see the problem is in the syntax used, as I get this ERROR message after echo CROP $i RASTER
:
syntax error, unexpected STRING, expecting '('
I tried also to use r.mapcalc
with:
"$i'_new' = $i'_old'"
'$i'_new' = $i'_old''
"${i}'_new' = ${i}'_old'"
"$i'_new' = if(MASK,$i'_old')"
this last one following r.mapcalc
manual page ( https://grass.osgeo.org/grass82/manuals/r.mapcalc.html ), where it says
To mask raster map layer b by raster map layer a:
result = if(a,b)
and this: https://grasswiki.osgeo.org/wiki/Cropping_maps
But nothing works. Previous similar questions (e.g., Loop using variable - r.mapcalc GRASS GIS ) didnt' help. I am using GRASS 7.8.6 on Ubuntu 18.04.6 (from remote, I have Windows 10).
/myPath/myRasters.txt
??vector=EU
, correct? The filemyRasters
contains, in each row, the name of each raster as it is loaded in my current mapset (e.g.,raster_1
,raster_2
, and so on), while the file itself would beraster_1.tiff
(orraster_1.img
), and so on. The filemyRasters.txt
has correct endings (I checked it withcat -v
) and has a last blank row.echo CROP $i RASTER
-->echo "CROP $i RASTER"
.r.mapcalc "$i'_new' = $i'_old'"
-->r.mapcalc "${i}_new = ${i}_old"
(the curly braces are enough, no need for single quotes).