Field3D
Hdf5Util Namespace Reference

Contains utility functions and classes for Hdf5 files. More...

Classes

class  H5Base
 Base class for all scoped Hdf5 util classes. More...
class  H5ScopedAget_space
 Scoped object - opens an attribute data space on creation and closes it on destruction. More...
class  H5ScopedAget_type
 Scoped object - opens an attribute data type on creation and closes it on destruction. More...
class  H5ScopedAopen
 Scoped object - Opens attribute by name and closes it on destruction. More...
class  H5ScopedAopenIdx
 Scoped object - Opens attribute by index and closes it on destruction. More...
class  H5ScopedDcreate
 Scoped object - creates a dataset on creation and closes it on destruction. More...
class  H5ScopedDget_space
 Scoped object - opens a dataset on creation and closes it on destruction. More...
class  H5ScopedDget_type
 Scoped object - opens a dataset on creation and closes it on destruction. More...
class  H5ScopedDopen
 Scoped object - opens a dataset on creation and closes it on destruction. More...
class  H5ScopedGcreate
 Scoped object - creates a group on creation and closes it on destruction. More...
class  H5ScopedGopen
 Scoped object - opens a group on creation and closes it on destruction. More...
class  H5ScopedScreate
 Scoped object - creates a dataspace on creation and closes it on destruction. More...
class  H5ScopedTget_native_type
 Scoped object - opens an native type id on creation and closes it on destruction. More...

Functions

bool checkHdf5Gzip ()
 Checks whether gzip is available in the current hdf5 library.
bool readAttribute (hid_t location, const string &attrName, string &value)
bool readAttribute (hid_t location, const string &attrName, unsigned int attrSize, int &value)
bool readAttribute (hid_t location, const string &attrName, unsigned int attrSize, float &value)
bool readAttribute (hid_t location, const string &attrName, unsigned int attrSize, double &value)
bool readAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, int &value)
bool readAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, float &value)
bool readAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, double &value)
bool writeAttribute (hid_t location, const string &attrName, const string &value)
bool writeAttribute (hid_t location, const string &attrName, unsigned int attrSize, const int &value)
bool writeAttribute (hid_t location, const string &attrName, unsigned int attrSize, const float &value)
bool writeAttribute (hid_t location, const string &attrName, unsigned int attrSize, const double &value)
bool writeAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, const int &value)
bool writeAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, const float &value)
bool writeAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, const double &value)
Read/write simple data to hdf5 location
template<typename T >
void writeSimpleData (hid_t location, const std::string &name, const std::vector< T > &data)
 Writes a simple linear data set to the given location.
template<typename T >
void readSimpleData (hid_t location, const std::string &name, std::vector< T > &data)
 Reads a simple linear data set from the given location.
Attribute reading
bool readAttribute (hid_t location, const std::string &attrName, std::string &value)
 Reads a string attribute.
bool readAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, int &value)
 Reads an int attribute of arbitrary size.
bool readAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, float &value)
 Reads a float attribute of arbitrary size.
bool readAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, double &value)
 Reads a double attribute of arbitrary size.
bool readAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, int &value)
 Reads a int attribute of arbitrary size and rank.
bool readAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, float &value)
 Reads a float attribute of arbitrary size and rank.
bool readAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, double &value)
 Reads a double attribute of arbitrary size and rank.
Attribute writing
bool writeAttribute (hid_t location, const std::string &attrName, const std::string &value)
 Writes a string attribute.
bool writeAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, const int &value)
 Writes an int attribute of arbitrary size.
bool writeAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, const float &value)
 Writes a float attribute of arbitrary size.
bool writeAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, const double &value)
 Writes a double attribute of arbitrary size.
bool writeAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, const int &value)
 Writes a float attribute of arbitrary size and rank.
bool writeAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, const float &value)
 Writes a float attribute of arbitrary size and rank.
bool writeAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, const double &value)
 Writes a double attribute of arbitrary size and rank.

