.. index:: ! grdmath

*******
grdmath
*******

.. only:: not man

    grdmath - Reverse Polish Notation (RPN) calculator for grids (element by element)

Synopsis
--------

.. include:: common_SYN_OPTs.rst_

**grdmath**
[ |SYN_OPT-I| ]
[ **-M** ] [ **-N** ]
[ |SYN_OPT-R| ] [ |SYN_OPT-V| ]
[ |SYN_OPT-bi| ]
[ |SYN_OPT-f| ]
[ |SYN_OPT-i| ]
[ |SYN_OPT-h| ]
[ |SYN_OPT-i| ]
[ |SYN_OPT-n| ]
[ **-r** ] *operand* [ *operand* ] **OPERATOR** [ *operand* ]
**OPERATOR** ... **=** *outgrdfile*

|No-spaces|

Description
-----------

**grdmath** will perform operations like add, subtract, multiply, and
divide on one or more grid 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 grid file. Grid operations are
element-by-element, not matrix manipulations. Some operators only
require one operand (see below). If no grid files are used in the
expression then options **-R**, **-I** must be set (and optionally
**-r**). The expression **=** *outgrdfile* can occur as many times as
the depth of the stack allows in order to save intermediate results.
Complicated or frequently occurring expressions may be coded as a macro
for future use or stored and recalled via named memory locations.

Required Arguments
------------------

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

*outgrdfile*
    The name of a 2-D grid file that will hold the final result. (See
    GRID FILE FORMATS below).

Optional Arguments
------------------

.. include:: explain_-I.rst_

**-M**
    By default any derivatives calculated are in z_units/ x(or
    y)\_units. However, the user may choose this option to convert dx,dy
    in degrees of longitude,latitude into meters using a flat Earth
    approximation, so that gradients are in z_units/meter.

**-N**
    Turn off strict domain match checking when multiple grids are
    manipulated [Default will insist that each grid domain is within
    1e-4 \* grid_spacing of the domain of the first grid listed].

.. |Add_-R| unicode:: 0x20 .. just an invisible code
.. include:: explain_-R.rst_

.. |Add_-V| unicode:: 0x20 .. just an invisible code
.. include:: explain_-V.rst_

.. |Add_-bi| replace:: The binary input option
    only applies to the data files needed by operators **LDIST**,
    **PDIST**, and **INSIDE**.
.. include:: explain_-bi.rst_

.. |Add_-f| unicode:: 0x20 .. just an invisible code
.. include:: explain_-f.rst_

.. |Add_-g| unicode:: 0x20 .. just an invisible code
.. include:: explain_-g.rst_

.. |Add_-h| unicode:: 0x20 .. just an invisible code
.. include:: explain_-h.rst_

.. include:: explain_-icols.rst_

.. include:: explain_-n.rst_

.. |Add_nodereg| replace:: Only used with **-R** **-I**.
.. include:: explain_nodereg.rst_

.. include:: explain_help.rst_

Operators
---------

Choose among the following 161 operators. "args" are the number of input
and output arguments.

