UFO
GaussianThinning.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2019 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_UFO_GAUSSIANTHINNING_H_
9 #define TEST_UFO_GAUSSIANTHINNING_H_
10 
11 #include <iomanip>
12 #include <memory>
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 "ioda/ObsSpace.h"
21 #include "ioda/ObsVector.h"
22 #include "oops/mpi/mpi.h"
23 #include "oops/runs/Test.h"
24 #include "oops/util/Expect.h"
25 #include "test/TestEnvironment.h"
27 #include "ufo/filters/Variables.h"
28 
29 namespace ufo {
30 namespace test {
31 
32 void testGaussianThinning(const eckit::LocalConfiguration &conf) {
33  util::DateTime bgn(conf.getString("window begin"));
34  util::DateTime end(conf.getString("window end"));
35 
36  const eckit::LocalConfiguration obsSpaceConf(conf, "obs space");
37  ioda::ObsTopLevelParameters obsParams;
38  obsParams.validateAndDeserialize(obsSpaceConf);
39  ioda::ObsSpace obsspace(obsParams, oops::mpi::world(), bgn, end, oops::mpi::myself());
40 
41  if (conf.has("air_pressures")) {
42  const std::vector<float> air_pressures = conf.getFloatVector("air_pressures");
43  obsspace.put_db("MetaData", "air_pressure", air_pressures);
44  const std::vector<float> air_pressure_obserrors(air_pressures.size(), 1.0f);
45  obsspace.put_db("ObsError", "air_pressure", air_pressure_obserrors);
46  }
47 
48  if (conf.has("category")) {
49  const std::vector<int> categories = conf.getIntVector("category");
50  obsspace.put_db("MetaData", "category", categories);
51  }
52 
53  if (conf.has("string_category")) {
54  const std::vector<std::string> categories = conf.getStringVector("string_category");
55  obsspace.put_db("MetaData", "string_category", categories);
56  }
57 
58  if (conf.has("priority")) {
59  const std::vector<int> priorities = conf.getIntVector("priority");
60  obsspace.put_db("MetaData", "priority", priorities);
61  }
62 
63  std::shared_ptr<ioda::ObsDataVector<float>> obserr(new ioda::ObsDataVector<float>(
64  obsspace, obsspace.obsvariables(), "ObsError"));
65  std::shared_ptr<ioda::ObsDataVector<int>> qcflags(new ioda::ObsDataVector<int>(
66  obsspace, obsspace.obsvariables()));
67 
68  eckit::LocalConfiguration filterConf(conf, "GaussianThinning");
69  ufo::GaussianThinningParameters filterParameters;
70  std::string expectedMessage;
71  if (conf.get("on_deserialization_expect_exception_with_message", expectedMessage)) {
72  EXPECT_THROWS_MSG(filterParameters.validateAndDeserialize(filterConf), expectedMessage.c_str());
73  return;
74  } else {
75  filterParameters.validateAndDeserialize(filterConf);
76  }
77 
78  ufo::Gaussian_Thinning filter(obsspace, filterParameters, qcflags, obserr);
79  filter.preProcess();
80 
81  const std::vector<size_t> expectedThinnedObsIndices =
82  conf.getUnsignedVector("expected_thinned_obs_indices");
83  std::vector<size_t> thinnedObsIndices;
84  for (size_t i = 0; i < qcflags->nlocs(); ++i)
85  if ((*qcflags)[0][i] == ufo::QCflags::thinned)
86  thinnedObsIndices.push_back(i);
87  EXPECT_EQUAL(thinnedObsIndices.size(), expectedThinnedObsIndices.size());
88  const bool equal = std::equal(thinnedObsIndices.begin(), thinnedObsIndices.end(),
89  expectedThinnedObsIndices.begin());
90  EXPECT(equal);
91 }
92 
93 class GaussianThinning : public oops::Test {
94  private:
95  std::string testid() const override {return "ufo::test::GaussianThinning";}
96 
97  void register_tests() const override {
98  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
99 
100  const eckit::LocalConfiguration conf(::test::TestEnvironment::config());
101  for (const std::string & testCaseName : conf.keys())
102  {
103  const eckit::LocalConfiguration testCaseConf(::test::TestEnvironment::config(), testCaseName);
104  ts.emplace_back(CASE("ufo/GaussianThinning/" + testCaseName, testCaseConf)
105  {
106  testGaussianThinning(testCaseConf);
107  });
108  }
109  }
110 
111  void clear() const override {}
112 };
113 
114 } // namespace test
115 } // namespace ufo
116 
117 #endif // TEST_UFO_GAUSSIANTHINNING_H_
Group observations into grid cells and preserve only one observation in each cell.
Options controlling the operation of the Gaussian_Thinning filter.
void preProcess() override
void register_tests() const override
std::string testid() const override
void clear() const override
constexpr int thinned
Definition: QCflags.h:26
void testGaussianThinning(const eckit::LocalConfiguration &conf)
CASE("ufo/DataExtractor/bilinearinterp/float_linear")
Definition: RunCRTM.h:27