1 #ifndef MHO_HDF5Attributes_HH__
2 #define MHO_HDF5Attributes_HH__
37 template <
typename XValueType >
40 if(H5Aexists(parent_dataset_id, key.c_str()) > 0)
42 msg_error(
"hdf5interface",
"attribute: "<<key<<
" already exists, skipping"<< eom);
46 hid_t TYPE_CODE = MHO_HDF5TypeCode<XValueType>();
47 hid_t attr_space = H5Screate(H5S_SCALAR);
48 hid_t attr_id = H5Acreate(parent_dataset_id, key.c_str(), TYPE_CODE, attr_space, H5P_DEFAULT, H5P_DEFAULT);
51 msg_error(
"hdf5interface",
"could not create attribute: "<<key<<
", id code is: "<<attr_id<< eom);
55 H5Awrite(attr_id, TYPE_CODE, &
value);
64 if(H5Aexists(parent_dataset_id, key.c_str()) > 0)
66 msg_error(
"hdf5interface",
"string attribute: "<<key<<
" already exists, skipping"<< eom);
72 msg_error(
"hdf5interface",
"string attribute: "<<key<<
" has size: "<<
value.size()<<
", skipping"<< eom);
76 hid_t attr_space = H5Screate(H5S_SCALAR);
77 hid_t TYPE_CODE = H5Tcopy(H5T_C_S1);
78 H5Tset_size(TYPE_CODE,
value.size());
79 H5Tset_strpad(TYPE_CODE, H5T_STR_NULLTERM);
80 hid_t attr_id = H5Acreate(parent_dataset_id, key.c_str(), TYPE_CODE, attr_space, H5P_DEFAULT, H5P_DEFAULT);
83 msg_error(
"hdf5interface",
"could not create string attribute: "<<key<<
", id code is: "<<attr_id<< eom);
86 H5Awrite(attr_id, TYPE_CODE,
value.c_str());
94 template<
typename XDataType >
97 const std::vector< XDataType >* data,
98 hid_t parent_dataset_id)
101 if(H5Aexists(parent_dataset_id, key.c_str()) > 0)
103 msg_error(
"hdf5interface",
"attribute: "<<key<<
" already exists, skipping"<< eom);
108 dims[0] = data->size();
109 hid_t attr_space = H5Screate_simple(1, dims, NULL);
111 hid_t TYPE_CODE = MHO_HDF5TypeCode<XDataType>();
112 hid_t attr_id = H5Acreate(parent_dataset_id, key.c_str(), TYPE_CODE, attr_space, H5P_DEFAULT, H5P_DEFAULT);
115 msg_error(
"hdf5interface",
"could not create vector attribute: "<<key<<
", id code is: "<<attr_id<< eom);
118 status = H5Awrite(attr_id, TYPE_CODE, data->data() );
120 H5Sclose(attr_space);
129 const std::vector< std::string >* data,
130 hid_t parent_dataset_id)
133 if(H5Aexists(parent_dataset_id, key.c_str()) > 0)
135 msg_error(
"hdf5interface",
"attribute: "<<key<<
" already exists, skipping"<< eom);
140 dims[0] = data->size();
141 hid_t TYPE_CODE = H5Tcopy(H5T_C_S1);
142 H5Tset_size(TYPE_CODE, H5T_VARIABLE);
145 std::vector<const char*> cstrs;
146 for(
const auto& s : *data)
148 cstrs.push_back(s.c_str());
150 hid_t attr_space = H5Screate_simple(1, dims, NULL);
151 hid_t attr_id = H5Acreate(parent_dataset_id, key.c_str(), TYPE_CODE, attr_space, H5P_DEFAULT, H5P_DEFAULT);
154 msg_error(
"hdf5interface",
"could not create string vector attribute: "<<key<<
", id code is: "<<attr_id<< eom);
157 status = H5Awrite(attr_id, TYPE_CODE, cstrs.data() );
159 H5Sclose(attr_space);
167 msg_debug(
"hdf5interface",
"creating attribute with key: "<< key << eom);
169 hid_t attr_space = H5Screate(H5S_SCALAR);
173 if (
value.is_string())
175 std::string strv =
value.get<std::string>();
178 else if (
value.is_number_integer())
180 int v =
value.get<
int>();
183 else if (
value.is_number_unsigned())
185 unsigned int v =
value.get<
unsigned int>();
188 else if (
value.is_number_float())
190 double v =
value.get<
double>();
193 else if (
value.is_boolean())
195 uint8_t v =
value.get<
bool>() ? 1 : 0;
198 else if (
value.is_array() &&
value.size() != 0)
200 if( !(
value.begin()->is_object() ) )
204 if(
value.begin()->is_number_integer())
206 std::vector<int> data =
value.get< std::vector<int> >();
209 if(
value.begin()->is_number_unsigned())
211 std::vector<unsigned int> data =
value.get< std::vector<unsigned int> >();
214 if(
value.begin()->is_number_float())
216 std::vector<double> data =
value.get< std::vector<double> >();
219 if(
value.begin()->is_string())
221 std::vector<std::string> data =
value.get< std::vector< std::string > >();
226 else if(
value.is_object() )
230 msg_debug(
"hdf5interface",
"composite object attribute: "<<key<<
", will stored as string" << eom);
231 std::stringstream ss;
233 std::string sval = ss.str();
#define msg_debug(xKEY, xCONTENT)
Definition: MHO_Message.hh:297
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
struct type_status status
Definition: fourfit3.c:53
Definition: MHO_ChannelLabeler.hh:17
void make_attribute(const std::string &key, XValueType value, hid_t parent_dataset_id)
Definition: MHO_HDF5Attributes.hh:38
void make_attribute< std::string >(const std::string &key, std::string value, hid_t parent_dataset_id)
Definition: MHO_HDF5Attributes.hh:62
herr_t make_vector_attribute(const std::string &key, const std::vector< XDataType > *data, hid_t parent_dataset_id)
Definition: MHO_HDF5Attributes.hh:96