diff --git a/src/pyaro/timeseries/Filter.py b/src/pyaro/timeseries/Filter.py index 4838a3a..14e086a 100644 --- a/src/pyaro/timeseries/Filter.py +++ b/src/pyaro/timeseries/Filter.py @@ -1315,14 +1315,22 @@ def _calculate_relative_altitude( # At most one degree of latitude (at equator) is roughly 111km. # Subsetting to based on this value with safety margin makes the # distance calculation MUCH more efficient. - s = 0.1 + (radius / 1000) / 100 - topo = topo.sel(lon=slice(lon - s, lon + s), lat=slice(lat - s, lat + s)) + if radius < 100_000: + margin = 0.1 + (radius / 1_000) / 100 + if lat >= 88 or lat <= -88: + # Include 360deg longitude near poles because poles are weird. + topo = topo.sel(lat=slice(lat - margin, lat + margin)) + else: + topo = topo.sel( + lon=slice(lon - margin, lon + margin), + lat=slice(lat - margin, lat + margin), + ) distances = haversine(topo["lon"], topo["lat"], lon, lat) within_radius = distances <= radius values_within_radius = topo[self._topo_var].where( - within_radius, other=False, drop=True + within_radius, other=np.nan, drop=True ) min_value = float(values_within_radius.min(skipna=True)) diff --git a/tests/test_CSVTimeSeriesReader.py b/tests/test_CSVTimeSeriesReader.py index 20b264b..f73320f 100644 --- a/tests/test_CSVTimeSeriesReader.py +++ b/tests/test_CSVTimeSeriesReader.py @@ -697,7 +697,7 @@ def test_valley_floor_filter(self): "flag": "0", }, ) as ts: - self.assertEqual(len(ts.stations()), 1) + self.assertEqual(len(ts.stations()), 3) if __name__ == "__main__":