8 #ifndef TEST_IO_OBSIOWRITE_H_
9 #define TEST_IO_OBSIOWRITE_H_
20 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
22 #include "eckit/config/LocalConfiguration.h"
23 #include "eckit/testing/Test.h"
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"
32 #include "ioda/core/IodaUtils.h"
33 #include "ioda/distribution/DistributionFactory.h"
34 #include "ioda/io/ObsIo.h"
35 #include "ioda/io/ObsIoFactory.h"
37 #include "ioda/ObsSpaceParameters.h"
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"));
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 testWrite obs space config: " << i <<
": "
64 << obsConfig << std::endl;
65 oops::Log::trace() <<
"ObsIo testWrite test data config: " << i <<
": "
66 << obsConfig << std::endl;
69 topParams.validateAndDeserialize(obsConfig);
71 oops::mpi::world(), oops::mpi::myself());
74 std::vector<eckit::LocalConfiguration> writeVarConfigs =
75 testConfig.getSubConfigurations(
"write variables");
76 std::vector<eckit::LocalConfiguration> writeDimConfigs =
77 testConfig.getSubConfigurations(
"write dimensions");
81 std::shared_ptr<ObsIo> obsIo =
85 for (std::size_t j = 0; j < writeVarConfigs.size(); ++j) {
86 std::string varName = writeVarConfigs[j].getString(
"name");
87 std::string varType = writeVarConfigs[j].getString(
"type");
88 std::vector<std::string> varDimNames =
89 writeVarConfigs[j].getStringVector(
"dims");
93 std::vector<ioda::Variable> varDims;
94 for (std::size_t idim = 0; idim < varDimNames.size(); ++idim) {
95 varDims.push_back(obsIo->vars().open(varDimNames[idim]));
100 params.compressWithGZIP();
101 if (varType ==
"int") {
102 params.setFillValue<
int>(-999);
104 .createWithScales<
int>(varName, varDims,
params);
105 }
else if (varType ==
"float") {
106 params.setFillValue<
float>(-999);
108 .createWithScales<
float>(varName, varDims,
params);
109 }
else if (varType ==
"string") {
110 params.setFillValue<std::string>(
"fill");
112 .createWithScales<std::string>(varName, varDims,
params);
116 if (varType ==
"int") {
117 std::vector<int> varValues =
118 writeVarConfigs[j].getIntVector(
"values");
119 var.
write<
int>(varValues);
120 }
else if (varType ==
"float") {
121 std::vector<float> varValues =
122 writeVarConfigs[j].getFloatVector(
"values");
123 var.
write<
float>(varValues);
124 }
else if (varType ==
"string") {
125 std::vector<std::string> varValues =
126 writeVarConfigs[j].getStringVector(
"values");
127 var.
write<std::string>(varValues);
132 obsIo->updateVarDimInfo();
136 std::vector<std::string> expectedDimList;
137 for (
size_t i = 0; i < writeDimConfigs.size(); ++i) {
138 expectedDimList.push_back(writeDimConfigs[i].getString(
"name"));
140 std::sort(expectedDimList.begin(), expectedDimList.end());
142 std::sort(dimList.begin(), dimList.end(), [](
auto &
p1,
auto & p2) {
143 return (p1.first < p2.first); });
144 for (
size_t i = 0; i < dimList.size(); ++i) {
145 EXPECT_EQUAL(dimList[i].first, expectedDimList[i]);
149 std::vector<std::string> expectedVarList;
150 for (
size_t i = 0; i < writeVarConfigs.size(); ++i) {
151 expectedVarList.push_back(writeVarConfigs[i].getString(
"name"));
153 std::sort(expectedVarList.begin(), expectedVarList.end());
155 std::sort(varList.begin(), varList.end(), [](
auto &
p1,
auto & p2) {
156 return (p1.first < p2.first); });
157 for (
size_t i = 0; i < dimList.size(); ++i) {
158 EXPECT_EQUAL(varList[i].first, expectedVarList[i]);
162 for (std::size_t j = 0; j < writeVarConfigs.size(); ++j) {
163 std::string varName = writeVarConfigs[j].getString(
"name");
164 std::string varType = writeVarConfigs[j].getString(
"type");
168 Variable var = obsIo->vars().open(varName);
169 if (varType ==
"int") {
170 std::vector<int> expectedVarValues =
171 writeVarConfigs[j].getIntVector(
"values");
172 std::vector<int> varValues;
173 var.read<
int>(varValues);
174 EXPECT_EQUAL(varValues, expectedVarValues);
175 }
else if (varType ==
"float") {
176 std::vector<float> expectedVarValues =
177 writeVarConfigs[j].getFloatVector(
"values");
178 std::vector<float> varValues;
179 var.read<
float>(varValues);
180 EXPECT_EQUAL(varValues, expectedVarValues);
181 }
else if (varType ==
"string") {
182 std::vector<std::string> expectedVarValues =
183 writeVarConfigs[j].getStringVector(
"values");
184 std::vector<std::string> varValues;
185 var.read<std::string>(varValues);
186 EXPECT_EQUAL(varValues, expectedVarValues);
200 std::string
testid()
const override {
return "test::ObsIoWrite";}
203 std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
205 ts.emplace_back(
CASE(
"ioda/ObsIoWrite/testWrite")
Interfaces for ioda::ObsGroup and related classes.
Interfaces for ioda::Variable and related classes.
static std::shared_ptr< ObsIo > create(ObsIoModes mode, const ObsSpaceParameters ¶meters)
Create and return a new instance of an ObsIo subclass.
ObsTopLevelParameters top_level_
sub groups of parameters
oops::OptionalParameter< ObsFileOutParameters > obsOutFile
output specification by writing to a file
virtual Variable write(gsl::span< char > data, const Type &in_memory_dataType, const Selection &mem_selection=Selection::all, const Selection &file_selection=Selection::all)
The fundamental write function. Backends overload this function to implement all write operations.
void register_tests() const override
void clear() const override
std::string testid() const override
CASE("Derived variable, unit conversion, and exception checking methods")
std::vector< std::pair< std::string, Variable > > VarNameObjectList
typedef for holding list of variable names with associated variable object
void setOfileParamsFromTestConfig(const eckit::LocalConfiguration &obsConfig, ioda::ObsSpaceParameters &obsParams)
set params for output file construction from test YAML configuration
Used to specify Variable creation-time properties.