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#
Representation of a rectilinear grid. |
|
Base class for regridding. |
|
Regrid with nearest neighbor interpolation. |
|
Regrid with bilinear interpolation. |
|
Regrid with linear conservative regridding. |
Functions#
|
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#
- property shape: tuple[int, int]#
- __eq__(other)#
- __hash__()#
- class credit.regrid.Regridder#
Base class for regridding.
- 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:
RegridderRegrid with nearest neighbor interpolation.
- indices()#
The interpolation indices associated with source_grid.