UFO
test/ufo/Variables.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2019 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 #ifndef TEST_UFO_VARIABLES_H_
9 #define TEST_UFO_VARIABLES_H_
10 
11 #include <algorithm>
12 #include <set>
13 #include <string>
14 #include <vector>
15 
16 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
17 
18 #include "eckit/config/LocalConfiguration.h"
19 #include "eckit/testing/Test.h"
20 #include "oops/base/Variables.h"
21 #include "oops/runs/Test.h"
22 #include "test/TestEnvironment.h"
23 #include "ufo/filters/Variable.h"
24 #include "ufo/filters/Variables.h"
25 
26 namespace ufo {
27 namespace test {
28 
29 // -----------------------------------------------------------------------------
30 
31 void testVariable() {
32  std::vector<eckit::LocalConfiguration> conf;
33  ::test::TestEnvironment::config().get("test variables", conf);
34  for (std::size_t jj = 0; jj < conf.size(); ++jj) {
35  // read variable from config
36  Variable var(conf[jj]);
37  // read reference vector of strings and group
38  std::vector<std::string> refvars(conf[jj].getStringVector("reference names"));
39  const std::string refgroup = conf[jj].getString("reference group");
40  // compare the two
41  EXPECT(var.size() == refvars.size());
42  EXPECT(var.group() == refgroup);
43  for (std::size_t jvar = 0; jvar < var.size(); ++jvar) {
44  EXPECT(var.variable(jvar) == refvars[jvar]);
45  }
46  }
47 }
48 
49 // -----------------------------------------------------------------------------
51  std::vector<eckit::LocalConfiguration> conf;
52  ::test::TestEnvironment::config().get("oops variables", conf);
53  for (std::size_t jj = 0; jj < conf.size(); ++jj) {
54  // read variable from config
55  oops::Variables oopsvars(conf[jj], "variables");
56  // read reference vector of strings
57  std::vector<std::string> refvars(conf[jj].getStringVector("reference names"));
58  // init ufo::Variables
59  Variables vars(oopsvars);
60  // compare the two
61  EXPECT(vars.nvars() == refvars.size());
62  for (std::size_t jvar = 0; jvar < vars.nvars(); ++jvar) {
63  EXPECT(vars.variable(jvar).variable() == refvars[jvar]);
64  }
65  }
66 }
67 
68 // -----------------------------------------------------------------------------
69 // Helper function: checks if variable is in variables. Ignores functions,
70 // expands channels (i.e. for bt channels 1-2 bt_1, bt_2 are in vars, bt is not)
71 bool hasVariable(const Variables & vars, const Variable & var) {
72  bool found = false;
73  for (size_t jj = 0; jj < vars.nvars(); ++jj) {
74  if (vars.variable(jj).variable() == var.variable() &&
75  vars.variable(jj).group() == var.group()) found = true;
76  }
77  return found;
78 }
79 
80 
81 // -----------------------------------------------------------------------------
82 // Test that ufo::Variables::allFromGroup() gets variables from the functions
84  Variables vars;
85  vars += Variable("height@GeoVaLs");
86  vars += Variable("Velocity@ObsFunction");
87  vars += Variable("latitude@MetaData");
88  vars += Variable("temperature@ObsValue");
89  vars += Variable("longitude@MetaData");
90 
91  Variables res = vars.allFromGroup("ObsValue");
92  oops::Log::info() << res << std::endl;
93 
94  EXPECT(res.size() == 3);
95  EXPECT(hasVariable(res, Variable("eastward_wind@ObsValue")));
96  EXPECT(hasVariable(res, Variable("northward_wind@ObsValue")));
97  EXPECT(hasVariable(res, Variable("temperature@ObsValue")));
98 }
99 
100 // -----------------------------------------------------------------------------
101 // Test that ufo::Variables::hasGroup() works for functions
102 void testHasGroup() {
103  ufo::Variables vars;
104  vars += Variable("latitude@MetaData");
105  vars += Variable("longitude@MetaData");
106 
107  EXPECT(vars.hasGroup("MetaData"));
108  EXPECT(!vars.hasGroup("ObsValue"));
109 
110  vars += Variable("Velocity@ObsFunction");
111 
112  EXPECT(vars.hasGroup("ObsValue"));
113 }
114 
115 
116 // -----------------------------------------------------------------------------
117 
118 class Variables : public oops::Test {
119  public:
121  virtual ~Variables() {}
122  private:
123  std::string testid() const override {return "ufo::test::Variables";}
124 
125  void register_tests() const override {
126  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
127 
128  ts.emplace_back(CASE("ufo/Variables/testVariable")
129  { testVariable(); });
130 
131  ts.emplace_back(CASE("ufo/Variables/testConstructor")
132  { testConstructor(); });
133 
134  ts.emplace_back(CASE("ufo/Variables/testAllFromGroup")
135  { testAllFromGroup(); });
136 
137  ts.emplace_back(CASE("ufo/Variables/testHasGroup")
138  { testHasGroup(); });
139  }
140 
141  void clear() const override {}
142 };
143 
144 // -----------------------------------------------------------------------------
145 
146 } // namespace test
147 } // namespace ufo
148 
149 #endif // TEST_UFO_VARIABLES_H_
ufo::Variables
Definition: src/ufo/filters/Variables.h:24
ufo::test::CASE
CASE("ufo/MetOfficeBuddyPairFinder/" "Duplicates, constraints on buddy counts, legacy pair collector")
Definition: test/ufo/MetOfficeBuddyPairFinder.h:171
ufo::test::Variables::~Variables
virtual ~Variables()
Definition: test/ufo/Variables.h:121
ufo::test::testConstructor
void testConstructor()
Definition: test/ufo/Variables.h:50
ufo::Variables::hasGroup
bool hasGroup(const std::string &) const
Definition: Variables.cc:156
ufo::Variable::size
size_t size() const
Definition: Variable.cc:79
ufo
Definition: RunCRTM.h:27
ufo::test::Variables::testid
std::string testid() const override
Definition: test/ufo/Variables.h:123
Variables.h
ufo::Variable::group
const std::string & group() const
Definition: Variable.cc:117
ufo::test::testVariable
void testVariable(const std::string &section)
Definition: test/ufo/ParallelObsDistribution.h:63
ufo::Variable::variable
const std::string & variable() const
Definition: Variable.cc:100
ufo::test::testHasGroup
void testHasGroup()
Definition: test/ufo/Variables.h:102
ufo::test::Variables::Variables
Variables()
Definition: test/ufo/Variables.h:120
ufo::test::Variables
Definition: test/ufo/Variables.h:118
ufo::test::testAllFromGroup
void testAllFromGroup()
Definition: test/ufo/Variables.h:83
ufo::Variable
Definition: Variable.h:23
ufo::test::hasVariable
bool hasVariable(const Variables &vars, const Variable &var)
Definition: test/ufo/Variables.h:71
conf
Definition: conf.py:1
ufo::test::Variables::register_tests
void register_tests() const override
Definition: test/ufo/Variables.h:125
ufo::test::Variables::clear
void clear() const override
Definition: test/ufo/Variables.h:141
Variable.h