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 51 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR <= 6) 53 # define H5Gopen(a, b, c) H5Gopen(a, b) 56 # define H5Gcreate(a, b, c, d, e) H5Gcreate(a, b, 1) 59 # define H5Dopen(a, b, c) H5Dopen(a, b) 62 # define H5Dcreate(a, b, c, d, e, f, g) H5Dcreate(a, b, c, d, f) 65 # define H5Acreate(a, b, c, d, e, f) H5Acreate(a, b, c, d, e) 67 # ifndef H5Pset_obj_track_times 68 # define H5Pset_obj_track_times(a, b) do {} while (0) 76 #include "multi_array.hxx" 77 #include "multi_iterator_coupled.hxx" 78 #include "multi_impex.hxx" 100 inline bool isHDF5(
char const * filename)
103 return _access(filename, 0) != -1 && H5Fis_hdf5(filename);
105 return access(filename, F_OK) == 0 && H5Fis_hdf5(filename);
130 H5E_auto2_t old_func_;
131 void *old_client_data_;
139 , old_client_data_(0)
141 H5Eget_auto2(H5E_DEFAULT, &old_func_, &old_client_data_);
142 H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
147 H5Eset_auto2(H5E_DEFAULT, old_func_, old_client_data_);
192 typedef herr_t (*Destructor)(hid_t);
196 Destructor destructor_;
227 HDF5Handle(hid_t h, Destructor destructor,
const char * error_message)
229 destructor_(destructor)
232 vigra_fail(error_message);
240 : handle_( h.handle_ ),
241 destructor_(h.destructor_)
252 if(h.handle_ != handle_)
256 destructor_ = h.destructor_;
278 if(handle_ && destructor_)
279 res = (*destructor_)(handle_);
300 void reset(hid_t h, Destructor destructor,
const char * error_message)
303 vigra_fail(error_message);
308 destructor_ = destructor;
318 std::swap(handle_, h.handle_);
319 std::swap(destructor_, h.destructor_);
337 operator hid_t()
const 346 return handle_ == h.handle_;
360 return handle_ != h.handle_;
413 typedef herr_t (*Destructor)(hid_t);
417 Destructor destructor_;
452 destructor_(destructor),
456 vigra_fail(error_message);
458 refcount_ =
new size_t(1);
465 : handle_( h.handle_ ),
466 destructor_(h.destructor_),
467 refcount_(h.refcount_)
479 if(h.handle_ != handle_)
483 destructor_ = h.destructor_;
484 refcount_ = h.refcount_;
515 res = (*destructor_)(handle_);
529 void reset(hid_t h, Destructor destructor,
const char * error_message)
532 vigra_fail(error_message);
537 destructor_ = destructor;
539 refcount_ =
new size_t(1);
558 return use_count() == 1;
567 std::swap(handle_, h.handle_);
568 std::swap(destructor_, h.destructor_);
569 std::swap(refcount_, h.refcount_);
587 operator hid_t()
const 596 return handle_ == h.handle_;
610 return handle_ != h.handle_;
664 enum PixelType { UINT8, UINT16, UINT32, UINT64,
665 INT8, INT16, INT32, INT64,
678 VIGRA_EXPORT
HDF5ImportInfo(
const char* filePath,
const char* pathInFile );
680 VIGRA_EXPORT ~HDF5ImportInfo();
684 VIGRA_EXPORT
const std::string& getFilePath()
const;
688 VIGRA_EXPORT
const std::string& getPathInFile()
const;
692 VIGRA_EXPORT hid_t getH5FileHandle()
const;
696 VIGRA_EXPORT hid_t getDatasetHandle()
const;
741 VIGRA_EXPORT
const char * getPixelType()
const;
760 VIGRA_EXPORT PixelType pixelType()
const;
764 std::string m_filename, m_path, m_pixeltype;
765 hssize_t m_dimensions;
773 struct HDF5TypeTraits
775 static hid_t getH5DataType()
777 std::runtime_error(
"getH5DataType(): invalid type");
781 static int numberOfBands()
783 std::runtime_error(
"numberOfBands(): invalid type");
789 inline hid_t getH5DataType()
791 return HDF5TypeTraits<T>::getH5DataType();
794 #define VIGRA_H5_DATATYPE(type, h5type) \ 796 struct HDF5TypeTraits<type> \ 798 static hid_t getH5DataType() \ 802 static int numberOfBands() \ 806 typedef type value_type; \ 809 struct HDF5TypeTraits<TinyVector<type, M> > \ 811 static hid_t getH5DataType() \ 815 static int numberOfBands() \ 819 typedef type value_type; \ 822 struct HDF5TypeTraits<RGBValue<type> > \ 824 static hid_t getH5DataType() \ 828 static int numberOfBands() \ 832 typedef type value_type; \ 835 VIGRA_H5_DATATYPE(
char, H5T_NATIVE_CHAR)
836 VIGRA_H5_DATATYPE(
signed char, H5T_NATIVE_SCHAR)
837 VIGRA_H5_DATATYPE(
unsigned char, H5T_NATIVE_UCHAR)
838 VIGRA_H5_DATATYPE(
signed short, H5T_NATIVE_SHORT)
839 VIGRA_H5_DATATYPE(
unsigned short, H5T_NATIVE_USHORT)
840 VIGRA_H5_DATATYPE(
signed int, H5T_NATIVE_INT)
841 VIGRA_H5_DATATYPE(
unsigned int, H5T_NATIVE_UINT)
842 VIGRA_H5_DATATYPE(
signed long, H5T_NATIVE_LONG)
843 VIGRA_H5_DATATYPE(
unsigned long, H5T_NATIVE_ULONG)
844 VIGRA_H5_DATATYPE(
signed long long, H5T_NATIVE_LLONG)
845 VIGRA_H5_DATATYPE(
unsigned long long, H5T_NATIVE_ULLONG)
846 VIGRA_H5_DATATYPE(
float, H5T_NATIVE_FLOAT)
847 VIGRA_H5_DATATYPE(
double, H5T_NATIVE_DOUBLE)
848 VIGRA_H5_DATATYPE(
long double, H5T_NATIVE_LDOUBLE)
852 struct HDF5TypeTraits<char*>
854 static hid_t getH5DataType()
856 hid_t stringtype = H5Tcopy (H5T_C_S1);
857 H5Tset_size(stringtype, H5T_VARIABLE);
861 static int numberOfBands()
868 struct HDF5TypeTraits<const char*>
870 static hid_t getH5DataType()
872 hid_t stringtype = H5Tcopy (H5T_C_S1);
873 H5Tset_size(stringtype, H5T_VARIABLE);
877 static int numberOfBands()
883 #undef VIGRA_H5_DATATYPE 887 inline hid_t getH5DataType<FFTWComplex<float> >()
890 H5Tinsert (complex_id,
"real", 0, H5T_NATIVE_FLOAT);
891 H5Tinsert (complex_id,
"imaginary",
sizeof(
float), H5T_NATIVE_FLOAT);
896 inline hid_t getH5DataType<FFTWComplex<double> >()
899 H5Tinsert (complex_id,
"real", 0, H5T_NATIVE_DOUBLE);
900 H5Tinsert (complex_id,
"imaginary",
sizeof(
double), H5T_NATIVE_DOUBLE);
909 void HDF5_ls_insert(
void*,
const std::string &);
914 VIGRA_EXPORT H5O_type_t HDF5_get_type(hid_t,
const char*);
915 extern "C" VIGRA_EXPORT herr_t HDF5_ls_inserter_callback(hid_t,
const char*,
const H5L_info_t*,
void*);
916 extern "C" VIGRA_EXPORT herr_t HDF5_listAttributes_inserter_callback(hid_t,
const char*,
const H5A_info_t*,
void*);
973 virtual void insert(
const std::string &) = 0;
974 virtual ~ls_closure() {}
978 struct lsOpData :
public ls_closure
980 std::vector<std::string> & objects;
981 lsOpData(std::vector<std::string> & o) : objects(o) {}
982 void insert(
const std::string & x)
984 objects.push_back(x);
989 template<
class Container>
990 struct ls_container_data :
public ls_closure
993 ls_container_data(Container & o) : objects(o) {}
994 void insert(
const std::string & x)
996 objects.insert(std::string(x));
1003 friend void HDF5_ls_insert(
void*,
const std::string &);
1018 ReadOnly = OpenReadOnly,
1038 : track_time(track_creation_times ? 1 : 0),
1051 : track_time(track_creation_times ? 1 : 0)
1053 open(filePath, mode);
1074 const std::string & pathname =
"",
1075 bool read_only =
false)
1076 : fileHandle_(fileHandle),
1077 read_only_(read_only)
1084 cGroupHandle_ =
HDF5Handle(openCreateGroup_(pathname), &H5Gclose,
1085 "HDF5File(fileHandle, pathname): Failed to open group");
1088 hbool_t track_times_tmp;
1089 HDF5Handle plist_id(H5Fget_create_plist(fileHandle_), &H5Pclose,
1090 "HDF5File(fileHandle, pathname): Failed to open file creation property list");
1091 herr_t status = H5Pget_obj_track_times(plist_id, &track_times_tmp );
1092 vigra_postcondition(status >= 0,
1093 "HDF5File(fileHandle, pathname): cannot access track time attribute");
1094 track_time = track_times_tmp;
1102 : fileHandle_(other.fileHandle_),
1103 track_time(other.track_time),
1104 read_only_(other.read_only_)
1106 cGroupHandle_ =
HDF5Handle(openCreateGroup_(other.currentGroupName_()), &H5Gclose,
1107 "HDF5File(HDF5File const &): Failed to open group.");
1130 fileHandle_ = other.fileHandle_;
1131 cGroupHandle_ =
HDF5Handle(openCreateGroup_(other.currentGroupName_()), &H5Gclose,
1132 "HDF5File::operator=(): Failed to open group.");
1133 track_time = other.track_time;
1134 read_only_ = other.read_only_;
1139 int file_use_count()
const 1146 return fileHandle_ != 0;
1149 bool isReadOnly()
const 1154 void setReadOnly(
bool stat=
true)
1166 std::string errorMessage =
"HDF5File.open(): Could not open or create file '" + filePath +
"'.";
1167 fileHandle_ =
HDF5HandleShared(createFile_(filePath, mode), &H5Fclose, errorMessage.c_str());
1168 cGroupHandle_ =
HDF5Handle(openCreateGroup_(
"/"), &H5Gclose,
"HDF5File.open(): Failed to open root group.");
1169 setReadOnly(mode == OpenReadOnly);
1176 bool success = cGroupHandle_.
close() >= 0 && fileHandle_.
close() >= 0;
1177 vigra_postcondition(success,
"HDF5File.close() failed.");
1184 std::string message =
"HDF5File::root(): Could not open group '/'.";
1185 cGroupHandle_ =
HDF5Handle(H5Gopen(fileHandle_,
"/", H5P_DEFAULT),&H5Gclose,message.c_str());
1191 inline void cd(std::string groupName)
1193 cGroupHandle_ = getGroupHandle(groupName,
"HDF5File::cd()");
1202 std::string groupName = currentGroupName_();
1205 if(groupName ==
"/"){
1209 size_t lastSlash = groupName.find_last_of(
'/');
1211 std::string parentGroup (groupName.begin(), groupName.begin()+lastSlash+1);
1224 std::string groupName = currentGroupName_();
1226 for(
int i = 0; i<levels; i++)
1231 if(groupName != currentGroupName_())
1243 inline void mkdir(std::string groupName)
1245 vigra_precondition(!isReadOnly(),
1246 "HDF5File::mkdir(): file is read-only.");
1248 std::string message =
"HDF5File::mkdir(): Could not create group '" + groupName +
"'.\n";
1251 groupName = get_absolute_path(groupName);
1253 HDF5Handle(openCreateGroup_(groupName.c_str()),&H5Gclose,message.c_str());
1260 inline void cd_mk(std::string groupName)
1262 vigra_precondition(!isReadOnly(),
1263 "HDF5File::cd_mk(): file is read-only.");
1265 std::string message =
"HDF5File::cd_mk(): Could not create group '" + groupName +
"'.";
1268 groupName = get_absolute_path(groupName);
1270 cGroupHandle_ =
HDF5Handle(openCreateGroup_(groupName.c_str()),&H5Gclose,message.c_str());
1274 void ls_H5Literate(ls_closure & data)
const 1276 H5Literate(cGroupHandle_, H5_INDEX_NAME, H5_ITER_NATIVE, NULL,
1277 HDF5_ls_inserter_callback, static_cast<void*>(&data));
1285 inline std::vector<std::string>
ls()
const 1287 std::vector<std::string> list;
1288 lsOpData data(list);
1289 ls_H5Literate(data);
1306 template<
class Container>
1307 void ls(Container & cont)
const 1309 ls_container_data<Container> data(cont);
1310 ls_H5Literate(data);
1315 inline std::string
pwd()
const 1317 return currentGroupName_();
1332 datasetName = get_absolute_path(datasetName);
1333 return (H5Lexists(fileHandle_, datasetName.c_str(), H5P_DEFAULT) > 0);
1342 HDF5Handle datasetHandle = getDatasetHandle(datasetName);
1344 return getDatasetDimensions_(datasetHandle);
1347 hssize_t getDatasetDimensions_(hid_t dataset)
const 1349 std::string errorMessage =
"HDF5File::getDatasetDimensions(): Unable to access dataspace.";
1350 HDF5Handle dataspaceHandle(H5Dget_space(dataset), &H5Sclose, errorMessage.c_str());
1353 return H5Sget_simple_extent_ndims(dataspaceHandle);
1372 datasetName = get_absolute_path(datasetName);
1375 std::string errorMessage =
"HDF5File::getDatasetShape(): Unable to open dataset '" + datasetName +
"'.";
1376 HDF5Handle datasetHandle =
HDF5Handle(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
1378 errorMessage =
"HDF5File::getDatasetShape(): Unable to access dataspace.";
1379 HDF5Handle dataspaceHandle(H5Dget_space(datasetHandle), &H5Sclose, errorMessage.c_str());
1386 H5Sget_simple_extent_dims(dataspaceHandle, shape.
data(), maxdims.
data());
1389 std::reverse(shape.
begin(), shape.
end());
1412 HDF5Handle datasetHandle = getDatasetHandle(datasetName);
1414 hid_t datatype = H5Dget_type(datasetHandle);
1415 H5T_class_t dataclass = H5Tget_class(datatype);
1416 size_t datasize = H5Tget_size(datatype);
1417 H5T_sign_t datasign = H5Tget_sign(datatype);
1419 if(dataclass == H5T_FLOAT)
1423 else if(datasize == 8)
1426 else if(dataclass == H5T_INTEGER)
1428 if(datasign == H5T_SGN_NONE)
1432 else if(datasize == 2)
1434 else if(datasize == 4)
1436 else if(datasize == 8)
1443 else if(datasize == 2)
1445 else if(datasize == 4)
1447 else if(datasize == 8)
1458 std::string errorMessage =
"HDF5File::getDatasetHandle(): Unable to open dataset '" + datasetName +
"'.";
1459 return HDF5Handle(getDatasetHandle_(get_absolute_path(datasetName)), &H5Dclose, errorMessage.c_str());
1466 std::string errorMessage =
"HDF5File::getDatasetHandle(): Unable to open dataset '" + datasetName +
"'.";
1467 return HDF5HandleShared(getDatasetHandle_(get_absolute_path(datasetName)), &H5Dclose, errorMessage.c_str());
1473 std::string function_name =
"HDF5File::getGroupHandle()")
1475 std::string errorMessage = function_name +
": Group '" + group_name +
"' not found.";
1478 group_name = get_absolute_path(group_name);
1481 vigra_precondition(group_name ==
"/" || H5Lexists(fileHandle_, group_name.c_str(), H5P_DEFAULT) != 0,
1482 errorMessage.c_str());
1485 return HDF5Handle(openCreateGroup_(group_name), &H5Gclose,
"Internal error");
1489 void ls_H5Aiterate(std::string
const & group_or_dataset, ls_closure & data)
const 1491 H5O_type_t h5_type = get_object_type_(group_or_dataset);
1492 vigra_precondition(h5_type == H5O_TYPE_GROUP || h5_type == H5O_TYPE_DATASET,
1493 "HDF5File::listAttributes(): object \"" + group_or_dataset +
"\" is neither a group nor a dataset.");
1495 HDF5Handle object_handle(h5_type == H5O_TYPE_GROUP
1496 ? const_cast<HDF5File*>(
this)->openCreateGroup_(group_or_dataset)
1497 : getDatasetHandle_(group_or_dataset),
1498 h5_type == H5O_TYPE_GROUP
1501 "HDF5File::listAttributes(): unable to open object.");
1503 H5Aiterate2(object_handle, H5_INDEX_NAME, H5_ITER_NATIVE, &n,
1504 HDF5_listAttributes_inserter_callback, static_cast<void*>(&data));
1512 inline std::vector<std::string>
listAttributes(std::string
const & group_or_dataset)
const 1514 std::vector<std::string> list;
1515 lsOpData data(list);
1516 ls_H5Aiterate(group_or_dataset, data);
1526 template<
class Container>
1527 void listAttributes(std::string
const & group_or_dataset, Container & container)
const 1529 ls_container_data<Container> data(container);
1530 ls_H5Aiterate(group_or_dataset, data);
1537 std::string message =
"HDF5File::getAttributeHandle(): Attribute '" + attribute_name +
"' not found.";
1538 return HDF5Handle(H5Aopen(getDatasetHandle(dataset_name), attribute_name.c_str(), H5P_DEFAULT),
1539 &H5Aclose, message.c_str());
1547 template<
unsigned int N,
class T,
class Str
ide>
1549 std::string attribute_name,
1553 object_name = get_absolute_path(object_name);
1555 write_attribute_(object_name, attribute_name, array, detail::getH5DataType<T>(), 1);
1558 template<
unsigned int N,
class T,
int SIZE,
class Str
ide>
1559 inline void writeAttribute(std::string datasetName,
1560 std::string attributeName,
1564 datasetName = get_absolute_path(datasetName);
1566 write_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), SIZE);
1569 template<
unsigned int N,
class T,
class Str
ide>
1570 inline void writeAttribute(std::string datasetName,
1571 std::string attributeName,
1575 datasetName = get_absolute_path(datasetName);
1577 write_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), 3);
1583 inline void writeAttribute(std::string object_name, std::string attribute_name,
char data)
1584 { writeAtomicAttribute(object_name,attribute_name,data); }
1585 inline void writeAttribute(std::string datasetName, std::string attributeName,
signed char data)
1586 { writeAtomicAttribute(datasetName,attributeName,data); }
1587 inline void writeAttribute(std::string datasetName, std::string attributeName,
signed short data)
1588 { writeAtomicAttribute(datasetName,attributeName,data); }
1589 inline void writeAttribute(std::string datasetName, std::string attributeName,
signed int data)
1590 { writeAtomicAttribute(datasetName,attributeName,data); }
1591 inline void writeAttribute(std::string datasetName, std::string attributeName,
signed long data)
1592 { writeAtomicAttribute(datasetName,attributeName,data); }
1593 inline void writeAttribute(std::string datasetName, std::string attributeName,
signed long long data)
1594 { writeAtomicAttribute(datasetName,attributeName,data); }
1595 inline void writeAttribute(std::string datasetName, std::string attributeName,
unsigned char data)
1596 { writeAtomicAttribute(datasetName,attributeName,data); }
1597 inline void writeAttribute(std::string datasetName, std::string attributeName,
unsigned short data)
1598 { writeAtomicAttribute(datasetName,attributeName,data); }
1599 inline void writeAttribute(std::string datasetName, std::string attributeName,
unsigned int data)
1600 { writeAtomicAttribute(datasetName,attributeName,data); }
1601 inline void writeAttribute(std::string datasetName, std::string attributeName,
unsigned long data)
1602 { writeAtomicAttribute(datasetName,attributeName,data); }
1603 inline void writeAttribute(std::string datasetName, std::string attributeName,
unsigned long long data)
1604 { writeAtomicAttribute(datasetName,attributeName,data); }
1605 inline void writeAttribute(std::string datasetName, std::string attributeName,
float data)
1606 { writeAtomicAttribute(datasetName,attributeName,data); }
1607 inline void writeAttribute(std::string datasetName, std::string attributeName,
double data)
1608 { writeAtomicAttribute(datasetName,attributeName,data); }
1609 inline void writeAttribute(std::string datasetName, std::string attributeName,
long double data)
1610 { writeAtomicAttribute(datasetName,attributeName,data); }
1611 inline void writeAttribute(std::string datasetName, std::string attributeName,
const char* data)
1612 { writeAtomicAttribute(datasetName,attributeName,data); }
1613 inline void writeAttribute(std::string datasetName, std::string attributeName, std::string
const & data)
1614 { writeAtomicAttribute(datasetName,attributeName,data.c_str()); }
1620 std::string obj_path = get_absolute_path(object_name);
1621 htri_t exists = H5Aexists_by_name(fileHandle_, obj_path.c_str(),
1622 attribute_name.c_str(), H5P_DEFAULT);
1623 vigra_precondition(exists >= 0,
"HDF5File::existsAttribute(): " 1624 "object '" + object_name +
"' " 1634 template<
unsigned int N,
class T,
class Str
ide>
1636 std::string attribute_name,
1640 object_name = get_absolute_path(object_name);
1642 read_attribute_(object_name, attribute_name, array, detail::getH5DataType<T>(), 1);
1645 template<
unsigned int N,
class T,
int SIZE,
class Str
ide>
1646 inline void readAttribute(std::string datasetName,
1647 std::string attributeName,
1651 datasetName = get_absolute_path(datasetName);
1653 read_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), SIZE);
1656 template<
unsigned int N,
class T,
class Str
ide>
1657 inline void readAttribute(std::string datasetName,
1658 std::string attributeName,
1662 datasetName = get_absolute_path(datasetName);
1664 read_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), 3);
1670 inline void readAttribute(std::string object_name, std::string attribute_name,
char &data)
1671 { readAtomicAttribute(object_name,attribute_name,data); }
1672 inline void readAttribute(std::string datasetName, std::string attributeName,
signed char &data)
1673 { readAtomicAttribute(datasetName,attributeName,data); }
1674 inline void readAttribute(std::string datasetName, std::string attributeName,
signed short &data)
1675 { readAtomicAttribute(datasetName,attributeName,data); }
1676 inline void readAttribute(std::string datasetName, std::string attributeName,
signed int &data)
1677 { readAtomicAttribute(datasetName,attributeName,data); }
1678 inline void readAttribute(std::string datasetName, std::string attributeName,
signed long &data)
1679 { readAtomicAttribute(datasetName,attributeName,data); }
1680 inline void readAttribute(std::string datasetName, std::string attributeName,
signed long long &data)
1681 { readAtomicAttribute(datasetName,attributeName,data); }
1682 inline void readAttribute(std::string datasetName, std::string attributeName,
unsigned char &data)
1683 { readAtomicAttribute(datasetName,attributeName,data); }
1684 inline void readAttribute(std::string datasetName, std::string attributeName,
unsigned short &data)
1685 { readAtomicAttribute(datasetName,attributeName,data); }
1686 inline void readAttribute(std::string datasetName, std::string attributeName,
unsigned int &data)
1687 { readAtomicAttribute(datasetName,attributeName,data); }
1688 inline void readAttribute(std::string datasetName, std::string attributeName,
unsigned long &data)
1689 { readAtomicAttribute(datasetName,attributeName,data); }
1690 inline void readAttribute(std::string datasetName, std::string attributeName,
unsigned long long &data)
1691 { readAtomicAttribute(datasetName,attributeName,data); }
1692 inline void readAttribute(std::string datasetName, std::string attributeName,
float &data)
1693 { readAtomicAttribute(datasetName,attributeName,data); }
1694 inline void readAttribute(std::string datasetName, std::string attributeName,
double &data)
1695 { readAtomicAttribute(datasetName,attributeName,data); }
1696 inline void readAttribute(std::string datasetName, std::string attributeName,
long double &data)
1697 { readAtomicAttribute(datasetName,attributeName,data); }
1698 inline void readAttribute(std::string datasetName, std::string attributeName, std::string &data)
1699 { readAtomicAttribute(datasetName,attributeName,data); }
1727 template<
unsigned int N,
class T,
class Str
ide>
1728 inline void write(std::string datasetName,
1730 int iChunkSize = 0,
int compression = 0)
1733 datasetName = get_absolute_path(datasetName);
1736 for(
unsigned int i = 0; i < N; i++){
1737 chunkSize[i] = iChunkSize;
1739 write_(datasetName, array, detail::getH5DataType<T>(), 1, chunkSize, compression);
1759 template<
unsigned int N,
class T,
class Str
ide>
1760 inline void write(std::string datasetName,
1765 datasetName = get_absolute_path(datasetName);
1767 write_(datasetName, array, detail::getH5DataType<T>(), 1, chunkSize, compression);
1781 template<
unsigned int N,
class T,
class Str
ide>
1787 datasetName = get_absolute_path(datasetName);
1788 typedef detail::HDF5TypeTraits<T> TypeTraits;
1789 writeBlock_(datasetName, blockOffset, array,
1790 TypeTraits::getH5DataType(), TypeTraits::numberOfBands());
1793 template<
unsigned int N,
class T,
class Str
ide>
1798 typedef detail::HDF5TypeTraits<T> TypeTraits;
1799 return writeBlock_(dataset, blockOffset, array,
1800 TypeTraits::getH5DataType(), TypeTraits::numberOfBands());
1804 template<
unsigned int N,
class T,
int SIZE,
class Str
ide>
1805 inline void write(std::string datasetName,
1807 int iChunkSize = 0,
int compression = 0)
1810 datasetName = get_absolute_path(datasetName);
1813 for(
int i = 0; i < N; i++){
1814 chunkSize[i] = iChunkSize;
1816 write_(datasetName, array, detail::getH5DataType<T>(), SIZE, chunkSize, compression);
1819 template<
unsigned int N,
class T,
int SIZE,
class Str
ide>
1820 inline void write(std::string datasetName,
1825 datasetName = get_absolute_path(datasetName);
1827 write_(datasetName, array, detail::getH5DataType<T>(), SIZE, chunkSize, compression);
1841 void write(
const std::string & datasetName,
1843 int compression = 0)
1848 write(datasetName, m_array, compression);
1852 template<
unsigned int N,
class T,
class Str
ide>
1853 inline void write(std::string datasetName,
1855 int iChunkSize = 0,
int compression = 0)
1858 datasetName = get_absolute_path(datasetName);
1861 for(
int i = 0; i < N; i++){
1862 chunkSize[i] = iChunkSize;
1864 write_(datasetName, array, detail::getH5DataType<T>(), 3, chunkSize, compression);
1867 template<
unsigned int N,
class T,
class Str
ide>
1868 inline void write(std::string datasetName,
1873 datasetName = get_absolute_path(datasetName);
1875 write_(datasetName, array, detail::getH5DataType<T>(), 3, chunkSize, compression);
1881 inline void write(std::string datasetName,
char data) { writeAtomic(datasetName,data); }
1882 inline void write(std::string datasetName,
signed char data) { writeAtomic(datasetName,data); }
1883 inline void write(std::string datasetName,
signed short data) { writeAtomic(datasetName,data); }
1884 inline void write(std::string datasetName,
signed int data) { writeAtomic(datasetName,data); }
1885 inline void write(std::string datasetName,
signed long data) { writeAtomic(datasetName,data); }
1886 inline void write(std::string datasetName,
signed long long data) { writeAtomic(datasetName,data); }
1887 inline void write(std::string datasetName,
unsigned char data) { writeAtomic(datasetName,data); }
1888 inline void write(std::string datasetName,
unsigned short data) { writeAtomic(datasetName,data); }
1889 inline void write(std::string datasetName,
unsigned int data) { writeAtomic(datasetName,data); }
1890 inline void write(std::string datasetName,
unsigned long data) { writeAtomic(datasetName,data); }
1891 inline void write(std::string datasetName,
unsigned long long data) { writeAtomic(datasetName,data); }
1892 inline void write(std::string datasetName,
float data) { writeAtomic(datasetName,data); }
1893 inline void write(std::string datasetName,
double data) { writeAtomic(datasetName,data); }
1894 inline void write(std::string datasetName,
long double data) { writeAtomic(datasetName,data); }
1895 inline void write(std::string datasetName,
const char* data) { writeAtomic(datasetName,data); }
1896 inline void write(std::string datasetName, std::string
const & data) { writeAtomic(datasetName,data.c_str()); }
1909 template<
unsigned int N,
class T,
class Str
ide>
1913 datasetName = get_absolute_path(datasetName);
1915 read_(datasetName, array, detail::getH5DataType<T>(), 1);
1927 template<
unsigned int N,
class T,
class Alloc>
1931 datasetName = get_absolute_path(datasetName);
1938 "HDF5File::readAndResize(): Array dimension disagrees with dataset dimension.");
1942 for(
int k=0; k < static_cast<int>(dimshape.
size()); ++k)
1943 shape[k] = static_cast<MultiArrayIndex>(dimshape[k]);
1946 read_(datasetName, array, detail::getH5DataType<T>(), 1);
1959 read(datasetName, m_array);
1971 datasetName = get_absolute_path(datasetName);
1978 "HDF5File::readAndResize(): Array dimension disagrees with Dataset dimension must equal one for vigra::ArrayVector.");
1986 read_(datasetName, m_array, detail::getH5DataType<T>(), 1);
2004 template<
unsigned int N,
class T,
class Str
ide>
2011 datasetName = get_absolute_path(datasetName);
2012 typedef detail::HDF5TypeTraits<T> TypeTraits;
2013 readBlock_(datasetName, blockOffset, blockShape, array,
2014 TypeTraits::getH5DataType(), TypeTraits::numberOfBands());
2017 template<
unsigned int N,
class T,
class Str
ide>
2023 typedef detail::HDF5TypeTraits<T> TypeTraits;
2024 return readBlock_(dataset, blockOffset, blockShape, array,
2025 TypeTraits::getH5DataType(), TypeTraits::numberOfBands());
2029 template<
unsigned int N,
class T,
int SIZE,
class Str
ide>
2033 datasetName = get_absolute_path(datasetName);
2035 read_(datasetName, array, detail::getH5DataType<T>(), SIZE);
2039 template<
unsigned int N,
class T,
int SIZE,
class Alloc>
2043 datasetName = get_absolute_path(datasetName);
2050 SIZE == dimshape[0],
2051 "HDF5File::readAndResize(): Array dimension disagrees with dataset dimension.");
2055 for(
int k=1; k < static_cast<int>(dimshape.
size()); ++k)
2056 shape[k-1] = static_cast<MultiArrayIndex>(dimshape[k]);
2057 array.reshape(shape);
2059 read_(datasetName, array, detail::getH5DataType<T>(), SIZE);
2063 template<
unsigned int N,
class T,
class Str
ide>
2067 datasetName = get_absolute_path(datasetName);
2069 read_(datasetName, array, detail::getH5DataType<T>(), 3);
2073 template<
unsigned int N,
class T,
class Alloc>
2077 datasetName = get_absolute_path(datasetName);
2085 "HDF5File::readAndResize(): Array dimension disagrees with dataset dimension.");
2089 for(
int k=1; k < static_cast<int>(dimshape.
size()); ++k)
2090 shape[k-1] = static_cast<MultiArrayIndex>(dimshape[k]);
2091 array.reshape(shape);
2093 read_(datasetName, array, detail::getH5DataType<T>(), 3);
2099 inline void read(std::string datasetName,
char &data) { readAtomic(datasetName,data); }
2100 inline void read(std::string datasetName,
signed char &data) { readAtomic(datasetName,data); }
2101 inline void read(std::string datasetName,
signed short &data) { readAtomic(datasetName,data); }
2102 inline void read(std::string datasetName,
signed int &data) { readAtomic(datasetName,data); }
2103 inline void read(std::string datasetName,
signed long &data) { readAtomic(datasetName,data); }
2104 inline void read(std::string datasetName,
signed long long &data) { readAtomic(datasetName,data); }
2105 inline void read(std::string datasetName,
unsigned char &data) { readAtomic(datasetName,data); }
2106 inline void read(std::string datasetName,
unsigned short &data) { readAtomic(datasetName,data); }
2107 inline void read(std::string datasetName,
unsigned int &data) { readAtomic(datasetName,data); }
2108 inline void read(std::string datasetName,
unsigned long &data) { readAtomic(datasetName,data); }
2109 inline void read(std::string datasetName,
unsigned long long &data) { readAtomic(datasetName,data); }
2110 inline void read(std::string datasetName,
float &data) { readAtomic(datasetName,data); }
2111 inline void read(std::string datasetName,
double &data) { readAtomic(datasetName,data); }
2112 inline void read(std::string datasetName,
long double &data) { readAtomic(datasetName,data); }
2113 inline void read(std::string datasetName, std::string &data) { readAtomic(datasetName,data); }
2139 template<
int N,
class T>
2141 createDataset(std::string datasetName,
2143 typename detail::HDF5TypeTraits<T>::value_type init =
2144 typename detail::HDF5TypeTraits<T>::value_type(),
2150 int compressionParameter = 0);
2153 template<
int N,
class T>
2155 createDataset(std::string datasetName,
2159 int compressionParameter = 0)
2162 for(
int i = 0; i < N; i++){
2163 chunkSize[i] = iChunkSize;
2165 return this->
template createDataset<N, T>(datasetName, shape, init,
2166 chunkSize, compressionParameter);
2174 H5Fflush(fileHandle_, H5F_SCOPE_GLOBAL);
2187 class SplitString:
public std::string {
2189 SplitString(std::string &sstring): std::string(sstring) {};
2192 std::string first(
char delimiter =
'/')
2194 size_t lastPos = find_last_of(delimiter);
2195 if(lastPos == std::string::npos)
2198 return std::string(begin(), begin()+lastPos+1);
2202 std::string last(
char delimiter =
'/')
2204 size_t lastPos = find_last_of(delimiter);
2205 if(lastPos == std::string::npos)
2206 return std::string(*
this);
2207 return std::string(begin()+lastPos+1, end());
2211 template <
class Shape>
2213 defineChunks(Shape chunks, Shape
const & shape,
int numBands,
int compression = 0)
2215 if(
prod(chunks) > 0)
2219 res.insert(res.begin(),
static_cast<hsize_t
>(numBands));
2222 else if(compression > 0)
2225 chunks = min(detail::ChunkShape<Shape::static_size>::defaultShape(), shape);
2228 res.insert(res.begin(),
static_cast<hsize_t
>(numBands));
2247 if(path.length() == 0 || path ==
"."){
2248 return currentGroupName_();
2253 if(relativePath_(path)){
2254 std::string cname = currentGroupName_();
2256 str = currentGroupName_()+path;
2258 str = currentGroupName_()+
"/"+path;
2264 std::string::size_type startpos = 0;
2265 while(str.find(std::string(
"./"), startpos) != std::string::npos){
2266 std::string::size_type pos = str.find(std::string(
"./"), startpos);
2269 if(str.substr(pos-1,3) !=
"../"){
2271 str = str.substr(0,pos) + str.substr(pos+2,str.length()-pos-2);
2277 while(str.find(std::string(
"..")) != std::string::npos){
2278 std::string::size_type pos = str.find(std::string(
".."));
2281 std::string::size_type end = str.find(
"/",pos);
2282 if(end != std::string::npos){
2292 std::string::size_type prev_slash = str.rfind(
"/",pos);
2294 vigra_invariant(prev_slash != 0 && prev_slash != std::string::npos,
2295 "Error parsing path: "+str);
2297 std::string::size_type begin = str.rfind(
"/",prev_slash-1);
2300 str = str.substr(0,begin+1) + str.substr(end,str.length()-end);
2310 inline bool relativePath_(std::string & path)
const 2312 std::string::size_type pos = path.find(
'/') ;
2321 inline std::string currentGroupName_()
const 2323 int len = H5Iget_name(cGroupHandle_,NULL,1000);
2325 H5Iget_name(cGroupHandle_,name.
begin(),len+1);
2327 return std::string(name.
begin());
2332 inline std::string fileName_()
const 2334 int len = H5Fget_name(fileHandle_,NULL,1000);
2336 H5Fget_name(fileHandle_,name.
begin(),len+1);
2338 return std::string(name.
begin());
2343 inline hid_t createFile_(std::string filePath,
OpenMode mode = Open)
2347 pFile = fopen ( filePath.c_str(),
"r" );
2351 if ( pFile == NULL )
2353 vigra_precondition(mode != OpenReadOnly,
2354 "HDF5File::open(): cannot open non-existing file in read-only mode.");
2355 fileId = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
2360 if(mode == OpenReadOnly)
2362 fileId = H5Fopen(filePath.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
2364 else if(mode == New)
2366 std::remove(filePath.c_str());
2367 fileId = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
2371 fileId = H5Fopen(filePath.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
2382 hid_t openGroup_(std::string groupName)
const 2384 return const_cast<HDF5File *
>(
this)->openCreateGroup_(groupName,
false);
2395 hid_t openCreateGroup_(std::string groupName,
bool create =
true)
2398 groupName = get_absolute_path(groupName);
2401 hid_t parent = H5Gopen(fileHandle_,
"/", H5P_DEFAULT);
2402 if(groupName ==
"/")
2408 groupName = std::string(groupName.begin()+1, groupName.end());
2411 if( groupName.size() != 0 && *groupName.rbegin() !=
'/')
2413 groupName = groupName +
'/';
2417 std::string::size_type begin = 0, end = groupName.find(
'/');
2418 while (end != std::string::npos)
2420 std::string group(groupName.begin()+begin, groupName.begin()+end);
2421 hid_t prevParent = parent;
2423 if(H5LTfind_dataset(parent, group.c_str()) == 0)
2426 parent = H5Gcreate(prevParent, group.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
2432 parent = H5Gopen(prevParent, group.c_str(), H5P_DEFAULT);
2434 H5Gclose(prevParent);
2441 end = groupName.find(
'/', begin);
2450 inline void deleteDataset_(hid_t parent, std::string datasetName)
2453 if(H5LTfind_dataset(parent, datasetName.c_str()))
2456 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR <= 6) 2457 if(H5Gunlink(parent, datasetName.c_str()) < 0)
2459 vigra_postcondition(
false,
"HDF5File::deleteDataset_(): Unable to delete existing data.");
2462 if(H5Ldelete(parent, datasetName.c_str(), H5P_DEFAULT ) < 0)
2464 vigra_postcondition(
false,
"HDF5File::deleteDataset_(): Unable to delete existing data.");
2472 hid_t getDatasetHandle_(std::string datasetName)
const 2475 datasetName = get_absolute_path(datasetName);
2477 std::string groupname = SplitString(datasetName).first();
2478 std::string setname = SplitString(datasetName).last();
2480 if(H5Lexists(fileHandle_, datasetName.c_str(), H5P_DEFAULT) <= 0)
2482 std::cerr <<
"HDF5File::getDatasetHandle_(): Dataset '" << datasetName <<
"' does not exist.\n";
2487 HDF5Handle groupHandle(openGroup_(groupname), &H5Gclose,
"HDF5File::getDatasetHandle_(): Internal error");
2489 return H5Dopen(groupHandle, setname.c_str(), H5P_DEFAULT);
2494 H5O_type_t get_object_type_(std::string name)
const 2496 name = get_absolute_path(name);
2497 std::string group_name = SplitString(name).first();
2498 std::string object_name = SplitString(name).last();
2499 if (!object_name.size())
2500 return H5O_TYPE_GROUP;
2502 htri_t exists = H5Lexists(fileHandle_, name.c_str(), H5P_DEFAULT);
2503 vigra_precondition(exists > 0,
"HDF5File::get_object_type_(): " 2504 "object \"" + name +
"\" " 2507 HDF5Handle group_handle(openGroup_(group_name), &H5Gclose,
"Internal error");
2508 return HDF5_get_type(group_handle, name.c_str());
2513 template<
unsigned int N,
class T,
class Str
ide>
2514 void write_attribute_(std::string name,
2515 const std::string & attribute_name,
2517 const hid_t datatype,
2518 const int numBandsOfType);
2526 inline void writeAtomicAttribute(std::string datasetName, std::string attributeName,
const T data)
2529 datasetName = get_absolute_path(datasetName);
2535 write_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), 1);
2540 template<
unsigned int N,
class T,
class Str
ide>
2541 void read_attribute_(std::string datasetName,
2542 std::string attributeName,
2544 const hid_t datatype,
const int numBandsOfType);
2552 inline void readAtomicAttribute(std::string datasetName, std::string attributeName, T & data)
2555 datasetName = get_absolute_path(datasetName);
2558 read_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), 1);
2562 inline void readAtomicAttribute(std::string datasetName, std::string attributeName, std::string & data)
2565 datasetName = get_absolute_path(datasetName);
2568 read_attribute_(datasetName, attributeName, array, detail::getH5DataType<const char *>(), 1);
2569 data = std::string(array[0]);
2574 template<
unsigned int N,
class T,
class Str
ide>
2575 void write_(std::string &datasetName,
2577 const hid_t datatype,
2578 const int numBandsOfType,
2580 int compressionParameter = 0);
2591 inline void writeAtomic(std::string datasetName,
const T data)
2594 datasetName = get_absolute_path(datasetName);
2600 write_(datasetName, array, detail::getH5DataType<T>(), 1, chunkSize,0);
2605 template<
unsigned int N,
class T,
class Str
ide>
2606 void read_(std::string datasetName,
2608 const hid_t datatype,
const int numBandsOfType);
2619 inline void readAtomic(std::string datasetName, T & data)
2622 datasetName = get_absolute_path(datasetName);
2625 read_(datasetName, array, detail::getH5DataType<T>(), 1);
2629 inline void readAtomic(std::string datasetName, std::string & data)
2632 datasetName = get_absolute_path(datasetName);
2635 read_(datasetName, array, detail::getH5DataType<const char *>(), 1);
2636 data = std::string(array[0]);
2641 template<
unsigned int N,
class T,
class Str
ide>
2642 void writeBlock_(std::string datasetName,
2645 const hid_t datatype,
2646 const int numBandsOfType)
2649 std::string errorMessage =
"HDF5File::writeBlock(): Error opening dataset '" + datasetName +
"'.";
2650 HDF5HandleShared dataset(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
2651 herr_t status = writeBlock_(dataset, blockOffset, array, datatype, numBandsOfType);
2652 vigra_postcondition(status >= 0,
2653 "HDF5File::writeBlock(): write to dataset '" + datasetName +
"' via H5Dwrite() failed.");
2660 template<
unsigned int N,
class T,
class Str
ide>
2664 const hid_t datatype,
2665 const int numBandsOfType);
2671 template<
unsigned int N,
class T,
class Str
ide>
2672 void readBlock_(std::string datasetName,
2676 const hid_t datatype,
const int numBandsOfType)
2678 std::string errorMessage (
"HDF5File::readBlock(): Unable to open dataset '" + datasetName +
"'.");
2679 HDF5HandleShared dataset(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
2680 herr_t status = readBlock_(dataset, blockOffset, blockShape, array, datatype, numBandsOfType);
2681 vigra_postcondition(status >= 0,
2682 "HDF5File::readBlock(): read from dataset '" + datasetName +
"' via H5Dread() failed.");
2690 template<
unsigned int N,
class T,
class Str
ide>
2695 const hid_t datatype,
const int numBandsOfType);
2700 template<
int N,
class T>
2704 typename detail::HDF5TypeTraits<T>::value_type init,
2706 int compressionParameter)
2708 vigra_precondition(!isReadOnly(),
2709 "HDF5File::createDataset(): file is read-only.");
2712 datasetName = get_absolute_path(datasetName);
2714 std::string groupname = SplitString(datasetName).first();
2715 std::string setname = SplitString(datasetName).last();
2717 hid_t parent = openCreateGroup_(groupname);
2720 deleteDataset_(parent, setname);
2724 typedef detail::HDF5TypeTraits<T> TypeTraits;
2726 if(TypeTraits::numberOfBands() > 1)
2728 shape_inv.resize(N+1);
2729 shape_inv[N] = TypeTraits::numberOfBands();
2733 shape_inv.resize(N);
2735 for(
int k=0; k<N; ++k)
2736 shape_inv[N-1-k] = shape[k];
2740 dataspaceHandle =
HDF5Handle(H5Screate_simple(shape_inv.
size(), shape_inv.
data(), NULL),
2741 &H5Sclose,
"HDF5File::createDataset(): unable to create dataspace for scalar data.");
2744 HDF5Handle plist ( H5Pcreate(H5P_DATASET_CREATE), &H5Pclose,
"HDF5File::createDataset(): unable to create property list." );
2745 H5Pset_fill_value(plist, TypeTraits::getH5DataType(), &init);
2748 H5Pset_obj_track_times(plist, track_time);
2751 ArrayVector<hsize_t> chunks(defineChunks(chunkSize, shape, TypeTraits::numberOfBands(), compressionParameter));
2752 if(chunks.
size() > 0)
2754 std::reverse(chunks.
begin(), chunks.
end());
2755 H5Pset_chunk (plist, chunks.
size(), chunks.
begin());
2759 if(compressionParameter > 0)
2761 H5Pset_deflate(plist, compressionParameter);
2766 TypeTraits::getH5DataType(),
2767 dataspaceHandle, H5P_DEFAULT, plist, H5P_DEFAULT),
2769 "HDF5File::createDataset(): unable to create dataset.");
2770 if(parent != cGroupHandle_)
2773 return datasetHandle;
2778 template<
unsigned int N,
class T,
class Str
ide>
2779 void HDF5File::write_(std::string &datasetName,
2781 const hid_t datatype,
2782 const int numBandsOfType,
2784 int compressionParameter)
2786 vigra_precondition(!isReadOnly(),
2787 "HDF5File::write(): file is read-only.");
2789 std::string groupname = SplitString(datasetName).first();
2790 std::string setname = SplitString(datasetName).last();
2794 std::reverse(shape.begin(), shape.end());
2796 if(numBandsOfType > 1)
2797 shape.push_back(numBandsOfType);
2799 HDF5Handle dataspace(H5Screate_simple(shape.size(), shape.begin(), NULL), &H5Sclose,
2800 "HDF5File::write(): Can not create dataspace.");
2803 std::string errorMessage (
"HDF5File::write(): can not create group '" + groupname +
"'.");
2804 HDF5Handle groupHandle(openCreateGroup_(groupname), &H5Gclose, errorMessage.c_str());
2807 deleteDataset_(groupHandle, setname.c_str());
2810 HDF5Handle plist(H5Pcreate(H5P_DATASET_CREATE), &H5Pclose,
2811 "HDF5File::write(): unable to create property list." );
2814 H5Pset_obj_track_times(plist, track_time);
2818 if(chunks.
size() > 0)
2820 std::reverse(chunks.
begin(), chunks.
end());
2821 H5Pset_chunk (plist, chunks.
size(), chunks.
begin());
2825 if(compressionParameter > 0)
2827 H5Pset_deflate(plist, compressionParameter);
2831 HDF5Handle datasetHandle(H5Dcreate(groupHandle, setname.c_str(), datatype, dataspace,H5P_DEFAULT, plist, H5P_DEFAULT),
2832 &H5Dclose,
"HDF5File::write(): Can not create dataset.");
2838 status = H5Dwrite(datasetHandle, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, array.
data());
2848 int offset = numBandsOfType > 1 ? 1 : 0;
2849 std::reverse(shape.begin(), shape.end());
2850 if(chunks.
size() > 0)
2853 std::reverse(chunks.
begin(), chunks.
end());
2859 chunks[0] = numBandsOfType;
2861 for(
unsigned int k=0; k<N; ++k)
2863 chunks[k+offset] = array.
shape(k);
2864 prod *= array.
shape(k);
2871 start(shape.size(), 0),
2872 count(shape.size(), 1);
2874 count[N-1-offset] = numBandsOfType;
2877 Shape chunkCount, chunkMaxShape;
2878 for(
unsigned int k=offset; k<chunks.
size(); ++k)
2880 chunkMaxShape[k-offset] = chunks[k];
2886 for(; chunkIter != chunkEnd; ++chunkIter)
2888 Shape chunkStart(chunkIter.point() * chunkMaxShape),
2889 chunkStop(min(chunkStart + chunkMaxShape, array.
shape()));
2892 for(
unsigned int k=0; k<N; ++k)
2894 start[N-1-k] = chunkStart[k];
2895 count[N-1-k] = buffer.shape(k);
2900 count[N] = numBandsOfType;
2902 HDF5Handle filespace(H5Dget_space(datasetHandle),
2903 &H5Sclose,
"HDF5File::write(): unable to create hyperslabs.");
2904 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, start.data(), NULL, count.
data(), NULL);
2909 &H5Sclose,
"HDF5File::write(): unable to create hyperslabs.");
2910 status = H5Sselect_hyperslab(dataspace2, H5S_SELECT_SET, null.data(), NULL, count.
data(), NULL);
2914 status = H5Dwrite(datasetHandle, datatype, dataspace2, filespace, H5P_DEFAULT, buffer.data());
2919 vigra_postcondition(status >= 0,
2920 "HDF5File::write(): write to dataset '" + datasetName +
"' via H5Dwrite() failed.");
2925 template<
unsigned int N,
class T,
class Str
ide>
2929 const hid_t datatype,
2930 const int numBandsOfType)
2932 vigra_precondition(!isReadOnly(),
2933 "HDF5File::writeBlock(): file is read-only.");
2936 hssize_t dimensions = getDatasetDimensions_(datasetHandle);
2937 if(numBandsOfType > 1)
2939 vigra_precondition(N+1 == dimensions,
2940 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
2942 boffset.resize(N+1);
2943 bshape[N] = numBandsOfType;
2948 vigra_precondition(N == dimensions,
2949 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
2954 for(
int i = 0; i < N; ++i)
2957 bshape[N-1-i] = array.
shape(i);
2958 boffset[N-1-i] = blockOffset[i];
2964 "Unable to get origin dataspace");
2967 HDF5Handle dataspaceHandle (H5Dget_space(datasetHandle),&H5Sclose,
"Unable to create target dataspace");
2968 H5Sselect_hyperslab(dataspaceHandle, H5S_SELECT_SET,
2975 status = H5Dwrite( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, array.
data());
2981 status = H5Dwrite( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, buffer.data());
2988 template<
unsigned int N,
class T,
class Str
ide>
2989 void HDF5File::write_attribute_(std::string name,
2990 const std::string & attribute_name,
2992 const hid_t datatype,
2993 const int numBandsOfType)
2995 vigra_precondition(!isReadOnly(),
2996 "HDF5File::writeAttribute(): file is read-only.");
3000 std::reverse(shape.begin(), shape.end());
3001 if(numBandsOfType > 1)
3002 shape.push_back(numBandsOfType);
3004 HDF5Handle dataspace(H5Screate_simple(shape.size(),
3005 shape.begin(), NULL),
3006 &H5Sclose,
"HDF5File::writeAttribute(): Can not" 3007 " create dataspace.");
3009 std::string errorMessage (
"HDF5File::writeAttribute(): can not find " 3010 "object '" + name +
"'.");
3012 H5O_type_t h5_type = get_object_type_(name);
3013 bool is_group = h5_type == H5O_TYPE_GROUP;
3014 if (!is_group && h5_type != H5O_TYPE_DATASET)
3015 vigra_precondition(0,
"HDF5File::writeAttribute(): object \"" 3016 + name +
"\" is neither a group nor a " 3020 ? openCreateGroup_(name)
3021 : getDatasetHandle_(name),
3025 errorMessage.c_str());
3027 bool exists = existsAttribute(name, attribute_name);
3029 ? H5Aopen(object_handle,
3030 attribute_name.c_str(),
3032 : H5Acreate(object_handle,
3033 attribute_name.c_str(), datatype,
3034 dataspace, H5P_DEFAULT,
3037 "HDF5File::writeAttribute(): Can not create" 3043 status = H5Awrite(attributeHandle, datatype, array.
data());
3050 status = H5Awrite(attributeHandle, datatype, buffer.data());
3052 vigra_postcondition(status >= 0,
3053 "HDF5File::writeAttribute(): write to attribute '" + attribute_name +
"' via H5Awrite() failed.");
3058 template<
unsigned int N,
class T,
class Str
ide>
3059 void HDF5File::read_(std::string datasetName,
3061 const hid_t datatype,
const int numBandsOfType)
3066 std::string errorMessage (
"HDF5File::read(): Unable to open dataset '" + datasetName +
"'.");
3067 HDF5Handle datasetHandle(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
3071 int offset = (numBandsOfType > 1)
3076 "HDF5File::read(): Array dimension disagrees with dataset dimension.");
3079 for(
int k=offset; k < (int)dimshape.
size(); ++k)
3082 vigra_precondition(shape == array.
shape(),
3083 "HDF5File::read(): Array shape disagrees with dataset shape.");
3085 vigra_precondition(dimshape[0] == static_cast<hsize_t>(numBandsOfType),
3086 "HDF5File::read(): Band count doesn't match destination array compound type.");
3092 status = H5Dread(datasetHandle, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, array.
data() );
3099 chunks(dimshape.
size(), 1),
3100 start(dimshape.
size(), 0),
3101 count(dimshape.
size(), 1);
3103 HDF5Handle properties(H5Dget_create_plist(datasetHandle),
3104 &H5Pclose,
"HDF5File::read(): failed to get property list");
3105 if(H5D_CHUNKED == H5Pget_layout(properties))
3108 H5Pget_chunk(properties, static_cast<int>(chunks.size()), chunks.
data());
3109 std::reverse(chunks.begin(), chunks.end());
3114 chunks[0] = numBandsOfType;
3116 for(
unsigned int k=0; k<N; ++k)
3118 chunks[k+offset] = array.
shape(k);
3119 prod *= array.
shape(k);
3125 count[N-1-offset] =
static_cast<hsize_t
>(numBandsOfType);
3128 Shape chunkCount, chunkMaxShape;
3129 for(
unsigned int k=offset; k<chunks.size(); ++k)
3131 chunkMaxShape[k-offset] = chunks[k];
3137 for(; chunkIter != chunkEnd; ++chunkIter)
3139 Shape chunkStart(chunkIter.point() * chunkMaxShape),
3140 chunkStop(min(chunkStart + chunkMaxShape, array.
shape()));
3143 for(
unsigned int k=0; k<N; ++k)
3145 start[N-1-k] = chunkStart[k];
3146 count[N-1-k] = buffer.shape(k);
3151 count[N] = numBandsOfType;
3153 HDF5Handle filespace(H5Dget_space(datasetHandle),
3154 &H5Sclose,
"HDF5File::read(): unable to create hyperslabs.");
3155 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, start.data(), NULL, count.data(), NULL);
3159 HDF5Handle dataspace(H5Screate_simple(count.size(), count.data(), NULL),
3160 &H5Sclose,
"HDF5File::read(): unable to create hyperslabs.");
3161 status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, null.data(), NULL, count.data(), NULL);
3165 status = H5Dread(datasetHandle, datatype, dataspace, filespace, H5P_DEFAULT, buffer.data());
3169 array.
subarray(chunkStart, chunkStop) = buffer;
3172 vigra_postcondition(status >= 0,
3173 "HDF5File::read(): read from dataset '" + datasetName +
"' via H5Dread() failed.");
3178 template<
unsigned int N,
class T,
class Str
ide>
3183 const hid_t datatype,
const int numBandsOfType)
3185 vigra_precondition(blockShape == array.
shape(),
3186 "HDF5File::readBlock(): Array shape disagrees with block size.");
3189 hssize_t dimensions = getDatasetDimensions_(datasetHandle);
3190 if(numBandsOfType > 1)
3192 vigra_precondition(N+1 == dimensions,
3193 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
3195 boffset.resize(N+1);
3196 bshape[N] = numBandsOfType;
3201 vigra_precondition(N == dimensions,
3202 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
3207 for(
int i = 0; i < N; ++i)
3210 bshape[N-1-i] = blockShape[i];
3211 boffset[N-1-i] = blockOffset[i];
3215 HDF5Handle memspace_handle(H5Screate_simple(bshape.size(), bshape.data(), NULL),
3217 "Unable to create target dataspace");
3220 HDF5Handle dataspaceHandle(H5Dget_space(datasetHandle), &H5Sclose,
3221 "Unable to get dataspace");
3222 H5Sselect_hyperslab(dataspaceHandle, H5S_SELECT_SET,
3223 boffset.data(), bones.data(), bones.data(), bshape.data());
3229 status = H5Dread( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, array.
data());
3235 status = H5Dread( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, buffer.data());
3245 template<
unsigned int N,
class T,
class Str
ide>
3246 void HDF5File::read_attribute_(std::string datasetName,
3247 std::string attributeName,
3249 const hid_t datatype,
const int numBandsOfType)
3251 std::string dataset_path = get_absolute_path(datasetName);
3253 std::string message =
"HDF5File::readAttribute(): could not get handle for attribute '"+attributeName+
"'' of object '"+dataset_path+
"'.";
3254 HDF5Handle attr_handle (H5Aopen_by_name(fileHandle_,dataset_path.c_str(),attributeName.c_str(),H5P_DEFAULT,H5P_DEFAULT),&H5Aclose, message.c_str());
3257 message =
"HDF5File::readAttribute(): could not get dataspace for attribute '"+attributeName+
"'' of object '"+dataset_path+
"'.";
3258 HDF5Handle attr_dataspace_handle (H5Aget_space(attr_handle),&H5Sclose,message.c_str());
3261 int raw_dims = H5Sget_simple_extent_ndims(attr_dataspace_handle);
3262 int dims = std::max(raw_dims, 1);
3265 H5Sget_simple_extent_dims(attr_dataspace_handle, dimshape.
data(), NULL);
3270 std::reverse(dimshape.begin(), dimshape.end());
3272 int offset = (numBandsOfType > 1)
3275 message =
"HDF5File::readAttribute(): Array dimension disagrees with dataset dimension.";
3279 for(
int k=offset; k < (int)dimshape.size(); ++k)
3281 "HDF5File::readAttribute(): Array shape disagrees with dataset shape");
3287 status = H5Aread( attr_handle, datatype, array.
data());
3294 status = H5Aread( attr_handle, datatype, buffer.data() );
3299 vigra_postcondition(status >= 0,
3300 "HDF5File::readAttribute(): read from attribute '" + attributeName +
"' via H5Aread() failed.");
3342 template<
unsigned int N,
class T,
class Str
ideTag>
3348 template<
unsigned int N,
class T,
class Str
ideTag>
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>
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);
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.");
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>
3560 writeHDF5(filePath, pathInFile, array, 0, 0);
3563 template<
unsigned int N,
class T,
class Str
ideTag>
3566 HDF5File file(filePath, HDF5File::Open);
3567 file.
write(pathInFile, array);
3582 void operator()(std::string
const & in)
3584 size = in.size() > size ?
3592 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8) || DOXYGEN 3599 template<
size_t N,
class T,
class C>
3604 if(H5Aexists(loc, name) > 0)
3605 H5Adelete(loc, name);
3608 array.
shape().end());
3610 dataspace_handle(H5Screate_simple(N, shape.data(), NULL),
3612 "writeToHDF5File(): unable to create dataspace.");
3616 detail::getH5DataType<T>(),
3618 H5P_DEFAULT ,H5P_DEFAULT ),
3620 "writeHDF5Attr: unable to create Attribute");
3624 for(
int ii = 0; ii < array.
size(); ++ii)
3625 buffer.push_back(array[ii]);
3626 H5Awrite(attr, detail::getH5DataType<T>(), buffer.
data());
3635 template<
size_t N,
class C>
3640 if(H5Aexists(loc, name) > 0)
3641 H5Adelete(loc, name);
3644 array.
shape().end());
3646 dataspace_handle(H5Screate_simple(N, shape.data(), NULL),
3648 "writeToHDF5File(): unable to create dataspace.");
3652 "writeToHDF5File(): unable to create type.");
3654 detail::MaxSizeFnc max_size;
3655 max_size = std::for_each(array.
data(),array.
data()+ array.
size(), max_size);
3656 H5Tset_size (atype, max_size.size);
3662 H5P_DEFAULT ,H5P_DEFAULT ),
3664 "writeHDF5Attr: unable to create Attribute");
3666 std::string buf =
"";
3667 for(
int ii = 0; ii < array.
size(); ++ii)
3669 buf = buf + array[ii]
3670 + std::string(max_size.size - array[ii].
size(),
' ');
3672 H5Awrite(attr, atype, buf.c_str());
3700 std::string pathInFile,
3703 std::string path_name(pathInFile), group_name, data_set_name, message, attr_name;
3704 std::string::size_type delimiter = path_name.rfind(
'/');
3707 HDF5Handle file_id(createFile(filePath), &H5Fclose,
3708 "writeToHDF5File(): unable to open output file.");
3711 if(delimiter == std::string::npos)
3714 data_set_name = path_name;
3719 group_name = std::string(path_name.begin(), path_name.begin()+delimiter);
3720 data_set_name = std::string(path_name.begin()+delimiter+1, path_name.end());
3722 delimiter = data_set_name.rfind(
'.');
3723 if(delimiter == std::string::npos)
3725 attr_name = path_name;
3726 data_set_name =
"/";
3730 attr_name = std::string(data_set_name.begin()+delimiter+1, data_set_name.end());
3731 data_set_name = std::string(data_set_name.begin(), data_set_name.begin()+delimiter);
3734 HDF5Handle group(openGroup(file_id, group_name), &H5Gclose,
3735 "writeToHDF5File(): Unable to create and open group. attr ver");
3737 if(data_set_name !=
"/")
3739 HDF5Handle dset(H5Dopen(group, data_set_name.c_str(), H5P_DEFAULT), &H5Dclose,
3740 "writeHDF5Attr():unable to open dataset");
3755 #endif // VIGRA_HDF5IMPEX_HXX HDF5File & operator=(HDF5File const &other)
Assign a HDF5File object.
Definition: hdf5impex.hxx:1125
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:1954
HDF5Handle()
Default constructor. Creates a NULL handle.
Definition: hdf5impex.hxx:203
void write(const std::string &datasetName, const ArrayVectorView< T > &array, int compression=0)
Write array vectors.
Definition: hdf5impex.hxx:1841
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:1222
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:1512
bool operator==(HDF5Handle const &h) const
Equality comparison of the contained handle.
Definition: hdf5impex.hxx:344
Temporarily disable HDF5's native error output.
Definition: hdf5impex.hxx:128
ArrayVector< hsize_t > getDatasetShape(std::string datasetName) const
Get the shape of each dimension of a certain dataset.
Definition: hdf5impex.hxx:1369
Wrapper for unique hid_t objects.
Definition: hdf5impex.hxx:189
HDF5File(std::string filePath, OpenMode mode, bool track_creation_times=false)
Open or create an HDF5File object.
Definition: hdf5impex.hxx:1050
bool operator!=(hid_t h) const
Inequality comparison of the contained handle.
Definition: hdf5impex.hxx:615
HDF5HandleShared()
Default constructor. Creates a NULL handle.
Definition: hdf5impex.hxx:425
const difference_type & shape() const
Definition: multi_array.hxx:1596
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:1635
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:1260
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:1340
Definition: array_vector.hxx:76
const_iterator begin() const
Definition: array_vector.hxx:223
pointer data() const
Definition: multi_array.hxx:1846
Definition: array_vector.hxx:954
HDF5Handle(hid_t h, Destructor destructor, const char *error_message)
Create a wrapper for a hid_t object.
Definition: hdf5impex.hxx:227
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:2245
void swap(HDF5HandleShared &h)
Swap the contents of two handle wrappers.
Definition: hdf5impex.hxx:565
void reshape(const difference_type &shape)
Definition: multi_array.hxx:2809
HDF5HandleShared getDatasetHandleShared(std::string const &datasetName) const
Obtain a shared HDF5 handle of a dataset.
Definition: hdf5impex.hxx:1464
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:1243
bool existsAttribute(std::string object_name, std::string attribute_name)
Test if attribute exists.
Definition: hdf5impex.hxx:1618
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:3600
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:2702
bool operator==(hid_t h) const
Equality comparison of the contained handle.
Definition: hdf5impex.hxx:351
Definition: accessor.hxx:43
hid_t release()
Return the contained handle and set the wrapper to NULL without calling close().
Definition: hdf5impex.hxx:288
const std::string & getPathInFile() const
herr_t close()
Close the handle if this is the unique (i.e. last) owner.
Definition: hdf5impex.hxx:506
void ls(Container &cont) const
List the contents of the current group into a container-like object via insert(). ...
Definition: hdf5impex.hxx:1307
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:1760
Argument object for the function readHDF5().
Definition: hdf5impex.hxx:661
bool isHDF5(char const *filename)
Check if given filename refers to a HDF5 file.
Definition: hdf5impex.hxx:100
void reset(hid_t h, Destructor destructor, const char *error_message)
Reset the handle to a new value.
Definition: hdf5impex.hxx:529
doxygen_overloaded_function(template<... > void separableConvolveBlockwise) template< unsigned int N
Separated convolution on ChunkedArrays.
HDF5File(HDF5File const &other)
Copy a HDF5File object.
Definition: hdf5impex.hxx:1101
HDF5HandleShared & operator=(HDF5HandleShared const &h)
Assignment. Call close() for the present LHS handle and share ownership with the RHS handle (analogou...
Definition: hdf5impex.hxx:477
HDF5HandleShared(HDF5HandleShared const &h)
Copy constructor. Shares ownership with the RHS handle (analogous to std::shared_ptr).
Definition: hdf5impex.hxx:464
bool operator!=(hid_t h) const
Inequality comparison of the contained handle.
Definition: hdf5impex.hxx:365
bool operator!=(HDF5Handle const &h) const
Inequality comparison of the contained handle.
Definition: hdf5impex.hxx:358
difference_type_1 size() const
Definition: multi_array.hxx:1589
Wrapper for shared hid_t objects.
Definition: hdf5impex.hxx:410
std::string filename() const
Get the name of the associated file.
Definition: hdf5impex.hxx:1322
HDF5File()
Default constructor.
Definition: hdf5impex.hxx:1029
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:250
~HDF5File()
The destructor flushes and closes the file.
Definition: hdf5impex.hxx:1112
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:594
CoupledScanOrderIterator getEndIterator() const
Definition: multi_iterator_coupled.hxx:282
HDF5File(HDF5HandleShared const &fileHandle, const std::string &pathname="", bool read_only=false)
Initialize an HDF5File object from HDF5 file handle.
Definition: hdf5impex.hxx:1073
HDF5Handle getAttributeHandle(std::string dataset_name, std::string attribute_name) const
Obtain the HDF5 handle of a attribute.
Definition: hdf5impex.hxx:1535
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:1928
bool operator==(hid_t h) const
Equality comparison of the contained handle.
Definition: hdf5impex.hxx:601
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:1967
~HDF5Handle()
Destructor. Calls close() for the contained handle.
Definition: hdf5impex.hxx:265
bool operator!=(HDF5HandleShared const &h) const
Inequality comparison of the contained handle.
Definition: hdf5impex.hxx:608
void read(std::string datasetName, char &data)
Read a single value. Specialization of the read function for simple datatypes.
Definition: hdf5impex.hxx:2099
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:1162
OpenMode
Set how a file is opened.
Definition: hdf5impex.hxx:1013
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:1670
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:1782
void flushToDisk()
Immediately write all data to disk.
Definition: hdf5impex.hxx:2171
HDF5Handle(HDF5Handle const &h)
Copy constructor.
Definition: hdf5impex.hxx:239
void reset(hid_t h, Destructor destructor, const char *error_message)
Reset the wrapper to a new handle.
Definition: hdf5impex.hxx:300
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.
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:1472
std::string pwd() const
Get the path of the current group.
Definition: hdf5impex.hxx:1315
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:1910
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:1527
bool unique() const
Check if this is the unique owner of the conatined handle.
Definition: hdf5impex.hxx:556
void write(std::string datasetName, char data)
Write a single value. Specialization of the write function for simple datatypes.
Definition: hdf5impex.hxx:1881
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:1583
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:1548
herr_t close()
Explicitly call the stored destructor (if one has been registered in the constructor) for the contain...
Definition: hdf5impex.hxx:275
void write(std::string datasetName, const MultiArrayView< N, T, Stride > &array, int iChunkSize=0, int compression=0)
Write multi arrays.
Definition: hdf5impex.hxx:1728
image import and export functions
bool isUnstrided(unsigned int dimension=N-1) const
Definition: multi_array.hxx:1234
void close()
Close the current file.
Definition: hdf5impex.hxx:1174
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:1200
Base class for, and view to, vigra::MultiArray.
Definition: multi_array.hxx:652
std::string getDatasetType(std::string const &datasetName) const
Definition: hdf5impex.hxx:1410
bool existsDataset(std::string datasetName) const
Check if given datasetName exists.
Definition: hdf5impex.hxx:1329
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:710
size_type size() const
Definition: array_vector.hxx:358
Iterate over multiple images simultaneously in scan order.
Definition: multi_fwd.hxx:167
MultiArrayView subarray(difference_type p, difference_type q) const
Definition: multi_array.hxx:1476
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:2005
Class for a single RGB value.
Definition: accessor.hxx:938
int ceil(FixedPoint< IntBits, FracBits > v)
rounding up.
Definition: fixedpoint.hxx:675
HDF5HandleShared(hid_t h, Destructor destructor, const char *error_message)
Create a shared wrapper for a plain hid_t object.
Definition: hdf5impex.hxx:450
~HDF5HandleShared()
Destructor (calls close())
Definition: hdf5impex.hxx:493
HDF5Handle getDatasetHandle(std::string const &datasetName) const
Obtain the HDF5 handle of a dataset.
Definition: hdf5impex.hxx:1456
Wrapper class for the FFTW complex types 'fftw_complex'.
Definition: fftw3.hxx:131
void root()
Change current group to "/".
Definition: hdf5impex.hxx:1182
HDF5File(bool track_creation_times)
Construct with time tagging of datasets enabled.
Definition: hdf5impex.hxx:1037
void cd(std::string groupName)
Change the current group. Both absolute and relative group names are allowed.
Definition: hdf5impex.hxx:1191
size_t use_count() const
Get the number of owners of the contained handle.
Definition: hdf5impex.hxx:545
void swap(HDF5Handle &h)
Swap the contents of two handle wrappers.
Definition: hdf5impex.hxx:316
Access to HDF5 files.
Definition: hdf5impex.hxx:956
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:1285