HOPS
HOPS class reference
MHO_HDF5ConversionHelpers.hh
Go to the documentation of this file.
1 #ifndef MHO_HDF5ConversionHelpers_HH__
2 #define MHO_HDF5ConversionHelpers_HH__
3 
4 #include "MHO_ClassIdentity.hh"
7 
8 #include "MHO_Axis.hh"
9 #include "MHO_AxisPack.hh"
10 #include "MHO_ObjectTags.hh"
11 #include "MHO_ScalarContainer.hh"
12 #include "MHO_TableContainer.hh"
13 #include "MHO_Taggable.hh"
14 #include "MHO_VectorContainer.hh"
15 
16 #include <sstream>
17 
18 #include "hdf5.h"
19 #include "hdf5_hl.h"
20 
21 #include "MHO_HDF5Attributes.hh"
22 #include "MHO_HDF5Datasets.hh"
23 #include "MHO_HDF5TypeCode.hh"
24 #include "MHO_NumpyTypeCode.hh"
25 
26 namespace hops
27 {
28 
38 inline void json_to_hdf5_attributes(const mho_json& j, hid_t parent_group)
39 {
40  for(auto it = j.begin(); it != j.end(); ++it)
41  {
42  const std::string& key = it.key();
43  const mho_json& value = it.value();
44 
45  if(value.is_object())
46  {
47  hid_t subgroup = H5Gcreate(parent_group, key.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
48  json_to_hdf5_attributes(value, subgroup);
49  H5Gclose(subgroup);
50  }
51  else if(value.is_array() && value.size() != 0)
52  {
53  if(!(value.begin()->is_object()))
54  {
55  make_attribute(key, value, parent_group);
56  }
57  else
58  {
59  hid_t array_group = H5Gcreate(parent_group, key.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
60  int idx = 0;
61  for(const auto& elem : value)
62  {
63  std::string item_name = std::to_string(idx++);
64  hid_t item_group = H5Gcreate(array_group, item_name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
65  json_to_hdf5_attributes(elem, item_group);
66  H5Gclose(item_group);
67  }
68  H5Gclose(array_group);
69  }
70  }
71  else
72  {
73  make_attribute(key, value, parent_group);
74  }
75  }
76 }
77 
78 } // namespace hops
79 
80 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
Definition: MHO_AdhocFlagging.hh:18
void make_attribute(const std::string &key, XValueType value, hid_t parent_dataset_id)
Definition: MHO_HDF5Attributes.hh:37
void json_to_hdf5_attributes(const mho_json &j, hid_t parent_group)
Definition: MHO_HDF5ConversionHelpers.hh:38
Definition: vex.h:175