OOPS
ObsTableView.cc
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 
9 
10 #include <algorithm>
11 #include <cmath>
12 #include <iostream>
13 #include <numeric>
14 #include <vector>
15 
16 #include "eckit/config/LocalConfiguration.h"
17 #include "eckit/types/Types.h"
18 
19 #include "oops/util/Logger.h"
20 #include "oops/util/missingValues.h"
21 
22 // -----------------------------------------------------------------------------
23 namespace lorenz95 {
24 
25 ObsTableView::ObsTableView(const eckit::Configuration & config, const eckit::mpi::Comm & comm,
26  const util::DateTime & bgn, const util::DateTime & end,
27  const eckit::mpi::Comm & time)
28  : obstable_(new ObsTable(config, comm, bgn, end, time)),
29  localobs_(obstable_->nobs()), obsdist_(obstable_->nobs(), 0.0)
30 {
31  std::iota(localobs_.begin(), localobs_.end(), 0);
32  oops::Log::trace() << "ObsTableView::ObsTableView created nobs = " << nobs() << std::endl;
33 }
34 
35 // -----------------------------------------------------------------------------
36 
38  const eckit::geometry::Point2 & center,
39  const eckit::Configuration & conf)
40  : obstable_(obstable.obstable_), localobs_(), obsdist_()
41 {
42  std::vector<double> locations = obstable.locations();
43  const double dist = conf.getDouble("lengthscale");
44  for (unsigned int jj = 0; jj < obstable.nobs(); ++jj) {
45  double curdist = std::abs(center[0] - locations[jj]);
46  curdist = std::min(curdist, 1.-curdist);
47  if ( curdist < dist ) {
48  localobs_.push_back(jj);
49  obsdist_.push_back(curdist);
50  }
51  }
52  oops::Log::trace() << "ObsTableView::ObsTableView created" << std::endl;
53 }
54 
55 // -----------------------------------------------------------------------------
56 
58  oops::Log::trace() << "ObsTableView::ObsTableView destructed" << std::endl;
59 }
60 
61 // -----------------------------------------------------------------------------
62 
63 bool ObsTableView::has(const std::string & col) const {
64  oops::Log::trace() << "ObsTableView::has" << std::endl;
65  return obstable_->has(col);
66 }
67 
68 // -----------------------------------------------------------------------------
69 
70 void ObsTableView::putdb(const std::string & col, const std::vector<int> & vec) const {
71  int missing;
72  std::vector<int> fullvec(obstable_->nobs(), util::missingValue(missing));
73  if (obstable_->has(col)) {
74  obstable_->getdb(col, fullvec);
75  }
76  for (unsigned int i = 0; i < nobs(); i++) {
77  fullvec[localobs_[i]] = vec[i];
78  }
79  obstable_->putdb(col, fullvec);
80  oops::Log::trace() << "ObsTableView::putdb done" << std::endl;
81 }
82 
83 // -----------------------------------------------------------------------------
84 
85 void ObsTableView::putdb(const std::string & col, const std::vector<float> & vec) const {
86  float missing;
87  std::vector<float> fullvec(obstable_->nobs(), util::missingValue(missing));
88  if (obstable_->has(col)) {
89  obstable_->getdb(col, fullvec);
90  }
91  for (unsigned int i = 0; i < nobs(); i++) {
92  fullvec[localobs_[i]] = vec[i];
93  }
94  obstable_->putdb(col, fullvec);
95  oops::Log::trace() << "ObsTableView::putdb done" << std::endl;
96 }
97 
98 // -----------------------------------------------------------------------------
99 
100 void ObsTableView::putdb(const std::string & col, const std::vector<double> & vec) const {
101  double missing;
102  std::vector<double> fullvec(obstable_->nobs(), util::missingValue(missing));
103  if (obstable_->has(col)) {
104  obstable_->getdb(col, fullvec);
105  }
106  for (unsigned int i = 0; i < nobs(); i++) {
107  fullvec[localobs_[i]] = vec[i];
108  }
109  obstable_->putdb(col, fullvec);
110  oops::Log::trace() << "ObsTableView::putdb done" << std::endl;
111 }
112 
113 // -----------------------------------------------------------------------------
114 
115 void ObsTableView::getdb(const std::string & col, std::vector<int> & vec) const {
116  std::vector<int> fullvec;
117  obstable_->getdb(col, fullvec);
118  vec.resize(nobs());
119  for (unsigned int i = 0; i < nobs(); i++) {
120  vec[i] = fullvec[localobs_[i]];
121  }
122  oops::Log::trace() << "ObsTableView::getdb done" << std::endl;
123 }
124 
125 // -----------------------------------------------------------------------------
126 
127 void ObsTableView::getdb(const std::string & col, std::vector<float> & vec) const {
128  std::vector<float> fullvec;
129  obstable_->getdb(col, fullvec);
130  vec.resize(nobs());
131  for (unsigned int i = 0; i < nobs(); i++) {
132  vec[i] = fullvec[localobs_[i]];
133  }
134  oops::Log::trace() << "ObsTableView::getdb done" << std::endl;
135 }
136 
137 // -----------------------------------------------------------------------------
138 
139 void ObsTableView::getdb(const std::string & col, std::vector<double> & vec) const {
140  std::vector<double> fullvec;
141  obstable_->getdb(col, fullvec);
142  vec.resize(nobs());
143  for (unsigned int i = 0; i < nobs(); i++) {
144  vec[i] = fullvec[localobs_[i]];
145  }
146  oops::Log::trace() << "ObsTableView::getdb done" << std::endl;
147 }
148 
149 // -----------------------------------------------------------------------------
150 
151 void ObsTableView::random(std::vector<double> & v) const {
152  obstable_->random(v);
153  oops::Log::trace() << "ObsTableView::random done" << std::endl;
154 }
155 
156 // -----------------------------------------------------------------------------
157 
158 unsigned int ObsTableView::nobs() const {
159  return localobs_.size();
160  oops::Log::trace() << "ObsTableView::nobs done" << std::endl;
161 }
162 
163 // -----------------------------------------------------------------------------
164 
165 std::vector<double> ObsTableView::locations() const {
166  std::vector<double> full = obstable_->locations();
167  std::vector<double> local(nobs());
168  for (unsigned int i = 0; i < nobs(); i++) {
169  local[i] = full[localobs_[i]];
170  }
171  oops::Log::trace() << "ObsTableView::locations done" << std::endl;
172  return local;
173 }
174 
175 // -----------------------------------------------------------------------------
176 
177 void ObsTableView::generateDistribution(const eckit::Configuration & conf) {
178  obstable_->generateDistribution(conf);
179  int nobs = obstable_->nobs();
180  for (int i = 0; i < nobs; i++)
181  localobs_.push_back(i);
182  oops::Log::trace() << "ObsTableView::generateDistribution done" << std::endl;
183 }
184 
185 // -----------------------------------------------------------------------------
186 
187 std::unique_ptr<LocsL95> ObsTableView::locations(const util::DateTime & t1,
188  const util::DateTime & t2) const {
189  // get times and locations from the obsspace
190  std::vector<util::DateTime> all_times = obstable_->times();
191  std::vector<double> all_locs = obstable_->locations();
192  // find local times that are within t1 and t2
193  std::vector<int> mask;
194  for (unsigned int i = 0; i < nobs(); i++) {
195  if (all_times[localobs_[i]] > t1 && all_times[localobs_[i]] <= t2)
196  mask.push_back(i);
197  }
198  // set up locations
199  const unsigned int nobs_t = mask.size();
200  std::vector<double> locs(nobs_t);
201  std::vector<util::DateTime> times(nobs_t);
202  for (unsigned int i = 0; i < nobs_t; i++) {
203  locs[i] = all_locs[localobs_[mask[i]]];
204  times[i] = all_times[localobs_[mask[i]]];
205  }
206  oops::Log::trace() << "ObsTableView::locations done" << std::endl;
207  return std::unique_ptr<LocsL95>(new LocsL95(locs, times));
208 }
209 
210 // -----------------------------------------------------------------------------
211 
212 void ObsTableView::printJo(const ObsVec1D & x1, const ObsVec1D & x2) {
213  oops::Log::info() << "ObsTableView::printJo not implemented" << std::endl;
214 }
215 
216 // -----------------------------------------------------------------------------
217 
218 void ObsTableView::print(std::ostream & os) const {
219  os << "Local observation indices: " << localobs_ << std::endl;
220 }
221 
222 // -----------------------------------------------------------------------------
223 
224 } // namespace lorenz95
lorenz95::ObsTable
A Simple Observation Data Handler.
Definition: ObsTable.h:42
ObsTableView.h
lorenz95::ObsTableView::obsdist_
std::vector< double > obsdist_
Definition: ObsTableView.h:74
lorenz95::ObsTableView::locations
std::vector< double > locations() const
Definition: ObsTableView.cc:165
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::ObsTableView::print
void print(std::ostream &) const
Definition: ObsTableView.cc:218
lorenz95::ObsTableView::ObsTableView
ObsTableView(const eckit::Configuration &, const eckit::mpi::Comm &, const util::DateTime &, const util::DateTime &, const eckit::mpi::Comm &)
Definition: ObsTableView.cc:25
lorenz95::ObsTableView::printJo
void printJo(const ObsVec1D &, const ObsVec1D &)
Definition: ObsTableView.cc:212
lorenz95::ObsTableView::random
void random(std::vector< double > &) const
Definition: ObsTableView.cc:151
lorenz95::LocsL95
LocsL95 class to handle locations for L95 model.
Definition: LocsL95.h:32
lorenz95::ObsTableView::obstable_
std::shared_ptr< ObsTable > obstable_
Definition: ObsTableView.h:72
lorenz95::ObsTableView::nobs
unsigned int nobs() const
Definition: ObsTableView.cc:158
lorenz95::ObsTableView::has
bool has(const std::string &) const
Definition: ObsTableView.cc:63
lorenz95::ObsTableView::~ObsTableView
~ObsTableView()
Definition: ObsTableView.cc:57
lorenz95::ObsTableView
A Simple Observation Data Handler.
Definition: ObsTableView.h:38
lorenz95::ObsTableView::localobs_
std::vector< size_t > localobs_
Definition: ObsTableView.h:73
lorenz95::ObsTableView::generateDistribution
void generateDistribution(const eckit::Configuration &)
Definition: ObsTableView.cc:177
lorenz95::ObsTableView::putdb
void putdb(const std::string &, const std::vector< int > &) const
Definition: ObsTableView.cc:70
lorenz95
The namespace for the L95 model.
Definition: l95/src/lorenz95/AnalyticInit.cc:17