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:
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. |
|
|
|
|
|
Calculate the area overlap as a function of latitude. |
|
Create a weight matrix for conservative regridding along latitude. |
|
Align the phase of a periodic number to match another. |
|
|
|
|
|
Calculate the overlap between two intervals considering periodicity. |
|
Calculate the area overlap as a function of longitude. |
|
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#
- 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.
- 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.
- credit.regrid._assert_increasing(x: numpy.ndarray) None#
- 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.