r.mapcalc

Hello GRASS programmers. A couple of questions and/or
comments looking for input about r.mapcalc
(which I can't seem to find documented in the man pages).
(I am trying to tidy up a r.mapcalc program which uses
8 temporary raster layers to calculate a single raster
which is a function of 3 raster layers. Seems just a tad
wasteful).

I gather that all variable names in a mapcalc "program"
refer to raster maps (there are no scalar variables).

With respect to the modulus operator (%), I am expecting
that the divisor must be an integer (or do both
operands need to be integer?).

Can logical operators be used in arithmetic statements?
( x = y * 3 && (z > 1.0) )?

There isn't by chance an else operator? Any kind of logical
flow control?

aTdHvAaNnKcSe

Gordon Haverland
goddard@gpu.ualberta.ca
ghaverla@freenet.edmonton.ab.ca

Hello GRASS programmers. A couple of questions and/or
comments looking for input about r.mapcalc

I gather that all variable names in a mapcalc "program"
refer to raster maps (there are no scalar variables).

Yes.

With respect to the modulus operator (%), I am expecting
that the divisor must be an integer (or do both
operands need to be integer?).

The best way to see how r.mapcalc works is to use numbers instead of map
layer names and to experiment. For example:

GRASS 4.1 > r.mapcalc junk = "7.9 % 3"

EXECUTING junk = ... 100%
CREATING SUPPORT FILES FOR junk
minimum value 2, maximum value 2

GRASS 4.1 > r.mapcalc junk = "7.9 % 3.1"

EXECUTING junk = ... 100%
CREATING SUPPORT FILES FOR junk
minimum value 2, maximum value 2

Thus, the answer is both operands may be real values.

Can logical operators be used in arithmetic statements?
( x = y * 3 && (z > 1.0) )?

GRASS 4.1 > r.mapcalc junk = "los6.2 && los4.4"

EXECUTING junk = ... 100%
CREATING SUPPORT FILES FOR junk
minimum value 0, maximum value 1

Yes. Result will be binary (0 or 1) depending on whether statement is
false or true.

There isn't by chance an else operator? Any kind of logical
flow control?

g.manual (grass4.1) of r.mapcalc:

if decision options:
       if(x) 1 if x not zero, 0 otherwise
       if(x,a) a if x not zero, 0 otherwise
       if(x,a,b) a if x not zero, b otherwise
       if(x,a,b,c) a if x > 0, b if x is zero, c if x < 0

Yes. For example:
if cost4 doesn't equal 0 then
  cost4
else if los6.2 doesn't equal 0 then
  2
else
  1
end if

would be:

GRASS 4.1 > r.mapcalc junk = "if(cost4,cost4,if(los6.2,1))"

EXECUTING junk = ... 100%
CREATING SUPPORT FILES FOR junk
minimum value 0, maximum value 180

In my own GRASS programming, I tend to keep program control within a
single r.mapcalc program simple and place several r.mapcalc programs
within a shell script, using while, if/then, and foreach statements for
more complex flow (it makes the debugging that much easier). Taking
advantage of unix can allow many interesting things to happen. For
example, the following program allows you to do a line of sight process
from the first seven points in a sites file (there might be a "simplier"
way but it's fun to string five unix commands on the same line):

cut here---------
#!/bin/csh -f
set temp = `g.tempfile pid=$$`
s.out.ascii sites=fromVis > $temp
set SITE = 1
r.mapcalc sumView = 0
while ( $SITE < 8 )
  eval set `cat $temp | sed -n '$SITE p' | awk '{print "fromE = " $1}' `
  eval set `cat $temp | sed -n '$SITE p' | awk '{print "fromN = " $2}' `
  r.los input=dtedSA output=losDTED$SITE coor=$fromE,$fromN max=4000
  r.mapcalc sumView = "sumView + if(losDTED$SITE )"
  @ SITE++
end
----------end of file

--
Charles Ehlschlaeger http://geo.swf.uc.edu/chuck/
Assistant Research Professor chuck@geo.swf.uc.edu
Department of Geography (131) work: 513-556-2849
University of Cincinnati fax: 513-556-3370
Cincinnati, OH 45221-0131

             "It's an e-mail message.
      It's supposssed to have spelling errors"