lair.geo.resample#
- lair.geo.resample(data, resolution, regrid_method='bilinear')[source]#
Resample the data to a new resolution.
Returns new data; the input is not modified.
The new grid starts at the source’s outer edge. When the resolution does not divide the source’s width (0.3 degrees over 4.0 degrees of longitude), the last cell is partial, and what becomes of it depends on the method:
"conservative"keeps it whole, so the new grid covers the whole source and its last cell reaches past the source’s edge. That cell holds the flux of the part inside the source averaged over the whole cell, the part outside counting as zero, so flux times area still adds up to the source’s total.Every other method (
"bilinear", the default, and"conservative_normed"included) keeps it only when its center is inside the source, which takes more than half of it, and otherwise drops the strip. A cell centered outside the source’s outermost cell centers is NaN from bilinear interpolation (regrid()), and"conservative_normed"would give it the flux of the part inside but count its whole area.
- Parameters:
data (xr.DataArray | xr.Dataset) – The data to resample.
resolution (float | tuple[x_res, y_res]) – The new resolution in degrees. If a single value is provided, the resolution is assumed to be the same in both dimensions.
regrid_method (str, optional) – The regridding method, by default ‘bilinear’.
- Returns:
xr.DataArray | xr.Dataset – The resampled data, the same type as
data.- Return type:
_XarrayT
See also
regridTo a grid you give.
lair.inventories.Inventory.resampleConservative by default, for a flux.
Examples
A flux of 1 on four cells of 0.1 degree, to cells of 0.05 degree. The default, bilinear, leaves the outer cells NaN (they are outside the outermost centers); a flux needs
"conservative":>>> import numpy as np >>> import xarray as xr >>> from lair.geo import resample, write_rio_crs >>> flux = xr.DataArray( ... np.ones((2, 2)), ... coords={"lat": [40.65, 40.75], "lon": [-111.95, -111.85]}, ... dims=("lat", "lon"), ... ) >>> flux = write_rio_crs(flux.rio.set_spatial_dims(x_dim="lon", y_dim="lat"), 4326) >>> resample(flux, 0.05).values array([[nan, nan, nan, nan], [nan, 1., 1., nan], [nan, 1., 1., nan], [nan, nan, nan, nan]]) >>> conservative = resample(flux, 0.05, regrid_method="conservative") >>> float(conservative.min().round(3)), float(conservative.max().round(3)) (1.0, 1.0)