I have MODIS NDVI images, which needs to be masked with VI_Quality. I am using GRASS software and executing GRASS commands using shell scripts. I need to perform raster calculation (multiplication) for NDVI images with the VI_Quality. I want to multiply ith NDVI image with the ith image of VI_Quality. I have used the following codes :

for file in $NDVIFILES;
r.mapcalc "$file = $NDVIFILES * $QAFILES;

for file in $NDVIFILES;
r.mapcalc "file $((i++)) = NDVIFILES $((i++)) * QAFILES $((i++))";

I am unable to do this raster multiplication in a loop. Please let me know the correct syntax to do this operation.


  • Please add some clarifications: What do you get if you type: echo $NDVIFILES ? What about: echo $QAFILES ? And what do you get if you run, for example: sed -n "3p" $NDVIFILES ? – Micha Apr 6 '14 at 6:37
  • NDVIFILES=$(g.mlist -p type=rast pattern=*NDVI mapset=shenkottah); QAFILES=$(g.mlist -p type=rast pattern=Qu* mapset=shenkottah); So when I use the command echo $NDVIFILES, I get the following o/p: rast files available in mapset <shenkottah>: A2013001h25v08NDVI A2013097h25v08NDVI A2013193h25v08NDVI A2013289h25v08NDVI A2013017h25v08NDVI A2013113h25v08NDVI A2013209h25v08NDVI A2013305h25v08NDVI; Similarly I get o/p for echo $QAFILES; sed -n "3p" $NDVIFILES is not working. Please let me know if I need to clear anything. Thanks – Karthik K Apr 7 '14 at 6:42

Here's what I suggest:

g.mlist --q type=rast pattern-"*NDVI" mapset=shenkottah >> NDVIFILES

and a similar expression for the QAFILES This will leave you with two files on your disk, each containing a list of the raster map names. To verify, test with the sed expression:

sed -n "4p" NDVIFILES

Then you should be able to run a loop to read the raster names and do your mapcalc, something like:

for i in `seq 1 22`; do \
NDVI=`sed -n "${i}p" NDVIFILES`;
QA=`sed -n "${i}p" QAFILES`;
r.mapcalc new_${i} = $NDVI * $QA;
  • Thanks a ton, Micha! This code worked!! --With regards, Karthik K. – Karthik K Apr 8 '14 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.