Skip to content

Commit c6a369c

Browse files
committed
Automatically detect dataset/template mode
1 parent e625e9b commit c6a369c

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed

examples/14_toml_template.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,22 @@ void write()
6161

6262
void read()
6363
{
64-
std::string config = R"(
65-
{
66-
"iteration_encoding": "variable_based",
67-
"toml": {
68-
"mode": "template"
69-
}
70-
}
71-
)";
64+
/*
65+
* The config is entirely optional, these things are also detected
66+
* automatically when reading
67+
*/
68+
69+
// std::string config = R"(
70+
// {
71+
// "iteration_encoding": "variable_based",
72+
// "toml": {
73+
// "mode": "template"
74+
// }
75+
// }
76+
// )";
77+
7278
openPMD::Series read(
73-
"../samples/tomlTemplate.toml", openPMD::Access::READ_ONLY, config);
79+
"../samples/tomlTemplate.toml", openPMD::Access::READ_ONLY);
7480

7581
std::string jsonConfig = R"(
7682
{

include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl
244244
};
245245

246246
IOMode m_mode = IOMode::Dataset;
247+
bool m_modeWasManuallySpecified = false;
247248

248249
/*
249250
* Is set by constructor.

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ namespace openPMD
5858
throw std::runtime_error((TEXT)); \
5959
}
6060

61+
namespace JSONDefaults
62+
{
63+
using const_str = char const *const;
64+
constexpr const_str openpmd_internal = "__openPMD_internal";
65+
constexpr const_str IOMode = "IO_mode";
66+
} // namespace JSONDefaults
67+
6168
namespace
6269
{
6370
struct DefaultValue
@@ -166,6 +173,7 @@ JSONIOHandlerImpl::JSONIOHandlerImpl(
166173
"Invalid value: '" + mode +
167174
"' (accepted values are 'dataset' and 'template'.");
168175
}
176+
m_modeWasManuallySpecified = true;
169177
}
170178
auto shadow = jsonConfig.invertShadow();
171179
if (shadow.size() > 0)
@@ -1600,6 +1608,55 @@ std::shared_ptr<nlohmann::json> JSONIOHandlerImpl::obtainJsonContents(File file)
16001608
break;
16011609
}
16021610
VERIFY(fh->good(), "[JSON] Failed reading from a file.");
1611+
if (res->contains(JSONDefaults::openpmd_internal))
1612+
{
1613+
auto const &openpmd_internal = res->at(JSONDefaults::openpmd_internal);
1614+
if (openpmd_internal.contains(JSONDefaults::IOMode))
1615+
{
1616+
auto modeOption = openPMD::json::asLowerCaseStringDynamic(
1617+
openpmd_internal.at(JSONDefaults::IOMode));
1618+
if (!modeOption.has_value())
1619+
{
1620+
std::cerr
1621+
<< "[JSON/TOML backend] Warning: Invalid value of "
1622+
"non-string type at internal meta table for entry '"
1623+
<< JSONDefaults::IOMode << "'. Will ignore and continue."
1624+
<< std::endl;
1625+
}
1626+
else if (modeOption.value() == "dataset")
1627+
{
1628+
if (m_modeWasManuallySpecified && m_mode == IOMode::Template)
1629+
{
1630+
std::cerr
1631+
<< "[JSON/TOML backend] Warning: IO Mode was manually "
1632+
"specified as 'Template', but opened file is in "
1633+
"'Dataset' mode. Will continue with dataset mode."
1634+
<< std::endl;
1635+
}
1636+
m_mode = IOMode::Dataset;
1637+
}
1638+
else if (modeOption.value() == "template")
1639+
{
1640+
if (m_modeWasManuallySpecified && m_mode == IOMode::Dataset)
1641+
{
1642+
std::cerr
1643+
<< "[JSON/TOML backend] Warning: IO Mode was manually "
1644+
"specified as 'Dataset', but opened file is in "
1645+
"'Template' mode. Will continue with template mode."
1646+
<< std::endl;
1647+
}
1648+
m_mode = IOMode::Template;
1649+
}
1650+
else
1651+
{
1652+
std::cerr << "[JSON/TOML backend] Warning: Invalid value '"
1653+
<< modeOption.value()
1654+
<< "' at internal meta table for entry '"
1655+
<< JSONDefaults::IOMode
1656+
<< "'. Will ignore and continue." << std::endl;
1657+
}
1658+
}
1659+
}
16031660
m_jsonVals.emplace(file, res);
16041661
return res;
16051662
}
@@ -1628,8 +1685,12 @@ void JSONIOHandlerImpl::putJsonContents(
16281685
{
16291686
case IOMode::Dataset:
16301687
(*it->second)["platform_byte_widths"] = platformSpecifics();
1688+
(*it->second)[JSONDefaults::openpmd_internal]
1689+
[JSONDefaults::IOMode] = "dataset";
16311690
break;
16321691
case IOMode::Template:
1692+
(*it->second)[JSONDefaults::openpmd_internal]
1693+
[JSONDefaults::IOMode] = "template";
16331694
break;
16341695
}
16351696

0 commit comments

Comments
 (0)