seagrid.resample#
Resample gridded data from one Grid to another.
Two methods, both from pyresample:
"bin"Every source cell is put in the target cell that contains its centre, and the target value is a statistic (mean, sum, count, min, max) of the finite source values in it (
pyresample.bucket.BucketResampler). Use it from a finer to a coarser grid."nearest"Every target cell takes the value of the nearest source cell within a search radius (
pyresample.kd_tree.resample_nearest()). Use it from a coarser to a finer grid, or for categorical fields.
The main entry point is regrid(), which works on xarray objects and
keeps leading dimensions such as time or depth. Data sources offer
the same as seagrid.sources.DataSource.regrid():
from seagrid import grids, ostia
sst = ostia.regrid(grids.cbiomes_halfdeg(), "2010-06-01")
Examples
>>> import numpy as np, xarray as xr
>>> from seagrid import grids
>>> from seagrid.resample import regrid
>>> fine = grids.regular_latlon(1.0)
>>> da = fine.data_array(np.ones(fine.shape), name="chl")
>>> regrid(da, grids.regular_latlon(10.0)).shape
(18, 36)
Functions
|
Bin a field onto a coarser grid by mean (the 2.x interface). |
|
Return a search radius for nearest-neighbour lookups on grid, in metres. |
|
Return the nearest target cell of every source cell (2.x helper). |
|
Resample a field by nearest neighbour. |
|
Resample an xarray object onto another grid. |
Classes
|
Bin data from a source grid onto a target grid, reusing the bin indices. |
- class seagrid.resample.BinResampler(source, target)[source]#
Bases:
objectBin data from a source grid onto a target grid, reusing the bin indices.
The cell that each source cell falls into is computed once, so applying the resampler to many fields on the same grids (every day of a time series) is cheap. With a target
area_def(regular or projected grids) this is pyresample’sBucketResampler; for curvilinear targets each source cell goes to the target cell with the nearest centre.- Parameters:
source (
Gridorpyresample geometry) – Grids of the input data and of the result (seeseagrid.grids.as_grid()).target (
Gridorpyresample geometry) – Grids of the input data and of the result (seeseagrid.grids.as_grid()).
Examples
>>> import numpy as np >>> from seagrid import grids >>> rs = BinResampler(grids.regular_latlon(1.0), grids.regular_latlon(30.0)) >>> data = np.ones(rs.source.shape) >>> rs.average(data).shape, int(rs.count(data).max()) ((6, 12), 900)
- stat(data, stat='mean')[source]#
Return a statistic of the finite source values in each target cell.
- Parameters:
data (
array_like) – Field on the source grid, in the source grid’s order.stat (
{"mean", "sum", "count", "min", "max"}, optional) – Default"mean".
- Returns:
numpy.ndarray– Field on the target grid; NaN in cells without finite source values (0 for"count").- Raises:
ValueError – If stat is unknown or data has the wrong shape.
- Return type:
- seagrid.resample.coarsegrid(source_geo_def, data, target_geo_def, ij=None, coarse_fill=True)[source]#
Bin a field onto a coarser grid by mean (the 2.x interface).
- Parameters:
source_geo_def (
Gridorpyresample geometry) – Source and target grids.target_geo_def (
Gridorpyresample geometry) – Source and target grids.data (
numpy.ndarray) – 2-D field on the source grid.ij (
numpy.ndarray, optional) – Flat target index of every source cell, fromkdquery(). When given, the binning uses it instead ofBinResampler.coarse_fill (
bool, optional) – Fill target cells without data by nearest neighbour. Default True.
- Returns:
numpy.ndarray– Field on the target grid.- Return type:
See also
regridthe same for xarray objects.
BinResamplerreuses the bin indices between calls.
- seagrid.resample.kdquery(source_geo_def, target_geo_def)[source]#
Return the nearest target cell of every source cell (2.x helper).
Distances are in degrees of longitude and latitude, as in 2.x.
- Parameters:
source_geo_def (
Gridorpyresample geometry)target_geo_def (
Gridorpyresample geometry)
- Returns:
dist (
numpy.ndarray) – Distance to the nearest target cell centre, in degrees.ij (
numpy.ndarray) – Flat index of that target cell.
- Return type:
tuple[ndarray[tuple[Any, …], dtype[float64]], ndarray[tuple[Any, …], dtype[int64]]]
- seagrid.resample.nearest(data, source, target, radius_of_influence=None, fill_value=np.nan)[source]#
Resample a field by nearest neighbour.
- Parameters:
data (
array_like) – Field on the source grid, in the source grid’s order.source (
Gridorpyresample geometry) – Grids (seeseagrid.grids.as_grid()).target (
Gridorpyresample geometry) – Grids (seeseagrid.grids.as_grid()).radius_of_influence (
float, optional) – Search radius in metres. Defaultdefault_radius()of the source grid.fill_value (
floatorNone, optional) – Value of target cells without a neighbour. Default NaN (integer data is then returned as float). None returns a masked array.
- Returns:
numpy.ndarray– Field on the target grid, in the target grid’s order.- Return type:
- seagrid.resample.regrid(obj, target, *, source=None, method='bin', stat='mean', fill=False, radius_of_influence=None)[source]#
Resample an xarray object onto another grid.
- Parameters:
obj (
xarray.DataArrayorxarray.Dataset) – Data whose last two dimensions span the source grid. Leading dimensions (time,depth, …) are kept. In a Dataset, variables that do not span the grid are kept unchanged if they share no dimension with it, and dropped otherwise.target (
Grid,pyresample geometryorgrid name) – Grid of the result (seeseagrid.grids.as_grid()).source (
Gridorpyresample geometry, optional) – Grid of obj. Default: built from its coordinates (seagrid.grids.from_dataset()).method (
{"bin", "nearest"}, optional) – See the module description. Default"bin".stat (
{"mean", "sum", "count", "min", "max"}, optional) – Statistic formethod="bin". Default"mean".fill (
bool, optional) – Formethod="bin": fill empty target cells by nearest neighbour. Default False.radius_of_influence (
float, optional) – Search radius in metres formethod="nearest"and fill. Defaultdefault_radius()of the source grid.
- Returns:
xarray.DataArrayorxarray.Dataset– On target, withlat/loncoordinates and the regridding recorded inattrs["seagrid_history"].- Raises:
ValueError – If method or stat is unknown, or obj does not span the source grid.
- Return type:
See also