IODA Bundle
ObsAuxControls.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2017-2019 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_OBSAUXCONTROLS_H_
9 #define OOPS_BASE_OBSAUXCONTROLS_H_
10 
11 #include <iostream>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include "oops/base/ObsSpaces.h"
18 #include "oops/util/ConfigFunctions.h"
19 #include "oops/util/Logger.h"
20 #include "oops/util/parameters/Parameters.h"
21 #include "oops/util/Printable.h"
22 
23 namespace oops {
24 
25 // -----------------------------------------------------------------------------
26 
27 template <typename OBS>
28 class ObsAuxControls : public util::Printable {
32 
33  public:
34  static const std::string classname() {return "oops::ObsAuxControls";}
35 
36  ObsAuxControls(const ObsSpaces_ &, const eckit::Configuration &);
37  ObsAuxControls(const ObsSpaces_ &, const std::vector<Parameters_> &);
38  explicit ObsAuxControls(const ObsAuxControls &, const bool copy = true);
40 
41 /// Access
42  std::size_t size() const {return auxs_.size();}
43  const ObsAuxControl_ & operator[](const std::size_t ii) const {return *auxs_.at(ii);}
44  ObsAuxControl_ & operator[](const std::size_t ii) {return *auxs_.at(ii);}
45 
46 /// I/O and diagnostics
47  void read(const eckit::Configuration &);
48  void write(const eckit::Configuration &) const;
49  double norm() const;
50 
52 
53  private:
54  void print(std::ostream &) const;
55  std::vector<std::unique_ptr<ObsAuxControl_> > auxs_;
56 };
57 
58 // =============================================================================
59 
60 template<typename OBS>
62  const std::vector<Parameters_> & params)
63  : auxs_(0)
64 {
65  ASSERT(odb.size() == params.size());
66  for (std::size_t jobs = 0; jobs < params.size(); ++jobs) {
67  auxs_.push_back(
68  std::unique_ptr<ObsAuxControl_>(new ObsAuxControl_(odb[jobs], params[jobs])));
69  }
70 }
71 
72 // -----------------------------------------------------------------------------
73 
74 template<typename OBS>
75 ObsAuxControls<OBS>::ObsAuxControls(const ObsSpaces_ & odb, const eckit::Configuration & conf)
76  : ObsAuxControls(odb,
77  // Split conf into subconfigurations, extract the "obs bias" section from each
78  // of them, then validate and deserialize that section into a Parameters_ object
79  validateAndDeserialize<Parameters_>(util::vectoriseAndFilter(conf, "obs bias")))
80 {}
81 
82 // -----------------------------------------------------------------------------
83 
84 template<typename OBS>
86  : auxs_(other.size())
87 {
88  Log::trace() << "ObsAuxControls<OBS>::ObsAuxControls copy starting" << std::endl;
89  for (std::size_t jobs = 0; jobs < other.size(); ++jobs) {
90  auxs_[jobs].reset(new ObsAuxControl_(other[jobs], copy));
91  }
92  Log::trace() << "ObsAuxControls<OBS>::ObsAuxControls copy done" << std::endl;
93 }
94 
95 // -----------------------------------------------------------------------------
96 
97 template<typename OBS>
99  Log::trace() << "ObsAuxControls<OBS>::~ObsAuxControls starting" << std::endl;
100  for (std::size_t jobs = 0; jobs < auxs_.size(); ++jobs) auxs_[jobs].reset();
101  Log::trace() << "ObsAuxControls<OBS>::~ObsAuxControls done" << std::endl;
102 }
103 
104 // -----------------------------------------------------------------------------
105 
106 template<typename OBS>
107 void ObsAuxControls<OBS>::read(const eckit::Configuration & conf) {
108  Log::trace() << "ObsAuxControls<OBS>::read starting" << std::endl;
109  std::vector<eckit::LocalConfiguration> obsconfs = conf.getSubConfigurations("observations");
110  ASSERT(obsconfs.size() == auxs_.size());
111  for (std::size_t jobs = 0; jobs < auxs_.size(); ++jobs) {
112  eckit::LocalConfiguration obsauxconf = obsconfs[jobs].getSubConfiguration("obs bias");
114  params.validateAndDeserialize(obsauxconf);
115  auxs_[jobs]->read(params);
116  }
117  Log::trace() << "ObsAuxControls<OBS>::read done" << std::endl;
118 }
119 
120 // -----------------------------------------------------------------------------
121 
122 template<typename OBS>
123 void ObsAuxControls<OBS>::write(const eckit::Configuration & conf) const {
124  Log::trace() << "ObsAuxControls<OBS>::write starting" << std::endl;
125  std::vector<eckit::LocalConfiguration> obsconfs = conf.getSubConfigurations("observations");
126  ASSERT(obsconfs.size() == auxs_.size());
127  for (std::size_t jobs = 0; jobs < auxs_.size(); ++jobs) {
128  eckit::LocalConfiguration obsauxconf = obsconfs[jobs].getSubConfiguration("obs bias");
130  params.validateAndDeserialize(obsauxconf);
131  auxs_[jobs]->write(params);
132  }
133  Log::trace() << "ObsAuxControls<OBS>::write done" << std::endl;
134 }
135 
136 // -----------------------------------------------------------------------------
137 
138 template<typename OBS>
140  Log::trace() << "ObsAuxControls<OBS>::norm starting" << std::endl;
141  double zz = static_cast<double>(0.0);
142  std::size_t ii = 0;
143  double norm;
144  for (std::size_t jobs = 0; jobs < auxs_.size(); ++jobs) {
145  norm = auxs_[jobs]->norm();
146  if (norm > 0.0) {
147  zz += norm*norm;
148  ++ii;
149  }
150  }
151  Log::trace() << "ObsAuxControls<OBS>::norm done" << std::endl;
152  return std::sqrt(zz/ii);
153 }
154 
155 // -----------------------------------------------------------------------------
156 
157 template<typename OBS>
158 void ObsAuxControls<OBS>::print(std::ostream & os) const {
159  for (const auto & aux : auxs_) {
160  os << *aux << std::endl;
161  }
162 }
163 
164 // -----------------------------------------------------------------------------
165 
166 } // namespace oops
167 
168 #endif // OOPS_BASE_OBSAUXCONTROLS_H_
ObsAuxControl_::Parameters_ Parameters_
void read(const eckit::Configuration &)
I/O and diagnostics.
std::vector< std::unique_ptr< ObsAuxControl_ > > auxs_
const ObsAuxControl_ & operator[](const std::size_t ii) const
ObsSpaces< OBS > ObsSpaces_
void write(const eckit::Configuration &) const
void print(std::ostream &) const
ObsAuxControl_::Parameters_ Parameters_
ObsAuxControls(const ObsSpaces_ &, const eckit::Configuration &)
ObsAuxControl< OBS > ObsAuxControl_
double norm() const
std::size_t size() const
Access.
ObsAuxControls & operator=(const ObsAuxControls &)
static const std::string classname()
ObsAuxControl_ & operator[](const std::size_t ii)
std::size_t size() const
Access.
Definition: ObsSpaces.h:53
IODA_DL void copy(const ObjectSelection &from, ObjectSelection &to, const ScaleMapping &scale_map)
Generic data copying function.
Definition: Copying.cpp:63
The namespace for the main oops code.