Skip to content

Commit

Permalink
Merge pull request #140 from ImperialCollegeLondon/overrides-bug
Browse files Browse the repository at this point in the history
Overrides bug
  • Loading branch information
barneydobson authored Jan 7, 2025
2 parents d05486d + 0c30156 commit 7167ead
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
12 changes: 11 additions & 1 deletion tests/test_land.py
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,14 @@ def test_apply_surface_overrides(tmp_path):
import yaml
from wsimod.orchestration.model import Model

with (tmp_path / "fake_surface_inputs.csv").open("w") as f:
f.write("node,surface,variable,time,value\n")
f.write("land1,Woodland,srp-dry,2000-01-01,1\n")

with (tmp_path / "fake_inputs.csv").open("w") as f:
f.write("node,variable,time,value\n")
f.write("land1,et0,2000-01-01,1\n")

config = {
"arcs": {
"arc1": {
Expand All @@ -1447,20 +1455,23 @@ def test_apply_surface_overrides(tmp_path):
"name": "land1",
"type_": "Land",
"percolation_residence_time": 0.1,
"filename": str(tmp_path / "fake_inputs.csv"),
"surfaces": {
"Woodland": {
"area": 100,
"datum": 10,
"type_": "GrowingSurface",
"ET_depletion_factor": 0.75,
"surface": "Woodland",
"filename": str(tmp_path / "fake_surface_inputs.csv"),
},
"Grass": {
"area": 200,
"datum": 20,
"type_": "GrowingSurface",
"ET_depletion_factor": 0.75,
"surface": "Grass",
"filename": str(tmp_path / "fake_surface_inputs.csv"),
},
},
},
Expand All @@ -1482,7 +1493,6 @@ def test_apply_surface_overrides(tmp_path):
"surfaces": {
"Woodland": {
"surface": "Woodland",
"type_": "GrowingSurface",
"area": 1000,
"ET_depletion_factor": 0.8,
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def test_data_overrides(self):
)
input_data = pd.read_csv(data_path)

overrides = {"data_input_dict": data_path}
overrides = {"filename": data_path}
node = Node(name="")
node.apply_overrides(overrides)
node.t = list(node.data_input_dict.keys())[0][1]
Expand All @@ -440,7 +440,7 @@ def test_data_overrides(self):
node.get_data_input("temperature"),
)
# test runtime error
self.assertRaises(RuntimeError, lambda: node.apply_overrides({}))
self.assertRaises(RuntimeError, lambda: node.apply_overrides({"filename": 123}))


if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions wsimod/nodes/land.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,12 @@ def apply_overrides(self, overrides=Dict[str, Any]):
self.surface_runoff.residence_time = self.surface_residence_time
self.subsurface_runoff.residence_time = self.subsurface_residence_time
self.percolation.residence_time = self.percolation_residence_time
super().apply_overrides(overrides)

for surface, override in overrides.get("surfaces", {}).items():
for surface, override in overrides.pop("surfaces", {}).items():
self.get_surface(surface).apply_overrides(override)

super().apply_overrides(overrides)

def apply_irrigation(self):
"""Iterate over any irrigation functions (needs further testing..
Expand Down
2 changes: 1 addition & 1 deletion wsimod/nodes/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def apply_overrides(self, overrides: Dict[str, Any] = {}) -> None:
# overrides data_input_dict
from wsimod.orchestration.model import read_csv

content = overrides.pop("data_input_dict", self.data_input_dict)
content = overrides.pop("filename", None)
if isinstance(content, str):
self.data_input_dict = read_csv(content)
elif not content:
Expand Down

0 comments on commit 7167ead

Please sign in to comment.