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 eckit::Configuration & config,
26  std::shared_ptr<ioda::ObsDataVector<int> > flags,
27  std::shared_ptr<ioda::ObsDataVector<float> > obserr)
28  : FilterBase(obsdb, config, flags, obserr)
29 {
30  oops::Log::debug() << "Thinning: config = " << config_ << 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_.gnlocs();
45 
46  // get global indices of the local locations
47  const std::vector<std::size_t> & gindex = obsdb_.index();
48 
49  const float thinning = config_.getFloat("amount");
50 
51  // create random numbers for each observation based on some seed
52  unsigned int random_seed = config_.getInt("random seed", std::time(0));
53  int mymember = config_.getInt("member", 0);
54  random_seed += mymember;
55 
56  util::UniformDistribution<float> rand(gnlocs, 0.0, 1.0, random_seed);
57 
58  for (size_t jv = 0; jv < filtervars.nvars(); ++jv) {
59  for (size_t jobs = 0; jobs < nlocs; ++jobs) {
60  if ( apply[jobs] && rand[gindex[jobs]] < thinning ) flagged[jv][jobs] = true;
61  }
62  }
63 }
64 
65 // -----------------------------------------------------------------------------
66 
67 void Thinning::print(std::ostream & os) const {
68  os << "Thinning: config = " << config_ << std::endl;
69 }
70 
71 // -----------------------------------------------------------------------------
72 
73 } // namespace ufo
ufo::Variables::nvars
size_t nvars() const
Definition: Variables.cc:104
ufo::Variables
Definition: src/ufo/filters/Variables.h:24
ufo_radiancerttov_utils_mod::debug
logical, public debug
Definition: ufo_radiancerttov_utils_mod.F90:100
ufo::FilterBase::obsdb_
ioda::ObsSpace & obsdb_
Definition: FilterBase.h:59
ufo::FilterBase
FilterBase: Base class for UFO QC filters.
Definition: FilterBase.h:42
ufo::Thinning::print
void print(std::ostream &) const override
Definition: Thinning.cc:67
ufo
Definition: RunCRTM.h:27
ufo::Thinning::~Thinning
~Thinning()
Definition: Thinning.cc:35
ioda::ObsDataVector< int >
ufo::FilterBase::config_
const eckit::LocalConfiguration config_
Definition: FilterBase.h:60
ufo::Thinning::applyFilter
void applyFilter(const std::vector< bool > &, const Variables &, std::vector< std::vector< bool >> &) const override
Definition: Thinning.cc:39
ufo::Thinning::Thinning
Thinning(ioda::ObsSpace &, const eckit::Configuration &, std::shared_ptr< ioda::ObsDataVector< int > >, std::shared_ptr< ioda::ObsDataVector< float > >)
Definition: Thinning.cc:25
Thinning.h