29 #if defined (HAVE_HDF5)
80 size_t nm_len = nm.length ();
84 if (! isalpha (nm[0]))
87 for (
size_t i = 0; i < nm_len; i++)
90 retval += (isalnum (c) || c ==
'_') ? c :
'_';
99 #define HAVE_HDF5_INT2FLOAT_CONVERSIONS 0
110 if ((n = H5Tget_nmembers (t1)) != H5Tget_nmembers (t2))
113 for (
int i = 0; i < n; ++i)
115 hid_t mt1 = H5Tget_member_type (t1, i);
116 hid_t mt2 = H5Tget_member_type (t2, i);
118 if (H5Tget_class (mt1) != H5Tget_class (mt2))
147 H5Eget_auto (H5E_DEFAULT, &err_func, &err_func_data);
148 H5Eset_auto (H5E_DEFAULT, 0, 0);
150 H5Eget_auto (&err_func, &err_func_data);
154 hid_t attr_id = H5Aopen_name (loc_id, attr_name);
165 H5Eset_auto (H5E_DEFAULT, err_func, err_func_data);
167 H5Eset_auto (err_func, err_func_data);
174 const char *attr_name,
void *buf)
189 H5Eget_auto (H5E_DEFAULT, &err_func, &err_func_data);
190 H5Eset_auto (H5E_DEFAULT, 0, 0);
192 H5Eget_auto (&err_func, &err_func_data);
196 hid_t attr_id = H5Aopen_name (loc_id, attr_name);
200 hid_t space_id = H5Aget_space (attr_id);
202 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
205 retval = H5Aread (attr_id, type_id, buf) >= 0;
211 H5Eset_auto (H5E_DEFAULT, err_func, err_func_data);
213 H5Eset_auto (err_func, err_func_data);
230 hid_t type_id = H5Tcreate (H5T_COMPOUND,
sizeof (
double) * 2);
232 H5Tinsert (type_id,
"real", 0 *
sizeof (
double), num_type);
233 H5Tinsert (type_id,
"imag", 1 *
sizeof (
double), num_type);
253 hid_t type_class_id = -1;
255 hid_t subgroup_id = -1;
256 hid_t space_id = -1;;
262 std::string vname = name;
276 H5Gget_objinfo (group_id, name, 1, &info);
278 if (info.type == H5G_GROUP && ident_valid)
281 subgroup_id = H5Gopen (group_id, name, H5P_DEFAULT);
283 subgroup_id = H5Gopen (group_id, name);
288 retval = subgroup_id;
295 data_id = H5Dopen (subgroup_id,
"type", H5P_DEFAULT);
297 data_id = H5Dopen (subgroup_id,
"type");
306 type_id = H5Dget_type (data_id);
308 type_class_id = H5Tget_class (type_id);
310 if (type_class_id != H5T_STRING)
313 space_id = H5Dget_space (data_id);
314 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
319 int slen = H5Tget_size (type_id);
326 hid_t st_id = H5Tcopy (H5T_C_S1);
327 H5Tset_size (st_id, slen);
329 if (H5Dread (data_id, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT,
338 retval = (d->
tc.
load_hdf5 (subgroup_id,
"value") ? 1 : -1);
343 H5Gclose (subgroup_id);
359 H5Gclose (subgroup_id);
361 retval = (d->
tc.
load_hdf5 (group_id, name) ? 1 : -1);
365 else if (info.type == H5G_DATASET && ident_valid)
369 data_id = H5Dopen (group_id, name, H5P_DEFAULT);
371 data_id = H5Dopen (group_id, name);
380 type_id = H5Dget_type (data_id);
382 type_class_id = H5Tget_class (type_id);
384 if (type_class_id == H5T_FLOAT)
386 space_id = H5Dget_space (data_id);
388 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
397 else if (type_class_id == H5T_INTEGER)
401 #ifdef HAVE_H5T_GET_NATIVE_TYPE
405 switch (H5Tget_native_type (type_id, H5T_DIR_ASCEND))
407 case H5T_NATIVE_CHAR:
411 case H5T_NATIVE_SHORT:
416 case H5T_NATIVE_LONG:
420 case H5T_NATIVE_LLONG:
424 case H5T_NATIVE_UCHAR:
428 case H5T_NATIVE_USHORT:
432 case H5T_NATIVE_UINT:
433 case H5T_NATIVE_ULONG:
437 case H5T_NATIVE_ULLONG:
442 hid_t int_sign = H5Tget_sign (type_id);
444 if (int_sign == H5T_SGN_ERROR)
445 warning (
"load: can't read '%s' (unknown datatype)", name);
448 if (int_sign == H5T_SGN_NONE)
449 int_typ.append (
"u");
450 int_typ.append (
"int");
452 int slen = H5Tget_size (type_id);
454 warning (
"load: can't read '%s' (unknown datatype)", name);
460 int_typ.append (
"8 ");
464 int_typ.append (
"16 ");
468 int_typ.append (
"32 ");
472 int_typ.append (
"64 ");
476 warning (
"load: can't read '%s' (unknown datatype)",
485 warning (
"load: can't read '%s' (unknown datatype)", name);
489 space_id = H5Dget_space (data_id);
491 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
494 int_typ.append (
"scalar");
496 int_typ.append (
"matrix");
502 else if (type_class_id == H5T_STRING)
504 else if (type_class_id == H5T_COMPOUND)
511 space_id = H5Dget_space (data_id);
512 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
526 H5Tclose (complex_type);
530 warning (
"load: can't read '%s' (unknown datatype)", name);
541 retval = (d->
tc.
load_hdf5 (group_id, name) ? 1 : -1);
548 warning (
"load: skipping invalid identifier '%s' in hdf5 file",
554 error (
"load: error while reading hdf5 item %s", name);
559 int comment_length = H5Gget_comment (group_id, name, 0, 0);
561 if (comment_length > 1)
564 H5Gget_comment (group_id, name, comment_length, tdoc);
567 else if (vname != name)
600 herr_t H5Giterate_retval = -1;
604 hid_t group_id = H5Gopen (hs.
file_id,
"/", H5P_DEFAULT);
606 hid_t group_id = H5Gopen (hs.
file_id,
"/");
608 H5Gget_num_objs (group_id, &num_obj);
613 bool load_named_vars = argv_idx < argc;
614 while (load_named_vars && hs.
current_item < static_cast<int> (num_obj))
616 std::vector<char> var_name;
621 var_name.resize (len+1);
624 for (
int i = argv_idx; i < argc; i++)
627 if (pattern.
match (std::string (&var_name[0])))
645 if (H5Giterate_retval > 0)
658 if (! d.
name.empty ())
671 hid_t as_id = H5Screate (H5S_SCALAR);
676 hid_t a_id = H5Acreate (loc_id, attr_name, H5T_NATIVE_UCHAR,
677 as_id, H5P_DEFAULT, H5P_DEFAULT);
679 hid_t a_id = H5Acreate (loc_id, attr_name,
680 H5T_NATIVE_UCHAR, as_id, H5P_DEFAULT);
684 unsigned char attr_val = 1;
686 retval = H5Awrite (a_id, H5T_NATIVE_UCHAR, &attr_val);
703 const char *attr_name,
void *buf)
707 hid_t as_id = H5Screate (H5S_SCALAR);
712 hid_t a_id = H5Acreate (loc_id, attr_name, type_id,
713 as_id, H5P_DEFAULT, H5P_DEFAULT);
715 hid_t a_id = H5Acreate (loc_id, attr_name,
716 type_id, as_id, H5P_DEFAULT);
720 retval = H5Awrite (a_id, type_id, buf);
745 hid_t space_hid = -1;
748 for (hsize_t i = 0; i < sz; i++)
758 space_hid = H5Screate_simple (1, &sz, 0);
759 if (space_hid < 0)
return space_hid;
762 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
769 H5Sclose (space_hid);
774 H5P_DEFAULT, dims) >= 0;
777 H5Sclose (space_hid);
782 return (retval == 0 ? 1 : retval);
795 hsize_t hdims, maxdims;
797 hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
799 hid_t data_hid = H5Dopen (loc_id, name);
801 hid_t space_id = H5Dget_space (data_hid);
802 H5Sget_simple_extent_dims (space_id, &hdims, &maxdims);
812 for (hsize_t i = 0; i < hdims; i++)
819 return (retval == 0 ? hdims : retval);
825 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS
835 return H5T_NATIVE_UCHAR;
838 return H5T_NATIVE_USHORT;
841 return H5T_NATIVE_UINT;
844 return H5T_NATIVE_CHAR;
847 return H5T_NATIVE_SHORT;
850 return H5T_NATIVE_INT;
853 return H5T_NATIVE_FLOAT;
857 return H5T_NATIVE_DOUBLE;
869 const std::string& name,
const std::string& doc,
870 bool mark_as_global,
bool save_as_floats)
873 hid_t type_id, space_id, data_id, data_type_id;
874 type_id = space_id = data_id = data_type_id = -1;
887 data_id = H5Gcreate (loc_id, name.c_str (), H5P_DEFAULT, H5P_DEFAULT,
890 data_id = H5Gcreate (loc_id, name.c_str (), 0);
896 type_id = H5Tcopy (H5T_C_S1); H5Tset_size (type_id, t.length () + 1);
901 space_id = H5Screate_simple (0 , dims, 0);
905 data_type_id = H5Dcreate (data_id,
"type", type_id, space_id,
906 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
908 data_type_id = H5Dcreate (data_id,
"type", type_id, space_id, H5P_DEFAULT);
910 if (data_type_id < 0 || H5Dwrite (data_type_id, type_id, H5S_ALL, H5S_ALL,
911 H5P_DEFAULT, t.c_str ()) < 0)
915 retval = val.
save_hdf5 (data_id,
"value", save_as_floats);
918 if (retval && doc.length () > 0
919 && H5Gset_comment (loc_id, name.c_str (), doc.c_str ()) < 0)
923 if (retval && mark_as_global)
932 if (data_type_id >= 0)
933 H5Dclose (data_type_id);
945 error (
"save: error while writing '%s' to hdf5 file", name.c_str ());
955 const std::string& name,
const std::string& doc,
956 bool mark_as_global,
bool save_as_floats)
963 mark_as_global, save_as_floats);
bool hdf5_types_compatible(hid_t t1, hid_t t2)
static int static_type_id(void)
octave_value full_value(void) const
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
std::string read_hdf5_data(std::istream &is, const std::string &, bool &global, octave_value &tc, std::string &doc, const string_vector &argv, int argv_idx, int argc)
void resize(int n, int fill_value=0)
bool hdf5_check_attr(hid_t loc_id, const char *attr_name)
void error(const char *fmt,...)
bool is_perm_matrix(void) const
herr_t hdf5_read_next_data(hid_t group_id, const char *name, void *dv)
herr_t hdf5_add_scalar_attr(hid_t loc_id, hid_t type_id, const char *attr_name, void *buf)
F77_RET_T const double const double double * d
int load_hdf5_empty(hid_t loc_id, const char *name, dim_vector &d)
hid_t hdf5_make_complex_type(hid_t num_type)
static octave_value lookup_type(const std::string &nm)
bool valid_identifier(const char *s)
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
bool add_hdf5_data(hid_t loc_id, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_as_global, bool save_as_floats)
herr_t hdf5_add_attr(hid_t loc_id, const char *attr_name)
bool hdf5_get_scalar_attr(hid_t loc_id, hid_t type_id, const char *attr_name, void *buf)
bool match(const std::string &str) const
bool save_hdf5_data(std::ostream &os, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_as_global, bool save_as_floats)
void warning(const char *fmt,...)
std::string type_name(void) const
int save_hdf5_empty(hid_t loc_id, const char *name, const dim_vector d)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
bool check_hdf5_id_type(bool warn)
static std::string make_valid_identifier(const std::string &nm)
bool is_diag_matrix(void) const