xrspatial.normalize.rescale#

xrspatial.normalize.rescale(agg, new_min=0.0, new_max=1.0, name='rescale')[source]#

Min-max normalization of a raster to a target range.

Linearly maps finite values from [data_min, data_max] to [new_min, new_max]. NaN and infinite values pass through unchanged. If all finite values are equal the output is filled with new_min.

Parameters:
  • agg (xr.DataArray or xr.Dataset) – 2D NumPy, CuPy, NumPy-backed Dask, or CuPy-backed Dask array.

  • new_min (float, default=0.0) – Lower bound of the output range.

  • new_max (float, default=1.0) – Upper bound of the output range.

  • name (str, default='rescale') – Name of the output DataArray.

Returns:

rescaled – Rescaled raster with the same shape, dims, coords, and attrs. If agg is a Dataset, each variable is rescaled independently.

Return type:

xr.DataArray or xr.Dataset

Examples

>>> import numpy as np
>>> import xarray as xr
>>> from xrspatial.normalize import rescale

>>> data = np.array([
        [np.nan, 0., 5.],
        [10.,   20., 30.],
    ], dtype=np.float64)
>>> agg = xr.DataArray(data)
>>> rescale(agg)
<xarray.DataArray 'rescale' (dim_0: 2, dim_1: 3)>
array([[       nan, 0.        , 0.16666667],
       [0.33333333, 0.66666667, 1.        ]])
Dimensions without coordinates: dim_0, dim_1