r3.mapcalc

NAME

r3.mapcalc - G3D grid volume data calculator.
(GRASS 3D Program)

SYNOPSIS

r3.mapcalc [result=expression]

DESCRIPTION

r3.mapcalc performs arithmetic on 3D grid volume data. New 3D grids can be created which are arithmetic expressions involving existing 3D grids,  floating point constants, and functions.

PROGRAM USE

If used without command line arguments, r3.mapcalc will read its input, one line at a time, from standard input (which is the keyboard, unless 
r.directed from a file or across a pipe). Otherwise, the expression on the command line is evaluated. r3.mapcalc expects its input to have the form:

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.

OPERATORS AND ORDER OF PRECEDENCE

The following operators are supported:
     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.

3D GRID NAMES

Anything in the expression which is not a number, operator, or function name is taken to be a 3D grid name. Examples:
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 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.)

THE NEIGHBORHOOD MODIFIER

3D grids are data base files stored in voxel format, i.e., three-dimensional matrices of float/double values. In r3.mapcalc, 3D grids may be followed by a neighborhood modifier that specifies a relative offset from the current cell being evaluated. The format is map[r,c,d], where r is the row offset, c is the column offset and d is the depth offset. For example, map[1,2,3] refers to the cell one row below, two columns to the right and 3 levels below of the current cell, map[-3,-2,-1] refers to the cell three rows above, two columns to the left and one level below of the current cell, and map[0,1,0] refers to the cell one column to the right of the current cell. This syntax permits the development of neighborhood-type filters within a single 3D grid or across multiple 3D grids.
 

FUNCTIONS

The functions currently supported are listed in the table below.
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 value

EXAMPLES

To compute the average of two 3D grids a and b:
        ave = (a + b)/2
To form a weighted average:
        ave = (5*a + 3*b)/8.0
To produce a binary representation of 3D grid a so that category 0 remains 0 and all other categories become 1:
        mask = a/a
This could also be accomplished by:
        mask = if(a)
To mask 3D grid b by 3D grid a:
        result = if(a,b)

REGION/MASK

The user must be aware of the current geographic region and current mask settings when using r3.mapcalc. All 3D grids are read into the current geographic region masked by the current mask. If it is desired to modify an existing 3D grid without involving other 3D grids, the geographic region should be set to agree with the cell header for the 3D grid. For example, suppose it is determined that the volume 3D grid must have each category value increased by 10 meters. The following expression is legal and will do the job:
        new_volume = volume + 10
Since 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.

NOTES

Extra care must be taken if the expression is given on the command line. Some characters have special meaning to the UNIX shell. These include, among others:

* ( ) > & |

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.

BUGS

Continuation lines must end with a \ and have NO trailing white space (blanks or tabs). If the user does leave white space at the end of continuation lines, the error messages produced by r3.mapcalc will be meaningless and the equation will not work as the user intended.

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.
 

SEE ALSO

r.mapcalc

AUTHOR

Tomas Paudits & Jaro Hofierka, funded by GeoModel s.r.o., Slovakia
tpaudits@mailbox.sk, hofi@geomodel.sk