Skip to content

Commit

Permalink
Merge pull request #103 from wavespectra/nearest_missing
Browse files Browse the repository at this point in the history
Added options to missing for nearest select
  • Loading branch information
rafa-guedes committed Dec 5, 2023
2 parents b313f68 + f4f6fa3 commit 18cd912
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions wavespectra/core/select.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Interpolate stations."""
import logging

import numpy as np
import xarray as xr
import logging

from wavespectra.core.attributes import attrs, set_spec_attributes


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -119,12 +119,12 @@ def nearer(self, lon, lat, tolerance=np.inf, max_sites=None):
def nearest(self, lon, lat):
"""Nearest station in (dset_lons, dset_lats) to site (lon, lat).
Args:
lon (float): Longitude to locate from lons.
lat (float): Latitude to locate from lats.
Args:
lon (float): Longitude to locate from lons.
lat (float): Latitude to locate from lats.
Returns:
Index and distance of closest station.
Returns:
Index and distance of closest station.
"""
dist = self.distance(lon, lat)
Expand All @@ -142,6 +142,7 @@ def sel_nearest(
exact=False,
dset_lons=None,
dset_lats=None,
missing="raise",
):
"""Select sites from nearest distance.
Expand All @@ -154,6 +155,9 @@ def sel_nearest(
exact (bool): Require exact matches.
dset_lons (array): Longitude of stations in dset.
dset_lats (array): Latitude of stations in dset.
missing (str): Action to take if no site is found within tolerance:
- 'raise': raise an error
- 'ignore': skip site
Returns:
Selected SpecDataset at locations defined by (lons, lats).
Expand All @@ -170,10 +174,17 @@ def sel_nearest(
for lon, lat in zip(coords.lons, coords.lats):
closest_id, closest_dist = coords.nearest(lon, lat)
if closest_dist > tolerance:
raise AssertionError(
f"Nearest site from (lat={lat}, lon={lon}) is {closest_dist:g} "
f"deg away but tolerance is {tolerance:g} deg."
)
if missing == "raise":
raise AssertionError(
f"Nearest site from (lat={lat}, lon={lon}) is {closest_dist:g} "
f"deg away but tolerance is {tolerance:g} deg."
)
elif missing == "ignore":
logger.debug(
f"No site within tolerance={tolerance} deg from (lat={lat}, "
f"lon={lon}), skipping"
)
continue
if exact and closest_dist > 0:
raise AssertionError(
f"Exact match required but no site at (lat={lat}, lon={lon}), "
Expand Down

0 comments on commit 18cd912

Please sign in to comment.