WSL/SLF GitLab Repository

FilterWindAvg.cc 3.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/***********************************************************************************/
/*  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 <cmath>
19
#include <meteoio/meteofilters/FilterWindAvg.h>
20
#include <meteoio/meteolaws/Meteoconst.h>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

using namespace std;

namespace mio {

FilterWindAvg::FilterWindAvg(const std::vector<std::string>& vec_args) : WindowedFilter("WIND_AVG")
{
	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;
}

37
void FilterWindAvg::process(const unsigned int& param, const std::vector<MeteoData>& ivec,
38
39
                            std::vector<MeteoData>& ovec)
{
40
	if(param!=MeteoData::VW && param!=MeteoData::DW) {
41
		stringstream ss;
42
		ss << "Can not use WIND_AVG processing on " << MeteoData::getParameterName(param);
43
44
		throw InvalidArgumentException(ss.str(), AT);
	}
45

46
	ovec = ivec;
47

48
	for (size_t ii=0; ii<ovec.size(); ii++){ //for every element in ivec, get a window
49
50
		double& value = ovec[ii](param);

51
		size_t start, end;
52
53
		if( get_window_specs(ii, ivec, start, end) ) {
			value = calc_avg(ivec, param, start, end);
54
		} if(!is_soft) value = IOUtils::nodata;
55
56
57
	}
}

58
double FilterWindAvg::calc_avg(const std::vector<MeteoData>& ivec, const unsigned int& param, const size_t& start, const size_t& end)
59
{
60
61
62
63
64
65
66
	//calculate ve and vn
	double ve=0.0, vn=0.0;
	size_t count=0;
	for (size_t ii=start; ii<=end; ii++) {
		const double VW = ivec[ii](MeteoData::VW);
		const double DW = ivec[ii](MeteoData::DW);
		if(VW!=IOUtils::nodata && DW!=IOUtils::nodata) {
67
68
			ve += VW * sin(DW*Cst::to_rad);
			vn += VW * cos(DW*Cst::to_rad);
69
			count++;
70
		}
71
	}
72

73
74
	if(count==0) return IOUtils::nodata;

75
76
	ve /= static_cast<double>(count);
	vn /= static_cast<double>(count);
77
78
79
80
81

	if(param==MeteoData::VW) {
		const double meanspeed = sqrt(ve*ve + vn*vn);
		return meanspeed;
	} else {
82
		const double meandirection = fmod( atan2(ve,vn) * Cst::to_deg + 360. , 360.); // turn into degrees [0;360)
83
84
		return meandirection;
	}
85
86
87
88
89
90
91
}

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

	if (vec_args.size() > 2){
92
		is_soft = ProcessingBlock::is_soft(vec_args);
93
94
95
96
	}

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

98
	convert_args(2, 2, vec_args, filter_args);
99
100

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

	min_data_points = (unsigned int)floor(filter_args[0]);
	min_time_span = Duration(filter_args[1] / 86400.0, 0.);
106
}
107
108

}