OOPS
ObsVec1D.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
3  * (C) Copyright 2020-2020 UCAR.
4  *
5  * This software is licensed under the terms of the Apache Licence Version 2.0
6  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
7  * In applying this licence, ECMWF does not waive the privileges and immunities
8  * granted to it by virtue of its status as an intergovernmental organisation nor
9  * does it submit to any jurisdiction.
10  */
11 
12 #include "lorenz95/ObsVec1D.h"
13 
14 #include <math.h>
15 #include <limits>
16 
17 #include "eckit/config/Configuration.h"
18 #include "eckit/exception/Exceptions.h"
19 #include "lorenz95/ObsTableView.h"
20 #include "oops/util/Logger.h"
21 #include "oops/util/missingValues.h"
22 
23 namespace lorenz95 {
24 // -----------------------------------------------------------------------------
26  const std::string & name, const bool fail)
27  : obsdb_(ot), data_(ot.nobs()), missing_(util::missingValue(missing_))
28 {
29  for (double & val : data_) { val = 0.0; }
30  if (!name.empty()) {
31  if (fail || obsdb_.has(name)) obsdb_.getdb(name, data_);
32  }
33 }
34 // -----------------------------------------------------------------------------
36  : obsdb_(other.obsdb_), data_(other.data_.size()), missing_(util::missingValue(missing_))
37 {
38  data_ = other.data_;
39 }
40 // -----------------------------------------------------------------------------
42  ASSERT(data_.size() == rhs.data_.size());
43  data_ = rhs.data_;
44  return *this;
45 }
46 
47 // -----------------------------------------------------------------------------
48 ObsVec1D::ObsVec1D(const ObsTableView & ot, const ObsVec1D & other)
49  : obsdb_(ot), data_(ot.nobs()), missing_(util::missingValue(missing_)) {
50  for (size_t ii = 0; ii < ot.nobs(); ++ii) {
51  data_[ii] = other[ot.index(ii)];
52  }
53 }
54 // -----------------------------------------------------------------------------
55 ObsVec1D & ObsVec1D::operator*= (const double & zz) {
56  for (double & val : data_) {
57  if (val != missing_) val *= zz;
58  }
59  return *this;
60 }
61 // -----------------------------------------------------------------------------
63  ASSERT(data_.size() == rhs.data_.size());
64  for (size_t jj = 0; jj < data_.size(); ++jj) {
65  if (data_[jj] == missing_ || rhs.data_[jj] == missing_) {
66  data_[jj] = missing_;
67  } else {
68  data_[jj] += rhs.data_[jj];
69  }
70  }
71  return *this;
72 }
73 // -----------------------------------------------------------------------------
75  ASSERT(data_.size() == rhs.data_.size());
76  for (size_t jj = 0; jj < data_.size(); ++jj) {
77  if (data_[jj] == missing_ || rhs.data_[jj] == missing_) {
78  data_[jj] = missing_;
79  } else {
80  data_[jj] -= rhs.data_[jj];
81  }
82  }
83  return *this;
84 }
85 // -----------------------------------------------------------------------------
87  ASSERT(data_.size() == rhs.data_.size());
88  for (size_t jj = 0; jj < data_.size(); ++jj) {
89  if (data_[jj] == missing_ || rhs.data_[jj] == missing_) {
90  data_[jj] = missing_;
91  } else {
92  data_[jj] *= rhs.data_[jj];
93  }
94  }
95  return *this;
96 }
97 // -----------------------------------------------------------------------------
99  ASSERT(data_.size() == rhs.data_.size());
100  for (size_t jj = 0; jj < data_.size(); ++jj) {
101  if (data_[jj] == missing_ || rhs.data_[jj] == missing_) {
102  data_[jj] = missing_;
103  } else {
104  data_[jj] /= rhs.data_[jj];
105  }
106  }
107  return *this;
108 }
109 // -----------------------------------------------------------------------------
111  for (double & val : data_) val = 0.0;
112 }
113 // -----------------------------------------------------------------------------
115  for (double & val : data_) {
116  if (val != missing_) val = 1.0/val;
117  }
118 }
119 // -----------------------------------------------------------------------------
120 void ObsVec1D::axpy(const double & zz, const ObsVec1D & rhs) {
121  ASSERT(data_.size() == rhs.data_.size());
122  for (size_t jj = 0; jj < data_.size(); ++jj) {
123  if (data_[jj] == missing_ || rhs.data_[jj] == missing_) {
124  data_[jj] = missing_;
125  } else {
126  data_[jj] += zz * rhs.data_[jj];
127  }
128  }
129 }
130 // -----------------------------------------------------------------------------
133 }
134 // -----------------------------------------------------------------------------
135 double ObsVec1D::dot_product_with(const ObsVec1D & other) const {
136  ASSERT(data_.size() == other.data_.size());
137  double zz = 0.0;
138  for (size_t jj = 0; jj < data_.size(); ++jj) {
139  if ((data_[jj] != missing_) && (other.data_[jj] != missing_)) {
140  zz += data_[jj] * other.data_[jj];
141  }
142  }
143  return zz;
144 }
145 // -----------------------------------------------------------------------------
146 double ObsVec1D::rms() const {
147  double zz = 0.0;
148  double iobs = 0.0;
149  for (const double & val : data_) {
150  if (val != missing_) {
151  zz += val * val;
152  iobs += 1.0;
153  }
154  }
155  if (iobs > 0.0) zz = sqrt(zz/iobs);
156  return zz;
157 }
158 // -----------------------------------------------------------------------------
159 unsigned int ObsVec1D::nobs() const {
160  return data_.size() - std::count(data_.begin(), data_.end(), missing_);
161 }
162 // -----------------------------------------------------------------------------
163 void ObsVec1D::mask(const ObsData1D<int> & mask) {
164  for (size_t jj = 0; jj < data_.size(); ++jj) {
165  if (mask[jj]) data_.at(jj) = missing_;
166  }
167 }
168 // -----------------------------------------------------------------------------
169 void ObsVec1D::save(const std::string & name) const {
170  obsdb_.putdb(name, data_);
171 }
172 // -----------------------------------------------------------------------------
173 Eigen::VectorXd ObsVec1D::packEigen() const {
174  Eigen::VectorXd vec(nobs());
175  size_t ii = 0;
176  for (const double & val : data_) {
177  if (val != missing_) {
178  vec(ii++) = val;
179  }
180  }
181  ASSERT(ii == nobs());
182  return vec;
183 }
184 // -----------------------------------------------------------------------------
185 void ObsVec1D::read(const std::string & name) {
186  obsdb_.getdb(name, data_);
187 }
188 // -----------------------------------------------------------------------------
189 void ObsVec1D::print(std::ostream & os) const {
190  double zmin = std::numeric_limits<double>::max();
191  double zmax = std::numeric_limits<double>::lowest();
192  double zavg = 0.0;
193  size_t iobs = 0;
194  for (const double & val : data_) {
195  if (val != missing_) {
196  if (val < zmin) zmin = val;
197  if (val > zmax) zmax = val;
198  zavg += val;
199  ++iobs;
200  }
201  }
202  if (iobs > 0) {
203  zavg /= static_cast<double>(iobs);
204  os << "Lorenz 95 nobs= " << iobs << " Min=" << zmin << ", Max=" << zmax
205  << ", Average=" << zavg;
206  } else {
207  os << "Lorenz 95 : No observations";
208  }
209 }
210 // -----------------------------------------------------------------------------
211 } // namespace lorenz95
ObsTableView.h
lorenz95::ObsVec1D::operator*=
ObsVec1D & operator*=(const double &)
Definition: ObsVec1D.cc:55
lorenz95::ObsVec1D::nobs
unsigned int nobs() const
Definition: ObsVec1D.cc:159
lorenz95::ObsVec1D::save
void save(const std::string &) const
Definition: ObsVec1D.cc:169
lorenz95::ObsVec1D::missing_
const double missing_
Definition: ObsVec1D.h:73
lorenz95::ObsTableView::getdb
void getdb(const std::string &, std::vector< int > &) const
Definition: ObsTableView.cc:115
lorenz95::ObsVec1D
Vector in observation space.
Definition: ObsVec1D.h:34
lorenz95::ObsVec1D::zero
void zero()
Definition: ObsVec1D.cc:110
lorenz95::ObsVec1D::rms
double rms() const
Definition: ObsVec1D.cc:146
lorenz95::ObsVec1D::ObsVec1D
ObsVec1D(const ObsTableView &, const std::string &name="", const bool fail=true)
Definition: ObsVec1D.cc:25
lorenz95::ObsTableView::index
size_t index(const size_t ii) const
Definition: ObsTableView.h:63
lorenz95::ObsVec1D::operator+=
ObsVec1D & operator+=(const ObsVec1D &)
Definition: ObsVec1D.cc:62
ObsVec1D.h
lorenz95::ObsTableView::random
void random(std::vector< double > &) const
Definition: ObsTableView.cc:151
lorenz95::ObsVec1D::random
void random()
Definition: ObsVec1D.cc:131
lorenz95::ObsVec1D::obsdb_
const ObsTableView & obsdb_
Definition: ObsVec1D.h:71
lorenz95::ObsVec1D::dot_product_with
double dot_product_with(const ObsVec1D &) const
Definition: ObsVec1D.cc:135
lorenz95::ObsVec1D::packEigen
Eigen::VectorXd packEigen() const
Definition: ObsVec1D.cc:173
lorenz95::ObsTableView::nobs
unsigned int nobs() const
Definition: ObsTableView.cc:158
run_time_test.val
val
Definition: run_time_test.py:28
lorenz95::ObsVec1D::invert
void invert()
Definition: ObsVec1D.cc:114
lorenz95::ObsVec1D::data_
std::vector< double > data_
Definition: ObsVec1D.h:72
lorenz95::ObsData1D
Data in observation space.
Definition: BackgroundCheck.h:24
lorenz95::ObsTableView::has
bool has(const std::string &) const
Definition: ObsTableView.cc:63
lorenz95::ObsTableView
A Simple Observation Data Handler.
Definition: ObsTableView.h:38
lorenz95::ObsVec1D::read
void read(const std::string &)
Definition: ObsVec1D.cc:185
lorenz95::ObsVec1D::mask
void mask(const ObsData1D< int > &)
Definition: ObsVec1D.cc:163
lorenz95::ObsVec1D::operator=
ObsVec1D & operator=(const ObsVec1D &)
Definition: ObsVec1D.cc:41
lorenz95::ObsVec1D::operator/=
ObsVec1D & operator/=(const ObsVec1D &)
Definition: ObsVec1D.cc:98
lorenz95::ObsVec1D::axpy
void axpy(const double &, const ObsVec1D &)
Definition: ObsVec1D.cc:120
lorenz95::ObsTableView::putdb
void putdb(const std::string &, const std::vector< int > &) const
Definition: ObsTableView.cc:70
lorenz95::ObsVec1D::operator-=
ObsVec1D & operator-=(const ObsVec1D &)
Definition: ObsVec1D.cc:74
lorenz95
The namespace for the L95 model.
Definition: l95/src/lorenz95/AnalyticInit.cc:17
lorenz95::ObsVec1D::print
void print(std::ostream &) const
Definition: ObsVec1D.cc:189
util
Definition: ObservationL95.h:32