IODA
test/ioda/ObsDataVector.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2021 Met Office UK
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 #ifndef TEST_IODA_OBSDATAVECTOR_H_
9 #define TEST_IODA_OBSDATAVECTOR_H_
10 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include <boost/make_unique.hpp>
16 
17 #include "eckit/config/LocalConfiguration.h"
18 #include "eckit/testing/Test.h"
19 
20 #include "oops/mpi/mpi.h"
21 #include "oops/runs/Test.h"
22 #include "oops/test/TestEnvironment.h"
23 
24 #include "ioda/ObsDataVector.h"
25 #include "ioda/ObsSpace.h"
26 
27 namespace ioda {
28 namespace test {
29 
30 class ObsDataVecTestFixture : private boost::noncopyable {
32 
33  public:
34  static ObsSpace_ & obspace() {return *getInstance().obspace_;}
35 
36  private:
38  static ObsDataVecTestFixture theObsDataVecTestFixture;
39  return theObsDataVecTestFixture;
40  }
41 
43  const eckit::Configuration &conf = ::test::TestEnvironment::config();
44  const util::DateTime bgn(conf.getString("window begin"));
45  const util::DateTime end(conf.getString("window end"));
46 
47  eckit::LocalConfiguration obsconf(conf, "obs space");
48  obspace_ = boost::make_unique<ObsSpace_>(obsconf, oops::mpi::world(),
49  bgn, end, oops::mpi::myself());
50  }
51 
52  std::unique_ptr<ObsSpace_> obspace_;
53 };
54 
55 std::string trim(const std::string & str) {
56  const auto strBegin = str.find_first_not_of("\n");
57  if (strBegin == std::string::npos) return "";
58  const auto strEnd = str.find_last_not_of("\n");
59  const auto strRange = strEnd - strBegin + 1;
60  return str.substr(strBegin, strRange);
61 }
62 
63 
64 template <typename T>
65 void testPrint(const std::string &datatype) {
66  eckit::LocalConfiguration conf(::test::TestEnvironment::config(), "print." + datatype);
67 
68  oops::Variables vars;
69  for (const std::string &var : conf.getStringVector("variables"))
70  vars.push_back(var);
71  const std::string group = conf.getString("group");
73 
74  std::stringstream stream;
75  stream << vector;
76  std::string output = trim(stream.str());
77  std::string expectedOutput = trim(conf.getString("expected output"));
78  EXPECT_EQUAL(output, expectedOutput);
79 }
80 
81 CASE("ioda/ObsDataVector/printFloat") {
82  testPrint<float>("float");
83 }
84 
85 CASE("ioda/ObsDataVector/printDouble") {
86  testPrint<double>("double");
87 }
88 
89 CASE("ioda/ObsDataVector/printInt") {
90  testPrint<int>("int");
91 }
92 
93 CASE("ioda/ObsDataVector/printString") {
94  testPrint<std::string>("string");
95 }
96 
97 CASE("ioda/ObsDataVector/printDateTime") {
98  testPrint<util::DateTime>("datetime");
99 }
100 
101 CASE("ioda/ObsDataVector/closeObsSpace") {
102  // In case the obsdataout spec is ever used
104 }
105 
106 class ObsDataVector : public oops::Test {
107  private:
108  std::string testid() const override {return "test::ObsDataVector<ioda::IodaTrait>";}
109 
110  void register_tests() const override {}
111 
112  void clear() const override {}
113 };
114 
115 // =============================================================================
116 
117 } // namespace test
118 } // namespace ioda
119 
120 #endif // TEST_IODA_OBSDATAVECTOR_H_
ObsDataVector<DATATYPE> handles vectors of data of type DATATYPE in observation space.
Observation data class for IODA.
Definition: src/ObsSpace.h:116
void save()
save the obs space data into a file (if obsdataout specified)
Definition: ObsSpace.cc:171
std::unique_ptr< ObsSpace_ > obspace_
static ObsDataVecTestFixture & getInstance()
void register_tests() const override
std::string testid() const override
void testPrint(const std::string &datatype)
CASE("Derived variable and unit conversion methods")
std::string trim(const std::string &str)