UFO
PredictorBase.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020 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 #ifndef UFO_PREDICTORS_PREDICTORBASE_H_
9 #define UFO_PREDICTORS_PREDICTORBASE_H_
10 
11 #include <map>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include <boost/make_unique.hpp>
17 #include <boost/noncopyable.hpp>
18 
19 #include "ioda/ObsVector.h"
20 
21 #include "oops/base/Variables.h"
22 #include "oops/util/AssociativeContainers.h"
23 #include "oops/util/parameters/OptionalParameter.h"
24 #include "oops/util/parameters/Parameters.h"
25 #include "oops/util/parameters/RequiredParameter.h"
26 
27 namespace ioda {
28  class ObsSpace;
29 }
30 
31 namespace ufo {
32  class GeoVaLs;
33  class ObsDiagnostics;
34 
35 // -----------------------------------------------------------------------------
36 /// Base class for predictor parameters
37 class PredictorParametersBase : public oops::Parameters {
38  OOPS_ABSTRACT_PARAMETERS(PredictorParametersBase, Parameters)
39 
40  public:
41  /// \brief Predictor name.
42  oops::RequiredParameter<std::string> name{"name", this};
43 };
44 
45 // -----------------------------------------------------------------------------
46 /// Concrete implementation of PredictorParametersBase with no new parameters. Useful for predictors
47 /// that don't take any options.
49  OOPS_CONCRETE_PARAMETERS(EmptyPredictorParameters, PredictorParametersBase)
50 
51  // no other parameters needed
52 };
53 
54 // -----------------------------------------------------------------------------
55 /// Base class for computing predictors
56 ///
57 /// Note: each concrete implementation should typedef `Parameters_` to the name of a subclass of
58 /// PredictorParametersBase encapsulating its configuration options. It should also provide
59 /// a constructor with the following signature:
60 ///
61 /// PredictorBase(const Parameters_ &, const oops::Variables &);
62 class PredictorBase : private boost::noncopyable {
63  public:
64  explicit PredictorBase(const PredictorParametersBase &, const oops::Variables &);
65  virtual ~PredictorBase() = default;
66 
67  /// compute the predictor
68  virtual void compute(const ioda::ObsSpace &,
69  const GeoVaLs &,
70  const ObsDiagnostics &,
71  ioda::ObsVector &) const = 0;
72 
73  /// geovars names required to compute the predictor
74  const oops::Variables & requiredGeovars() const {return geovars_;}
75 
76  /// hdiags names required to compute the predictor
77  const oops::Variables & requiredHdiagnostics() const {return hdiags_;}
78 
79  /// predictor name
80  std::string & name() {return func_name_;}
81  const std::string & name() const {return func_name_;}
82 
83  protected:
84  oops::Variables vars_; ///< variables that will be bias-corrected using this predictor
85  oops::Variables geovars_; ///< required GeoVaLs
86  oops::Variables hdiags_; ///< required ObsDiagnostics
87 
88  private:
89  std::string func_name_; ///< predictor name
90 };
91 
92 typedef std::vector<std::shared_ptr<PredictorBase>> Predictors;
93 
94 // -----------------------------------------------------------------------------
95 
96 /// Predictor Factory
98  public:
99  /// \brief Create and return a new predictor.
100  ///
101  /// The predictor type is determined by the \c name attribute of \p parameters.
102  /// \p parameters must be an instance of the subclass of PredictorParametersBase
103  /// associated with that predictor type, otherwise an exception will be thrown.
104  static std::unique_ptr<PredictorBase> create(const PredictorParametersBase &parameters,
105  const oops::Variables &vars);
106 
107  /// \brief Create and return an instance of the subclass of PredictorParametersBase
108  /// storing parameters of predictors of the specified type.
109  static std::unique_ptr<PredictorParametersBase> createParameters(const std::string &name);
110 
111  /// \brief Return the names of all predictors that can be created by one of the registered makers.
112  static std::vector<std::string> getMakerNames() {
113  return oops::keys(getMakers());
114  }
115 
116  /// \brief Return true if a maker has been registered for a predictor of type \p name.
117  static bool predictorExists(const std::string &name);
118 
119  virtual ~PredictorFactory() = default;
120 
121  protected:
122  /// \brief Register a maker able to create predictors of type \p name.
123  explicit PredictorFactory(const std::string &name);
124 
125  private:
126  virtual std::unique_ptr<PredictorBase> make(const PredictorParametersBase &,
127  const oops::Variables &) = 0;
128 
129  virtual std::unique_ptr<PredictorParametersBase> makeParameters() const = 0;
130 
131  static std::map < std::string, PredictorFactory * > & getMakers() {
132  static std::map < std::string, PredictorFactory * > makers_;
133  return makers_;
134  }
135 };
136 
137 // -----------------------------------------------------------------------------
138 
139 template<class T>
141  typedef typename T::Parameters_ Parameters_;
142 
143  std::unique_ptr<PredictorBase> make(const PredictorParametersBase& parameters,
144  const oops::Variables & vars) override {
145  const auto &stronglyTypedParameters = dynamic_cast<const Parameters_&>(parameters);
146  return boost::make_unique<T>(stronglyTypedParameters, vars);
147  }
148 
149  std::unique_ptr<PredictorParametersBase> makeParameters() const override {
150  return boost::make_unique<Parameters_>();
151  }
152 
153  public:
154  explicit PredictorMaker(const std::string & name)
155  : PredictorFactory(name) {}
156 };
157 
158 // -----------------------------------------------------------------------------
159 
160 } // namespace ufo
161 
162 #endif // UFO_PREDICTORS_PREDICTORBASE_H_
GeoVaLs: geophysical values at locations.
std::string & name()
predictor name
Definition: PredictorBase.h:80
std::string func_name_
predictor name
Definition: PredictorBase.h:89
const std::string & name() const
Definition: PredictorBase.h:81
virtual void compute(const ioda::ObsSpace &, const GeoVaLs &, const ObsDiagnostics &, ioda::ObsVector &) const =0
compute the predictor
oops::Variables vars_
variables that will be bias-corrected using this predictor
Definition: PredictorBase.h:84
oops::Variables geovars_
required GeoVaLs
Definition: PredictorBase.h:85
PredictorBase(const PredictorParametersBase &, const oops::Variables &)
const oops::Variables & requiredGeovars() const
geovars names required to compute the predictor
Definition: PredictorBase.h:74
oops::Variables hdiags_
required ObsDiagnostics
Definition: PredictorBase.h:86
virtual ~PredictorBase()=default
const oops::Variables & requiredHdiagnostics() const
hdiags names required to compute the predictor
Definition: PredictorBase.h:77
Predictor Factory.
Definition: PredictorBase.h:97
static std::unique_ptr< PredictorBase > create(const PredictorParametersBase &parameters, const oops::Variables &vars)
Create and return a new predictor.
PredictorFactory(const std::string &name)
Register a maker able to create predictors of type name.
static bool predictorExists(const std::string &name)
Return true if a maker has been registered for a predictor of type name.
virtual std::unique_ptr< PredictorParametersBase > makeParameters() const =0
virtual std::unique_ptr< PredictorBase > make(const PredictorParametersBase &, const oops::Variables &)=0
static std::vector< std::string > getMakerNames()
Return the names of all predictors that can be created by one of the registered makers.
virtual ~PredictorFactory()=default
static std::map< std::string, PredictorFactory * > & getMakers()
static std::unique_ptr< PredictorParametersBase > createParameters(const std::string &name)
Create and return an instance of the subclass of PredictorParametersBase storing parameters of predic...
T::Parameters_ Parameters_
std::unique_ptr< PredictorBase > make(const PredictorParametersBase &parameters, const oops::Variables &vars) override
std::unique_ptr< PredictorParametersBase > makeParameters() const override
PredictorMaker(const std::string &name)
Base class for predictor parameters.
Definition: PredictorBase.h:37
oops::RequiredParameter< std::string > name
Predictor name.
Definition: PredictorBase.h:42
Forward declarations.
Definition: ObsAodExt.h:25
Definition: RunCRTM.h:27
std::vector< std::shared_ptr< PredictorBase > > Predictors
Definition: PredictorBase.h:92