environmentaltools.common.xrnearest

environmentaltools.common.xrnearest(ds, lat, lon, lat_name=None, lon_name=None, variable_mask=None, time_mask=0)[source]

Find the nearest grid point to specified coordinates in xarray dataset.

Locates the dataset grid point closest to the given latitude and longitude coordinates, optionally applying a mask to exclude invalid data points.

Parameters:
  • ds (xarray.Dataset) – Input dataset with coordinate information.

  • lat (float) – Target latitude coordinate.

  • lon (float) – Target longitude coordinate.

  • lat_name (str, optional) – Name of latitude variable. Auto-detected if None.

  • lon_name (str, optional) – Name of longitude variable. Auto-detected if None.

  • variable_mask (str, optional) – Variable name to use for masking NaN values.

  • time_mask (int) – Time index to use for masking. Defaults to 0.

Returns:

Subset of dataset at the nearest grid point.

Return type:

xarray.Dataset

Raises:

Exception – If coordinate dimensions are not found in dataset.