Detailed Description

Contains utility functions and classes for Hdf5 files.


Function Documentation

bool Hdf5Util::readAttribute ( hid_t  location,
const string &  attrName,
string &  value 
)

Definition at line 68 of file Hdf5Util.cpp.

{
H5T_class_t typeClass;
H5A_info_t attrInfo;
hsize_t strLen;
if (H5Aexists(location, attrName.c_str()) < 1)
throw MissingAttributeException("Couldn't find attribute " + attrName);
H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
H5ScopedAget_space attrSpace(attr);
H5ScopedAget_type attrType(attr);
if (H5Aget_info(attr, &attrInfo) < 0) {
throw MissingAttributeException("Couldn't get attribute info " + attrName);
} else {
strLen = attrInfo.data_size;
}
typeClass = H5Tget_class(attrType);
if (typeClass != H5T_STRING)
throw MissingAttributeException("Bad attribute type class for " + attrName);
H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
std::vector<char> tempString(strLen + 1);
if (H5Aread(attr, nativeType, &tempString[0]) < 0)
throw MissingAttributeException("Couldn't read attribute " + attrName);
value = string(&tempString[0]);
return true;
}
bool Hdf5Util::readAttribute ( hid_t  location,
const string &  attrName,
unsigned int  attrSize,
int &  value 
)

Definition at line 108 of file Hdf5Util.cpp.

{
H5T_class_t typeClass;
if (H5Aexists(location, attrName.c_str()) < 1)
throw MissingAttributeException("Couldn't find attribute " + attrName);
H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
H5ScopedAget_space attrSpace(attr);
H5ScopedAget_type attrType(attr);
if (H5Sget_simple_extent_ndims(attrSpace) != 1)
throw MissingAttributeException("Bad attribute rank for attribute " +
attrName);
hsize_t dims[1];
H5Sget_simple_extent_dims(attrSpace, dims, NULL);
if (dims[0] != attrSize)
throw MissingAttributeException("Invalid attribute size for attribute " +
attrName);
typeClass = H5Tget_class(attrType);
if (typeClass != H5T_INTEGER)
throw MissingAttributeException("Bad attribute type class for " +
attrName);
H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
if (H5Aread(attr, nativeType, &value) < 0)
throw MissingAttributeException("Couldn't read attribute " + attrName);
return true;
}
bool Hdf5Util::readAttribute ( hid_t  location,
const string &  attrName,
unsigned int  attrSize,
float &  value 
)

Definition at line 149 of file Hdf5Util.cpp.

{
H5T_class_t typeClass;
if (H5Aexists(location, attrName.c_str()) < 1)
throw MissingAttributeException("Couldn't find attribute " + attrName);
H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
H5ScopedAget_space attrSpace(attr);
H5ScopedAget_type attrType(attr);
if (H5Sget_simple_extent_ndims(attrSpace) != 1)
throw MissingAttributeException("Bad attribute rank for attribute " +
attrName);
hsize_t dims[1];
H5Sget_simple_extent_dims(attrSpace, dims, NULL);
if (dims[0] != attrSize)
throw MissingAttributeException("Invalid attribute size for attribute " +
attrName);
typeClass = H5Tget_class(attrType);
if (typeClass != H5T_FLOAT)
throw MissingAttributeException("Bad attribute type class for " +
attrName);
H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
if (H5Aread(attr, nativeType, &value) < 0)
throw MissingAttributeException("Couldn't read attribute " + attrName);
return true;
}
bool Hdf5Util::readAttribute ( hid_t  location,
const string &  attrName,
unsigned int  attrSize,
double &  value 
)

Definition at line 189 of file Hdf5Util.cpp.

