OOPS
ObsErrorBase.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
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  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #ifndef OOPS_BASE_OBSERRORBASE_H_
12 #define OOPS_BASE_OBSERRORBASE_H_
13 
14 #include <map>
15 #include <memory>
16 #include <string>
17 
18 #include <boost/noncopyable.hpp>
19 #include "eckit/config/Configuration.h"
20 
22 #include "oops/util/abor1_cpp.h"
23 #include "oops/util/Logger.h"
24 #include "oops/util/Printable.h"
25 
26 namespace oops {
27 
28 // -----------------------------------------------------------------------------
29 /// \brief Base class for observation error covariance matrices.
30 template<typename OBS>
31 class ObsErrorBase : public util::Printable,
32  private boost::noncopyable {
35 
36  public:
37  ObsErrorBase() = default;
38  virtual ~ObsErrorBase() = default;
39 
40 /// Multiply a Departure \p dy by \f$R\f$$
41  virtual void multiply(ObsVector_ & dy) const = 0;
42 /// Multiply a Departure \p dy by \f$R^{-1}\f$
43  virtual void inverseMultiply(ObsVector_ & dy) const = 0;
44 
45 /// Generate random perturbation in \p dy
46  virtual void randomize(ObsVector_ & dy) const = 0;
47 
48 /// Return inverseVariance
49  virtual const ObsVector_ & inverseVariance() const = 0;
50 
51 /// Get mean error for Jo table
52  virtual double getRMSE() const = 0;
53 };
54 
55 // =============================================================================
56 
57 /// ObsErrorFactory Factory
58 template <typename OBS>
61  public:
62  static std::unique_ptr<ObsErrorBase<OBS> > create(const eckit::Configuration &,
63  const ObsSpace_ &);
64  virtual ~ObsErrorFactory() = default;
65  protected:
66  explicit ObsErrorFactory(const std::string &);
67  private:
68  virtual ObsErrorBase<OBS> * make(const eckit::Configuration &, const ObsSpace_ &) = 0;
69  static std::map < std::string, ObsErrorFactory<OBS> * > & getMakers() {
70  static std::map < std::string, ObsErrorFactory<OBS> * > makers_;
71  return makers_;
72  }
73 };
74 
75 // -----------------------------------------------------------------------------
76 
77 template<class OBS, class T>
78 class ObsErrorMaker : public ObsErrorFactory<OBS> {
80  virtual ObsErrorBase<OBS> * make(const eckit::Configuration & conf,
81  const ObsSpace_ & obs)
82  { return new T(conf, obs); }
83  public:
84  explicit ObsErrorMaker(const std::string & name) : ObsErrorFactory<OBS>(name) {}
85 };
86 
87 // =============================================================================
88 
89 template <typename OBS>
90 ObsErrorFactory<OBS>::ObsErrorFactory(const std::string & name) {
91  if (getMakers().find(name) != getMakers().end()) {
92  throw std::runtime_error(name + " already registered in obs error factory.");
93  }
94  getMakers()[name] = this;
95 }
96 
97 // -----------------------------------------------------------------------------
98 
99 template <typename OBS>
100 std::unique_ptr<ObsErrorBase<OBS>>
101 ObsErrorFactory<OBS>::create(const eckit::Configuration & conf, const ObsSpace_ & obs) {
102  Log::trace() << "ObsErrorBase<OBS>::create starting" << std::endl;
103  const std::string id = conf.getString("covariance model");
104  typename std::map<std::string, ObsErrorFactory<OBS>*>::iterator
105  jerr = getMakers().find(id);
106  if (jerr == getMakers().end()) {
107  throw std::runtime_error(id + " does not exist in obs error factory.");
108  }
109  std::unique_ptr<ObsErrorBase<OBS>> ptr(jerr->second->make(conf, obs));
110  Log::trace() << "ObsErrorBase<OBS>::create done" << std::endl;
111  return ptr;
112 }
113 
114 // -----------------------------------------------------------------------------
115 
116 } // namespace oops
117 
118 #endif // OOPS_BASE_OBSERRORBASE_H_
oops::ObsErrorFactory
ObsErrorFactory Factory.
Definition: ObsErrorBase.h:59
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
oops::ObsErrorBase::ObsErrorBase
ObsErrorBase()=default
oops::ObsErrorFactory::~ObsErrorFactory
virtual ~ObsErrorFactory()=default
oops::ObsErrorBase::ObsVector_
ObsVector< OBS > ObsVector_
Definition: ObsErrorBase.h:33
oops::ObsSpace
Definition: oops/interface/ObsSpace.h:42
oops::ObsErrorBase
Base class for observation error covariance matrices.
Definition: ObsErrorBase.h:32
oops::ObsErrorMaker::ObsErrorMaker
ObsErrorMaker(const std::string &name)
Definition: ObsErrorBase.h:84
oops::ObsErrorBase::~ObsErrorBase
virtual ~ObsErrorBase()=default
oops::ObsErrorBase::getRMSE
virtual double getRMSE() const =0
Get mean error for Jo table.
oops::ObsErrorBase::ObsSpace_
ObsSpace< OBS > ObsSpace_
Definition: ObsErrorBase.h:34
oops::ObsVector
Definition: oops/interface/ObsSpace.h:36
oops::ObsErrorBase::inverseMultiply
virtual void inverseMultiply(ObsVector_ &dy) const =0
Multiply a Departure dy by .
oops::ObsErrorFactory::create
static std::unique_ptr< ObsErrorBase< OBS > > create(const eckit::Configuration &, const ObsSpace_ &)
Definition: ObsErrorBase.h:101
oops::ObsErrorFactory::getMakers
static std::map< std::string, ObsErrorFactory< OBS > * > & getMakers()
Definition: ObsErrorBase.h:69
oops::ObsErrorFactory::ObsErrorFactory
ObsErrorFactory(const std::string &)
Definition: ObsErrorBase.h:90
oops::ObsErrorFactory::ObsSpace_
ObsSpace< OBS > ObsSpace_
Definition: ObsErrorBase.h:60
ObsSpace.h
oops::ObsErrorMaker::ObsSpace_
ObsSpace< OBS > ObsSpace_
Definition: ObsErrorBase.h:79
oops::ObsErrorMaker
Definition: ObsErrorBase.h:78
oops::ObsErrorBase::randomize
virtual void randomize(ObsVector_ &dy) const =0
Generate random perturbation in dy.
oops::ObsErrorBase::multiply
virtual void multiply(ObsVector_ &dy) const =0
Multiply a Departure dy by $.
oops::ObsErrorMaker::make
virtual ObsErrorBase< OBS > * make(const eckit::Configuration &conf, const ObsSpace_ &obs)
Definition: ObsErrorBase.h:80
oops::ObsErrorFactory::make
virtual ObsErrorBase< OBS > * make(const eckit::Configuration &, const ObsSpace_ &)=0
oops::ObsErrorBase::inverseVariance
virtual const ObsVector_ & inverseVariance() const =0
Return inverseVariance.