FV3 Bundle
test/ioda/ObsSpace.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
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  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #ifndef TEST_INTERFACE_OBSSPACE_H_
12 #define TEST_INTERFACE_OBSSPACE_H_
13 
14 #include <string>
15 #include <cmath>
16 
17 #define BOOST_TEST_NO_MAIN
18 #define BOOST_TEST_ALTERNATIVE_INIT_API
19 #define BOOST_TEST_DYN_LINK
20 #include <boost/test/unit_test.hpp>
21 
22 #include <boost/noncopyable.hpp>
23 #include <boost/scoped_ptr.hpp>
24 
25 #include "eckit/config/LocalConfiguration.h"
26 #include "ioda/IodaTrait.h"
27 #include "ioda/ObsSpace.h"
28 #include "oops/runs/Test.h"
30 #include "test/TestEnvironment.h"
31 
32 namespace ioda {
33 namespace test {
34 
35 // -----------------------------------------------------------------------------
36 
37 void testConstructor() {
38  typedef ::test::ObsTestsFixture<ioda::IodaTrait> Test_;
39 
40  ioda::ObsSpace * Odb;
41 
42  std::size_t Nobs;
43  std::size_t Nlocs;
44 
45  int ExpectedNobs;
46  int ExpectedNlocs;
47 
48  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
49  BOOST_CHECK_EQUAL(Test_::obspace()[jj].windowStart(), Test_::tbgn());
50  BOOST_CHECK_EQUAL(Test_::obspace()[jj].windowEnd(), Test_::tend());
51 
52  Odb = &(Test_::obspace()[jj].observationspace());
53 
54  // Get the numbers of observations (nobs) and locations (nlocs) from the obspace object
55  Nobs = Odb->nobs();
56  Nlocs = Odb->nlocs();
57 
58  // Get the expected nobs and nlocs from the obspace object's configuration
59  const eckit::Configuration & Conf(Odb->config());
60  ExpectedNobs = Conf.getInt("ObsData.ObsDataIn.metadata.nobs");
61  ExpectedNlocs = Conf.getInt("ObsData.ObsDataIn.metadata.nlocs");
62 
63  BOOST_CHECK_EQUAL(Nobs, ExpectedNobs);
64  BOOST_CHECK_EQUAL(Nlocs, ExpectedNlocs);
65  }
66 }
67 
68 // -----------------------------------------------------------------------------
69 
71  typedef ::test::ObsTestsFixture<ioda::IodaTrait> Test_;
72 
73  ioda::ObsSpace * Odb;
74 
75  std::size_t Nlocs;
76 
77  double Vnorm;
78  double Tol;
79 
80  std::vector<std::string> VarNames;
81  std::vector<double> ExpectedVnorms;
82 
83  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
84 
85  // Set up a pointer to the ObsSpace object for convenience
86  Odb = &(Test_::obspace()[jj].observationspace());
87 
88  // Read in the variable names and expected norm values from the configuration
89  const eckit::Configuration & Conf(Odb->config());
90  VarNames = Conf.getStringVector("ObsData.ObsDataIn.TestData.variables");
91  ExpectedVnorms = Conf.getDoubleVector("ObsData.ObsDataIn.TestData.norms");
92  Tol = Conf.getDouble("ObsData.ObsDataIn.TestData.tolerance");
93 
94  Nlocs = Odb->nlocs();
95  for (std::size_t i = 0; i < VarNames.size(); ++i) {
96  // Read in the table, calculate the norm and compare with the expected norm.
97  std::vector<double> TestVec(Nlocs);
98  Odb->getObsVector(VarNames[i], TestVec);
99 
100  // Calculate the norm of the vector
101  Vnorm = 0.0;
102  for (std::size_t j = 0; j < Nlocs; ++j) {
103  Vnorm += pow(TestVec[j], 2.0);
104  }
105  Vnorm = sqrt(Vnorm);
106 
107  BOOST_CHECK_CLOSE(Vnorm, ExpectedVnorms[i], Tol);
108  }
109  }
110 }
111 
112 // -----------------------------------------------------------------------------
113 
115  typedef ::test::ObsTestsFixture<ioda::IodaTrait> Test_;
116 
117  ioda::ObsSpace * Odb;
118 
119  std::size_t Nlocs;
120  bool VecMatch;
121 
122  std::string VarName("DummyVector");
123 
124  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
125 
126  // Set up a pointer to the ObsSpace object for convenience
127  Odb = &(Test_::obspace()[jj].observationspace());
128 
129  // Create a dummy vector to put into the database
130  // Load up the vector with contrived data, put the vector then
131  // get the vector and see if the contrived data made it through.
132  Nlocs = Odb->nlocs();
133  std::vector<double> TestVec(Nlocs);
134  std::vector<double> ExpectedVec(Nlocs);
135 
136  for (std::size_t i = 0; i < Nlocs; ++i) {
137  ExpectedVec[i] = double(i);
138  }
139 
140  // Put the vector into the database. Then read the vector back from the database
141  // and compare to the original
142  Odb->putObsVector(VarName, ExpectedVec);
143  Odb->getObsVector(VarName, TestVec);
144 
145  VecMatch = true;
146  for (std::size_t i = 0; i < Nlocs; ++i) {
147  VecMatch = VecMatch && (int(ExpectedVec[i]) == int(TestVec[i]));
148  }
149 
150  BOOST_CHECK(VecMatch);
151  }
152 }
153 
154 // -----------------------------------------------------------------------------
155 
156 void testGetDb() {
157  typedef ::test::ObsTestsFixture<ioda::IodaTrait> Test_;
158 
159  ioda::ObsSpace * Odb;
160 
161  std::size_t Nlocs;
162 
163  double Vnorm;
164  double Tol;
165 
166  std::vector<std::string> VarNames;
167  std::vector<double> ExpectedVnorms;
168 
169  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
170 
171  // Set up a pointer to the ObsSpace object for convenience
172  Odb = &(Test_::obspace()[jj].observationspace());
173 
174  // Read in the variable names and expected norm values from the configuration
175  const eckit::Configuration & Conf(Odb->config());
176  VarNames = Conf.getStringVector("ObsData.ObsDataIn.TestData.variables");
177  ExpectedVnorms = Conf.getDoubleVector("ObsData.ObsDataIn.TestData.norms");
178  Tol = Conf.getDouble("ObsData.ObsDataIn.TestData.tolerance");
179 
180  Nlocs = Odb->nlocs();
181  for (std::size_t i = 0; i < VarNames.size(); ++i) {
182  // Read in the table, calculate the norm and compare with the expected norm.
183  std::vector<double> TestVec(Nlocs);
184  Odb->get_db("MetaData", VarNames[i], Nlocs, TestVec.data());
185 
186  // Calculate the norm of the vector
187  Vnorm = 0.0;
188  for (std::size_t j = 0; j < Nlocs; ++j) {
189  Vnorm += pow(TestVec[j], 2.0);
190  }
191  Vnorm = sqrt(Vnorm);
192 
193  BOOST_CHECK_CLOSE(Vnorm, ExpectedVnorms[i], Tol);
194  }
195  }
196 }
197 
198 // -----------------------------------------------------------------------------
199 
200 void testPutDb() {
201  typedef ::test::ObsTestsFixture<ioda::IodaTrait> Test_;
202 
203  ioda::ObsSpace * Odb;
204 
205  std::size_t Nlocs;
206  bool VecMatch;
207 
208  std::string VarName("DummyRow");
209 
210  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
211 
212  // Set up a pointer to the ObsSpace object for convenience
213  Odb = &(Test_::obspace()[jj].observationspace());
214 
215  // Create a dummy vector to put into the database
216  // Load up the vector with contrived data, put the vector then
217  // get the vector and see if the contrived data made it through.
218  Nlocs = Odb->nlocs();
219  std::vector<double> TestVec(Nlocs);
220  std::vector<double> ExpectedVec(Nlocs);
221 
222  for (std::size_t i = 0; i < Nlocs; ++i) {
223  ExpectedVec[i] = double(i);
224  }
225 
226  // Put the vector into the database. Then read the vector back from the database
227  // and compare to the original
228  Odb->put_db("MetaData", VarName, Nlocs, ExpectedVec.data());
229  Odb->get_db("MetaData", VarName, Nlocs, TestVec.data());
230 
231  VecMatch = true;
232  for (std::size_t i = 0; i < Nlocs; ++i) {
233  VecMatch = VecMatch && (int(ExpectedVec[i]) == int(TestVec[i]));
234  }
235 
236  BOOST_CHECK(VecMatch);
237  }
238 }
239 
240 // -----------------------------------------------------------------------------
241 
242 class ObsSpace : public oops::Test {
243  public:
244  ObsSpace() {}
245  virtual ~ObsSpace() {}
246  private:
247  std::string testid() const {return "test::ObsSpace<ioda::IodaTrait>";}
248 
249  void register_tests() const {
250  boost::unit_test::test_suite * ts = BOOST_TEST_SUITE("ObsSpace");
251 
252  ts->add(BOOST_TEST_CASE(&testConstructor));
253  ts->add(BOOST_TEST_CASE(&testGetObsVector));
254  ts->add(BOOST_TEST_CASE(&testPutObsVector));
255  ts->add(BOOST_TEST_CASE(&testGetDb));
256  ts->add(BOOST_TEST_CASE(&testPutDb));
257 
258  boost::unit_test::framework::master_test_suite().add(ts);
259  }
260 };
261 
262 // -----------------------------------------------------------------------------
263 
264 } // namespace test
265 } // namespace ioda
266 
267 #endif // TEST_INTERFACE_OBSSPACE_H_
void get_db(const std::string &, const std::string &, const std::size_t &, int[]) const
Definition: ObsSpace.cc:57
l_size ! loop over number of fields ke do je do i
int nlocs() const
Definition: ObsSpace.cc:98
integer, parameter, public double
Definition: Type_Kinds.f90:106
void testGetObsVector()
Wrapper around ObsHelpQG, mostly to hide the factory.
l_size ! loop over number of fields ke do j
std::string testid() const
void getObsVector(const std::string &, std::vector< double > &) const
Definition: ObsSpace.cc:46
int nobs() const
Definition: ObsSpace.cc:89
void testPutObsVector()
void testConstructor()
void putObsVector(const std::string &, const std::vector< double > &) const
Definition: ObsSpace.cc:52
void put_db(const std::string &, const std::string &, const std::size_t &, const int[]) const
Definition: ObsSpace.cc:67
const eckit::Configuration & config() const
Access information.
Definition: ObsSpaceBase.h:34