Skip to content

Commit

Permalink
Properly store recharge models and settings (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulcollenteur committed Jan 26, 2023
1 parent c894dbf commit 4546b93
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
11 changes: 9 additions & 2 deletions pastas/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,22 @@ def _load_stressmodel(ts, data):
data["parameters"].loc[wnam + "_b", pcol]
)

# Deal with old-style response functions (TODO remove in 1.0)
if version.parse(data["file_info"]["pastas_version"]) < version.parse("0.23.0"):
if "recharge" in ts.keys():
recharge_kwargs = ts.pop("recharge_kwargs", {})
recharge_kwargs["class"] = ts["recharge"]
ts["recharge"] = recharge_kwargs

# Create and add stress model
stressmodel = getattr(ps.stressmodels, ts["stressmodel"])
ts.pop("stressmodel")
if "rfunc" in ts.keys():
rfunc_kwargs = ts.pop("rfunc_kwargs", {})
ts["rfunc"] = getattr(ps.rfunc, ts["rfunc"])(**rfunc_kwargs)
if "recharge" in ts.keys():
recharge_kwargs = ts.pop("recharge_kwargs", {})
ts["recharge"] = getattr(ps.recharge, ts["recharge"])(**recharge_kwargs)
recharge_class = ts["recharge"].pop("class")
ts["recharge"] = getattr(ps.recharge, recharge_class)(**ts["recharge"])

metadata = []
settings = []
Expand Down
34 changes: 33 additions & 1 deletion pastas/recharge.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@
class RechargeBase:
"""Base class for classes that calculate the recharge."""

_name = "RechargeBase"

def __init__(self) -> None:
self.snow = False
self.nparam = 0
self.kwargs = {}

@staticmethod
def get_init_parameters(name: str = "recharge") -> DataFrame:
Expand All @@ -77,6 +78,20 @@ def get_init_parameters(name: str = "recharge") -> DataFrame:
def simulate(self, prec, evap, p, dt=1.0, return_full=False, **kwargs):
pass

def to_dict(self):
"""Method to export the recharge model object.
Returns
-------
data: dict
dictionary with all necessary information to reconstruct the StressModel
object.
"""
data = {
"class": self._name,
}
return data


class Linear(RechargeBase):
"""Linear model for precipitation excess according to
Expand Down Expand Up @@ -532,6 +547,23 @@ def check_root_zone_balance(
error = sr[0] - sr[-1] + (r + ea + q + pe).sum()
return error

def to_dict(self):
"""Method to export the recharge model object.
Returns
-------
data: dict
dictionary with all necessary information to reconstruct the recharge
object.
"""
data = {
"class": self._name,
"interception": self.interception,
"snow": self.snow,
"gw_uptake": self.gw_uptake,
}
return data


class Berendrecht(RechargeBase):
"""Recharge to the groundwater calculated according to
Expand Down
3 changes: 1 addition & 2 deletions pastas/stressmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -1351,8 +1351,7 @@ def to_dict(self, series: bool = True) -> dict:
"rfunc": self.rfunc._name,
"rfunc_kwargs": self.rfunc.kwargs,
"name": self.name,
"recharge": self.recharge._name,
"recharge_kwargs": self.recharge.kwargs,
"recharge": self.recharge.to_dict(),
"cutoff": self.rfunc.cutoff,
"temp": self.temp.to_dict() if self.temp else None,
}
Expand Down

0 comments on commit 4546b93

Please sign in to comment.