From a8e71bd2dd19ce24aec37279e0276614f972a0d7 Mon Sep 17 00:00:00 2001 From: Carlo Russo Date: Tue, 2 Nov 2021 14:56:32 +0100 Subject: [PATCH] Fixes to enable daily calibration frequency --- liscal/hydro_model.py | 7 ++++++- liscal/objective.py | 12 +++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/liscal/hydro_model.py b/liscal/hydro_model.py index 60492e7..a556f21 100644 --- a/liscal/hydro_model.py +++ b/liscal/hydro_model.py @@ -236,7 +236,12 @@ def generate_benchmark(cfg, subcatch, lis_template, param_target, outfile, start simulated_streamflow = utils.read_tss(Qsim_tss) simulated_streamflow[1][simulated_streamflow[1] == 1e31] = np.nan Qsim = simulated_streamflow[1].values - index = pd.to_datetime(pd.date_range(start, end, freq='6H'), format='%d/%m/%Y %H:%M', errors='raise') + if cfg.calibration_freq == r"6-hourly": + index = pd.to_datetime(pd.date_range(start, end, freq='6H'), format='%d/%m/%Y %H:%M', errors='raise') + elif cfg.calibration_freq == r'daily': + index = pd.to_datetime(pd.date_range(start, end, freq='24H'), format='%d/%m/%Y %H:%M', errors='raise') + else: + raise Exception('Calibration freq {} not supported'.format(cfg.calibration_freq)) Qsim = pd.DataFrame(data=Qsim, index=index) Qsim.columns = [str(subcatch.obsid)] Qsim.index.name = 'Timestamp' diff --git a/liscal/objective.py b/liscal/objective.py index f2eb11c..dbe5a81 100644 --- a/liscal/objective.py +++ b/liscal/objective.py @@ -70,8 +70,13 @@ def read_simulated_streamflow(self, run_id, start, end): simulated_streamflow = utils.read_tss(Qsim_tss)[1] # need to take [1] or we get 2d array simulated_streamflow[simulated_streamflow==1e31] = np.nan # PCRaster will put 1e31 instead of NaN, set to NaN to catch errors - simulated_streamflow.index = [(datetime.strptime(start, "%d/%m/%Y %H:%M") + timedelta(hours=6*i)).strftime('%d/%m/%Y %H:%M') for i in range(len(simulated_streamflow.index))] - + if self.cfg.calibration_freq == r"6-hourly": + simulated_streamflow.index = [(datetime.strptime(start, "%d/%m/%Y %H:%M") + timedelta(hours=6*i)).strftime('%d/%m/%Y %H:%M') for i in range(len(simulated_streamflow.index))] + elif self.cfg.calibration_freq == r'daily': + simulated_streamflow.index = [(datetime.strptime(start, "%d/%m/%Y %H:%M") + timedelta(hours=24*i)).strftime('%d/%m/%Y %H:%M') for i in range(len(simulated_streamflow.index))] + else: + raise Exception('Calibration freq {} not supported'.format(self.cfg.calibration_freq)) + if simulated_streamflow.index[-1] != end: raise ValueError('Simulated streamflow with run_id {} not consistent: end date is {} and should be {}'.format(run_id, simulated_streamflow.index[-1], end)) @@ -85,8 +90,8 @@ def resample_streamflows(self, start, end, simulated_streamflow, observed_stream # Finally, extract equal-length arrays from it Qobs = observed_streamflow[start:end] Qsim = simulated_streamflow[start:end] - date_range = pd.date_range(start_pd, end_pd, freq="360min") if cfg.calibration_freq == r"6-hourly": + date_range = pd.date_range(start_pd, end_pd, freq="360min") # DD: Check if daily or 6-hourly observed streamflow is available # DD: Aggregate 6-hourly simulated streamflow to daily ones if self.subcatch.data["CAL_TYPE"].find("_24h") > -1: @@ -101,6 +106,7 @@ def resample_streamflows(self, start, end, simulated_streamflow, observed_stream date_range = Qobs.index elif cfg.calibration_freq == r"daily": + date_range = pd.date_range(start_pd, end_pd, freq="24H") # DD Untested code! DEBUG TODO Qobs.index = date_range Qobs = Qobs.resample('24H', label="right", closed="right").mean()