WSL/SLF GitLab Repository

Meteo2DInterpolator.h 10.1 KB
Newer Older
1
/***********************************************************************************/
2
/*  Copyright 2014 WSL Institute for Snow and Avalanche Research    SLF-DAVOS      */
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 <http://www.gnu.org/licenses/>.
*/

#ifndef __METEO2DINTERPOLATOR_H__
#define __METEO2DINTERPOLATOR_H__

22
#include <meteoio/TimeSeriesManager.h>
23
#include <meteoio/Config.h>
24
#include <meteoio/dataClasses/Buffer.h>
25
26
27
#include <meteoio/dataClasses/Date.h>
#include <meteoio/dataClasses/MeteoData.h>
#include <meteoio/dataClasses/DEMObject.h>
28
#include <meteoio/InterpolationAlgorithms.h>
29
30
31
32
33
34
35

#include <memory>
#include <vector>
#include <map>

namespace mio {

36
class InterpolationAlgorithm;
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,
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):
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.
72
 *
73
 * The calculate method must properly erase and reste the grid that it receives before filling it. If necessary,
74
 * (as is the case for precipitation, relative humidity and snow height, for example) the grid can be checked for min/max by
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!
89
 *
90
91
*/

92
93
94
95
/**
 * @class Meteo2DInterpolator
 * @brief A class to spatially interpolate meteo parameters. For more, see \ref interpol2d
 *
96
 * @ingroup stats
97
98
99
100
101
 * @author Mathias Bavay and Thomas Egger
 * @date   2010-01-14
 */

class Meteo2DInterpolator {
Mathias Bavay's avatar
Mathias Bavay committed
102
	public:
103
104
105
		/**
		* @brief Constructor.
		*/
106
		Meteo2DInterpolator(const Config& i_cfg, TimeSeriesManager& i_tsmanager, GridsManager& i_gridsmanager);
107
108

		~Meteo2DInterpolator();
109

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;

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
122
		 * @param meteoparam Any MeteoData member variable as specified in the
123
124
125
		 * 				 enum MeteoData::Parameters (e.g. MeteoData::TA)
		 * @param result A Grid2DObject that will be filled with the interpolated data
		 */
126
		void interpolate(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam,
127
		                 Grid2DObject& result);
128

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
134
		 * @param meteoparam Any MeteoData member variable as specified in the
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)
138
		 */
139
		void interpolate(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam,
140
		                 Grid2DObject& result, std::string& InfoString);
141

142
		void interpolate(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam,
143
                            const std::vector<Coords>& in_coords, std::vector<double>& result, std::string& info_string);
144

145
		/**
146
		 * @brief Retrieve the arguments vector for a given interpolation algorithm
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,
152
153
		                                const std::string& algorithm,
		                                std::vector<std::string>& vecArgs) const;
154

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

163
		const std::string toString() const;
164

165
	private:
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<MeteoData>& vec_meteo);
		static size_t get_parameters(const Config& cfg, std::set<std::string>& set_parameters);
		static size_t getAlgorithmsForParameter(const Config& cfg, const std::string& parname, std::vector<std::string>& vecAlgorithms);

172
		size_t getVirtualStationsData(const Date& i_date, METEO_SET& vecMeteo);
173
		void setAlgorithms();
174
		void initVirtualStations();
175

176
		const Config& cfg; ///< Reference to Config object, initialized during construction
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;
180

181
		std::map< std::string, std::vector<InterpolationAlgorithm*> > mapAlgorithms; //per parameter interpolation algorithms
182
183
184
185
186
187

		std::vector<size_t> v_params; ///< Parameters for virtual stations
		std::vector<Coords> v_coords; ///< Coordinates for virtual stations
		std::vector<StationData> v_stations; ///< metadata for virtual stations
		std::map<Date, METEO_SET > virtual_point_cache;  ///< stores already resampled virtual data points

188
		bool algorithms_ready; ///< Have the algorithms objects been constructed?
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
192
};
193

194
195
196
} //end namespace

#endif