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 ObsSpace 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::ObsSpace 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_vars().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  obsdata.save();
77 }
78 
79 class DescendingSort : public oops::Test {
80  private:
81  std::string testid() const override {return "ioda::test::DescendingSort";}
82 
83  void register_tests() const override {
84  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
85 
86  const eckit::LocalConfiguration conf(::test::TestEnvironment::config());
87  for (const std::string & testCaseName : conf.keys())
88  {
89  const eckit::LocalConfiguration testCaseConf(::test::TestEnvironment::config(), testCaseName);
90  ts.emplace_back(CASE("ioda/DescendingSort/" + testCaseName, testCaseConf)
91  {
92  testDescendingSort(testCaseConf);
93  });
94  }
95  }
96 
97  void clear() const override {}
98 };
99 
100 } // namespace test
101 } // namespace ioda
102 
103 #endif // TEST_IODA_DESCENDINGSORT_H_
Observation data class for IODA.
Definition: src/ObsSpace.h:116
const std::vector< std::size_t > & recnum() const
return reference to the record number vector
Definition: src/ObsSpace.h:222
std::vector< std::size_t > recidx_all_recnums() const
return all record numbers from the recidx_ data member
Definition: ObsSpace.cc:382
const std::vector< std::string > & obs_group_vars() const
return YAML configuration parameter: obsdatain.obsgrouping.group variables
Definition: ObsSpace.cc:210
const std::vector< std::size_t > & recidx_vector(const RecIdxIter &irec) const
return record number vector pointed to by the given iterator
Definition: ObsSpace.cc:365
size_t nlocs() const
return the number of locations in the obs space. Note that nlocs may be smaller than global unique nl...
Definition: src/ObsSpace.h:176
void get_db(const std::string &group, const std::string &name, std::vector< int > &vdata, const std::vector< int > &chanSelect={ }) const
transfer data from the obs container to vdata
Definition: ObsSpace.cc:270
std::string obs_sort_order() const
return YAML configuration parameter: obsdatain.obsgrouping.sort order
Definition: ObsSpace.cc:220
void save()
save the obs space data into a file (if obsdataout specified)
Definition: ObsSpace.cc:171
void register_tests() const override
void clear() const override
std::string testid() const override
CASE("Derived variable and unit conversion methods")
void testDescendingSort(const eckit::LocalConfiguration &conf)