# grdmask

Create mask grid from polygons or point coverage

## Synopsis

gmt grdmask table -G outgrid -I increment -R region [ -A[m|p|x|y|r|t] ] [ -C f|l|o|u ] [ -N[z|Z|p|P]values ] [ -S radius|xlim/ylim ] [ -V[level] ] [ -a flags ] [ -bi binary ] [ -di nodata[+c col] ] [ -e regexp ] [ -f flags ] [ -g gaps ] [ -h headers ] [ -i flags ] [ -j flags ] [ -n flags ] [ -qi flags ] [ -r reg ] [ -w flags ] [ -x[[-]n] ] [ -:[i|o] ] [ --PAR=value ]

Note: No space is allowed between the option flag and the associated arguments.

## Description

grdmask can operate in two different modes. 1. It reads one or more pathfiles that each define a closed polygon. The nodes defined by the specified region and lattice spacing will be set equal to one of three possible values depending on whether the node is outside, on the polygon perimeter, or inside the polygon, with the assigned z value selected via -N. If multiple polygons overlap the same nodes then the polygon selected depends on the -C selection. The resulting mask may be used in subsequent operations involving grdmath to mask out data from polygonal areas. 2. The pathfiles simply represent data point locations and the mask is set to the inside or outside value depending on whether a node is within a maximum distance from the nearest data point. If the distance specified is zero then only the nodes nearest each data point are considered “inside”.

## Required Arguments

table

  The name of 1 or more ASCII [or binary, see -bi] files holding the polygon(s) or data points.

-G outgrid[=ID][+d divisor][+n invalid][+o offset|a][+s scale|a][:driver[dataType][+c options]]

> Optionally, append =ID for writing a specific file format. The following modifiers are supported:

> > - +d - Divide data values by given divisor [Default is 1].

> > - +n - Replace data values matching invalid with a NaN.

> > - +o - Offset data values by the given offset, or append a for automatic range offset to preserve precision for integer grids [Default is 0].

> > - +s - Scale data values by the given scale, or append a for automatic scaling to preserve precision for integer grids [Default is 1].

> Note: Any offset is added before any scaling. +sa also sets +oa (unless overridden). To write specific formats via GDAL, use =gd and supply driver (and optionally dataType) and/or one or more concatenated GDAL -co options using +c. See the “Writing grids and images” cookbook section for more details.

-I x_inc[+e|n][/y_inc[+e|n]]

  Set the grid spacing as x_inc [and optionally y_inc].

  Geographical (degrees) coordinates: Optionally, append an increment unit. Choose among:

  > - d - Indicate arc degrees

  > - m - Indicate arc minutes

  > - s - Indicate arc seconds

  > If one of e (meter), f (foot), k (km), M (mile), n (nautical mile) or u (US survey foot), the the increment will be converted to the equivalent degrees longitude at the middle latitude of the region (the conversion depends on PROJ_ELLIPSOID). If y_inc is not given or given but set to 0 it will be reset equal to x_inc; otherwise it will be converted to degrees latitude.

  All coordinates: The following modifiers are supported:

  > - +e - Slightly adjust the max x (east) or y (north) to fit exactly the given increment if needed [Default is to slightly adjust the increment to fit the given domain].

  > - +n - Define the number of nodes rather than the increment, in which case the increment is recalculated from the number of nodes, the registration (see GMT File Formats), and the domain. Note: If -R grdfile is used then the grid spacing and the registration have already been initialized; use -I and -R to override these values.

-R xmin/xmax/ymin/ymax[+r][+u unit]

  Specify the region of interest. (See full description) (See cookbook information).

## Optional Arguments

-A[m|p|x|y]

  If the input data are geographic (as indicated by -f) then the sides in the polygons will be approximated by great circle arcs. When using the -A sides will be regarded as straight lines. Alternatively, append m to have sides first follow meridians, then parallels. Or append p to first follow parallels, then meridians. For Cartesian data, points are simply connected, unless you append x or y to construct stair-case paths whose first move is along x or y, respectively. If your Cartesian data are polar (theta, r), append t or r to construct stair-case paths whose first move is along theta or r, respectively.

