UFO
ObsBiasIncrement.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2018-2021 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 #include "ufo/ObsBiasIncrement.h"
9 
10 #include <iomanip>
11 #include <memory>
12 
13 #include "eckit/mpi/Comm.h"
14 #include "ioda/ObsSpace.h"
15 #include "oops/util/Logger.h"
16 #include "ufo/ObsBias.h"
17 
18 namespace ufo {
19 
20 // -----------------------------------------------------------------------------
21 
22 ObsBiasIncrement::ObsBiasIncrement(const ioda::ObsSpace & odb,
23  const Parameters_ & params)
24  : vars_(odb.obsvariables()) {
25  oops::Log::trace() << "ufo::ObsBiasIncrement::create starting." << std::endl;
26 
27  // Predictor factory
28  for (const PredictorParametersWrapper &wrapper :
29  params.variationalBC.value().predictors.value()) {
30  std::unique_ptr<PredictorBase> predictor(PredictorFactory::create(wrapper.predictorParameters,
31  vars_));
32  prednames_.push_back(predictor->name());
33  }
34 
35  // initialize bias coefficient perturbations
36  biascoeffsinc_ = Eigen::VectorXd::Zero(prednames_.size() * vars_.size());
37 
38  oops::Log::trace() << "ufo::ObsBiasIncrement::create done." << std::endl;
39 }
40 
41 // -----------------------------------------------------------------------------
42 
44  : prednames_(other.prednames_), vars_(other.vars_) {
45  oops::Log::trace() << "ufo::ObsBiasIncrement::copy ctor starting" << std::endl;
46 
47  // Copy the bias coefficients data, or fill in with zeros
48  if (copy) {
50  } else {
51  biascoeffsinc_ = Eigen::VectorXd::Zero(prednames_.size() * vars_.size());
52  }
53 
54  oops::Log::trace() << "ufo::ObsBiasIncrement::copy ctor done." << std::endl;
55 }
56 
57 // -----------------------------------------------------------------------------
58 
59 void ObsBiasIncrement::diff(const ObsBias & b1, const ObsBias & b2) {
60  biascoeffsinc_ = b1.data() - b2.data();
61 }
62 
63 // -----------------------------------------------------------------------------
64 
66  biascoeffsinc_ = Eigen::VectorXd::Zero(prednames_.size() * vars_.size());
67 }
68 
69 // -----------------------------------------------------------------------------
70 
72  if (rhs) {
73  prednames_ = rhs.prednames_;
74  vars_ = rhs.vars_;
76  }
77  return *this;
78 }
79 
80 // -----------------------------------------------------------------------------
81 
84  return *this;
85 }
86 
87 // -----------------------------------------------------------------------------
88 
91  return *this;
92 }
93 
94 // -----------------------------------------------------------------------------
95 
97  biascoeffsinc_ *= fact;
98  return *this;
99 }
100 
101 // -----------------------------------------------------------------------------
102 
103 void ObsBiasIncrement::axpy(const double fact, const ObsBiasIncrement & rhs) {
104  biascoeffsinc_ += fact * rhs.biascoeffsinc_;
105 }
106 
107 // -----------------------------------------------------------------------------
108 
110  return biascoeffsinc_.dot(rhs.biascoeffsinc_);
111 }
112 
113 // -----------------------------------------------------------------------------
114 
115 double ObsBiasIncrement::norm() const {
116  double zz = 0.0;
117  if (biascoeffsinc_.size() > 0) {
118  zz = biascoeffsinc_.norm()/std::sqrt(biascoeffsinc_.size());
119  }
120  return zz;
121 }
122 
123 // -----------------------------------------------------------------------------
124 
125 void ObsBiasIncrement::serialize(std::vector<double> & vect) const {
126  if (this->serialSize() > 0) {
127  throw eckit::NotImplemented("ufo::ObsBiasIncrement::serialize not implemented", Here());
128  }
129 }
130 
131 // -----------------------------------------------------------------------------
132 
133 void ObsBiasIncrement::deserialize(const std::vector<double> & vect, std::size_t & index) {
134  if (this->serialSize() > 0) {
135  throw eckit::NotImplemented("ufo::ObsBiasIncrement::deserialize not implemented", Here());
136  }
137 }
138 
139 // -----------------------------------------------------------------------------
140 
141 void ObsBiasIncrement::print(std::ostream & os) const {
142  if (this->serialSize() > 0) {
143  // map bias coeffs to eigen matrix
144  Eigen::Map<const Eigen::MatrixXd>
145  coeffs(biascoeffsinc_.data(), prednames_.size(), vars_.size());
146  os << "ufo::ObsBiasIncrement::print " << std::endl;
147  os << "---------------------------------------------------------------" << std::endl;
148  for (std::size_t p = 0; p < prednames_.size(); ++p) {
149  os << std::fixed << std::setw(20) << prednames_[p]
150  << ": Min= " << std::setw(15) << std::setprecision(8)
151  << coeffs.row(p).minCoeff()
152  << ", Max= " << std::setw(15) << std::setprecision(8)
153  << coeffs.row(p).maxCoeff()
154  << ", Norm= " << std::setw(15) << std::setprecision(8)
155  << coeffs.row(p).norm()
156  << std::endl;
157  }
158  os << "---------------------------------------------------------------" << std::endl;
159  os << "ufo::ObsBiasIncrement::print done" << std::endl;
160  }
161 }
162 
163 // -----------------------------------------------------------------------------
164 
165 } // namespace ufo
const Eigen::VectorXd & data() const
Return bias correction coefficients (for variable predictors)
Contains increments to bias correction coefficients.
void serialize(std::vector< double > &) const
ObsBiasIncrement & operator-=(const ObsBiasIncrement &)
double dot_product_with(const ObsBiasIncrement &) const
void axpy(const double, const ObsBiasIncrement &)
ObsBiasIncrement & operator+=(const ObsBiasIncrement &)
Eigen::VectorXd biascoeffsinc_
Bias coefficient increments.
std::vector< std::string > prednames_
void diff(const ObsBias &, const ObsBias &)
ObsBiasIncrement & operator=(const ObsBiasIncrement &)
std::size_t serialSize() const
oops::Variables vars_
Variables that need to be bias-corrected.
ObsBiasIncrement & operator*=(const double)
ObsBiasIncrement(const ioda::ObsSpace &odb, const Parameters_ &params)
void print(std::ostream &) const
void deserialize(const std::vector< double > &, std::size_t &)
Parameters influencing the bias correction process.
oops::Parameter< StaticOrVariationalBCParameters > variationalBC
List of predictors with coefficients determined by variational analysis (VarBC).
static std::unique_ptr< PredictorBase > create(const PredictorParametersBase &parameters, const oops::Variables &vars)
Create and return a new predictor.
Contains a polymorphic parameter holding an instance of a subclass of PredictorParametersBase.
Definition: RunCRTM.h:27