UFO
DifferenceCheck.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2017-2018 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 
9 
10 #include <cmath>
11 #include <vector>
12 
13 #include "eckit/config/Configuration.h"
14 
15 #include "ioda/ObsDataVector.h"
16 #include "ioda/ObsSpace.h"
17 
18 #include "oops/util/Logger.h"
19 
20 namespace ufo {
21 
22 // -----------------------------------------------------------------------------
23 
24 DifferenceCheck::DifferenceCheck(ioda::ObsSpace & obsdb, const Parameters_ & parameters,
25  std::shared_ptr<ioda::ObsDataVector<int> > flags,
26  std::shared_ptr<ioda::ObsDataVector<float> > obserr)
27  : FilterBase(obsdb, parameters, flags, obserr),
28  parameters_(parameters)
29 {
30  oops::Log::trace() << "DifferenceCheck contructor starting" << std::endl;
33 }
34 
35 // -----------------------------------------------------------------------------
36 
38  oops::Log::trace() << "DifferenceCheck destructed" << std::endl;
39 }
40 
41 // -----------------------------------------------------------------------------
42 
43 void DifferenceCheck::applyFilter(const std::vector<bool> & apply,
44  const Variables & filtervars,
45  std::vector<std::vector<bool>> & flagged) const {
46  oops::Log::trace() << "DifferenceCheck priorFilter" << std::endl;
47 
48  const float missing = util::missingValue(missing);
49  const size_t nlocs = obsdb_.nlocs();
50 
51 // min/max value setup
52  float vmin = parameters_.minvalue.value().value_or(missing);
53  float vmax = parameters_.maxvalue.value().value_or(missing);
54 
55 // check for threshold and if exists, set vmin and vmax appropriately
56  const float thresh = config_.getFloat("threshold", missing);
57  if (thresh != missing) {
58  vmin = -thresh;
59  vmax = thresh;
60  }
61 
62 // Get reference values and values to compare (as floats)
63  std::vector<float> ref, val;
64  data_.get(parameters_.ref, ref);
65  data_.get(parameters_.val, val);
66  ASSERT(ref.size() == val.size());
67 
68 // Loop over all obs
69  for (size_t jobs = 0; jobs < nlocs; ++jobs) {
70  if (apply[jobs]) {
71  // check to see if one of the reference or value is missing
72  if (val[jobs] == missing || ref[jobs] == missing) {
73  for (size_t jv = 0; jv < filtervars.nvars(); ++jv) {
74  flagged[jv][jobs] = true;
75  }
76  } else {
77 // Check if difference is within min/max value range and set flag
78  float diff = val[jobs] - ref[jobs];
79  for (size_t jv = 0; jv < filtervars.nvars(); ++jv) {
80  if (vmin != missing && diff < vmin) flagged[jv][jobs] = true;
81  if (vmax != missing && diff > vmax) flagged[jv][jobs] = true;
82  }
83  }
84  }
85  }
86 }
87 
88 // -----------------------------------------------------------------------------
89 
90 void DifferenceCheck::print(std::ostream & os) const {
91  os << "DifferenceCheck::print not yet implemented ";
92 }
93 
94 // -----------------------------------------------------------------------------
95 
96 } // namespace ufo
void print(std::ostream &) const override
DifferenceCheck(ioda::ObsSpace &, const Parameters_ &, std::shared_ptr< ioda::ObsDataVector< int > >, std::shared_ptr< ioda::ObsDataVector< float > >)
void applyFilter(const std::vector< bool > &, const Variables &, std::vector< std::vector< bool >> &) const override
Parameters controlling the operation of the DifferenceCheck filter.
oops::RequiredParameter< Variable > ref
Name of the reference variable.
oops::OptionalParameter< float > maxvalue
oops::RequiredParameter< Variable > val
Name of the test variable.
oops::OptionalParameter< float > minvalue
Base class for UFO QC filters.
Definition: FilterBase.h:45
const eckit::LocalConfiguration config_
Definition: FilterBase.h:59
void get(const Variable &varname, std::vector< float > &values) const
Fills a std::vector with values of the specified variable.
ufo::Variables allvars_
ioda::ObsSpace & obsdb_
size_t nvars() const
Return the number of constituent "primitive" (single-channel) variables.
Definition: Variables.cc:104
constexpr int missing
Definition: QCflags.h:20
integer function nlocs(this)
Return the number of observational locations in this Locations object.
Definition: RunCRTM.h:27