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.

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.find_fill_value(data_array)#

Float-typed variables can easily be masked. If they don’t already have a fill value, they can be masked using NaN without issue. However there are some int-typed variables without a fill value that _cant_ be automatically masked.

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, size=1)#

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]]