credit.regrid#

This scripts contains functions that performs nearest, bilinear, and conservative interpolation on xarray.Datasets. The original version of this script is available at WeatherBench2.

Note: only rectalinear grids (one dimensional lat/lon coordinates) are supported.

Reference:

- WeatherBench2 regridding:
    https://github.com/google-research/weatherbench2/blob/main/weatherbench2/regridding.py

Example usage:

# ================================================================================== #
import credit.regrid as regrid

# --------------------- #
# prepare grids

# target grid
lon_1deg = np.arange(0, 360, 1)
lat_1deg = np.arange(-90, 91, 1)
target_grid = regrid.Grid.from_degrees(lon_1deg, lat_1deg)

# input grid (flip 90 --> -90 to -90 --> 90)
lon_025deg = ds_static['longitude'].values
lat_025deg = ds_static['latitude'].values[::-1]
source_grid = regrid.Grid.from_degrees(lon_025deg, lat_025deg)

# --------------------- #
# define regridder
regridder = regrid.ConservativeRegridder(source=source_grid, target=target_grid)

# --------------------- #
# clear old chunking and interpolate data
ds_static = ds_static.chunk({'longitude': -1, 'latitude': -1})
ds_static_1deg = regridder.regrid_dataset(ds_static)

# --------------------- #
# ... some xarray operations to preserve the order of dims ... #

# assign coordinates
lon_1deg = np.arange(0, 360, 1)
lat_1deg = np.arange(-90, 91, 1)
ds_static_1deg = ds_static_1deg.assign_coords({
    'latitude': lat_1deg,
    'longitude': lon_1deg
})

# flip latitude from -90 --> 90 to 90 --> -90
ds_static_1deg = ds_static_1deg.isel(latitude=slice(None, None, -1))

Attributes#

Classes#

Grid

Representation of a rectilinear grid.

Regridder

Base class for regridding.

NearestRegridder

Regrid with nearest neighbor interpolation.

BilinearRegridder

Regrid with bilinear interpolation.

ConservativeRegridder

Regrid with linear conservative regridding.

Functions#

nearest_neighbor_indices(→ numpy.ndarray)

Returns Haversine nearest neighbor indices from source_grid to target_grid.

Module Contents#

credit.regrid.Array#
class credit.regrid.Grid#

Representation of a rectilinear grid.

lon: numpy.ndarray#
lat: numpy.ndarray#
classmethod from_degrees(lon: numpy.ndarray, lat: numpy.ndarray) → Grid#
property shape: tuple[int, int]#
__eq__(other)#
__hash__()#
class credit.regrid.Regridder#

Base class for regridding.

source: Grid#
target: Grid#
abstractmethod regrid_array(field: Array) → numpy.ndarray#

Regrid an array with dimensions (…, lon, lat) from source to target.

regrid_dataset(dataset: xarray.Dataset) → xarray.Dataset#

Regrid an xarray.Dataset from source to target.

credit.regrid.nearest_neighbor_indices(source_grid: Grid, target_grid: Grid) → numpy.ndarray#

Returns Haversine nearest neighbor indices from source_grid to target_grid.

class credit.regrid.NearestRegridder#

Bases: Regridder

Regrid with nearest neighbor interpolation.

indices()#

The interpolation indices associated with source_grid.

regrid_array(field: Array) → numpy.ndarray#

Regrid an array with dimensions (…, lon, lat) from source to target.

class credit.regrid.BilinearRegridder#

Bases: Regridder

Regrid with bilinear interpolation.

regrid_array(field: Array) → numpy.ndarray#

Regrid an array with dimensions (…, lon, lat) from source to target.

class credit.regrid.ConservativeRegridder#

Bases: Regridder

Regrid with linear conservative regridding.

regrid_array(field: Array) → numpy.ndarray#

Regrid an array with dimensions (…, lon, lat) from source to target.