SABER
StdDevVariableChange.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_STDDEVVARIABLECHANGE_H_
9 #define SABER_OOPS_STDDEVVARIABLECHANGE_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 StdDevVariableChange : 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::StdDevVariableChange";}
50 
51  StdDevVariableChange(const State_ &, const State_ &,
52  const Geometry_ &, const eckit::Configuration &);
53  virtual ~StdDevVariableChange();
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() << "StdDevVariableChange<MODEL>::StdDevVariableChange 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() << "StdDevVariableChange<MODEL>::StdDevVariableChange done" << std::endl;
95 }
96 
97 template<typename MODEL>
99  oops::Log::trace() << "StdDevVariableChange<MODEL>::~StdDevVariableChange starting" << std::endl;
100  util::Timer timer(classname(), "~StdDevVariableChange");
101  oops::Log::trace() << "StdDevVariableChange<MODEL>::~StdDevVariableChange done" << std::endl;
102 }
103 
104 template<typename MODEL>
106  oops::Log::trace() << "StdDevVariableChange<MODEL>::multiply starting" << std::endl;
107 
108  ooBump_->multiplyStdDev(in, out);
109 
110  oops::Log::trace() << "StdDevVariableChange<MODEL>::multiply done" << std::endl;
111 }
112 
113 template<typename MODEL>
115  oops::Log::trace() << "StdDevVariableChange<MODEL>::multiplyInverse starting" << std::endl;
116 
117  ooBump_->multiplyStdDevInv(in, out);
118 
119  oops::Log::trace() << "StdDevVariableChange<MODEL>::multiplyInverse done" << std::endl;
120 }
121 
122 template<typename MODEL>
124  oops::Log::trace() << "StdDevVariableChange<MODEL>::multiplyAD starting" << std::endl;
125 
126  ooBump_->multiplyStdDev(in, out);
127 
128  oops::Log::trace() << "StdDevVariableChange<MODEL>::multiplyAD done" << std::endl;
129 }
130 
131 template<typename MODEL>
133  oops::Log::trace() << "StdDevVariableChange<MODEL>::multiplyInverseAD starting" << std::endl;
134 
135  ooBump_->multiplyStdDevInv(in, out);
136 
137  oops::Log::trace() << "StdDevVariableChange<MODEL>::multiplyInverseAD done" << std::endl;
138 }
139 
140 template<typename MODEL>
141 void StdDevVariableChange<MODEL>::print(std::ostream & os) const {
142 }
143 
144 template<typename MODEL>
145 std::string StdDevVariableChange<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 StdDevVariableChange<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 StdDevVariableChange<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_STDDEVVARIABLECHANGE_H_
oops
Definition: ErrorCovarianceBUMP.h:33
saber::StdDevVariableChange::classname
static const std::string classname()
Definition: StdDevVariableChange.h:49
saber::ParametersBUMP
BUMP parameters.
Definition: ParametersBUMP.h:43
saber::StdDevVariableChange::ooBump_
std::unique_ptr< OoBump_ > ooBump_
Definition: StdDevVariableChange.h:72
saber::StdDevVariableChange::multiplyInverse
void multiplyInverse(const Increment_ &, Increment_ &) const override
Definition: StdDevVariableChange.h:114
saber::StdDevVariableChange::print
void print(std::ostream &) const override
Definition: StdDevVariableChange.h:141
saber::StdDevVariableChange::Increment_
oops::Increment< MODEL > Increment_
Definition: StdDevVariableChange.h:43
saber::StdDevVariableChange::multiply
void multiply(const Increment_ &, Increment_ &) const override
Definition: StdDevVariableChange.h:105
saber::StdDevVariableChange::multiplyInverseAD
void multiplyInverseAD(const Increment_ &, Increment_ &) const override
Definition: StdDevVariableChange.h:132
saber::StdDevVariableChange
Derived class of generic variable transform for statistical.
Definition: StdDevVariableChange.h:41
ParametersBUMP.h
saber
Definition: type_bump.h:22
saber::OoBump
OoBump C++ interface.
Definition: OoBump.h:47
saber::StdDevVariableChange::State_
oops::State< MODEL > State_
Definition: StdDevVariableChange.h:45
saber::StdDevVariableChange::OoBump_
OoBump< MODEL > OoBump_
Definition: StdDevVariableChange.h:44
eckit
Definition: type_bump.h:18
OoBump.h
saber::ParametersBUMP::getOoBump
OoBump_ & getOoBump()
Definition: ParametersBUMP.h:60
saber::StdDevVariableChange::~StdDevVariableChange
virtual ~StdDevVariableChange()
Definition: StdDevVariableChange.h:98
saber::StdDevVariableChange::StdDevVariableChange
StdDevVariableChange(const State_ &, const State_ &, const Geometry_ &, const eckit::Configuration &)
Definition: StdDevVariableChange.h:78
saber::StdDevVariableChange::ParametersBUMP_
ParametersBUMP< MODEL > ParametersBUMP_
Definition: StdDevVariableChange.h:46
saber::StdDevVariableChange::ExtractFilename
std::string ExtractFilename(std::string const pathString)
Definition: StdDevVariableChange.h:145
saber::StdDevVariableChange::multiplyAD
void multiplyAD(const Increment_ &, Increment_ &) const override
Definition: StdDevVariableChange.h:123
saber::StdDevVariableChange::pathStrList
const std::vector< std::string > pathStrList
Definition: StdDevVariableChange.h:61
saber::StdDevVariableChange::VariableLists
void VariableLists(const std::vector< std::string > pathStrList, std::vector< std::string > &modelVarToCalcList, std::vector< std::string > &varRegrByList)
Definition: StdDevVariableChange.h:194
saber::StdDevVariableChange::Geometry_
oops::Geometry< MODEL > Geometry_
Definition: StdDevVariableChange.h:42
saber::StdDevVariableChange::ExtractModelVarForCalc
void ExtractModelVarForCalc(std::string const fileString, std::string &modelVarToCalcString, std::string &varRegrByString)
Definition: StdDevVariableChange.h:164