Skip to content

Commit f19676c

Browse files
committed
Full support for the set_filter API
1 parent 34a0557 commit f19676c

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

examples/7_extended_write_serial.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ int main()
123123
}
124124
]
125125
}
126+
},
127+
"hdf5": {
128+
"dataset": {
129+
"chunks": "auto",
130+
"permanent_filters": {
131+
"id": "fletcher32",
132+
"flags": "optional"
133+
}
134+
}
126135
}
127136
})END";
128137
d.options = datasetConfig;
@@ -157,7 +166,7 @@ int main()
157166
158167
hdf5.dataset.permanent_filters = [
159168
{type = "zlib", aggression = 5},
160-
{id = "shuffle"}
169+
{id = "shuffle", "flags" = "MANDATORY"}
161170
]
162171
)");
163172
electrons.particlePatches["numParticles"].resetDataset(dset);

src/IO/HDF5/HDF5IOHandler.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "openPMD/auxiliary/JSON_internal.hpp"
2828
#include "openPMD/auxiliary/Variant.hpp"
2929
#include <H5Ppublic.h>
30+
#include <nlohmann/json.hpp>
3031
#include <optional>
3132
#include <sstream>
3233
#include <stdexcept>
@@ -600,6 +601,65 @@ namespace
600601
}
601602
throw id_error();
602603
}();
604+
byID.flags = [&]() -> unsigned int {
605+
if (!json_accessor(filter_config).contains("flags"))
606+
{
607+
return 0;
608+
}
609+
auto const &flag_config = json_accessor(filter_config["flags"]);
610+
using pair_t = std::pair<std::string, unsigned int>;
611+
std::array<pair_t, 2> filter_types{
612+
pair_t{"optional", H5Z_FLAG_OPTIONAL},
613+
pair_t{"mandatory", H5Z_FLAG_MANDATORY}};
614+
auto flag_error = [&]() {
615+
std::stringstream error;
616+
error
617+
<< "Must be either of unsigned integer type or one of:";
618+
for (auto const &pair : filter_types)
619+
{
620+
error << " '" << pair.first << "'";
621+
}
622+
error << ".";
623+
return error::BackendConfigSchema(
624+
{"hdf5", "dataset", "permanent_filters", "flags"},
625+
error.str());
626+
};
627+
if (flag_config.is_number_integer())
628+
{
629+
return flag_config.template get<unsigned int>();
630+
}
631+
auto maybe_string = json::asLowerCaseStringDynamic(flag_config);
632+
if (!maybe_string.has_value())
633+
{
634+
throw flag_error();
635+
}
636+
for (auto const &[key, res_type] : filter_types)
637+
{
638+
if (*maybe_string == key)
639+
{
640+
return res_type;
641+
}
642+
}
643+
throw flag_error();
644+
}();
645+
if (json_accessor(filter_config).contains("c_values"))
646+
{
647+
auto const &c_values_config =
648+
json_accessor(filter_config["c_values"]);
649+
try
650+
{
651+
652+
byID.c_values =
653+
c_values_config
654+
.template get<std::vector<unsigned int>>();
655+
}
656+
catch (nlohmann::json::type_error const &)
657+
{
658+
throw error::BackendConfigSchema(
659+
{"hdf5", "dataset", "permanent_filters", "c_values"},
660+
"Must be an array of unsigned integers.");
661+
}
662+
}
603663
return byID;
604664
}
605665
break;

0 commit comments

Comments
 (0)