seagrid.match#

Match point observations (cruise stations, floats, tracks) to gridded data.

Match finds the grid cell of every observation once, then opens each time step of the data source once and samples all observations of that time step. Data sources offer the same as seagrid.sources.DataSource.match():

from seagrid import ostia

df["sst"] = ostia.match(df.lon, df.lat, df.index, "analysed_sst")

Observations are grouped by the source’s time step: with a monthly source, all observations in June 2010 are matched to the June 2010 field.

Examples

>>> import numpy as np, pandas as pd
>>> from seagrid import match, synthetic
>>> mt = match.Match(synthetic)
>>> mt.sameday([10.0], [40.0], pd.DatetimeIndex(["2011-01-01"]), "lats")
array([40.])

Functions

ll2ij(grid, lon, lat, *[, nei, max_distance])

Return the grid columns, rows and distances of positions.

Classes

Match(data_source[, lonname, latname, dskw, ...])

Match point observations to a gridded data source.

class seagrid.match.Match(data_source, lonname='lon', latname='lat', dskw=None, *, grid=None, max_distance=None, progress=False)[source]#

Bases: object

Match point observations to a gridded data source.

Parameters:
  • data_source (DataSource, str, module or callable) – A data source (seagrid.get_source("ostia")), its name ("ostia"), its module (seagrid.ostia), a 2.x-style module with setup_grid() and open_dataset(dtm=..., **kw), or a callable returning an xarray.Dataset.

  • lonname (str, optional) – Coordinate names, for a callable data source.

  • latname (str, optional) – Coordinate names, for a callable data source.

  • dskw (dict, optional) – Keywords for open_dataset (and 2.x setup_grid), e.g. {"timetype": "monthly"}.

  • grid (Grid or pyresample geometry, optional) – The data grid. Default: the source’s grid.

  • max_distance (float, optional) – Observations farther than this (metres) from the nearest cell centre get NaN. Default: no limit.

  • progress (bool, optional) – Show a progress bar while opening time steps. Default False.

Variables:
  • grid (seagrid.grids.Grid) – The data grid.

  • source (DataSource or None) – The data source, when data_source is one.

ll2ij(lonvec, latvec, nei=1, all_nei=False, return_dist=False, ball=True, keep_ij=False)[source]#

Return the grid columns and rows of positions.

Parameters:
  • lonvec (array_like) – Positions in decimal degrees.

  • latvec (array_like) – Positions in decimal degrees.

  • nei (int, optional) – Number of nearest cells. Default 1.

  • all_nei (bool, optional) – With nei > 1: return all nei cells (shape (n, nei)) instead of only the nei-th nearest.

  • return_dist (bool, optional) – Also return the distances, in metres.

  • ball (bool, optional) – Ignored; distances are always great-circle distances. Kept for 2.x code.

  • keep_ij (bool, optional) – Store the result as self.ivec and self.jvec.

Returns:

Return type:

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

ll2ij_kd(lonvec, latvec, **kwargs)[source]#

Return ll2ij(); the 2.x name.

Parameters:
  • lonvec (ArrayLike)

  • latvec (ArrayLike)

  • kwargs (Any)

Return type:

Any

ll2ij_ball(lonvec, latvec, **kwargs)[source]#

Return ll2ij(); the 2.x name.

Parameters:
  • lonvec (ArrayLike)

  • latvec (ArrayLike)

  • kwargs (Any)

Return type:

Any

static nearest_nei(data_arr)[source]#

Return the nearest finite value of each row of a neighbour array.

Parameters:

data_arr (numpy.ndarray) – Values of shape (n_obs, n_nei), nearest neighbour first, or (n_obs,) (returned unchanged).

Returns:

numpy.ndarray – Shape (n_obs,); NaN where no neighbour is finite.

Return type:

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

sample(lon, lat, time, data_var=None, *, nei=1, all_nei=False, days_behind=0, days_ahead=0)[source]#

Return the gridded value at each observation.

Parameters:
  • lon (array_like) – Positions in decimal degrees.

  • lat (array_like) – Positions in decimal degrees.

  • time (array_like) – Time of each observation (anything pandas.to_datetime() accepts).

  • data_var (str, optional) – Variable. Default: the source’s default variable.

  • nei (int, optional) – Number of nearest cells to consider. Default 1.

  • all_nei (bool, optional) – Return the values of all nei cells (shape (n, nei)) instead of the nearest finite one. Only without a time window.

  • days_behind (int, optional) – Also sample this many days before and after each observation; the result then has one column per day (days_behind first).

  • days_ahead (int, optional) – Also sample this many days before and after each observation; the result then has one column per day (days_behind first).

Returns:

numpy.ndarray – Shape (n,); (n, nei) with all_nei; (n, n_days) with a time window. NaN where there is no data. Extra dimensions of the field (e.g. depth) are added at the end.

Return type:

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

sameday(lonvec, latvec, dtmvec, data_var, nei=1, all_nei=False, keep_ij=False)[source]#

Return the value of data_var at each observation’s time step.

As sample() with no time window. With nei > 1 and all_nei=False the nearest finite neighbour is returned.

Parameters:
  • lonvec (ArrayLike)

  • latvec (ArrayLike)

  • dtmvec (ArrayLike)

  • data_var (str)

  • nei (int)

  • all_nei (bool)

  • keep_ij (bool)

Return type:

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

sameday_df(df, data_var, nei=1, all_nei=False, keep_ij=False)[source]#

Add a column data_var to df (lon, lat, DatetimeIndex).

Parameters:
  • df (DataFrame)

  • data_var (str)

  • nei (int)

  • all_nei (bool)

  • keep_ij (bool)

Return type:

None

static dtm_array(dtmvec, days_ahead=0, days_behind=0)[source]#

Return the dates from days_behind before to days_ahead after each date.

Returns:

numpy.ndarray – Shape (n, days_behind + 1 + days_ahead).

Parameters:
  • dtmvec (DatetimeIndex)

  • days_ahead (int)

  • days_behind (int)

Return type:

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

multiday(lonvec, latvec, dtmvec, data_var, days_ahead=0, days_behind=0, nei=9, all_nei=True)[source]#

Return values over a window of days, nearest finite of nei cells.

As sample() with days_behind/days_ahead; all_nei is ignored (kept for 2.x code).

Returns:

numpy.ndarray – Shape (n, days_behind + 1 + days_ahead).

Parameters:
  • lonvec (ArrayLike)

  • latvec (ArrayLike)

  • dtmvec (ArrayLike)

  • data_var (str)

  • days_ahead (int)

  • days_behind (int)

  • nei (int)

  • all_nei (bool)

Return type:

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

seagrid.match.ll2ij(grid, lon, lat, *, nei=1, max_distance=None)[source]#

Return the grid columns, rows and distances of positions.

Parameters:
  • grid (Grid, pyresample geometry or data source) – The grid (see seagrid.grids.as_grid()).

  • lon (array_like) – Positions in decimal degrees.

  • lat (array_like) – Positions in decimal degrees.

  • nei (int, optional) – Number of nearest cells per position. Default 1.

  • max_distance (float, optional) – Cells farther away than this (metres) are not matched: their index is -1. Default: no limit.

Returns:

Return type:

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