OOPS
CalcHofX.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 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 #ifndef OOPS_ASSIMILATION_CALCHOFX_H_
8 #define OOPS_ASSIMILATION_CALCHOFX_H_
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "eckit/config/LocalConfiguration.h"
15 
18 #include "oops/base/Observations.h"
19 #include "oops/base/Observers.h"
20 #include "oops/base/ObsSpaces.h"
22 #include "oops/base/QCData.h"
23 #include "oops/base/StateInfo.h"
25 #include "oops/interface/Model.h"
27 #include "oops/interface/State.h"
28 #include "oops/util/Duration.h"
29 #include "oops/util/Logger.h"
30 
31 namespace oops {
32 
33 // -----------------------------------------------------------------------------
34 
35 /// \brief Computes observation operator (while running model, or with State4D)
36 
37 template <typename MODEL, typename OBS>
38 class CalcHofX {
49  template <typename DATA> using ObsData_ = ObsDataVector<OBS, DATA>;
50 
51  public:
52 /// \brief Initializes Observers
53  CalcHofX(const ObsSpaces_ &, const Geometry_ &, const eckit::Configuration &);
54 
55 /// \brief Computes 4D H(x) (running the model)
56  const Observations_ & compute(const Model_ &, State_ &, PostProcessor_ &, const util::Duration &);
57 /// \brief Computes 4D H(x) (using State4D)
58  const Observations_ & compute(const State4D_ &);
59 
60 /// \brief saves QC flags to ObsSpaces
61  void saveQcFlags(const std::string &) const;
62 /// \brief saves obs error variances (modified in QC) to ObsSpaces
63  void saveObsErrors(const std::string &) const;
64 /// Mask out the obs errors where the passed in qc flags are > 0
65  void maskObsErrors(const QCData_ &);
66 
67  std::shared_ptr<QCData_> qc() const {return qc_;}
68 
69  private:
70 /// \brief helper method to initialize qc flags and observer
71  void initObserver();
72 
73  const eckit::LocalConfiguration obsconf_; // configuration for observer
74  const ObsSpaces_ & obspaces_; // ObsSpaces used in H(x)
75  ObsAuxCtrls_ ybias_; // obs bias
76  const Geometry_ & geometry_; // Model Geometry
77  ModelAux_ moderr_; // model bias
78  const util::DateTime winbgn_; // window for assimilation
79  const util::Duration winlen_;
80  std::shared_ptr<QCData_> qc_; // QC-related (flags and obserrors)
81  std::shared_ptr<Observers<MODEL, OBS> > pobs_; // Observer
82 };
83 
84 // -----------------------------------------------------------------------------
85 
86 template <typename MODEL, typename OBS>
87 CalcHofX<MODEL, OBS>::CalcHofX(const ObsSpaces_ & obspaces, const Geometry_ & geometry,
88  const eckit::Configuration & config) :
89  obsconf_(config), obspaces_(obspaces), ybias_(obspaces_, obsconf_),
90  geometry_(geometry), moderr_(geometry_, config.getSubConfiguration("model aux control")),
91  winbgn_(config.getString("window begin")),
92  winlen_(config.getString("window length")) {}
93 
94 // -----------------------------------------------------------------------------
95 template <typename MODEL, typename OBS>
97  qc_.reset(new QCData_(obspaces_));
98 // Setup Observers
99  pobs_.reset(new Observers<MODEL, OBS>(obsconf_, obspaces_, ybias_, *qc_));
100 }
101 
102 // -----------------------------------------------------------------------------
103 
104 template <typename MODEL, typename OBS>
106  PostProcessor_ & post, const util::Duration & length) {
107  oops::Log::trace() << "CalcHofX<MODEL, OBS>::compute (model) start" << std::endl;
108 
109  this->initObserver();
110 // run the model and compute H(x)
111  post.enrollProcessor(pobs_);
112  model.forecast(xx, moderr_, length, post);
113 
114  oops::Log::trace() << "CalcHofX<MODEL, OBS>::compute (model) done" << std::endl;
115  return pobs_->hofx();
116 }
117 
118 // -----------------------------------------------------------------------------
119 
120 template <typename MODEL, typename OBS>
122  oops::Log::trace() << "CalcHofX<MODEL, OBS>::compute (state4D) start" << std::endl;
123 
124  this->initObserver();
125  size_t nstates = xx.size();
126  util::DateTime winend = winbgn_ + winlen_;
127  util::Duration tstep = winlen_; // for a single state
128  // if using several states, compute the timestep and check that it's the same
129  // for all states
130  if (nstates > 1) {
131  tstep = xx[1].validTime() - xx[0].validTime();
132  for (size_t ii = 1; ii < xx.size(); ++ii) {
133  ASSERT(tstep == (xx[ii].validTime() - xx[ii-1].validTime()));
134  }
135  }
136  // check that initial and last states have valid times
137  ASSERT(xx[0].validTime() <= (winbgn_ + tstep/2));
138  ASSERT(xx[nstates-1].validTime() >= (winend - tstep/2));
139 
140  // run Observer looping through all the states
141  pobs_->initialize(xx[0], winend, tstep);
142  for (size_t ii = 0; ii < xx.size(); ++ii) {
143  pobs_->process(xx[ii]);
144  }
145  pobs_->finalize(xx[nstates-1]);
146 
147  oops::Log::trace() << "CalcHofX<MODEL, OBS>::compute (state4D) done" << std::endl;
148  return pobs_->hofx();
149 }
150 
151 // -----------------------------------------------------------------------------
152 
153 template <typename MODEL, typename OBS>
154 void CalcHofX<MODEL, OBS>::saveQcFlags(const std::string & name) const {
155  for (size_t jj = 0; jj < obspaces_.size(); ++jj) {
156  qc_->qcFlags(jj)->save(name);
157  }
158 }
159 
160 // -----------------------------------------------------------------------------
161 
162 template <typename MODEL, typename OBS>
163 void CalcHofX<MODEL, OBS>::saveObsErrors(const std::string & name) const {
164  for (size_t jj = 0; jj < obspaces_.size(); ++jj) {
165  qc_->obsErrors(jj)->save(name);
166  }
167 }
168 
169 // -----------------------------------------------------------------------------
170 
171 template <typename MODEL, typename OBS>
173  for (size_t jj = 0; jj < obspaces_.size(); ++jj) {
174  qc_->obsErrors(jj)->mask(*qcMask.qcFlags(jj));
175  }
176 }
177 } // namespace oops
178 
179 #endif // OOPS_ASSIMILATION_CALCHOFX_H_
oops::CalcHofX::ObsSpaces_
ObsSpaces< OBS > ObsSpaces_
Definition: CalcHofX.h:44
oops::CalcHofX::QCData_
QCData< OBS > QCData_
Definition: CalcHofX.h:48
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
oops::QCData
container for QC-related things (flags & obserrors)
Definition: QCData.h:24
oops::CalcHofX::ModelAux_
ModelAuxControl< MODEL > ModelAux_
Definition: CalcHofX.h:41
oops::CalcHofX::Geometry_
Geometry< MODEL > Geometry_
Definition: CalcHofX.h:39
oops::ObsAuxControls
Definition: ObsAuxControls.h:26
oops::CalcHofX::obspaces_
const ObsSpaces_ & obspaces_
Definition: CalcHofX.h:74
QCData.h
oops::CalcHofX
Computes observation operator (while running model, or with State4D)
Definition: CalcHofX.h:38
ObsSpaces.h
oops::CalcHofX::Observations_
Observations< OBS > Observations_
Definition: CalcHofX.h:43
oops::CalcHofX::Model_
Model< MODEL > Model_
Definition: CalcHofX.h:40
model
Definition: l95/defaults/model.py:1
oops::CalcHofX::saveQcFlags
void saveQcFlags(const std::string &) const
saves QC flags to ObsSpaces
Definition: CalcHofX.h:154
oops::ModelAuxControl
Definition: oops/interface/ModelAuxControl.h:35
Observers.h
oops::Observers
Computes observation equivalent during model run.
Definition: Observers.h:37
oops::CalcHofX::CalcHofX
CalcHofX(const ObsSpaces_ &, const Geometry_ &, const eckit::Configuration &)
Initializes Observers.
Definition: CalcHofX.h:87
ObsAuxControls.h
oops::CalcHofX::compute
const Observations_ & compute(const Model_ &, State_ &, PostProcessor_ &, const util::Duration &)
Computes 4D H(x) (running the model)
Definition: CalcHofX.h:105
Observations.h
oops::PostProcessor::enrollProcessor
void enrollProcessor(PostBase_ *pp)
Definition: PostProcessor.h:38
oops::CalcHofX::State4D_
State4D< MODEL > State4D_
Definition: CalcHofX.h:46
PostProcessor.h
oops::CalcHofX::maskObsErrors
void maskObsErrors(const QCData_ &)
Mask out the obs errors where the passed in qc flags are > 0.
Definition: CalcHofX.h:172
oops::CalcHofX::winbgn_
const util::DateTime winbgn_
Definition: CalcHofX.h:78
oops::CalcHofX::saveObsErrors
void saveObsErrors(const std::string &) const
saves obs error variances (modified in QC) to ObsSpaces
Definition: CalcHofX.h:163
oops::ObsDataVector
Definition: ObsDataVector.h:28
oops::State4D
Four dimensional state.
Definition: State4D.h:35
oops::Geometry
Geometry class used in oops; subclass of interface class above.
Definition: oops/interface/Geometry.h:189
Model.h
oops::State4D::size
size_t size() const
Definition: State4D.h:53
oops::CalcHofX::moderr_
ModelAux_ moderr_
Definition: CalcHofX.h:77
oops::CalcHofX::State_
State< MODEL > State_
Definition: CalcHofX.h:45
oops::CalcHofX::qc
std::shared_ptr< QCData_ > qc() const
Definition: CalcHofX.h:67
oops::CalcHofX::ObsAuxCtrls_
ObsAuxControls< OBS > ObsAuxCtrls_
Definition: CalcHofX.h:42
State4D.h
oops::CalcHofX::ybias_
ObsAuxCtrls_ ybias_
Definition: CalcHofX.h:75
oops::CalcHofX::qc_
std::shared_ptr< QCData_ > qc_
Definition: CalcHofX.h:80
oops::Observations
Observations Class.
Definition: oops/base/Departures.h:30
StateInfo.h
oops::State
Encapsulates the model state.
Definition: CostJbState.h:28
oops::PostProcessor
Control model post processing.
Definition: PostProcessor.h:30
State.h
oops::CalcHofX::PostProcessor_
PostProcessor< State_ > PostProcessor_
Definition: CalcHofX.h:47
oops::ObsSpaces
Definition: ObsSpaces.h:41
oops::CalcHofX::geometry_
const Geometry_ & geometry_
Definition: CalcHofX.h:76
oops::CalcHofX::obsconf_
const eckit::LocalConfiguration obsconf_
Definition: CalcHofX.h:73
oops::CalcHofX::initObserver
void initObserver()
helper method to initialize qc flags and observer
Definition: CalcHofX.h:96
oops::Model
Encapsulates the nonlinear forecast model Note: to see methods that need to be implemented in the for...
Definition: oops/interface/Model.h:54
ModelAuxControl.h
oops::QCData::qcFlags
const ObsDataPtr_< int > qcFlags(const size_t ii) const
accessor to QC flag
Definition: QCData.h:35
Geometry.h
oops::CalcHofX::pobs_
std::shared_ptr< Observers< MODEL, OBS > > pobs_
Definition: CalcHofX.h:81
oops::CalcHofX::winlen_
const util::Duration winlen_
Definition: CalcHofX.h:79