1 #ifndef MHO_HDF5Attributes_HH__
2 #define MHO_HDF5Attributes_HH__
37 template<
typename XValueType >
inline void make_attribute(
const std::string& key, XValueType
value, hid_t parent_dataset_id)
39 if(H5Aexists(parent_dataset_id, key.c_str()) > 0)
41 msg_error(
"hdf5interface",
"attribute: " << key <<
" already exists, skipping" << eom);
45 hid_t TYPE_CODE = MHO_HDF5TypeCode< XValueType >();
46 hid_t attr_space = H5Screate(H5S_SCALAR);
47 hid_t attr_id = H5Acreate(parent_dataset_id, key.c_str(), TYPE_CODE, attr_space, H5P_DEFAULT, H5P_DEFAULT);
50 msg_error(
"hdf5interface",
"could not create attribute: " << key <<
", id code is: " << attr_id << eom);
54 H5Awrite(attr_id, TYPE_CODE, &
value);
62 if(H5Aexists(parent_dataset_id, key.c_str()) > 0)
64 msg_error(
"hdf5interface",
"string attribute: " << key <<
" already exists, skipping" << eom);
70 msg_error(
"hdf5interface",
"string attribute: " << key <<
" has size: " <<
value.size() <<
", skipping" << eom);
74 hid_t attr_space = H5Screate(H5S_SCALAR);
75 hid_t TYPE_CODE = H5Tcopy(H5T_C_S1);
76 H5Tset_size(TYPE_CODE,
value.size());
77 H5Tset_strpad(TYPE_CODE, H5T_STR_NULLTERM);
78 hid_t attr_id = H5Acreate(parent_dataset_id, key.c_str(), TYPE_CODE, attr_space, H5P_DEFAULT, H5P_DEFAULT);
81 msg_error(
"hdf5interface",
"could not create string attribute: " << key <<
", id code is: " << attr_id << eom);
84 H5Awrite(attr_id, TYPE_CODE,
value.c_str());
92 template<
typename XDataType >
96 if(H5Aexists(parent_dataset_id, key.c_str()) > 0)
98 msg_error(
"hdf5interface",
"attribute: " << key <<
" already exists, skipping" << eom);
103 dims[0] =
data->size();
104 hid_t attr_space = H5Screate_simple(1, dims, NULL);
106 hid_t TYPE_CODE = MHO_HDF5TypeCode< XDataType >();
107 hid_t attr_id = H5Acreate(parent_dataset_id, key.c_str(), TYPE_CODE, attr_space, H5P_DEFAULT, H5P_DEFAULT);
110 msg_error(
"hdf5interface",
"could not create vector attribute: " << key <<
", id code is: " << attr_id << eom);
113 status = H5Awrite(attr_id, TYPE_CODE,
data->data());
115 H5Sclose(attr_space);
124 if(H5Aexists(parent_dataset_id, key.c_str()) > 0)
126 msg_error(
"hdf5interface",
"attribute: " << key <<
" already exists, skipping" << eom);
131 dims[0] =
data->size();
132 hid_t TYPE_CODE = H5Tcopy(H5T_C_S1);
133 H5Tset_size(TYPE_CODE, H5T_VARIABLE);
136 std::vector< const char* > cstrs;
137 for(
const auto& s : *
data)
139 cstrs.push_back(s.c_str());
141 hid_t attr_space = H5Screate_simple(1, dims, NULL);
142 hid_t attr_id = H5Acreate(parent_dataset_id, key.c_str(), TYPE_CODE, attr_space, H5P_DEFAULT, H5P_DEFAULT);
145 msg_error(
"hdf5interface",
"could not create string vector attribute: " << key <<
", id code is: " << attr_id << eom);
148 status = H5Awrite(attr_id, TYPE_CODE, cstrs.data());
150 H5Sclose(attr_space);
158 msg_debug(
"hdf5interface",
"creating attribute with key: " << key << eom);
160 hid_t attr_space = H5Screate(H5S_SCALAR);
164 if(
value.is_string())
166 std::string strv =
value.get< std::string >();
169 else if(
value.is_number_integer())
171 int v =
value.get<
int >();
174 else if(
value.is_number_unsigned())
176 unsigned int v =
value.get<
unsigned int >();
179 else if(
value.is_number_float())
181 double v =
value.get<
double >();
184 else if(
value.is_boolean())
186 uint8_t v =
value.get<
bool >() ? 1 : 0;
189 else if(
value.is_array() &&
value.size() != 0)
191 if(!(
value.begin()->is_object()))
195 if(
value.begin()->is_number_integer())
197 std::vector< int >
data =
value.get< std::vector< int > >();
200 if(
value.begin()->is_number_unsigned())
202 std::vector< unsigned int >
data =
value.get< std::vector< unsigned int > >();
205 if(
value.begin()->is_number_float())
207 std::vector< double >
data =
value.get< std::vector< double > >();
210 if(
value.begin()->is_string())
212 std::vector< std::string >
data =
value.get< std::vector< std::string > >();
217 else if(
value.is_object())
221 msg_debug(
"hdf5interface",
"composite object attribute: " << key <<
", will stored as string" << eom);
222 std::stringstream ss;
224 std::string sval = ss.str();
#define msg_debug(xKEY, xCONTENT)
Definition: MHO_Message.hh:291
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
struct type_status status
Definition: fourfit3.c:53
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 make_attribute< std::string >(const std::string &key, std::string value, hid_t parent_dataset_id)
Definition: MHO_HDF5Attributes.hh:60
herr_t make_vector_attribute(const std::string &key, const std::vector< XDataType > *data, hid_t parent_dataset_id)
Definition: MHO_HDF5Attributes.hh:93