IODA
ObsFrameConstructor.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2018 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_IO_OBSFRAMECONSTRUCTOR_H_
9 #define TEST_IO_OBSFRAMECONSTRUCTOR_H_
10 
11 #include <algorithm>
12 #include <functional>
13 #include <map>
14 #include <memory>
15 #include <numeric>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
21 
22 #include "eckit/config/LocalConfiguration.h"
23 #include "eckit/testing/Test.h"
24 
25 #include "oops/mpi/mpi.h"
26 #include "oops/runs/Test.h"
27 #include "oops/test/TestEnvironment.h"
28 #include "oops/util/DateTime.h"
29 #include "oops/util/FloatCompare.h"
30 #include "oops/util/Logger.h"
31 
32 #include "ioda/core/IodaUtils.h"
33 #include "ioda/distribution/DistributionFactory.h"
34 #include "ioda/io/ObsFrameRead.h"
35 #include "ioda/io/ObsFrameWrite.h"
36 #include "ioda/ObsGroup.h"
37 #include "ioda/ObsSpaceParameters.h"
39 
40 namespace ioda {
41 namespace test {
42 
43 // -----------------------------------------------------------------------------
44 // Helper Functions
45 // -----------------------------------------------------------------------------
46 
47 // -----------------------------------------------------------------------------
48 // Test Functions
49 // -----------------------------------------------------------------------------
50 
51 // -----------------------------------------------------------------------------
52 void testConstructor() {
53  const eckit::LocalConfiguration conf(::test::TestEnvironment::config());
54  std::vector<eckit::LocalConfiguration> confOspaces = conf.getSubConfigurations("observations");
55  util::DateTime bgn(::test::TestEnvironment::config().getString("window begin"));
56  util::DateTime end(::test::TestEnvironment::config().getString("window end"));
57 
58  for (std::size_t i = 0; i < confOspaces.size(); ++i) {
59  eckit::LocalConfiguration obsConfig;
60  eckit::LocalConfiguration testConfig;
61  confOspaces[i].get("obs space", obsConfig);
62  confOspaces[i].get("test data", testConfig);
63  oops::Log::trace() << "ObsFrame obs space config: " << i << ": " << obsConfig << std::endl;
64  oops::Log::trace() << "ObsFrame test data config: " << i << ": " << testConfig << std::endl;
65 
66  ioda::ObsSpaceParameters obsParams(bgn, end, oops::mpi::world(), oops::mpi::myself());
67  obsParams.deserialize(obsConfig);
68 
69  // Try the input constructor first - should have one to try if we got here
70  std::shared_ptr<ObsFrame> obsFrame = std::make_shared<ObsFrameRead>(obsParams);
71 
72  // Test the counts that should be set on construction
73  ioda::Dimensions_t expectedMaxVarSize = testConfig.getInt("max var size", 0);
74  ioda::Dimensions_t maxVarSize = obsFrame->ioMaxVarSize();
75  EXPECT_EQUAL(maxVarSize, expectedMaxVarSize);
76 
77  ioda::Dimensions_t expectedNumLocs = testConfig.getInt("nlocs", 0);
78  ioda::Dimensions_t numLocs = obsFrame->ioNumLocs();
79  EXPECT_EQUAL(numLocs, expectedNumLocs);
80 
81  ioda::Dimensions_t expectedNumVars = testConfig.getInt("nvars", 0);
82  ioda::Dimensions_t numVars = obsFrame->ioNumVars();
83  EXPECT_EQUAL(numVars, expectedNumVars);
84 
85  ioda::Dimensions_t expectedNumDimVars = testConfig.getInt("ndvars", 0);
86  ioda::Dimensions_t numDimVars = obsFrame->ioNumDimVars();
87  EXPECT_EQUAL(numDimVars, expectedNumDimVars);
88 
89  // Try the output constructor, if one was specified
90  if (obsParams.top_level_.obsOutFile.value() != boost::none) {
91  setOfileParamsFromTestConfig(testConfig, obsParams);
92  obsFrame = std::make_shared<ObsFrameWrite>(obsParams);
93 
94  // See if we get expected number of locations
95  std::vector<eckit::LocalConfiguration> writeDimConfigs =
96  testConfig.getSubConfigurations("write dimensions");
97  ioda::Dimensions_t expectedNumLocs = 0;
98  for (std::size_t j = 0; j < writeDimConfigs.size(); ++j) {
99  std::string dimName = writeDimConfigs[i].getString("name");
100  Dimensions_t dimSize = writeDimConfigs[i].getInt("size");
101  if (dimName == "nlocs") {
102  expectedNumLocs = dimSize;
103  }
104  }
105 
106  ioda::Dimensions_t numLocs = obsFrame->ioNumLocs();
107  EXPECT_EQUAL(numLocs, expectedNumLocs);
108  }
109  }
110 }
111 
112 // -----------------------------------------------------------------------------
113 
114 class ObsFrameConstructor : public oops::Test {
115  public:
117  virtual ~ObsFrameConstructor() {}
118  private:
119  std::string testid() const override {return "test::ObsFrameConstructor";}
120 
121  void register_tests() const override {
122  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
123 
124  ts.emplace_back(CASE("ioda/ObsFrameConstructor/testConstructor")
125  { testConstructor(); });
126  }
127 
128  void clear() const override {}
129 };
130 
131 // -----------------------------------------------------------------------------
132 
133 } // namespace test
134 } // namespace ioda
135 
136 #endif // TEST_IO_OBSFRAMECONSTRUCTOR_H_
Interfaces for ioda::ObsGroup and related classes.
Interfaces for ioda::Variable and related classes.
void deserialize(const eckit::Configuration &config)
deserialize the parameter sub groups
ObsTopLevelParameters top_level_
sub groups of parameters
oops::OptionalParameter< ObsFileOutParameters > obsOutFile
output specification by writing to a file
std::string testid() const override
void register_tests() const override
CASE("Derived variable and unit conversion methods")
void setOfileParamsFromTestConfig(const eckit::LocalConfiguration &obsConfig, ioda::ObsSpaceParameters &obsParams)
set params for output file construction from test YAML configuration
Definition: IodaUtils.cc:306