gridmarthe.sel_by_coords

gridmarthe.sel_by_coords(ds, x=None, y=None, z=None, method=None, tolerance=1000.0, return_mask=False)[source]

Filter a 1D flattened xarray Dataset by spatial (x, y) and/or z ranges.

When grid are stored as 1D vector for spatial dimension, xarray.Dataset.sel method cannont be used on x and y coordinates. This function is a workaround to select a point in the grid when coordinates (‘x’, ‘y’) are variables.

Parameters:
dsxarray.Dataset

The input dataset with 1D ‘zone’ dimension and variables ‘x’, ‘y’, ‘z’.

xfloat or tuple, optional

x-coordinate: float for point selection, (xmin, xmax) for range.

yfloat or tuple, optional

y-coordinate: float for point selection, (ymin, ymax) for range.

zint or tuple, optional

z-level: int for exact, (zmin, zmax) for range.

methodstr, optional

For point selection (x,y), use ‘nearest’ to find closest point. Requires tolerance (max distance).

tolerancefloat, optional

Max distance when using method=’nearest’ (in same units as x/y). Default is 1e3 (meters).

return_maskbool, optional

Option to return mask array

Returns:
xarray.Dataset

Filtered dataset with matching indices, still 1D.

Examples

Select by spatial box

>>> subset = sel_xy(dataset, x=(4.5e5, 5.0e5), y=(2.5e6, 2.6e6))

Select by z level

>>> subset = sel_xy(dataset, z=(1, 3))

Select by point (nearest)

>>> subset = sel_xy(dataset, x=4.35e5, y=2.58e6, method='nearest', tolerance=1e3)

Combine x, y, z

>>> subset = sel_xy(dataset, x=(4.5e5, 5.0e5), y=(2.5e6, 2.6e6), z=2)