SABER
StatsVariableChange.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2018 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_STATSVARIABLECHANGE_H_
9 #define SABER_OOPS_STATSVARIABLECHANGE_H_
10 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "eckit/config/Configuration.h"
16 
17 #include "oops/base/LinearVariableChangeBase.h"
18 #include "oops/base/Variables.h"
19 #include "oops/interface/Geometry.h"
20 #include "oops/interface/Increment.h"
21 #include "oops/interface/State.h"
22 
23 #include "saber/oops/OoBump.h"
25 
26 namespace eckit {
27  class Configuration;
28 }
29 
30 namespace oops {
31  class Variables;
32 }
33 
34 namespace saber {
35 
36 // -----------------------------------------------------------------------------
37 
38 /// Derived class of generic variable transform for statistical
39 
40 template <typename MODEL>
41 class StatsVariableChange : public oops::LinearVariableChangeBase<MODEL> {
42  typedef oops::Geometry<MODEL> Geometry_;
43  typedef oops::Increment<MODEL> Increment_;
45  typedef oops::State<MODEL> State_;
47 
48  public:
49  static const std::string classname() {return "saber::StatsVariableChange";}
50 
51  StatsVariableChange(const State_ &, const State_ &,
52  const Geometry_ &, const eckit::Configuration &);
53  virtual ~StatsVariableChange();
54 
55  void multiply(const Increment_ &, Increment_ &) const override;
56  void multiplyInverse(const Increment_ &, Increment_ &) const override;
57  void multiplyAD(const Increment_ &, Increment_ &) const override;
58  void multiplyInverseAD(const Increment_ &, Increment_ &) const override;
59 
60  private:
61  const std::vector<std::string> pathStrList;
62 
63  void print(std::ostream &) const override;
64  std::string ExtractFilename(std::string const pathString);
65  void ExtractModelVarForCalc(std::string const fileString,
66  std::string& modelVarToCalcString,
67  std::string& varRegrByString);
68  void VariableLists(const std::vector<std::string> pathStrList,
69  std::vector<std::string>& modelVarToCalcList,
70  std::vector<std::string>& varRegrByList);
71 
72  std::unique_ptr<OoBump_> ooBump_;
73 
74  // StatsVarData populate(const varin_ , const varout_, const eckit::Configuration);
75 };
76 
77 template<typename MODEL>
79  const Geometry_ & resol,
80  const eckit::Configuration & conf)
81  : oops::LinearVariableChangeBase<MODEL>(conf), ooBump_()
82 {
83  oops::Log::trace() << "StatsVariableChange<MODEL>::StatsVariableChange starting" << std::endl;
84 
85 // Setup variables
86  const oops::Variables vars(conf, "input variables");
87 
88 // Setup parameters
89  ParametersBUMP_ param(resol, vars, xb.validTime(), conf);
90 
91 // Transfer OoBump pointer
92  ooBump_.reset(new OoBump_(param.getOoBump()));
93 
94  oops::Log::trace() << "StatsVariableChange<MODEL>::StatsVariableChange done" << std::endl;
95 }
96 
97 template<typename MODEL>
99  oops::Log::trace() << "StatsVariableChange<MODEL>::~StatsVariableChange starting" << std::endl;
100  util::Timer timer(classname(), "~StatsVariableChange");
101  oops::Log::trace() << "StatsVariableChange<MODEL>::~StatsVariableChange done" << std::endl;
102 }
103 
104 template<typename MODEL>
106  oops::Log::trace() << "StatsVariableChange<MODEL>::multiply starting" << std::endl;
107 
108  ooBump_->multiplyVbal(in, out);
109 
110  oops::Log::trace() << "StatsVariableChange<MODEL>::multiply done" << std::endl;
111 }
112 
113 template<typename MODEL>
115  oops::Log::trace() << "StatsVariableChange<MODEL>::multiplyInverse starting" << std::endl;
116 
117  ooBump_->multiplyVbalInv(in, out);
118 
119  oops::Log::trace() << "StatsVariableChange<MODEL>::multiplyInverse done" << std::endl;
120 }
121 
122 template<typename MODEL>
124  oops::Log::trace() << "StatsVariableChange<MODEL>::multiplyAD starting" << std::endl;
125 
126  ooBump_->multiplyVbalAd(in, out);
127 
128  oops::Log::trace() << "StatsVariableChange<MODEL>::multiplyAD done" << std::endl;
129 }
130 
131 template<typename MODEL>
133  oops::Log::trace() << "StatsVariableChange<MODEL>::multiplyInverseAD starting" << std::endl;
134 
135  ooBump_->multiplyVbalInvAd(in, out);
136 
137  oops::Log::trace() << "StatsVariableChange<MODEL>::multiplyInverseAD done" << std::endl;
138 }
139 
140 template<typename MODEL>
141 void StatsVariableChange<MODEL>::print(std::ostream & os) const {
142 }
143 
144 template<typename MODEL>
145 std::string StatsVariableChange<MODEL>::ExtractFilename(std::string const pathString)
146 {
147  int tempIndex;
148  for (int iCharIndex = pathString.length(); iCharIndex > 0; --iCharIndex)
149  {
150  if (pathString.substr(iCharIndex - 1, 1) == "/")
151  {
152  return pathString.substr(iCharIndex, pathString.length() - iCharIndex);
153  }
154  tempIndex = iCharIndex;
155  }
156  std::cout << "ExtractFileName: tempIndex = " << tempIndex << std::endl;
157  throw std::runtime_error("FileName not extracted from path " + pathString);
158 }
159 
160 // extract {model_variable_to_be_calculated} string and {variable_regressed_by} string
161 // from fileString
162 
163 template<typename MODEL>
164 void StatsVariableChange<MODEL>::ExtractModelVarForCalc(std::string const fileString,
165  std::string& modelVarToCalcString,
166  std::string& varRegrByString)
167 {
168 // FileName is of the form {modelVarToCalcString}__{varRegrByString}__reg.nc
169 // find first "__" and the last "__" to find the appropriate strings
170  int firstUnderscoreIndex, lastUnderscoreIndex;
171  for (int iCharIndex = 0; iCharIndex < fileString.length() - 1; ++iCharIndex)
172  {
173  firstUnderscoreIndex = iCharIndex;
174  if (fileString.substr(iCharIndex, 2) == "__")
175  {
176  break;
177  }
178  }
179  for (int iCharIndex = fileString.length(); iCharIndex > 0; --iCharIndex)
180  {
181  lastUnderscoreIndex = iCharIndex - 1;
182  if (fileString.substr(iCharIndex-1, 2) == "__")
183  {
184  break;
185  }
186  }
187  modelVarToCalcString = fileString.substr(0, firstUnderscoreIndex);
188  varRegrByString = fileString.substr(firstUnderscoreIndex + 2,
189  lastUnderscoreIndex - firstUnderscoreIndex - 2);
190 }
191 
192 // append the modelVarToCalcList and varRegrByList
193 template<typename MODEL>
194 void StatsVariableChange<MODEL>::VariableLists(const std::vector<std::string> pathStrList,
195  std::vector<std::string>& modelVarToCalcList,
196  std::vector<std::string>& varRegrByList)
197 {
198  std::string modelVarToCalcString;
199  std::string varRegrByString;
200  std::string filename;
201  for (auto pathString : pathStrList)
202  {
203  modelVarToCalcString.clear();
204  varRegrByString.clear();
205  filename = ExtractFilename(pathString);
206  ExtractModelVarForCalc(filename, modelVarToCalcString, varRegrByString);
207  modelVarToCalcList.push_back(modelVarToCalcString);
208  varRegrByList.push_back(varRegrByString);
209  }
210 }
211 
212 
213 } // namespace saber
214 
215 #endif // SABER_OOPS_STATSVARIABLECHANGE_H_
oops
Definition: ErrorCovarianceBUMP.h:33
saber::StatsVariableChange::ExtractFilename
std::string ExtractFilename(std::string const pathString)
Definition: StatsVariableChange.h:145
saber::StatsVariableChange::pathStrList
const std::vector< std::string > pathStrList
Definition: StatsVariableChange.h:61
saber::ParametersBUMP
BUMP parameters.
Definition: ParametersBUMP.h:43
saber::StatsVariableChange::ParametersBUMP_
ParametersBUMP< MODEL > ParametersBUMP_
Definition: StatsVariableChange.h:46
saber::StatsVariableChange::ooBump_
std::unique_ptr< OoBump_ > ooBump_
Definition: StatsVariableChange.h:72
saber::StatsVariableChange::classname
static const std::string classname()
Definition: StatsVariableChange.h:49
saber::StatsVariableChange::ExtractModelVarForCalc
void ExtractModelVarForCalc(std::string const fileString, std::string &modelVarToCalcString, std::string &varRegrByString)
Definition: StatsVariableChange.h:164
saber::StatsVariableChange::~StatsVariableChange
virtual ~StatsVariableChange()
Definition: StatsVariableChange.h:98
saber::StatsVariableChange::Increment_
oops::Increment< MODEL > Increment_
Definition: StatsVariableChange.h:43
saber::StatsVariableChange::print
void print(std::ostream &) const override
Definition: StatsVariableChange.h:141
saber::StatsVariableChange::OoBump_
OoBump< MODEL > OoBump_
Definition: StatsVariableChange.h:44
ParametersBUMP.h
saber
Definition: type_bump.h:22
saber::OoBump
OoBump C++ interface.
Definition: OoBump.h:47
saber::StatsVariableChange::multiplyInverse
void multiplyInverse(const Increment_ &, Increment_ &) const override
Definition: StatsVariableChange.h:114
saber::StatsVariableChange::multiplyAD
void multiplyAD(const Increment_ &, Increment_ &) const override
Definition: StatsVariableChange.h:123
eckit
Definition: type_bump.h:18
OoBump.h
saber::ParametersBUMP::getOoBump
OoBump_ & getOoBump()
Definition: ParametersBUMP.h:60
saber::StatsVariableChange::Geometry_
oops::Geometry< MODEL > Geometry_
Definition: StatsVariableChange.h:42
saber::StatsVariableChange::multiply
void multiply(const Increment_ &, Increment_ &) const override
Definition: StatsVariableChange.h:105
saber::StatsVariableChange::StatsVariableChange
StatsVariableChange(const State_ &, const State_ &, const Geometry_ &, const eckit::Configuration &)
Definition: StatsVariableChange.h:78
saber::StatsVariableChange::multiplyInverseAD
void multiplyInverseAD(const Increment_ &, Increment_ &) const override
Definition: StatsVariableChange.h:132
saber::StatsVariableChange
Derived class of generic variable transform for statistical.
Definition: StatsVariableChange.h:41
saber::StatsVariableChange::VariableLists
void VariableLists(const std::vector< std::string > pathStrList, std::vector< std::string > &modelVarToCalcList, std::vector< std::string > &varRegrByList)
Definition: StatsVariableChange.h:194
saber::StatsVariableChange::State_
oops::State< MODEL > State_
Definition: StatsVariableChange.h:45