OOPS
Observers.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 OOPS_BASE_OBSERVERS_H_
9 #define OOPS_BASE_OBSERVERS_H_
10 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "eckit/config/LocalConfiguration.h"
16 
17 #include "oops/base/Geometry.h"
20 #include "oops/base/ObsErrors.h"
21 #include "oops/base/Observations.h"
22 #include "oops/base/Observer.h"
23 #include "oops/base/ObsSpaces.h"
24 #include "oops/base/ObsVector.h"
26 #include "oops/base/State.h"
27 #include "oops/util/Logger.h"
28 
29 namespace oops {
30 
31 // -----------------------------------------------------------------------------
32 
33 /// \brief Computes observation operator (from GeoVaLs), applies bias correction
34 /// and runs QC filters
35 template <typename MODEL, typename OBS>
36 class Observers {
48  template <typename DATA> using ObsData_ = ObsDataVector<OBS, DATA>;
49  template <typename DATA> using ObsDataVec_ = std::vector<std::shared_ptr<ObsData_<DATA>>>;
50 
51  public:
52 /// \brief Initializes ObsOperators, Locations, and QC data
53  Observers(const ObsSpaces_ &, const std::vector<ObserverParameters_> &);
54  Observers(const ObsSpaces_ &, const eckit::Configuration &);
55 
56 /// \brief Initializes variables, obs bias, obs filters (could be different for
57 /// different iterations
58  void initialize(const Geometry_ &, const ObsAuxCtrls_ &, ObsErrors_ &,
59  PostProc_ &, const eckit::Configuration & = eckit::LocalConfiguration());
60 
61 /// \brief Computes H(x) from the filled in GeoVaLs
62  void finalize(Observations_ &);
63 
64  private:
65  static std::vector<ObserverParameters_> convertToParameters(const eckit::Configuration &config);
66 
67  private:
68  std::vector<std::unique_ptr<Observer_>> observers_;
69 };
70 
71 // -----------------------------------------------------------------------------
72 
73 template <typename MODEL, typename OBS>
75  const std::vector<ObserverParameters_> &params)
76  : observers_()
77 {
78  Log::trace() << "Observers<MODEL, OBS>::Observers start" << std::endl;
79 
80  ASSERT(obspaces.size() == params.size());
81  for (size_t jj = 0; jj < obspaces.size(); ++jj) {
82  observers_.emplace_back(new Observer_(obspaces[jj], params[jj]));
83  }
84 
85  Log::trace() << "Observers<MODEL, OBS>::Observers done" << std::endl;
86 }
87 
88 // -----------------------------------------------------------------------------
89 
90 template <typename MODEL, typename OBS>
91 Observers<MODEL, OBS>::Observers(const ObsSpaces_ & obspaces, const eckit::Configuration & config)
92  : Observers(obspaces, convertToParameters(config))
93 {}
94 
95 // -----------------------------------------------------------------------------
96 
97 template <typename MODEL, typename OBS>
98 void Observers<MODEL, OBS>::initialize(const Geometry_ & geom, const ObsAuxCtrls_ & obsaux,
99  ObsErrors_ & Rmat, PostProc_ & pp,
100  const eckit::Configuration & conf) {
101  Log::trace() << "Observers<MODEL, OBS>::initialize start" << std::endl;
102 
103  std::shared_ptr<GetValuePosts_> getvals(new GetValuePosts_());
104  for (size_t jj = 0; jj < observers_.size(); ++jj) {
105  getvals->append(observers_[jj]->initialize(geom, obsaux[jj], Rmat[jj], conf));
106  }
107  pp.enrollProcessor(getvals);
108 
109  Log::trace() << "Observers<MODEL, OBS>::initialize done" << std::endl;
110 }
111 
112 // -----------------------------------------------------------------------------
113 
114 template <typename MODEL, typename OBS>
116  oops::Log::trace() << "Observers<MODEL, OBS>::finalize start" << std::endl;
117 
118  for (size_t jj = 0; jj < observers_.size(); ++jj) {
119  observers_[jj]->finalize(yobs[jj]);
120  }
121 
122  oops::Log::trace() << "Observers<MODEL, OBS>::finalize done" << std::endl;
123 }
124 
125 // -----------------------------------------------------------------------------
126 
127 template <typename MODEL, typename OBS>
128 std::vector<ObserverParameters<OBS>> Observers<MODEL, OBS>::convertToParameters(
129  const eckit::Configuration &config) {
130  oops::Log::trace() << "Observers<MODEL, OBS>::convertToParameters start" << std::endl;
131 
132  std::vector<eckit::LocalConfiguration> subconfigs = config.getSubConfigurations();
133  std::vector<ObserverParameters<OBS>> parameters(subconfigs.size());
134  for (size_t i = 0; i < subconfigs.size(); ++i) {
135  const eckit::LocalConfiguration &subconfig = subconfigs[i];
136 
137  // 'subconfig' will, in general, contain options irrelevant to the observer (e.g. 'obs space').
138  // So we need to extract the relevant parts into a new Configuration object, 'observerConfig',
139  // before validation and deserialization. Otherwise validation might fail.
140 
141  eckit::LocalConfiguration observerConfig;
142 
143  // Required keys
144  observerConfig.set("obs operator", eckit::LocalConfiguration(subconfig, "obs operator"));
145 
146  // Optional keys
147  std::vector<eckit::LocalConfiguration> filterConfigs;
148  if (subconfig.get("obs filters", filterConfigs))
149  observerConfig.set("obs filters", filterConfigs);
150  eckit::LocalConfiguration getValuesConfig;
151  if (subconfig.get("get values", getValuesConfig))
152  observerConfig.set("get values", getValuesConfig);
153  eckit::LocalConfiguration linearGetValuesConfig;
154  if (subconfig.get("linear get values", linearGetValuesConfig))
155  observerConfig.set("linear get values", linearGetValuesConfig);
156 
157  parameters[i].validateAndDeserialize(observerConfig);
158  }
159 
160  oops::Log::trace() << "Observers<MODEL, OBS>::convertToParameters start" << std::endl;
161 
162  return parameters;
163 }
164 
165 } // namespace oops
166 
167 #endif // OOPS_BASE_OBSERVERS_H_
Geometry class used in oops; subclass of interface class interface::Geometry.
Fills GeoVaLs with requested variables at requested locations during model run.
Definition: GetValuePosts.h:29
Holds a vector of ObsAuxControl.
ObsDataVector is a vector templated on data type, in the observation space.
Container for ObsErrors for all observation types that are used in DA.
Definition: ObsErrors.h:34
std::size_t size() const
Access.
Definition: ObsSpaces.h:61
ObsVector class used in oops; subclass of interface class interface::ObsVector.
Observations Class.
Definition: Observations.h:35
Computes observation operator, applying bias correction and QC filters.
Definition: Observer.h:56
Computes observation operator (from GeoVaLs), applies bias correction and runs QC filters.
Definition: Observers.h:36
Geometry< MODEL > Geometry_
Definition: Observers.h:37
Observer< MODEL, OBS > Observer_
Definition: Observers.h:42
ObsErrors< OBS > ObsErrors_
Definition: Observers.h:40
ObserverParameters< OBS > ObserverParameters_
Definition: Observers.h:43
std::vector< std::shared_ptr< ObsData_< DATA > >> ObsDataVec_
Definition: Observers.h:49
ObsVector< OBS > ObsVector_
Definition: Observers.h:45
State< MODEL > State_
Definition: Observers.h:46
Observers(const ObsSpaces_ &, const std::vector< ObserverParameters_ > &)
Initializes ObsOperators, Locations, and QC data.
Definition: Observers.h:74
PostProcessor< State_ > PostProc_
Definition: Observers.h:47
ObsAuxControls< OBS > ObsAuxCtrls_
Definition: Observers.h:39
void initialize(const Geometry_ &, const ObsAuxCtrls_ &, ObsErrors_ &, PostProc_ &, const eckit::Configuration &=eckit::LocalConfiguration())
Initializes variables, obs bias, obs filters (could be different for different iterations.
Definition: Observers.h:98
ObsSpaces< OBS > ObsSpaces_
Definition: Observers.h:44
GetValuePosts< MODEL, OBS > GetValuePosts_
Definition: Observers.h:38
void finalize(Observations_ &)
Computes H(x) from the filled in GeoVaLs.
Definition: Observers.h:115
Observations< OBS > Observations_
Definition: Observers.h:41
static std::vector< ObserverParameters_ > convertToParameters(const eckit::Configuration &config)
Definition: Observers.h:128
std::vector< std::unique_ptr< Observer_ > > observers_
Definition: Observers.h:68
Control model post processing.
Definition: PostProcessor.h:30
void enrollProcessor(PostBase_ *pp)
Definition: PostProcessor.h:38
State class used in oops; subclass of interface class interface::State.
The namespace for the main oops code.