36 #ifndef VIGRA_HDF5IMPEX_HXX
37 #define VIGRA_HDF5IMPEX_HXX
43 #define H5Gcreate_vers 2
44 #define H5Gopen_vers 2
45 #define H5Dopen_vers 2
46 #define H5Dcreate_vers 2
47 #define H5Acreate_vers 2
48 #define H5Eset_auto_vers 2
49 #define H5Eget_auto_vers 2
53 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR <= 6)
55 # define H5Gopen(a, b, c) H5Gopen(a, b)
58 # define H5Gcreate(a, b, c, d, e) H5Gcreate(a, b, 1)
61 # define H5Dopen(a, b, c) H5Dopen(a, b)
64 # define H5Dcreate(a, b, c, d, e, f, g) H5Dcreate(a, b, c, d, f)
67 # define H5Acreate(a, b, c, d, e, f) H5Acreate(a, b, c, d, e)
69 # ifndef H5Pset_obj_track_times
70 # define H5Pset_obj_track_times(a, b) do {} while (0)
78 #include "multi_array.hxx"
79 #include "multi_iterator_coupled.hxx"
80 #include "multi_impex.hxx"
102 inline bool isHDF5(
char const * filename)
105 return _access(filename, 0) != -1 && H5Fis_hdf5(filename);
107 return access(filename, F_OK) == 0 && H5Fis_hdf5(filename);
132 H5E_auto2_t old_func_;
133 void *old_client_data_;
141 , old_client_data_(0)
143 H5Eget_auto2(H5E_DEFAULT, &old_func_, &old_client_data_);
144 H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
149 H5Eset_auto2(H5E_DEFAULT, old_func_, old_client_data_);
194 typedef herr_t (*Destructor)(hid_t);
198 Destructor destructor_;
229 HDF5Handle(hid_t h, Destructor destructor,
const char * error_message)
231 destructor_(destructor)
234 vigra_fail(error_message);
242 : handle_( h.handle_ ),
243 destructor_(h.destructor_)
254 if(h.handle_ != handle_)
258 destructor_ = h.destructor_;
280 if(handle_ && destructor_)
281 res = (*destructor_)(handle_);
302 void reset(hid_t h, Destructor destructor,
const char * error_message)
305 vigra_fail(error_message);
310 destructor_ = destructor;
320 std::swap(handle_, h.handle_);
321 std::swap(destructor_, h.destructor_);
339 operator hid_t()
const
348 return handle_ == h.handle_;
362 return handle_ != h.handle_;
415 typedef herr_t (*Destructor)(hid_t);
419 Destructor destructor_;
454 destructor_(destructor),
458 vigra_fail(error_message);
460 refcount_ =
new size_t(1);
467 : handle_( h.handle_ ),
468 destructor_(h.destructor_),
469 refcount_(h.refcount_)
481 if(h.handle_ != handle_)
485 destructor_ = h.destructor_;
486 refcount_ = h.refcount_;
517 res = (*destructor_)(handle_);
531 void reset(hid_t h, Destructor destructor,
const char * error_message)
534 vigra_fail(error_message);
539 destructor_ = destructor;
541 refcount_ =
new size_t(1);
569 std::swap(handle_, h.handle_);
570 std::swap(destructor_, h.destructor_);
571 std::swap(refcount_, h.refcount_);
589 operator hid_t()
const
598 return handle_ == h.handle_;
612 return handle_ != h.handle_;
666 enum PixelType { UINT8, UINT16, UINT32, UINT64,
667 INT8, INT16, INT32, INT64,
680 VIGRA_EXPORT
HDF5ImportInfo(
const char* filePath,
const char* pathInFile );
682 VIGRA_EXPORT ~HDF5ImportInfo();
686 VIGRA_EXPORT
const std::string&
getFilePath()
const;
762 VIGRA_EXPORT PixelType
pixelType()
const;
766 std::string m_filename, m_path, m_pixeltype;
767 hssize_t m_dimensions;
775 struct HDF5TypeTraits
777 static hid_t getH5DataType()
779 std::runtime_error(
"getH5DataType(): invalid type");
783 static int numberOfBands()
785 std::runtime_error(
"numberOfBands(): invalid type");
791 inline hid_t getH5DataType()
793 return HDF5TypeTraits<T>::getH5DataType();
796 #define VIGRA_H5_DATATYPE(type, h5type) \
798 struct HDF5TypeTraits<type> \
800 static hid_t getH5DataType() \
804 static int numberOfBands() \
808 typedef type value_type; \
811 struct HDF5TypeTraits<TinyVector<type, M> > \
813 static hid_t getH5DataType() \
817 static int numberOfBands() \
821 typedef type value_type; \
824 struct HDF5TypeTraits<RGBValue<type> > \
826 static hid_t getH5DataType() \
830 static int numberOfBands() \
834 typedef type value_type; \
837 VIGRA_H5_DATATYPE(
char, H5T_NATIVE_CHAR)
838 VIGRA_H5_DATATYPE(
signed char, H5T_NATIVE_SCHAR)
839 VIGRA_H5_DATATYPE(
unsigned char, H5T_NATIVE_UCHAR)
840 VIGRA_H5_DATATYPE(
signed short, H5T_NATIVE_SHORT)
841 VIGRA_H5_DATATYPE(
unsigned short, H5T_NATIVE_USHORT)
842 VIGRA_H5_DATATYPE(
signed int, H5T_NATIVE_INT)
843 VIGRA_H5_DATATYPE(
unsigned int, H5T_NATIVE_UINT)
844 VIGRA_H5_DATATYPE(
signed long, H5T_NATIVE_LONG)
845 VIGRA_H5_DATATYPE(
unsigned long, H5T_NATIVE_ULONG)
846 VIGRA_H5_DATATYPE(
signed long long, H5T_NATIVE_LLONG)
847 VIGRA_H5_DATATYPE(
unsigned long long, H5T_NATIVE_ULLONG)
848 VIGRA_H5_DATATYPE(
float, H5T_NATIVE_FLOAT)
849 VIGRA_H5_DATATYPE(
double, H5T_NATIVE_DOUBLE)
850 VIGRA_H5_DATATYPE(
long double, H5T_NATIVE_LDOUBLE)
854 struct HDF5TypeTraits<
char*>
856 static hid_t getH5DataType()
858 hid_t stringtype = H5Tcopy (H5T_C_S1);
859 H5Tset_size(stringtype, H5T_VARIABLE);
863 static int numberOfBands()
870 struct HDF5TypeTraits<const char*>
872 static hid_t getH5DataType()
874 hid_t stringtype = H5Tcopy (H5T_C_S1);
875 H5Tset_size(stringtype, H5T_VARIABLE);
879 static int numberOfBands()
885 #undef VIGRA_H5_DATATYPE
889 inline hid_t getH5DataType<FFTWComplex<float> >()
891 hid_t complex_id = H5Tcreate (H5T_COMPOUND,
sizeof (FFTWComplex<float>));
892 H5Tinsert (complex_id,
"real", 0, H5T_NATIVE_FLOAT);
893 H5Tinsert (complex_id,
"imaginary",
sizeof(
float), H5T_NATIVE_FLOAT);
898 inline hid_t getH5DataType<FFTWComplex<double> >()
900 hid_t complex_id = H5Tcreate (H5T_COMPOUND,
sizeof (FFTWComplex<double>));
901 H5Tinsert (complex_id,
"real", 0, H5T_NATIVE_DOUBLE);
902 H5Tinsert (complex_id,
"imaginary",
sizeof(
double), H5T_NATIVE_DOUBLE);
911 void HDF5_ls_insert(
void*,
const std::string &);
916 VIGRA_EXPORT H5O_type_t HDF5_get_type(hid_t,
const char*);
917 extern "C" VIGRA_EXPORT herr_t HDF5_ls_inserter_callback(hid_t,
const char*,
const H5L_info_t*,
void*);
918 extern "C" VIGRA_EXPORT herr_t HDF5_listAttributes_inserter_callback(hid_t,
const char*,
const H5A_info_t*,
void*);
975 virtual void insert(
const std::string &) = 0;
976 virtual ~ls_closure() {}
980 struct lsOpData :
public ls_closure
982 std::vector<std::string> & objects;
983 lsOpData(std::vector<std::string> & o) : objects(o) {}
984 void insert(
const std::string & x)
986 objects.push_back(x);
991 template<
class Container>
992 struct ls_container_data :
public ls_closure
995 ls_container_data(Container & o) : objects(o) {}
996 void insert(
const std::string & x)
998 objects.insert(std::string(x));
1005 friend void HDF5_ls_insert(
void*,
const std::string &);
1020 ReadOnly = OpenReadOnly,
1040 : track_time(track_creation_times ? 1 : 0),
1053 : track_time(track_creation_times ? 1 : 0)
1055 open(filePath, mode);
1076 const std::string & pathname =
"",
1077 bool read_only =
false)
1078 : fileHandle_(fileHandle),
1079 read_only_(read_only)
1086 cGroupHandle_ =
HDF5Handle(openCreateGroup_(pathname), &H5Gclose,
1087 "HDF5File(fileHandle, pathname): Failed to open group");
1090 hbool_t track_times_tmp;
1091 HDF5Handle plist_id(H5Fget_create_plist(fileHandle_), &H5Pclose,
1092 "HDF5File(fileHandle, pathname): Failed to open file creation property list");
1093 herr_t status = H5Pget_obj_track_times(plist_id, &track_times_tmp );
1094 vigra_postcondition(status >= 0,
1095 "HDF5File(fileHandle, pathname): cannot access track time attribute");
1096 track_time = track_times_tmp;
1104 : fileHandle_(other.fileHandle_),
1105 track_time(other.track_time),
1106 read_only_(other.read_only_)
1108 cGroupHandle_ =
HDF5Handle(openCreateGroup_(other.currentGroupName_()), &H5Gclose,
1109 "HDF5File(HDF5File const &): Failed to open group.");
1132 fileHandle_ = other.fileHandle_;
1133 cGroupHandle_ =
HDF5Handle(openCreateGroup_(other.currentGroupName_()), &H5Gclose,
1134 "HDF5File::operator=(): Failed to open group.");
1135 track_time = other.track_time;
1136 read_only_ = other.read_only_;
1141 int file_use_count()
const
1148 return fileHandle_ != 0;
1151 bool isReadOnly()
const
1156 void setReadOnly(
bool stat=
true)
1168 std::string errorMessage =
"HDF5File.open(): Could not open or create file '" + filePath +
"'.";
1169 fileHandle_ =
HDF5HandleShared(createFile_(filePath, mode), &H5Fclose, errorMessage.c_str());
1170 cGroupHandle_ =
HDF5Handle(openCreateGroup_(
"/"), &H5Gclose,
"HDF5File.open(): Failed to open root group.");
1171 setReadOnly(mode == OpenReadOnly);
1178 bool success = cGroupHandle_.
close() >= 0 && fileHandle_.
close() >= 0;
1179 vigra_postcondition(success,
"HDF5File.close() failed.");
1186 std::string message =
"HDF5File::root(): Could not open group '/'.";
1187 cGroupHandle_ =
HDF5Handle(H5Gopen(fileHandle_,
"/", H5P_DEFAULT),&H5Gclose,message.c_str());
1193 inline void cd(std::string groupName)
1204 std::string groupName = currentGroupName_();
1207 if(groupName ==
"/"){
1211 size_t lastSlash = groupName.find_last_of(
'/');
1213 std::string parentGroup (groupName.begin(), groupName.begin()+lastSlash+1);
1226 std::string groupName = currentGroupName_();
1228 for(
int i = 0; i<levels; i++)
1233 if(groupName != currentGroupName_())
1245 inline void mkdir(std::string groupName)
1247 vigra_precondition(!isReadOnly(),
1248 "HDF5File::mkdir(): file is read-only.");
1250 std::string message =
"HDF5File::mkdir(): Could not create group '" + groupName +
"'.\n";
1255 HDF5Handle(openCreateGroup_(groupName.c_str()),&H5Gclose,message.c_str());
1262 inline void cd_mk(std::string groupName)
1264 vigra_precondition(!isReadOnly(),
1265 "HDF5File::cd_mk(): file is read-only.");
1267 std::string message =
"HDF5File::cd_mk(): Could not create group '" + groupName +
"'.";
1272 cGroupHandle_ =
HDF5Handle(openCreateGroup_(groupName.c_str()),&H5Gclose,message.c_str());
1276 void ls_H5Literate(ls_closure & data)
const
1278 H5Literate(cGroupHandle_, H5_INDEX_NAME, H5_ITER_NATIVE, NULL,
1279 HDF5_ls_inserter_callback, static_cast<void*>(&data));
1287 inline std::vector<std::string>
ls()
const
1289 std::vector<std::string> list;
1290 lsOpData data(list);
1291 ls_H5Literate(data);
1308 template<
class Container>
1309 void ls(Container & cont)
const
1311 ls_container_data<Container> data(cont);
1312 ls_H5Literate(data);
1317 inline std::string
pwd()
const
1319 return currentGroupName_();
1335 return (H5Lexists(fileHandle_, datasetName.c_str(), H5P_DEFAULT) > 0);
1346 return getDatasetDimensions_(datasetHandle);
1349 hssize_t getDatasetDimensions_(hid_t dataset)
const
1351 std::string errorMessage =
"HDF5File::getDatasetDimensions(): Unable to access dataspace.";
1352 HDF5Handle dataspaceHandle(H5Dget_space(dataset), &H5Sclose, errorMessage.c_str());
1355 return H5Sget_simple_extent_ndims(dataspaceHandle);
1377 std::string errorMessage =
"HDF5File::getDatasetShape(): Unable to open dataset '" + datasetName +
"'.";
1378 HDF5Handle datasetHandle =
HDF5Handle(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
1380 errorMessage =
"HDF5File::getDatasetShape(): Unable to access dataspace.";
1381 HDF5Handle dataspaceHandle(H5Dget_space(datasetHandle), &H5Sclose, errorMessage.c_str());
1388 H5Sget_simple_extent_dims(dataspaceHandle, shape.
data(), maxdims.
data());
1391 std::reverse(shape.
begin(), shape.
end());
1416 hid_t datatype = H5Dget_type(datasetHandle);
1417 H5T_class_t dataclass = H5Tget_class(datatype);
1418 size_t datasize = H5Tget_size(datatype);
1419 H5T_sign_t datasign = H5Tget_sign(datatype);
1421 if(dataclass == H5T_FLOAT)
1425 else if(datasize == 8)
1428 else if(dataclass == H5T_INTEGER)
1430 if(datasign == H5T_SGN_NONE)
1434 else if(datasize == 2)
1436 else if(datasize == 4)
1438 else if(datasize == 8)
1445 else if(datasize == 2)
1447 else if(datasize == 4)
1449 else if(datasize == 8)
1460 std::string errorMessage =
"HDF5File::getDatasetHandle(): Unable to open dataset '" + datasetName +
"'.";
1468 std::string errorMessage =
"HDF5File::getDatasetHandle(): Unable to open dataset '" + datasetName +
"'.";
1475 std::string function_name =
"HDF5File::getGroupHandle()")
1477 std::string errorMessage = function_name +
": Group '" + group_name +
"' not found.";
1483 vigra_precondition(group_name ==
"/" || H5Lexists(fileHandle_, group_name.c_str(), H5P_DEFAULT) != 0,
1484 errorMessage.c_str());
1487 return HDF5Handle(openCreateGroup_(group_name), &H5Gclose,
"Internal error");
1491 void ls_H5Aiterate(std::string
const & group_or_dataset, ls_closure & data)
const
1493 H5O_type_t h5_type = get_object_type_(group_or_dataset);
1494 vigra_precondition(h5_type == H5O_TYPE_GROUP || h5_type == H5O_TYPE_DATASET,
1495 "HDF5File::listAttributes(): object \"" + group_or_dataset +
"\" is neither a group nor a dataset.");
1497 HDF5Handle object_handle(h5_type == H5O_TYPE_GROUP
1498 ? const_cast<HDF5File*>(
this)->openCreateGroup_(group_or_dataset)
1499 : getDatasetHandle_(group_or_dataset),
1500 h5_type == H5O_TYPE_GROUP
1503 "HDF5File::listAttributes(): unable to open object.");
1505 H5Aiterate2(object_handle, H5_INDEX_NAME, H5_ITER_NATIVE, &n,
1506 HDF5_listAttributes_inserter_callback, static_cast<void*>(&data));
1514 inline std::vector<std::string>
listAttributes(std::string
const & group_or_dataset)
const
1516 std::vector<std::string> list;
1517 lsOpData data(list);
1518 ls_H5Aiterate(group_or_dataset, data);
1528 template<
class Container>
1529 void listAttributes(std::string
const & group_or_dataset, Container & container)
const
1531 ls_container_data<Container> data(container);
1532 ls_H5Aiterate(group_or_dataset, data);
1539 std::string message =
"HDF5File::getAttributeHandle(): Attribute '" + attribute_name +
"' not found.";
1541 &H5Aclose, message.c_str());
1549 template<
unsigned int N,
class T,
class Str
ide>
1551 std::string attribute_name,
1557 write_attribute_(object_name, attribute_name, array, detail::getH5DataType<T>(), 1);
1560 template<
unsigned int N,
class T,
int SIZE,
class Str
ide>
1562 std::string attributeName,
1568 write_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), SIZE);
1571 template<
unsigned int N,
class T,
class Str
ide>
1573 std::string attributeName,
1574 const MultiArrayView<N, RGBValue<T>, Stride> & array)
1579 write_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), 3);
1585 inline void writeAttribute(std::string object_name, std::string attribute_name,
char data)
1586 { writeAtomicAttribute(object_name,attribute_name,data); }
1587 inline void writeAttribute(std::string datasetName, std::string attributeName,
signed char data)
1588 { writeAtomicAttribute(datasetName,attributeName,data); }
1589 inline void writeAttribute(std::string datasetName, std::string attributeName,
signed short data)
1590 { writeAtomicAttribute(datasetName,attributeName,data); }
1591 inline void writeAttribute(std::string datasetName, std::string attributeName,
signed int data)
1592 { writeAtomicAttribute(datasetName,attributeName,data); }
1593 inline void writeAttribute(std::string datasetName, std::string attributeName,
signed long data)
1594 { writeAtomicAttribute(datasetName,attributeName,data); }
1595 inline void writeAttribute(std::string datasetName, std::string attributeName,
signed long long data)
1596 { writeAtomicAttribute(datasetName,attributeName,data); }
1597 inline void writeAttribute(std::string datasetName, std::string attributeName,
unsigned char data)
1598 { writeAtomicAttribute(datasetName,attributeName,data); }
1599 inline void writeAttribute(std::string datasetName, std::string attributeName,
unsigned short data)
1600 { writeAtomicAttribute(datasetName,attributeName,data); }
1601 inline void writeAttribute(std::string datasetName, std::string attributeName,
unsigned int data)
1602 { writeAtomicAttribute(datasetName,attributeName,data); }
1603 inline void writeAttribute(std::string datasetName, std::string attributeName,
unsigned long data)
1604 { writeAtomicAttribute(datasetName,attributeName,data); }
1605 inline void writeAttribute(std::string datasetName, std::string attributeName,
unsigned long long data)
1606 { writeAtomicAttribute(datasetName,attributeName,data); }
1607 inline void writeAttribute(std::string datasetName, std::string attributeName,
float data)
1608 { writeAtomicAttribute(datasetName,attributeName,data); }
1609 inline void writeAttribute(std::string datasetName, std::string attributeName,
double data)
1610 { writeAtomicAttribute(datasetName,attributeName,data); }
1611 inline void writeAttribute(std::string datasetName, std::string attributeName,
long double data)
1612 { writeAtomicAttribute(datasetName,attributeName,data); }
1613 inline void writeAttribute(std::string datasetName, std::string attributeName,
const char* data)
1614 { writeAtomicAttribute(datasetName,attributeName,data); }
1615 inline void writeAttribute(std::string datasetName, std::string attributeName, std::string
const & data)
1616 { writeAtomicAttribute(datasetName,attributeName,data.c_str()); }
1623 htri_t exists = H5Aexists_by_name(fileHandle_, obj_path.c_str(),
1624 attribute_name.c_str(), H5P_DEFAULT);
1625 vigra_precondition(exists >= 0,
"HDF5File::existsAttribute(): "
1626 "object '" + object_name +
"' "
1636 template<
unsigned int N,
class T,
class Str
ide>
1638 std::string attribute_name,
1644 read_attribute_(object_name, attribute_name, array, detail::getH5DataType<T>(), 1);
1647 template<
unsigned int N,
class T,
int SIZE,
class Str
ide>
1649 std::string attributeName,
1655 read_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), SIZE);
1658 template<
unsigned int N,
class T,
class Str
ide>
1660 std::string attributeName,
1661 MultiArrayView<N, RGBValue<T>, Stride> array)
1666 read_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), 3);
1672 inline void readAttribute(std::string object_name, std::string attribute_name,
char &data)
1673 { readAtomicAttribute(object_name,attribute_name,data); }
1674 inline void readAttribute(std::string datasetName, std::string attributeName,
signed char &data)
1675 { readAtomicAttribute(datasetName,attributeName,data); }
1676 inline void readAttribute(std::string datasetName, std::string attributeName,
signed short &data)
1677 { readAtomicAttribute(datasetName,attributeName,data); }
1678 inline void readAttribute(std::string datasetName, std::string attributeName,
signed int &data)
1679 { readAtomicAttribute(datasetName,attributeName,data); }
1680 inline void readAttribute(std::string datasetName, std::string attributeName,
signed long &data)
1681 { readAtomicAttribute(datasetName,attributeName,data); }
1682 inline void readAttribute(std::string datasetName, std::string attributeName,
signed long long &data)
1683 { readAtomicAttribute(datasetName,attributeName,data); }
1684 inline void readAttribute(std::string datasetName, std::string attributeName,
unsigned char &data)
1685 { readAtomicAttribute(datasetName,attributeName,data); }
1686 inline void readAttribute(std::string datasetName, std::string attributeName,
unsigned short &data)
1687 { readAtomicAttribute(datasetName,attributeName,data); }
1688 inline void readAttribute(std::string datasetName, std::string attributeName,
unsigned int &data)
1689 { readAtomicAttribute(datasetName,attributeName,data); }
1690 inline void readAttribute(std::string datasetName, std::string attributeName,
unsigned long &data)
1691 { readAtomicAttribute(datasetName,attributeName,data); }
1692 inline void readAttribute(std::string datasetName, std::string attributeName,
unsigned long long &data)
1693 { readAtomicAttribute(datasetName,attributeName,data); }
1694 inline void readAttribute(std::string datasetName, std::string attributeName,
float &data)
1695 { readAtomicAttribute(datasetName,attributeName,data); }
1696 inline void readAttribute(std::string datasetName, std::string attributeName,
double &data)
1697 { readAtomicAttribute(datasetName,attributeName,data); }
1698 inline void readAttribute(std::string datasetName, std::string attributeName,
long double &data)
1699 { readAtomicAttribute(datasetName,attributeName,data); }
1700 inline void readAttribute(std::string datasetName, std::string attributeName, std::string &data)
1701 { readAtomicAttribute(datasetName,attributeName,data); }
1729 template<
unsigned int N,
class T,
class Str
ide>
1730 inline void write(std::string datasetName,
1732 int iChunkSize = 0,
int compression = 0)
1738 for(
unsigned int i = 0; i < N; i++){
1739 chunkSize[i] = iChunkSize;
1741 write_(datasetName, array, detail::getH5DataType<T>(), 1, chunkSize, compression);
1761 template<
unsigned int N,
class T,
class Str
ide>
1762 inline void write(std::string datasetName,
1769 write_(datasetName, array, detail::getH5DataType<T>(), 1, chunkSize, compression);
1783 template<
unsigned int N,
class T,
class Str
ide>
1790 typedef detail::HDF5TypeTraits<T> TypeTraits;
1791 writeBlock_(datasetName, blockOffset, array,
1792 TypeTraits::getH5DataType(), TypeTraits::numberOfBands());
1795 template<
unsigned int N,
class T,
class Str
ide>
1800 typedef detail::HDF5TypeTraits<T> TypeTraits;
1801 return writeBlock_(dataset, blockOffset, array,
1802 TypeTraits::getH5DataType(), TypeTraits::numberOfBands());
1806 template<
unsigned int N,
class T,
int SIZE,
class Str
ide>
1807 inline void write(std::string datasetName,
1808 const MultiArrayView<N, TinyVector<T, SIZE>, Stride> & array,
1809 int iChunkSize = 0,
int compression = 0)
1814 typename MultiArrayShape<N>::type chunkSize;
1815 for(
unsigned i = 0; i < N; i++){
1816 chunkSize[i] = iChunkSize;
1818 write_(datasetName, array, detail::getH5DataType<T>(), SIZE, chunkSize, compression);
1821 template<
unsigned int N,
class T,
int SIZE,
class Str
ide>
1822 inline void write(std::string datasetName,
1823 const MultiArrayView<N, TinyVector<T, SIZE>, Stride> & array,
1824 typename MultiArrayShape<N>::type chunkSize,
int compression = 0)
1829 write_(datasetName, array, detail::getH5DataType<T>(), SIZE, chunkSize, compression);
1843 void write(
const std::string & datasetName,
1845 int compression = 0)
1850 write(datasetName, m_array, compression);
1854 template<
unsigned int N,
class T,
class Str
ide>
1855 inline void write(std::string datasetName,
1857 int iChunkSize = 0,
int compression = 0)
1863 for(
unsigned i = 0; i < N; i++){
1864 chunkSize[i] = iChunkSize;
1866 write_(datasetName, array, detail::getH5DataType<T>(), 3, chunkSize, compression);
1869 template<
unsigned int N,
class T,
class Str
ide>
1870 inline void write(std::string datasetName,
1871 const MultiArrayView<N, RGBValue<T>, Stride> & array,
1872 typename MultiArrayShape<N>::type chunkSize,
int compression = 0)
1877 write_(datasetName, array, detail::getH5DataType<T>(), 3, chunkSize, compression);
1883 inline void write(std::string datasetName,
char data) { writeAtomic(datasetName,data); }
1884 inline void write(std::string datasetName,
signed char data) { writeAtomic(datasetName,data); }
1885 inline void write(std::string datasetName,
signed short data) { writeAtomic(datasetName,data); }
1886 inline void write(std::string datasetName,
signed int data) { writeAtomic(datasetName,data); }
1887 inline void write(std::string datasetName,
signed long data) { writeAtomic(datasetName,data); }
1888 inline void write(std::string datasetName,
signed long long data) { writeAtomic(datasetName,data); }
1889 inline void write(std::string datasetName,
unsigned char data) { writeAtomic(datasetName,data); }
1890 inline void write(std::string datasetName,
unsigned short data) { writeAtomic(datasetName,data); }
1891 inline void write(std::string datasetName,
unsigned int data) { writeAtomic(datasetName,data); }
1892 inline void write(std::string datasetName,
unsigned long data) { writeAtomic(datasetName,data); }
1893 inline void write(std::string datasetName,
unsigned long long data) { writeAtomic(datasetName,data); }
1894 inline void write(std::string datasetName,
float data) { writeAtomic(datasetName,data); }
1895 inline void write(std::string datasetName,
double data) { writeAtomic(datasetName,data); }
1896 inline void write(std::string datasetName,
long double data) { writeAtomic(datasetName,data); }
1897 inline void write(std::string datasetName,
const char* data) { writeAtomic(datasetName,data); }
1898 inline void write(std::string datasetName, std::string
const & data) { writeAtomic(datasetName,data.c_str()); }
1911 template<
unsigned int N,
class T,
class Str
ide>
1917 read_(datasetName, array, detail::getH5DataType<T>(), 1);
1929 template<
unsigned int N,
class T,
class Alloc>
1940 "HDF5File::readAndResize(): Array dimension disagrees with dataset dimension.");
1944 for(
int k=0; k < static_cast<int>(dimshape.
size()); ++k)
1945 shape[k] = static_cast<MultiArrayIndex>(dimshape[k]);
1948 read_(datasetName, array, detail::getH5DataType<T>(), 1);
1961 read(datasetName, m_array);
1980 "HDF5File::readAndResize(): Array dimension disagrees with Dataset dimension must equal one for vigra::ArrayVector.");
1988 read_(datasetName, m_array, detail::getH5DataType<T>(), 1);
2006 template<
unsigned int N,
class T,
class Str
ide>
2014 typedef detail::HDF5TypeTraits<T> TypeTraits;
2015 readBlock_(datasetName, blockOffset, blockShape, array,
2016 TypeTraits::getH5DataType(), TypeTraits::numberOfBands());
2019 template<
unsigned int N,
class T,
class Str
ide>
2025 typedef detail::HDF5TypeTraits<T> TypeTraits;
2026 return readBlock_(dataset, blockOffset, blockShape, array,
2027 TypeTraits::getH5DataType(), TypeTraits::numberOfBands());
2031 template<
unsigned int N,
class T,
int SIZE,
class Str
ide>
2032 inline void read(std::string datasetName, MultiArrayView<N, TinyVector<T, SIZE>, Stride> array)
2037 read_(datasetName, array, detail::getH5DataType<T>(), SIZE);
2041 template<
unsigned int N,
class T,
int SIZE,
class Alloc>
2042 inline void readAndResize(std::string datasetName, MultiArray<N, TinyVector<T, SIZE>, Alloc> & array)
2052 SIZE == dimshape[0],
2053 "HDF5File::readAndResize(): Array dimension disagrees with dataset dimension.");
2056 typename MultiArrayShape<N>::type shape;
2057 for(
int k=1; k < static_cast<int>(dimshape.size()); ++k)
2058 shape[k-1] = static_cast<MultiArrayIndex>(dimshape[k]);
2059 array.reshape(shape);
2061 read_(datasetName, array, detail::getH5DataType<T>(), SIZE);
2065 template<
unsigned int N,
class T,
class Str
ide>
2066 inline void read(std::string datasetName, MultiArrayView<N, RGBValue<T>, Stride> array)
2071 read_(datasetName, array, detail::getH5DataType<T>(), 3);
2075 template<
unsigned int N,
class T,
class Alloc>
2076 inline void readAndResize(std::string datasetName, MultiArray<N, RGBValue<T>, Alloc> & array)
2087 "HDF5File::readAndResize(): Array dimension disagrees with dataset dimension.");
2090 typename MultiArrayShape<N>::type shape;
2091 for(
int k=1; k < static_cast<int>(dimshape.size()); ++k)
2092 shape[k-1] = static_cast<MultiArrayIndex>(dimshape[k]);
2093 array.reshape(shape);
2095 read_(datasetName, array, detail::getH5DataType<T>(), 3);
2101 inline void read(std::string datasetName,
char &data) { readAtomic(datasetName,data); }
2102 inline void read(std::string datasetName,
signed char &data) { readAtomic(datasetName,data); }
2103 inline void read(std::string datasetName,
signed short &data) { readAtomic(datasetName,data); }
2104 inline void read(std::string datasetName,
signed int &data) { readAtomic(datasetName,data); }
2105 inline void read(std::string datasetName,
signed long &data) { readAtomic(datasetName,data); }
2106 inline void read(std::string datasetName,
signed long long &data) { readAtomic(datasetName,data); }
2107 inline void read(std::string datasetName,
unsigned char &data) { readAtomic(datasetName,data); }
2108 inline void read(std::string datasetName,
unsigned short &data) { readAtomic(datasetName,data); }
2109 inline void read(std::string datasetName,
unsigned int &data) { readAtomic(datasetName,data); }
2110 inline void read(std::string datasetName,
unsigned long &data) { readAtomic(datasetName,data); }
2111 inline void read(std::string datasetName,
unsigned long long &data) { readAtomic(datasetName,data); }
2112 inline void read(std::string datasetName,
float &data) { readAtomic(datasetName,data); }
2113 inline void read(std::string datasetName,
double &data) { readAtomic(datasetName,data); }
2114 inline void read(std::string datasetName,
long double &data) { readAtomic(datasetName,data); }
2115 inline void read(std::string datasetName, std::string &data) { readAtomic(datasetName,data); }
2141 template<
int N,
class T>
2144 TinyVector<MultiArrayIndex, N>
const & shape,
2145 typename detail::HDF5TypeTraits<T>::value_type init =
2146 typename detail::HDF5TypeTraits<T>::value_type(),
2148 TinyVector<MultiArrayIndex, N>
const & chunkSize = TinyVector<MultiArrayIndex, N>(),
2150 TinyVector<MultiArrayIndex, N>
const & chunkSize = (TinyVector<MultiArrayIndex, N>()),
2152 int compressionParameter = 0);
2155 template<
int N,
class T>
2158 TinyVector<MultiArrayIndex, N>
const & shape,
2161 int compressionParameter = 0)
2163 typename MultiArrayShape<N>::type chunkSize;
2164 for(
int i = 0; i < N; i++){
2165 chunkSize[i] = iChunkSize;
2167 return this->
template createDataset<N, T>(datasetName, shape, init,
2168 chunkSize, compressionParameter);
2176 H5Fflush(fileHandle_, H5F_SCOPE_GLOBAL);
2189 class SplitString:
public std::string {
2191 SplitString(std::string &sstring): std::string(sstring) {};
2194 std::string first(
char delimiter =
'/')
2196 size_t lastPos = find_last_of(delimiter);
2197 if(lastPos == std::string::npos)
2200 return std::string(begin(), begin()+lastPos+1);
2204 std::string last(
char delimiter =
'/')
2206 size_t lastPos = find_last_of(delimiter);
2207 if(lastPos == std::string::npos)
2208 return std::string(*
this);
2209 return std::string(begin()+lastPos+1, end());
2213 template <
class Shape>
2214 ArrayVector<hsize_t>
2215 defineChunks(Shape chunks, Shape
const & shape,
int numBands,
int compression = 0)
2217 if(
prod(chunks) > 0)
2219 ArrayVector<hsize_t> res(chunks.begin(), chunks.end());
2221 res.insert(res.begin(),
static_cast<hsize_t
>(numBands));
2224 else if(compression > 0)
2227 chunks = min(detail::ChunkShape<Shape::static_size>::defaultShape(), shape);
2228 ArrayVector<hsize_t> res(chunks.begin(), chunks.end());
2230 res.insert(res.begin(),
static_cast<hsize_t
>(numBands));
2235 return ArrayVector<hsize_t>();
2249 if(path.length() == 0 || path ==
"."){
2250 return currentGroupName_();
2255 if(relativePath_(path)){
2256 std::string cname = currentGroupName_();
2258 str = currentGroupName_()+path;
2260 str = currentGroupName_()+
"/"+path;
2266 std::string::size_type startpos = 0;
2267 while(str.find(std::string(
"./"), startpos) != std::string::npos){
2268 std::string::size_type pos = str.find(std::string(
"./"), startpos);
2271 if(str.substr(pos-1,3) !=
"../"){
2273 str = str.substr(0,pos) + str.substr(pos+2,str.length()-pos-2);
2279 while(str.find(std::string(
"..")) != std::string::npos){
2280 std::string::size_type pos = str.find(std::string(
".."));
2283 std::string::size_type end = str.find(
"/",pos);
2284 if(end != std::string::npos){
2294 std::string::size_type prev_slash = str.rfind(
"/",pos);
2296 vigra_invariant(prev_slash != 0 && prev_slash != std::string::npos,
2297 "Error parsing path: "+str);
2299 std::string::size_type begin = str.rfind(
"/",prev_slash-1);
2302 str = str.substr(0,begin+1) + str.substr(end,str.length()-end);
2312 inline bool relativePath_(std::string & path)
const
2314 std::string::size_type pos = path.find(
'/') ;
2323 inline std::string currentGroupName_()
const
2325 int len = H5Iget_name(cGroupHandle_,NULL,1000);
2326 ArrayVector<char> name (len+1,0);
2327 H5Iget_name(cGroupHandle_,name.begin(),len+1);
2329 return std::string(name.begin());
2334 inline std::string fileName_()
const
2336 int len = H5Fget_name(fileHandle_,NULL,1000);
2337 ArrayVector<char> name (len+1,0);
2338 H5Fget_name(fileHandle_,name.begin(),len+1);
2340 return std::string(name.begin());
2345 inline hid_t createFile_(std::string filePath,
OpenMode mode = Open)
2349 pFile = fopen ( filePath.c_str(),
"r" );
2353 if ( pFile == NULL )
2355 vigra_precondition(mode != OpenReadOnly,
2356 "HDF5File::open(): cannot open non-existing file in read-only mode.");
2357 fileId = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
2362 if(mode == OpenReadOnly)
2364 fileId = H5Fopen(filePath.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
2366 else if(mode == New)
2368 std::remove(filePath.c_str());
2369 fileId = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
2373 fileId = H5Fopen(filePath.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
2384 hid_t openGroup_(std::string groupName)
const
2386 return const_cast<HDF5File *
>(
this)->openCreateGroup_(groupName,
false);
2397 hid_t openCreateGroup_(std::string groupName,
bool create =
true)
2403 hid_t parent = H5Gopen(fileHandle_,
"/", H5P_DEFAULT);
2404 if(groupName ==
"/")
2410 groupName = std::string(groupName.begin()+1, groupName.end());
2413 if( groupName.size() != 0 && *groupName.rbegin() !=
'/')
2415 groupName = groupName +
'/';
2423 H5E_auto2_t error_handler_ori;
2424 void *error_data_ori;
2425 H5Eget_auto(H5E_DEFAULT, &error_handler_ori, &error_data_ori);
2428 H5Eset_auto(H5E_DEFAULT, NULL, NULL);
2430 H5Eset_auto(H5E_DEFAULT, error_handler_ori, error_data_ori));
2433 std::string::size_type begin = 0, end = groupName.find(
'/');
2434 while (end != std::string::npos)
2436 std::string group(groupName.begin()+begin, groupName.begin()+end);
2438 hid_t prevParent = parent;
2439 parent = H5Gopen(prevParent, group.c_str(), H5P_DEFAULT);
2440 if(parent < 0 && create)
2441 parent = H5Gcreate(prevParent, group.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
2442 H5Gclose(prevParent);
2448 end = groupName.find(
'/', begin);
2457 inline void deleteDataset_(hid_t parent, std::string datasetName)
2460 if(H5LTfind_dataset(parent, datasetName.c_str()))
2463 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR <= 6)
2464 if(H5Gunlink(parent, datasetName.c_str()) < 0)
2466 vigra_postcondition(
false,
"HDF5File::deleteDataset_(): Unable to delete existing data.");
2469 if(H5Ldelete(parent, datasetName.c_str(), H5P_DEFAULT ) < 0)
2471 vigra_postcondition(
false,
"HDF5File::deleteDataset_(): Unable to delete existing data.");
2479 hid_t getDatasetHandle_(std::string datasetName)
const
2484 std::string groupname = SplitString(datasetName).first();
2485 std::string setname = SplitString(datasetName).last();
2487 if(H5Lexists(fileHandle_, datasetName.c_str(), H5P_DEFAULT) <= 0)
2489 std::cerr <<
"HDF5File::getDatasetHandle_(): Dataset '" << datasetName <<
"' does not exist.\n";
2494 HDF5Handle groupHandle(openGroup_(groupname), &H5Gclose,
"HDF5File::getDatasetHandle_(): Internal error");
2496 return H5Dopen(groupHandle, setname.c_str(), H5P_DEFAULT);
2501 H5O_type_t get_object_type_(std::string name)
const
2504 std::string group_name = SplitString(name).first();
2505 std::string object_name = SplitString(name).last();
2506 if (!object_name.size())
2507 return H5O_TYPE_GROUP;
2509 htri_t exists = H5Lexists(fileHandle_, name.c_str(), H5P_DEFAULT);
2510 vigra_precondition(exists > 0,
"HDF5File::get_object_type_(): "
2511 "object \"" + name +
"\" "
2514 HDF5Handle group_handle(openGroup_(group_name), &H5Gclose,
"Internal error");
2515 return HDF5_get_type(group_handle, name.c_str());
2520 template<
unsigned int N,
class T,
class Str
ide>
2521 void write_attribute_(std::string name,
2522 const std::string & attribute_name,
2523 const MultiArrayView<N, T, Stride> & array,
2524 const hid_t datatype,
2525 const int numBandsOfType);
2533 inline void writeAtomicAttribute(std::string datasetName, std::string attributeName,
const T data)
2538 typename MultiArrayShape<1>::type chunkSize;
2540 MultiArray<1,T> array(MultiArrayShape<1>::type(1));
2542 write_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), 1);
2547 template<
unsigned int N,
class T,
class Str
ide>
2548 void read_attribute_(std::string datasetName,
2549 std::string attributeName,
2550 MultiArrayView<N, T, Stride> array,
2551 const hid_t datatype,
const int numBandsOfType);
2559 inline void readAtomicAttribute(std::string datasetName, std::string attributeName, T & data)
2564 MultiArray<1,T> array(MultiArrayShape<1>::type(1));
2565 read_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), 1);
2569 inline void readAtomicAttribute(std::string datasetName, std::string attributeName, std::string & data)
2574 MultiArray<1,const char *> array(MultiArrayShape<1>::type(1));
2575 read_attribute_(datasetName, attributeName, array, detail::getH5DataType<const char *>(), 1);
2576 data = std::string(array[0]);
2581 template<
unsigned int N,
class T,
class Str
ide>
2582 void write_(std::string &datasetName,
2583 const MultiArrayView<N, T, Stride> & array,
2584 const hid_t datatype,
2585 const int numBandsOfType,
2586 typename MultiArrayShape<N>::type &chunkSize,
2587 int compressionParameter = 0);
2598 inline void writeAtomic(std::string datasetName,
const T data)
2603 typename MultiArrayShape<1>::type chunkSize;
2605 MultiArray<1,T> array(MultiArrayShape<1>::type(1));
2607 write_(datasetName, array, detail::getH5DataType<T>(), 1, chunkSize,0);
2612 template<
unsigned int N,
class T,
class Str
ide>
2613 void read_(std::string datasetName,
2614 MultiArrayView<N, T, Stride> array,
2615 const hid_t datatype,
const int numBandsOfType);
2626 inline void readAtomic(std::string datasetName, T & data)
2631 MultiArray<1,T> array(MultiArrayShape<1>::type(1));
2632 read_(datasetName, array, detail::getH5DataType<T>(), 1);
2636 inline void readAtomic(std::string datasetName, std::string & data)
2641 MultiArray<1,const char *> array(MultiArrayShape<1>::type(1));
2642 read_(datasetName, array, detail::getH5DataType<const char *>(), 1);
2643 data = std::string(array[0]);
2648 template<
unsigned int N,
class T,
class Str
ide>
2649 void writeBlock_(std::string datasetName,
2650 typename MultiArrayShape<N>::type &blockOffset,
2651 const MultiArrayView<N, T, Stride> & array,
2652 const hid_t datatype,
2653 const int numBandsOfType)
2656 std::string errorMessage =
"HDF5File::writeBlock(): Error opening dataset '" + datasetName +
"'.";
2657 HDF5HandleShared dataset(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
2658 herr_t status = writeBlock_(dataset, blockOffset, array, datatype, numBandsOfType);
2659 vigra_postcondition(status >= 0,
2660 "HDF5File::writeBlock(): write to dataset '" + datasetName +
"' via H5Dwrite() failed.");
2667 template<
unsigned int N,
class T,
class Str
ide>
2668 herr_t writeBlock_(HDF5HandleShared dataset,
2669 typename MultiArrayShape<N>::type &blockOffset,
2670 const MultiArrayView<N, T, Stride> & array,
2671 const hid_t datatype,
2672 const int numBandsOfType);
2678 template<
unsigned int N,
class T,
class Str
ide>
2679 void readBlock_(std::string datasetName,
2680 typename MultiArrayShape<N>::type &blockOffset,
2681 typename MultiArrayShape<N>::type &blockShape,
2682 MultiArrayView<N, T, Stride> array,
2683 const hid_t datatype,
const int numBandsOfType)
2685 std::string errorMessage (
"HDF5File::readBlock(): Unable to open dataset '" + datasetName +
"'.");
2686 HDF5HandleShared dataset(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
2687 herr_t status = readBlock_(dataset, blockOffset, blockShape, array, datatype, numBandsOfType);
2688 vigra_postcondition(status >= 0,
2689 "HDF5File::readBlock(): read from dataset '" + datasetName +
"' via H5Dread() failed.");
2697 template<
unsigned int N,
class T,
class Str
ide>
2698 herr_t readBlock_(HDF5HandleShared dataset,
2699 typename MultiArrayShape<N>::type &blockOffset,
2700 typename MultiArrayShape<N>::type &blockShape,
2701 MultiArrayView<N, T, Stride> array,
2702 const hid_t datatype,
const int numBandsOfType);
2707 template<
int N,
class T>
2711 typename detail::HDF5TypeTraits<T>::value_type init,
2713 int compressionParameter)
2715 vigra_precondition(!isReadOnly(),
2716 "HDF5File::createDataset(): file is read-only.");
2721 std::string groupname = SplitString(datasetName).first();
2722 std::string setname = SplitString(datasetName).last();
2724 hid_t parent = openCreateGroup_(groupname);
2727 deleteDataset_(parent, setname);
2731 typedef detail::HDF5TypeTraits<T> TypeTraits;
2733 if(TypeTraits::numberOfBands() > 1)
2735 shape_inv.resize(N+1);
2736 shape_inv[N] = TypeTraits::numberOfBands();
2740 shape_inv.resize(N);
2742 for(
int k=0; k<N; ++k)
2743 shape_inv[N-1-k] = shape[k];
2747 dataspaceHandle =
HDF5Handle(H5Screate_simple(shape_inv.
size(), shape_inv.
data(), NULL),
2748 &H5Sclose,
"HDF5File::createDataset(): unable to create dataspace for scalar data.");
2751 HDF5Handle plist ( H5Pcreate(H5P_DATASET_CREATE), &H5Pclose,
"HDF5File::createDataset(): unable to create property list." );
2752 H5Pset_fill_value(plist, TypeTraits::getH5DataType(), &init);
2755 H5Pset_obj_track_times(plist, track_time);
2758 ArrayVector<hsize_t> chunks(defineChunks(chunkSize, shape, TypeTraits::numberOfBands(), compressionParameter));
2759 if(chunks.
size() > 0)
2761 std::reverse(chunks.
begin(), chunks.
end());
2762 H5Pset_chunk (plist, chunks.
size(), chunks.
begin());
2766 if(compressionParameter > 0)
2768 H5Pset_deflate(plist, compressionParameter);
2773 TypeTraits::getH5DataType(),
2774 dataspaceHandle, H5P_DEFAULT, plist, H5P_DEFAULT),
2776 "HDF5File::createDataset(): unable to create dataset.");
2777 if(parent != cGroupHandle_)
2780 return datasetHandle;
2785 template<
unsigned int N,
class T,
class Str
ide>
2786 void HDF5File::write_(std::string &datasetName,
2788 const hid_t datatype,
2789 const int numBandsOfType,
2791 int compressionParameter)
2793 vigra_precondition(!isReadOnly(),
2794 "HDF5File::write(): file is read-only.");
2796 std::string groupname = SplitString(datasetName).first();
2797 std::string setname = SplitString(datasetName).last();
2801 std::reverse(shape.begin(), shape.end());
2803 if(numBandsOfType > 1)
2804 shape.push_back(numBandsOfType);
2806 HDF5Handle dataspace(H5Screate_simple(shape.size(), shape.begin(), NULL), &H5Sclose,
2807 "HDF5File::write(): Can not create dataspace.");
2810 std::string errorMessage (
"HDF5File::write(): can not create group '" + groupname +
"'.");
2811 HDF5Handle groupHandle(openCreateGroup_(groupname), &H5Gclose, errorMessage.c_str());
2814 deleteDataset_(groupHandle, setname.c_str());
2817 HDF5Handle plist(H5Pcreate(H5P_DATASET_CREATE), &H5Pclose,
2818 "HDF5File::write(): unable to create property list." );
2821 H5Pset_obj_track_times(plist, track_time);
2825 if(chunks.size() > 0)
2827 std::reverse(chunks.begin(), chunks.end());
2828 H5Pset_chunk (plist, chunks.size(), chunks.begin());
2832 if(compressionParameter > 0)
2834 H5Pset_deflate(plist, compressionParameter);
2838 HDF5Handle datasetHandle(H5Dcreate(groupHandle, setname.c_str(), datatype, dataspace,H5P_DEFAULT, plist, H5P_DEFAULT),
2839 &H5Dclose,
"HDF5File::write(): Can not create dataset.");
2845 status = H5Dwrite(datasetHandle, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, array.
data());
2855 int offset = numBandsOfType > 1 ? 1 : 0;
2856 std::reverse(shape.begin(), shape.end());
2857 if(chunks.size() > 0)
2860 std::reverse(chunks.begin(), chunks.end());
2865 ArrayVector<hsize_t>(shape.size(), 1).swap(chunks);
2866 chunks[0] = numBandsOfType;
2868 for(
unsigned int k=0; k<N; ++k)
2870 chunks[k+offset] = array.
shape(k);
2871 prod *= array.
shape(k);
2877 ArrayVector<hsize_t> null(shape.size(), 0),
2878 start(shape.size(), 0),
2879 count(shape.size(), 1);
2881 count[N-1-offset] = numBandsOfType;
2883 typedef typename MultiArrayShape<N>::type Shape;
2884 Shape chunkCount, chunkMaxShape;
2885 for(
unsigned int k=offset; k<chunks.size(); ++k)
2887 chunkMaxShape[k-offset] = chunks[k];
2891 typename CoupledIteratorType<N>::type chunkIter = createCoupledIterator(chunkCount),
2892 chunkEnd = chunkIter.getEndIterator();
2893 for(; chunkIter != chunkEnd; ++chunkIter)
2895 Shape chunkStart(chunkIter.point() * chunkMaxShape),
2896 chunkStop(min(chunkStart + chunkMaxShape, array.
shape()));
2897 MultiArray<N, T> buffer(array.
subarray(chunkStart, chunkStop));
2899 for(
unsigned int k=0; k<N; ++k)
2901 start[N-1-k] = chunkStart[k];
2902 count[N-1-k] = buffer.shape(k);
2907 count[N] = numBandsOfType;
2909 HDF5Handle filespace(H5Dget_space(datasetHandle),
2910 &H5Sclose,
"HDF5File::write(): unable to create hyperslabs.");
2911 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, start.data(), NULL, count.data(), NULL);
2915 HDF5Handle dataspace2(H5Screate_simple(count.size(), count.data(), NULL),
2916 &H5Sclose,
"HDF5File::write(): unable to create hyperslabs.");
2917 status = H5Sselect_hyperslab(dataspace2, H5S_SELECT_SET, null.data(), NULL, count.data(), NULL);
2921 status = H5Dwrite(datasetHandle, datatype, dataspace2, filespace, H5P_DEFAULT, buffer.data());
2926 vigra_postcondition(status >= 0,
2927 "HDF5File::write(): write to dataset '" + datasetName +
"' via H5Dwrite() failed.");
2932 template<
unsigned int N,
class T,
class Str
ide>
2933 herr_t HDF5File::writeBlock_(HDF5HandleShared datasetHandle,
2934 typename MultiArrayShape<N>::type &blockOffset,
2935 const MultiArrayView<N, T, Stride> & array,
2936 const hid_t datatype,
2937 const int numBandsOfType)
2939 vigra_precondition(!isReadOnly(),
2940 "HDF5File::writeBlock(): file is read-only.");
2942 ArrayVector<hsize_t> boffset, bshape, bones(N+1, 1);
2943 hssize_t dimensions = getDatasetDimensions_(datasetHandle);
2944 if(numBandsOfType > 1)
2946 vigra_precondition(N+1 == dimensions,
2947 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
2949 boffset.resize(N+1);
2950 bshape[N] = numBandsOfType;
2955 vigra_precondition(N == dimensions,
2956 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
2961 for(
unsigned i = 0; i < N; ++i)
2964 bshape[N-1-i] = array.shape(i);
2965 boffset[N-1-i] = blockOffset[i];
2969 HDF5Handle memspace_handle (H5Screate_simple(bshape.size(), bshape.data(), NULL),
2971 "Unable to get origin dataspace");
2974 HDF5Handle dataspaceHandle (H5Dget_space(datasetHandle),&H5Sclose,
"Unable to create target dataspace");
2975 H5Sselect_hyperslab(dataspaceHandle, H5S_SELECT_SET,
2976 boffset.data(), bones.data(), bones.data(), bshape.data());
2979 if(array.isUnstrided())
2982 status = H5Dwrite( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, array.data());
2987 MultiArray<N, T> buffer(array);
2988 status = H5Dwrite( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, buffer.data());
2995 template<
unsigned int N,
class T,
class Str
ide>
2996 void HDF5File::write_attribute_(std::string name,
2997 const std::string & attribute_name,
2998 const MultiArrayView<N, T, Stride> & array,
2999 const hid_t datatype,
3000 const int numBandsOfType)
3002 vigra_precondition(!isReadOnly(),
3003 "HDF5File::writeAttribute(): file is read-only.");
3006 ArrayVector<hsize_t> shape(array.shape().begin(), array.shape().end());
3007 std::reverse(shape.begin(), shape.end());
3008 if(numBandsOfType > 1)
3009 shape.push_back(numBandsOfType);
3011 HDF5Handle dataspace(H5Screate_simple(shape.size(),
3012 shape.begin(), NULL),
3013 &H5Sclose,
"HDF5File::writeAttribute(): Can not"
3014 " create dataspace.");
3016 std::string errorMessage (
"HDF5File::writeAttribute(): can not find "
3017 "object '" + name +
"'.");
3019 H5O_type_t h5_type = get_object_type_(name);
3020 bool is_group = h5_type == H5O_TYPE_GROUP;
3021 if (!is_group && h5_type != H5O_TYPE_DATASET)
3022 vigra_precondition(0,
"HDF5File::writeAttribute(): object \""
3023 + name +
"\" is neither a group nor a "
3026 HDF5Handle object_handle(is_group
3027 ? openCreateGroup_(name)
3028 : getDatasetHandle_(name),
3032 errorMessage.c_str());
3035 HDF5Handle attributeHandle(exists
3036 ? H5Aopen(object_handle,
3037 attribute_name.c_str(),
3039 : H5Acreate(object_handle,
3040 attribute_name.c_str(), datatype,
3041 dataspace, H5P_DEFAULT,
3044 "HDF5File::writeAttribute(): Can not create"
3047 if(array.isUnstrided())
3050 status = H5Awrite(attributeHandle, datatype, array.data());
3056 MultiArray<N, T> buffer(array);
3057 status = H5Awrite(attributeHandle, datatype, buffer.data());
3059 vigra_postcondition(status >= 0,
3060 "HDF5File::writeAttribute(): write to attribute '" + attribute_name +
"' via H5Awrite() failed.");
3065 template<
unsigned int N,
class T,
class Str
ide>
3066 void HDF5File::read_(std::string datasetName,
3067 MultiArrayView<N, T, Stride> array,
3068 const hid_t datatype,
const int numBandsOfType)
3073 std::string errorMessage (
"HDF5File::read(): Unable to open dataset '" + datasetName +
"'.");
3074 HDF5Handle datasetHandle(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
3078 int offset = (numBandsOfType > 1)
3083 "HDF5File::read(): Array dimension disagrees with dataset dimension.");
3085 typename MultiArrayShape<N>::type shape;
3086 for(
int k=offset; k < (int)dimshape.size(); ++k)
3089 vigra_precondition(shape == array.shape(),
3090 "HDF5File::read(): Array shape disagrees with dataset shape.");
3092 vigra_precondition(dimshape[0] == static_cast<hsize_t>(numBandsOfType),
3093 "HDF5File::read(): Band count doesn't match destination array compound type.");
3096 if(array.isUnstrided())
3099 status = H5Dread(datasetHandle, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, array.data() );
3105 ArrayVector<hsize_t> null(dimshape.size(), 0),
3106 chunks(dimshape.size(), 1),
3107 start(dimshape.size(), 0),
3108 count(dimshape.size(), 1);
3110 HDF5Handle properties(H5Dget_create_plist(datasetHandle),
3111 &H5Pclose,
"HDF5File::read(): failed to get property list");
3112 if(H5D_CHUNKED == H5Pget_layout(properties))
3115 H5Pget_chunk(properties, static_cast<int>(chunks.size()), chunks.data());
3116 std::reverse(chunks.begin(), chunks.end());
3121 chunks[0] = numBandsOfType;
3123 for(
unsigned int k=0; k<N; ++k)
3125 chunks[k+offset] = array.shape(k);
3126 prod *= array.shape(k);
3132 count[N-1-offset] =
static_cast<hsize_t
>(numBandsOfType);
3134 typedef typename MultiArrayShape<N>::type Shape;
3135 Shape chunkCount, chunkMaxShape;
3136 for(
unsigned int k=offset; k<chunks.size(); ++k)
3138 chunkMaxShape[k-offset] = chunks[k];
3142 typename CoupledIteratorType<N>::type chunkIter = createCoupledIterator(chunkCount),
3143 chunkEnd = chunkIter.getEndIterator();
3144 for(; chunkIter != chunkEnd; ++chunkIter)
3146 Shape chunkStart(chunkIter.point() * chunkMaxShape),
3147 chunkStop(min(chunkStart + chunkMaxShape, array.shape()));
3148 MultiArray<N, T> buffer(chunkStop - chunkStart);
3150 for(
unsigned int k=0; k<N; ++k)
3152 start[N-1-k] = chunkStart[k];
3153 count[N-1-k] = buffer.shape(k);
3158 count[N] = numBandsOfType;
3160 HDF5Handle filespace(H5Dget_space(datasetHandle),
3161 &H5Sclose,
"HDF5File::read(): unable to create hyperslabs.");
3162 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, start.data(), NULL, count.data(), NULL);
3166 HDF5Handle dataspace(H5Screate_simple(count.size(), count.data(), NULL),
3167 &H5Sclose,
"HDF5File::read(): unable to create hyperslabs.");
3168 status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, null.data(), NULL, count.data(), NULL);
3172 status = H5Dread(datasetHandle, datatype, dataspace, filespace, H5P_DEFAULT, buffer.data());
3176 array.subarray(chunkStart, chunkStop) = buffer;
3179 vigra_postcondition(status >= 0,
3180 "HDF5File::read(): read from dataset '" + datasetName +
"' via H5Dread() failed.");
3185 template<
unsigned int N,
class T,
class Str
ide>
3186 herr_t HDF5File::readBlock_(HDF5HandleShared datasetHandle,
3187 typename MultiArrayShape<N>::type &blockOffset,
3188 typename MultiArrayShape<N>::type &blockShape,
3189 MultiArrayView<N, T, Stride> array,
3190 const hid_t datatype,
const int numBandsOfType)
3192 vigra_precondition(blockShape == array.shape(),
3193 "HDF5File::readBlock(): Array shape disagrees with block size.");
3195 ArrayVector<hsize_t> boffset, bshape, bones(N+1, 1);
3196 hssize_t dimensions = getDatasetDimensions_(datasetHandle);
3197 if(numBandsOfType > 1)
3199 vigra_precondition(N+1 == dimensions,
3200 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
3202 boffset.resize(N+1);
3203 bshape[N] = numBandsOfType;
3208 vigra_precondition(N == dimensions,
3209 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
3214 for(
unsigned i = 0; i < N; ++i)
3217 bshape[N-1-i] = blockShape[i];
3218 boffset[N-1-i] = blockOffset[i];
3222 HDF5Handle memspace_handle(H5Screate_simple(bshape.size(), bshape.data(), NULL),
3224 "Unable to create target dataspace");
3227 HDF5Handle dataspaceHandle(H5Dget_space(datasetHandle), &H5Sclose,
3228 "Unable to get dataspace");
3229 H5Sselect_hyperslab(dataspaceHandle, H5S_SELECT_SET,
3230 boffset.data(), bones.data(), bones.data(), bshape.data());
3233 if(array.isUnstrided())
3236 status = H5Dread( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, array.data());
3241 MultiArray<N, T> buffer(array.shape());
3242 status = H5Dread( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, buffer.data());
3252 template<
unsigned int N,
class T,
class Str
ide>
3253 void HDF5File::read_attribute_(std::string datasetName,
3254 std::string attributeName,
3255 MultiArrayView<N, T, Stride> array,
3256 const hid_t datatype,
const int numBandsOfType)
3260 std::string message =
"HDF5File::readAttribute(): could not get handle for attribute '"+attributeName+
"'' of object '"+dataset_path+
"'.";
3261 HDF5Handle attr_handle (H5Aopen_by_name(fileHandle_,dataset_path.c_str(),attributeName.c_str(),H5P_DEFAULT,H5P_DEFAULT),&H5Aclose, message.c_str());
3264 message =
"HDF5File::readAttribute(): could not get dataspace for attribute '"+attributeName+
"'' of object '"+dataset_path+
"'.";
3265 HDF5Handle attr_dataspace_handle (H5Aget_space(attr_handle),&H5Sclose,message.c_str());
3268 int raw_dims = H5Sget_simple_extent_ndims(attr_dataspace_handle);
3269 int dims = std::max(raw_dims, 1);
3270 ArrayVector<hsize_t> dimshape(dims);
3272 H5Sget_simple_extent_dims(attr_dataspace_handle, dimshape.data(), NULL);
3277 std::reverse(dimshape.begin(), dimshape.end());
3279 int offset = (numBandsOfType > 1)
3282 message =
"HDF5File::readAttribute(): Array dimension disagrees with dataset dimension.";
3286 for(
int k=offset; k < (int)dimshape.size(); ++k)
3287 vigra_precondition(array.shape()[k-offset] == (
MultiArrayIndex)dimshape[k],
3288 "HDF5File::readAttribute(): Array shape disagrees with dataset shape");
3291 if(array.isUnstrided())
3294 status = H5Aread( attr_handle, datatype, array.data());
3300 MultiArray<N, T> buffer(array.shape());
3301 status = H5Aread( attr_handle, datatype, buffer.data() );
3306 vigra_postcondition(status >= 0,
3307 "HDF5File::readAttribute(): read from attribute '" + attributeName +
"' via H5Aread() failed.");
3349 template<
unsigned int N,
class T,
class Str
ideTag>
3350 inline void readHDF5(
const HDF5ImportInfo &info, MultiArrayView<N, T, StrideTag> array)
3352 HDF5File file(info.getFilePath(), HDF5File::OpenReadOnly);
3353 file.read(info.getPathInFile(), array);
3356 inline hid_t openGroup(hid_t parent, std::string group_name)
3359 size_t last_slash = group_name.find_last_of(
'/');
3360 if (last_slash == std::string::npos || last_slash != group_name.size() - 1)
3361 group_name = group_name +
'/';
3362 std::string::size_type begin = 0, end = group_name.find(
'/');
3364 while (end != std::string::npos)
3366 std::string group(group_name.begin()+begin, group_name.begin()+end);
3367 hid_t prev_parent = parent;
3368 parent = H5Gopen(prev_parent, group.c_str(), H5P_DEFAULT);
3370 if(ii != 0) H5Gclose(prev_parent);
3371 if(parent < 0)
return parent;
3374 end = group_name.find(
'/', begin);
3379 inline hid_t createGroup(hid_t parent, std::string group_name)
3381 if(group_name.size() == 0 ||*group_name.rbegin() !=
'/')
3382 group_name = group_name +
'/';
3383 if(group_name ==
"/")
3384 return H5Gopen(parent, group_name.c_str(), H5P_DEFAULT);
3386 std::string::size_type begin = 0, end = group_name.find(
'/');
3388 while (end != std::string::npos)
3390 std::string group(group_name.begin()+begin, group_name.begin()+end);
3391 hid_t prev_parent = parent;
3393 if(H5LTfind_dataset(parent, group.c_str()) == 0)
3395 parent = H5Gcreate(prev_parent, group.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
3397 parent = H5Gopen(prev_parent, group.c_str(), H5P_DEFAULT);
3400 if(ii != 0) H5Gclose(prev_parent);
3401 if(parent < 0)
return parent;
3404 end = group_name.find(
'/', begin);
3409 inline void deleteDataset(hid_t parent, std::string dataset_name)
3412 if(H5LTfind_dataset(parent, dataset_name.c_str()))
3415 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR <= 6)
3416 if(H5Gunlink(parent, dataset_name.c_str()) < 0)
3418 vigra_postcondition(
false,
"writeToHDF5File(): Unable to delete existing data.");
3421 if(H5Ldelete(parent, dataset_name.c_str(), H5P_DEFAULT ) < 0)
3423 vigra_postcondition(
false,
"createDataset(): Unable to delete existing data.");
3429 inline hid_t createFile(std::string filePath,
bool append_ =
true)
3432 pFile = fopen ( filePath.c_str(),
"r" );
3434 if ( pFile == NULL )
3436 file_id = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
3441 file_id = H5Fopen(filePath.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
3446 std::remove(filePath.c_str());
3447 file_id = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
3452 template<
unsigned int N,
class T,
class Tag>
3453 void createDataset(
const char* filePath,
const char* pathInFile,
const MultiArrayView<N, T, Tag> & array,
const hid_t datatype,
const int numBandsOfType, HDF5Handle & file_handle, HDF5Handle & dataset_handle)
3455 std::string path_name(pathInFile), group_name, data_set_name, message;
3456 std::string::size_type delimiter = path_name.rfind(
'/');
3459 file_handle = HDF5Handle(createFile(filePath), &H5Fclose,
3460 "createDataset(): unable to open output file.");
3463 if(delimiter == std::string::npos)
3466 data_set_name = path_name;
3470 group_name = std::string(path_name.begin(), path_name.begin()+delimiter);
3471 data_set_name = std::string(path_name.begin()+delimiter+1, path_name.end());
3475 HDF5Handle group(createGroup(file_handle, group_name), &H5Gclose,
3476 "createDataset(): Unable to create and open group. generic v");
3479 deleteDataset(group, data_set_name);
3483 HDF5Handle dataspace_handle;
3484 if(numBandsOfType > 1) {
3486 hsize_t shape_inv[N+1];
3487 for(
unsigned int k=0; k<N; ++k) {
3488 shape_inv[N-1-k] = array.shape(k);
3491 shape_inv[N] = numBandsOfType;
3494 dataspace_handle = HDF5Handle(H5Screate_simple(N+1, shape_inv, NULL),
3495 &H5Sclose,
"createDataset(): unable to create dataspace for non-scalar data.");
3498 hsize_t shape_inv[N];
3499 for(
unsigned int k=0; k<N; ++k)
3500 shape_inv[N-1-k] = array.shape(k);
3503 dataspace_handle = HDF5Handle(H5Screate_simple(N, shape_inv, NULL),
3504 &H5Sclose,
"createDataset(): unable to create dataspace for scalar data.");
3508 dataset_handle = HDF5Handle(H5Dcreate(group,
3509 data_set_name.c_str(),
3512 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT),
3513 &H5Dclose,
"createDataset(): unable to create dataset.");
3556 template<
unsigned int N,
class T,
class Str
ideTag>
3557 inline void writeHDF5(
const char* filePath,
const char* pathInFile,
const MultiArrayView<N, T, StrideTag> & array)
3559 HDF5File file(filePath, HDF5File::Open);
3560 file.write(pathInFile, array);
3573 void operator()(std::string
const & in)
3575 size = in.size() > size ?
3583 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8) || DOXYGEN
3590 template<
size_t N,
class T,
class C>
3595 if(H5Aexists(loc, name) > 0)
3596 H5Adelete(loc, name);
3599 array.
shape().end());
3601 dataspace_handle(H5Screate_simple(N, shape.data(), NULL),
3603 "writeToHDF5File(): unable to create dataspace.");
3607 detail::getH5DataType<T>(),
3609 H5P_DEFAULT ,H5P_DEFAULT ),
3611 "writeHDF5Attr: unable to create Attribute");
3615 for(
int ii = 0; ii < array.
size(); ++ii)
3616 buffer.push_back(array[ii]);
3617 H5Awrite(attr, detail::getH5DataType<T>(), buffer.
data());
3626 template<
size_t N,
class C>
3631 if(H5Aexists(loc, name) > 0)
3632 H5Adelete(loc, name);
3635 array.
shape().end());
3637 dataspace_handle(H5Screate_simple(N, shape.data(), NULL),
3639 "writeToHDF5File(): unable to create dataspace.");
3643 "writeToHDF5File(): unable to create type.");
3645 detail::MaxSizeFnc max_size;
3646 max_size = std::for_each(array.
data(),array.
data()+ array.
size(), max_size);
3647 H5Tset_size (atype, max_size.size);
3653 H5P_DEFAULT ,H5P_DEFAULT ),
3655 "writeHDF5Attr: unable to create Attribute");
3657 std::string buf =
"";
3658 for(
int ii = 0; ii < array.
size(); ++ii)
3660 buf = buf + array[ii]
3661 + std::string(max_size.size - array[ii].
size(),
' ');
3663 H5Awrite(attr, atype, buf.c_str());
3691 std::string pathInFile,
3694 std::string path_name(pathInFile), group_name, data_set_name, message, attr_name;
3695 std::string::size_type delimiter = path_name.rfind(
'/');
3698 HDF5Handle file_id(createFile(filePath), &H5Fclose,
3699 "writeToHDF5File(): unable to open output file.");
3702 if(delimiter == std::string::npos)
3705 data_set_name = path_name;
3710 group_name = std::string(path_name.begin(), path_name.begin()+delimiter);
3711 data_set_name = std::string(path_name.begin()+delimiter+1, path_name.end());
3713 delimiter = data_set_name.rfind(
'.');
3714 if(delimiter == std::string::npos)
3716 attr_name = path_name;
3717 data_set_name =
"/";
3721 attr_name = std::string(data_set_name.begin()+delimiter+1, data_set_name.end());
3722 data_set_name = std::string(data_set_name.begin(), data_set_name.begin()+delimiter);
3725 HDF5Handle group(openGroup(file_id, group_name), &H5Gclose,
3726 "writeToHDF5File(): Unable to create and open group. attr ver");
3728 if(data_set_name !=
"/")
3730 HDF5Handle dset(H5Dopen(group, data_set_name.c_str(), H5P_DEFAULT), &H5Dclose,
3731 "writeHDF5Attr():unable to open dataset");
3746 #endif // VIGRA_HDF5IMPEX_HXX
HDF5File & operator=(HDF5File const &other)
Assign a HDF5File object.
Definition: hdf5impex.hxx:1127
void read(const std::string &datasetName, ArrayVectorView< T > array)
Read data into an array vector. If the first character of datasetName is a "/", the path will be inte...
Definition: hdf5impex.hxx:1956
HDF5Handle()
Default constructor. Creates a NULL handle.
Definition: hdf5impex.hxx:205
void write(const std::string &datasetName, const ArrayVectorView< T > &array, int compression=0)
Write array vectors.
Definition: hdf5impex.hxx:1843
bool cd_up(int levels)
Change the current group to its parent group. Returns true if successful, false otherwise. If unsuccessful, the group will not change.
Definition: hdf5impex.hxx:1224
std::vector< std::string > listAttributes(std::string const &group_or_dataset) const
List the attribute names of the given group or dataset.
Definition: hdf5impex.hxx:1514
bool operator==(HDF5Handle const &h) const
Equality comparison of the contained handle.
Definition: hdf5impex.hxx:346
Temporarily disable HDF5's native error output.
Definition: hdf5impex.hxx:130
ArrayVector< hsize_t > getDatasetShape(std::string datasetName) const
Get the shape of each dimension of a certain dataset.
Definition: hdf5impex.hxx:1371
Wrapper for unique hid_t objects.
Definition: hdf5impex.hxx:191
HDF5File(std::string filePath, OpenMode mode, bool track_creation_times=false)
Open or create an HDF5File object.
Definition: hdf5impex.hxx:1052
MultiArrayIndex numDimensions() const
bool operator!=(hid_t h) const
Inequality comparison of the contained handle.
Definition: hdf5impex.hxx:617
HDF5HandleShared()
Default constructor. Creates a NULL handle.
Definition: hdf5impex.hxx:427
const difference_type & shape() const
Definition: multi_array.hxx:1594
void readAttribute(std::string object_name, std::string attribute_name, MultiArrayView< N, T, Stride > array)
Read MultiArray Attributes. In contrast to datasets, subarray access is not available.
Definition: hdf5impex.hxx:1637
void cd_mk(std::string groupName)
Change the current group; create it if necessary. If the first character is a "/", the path will be interpreted as absolute path, otherwise it will be interpreted as path relative to the current group.
Definition: hdf5impex.hxx:1262
hssize_t getDatasetDimensions(std::string datasetName) const
Get the number of dimensions of a certain dataset If the first character is a "/", the path will be interpreted as absolute path, otherwise it will be interpreted as path relative to the current group.
Definition: hdf5impex.hxx:1342
Definition: array_vector.hxx:76
const_iterator begin() const
Definition: array_vector.hxx:223
pointer data() const
Definition: multi_array.hxx:1844
HDF5ImportInfo(const char *filePath, const char *pathInFile)
HDF5Handle(hid_t h, Destructor destructor, const char *error_message)
Create a wrapper for a hid_t object.
Definition: hdf5impex.hxx:229
std::string get_absolute_path(std::string path) const
takes any path and converts it into an absolute path in the current file.
Definition: hdf5impex.hxx:2247
void swap(HDF5HandleShared &h)
Swap the contents of two handle wrappers.
Definition: hdf5impex.hxx:567
void reshape(const difference_type &shape)
Definition: multi_array.hxx:2807
HDF5HandleShared getDatasetHandleShared(std::string const &datasetName) const
Obtain a shared HDF5 handle of a dataset.
Definition: hdf5impex.hxx:1466
void mkdir(std::string groupName)
Create a new group. If the first character is a "/", the path will be interpreted as absolute path...
Definition: hdf5impex.hxx:1245
bool existsAttribute(std::string object_name, std::string attribute_name)
Test if attribute exists.
Definition: hdf5impex.hxx:1620
const std::string & getFilePath() const
std::ptrdiff_t MultiArrayIndex
Definition: multi_fwd.hxx:60
void writeHDF5Attr(hid_t loc, const char *name, MultiArrayView< N, T, C > const &array)
Definition: hdf5impex.hxx:3591
HDF5HandleShared createDataset(std::string datasetName, TinyVector< MultiArrayIndex, N > const &shape, typename detail::HDF5TypeTraits< T >::value_type init=typename detail::HDF5TypeTraits< T >::value_type(), TinyVector< MultiArrayIndex, N > const &chunkSize=(TinyVector< MultiArrayIndex, N >()), int compressionParameter=0)
Create a new dataset. This function can be used to create a dataset filled with a default value init...
Definition: hdf5impex.hxx:2709
bool operator==(hid_t h) const
Equality comparison of the contained handle.
Definition: hdf5impex.hxx:353
hid_t release()
Return the contained handle and set the wrapper to NULL without calling close().
Definition: hdf5impex.hxx:290
const std::string & getPathInFile() const
herr_t close()
Close the handle if this is the unique (i.e. last) owner.
Definition: hdf5impex.hxx:508
#define VIGRA_FINALLY(destructor)
Definition: utilities.hxx:206
void ls(Container &cont) const
List the contents of the current group into a container-like object via insert(). ...
Definition: hdf5impex.hxx:1309
void write(std::string datasetName, const MultiArrayView< N, T, Stride > &array, typename MultiArrayShape< N >::type chunkSize, int compression=0)
Write multi arrays. Chunks can be activated by providing a MultiArrayShape as chunkSize. chunkSize must have equal dimension as array.
Definition: hdf5impex.hxx:1762
Argument object for the function readHDF5().
Definition: hdf5impex.hxx:663
bool isHDF5(char const *filename)
Check if given filename refers to a HDF5 file.
Definition: hdf5impex.hxx:102
void reset(hid_t h, Destructor destructor, const char *error_message)
Reset the handle to a new value.
Definition: hdf5impex.hxx:531
HDF5File(HDF5File const &other)
Copy a HDF5File object.
Definition: hdf5impex.hxx:1103
HDF5HandleShared & operator=(HDF5HandleShared const &h)
Assignment. Call close() for the present LHS handle and share ownership with the RHS handle (analogou...
Definition: hdf5impex.hxx:479
HDF5HandleShared(HDF5HandleShared const &h)
Copy constructor. Shares ownership with the RHS handle (analogous to std::shared_ptr).
Definition: hdf5impex.hxx:466
bool operator!=(hid_t h) const
Inequality comparison of the contained handle.
Definition: hdf5impex.hxx:367
bool operator!=(HDF5Handle const &h) const
Inequality comparison of the contained handle.
Definition: hdf5impex.hxx:360
difference_type_1 size() const
Definition: multi_array.hxx:1587
Wrapper for shared hid_t objects.
Definition: hdf5impex.hxx:412
std::string filename() const
Get the name of the associated file.
Definition: hdf5impex.hxx:1324
HDF5File()
Default constructor.
Definition: hdf5impex.hxx:1031
HDF5Handle & operator=(HDF5Handle const &h)
Assignment. Calls close() for the LHS handle and hands over ownership of the RHS handle (analogous to...
Definition: hdf5impex.hxx:252
~HDF5File()
The destructor flushes and closes the file.
Definition: hdf5impex.hxx:1114
NumericTraits< V >::Promote prod(TinyVectorBase< V, SIZE, D1, D2 > const &l)
product of the vector's elements
Definition: tinyvector.hxx:2097
bool operator==(HDF5HandleShared const &h) const
Equality comparison of the contained handle.
Definition: hdf5impex.hxx:596
HDF5File(HDF5HandleShared const &fileHandle, const std::string &pathname="", bool read_only=false)
Initialize an HDF5File object from HDF5 file handle.
Definition: hdf5impex.hxx:1075
HDF5Handle getAttributeHandle(std::string dataset_name, std::string attribute_name) const
Obtain the HDF5 handle of a attribute.
Definition: hdf5impex.hxx:1537
void readAndResize(std::string datasetName, MultiArray< N, T, Alloc > &array)
Read data into a MultiArray. Resize MultiArray to the correct size. If the first character of dataset...
Definition: hdf5impex.hxx:1930
bool operator==(hid_t h) const
Equality comparison of the contained handle.
Definition: hdf5impex.hxx:603
void readHDF5(...)
Read the data specified by the given vigra::HDF5ImportInfo object and write the into the given 'array...
void readAndResize(std::string datasetName, ArrayVector< T > &array)
Read data into an array vector. Resize the array vector to the correct size. If the first character o...
Definition: hdf5impex.hxx:1969
~HDF5Handle()
Destructor. Calls close() for the contained handle.
Definition: hdf5impex.hxx:267
bool operator!=(HDF5HandleShared const &h) const
Inequality comparison of the contained handle.
Definition: hdf5impex.hxx:610
void read(std::string datasetName, char &data)
Read a single value. Specialization of the read function for simple datatypes.
Definition: hdf5impex.hxx:2101
doxygen_overloaded_function(template<...> void separableConvolveBlockwise) template< unsigned int N
Separated convolution on ChunkedArrays.
void open(std::string filePath, OpenMode mode)
Open or create the given file in the given mode and set the group to "/". If another file is currentl...
Definition: hdf5impex.hxx:1164
PixelType pixelType() const
OpenMode
Set how a file is opened.
Definition: hdf5impex.hxx:1015
void readAttribute(std::string object_name, std::string attribute_name, char &data)
Read a single value. Specialization of the read function for simple datatypes.
Definition: hdf5impex.hxx:1672
void writeBlock(std::string datasetName, typename MultiArrayShape< N >::type blockOffset, const MultiArrayView< N, T, Stride > &array)
Write a multi array into a larger volume. blockOffset determines the position, where array is written...
Definition: hdf5impex.hxx:1784
void flushToDisk()
Immediately write all data to disk.
Definition: hdf5impex.hxx:2173
HDF5Handle(HDF5Handle const &h)
Copy constructor.
Definition: hdf5impex.hxx:241
void reset(hid_t h, Destructor destructor, const char *error_message)
Reset the wrapper to a new handle.
Definition: hdf5impex.hxx:302
Class for fixed size vectors.This class contains an array of size SIZE of the specified VALUETYPE...
Definition: accessor.hxx:940
void writeHDF5(...)
Store array data in an HDF5 file.
MultiArrayIndex shapeOfDimension(const int dim) const
HDF5Handle getGroupHandle(std::string group_name, std::string function_name="HDF5File::getGroupHandle()")
Obtain the HDF5 handle of a group (create the group if it doesn't exist).
Definition: hdf5impex.hxx:1474
std::string pwd() const
Get the path of the current group.
Definition: hdf5impex.hxx:1317
void read(std::string datasetName, MultiArrayView< N, T, Stride > array)
Read data into a multi array. If the first character of datasetName is a "/", the path will be interp...
Definition: hdf5impex.hxx:1912
void listAttributes(std::string const &group_or_dataset, Container &container) const
Insert the attribute names of the given group or dataset into the given container by calling containe...
Definition: hdf5impex.hxx:1529
bool unique() const
Check if this is the unique owner of the conatined handle.
Definition: hdf5impex.hxx:558
void write(std::string datasetName, char data)
Write a single value. Specialization of the write function for simple datatypes.
Definition: hdf5impex.hxx:1883
const char * getPixelType() const
void writeAttribute(std::string object_name, std::string attribute_name, char data)
Write a single value. Specialization of the write function for simple datatypes.
Definition: hdf5impex.hxx:1585
void writeAttribute(std::string object_name, std::string attribute_name, const MultiArrayView< N, T, Stride > &array)
Write MultiArray Attributes. In contrast to datasets, subarray access, chunks and compression are not...
Definition: hdf5impex.hxx:1550
herr_t close()
Explicitly call the stored destructor (if one has been registered in the constructor) for the contain...
Definition: hdf5impex.hxx:277
void write(std::string datasetName, const MultiArrayView< N, T, Stride > &array, int iChunkSize=0, int compression=0)
Write multi arrays.
Definition: hdf5impex.hxx:1730
image import and export functions
bool isUnstrided(unsigned int dimension=N-1) const
Definition: multi_array.hxx:1232
void close()
Close the current file.
Definition: hdf5impex.hxx:1176
bool cd_up()
Change the current group to its parent group. Returns true if successful, false otherwise. If unsuccessful, the group will not change.
Definition: hdf5impex.hxx:1202
Base class for, and view to, vigra::MultiArray.
Definition: multi_array.hxx:650
std::string getDatasetType(std::string const &datasetName) const
Definition: hdf5impex.hxx:1412
bool existsDataset(std::string datasetName) const
Check if given datasetName exists.
Definition: hdf5impex.hxx:1331
const_iterator end() const
Definition: array_vector.hxx:237
const_pointer data() const
Definition: array_vector.hxx:209
ArrayVector< hsize_t > const & shape() const
Definition: hdf5impex.hxx:712
size_type size() const
Definition: array_vector.hxx:358
MultiArrayView subarray(difference_type p, difference_type q) const
Definition: multi_array.hxx:1474
void readBlock(std::string datasetName, typename MultiArrayShape< N >::type blockOffset, typename MultiArrayShape< N >::type blockShape, MultiArrayView< N, T, Stride > array)
Read a block of data into a multi array. This function allows to read a small block out of a larger v...
Definition: hdf5impex.hxx:2007
Class for a single RGB value.
Definition: accessor.hxx:938
int ceil(FixedPoint< IntBits, FracBits > v)
rounding up.
Definition: fixedpoint.hxx:675
hid_t getDatasetHandle() const
HDF5HandleShared(hid_t h, Destructor destructor, const char *error_message)
Create a shared wrapper for a plain hid_t object.
Definition: hdf5impex.hxx:452
hid_t getH5FileHandle() const
~HDF5HandleShared()
Destructor (calls close())
Definition: hdf5impex.hxx:495
HDF5Handle getDatasetHandle(std::string const &datasetName) const
Obtain the HDF5 handle of a dataset.
Definition: hdf5impex.hxx:1458
void root()
Change current group to "/".
Definition: hdf5impex.hxx:1184
HDF5File(bool track_creation_times)
Construct with time tagging of datasets enabled.
Definition: hdf5impex.hxx:1039
void cd(std::string groupName)
Change the current group. Both absolute and relative group names are allowed.
Definition: hdf5impex.hxx:1193
size_t use_count() const
Get the number of owners of the contained handle.
Definition: hdf5impex.hxx:547
void swap(HDF5Handle &h)
Swap the contents of two handle wrappers.
Definition: hdf5impex.hxx:318
Access to HDF5 files.
Definition: hdf5impex.hxx:958
std::vector< std::string > ls() const
List the contents of the current group. The function returns a vector of strings holding the entries ...
Definition: hdf5impex.hxx:1287