Skip to content

Commit

Permalink
Add tests and test topography data
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbjoernl committed Sep 5, 2024
1 parent da22b65 commit 0c02e39
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
14 changes: 14 additions & 0 deletions scripts/make_topography_subset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import xarray as xr

# Script which extracts only the first time point and topography value from the emep topography
# file to reduce data for the tests.
data = xr.open_dataset("/lustre/storeB/project/fou/kl/emep/Auxiliary/topography.nc")

start_time = data["time"][0]

data = data.sel(time=slice(start_time))

data = data["topography"]


data.to_netcdf("tests/testdata/datadir_elevation/topography.nc")
1 change: 1 addition & 0 deletions src/pyaro/timeseries/Filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ def __init__(self, topo_file: str = "/lustre/storeB/project/fou/kl/emep/Auxiliar
self._topography = xr.open_dataset(self._file)

def _model_altitude_from_lat_lon(self, lat: float, lon: float) -> float:
# TODO: Include a tolerance?
data = self._topography.sel({"lat": lat, "lon": lon}, method="nearest")

# Should not vary in time too much so picking the first one here.
Expand Down
59 changes: 56 additions & 3 deletions tests/test_CSVTimeSeriesReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ 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")],
filters=[pyaro.timeseries.filters.get("relaltitude", topo_file = "./tests/testdata/datadir_elevation/topography.nc", rtol=0)],
columns={
"variable": 0,
"station": 1,
Expand All @@ -506,8 +506,61 @@ def test_relaltitude_filter_1(self):
"flag": "0",
}
) as ts:
ts.stations()
self.assertTrue(True)
# Altitudes in dataset:
# Station | Alt_obs | Modeobs |
# Station 1 | 100 | 12.2554 |
# Station 2 | 200 | 4.9016 |
# Station 3 | 300 | 4.9016 |
# Since rtol = 0, no station should be included.
self.assertEqual(len(ts.stations()), 0)

def test_relaltitude_filter_2(self):
engines = pyaro.list_timeseries_engines()
with engines["csv_timeseries"].open(
filename=self.elevation_file,
filters=[pyaro.timeseries.filters.get("relaltitude", topo_file = "./tests/testdata/datadir_elevation/topography.nc", rtol=0.89)],
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:
# At rtol = 0.89, only the first station should be included.
self.assertEqual(len(ts.stations()), 1)

def test_relaltitude_filter_3(self):
engines = pyaro.list_timeseries_engines()
with engines["csv_timeseries"].open(
filename=self.elevation_file,
filters=[pyaro.timeseries.filters.get("relaltitude", topo_file = "./tests/testdata/datadir_elevation/topography.nc", rtol=1)],
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:
# Since rtol=1, all stations should be included.
self.assertEqual(len(ts.stations()), 3)




if __name__ == "__main__":
Expand Down
Binary file added tests/testdata/datadir_elevation/topography.nc
Binary file not shown.

0 comments on commit 0c02e39

Please sign in to comment.