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 -------------------------------------------------------------- - negation Arithmetic 12 ~ one's complement Bitwise 12 ! not Logical 12 ^ exponentiation Arithmetic 11 % modulus Arithmetic 10 / division Arithmetic 10 * multiplication Arithmetic 10 + addition Arithmetic 9 - subtraction Arithmetic 9 << left shift Bitwise 8 >> right shift Bitwise 8 >>> right shift (unsigned) Bitwise 8 > greater than Logical 7 >= greater than or equal Logical 7 < less than Logical 7 <= less than or equal Logical 7 == equal Logical 6 != not equal Logical 6 & bitwise and Bitwise 5 | bitwise or Bitwise 4 && logical and Logical 3 &&& logical and[1] Logical 3 || logical or Logical 2 ||| logical or[1] Logical 2 ?: conditional Logical 1
[1] The &&& and ||| operators handle null values differently to other operators. See the section entitled NULL support below for more details.
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 NULL result. The logical operators give a 1 result if the comparison is true, 0 otherwise.
volume x3 3d.his
Most GRASS raster map layers and 3D grids meet this naming convention. However, if a 3D grid has a name which conflicts with the above rule, it should be quoted. For example, the expression
x = a-b
would be interpreted as: x equals a minus b, whereas
x = "a-b"
would be interpreted as: x equals the 3D grid named a-b
Also
x = 3107
would create x filled with the number 3107, while
x = "3107"
would copy the 3D grid 3107 to the 3D grid x.
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:
name@mapset
For example, the following is a legal expression:
result = x@PERMANENT / y@SOILS
The mapset specified does not have to be in the mapset search path. (This method of overriding the mapset search path is common to all GRASS commands, not just r3.mapcalc.)
function description type --------------------------------------------------------------------------- abs(x) return absolute value of x * acos(x) inverse cosine of x (result is in degrees) F asin(x) inverse sine of x (result is in degrees) F atan(x) inverse tangent of x (result is in degrees) F atan(x,y) inverse tangent of y/x (result is in degrees) F cos(x) cosine of x (x is in degrees) F double(x) convert x to double-precision floating point F eval([x,y,...,]z) evaluate values of listed expr, pass results to z exp(x) exponential function of x F exp(x,y) x to the power y F float(x) convert x to single-precision floating point F graph(x,x1,y1[x2,y2..]) convert the x to a y based on points in a graph F 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 int(x) convert x to integer [ truncates ] I isnull(x) check if x = NULL log(x) natural log of x F log(x,b) log of x base b F 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...]) mode value of those listed * not(x) 1 if x is zero, 0 otherwise pow(x,y) x to the power y * rand(a,b) random value x : a <= x < b round(x) round x to nearest integer I sin(x) sine of x (x is in degrees) F sqrt(x) square root of x F tan(x) tangent of x (x is in degrees) F xor(x,y) exclusive-or (XOR) of x and y I
Internal variables: row() current row of moving window col() current col of moving window depth() return current depth x() current x-coordinate of moving window y() current y-coordinate of moving window z() return current z value ewres() current east-west resolution nsres() current north-south resolution tbres() current top-bottom resolution null() NULL value
2.3 12.0 12. .81
Note: If you calculate with integer numbers, the resulting map will be integer. If you want to get a float result, add the decimal point to integer number(s).
If you want floating point division, at least one of the arguments has to be a floating point value. Multiplying one of them by 1.0 will produce a floating-point result, as will using float():
r.mapcalc "ndvi=float(lsat.4 - lsat.3) / (lsat.4 + lsat.3)"
x &&& false == false false &&& x == false x ||| true == true true ||| x == true
if(x) NULL if x is NULL; 0 if x is zero; 1 otherwise if(x,a) NULL if x is NULL; a if x is non-zero; 0 otherwise if(x,a,b) NULL if x is NULL; a if x is non-zero; b otherwise if(x,n,z,p) NULL if x is NULL; n if x is negative; z if x is zero; p if x is positive
Examples: log(-2) sqrt(-2) pow(a,b) where a is negative and b is not an integer
NULL support: Please note that any math performed with NULL cells always results in a NULL value for these cells. If you want to replace a NULL cell on-the-fly, use the isnull() test function in a if-statement.
Example: The users wants the NULL-valued cells to be treated like zeros. To add maps A and B (where B contains NULLs) to get a map C the user can use a construction like:
C=A + if(isnull(B),0,B)
NULL and conditions:
For the one argument form:
if(x) = NULL if x is NULL if(x) = 0 if x = 0 if(x) = 1 otherwise (i.e. x is neither NULL nor 0).
For the two argument form:
if(x,a) = NULL if x is NULL if(x,a) = 0 if x = 0 if(x,a) = a otherwise (i.e. x is neither NULL nor 0).
For the three argument form:
if(x,a,b) = NULL if x is NULL if(x,a,b) = b if x = 0 if(x,a,b) = a otherwise (i.e. x is neither NULL nor 0).
For the four argument form:
if(x,a,b,c) = NULL if x is NULL if(x,a,b,c) = a if x > 0 if(x,a,b,c) = b if x = 0 if(x,a,b,c) = c if x < 0
All forms of if() return NULL if the first argument is NULL. The 2, 3 and 4 argument forms of if() return NULL if the "selected" argument is NULL, e.g.:
if(0,a,b) = b regardless of whether a is NULL if(1,a,b) = a regardless of whether b is NULL
Note: The user cannot test for NULL using the == operator, as that
returns NULL if either or both arguments are NULL, i.e. if x and y are
both NULL, then "x == y" and "x != y" are both NULL rather than 1 and
0 respectively.
The behaviour makes sense if the user considers NULL as representing an
unknown quantity. E.g. if x and y are both unknown, then the values of
"x == y" and "x != y" are also unknown; if they both have unknown
values, the user doesn't know whether or not they both have the same value.
ave = (a + b)/2
ave = (5*a + 3*b)/8.0
mask = a != 0
mask = if(a)
result = if(a,b)
newmap = if(map<5, null(), 5)
newmap = graph(map, 1,10, 2,25, 3,50)
0, 10 1, 10, 1.5, 16.5 2.9, 47.5 4, 50 100, 50
* ( ) > & |
It is advisable to put single quotes around the expression; e.g.:
result = 'elevation * 2'
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.
The environment variable GRASS_RND_SEED is read to initialise the random number generator.
Error messages produced by r.mapcalc are almost useless. In future, r.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.
r.mapcalc does not print a warning in case of operations on NULL cells. It is left to the user to utilize the isnull() function.
Performing Map Calculations on GRASS Data: r.mapcalc Program Tutorial, by Marji Larson, Michael Shapiro and Scott Tweddale, U.S. Army Construction Engineering Research Laboratory (December 1991)
Last changed: $Date: 2010-01-20 06:49:17 -0800 (Wed, 20 Jan 2010) $
Main index - raster3D index - Full index
© 1999-2009 GRASS Development Team