WSL/SLF GitLab Repository

ProcessingStack.cc 5.04 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
22
23

using namespace std;

namespace mio {

24
ProcessingStack::ProcessingStack(const Config& cfg, const std::string& parname) : filter_stack(), param_name(parname)
25
{
26
27
28
	vector<string> vecFilters;
	cfg.getValues(parname+"::filter", "Filters", vecFilters);
	const size_t nr_of_filters = vecFilters.size();
29
	for (size_t ii=0; ii<nr_of_filters; ii++){
30
		//create a processing block for each filter
31
		const string block_name = IOUtils::strToUpper( vecFilters[ii] );
32
		std::vector<std::string> vec_args;
33
		std::ostringstream tmp;
34
35
		tmp << param_name << "::arg" << (ii+1);

36
		getArgumentsForFilter(cfg, tmp.str(), vec_args); //Read arguments
37
		filter_stack.push_back( BlockFactory::getBlock(block_name, vec_args) );
38
39
40
	}
}

41
42
ProcessingStack::~ProcessingStack()
{
43
	for (size_t ii=0; ii<filter_stack.size(); ii++)
44
45
46
		delete filter_stack[ii];
}

47
48
void ProcessingStack::getWindowSize(ProcessingProperties& o_properties)
{
49
50
	o_properties.points_before = 0;
	o_properties.points_after = 0;
51
52
	o_properties.time_after = Duration(0.0, 0.);
	o_properties.time_before = Duration(0.0, 0.);
53

54
	for (size_t jj=0; jj<filter_stack.size(); jj++){
55
56
57
58
59
60
61
62
63
64
65
66
67
		const ProcessingProperties& properties = (*filter_stack[jj]).getProperties();

		o_properties.points_before = MAX(o_properties.points_before, properties.points_before);
		o_properties.points_after = MAX(o_properties.points_after, properties.points_after);

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

68
void ProcessingStack::getArgumentsForFilter(const Config& cfg, const std::string& keyname,
69
                                                    std::vector<std::string>& vecArguments)
70
{
71
	// Retrieve the values for a given 'keyname' and store them in a vector calles 'vecArguments'
72
	cfg.getValue(keyname, "Filters", vecArguments, IOUtils::nothrow);
73
74
}

75
76
//this method applies the whole processing stack for all the stations, all the data points for one meteo param
//(as defined in the constructor)
77
78
79
void ProcessingStack::process(const std::vector< std::vector<MeteoData> >& ivec,
                              std::vector< std::vector<MeteoData> >& ovec, const bool& second_pass)
{
80
	ovec.resize( ivec.size() );
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	const size_t nr_of_filters = filter_stack.size();
	const size_t nr_stations = ivec.size();

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

		//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){
			std::vector<MeteoData> tmp( ivec[ii] );

			//Now call the filters in a row
			bool appliedFilter = false;
			for (size_t jj=0; jj<nr_of_filters; jj++){
				if (second_pass){
					if ((*filter_stack[jj]).getProperties().stage==ProcessingProperties::first
						|| (*filter_stack[jj]).getProperties().stage==ProcessingProperties::none)
						continue;
				}
				if (!second_pass){
					if ((*filter_stack[jj]).getProperties().stage==ProcessingProperties::second
						|| (*filter_stack[jj]).getProperties().stage==ProcessingProperties::none)
						continue;
				}
				appliedFilter = true;
106

107
				(*filter_stack[jj]).process(static_cast<unsigned int>(param), tmp, ovec[ii]);
108

109
110
111
112
				if (tmp.size() == ovec[ii].size()){
					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);
113
114
						}
					}
115
116
				} else {
					tmp = ovec[ii];
117
				}
118
			}
119

120
			if (!appliedFilter) //if not a single filter was applied
121
				ovec[ii] = ivec[ii]; //just copy input to output
122
123
		} else {
			ovec[ii] = ivec[ii]; //just copy input to output
124
125
126
127
		}
	}
}

128
129
130
const std::string ProcessingStack::toString() const
{
	std::stringstream os;
131
	//os << "<ProcessingStack>";
132
	os << setw(10) << param_name << "::";
133

134
135
	for(size_t ii=0; ii<filter_stack.size(); ii++) {
		os << setw(10) << (*filter_stack[ii]).toString();
136
137
138
139
	}

	//os << "</ProcessingStack>";
	os << "\n";
140
	return os.str();
141
142
}

143
} //end namespace