GRDMATH(1)

NAME
grdmath - Reverse Polish Notation calculator for grd files

SYNOPSIS
grdmath [ -F ] [ -Ixinc[unit][=|+][/yinc[unit][=|+]] ] [ -Rwest/east/south/north[r] ] [ -V ] [ -bi[s|S|d|D][ncol] ] [ -fcolinfo ] operand [ operand ] OPERATOR [ operand ] OPERATOR ... = outgrdfile

DESCRIPTION
grdmath will perform operations like add, subtract, multiply, and divide on one or more grd files or constants using Reverse Polish Notation (RPN) syntax (e.g., Hewlett-Packard calculator-style). Arbitrarily complicated expressions may therefore be evaluated; the final result is written to an output grd file. When two grd files are on the stack, each element in file A is modified by the corresponding element in file B. However, some operators only require one operand (see below). If no grdfiles are used in the expression then options -R, -I must be set (and optionally -F). The expression = outgrdfile can occur as many times as the depth of the stack allows.

operand
    If operand can be opened as a file it will be read as a grd file. If not a file, it is interpreted as a numerical constant or a special symbol (see below).

outgrdfile is a 2-D grd file that will hold the final result.

OPERATORS
    Choose among the following operators:
    Operator    n_args    Returns

    ABS    1    abs (A).
    ACOS    1    acos (A).
    ACOSH    1    acosh (A).
    ADD    2    A + B.
    AND    2    NaN if A and B == NaN, B if A == NaN, else A.
    ASIN    1    asin (A).
    ASINH    1    asinh (A).
    ATAN    1    atan (A).
    ATAN2    2    atan2 (A, B).
    ATANH    1    atanh (A).
    BEI    1    bei (A).
    BER    1    ber (A).
    CAZ    2    Cartesian azimuth from grid nodes to stack x,y.
    CDIST    2    Cartesian distance between grid nodes and stack x,y.
    CEIL    1    ceil (A) (smallest integer >= A).
    CHICRIT    2    Critical value for chi-squared-distribution, with alpha = A and n = B.
    CHIDIST    2    chi-squared-distribution P(chi2,nn), with chi2 = A and n = B.
    CORRCOEFF    2    Correlation coefficient r(A, B).
    COS    1    cos (A) (A in radians).
    COSD    1    cos (A) (A in degrees).
    COSH    1    cosh (A).
    CPOISS    2    Cumulative Poisson distribution F(x,lambda), with x = A and lambda = B.
    CURV    1    Curvature of A (Laplacian).
    D2DX2    1    d^2(A)/dx^2 2nd derivative.
    D2DY2    1    d^2(A)/dy^2 2nd derivative.
    D2R    1    Converts Degrees to Radians.
    DDX    1    d(A)/dx 1st derivative.
    DDY    1    d(A)/dy 1st derivative.
    DILOG    1    dilog (A).
    DIV    2    A / B.
    DUP    1    Places duplicate of A on the stack.
    ERF    1    Error function erf (A).
    ERFC    1    Complementary Error function erfc (A).
    ERFINV    1    Inverse error function of A.
    EQ    2    1 if A == B, else 0.
    EXCH    2    Exchanges A and B on the stack.
    EXP    1    exp (A).
    EXTREMA    1    Local Extrema: +2/-2 is max/min, +1/-1 is saddle with max/min in x, 0 elsewhere.
    FCRIT    3    Critical value for F-distribution, with alpha = A, n1 = B, and n2 = C.
    FDIST    3    F-distribution Q(F,n1,n2), with F = A, n1 = B, and n2 = C.
    FLIPLR    1    Reverse order of values in each row.
    FLIPUD    1    Reverse order of values in each column.
    FLOOR    1    floor (A) (greatest integer <= A).
    FMOD    2    A % B (remainder).
    GE    2    1 if A >= B, else 0.
    GT    2    1 if A > B, else 0.
    HYPOT    2    hypot (A, B) = sqrt (A*A + B*B).
    I0    1    Modified Bessel function of A (1st kind, order 0).
    I1    1    Modified Bessel function of A (1st kind, order 1).
    IN    2    Modified Bessel function of A (1st kind, order B).
    INRANGE    3    1 if B <= A <= C, else 0.
    INSIDE    1    1 when inside or on polygon(s) in A, else 0.
    INV    1    1 / A.
    ISNAN    1    1 if A == NaN, else 0.
    J0    1    Bessel function of A (1st kind, order 0).
    J1    1    Bessel function of A (1st kind, order 1).
    JN    2    Bessel function of A (1st kind, order B).
    K0    1    Modified Kelvin function of A (2nd kind, order 0).
    K1    1    Modified Bessel function of A (2nd kind, order 1).
    KN    2    Modified Bessel function of A (2nd kind, order B).
    KEI    1    kei (A).
    KER    1    ker (A).
    LDIST    1    Compute distance from lines in multi-segment ASCII file A.
    LE    2    1 if A <= B, else 0.
    LMSSCL    1    LMS scale estimate (LMS STD) of A.
    LOG    1    log (A) (natural log).
    LOG10    1    log10 (A) (base 10).
    LOG1P    1    log (1+A) (accurate for small A).
    LOG2    1    log2 (A) (base 2).
    LOWER    1    The lowest (minimum) value of A.
    LRAND    2    Laplace random noise with mean A and std. deviation B.
    LT    2    1 if A < B, else 0.
    MAD    1    Median Absolute Deviation (L1 STD) of A.
    MAX    2    Maximum of A and B.
    MEAN    1    Mean value of A.
    MED    1    Median value of A.
    MIN    2    Minimum of A and B.
    MODE    1    Mode value (Least Median of Squares) of A.
    MUL    2    A * B.
    NAN    2    NaN if A == B, else A.
    NEG    1    -A.
    NEQ    2    1 if A != B, else 0.
    NRAND    2    Normal, random values with mean A and std. deviation B.
    OR    2    NaN if A or B == NaN, else A.
    PDIST    1    Compute distance from points in ASCII file A.
    PLM    3    Associated Legendre polynomial P(-1<A<+1) degree B order C.
    POP    1    Delete top element from the stack.
    POW    2    A ^ B.
    R2    2    R2 = A^2 + B^2.
    R2D    1    Convert Radians to Degrees.
    RAND    2    Uniform random values between A and B.
    RINT    1    rint (A) (nearest integer).
    ROTX    2    Rotate A by the (constant) shift B in x-direction.
    ROTY    2    Rotate A by the (constant) shift B in y-direction.
    SAZ    2    Sperhical azimuth from grid nodes to stack x,y.
    SDIST    2    Spherical (Great circle) distance (in degrees) between grid nodes and stack lon,lat (A, B).
    SIGN    1    sign (+1 or -1) of A.
    SIN    1    sin (A) (A in radians).
    SINC    1    sinc (A) (sin (pi*A)/(pi*A)).
    SIND    1    sin (A) (A in degrees).
    SINH    1    sinh (A).
    SQRT    1    sqrt (A).
    STD    1    Standard deviation of A.
    STEP    1    Heaviside step function: H(A).
    STEPX    1    Heaviside step function in x: H(x-A).
    STEPY    1    Heaviside step function in y: H(y-A).
    SUB    2    A - B.
    TAN    1    tan (A) (A in radians).
    TAND    1    tan (A) (A in degrees).
    TANH    1    tanh (A).
    TCRIT    2    Critical value for Student's t-distribution, with alpha = A and n = B.
    TDIST    2    Student's t-distribution A(t,n), with t = A, and n = B.
    TN    2    Chebyshev polynomial Tn(-1<t<+1,n), with t = A, and n = B.
    UPPER    1    The highest (maximum) value of A.
    XOR    2    B if A == NaN, else A.
    Y0    1    Bessel function of A (2nd kind, order 0).
    Y1    1    Bessel function of A (2nd kind, order 1).
    YLM    2    Re and Im normalized surface harmonics (degree A, order B).
    YN    2    Bessel function of A (2nd kind, order B).
    ZCRIT    1    Critical value for the normal-distribution, with alpha = A.
    ZDIST    1    Cumulative normal-distribution C(x), with x = A.

