IODA
ObsIoConstructor.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_OBSIOCONSTRUCTOR_H_
9 #define TEST_IO_OBSIOCONSTRUCTOR_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/ObsIo.h"
35 #include "ioda/io/ObsIoFactory.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() << "ObsIo obs space config: " << i << ": " << obsConfig << std::endl;
64  oops::Log::trace() << "ObsIo 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<ObsIo> obsIo;
71  obsIo = ObsIoFactory::create(ObsIoModes::READ, obsParams);
72 
73  // Test the counts that should be set on construction
74  ioda::Dimensions_t expectedMaxVarSize = testConfig.getInt("max var size", 0);
75  ioda::Dimensions_t maxVarSize = obsIo->maxVarSize();
76  EXPECT_EQUAL(maxVarSize, expectedMaxVarSize);
77 
78  ioda::Dimensions_t expectedNumLocs = testConfig.getInt("nlocs", 0);
79  ioda::Dimensions_t numLocs = obsIo->numLocs();
80  EXPECT_EQUAL(numLocs, expectedNumLocs);
81 
82  ioda::Dimensions_t expectedNumVars = testConfig.getInt("nvars", 0);
83  ioda::Dimensions_t numVars = obsIo->numVars();
84  EXPECT_EQUAL(numVars, expectedNumVars);
85 
86  ioda::Dimensions_t expectedNumDimVars = testConfig.getInt("ndvars", 0);
87  ioda::Dimensions_t numDimVars = obsIo->numDimVars();
88  EXPECT_EQUAL(numDimVars, expectedNumDimVars);
89 
90  // Try the output constructor, if one was specified
91  if (obsParams.top_level_.obsOutFile.value() != boost::none) {
92  setOfileParamsFromTestConfig(testConfig, obsParams);
93  obsIo = ObsIoFactory::create(ObsIoModes::WRITE, obsParams);
94 
95  // See if we get expected number of locations
96  std::vector<eckit::LocalConfiguration> writeDimConfigs =
97  testConfig.getSubConfigurations("write dimensions");
98  ioda::Dimensions_t expectedNumLocs = 0;
99  for (std::size_t j = 0; j < writeDimConfigs.size(); ++j) {
100  std::string dimName = writeDimConfigs[i].getString("name");
101  Dimensions_t dimSize = writeDimConfigs[i].getInt("size");
102  if (dimName == "nlocs") {
103  expectedNumLocs = dimSize;
104  }
105  }
106 
107  ioda::Dimensions_t numLocs = obsIo->numLocs();
108  EXPECT_EQUAL(numLocs, expectedNumLocs);
109  }
110  }
111 }
112 
113 // -----------------------------------------------------------------------------
114 
115 class ObsIoConstructor : public oops::Test {
116  public:
118  virtual ~ObsIoConstructor() {}
119  private:
120  std::string testid() const override {return "test::ObsIoConstructor";}
121 
122  void register_tests() const override {
123  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
124 
125  ts.emplace_back(CASE("ioda/ObsIoConstructor/testConstructor")
126  { testConstructor(); });
127  }
128 
129  void clear() const override {}
130 };
131 
132 // -----------------------------------------------------------------------------
133 
134 } // namespace test
135 } // namespace ioda
136 
137 #endif // TEST_IO_OBSIOCONSTRUCTOR_H_
Interfaces for ioda::ObsGroup and related classes.
Interfaces for ioda::Variable and related classes.
static std::shared_ptr< ObsIo > create(ObsIoModes mode, const ObsSpaceParameters &parameters)
Create and return a new instance of an ObsIo subclass.
Definition: ObsIoFactory.cc:24
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
void clear() const override
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