FV3 Bundle
src/lorenz95/StateL95.cc
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 #include "lorenz95/StateL95.h"
12 
13 #include <fstream>
14 #include <string>
15 
16 #include "eckit/config/Configuration.h"
17 #include "lorenz95/FieldL95.h"
18 #include "lorenz95/GomL95.h"
19 #include "lorenz95/IncrementL95.h"
20 #include "lorenz95/LocsL95.h"
21 #include "lorenz95/ModelBias.h"
22 #include "lorenz95/ModelL95.h"
24 #include "lorenz95/Nothing.h"
25 #include "lorenz95/Resolution.h"
27 #include "oops/util/abor1_cpp.h"
28 #include "oops/util/DateTime.h"
29 #include "oops/util/Duration.h"
30 #include "oops/util/Logger.h"
31 
32 namespace oops {
33  class Variables;
34 }
35 
36 namespace lorenz95 {
37 
38 // -----------------------------------------------------------------------------
39 /// Constructor, destructor
40 // -----------------------------------------------------------------------------
42  const util::DateTime & vt)
43  : fld_(resol), time_(vt)
44 {
45  oops::Log::trace() << "StateL95::StateL95 created" << std::endl;
46 }
47 // -----------------------------------------------------------------------------
49  const eckit::Configuration & conf)
50  : fld_(resol), time_(conf.getString("date"))
51 {
52  oops::Log::trace() << "StateL95::StateL95 conf " << conf << std::endl;
53  if (conf.has("filename")) {
54  this->read(conf);
55  } else {
57  }
58  oops::Log::trace() << "StateL95::StateL95 created and read in." << std::endl;
59 }
60 // -----------------------------------------------------------------------------
61 StateL95::StateL95(const Resolution & resol, const StateL95 & xx)
62  : fld_(resol), time_(xx.time_)
63 {
64  fld_ = xx.fld_;
65  oops::Log::trace() << "StateL95::StateL95 created by interpolation." << std::endl;
66 }
67 // -----------------------------------------------------------------------------
69  : fld_(xx.fld_), time_(xx.time_)
70 {
71  oops::Log::trace() << "StateL95::StateL95 copy-created." << std::endl;
72 }
73 // -----------------------------------------------------------------------------
75  oops::Log::trace() << "StateL95::StateL95 destructed." << std::endl;
76 }
77 // -----------------------------------------------------------------------------
78 /// Basic operators
79 // -----------------------------------------------------------------------------
81  fld_ = rhs.fld_;
82  time_ = rhs.time_;
83  return *this;
84 }
85 // -----------------------------------------------------------------------------
86 /// Get state values at obs locations
87 // -----------------------------------------------------------------------------
88 void StateL95::getValues(const LocsL95 & locs, const oops::Variables &, GomL95 & vals) const {
89  fld_.interp(locs, vals);
90 }
91 // -----------------------------------------------------------------------------
92 void StateL95::getValues(const LocsL95 & locs, const oops::Variables &, GomL95 & vals,
93  Nothing &) const {
94  fld_.interp(locs, vals);
95 }
96 // -----------------------------------------------------------------------------
97 /// Interactions with Increments
98 // -----------------------------------------------------------------------------
100  ASSERT(time_ == dx.validTime());
101  fld_ += dx.getField();
102  return *this;
103 }
104 // -----------------------------------------------------------------------------
105 /// Utilities
106 // -----------------------------------------------------------------------------
107 void StateL95::read(const eckit::Configuration & config) {
108  const std::string filename(config.getString("filename"));
109  oops::Log::trace() << "StateL95::read opening " << filename << std::endl;
110  std::ifstream fin(filename.c_str());
111  if (!fin.is_open()) ABORT("StateL95::read: Error opening file");
112 
113  int resol;
114  fin >> resol;
115  ASSERT(fld_.resol() == resol);
116 
117  std::string stime;
118  fin >> stime;
119  const util::DateTime tt(stime);
120  if (time_ != tt) {
121  ABORT("StateL95::read: date and data file inconsistent.");
122  }
123 
124  fld_.read(fin);
125 
126  fin.close();
127  oops::Log::trace() << "StateL95::read: file closed." << std::endl;
128 }
129 // -----------------------------------------------------------------------------
130 void StateL95::write(const eckit::Configuration & config) const {
131  std::string dir = config.getString("datadir");
132  std::string exp = config.getString("exp");
133  std::string type = config.getString("type");
134  std::string filename = dir+"/"+exp+"."+type;
135 
136  if (type == "ens") {
137  std::string memb = config.getString("member");
138  filename += "."+memb;
139  }
140 
141  if (type == "fc" || type == "ens") {
142  const util::DateTime antime(config.getString("date"));
143  filename += "."+antime.toString();
144  const util::Duration step = time_ - antime;
145  filename += "."+step.toString();
146  }
147 
148  if (type == "an") {
149  filename += "."+time_.toString();
150  }
151 
152  oops::Log::trace() << "StateL95::write opening " << filename << std::endl;
153  std::ofstream fout(filename.c_str());
154  if (!fout.is_open()) ABORT("StateL95::write: Error opening file");
155 
156  fout << fld_.resol() << std::endl;
157  fout << time_ << std::endl;
158  fld_.write(fout);
159  fout << std::endl;
160 
161  fout.close();
162  oops::Log::trace() << "StateL95::write file closed." << std::endl;
163 }
164 // -----------------------------------------------------------------------------
165 void StateL95::print(std::ostream & os) const {
166  os << std::endl << " Valid time: " << time_;
167  os << std::endl << fld_;
168 }
169 // -----------------------------------------------------------------------------
170 /// For accumulator
171 // -----------------------------------------------------------------------------
173  fld_.zero();
174 }
175 // -----------------------------------------------------------------------------
176 void StateL95::accumul(const double & zz, const StateL95 & xx) {
177  fld_.axpy(zz, xx.fld_);
178 }
179 // -----------------------------------------------------------------------------
180 
181 
182 } // namespace lorenz95
GomL95 class to handle locations for L95 model.
Definition: GomL95.h:32
void interp(const LocsL95 &, GomL95 &) const
Interpolate to given location.
Definition: FieldL95.cc:148
Increment Class: Difference between two states.
Definition: IncrementL95.h:51
Definition: conf.py:1
StateL95(const Resolution &, const oops::Variables &, const util::DateTime &)
Constructor, destructor.
void zero()
For accumulator.
FieldL95 fld_
Definition: StateL95.h:90
LocsL95 class to handle locations for L95 model.
Definition: LocsL95.h:28
Handles resolution.
Definition: Resolution.h:25
The namespace for the main oops code.
void zero()
Linear algebra.
Definition: FieldL95.cc:56
const FieldL95 & getField() const
Access to data.
Definition: IncrementL95.h:97
util::DateTime time_
Definition: StateL95.h:91
StateL95 & operator+=(const IncrementL95 &)
Interactions with increments.
type
Definition: c2f.py:15
real, dimension(:,:,:), allocatable vt
void print(std::ostream &) const
void read(std::ifstream &)
Utilities.
Definition: FieldL95.cc:183
The namespace for the L95 model.
StateL95 & operator=(const StateL95 &)
Basic operators.
subroutine, public step(x, g)
const int & resol() const
Set and get.
Definition: FieldL95.h:66
void getValues(const LocsL95 &, const oops::Variables &, GomL95 &) const
Get state values at obs locations.
void generate(const eckit::Configuration &)
Definition: FieldL95.cc:75
void write(const eckit::Configuration &) const
void accumul(const double &, const StateL95 &)
L95 model state.
Definition: StateL95.h:50
void read(const eckit::Configuration &)
Utilities.
const util::DateTime & validTime() const
Definition: IncrementL95.h:92
void write(std::ofstream &) const
Definition: FieldL95.cc:187
void axpy(const double &, const FieldL95 &)
Definition: FieldL95.cc:125