SYMBOLS
    The following symbols have special meaning:

    PI    3.1415926...
    E     2.7182818...
    X     Grid with x-coordinates
    Y     Grid with y-coordinates
    Xn     Grid with normalized [-1 to +1] x-coordinates
    Yn     Grid with normalized [-1 to +1] y-coordinates

OPTIONS

-F  Select pixel registration (used with -R, -I). [Default is grid registration].

-I  x_inc [and optionally y_inc] is the grid spacing. Append m to indicate minutes or c to indicate seconds. If one of the units e, k, i, or n is appended instead, the increment will be assumed to be in meter, km, miles, or nautical miles, respectively, and will be converted to the equivalent degrees longitude at the middle latitude of the region (the conversion depends on ELLIPSOID). If /y_inc is given but set to 0 it will be reset equal to x_inc; otherwise it will be converted to degrees latitude. If = is appended then the corresponding max x (east) or y (north) may be slightly adjusted to fit exactly the given increment [by default the increment may be adjusted slightly to fit the given domain]. Finally, instead of giving an increment you may specify the number of nodes desired by appending + to the supplied increment; the increment is then recalculated from the number of nodes and the domain. The resulting increment value depends on whether you have selected a gridline-registered or pixel- registered grid; see Appendix B for details.

-R  xmin, xmax, ymin, and ymax specify the Region of interest. For geographic regions, these limits correspond to west, east, south, and north and you may specify them in decimal degrees or in [+-]dd:mm[:ss.xxx][W|E|S|N] format. Append r if lower left and upper right map coordinates are given instead of wesn. The two shorthands -Rg -Rd stand for global domain (0/360 or -180/+180 in longitude respectively, with -90/+90 in latitude). For calendar time coordinates you may either give relative time (relative to the selected TIME_EPOCH and in the selected TIME_UNIT; append t to -JX|x), or absolute time of the form [date]T[clock] (append T to -JX|x). At least one of date and clock must be present; the T is always required. The date string must be of the form [-]yyyy[-mm[-dd]] (Gregorian calendar) or yyyy[-Www[-d]] (ISO week calendar), while the clock string must be of the form hh:mm:ss[.xxx]. The use of delimiters and their type and positions must be as indicated (however, input/output and plotting formats are flexible).