{
H5T_class_t typeClass;
if (H5Aexists(location, attrName.c_str()) < 0)
throw MissingAttributeException("Couldn't find attribute " + attrName);
H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
H5ScopedAget_space attrSpace(attr);
H5ScopedAget_type attrType(attr);
if (H5Sget_simple_extent_ndims(attrSpace) != 1)
throw MissingAttributeException("Bad attribute rank for attribute " +
attrName);
hsize_t dims[1];
H5Sget_simple_extent_dims(attrSpace, dims, NULL);
if (dims[0] != attrSize)
throw MissingAttributeException("Invalid attribute size for attribute " +
attrName);
typeClass = H5Tget_class(attrType);
if (typeClass != H5T_FLOAT)
throw MissingAttributeException("Bad attribute type class for " +
attrName);
H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
if (H5Aread(attr, nativeType, &value) < 0)
throw MissingAttributeException("Couldn't read attribute " + attrName);
return true;
}
bool Hdf5Util::readAttribute ( hid_t  location,
const string &  attrName,
std::vector< unsigned int > &  attrSize,
int &  value 
)

Definition at line 229 of file Hdf5Util.cpp.

{
H5T_class_t typeClass;
int rank = attrSize.size();
if (H5Aexists(location, attrName.c_str()) < 0)
throw MissingAttributeException("Couldn't find attribute " + attrName);
H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
H5ScopedAget_space attrSpace(attr);
H5ScopedAget_type attrType(attr);
if (H5Sget_simple_extent_ndims(attrSpace) != rank)
throw MissingAttributeException("Bad attribute rank for attribute " +
attrName);
hsize_t dims[rank];
H5Sget_simple_extent_dims(attrSpace, dims, NULL);
for (int i=0; i < rank; i++) {
if (dims[i] != attrSize[i])
throw MissingAttributeException("Invalid attribute size for attribute " +
attrName);
}
typeClass = H5Tget_class(attrType);
if (typeClass != H5T_INTEGER)
throw MissingAttributeException("Bad attribute type class for " +
attrName);
H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
if (H5Aread(attr, nativeType, &value) < 0)
throw MissingAttributeException("Couldn't read attribute " + attrName);
return true;
}
bool Hdf5Util::readAttribute ( hid_t  location,
const string &  attrName,
std::vector< unsigned int > &  attrSize,
float &  value 
)

Definition at line 273 of file Hdf5Util.cpp.

{
H5T_class_t typeClass;
int rank = attrSize.size();
if (H5Aexists(location, attrName.c_str()) < 0)
throw MissingAttributeException("Couldn't find attribute " + attrName);
H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
H5ScopedAget_space attrSpace(attr);
H5ScopedAget_type attrType(attr);
if (H5Sget_simple_extent_ndims(attrSpace) != rank)
throw MissingAttributeException("Bad attribute rank for attribute " +
attrName);
hsize_t dims[rank];
H5Sget_simple_extent_dims(attrSpace, dims, NULL);
for (int i=0; i < rank; i++) {
if (dims[i] != attrSize[i])
throw MissingAttributeException("Invalid attribute size for attribute " +
attrName);
}
typeClass = H5Tget_class(attrType);
if (typeClass != H5T_FLOAT)
throw MissingAttributeException("Bad attribute type class for " +
attrName);
H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
if (H5Aread(attr, nativeType, &value) < 0)
throw MissingAttributeException("Couldn't read attribute " + attrName);
return true;
}
bool Hdf5Util::readAttribute ( hid_t  location,
const string &  attrName,
std::vector< unsigned int > &  attrSize,
double &  value 
)

Definition at line 317 of file Hdf5Util.cpp.

