MeteoIODoc 20241221.207bde49
ResamplingAlgorithms.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4/***********************************************************************************/
5/* This file is part of MeteoIO.
6 MeteoIO is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MeteoIO is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef RESAMPLINGALGORITHMS_H
20#define RESAMPLINGALGORITHMS_H
21
23#include <meteoio/IOUtils.h>
24
25#include <string>
26#include <vector>
27
28#ifdef _MSC_VER
29 #pragma warning(disable:4512) //we don't need any = operator!
30#endif
31
32namespace mio {
44
45 public:
51 end
52 };
53
54 typedef struct GAP_INFO {
55 GAP_INFO() : start(), end(), startIdx(IOUtils::npos), endIdx(IOUtils::npos) {}
56 void extend(const size_t& idx, const std::vector<MeteoData>& vecM) {if (idx<startIdx) setStart(idx, vecM); if (idx>endIdx) setEnd(idx, vecM);}
57 void setStart(const size_t& idx, const std::vector<MeteoData>& vecM) {if (idx>=vecM.size()) return; startIdx=idx; start=vecM[idx].date;}
58 void setEnd(const size_t& idx, const std::vector<MeteoData>& vecM) {if (idx>=vecM.size()) return; endIdx=idx; end=vecM[idx].date;}
59 std::string toString() const {std::ostringstream os; os << start.toString(Date::ISO) << " (" << startIdx << ") - " << end.toString(Date::ISO) << " (" << endIdx << ")"; return os.str();}
60 double size() const {return std::abs(end.getJulian(true)-start.getJulian(true));}
61 Date start, end;
62 size_t startIdx, endIdx;
63 } gap_info;
64
65 static size_t searchBackward(gap_info &last_gap, const size_t& pos, const size_t& paramindex, const std::vector<MeteoData>& vecM, const Date& resampling_date,
66 const double& i_max_gap_size);
67 static size_t searchForward(gap_info &last_gap, const size_t& pos, const size_t& paramindex, const std::vector<MeteoData>& vecM, const Date& resampling_date,
68 const double& i_max_gap_size, const size_t& indexP1);
69
70 static gap_info findGap(const size_t& pos, const size_t& paramindex, const std::vector<MeteoData>& vecM, const Date& resampling_date,
71 const double& i_max_gap_size);
72
73 ResamplingAlgorithms(const std::string& i_algoname, const std::string& i_parname, const double& dflt_max_gap_size, const std::vector< std::pair<std::string, std::string> >& /*vecArgs*/)
74 : algo(i_algoname), parname(i_parname), max_gap_size(dflt_max_gap_size), gaps() {}
75
77
78 const std::string getAlgo() const {return algo;}
79
80 virtual void resample(const std::string& stationHash, const size_t& index, const ResamplingPosition& position, const size_t& paramindex,
81 const std::vector<MeteoData>& vecM, MeteoData& md) = 0;
82
83 void resetResampling() {gaps.clear();} //invalidate all gaps, usually after rebuffering
84
85 virtual std::string toString() const = 0;
86
87 protected:
88 static double partialAccumulateAtLeft(const std::vector<MeteoData>& vecM, const size_t& paramindex,
89 const size_t& pos, const Date& curr_date);
90 static double partialAccumulateAtRight(const std::vector<MeteoData>& vecM, const size_t& paramindex,
91 const size_t& pos, const Date& curr_date);
92 void getNearestValidPts(const std::string& stationHash, const size_t& pos, const size_t& paramindex, const std::vector<MeteoData>& vecM, const Date& resampling_date,
93 const double& i_max_gap_size, size_t& indexP1, size_t& indexP2);
94 static double linearInterpolation(const double& x1, const double& y1,
95 const double& x2, const double& y2, const double& x3);
96 static Date getDailyStart(const Date& resampling_date);
97 static size_t getDailyValue(const std::vector<MeteoData>& vecM, const size_t& paramindex, size_t pos, const Date& intervalStart, const Date& intervalEnd);
98
99 const std::string algo, parname;
101 static const double soil_albedo, snow_albedo, snow_thresh;
102
103 private:
104 std::map<std::string, gap_info> gaps;
105
106};
107
109 public:
110 static ResamplingAlgorithms* getAlgorithm(const std::string& i_algoname, const std::string& parname, const double& max_gap_size, const std::vector< std::pair<std::string, std::string> >& vecArgs, const Config &cfg);
111};
112
113} //end namespace
114#endif
A class that reads a key/value file. These files (typically named *.ini) follow the INI file format s...
Definition: Config.h:79
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:87
@ ISO
ISO 8601 extended format combined date: YYYY-MM-DDTHH:mm:SS.sss (fields might be dropped,...
Definition: Date.h:91
A class to represent a singular measurement received from one station at a certain time (represented ...
Definition: MeteoData.h:108
Definition: ResamplingAlgorithms.h:108
static ResamplingAlgorithms * getAlgorithm(const std::string &i_algoname, const std::string &parname, const double &max_gap_size, const std::vector< std::pair< std::string, std::string > > &vecArgs, const Config &cfg)
Definition: ResamplingAlgorithms.cc:95
Interface class for the temporal resampling algorithms.
Definition: ResamplingAlgorithms.h:43
static const double snow_thresh
These thresholds are used to handle solar radiation.
Definition: ResamplingAlgorithms.h:101
static double partialAccumulateAtRight(const std::vector< MeteoData > &vecM, const size_t &paramindex, const size_t &pos, const Date &curr_date)
Definition: ResamplingAlgorithms.cc:144
virtual ~ResamplingAlgorithms()
Definition: ResamplingAlgorithms.h:76
const std::string parname
Definition: ResamplingAlgorithms.h:99
ResamplingAlgorithms(const std::string &i_algoname, const std::string &i_parname, const double &dflt_max_gap_size, const std::vector< std::pair< std::string, std::string > > &)
Definition: ResamplingAlgorithms.h:73
void getNearestValidPts(const std::string &stationHash, const size_t &pos, const size_t &paramindex, const std::vector< MeteoData > &vecM, const Date &resampling_date, const double &i_max_gap_size, size_t &indexP1, size_t &indexP2)
This function returns the last and next valid points around a given position.
Definition: ResamplingAlgorithms.cc:284
virtual std::string toString() const =0
static gap_info findGap(const size_t &pos, const size_t &paramindex, const std::vector< MeteoData > &vecM, const Date &resampling_date, const double &i_max_gap_size)
Definition: ResamplingAlgorithms.cc:258
void resetResampling()
Definition: ResamplingAlgorithms.h:83
virtual void resample(const std::string &stationHash, const size_t &index, const ResamplingPosition &position, const size_t &paramindex, const std::vector< MeteoData > &vecM, MeteoData &md)=0
const std::string getAlgo() const
Definition: ResamplingAlgorithms.h:78
static double partialAccumulateAtLeft(const std::vector< MeteoData > &vecM, const size_t &paramindex, const size_t &pos, const Date &curr_date)
Definition: ResamplingAlgorithms.cc:125
const std::string algo
Definition: ResamplingAlgorithms.h:99
static const double snow_albedo
snow albedo
Definition: ResamplingAlgorithms.h:101
static Date getDailyStart(const Date &resampling_date)
For a given date, find the start of the day, considering that for midnight we return the day before!...
Definition: ResamplingAlgorithms.cc:318
ResamplingPosition
Definition: ResamplingAlgorithms.h:46
@ after
Definition: ResamplingAlgorithms.h:49
@ end
Definition: ResamplingAlgorithms.h:51
@ before
Definition: ResamplingAlgorithms.h:48
@ exact_match
Definition: ResamplingAlgorithms.h:47
@ begin
Definition: ResamplingAlgorithms.h:50
static const double soil_albedo
grass albedo
Definition: ResamplingAlgorithms.h:101
static double linearInterpolation(const double &x1, const double &y1, const double &x2, const double &y2, const double &x3)
This function solves the equation y = ax + b for two given points and returns y for a given x.
Definition: ResamplingAlgorithms.cc:301
static size_t getDailyValue(const std::vector< MeteoData > &vecM, const size_t &paramindex, size_t pos, const Date &intervalStart, const Date &intervalEnd)
Find a unique value in a given time interval. This is useful for retrieving a unique daily average,...
Definition: ResamplingAlgorithms.cc:338
static size_t searchBackward(gap_info &last_gap, const size_t &pos, const size_t &paramindex, const std::vector< MeteoData > &vecM, const Date &resampling_date, const double &i_max_gap_size)
Definition: ResamplingAlgorithms.cc:164
double max_gap_size
Definition: ResamplingAlgorithms.h:100
static size_t searchForward(gap_info &last_gap, const size_t &pos, const size_t &paramindex, const std::vector< MeteoData > &vecM, const Date &resampling_date, const double &i_max_gap_size, const size_t &indexP1)
Definition: ResamplingAlgorithms.cc:212
const size_t npos
npos is the out-of-range value
Definition: IOUtils.h:80
Definition: Config.cc:31
Definition: ResamplingAlgorithms.h:54
std::string toString() const
Definition: ResamplingAlgorithms.h:59
void extend(const size_t &idx, const std::vector< MeteoData > &vecM)
Definition: ResamplingAlgorithms.h:56
size_t endIdx
Definition: ResamplingAlgorithms.h:62
GAP_INFO()
Definition: ResamplingAlgorithms.h:55
void setStart(const size_t &idx, const std::vector< MeteoData > &vecM)
Definition: ResamplingAlgorithms.h:57
double size() const
Definition: ResamplingAlgorithms.h:60
Date end
Definition: ResamplingAlgorithms.h:61
void setEnd(const size_t &idx, const std::vector< MeteoData > &vecM)
Definition: ResamplingAlgorithms.h:58