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

coarsegrid(source_geo_def, data, target_geo_def)

Bin a field onto a coarser grid by mean (the 2.x interface).

default_radius(grid)

Return a search radius for nearest-neighbour lookups on grid, in metres.

kdquery(source_geo_def, target_geo_def)

Return the nearest target cell of every source cell (2.x helper).

nearest(data, source, target[, ...])

Resample a field by nearest neighbour.

regrid(obj, target, *[, source, method, ...])

Resample an xarray object onto another grid.

Classes

BinResampler(source, target)

Bin data from a source grid onto a target grid, reusing the bin indices.

class seagrid.resample.BinResampler(source, target)[source]#

Bases: object

Bin 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’s BucketResampler; for curvilinear targets each source cell goes to the target cell with the nearest centre.

Parameters:

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:

ndarray[tuple[Any, …], dtype[float64]]

average(data, fill=False)[source]#

Return the mean of the finite source values in each target cell.

Parameters:
  • data (array_like) – Field on the source grid.

  • fill (bool, optional) – Fill target cells that got no value with the nearest source value (within default_radius() of the source grid). Default False.

Returns:

numpy.ndarray

Return type:

ndarray[tuple[Any, …], dtype[float64]]

count(data)[source]#

Return the number of finite source values in each target cell.

Parameters:

data (ArrayLike)

Return type:

ndarray[tuple[Any, …], dtype[float64]]

sum(data)[source]#

Return the sum of the finite source values in each target cell.

Parameters:

data (ArrayLike)

Return type:

ndarray[tuple[Any, …], dtype[float64]]

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 (Grid or pyresample geometry) – Source and target grids.

  • target_geo_def (Grid or pyresample 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, from kdquery(). When given, the binning uses it instead of BinResampler.

  • coarse_fill (bool, optional) – Fill target cells without data by nearest neighbour. Default True.

Returns:

numpy.ndarray – Field on the target grid.

Return type:

ndarray[tuple[Any, …], dtype[float64]]

See also

regrid

the same for xarray objects.

BinResampler

reuses 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 (Grid or pyresample geometry)

  • target_geo_def (Grid or pyresample 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 (Grid or pyresample geometry) – Grids (see seagrid.grids.as_grid()).

  • target (Grid or pyresample geometry) – Grids (see seagrid.grids.as_grid()).

  • radius_of_influence (float, optional) – Search radius in metres. Default default_radius() of the source grid.

  • fill_value (float or None, 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:

ndarray[tuple[Any, …], dtype[Any]]

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.DataArray or xarray.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 geometry or grid name) – Grid of the result (see seagrid.grids.as_grid()).

  • source (Grid or pyresample 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 for method="bin". Default "mean".

  • fill (bool, optional) – For method="bin": fill empty target cells by nearest neighbour. Default False.

  • radius_of_influence (float, optional) – Search radius in metres for method="nearest" and fill. Default default_radius() of the source grid.

Returns:

xarray.DataArray or xarray.Dataset – On target, with lat/lon coordinates and the regridding recorded in attrs["seagrid_history"].

Raises:

ValueError – If method or stat is unknown, or obj does not span the source grid.

Return type:

DataArray | Dataset

See also

BinResampler, nearest