Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ and produce a hydrograph.
In condensed code:

```python
forcing = ewatercycle.forcing.sources['MarrmotForcing'].generate(...)
model = ewatercycle.models.sources['MarrmotM14'](forcing)
forcing = ewatercycle.forcing.sources["MarrmotForcing"].generate(...)
model = ewatercycle.models.sources["MarrmotM14"](forcing)
model.setup(...)
model.initialize()
while (model.time < model.end_time):
while model.time < model.end_time:
model.update()
value = model.get_value_as_xarray('flux_out_Q')
value = model.get_value_as_xarray("flux_out_Q")
model.finalize()
ewatercycle.analysis.hydrograph(...)
```
Expand All @@ -98,14 +98,14 @@ from ewatercycle.testing.fixtures import rhine_shape
import shapefile
import xarray as xr

forcing = ewatercycle.forcing.sources['MarrmotForcing'].generate(
dataset='ERA5',
start_time='2010-01-01T00:00:00Z',
end_time='2010-12-31T00:00:00Z',
shape=rhine_shape()
forcing = ewatercycle.forcing.sources["MarrmotForcing"].generate(
dataset="ERA5",
start_time="2010-01-01T00:00:00Z",
end_time="2010-12-31T00:00:00Z",
shape=rhine_shape(),
)

model = ewatercycle.models.sources['MarrmotM14'](version='2020.11', forcing=forcing)
model = ewatercycle.models.sources["MarrmotM14"](version="2020.11", forcing=forcing)

cfg_file, cfg_dir = model.setup(
threshold_flow_generation_evap_change=0.1,
Expand All @@ -115,31 +115,29 @@ model.initialize(cfg_file)

# flux_out_Q unit conversion factor from mm/day to m3/s
sf = shapefile.Reader(rhine_shape())
area = sf.record(0)['SUB_AREA'] * 1e6 # from shapefile in m2
area = sf.record(0)["SUB_AREA"] * 1e6 # from shapefile in m2
conversion_mmday2m3s = 1 / (1000 * 24 * 60 * 60)
conversion = conversion_mmday2m3s * area

simulated_discharge = []
while (model.time < model.end_time):
while model.time < model.end_time:
model.update()
simulated_discharge.append(
model.get_value_as_xarray('flux_out_Q')
)
simulated_discharge.append(model.get_value_as_xarray("flux_out_Q"))

observations_ds = ewatercycle.observation.grdc.get_grdc_data(
station_id=6335020, # Rees, Germany
start_time=model.start_time_as_isostr,
end_time=model.end_time_as_isostr,
column='observation',
column="observation",
)

# Combine the simulated discharge with the observations
sim_da = xr.concat(simulated_discharge, dim='time') * conversion
sim_da.name = 'simulated'
sim_da = xr.concat(simulated_discharge, dim="time") * conversion
sim_da.name = "simulated"
discharge = xr.merge([sim_da, observations_ds["observation"]]).to_dataframe()
discharge = discharge[["observation", "simulated"]].dropna()

ewatercycle.analysis.hydrograph(discharge, reference='observation')
ewatercycle.analysis.hydrograph(discharge, reference="observation")

model.finalize()
```
Expand Down
2 changes: 1 addition & 1 deletion src/ewatercycle/_forcings/caravan.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def generate( # type: ignore[override]
raise ValueError(msg)
basin_id = str(kwargs["basin_id"])

dataset: str = basin_id.split("_")[0]
dataset: str = basin_id.split("_", maxsplit=1)[0]
ds = cls.get_dataset(dataset)
ds_basin = ds.sel(basin_id=basin_id.encode())
ds_basin_time = crop_ds(ds_basin, start_time, end_time)
Expand Down
2 changes: 1 addition & 1 deletion src/ewatercycle/base/parameter_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def download_github_repo(
"""
zip_url = f"https://github.com/{org}/{repo}/archive/refs/heads/{branch}.zip"

https_response = urlopen(zip_url) # noqa: S310
https_response = urlopen(zip_url)
if https_response.status != 200:
msg = (
f"HTTP error {https_response.status}\n"
Expand Down
4 changes: 2 additions & 2 deletions src/ewatercycle/esmvaltool/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ def add_variable(
units: str | None = None,
stats: ClimateStatistics | None = None,
short_name: str | None = None,
start_year: int | None | Literal[False] = None,
end_year: int | None | Literal[False] = None,
start_year: int | Literal[False] | None = None,
end_year: int | Literal[False] | None = None,
):
"""Add a variable to the recipe.

Expand Down
9 changes: 8 additions & 1 deletion src/ewatercycle/esmvaltool/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
type="reanaly",
version=1,
),
"ERA5-Land": Dataset(
dataset="ERA5-Land",
project="OBS6",
tier=3,
type="reanaly",
version=1,
),
}
"""Dictionary of predefined forcing datasets.

Expand All @@ -28,7 +35,7 @@

>> from ewatercycle.forcing import DATASETS
>> list(DATASETS.keys())
['ERA5', 'ERA-Interim']
['ERA5', 'ERA-Interim', 'ERA5-Land']

"""

Expand Down
2 changes: 1 addition & 1 deletion src/ewatercycle/observation/caravan.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_caravan_data(
NCO: netCDF Operators version 5.0.6 (Homepage = http://nco.sf....
_NCProperties: version=2,netcdf=4.8.1,hdf5=1.10.7
""" # noqa: D214,D410,D411
dataset: str = basin_id.split("_")[0]
dataset: str = basin_id.split("_", maxsplit=1)[0]
ds = CaravanForcing.get_dataset(dataset)
ds_basin = ds.sel(basin_id=basin_id.encode())
ds_basin_time = crop_ds(ds_basin, start_time, end_time)
Expand Down
2 changes: 1 addition & 1 deletion src/ewatercycle/observation/grdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def get_grdc_data(
institution: GRDC
history: Download from GRDC Database, 21/06/2024
missing_value: -999.000
""" # noqa: D214,D410,D411
"""
if data_home:
data_path = to_absolute_path(data_home)
elif CFG.grdc_location:
Expand Down
2 changes: 1 addition & 1 deletion src/ewatercycle/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def extract_package_name(value: str) -> str:
E.g. "ewatercycle_HBV.model:HBV" will return
"ewatercycle_HBV".
"""
source = value.split(":")[0]
source = value.split(":", maxsplit=1)[0]
return source.split(".")[0]


Expand Down
4 changes: 2 additions & 2 deletions tests/src/base/test_forcing.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def test_retrieve_caravan_forcing(tmp_path: Path, mock_retrieve: mock.MagicMock)
content = list(ds.data_vars.keys())
expected = ["Q", "evspsblpot", "pr", "tas", "tasmax", "tasmin"]
assert content == expected
mock_retrieve.assert_called_once_with(basin_id.split("_")[0])
mock_retrieve.assert_called_once_with(basin_id.split("_", maxsplit=1)[0])

assert caravan_forcing.to_xarray()["evspsblpot"].attrs["unit"] == "kg m-2 s-1"
assert caravan_forcing.to_xarray()["pr"].attrs["unit"] == "kg m-2 s-1"
Expand All @@ -394,7 +394,7 @@ def test_retrieve_caravan_forcing_empty_vars(
content = list(ds.data_vars.keys())
expected = ["Q", "evspsblpot", "pr", "tas", "tasmax", "tasmin"]
assert content == expected
mock_retrieve.assert_called_once_with(basin_id.split("_")[0])
mock_retrieve.assert_called_once_with(basin_id.split("_", maxsplit=1)[0])


def test_retrieve_caravan_forcing_no_basin_id(
Expand Down
Loading