+---------------+-------+--------------------------------------------------------------------------------------------------------+
| Operator      | args  | Returns                                                                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ABS**       | 1 1   | abs (A)                                                                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ACOS**      | 1 1   | acos (A)                                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ACOSH**     | 1 1   | acosh (A)                                                                                              |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ACOT**      | 1 1   | acot (A)                                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ACSC**      | 1 1   | acsc (A)                                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ADD**       | 2 1   | A + B                                                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **AND**       | 2 1   | B if A == NaN, else A                                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ASEC**      | 1 1   | asec (A)                                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ASIN**      | 1 1   | asin (A)                                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ASINH**     | 1 1   | asinh (A)                                                                                              |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ATAN**      | 1 1   | atan (A)                                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ATAN2**     | 2 1   | atan2 (A, B)                                                                                           |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ATANH**     | 1 1   | atanh (A)                                                                                              |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **BEI**       | 1 1   | bei (A)                                                                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **BER**       | 1 1   | ber (A)                                                                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **BITAND**    | 2 1   | A & B (bitwise AND operator)                                                                           |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **BITLEFT**   | 2 1   | A << B (bitwise left-shift operator)                                                                   |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **BITNOT**    | 1 1   | ~A (bitwise NOT operator, i.e., return two's complement)                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **BITOR**     | 2 1   | A \| B (bitwise OR operator)                                                                           |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **BITRIGHT**  | 2 1   | A >> B (bitwise right-shift operator)                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **BITTEST**   | 2 1   | 1 if bit B of A is set, else 0 (bitwise TEST operator)                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **BITXOR**    | 2 1   | A ^ B (bitwise XOR operator)                                                                           |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CAZ**       | 2 1   | Cartesian azimuth from grid nodes to stack x,y (i.e., A, B)                                            |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CBAZ**      | 2 1   | Cartesian backazimuth from grid nodes to stack x,y (i.e., A, B)                                        |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CDIST**     | 2 1   | Cartesian distance between grid nodes and stack x,y (i.e., A, B)                                       |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CEIL**      | 1 1   | ceil (A) (smallest integer >= A)                                                                       |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CHICRIT**   | 2 1   | Critical value for chi-squared-distribution, with alpha = A and n = B                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CHIDIST**   | 2 1   | chi-squared-distribution P(chi2,n), with chi2 = A and n = B                                            |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CORRCOEFF** | 2 1   | Correlation coefficient r(A, B)                                                                        |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **COS**       | 1 1   | cos (A) (A in radians)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **COSD**      | 1 1   | cos (A) (A in degrees)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **COSH**      | 1 1   | cosh (A)                                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **COT**       | 1 1   | cot (A) (A in radians)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **COTD**      | 1 1   | cot (A) (A in degrees)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CPOISS**    | 2 1   | Cumulative Poisson distribution F(x,lambda), with x = A and lambda = B                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CSC**       | 1 1   | csc (A) (A in radians)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CSCD**      | 1 1   | csc (A) (A in degrees)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **CURV**      | 1 1   | Curvature of A (Laplacian)                                                                             |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **D2DX2**     | 1 1   | d^2(A)/dx^2 2nd derivative                                                                             |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **D2DY2**     | 1 1   | d^2(A)/dy^2 2nd derivative                                                                             |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **D2DXY**     | 1 1   | d^2(A)/dxdy 2nd derivative                                                                             |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **D2R**       | 1 1   | Converts Degrees to Radians                                                                            |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **DDX**       | 1 1   | d(A)/dx Central 1st derivative                                                                         |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **DDY**       | 1 1   | d(A)/dy Central 1st derivative                                                                         |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **DEG2KM**    | 1 1   | Converts Spherical Degrees to Kilometers                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **DILOG**     | 1 1   | dilog (A)                                                                                              |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **DIV**       | 2 1   | A / B                                                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **DUP**       | 1 2   | Places duplicate of A on the stack                                                                     |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ERF**       | 1 1   | Error function erf (A)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ERFC**      | 1 1   | Complementary Error function erfc (A)                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **EQ**        | 2 1   | 1 if A == B, else 0                                                                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ERFINV**    | 1 1   | Inverse error function of A                                                                            |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **EXCH**      | 2 2   | Exchanges A and B on the stack                                                                         |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **EXP**       | 1 1   | exp (A)                                                                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **FACT**      | 1 1   | A! (A factorial)                                                                                       |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **EXTREMA**   | 1 1   | Local Extrema: +2/-2 is max/min, +1/-1 is saddle with max/min in x, 0 elsewhere                        |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **FCRIT**     | 3 1   | Critical value for F-distribution, with alpha = A, n1 = B, and n2 = C                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **FDIST**     | 3 1   | F-distribution Q(F,n1,n2), with F = A, n1 = B, and n2 = C                                              |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **FLIPLR**    | 1 1   | Reverse order of values in each row                                                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **FLIPUD**    | 1 1   | Reverse order of values in each column                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **FLOOR**     | 1 1   | floor (A) (greatest integer <= A)                                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **FMOD**      | 2 1   | A % B (remainder after truncated division)                                                             |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **GE**        | 2 1   | 1 if A >= B, else 0                                                                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **GT**        | 2 1   | 1 if A > B, else 0                                                                                     |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **HYPOT**     | 2 1   | hypot (A, B) = sqrt (A\*A + B\*B)                                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **I0**        | 1 1   | Modified Bessel function of A (1st kind, order 0)                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **I1**        | 1 1   | Modified Bessel function of A (1st kind, order 1)                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **IFELSE**    | 3 1   | B if A != 0, else C                                                                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **IN**        | 2 1   | Modified Bessel function of A (1st kind, order B)                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **INRANGE**   | 3 1   | 1 if B <= A <= C, else 0                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **INSIDE**    | 1 1   | 1 when inside or on polygon(s) in A, else 0                                                            |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **INV**       | 1 1   | 1 / A                                                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ISFINITE**  | 1 1   | 1 if A is finite, else 0                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ISNAN**     | 1 1   | 1 if A == NaN, else 0                                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **J0**        | 1 1   | Bessel function of A (1st kind, order 0)                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **J1**        | 1 1   | Bessel function of A (1st kind, order 1)                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **JN**        | 2 1   | Bessel function of A (1st kind, order B)                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **K0**        | 1 1   | Modified Kelvin function of A (2nd kind, order 0)                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **K1**        | 1 1   | Modified Bessel function of A (2nd kind, order 1)                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **KEI**       | 1 1   | kei (A)                                                                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **KER**       | 1 1   | ker (A)                                                                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **KM2DEG**    | 1 1   | Converts Kilometers to Spherical Degrees                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **KN**        | 2 1   | Modified Bessel function of A (2nd kind, order B)                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **KURT**      | 1 1   | Kurtosis of A                                                                                          |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LDIST**     | 1 1   | Compute minimum distance (in km if -fg) from lines in multi-segment ASCII file A                       |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LDIST2**    | 2 1   | As LDIST, from lines in ASCII file B but only to nodes where A != 0                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LE**        | 2 1   | 1 if A <= B, else 0                                                                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LOG**       | 1 1   | log (A) (natural log)                                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LOG10**     | 1 1   | log10 (A) (base 10)                                                                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LOG1P**     | 1 1   | log (1+A) (accurate for small A)                                                                       |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LOG2**      | 1 1   | log2 (A) (base 2)                                                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LMSSCL**    | 1 1   | LMS scale estimate (LMS STD) of A                                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LOWER**     | 1 1   | The lowest (minimum) value of A                                                                        |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LRAND**     | 2 1   | Laplace random noise with mean A and std. deviation B                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **LT**        | 2 1   | 1 if A < B, else 0                                                                                     |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **MAD**       | 1 1   | Median Absolute Deviation (L1 STD) of A                                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **MAX**       | 2 1   | Maximum of A and B                                                                                     |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **MEAN**      | 1 1   | Mean value of A                                                                                        |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **MED**       | 1 1   | Median value of A                                                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **MIN**       | 2 1   | Minimum of A and B                                                                                     |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **MOD**       | 2 1   | A mod B (remainder after floored division)                                                             |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **MODE**      | 1 1   | Mode value (Least Median of Squares) of A                                                              |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **MUL**       | 2 1   | A \* B                                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **NAN**       | 2 1   | NaN if A == B, else A                                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **NEG**       | 1 1   | -A                                                                                                     |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **NEQ**       | 2 1   | 1 if A != B, else 0                                                                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **NORM**      | 1 1   | Normalize (A) so max(A)-min(A) = 1                                                                     |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **NOT**       | 1 1   | NaN if A == NaN, 1 if A == 0, else 0                                                                   |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **NRAND**     | 2 1   | Normal, random values with mean A and std. deviation B                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **OR**        | 2 1   | NaN if B == NaN, else A                                                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **PDIST**     | 1 1   | Compute minimum distance (in km if -fg) from points in ASCII file A                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **PDIST2**    | 2 1   | As PDIST, from points in ASCII file B but only to nodes where A != 0                                   |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **POP**       | 1 0   | Delete top element from the stack                                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **PLM**       | 3 1   | Associated Legendre polynomial P(A) degree B order C                                                   |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **PLMg**      | 3 1   | Normalized associated Legendre polynomial P(A) degree B order C (geophysical convention)               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **POW**       | 2 1   | A ^ B                                                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **PQUANT**    | 2 1   | The B'th Quantile (0-100%) of A                                                                        |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **PSI**       | 1 1   | Psi (or Digamma) of A                                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **PV**        | 3 1   | Legendre function Pv(A) of degree v = real(B) + imag(C)                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **QV**        | 3 1   | Legendre function Qv(A) of degree v = real(B) + imag(C)                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **R2**        | 2 1   | R2 = A^2 + B^2                                                                                         |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **R2D**       | 1 1   | Convert Radians to Degrees                                                                             |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **RAND**      | 2 1   | Uniform random values between A and B                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **RINT**      | 1 1   | rint (A) (round to integral value nearest to A)                                                        |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ROTX**      | 2 1   | Rotate A by the (constant) shift B in x-direction                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ROTY**      | 2 1   | Rotate A by the (constant) shift B in y-direction                                                      |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SDIST**     | 2 1   | Spherical (Great circle|geodesic) distance (in km) between nodes and stack (A, B) :ref:`(...) <SDIST>` |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SAZ**       | 2 1   | Spherical azimuth from grid nodes to stack lon, lat (i.e., A, B)                                       |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SBAZ**      | 2 1   | Spherical backazimuth from grid nodes to stack lon, lat (i.e., A, B)                                   |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SEC**       | 1 1   | sec (A) (A in radians)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SECD**      | 1 1   | sec (A) (A in degrees)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SIGN**      | 1 1   | sign (+1 or -1) of A                                                                                   |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SIN**       | 1 1   | sin (A) (A in radians)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SINC**      | 1 1   | sinc (A) (sin (pi\*A)/(pi\*A))                                                                         |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SIND**      | 1 1   | sin (A) (A in degrees)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SINH**      | 1 1   | sinh (A)                                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SKEW**      | 1 1   | Skewness of A                                                                                          |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SQR**       | 1 1   | A^2                                                                                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SQRT**      | 1 1   | sqrt (A)                                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **STD**       | 1 1   | Standard deviation of A                                                                                |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **STEP**      | 1 1   | Heaviside step function: H(A)                                                                          |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **STEPX**     | 1 1   | Heaviside step function in x: H(x-A)                                                                   |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **STEPY**     | 1 1   | Heaviside step function in y: H(y-A)                                                                   |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SUB**       | 2 1   | A - B                                                                                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **SUM**       | 1 1   | Sum of all values in A                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **TAN**       | 1 1   | tan (A) (A in radians)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **TAND**      | 1 1   | tan (A) (A in degrees)                                                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **TANH**      | 1 1   | tanh (A)                                                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **TAPER**     | 2 1   | Unit weights cosine-tapered to zero within A and B of x and y grid margins                             |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **TN**        | 2 1   | Chebyshev polynomial Tn(-1<t<+1,n), with t = A, and n = B                                              |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **TCRIT**     | 2 1   | Critical value for Student's t-distribution, with alpha = A and n = B                                  |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **TDIST**     | 2 1   | Student's t-distribution A(t,n), with t = A, and n = B                                                 |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **UPPER**     | 1 1   | The highest (maximum) value of A                                                                       |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **XOR**       | 2 1   | 0 if A == NaN and B == NaN, NaN if B == NaN, else A                                                    |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **Y0**        | 1 1   | Bessel function of A (2nd kind, order 0)                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **Y1**        | 1 1   | Bessel function of A (2nd kind, order 1)                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **YLM**       | 2 2   | Re and Im orthonormalized spherical harmonics degree A order B                                         |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **YLMg**      | 2 2   | Cos and Sin normalized spherical harmonics degree A order B (geophysical convention)                   |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **YN**        | 2 1   | Bessel function of A (2nd kind, order B)                                                               |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ZCRIT**     | 1 1   | Critical value for the normal-distribution, with alpha = A                                             |
+---------------+-------+--------------------------------------------------------------------------------------------------------+
| **ZDIST**     | 1 1   | Cumulative normal-distribution C(x), with x = A                                                        |
+---------------+-------+--------------------------------------------------------------------------------------------------------+

Symbols
-------

The following symbols have special meaning:

+-------------+-------------------------------------------------+
| **PI**      | 3.1415926...                                    |
+-------------+-------------------------------------------------+
| **E**       | 2.7182818...                                    |
+-------------+-------------------------------------------------+
| **EULER**   | 0.5772156...                                    |
+-------------+-------------------------------------------------+
| **XMIN**    | Minimum x value                                 |
+-------------+-------------------------------------------------+
| **XMAX**    | Maximum x value                                 |
+-------------+-------------------------------------------------+
| **XINC**    | x increment                                     |
+-------------+-------------------------------------------------+
| **NX**      | The number of x nodes                           |
+-------------+-------------------------------------------------+
| **YMIN**    | Minimum y value                                 |
+-------------+-------------------------------------------------+
| **YMAX**    | Maximum y value                                 |
+-------------+-------------------------------------------------+
| **YINC**    | y increment                                     |
+-------------+-------------------------------------------------+
| **NY**      | The number of y nodes                           |
+-------------+-------------------------------------------------+
| **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   |
+-------------+-------------------------------------------------+

Notes On Operators
------------------

#. The operator **SDIST** calculates spherical distances in km between 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.
   Similarly, the **SAZ** and **SBAZ** operators calculate spherical
   azimuth and back-azimuths in degrees, respectively. The operators
   **LDIST** and **PDIST** compute spherical distances in km if **-fg** is
   set or implied, else they return Cartesian distances. Note: If the current
   :ref:`PROJ_ELLIPSOID <Projection Parameters>` is ellipsoidal then
   geodesics are used in calculations of distances, which can be slow.
   You can trade speed with accuracy by changing the algorithm used to
   compute the geodesic (see :ref:`PROJ_GEODESIC <Projection Parameters>`).

#. The operator **PLM** calculates the associated Legendre polynomial
   of degree L and order M (0 <= M <= L), and its argument is the sine of
   the latitude. **PLM** is not normalized and includes the Condon-Shortley
   phase (-1)^M. **PLMg** is normalized in the way that is most commonly
   used in geophysics. The C-S phase can be added by using -M as argument.
   **PLM** will overflow at higher degrees, whereas **PLMg** is stable
   until ultra high degrees (at least 3000).

#. The operators **YLM** and **YLMg** calculate normalized spherical
   harmonics for degree L and order M (0 <= M <= L) for all positions in
   the grid, which is assumed to be in degrees. **YLM** and **YLMg** return
   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 consecutive = file.nc calls.

   The orthonormalized complex harmonics **YLM** are most commonly used in
   physics and seismology. The square of **YLM** integrates to 1 over a
   sphere. In geophysics, **YLMg** is normalized to produce unit power when
   averaging the cosine and sine terms (separately!) over a sphere (i.e.,
   their squares each integrate to 4 pi). The Condon-Shortley phase (-1)^M
   is not included in **YLM** or **YLMg**, but it can be added by using -M
   as argument.

#. All the derivatives are based on central finite differences, with
   natural boundary conditions.

#. 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).

#. Piping of files is not allowed.

#. The stack depth limit is hard-wired to 100.

#. All functions expecting a positive radius (e.g., **LOG**, **KEI**,
   etc.) are passed the absolute value of their argument. (9) The bitwise
   operators (**BITAND**, **BITLEFT**, **BITNOT**, **BITOR**, **BITRIGHT**,
   **BITTEST**, and **BITXOR**) convert a grid's single precision values to
   unsigned 32-bit ints to perform the bitwise operations. Consequently,
   the largest whole integer value that can be stored in a float grid is
   2^24 or 16,777,216. Any higher result will be masked to fit in the lower
   24 bits.  Thus, bit operations are effectively limited to 24 bit.  All
   bitwise operators return NaN if given NaN arguments or bit-settings <= 0.

