WSL/SLF GitLab Repository

ProcessingStack.cc 4.67 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
37
		//Read arguments
		cfg.getValue(tmp.str(), "Filters", vec_args, IOUtils::nothrow);
38
		filter_stack.push_back( BlockFactory::getBlock(block_name, vec_args) );
39
40
41
	}
}

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

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

55
	for (size_t jj=0; jj<filter_stack.size(); jj++){
56
57
58
59
60
61
62
63
64
65
66
67
68
		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;
	}
}

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

	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++){
Mathias Bavay's avatar
Mathias Bavay committed
89
90
91
				const ProcessingProperties::proc_stage& filter_stage = filter_stack[jj]->getProperties().stage;
				if ( second_pass && ((filter_stage==ProcessingProperties::first) || (filter_stage==ProcessingProperties::none)) )
					continue;
92

Mathias Bavay's avatar
Mathias Bavay committed
93
94
95
96
				if ( !second_pass && ((filter_stage==ProcessingProperties::second) || (filter_stage==ProcessingProperties::none)) )
					continue;

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

99
100
101
102
				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);
103
104
						}
					}
105
106
				} else {
					tmp = ovec[ii];
107
				}
108
			}
109

110
			if (!appliedFilter) //if not a single filter was applied
111
				ovec[ii] = ivec[ii]; //just copy input to output
112
113
		} else {
			ovec[ii] = ivec[ii]; //just copy input to output
114
115
116
117
		}
	}
}

118
119
120
const std::string ProcessingStack::toString() const
{
	std::stringstream os;
121
	//os << "<ProcessingStack>";
122
	os << setw(10) << param_name << "::";
123

124
125
	for(size_t ii=0; ii<filter_stack.size(); ii++) {
		os << setw(10) << (*filter_stack[ii]).toString();
126
127
128
129
	}

	//os << "</ProcessingStack>";
	os << "\n";
130
	return os.str();
131
132
}

133
} //end namespace