OOPS
ConvertState.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_RUNS_CONVERTSTATE_H_
9 #define OOPS_RUNS_CONVERTSTATE_H_
10 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "eckit/config/LocalConfiguration.h"
19 #include "oops/interface/State.h"
20 #include "oops/mpi/mpi.h"
21 #include "oops/runs/Application.h"
22 #include "oops/util/DateTime.h"
23 #include "oops/util/Duration.h"
24 #include "oops/util/Logger.h"
25 
26 namespace oops {
27 
28 template <typename MODEL> class ConvertState : public Application {
33 
34  public:
35 // -------------------------------------------------------------------------------------------------
36  explicit ConvertState(const eckit::mpi::Comm & comm = oops::mpi::world()) : Application(comm) {
37  instantiateVariableChangeFactory<MODEL>();
38  }
39 // -------------------------------------------------------------------------------------------------
40  virtual ~ConvertState() {}
41 // -------------------------------------------------------------------------------------------------
42  int execute(const eckit::Configuration & fullConfig) const {
43 // Setup resolution for intput and output
44  const eckit::LocalConfiguration inputResolConfig(fullConfig, "input geometry");
45  const Geometry_ resol1(inputResolConfig, this->getComm());
46 
47  const eckit::LocalConfiguration outputResolConfig(fullConfig, "output geometry");
48  const Geometry_ resol2(outputResolConfig, this->getComm());
49 
50 // Variable transform(s)
51  std::vector<std::unique_ptr<VariableChange_>> chvars;
52  std::vector<bool> inverse;
53 
54  std::vector<eckit::LocalConfiguration> chvarconfs;
55  fullConfig.get("variable changes", chvarconfs);
56  for (size_t cv = 0; cv < chvarconfs.size(); ++cv) {
57  chvars.emplace_back(VariableChangeFactory_::create(chvarconfs[cv], resol2));
58  inverse.push_back(chvarconfs[cv].getBool("do inverse", false));
59  }
60 
61 // List of input and output states
62  std::vector<eckit::LocalConfiguration> statesConf;
63  fullConfig.get("states", statesConf);
64  int nstates = statesConf.size();
65 
66 // Loop over states
67  for (int jm = 0; jm < nstates; ++jm) {
68 // Print output
69  Log::info() << "Converting state " << jm+1 << " of " << nstates << std::endl;
70 
71 // Read state
72  const eckit::LocalConfiguration inputConfig(statesConf[jm], "input");
73  State_ xxi(resol1, inputConfig);
74  Log::test() << "Input state: " << xxi << std::endl;
75 
76 // Copy and change resolution
77  std::unique_ptr<State_> xx(new State_(resol2, xxi)); // Pointer that can be reset after chvar
78 
79 // Variable transform(s)
80  for (size_t cv = 0; cv < chvars.size(); ++cv) {
81  if (!inverse[cv]) {
82  State_ xchvarout = chvars[cv]->changeVar(*xx);
83  xx.reset(new State_(xchvarout));
84  } else {
85  State_ xchvarout = chvars[cv]->changeVarInverse(*xx);
86  xx.reset(new State_(xchvarout));
87  }
88  Log::test() << "State after " << *chvars[cv] << " transform: " << *xx << std::endl;
89  }
90 
91 // Write state
92  const eckit::LocalConfiguration outputConfig(statesConf[jm], "output");
93  xx->write(outputConfig);
94 
95  Log::test() << "Output state: " << *xx << std::endl;
96  }
97  return 0;
98  }
99 // -------------------------------------------------------------------------------------------------
100  private:
101  std::string appname() const {
102  return "oops::ConvertState<" + MODEL::name() + ">";
103  }
104 // -------------------------------------------------------------------------------------------------
105 };
106 
107 } // namespace oops
108 #endif // OOPS_RUNS_CONVERTSTATE_H_
oops::ConvertState::execute
int execute(const eckit::Configuration &fullConfig) const
Definition: ConvertState.h:42
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
oops::VariableChangeBase
Definition: VariableChangeBase.h:49
oops::VariableChangeFactory::create
static VariableChangeBase< MODEL > * create(const VariableChangeParametersBase &, const Geometry_ &)
Create and return a new variable change.
Definition: VariableChangeBase.h:200
oops::ConvertState::VariableChange_
VariableChangeBase< MODEL > VariableChange_
Definition: ConvertState.h:31
mpi.h
oops::VariableChangeFactory
VariableChange factory.
Definition: VariableChangeBase.h:75
oops::ConvertState::State_
State< MODEL > State_
Definition: ConvertState.h:30
oops::ConvertState::~ConvertState
virtual ~ConvertState()
Definition: ConvertState.h:40
oops::ConvertState::ConvertState
ConvertState(const eckit::mpi::Comm &comm=oops::mpi::world())
Definition: ConvertState.h:36
Application.h
instantiateVariableChangeFactory.h
oops::Application::getComm
const eckit::mpi::Comm & getComm() const
Definition: Application.h:36
oops::Geometry
Geometry class used in oops; subclass of interface class above.
Definition: oops/interface/Geometry.h:189
oops::State
Encapsulates the model state.
Definition: CostJbState.h:28
oops::Application
Definition: Application.h:29
State.h
oops::mpi::world
const eckit::mpi::Comm & world()
Default communicator with all MPI tasks (ie MPI_COMM_WORLD)
Definition: oops/mpi/mpi.cc:22
oops::ConvertState::appname
std::string appname() const
Definition: ConvertState.h:101
oops::ConvertState::Geometry_
Geometry< MODEL > Geometry_
Definition: ConvertState.h:29
VariableChangeBase.h
oops::ConvertState
Definition: ConvertState.h:28
Geometry.h
oops::ConvertState::VariableChangeFactory_
VariableChangeFactory< MODEL > VariableChangeFactory_
Definition: ConvertState.h:32