diff --git a/pastas/io/base.py b/pastas/io/base.py index 419ec610..5bd80406 100644 --- a/pastas/io/base.py +++ b/pastas/io/base.py @@ -154,6 +154,13 @@ 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") @@ -161,8 +168,8 @@ def _load_stressmodel(ts, data): 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 = [] diff --git a/pastas/recharge.py b/pastas/recharge.py index ffada375..3a9c90b7 100644 --- a/pastas/recharge.py +++ b/pastas/recharge.py @@ -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: @@ -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 @@ -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 diff --git a/pastas/stressmodels.py b/pastas/stressmodels.py index 5e1072f1..a4fc5dd4 100644 --- a/pastas/stressmodels.py +++ b/pastas/stressmodels.py @@ -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, }