29 #if defined (HAVE_HDF5)
79 size_t nm_len = nm.length ();
83 if (! isalpha (nm[0]))
86 for (
size_t i = 0; i < nm_len; i++)
89 retval += (isalnum (c) || c ==
'_') ? c :
'_';
98 #define HAVE_HDF5_INT2FLOAT_CONVERSIONS 0
109 if ((n = H5Tget_nmembers (t1)) != H5Tget_nmembers (t2))
112 for (
int i = 0; i < n; ++i)
114 hid_t mt1 = H5Tget_member_type (t1, i);
115 hid_t mt2 = H5Tget_member_type (t2, i);
117 if (H5Tget_class (mt1) != H5Tget_class (mt2))
146 H5Eget_auto (H5E_DEFAULT, &err_func, &err_func_data);
147 H5Eset_auto (H5E_DEFAULT, 0, 0);
149 H5Eget_auto (&err_func, &err_func_data);
153 hid_t attr_id = H5Aopen_name (loc_id, attr_name);
164 H5Eset_auto (H5E_DEFAULT, err_func, err_func_data);
166 H5Eset_auto (err_func, err_func_data);
173 const char *attr_name,
void *buf)
188 H5Eget_auto (H5E_DEFAULT, &err_func, &err_func_data);
189 H5Eset_auto (H5E_DEFAULT, 0, 0);
191 H5Eget_auto (&err_func, &err_func_data);
195 hid_t attr_id = H5Aopen_name (loc_id, attr_name);
199 hid_t space_id = H5Aget_space (attr_id);
201 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
204 retval = H5Aread (attr_id, type_id, buf) >= 0;
210 H5Eset_auto (H5E_DEFAULT, err_func, err_func_data);
212 H5Eset_auto (err_func, err_func_data);
229 hid_t type_id = H5Tcreate (H5T_COMPOUND,
sizeof (
double) * 2);
231 H5Tinsert (type_id,
"real", 0 *
sizeof (
double), num_type);
232 H5Tinsert (type_id,
"imag", 1 *
sizeof (
double), num_type);
252 hid_t type_class_id = -1;
254 hid_t subgroup_id = -1;
255 hid_t space_id = -1;;
261 std::string vname = name;
275 H5Gget_objinfo (group_id, name, 1, &info);
277 if (info.type == H5G_GROUP && ident_valid)
280 subgroup_id = H5Gopen (group_id, name, H5P_DEFAULT);
282 subgroup_id = H5Gopen (group_id, name);
287 retval = subgroup_id;
294 data_id = H5Dopen (subgroup_id,
"type", H5P_DEFAULT);
296 data_id = H5Dopen (subgroup_id,
"type");
305 type_id = H5Dget_type (data_id);
307 type_class_id = H5Tget_class (type_id);
309 if (type_class_id != H5T_STRING)
312 space_id = H5Dget_space (data_id);
313 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
318 int slen = H5Tget_size (type_id);
325 hid_t st_id = H5Tcopy (H5T_C_S1);
326 H5Tset_size (st_id, slen);
328 if (H5Dread (data_id, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT,
337 retval = (d->
tc.
load_hdf5 (subgroup_id,
"value") ? 1 : -1);
342 H5Gclose (subgroup_id);
358 H5Gclose (subgroup_id);
360 retval = (d->
tc.
load_hdf5 (group_id, name) ? 1 : -1);
364 else if (info.type == H5G_DATASET && ident_valid)
368 data_id = H5Dopen (group_id, name, H5P_DEFAULT);
370 data_id = H5Dopen (group_id, name);
379 type_id = H5Dget_type (data_id);
381 type_class_id = H5Tget_class (type_id);
383 if (type_class_id == H5T_FLOAT)
385 space_id = H5Dget_space (data_id);
387 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
396 else if (type_class_id == H5T_INTEGER)
400 #ifdef HAVE_H5T_GET_NATIVE_TYPE
404 switch (H5Tget_native_type (type_id, H5T_DIR_ASCEND))
406 case H5T_NATIVE_CHAR:
410 case H5T_NATIVE_SHORT:
415 case H5T_NATIVE_LONG:
419 case H5T_NATIVE_LLONG:
423 case H5T_NATIVE_UCHAR:
427 case H5T_NATIVE_USHORT:
431 case H5T_NATIVE_UINT:
432 case H5T_NATIVE_ULONG:
436 case H5T_NATIVE_ULLONG:
441 hid_t int_sign = H5Tget_sign (type_id);
443 if (int_sign == H5T_SGN_ERROR)
444 warning (
"load: can't read '%s' (unknown datatype)", name);
447 if (int_sign == H5T_SGN_NONE)
448 int_typ.append (
"u");
449 int_typ.append (
"int");
451 int slen = H5Tget_size (type_id);
453 warning (
"load: can't read '%s' (unknown datatype)", name);
459 int_typ.append (
"8 ");
463 int_typ.append (
"16 ");
467 int_typ.append (
"32 ");
471 int_typ.append (
"64 ");
475 warning (
"load: can't read '%s' (unknown datatype)",
484 warning (
"load: can't read '%s' (unknown datatype)", name);
488 space_id = H5Dget_space (data_id);
490 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
493 int_typ.append (
"scalar");
495 int_typ.append (
"matrix");
501 else if (type_class_id == H5T_STRING)
503 else if (type_class_id == H5T_COMPOUND)
510 space_id = H5Dget_space (data_id);
511 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
525 H5Tclose (complex_type);
529 warning (
"load: can't read '%s' (unknown datatype)", name);
539 retval = (d->
tc.
load_hdf5 (group_id, name) ? 1 : -1);
546 warning (
"load: skipping invalid identifier '%s' in hdf5 file",
552 error (
"load: error while reading hdf5 item %s", name);
557 int comment_length = H5Gget_comment (group_id, name, 0, 0);
559 if (comment_length > 1)
562 H5Gget_comment (group_id, name, comment_length, tdoc);
565 else if (vname != name)
596 herr_t H5Giterate_retval = -1;
600 hid_t group_id = H5Gopen (hs.
file_id,
"/", H5P_DEFAULT);
602 hid_t group_id = H5Gopen (hs.
file_id,
"/");
604 H5Gget_num_objs (group_id, &num_obj);
609 bool load_named_vars = argv_idx < argc;
610 while (load_named_vars && hs.
current_item < static_cast<int> (num_obj))
612 std::vector<char> var_name;
617 var_name.resize (len+1);
620 for (
int i = argv_idx; i < argc; i++)
623 if (pattern.
match (std::string (&var_name[0])))
641 if (H5Giterate_retval > 0)
654 if (! d.
name.empty ())
667 hid_t as_id = H5Screate (H5S_SCALAR);
672 hid_t a_id = H5Acreate (loc_id, attr_name, H5T_NATIVE_UCHAR,
673 as_id, H5P_DEFAULT, H5P_DEFAULT);
675 hid_t a_id = H5Acreate (loc_id, attr_name,
676 H5T_NATIVE_UCHAR, as_id, H5P_DEFAULT);
680 unsigned char attr_val = 1;
682 retval = H5Awrite (a_id, H5T_NATIVE_UCHAR, &attr_val);
699 const char *attr_name,
void *buf)
703 hid_t as_id = H5Screate (H5S_SCALAR);
708 hid_t a_id = H5Acreate (loc_id, attr_name, type_id,
709 as_id, H5P_DEFAULT, H5P_DEFAULT);
711 hid_t a_id = H5Acreate (loc_id, attr_name,
712 type_id, as_id, H5P_DEFAULT);
716 retval = H5Awrite (a_id, type_id, buf);
741 hid_t space_hid = -1, data_hid = -1;
743 for (hsize_t i = 0; i < sz; i++)
753 space_hid = H5Screate_simple (1, &sz, 0);
754 if (space_hid < 0)
return space_hid;
757 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
764 H5Sclose (space_hid);
769 H5P_DEFAULT, dims) >= 0;
772 H5Sclose (space_hid);
777 return (retval == 0 ? 1 : retval);
790 hsize_t hdims, maxdims;
792 hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
794 hid_t data_hid = H5Dopen (loc_id, name);
796 hid_t space_id = H5Dget_space (data_hid);
797 H5Sget_simple_extent_dims (space_id, &hdims, &maxdims);
807 for (hsize_t i = 0; i < hdims; i++)
814 return (retval == 0 ? hdims : retval);
820 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS
830 return H5T_NATIVE_UCHAR;
833 return H5T_NATIVE_USHORT;
836 return H5T_NATIVE_UINT;
839 return H5T_NATIVE_CHAR;
842 return H5T_NATIVE_SHORT;
845 return H5T_NATIVE_INT;
848 return H5T_NATIVE_FLOAT;
852 return H5T_NATIVE_DOUBLE;
864 const std::string& name,
const std::string& doc,
865 bool mark_as_global,
bool save_as_floats)
868 hid_t type_id = -1, space_id = -1, data_id = -1, data_type_id = -1;
880 data_id = H5Gcreate (loc_id, name.c_str (), H5P_DEFAULT, H5P_DEFAULT,
883 data_id = H5Gcreate (loc_id, name.c_str (), 0);
889 type_id = H5Tcopy (H5T_C_S1); H5Tset_size (type_id, t.length () + 1);
894 space_id = H5Screate_simple (0 , dims, 0);
898 data_type_id = H5Dcreate (data_id,
"type", type_id, space_id,
899 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
901 data_type_id = H5Dcreate (data_id,
"type", type_id, space_id, H5P_DEFAULT);
903 if (data_type_id < 0 || H5Dwrite (data_type_id, type_id, H5S_ALL, H5S_ALL,
904 H5P_DEFAULT, t.c_str ()) < 0)
908 retval = val.
save_hdf5 (data_id,
"value", save_as_floats);
911 if (retval && doc.length () > 0
912 && H5Gset_comment (loc_id, name.c_str (), doc.c_str ()) < 0)
916 if (retval && mark_as_global)
925 if (data_type_id >= 0)
926 H5Dclose (data_type_id);
938 error (
"save: error while writing '%s' to hdf5 file", name.c_str ());
948 const std::string& name,
const std::string& doc,
949 bool mark_as_global,
bool save_as_floats)
954 mark_as_global, save_as_floats);