WSL/SLF GitLab Repository

ProcExpSmoothing.cc 3.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/***********************************************************************************/
/*  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/>.
*/
#include <meteoio/meteofilters/ProcExpSmoothing.h>
#include <cmath>

using namespace std;

namespace mio {

25
26
ProcExpSmoothing::ProcExpSmoothing(const std::vector<std::string>& vec_args)
                 : WindowedFilter("EXP_SMOOTHING"), alpha(.5)
27
28
29
30
31
32
33
34
35
36
37
38
39
{
	parse_args(vec_args);

	//This is safe, but maybe too imprecise:
	properties.time_before = min_time_span;
	properties.time_after  = min_time_span;
	properties.points_before = min_data_points;
	properties.points_after = min_data_points;
}

void ProcExpSmoothing::process(const unsigned int& param, const std::vector<MeteoData>& ivec,
                            std::vector<MeteoData>& ovec)
{
40
41
	ovec = ivec;
	for (size_t ii=0; ii<ovec.size(); ii++){ //for every element in ivec, get a window
42
43
		double& value = ovec[ii](param);

44
		size_t start, end;
45
		if( get_window_specs(ii, ivec, start, end) ) {
46
			value = calcExpSmoothing(ivec, param, start, end, ii);
47
48
49
50
51
		} else if(!is_soft) value = IOUtils::nodata;
	}

}

52
double ProcExpSmoothing::calcExpSmoothing(const std::vector<MeteoData>& ivec, const unsigned int& param, const size_t& start, const size_t& end, const size_t& pos)
53
{
54
	const size_t max_len = MAX(pos-start, end-pos);
55
56
57
	bool initCompleted = false;
	double expavg = IOUtils::nodata;

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
	for (size_t ii=1; ii<=max_len; ii++) {
		//getting values left and right of the current point
		const double val1 = ( pos>=ii && (pos-ii)>=start )? ivec[pos-ii](param) : IOUtils::nodata;
		const double val2 = ( (pos+ii)<=end )? ivec[pos+ii](param) : IOUtils::nodata;

		//computing the average (centered window) or take the proper point (left or right window)
		double val;
		if(val1!=IOUtils::nodata && val2!=IOUtils::nodata) {
			val = (val1+val2) * .5;
		} else if(val1!=IOUtils::nodata) {
			val = val1;
		} else if(val2!=IOUtils::nodata) {
			val = val2;
		} else
			val = IOUtils::nodata;

		//add the contribution
		if(val!=IOUtils::nodata) {
			if (initCompleted){
				expavg = alpha*val + (1.-alpha)*expavg;
78
			} else {
79
80
				expavg = val;
				initCompleted = true;
81
82
83
84
85
86
87
88
89
90
91
92
			}
		}
	}

	return expavg;
}

void ProcExpSmoothing::parse_args(std::vector<std::string> vec_args)
{
	vector<double> filter_args;

	if (vec_args.size() > 2){
93
		is_soft = ProcessingBlock::is_soft(vec_args);
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	}

	if (vec_args.size() > 2)
		centering = (WindowedFilter::Centering)WindowedFilter::get_centering(vec_args);

	convert_args(3, 3, vec_args, filter_args);

	if ((filter_args[0] < 1) || (filter_args[1] < 0)){
		throw InvalidArgumentException("Invalid window size configuration for filter " + getName(), AT);
	}

	min_data_points = (unsigned int)floor(filter_args[0]);
	min_time_span = Duration(filter_args[1] / 86400.0, 0.);
	alpha = filter_args[2];
	if(alpha<0. || alpha>1.) {
		throw InvalidArgumentException("The alpha parameter for filter " + getName() + " must be between 0 and 1!", AT);
	}
}

} //namespace