UFO
BayesianBackgroundCheck.h
Go to the documentation of this file.
1 /*
2  * (C) Crown copyright 2021, Met Office
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 #ifndef UFO_FILTERS_BAYESIANBACKGROUNDCHECK_H_
9 #define UFO_FILTERS_BAYESIANBACKGROUNDCHECK_H_
10 
11 #include <memory>
12 #include <ostream>
13 #include <string>
14 #include <vector>
15 
16 #include "oops/util/ObjectCounter.h"
17 #include "oops/util/parameters/NumericConstraints.h"
18 #include "oops/util/parameters/Parameters.h"
19 #include "oops/util/parameters/RequiredParameter.h"
20 #include "oops/util/Printable.h"
21 #include "ufo/filters/FilterBase.h"
22 #include "ufo/filters/QCflags.h"
23 #include "ufo/filters/Variable.h"
26 
27 namespace ioda {
28  template <typename DATATYPE> class ObsDataVector;
29  class ObsSpace;
30 }
31 
32 namespace ufo {
33 
34 /// Parameters controlling the operation of the BayesianBackgroundCheck filter.
37 
38  public:
39  /// Parameters related to PGE calculations
41  // Probability density of observation being bad:
42  oops::RequiredParameter<float> PdBad{"prob density bad obs", this,
43  {oops::exclusiveMinConstraint(0.0f)}};
44  // Initial probability of gross error:
45  oops::RequiredParameter<float> PGE{"initial prob gross error", this,
46  {oops::exclusiveMinConstraint(0.0f)}};
47 };
48 
49 /// \brief BayesianBackgroundCheck: check observation closeness to background, accounting
50 /// for probability of gross error, i.e. that observation is bad.
51 /// \details Flag observations whose values differ from their model equivalents by too much.
52 /// "Too much" depends on the uncertainties of both observation and model
53 /// background, and the prior probability of the observation being "bad", or in
54 /// gross error. Can apply to scalar or vector observation types on single-level.
55 /// (Cannot handle profiles.) Calls function ufo::BayesianPGEUpdate.
56 ///
57 /// Requires the following be specified in .yaml, under
58 ///
59 /// obs filters:
60 /// - filter: Bayesian Background Check
61 ///
62 /// * prob density bad obs: uniform probability density that observation is "bad",
63 /// i.e. in gross error;
64 /// * initial prob gross error: initial PGE value of the uniform probability
65 /// distribution, before adjustment depending on obs-BG closeness;
66 ///
67 /// May also specify the following optional parameters (defaults in
68 /// ufo/utils/ProbabilityOfGrossErrorParameters.h):
69 /// * PGE threshold: fail if after-check PGE exceeds this value;
70 /// * obs minus BG threshold: fail if \f$([y-H(x)]/{\sigma})^2\f$ exceeds this;
71 /// * max exponent: maximum allowed value of the exponent in the 'good'
72 /// probability distribution;
73 /// * obs error multiplier: weight of observation error in combined error variance;
74 /// * BG error multiplier: weight of background error in combined error variance.
76  private util::ObjectCounter<BayesianBackgroundCheck> {
77  public:
78  /// The type of parameters accepted by the constructor of this filter.
79  /// This typedef is used by the FilterFactory.
81 
82  static const std::string classname() {return "ufo::BayesianBackgroundCheck";}
83 
84  BayesianBackgroundCheck(ioda::ObsSpace & obsdb, const Parameters_ & parameters,
85  std::shared_ptr<ioda::ObsDataVector<int> > flags,
86  std::shared_ptr<ioda::ObsDataVector<float> > obserr);
88 
89  private:
90  void print(std::ostream &) const override;
91 
92  /// \brief Apply Bayesian background check filter. Return flagged=true for rejected obs.
93  void applyFilter(const std::vector<bool> & apply, const Variables & filtervars,
94  std::vector<std::vector<bool>> & flagged) const override;
95 
96  /// \brief Return bayesianQC flag for observations rejected by Bayesian BG check.
97  int qcFlag() const override {return QCflags::bayesianQC;}
98 
99  /// \brief Return the name of the variable containing the background error estimate of the
100  /// specified filter variable.
101  Variable backgrErrVariable(const Variable & filterVariable) const;
102 
103  /// \brief Reduce a vector to only the elements for which j_reduced=true.
104  /// \param[in] vector_full: full vector,
105  /// \param[in] j_reduced: indices of vector_full to be copied into...
106  /// \return vector_reduced.
107  template <class T>
108  std::vector<T> reduceVector(const std::vector<T> & vector_full,
109  const std::vector<size_t> & j_reduced) const;
110 
111  /// \brief Copy a reduced vector back into the correct indices of the full vector.
112  /// \param[in] vector_reduced: reduced vector to restore to...
113  /// \param[inout] vector_full: full vector,
114  /// \param[in] j_reduced: indices of vector_full to be populated.
115  template <class T>
116  void unreduceVector(const std::vector<T> & vector_reduced,
117  std::vector<T> & vector_full,
118  const std::vector<size_t> & j_reduced) const;
120 };
121 
122 } // namespace ufo
123 
124 #endif // UFO_FILTERS_BAYESIANBACKGROUNDCHECK_H_
BayesianBackgroundCheck: check observation closeness to background, accounting for probability of gro...
std::vector< T > reduceVector(const std::vector< T > &vector_full, const std::vector< size_t > &j_reduced) const
Reduce a vector to only the elements for which j_reduced=true.
void applyFilter(const std::vector< bool > &apply, const Variables &filtervars, std::vector< std::vector< bool >> &flagged) const override
Apply Bayesian background check filter. Return flagged=true for rejected obs.
BayesianBackgroundCheckParameters Parameters_
static const std::string classname()
void unreduceVector(const std::vector< T > &vector_reduced, std::vector< T > &vector_full, const std::vector< size_t > &j_reduced) const
Copy a reduced vector back into the correct indices of the full vector.
Variable backgrErrVariable(const Variable &filterVariable) const
Return the name of the variable containing the background error estimate of the specified filter vari...
void print(std::ostream &) const override
int qcFlag() const override
Return bayesianQC flag for observations rejected by Bayesian BG check.
BayesianBackgroundCheck(ioda::ObsSpace &obsdb, const Parameters_ &parameters, std::shared_ptr< ioda::ObsDataVector< int > > flags, std::shared_ptr< ioda::ObsDataVector< float > > obserr)
Parameters controlling the operation of the BayesianBackgroundCheck filter.
oops::RequiredParameter< float > PGE
ProbabilityOfGrossErrorParameters PGEParameters
Parameters related to PGE calculations.
oops::RequiredParameter< float > PdBad
Base class for UFO QC filters.
Definition: FilterBase.h:45
Parameters shared by all filters having a default action (typically "reject").
Options controlling the operation of the calculations involving probability of gross error.
Forward declarations.
Definition: ObsAodExt.h:25
constexpr int bayesianQC
Definition: QCflags.h:36
Definition: RunCRTM.h:27