iconarray.core.utilities.ind_from_latlon#
- iconarray.core.utilities.ind_from_latlon(lon_array: xarray.DataArray, lat_array: xarray.DataArray, lon_point: float, lat_point: float, n: int = 1, verbose: bool = False) List[int] #
Find the indices of the n closest cells in a grid, relative to a given latitude/longitude point.
The function builds a BallTree from the provided 1D or 2D array of longitude and latitude coordinates, and queries it to find the n nearest neighbors to the given point.
- Parameters:
lon_array (
xr.DataArray
) – A 1D or 2D xr of longitude values [degree].lat_array (
xr.DataArray
) – A 1D or 2D xr of latitude values [degree].lon_point (
float
) – The longitude value [degree] of the point to find the closest point(s) to.lat_point (
float
) – The latitude value [degree] of the point to find the closest point(s) to.n (
int
, optional) – The number of closest points to return. Default is 1.verbose (
bool
, optional) – Print information. Defaults to False.
- Returns:
List[int]
– The indices of the closest n points to the given point.
Example
>>> # Get values of grid cell closest to coordinate >>> # E.g. Zürich: >>> lon = 8.54 >>> lat = 47.38 >>> lats = ds.clat >>> lons = ds.clon >>> if ds.clat.attrs.get('units') == 'radian': >>> lats = np.rad2deg(lats) >>> lons = np.rad2deg(lons) >>> ind = iconarray.ind_from_latlon( ... lats,lons,lat,lon, ... verbose=True, n=1 ... )
>>> ind 3352 # Closest ind: 3352 # Given lat: 47.380 deg. Closest 1 lat/s found: 47.372 # Given lon: 8.540 deg. Closest 1 lon/s found: 8.527