Skip to content

Commit 8bba516

Browse files
authored
Make the Reservoir parser more robust to missing optional fields. (#544)
### What kind of change does this PR introduce? * Avoid parser failures when optional fields are missing. The original code was designed too closely on one specific example. ### Does this PR introduce a breaking change? No
2 parents c3e2b7d + 8b0b467 commit 8bba516

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Contributors: David Huard (:user:`huard`), Trevor James Smith (:user:`Zeitsperre
99

1010
Fixes
1111
^^^^^
12+
* Fix `Reservoir` command parser failing when optional fields were missing. (PR #544)
1213
* Add `GaugedSubBasinGroup` command to `RVH` class. (Issue #546, PR #547)
1314
* In `nc_specs`, set `dim_names_nc` in the order expected by Raven (x, y, t). Previously, we only made sure that `time` was the last dimension, but did not ensure x and y were in the right order. (PR #533)
1415
* Adjusted the `Perform_a_climate_change_impact_study_on_a_watershed.ipynb` notebook to reduce the number of years in the simulation to speed up execution time. (PR #535)

src/ravenpy/config/commands.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -613,34 +613,30 @@ def parse(cls, s) -> list:
613613
pat = r":Reservoir\s+(\w+)\s+(.+?):EndReservoir"
614614
out = []
615615

616+
a = {}
616617
for name, content in re.findall(pat, s.strip(), re.DOTALL):
617618
# Extract parameters
618-
subbasin_id = re.search(r":SubBasinID\s+(\d+)", content).group(1)
619-
hru_id = re.search(r":HRUID\s+(\d+)", content).group(1)
620-
type_ = re.search(r":Type\s+(\w+)", content).group(1)
621-
weir_coefficient = re.search(r":WeirCoefficient\s+([\d.-]+)", content).group(1)
622-
crest_width = re.search(r":CrestWidth\s+([\d.-]+)", content).group(1)
623-
max_depth = re.search(r":MaxDepth\s+([\d.-]+)", content).group(1)
624-
lake_area = re.search(r":LakeArea\s+([\d.-]+)", content).group(1)
625-
seepage_parameters = re.search(r":SeepageParameters\s+([\d.-]+)\s+([\d.-]+)", content).groups()
619+
a["subbasin_id"] = re.search(r":SubBasinID\s+(\d+)", content).group(1)
620+
a["hru_id"] = re.search(r":HRUID\s+(\d+)", content).group(1)
621+
a["type"] = re.search(r":Type\s+(\w+)", content).group(1)
622+
623+
if m := re.search(r":WeirCoefficient\s+([\d.-]+)", content):
624+
a["weir_coefficient"] = m.group(1)
625+
626+
if m := re.search(r":CrestWidth\s+([\d.-]+)", content):
627+
a["crest_width"] = m.group(1)
628+
629+
if m := re.search(r":MaxDepth\s+([\d.-]+)", content):
630+
a["max_depth"] = m.group(1)
631+
632+
if m := re.search(r":LakeArea\s+([\d.-]+)", content):
633+
a["lake_area"] = m.group(1)
634+
635+
if m := re.search(r":SeepageParameters\s+([\d.-]+)\s+([\d.-]+)", content):
636+
a["seepage_parameters"] = dict(zip(["k_seep", "h_ref"], m.groups()))
626637

627638
# Convert to Reservoir record
628-
out.append(
629-
cls(
630-
name=name,
631-
subbasin_id=subbasin_id,
632-
hru_id=hru_id,
633-
type=type_,
634-
weir_coefficient=weir_coefficient,
635-
crest_width=crest_width,
636-
max_depth=max_depth,
637-
lake_area=lake_area,
638-
seepage_parameters=cls.SeepageParameters(
639-
k_seep=seepage_parameters[0],
640-
h_ref=seepage_parameters[1],
641-
),
642-
)
643-
)
639+
out.append(cls(name=name, **a))
644640
return out
645641

646642

@@ -672,7 +668,7 @@ def parse(cls, s) -> list:
672668
out = []
673669

674670
for name, content in re.findall(pat, s.strip(), re.DOTALL):
675-
sb_ids = re.split(r"\s+", content.strip())
671+
sb_ids = re.findall(r"\d+", content)
676672

677673
# Convert to SubBasinGroup record
678674
out.append(SubBasinGroup(name=name, sb_ids=sb_ids))

0 commit comments

Comments
 (0)