IODA Bundle
ObsFilter.h
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 
8 #ifndef OOPS_INTERFACE_OBSFILTER_H_
9 #define OOPS_INTERFACE_OBSFILTER_H_
10 
11 #include <memory>
12 #include <string>
13 
14 #include "eckit/config/LocalConfiguration.h"
16 #include "oops/base/Variables.h"
17 #include "oops/interface/GeoVaLs.h"
22 #include "oops/util/dot_product.h"
23 #include "oops/util/Logger.h"
24 #include "oops/util/parameters/HasParameters_.h"
25 #include "oops/util/parameters/ParametersOrConfiguration.h"
26 
27 namespace oops {
28 
29 // -----------------------------------------------------------------------------
30 
31 /// Note: implementations of this interface can opt to extract their settings either from
32 /// a Configuration object or from a subclass of ObsFilterParametersBase.
33 ///
34 /// In the former case, they should provide a constructor with the following signature:
35 ///
36 /// ObsFilter(const ObsSpace_ &, const eckit::Configuration &,
37 /// ObsDataPtr_<int>, ObsDataPtr_<float>);
38 ///
39 /// In the latter case, the implementer should first define a subclass of ObsFilterParametersBase
40 /// holding the settings of the filter in question. The implementation of the ObsFilter interface
41 /// should then typedef `Parameters_` to the name of that subclass and provide a constructor with
42 /// the following signature:
43 ///
44 /// ObsFilter(const ObsSpace_ &, const Parameters_ &,
45 /// ObsDataPtr_<int>, ObsDataPtr_<float>);
46 template <typename OBS, typename FILTER>
47 class ObsFilter : public ObsFilterBase<OBS> {
52  template <typename DATA> using ObsDataPtr_ = std::shared_ptr<ObsDataVector<OBS, DATA> >;
53  template <typename DATA> using ObsDataVec_ = typename OBS::template ObsDataVector<DATA>;
54 
55  public:
56  /// Defined as FILTER::Parameters_ if FILTER defines a Parameters_ type; otherwise as
57  /// GenericObsFilterParameters
58  typedef TParameters_IfAvailableElseFallbackType_t<FILTER, GenericObsFilterParameters> Parameters_;
59 
60  static const std::string classname() {return "oops::ObsFilter";}
61 
62  ObsFilter(const ObsSpace_ &, const Parameters_ &,
63  ObsDataPtr_<int>, ObsDataPtr_<float>);
64  ObsFilter(const ObsSpace_ &, const eckit::Configuration &,
65  ObsDataPtr_<int>, ObsDataPtr_<float>);
66  ~ObsFilter();
67 
68  void preProcess() const override;
69  void priorFilter(const GeoVaLs_ &) const override;
70  void postFilter(const ObsVector_ &, const ObsDiags_ &) const override;
71 
72  Variables requiredVars() const override;
73  Variables requiredHdiagnostics() const override;
74 
75  private:
76  void print(std::ostream &) const override;
77 
78  const ObsSpace_ & obsdb_;
79  const std::unique_ptr<Parameters_> parameters_;
80  std::unique_ptr<FILTER> ofilt_;
81 };
82 
83 // -----------------------------------------------------------------------------
84 
85 template <typename OBS, typename FILTER>
87  const Parameters_ & parameters,
89  : obsdb_(os), parameters_(parameters.clone()), ofilt_()
90 {
91  Log::trace() << "ObsFilter<OBS, FILTER>::ObsFilter Configuration starting" << std::endl;
92  util::Timer timer(classname(), "ObsFilter");
93 
94  std::shared_ptr<ObsDataVec_<int> > qc;
95  std::shared_ptr<ObsDataVec_<float> > oberr;
96  if (flags) qc = flags->obsdatavectorptr();
97  if (obserr) oberr = obserr->obsdatavectorptr();
98 
99  ofilt_.reset(new FILTER(obsdb_.obsspace(),
100  parametersOrConfiguration<HasParameters_<FILTER>::value>(parameters),
101  qc, oberr));
102  Log::trace() << "ObsFilter<OBS, FILTER>::ObsFilter Configuration done" << std::endl;
103 }
104 
105 // -----------------------------------------------------------------------------
106 
107 template <typename OBS, typename FILTER>
109  const eckit::Configuration & conf,
110  ObsDataPtr_<int> flags, ObsDataPtr_<float> obserr)
111  : ObsFilter(os, validateAndDeserialize<Parameters_>(conf), flags, obserr)
112 {}
113 
114 // -----------------------------------------------------------------------------
115 
116 template <typename OBS, typename FILTER>
118  Log::trace() << "ObsFilter<OBS, FILTER>::~ObsFilter starting" << std::endl;
119  util::Timer timer(classname(), "~ObsFilter");
120  ofilt_.reset();
121  Log::trace() << "ObsFilter<OBS, FILTER>::~ObsFilter done" << std::endl;
122 }
123 
124 // -----------------------------------------------------------------------------
125 
126 template <typename OBS, typename FILTER>
128  Log::trace() << "ObsFilter<OBS, FILTER>:: preProcess starting" << std::endl;
129  util::Timer timer(classname(), "preProcess");
130  ofilt_->preProcess();
131  Log::trace() << "ObsFilter<OBS, FILTER>:: preProcess done" << std::endl;
132 }
133 
134 // -----------------------------------------------------------------------------
135 
136 template <typename OBS, typename FILTER>
138  Log::trace() << "ObsFilter<OBS, FILTER>:: priorFilter starting" << std::endl;
139  util::Timer timer(classname(), "priorFilter");
140  ofilt_->priorFilter(gv.geovals());
141  Log::trace() << "ObsFilter<OBS, FILTER>:: priorFilter done" << std::endl;
142 }
143 
144 // -----------------------------------------------------------------------------
145 
146 template <typename OBS, typename FILTER>
147 void ObsFilter<OBS, FILTER>::postFilter(const ObsVector_ & ov, const ObsDiags_ & dv) const {
148  Log::trace() << "ObsFilter<OBS, FILTER>::postFilter starting" << std::endl;
149  util::Timer timer(classname(), "postFilter");
150  ofilt_->postFilter(ov.obsvector(), dv.obsdiagnostics());
151  Log::trace() << "ObsFilter<OBS, FILTER>::postFilter done" << std::endl;
152 }
153 
154 // -----------------------------------------------------------------------------
155 
156 template <typename OBS, typename FILTER>
158  Log::trace() << "ObsFilter::requiredVars" << std::endl;
159  return ofilt_->requiredVars();
160 }
161 
162 // -----------------------------------------------------------------------------
163 
164 template <typename OBS, typename FILTER>
166  Log::trace() << "ObsFilter::requiredHdiagnostics" << std::endl;
167  return ofilt_->requiredHdiagnostics();
168 }
169 
170 // -----------------------------------------------------------------------------
171 
172 template <typename OBS, typename FILTER>
173 void ObsFilter<OBS, FILTER>::print(std::ostream & os) const {
174  os << "ObsFilter " << *parameters_;
175 }
176 
177 // -----------------------------------------------------------------------------
178 
179 } // namespace oops
180 
181 #endif // OOPS_INTERFACE_OBSFILTER_H_
const GeoVaLs_ & geovals() const
Interfacing.
ObsDiags_ & obsdiagnostics()
Interfacing.
Base class for QC filters applied to observations.
Definition: ObsFilterBase.h:42
void preProcess() const override
Definition: ObsFilter.h:127
std::shared_ptr< ObsDataVector< OBS, DATA > > ObsDataPtr_
Definition: ObsFilter.h:52
ObsSpace< OBS > ObsSpace_
Definition: ObsFilter.h:50
typename OBS::template ObsDataVector< DATA > ObsDataVec_
Definition: ObsFilter.h:53
GeoVaLs< OBS > GeoVaLs_
Definition: ObsFilter.h:48
std::unique_ptr< FILTER > ofilt_
Definition: ObsFilter.h:80
ObsVector< OBS > ObsVector_
Definition: ObsFilter.h:51
void postFilter(const ObsVector_ &, const ObsDiags_ &) const override
Definition: ObsFilter.h:147
TParameters_IfAvailableElseFallbackType_t< FILTER, GenericObsFilterParameters > Parameters_
Definition: ObsFilter.h:58
ObsDiagnostics< OBS > ObsDiags_
Definition: ObsFilter.h:49
const ObsSpace_ & obsdb_
Definition: ObsFilter.h:78
void print(std::ostream &) const override
Definition: ObsFilter.h:173
void priorFilter(const GeoVaLs_ &) const override
Definition: ObsFilter.h:137
static const std::string classname()
Definition: ObsFilter.h:60
ObsFilter(const ObsSpace_ &, const Parameters_ &, ObsDataPtr_< int >, ObsDataPtr_< float >)
Definition: ObsFilter.h:86
Variables requiredHdiagnostics() const override
Definition: ObsFilter.h:165
Variables requiredVars() const override
Definition: ObsFilter.h:157
const std::unique_ptr< Parameters_ > parameters_
Definition: ObsFilter.h:79
ObsSpace_ & obsspace() const
Interfacing.
ObsVector_ & obsvector()
Interfacing.
The namespace for the main oops code.