pygmt.grdmask

pygmt.grdmask(data, outgrid=None, spacing=None, region=None, outside=0, edge=0, inside=1, verbose=False, **kwargs)[source]

Create mask grid from polygons or point coverage.

Reads one or more files (or standard input) containing polygon or data point coordinates, and creates a binary grid file where nodes that fall inside, on the edge, or outside the polygons (or within the search radius from data points) are assigned values based on outside, edge, and inside parameters.

The mask grid can be used to mask out specific regions in other grids using pygmt.grdmath or similar tools. For masking based on coastline features, consider using pygmt.grdlandmask instead.

Full GMT docs at https://docs.generic-mapping-tools.org/6.6/grdmask.html.

Aliases

  • G = outgrid

  • I = spacing

  • N = outside/edge/inside

  • R = region

  • V = verbose

Parameters:
  • data

    Pass in either a file name, pandas.DataFrame, numpy.ndarray, or a list of file names containing the polygon(s) or data points. Input can be:

    • Polygon mode: One or more files containing closed polygon coordinates

    • Point coverage mode: Data points (used with search_radius parameter)

  • outgrid (str | PathLike | None, default: None) – Name of the output netCDF grid file. If not specified, will return an xarray.DataArray object. For writing a specific grid file format or applying basic data operations to the output grid, see https://docs.generic-mapping-tools.org/6.6/gmt.html#grd-inout-full for the available modifiers.

  • spacing (float, str, or list) –

    x_inc[+e|n][/y_inc[+e|n]]. x_inc [and optionally y_inc] is the grid spacing.

    • Geographical (degrees) coordinates: Optionally, append an increment unit. Choose among m to indicate arc-minutes or s to indicate arc-seconds. If one of the units e, f, k, M, n or u is appended instead, the increment is assumed to be given in meter, foot, km, mile, nautical mile or US survey foot, respectively, and 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 given but set to 0 it will be reset equal to x_inc; otherwise it will be converted to degrees latitude.

    • All coordinates: If +e 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 +n to the supplied integer argument; the increment is then recalculated from the number of nodes, the registration, and the domain. The resulting increment value depends on whether you have selected a gridline-registered or pixel-registered grid; see GMT File Formats for details.

    Note: If region=grdfile is used then the grid spacing and the registration have already been initialized; use spacing and registration to override these values.

  • outside (float | Literal['z', 'id'], default: 0) –

    Set the value assigned to nodes outside the polygons. Default is 0. Can be any number, or one of None, "NaN", and np.nan for NaN.

    When using inside="z" or inside="id", this sets the outside value appended after the mode (e.g., outside=1, inside="z" gives -Nz/1).

  • edge (float | Literal['z', 'id'], default: 0) –

    Set the value assigned to nodes on the polygon edges. Default is 0. Can be any number, or one of None, "NaN", and np.nan for NaN.

    When using inside="z", setting edge="z" treats edges as inside (corresponds to -NZ). Similarly, inside="id", edge="id" gives -NP. The combination inside="z", edge="id" or inside="id", edge="z" is invalid and will raise an error.

  • inside (float | Literal['z', 'id'], default: 1) –

    Set the value assigned to nodes inside the polygons. Default is 1. Can be any number, or one of None, "NaN", and np.nan for NaN.

    Special values:

    • "z": Use the z-value from polygon data (segment header -Zzval, -Lheader, or via -aZ=name). Corresponds to GMT -Nz.

    • "id": Use a running polygon ID number. Corresponds to GMT -Np.

  • region (str or list) – xmin/xmax/ymin/ymax[+r][+uunit]. Specify the region of interest.

  • verbose (bool or str) – Select verbosity level [Full usage].

Return type:

DataArray | None

Returns:

ret – Return type depends on whether the outgrid parameter is set:

  • xarray.DataArray if outgrid is not set

  • None if outgrid is set (grid output will be stored in the file set by outgrid)

Example

>>> import pygmt
>>> import numpy as np
>>> # Create a simple polygon as a triangle
>>> polygon = np.array([[125, 30], [130, 30], [130, 35], [125, 30]])
>>> # Create a mask grid with 1 arc-degree spacing
>>> mask = pygmt.grdmask(data=polygon, spacing=1, region=[125, 130, 30, 35])
>>> mask.values
array([[0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0.],
       [0., 0., 1., 1., 1., 0.],
       [0., 0., 1., 1., 1., 0.],
       [0., 0., 1., 1., 1., 0.],
       [0., 0., 0., 0., 0., 0.]])