result=expression
where result is the name of a 3D grid to contain the result of the calculation and expression is any legal arithmetic expression involving existing 3D grid, floating point constants, and functions known to the calculator. Parentheses are allowed in the expression and may be nested to any depth. result will be created in the user's current mapset.
The formula entered to r3.mapcalc by the user is recorded both in the result grid title (which appears in the category file for result) and in the history file for result.
Some characters have special meaning to the command shell. If the user is entering input to r.mapcalc on the command line, expressions should be enclosed within single quotes. See NOTES, below.
Operator Meaning Type Precedence ----------------------------------------------------------------------- % modulus (remainder upon division) Arithmetic 4 / division Arithmetic 4 * multiplication Arithmetic 4 + addition Arithmetic 3 - subtraction Arithmetic 3 == equal Logical 2 != not equal Logical 2 > greater than Logical 2 >= greater than or equal Logical 2 < less than Logical 2 <= less than or equal Logical 2 && and Logical 1 || or Logical 1
The operators are applied from left to right, with those of higher precedence applied before those with lower precedence. Division by 0 and modulus by 0 are acceptable and give a 0 result. The logical operators give a 1 result if the comparison is true, 0 otherwise.
Also
Quotes are not required unless the 3D grid names look like numbers or contain operators, OR unless the program is run non-interactively. Examples given here assume the program is run interactively. See NOTES, below.
r3.mapcalc will look for the 3D grids according to the user's current mapset search path. It is possible to override the search path and specify the mapset from which to select the 3D grid. This is done by specifying the 3D grid name in the form:
function description --------------------------------------------------------------------------- abs(x) return absolute value of x atan(x) inverse tangent of x (result is in degrees) cos(x) cosine of x (x is in degrees) col() return current column depth() return current depth eval([x,y,...,]z) evaluate values of listed expr, pass results to z exp(x) exponential function of x exp(x,y) x to the power y ewres() east-west resolution from WIND3D 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 isnull(x) 1 if x not zero, 0 otherwise log(x) natural log of x log(x,b) log of x base b max(x,y[,z...]) largest value of those listed median(x,y[,z...]) median value of those listed min(x,y[,z...]) smallest value of those listed mode(x,y[,z...]) most frequently value of those listed null() return 0 nsres() north-south resolution from WIND3D rand(x,y) random value between x and y round(x) round x row() return current row sin(x) sine of x (x is in degrees) sqrt(x) square root of x tan(x) tangent of x (x is in degrees) tbres() top-bottom resolution from WIND3D x() return current x value y() return current y value z() return current z valueNote, that the row(), col() and depth() indexing starts with 1.
ave = (a + b)/2To form a weighted average:
ave = (5*a + 3*b)/8.0To produce a binary representation of 3D grid a so that category 0 remains 0 and all other categories become 1:
mask = a/aThis could also be accomplished by:
mask = if(a)To mask 3D grid b by 3D grid a:
result = if(a,b)
new_volume = volume + 10Since a category value of 0 is used in GRASS for locations which do not exist in 3D grid, the new 3D grid will contain the category value 10 in the locations that did not exist in the original volume. Therefore, in this example, it is essential that the boundaries of the geographic region be set to agree with the cell header.
However, if there is a current mask, then the resultant 3D grid is masked when it is written; i.e., 0 category values in the mask force zero values in the output.
* ( ) > & |
It is advisable to put single quotes around the expression; e.g.:
result = 'elevation * 2'Without the quotes, the *, which has special meaning to the UNIX shell, would be altered and r3.mapcalc would see something other than the *.
If the input comes directly from the keyboard and the result 3D grid exists, the user will be asked if it can be overwritten. Otherwise, the result 3D grid will automatically be overwritten if it exists.
Quoting result is not allowed. However, it is never necessary to quote result since it is always taken to be a 3D grid name.
For formulas that the user enters from standard input (rather than from the command line), a line continuation feature now exists. If the user adds \e to the end of an input line, r3.mapcalc assumes that the formula being entered by the user continues on to the next input line. There is no limit to the possible number of input lines or to the length of a formula.
If the r3.mapcalc formula entered by the user is very long, the map title will contain only some of it, but most (if not all) of the formula will be placed into the history file for the result map.
When the user enters input to r3.mapcalc non-interactively on the command line, the program will not warn the user not to overwrite existing 3D grids. Users should therefore take care to assign program outputs 3D grid file names that do not yet exist in their current mapsets.
Error messages produced by r3.mapcalc are almost useless. In future, r3.mapcalc should make some attempt to point the user to the offending section of the equation, e.g.:
x = a * b ++ c ERROR: somewhere in line 1: ... b ++ c ...Currently, there is no comment mechanism in r3.mapcalc. Perhaps adding a capability that would cause the entire line to be ignored when the user inserted a # at the start of a line as if it were not present, would do the trick.
The function should require the user to type "end" or "exit" instead
of simply a blank line. This would make separation of multiple scripts
separable by white space.
Last changed: $Date: 2002/01/25 05:45:35 $