{
H5T_class_t typeClass;
int rank = attrSize.size();
if (H5Aexists(location, attrName.c_str()) < 0)
throw MissingAttributeException("Couldn't find attribute " + attrName);
H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
H5ScopedAget_space attrSpace(attr);
H5ScopedAget_type attrType(attr);
if (H5Sget_simple_extent_ndims(attrSpace) != rank)
throw MissingAttributeException("Bad attribute rank for attribute " +
attrName);
hsize_t dims[rank];
H5Sget_simple_extent_dims(attrSpace, dims, NULL);
for (int i=0; i < rank; i++) {
if (dims[i] != attrSize[i])
throw MissingAttributeException("Invalid attribute size for attribute " +
attrName);
}
typeClass = H5Tget_class(attrType);
if (typeClass != H5T_FLOAT)
throw MissingAttributeException("Bad attribute type class for " +
attrName);
H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
if (H5Aread(attr, nativeType, &value) < 0)
throw MissingAttributeException("Couldn't read attribute " + attrName);
return true;
}
bool Hdf5Util::writeAttribute ( hid_t  location,
const string &  attrName,
const string &  value 
)

Definition at line 362 of file Hdf5Util.cpp.

References Msg::print(), and Msg::SevWarning.

{
hid_t attr = -1;
hid_t attrSpace;
hid_t attrType;
bool success = true;
attrSpace = H5Screate(H5S_SCALAR);
if (attrSpace == -1)
success = false;
attrType = H5Tcopy(H5T_C_S1);
if (attrType == -1)
success = false;
if (value.size()) {
// if the string is null the following will return error
// which we don't want.
if (success && H5Tset_size(attrType, value.size()) == -1){
success = false;
}
}
if (success) {
H5Tset_strpad(attrType, H5T_STR_NULLTERM);
attr = H5Acreate(location, attrName.c_str(), attrType, attrSpace,
H5P_DEFAULT, H5P_DEFAULT);
}
if (attr == -1) {
Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
success = false;
}
if (success && H5Awrite(attr, attrType, value.c_str()) == -1) {
Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
success = false;
}
H5Aclose(attr);
H5Tclose(attrType);
H5Sclose(attrSpace);
return success;
}
bool Hdf5Util::writeAttribute ( hid_t  location,
const string &  attrName,
unsigned int  attrSize,
const int &  value 
)

Definition at line 413 of file Hdf5Util.cpp.

References Msg::print(), and Msg::SevWarning.

