Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to enable daily calibration frequency #4

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion liscal/hydro_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doc78 , I'm wondering if it wouldn't be cleaner to put the if statement for the calibration_freq in the config and then use the value to derive directly the step when constructing the time series:
it would give something like:
index = pd.to_datetime(pd.date_range(start, end, freq='{}H'.format(cfg.freq_hours)), format='%d/%m/%Y %H:%M', errors='raise')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could even put a value in minutes to be more future proof.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

Qsim = pd.DataFrame(data=Qsim, index=index)
Qsim.columns = [str(subcatch.obsid)]
Qsim.index.name = 'Timestamp'
Expand Down
12 changes: 9 additions & 3 deletions liscal/objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, it would give:
simulated_streamflow.index = [(datetime.strptime(start, "%d/%m/%Y %H:%M") + timedelta(hours=cfg.freq_hours*i)).strftime('%d/%m/%Y %H:%M') for i in range(len(simulated_streamflow.index))]

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))

Expand All @@ -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:
Expand All @@ -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()
Expand Down