-V  Selects verbose mode, which will send progress reports to stderr [Default runs "silently"].

-f  Special formatting of input and output columns (time or geographical data). Specify i(nput) or o(utput) [Default is both input and output]. Give one or more columns (or column ranges) separated by commas. Append T (Absolute calendar time), t (time relative to chosen TIME_EPOCH), x (longitude), y (latitude), or f (floating point) to each column or column range item. Shorthand -f[i|o]g means -f[i|o]0x,1y (geographic coordinates).

BEWARE
(1) The operator SDIST calculates spherical distances bewteen the (lon, lat) point on the stack and all node positions in the grid. The grid domain and the (lon, lat) point are expected to be in degrees.
(2) The operator YLM calculates the fully normalized spherical harmonics for degree L and order M for all positions in the grid, which is assumed to be in degrees. YLM returns two grids, the Real (cosine) and Imaginary (sine) component of the complex spherical harmonic. Use the POP operator (and EXCH) to get rid of one of them, or save both by giving two consequtive = file.grd calls.
(3) The operator PLM calculates the associated Legendre polynomial of degree L and order M, and its argument is the cosine of the colatitude which must satisfy -1 <= x <= +1. Unlike YLM, PLM is not normalized.
(4) All the derivatives are based on central finite differences, with natural boundary conditions.

GRID VALUES PRECISION
Regardless of the precision of the input data, GMT programs that create gridded files will internally hold the grids in 4-byte floating point arrays. This is done to conserve memory and futhermore most if not all real data can be stored using 4-byte floating point values. Data with higher precision (i.e., double precision values) will loose that precision once GMT operates on the grid or writes out new grids. To limit loss of precision when processing data you should always consider normalizing the data prior to processing.

EXAMPLES
To take log10 of the average of 2 files, use

grdmath file1.grd file2.grd ADD 0.5 MUL LOG10 = file3.grd

Given the file ages.grd, which holds seafloor ages in m.y., use the relation depth(in m) = 2500 + 350 * sqrt (age) to estimate normal seafloor depths:

grdmath ages.grd SQRT 350 MUL 2500 ADD = depths.grd

To find the angle a (in degrees) of the largest principal stress from the stress tensor given by the three files s_xx.grd s_yy.grd, and s_xy.grd from the relation tan (2*a) = 2 * s_xy / (s_xx - s_yy), use

grdmath 2 s_xy.grd MUL s_xx.grd s_yy.grd SUB DIV ATAN2 2 DIV = direction.grd

To calculate the fully normalized spherical harmonic of degree 8 and order 4 on a 1 by 1 degree world map, using the real amplitude 0.4 and the imaginary amplitude 1.1:

grdmath -R0/360/-90/90 -I1 8 4 YML 1.1 MUL EXCH 0.4 MUL ADD = harm.grd

To extract the locations of local maxima that exceed 100 mGal in the file faa.grd:

grdmath faa.grd DUP EXTREMA 2 EQ MUL DUP 100 GT MUL 0 NAN = z.grd
grd2xyz z.grd -S > max.xyz

REMARKS
(1) Files that have the same names as some operators, e.g., ADD, SIGN, =, etc. should be identified by prepending the current directory (i.e., ./LOG).
(2) Piping of files are not allowed.
(3) The stack depth limit is hard-wired to 100.
(4) All functions expecting a positive radius (e.g., LOG, KEI, etc.) are passed the absolute value of their argument.

REFERENCES
Abramowitz, M., and I. A. Stegun, 1964, Handbook of Mathematical Functions, Applied Mathematics Series, vol. 55, Dover, New York.
Press, W. H., S. A. Teukolsky, W. T. Vetterling, B. P. Flannery, 1992, Numerical Recipes, 2nd edition, Cambridge Univ., New York.

SEE ALSO
GMT(1), gmtmath(1), grd2xyz(1), grdedit(1), grdinfo(1), xyz2grd(1)