{
hid_t attr;
hid_t attrSpace;
hsize_t dims[1];
dims[0] = attrSize;
attrSpace = H5Screate(H5S_SIMPLE);
if (attrSpace < 0)
return false;
if (H5Sset_extent_simple(attrSpace, 1, dims, NULL) < 0)
return false;
attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_INT,
attrSpace, H5P_DEFAULT, H5P_DEFAULT);
if (attr < 0) {
Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
if (H5Awrite(attr, H5T_NATIVE_INT, &value) < 0) {
Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
H5Aclose(attr);
H5Sclose(attrSpace);
return true;
}
bool Hdf5Util::writeAttribute ( hid_t  location,
const string &  attrName,
unsigned int  attrSize,
const float &  value 
)

Definition at line 454 of file Hdf5Util.cpp.

References Msg::print(), and Msg::SevWarning.

{
hid_t attr;
hid_t attrSpace;
hsize_t dims[1];
dims[0] = attrSize;
attrSpace = H5Screate(H5S_SIMPLE);
if (attrSpace < 0)
return false;
if (H5Sset_extent_simple(attrSpace, 1, dims, NULL) < 0)
return false;
attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_FLOAT,
attrSpace, H5P_DEFAULT, H5P_DEFAULT);
if (attr < 0) {
Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
if (H5Awrite(attr, H5T_NATIVE_FLOAT, &value) < 0) {
Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
H5Aclose(attr);
H5Sclose(attrSpace);
return true;
}
bool Hdf5Util::writeAttribute ( hid_t  location,
const string &  attrName,
unsigned int  attrSize,
const double &  value 
)

Definition at line 495 of file Hdf5Util.cpp.

References Msg::print(), and Msg::SevWarning.

{
hid_t attr;
hid_t attrSpace;
hsize_t dims[1];
dims[0] = attrSize;
attrSpace = H5Screate(H5S_SIMPLE);
if (attrSpace < 0)
return false;
if (H5Sset_extent_simple(attrSpace, 1, dims, NULL) < 0)
return false;
attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_DOUBLE,
attrSpace, H5P_DEFAULT, H5P_DEFAULT);
if (attr < 0) {
Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
if (H5Awrite(attr, H5T_NATIVE_DOUBLE, &value) < 0) {
Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
H5Aclose(attr);
H5Sclose(attrSpace);
return true;
}
bool Hdf5Util::writeAttribute ( hid_t  location,
const string &  attrName,
std::vector< unsigned int > &  attrSize,
const int &  value 
)

Definition at line 537 of file Hdf5Util.cpp.

References Msg::print(), and Msg::SevWarning.

{
hid_t attr;
hid_t attrSpace;
size_t rank = attrSize.size();
hsize_t current_dims[rank];
hsize_t max_dims[rank];
for (size_t i=0; i < rank; i++)
current_dims[i] = attrSize[i];
for (size_t i=0; i < rank; i++)
max_dims[i] = H5S_UNLIMITED;
attrSpace = H5Screate(H5S_SIMPLE);
if (attrSpace < 0)
return false;
if (H5Sset_extent_simple(attrSpace, rank, current_dims, max_dims) < 0) {
return false;
}
attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_INT,
attrSpace, H5P_DEFAULT, H5P_DEFAULT);
if (attr < 0) {
Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
if (H5Awrite(attr, H5T_NATIVE_INT, &value) < 0) {
Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
H5Aclose(attr);
H5Sclose(attrSpace);
return true;
}
bool Hdf5Util::writeAttribute ( hid_t  location,
const string &  attrName,
std::vector< unsigned int > &  attrSize,
const float &  value 
)

Definition at line 585 of file Hdf5Util.cpp.

References Msg::print(), and Msg::SevWarning.

{
hid_t attr;
hid_t attrSpace;
size_t rank = attrSize.size();
hsize_t current_dims[rank];
hsize_t max_dims[rank];
for (size_t i=0; i < rank; i++)
current_dims[i] = attrSize[i];
for (size_t i=0; i < rank; i++)
max_dims[i] = H5S_UNLIMITED;
attrSpace = H5Screate(H5S_SIMPLE);
if (attrSpace < 0)
return false;
if (H5Sset_extent_simple(attrSpace, rank, current_dims, max_dims) < 0) {
return false;
}
attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_FLOAT,
attrSpace, H5P_DEFAULT, H5P_DEFAULT);
if (attr < 0) {
Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
if (H5Awrite(attr, H5T_NATIVE_FLOAT, &value) < 0) {
Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
H5Aclose(attr);
H5Sclose(attrSpace);
return true;
}
bool Hdf5Util::writeAttribute ( hid_t  location,
const string &  attrName,
std::vector< unsigned int > &  attrSize,
const double &  value 
)

Definition at line 633 of file Hdf5Util.cpp.

References Msg::print(), and Msg::SevWarning.

{
hid_t attr;
hid_t attrSpace;
size_t rank = attrSize.size();
hsize_t current_dims[rank];
hsize_t max_dims[rank];
for (size_t i=0; i < rank; i++)
current_dims[i] = attrSize[i];
for (size_t i=0; i < rank; i++)
max_dims[i] = H5S_UNLIMITED;
attrSpace = H5Screate(H5S_SIMPLE);
if (attrSpace < 0)
return false;
if (H5Sset_extent_simple(attrSpace, rank, current_dims, max_dims) < 0) {
return false;
}
attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_DOUBLE,
attrSpace, H5P_DEFAULT, H5P_DEFAULT);
if (attr < 0) {
Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
if (H5Awrite(attr, H5T_NATIVE_DOUBLE, &value) < 0) {
Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
H5Aclose(attr);
H5Sclose(attrSpace);
return false;
}
H5Aclose(attr);
H5Sclose(attrSpace);
return true;
}