.. include:: explain_float.rst_

.. include:: explain_grd_inout.rst_

.. include:: explain_grd_coord.rst_

.. include:: explain_sto_rcl_clr.rst_

Macros
------

Users may save their favorite operator combinations as macros via the
file *grdmath.macros* in their current or user directory. The file may contain
any number of macros (one per record); comment lines starting with # are
skipped. The format for the macros is **name** = **arg1 arg2 ... arg2**
: *comment* where **name** is how the macro will be used. When this
operator appears on the command line we simply replace it with the
listed argument list. No macro may call another macro. As an example,
the following macro expects three arguments (radius x0 y0) and sets the
modes that are inside the given circle to 1 and those outside to 0:

INCIRCLE = CDIST EXCH DIV 1 LE : usage: r x y INCIRCLE to return 1
inside circle

Note: Because geographic or time constants may be present in a macro, it
is required that the optional comment flag (:) must be followed by a space.

Examples
--------

.. include:: grdmath_examples.rst_

To take log10 of the average of 2 files, use

   ::

    gmt grdmath file1.nc file2.nc ADD 0.5 MUL LOG10 = file3.nc

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

   ::

    gmt grdmath ages.nc SQRT 350 MUL 2500 ADD = depths.nc

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

   ::

    gmt grdmath 2 s_xy.nc MUL s_xx.nc s_yy.nc SUB DIV ATAN 2 DIV = direction.nc

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:

   ::

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

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

   ::

    gmt grdmath faa.nc DUP EXTREMA 2 EQ MUL DUP 100 GT MUL 0 NAN = z.nc
    gmt grd2xyz z.nc -s > max.xyz

To demonstrate the use of named variables, consider this radial wave
where we store and recall the normalized radial arguments in radians:

   ::

    gmt grdmath -R0/10/0/10 -I0.25 5 5 CDIST 2 MUL PI MUL 5 DIV STO@r COS @r SIN MUL = wave.nc

References
----------

Abramowitz, M., and I. A. Stegun, 1964, *Handbook of Mathematical
Functions*, Applied Mathematics Series, vol. 55, Dover, New York.

Holmes, S. A., and W. E. Featherstone, 2002, A unified approach to the
Clenshaw summation and the recursive computation of very high degree and
order normalised associated Legendre functions. *Journal of Geodesy*,
76, 279-299.

Press, W. H., S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery,
1992, *Numerical Recipes*, 2nd edition, Cambridge Univ., New York.

Spanier, J., and K. B. Oldman, 1987, *An Atlas of Functions*, Hemisphere
Publishing Corp.

See Also
--------

:doc:`gmt`, :doc:`gmtmath`,
:doc:`grd2xyz`, :doc:`grdedit`,
:doc:`grdinfo`, :doc:`xyz2grd`
