emsarray.masking#

emsarray.masking.mask_grid_dataset(dataset, mask, work_dir, **kwargs)#

Apply a mask to a grid dataset.

Parameters
  • dataset – The Dataset instance to mask

  • mask – The mask to apply. Different types of datasets need different masks.

  • work_dir – An empty directory where temporary files can be stored while applying the mask. The returned dataset will be built from files inside this directory, so callers must save the returned dataset before deleting this directory.

  • kwargs – Any extra kwargs are passed to open_mfdataset when assembling the new, clipped dataset. This can be used to pass mask_and_scale=False, for example.

Returns

Dataset – The masked dataset

emsarray.masking.mask_grid_data_array(mask, data_array)#

Apply one mask from a dataset to a data array. The mask to apply is selected by comparing dimensions - the first mask found which has dimensions that are a subset of the data array dimensions is used.

The returned data array is not trimmed to the size of the mask. If no appropriate mask is found, the original data array is returned unmodified.

emsarray.masking.calculate_grid_mask_bounds(mask)#

Calculate the included bounds of a mask dataset for each dimension. The mask dataset should contain one or more boolean data arrays. A dict of {dimension_name: slice(min_index, max_index)} will be returned. This dict can be passed directly in to a xarray.Dataset.isel() call to trim a dataset to the bounds of a mask.

emsarray.masking.smear_mask(arr, pad_axes)#

Take a boolean numpy array and a list indicating which axes to smear along. Return a new array, expanded along the axes, with the boolean values smeared accordingly:

>>> arr
array([[0, 0, 1, 0, 0],
       [0, 1, 0, 1, 0],
       [1, 0, 0, 0, 1]]

Smear along the y-axis:

>>> smear_mask(arr, [False, True])
array([[0, 0, 1, 1, 0, 0],
       [0, 1, 1, 1, 1, 0],
       [1, 1, 0, 0, 1, 1]]

Smear along both axes:

>>> smear_mask(arr, [True, True])
array([[0, 0, 1, 1, 0, 0],
       [0, 1, 1, 1, 1, 0],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 0, 0, 1, 1])

This is a half baked convolution operator where the pad_axes parameter is used to build the kernel.

Parameters
  • arr – A boolean numpy numpy.ndarray.

  • pad_axes – A list of booleans, indicating which axes to smear along.

emsarray.masking.blur_mask(arr)#

Take a boolean numpy array and blur it, such that all indices neighbouring a True value in the input array are True in the output array. The output array will have the same shape as the input array.

>>> arr = numpy.array([
...     [1, 0, 0, 0, 0],
...     [0, 0, 0, 0, 0],
...     [0, 0, 0, 1, 0],
...     [0, 0, 0, 0, 1]])
>>> blur_mask(arr)
array([[1, 1, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [0, 0, 1, 1, 1],
       [0, 0, 1, 1, 1]]