diff --git a/pyphare/pyphare/pharein/__init__.py b/pyphare/pyphare/pharein/__init__.py index 202b13eee..5342dfab1 100644 --- a/pyphare/pyphare/pharein/__init__.py +++ b/pyphare/pyphare/pharein/__init__.py @@ -70,24 +70,20 @@ def getSimulation(): return sim -def _patch_data_ids(path): +def _patch_data_ids(restart_file_dir): """ for restarts we save samrai patch data ids to the restart files, which we access from here to tell samrai which patch datas to load from the restart file on restart """ - import h5py from pyphare.cpp import cpp_etc_lib - h5File = cpp_etc_lib().samrai_restart_file(path) - return h5py.File(h5File, "r")["phare"]["patch"]["ids"][:] + return cpp_etc_lib().patch_data_ids(restart_file_dir) -def _serialized_simulation_string(path): - import h5py +def _serialized_simulation_string(restart_file_dir): from pyphare.cpp import cpp_etc_lib - h5File = cpp_etc_lib().samrai_restart_file(path) - return h5py.File(h5File, "r")["phare"].attrs["serialized_simulation"] + return cpp_etc_lib().serialized_simulation_string(restart_file_dir) # converts scalars to array of expected size diff --git a/src/hdf5/detail/h5/h5_file.hpp b/src/hdf5/detail/h5/h5_file.hpp index 69980515c..4f92645ff 100644 --- a/src/hdf5/detail/h5/h5_file.hpp +++ b/src/hdf5/detail/h5/h5_file.hpp @@ -69,7 +69,10 @@ class HighFiveFile ~HighFiveFile() {} - NO_DISCARD HiFile& file() { return h5file_; } + NO_DISCARD HiFile& file() + { + return h5file_; + } template @@ -235,6 +238,15 @@ class HighFiveFile } } + template + auto read_attribute(std::string const& path, std::string const& key) + { + auto at = h5file_.getGroup(path).getAttribute(key); + Attr attr; + at.read(attr); + return attr; + } + HighFiveFile(const HighFiveFile&) = delete; HighFiveFile(const HighFiveFile&&) = delete; diff --git a/src/python3/cpp_etc.cpp b/src/python3/cpp_etc.cpp index 0550c5a0a..5dddfdd3d 100644 --- a/src/python3/cpp_etc.cpp +++ b/src/python3/cpp_etc.cpp @@ -33,6 +33,9 @@ auto samrai_version() PYBIND11_MODULE(cpp_etc, m) { + auto samrai_restart_file = [](std::string path) { + return PHARE::amr::HierarchyRestarter::getRestartFileFullPath(path); + }; py::class_, std::shared_ptr>>(m, "Span"); py::class_, std::shared_ptr>, core::Span>( m, "PyWrapper"); @@ -46,14 +49,33 @@ PYBIND11_MODULE(cpp_etc, m) return versions; }); - m.def("samrai_restart_file", [](std::string path) { - return PHARE::amr::HierarchyRestarter::getRestartFileFullPath(path); - }); + m.def("samrai_restart_file", samrai_restart_file); m.def("restart_path_for_time", [](std::string path, double timestamp) { return PHARE::amr::Hierarchy::restartFilePathForTime(path, timestamp); }); m.def("phare_build_config", []() { return PHARE::build_config(); }); + + m.def("patch_data_ids", [&](std::string const& path) -> std::vector { + _PHARE_WITH_HIGHFIVE({ + auto const& restart_file = samrai_restart_file(path); + PHARE::hdf5::h5::HighFiveFile h5File{restart_file, HighFive::File::ReadOnly, + /*para=*/false}; + return h5File.read_data_set("/phare/patch/ids"); + }); + + throw std::runtime_error("PHARE not built with highfive support"); + }); + m.def("serialized_simulation_string", [&](std::string const& path) -> std::string { + _PHARE_WITH_HIGHFIVE({ + auto const& restart_file = samrai_restart_file(path); + PHARE::hdf5::h5::HighFiveFile h5File{restart_file, HighFive::File::ReadOnly, + /*para=*/false}; + return h5File.read_attribute("/phare", "serialized_simulation"); + }); + + throw std::runtime_error("PHARE not built with highfive support"); + }); } } // namespace PHARE::pydata