earthpy.mask

Utilities for masking values in arrays.

earthpy.mask.mask_pixels(arr, mask_arr, vals=None)[source]

Apply a mask to an input array.

Masks values in an n-dimensional input array (arr) based on input 1-dimensional array (mask_arr). If mask_arr is provided in a boolean format, it is used as a mask. If mask_arr is provided as a non-boolean format and values to mask (vals) are provided, a Boolean masked array is created from the mask_arr and the indicated vals to mask, and then this new 1-dimensional masked array is used to mask the input arr.

This function is useful when masking cloud and other unwanted pixels.

Parameters
  • arr (numpy array) – The array to mask in rasterio (band, row, col) order.

  • mask_arr (numpy array) – An array of either the pixel_qa or mask of interest.

  • vals (list of numbers either int or float (optional)) – A list of values from the pixel qa layer that will be used to create the mask for the final return array. If vals are not passed in, it is assumed the mask_arr given is the mask of interest.

Returns

arr – A numpy array populated with 1’s where the mask is applied (a Boolean True) and the original numpy array’s value where no masking was done.

Return type

numpy array

Example

>>> import numpy as np
>>> from earthpy.mask import mask_pixels
>>> im = np.arange(9).reshape((3, 3))
>>> im
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> im_mask = np.array([1, 1, 1, 0, 0, 0, 1, 1, 1]).reshape(3, 3)
>>> im_mask
array([[1, 1, 1],
       [0, 0, 0],
       [1, 1, 1]])
>>> mask_pixels(im, mask_arr=im_mask, vals=[1])
masked_array(
  data=[[--, --, --],
        [3, 4, 5],
        [--, --, --]],
  mask=[[ True,  True,  True],
        [False, False, False],
        [ True,  True,  True]],
  fill_value=999999)