Commit 2516bf3e authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Adding cfgfile interface to support loading HYSPLIT SETUP.CFG.

parent dfafc980
Pipeline #12214 passed with stages
in 4 minutes and 48 seconds
......@@ -4,6 +4,7 @@ TRIBITS_SUBPACKAGE(io)
SET(SOURCE
commandline.cc
csvfile.cc
cfgfile.cc
eafstream.cc
endian.cc
gfsfile.cc
......@@ -15,6 +16,7 @@ SET(HEADERS
commandline.hh
commandline.i.hh
csvfile.hh
cfgfile.hh
eafstream.hh
endian.hh
gfsfile.hh
......
#include <fstream>
#include <string>
#include "radixcore/stringfunctions.hh"
#include "radixio/cfgfile.hh"
namespace radix
{
radix::CFGFile::CFGFile(const std::string& file)
: mFile(file)
{
std::ifstream in;
in.open(mFile, std::ios::in);
if (!in.is_open())
throw std::logic_error(std::string("***Error: file could not be opened:") +
file);
std::string line;
bool key_captured = false;
while (std::getline(in, line))
{
// trim string
line = radix::trim_string(line);
if (line.empty()) continue;
if (!key_captured)
{
// capture the key
// &KEY_NAME
if (line.size() < 2)
throw std::logic_error(
"***Error: Block key must be at least 1 character.");
mKey = line.substr(1);
key_captured = true;
}
else
{
// capture parameters
// key = value,
// '/' terminates
// check exit criteria
if (line.compare("/") == 0) break;
// split on '='
auto pos = line.find("=");
if (pos == std::string::npos)
std::cerr << "**Warning: Ignoring line '" << line
<< "' with out '=' character." << std::endl;
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
// remove trailing ','
auto comma_pos = value.find(",");
if (comma_pos != std::string::npos)
{
value = value.erase(comma_pos);
}
// trim strings
key = radix::trim_string(key);
value = radix::trim_string(value);
radix_tagged_line(key << " = " << value);
mParameters.insert(std::make_pair(key, value));
}
}
in.close();
}
std::string CFGFile::file() const { return mFile; }
std::string CFGFile::key() const { return mKey; }
const std::unordered_map<std::string, std::string>& CFGFile::parameters() const
{
return mParameters;
}
} // namespace radix
#ifndef RADIXIO_CFGFILE_HH_
#define RADIXIO_CFGFILE_HH_
#include <string>
#include <unordered_map>
#include "radixdl/visibility.hh"
namespace radix
{
/**
* @brief The CFGFile class
*/
class RADIX_PUBLIC CFGFile
{
private:
std::string mFile;
std::unordered_map<std::string, std::string> mParameters;
std::string mKey;
public:
CFGFile(const std::string& file);
std::string file() const;
std::string key() const;
const std::unordered_map<std::string, std::string>& parameters() const;
}; // class CFGFile
} // namespace radix
#endif /** RADIXIO_CFGFILE_HH_ */
TRIBITS_PACKAGE_DEFINE_DEPENDENCIES(
LIB_REQUIRED_PACKAGES radixdl radixmath
LIB_REQUIRED_PACKAGES radixdl radixmath radixcore
LIB_OPTIONAL_PACKAGES
TEST_REQUIRED_PACKAGES testframework
TEST_OPTIONAL_PACKAGES
......
INCLUDE(GoogleTest)
ADD_GOOGLE_TEST(tstCSVFile.cc NP 1)
ADD_GOOGLE_TEST(tstCFGFile.cc NP 1)
ADD_GOOGLE_TEST(tstEafstream.cc NP 1)
ADD_GOOGLE_TEST(tstHysplitCDump.cc NP 1)
&SETUP
cmass = 0,
conage = 48,
cpack = 1,
delt = 1,
dxf = 1,
dyf = 1,
dzf = 0.010000,
frhmax = 3,
frhs = 1,
frme = 0.100000,
frmr = 0,
frts = 0.100000,
frvs = 0.010000,
hscale = 10800,
ichem = 0,
initd = 0,
k10m = 1,
kbls = 1,
kblt = 2,
kdef = 0,
khinp = 0,
khmax = 9999,
kmixd = 0,
kmix0 = 250,
kmsl = 0,
kpuff = 0,
krand = 2,
krnd = 6,
kspl = 1,
kwet = 0,
kzmix = 0,
maxpar = 500000000,
messg = 'MESSAGE',
mgmin = 10,
nbptyp = 1,
ncycl = 0,
ndump = 0,
ninit = 1,
numpar = 5000000,
p10f = 1,
pinbc = 'PARINBC',
pinpf = 'PARINIT',
qcycle = 0,
rhb = 80,
rht = 60,
splitf = 1,
tkerd = 0.180000,
tkern = 0.180000,
tratio = 0.750000,
tvmix = 1,
vdist = 'VMSDIST',
vscale = 200,
vscales = 200,
vscaleu = 200,
isot = -99,
efile = 'EMITIMES',
/
#include "gtest/gtest.h"
#include "radixcore/system.hh"
#include "radixio/cfgfile.hh"
#include <fstream>
using namespace radix;
TEST(Radix, CFGFile)
{
CFGFile csv(to_native_path(dirname(__FILE__) + "/data/SETUP.CFG"));
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment