verde.convexhull_mask(data_coordinates, coordinates=None, grid=None, projection=None)[source]

Mask grid points that are outside the convex hull of the given data points.

Either coordinates or grid must be given:

  • If coordinates is not None, produces an array that is False when a point is outside the convex hull and True otherwise.

  • If grid is not None, produces a mask and applies it to grid (an xarray.Dataset).

  • data_coordinates (tuple of arrays) – Same as coordinates but for the data points.

  • coordinates (None or tuple of arrays) – Arrays with the coordinates of each point that will be masked. Should be in the following order: (easting, northing, …). Only easting and northing will be used, all subsequent coordinates will be ignored.

  • grid (None or xarray.Dataset) – 2D grid with values to be masked. Will use the first two dimensions of the grid as northing and easting coordinates, respectively. For this to work, the grid dimensions must be ordered as northing then easting. The mask will be applied to grid using the xarray.Dataset.where method.

  • projection (callable or None) – If not None, then should be a callable object projection(easting, northing) -> (proj_easting, proj_northing) that takes in easting and northing coordinate arrays and returns projected easting and northing coordinate arrays. This function will be used to project the given coordinates (or the ones extracted from the grid) before calculating distances.


mask (array or xarray.Dataset) – If coordinates was given, then a boolean array with the same shape as the elements of coordinates. If grid was given, then an xarray.Dataset with the mask applied to it.


>>> from verde import grid_coordinates
>>> region = (0, 5, -10, -4)
>>> spacing = 1
>>> coords = grid_coordinates(region, spacing=spacing)
>>> data_coords = ((2, 3, 2, 3), (-9, -9, -6, -6))
>>> mask = convexhull_mask(data_coords, coordinates=coords)
>>> print(mask)
[[False False False False False False]
 [False False  True  True False False]
 [False False  True  True False False]
 [False False  True  True False False]
 [False False  True  True False False]
 [False False False False False False]
 [False False False False False False]]
>>> # Mask an xarray.Dataset directly
>>> import xarray as xr
>>> coords_dict = {"easting": coords[0][0, :], "northing": coords[1][:, 0]}
>>> data_vars = {"scalars": (["northing", "easting"], np.ones(mask.shape))}
>>> grid = xr.Dataset(data_vars, coords=coords_dict)
>>> masked = convexhull_mask(data_coords, grid=grid)
>>> print(masked.scalars.values)
[[nan nan nan nan nan nan]
 [nan nan  1.  1. nan nan]
 [nan nan  1.  1. nan nan]
 [nan nan  1.  1. nan nan]
 [nan nan  1.  1. nan nan]
 [nan nan nan nan nan nan]
 [nan nan nan nan nan nan]]