-C f|l|o|u

  Clobber mode: Selects the polygon whose z-value will determine the grid nodes. Choose from the following modes: f for the first polygon to overlap a node; o for the last polygon to overlap a node; l for the polygon with the lowest z-value, and u for the polygon with the uppermost z-value [Default is o]. Note: Does not apply to -S. For polygon z-values, see -N.

-N[z|Z|p|P]values

  Sets the out/edge/in that will be assigned to nodes that are outside the polygons, on the edge, or inside. Values can be any number, including the textstring NaN [Default is 0/0/1]. Optionally, use Nz to set polygon insides to the z-value obtained from the data (either segment header -Z zval, -L header or via -aZ=name); use -NZ to consider the polygon boundary as part of the inside. Alternatively, use -Np to use a running number as polygon ID; optionally append start of the sequence [0]. Here, -NP includes the polygon perimeter as inside. Note: -N z|Z|p|P cannot be used in conjunction with -S; they also all optionally accept /out [0].

-S radius|xlim/ylim

  Set nodes to inside, on edge, or outside depending on their distance to the nearest data point. Nodes within the searsch radius [0] from the nearest data point are considered inside; append a distance unit (see Units). If radius is given as z then we instead read individual radii from the 3rd input column. Unless Cartesian data, specify the unit of these radii by appending it after -Sz. If -S is not set then we consider the input data to define one or more closed polygon(s) instead. For Cartesian data with different units you can instead append xlim/ylim which will perform a rectangular search where all nodes within ±xlim and ±ylim of a data point will be considered inside. One can also achieve the rectangular selection effect by using the -S n_cells c form. Here n_cells means the number of cells around each data point. As an example, -S0c means that only the cell where point lies is masked, -S1c masks one cell beyond that (i.e. makes a 3x3 neighborhood), and so on.

-V[level]

  Select verbosity level [w]. (See full description) (See cookbook information).

-a[[col=]name[,…]] (more …)

  Set aspatial column associations col=name.

-bi record[+b|l] (more …)

  Select native binary format for primary table input. [Default is 2 input columns (3 with -Sz)].

-di nodata[+c col] (more …)

  Replace input columns that equal nodata with NaN.

-e[~]“pattern” | -e[~]/regexp/[i] (more …)

  Only accept data records that match the given pattern.

-f[i|o]colinfo (more …)

  Specify data types of input and/or output columns.

-g x|y|z|d|X|Y|D gap[u][+a][+c col][+n|p] (more …)

  Determine data gaps and line breaks.

-h[i|o][n][+c][+d][+m segheader][+r remark][+t title] (more …)

  Skip or produce header record(s).

-i cols[+l][+d divisor][+s scale|d|k][+o offset][,…][,t[word]] (more …)

  Select input columns and transformations (0 is first column, t is trailing text, append word to read one word only).

-je|f|g (more …)

  Determine how spherical distances or coordinate transformations are calculated.

-n[b|c|l|n][+a][+b BC][+t threshold]

  Append +b BC to set any boundary conditions to be used, adding g for geographic, p for periodic, or n for natural boundary conditions. For the latter two you may append x or y to specify just one direction, otherwise both are assumed. [Default is geographic if grid is geographic].

-qi[~]rows|limits[+c col][+a|t|s] (more …)

  Select input rows or data limit(s) [default is all rows].

-r[g|p] (more …)

  Set node registration [gridline].

-wy|a|w|d|h|m|s|c period[/phase][+c col] (more …)

  Convert an input coordinate to a cyclical coordinate.

-x[[-]n] (more …)

  Limit number of cores used in multi-threaded algorithms (OpenMP required).

-^ or just -

  Print a short message about the syntax of the command, then exit (Note: on Windows just use -).

