netsse.tools.misc_func.find_nearest_gridpoint#
- netsse.tools.misc_func.find_nearest_gridpoint(lat_wps, lon_wps, lat_grid, lon_grid, interv=0.01)#
Finds the nearest neighbours to a series of waypoints within a grid of points.
Note
The grid points do not need to be uniformly spaced in any direction. The grid can also have been rotated of any angle about the vertical direction (e.g., to be aligned with some shoreline).
For a given waypoint at
(lat_wp,lon_wp)
, the nearest-neighbour candidates are first selected within the gridpoints that fall in the geographical area delimited bylat_wp*[1-interv,1+interv]
in latitude andlon_wp*[1-interv,1+interv]
in longitude. Then, the distance between the waypoint and the nearest-neighbour candidates is computed. The output neighbour is found as the candidate that minimises this distance.- Parameters:
lat_wps (float, or 1d-array of shape (Nwp,)) – Vector of waypoint latitudes [deg].
lon_wps (float, or 1d-array of shape (Nwp,)) – Vector of waypoint longitudes [deg].
lat_grid (2d-array) – Matrix defining the latitudes of the grid points [deg].
lon_grid (2d-array) – Matrix defining the longitudes of the grid points [deg].
interv (float, default 0.01) – Half-width [-] of the interval band fraction in latitude and longitude, used for screening the potential nearest-neighbour candidates.
- Returns:
index_nearest (numpy.array of shape (Nwp,2)) – The coordinates in (latitude,longitude) of the nearest gridpoints to the waypoints [deg].
dist_nearest (numpy.array of shape (Nwp,)) – The distance between the nearest gridpoints and the waypoints [km].
Example
>>> index_nearest, dist_nearest = ... find_nearest_gridpoint(lat_wps, lon_wps, lat_grid, lon_grid, interv=0.01)