Skip to content

Commit

Permalink
Automatically detect dataset/template mode
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Aug 15, 2022
1 parent 292876c commit b07a2a8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
24 changes: 15 additions & 9 deletions examples/14_toml_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,22 @@ void write()

void read()
{
std::string config = R"(
{
"iteration_encoding": "variable_based",
"toml": {
"mode": "template"
}
}
)";
/*
* The config is entirely optional, these things are also detected
* automatically when reading
*/

// std::string config = R"(
// {
// "iteration_encoding": "variable_based",
// "toml": {
// "mode": "template"
// }
// }
// )";

openPMD::Series read(
"../samples/tomlTemplate.toml", openPMD::Access::READ_ONLY, config);
"../samples/tomlTemplate.toml", openPMD::Access::READ_ONLY);

std::string jsonConfig = R"(
{
Expand Down
1 change: 1 addition & 0 deletions include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl
};

IOMode m_mode = IOMode::Dataset;
bool m_modeWasManuallySpecified = false;

/*
* Is set by constructor.
Expand Down
61 changes: 61 additions & 0 deletions src/IO/JSON/JSONIOHandlerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ namespace openPMD
throw std::runtime_error((TEXT)); \
}

namespace JSONDefaults
{
using const_str = char const *const;
constexpr const_str openpmd_internal = "__openPMD_internal";
constexpr const_str IOMode = "IO_mode";
} // namespace JSONDefaults

namespace
{
struct DefaultValue
Expand Down Expand Up @@ -166,6 +173,7 @@ JSONIOHandlerImpl::JSONIOHandlerImpl(
"Invalid value: '" + mode +
"' (accepted values are 'dataset' and 'template'.");
}
m_modeWasManuallySpecified = true;
}
auto shadow = jsonConfig.invertShadow();
if (shadow.size() > 0)
Expand Down Expand Up @@ -1580,6 +1588,55 @@ std::shared_ptr<nlohmann::json> JSONIOHandlerImpl::obtainJsonContents(File file)
break;
}
VERIFY(fh->good(), "[JSON] Failed reading from a file.");
if (res->contains(JSONDefaults::openpmd_internal))
{
auto const &openpmd_internal = res->at(JSONDefaults::openpmd_internal);
if (openpmd_internal.contains(JSONDefaults::IOMode))
{
auto modeOption = openPMD::json::asLowerCaseStringDynamic(
openpmd_internal.at(JSONDefaults::IOMode));
if (!modeOption.has_value())
{
std::cerr
<< "[JSON/TOML backend] Warning: Invalid value of "
"non-string type at internal meta table for entry '"
<< JSONDefaults::IOMode << "'. Will ignore and continue."
<< std::endl;
}
else if (modeOption.value() == "dataset")
{
if (m_modeWasManuallySpecified && m_mode == IOMode::Template)
{
std::cerr
<< "[JSON/TOML backend] Warning: IO Mode was manually "
"specified as 'Template', but opened file is in "
"'Dataset' mode. Will continue with dataset mode."
<< std::endl;
}
m_mode = IOMode::Dataset;
}
else if (modeOption.value() == "template")
{
if (m_modeWasManuallySpecified && m_mode == IOMode::Dataset)
{
std::cerr
<< "[JSON/TOML backend] Warning: IO Mode was manually "
"specified as 'Dataset', but opened file is in "
"'Template' mode. Will continue with template mode."
<< std::endl;
}
m_mode = IOMode::Template;
}
else
{
std::cerr << "[JSON/TOML backend] Warning: Invalid value '"
<< modeOption.value()
<< "' at internal meta table for entry '"
<< JSONDefaults::IOMode
<< "'. Will ignore and continue." << std::endl;
}
}
}
m_jsonVals.emplace(file, res);
return res;
}
Expand Down Expand Up @@ -1608,8 +1665,12 @@ void JSONIOHandlerImpl::putJsonContents(
{
case IOMode::Dataset:
(*it->second)["platform_byte_widths"] = platformSpecifics();
(*it->second)[JSONDefaults::openpmd_internal]
[JSONDefaults::IOMode] = "dataset";
break;
case IOMode::Template:
(*it->second)[JSONDefaults::openpmd_internal]
[JSONDefaults::IOMode] = "template";
break;
}

Expand Down

0 comments on commit b07a2a8

Please sign in to comment.