-+ or just +

  Print an extensive usage (help) message, including the explanation of any module-specific option (but not the GMT common options), then exit.

-? or no arguments

  Print a complete usage (help) message, including the explanation of all options, then exit.

--PAR=value

  Temporarily override a GMT default setting; repeatable. See gmt.conf for parameters.

## Units

For map distance unit, append unit d for arc degree, m for arc minute, and s for arc second, or e for meter [Default unless stated otherwise], f for foot, k for km, M for statute mile, n for nautical mile, and u for US survey foot. By default we compute such distances using a spherical approximation with great circles (-jg) using the authalic radius (see PROJ_MEAN_RADIUS). You can use -jf to perform “Flat Earth” calculations (quicker but less accurate) or -je to perform exact geodesic calculations (slower but more accurate; see PROJ_GEODESIC for method used).

## Geographical And Time Coordinates

When the output grid type is netCDF, the coordinates will be labeled “longitude”, “latitude”, or “time” based on the attributes of the input data or grid (if any) or on the -f or -R options. For example, both -f0x -f1t and -R90w/90e/0t/3t will result in a longitude/time grid. When the x, y, or z coordinate is time, it will be stored in the grid as relative time since epoch as specified by TIME_UNIT and TIME_EPOCH in the gmt.conf file or on the command line. In addition, the unit attribute of the time variable will indicate both this unit and epoch.

## Inside/outside Status

To determine if a point is inside, outside, or exactly on the boundary of a polygon we need to balance the complexity (and execution time) of the algorithm with the type of data and shape of the polygons. For any Cartesian data we use a non-zero winding algorithm, which is quite fast. For geographic data we will also use this algorithm as long as (1) the polygons do not include a geographic pole, and (2) the longitude extent of the polygons is less than 360. If this is the situation we also carefully adjust the test point longitude for any 360 degree offsets, if appropriate. Otherwise, we employ a full spherical ray-shooting method to determine a points status.

## Notes

A grid produced by grdmask is a categorical dataset. As such, one has to be careful not to interpolate it with standard methods, such as splines. However, if you make a map of this grid using a map projection the grid will be reprojected to yield a rectangular matrix in the projected coordinates. This interpolation is done using splines by default and thus may yield artifacts in your map. We recommend you use grdimage -nn to instead use a nearest neighbor interpolation for such cases.

## Save storage space

Since most uses of grdmask revolves around creating mask grids that hold just a few integer values (and perhaps NaN), we choose to write them to disk as byte grids by appending the suffix =nb to the desired grid filename. Some situations may store integers that exceed the range available in a byte and for those we specify a short integer grid with =ns. For larger integers you may consider =ni, otherwise use the default float grid format.

## Examples

Note: Below are some examples of valid syntax for this module. The examples that use remote files (file names starting with `@`) can be cut and pasted into your terminal for testing. Other commands requiring input files are just dummy examples of the types of uses that are common but cannot be run verbatim as written.

To set all nodes inside and on the polygons coastline_*.xy to 0, and outside points to 1, do

    gmt grdmask coastline_*.xy -R-60/-40/-40/-30 -I5m -N1/0/0 -Gland_mask.nc=nb -V

To set nodes within 50 km of data points to 1 and other nodes to NaN, do

    gmt grdmask data.xyz -R-60/-40/-40/-30 -I5m -NNaN/1/1 -S50k -Gdata_mask.nc=nb -V

To assign polygon IDs to the gridnodes using the insides of the polygons in plates.gmt, based on the attribute POL_ID, do

    gmt grdmask plates.gmt -R-40/40/-40/40 -I2m -Nz -Gplate_IDs.nc=ns -aZ=POL_ID -V

Same exercise, but instead compute running polygon IDs starting at 100, do

    gmt grdmask plates.gmt -R-40/40/-40/40 -I2m -Np100 -Gplate_IDs.nc=ns -V

## See Also

gmt, grdlandmask, grdmath, grdclip, mask, clip