OOPS
Observations.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_OBSERVATIONS_H_
12 #define OOPS_BASE_OBSERVATIONS_H_
13 
14 #include <cstddef>
15 #include <ostream>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include "oops/base/Departures.h"
21 #include "oops/base/ObsErrors.h"
22 #include "oops/base/ObsSpaces.h"
24 #include "oops/util/Logger.h"
25 #include "oops/util/Printable.h"
26 
27 namespace oops {
28 
29 /// Observations Class.
30 /*!
31  * Contains observed values or their model equivalents
32  */
33 
34 // -----------------------------------------------------------------------------
35 template <typename OBS> class Observations : public util::Printable {
40 
41  public:
42 /// \brief create Observations for all obs (read from ObsSpace if name is specified)
43  explicit Observations(const ObsSpaces_ &, const std::string & name = "");
44 /// \brief create local Observations
45  Observations(const ObsSpaces_ &, const Observations &);
46 
47 /// destructor and copy/move constructor/assignments
48  ~Observations() = default;
49  Observations(const Observations &);
53 
54 /// Access
55  std::size_t size() const {return obs_.size();}
56  ObsVector_ & operator[](const std::size_t ii) {return obs_.at(ii);}
57  const ObsVector_ & operator[](const std::size_t ii) const {return obs_.at(ii);}
58 
59 /// Interactions with Departures
60  Departures_ operator-(const Observations & other) const;
62 
63 /// Save/read observations values
64  void save(const std::string &) const;
65  void read(const std::string &);
66 
67 /// Accumulator
68  void zero();
69  void accumul(const Observations &);
70  Observations & operator*=(const double);
71 
72 /// Perturbations
73  void perturb(const ObsErrors_ &);
74 
75  private:
76  void print(std::ostream &) const;
77  size_t nobs() const;
78 
79 /// Data
80  const ObsSpaces_ & obsdb_;
81  std::vector<ObsVector_> obs_;
82 };
83 
84 // =============================================================================
85 
86 template <typename OBS>
88  const std::string & name): obsdb_(obsdb), obs_()
89 {
90  obs_.reserve(obsdb.size());
91  for (std::size_t jj = 0; jj < obsdb.size(); ++jj) {
92  obs_.emplace_back(obsdb[jj], name, true);
93  }
94  Log::trace() << "Observations created" << std::endl;
95 }
96 // -----------------------------------------------------------------------------
97 template <typename OBS>
99  const Observations & other): obsdb_(obsdb), obs_() {
100  obs_.reserve(obsdb.size());
101  for (std::size_t jj = 0; jj < other.size(); ++jj) {
102  obs_.emplace_back(obsdb[jj], other[jj]);
103  }
104  Log::trace() << "Local observations created" << std::endl;
105 }
106 // -----------------------------------------------------------------------------
107 template <typename OBS>
109 : obsdb_(other.obsdb_), obs_(other.obs_) {
110 }
111 // -----------------------------------------------------------------------------
112 
113 template <typename OBS>
115 : obsdb_(other.obsdb_), obs_(std::move(other.obs_)) {
116 }
117 // -----------------------------------------------------------------------------
118 template <typename OBS>
120 // only allow assignment for Observations created from the same ObsSpaces
121  ASSERT(&obsdb_ == &other.obsdb_);
122  obs_ = other.obs_;
123  return *this;
124 }
125 // -----------------------------------------------------------------------------
126 template <typename OBS>
128 // only allow assignment for Observations created from the same ObsSpaces
129  ASSERT(&obsdb_ == &other.obsdb_);
130  obs_ = std::move(other.obs_);
131  return *this;
132 }
133 // -----------------------------------------------------------------------------
134 template <typename OBS>
136  Departures_ diff(obsdb_);
137  for (std::size_t jj = 0; jj < obs_.size(); ++jj) {
138  diff[jj] = obs_[jj];
139  diff[jj] -= other[jj];
140  }
141  return diff;
142 }
143 // -----------------------------------------------------------------------------
144 template <typename OBS>
146  for (std::size_t jj = 0; jj < obs_.size(); ++jj) {
147  obs_[jj] += dy[jj];
148  }
149  return *this;
150 }
151 // -----------------------------------------------------------------------------
152 template <typename OBS>
153 void Observations<OBS>::save(const std::string & name) const {
154  for (std::size_t jj = 0; jj < obs_.size(); ++jj) {
155  obs_[jj].save(name);
156  }
157 }
158 // -----------------------------------------------------------------------------
159 template <typename OBS>
160 void Observations<OBS>::read(const std::string & name) {
161  for (std::size_t jj = 0; jj < obs_.size(); ++jj) {
162  obs_[jj].read(name);
163  }
164 }
165 // -----------------------------------------------------------------------------
166 template <typename OBS>
168  for (std::size_t jj = 0; jj < obs_.size(); ++jj) {
169  obs_[jj].zero();
170  }
171 }
172 // -----------------------------------------------------------------------------
173 template <typename OBS>
175  for (std::size_t jj = 0; jj < obs_.size(); ++jj) {
176  obs_[jj] += y[jj];
177  }
178 }
179 // -----------------------------------------------------------------------------
180 template <typename OBS>
182  for (std::size_t jj = 0; jj < obs_.size(); ++jj) {
183  obs_[jj] *= factor;
184  }
185  return *this;
186 }
187 // -----------------------------------------------------------------------------
188 template<typename OBS>
189 size_t Observations<OBS>::nobs() const {
190  size_t nobs = 0;
191  for (size_t jj = 0; jj < obs_.size(); ++jj) {
192  nobs += obs_[jj].nobs();
193  }
194  return nobs;
195 }
196 // -----------------------------------------------------------------------------
197 template <typename OBS>
199  Departures_ ypert(obsdb_);
200  Rmat.randomize(ypert);
201  *this += ypert;
202  Log::trace() << "Observations perturbed" << std::endl;
203 }
204 // -----------------------------------------------------------------------------
205 template <typename OBS>
206 void Observations<OBS>::print(std::ostream & os) const {
207  for (std::size_t jj = 0; jj < obs_.size(); ++jj) os << obs_[jj] << std::endl;
208 }
209 // -----------------------------------------------------------------------------
210 } // namespace oops
211 
212 #endif // OOPS_BASE_OBSERVATIONS_H_
oops::Observations::ObsSpaces_
ObsSpaces< OBS > ObsSpaces_
Definition: Observations.h:38
oops::Observations::operator=
Observations & operator=(const Observations &)
Definition: Observations.h:119
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
oops::Observations::perturb
void perturb(const ObsErrors_ &)
Perturbations.
Definition: Observations.h:198
oops::Observations::obs_
std::vector< ObsVector_ > obs_
Definition: Observations.h:81
oops::Observations::obsdb_
const ObsSpaces_ & obsdb_
Data.
Definition: Observations.h:80
ObsSpaces.h
oops::Observations::ObsVector_
ObsVector< OBS > ObsVector_
Definition: Observations.h:39
oops::Observations::Departures_
Departures< OBS > Departures_
Definition: Observations.h:36
oops::ObsErrors
\biref Container for ObsErrors for all observation types that are used in DA
Definition: ObsErrors.h:33
oops::Observations::zero
void zero()
Accumulator.
Definition: Observations.h:167
oops::Observations::ObsErrors_
ObsErrors< OBS > ObsErrors_
Definition: Observations.h:37
oops::ObsVector
Definition: oops/interface/ObsSpace.h:36
Departures.h
oops::Observations::operator[]
const ObsVector_ & operator[](const std::size_t ii) const
Definition: Observations.h:57
oops::Observations::operator-
Departures_ operator-(const Observations &other) const
Interactions with Departures.
Definition: Observations.h:135
oops::ObsSpaces::size
std::size_t size() const
Access.
Definition: ObsSpaces.h:57
oops::Observations::nobs
size_t nobs() const
Definition: Observations.h:189
ObsErrors.h
oops::Observations::operator+=
Observations & operator+=(const Departures_ &)
Definition: Observations.h:145
oops::Departures
Difference between two observation vectors.
Definition: oops/base/Departures.h:44
oops::Observations::accumul
void accumul(const Observations &)
Definition: Observations.h:174
oops::Observations::print
void print(std::ostream &) const
Definition: Observations.h:206
oops::Observations::read
void read(const std::string &)
Definition: Observations.h:160
oops::Observations::operator*=
Observations & operator*=(const double)
Definition: Observations.h:181
oops::Observations::~Observations
~Observations()=default
destructor and copy/move constructor/assignments
oops::Observations::Observations
Observations(const ObsSpaces_ &, const std::string &name="")
create Observations for all obs (read from ObsSpace if name is specified)
Definition: Observations.h:87
oops::Observations
Observations Class.
Definition: oops/base/Departures.h:30
oops::Observations::operator[]
ObsVector_ & operator[](const std::size_t ii)
Definition: Observations.h:56
oops::Observations::size
std::size_t size() const
Access.
Definition: Observations.h:55
ObsVector.h
oops::ObsSpaces
Definition: ObsSpaces.h:41
oops::ObsErrors::randomize
void randomize(Departures_ &) const
Generate random perturbation.
Definition: ObsErrors.h:99
oops::Observations::save
void save(const std::string &) const
Save/read observations values.
Definition: Observations.h:153