OOPS
HofX3D.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2018-2020 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_HOFX3D_H_
9 #define OOPS_RUNS_HOFX3D_H_
10 
11 #include <algorithm>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include "eckit/config/LocalConfiguration.h"
18 #include "oops/base/Geometry.h"
20 #include "oops/base/Observations.h"
21 #include "oops/base/ObsSpaces.h"
22 #include "oops/base/State.h"
23 #include "oops/base/Variables.h"
26 #include "oops/interface/GeoVaLs.h"
29 #include "oops/mpi/mpi.h"
30 #include "oops/runs/Application.h"
31 #include "oops/util/ConfigFunctions.h"
32 #include "oops/util/DateTime.h"
33 #include "oops/util/Duration.h"
34 #include "oops/util/Logger.h"
35 
36 namespace oops {
37 
38 template <typename MODEL, typename OBS> class HofX3D : public Application {
49 
50  typedef std::vector<std::unique_ptr<GeoVaLs_>> GeoVaLsVec_;
51  typedef std::vector<std::unique_ptr<Locations_>> LocationsVec_;
52  typedef std::vector<Variables> VariablesVec_;
53 
54 
55  public:
56 // -----------------------------------------------------------------------------
57  explicit HofX3D(const eckit::mpi::Comm & comm = oops::mpi::world()) : Application(comm) {
58  instantiateObsFilterFactory<OBS>();
59  instantiateVariableChangeFactory<MODEL>();
60  }
61 // -----------------------------------------------------------------------------
62  virtual ~HofX3D() {}
63 // -----------------------------------------------------------------------------
64  int execute(const eckit::Configuration & fullConfig) const {
65  // Setup observation window
66  const util::Duration winlen(fullConfig.getString("window length"));
67  const util::DateTime winbgn(fullConfig.getString("window begin"));
68  const util::DateTime winend(winbgn + winlen);
69  Log::info() << "Observation window from " << winbgn << " to " << winend << std::endl;
70 
71  // Setup geometry
72  const eckit::LocalConfiguration geometryConfig(fullConfig, "geometry");
73  const Geometry_ geometry(geometryConfig, this->getComm());
74 
75  // Setup states for H(x)
76  const eckit::LocalConfiguration stateConfig(fullConfig, "state");
77  Log::info() << "State configuration is:" << stateConfig << std::endl;
78  State_ xx(geometry, stateConfig);
79  Log::test() << "State: " << xx << std::endl;
80 
81  // Setup observations
82  const eckit::LocalConfiguration obsConfig(fullConfig, "observations");
83  ObsSpaces_ obspaces(obsConfig, this->getComm(), winbgn, winend);
84  ObsAux_ obsaux(obspaces, obsConfig);
85  CalcHofX_ hofx(obspaces, obsConfig);
86  hofx.initialize(obsaux);
87 
88  // fill in GeoVaLs
89  GeoVaLsVec_ geovals;
90  const LocationsVec_ & locations = hofx.locations();
91  const VariablesVec_ & vars = hofx.requiredVars();
92  Log::debug() << "HofX3D: Required hofx size = " << hofx.requiredVars().size() << std::endl;
93 
94  Variables geovars;
95  Log::debug() << "HofX3D: Required vars size = " << vars.size() << std::endl;
96  for (size_t jj = 0; jj < vars.size(); ++jj) {
97  Log::debug() << "HofX3D: Required vars:" << vars[jj] << std::endl;
98  geovars += vars[jj];
99  }
100  Log::debug() << "HofX3D: Required variables:" << geovars << std::endl;
101  eckit::LocalConfiguration chvarconf; // empty for now
102  ChangeVariables_ chvar(chvarconf, geometry, xx.variables(), geovars);
103 
104  State_ zz(geometry, geovars, xx.validTime());
105  chvar.changeVar(xx, zz);
106 
107  std::vector<eckit::LocalConfiguration> getValuesConfig =
108  util::vectoriseAndFilter(obsConfig, "get values");
109 
110  // loop over all observation types
111  for (size_t jj = 0; jj < obspaces.size(); ++jj) {
112  GetValues_ getvals(geometry, *locations[jj], getValuesConfig[jj]);
113  geovals.emplace_back(new GeoVaLs_(*locations[jj], vars[jj],
114  geometry.variableSizes(vars[jj])));
115  getvals.fillGeoVaLs(zz, winbgn, winend, *geovals[jj]);
116  }
117 
118  // Compute H(x) on filled in geovals and run the filters
119  Observations_ yobs = hofx.compute(geovals);
120  hofx.saveQcFlags("EffectiveQC");
121  hofx.saveObsErrors("EffectiveError");
122 
123  // Save H(x)
124  Log::test() << "H(x): " << std::endl << yobs << "End H(x)" << std::endl;
125  yobs.save("hofx");
126  obspaces.save();
127 
128  return 0;
129  }
130 // -----------------------------------------------------------------------------
131  private:
132  std::string appname() const {
133  return "oops::HofX3D<" + MODEL::name() + ", " + OBS::name() + ">";
134  }
135 // -----------------------------------------------------------------------------
136 };
137 
138 } // namespace oops
139 
140 #endif // OOPS_RUNS_HOFX3D_H_
const eckit::mpi::Comm & getComm() const
Definition: Application.h:36
Computes observation operator (from GeoVaLs), applies bias correction and runs QC filters.
Definition: CalcHofX.h:50
void saveObsErrors(const std::string &) const
save obs error variances (modified in QC) to ObsSpaces
Definition: CalcHofX.h:225
void initialize(const ObsAuxCtrls_ &, const int iteration=0)
Initializes variables, obs bias, obs filters (could be different for different iterations.
Definition: CalcHofX.h:147
const LocationsVec_ & locations() const
accessor to the locations
Definition: CalcHofX.h:82
Observations_ compute(const GeoVaLsVec_ &)
Computes H(x) from the filled in GeoVaLs.
Definition: CalcHofX.h:170
void saveQcFlags(const std::string &) const
save QC flags to ObsSpaces
Definition: CalcHofX.h:216
const VariablesVec_ & requiredVars() const
accessor to variables required from the model
Definition: CalcHofX.h:84
Encapsulates the nonlinear variable change There should not be a factory for ChangeVariable,...
void changeVar(const State_ &xin, State_ &xout) const
change variable from state xin to xout
Geometry class used in oops; subclass of interface class interface::Geometry.
Gets values from model State to observation locations (fills GeoVaLs)
void fillGeoVaLs(const State_ &, const util::DateTime &, const util::DateTime &, GeoVaLs_ &) const
Get state values at observation locations.
int execute(const eckit::Configuration &fullConfig) const
Definition: HofX3D.h:64
GeoVaLs< OBS > GeoVaLs_
Definition: HofX3D.h:41
GetValues< MODEL, OBS > GetValues_
Definition: HofX3D.h:42
std::vector< std::unique_ptr< Locations_ > > LocationsVec_
Definition: HofX3D.h:51
std::vector< Variables > VariablesVec_
Definition: HofX3D.h:52
Geometry< MODEL > Geometry_
Definition: HofX3D.h:40
HofX3D(const eckit::mpi::Comm &comm=oops::mpi::world())
Definition: HofX3D.h:57
ChangeVariables< MODEL > ChangeVariables_
Definition: HofX3D.h:48
State< MODEL > State_
Definition: HofX3D.h:47
Observations< OBS > Observations_
Definition: HofX3D.h:45
ObsAuxControls< OBS > ObsAux_
Definition: HofX3D.h:44
Locations< OBS > Locations_
Definition: HofX3D.h:43
std::vector< std::unique_ptr< GeoVaLs_ > > GeoVaLsVec_
Definition: HofX3D.h:50
virtual ~HofX3D()
Definition: HofX3D.h:62
CalcHofX< OBS > CalcHofX_
Definition: HofX3D.h:39
ObsSpaces< OBS > ObsSpaces_
Definition: HofX3D.h:46
std::string appname() const
Definition: HofX3D.h:132
Locations of observations for observation operator.
Holds a vector of ObsAuxControl.
void save() const
Save files.
Definition: ObsSpaces.h:114
std::size_t size() const
Access.
Definition: ObsSpaces.h:61
Observations Class.
Definition: Observations.h:35
void save(const std::string &) const
Save/read observations values.
Definition: Observations.h:141
State class used in oops; subclass of interface class interface::State.
size_t size() const
std::vector< size_t > variableSizes(const Variables &) const
const util::DateTime validTime() const
Accessor to the time of this State.
const Variables & variables() const
Accessor to variables associated with this State.
const eckit::mpi::Comm & world()
Default communicator with all MPI tasks (ie MPI_COMM_WORLD)
Definition: oops/mpi/mpi.cc:84
The namespace for the main oops code.