Alpine3D  Alpine3D-3.2.0
Runoff.h
Go to the documentation of this file.
1 /***********************************************************************************/
2 /* Copyright 2009-2015 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
3 /***********************************************************************************/
4 /* This file is part of Alpine3D.
5  Alpine3D is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Alpine3D is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Alpine3D. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef RUNOFF_H
19 #define RUNOFF_H
20 
21 #include <iostream>
22 #include <vector>
23 #include <map>
24 #include <limits>
25 #include <sstream>
26 #include <meteoio/MeteoIO.h>
29 
30 typedef unsigned long long longuint;
31 
32 class SnowpackInterface; // forward declaration, cyclic header include
33 
82 class Runoff
83 {
84  public:
85  Runoff(const mio::Config& in_cfg, const mio::DEMObject& in_dem,
86  const double& in_thresh_rain);
87  Runoff(const Runoff& copy);
88 
89  // Methods to set other modules
90  void setSnowPack(SnowpackInterface &sn_interface);
91 
92  virtual void output(const mio::Date& i_date, const mio::Grid2DObject& psum,
93  const mio::Grid2DObject& ta, mio::IOManager& io_in);
94  virtual ~Runoff();
95 
96  std::string getGridsRequirements() const;
97  double getTiming() const;
98 
99  protected:
100  mio::IOManager *io;
102 
103  const double thresh_rain;
104  const double tz_out;
105  bool output_grids; //< output grid of runoff data? (for taking it over with an external hydro model)
106  bool output_sums; //< output total runoff per subcatchment?
107  std::string catchment_out_path; //< folder in which the extracted catchment sums should be written
108  double resampling_cell_size; //< cell size [m] to which the runoff grids should be resampled
110  const size_t n_grid_cells;
112 
113  mio::Timer timer;
114  const mio::Grid2DObject slope_correction;
117  mio::Grid2DObject total_runoff, glacier_mask;
118  std::vector<SnGrids::Parameters> extra_meteo_variables;
120  std::map<size_t, mio::Grid2DObject> catchment_masks;
121  bool use_external_iomanager_for_grids; // To know if the same io manager than for the other grids must be used
122  // (to avoid having multiple netcdf files open)
123  static const double MIN_CELL_SIZE; //< [m] two points closer to each other than this value will be assumed to overlap
124  static const double DISTANCE_ABSOLUTE_PRECISION; //< [m] minimum size of a grid cell
125 
126  virtual void constructCatchmentMasks(mio::Grid2DObject catchmentGrid);
127  virtual void updateTotalRunoffGrid();
128  virtual void updateGlacierMask();
129  virtual mio::Grid2DObject computePrecipRunoff(const mio::Grid2DObject& psum, const mio::Grid2DObject& ta) const;
130  virtual void getExtraMeteoGrids(std::vector<mio::Grid2DObject>& grids) const;
131  virtual void initializeOutputFiles(const mio::Grid2DObject& dem) const;
132  virtual void updateOutputFile(const size_t& catchId, const mio::Date& currTime,
133  const double& totalRunoff, const double& precipRunoff,
134  const double& snowRunoff, const double& glacierRunoff,
135  const std::vector<double>& meteoVars) const;
136 
137  static mio::IOManager* getIOManager(mio::Config in_cfg, const bool& outputGrids);
138  static mio::Grid2DObject getSlopeCorrection(const mio::DEMObject& dem);
139  static NumberingType getCatchmentNumberingScheme(const mio::Config& in_cfg);
140  static bool getIsGlacierDynamic(const mio::Config& cfg);
141  static std::vector<SnGrids::Parameters> getExtraMeteoVariables(const mio::Config& cfg);
142  static double getResamplingCellSize(const mio::DEMObject& in_dem, const mio::Grid2DObject& catchments);
143  static bool isMultiple(const double& a, const double& b);
144  static double estimateResamplingCellSize(const double& llOffset, const double& currSizeEstimate);
145  static std::vector<size_t> factorizeCatchmentNumber(longuint value);
146  static void cropMask(mio::Grid2DObject& mask);
147  static double sumOverMask(const mio::Grid2DObject& grid, const mio::Grid2DObject& mask);
148  static double averageOverMask(const mio::Grid2DObject& grid, const mio::Grid2DObject& mask);
149 
150  private:
151  Runoff& operator=(const Runoff&) {return *this;} //< private in order to avoid being used and suppress compiler warning
152 };
153 
154 #endif
static double getResamplingCellSize(const mio::DEMObject &in_dem, const mio::Grid2DObject &catchments)
Returns the cell size which must be used to resample both the catchment masks and the runoff grids...
Definition: Runoff.cc:578
virtual ~Runoff()
Destructor of class Runoff.
Definition: Runoff.cc:220
const size_t n_grid_cells
Definition: Runoff.h:110
virtual void getExtraMeteoGrids(std::vector< mio::Grid2DObject > &grids) const
Returns the grids corresponding to the additional meteo variables which have to be averaged over the ...
Definition: Runoff.cc:347
static void cropMask(mio::Grid2DObject &mask)
Crops the mask so as to remove as many nodata cells as possible (note: this method possibly changes t...
Definition: Runoff.cc:659
static bool getIsGlacierDynamic(const mio::Config &cfg)
Returns whether the glaciers are dynamic, i.e. whether the glacier mask should be expected to change ...
Definition: Runoff.cc:542
const mio::Grid2DObject slope_correction
Definition: Runoff.h:114
virtual mio::Grid2DObject computePrecipRunoff(const mio::Grid2DObject &psum, const mio::Grid2DObject &ta) const
Computes the grid storing the runoff which originates from liquid precipitation.
Definition: Runoff.cc:313
Definition: Runoff.h:111
static double estimateResamplingCellSize(const double &llOffset, const double &currSizeEstimate)
Definition: Runoff.cc:620
static mio::Grid2DObject getSlopeCorrection(const mio::DEMObject &dem)
Computes the grid used to correct the runoff values for slope (Alpine3D assumes the cells to be flat...
Definition: Runoff.cc:500
double getTiming() const
Definition: Runoff.cc:716
static const double MIN_CELL_SIZE
Definition: Runoff.h:123
virtual void output(const mio::Date &i_date, const mio::Grid2DObject &psum, const mio::Grid2DObject &ta, mio::IOManager &io_in)
Writes the results for a specific day.
Definition: Runoff.cc:149
mio::IOManager * io
Definition: Runoff.h:100
const double thresh_rain
Definition: Runoff.h:103
std::string getGridsRequirements() const
Definition: Runoff.cc:127
Definition: SnowpackInterface.h:127
bool is_glacier_mask_set
Definition: Runoff.h:116
static mio::IOManager * getIOManager(mio::Config in_cfg, const bool &outputGrids)
Returns a IOManager object which will write the runoff grids in the correct output folder...
Definition: Runoff.cc:472
Runoff(const mio::Config &in_cfg, const mio::DEMObject &in_dem, const double &in_thresh_rain)
Constructor of Runoff instance.
Definition: Runoff.cc:29
unsigned long long longuint
Definition: Runoff.h:30
static std::vector< SnGrids::Parameters > getExtraMeteoVariables(const mio::Config &cfg)
This method returns the meteo variables which have to be averaged over the subwatershed areas and wri...
Definition: Runoff.cc:555
mio::Grid2DObject glacier_mask
Definition: Runoff.h:117
std::map< size_t, mio::Grid2DObject > catchment_masks
Definition: Runoff.h:120
static NumberingType getCatchmentNumberingScheme(const mio::Config &in_cfg)
Returns the numbering scheme used in the grid defining the catchments.
Definition: Runoff.cc:519
virtual void updateOutputFile(const size_t &catchId, const mio::Date &currTime, const double &totalRunoff, const double &precipRunoff, const double &snowRunoff, const double &glacierRunoff, const std::vector< double > &meteoVars) const
Writes the runoff values aggregated over a given catchment in the corresponding SMET file...
Definition: Runoff.cc:436
std::vector< SnGrids::Parameters > extra_meteo_variables
Definition: Runoff.h:118
virtual void constructCatchmentMasks(mio::Grid2DObject catchmentGrid)
Initializes private attribute catchment_masks.
Definition: Runoff.cc:233
Definition: Runoff.h:82
static const double DISTANCE_ABSOLUTE_PRECISION
Definition: Runoff.h:124
static bool isMultiple(const double &a, const double &b)
Returns whether the first input is a multiple of the second one.
Definition: Runoff.cc:614
static double sumOverMask(const mio::Grid2DObject &grid, const mio::Grid2DObject &mask)
Sums the values of the grid cells which are located over the mask.
Definition: Runoff.cc:683
Definition: Runoff.h:111
std::string catchment_out_path
Definition: Runoff.h:107
bool use_external_iomanager_for_grids
Definition: Runoff.h:121
virtual void updateGlacierMask()
Updates protected attribute glacier_mask.
Definition: Runoff.cc:293
void setSnowPack(SnowpackInterface &sn_interface)
Sets the internal reference to SnowpackInterface object.
Definition: Runoff.cc:137
static std::vector< size_t > factorizeCatchmentNumber(longuint value)
This function splits up a given unsigned int value into a sum of powers of 2.
Definition: Runoff.cc:640
static double averageOverMask(const mio::Grid2DObject &grid, const mio::Grid2DObject &mask)
Averages the values of the grid cells which are located over the mask.
Definition: Runoff.cc:711
SnowpackInterface * snowpack
Reference to SnowpackInterface object, used for callbacks, initialized during construction.
Definition: Runoff.h:101
bool output_grids
Definition: Runoff.h:105
const double tz_out
Definition: Runoff.h:104
enum Runoff::NumberingType catchment_numbering
NumberingType
Definition: Runoff.h:111
mio::Timer timer
Definition: Runoff.h:113
const bool is_glacier_mask_dynamic
Definition: Runoff.h:115
size_t n_extra_meteo_variables
Definition: Runoff.h:119
virtual void updateTotalRunoffGrid()
Updates protected attribute total_runoff.
Definition: Runoff.cc:277
double grid_size_factor
Definition: Runoff.h:109
bool output_sums
Definition: Runoff.h:106
mio::Grid2DObject total_runoff
Definition: Runoff.h:117
virtual void initializeOutputFiles(const mio::Grid2DObject &dem) const
Initializes the SMET files in which the per-catchment-aggregated runoff values will be written...
Definition: Runoff.cc:376
double resampling_cell_size
Definition: Runoff.h:108