Skip to content

Commit 81b3973

Browse files
James CraigJames Craig
authored andcommitted
v531 - Assimilation + management fix
support reservoir stage assimilation with management-derived outflow in same basin (Reservoir.cpp) bug fix: time shift issue in CModel::GenerateAveSubdailyTempFromMinMax() cleanup: converted CForcingGrid::GetValue_*** functions to take start index rather than raw time (as was actually happening anyhow) (ForcingGrid.cpp/.h)
1 parent d1b31ad commit 81b3973

File tree

5 files changed

+44
-35
lines changed

5 files changed

+44
-35
lines changed

src/ForcingGrid.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,8 +1843,8 @@ int CForcingGrid::GetChunkSize() const{return _ChunkSize;}
18431843
int CForcingGrid::GetnHydroUnits() const{return _nHydroUnits;}
18441844

18451845
///////////////////////////////////////////////////////////////////
1846-
/// \brief returns time index idx corresponding to t+interval/2
1847-
/// \return time index idx corresponding to t+interval/2
1846+
/// \brief returns time index idx corresponding to t
1847+
/// \return time index idx corresponding to t
18481848
/// param t [in] model time [days]
18491849
//
18501850
int CForcingGrid::GetTimeIndex(const double &t) const
@@ -1977,9 +1977,9 @@ double CForcingGrid::GetValue(const int ic, const int it) const
19771977
/// \param n [in] Number of time steps
19781978
/// \return Magnitude of time series data point for which t is an index
19791979
//
1980-
double CForcingGrid::GetValue_avg(const int ic, const double &t_idx, const int nsteps) const
1980+
double CForcingGrid::GetValue_avg(const int ic, const int t_idx, const int nsteps) const
19811981
{
1982-
int it_start=max((int)(t_idx),0);
1982+
int it_start=max(t_idx,0);
19831983
int lim=min(nsteps,_ChunkSize-it_start);
19841984
double sum = 0.0;
19851985
for (int it=it_start; it<it_start+lim;it++){
@@ -1993,14 +1993,14 @@ double CForcingGrid::GetValue_avg(const int ic, const double &t_idx, const int n
19931993
///////////////////////////////////////////////////////////////////
19941994
/// \brief Returns minimum of n timesteps of time series data point for which t is an index
19951995
/// \param ic [in] Index of grid cell with non-zero weighting (value between 0 and _nNonZeroWeightedGridCells)
1996-
/// \param t [in] Time index
1997-
/// \param n [in] Number of time steps
1996+
/// \param t_idx [in] Time index
1997+
/// \param n [in] Number of time intervals
19981998
/// \return Magnitude of time series data point for which t is an index
19991999
//
2000-
double CForcingGrid::GetValue_min(const int ic, const double &t, const int nsteps) const
2000+
double CForcingGrid::GetValue_min(const int ic, const int t_idx, const int nsteps) const
20012001
{
20022002
double min_val = ALMOST_INF ;
2003-
int it_start=max((int)(t),0);
2003+
int it_start=max(t_idx,0);
20042004
int lim=min(nsteps,_ChunkSize-it_start);
20052005
for (int it=it_start; it<it_start+lim;it++){
20062006
if(_aVal[it][ic] < min_val){min_val=_aVal[it][ic];}
@@ -2011,14 +2011,14 @@ double CForcingGrid::GetValue_min(const int ic, const double &t, const int nstep
20112011
///////////////////////////////////////////////////////////////////
20122012
/// \brief Returns maximum of n timesteps of time series data point for which t is an index
20132013
/// \param ic [in] Index of grid cell with non-zero weighting (value between 0 and _nNonZeroWeightedGridCells)
2014-
/// \param t [in] Time index
2014+
/// \param t_idx [in] Time index
20152015
/// \param n [in] Number of time steps
20162016
/// \return Magnitude of time series data point for which t is an index
20172017
//
2018-
double CForcingGrid::GetValue_max(const int ic, const double &t, const int nsteps) const
2018+
double CForcingGrid::GetValue_max(const int ic, const int t_idx, const int nsteps) const
20192019
{
20202020
double max_val = -ALMOST_INF ;
2021-
int it_start=max((int)(t),0);
2021+
int it_start=max(t_idx,0);
20222022
int lim=min(nsteps,_ChunkSize-it_start);
20232023
for (int it=it_start; it<it_start+lim;it++){
20242024
if(_aVal[it][ic] > max_val){max_val=_aVal[it][ic];}

src/ForcingGrid.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ class CForcingGrid //: public CForcingGridABC
149149

150150
// accessors
151151
double GetValue (const int ic, const int it) const;
152-
double GetValue_avg (const int ic, const double &t, const int n) const;
153-
double GetValue_min (const int ic, const double &t, const int n) const;
154-
double GetValue_max (const int ic, const double &t, const int n) const;
152+
double GetValue_avg (const int ic, const int t_idx, const int n) const;
153+
double GetValue_min (const int ic, const int t_idx, const int n) const;
154+
double GetValue_max (const int ic, const int t_idx, const int n) const;
155155

156156
// Weighting matrix associated routines
157157
void AllocateWeightArray( const int nHydroUnits,

src/ModelForcingGrids.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ CForcingGrid *CModel::ForcingCopyCreate(const CForcingGrid *pGrid,
5252
pTout=GetForcingGrid(typ);
5353
}
5454

55-
//int nGridHRUs=pGrid->GetnHydroUnits();
56-
//int nCells =pGrid->GetRows()*pGrid->GetCols(); //handles 3D or 2D
57-
//cout<<"**ForcingGrid Copy Create : "<<ForcingToString(pGrid->GetForcingType())<<"-->"<<ForcingToString(pTout->GetForcingType())<< " is new?:"<<is_new<<"**"<<endl;
58-
5955
return pTout;
6056
}
6157

@@ -126,6 +122,7 @@ void CModel::GenerateGriddedPrecipVars(const optStruct &Options)
126122
/// \brief Creates all missing gridded temperature data based on gridded information available,
127123
/// e.g when only sub-daily temperature is provided estimate daily average, minimum and maximum temperature.
128124
/// data are assumed to have the same resolution and hence can be initialized together.
125+
/// called with each new chunk of data
129126
///
130127
/// \param Options [in] major options of the model
131128
//
@@ -261,10 +258,10 @@ void CModel::GenerateAveSubdailyTempFromMinMax(const optStruct &Options)
261258
for (int it=0; it<nVals; it++) { // loop over all time points (nVals)
262259
for (int ic=0; ic<nNonZero; ic++){ // loop over non-zero grid cell indexes
263260
time_idx_chunk = int(floor(t+TIME_CORRECTION));
264-
Tmin = pTmin->GetValue_avg(ic, floor(t +time_shift+TIME_CORRECTION)*nValsPerDay, nValsPerDay);
265-
Tmax = pTmax->GetValue_avg(ic, floor(t +time_shift+TIME_CORRECTION)*nValsPerDay, nValsPerDay);
266-
T1corr = pTave->DailyTempCorrection(t+time_shift);
267-
T2corr = pTave->DailyTempCorrection(t+time_shift+Options.timestep);
261+
Tmin = pTmin->GetValue_avg(ic, floor(t-time_shift+TIME_CORRECTION)*nValsPerDay, nValsPerDay);
262+
Tmax = pTmax->GetValue_avg(ic, floor(t-time_shift+TIME_CORRECTION)*nValsPerDay, nValsPerDay);
263+
T1corr = pTave->DailyTempCorrection(t);
264+
T2corr = pTave->DailyTempCorrection(t+Options.timestep);
268265
val=pTave_daily->GetValue(ic, time_idx_chunk)+0.25*(Tmax-Tmin)*(T1corr+T2corr);
269266
pTave->SetValue( ic, it, val);
270267
}
@@ -280,8 +277,8 @@ void CModel::GenerateAveSubdailyTempFromMinMax(const optStruct &Options)
280277
pTave = ForcingCopyCreate(pTave_daily,F_TEMP_AVE,1.0,nVals,Options);
281278

282279
int nNonZero =pTave->GetNumberNonZeroGridCells();
283-
for(int it=0; it<nVals; it++) { // loop over time points in buffer
284-
for(int ic=0; ic<nNonZero; ic++) { // loop over non-zero grid cell indexes
280+
for(int it=0; it<nVals; it++) { // loop over time points in buffer
281+
for(int ic=0; ic<nNonZero; ic++) { // loop over non-zero grid cell indexes
285282
pTave->SetValue(ic,it,pTave_daily->GetValue(ic,it)); // --> just copy daily average values
286283
}
287284
}
@@ -308,18 +305,20 @@ void CModel::GenerateMinMaxAveTempFromSubdaily(const optStruct &Options)
308305
pTmax_daily = ForcingCopyCreate(pTave,F_TEMP_DAILY_MAX,1.0,nVals,Options);
309306
pTave_daily = ForcingCopyCreate(pTave,F_TEMP_DAILY_AVE,1.0,nVals,Options);
310307

308+
int t_idx;
311309
double time;
312310
double time_shift=Options.julian_start_day-floor(Options.julian_start_day+TIME_CORRECTION);
313-
int nsteps_in_day=int(1.0/interval);
311+
int nsteps_in_day=(int)(rvn_round(1.0/interval));
314312

315313
for (int it=0; it<nVals; it++) { // loop over time points in buffer
316-
time=(double)it*1.0/interval;
314+
time=(double)it*nsteps_in_day;
317315
time=floor(time-time_shift+TIME_CORRECTION); //model time corresponding to 00:00 on day of it
316+
t_idx=(int)(time);
318317

319318
for (int ic=0; ic<pTave->GetNumberNonZeroGridCells(); ic++){ // loop over non-zero grid cell indexes
320-
pTmin_daily->SetValue(ic, it, pTave->GetValue_min(ic, time, nsteps_in_day));
321-
pTmax_daily->SetValue(ic, it, pTave->GetValue_max(ic, time, nsteps_in_day));
322-
pTave_daily->SetValue(ic, it, pTave->GetValue_avg(ic, time, nsteps_in_day));
319+
pTmin_daily->SetValue(ic, it, pTave->GetValue_min(ic, t_idx, nsteps_in_day));
320+
pTmax_daily->SetValue(ic, it, pTave->GetValue_max(ic, t_idx, nsteps_in_day));
321+
pTave_daily->SetValue(ic, it, pTave->GetValue_avg(ic, t_idx, nsteps_in_day));
323322
}
324323
}
325324

src/RavenInclude.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ const double WIND_EXTINCT =2.5;
235235
/// \note This is always 2 for broadleaves, and ranges from 2 for flat needles to pi for cylindrical needles.
236236
const double LEAF_PROJ_RAT =2.2; ///< [m2/m2] ratio of total leaf area to projected area.
237237

238-
const double HBV_REFERENCE_ELEV =1000; ///< [masl] (zref in HBV)
239-
const double HBV_PET_ELEV_CORR =0.0005; ///< [mm/m-d] PET lapse rate (ECALT in HBV-EC)
240-
const double HBV_PET_TEMP_CORR =0.5; ///< [-] (ETF in HBV-EC)
241-
const double GLOBAL_PET_CORR =1.0; ///< [-] (ECORR in HBV-EC)
238+
const double HBV_REFERENCE_ELEV =1000; ///< [masl] (zref in HBV)
239+
const double HBV_PET_ELEV_CORR =0.0005; ///< [mm/m-d] PET lapse rate (ECALT in HBV-EC)
240+
const double HBV_PET_TEMP_CORR =0.5; ///< [-] (ETF in HBV-EC)
241+
const double GLOBAL_PET_CORR =1.0; ///< [-] (ECORR in HBV-EC)
242242

243243
//Flag variables
244244
const int DOESNT_EXIST =-1; ///< return value for nonexistent index

src/Reservoir.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,12 @@ void CReservoir::UpdateReservoir(const time_struct &tt, const optStruct &Options
13901390
double obs_stage=_pObsStage->GetSampledValue(nn);
13911391
if(obs_stage!=RAV_BLANK_DATA) {
13921392
_stage=obs_stage;
1393-
_Qout =GetWeirOutflow(_stage,weir_adj);//[m3/s]
1393+
if ((Options.management_optimization) && (_Qoptimized != RAV_BLANK_DATA)) {
1394+
_Qout=_Qoptimized; //overriding stage AND outflow
1395+
}
1396+
else{
1397+
_Qout =GetWeirOutflow(_stage,weir_adj);//[m3/s] //default - outflow function of assimilated stage
1398+
}
13941399
_assim_blank=false;
13951400
}
13961401
}
@@ -1542,7 +1547,12 @@ double CReservoir::RouteWater(const double &Qin_old,
15421547
{
15431548
if ((_assimilate_stage) && (!_assim_blank))
15441549
{
1545-
res_outflow=_Qout;//stage from data, outflow calculated from SD curve in UpdateReservoir()
1550+
if ((Options.management_optimization) && (_Qoptimized != RAV_BLANK_DATA)) {
1551+
res_outflow=_Qoptimized;
1552+
}
1553+
else{
1554+
res_outflow=_Qout;//stage from data, outflow calculated from SD curve in UpdateReservoir()
1555+
}
15461556
return _stage;
15471557
}
15481558

0 commit comments

Comments
 (0)