OOPS
LocalObsSpace.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2017-2020 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_INTERFACE_LOCALOBSSPACE_H_
9 #define TEST_INTERFACE_LOCALOBSSPACE_H_
10 
11 #include <string>
12 #include <vector>
13 
14 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
15 
16 #include "eckit/config/LocalConfiguration.h"
17 #include "eckit/geometry/Point2.h"
18 #include "eckit/testing/Test.h"
21 #include "oops/runs/Test.h"
22 #include "oops/util/dot_product.h"
24 #include "test/TestEnvironment.h"
25 
26 namespace test {
27 
28 // -----------------------------------------------------------------------------
29 /// Tests that number of local observations in LocalObsSpace is the same as in reference
30 template <typename OBS> void testLocalObsSpace() {
31  typedef ObsTestsFixture<OBS> Test_;
32  typedef oops::ObsSpace<OBS> LocalObsSpace_;
33  typedef oops::ObsVector<OBS> ObsVector_;
34 
35  const eckit::LocalConfiguration localconf(TestEnvironment::config(), "local obs space");
36 
37  // get center (for localization) from yaml
38  eckit::LocalConfiguration geolocconf(localconf, "location");
39  double lon = geolocconf.getDouble("lon");
40  double lat = geolocconf.getDouble("lat");
41  const eckit::geometry::Point2 center(lon, lat);
42 
43  // get localization test from yaml
44  eckit::LocalConfiguration locconf(localconf, "localization");
45 
46  // count local nobs for all obs types
47  int totalNobs = 0;
48  for (size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
49  // initialize local observation space
50  LocalObsSpace_ localobs(Test_::obspace()[jj], center, locconf);
51  oops::Log::test() << "Local obs within " << locconf << " from " << center <<
52  ": " << localobs << std::endl;
53 
54  // count local nobs
55  ObsVector_ localvec(localobs);
56  totalNobs += localvec.nobs();
57  }
58 
59  // test that local nobs is equal to the reference value
60  const int ref_nobs = localconf.getInt("reference nobs");
61  EXPECT(totalNobs == ref_nobs);
62 }
63 
64 // -----------------------------------------------------------------------------
65 /// Tests that constructing local ObsVector from local ObsSpace by reading
66 /// (ObsVector(const ObsSpace &, const std::string &) ctor), and by subsetting
67 /// full ObsVector (ObsVector(const ObsSpace &, const ObsVector &) ctor)
68 /// give the same results
69 template <typename OBS> void testLocalObsVector() {
70  typedef ObsTestsFixture<OBS> Test_;
71  typedef oops::ObsSpace<OBS> LocalObsSpace_;
72  typedef oops::ObsVector<OBS> ObsVector_;
73 
74  const eckit::LocalConfiguration localconf(TestEnvironment::config(), "local obs space");
75  // get center (for localization) from yaml
76  eckit::LocalConfiguration geolocconf(localconf, "location");
77  double lon = geolocconf.getDouble("lon");
78  double lat = geolocconf.getDouble("lat");
79  const eckit::geometry::Point2 center(lon, lat);
80 
81  // get localization test from yaml
82  eckit::LocalConfiguration locconf(localconf, "localization");
83  const std::string varname = localconf.getString("variable name");
84 
85  for (size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
86  // initialize full ObsVector for a specified variable
87  ObsVector_ fullvec(Test_::obspace()[jj], varname);
88  oops::Log::test() << "Full Obsvector: " << fullvec << std::endl;
89 
90  // initialize local observation space
91  LocalObsSpace_ localobs(Test_::obspace()[jj], center, locconf);
92  oops::Log::test() << "Local obs within " << locconf << " from " << center <<
93  ": " << localobs << std::endl;
94 
95  // intialize local obsvector by reading specified variable from local obsspace
96  ObsVector_ localvec1(localobs, varname);
97  oops::Log::test() << "Local Obsvector from Local Obsspace: " << localvec1 << std::endl;
98 
99  // initialize local obsvector from full obsvector using local obsspace
100  ObsVector_ localvec2(localobs, fullvec);
101  oops::Log::test() << "Local ObsVector from full ObsVector: " << localvec2 << std::endl;
102  // check that the two are equal
103  EXPECT(localvec1.nobs() == localvec2.nobs());
104  localvec2 -= localvec1;
105  const double rms = dot_product(localvec2, localvec2);
106  EXPECT(rms == 0);
107  }
108 }
109 
110 // -----------------------------------------------------------------------------
111 
112 template <typename OBS> class LocalObsSpace : public oops::Test {
114  public:
116  virtual ~LocalObsSpace() {}
117  private:
118  std::string testid() const override {return "test::LocalObsSpace<" + OBS::name() + ">";}
119 
120  void register_tests() const override {
121  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
122  ts.emplace_back(CASE("interface/LocalObsSpace/testLocalObsSpace")
123  { testLocalObsSpace<OBS>(); });
124  ts.emplace_back(CASE("interface/LocalObsSpace/testLocalObsVector")
125  { testLocalObsVector<OBS>(); });
126  }
127 
128  void clear() const override {
129  Test_::reset();
130  }
131 };
132 
133 // =============================================================================
134 
135 } // namespace test
136 
137 #endif // TEST_INTERFACE_LOCALOBSSPACE_H_
test::ObsTestsFixture
Definition: ObsTestsFixture.h:29
test::ObsTestsFixture::reset
static void reset()
Definition: ObsTestsFixture.h:37
test::testLocalObsSpace
void testLocalObsSpace()
Tests that number of local observations in LocalObsSpace is the same as in reference.
Definition: LocalObsSpace.h:30
test::LocalObsSpace::clear
void clear() const override
Definition: LocalObsSpace.h:128
oops::ObsSpace
Definition: oops/interface/ObsSpace.h:42
test::CASE
CASE("test_linearmodelparameterswrapper_valid_name")
Definition: LinearModelFactory.cc:22
test
Definition: LinearModelFactory.cc:20
oops::ObsVector
Definition: oops/interface/ObsSpace.h:36
test::LocalObsSpace::testid
std::string testid() const override
Definition: LocalObsSpace.h:118
Test.h
test::LocalObsSpace::LocalObsSpace
LocalObsSpace()
Definition: LocalObsSpace.h:115
test::TestEnvironment::config
static const eckit::Configuration & config()
Definition: TestEnvironment.h:40
ObsTestsFixture.h
test::LocalObsSpace::Test_
ObsTestsFixture< OBS > Test_
Definition: LocalObsSpace.h:113
ObsSpace.h
TestEnvironment.h
oops::Test
Definition: Test.h:39
test::LocalObsSpace
Definition: LocalObsSpace.h:112
ObsVector.h
test::LocalObsSpace::register_tests
void register_tests() const override
Definition: LocalObsSpace.h:120
test::testLocalObsVector
void testLocalObsVector()
Definition: LocalObsSpace.h:69
test::LocalObsSpace::~LocalObsSpace
virtual ~LocalObsSpace()
Definition: LocalObsSpace.h:116