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
|
Return the grid columns, rows and distances of positions. |
Classes
|
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:
objectMatch point observations to a gridded data source.
- Parameters:
data_source (
DataSource,str,moduleorcallable) – A data source (seagrid.get_source("ostia")), its name ("ostia"), its module (seagrid.ostia), a 2.x-style module withsetup_grid()andopen_dataset(dtm=..., **kw), or a callable returning anxarray.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 foropen_dataset(and 2.xsetup_grid), e.g.{"timetype": "monthly"}.grid (
Gridorpyresample 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 (
DataSourceorNone) – 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) – Withnei > 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 asself.ivecandself.jvec.
- Returns:
ivec, jvec (
numpy.ndarrayofint) – Columns and rows;-1where unmatched.dist (
numpy.ndarray) – Only if return_dist.
- Return type:
- 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:
- 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 (anythingpandas.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_behindfirst).days_ahead (
int, optional) – Also sample this many days before and after each observation; the result then has one column per day (days_behindfirst).
- 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:
- 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. Withnei > 1andall_nei=Falsethe nearest finite neighbour is returned.
- sameday_df(df, data_var, nei=1, all_nei=False, keep_ij=False)[source]#
Add a column data_var to df (
lon,lat, DatetimeIndex).
- 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:
- Return type:
ndarray[tuple[Any, …], dtype[datetime64]]
- 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 geometryordata source) – The grid (seeseagrid.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:
i, j (
numpy.ndarrayofint) – Columns and rows, shape(n,)or(n, nei)(nearest first).dist (
numpy.ndarray) – Distance in metres.
- Return type:
tuple[ndarray[tuple[Any, …], dtype[int64]], ndarray[tuple[Any, …], dtype[int64]], ndarray[tuple[Any, …], dtype[float64]]]
See also