Skip to content

Commit 1db63f6

Browse files
committed
Automatically detect dataset/template mode
1 parent 3bbe2b9 commit 1db63f6

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
@@ -242,6 +242,7 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl
242242
};
243243

244244
IOMode m_mode = IOMode::Dataset;
245+
bool m_modeWasManuallySpecified = false;
245246

246247
/*
247248
* 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)
@@ -1580,6 +1588,55 @@ std::shared_ptr<nlohmann::json> JSONIOHandlerImpl::obtainJsonContents(File file)
15801588
break;
15811589
}
15821590
VERIFY(fh->good(), "[JSON] Failed reading from a file.");
1591+
if (res->contains(JSONDefaults::openpmd_internal))
1592+
{
1593+
auto const &openpmd_internal = res->at(JSONDefaults::openpmd_internal);
1594+
if (openpmd_internal.contains(JSONDefaults::IOMode))
1595+
{
1596+
auto modeOption = openPMD::json::asLowerCaseStringDynamic(
1597+
openpmd_internal.at(JSONDefaults::IOMode));
1598+
if (!modeOption.has_value())
1599+
{
1600+
std::cerr
1601+
<< "[JSON/TOML backend] Warning: Invalid value of "
1602+
"non-string type at internal meta table for entry '"
1603+
<< JSONDefaults::IOMode << "'. Will ignore and continue."
1604+
<< std::endl;
1605+
}
1606+
else if (modeOption.value() == "dataset")
1607+
{
1608+
if (m_modeWasManuallySpecified && m_mode == IOMode::Template)
1609+
{
1610+
std::cerr
1611+
<< "[JSON/TOML backend] Warning: IO Mode was manually "
1612+
"specified as 'Template', but opened file is in "
1613+
"'Dataset' mode. Will continue with dataset mode."
1614+
<< std::endl;
1615+
}
1616+
m_mode = IOMode::Dataset;
1617+
}
1618+
else if (modeOption.value() == "template")
1619+
{
1620+
if (m_modeWasManuallySpecified && m_mode == IOMode::Dataset)
1621+
{
1622+
std::cerr
1623+
<< "[JSON/TOML backend] Warning: IO Mode was manually "
1624+
"specified as 'Dataset', but opened file is in "
1625+
"'Template' mode. Will continue with template mode."
1626+
<< std::endl;
1627+
}
1628+
m_mode = IOMode::Template;
1629+
}
1630+
else
1631+
{
1632+
std::cerr << "[JSON/TOML backend] Warning: Invalid value '"
1633+
<< modeOption.value()
1634+
<< "' at internal meta table for entry '"
1635+
<< JSONDefaults::IOMode
1636+
<< "'. Will ignore and continue." << std::endl;
1637+
}
1638+
}
1639+
}
15831640
m_jsonVals.emplace(file, res);
15841641
return res;
15851642
}
@@ -1608,8 +1665,12 @@ void JSONIOHandlerImpl::putJsonContents(
16081665
{
16091666
case IOMode::Dataset:
16101667
(*it->second)["platform_byte_widths"] = platformSpecifics();
1668+
(*it->second)[JSONDefaults::openpmd_internal]
1669+
[JSONDefaults::IOMode] = "dataset";
16111670
break;
16121671
case IOMode::Template:
1672+
(*it->second)[JSONDefaults::openpmd_internal]
1673+
[JSONDefaults::IOMode] = "template";
16131674
break;
16141675
}
16151676

0 commit comments

Comments
 (0)