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

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.

_assert_increasing(→ None)

_latitude_cell_bounds(→ numpy.ndarray)

_latitude_overlap(→ numpy.ndarray)

Calculate the area overlap as a function of latitude.

_conservative_latitude_weights(→ numpy.ndarray)

Create a weight matrix for conservative regridding along latitude.

_align_phase_with(x, target, period)

Align the phase of a periodic number to match another.

_periodic_upper_bounds(x, period)

_periodic_lower_bounds(x, period)

_periodic_overlap(x0, x1, y0, y1, period)

Calculate the overlap between two intervals considering periodicity.

_longitude_overlap(→ numpy.ndarray)

Calculate the area overlap as a function of longitude.

_conservative_longitude_weights(→ numpy.ndarray)

Create a weight matrix for conservative regridding along longitude.

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]#
_to_tuple() tuple[tuple[float, Ellipsis], tuple[float, Ellipsis]]#
__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.

_nearest_neighbor_2d(array: Array) numpy.ndarray#

2D nearest neighbor interpolation using BallTree.

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.

credit.regrid._assert_increasing(x: numpy.ndarray) None#
credit.regrid._latitude_cell_bounds(x: Array) numpy.ndarray#
credit.regrid._latitude_overlap(source_points: Array, target_points: Array) numpy.ndarray#

Calculate the area overlap as a function of latitude.

credit.regrid._conservative_latitude_weights(source_points: Array, target_points: Array) numpy.ndarray#

Create a weight matrix for conservative regridding along latitude.

Parameters:
  • source_points – 1D latitude coordinates in radians for centers of source cells.

  • target_points – 1D latitude coordinates in radians for centers of target cells.

Returns:

NumPy array with shape (target_size, source_size). Rows sum to 1.

credit.regrid._align_phase_with(x, target, period)#

Align the phase of a periodic number to match another.

credit.regrid._periodic_upper_bounds(x, period)#
credit.regrid._periodic_lower_bounds(x, period)#
credit.regrid._periodic_overlap(x0, x1, y0, y1, period)#

Calculate the overlap between two intervals considering periodicity.

credit.regrid._longitude_overlap(first_points: Array, second_points: Array, period: float = 2 * np.pi) numpy.ndarray#

Calculate the area overlap as a function of longitude.

credit.regrid._conservative_longitude_weights(source_points: numpy.ndarray, target_points: numpy.ndarray) numpy.ndarray#

Create a weight matrix for conservative regridding along longitude.

Parameters:
  • source_points – 1D longitude coordinates in radians for centers of source cells.

  • target_points – 1D longitude coordinates in radians for centers of target cells.

Returns:

NumPy array with shape (target_size, source_size). Rows sum to 1.

class credit.regrid.ConservativeRegridder#

Bases: Regridder

Regrid with linear conservative regridding.

_mean(field: Array) numpy.ndarray#

Computes cell-averages of field on the target grid.

_nanmean(field: Array) numpy.ndarray#

Compute cell-averages skipping NaNs like np.nanmean.

regrid_array(field: Array) numpy.ndarray#

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