WSL/SLF GitLab Repository

ProcessingStack.cc 5.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/***********************************************************************************/
/*  Copyright 2009 WSL Institute for Snow and Avalanche Research    SLF-DAVOS      */
/***********************************************************************************/
/* 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/>.
*/
18
#include <meteoio/meteoFilters/ProcessingStack.h>
19

20
21
#include <algorithm>

22
23
24
25
using namespace std;

namespace mio {

26
ProcessingStack::ProcessingStack(const Config& cfg, const std::string& parname) : filter_stack(), param_name(parname)
27
{
28
	 //this is required by filters that need to read some parameters in extra files
29
	const string root_path = cfg.getConfigRootDir();
30
31
	vector<string> vecFilters;
	cfg.getValues(parname+"::filter", "Filters", vecFilters);
32

33
	const size_t nr_of_filters = vecFilters.size();
34
	for (size_t ii=0; ii<nr_of_filters; ii++){
35
		//create a processing block for each filter
36
		const string block_name = IOUtils::strToUpper( vecFilters[ii] );
37
		std::vector<std::string> vec_args;
38
		std::ostringstream tmp;
39
40
		tmp << param_name << "::arg" << (ii+1);

41
42
		//Read arguments
		cfg.getValue(tmp.str(), "Filters", vec_args, IOUtils::nothrow);
43
		filter_stack.push_back( BlockFactory::getBlock(block_name, vec_args, root_path) );
44
45
46
	}
}

47
48
ProcessingStack::~ProcessingStack()
{
49
	for (size_t ii=0; ii<filter_stack.size(); ii++)
50
51
52
		delete filter_stack[ii];
}

53
54
void ProcessingStack::getWindowSize(ProcessingProperties& o_properties)
{
55
56
	o_properties.points_before = 0;
	o_properties.points_after = 0;
57
58
	o_properties.time_after = Duration(0.0, 0.);
	o_properties.time_before = Duration(0.0, 0.);
59

60
	for (size_t jj=0; jj<filter_stack.size(); jj++){
61
62
		const ProcessingProperties& properties = (*filter_stack[jj]).getProperties();

63
64
		o_properties.points_before = max(o_properties.points_before, properties.points_before);
		o_properties.points_after = max(o_properties.points_after, properties.points_after);
65
66
67
68
69
70
71
72
73

		if (properties.time_before > o_properties.time_before)
			o_properties.time_before = properties.time_before;

		if (properties.time_after > o_properties.time_after)
			o_properties.time_after = properties.time_after;
	}
}

74
75
//this method applies the whole processing stack for all the stations, all the data points for one meteo param
//(as defined in the constructor)
76
77
78
void ProcessingStack::process(const std::vector< std::vector<MeteoData> >& ivec,
                              std::vector< std::vector<MeteoData> >& ovec, const bool& second_pass)
{
79
80
	const size_t nr_of_filters = filter_stack.size();
	const size_t nr_stations = ivec.size();
Mathias Bavay's avatar
Mathias Bavay committed
81
	ovec.resize( nr_stations );
82
83

	for (size_t ii=0; ii<nr_stations; ii++){ //for every station
84
		if ( ivec[ii].empty() ) continue; //no data, nothing to do!
85
86
87
88

		//pick one element and check whether the param_name parameter exists
		const size_t param = ivec[ii].front().getParameterIndex(param_name);
		if (param != IOUtils::npos){
89
			//since all filters start with ovec=ivec, maybe we could just swap pointers instead for copying
90
91
			std::vector<MeteoData> tmp( ivec[ii] );

92
93
94
95
96
97
98
99
100
			#ifdef DATA_QA
			for (size_t kk=0; kk<ivec[ii].size(); kk++) {
				const double orig = ivec[ii][kk](param);
				if (orig==IOUtils::nodata) {
					cout << "[DATA_QA] Filtering " << param_name << " is nodata " << ivec[ii][kk].date.toString(Date::ISO_TZ) << "\n";
				}
			}
			#endif

101
			//Now call the filters one after another for the current station and parameter
102
103
			bool appliedFilter = false;
			for (size_t jj=0; jj<nr_of_filters; jj++){
Mathias Bavay's avatar
Mathias Bavay committed
104
105
106
				const ProcessingProperties::proc_stage& filter_stage = filter_stack[jj]->getProperties().stage;
				if ( second_pass && ((filter_stage==ProcessingProperties::first) || (filter_stage==ProcessingProperties::none)) )
					continue;
107

Mathias Bavay's avatar
Mathias Bavay committed
108
109
110
111
				if ( !second_pass && ((filter_stage==ProcessingProperties::second) || (filter_stage==ProcessingProperties::none)) )
					continue;

				appliedFilter = true;
112
				(*filter_stack[jj]).process(static_cast<unsigned int>(param), tmp, ovec[ii]);
113

114
				if (tmp.size() == ovec[ii].size()){
115
					#ifdef DATA_QA
116
					for (size_t kk=0; kk<ovec[ii].size(); kk++) {
117
118
						const double orig = tmp[kk](param);
						const double filtered = ovec[ii][kk](param);
119
						if (orig!=filtered) {
120
							const string filtername = (*filter_stack[jj]).getName();
121
							cout << "[DATA_QA] Filtering " << param_name << "::" << filtername << " " << tmp[kk].date.toString(Date::ISO_TZ) << "\n";
122
123
124
						}
					}
					#endif
125
126
127
					if ((jj+1) != nr_of_filters){//after the last filter not necessary
						for (size_t kk=0; kk<ovec[ii].size(); kk++){
							tmp[kk](param) = ovec[ii][kk](param);
128
129
						}
					}
130
				} else {
131
132
133
134
					ostringstream ss;
					ss << "The filter \"" << (*filter_stack[jj]).getName() << "\" received " << tmp.size();
					ss << " timestamps and returned " << ovec[ii].size() << " timestamps!";
					throw IndexOutOfBoundsException(ss.str(), AT);
135
				}
136
			}
137

138
			if (!appliedFilter) //if not a single filter was applied
139
				ovec[ii] = ivec[ii]; //just copy input to output
140
141
		} else {
			ovec[ii] = ivec[ii]; //just copy input to output
142
143
144
145
		}
	}
}

146
147
const std::string ProcessingStack::toString() const
{
148
	std::ostringstream os;
149
	//os << "<ProcessingStack>";
150
	os << setw(10) << param_name << "::";
151

152
	for (size_t ii=0; ii<filter_stack.size(); ii++) {
153
		os << setw(10) << (*filter_stack[ii]).toString();
154
155
156
157
	}

	//os << "</ProcessingStack>";
	os << "\n";
158
	return os.str();
159
160
}

161
} //end namespace