SOCA
State.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2017-2021 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 #include <iomanip>
9 #include <vector>
10 
11 #include "soca/Geometry/Geometry.h"
13 #include "soca/State/State.h"
15 
16 #include "eckit/config/LocalConfiguration.h"
17 #include "eckit/exception/Exceptions.h"
18 
19 #include "oops/base/Variables.h"
20 #include "oops/util/DateTime.h"
21 #include "oops/util/Logger.h"
22 
23 #include "ufo/GeoVaLs.h"
24 #include "ufo/Locations.h"
25 
26 using oops::Log;
27 
28 namespace soca {
29 
30  // -----------------------------------------------------------------------------
31  /// Constructor, destructor
32  // -----------------------------------------------------------------------------
33  State::State(const Geometry & geom, const oops::Variables & vars,
34  const util::DateTime & vt)
35  : time_(vt), vars_(vars), geom_(new Geometry(geom))
36  {
37  soca_state_create_f90(keyFlds_, geom_->toFortran(), vars_);
38  Log::trace() << "State::State created." << std::endl;
39  }
40  // -----------------------------------------------------------------------------
41  State::State(const Geometry & geom, const eckit::Configuration & file)
42  : time_(),
43  vars_(file, "state variables"),
44  geom_(new Geometry(geom))
45  {
46  util::DateTime * dtp = &time_;
47  oops::Variables vars(vars_);
48  soca_state_create_f90(keyFlds_, geom_->toFortran(), vars);
49  soca_state_read_file_f90(toFortran(), &file, &dtp);
50  Log::trace() << "State::State created and read in." << std::endl;
51  }
52  // -----------------------------------------------------------------------------
53  State::State(const Geometry & geom, const State & other)
54  : vars_(other.vars_), time_(other.time_), geom_(new Geometry(geom))
55  {
56  soca_state_create_f90(keyFlds_, geom_->toFortran(), vars_);
58  Log::trace() << "State::State created by interpolation." << std::endl;
59  }
60  // -----------------------------------------------------------------------------
61  State::State(const State & other)
62  : vars_(other.vars_), time_(other.time_), geom_(new Geometry(*other.geom_))
63  {
64  soca_state_create_f90(keyFlds_, geom_->toFortran(), vars_);
66  Log::trace() << "State::State copied." << std::endl;
67  }
68  // -----------------------------------------------------------------------------
71  Log::trace() << "State::State destructed." << std::endl;
72  }
73  // -----------------------------------------------------------------------------
74  /// Basic operators
75  // -----------------------------------------------------------------------------
76  State & State::operator=(const State & rhs) {
77  time_ = rhs.time_;
79  return *this;
80  }
81  // -----------------------------------------------------------------------------
82  /// Rotations
83  // -----------------------------------------------------------------------------
84  void State::rotate2north(const oops::Variables & u,
85  const oops::Variables & v) const {
86  Log::trace() << "State::State rotate from logical to geographical North."
87  << std::endl;
89  }
90  // -----------------------------------------------------------------------------
91  void State::rotate2grid(const oops::Variables & u,
92  const oops::Variables & v) const {
93  Log::trace() << "State::State rotate from geographical to logical North."
94  << std::endl;
96  }
97  // -----------------------------------------------------------------------------
98  /// Interactions with Increments
99  // -----------------------------------------------------------------------------
101  ASSERT(validTime() == dx.validTime());
102  // Interpolate increment to analysis grid
103  Increment dx_hr(*geom_, dx);
104 
105  // Add increment to background state
107  return *this;
108  }
109  // -----------------------------------------------------------------------------
110  /// I/O and diagnostics
111  // -----------------------------------------------------------------------------
112  void State::read(const eckit::Configuration & files) {
113  Log::trace() << "State::State read started." << std::endl;
114  util::DateTime * dtp = &time_;
115  soca_state_read_file_f90(toFortran(), &files, &dtp);
116  Log::trace() << "State::State read done." << std::endl;
117  }
118  // -----------------------------------------------------------------------------
119  void State::write(const eckit::Configuration & files) const {
120  const util::DateTime * dtp = &time_;
121  soca_state_write_file_f90(toFortran(), &files, &dtp);
122  }
123  // -----------------------------------------------------------------------------
124  void State::print(std::ostream & os) const {
125  os << std::endl << " Valid time: " << validTime();
126  int n0, nf;
127  soca_state_sizes_f90(toFortran(), n0, n0, n0, nf);
128  std::vector<double> zstat(3*nf);
129  soca_state_gpnorm_f90(toFortran(), nf, zstat[0]);
130  for (int jj = 0; jj < nf; ++jj) {
131  os << std::endl << std::right << std::setw(7) << vars_[jj]
132  << " min=" << std::fixed << std::setw(12) <<
133  std::right << zstat[3*jj]
134  << " max=" << std::fixed << std::setw(12) <<
135  std::right << zstat[3*jj+1]
136  << " mean=" << std::fixed << std::setw(12) <<
137  std::right << zstat[3*jj+2];
138  }
139  }
140  // -----------------------------------------------------------------------------
141  /// Serialization
142  // -----------------------------------------------------------------------------
143  size_t State::serialSize() const {
144  // Field
145  size_t nn;
146  soca_state_serial_size_f90(toFortran(), geom_->toFortran(), nn);
147 
148  // Magic factor
149  nn += 1;
150 
151  // Date and time
152  nn += time_.serialSize();
153  return nn;
154  }
155  // -----------------------------------------------------------------------------
156  constexpr double SerializeCheckValue = -54321.98765;
157  void State::serialize(std::vector<double> & vect) const {
158  // Serialize the field
159  size_t nn;
160  soca_state_serial_size_f90(toFortran(), geom_->toFortran(), nn);
161  std::vector<double> vect_field(nn, 0);
162  vect.reserve(vect.size() + nn + 1 + time_.serialSize());
163  soca_state_serialize_f90(toFortran(), geom_->toFortran(), nn,
164  vect_field.data());
165  vect.insert(vect.end(), vect_field.begin(), vect_field.end());
166 
167  // Magic value placed in serialization; used to validate deserialization
168  vect.push_back(SerializeCheckValue);
169 
170  // Serialize the date and time
171  time_.serialize(vect);
172  }
173  // -----------------------------------------------------------------------------
174  void State::deserialize(const std::vector<double> & vect, size_t & index) {
175  // Deserialize the field
176  soca_state_deserialize_f90(toFortran(), geom_->toFortran(), vect.size(),
177  vect.data(), index);
178 
179  // Use magic value to validate deserialization
180  ASSERT(vect.at(index) == SerializeCheckValue);
181  ++index;
182 
183  // Deserialize the date and time
184  time_.deserialize(vect, index);
185  }
186  // -----------------------------------------------------------------------------
187  /// For accumulator
188  // -----------------------------------------------------------------------------
189  void State::zero() {
191  }
192  // -----------------------------------------------------------------------------
193  void State::accumul(const double & zz, const State & xx) {
195  }
196  // -----------------------------------------------------------------------------
197  double State::norm() const {
198  double zz = 0.0;
200  return zz;
201  }
202  // -----------------------------------------------------------------------------
203  /// Logarithmic and exponential transformations
204  // -----------------------------------------------------------------------------
205  void State::logtrans(const oops::Variables & trvar) const {
206  Log::trace() << "State::State apply logarithmic transformation."
207  << std::endl;
209  }
210  // -----------------------------------------------------------------------------
211  void State::expontrans(const oops::Variables & trvar) const {
212  Log::trace() << "State::State apply exponential transformation."
213  << std::endl;
215  }
216 
217  // -----------------------------------------------------------------------------
218  const util::DateTime & State::validTime() const {return time_;}
219  // -----------------------------------------------------------------------------
220  util::DateTime & State::validTime() {return time_;}
221  // -----------------------------------------------------------------------------
222  std::shared_ptr<const Geometry> State::geometry() const {return geom_;}
223  // -----------------------------------------------------------------------------
224 
225 } // namespace soca
Geometry handles geometry for SOCA model.
Definition: Geometry.h:48
Increment Class: Difference between two states.
Definition: Increment.h:61
int & toFortran()
Definition: Increment.h:112
const util::DateTime & validTime() const
Definition: Increment.cc:253
SOCA model state.
Definition: State.h:48
std::shared_ptr< const Geometry > geometry() const
Definition: State.cc:222
void serialize(std::vector< double > &) const override
Definition: State.cc:157
void rotate2grid(const oops::Variables &, const oops::Variables &) const
Definition: State.cc:91
double norm() const
Definition: State.cc:197
void accumul(const double &, const State &)
Definition: State.cc:193
F90flds keyFlds_
Definition: State.h:100
const util::DateTime & validTime() const
Definition: State.cc:218
void deserialize(const std::vector< double > &, size_t &) override
Definition: State.cc:174
int & toFortran()
Definition: State.h:88
State & operator+=(const Increment &)
Interactions with Increment.
Definition: State.cc:100
void write(const eckit::Configuration &) const
Definition: State.cc:119
State(const Geometry &, const oops::Variables &, const util::DateTime &)
Constructor, destructor.
Definition: State.cc:33
void expontrans(const oops::Variables &) const
Definition: State.cc:211
oops::Variables vars_
Definition: State.h:103
void zero()
Other.
Definition: State.cc:189
void print(std::ostream &) const override
Definition: State.cc:124
size_t serialSize() const override
Serialize and deserialize.
Definition: State.cc:143
void rotate2north(const oops::Variables &, const oops::Variables &) const
Rotations.
Definition: State.cc:84
virtual ~State()
Definition: State.cc:69
util::DateTime time_
Definition: State.h:104
void logtrans(const oops::Variables &) const
Logarithmic and exponential transformations.
Definition: State.cc:205
std::shared_ptr< const Geometry > geom_
Definition: State.h:102
void read(const eckit::Configuration &)
I/O and diagnostics.
Definition: State.cc:112
State & operator=(const State &)
Basic operators.
Definition: State.cc:76
void soca_state_add_incr_f90(const F90flds &, const F90flds &)
void soca_state_sizes_f90(const F90flds &, int &, int &, int &, int &)
void soca_state_create_f90(F90flds &, const F90geom &, const oops::Variables &)
void soca_state_rotate2grid_f90(const F90flds &, const oops::Variables &, const oops::Variables &)
void soca_state_gpnorm_f90(const F90flds &, const int &, double &)
void soca_state_deserialize_f90(const F90flds &, const F90geom &, const size_t &, const double[], size_t &)
void soca_state_write_file_f90(const F90flds &, const eckit::Configuration *const &, const util::DateTime *const *)
void soca_state_serial_size_f90(const F90flds &, const F90geom &, size_t &)
void soca_state_logtrans_f90(const F90flds &, const oops::Variables &)
void soca_state_axpy_f90(const F90flds &, const double &, const F90flds &)
void soca_state_expontrans_f90(const F90flds &, const oops::Variables &)
void soca_state_zero_f90(const F90flds &)
constexpr double SerializeCheckValue
Definition: Increment.cc:279
void soca_state_copy_f90(const F90flds &, const F90flds &)
void soca_state_delete_f90(F90flds &)
void soca_state_change_resol_f90(const F90flds &, const F90flds &)
void soca_state_read_file_f90(const F90flds &, const eckit::Configuration *const &, util::DateTime *const *)
void soca_state_rms_f90(const F90flds &, double &)
void soca_state_rotate2north_f90(const F90flds &, const oops::Variables &, const oops::Variables &)
void soca_state_serialize_f90(const F90flds &, const F90geom &, const size_t &, double[])