SABER
ParametersBUMP.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2019 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 SABER_OOPS_PARAMETERSBUMP_H_
9 #define SABER_OOPS_PARAMETERSBUMP_H_
10 
11 #include <fstream>
12 #include <memory>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 
17 #include "eckit/config/Configuration.h"
18 
19 #include "oops/base/IncrementEnsemble.h"
20 #include "oops/base/Variables.h"
21 #include "oops/interface/State.h"
22 #include "oops/util/DateTime.h"
23 #include "oops/util/Duration.h"
24 #include "oops/util/Logger.h"
25 #include "oops/util/missingValues.h"
26 
27 #include "saber/oops/OoBump.h"
28 
29 namespace eckit {
30  class Configuration;
31 }
32 
33 namespace oops {
34  class Variables;
35 }
36 
37 namespace saber {
38 
39 // -----------------------------------------------------------------------------
40 /// BUMP parameters
41 
42 template<typename MODEL>
44  typedef oops::Geometry<MODEL> Geometry_;
45  typedef oops::Increment<MODEL> Increment_;
47  typedef oops::State<MODEL> State_;
48  typedef std::shared_ptr<oops::IncrementEnsemble<MODEL>> EnsemblePtr_;
49 
50  public:
51  static const std::string classname() {return "oops::ParametersBUMP";}
52  ParametersBUMP(const Geometry_ &,
53  const oops::Variables &,
54  const util::DateTime &,
55  const eckit::Configuration &,
56  const EnsemblePtr_ ens1 = NULL,
57  const EnsemblePtr_ ens2 = NULL);
59 
60  OoBump_ & getOoBump() {return *ooBump_;}
61  void write() const;
62 
63  private:
65  const oops::Variables vars_;
66  util::DateTime time_;
67  const eckit::LocalConfiguration conf_;
68  std::unique_ptr<OoBump_> ooBump_;
69 };
70 
71 // =============================================================================
72 
73 template<typename MODEL>
75  const oops::Variables & vars,
76  const util::DateTime & time,
77  const eckit::Configuration & conf,
78  const EnsemblePtr_ ens1,
79  const EnsemblePtr_ ens2)
80  : resol_(resol), vars_(vars), time_(time), conf_(conf), ooBump_()
81 {
82  oops::Log::trace() << "ParametersBUMP<MODEL>::ParametersBUMP construction starting" << std::endl;
83  util::Timer timer(classname(), "ParametersBUMP");
84 
85  // Setup BUMP configuration
86  eckit::LocalConfiguration BUMPConf(conf_, "bump");
87 
88  // Get ensemble 1 size if ensemble 1 is available
89  int ens1_ne = 0;
90  if (ens1) ens1_ne = ens1->size();
91  BUMPConf.set("ens1_ne", ens1_ne);
92  if (!BUMPConf.has("ens1_nsub")) BUMPConf.set("ens1_nsub", 1);
93 
94  // Get ensemble 2 size if ensemble 2 is available
95  int ens2_ne = 0;
96  if (ens2) ens2_ne = ens2->size();
97  BUMPConf.set("ens2_ne", ens2_ne);
98  if (!BUMPConf.has("ens2_nsub")) BUMPConf.set("ens2_nsub", 1);
99 
100  // Get missing value
101  const double msvalr = util::missingValue(msvalr);
102  BUMPConf.set("msvalr", msvalr);
103 
104  // Create BUMP
105  oops::Log::info() << "Create BUMP" << std::endl;
106  ooBump_.reset(new OoBump_(resol, vars, time_, BUMPConf));
107 
108  // Add members of ensemble 1
109  if (ens1) {
110  oops::Log::info() << "--- Add members of ensemble 1" << std::endl;
111  for (int ie = 0; ie < ens1_ne; ++ie) {
112  oops::Log::info() << " Member " << ie+1 << " / " << ens1_ne << std::endl;
113  ooBump_->addMember((*ens1)[ie].atlas(), ie, 1);
114  }
115  }
116 
117  // Add members of ensemble 2
118  if (ens2) {
119  oops::Log::info() << "--- Add members of ensemble 2" << std::endl;
120  for (int ie = 0; ie < ens2_ne; ++ie) {
121  oops::Log::info() << " Member " << ie+1 << " / " << ens2_ne << std::endl;
122  ooBump_->addMember((*ens2)[ie].atlas(), ie, 2);
123  }
124  }
125 
126  // Read data from files
127  oops::Log::info() << " Read data from files" << std::endl;
128  if (conf_.has("input")) {
129  // Set BUMP input parameters
130  std::vector<eckit::LocalConfiguration> inputConfs;
131  conf_.get("input", inputConfs);
132 
133  for (const auto & inputConf : inputConfs) {
134  // Read parameter for the specified time
135  const util::DateTime date(inputConf.getString("date"));
136 
137  // Setup increment
139  dx.read(inputConf);
140 
141  // Set parameter to BUMP
142  std::string param = inputConf.getString("parameter");
143  ooBump_->setParameter(param, dx);
144  }
145  }
146 
147  // Estimate parameters
148  ooBump_->runDrivers();
149 
150  oops::Log::trace() << "ParametersBUMP:ParametersBUMP constructed" << std::endl;
151 }
152 
153 // -----------------------------------------------------------------------------
154 
155 template<typename MODEL>
157  oops::Log::trace() << "ParametersBUMP<MODEL>::~ParametersBUMP destruction starting" << std::endl;
158  util::Timer timer(classname(), "~ParametersBUMP");
159  oops::Log::trace() << "ParametersBUMP:~ParametersBUMP destructed" << std::endl;
160 }
161 
162 // -----------------------------------------------------------------------------
163 
164 template<typename MODEL>
166  oops::Log::trace() << "ParametersBUMP::write starting" << std::endl;
167  util::Timer timer(classname(), "write");
168 
169 // Write parameters
170  oops::Log::info() <<
171  "-------------------------------------------------------------------" << std::endl;
172  oops::Log::info() << "--- Write parameters" << std::endl;
173 
174  std::vector<eckit::LocalConfiguration> outputConfs;
175  conf_.get("output", outputConfs);
176  for (const auto & outputConf : outputConfs) {
177  // Setup dummy increment
178  Increment_ dx(resol_, vars_, time_);
179  dx.zero();
180 
181  // Get parameter from BUMP
182  std::string param = outputConf.getString("parameter");
183  ooBump_->getParameter(param, dx);
184 
185  // Write parameter for the specified time
186  const util::DateTime date(outputConf.getString("date"));
187  dx.write(outputConf);
188  oops::Log::test() << "Norm of " << param << " at " << date << ": " << std::scientific
189  << std::setprecision(3) << dx.norm() << std::endl;
190  }
191  oops::Log::info() <<
192  "-------------------------------------------------------------------" << std::endl;
193  oops::Log::trace() << "ParametersBUMP::write done" << std::endl;
194 }
195 
196 // -----------------------------------------------------------------------------
197 
198 } // namespace saber
199 
200 #endif // SABER_OOPS_PARAMETERSBUMP_H_
oops
Definition: ErrorCovarianceBUMP.h:33
saber::ParametersBUMP
BUMP parameters.
Definition: ParametersBUMP.h:43
saber::ParametersBUMP::conf_
const eckit::LocalConfiguration conf_
Definition: ParametersBUMP.h:67
saber::ParametersBUMP::OoBump_
OoBump< MODEL > OoBump_
Definition: ParametersBUMP.h:46
saber::ParametersBUMP::Geometry_
oops::Geometry< MODEL > Geometry_
Definition: ParametersBUMP.h:44
saber::ParametersBUMP::Increment_
oops::Increment< MODEL > Increment_
Definition: ParametersBUMP.h:45
saber::ParametersBUMP::ooBump_
std::unique_ptr< OoBump_ > ooBump_
Definition: ParametersBUMP.h:68
saber::ParametersBUMP::ParametersBUMP
ParametersBUMP(const Geometry_ &, const oops::Variables &, const util::DateTime &, const eckit::Configuration &, const EnsemblePtr_ ens1=NULL, const EnsemblePtr_ ens2=NULL)
Definition: ParametersBUMP.h:74
saber
Definition: type_bump.h:22
saber::ParametersBUMP::write
void write() const
Definition: ParametersBUMP.h:165
saber::OoBump
OoBump C++ interface.
Definition: OoBump.h:47
saber::ParametersBUMP::classname
static const std::string classname()
Definition: ParametersBUMP.h:51
eckit
Definition: type_bump.h:18
OoBump.h
saber::ParametersBUMP::getOoBump
OoBump_ & getOoBump()
Definition: ParametersBUMP.h:60
saber::ParametersBUMP::EnsemblePtr_
std::shared_ptr< oops::IncrementEnsemble< MODEL > > EnsemblePtr_
Definition: ParametersBUMP.h:48
saber::ParametersBUMP::vars_
const oops::Variables vars_
Definition: ParametersBUMP.h:65
saber::ParametersBUMP::State_
oops::State< MODEL > State_
Definition: ParametersBUMP.h:47
saber::ParametersBUMP::time_
util::DateTime time_
Definition: ParametersBUMP.h:66
saber::ParametersBUMP::resol_
const Geometry_ resol_
Definition: ParametersBUMP.h:64
saber::ParametersBUMP::~ParametersBUMP
~ParametersBUMP()
Definition: ParametersBUMP.h:156