IODA
DescendingSort.h
Go to the documentation of this file.
1 /*
2  * (C) Crown copyright 2020, Met Office
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_DESCENDINGSORT_H_
9 #define TEST_IODA_DESCENDINGSORT_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/testing/Test.h"
18 
19 #include "oops/mpi/mpi.h"
20 #include "oops/runs/Test.h"
21 #include "oops/test/TestEnvironment.h"
22 #include "oops/util/Expect.h"
23 
24 #include "ioda/IodaTrait.h"
25 #include "ioda/ObsSpace.h"
26 
27 namespace ioda {
28 
29 namespace test {
30 
31 void testDescendingSort(const eckit::LocalConfiguration &conf) {
32  // Produce and configure ObsData object
33  util::DateTime bgn(conf.getString("window begin"));
34  util::DateTime end(conf.getString("window end"));
35  const eckit::LocalConfiguration obsSpaceConf(conf, "obs space");
36  ioda::ObsData obsdata(obsSpaceConf, oops::mpi::world(), bgn, end, oops::mpi::myself());
37 
38  // This test only works for grouped data with descending sort order
39  if (obsdata.obs_sort_order() != "descending") {
40  throw eckit::BadValue("Must set sort_order to descending", Here());
41  }
42  if (obsdata.obs_group_var().empty()) {
43  throw eckit::BadValue("Must set group_variable", Here());
44  }
45 
46  // Number of locations
47  const size_t nlocs = obsdata.nlocs();
48 
49  // All expected sort indices, obtained from input file
50  std::vector <int> expectedIndicesAll;
51  expectedIndicesAll.assign(nlocs, 0);
52  obsdata.get_db("MetaData", "expected_indices", expectedIndicesAll);
53 
54  // Record index for each location
55  const std::vector <size_t> recnums = obsdata.recnum();
56 
57  // List of unique record indices
58  const std::vector <size_t> recnumList = obsdata.recidx_all_recnums();
59 
60  for (size_t rn = 0; rn < recnumList.size(); ++rn) {
61  // Expected record indices for this recnum
62  std::vector <size_t> expectedRecordIndices;
63  for (size_t rnindex = 0; rnindex < nlocs; ++rnindex)
64  if (recnums[rnindex] == rn)
65  expectedRecordIndices.emplace_back(expectedIndicesAll[rnindex]);
66 
67  // Actual record indices for this recnum
68  const std::vector<size_t> recordIndices = obsdata.recidx_vector(rn);
69 
70  // Compare the two vectors and throw an exception if they differ
71  const bool equal = std::equal(recordIndices.begin(),
72  recordIndices.end(),
73  expectedRecordIndices.begin());
74  EXPECT(equal);
75  }
76 }
77 
78 class DescendingSort : public oops::Test {
79  private:
80  std::string testid() const override {return "ioda::test::DescendingSort";}
81 
82  void register_tests() const override {
83  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
84 
85  const eckit::LocalConfiguration conf(::test::TestEnvironment::config());
86  for (const std::string & testCaseName : conf.keys())
87  {
88  const eckit::LocalConfiguration testCaseConf(::test::TestEnvironment::config(), testCaseName);
89  ts.emplace_back(CASE("ioda/DescendingSort/" + testCaseName, testCaseConf)
90  {
91  testDescendingSort(testCaseConf);
92  });
93  }
94  }
95 
96  void clear() const override {}
97 };
98 
99 } // namespace test
100 } // namespace ioda
101 
102 #endif // TEST_IODA_DESCENDINGSORT_H_
ioda::test::DescendingSort::testid
std::string testid() const override
Definition: DescendingSort.h:80
ioda::test::DescendingSort::register_tests
void register_tests() const override
Definition: DescendingSort.h:82
ioda::test::DescendingSort
Definition: DescendingSort.h:78
ObsSpace.h
ioda::ObsData::recnum
const std::vector< std::size_t > & recnum() const
Definition: ObsData.cc:380
ioda::test::testDescendingSort
void testDescendingSort(const eckit::LocalConfiguration &conf)
Definition: DescendingSort.h:31
ioda::ObsData
Observation Data.
Definition: ObsData.h:87
ioda
Definition: IodaUtils.cc:13
ioda::ObsData::recidx_all_recnums
std::vector< std::size_t > recidx_all_recnums() const
Definition: ObsData.cc:460
ioda::ObsData::recidx_vector
const std::vector< std::size_t > & recidx_vector(const RecIdxIter &Irec) const
Definition: ObsData.cc:435
ioda::ObsData::obs_sort_order
std::string obs_sort_order() const
Definition: ObsData.cc:327
ioda::ObsData::get_db
void get_db(const std::string &group, const std::string &name, std::vector< int > &vdata) const
transfer data from the obs container to vdata
Definition: ObsData.cc:191
ioda::test::DescendingSort::clear
void clear() const override
Definition: DescendingSort.h:96
ioda::ObsData::nlocs
std::size_t nlocs() const
Definition: ObsData.cc:351
ioda::ObsData::obs_group_var
std::string obs_group_var() const
Definition: ObsData.cc:309