Skip to content

Commit

Permalink
Merge branch 'master' into fix-coveralls
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre authored Dec 12, 2023
2 parents 321903f + 59ed853 commit a4aad46
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
History
=======

0.13 (unreleased)
-----------------
* Fixed problem with scalar elevation in netCDF files parsed with `nc_specs` (issue #279)


0.12.3 (2023-10-02)
-------------------
* `RavenPy` now uses `platformdirs` to write `raven_testing` to the user's cache directory. Dynamic paths are now used to cache data dependent on the user's operating system. Developers can now safely delete the `.raven_testing_data` folder in their home directory without affecting the functionality of `RavenPy`.
Expand Down
8 changes: 6 additions & 2 deletions ravenpy/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,17 @@ def nc_specs(

try:
nc_elev = ds.cf["vertical"].name
attrs["elevation"] = ds.cf["vertical"][i]
except KeyError:
nc_elev = "elevation" if "elevation" in ds else None

finally:
if nc_elev is not None:
attrs["elevation_var_name_nc"] = nc_elev
attrs["elevation"] = ds["elevation"][i]
try:
attrs["elevation"] = ds[nc_elev][i]
except IndexError:
# Elevation is a scalar
attrs["elevation"] = ds[nc_elev].item(0)

if "station_id" in ds:
if ds["station_id"].shape and len(ds["station_id"]) > i:
Expand Down
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ def fit_params(ts_stats, tmp_path):
return fn


@pytest.fixture(scope="session")
def bad_netcdf(salmon_meteo):
fn = salmon_meteo.parent / "bad_netcdf.nc"

if not fn.exists():
# Test with scalar elevation. Should normally have a station dimension, but not always the case.
ds = xr.open_dataset(salmon_meteo)
ds["station_name"] = ds["station_name"].astype("str")
ds["elevation"] = 1.0
ds.to_netcdf(fn)

return fn


@pytest.fixture(scope="session")
def salmon_hru():
out = {}
Expand Down
7 changes: 7 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import xarray as xr

from ravenpy.config.utils import nc_specs

Expand All @@ -11,6 +12,12 @@ def test_nc_specs(get_local_testdata):
assert "file_name_nc" in attrs


def test_nc_specs_bad(bad_netcdf):
# Test with scalar elevation. Should normally have a station dimension, but not always the case.
attrs_s = nc_specs(bad_netcdf, "PRECIP", station_idx=1, alt_names=("rain",))
assert attrs_s["elevation"] == 1.0


@pytest.mark.online
def test_dap_specs():
# Link to THREDDS Data Server netCDF testdata
Expand Down

0 comments on commit a4aad46

Please sign in to comment.