OOPS
ObsEnsemble.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2018-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_OBSENSEMBLE_H_
9 #define OOPS_BASE_OBSENSEMBLE_H_
10 
11 #include <vector>
12 
13 #include "oops/base/Observations.h"
14 #include "oops/base/ObsSpaces.h"
15 #include "oops/util/Logger.h"
16 
17 namespace oops {
18 
19 // -----------------------------------------------------------------------------
20 /// \brief Ensemble of observations (can hold ensemble of H(x))
21 template<typename OBS> class ObsEnsemble {
24 
25  public:
26  /// Create ensemble of empty Observations size \p nens
27  ObsEnsemble(const ObsSpaces_ &, const size_t & nens);
28 
29  /// Accessors and size
30  size_t size() const {return ensemble_.size();}
31  Observations_ & operator[](const size_t ii) {return ensemble_[ii];}
32  const Observations_ & operator[](const size_t ii) const { return ensemble_[ii];}
33 
34  /// Compute ensemble mean
35  Observations_ mean() const;
36 
37  private:
38  const ObsSpaces_ & obsdb_; // ObsSpaces used for creating ensemble members
39  std::vector<Observations_> ensemble_; // ensemble members
40 };
41 
42 // ====================================================================================
43 
44 template<typename OBS>
45 ObsEnsemble<OBS>::ObsEnsemble(const ObsSpaces_ & obsdb, const size_t & nens)
46  : obsdb_(obsdb), ensemble_()
47 {
48  ensemble_.reserve(nens);
49  for (size_t iens = 0; iens < nens ; ++iens) {
50  ensemble_.emplace_back(obsdb_);
51  }
52  Log::trace() << "ObsEnsemble created" << std::endl;
53 }
54 
55 // -----------------------------------------------------------------------------
56 
57 template<typename OBS>
59  Observations_ mean_obs(obsdb_);
60  mean_obs.zero();
61  for (const auto & yy : ensemble_) {
62  mean_obs.accumul(yy);
63  }
64  mean_obs *= 1.0/static_cast<float>(ensemble_.size());
65  Log::trace() << "ObsEnsemble::mean done" << std::endl;
66  return mean_obs;
67 }
68 
69 } // namespace oops
70 
71 #endif // OOPS_BASE_OBSENSEMBLE_H_
Ensemble of observations (can hold ensemble of H(x))
Definition: ObsEnsemble.h:21
const Observations_ & operator[](const size_t ii) const
Definition: ObsEnsemble.h:32
std::vector< Observations_ > ensemble_
Definition: ObsEnsemble.h:39
Observations_ & operator[](const size_t ii)
Definition: ObsEnsemble.h:31
Observations< OBS > Observations_
Definition: ObsEnsemble.h:22
Observations_ mean() const
Compute ensemble mean.
Definition: ObsEnsemble.h:58
size_t size() const
Accessors and size.
Definition: ObsEnsemble.h:30
ObsSpaces< OBS > ObsSpaces_
Definition: ObsEnsemble.h:23
const ObsSpaces_ & obsdb_
Definition: ObsEnsemble.h:38
ObsEnsemble(const ObsSpaces_ &, const size_t &nens)
Create ensemble of empty Observations size nens.
Definition: ObsEnsemble.h:45
Observations Class.
Definition: Observations.h:35
void zero()
Accumulator.
Definition: Observations.h:155
void accumul(const Observations &)
Definition: Observations.h:162
The namespace for the main oops code.