netxsimdg
Loading...
Searching...
No Matches
NetcdfMetadataConfiguration.cpp
Go to the documentation of this file.
1
9
10#include <map>
11#include <ncFile.h>
12#include <ncGroup.h>
13#include <ncVar.h>
14
15namespace Nextsim {
16
17std::stringstream NetcdfMetadataConfiguration::read(const std::string& source)
18{
19 // Parse the current configuration to look for fileOptionName
20 boost::program_options::options_description opt;
21 opt.add_options()(
22 source.c_str(), boost::program_options::value<std::string>()->default_value(""), "");
23 std::string fileName = Configurator::parse(opt)[source].as<std::string>();
24
25 netCDF::NcFile ncFile(fileName, netCDF::NcFile::read);
26 // Get the configuration group, allowing for it to not exist. In which
27 // case, do nothing
28 netCDF::NcGroup metadataGroup(ncFile.getGroup("metadata"));
29 if (metadataGroup.isNull()) {
30 return std::stringstream();
31 }
32 netCDF::NcGroup configurationGroup(metadataGroup.getGroup("configuration"));
33
34 std::stringstream config;
35
36 std::multimap<std::string, netCDF::NcVar> configs = configurationGroup.getVars();
37 for (auto entry : configs) {
38 config << entry.first << " = ";
39 if (entry.second.getType() == netCDF::ncDouble) {
40 double value;
41 entry.second.getVar(&value);
42 config << value;
43 } else if (entry.second.getType() == netCDF::ncInt) {
44 int value;
45 entry.second.getVar(&value);
46 config << value;
47 } else if (entry.second.getType() == netCDF::ncUint) {
48 unsigned value;
49 entry.second.getVar(&value);
50 config << value;
51 } else if (entry.second.getType() == netCDF::ncString) {
52 char* cValue;
53 entry.second.getVar(&cValue);
54 config << cValue;
55 }
56 config << std::endl;
57 }
58
59 ncFile.close();
60
61 return config;
62}
63
64} /* namespace Nextsim */
static boost::program_options::variables_map parse(const boost::program_options::options_description &opt)
Parses all configuration sources.
std::stringstream read(const std::string &source) override
Reads the additional configuration from the provided source, which is interpreted in an implementatio...