@@ -54,8 +54,8 @@ CForcingGrid::CForcingGrid(string ForcingType,
5454 // -------------------------------
5555 // Additional variables initialized and eventually overwritten by ParseTimeSeries
5656 // -------------------------------
57- _rainfall_corr = 1.0 ;
58- _snowfall_corr = 1.0 ;
57+ _rainfall_corr = 1.0 ;
58+ _snowfall_corr = 1.0 ;
5959 _temperature_corr = 0.0 ;
6060
6161 _cloud_min_temp =-20.0 ; // ensures cloud-free status always unless overriden
@@ -102,6 +102,8 @@ CForcingGrid::CForcingGrid(string ForcingType,
102102// / \param ForcingType [in] an existing grid
103103CForcingGrid::CForcingGrid ( const CForcingGrid &grid )
104104{
105+ ExitGracefullyIf (grid._nNonZeroWeightedGridCells == 0 , " CForcingGrid copy constructor" ,RUNTIME_ERR);
106+
105107 _ForcingType = grid._ForcingType ;
106108 _filename = grid._filename ;
107109 _varname = grid._varname ;
@@ -111,10 +113,6 @@ CForcingGrid::CForcingGrid( const CForcingGrid &grid )
111113 _LinTrans_a = grid._LinTrans_a ;
112114 _LinTrans_b = grid._LinTrans_b ;
113115 _period_ending = grid._period_ending ;
114- for (int ii=0 ; ii<3 ; ii++) {_DimNames [ii]= grid._DimNames [ii]; }
115- for (int ii=0 ; ii<3 ; ii++) {_GridDims [ii]= grid._GridDims [ii]; }
116- for (int ii=0 ; ii<3 ; ii++) {_WinLength[ii]= grid._WinLength [ii]; }
117- for (int ii=0 ; ii<3 ; ii++) {_WinStart [ii]= grid._WinStart [ii]; }
118116 _nCells = grid._nCells ;
119117 _nNonZeroWeightedGridCells = grid._nNonZeroWeightedGridCells ;
120118 _nHydroUnits = grid._nHydroUnits ;
@@ -128,17 +126,20 @@ CForcingGrid::CForcingGrid( const CForcingGrid &grid )
128126 _dim_order = grid._dim_order ;
129127 _is_derived = true ;
130128 _nPulses = grid._nPulses ;
131- _pulse = grid._pulse ;
132129 _t_corr = grid._t_corr ;
133130 _rainfall_corr = grid._rainfall_corr ;
134131 _snowfall_corr = grid._snowfall_corr ;
135132 _temperature_corr = grid._temperature_corr ;
136133 _cloud_min_temp = grid._cloud_min_temp ;
137134 _cloud_max_temp = grid._cloud_max_temp ;
138- for (int ii=0 ; ii<12 ; ii++) {_aAveTemp[ii] = grid._aAveTemp [ii];}
139- for (int ii=0 ; ii<12 ; ii++) {_aMinTemp[ii] = grid._aMinTemp [ii];}
140- for (int ii=0 ; ii<12 ; ii++) {_aMaxTemp[ii] = grid._aMaxTemp [ii];}
141- for (int ii=0 ; ii<12 ; ii++) {_aAvePET [ii] = grid._aAvePET [ii];}
135+ for (int ii=0 ; ii<3 ; ii++) {_DimNames [ii] = grid._DimNames [ii]; }
136+ for (int ii=0 ; ii<3 ; ii++) {_GridDims [ii] = grid._GridDims [ii]; }
137+ for (int ii=0 ; ii<3 ; ii++) {_WinLength[ii] = grid._WinLength [ii]; }
138+ for (int ii=0 ; ii<3 ; ii++) {_WinStart [ii] = grid._WinStart [ii]; }
139+ for (int ii=0 ; ii<12 ; ii++) {_aAveTemp [ii] = grid._aAveTemp [ii]; }
140+ for (int ii=0 ; ii<12 ; ii++) {_aMinTemp [ii] = grid._aMinTemp [ii]; }
141+ for (int ii=0 ; ii<12 ; ii++) {_aMaxTemp [ii] = grid._aMaxTemp [ii]; }
142+ for (int ii=0 ; ii<12 ; ii++) {_aAvePET [ii] = grid._aAvePET [ii]; }
142143
143144 _aVal=NULL ;
144145 _aVal = new double *[_ChunkSize];
@@ -230,8 +231,7 @@ CForcingGrid::~CForcingGrid()
230231// / dimensions and buffersize (data are only initalized but not read from file)
231232// /
232233// / \note Needs _ForcingType, _filename, _varname, _DimNames to be set already.
233- // / Use for that either "SetForcingType", "SetFilename", "SetVarname", and "SetDimNames" \n
234- // / or "CForcingGrid()".
234+ // / Use for that either "SetForcingType", "SetFilename", "SetVarname", and "SetDimNames" or "CForcingGrid()".
235235void CForcingGrid::ForcingGridInit (const optStruct &Options)
236236{
237237#ifdef _RVNETCDF_
@@ -317,7 +317,8 @@ void CForcingGrid::ForcingGridInit(const optStruct &Options)
317317
318318 _nCells=_GridDims[0 ]*_GridDims[1 ];
319319 }
320- else {
320+ else // 2D
321+ {
321322 // dimension x = number of stations in NetCDF file
322323 retval = nc_inq_dimid (ncid,_DimNames[0 ].c_str (),&dimid_x); HandleNetCDFErrors (retval);
323324 retval = nc_inq_dimlen (ncid,dimid_x,&GridDim_t); HandleNetCDFErrors (retval);
@@ -335,18 +336,18 @@ void CForcingGrid::ForcingGridInit(const optStruct &Options)
335336 string varname_e=_varname;
336337 SubstringReplace (varname_e," *" ,to_string (g_current_e+1 ));// replaces wildcard for ensemble runs
337338
338- retval = nc_inq_varid (ncid,varname_e.c_str (),&varid_f); HandleNetCDFErrors (retval);
339+ retval = nc_inq_varid (ncid,varname_e.c_str (),&varid_f); HandleNetCDFErrors (retval);
339340
340341 // determine in which order the dimensions are in variable
341342 retval = nc_inq_vardimid (ncid,varid_f,dimids_var); HandleNetCDFErrors (retval);
342343
343344 int ndim;
344- retval = nc_inq_varndims (ncid,varid_f,&ndim); HandleNetCDFErrors (retval);
345+ retval = nc_inq_varndims (ncid,varid_f,&ndim); HandleNetCDFErrors (retval);
345346 if ((!_is_3D) && (ndim > 2 )) {
346347 ExitGracefully (" ForcingGridInit: NetCDF dataset has more than 2 dimensions, but only two supplied." ,BAD_DATA);
347348 }
348349 else if ((_is_3D) && (ndim > 3 )){
349- ExitGracefully (" ForcingGridInit: NetCDF dataset has more than 3 dimensions. Gridded data must be read from a 2D (time x cell) or 3D (time x row x column) NetCDF variable" ,BAD_DATA);
350+ ExitGracefully (" ForcingGridInit: NetCDF dataset has more than 3 dimensions. Gridded data must be read from a 2D (time x cell/station ) or 3D (time x row x column) NetCDF variable" ,BAD_DATA);
350351 }
351352
352353
@@ -430,8 +431,7 @@ void CForcingGrid::ForcingGridInit(const optStruct &Options)
430431 double time_zone=0 ;
431432 GetTimeInfoFromNetCDF (unit_t ,calendar,my_time,ntime,_filename,_interval,_start_day,_start_year,time_zone);
432433 _steps_per_day=(int )(rvn_round (1.0 /_interval)); // pre-calculate for speed.
433- delete[] unit_t ;
434-
434+
435435 /*
436436 printf("ForcingGrid: unit_t: %s\n",unit_t_str.c_str());
437437 printf("ForcingGrid: tt.julian_day: %f\n",tt.julian_day);
@@ -449,7 +449,7 @@ void CForcingGrid::ForcingGridInit(const optStruct &Options)
449449 JulianConvert(0.0,_start_day-_interval,_start_year,calendar,tp);
450450 printf("ForcingGrid: start string: %s %s\n",tp.date_string.c_str(),DecDaysToHours(tp.julian_day).c_str());
451451 */
452-
452+ delete[] unit_t ;
453453 delete[] my_time;
454454
455455 // QA/QC:
@@ -521,7 +521,6 @@ void CForcingGrid::ForcingGridInit(const optStruct &Options)
521521 // Set number of pulses and pulse type to be consistent with class CTimeSeries
522522 // -------------------------------
523523 _nPulses = ntime;
524- _pulse = true ;
525524 ExitGracefullyIf (_nPulses<=0 ,
526525 " CForcingGrid: ForcingGridInit: no time point entries in forcing grid" ,BAD_DATA);
527526
@@ -562,21 +561,16 @@ void CForcingGrid::ForcingGridInit(const optStruct &Options)
562561//
563562void CForcingGrid::ReallocateArraysInForcingGrid ( )
564563{
565- int ntime ; // number of time steps
566-
567- ntime = _GridDims[2 ]; // assumes _Is_3D
568- // if (_is_3D) {ntime = _GridDims[2];}
569- // else {ntime = _GridDims[1];}
570- //
571564 // -------------------------------
572565 // Initialize data array and set all entries to NODATA value
573566 // -------------------------------
574567 _aVal = NULL ;
575- _aVal = new double *[ntime];
576- for (int it=0 ; it<ntime; it++) { // loop over time points in buffer
568+ _aVal = new double *[_ChunkSize];
569+ ExitGracefullyIf (_aVal==NULL ," CForcingGrid::ReallocateArraysInForcingGrid (1)" ,OUT_OF_MEMORY);
570+ for (int it=0 ; it<_ChunkSize; it++) { // loop over time points in buffer
577571 _aVal[it]=NULL ;
578572 _aVal[it] = new double [_nNonZeroWeightedGridCells];
579- ExitGracefullyIf (_aVal[it]==NULL ," CForcingGrid::ReallocateArraysInForcingGrid" ,OUT_OF_MEMORY);
573+ ExitGracefullyIf (_aVal[it]==NULL ," CForcingGrid::ReallocateArraysInForcingGrid (2) " ,OUT_OF_MEMORY);
580574 for (int ic=0 ; ic<_nNonZeroWeightedGridCells;ic++){ // loop over non-zero weighted cells
581575 _aVal[it][ic]=NETCDF_BLANK_VALUE; // initialize
582576 }
@@ -1337,7 +1331,7 @@ void CForcingGrid::SetIdxNonZeroGridCells(const int nHydroUnits, const int nGrid
13371331}
13381332
13391333// /////////////////////////////////////////////////////////////////
1340- // / \brief calculates _ChunkSize and total number of chunks to read _nChunks, sets the id of the current chunk
1334+ // / \brief calculates _ChunkSize and total number of chunks to read ( _nChunks)
13411335// / depending upon size of grid (_nNonZeroWeightedGridCells*buffersize*8byte <= 10 MB=10*1024*1024 byte)
13421336// / needs to be called after SetIdxNonZeroGridCells()
13431337// /
@@ -1355,22 +1349,22 @@ void CForcingGrid::CalculateChunkSize(const optStruct& Options)
13551349 else { ntime = _GridDims[1 ]; }
13561350
13571351 int BytesPerTimestep; // Memory requirement for one timestep of gridded forcing file [Bytes]
1358- if (_is_3D) { BytesPerTimestep = 8 * _WinLength[0 ] * _WinLength[1 ]; }
1359- else { BytesPerTimestep = 8 * _WinLength[0 ]; }
1352+ // if(_is_3D) { BytesPerTimestep = 8 * _WinLength[0] * _WinLength[1]; }
1353+ // else { BytesPerTimestep = 8 * _WinLength[0]; }
13601354
1361- // BytesPerTimestep = 8 * _nNonZeroWeightedGridCells; //?? amount actually stored in memory?
1355+ BytesPerTimestep = 8 * _nNonZeroWeightedGridCells; // ?? amount actually stored in memory?
13621356
13631357 int CHUNK_MEMORY=Options.NetCDF_chunk_mem *1024 * 1024 ;
13641358
13651359 int tmpChunkSize;
13661360
1367- tmpChunkSize = (int )(max (min (CHUNK_MEMORY / BytesPerTimestep,ntime),1 )); // number of timesteps per chunk - 10MB chunks
1361+ tmpChunkSize = (int )(max (min (CHUNK_MEMORY / BytesPerTimestep,ntime),1 )); // number of timesteps per chunk - 10MB chunks
13681362
13691363 if (!Options.deltaresFEWS ) {
13701364 tmpChunkSize = (int )((int )(tmpChunkSize*_interval)/_interval); // make sure chunks are complete days (have to relax for FEWS)
13711365 }
13721366
1373- tmpChunkSize = max (int (rvn_round (1.0 /_interval)),tmpChunkSize); // make sure at least one day is read
1367+ tmpChunkSize = max (int (rvn_round (1.0 /_interval)),tmpChunkSize); // make sure at least one day is read
13741368 // support larger chunk if model duration is small
13751369 double partday=Options.julian_start_day -floor (Options.julian_start_day );
13761370 tmpChunkSize = min (tmpChunkSize,(int ) ceil (ceil (Options.duration +partday)/_interval));// ensures goes to midnight of last day
@@ -1383,7 +1377,6 @@ void CForcingGrid::CalculateChunkSize(const optStruct& Options)
13831377 cout<<" Finished CalculateChunkSize routine, # of time steps per chunk: " <<_ChunkSize<<endl;
13841378 cout<<" # of time chunks: " <<_nChunk <<endl;
13851379 }
1386- /* cout<<"ntime: "<<ntime<<endl;*/
13871380}
13881381// /////////////////////////////////////////////////////////////////
13891382// / \brief sets the _nHydroUnits in class CForcingGrid
@@ -1523,9 +1516,9 @@ void CForcingGrid::SetaAvePET(const double aAvePET [12]){
15231516void CForcingGrid::SetValue ( const int ic, const int it, const double aVal) {
15241517#ifdef _STRICTCHECK_
15251518 if (it>=_ChunkSize) {
1526- ExitGracefully (" CForcingGrid::SetValue:invalid index" ,RUNTIME_ERR);}
1519+ ExitGracefully (" CForcingGrid::SetValue:invalid time index" ,RUNTIME_ERR);}
15271520 if (ic>=_nNonZeroWeightedGridCells) {
1528- ExitGracefully (" CForcingGrid::SetValue:invalid index" ,RUNTIME_ERR);}
1521+ ExitGracefully (" CForcingGrid::SetValue:invalid cell index" ,RUNTIME_ERR);}
15291522#endif
15301523 _aVal[it][ic] = aVal;
15311524}
0 commit comments