WSL/SLF GitLab Repository

Commit 58999ecb authored by Mathias Bavay's avatar Mathias Bavay
Browse files

It is now possible to only remove a specified fraction of a data set with the...

It is now possible to only remove a specified fraction of a data set with the SUPPR filter (at random)
parent d6887d53
......@@ -15,6 +15,9 @@
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 <ctime>
#include <cstdlib>
#include <meteoio/meteoFilters/FilterSuppr.h>
using namespace std;
......@@ -22,7 +25,7 @@ using namespace std;
namespace mio {
FilterSuppr::FilterSuppr(const std::vector<std::string>& vec_args, const std::string& name)
: FilterBlock(name)
: FilterBlock(name), range(IOUtils::nodata)
{
parse_args(vec_args);
properties.stage = ProcessingProperties::first; //for the rest: default values
......@@ -32,16 +35,39 @@ void FilterSuppr::process(const unsigned int& param, const std::vector<MeteoData
std::vector<MeteoData>& ovec)
{
ovec = ivec;
for (size_t ii=0; ii<ovec.size(); ii++){
ovec[ii](param) = IOUtils::nodata;
if (range==IOUtils::nodata) { //remove all
for (size_t ii=0; ii<ovec.size(); ii++){
ovec[ii](param) = IOUtils::nodata;
}
} else { //only remove a given fraction
const size_t set_size = ovec.size();
const size_t nrRemove = static_cast<size_t>( round( (double)set_size*range ) );
srand( static_cast<unsigned int>(time(NULL)) );
size_t ii=1;
while(ii<nrRemove) {
const size_t idx = rand() % set_size;
if (ivec[idx](param)!=IOUtils::nodata && ovec[idx](param)==IOUtils::nodata) continue; //the point was already removed
ovec[idx](param)=IOUtils::nodata; //ie nodata points remain and are counted
ii++;
}
}
}
void FilterSuppr::parse_args(std::vector<std::string> vec_args)
{
const size_t nrArgs = vec_args.size();
void FilterSuppr::parse_args(std::vector<std::string> vec_args) {
if ( !vec_args.empty() )
if (nrArgs>1)
throw InvalidArgumentException("Wrong number of arguments for filter " + getName(), AT);
if (nrArgs==1) {
IOUtils::convertString(range, vec_args[0]);
if (range<0. || range>1.)
throw InvalidArgumentException("Wrong range for filter " + getName() + ", it should be between 0 and 1", AT);
}
}
} //end namespace
......@@ -30,10 +30,16 @@ namespace mio {
* @author Mathias Bavay
* @date 2013-12-06
* @brief Suppression filter.
* This filter simply reject all values. This is convenient to quickly turn a parameter off
* without modifying the original data.
* Normally, this filter simply reject all values. This is convenient to quickly turn a parameter off
* without modifying the original data.
* But it is also possible to suppress a given fraction of the data at random by providing
* such fraction as an argument. For example, <i>0.5</i> would ensure that at least <i>50%</i> of the
* data set contains <i>nodata</i> for this parameter.
* @code
* ILWR::filter1 = suppr
* ILWR::filter1 = suppr
*
* TA::filter1 = suppr
* TA::arg1 = 0.5
* @endcode
*/
......@@ -46,6 +52,8 @@ class FilterSuppr : public FilterBlock {
private:
void parse_args(std::vector<std::string> vec_args);
double range;
};
} //end namespace
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment