OOPS
State4D.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_ASSIMILATION_STATE4D_H_
12 #define OOPS_ASSIMILATION_STATE4D_H_
13 
14 #include <cmath>
15 #include <ostream>
16 #include <string>
17 #include <vector>
18 
19 #include "eckit/config/LocalConfiguration.h"
20 #include "eckit/exception/Exceptions.h"
21 
23 #include "oops/interface/State.h"
24 #include "oops/util/Logger.h"
25 #include "oops/util/Printable.h"
26 
27 namespace oops {
28 
29 /// Four dimensional state
30 /*!
31  * The 4D state is mostly used as part of the VDA control variable.
32  */
33 
34 // -----------------------------------------------------------------------------
35 template<typename MODEL> class State4D : public util::Printable {
38 
39  public:
40  static const std::string classname() {return "State4D";}
41 
42 /// The arguments define the number of sub-windows and the resolution
43  State4D(const Geometry_ &, const eckit::Configuration &);
44  explicit State4D(const State_ &);
45 
46 /// I/O and diagnostics
47  void read(const eckit::Configuration &);
48  void write(const eckit::Configuration &) const;
49  double norm() const;
50 
51 /// Get model space control variable
52  bool checkStatesNumber(const unsigned int nn) const {return state4d_.size() == nn;}
53  size_t size() const {return state4d_.size();}
54  State_ & operator[](const int ii) {return state4d_[ii];}
55  const State_ & operator[](const int ii) const {return state4d_[ii];}
56 
57  Geometry_ geometry() const { return state4d_[0].geometry(); }
58  const Variables & variables() const {return state4d_[0].variables();}
59  const std::vector<util::DateTime> validTimes() const;
60 
61 /// Accumulator
62  void zero();
63  void accumul(const double &, const State4D &);
64 
65  private:
66  void print(std::ostream &) const;
67 
68  std::vector<State_> state4d_;
69 };
70 
71 // =============================================================================
72 
73 template<typename MODEL>
74 State4D<MODEL>::State4D(const Geometry_ & resol, const eckit::Configuration & config) {
75  Log::trace() << "State4D config : " << config << std::endl;
76  // 4D state:
77  if (config.has("states")) {
78  std::vector<eckit::LocalConfiguration> confs;
79  config.get("states", confs);
80  state4d_.reserve(confs.size());
81  for (auto & conf : confs) {
82  state4d_.emplace_back(State_(resol, conf));
83  }
84  } else {
85  // 3D state:
86  state4d_.emplace_back(State_(resol, config));
87  }
88  for (size_t jj = 1; jj < state4d_.size(); ++jj) {
89  ASSERT(state4d_[jj].variables() == state4d_[0].variables());
90  }
91  Log::trace() << "State4D constructed." << std::endl;
92 }
93 
94 // -----------------------------------------------------------------------------
95 
96 template<typename MODEL>
97 State4D<MODEL>::State4D(const State_ & state3d) {
98  state4d_.emplace_back(state3d);
99  Log::trace() << "State4D constructed." << std::endl;
100 }
101 
102 // -----------------------------------------------------------------------------
103 
104 template<typename MODEL>
105 void State4D<MODEL>::read(const eckit::Configuration & config) {
106  // 4D state
107  if (config.has("states")) {
108  std::vector<eckit::LocalConfiguration> confs;
109  config.get("states", confs);
110  ASSERT(state4d_.size() == confs.size());
111  for (size_t jj = 0; jj < state4d_.size(); ++jj) {
112  state4d_[jj].read(confs[jj]);;
113  }
114  } else {
115  // 3D state
116  ASSERT(state4d_.size() == 1);
117  state4d_[0].read(config);
118  }
119  for (size_t jj = 1; jj < state4d_.size(); ++jj) {
120  ASSERT(state4d_[jj].variables() == state4d_[0].variables());
121  }
122 }
123 
124 // -----------------------------------------------------------------------------
125 
126 template<typename MODEL>
127 void State4D<MODEL>::write(const eckit::Configuration & config) const {
128  // 4D state
129  if (config.has("states")) {
130  std::vector<eckit::LocalConfiguration> confs;
131  config.get("states", confs);
132  ASSERT(state4d_.size() == confs.size());
133  for (size_t jj = 0; jj < state4d_.size(); ++jj) {
134  if (config.has("member")) confs[jj].set("member", config.getInt("member"));
135  state4d_[jj].write(confs[jj]);
136  }
137  } else {
138  // 3D state
139  ASSERT(state4d_.size() == 1);
140  state4d_[0].write(config);
141  }
142 }
143 
144 // -----------------------------------------------------------------------------
145 
146 template<typename MODEL>
147 const std::vector<util::DateTime> State4D<MODEL>::validTimes() const {
148  std::vector<util::DateTime> times;
149  times.reserve(state4d_.size());
150  for (const State_ & state : state4d_) {
151  times.push_back(state.validTime());
152  }
153  return times;
154 }
155 
156 // -----------------------------------------------------------------------------
157 
158 template<typename MODEL>
160  Log::trace() << "State4D<MODEL>::zero starting" << std::endl;
161  for (State_ & state : state4d_) {
162  state.zero();
163  }
164  Log::trace() << "State4D<MODEL>::zero done" << std::endl;
165 }
166 
167 // -----------------------------------------------------------------------------
168 
169 template<typename MODEL>
170 void State4D<MODEL>::accumul(const double & zz, const State4D & xx) {
171  Log::trace() << "State4D<MODEL>::accumul starting" << std::endl;
172  ASSERT(xx.size() == state4d_.size());
173  for (size_t jj = 0; jj < state4d_.size(); ++jj) {
174  state4d_[jj].accumul(zz, xx[jj]);
175  }
176  Log::trace() << "State4D<MODEL>::accumul done" << std::endl;
177 }
178 
179 // -----------------------------------------------------------------------------
180 
181 template <typename MODEL>
182 void State4D<MODEL>::print(std::ostream & outs) const {
183  for (const State_ & state : state4d_) {
184  outs << state << std::endl;
185  }
186 }
187 
188 // -----------------------------------------------------------------------------
189 
190 template<typename MODEL>
191 double State4D<MODEL>::norm() const {
192  double zn = 0.0;
193  for (const State_ & state : state4d_) {
194  double zz = state.norm();
195  zn += zz * zz;
196  }
197  return sqrt(zn);
198 }
199 
200 // -----------------------------------------------------------------------------
201 
202 } // namespace oops
203 
204 #endif // OOPS_ASSIMILATION_STATE4D_H_
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
oops::State4D::print
void print(std::ostream &) const
Definition: State4D.h:182
oops::State4D::read
void read(const eckit::Configuration &)
I/O and diagnostics.
Definition: State4D.h:105
oops::State4D::operator[]
State_ & operator[](const int ii)
Definition: State4D.h:54
oops::State4D::write
void write(const eckit::Configuration &) const
Definition: State4D.h:127
oops::State4D::validTimes
const std::vector< util::DateTime > validTimes() const
Definition: State4D.h:147
oops::State4D::classname
static const std::string classname()
Definition: State4D.h:40
oops::State4D::geometry
Geometry_ geometry() const
Definition: State4D.h:57
oops::State4D::operator[]
const State_ & operator[](const int ii) const
Definition: State4D.h:55
oops::State4D::norm
double norm() const
Definition: State4D.h:191
oops::State4D::zero
void zero()
Accumulator.
Definition: State4D.h:159
oops::State4D
Four dimensional state.
Definition: State4D.h:35
oops::Geometry
Geometry class used in oops; subclass of interface class above.
Definition: oops/interface/Geometry.h:189
oops::State4D::checkStatesNumber
bool checkStatesNumber(const unsigned int nn) const
Get model space control variable.
Definition: State4D.h:52
oops::State4D::size
size_t size() const
Definition: State4D.h:53
oops::State4D::state4d_
std::vector< State_ > state4d_
Definition: State4D.h:68
oops::State4D::State4D
State4D(const Geometry_ &, const eckit::Configuration &)
The arguments define the number of sub-windows and the resolution.
Definition: State4D.h:74
oops::State4D::variables
const Variables & variables() const
Definition: State4D.h:58
oops::State
Encapsulates the model state.
Definition: CostJbState.h:28
State.h
oops::Variables
Definition: oops/base/Variables.h:23
oops::State4D::Geometry_
Geometry< MODEL > Geometry_
Definition: State4D.h:36
oops::State4D::accumul
void accumul(const double &, const State4D &)
Definition: State4D.h:170
oops::State4D::State_
State< MODEL > State_
Definition: State4D.h:37
Geometry.h