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);
2173 H5Fflush(fileHandle_, H5F_SCOPE_GLOBAL);
2186 class SplitString:
public std::string {
2188 SplitString(std::string &sstring): std::string(sstring) {};
2191 std::string first(
char delimiter =
'/')
2193 size_t lastPos = find_last_of(delimiter);
2194 if(lastPos == std::string::npos)
2197 return std::string(begin(), begin()+lastPos+1);
2201 std::string last(
char delimiter =
'/')
2203 size_t lastPos = find_last_of(delimiter);
2204 if(lastPos == std::string::npos)
2205 return std::string(*
this);
2206 return std::string(begin()+lastPos+1, end());
2210 template <
class Shape>
2212 defineChunks(Shape chunks, Shape
const & shape,
int numBands,
int compression = 0)
2214 if(
prod(chunks) > 0)
2218 res.insert(res.begin(),
static_cast<hsize_t
>(numBands));
2221 else if(compression > 0)
2224 chunks = min(detail::ChunkShape<Shape::static_size>::defaultShape(), shape);
2227 res.insert(res.begin(),
static_cast<hsize_t
>(numBands));
2246 if(path.length() == 0 || path ==
"."){
2247 return currentGroupName_();
2252 if(relativePath_(path)){
2253 std::string cname = currentGroupName_();
2255 str = currentGroupName_()+path;
2257 str = currentGroupName_()+
"/"+path;
2263 std::string::size_type startpos = 0;
2264 while(str.find(std::string(
"./"), startpos) != std::string::npos){
2265 std::string::size_type pos = str.find(std::string(
"./"), startpos);
2268 if(str.substr(pos-1,3) !=
"../"){
2270 str = str.substr(0,pos) + str.substr(pos+2,str.length()-pos-2);
2276 while(str.find(std::string(
"..")) != std::string::npos){
2277 std::string::size_type pos = str.find(std::string(
".."));
2280 std::string::size_type end = str.find(
"/",pos);
2281 if(end != std::string::npos){
2291 std::string::size_type prev_slash = str.rfind(
"/",pos);
2293 vigra_invariant(prev_slash != 0 && prev_slash != std::string::npos,
2294 "Error parsing path: "+str);
2296 std::string::size_type begin = str.rfind(
"/",prev_slash-1);
2299 str = str.substr(0,begin+1) + str.substr(end,str.length()-end);
2309 inline bool relativePath_(std::string & path)
const 2311 std::string::size_type pos = path.find(
'/') ;
2320 inline std::string currentGroupName_()
const 2322 int len = H5Iget_name(cGroupHandle_,NULL,1000);
2324 H5Iget_name(cGroupHandle_,name.
begin(),len+1);
2326 return std::string(name.
begin());
2331 inline std::string fileName_()
const 2333 int len = H5Fget_name(fileHandle_,NULL,1000);
2335 H5Fget_name(fileHandle_,name.
begin(),len+1);
2337 return std::string(name.
begin());
2342 inline hid_t createFile_(std::string filePath,
OpenMode mode = Open)
2346 pFile = fopen ( filePath.c_str(),
"r" );
2350 if ( pFile == NULL )
2352 vigra_precondition(mode != OpenReadOnly,
2353 "HDF5File::open(): cannot open non-existing file in read-only mode.");
2354 fileId = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
2359 if(mode == OpenReadOnly)
2361 fileId = H5Fopen(filePath.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
2363 else if(mode == New)
2365 std::remove(filePath.c_str());
2366 fileId = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
2370 fileId = H5Fopen(filePath.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
2381 hid_t openGroup_(std::string groupName)
const 2383 return const_cast<HDF5File *
>(
this)->openCreateGroup_(groupName,
false);
2394 hid_t openCreateGroup_(std::string groupName,
bool create =
true)
2397 groupName = get_absolute_path(groupName);
2400 hid_t parent = H5Gopen(fileHandle_,
"/", H5P_DEFAULT);
2401 if(groupName ==
"/")
2407 groupName = std::string(groupName.begin()+1, groupName.end());
2410 if( groupName.size() != 0 && *groupName.rbegin() !=
'/')
2412 groupName = groupName +
'/';
2416 std::string::size_type begin = 0, end = groupName.find(
'/');
2417 while (end != std::string::npos)
2419 std::string group(groupName.begin()+begin, groupName.begin()+end);
2420 hid_t prevParent = parent;
2422 if(H5LTfind_dataset(parent, group.c_str()) == 0)
2425 parent = H5Gcreate(prevParent, group.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
2431 parent = H5Gopen(prevParent, group.c_str(), H5P_DEFAULT);
2433 H5Gclose(prevParent);
2440 end = groupName.find(
'/', begin);
2449 inline void deleteDataset_(hid_t parent, std::string datasetName)
2452 if(H5LTfind_dataset(parent, datasetName.c_str()))
2455 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR <= 6) 2456 if(H5Gunlink(parent, datasetName.c_str()) < 0)
2458 vigra_postcondition(
false,
"HDF5File::deleteDataset_(): Unable to delete existing data.");
2461 if(H5Ldelete(parent, datasetName.c_str(), H5P_DEFAULT ) < 0)
2463 vigra_postcondition(
false,
"HDF5File::deleteDataset_(): Unable to delete existing data.");
2471 hid_t getDatasetHandle_(std::string datasetName)
const 2474 datasetName = get_absolute_path(datasetName);
2476 std::string groupname = SplitString(datasetName).first();
2477 std::string setname = SplitString(datasetName).last();
2479 if(H5Lexists(fileHandle_, datasetName.c_str(), H5P_DEFAULT) <= 0)
2481 std::cerr <<
"HDF5File::getDatasetHandle_(): Dataset '" << datasetName <<
"' does not exist.\n";
2486 HDF5Handle groupHandle(openGroup_(groupname), &H5Gclose,
"HDF5File::getDatasetHandle_(): Internal error");
2488 return H5Dopen(groupHandle, setname.c_str(), H5P_DEFAULT);
2493 H5O_type_t get_object_type_(std::string name)
const 2495 name = get_absolute_path(name);
2496 std::string group_name = SplitString(name).first();
2497 std::string object_name = SplitString(name).last();
2498 if (!object_name.size())
2499 return H5O_TYPE_GROUP;
2501 htri_t exists = H5Lexists(fileHandle_, name.c_str(), H5P_DEFAULT);
2502 vigra_precondition(exists > 0,
"HDF5File::get_object_type_(): " 2503 "object \"" + name +
"\" " 2506 HDF5Handle group_handle(openGroup_(group_name), &H5Gclose,
"Internal error");
2507 return HDF5_get_type(group_handle, name.c_str());
2512 template<
unsigned int N,
class T,
class Str
ide>
2513 void write_attribute_(std::string name,
2514 const std::string & attribute_name,
2516 const hid_t datatype,
2517 const int numBandsOfType);
2525 inline void writeAtomicAttribute(std::string datasetName, std::string attributeName,
const T data)
2528 datasetName = get_absolute_path(datasetName);
2534 write_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), 1);
2539 template<
unsigned int N,
class T,
class Str
ide>
2540 void read_attribute_(std::string datasetName,
2541 std::string attributeName,
2543 const hid_t datatype,
const int numBandsOfType);
2551 inline void readAtomicAttribute(std::string datasetName, std::string attributeName, T & data)
2554 datasetName = get_absolute_path(datasetName);
2557 read_attribute_(datasetName, attributeName, array, detail::getH5DataType<T>(), 1);
2561 inline void readAtomicAttribute(std::string datasetName, std::string attributeName, std::string & data)
2564 datasetName = get_absolute_path(datasetName);
2567 read_attribute_(datasetName, attributeName, array, detail::getH5DataType<const char *>(), 1);
2568 data = std::string(array[0]);
2573 template<
unsigned int N,
class T,
class Str
ide>
2574 void write_(std::string &datasetName,
2576 const hid_t datatype,
2577 const int numBandsOfType,
2579 int compressionParameter = 0);
2590 inline void writeAtomic(std::string datasetName,
const T data)
2593 datasetName = get_absolute_path(datasetName);
2599 write_(datasetName, array, detail::getH5DataType<T>(), 1, chunkSize,0);
2604 template<
unsigned int N,
class T,
class Str
ide>
2605 void read_(std::string datasetName,
2607 const hid_t datatype,
const int numBandsOfType);
2618 inline void readAtomic(std::string datasetName, T & data)
2621 datasetName = get_absolute_path(datasetName);
2624 read_(datasetName, array, detail::getH5DataType<T>(), 1);
2628 inline void readAtomic(std::string datasetName, std::string & data)
2631 datasetName = get_absolute_path(datasetName);
2634 read_(datasetName, array, detail::getH5DataType<const char *>(), 1);
2635 data = std::string(array[0]);
2640 template<
unsigned int N,
class T,
class Str
ide>
2641 void writeBlock_(std::string datasetName,
2644 const hid_t datatype,
2645 const int numBandsOfType)
2648 std::string errorMessage =
"HDF5File::writeBlock(): Error opening dataset '" + datasetName +
"'.";
2649 HDF5HandleShared dataset(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
2650 herr_t status = writeBlock_(dataset, blockOffset, array, datatype, numBandsOfType);
2651 vigra_postcondition(status >= 0,
2652 "HDF5File::writeBlock(): write to dataset '" + datasetName +
"' via H5Dwrite() failed.");
2659 template<
unsigned int N,
class T,
class Str
ide>
2663 const hid_t datatype,
2664 const int numBandsOfType);
2670 template<
unsigned int N,
class T,
class Str
ide>
2671 void readBlock_(std::string datasetName,
2675 const hid_t datatype,
const int numBandsOfType)
2677 std::string errorMessage (
"HDF5File::readBlock(): Unable to open dataset '" + datasetName +
"'.");
2678 HDF5HandleShared dataset(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
2679 herr_t status = readBlock_(dataset, blockOffset, blockShape, array, datatype, numBandsOfType);
2680 vigra_postcondition(status >= 0,
2681 "HDF5File::readBlock(): read from dataset '" + datasetName +
"' via H5Dread() failed.");
2689 template<
unsigned int N,
class T,
class Str
ide>
2694 const hid_t datatype,
const int numBandsOfType);
2699 template<
int N,
class T>
2703 typename detail::HDF5TypeTraits<T>::value_type init,
2705 int compressionParameter)
2707 vigra_precondition(!isReadOnly(),
2708 "HDF5File::createDataset(): file is read-only.");
2711 datasetName = get_absolute_path(datasetName);
2713 std::string groupname = SplitString(datasetName).first();
2714 std::string setname = SplitString(datasetName).last();
2716 hid_t parent = openCreateGroup_(groupname);
2719 deleteDataset_(parent, setname);
2723 typedef detail::HDF5TypeTraits<T> TypeTraits;
2725 if(TypeTraits::numberOfBands() > 1)
2727 shape_inv.resize(N+1);
2728 shape_inv[N] = TypeTraits::numberOfBands();
2732 shape_inv.resize(N);
2734 for(
int k=0; k<N; ++k)
2735 shape_inv[N-1-k] = shape[k];
2739 dataspaceHandle =
HDF5Handle(H5Screate_simple(shape_inv.
size(), shape_inv.
data(), NULL),
2740 &H5Sclose,
"HDF5File::createDataset(): unable to create dataspace for scalar data.");
2743 HDF5Handle plist ( H5Pcreate(H5P_DATASET_CREATE), &H5Pclose,
"HDF5File::createDataset(): unable to create property list." );
2744 H5Pset_fill_value(plist, TypeTraits::getH5DataType(), &init);
2747 H5Pset_obj_track_times(plist, track_time);
2750 ArrayVector<hsize_t> chunks(defineChunks(chunkSize, shape, TypeTraits::numberOfBands(), compressionParameter));
2751 if(chunks.
size() > 0)
2753 std::reverse(chunks.
begin(), chunks.
end());
2754 H5Pset_chunk (plist, chunks.
size(), chunks.
begin());
2758 if(compressionParameter > 0)
2760 H5Pset_deflate(plist, compressionParameter);
2765 TypeTraits::getH5DataType(),
2766 dataspaceHandle, H5P_DEFAULT, plist, H5P_DEFAULT),
2768 "HDF5File::createDataset(): unable to create dataset.");
2769 if(parent != cGroupHandle_)
2772 return datasetHandle;
2777 template<
unsigned int N,
class T,
class Str
ide>
2778 void HDF5File::write_(std::string &datasetName,
2780 const hid_t datatype,
2781 const int numBandsOfType,
2783 int compressionParameter)
2785 vigra_precondition(!isReadOnly(),
2786 "HDF5File::write(): file is read-only.");
2788 std::string groupname = SplitString(datasetName).first();
2789 std::string setname = SplitString(datasetName).last();
2793 std::reverse(shape.begin(), shape.end());
2795 if(numBandsOfType > 1)
2796 shape.push_back(numBandsOfType);
2798 HDF5Handle dataspace(H5Screate_simple(shape.size(), shape.begin(), NULL), &H5Sclose,
2799 "HDF5File::write(): Can not create dataspace.");
2802 std::string errorMessage (
"HDF5File::write(): can not create group '" + groupname +
"'.");
2803 HDF5Handle groupHandle(openCreateGroup_(groupname), &H5Gclose, errorMessage.c_str());
2806 deleteDataset_(groupHandle, setname.c_str());
2809 HDF5Handle plist(H5Pcreate(H5P_DATASET_CREATE), &H5Pclose,
2810 "HDF5File::write(): unable to create property list." );
2813 H5Pset_obj_track_times(plist, track_time);
2817 if(chunks.
size() > 0)
2819 std::reverse(chunks.
begin(), chunks.
end());
2820 H5Pset_chunk (plist, chunks.
size(), chunks.
begin());
2824 if(compressionParameter > 0)
2826 H5Pset_deflate(plist, compressionParameter);
2830 HDF5Handle datasetHandle(H5Dcreate(groupHandle, setname.c_str(), datatype, dataspace,H5P_DEFAULT, plist, H5P_DEFAULT),
2831 &H5Dclose,
"HDF5File::write(): Can not create dataset.");
2837 status = H5Dwrite(datasetHandle, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, array.
data());
2847 int offset = numBandsOfType > 1 ? 1 : 0;
2848 std::reverse(shape.begin(), shape.end());
2849 if(chunks.
size() > 0)
2852 std::reverse(chunks.
begin(), chunks.
end());
2858 chunks[0] = numBandsOfType;
2860 for(
unsigned int k=0; k<N; ++k)
2862 chunks[k+offset] = array.
shape(k);
2863 prod *= array.
shape(k);
2870 start(shape.size(), 0),
2871 count(shape.size(), 1);
2873 count[N-1-offset] = numBandsOfType;
2876 Shape chunkCount, chunkMaxShape;
2877 for(
unsigned int k=offset; k<chunks.
size(); ++k)
2879 chunkMaxShape[k-offset] = chunks[k];
2885 for(; chunkIter != chunkEnd; ++chunkIter)
2887 Shape chunkStart(chunkIter.point() * chunkMaxShape),
2888 chunkStop(min(chunkStart + chunkMaxShape, array.
shape()));
2891 for(
unsigned int k=0; k<N; ++k)
2893 start[N-1-k] = chunkStart[k];
2894 count[N-1-k] = buffer.shape(k);
2899 count[N] = numBandsOfType;
2901 HDF5Handle filespace(H5Dget_space(datasetHandle),
2902 &H5Sclose,
"HDF5File::write(): unable to create hyperslabs.");
2903 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, start.data(), NULL, count.
data(), NULL);
2908 &H5Sclose,
"HDF5File::write(): unable to create hyperslabs.");
2909 status = H5Sselect_hyperslab(dataspace2, H5S_SELECT_SET, null.data(), NULL, count.
data(), NULL);
2913 status = H5Dwrite(datasetHandle, datatype, dataspace2, filespace, H5P_DEFAULT, buffer.data());
2918 vigra_postcondition(status >= 0,
2919 "HDF5File::write(): write to dataset '" + datasetName +
"' via H5Dwrite() failed.");
2924 template<
unsigned int N,
class T,
class Str
ide>
2928 const hid_t datatype,
2929 const int numBandsOfType)
2931 vigra_precondition(!isReadOnly(),
2932 "HDF5File::writeBlock(): file is read-only.");
2935 hssize_t dimensions = getDatasetDimensions_(datasetHandle);
2936 if(numBandsOfType > 1)
2938 vigra_precondition(N+1 == dimensions,
2939 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
2941 boffset.resize(N+1);
2942 bshape[N] = numBandsOfType;
2947 vigra_precondition(N == dimensions,
2948 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
2953 for(
int i = 0; i < N; ++i)
2956 bshape[N-1-i] = array.
shape(i);
2957 boffset[N-1-i] = blockOffset[i];
2963 "Unable to get origin dataspace");
2966 HDF5Handle dataspaceHandle (H5Dget_space(datasetHandle),&H5Sclose,
"Unable to create target dataspace");
2967 H5Sselect_hyperslab(dataspaceHandle, H5S_SELECT_SET,
2974 status = H5Dwrite( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, array.
data());
2980 status = H5Dwrite( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, buffer.data());
2987 template<
unsigned int N,
class T,
class Str
ide>
2988 void HDF5File::write_attribute_(std::string name,
2989 const std::string & attribute_name,
2991 const hid_t datatype,
2992 const int numBandsOfType)
2994 vigra_precondition(!isReadOnly(),
2995 "HDF5File::writeAttribute(): file is read-only.");
2999 std::reverse(shape.begin(), shape.end());
3000 if(numBandsOfType > 1)
3001 shape.push_back(numBandsOfType);
3003 HDF5Handle dataspace(H5Screate_simple(shape.size(),
3004 shape.begin(), NULL),
3005 &H5Sclose,
"HDF5File::writeAttribute(): Can not" 3006 " create dataspace.");
3008 std::string errorMessage (
"HDF5File::writeAttribute(): can not find " 3009 "object '" + name +
"'.");
3011 H5O_type_t h5_type = get_object_type_(name);
3012 bool is_group = h5_type == H5O_TYPE_GROUP;
3013 if (!is_group && h5_type != H5O_TYPE_DATASET)
3014 vigra_precondition(0,
"HDF5File::writeAttribute(): object \"" 3015 + name +
"\" is neither a group nor a " 3019 ? openCreateGroup_(name)
3020 : getDatasetHandle_(name),
3024 errorMessage.c_str());
3026 bool exists = existsAttribute(name, attribute_name);
3028 ? H5Aopen(object_handle,
3029 attribute_name.c_str(),
3031 : H5Acreate(object_handle,
3032 attribute_name.c_str(), datatype,
3033 dataspace, H5P_DEFAULT,
3036 "HDF5File::writeAttribute(): Can not create" 3042 status = H5Awrite(attributeHandle, datatype, array.
data());
3049 status = H5Awrite(attributeHandle, datatype, buffer.data());
3051 vigra_postcondition(status >= 0,
3052 "HDF5File::writeAttribute(): write to attribute '" + attribute_name +
"' via H5Awrite() failed.");
3057 template<
unsigned int N,
class T,
class Str
ide>
3058 void HDF5File::read_(std::string datasetName,
3060 const hid_t datatype,
const int numBandsOfType)
3065 std::string errorMessage (
"HDF5File::read(): Unable to open dataset '" + datasetName +
"'.");
3066 HDF5Handle datasetHandle(getDatasetHandle_(datasetName), &H5Dclose, errorMessage.c_str());
3070 int offset = (numBandsOfType > 1)
3075 "HDF5File::read(): Array dimension disagrees with dataset dimension.");
3078 for(
int k=offset; k < (int)dimshape.
size(); ++k)
3081 vigra_precondition(shape == array.
shape(),
3082 "HDF5File::read(): Array shape disagrees with dataset shape.");
3084 vigra_precondition(dimshape[0] == static_cast<hsize_t>(numBandsOfType),
3085 "HDF5File::read(): Band count doesn't match destination array compound type.");
3091 status = H5Dread(datasetHandle, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, array.
data() );
3098 chunks(dimshape.
size(), 1),
3099 start(dimshape.
size(), 0),
3100 count(dimshape.
size(), 1);
3102 HDF5Handle properties(H5Dget_create_plist(datasetHandle),
3103 &H5Pclose,
"HDF5File::read(): failed to get property list");
3104 if(H5D_CHUNKED == H5Pget_layout(properties))
3107 H5Pget_chunk(properties, static_cast<int>(chunks.size()), chunks.
data());
3108 std::reverse(chunks.begin(), chunks.end());
3113 chunks[0] = numBandsOfType;
3115 for(
unsigned int k=0; k<N; ++k)
3117 chunks[k+offset] = array.
shape(k);
3118 prod *= array.
shape(k);
3124 count[N-1-offset] =
static_cast<hsize_t
>(numBandsOfType);
3127 Shape chunkCount, chunkMaxShape;
3128 for(
unsigned int k=offset; k<chunks.size(); ++k)
3130 chunkMaxShape[k-offset] = chunks[k];
3136 for(; chunkIter != chunkEnd; ++chunkIter)
3138 Shape chunkStart(chunkIter.point() * chunkMaxShape),
3139 chunkStop(min(chunkStart + chunkMaxShape, array.
shape()));
3142 for(
unsigned int k=0; k<N; ++k)
3144 start[N-1-k] = chunkStart[k];
3145 count[N-1-k] = buffer.shape(k);
3150 count[N] = numBandsOfType;
3152 HDF5Handle filespace(H5Dget_space(datasetHandle),
3153 &H5Sclose,
"HDF5File::read(): unable to create hyperslabs.");
3154 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, start.data(), NULL, count.data(), NULL);
3158 HDF5Handle dataspace(H5Screate_simple(count.size(), count.data(), NULL),
3159 &H5Sclose,
"HDF5File::read(): unable to create hyperslabs.");
3160 status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, null.data(), NULL, count.data(), NULL);
3164 status = H5Dread(datasetHandle, datatype, dataspace, filespace, H5P_DEFAULT, buffer.data());
3168 array.
subarray(chunkStart, chunkStop) = buffer;
3171 vigra_postcondition(status >= 0,
3172 "HDF5File::read(): read from dataset '" + datasetName +
"' via H5Dread() failed.");
3177 template<
unsigned int N,
class T,
class Str
ide>
3182 const hid_t datatype,
const int numBandsOfType)
3184 vigra_precondition(blockShape == array.
shape(),
3185 "HDF5File::readBlock(): Array shape disagrees with block size.");
3188 hssize_t dimensions = getDatasetDimensions_(datasetHandle);
3189 if(numBandsOfType > 1)
3191 vigra_precondition(N+1 == dimensions,
3192 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
3194 boffset.resize(N+1);
3195 bshape[N] = numBandsOfType;
3200 vigra_precondition(N == dimensions,
3201 "HDF5File::readBlock(): Array dimension disagrees with data dimension.");
3206 for(
int i = 0; i < N; ++i)
3209 bshape[N-1-i] = blockShape[i];
3210 boffset[N-1-i] = blockOffset[i];
3214 HDF5Handle memspace_handle(H5Screate_simple(bshape.size(), bshape.data(), NULL),
3216 "Unable to create target dataspace");
3219 HDF5Handle dataspaceHandle(H5Dget_space(datasetHandle), &H5Sclose,
3220 "Unable to get dataspace");
3221 H5Sselect_hyperslab(dataspaceHandle, H5S_SELECT_SET,
3222 boffset.data(), bones.data(), bones.data(), bshape.data());
3228 status = H5Dread( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, array.
data());
3234 status = H5Dread( datasetHandle, datatype, memspace_handle, dataspaceHandle, H5P_DEFAULT, buffer.data());
3244 template<
unsigned int N,
class T,
class Str
ide>
3245 void HDF5File::read_attribute_(std::string datasetName,
3246 std::string attributeName,
3248 const hid_t datatype,
const int numBandsOfType)
3250 std::string dataset_path = get_absolute_path(datasetName);
3252 std::string message =
"HDF5File::readAttribute(): could not get handle for attribute '"+attributeName+
"'' of object '"+dataset_path+
"'.";
3253 HDF5Handle attr_handle (H5Aopen_by_name(fileHandle_,dataset_path.c_str(),attributeName.c_str(),H5P_DEFAULT,H5P_DEFAULT),&H5Aclose, message.c_str());
3256 message =
"HDF5File::readAttribute(): could not get dataspace for attribute '"+attributeName+
"'' of object '"+dataset_path+
"'.";
3257 HDF5Handle attr_dataspace_handle (H5Aget_space(attr_handle),&H5Sclose,message.c_str());
3260 int raw_dims = H5Sget_simple_extent_ndims(attr_dataspace_handle);
3261 int dims = std::max(raw_dims, 1);
3264 H5Sget_simple_extent_dims(attr_dataspace_handle, dimshape.
data(), NULL);
3269 std::reverse(dimshape.begin(), dimshape.end());
3271 int offset = (numBandsOfType > 1)
3274 message =
"HDF5File::readAttribute(): Array dimension disagrees with dataset dimension.";
3278 for(
int k=offset; k < (int)dimshape.size(); ++k)
3280 "HDF5File::readAttribute(): Array shape disagrees with dataset shape");
3286 status = H5Aread( attr_handle, datatype, array.
data());
3293 status = H5Aread( attr_handle, datatype, buffer.data() );
3298 vigra_postcondition(status >= 0,
3299 "HDF5File::readAttribute(): read from attribute '" + attributeName +
"' via H5Aread() failed.");
3341 template<
unsigned int N,
class T,
class Str
ideTag>
3347 template<
unsigned int N,
class T,
class Str
ideTag>
3355 inline hid_t openGroup(hid_t parent, std::string group_name)
3358 size_t last_slash = group_name.find_last_of(
'/');
3359 if (last_slash == std::string::npos || last_slash != group_name.size() - 1)
3360 group_name = group_name +
'/';
3361 std::string::size_type begin = 0, end = group_name.find(
'/');
3363 while (end != std::string::npos)
3365 std::string group(group_name.begin()+begin, group_name.begin()+end);
3366 hid_t prev_parent = parent;
3367 parent = H5Gopen(prev_parent, group.c_str(), H5P_DEFAULT);
3369 if(ii != 0) H5Gclose(prev_parent);
3370 if(parent < 0)
return parent;
3373 end = group_name.find(
'/', begin);
3378 inline hid_t createGroup(hid_t parent, std::string group_name)
3380 if(group_name.size() == 0 ||*group_name.rbegin() !=
'/')
3381 group_name = group_name +
'/';
3382 if(group_name ==
"/")
3383 return H5Gopen(parent, group_name.c_str(), H5P_DEFAULT);
3385 std::string::size_type begin = 0, end = group_name.find(
'/');
3387 while (end != std::string::npos)
3389 std::string group(group_name.begin()+begin, group_name.begin()+end);
3390 hid_t prev_parent = parent;
3392 if(H5LTfind_dataset(parent, group.c_str()) == 0)
3394 parent = H5Gcreate(prev_parent, group.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
3396 parent = H5Gopen(prev_parent, group.c_str(), H5P_DEFAULT);
3399 if(ii != 0) H5Gclose(prev_parent);
3400 if(parent < 0)
return parent;
3403 end = group_name.find(
'/', begin);
3408 inline void deleteDataset(hid_t parent, std::string dataset_name)
3411 if(H5LTfind_dataset(parent, dataset_name.c_str()))
3414 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR <= 6) 3415 if(H5Gunlink(parent, dataset_name.c_str()) < 0)
3417 vigra_postcondition(
false,
"writeToHDF5File(): Unable to delete existing data.");
3420 if(H5Ldelete(parent, dataset_name.c_str(), H5P_DEFAULT ) < 0)
3422 vigra_postcondition(
false,
"createDataset(): Unable to delete existing data.");
3428 inline hid_t createFile(std::string filePath,
bool append_ =
true)
3431 pFile = fopen ( filePath.c_str(),
"r" );
3433 if ( pFile == NULL )
3435 file_id = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
3440 file_id = H5Fopen(filePath.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
3445 std::remove(filePath.c_str());
3446 file_id = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
3451 template<
unsigned int N,
class T,
class Tag>
3454 std::string path_name(pathInFile), group_name, data_set_name, message;
3455 std::string::size_type delimiter = path_name.rfind(
'/');
3458 file_handle =
HDF5Handle(createFile(filePath), &H5Fclose,
3459 "createDataset(): unable to open output file.");
3462 if(delimiter == std::string::npos)
3465 data_set_name = path_name;
3469 group_name = std::string(path_name.begin(), path_name.begin()+delimiter);
3470 data_set_name = std::string(path_name.begin()+delimiter+1, path_name.end());
3474 HDF5Handle group(createGroup(file_handle, group_name), &H5Gclose,
3475 "createDataset(): Unable to create and open group. generic v");
3478 deleteDataset(group, data_set_name);
3483 if(numBandsOfType > 1) {
3485 hsize_t shape_inv[N+1];
3486 for(
unsigned int k=0; k<N; ++k) {
3487 shape_inv[N-1-k] = array.
shape(k);
3490 shape_inv[N] = numBandsOfType;
3493 dataspace_handle =
HDF5Handle(H5Screate_simple(N+1, shape_inv, NULL),
3494 &H5Sclose,
"createDataset(): unable to create dataspace for non-scalar data.");
3497 hsize_t shape_inv[N];
3498 for(
unsigned int k=0; k<N; ++k)
3499 shape_inv[N-1-k] = array.
shape(k);
3502 dataspace_handle =
HDF5Handle(H5Screate_simple(N, shape_inv, NULL),
3503 &H5Sclose,
"createDataset(): unable to create dataspace for scalar data.");
3508 data_set_name.c_str(),
3511 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT),
3512 &H5Dclose,
"createDataset(): unable to create dataset.");
3555 template<
unsigned int N,
class T,
class Str
ideTag>
3559 writeHDF5(filePath, pathInFile, array, 0, 0);
3562 template<
unsigned int N,
class T,
class Str
ideTag>
3565 HDF5File file(filePath, HDF5File::Open);
3566 file.
write(pathInFile, array);
3581 void operator()(std::string
const & in)
3583 size = in.size() > size ?
3591 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8) || DOXYGEN 3598 template<
size_t N,
class T,
class C>
3603 if(H5Aexists(loc, name) > 0)
3604 H5Adelete(loc, name);
3607 array.
shape().end());
3609 dataspace_handle(H5Screate_simple(N, shape.data(), NULL),
3611 "writeToHDF5File(): unable to create dataspace.");
3615 detail::getH5DataType<T>(),
3617 H5P_DEFAULT ,H5P_DEFAULT ),
3619 "writeHDF5Attr: unable to create Attribute");
3623 for(
int ii = 0; ii < array.
size(); ++ii)
3624 buffer.push_back(array[ii]);
3625 H5Awrite(attr, detail::getH5DataType<T>(), buffer.
data());
3634 template<
size_t N,
class C>
3639 if(H5Aexists(loc, name) > 0)
3640 H5Adelete(loc, name);
3643 array.
shape().end());
3645 dataspace_handle(H5Screate_simple(N, shape.data(), NULL),
3647 "writeToHDF5File(): unable to create dataspace.");
3651 "writeToHDF5File(): unable to create type.");
3653 detail::MaxSizeFnc max_size;
3654 max_size = std::for_each(array.
data(),array.
data()+ array.
size(), max_size);
3655 H5Tset_size (atype, max_size.size);
3661 H5P_DEFAULT ,H5P_DEFAULT ),
3663 "writeHDF5Attr: unable to create Attribute");
3665 std::string buf =
"";
3666 for(
int ii = 0; ii < array.
size(); ++ii)
3668 buf = buf + array[ii]
3669 + std::string(max_size.size - array[ii].
size(),
' ');
3671 H5Awrite(attr, atype, buf.c_str());
3699 std::string pathInFile,
3702 std::string path_name(pathInFile), group_name, data_set_name, message, attr_name;
3703 std::string::size_type delimiter = path_name.rfind(
'/');
3706 HDF5Handle file_id(createFile(filePath), &H5Fclose,
3707 "writeToHDF5File(): unable to open output file.");
3710 if(delimiter == std::string::npos)
3713 data_set_name = path_name;
3718 group_name = std::string(path_name.begin(), path_name.begin()+delimiter);
3719 data_set_name = std::string(path_name.begin()+delimiter+1, path_name.end());
3721 delimiter = data_set_name.rfind(
'.');
3722 if(delimiter == std::string::npos)
3724 attr_name = path_name;
3725 data_set_name =
"/";
3729 attr_name = std::string(data_set_name.begin()+delimiter+1, data_set_name.end());
3730 data_set_name = std::string(data_set_name.begin(), data_set_name.begin()+delimiter);
3733 HDF5Handle group(openGroup(file_id, group_name), &H5Gclose,
3734 "writeToHDF5File(): Unable to create and open group. attr ver");
3736 if(data_set_name !=
"/")
3738 HDF5Handle dset(H5Dopen(group, data_set_name.c_str(), H5P_DEFAULT), &H5Dclose,
3739 "writeHDF5Attr():unable to open dataset");
3754 #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:2244
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:3599
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:2701
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