Skip to content

Commit

Permalink
Select template mode via JSON param
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed May 17, 2022
1 parent ab29e02 commit 5c15f37
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
10 changes: 9 additions & 1 deletion examples/14_json_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

int main()
{
std::string config = R"(
iteration_encoding = "variable_based"
[json]
mode = "template"
)";


openPMD::Series writeTemplate(
"../samples/jsonTemplate.json",
openPMD::Access::CREATE,
R"(iteration_encoding = "variable_based")");
config);
auto iteration = writeTemplate.writeIterations()[0];

auto temperature =
Expand Down
4 changes: 3 additions & 1 deletion include/openPMD/IO/JSON/JSONIOHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@

#include "openPMD/IO/AbstractIOHandler.hpp"
#include "openPMD/IO/JSON/JSONIOHandlerImpl.hpp"
#include "openPMD/auxiliary/JSON_internal.hpp"

namespace openPMD
{
class JSONIOHandler : public AbstractIOHandler
{
public:
JSONIOHandler(std::string path, Access at);
JSONIOHandler(
std::string path, Access at, openPMD::json::TracingJSON config);

~JSONIOHandler() override;

Expand Down
6 changes: 4 additions & 2 deletions include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "openPMD/IO/Access.hpp"
#include "openPMD/IO/JSON/JSONFilePosition.hpp"
#include "openPMD/auxiliary/Filesystem.hpp"
#include "openPMD/auxiliary/JSON_internal.hpp"
#include "openPMD/config.hpp"

#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -153,7 +154,8 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl
using json = nlohmann::json;

public:
explicit JSONIOHandlerImpl(AbstractIOHandler *);
explicit JSONIOHandlerImpl(
AbstractIOHandler *, openPMD::json::TracingJSON config);

~JSONIOHandlerImpl() override;

Expand Down Expand Up @@ -230,7 +232,7 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl
Template
};

IOMode m_mode = IOMode::Template;
IOMode m_mode = IOMode::Dataset;

// HELPER FUNCTIONS

Expand Down
2 changes: 1 addition & 1 deletion src/IO/AbstractIOHandlerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ std::shared_ptr<AbstractIOHandler> createIOHandler<json::TracingJSON>(
"ADIOS2", path, access, std::move(options), "ssc");
case Format::JSON:
return constructIOHandler<JSONIOHandler, openPMD_HAVE_JSON>(
"JSON", path, access);
"JSON", path, access, std::move(options));
default:
throw std::runtime_error(
"Unknown file format! Did you specify a file ending?");
Expand Down
5 changes: 3 additions & 2 deletions src/IO/JSON/JSONIOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ namespace openPMD
{
JSONIOHandler::~JSONIOHandler() = default;

JSONIOHandler::JSONIOHandler(std::string path, Access at)
: AbstractIOHandler{path, at}, m_impl{JSONIOHandlerImpl{this}}
JSONIOHandler::JSONIOHandler(
std::string path, Access at, openPMD::json::TracingJSON jsonCfg)
: AbstractIOHandler{path, at}, m_impl{this, std::move(jsonCfg)}
{}

std::future<void> JSONIOHandler::flush(internal::FlushParams const &)
Expand Down
57 changes: 55 additions & 2 deletions src/IO/JSON/JSONIOHandlerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,62 @@ namespace openPMD
throw std::runtime_error((TEXT)); \
}

JSONIOHandlerImpl::JSONIOHandlerImpl(AbstractIOHandler *handler)
JSONIOHandlerImpl::JSONIOHandlerImpl(
AbstractIOHandler *handler, openPMD::json::TracingJSON config)
: AbstractIOHandlerImpl(handler)
{}
{
if (config.json().contains("json"))
{
auto jsonConfig = config["json"];
if (jsonConfig.json().contains("mode"))
{
auto modeOption = openPMD::json::asLowerCaseStringDynamic(
jsonConfig["mode"].json());
if (!modeOption.has_value())
{
throw error::BackendConfigSchema(
{"json", "mode"},
"Invalid value of non-string type (accepted values are "
"'dataset' and 'template'.");
}
auto mode = modeOption.value();
if (mode == "dataset")
{
m_mode = IOMode::Dataset;
}
else if (mode == "template")
{
m_mode = IOMode::Template;
}
else
{
throw error::BackendConfigSchema(
{"json", "mode"},
"Invalid value: '" + mode +
"' (accepted values are 'dataset' and 'template'.");
}
}
auto shadow = jsonConfig.invertShadow();
if (shadow.size() > 0)
{
switch (jsonConfig.originallySpecifiedAs)
{
case openPMD::json::SupportedLanguages::JSON:
std::cerr << "Warning: parts of the backend configuration for "
"JSON backend remain unused:\n"
<< shadow << std::endl;
break;
case openPMD::json::SupportedLanguages::TOML: {
auto asToml = openPMD::json::jsonToToml(shadow);
std::cerr << "Warning: parts of the backend configuration for "
"JSON backend remain unused:\n"
<< asToml << std::endl;
break;
}
}
}
}
}

JSONIOHandlerImpl::~JSONIOHandlerImpl()
{
Expand Down

0 comments on commit 5c15f37

Please sign in to comment.