Useful utilities for working with raster data, typically numpy arrays.

eolearn.core.utils.raster.fast_nanpercentile(data, percentile, *, method='linear')[source]

This is an alternative implementation of numpy.nanpercentile. For cases where the size of the first dimension is relatively small compared to the size of the entire array it works much faster than the original.

The algorithm divides pixel data over the first axis into groups by how many NaN values they have. In each group NaN values are removed and numpy.percentile function is applied. If any series contains only NaN values also any percentile of that series will be NaN.

This function differs from numpy implementations only in the following:

  • In case the size of the first dimension of data is 0 this method will still return an output array with all NaN values. This matches with numpy.nanpercentile while numpy.percentile raises an error.

  • The output dtype of this method will be always the same as the input dtype while numpy implementations in many cases use float64 as the output dtype.

  • data (numpy.ndarray) – An array for which percentiles will be calculated along the first axis.

  • percentile (float) – A percentile to compute, which must be between 0 and 100 inclusive.

  • method (str) – A method for estimating the percentile. This parameter is propagated to numpy.percentile.


An array of percentiles and a shape equal to the shape of data array without the first dimension.

Return type


eolearn.core.utils.raster.constant_pad(array, multiple_of, up_down_rule='even', left_right_rule='even', pad_value=0)[source]

Function pads an image of shape (rows, columns, channels) with zeros.

It pads an image so that the shape becomes (rows + padded_rows, columns + padded_columns, channels), where padded_rows = (int(rows/multiple_of[0]) + 1) * multiple_of[0] - rows

Same rule is applied to columns.

  • array (numpy.ndarray) – Array with shape (rows, columns, …) to be padded.

  • multiple_of (Tuple[int, int]) – make array’ rows and columns multiple of this tuple

  • up_down_rule (Literal['even', 'up', 'down']) – Add padded rows evenly to the top/bottom of the image, or up (top) / down (bottom) only

  • left_right_rule (Literal['even', 'left', 'right']) – Add padded columns evenly to the left/right of the image, or left / right only

  • pad_value (float) – Value to be assigned to padded rows and columns

Return type