UFO
Thinning.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2019 UCAR
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #include "ufo/filters/Thinning.h"
9 
10 #include <string>
11 #include <vector>
12 
13 #include "eckit/config/Configuration.h"
14 
15 #include "ioda/ObsDataVector.h"
16 #include "ioda/ObsSpace.h"
17 #include "oops/base/Variables.h"
18 #include "oops/util/Logger.h"
19 #include "oops/util/Random.h"
20 
21 namespace ufo {
22 
23 // -----------------------------------------------------------------------------
24 
25 Thinning::Thinning(ioda::ObsSpace & obsdb, const Parameters_ & parameters,
26  std::shared_ptr<ioda::ObsDataVector<int> > flags,
27  std::shared_ptr<ioda::ObsDataVector<float> > obserr)
28  : FilterBase(obsdb, parameters, flags, obserr), parameters_(parameters)
29 {
30  oops::Log::debug() << "Thinning: config = " << parameters_ << std::endl;
31 }
32 
33 // -----------------------------------------------------------------------------
34 
36 
37 // -----------------------------------------------------------------------------
38 
39 void Thinning::applyFilter(const std::vector<bool> & apply,
40  const Variables & filtervars,
41  std::vector<std::vector<bool>> & flagged) const {
42  // get local and global number of locations
43  const size_t nlocs = obsdb_.nlocs();
44  const size_t gnlocs = obsdb_.globalNumLocs();
45 
46  // get global indices of the local locations
47  const std::vector<std::size_t> & gindex = obsdb_.index();
48 
49  const float amount = parameters_.amount;
50 
51  // create random numbers for each observation based on some seed
52  unsigned int random_seed = parameters_.randomSeed.value().value_or(std::time(0));
53  random_seed += parameters_.member;
54 
55  util::UniformDistribution<float> rand(gnlocs, 0.0, 1.0, random_seed);
56 
57  for (size_t jv = 0; jv < filtervars.nvars(); ++jv) {
58  for (size_t jobs = 0; jobs < nlocs; ++jobs) {
59  if ( apply[jobs] && rand[gindex[jobs]] < amount ) flagged[jv][jobs] = true;
60  }
61  }
62 }
63 
64 // -----------------------------------------------------------------------------
65 
66 void Thinning::print(std::ostream & os) const {
67  os << "Thinning: config = " << parameters_ << std::endl;
68 }
69 
70 // -----------------------------------------------------------------------------
71 
72 } // namespace ufo
Base class for UFO QC filters.
Definition: FilterBase.h:45
ioda::ObsSpace & obsdb_
void applyFilter(const std::vector< bool > &, const Variables &, std::vector< std::vector< bool >> &) const override
Definition: Thinning.cc:39
Parameters_ parameters_
Definition: Thinning.h:75
void print(std::ostream &) const override
Definition: Thinning.cc:66
Thinning(ioda::ObsSpace &, const Parameters_ &, std::shared_ptr< ioda::ObsDataVector< int > >, std::shared_ptr< ioda::ObsDataVector< float > >)
Definition: Thinning.cc:25
Parameters controlling the operation of the Thinning filter.
Definition: Thinning.h:36
oops::Parameter< int > member
Index of the ensemble member.
Definition: Thinning.h:48
oops::OptionalParameter< int > randomSeed
Definition: Thinning.h:45
oops::RequiredParameter< float > amount
(Approximate) fraction of observations to be thinned.
Definition: Thinning.h:41
size_t nvars() const
Return the number of constituent "primitive" (single-channel) variables.
Definition: Variables.cc:104
integer function nlocs(this)
Return the number of observational locations in this Locations object.
Definition: RunCRTM.h:27