Skip to content

Commit

Permalink
Progress on implementing relative altitude filter
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbjoernl committed Sep 4, 2024
1 parent 0f2427b commit 461f1fc
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
6 changes: 1 addition & 5 deletions src/pyaro/timeseries/AutoFilterReaderEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ def supported_filters(cls) -> list[Filter]:
:return: list of filters
"""
<<<<<<< HEAD
supported = "variables,stations,countries,bounding_boxes,duplicates,time_bounds,time_resolution,flags,time_variable_station,altitude".split(
=======
supported = "variables,stations,countries,bounding_boxes,duplicates,time_bounds,time_resolution,flags,time_variable_station,relaltitude".split(
>>>>>>> 0ad9bdf (Boilerplate)
supported = "variables,stations,countries,bounding_boxes,duplicates,time_bounds,time_resolution,flags,time_variable_station,altitude,relaltitude".split(
","
)
return [filters.get(name) for name in supported]
Expand Down
57 changes: 53 additions & 4 deletions src/pyaro/timeseries/Filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any

import numpy as np
import xarray as xr

from .Data import Data, Flag
from .Station import Station
Expand Down Expand Up @@ -857,21 +858,69 @@ def filter_stations(self, stations: dict[str, Station]) -> dict[str, Station]:

return stations

@registered_filter
class RelativeAltitudeFilter(StationFilter):
"""
Filter class which filters stations based on the relative difference between
the station altitude, and the model topography altitude.
https://github.com/metno/pyaro/issues/39
"""
def __init__(self):
pass
def __init__(self, topo_file: str = "/lustre/storeB/project/fou/kl/emep/Auxiliary/topography.nc", topo_var: str = "topography", rtol: float = 1):
"""
:param topo_file : A .nc file from which to read model topography data.
:param topo_var : Name of variable that stores altitude.
:param rtol : Relative toleranse.
"""
self._file = topo_file
self._topo_var = topo_var
self._rtol = rtol

self._topography = xr.open_dataset(self._file)

def _model_altitude_from_lat_lon(self, lat: float, lon: float) -> float:
data = self._topography.sel({"lat": lat, "lon": lon}, method="nearest")

# Should not vary in time too much so picking the first one here.
altitude = data["topography"][0]

return float(altitude)

def _is_close(self, altmod: float, altobs: float) -> bool:
"""
Function to check if two altitudes are within a relative tolerance of each
other.
:param altmod : Model altitude (in meters).
:param altobs : Observation / station altitude (in meters).
:returns :
True if the values are close with station altitude as the reference
value.
"""
return abs(altmod-altobs) <= (self._rtol * abs(altobs))

def init_kwargs(self):
return {}
return {
"topo_file": self._file,
"topo_var": self._topo_var,
"rtol": self._rtol
}

def name(self):
return "relaltitude"

def filter_stations(self, stations: dict[str, Station]) -> dict[str, Station]:
return stations
new_stations = dict()

for name, station in stations.items():
lat = station["latitude"]
lon = station["longitude"]

altobs = station["altitude"]
altmod = self._model_altitude_from_lat_lon(lat, lon)

if self._is_close(altmod, altobs):
new_stations[name] = station

return new_stations
23 changes: 23 additions & 0 deletions tests/test_CSVTimeSeriesReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,29 @@ def test_altitude_filter_3(self):
}
) as ts:
self.assertEqual(len(ts.stations()), 1)

def test_relaltitude_filter_1(self):
engines = pyaro.list_timeseries_engines()
with engines["csv_timeseries"].open(
filename=self.elevation_file,
filters=[pyaro.timeseries.filters.get("relaltitude")],
columns={
"variable": 0,
"station": 1,
"longitude": 2,
"latitude": 3,
"value": 4,
"units": 5,
"start_time": 6,
"end_time": 7,
"altitude": 9,
"country": "NO",
"standard_deviation": "NaN",
"flag": "0",
}
) as ts:
ts.stations()
self.assertTrue(True)


if __name__ == "__main__":
Expand Down

0 comments on commit 461f1fc

Please sign in to comment.