2

I am doing threshold classification of NDVI rasters. One equation looks like that:

r.mapcalc 'prog_r43_n65=if(ndvi_r43_n65>=0.6,1,2)'

Now I want to make a loop for more than 2000 rasters, so I created this:

for red in {43..64}
do
for nir in {65..193}
do
progi="ndvi_r"$red"_n"$nir    #I have files named ex. ndvi_r43_n65
progowa="prog_r"$red"_n"$nir
r.mapcalc '"$progowa"=if("$progi">=0.6,1,2)'
done
done

And here is the problem.. It does not read "progi" as a variable.. Does somebody guess why?

  • How do you invoke the bash script? What system are you using? Try an echo ... in the script with the same argument as r.mapcalc or the an echo $progi $progwa and see if the variables are being assigned as expected. – Dave X Jan 14 '18 at 16:48
4

I made it finally.. One simple thing: ' despite " :

for red in {43..64}
do
for nir in {65..193}
do
r.mapcalc 'prog_r'$red'_nir'$nir'=if(ndvi_r'$red'_n'$nir'>=0.6,1,2)'
done
done
  • Getting the quoteing right is sometimes hard. – Dave X Jan 14 '18 at 16:49
0

It is the single quotes around the argument to r.mapcalc, they inhibit the shell expansion of the $ variables. maybe try this:

r.mapcalc "'$progowa=if($progi>=0.6,1,2)'"

It should expand the shell variables and pass the single quotes to r.mapcalc.

  • Does not work unfortunately. – AngelaG Jan 13 '18 at 7:27
  • Bummer. Seemed to work on my command line, but within scripts, there can be another level of parsing quotes. – Dave X Jan 15 '18 at 5:04
0

Perhaps this:

r.mapcalc "$progowa = if(${progi}>=0.6,1,2)"

i.e. space around the '=' and enclose the variable in in {}

  • This one as well, does not work. – AngelaG Jan 13 '18 at 7:27
  • This might not work because the bash interpreter uses up the quotes and passes this as three arguments to mapcalc.r, and then redirects the output to some awkward file name. – Dave X Jan 15 '18 at 5:02

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.