From b07a2a86220242878333835907c829737b679bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Fri, 22 Jul 2022 14:58:34 +0200 Subject: [PATCH] Automatically detect dataset/template mode --- examples/14_toml_template.cpp | 24 +++++--- include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp | 1 + src/IO/JSON/JSONIOHandlerImpl.cpp | 61 +++++++++++++++++++ 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/examples/14_toml_template.cpp b/examples/14_toml_template.cpp index be2f7657fe..8630577304 100644 --- a/examples/14_toml_template.cpp +++ b/examples/14_toml_template.cpp @@ -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"( { diff --git a/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp b/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp index aa8a44dc94..117d923128 100644 --- a/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp +++ b/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp @@ -242,6 +242,7 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl }; IOMode m_mode = IOMode::Dataset; + bool m_modeWasManuallySpecified = false; /* * Is set by constructor. diff --git a/src/IO/JSON/JSONIOHandlerImpl.cpp b/src/IO/JSON/JSONIOHandlerImpl.cpp index 168fd19771..a8296fdb0a 100644 --- a/src/IO/JSON/JSONIOHandlerImpl.cpp +++ b/src/IO/JSON/JSONIOHandlerImpl.cpp @@ -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 @@ -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) @@ -1580,6 +1588,55 @@ std::shared_ptr 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; } @@ -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; }