From the r.mapcalc manual, the 'eval' option is described to run more complex expressions. But I wonder how I apply the example below in a python script?
r.mapcalc << EOF
eval(elev_200 = elevation - 200, \
elev_5 = 5 * elevation, \
elev_p = pow(elev_5, 2))
elevation_result = (0.5 * elev_200) + 0.8 * elev_p
EOF
Somewhat related, if I have a large number of maps. For each map I need to compute a value. At the end, the resulting maps need to be summed up. One way is to run a loop, and use r.series to sum up the results. E.g.,
for i in xrange(len(IN)):
out = 'shi' + str(i)
out.append(out)
grass.mapcalc("$out = log($in)", out=out[i], in=in[i])
grass.run_command("r.series", output="endresult", input=out, method="sum")
This generates a lot of intermediate layers, which can potentially take up a lot of space on HD. To avoid that, I could do
grass.mapcalc("$out = 0", out=out)
for i in xrange(len(IN)):
grass.mapcalc("$out = $out + log($in)", out=out, in=in[i]), overwrite=True
An alternative is to construct one large expression along the lines of expression = "log(in[1]) + log(in[2] + ....". I assume this will be faster? It would result in a unwieldy large expression though, and I remember something about a limit to the length of the expression? So that is why I wonder if using the 'eval' option would be useful.
Any tips for the best approach?