WSL/SLF GitLab Repository

Meteo2DInterpolator.h 10.1 KB
 Thomas Egger committed Jun 21, 2010 1 /***********************************************************************************/  Mathias Bavay committed Oct 30, 2014 2 /* Copyright 2014 WSL Institute for Snow and Avalanche Research SLF-DAVOS */  Thomas Egger committed Jun 21, 2010 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /***********************************************************************************/ /* This file is part of MeteoIO. MeteoIO is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. MeteoIO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with MeteoIO. If not, see . */ #ifndef __METEO2DINTERPOLATOR_H__ #define __METEO2DINTERPOLATOR_H__  Mathias Bavay committed Oct 30, 2014 22 #include  23 #include  24 #include  25 26 27 #include #include #include  28 #include  Thomas Egger committed Jun 21, 2010 29 30 31 32 33 34 35  #include #include #include namespace mio {  Mathias Bavay committed Oct 30, 2014 36 class InterpolationAlgorithm;  Thomas Egger committed Jan 14, 2011 37   38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 /** * @page dev_2Dinterpol How to write a spatial interpolation algorithm * Point measurements can be spatially interpolated by MeteoIO, through the use of interpolation * algorithms. The user will then be able to choose for each meteorological parameter which * interpolations could be applicable and the system will choose (through a heuristic) which one * to apply at each time step (depending on the conditions of the moment, like the number of measurements). * * @section structure_2Dinterpol Structure * The selection of which interpolation algorithm to use at any given time step, for a given parameter is * performed by the Meteo2DInterpolator class. This class provides the interface for the spatial * interpolations. The interpolation algorithms themselves derive from the * InterpolationAlgorithm class that standardizes their public API (which would not be used by anything * but a Meteo2DInterpolator object). It contains a getQualityRating() method that must return a quality * index between 0 (algorithm not applicable) and 1 (perfect result if using this algorithm). This is currently * only based on extremely simple heuristics, using general knowledge about the applicability of the various * spatial interpolation methods depending on some obvious factors (number of measurement points, etc). The * Meteo2DInterpolator object will call this method from all the algorithms listed by the user (in his io.ini * configuration file) and keep the one that gets the highest score for interpolating the current parameter * at the current time step. The interpolation is then done calling the algorithm's calculate method. * * @section implementation_2Dinterpol Implementation * It is therefore necessary to create in InterpolationAlgorithms.cc (and declared in the .h) a new class,  Mathias Bavay committed Oct 29, 2010 60  * nammed after the algorithm that will be implemented and inheriting InterpolationAlgorithm. Three methods need  61 62  * to be implemented (the constructor being inherited from InterpolationAlgorithm and automatically called * by an object factory):  Mathias Bavay committed Oct 29, 2010 63 64 65 66 67 68 69 70 71  * - void initialize(const MeteoData::Parameters& in_param) * - double getQualityRating() * - void calculate(Grid2DObject& grid) * * The initialize method takes the meteorological parameter that will be interpolated and set the param * private member to it. It then computes the private member nrOfMeasurments that contains the number of * stations that have this meteorological parameter available by calling getData(param, vecData, vecMeta), which * also fills the vectors vecData and vecMeta with the available data (as double) and metadata (as StationData). * Custom data preparation can obviously be done in this method.  Mathias Bavay committed May 04, 2011 72  *  73  * The calculate method must properly erase and reste the grid that it receives before filling it. If necessary,  Mathias Bavay committed Oct 29, 2010 74  * (as is the case for precipitation, relative humidity and snow height, for example) the grid can be checked for min/max by  Mathias Bavay committed Oct 29, 2010 75 76 77  * calling checkMinMax() at the end of Meteo2DInterpolator::interpolate.It can also add extra information about the * interpolation process (such as a regression coefficient or error estimate) to the InterpolationAlgorithm::info * stringstream (which will be made available to external programs, such as GUIs).  78  *  79 80  * The new class and its associated end user key must be used and its constructor called in AlgorithmFactory::getAlgorithm. * It is recommended that any generic statistical  81 82 83 84 85 86 87 88  * spatial processing be implemented as a static class in libinterpol2D.cc so that it could be reused by other * algorithms (see for example Interpol2D::IDW and IDWCore). In any case, proper doxygen documentation * must be written alongside the implementation. * * @section doc_2Dinterpol Documentation * The newly added interpolation algorithm must be added to the list of available algorithms in * InterpolationAlgorithms.h with a proper description. An example can also be given in the example section * of the same file. Please feel free to add necessary bibliographic references to the bibliographic section!  Mathias Bavay committed Oct 29, 2010 89  *  90 91 */  Thomas Egger committed Jun 21, 2010 92 93 94 95 /** * @class Meteo2DInterpolator * @brief A class to spatially interpolate meteo parameters. For more, see \ref interpol2d *  Mathias Bavay committed Jan 26, 2011 96  * @ingroup stats  Thomas Egger committed Jun 21, 2010 97 98 99 100 101  * @author Mathias Bavay and Thomas Egger * @date 2010-01-14 */ class Meteo2DInterpolator {  Mathias Bavay committed Jul 31, 2014 102  public:  Thomas Egger committed Jun 21, 2010 103 104 105  /** * @brief Constructor. */  Mathias Bavay committed Oct 30, 2014 106  Meteo2DInterpolator(const Config& i_cfg, TimeSeriesManager& i_tsmanager, GridsManager& i_gridsmanager);  Mathias Bavay committed Sep 01, 2013 107 108  ~Meteo2DInterpolator();  Thomas Egger committed Jun 21, 2010 109   Mathias Bavay committed Oct 30, 2014 110 111 112 113 114 115 116  ///Keywords for virtual stations strategy typedef enum VSTATIONS_POLICY { VSTATIONS, ///< extract virtual stations as specified in the ini file DOWNSCALING, ///< extract all grid points from a provided grid SMART_DOWNSCALING ///< extract all relevant grid points from a provided grid } vstations_policy;  Mathias Bavay committed Oct 27, 2010 117 118  /** * @brief A generic function that can interpolate for any given MeteoData member variable  119 120 121  * * @param date date for which to interpolate * @param dem Digital Elevation Model on which to perform the interpolation  Mathias Bavay committed May 04, 2011 122  * @param meteoparam Any MeteoData member variable as specified in the  Mathias Bavay committed Oct 27, 2010 123 124 125  * enum MeteoData::Parameters (e.g. MeteoData::TA) * @param result A Grid2DObject that will be filled with the interpolated data */  Thomas Egger committed Jan 14, 2011 126  void interpolate(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam,  127  Grid2DObject& result);  Mathias Bavay committed Oct 27, 2010 128   Thomas Egger committed Jun 21, 2010 129 130  /** * @brief A generic function that can interpolate for any given MeteoData member variable  131 132 133  * * @param date date for which to interpolate * @param dem Digital Elevation Model on which to perform the interpolation  Mathias Bavay committed May 04, 2011 134  * @param meteoparam Any MeteoData member variable as specified in the  Thomas Egger committed Jun 21, 2010 135 136  * enum MeteoData::Parameters (e.g. MeteoData::TA) * @param result A Grid2DObject that will be filled with the interpolated data  137  * @param InfoString some information about the interpolation process (useful for GUIs)  Thomas Egger committed Jun 21, 2010 138  */  Thomas Egger committed Jan 14, 2011 139  void interpolate(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam,  140  Grid2DObject& result, std::string& InfoString);  Thomas Egger committed Aug 16, 2010 141   Mathias Bavay committed Jun 19, 2014 142  void interpolate(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam,  Mathias Bavay committed Oct 30, 2014 143  const std::vector& in_coords, std::vector& result, std::string& info_string);  Mathias Bavay committed Jun 19, 2014 144   Mathias Bavay committed Nov 02, 2010 145  /**  146  * @brief Retrieve the arguments vector for a given interpolation algorithm  Mathias Bavay committed Nov 02, 2010 147 148 149 150  * @param param the meteorological parameter that is concerned * @param algorithm the desired algorithm * @param vecArgs a vector of strings containing the arguments */  151  size_t getArgumentsForAlgorithm(const std::string& param,  Mathias Bavay committed May 17, 2011 152 153  const std::string& algorithm, std::vector& vecArgs) const;  154   Mathias Bavay committed Oct 30, 2014 155 156 157 158 159 160 161 162  /** * @brief Compute point measurements from grids following a given computing strategy * @param strategy sampling/computing strategy * @param i_date when to compute the virtual stations * @param vecMeteo a vector of meteodata for the configured virtual stations */ size_t getVirtualMeteoData(const vstations_policy& strategy, const Date& i_date, METEO_SET& vecMeteo);  Mathias Bavay committed Apr 17, 2013 163  const std::string toString() const;  164   Thomas Egger committed Jun 21, 2010 165  private:  Mathias Bavay committed Oct 30, 2014 166  static Config stripVirtualConfig(const Config& cfg);  167 168 169 170 171  static void checkMinMax(const double& minval, const double& maxval, Grid2DObject& gridobj); static void check_projections(const DEMObject& dem, const std::vector& vec_meteo); static size_t get_parameters(const Config& cfg, std::set& set_parameters); static size_t getAlgorithmsForParameter(const Config& cfg, const std::string& parname, std::vector& vecAlgorithms);  Mathias Bavay committed Oct 30, 2014 172  size_t getVirtualStationsData(const Date& i_date, METEO_SET& vecMeteo);  Mathias Bavay committed May 25, 2012 173  void setAlgorithms();  Mathias Bavay committed Oct 30, 2014 174  void initVirtualStations();  Thomas Egger committed Jan 14, 2011 175   Thomas Egger committed Jul 26, 2010 176  const Config& cfg; ///< Reference to Config object, initialized during construction  Mathias Bavay committed Oct 30, 2014 177 178  TimeSeriesManager& tsmanager; ///< Reference to TimeSeriesManager object, used for callbacks, initialized during construction GridsManager& gridsmanager; ///< Reference to GridsManager object, used for callbacks, initialized during construction  179  GridBuffer grid_buffer;  Mathias Bavay committed Oct 30, 2014 180   Mathias Bavay committed Sep 01, 2013 181  std::map< std::string, std::vector > mapAlgorithms; //per parameter interpolation algorithms  Mathias Bavay committed Oct 30, 2014 182 183 184 185 186 187  std::vector v_params; ///< Parameters for virtual stations std::vector v_coords; ///< Coordinates for virtual stations std::vector v_stations; ///< metadata for virtual stations std::map virtual_point_cache; ///< stores already resampled virtual data points  Mathias Bavay committed Jun 19, 2014 188  bool algorithms_ready; ///< Have the algorithms objects been constructed?  Mathias Bavay committed Oct 30, 2014 189 190 191  bool use_full_dem; ///< use full dem for point-wise spatial interpolations bool downscaling; ///< Are we downscaling meteo grids instead of interpolating stations' data? bool virtual_stations; ///< compute the meteo values at virtual stations  Thomas Egger committed Jun 21, 2010 192 };  193   Thomas Egger committed Jun 21, 2010 194 195 196 } //end namespace #endif