FV3 Bundle
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 OOPS_GENERIC_STATSVARIABLECHANGE_H_
9 #define OOPS_GENERIC_STATSVARIABLECHANGE_H_
10 
11 #include <string>
12 #include <vector>
13 #include <boost/noncopyable.hpp>
14 #include "eckit/config/Configuration.h"
16 #include "oops/base/Variables.h"
17 #include "oops/generic/oobump_f.h"
21 #include "oops/interface/State.h"
22 
23 namespace eckit {
24  class Configuration;
25 }
26 
27 namespace oops {
28 
29 // -----------------------------------------------------------------------------
30 
31 /// Derived class of generic variable transform for statistical
32 
33 template <typename MODEL>
38 
39  public:
40  static const std::string classname() {return "oops::StatsVariableChange";}
41 
42  StatsVariableChange(const State_ &, const State_ &,
43  const Geometry_ &, const eckit::Configuration &);
44  virtual ~StatsVariableChange();
45 
46  void multiply(const Increment_ &, Increment_ &) const override;
47  void multiplyInverse(const Increment_ &, Increment_ &) const override;
48  void multiplyAD(const Increment_ &, Increment_ &) const override;
49  void multiplyInverseAD(const Increment_ &, Increment_ &) const override;
50 
51  private:
52  const std::vector<std::string> pathStrList;
53 
54  void print(std::ostream &) const override;
55  std::string ExtractFilename(std::string const pathString);
56  void ExtractModelVarForCalc(std::string const fileString,
57  std::string& modelVarToCalcString,
58  std::string& varRegrByString);
59  void VariableLists(const std::vector<std::string> pathStrList,
60  std::vector<std::string>& modelVarToCalcList,
61  std::vector<std::string>& varRegrByList);
62 
64  int keyBUMP_;
65 
66  // StatsVarData populate(const varin_ , const varout_, const eckit::Configuration);
67 };
68 
69 template<typename MODEL>
71  const Geometry_ & resol,
72  const eckit::Configuration & conf)
73  : LinearVariableChangeBase<MODEL>(conf), colocated_(1), keyBUMP_(0)
74 {
75  Log::trace() << "StatsVariableChange<MODEL>::StatsVariableChange starting" << std::endl;
76 
77  const eckit::Configuration * fconf = &conf;
78 
79 // Setup variables
80  std::cout << conf << std::endl;
81  const eckit::LocalConfiguration varConfig(conf, "variables");
82  const Variables vars(varConfig);
83 
84 // Setup dummy increment
85  Increment_ dx(resol, vars, xb.validTime());
86 
87 // Define unstructured grid coordinates
89  dx.ug_coord(ug, colocated_);
90 
91 // Create BUMP
92  create_oobump_f90(keyBUMP_, ug.toFortran(), &fconf, 0, 1, 0, 1);
93 
94 // Run BUMP
96 
97 // Copy test
98  std::ifstream infile("bump.test");
99  std::string line;
100  while (std::getline(infile, line)) Log::test() << line << std::endl;
101  remove("bump.test");
102 
103  Log::trace() << "StatsVariableChange<MODEL>::StatsVariableChange done" << std::endl;
104 }
105 
106 template<typename MODEL>
108  Log::trace() << "StatsVariableChange<MODEL>::~StatsVariableChange starting" << std::endl;
109  Log::trace() << "StatsVariableChange<MODEL>::~StatsVariableChange done" << std::endl;
110 }
111 
112 template<typename MODEL>
114  Log::trace() << "StatsVariableChange<MODEL>::multiply starting" << std::endl;
115 
116  UnstructuredGrid ug;
117  in.field_to_ug(ug, colocated_);
118  multiply_oobump_vbal_f90(keyBUMP_, ug.toFortran());
119  out.field_from_ug(ug);
120 
121  Log::trace() << "StatsVariableChange<MODEL>::multiply done" << std::endl;
122 }
123 
124 template<typename MODEL>
126  Log::trace() << "StatsVariableChange<MODEL>::multiplyInverse starting" << std::endl;
127 
128  UnstructuredGrid ug;
129  in.field_to_ug(ug, colocated_);
130  multiply_oobump_vbal_inv_f90(keyBUMP_, ug.toFortran());
131  out.field_from_ug(ug);
132 
133  Log::trace() << "StatsVariableChange<MODEL>::multiplyInverse done" << std::endl;
134 }
135 
136 template<typename MODEL>
138  Log::trace() << "StatsVariableChange<MODEL>::multiplyAD starting" << std::endl;
139 
140  UnstructuredGrid ug;
141  in.field_to_ug(ug, colocated_);
142  multiply_oobump_vbal_ad_f90(keyBUMP_, ug.toFortran());
143  out.field_from_ug(ug);
144 
145  Log::trace() << "StatsVariableChange<MODEL>::multiplyAD done" << std::endl;
146 }
147 
148 template<typename MODEL>
150  Log::trace() << "StatsVariableChange<MODEL>::multiplyInverseAD starting" << std::endl;
151 
152  UnstructuredGrid ug;
153  in.field_to_ug(ug, colocated_);
155  out.field_from_ug(ug);
156 
157  Log::trace() << "StatsVariableChange<MODEL>::multiplyInverseAD done" << std::endl;
158 }
159 
160 template<typename MODEL>
161 void StatsVariableChange<MODEL>::print(std::ostream & os) const {
162 }
163 
164 template<typename MODEL>
165 std::string StatsVariableChange<MODEL>::ExtractFilename(std::string const pathString)
166 {
167  int tempIndex;
168  for (int iCharIndex = pathString.length(); iCharIndex > 0; --iCharIndex)
169  {
170  if (pathString.substr(iCharIndex - 1, 1) == "/")
171  {
172  return pathString.substr(iCharIndex, pathString.length() - iCharIndex);
173  }
174  tempIndex = iCharIndex;
175  }
176  std::cout << "ExtractFileName: tempIndex = " << tempIndex << std::endl;
177  throw std::runtime_error("FileName not extracted from path " + pathString);
178 }
179 
180 // extract {model_variable_to_be_calculated} string and {variable_regressed_by} string
181 // from fileString
182 
183 template<typename MODEL>
184 void StatsVariableChange<MODEL>::ExtractModelVarForCalc(std::string const fileString,
185  std::string& modelVarToCalcString,
186  std::string& varRegrByString)
187 {
188 // FileName is of the form {modelVarToCalcString}__{varRegrByString}__reg.nc
189 // find first "__" and the last "__" to find the appropriate strings
190  int firstUnderscoreIndex, lastUnderscoreIndex;
191  for (int iCharIndex = 0; iCharIndex < fileString.length() - 1; ++iCharIndex)
192  {
193  firstUnderscoreIndex = iCharIndex;
194  if (fileString.substr(iCharIndex, 2) == "__")
195  {
196  break;
197  }
198  }
199  for (int iCharIndex = fileString.length(); iCharIndex > 0; --iCharIndex)
200  {
201  lastUnderscoreIndex = iCharIndex - 1;
202  if (fileString.substr(iCharIndex-1, 2) == "__")
203  {
204  break;
205  }
206  }
207  modelVarToCalcString = fileString.substr(0, firstUnderscoreIndex);
208  varRegrByString = fileString.substr(firstUnderscoreIndex + 2,
209  lastUnderscoreIndex - firstUnderscoreIndex - 2);
210 }
211 
212 // append the modelVarToCalcList and varRegrByList
213 template<typename MODEL>
214 void StatsVariableChange<MODEL>::VariableLists(const std::vector<std::string> pathStrList,
215  std::vector<std::string>& modelVarToCalcList,
216  std::vector<std::string>& varRegrByList)
217 {
218  std::string modelVarToCalcString;
219  std::string varRegrByString;
220  std::string filename;
221  for (auto pathString : pathStrList)
222  {
223  modelVarToCalcString.clear();
224  varRegrByString.clear();
225  filename = ExtractFilename(pathString);
226  ExtractModelVarForCalc(filename, modelVarToCalcString, varRegrByString);
227  modelVarToCalcList.push_back(modelVarToCalcString);
228  varRegrByList.push_back(varRegrByString);
229  }
230 }
231 
232 
233 } // namespace oops
234 
235 #endif // OOPS_GENERIC_STATSVARIABLECHANGE_H_
void multiplyInverse(const Increment_ &, Increment_ &) const override
void ExtractModelVarForCalc(std::string const fileString, std::string &modelVarToCalcString, std::string &varRegrByString)
static const std::string classname()
void multiply(const Increment_ &, Increment_ &) const override
Definition: conf.py:1
void multiply_oobump_vbal_inv_ad_f90(const int &, const int &)
void multiply_oobump_vbal_f90(const int &, const int &)
StatsVariableChange(const State_ &, const State_ &, const Geometry_ &, const eckit::Configuration &)
Encapsulates the model state.
program test
The namespace for the main oops code.
void create_oobump_f90(int &, const int &, const eckit::Configuration *const *, const int &, const int &, const int &, const int &)
void multiply_oobump_vbal_ad_f90(const int &, const int &)
Derived class of generic variable transform for statistical.
void field_to_ug(UnstructuredGrid &, const int &) const
real(fp), parameter xb
Definition: ufo_aod_mod.F90:41
Increment< MODEL > Increment_
Base class for generic variable transform.
std::string ExtractFilename(std::string const pathString)
void multiplyAD(const Increment_ &, Increment_ &) const override
Increment Class: Difference between two states.
void multiplyInverseAD(const Increment_ &, Increment_ &) const override
void run_oobump_drivers_f90(const int &)
void VariableLists(const std::vector< std::string > pathStrList, std::vector< std::string > &modelVarToCalcList, std::vector< std::string > &varRegrByList)
void print(std::ostream &) const override
const std::vector< std::string > pathStrList
void multiply_oobump_vbal_inv_f90(const int &, const int &)