kspaceFirstOrder3D-OMP  1.2
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
Hdf5File Class Reference

Class wrapping the HDF5 routines. More...

#include <Hdf5File.h>

Collaboration diagram for Hdf5File:

Public Types

enum  MatrixDataType { MatrixDataType::kFloat = 0, MatrixDataType::kLong = 1 }
 HDF5 matrix data type (float or uint64). More...
 
enum  MatrixDomainType { MatrixDomainType::kReal = 0, MatrixDomainType::kComplex = 1 }
 HDF5 Matrix domain type (real or complex). More...
 

Public Member Functions

 Hdf5File ()
 Constructor of the class. More...
 
 Hdf5File (const Hdf5File &)=delete
 Copy constructor is not allowed.
 
virtual ~Hdf5File ()
 Destructor. More...
 
Hdf5Fileoperator= (const Hdf5File &)=delete
 Operator = is not allowed.
 
void create (const std::string &fileName, unsigned int flags=H5F_ACC_TRUNC)
 Create the HDF5 file. More...
 
void open (const std::string &fileName, unsigned int flags=H5F_ACC_RDONLY)
 Open the HDF5 file. More...
 
bool isOpen () const
 Is the file opened? More...
 
void close ()
 Close the HDF5 file. More...
 
hid_t createGroup (const hid_t parentGroup, MatrixName &groupName)
 Create a HDF5 group at a specified place in the file tree. More...
 
hid_t openGroup (const hid_t parentGroup, MatrixName &groupName)
 Open a HDF5 group at a specified place in the file tree. More...
 
void closeGroup (const hid_t group)
 Close a group. More...
 
hid_t getRootGroup () const
 Get handle to the root group of the file. More...
 
hid_t openDataset (const hid_t parentGroup, MatrixName &datasetName)
 Open a dataset at a specified place in the file tree. More...
 
hid_t createDataset (const hid_t parentGroup, MatrixName &datasetName, const DimensionSizes &dimensionSizes, const DimensionSizes &chunkSizes, const MatrixDataType matrixDataType, const size_t compressionLevel)
 Create a float HDF5 dataset at a specified place in the file tree (3D/4D). More...
 
void closeDataset (const hid_t dataset)
 Close dataset. More...
 
template<class T >
void writeHyperSlab (const hid_t dataset, const DimensionSizes &position, const DimensionSizes &size, const T *data)
 Write a hyperslab into the dataset. More...
 
void writeCuboidToHyperSlab (const hid_t dataset, const DimensionSizes &hyperslabPosition, const DimensionSizes &cuboidPosition, const DimensionSizes &cuboidSize, const DimensionSizes &matrixDimensions, const float *matrixData)
 Write a cuboid selected within the matrixData into a hyperslab. More...
 
void writeSensorByMaskToHyperSlab (const hid_t dataset, const DimensionSizes &hyperslabPosition, const size_t indexSensorSize, const size_t *indexSensorData, const DimensionSizes &matrixDimensions, const float *matrixData)
 Write sensor data selected by the sensor mask - Occasionally very slow, do not use! More...
 
template<class T >
void writeScalarValue (const hid_t parentGroup, MatrixName &datasetName, const T value)
 Write the scalar value under a specified group. More...
 
template<class T >
void readScalarValue (const hid_t parentGroup, MatrixName &datasetName, T &value)
 Read the scalar value under a specified group. More...
 
template<class T >
void readCompleteDataset (const hid_t parentGroup, MatrixName &datasetName, const DimensionSizes &dimensionSizes, T *data)
 Read data from the dataset at a specified place in the file tree. More...
 
DimensionSizes getDatasetDimensionSizes (const hid_t parentGroup, MatrixName &datasetName)
 Get dimension sizes of the dataset under a specified group. More...
 
size_t getDatasetNumberOfDimensions (const hid_t parentGroup, MatrixName &datasetName)
 Get number of dimensions of the dataset under a specified group. More...
 
size_t getDatasetSize (const hid_t parentGroup, MatrixName &datasetName)
 Get dataset element count at a specified place in the file tree. More...
 
void writeMatrixDataType (const hid_t parentGroup, MatrixName &datasetName, const MatrixDataType &matrixDataType)
 Write matrix data type into the dataset at a specified place in the file tree. More...
 
void writeMatrixDomainType (const hid_t parentGroup, MatrixName &datasetName, const MatrixDomainType &matrixDomainType)
 Write matrix data type into the dataset at a specified place in the file tree. More...
 
MatrixDataType readMatrixDataType (const hid_t parentGroup, MatrixName &datasetName)
 Read matrix data type from the dataset at a specified place in the file tree. More...
 
MatrixDomainType readMatrixDomainType (const hid_t parentGroup, MatrixName &datasetName)
 Read matrix dataset domain type at a specified place in the file tree. More...
 
void writeStringAttribute (const hid_t parentGroup, MatrixName &datasetName, MatrixName &attributeName, const std::string &value)
 Write string attribute into the dataset under the root group. More...
 
std::string readStringAttribute (const hid_t parentGroup, MatrixName &datasetName, MatrixName &attributeName)
 Read string attribute from the dataset under the root group. More...
 

Static Public Member Functions

static bool canAccess (const std::string &fileName)
 Can I access the file. More...
 

Private Attributes

hid_t mFile
 HDF file handle.
 
std::string mFileName
 File name.
 

Static Private Attributes

static const std::string kMatrixDomainTypeName = "domain_type"
 String representation of the Domain type in the HDF5 file.
 
static const std::string kMatrixDataTypeName = "data_type"
 String representation of the Data type in the HDF5 file.
 
static const std::string kMatrixDomainTypeNames [] = {"real", "complex"}
 String representation of different domain types.
 
static const std::string kMatrixDataTypeNames [] = {"float", "long"}
 String representation of different data types.
 

Detailed Description

This class is responsible for working with HDF5 files. It offers routines to manage files (create, open, close) as well as creating, reading and modifying the contents (groups and datasets).

Definition at line 490 of file Hdf5File.h.

Member Enumeration Documentation

◆ MatrixDataType

HDF5 matrix data type (float or uint64).

Enumerator
kFloat 

The matrix is stored in floating point 32b wide format.

kLong 

The matrix is stored in fixed point point 64b wide format.

Definition at line 499 of file Hdf5File.h.

◆ MatrixDomainType

HDF5 Matrix domain type (real or complex).

Enumerator
kReal 

The matrix is defined on real domain.

kComplex 

The matrix is defined on complex domain.

Definition at line 512 of file Hdf5File.h.

Constructor & Destructor Documentation

◆ Hdf5File()

Hdf5File::Hdf5File ( )

Constructor.

Definition at line 73 of file Hdf5File.cpp.

◆ ~Hdf5File()

Hdf5File::~Hdf5File ( )
virtual

Destructor.

Definition at line 83 of file Hdf5File.cpp.

Member Function Documentation

◆ canAccess()

bool Hdf5File::canAccess ( const std::string &  fileName)
static

Can the code access the file, e.g. does it exist, do we have enough privileges, etc.

Parameters
[in]fileName- Name of the file.
Returns
true - If it is possible to access the file.

Can I access the file.

Definition at line 146 of file Hdf5File.cpp.

◆ close()

void Hdf5File::close ( )
Exceptions
ios::failure- If an error happens

Close the HDF5 file.

Definition at line 161 of file Hdf5File.cpp.

◆ closeDataset()

void Hdf5File::closeDataset ( const hid_t  dataset)
Parameters
[in]dataset- Dataset to close.

Close dataset.

Definition at line 325 of file Hdf5File.cpp.

◆ closeGroup()

void Hdf5File::closeGroup ( const hid_t  group)
Parameters
[in]group- Group to close.

Close a group.

Definition at line 214 of file Hdf5File.cpp.

◆ create()

void Hdf5File::create ( const std::string &  fileName,
unsigned int  flags = H5F_ACC_TRUNC 
)

The file is always created in read write mode mode, by default overwriting an old file of the same name. Other HDF5 flags are set to default.

Parameters
[in]fileName- File name.
[in]flags- How to create the file, by default overwrite existing file.
Exceptions
ios:failure- If error happened (file is open or cannot be created).

Create an HDF5 file.

Definition at line 93 of file Hdf5File.cpp.

◆ createDataset()

hid_t Hdf5File::createDataset ( const hid_t  parentGroup,
MatrixName datasetName,
const DimensionSizes dimensionSizes,
const DimensionSizes chunkSizes,
const MatrixDataType  matrixDataType,
const size_t  compressionLevel 
)

Other HDF5 flags are set to default.

Parameters
[in]parentGroup- Parent group id.
[in]datasetName- Dataset name.
[in]dimensionSizes- Dimension sizes.
[in]chunkSizes- Chunk sizes.
[in]matrixDataType- Matrix data type
[in]compressionLevel- Compression level.
Returns
A handle to the new dataset.
Exceptions
ios::failure- If error happens.

Create dataset.

Definition at line 241 of file Hdf5File.cpp.

◆ createGroup()

hid_t Hdf5File::createGroup ( const hid_t  parentGroup,
MatrixName groupName 
)

Other HDF5 flags are set to default.

Parameters
[in]parentGroup- Where to link the group at.
[in]groupName- Group name.
Returns
A handle to the new group.
Exceptions
ios::failure- If error happens.

Create a HDF5 group at a specified place in the file tree.

Definition at line 178 of file Hdf5File.cpp.

◆ getDatasetDimensionSizes()

DimensionSizes Hdf5File::getDatasetDimensionSizes ( const hid_t  parentGroup,
MatrixName datasetName 
)
Parameters
[in]parentGroup- Where the dataset is.
[in]datasetName- Dataset name.
Returns
Dimension sizes of the dataset.
Exceptions
ios::failure- If error happens.

Get dimension sizes of the dataset at a specified place in the file tree.

Definition at line 731 of file Hdf5File.cpp.

◆ getDatasetNumberOfDimensions()

size_t Hdf5File::getDatasetNumberOfDimensions ( const hid_t  parentGroup,
MatrixName datasetName 
)
Parameters
[in]parentGroup- Where the dataset is.
[in]datasetName- Dataset name.
Returns
Number of dimensions.
Exceptions
ios::failure- If error happens.

Get number of dimensions of the dataset under a specified group.

Definition at line 757 of file Hdf5File.cpp.

◆ getDatasetSize()

size_t Hdf5File::getDatasetSize ( const hid_t  parentGroup,
MatrixName datasetName 
)
Parameters
[in]parentGroup- Where the dataset is.
[in]datasetName- Dataset name.
Returns
Number of elements.
Exceptions
ios::failure- If error happens.

Get dataset element count at a specified place in the file tree.

Definition at line 775 of file Hdf5File.cpp.

◆ getRootGroup()

hid_t Hdf5File::getRootGroup ( ) const
inline

Get handle to the root group of the file.

Returns
Handle to the root group.

Definition at line 608 of file Hdf5File.h.

◆ isOpen()

bool Hdf5File::isOpen ( ) const
inline

Is the file opened?

Returns
true - If the file is opened.

Definition at line 560 of file Hdf5File.h.

◆ open()

void Hdf5File::open ( const std::string &  fileName,
unsigned int  flags = H5F_ACC_RDONLY 
)

The file is opened in read only mode by default. Other HDF5 flags are set to default.

Parameters
[in]fileName- File name
[in]flags- Open mode, by default read only.
Exceptions
ios:failure- If error happened (file not found, file is not a HDF5 file, file is already open).

Open the HDF5 file.

Definition at line 117 of file Hdf5File.cpp.

◆ openDataset()

hid_t Hdf5File::openDataset ( const hid_t  parentGroup,
MatrixName datasetName 
)

Other HDF5 flags are set to default.

Parameters
[in]parentGroup- Parent group id (can be the file id for root)..
[in]datasetName- Dataset name.
Returns
A handle to open dataset.
Exceptions
ios::failure- If error happens.

Open a dataset at a specified place in the file tree.

Definition at line 223 of file Hdf5File.cpp.

◆ openGroup()

hid_t Hdf5File::openGroup ( const hid_t  parentGroup,
MatrixName groupName 
)

Other HDF5 flags are set to default.

Parameters
[in]parentGroup- Parent group.
[in]groupName- Group name.
Returns
A handle to the group.
Exceptions
ios::failure- If error happens.

Open a HDF5 group at a specified place in the file tree.

Definition at line 196 of file Hdf5File.cpp.

◆ readCompleteDataset()

template<class T >
template void Hdf5File::readCompleteDataset< size_t > ( const hid_t  parentGroup,
MatrixName datasetName,
const DimensionSizes dimensionSizes,
T *  data 
)
Template Parameters
T- Data type to be written.
Parameters
[in]parentGroup- Where is the dataset situated.
[in]datasetName- Dataset name.
[in]dimensionSizes- Dimension sizes.
[out]data- Pointer to data.
Exceptions
ios::failure- If error happens.

Read data from the dataset at a specified place in the file tree, float version.

Read data from the dataset at a specified place in the file tree, float version explicit instance.

Read data from the dataset at a specified place in the file tree, index version explicit instance.

Definition at line 678 of file Hdf5File.cpp.

◆ readMatrixDataType()

Hdf5File::MatrixDataType Hdf5File::readMatrixDataType ( const hid_t  parentGroup,
MatrixName datasetName 
)
Parameters
[in]parentGroup- Where the dataset is.
[in]datasetName- Dataset name.
Returns
Matrix data type.
Exceptions
ios::failure- If error happens.

Read matrix data type from the dataset at a specified place in the file tree.

Definition at line 821 of file Hdf5File.cpp.

◆ readMatrixDomainType()

Hdf5File::MatrixDomainType Hdf5File::readMatrixDomainType ( const hid_t  parentGroup,
MatrixName datasetName 
)
Parameters
[in]parentGroup- Where the dataset is.
[in]datasetName- Dataset name.
Returns
Matrix domain type.
Exceptions
ios::failure- If error happens.

Read matrix dataset domain type at a specified place in the file tree.

Definition at line 848 of file Hdf5File.cpp.

◆ readScalarValue()

template<class T >
template void Hdf5File::readScalarValue< size_t > ( const hid_t  parentGroup,
MatrixName datasetName,
T &  value 
)
Template Parameters
T- Data type to be written.
Parameters
[in]parentGroup- Where to link the scalar dataset.
[in]datasetName- HDF5 dataset name.
[out]value- Data to be read.
Exceptions
ios::failure- If error happens.
Warning
Limited to float and size_t data types

Read the scalar value under a specified group.

Read the scalar value under a specified group, float value, explicit instance.

Read the scalar value under a specified group, float value, explicit instance

Definition at line 646 of file Hdf5File.cpp.

◆ readStringAttribute()

string Hdf5File::readStringAttribute ( const hid_t  parentGroup,
MatrixName datasetName,
MatrixName attributeName 
)
Parameters
[in]parentGroup- Where the dataset is.
[in]datasetName- Dataset name.
[in]attributeName- Attribute name.
Returns
Attribute value.
Exceptions
ios::failure- If error happens.

Read integer attribute at a specified place in the file tree.

Definition at line 892 of file Hdf5File.cpp.

◆ writeCuboidToHyperSlab()

void Hdf5File::writeCuboidToHyperSlab ( const hid_t  dataset,
const DimensionSizes hyperslabPosition,
const DimensionSizes cuboidPosition,
const DimensionSizes cuboidSize,
const DimensionSizes matrixDimensions,
const float *  matrixData 
)

The routine writes 3D cuboid into a 4D dataset (only intended for raw time series).

Parameters
[in]dataset- Dataset to write MatrixData into.
[in]hyperslabPosition- Position in the dataset (hyperslab) - may be 3D/4D.
[in]cuboidPosition- Position of the cuboid in MatrixData (what to sample) - must be 3D.
[in]cuboidSize- Cuboid size (size of data being sampled) - must by 3D.
[in]matrixDimensions- Size of the original matrix (the sampled one).
[in]matrixData- C array of matrix data.
Exceptions
ios::failure- If error happens.

Write a cuboid selected within the matrixData into a hyperslab.

Definition at line 431 of file Hdf5File.cpp.

◆ writeHyperSlab()

template<class T >
template void Hdf5File::writeHyperSlab< size_t > ( const hid_t  dataset,
const DimensionSizes position,
const DimensionSizes size,
const T *  data 
)
Template Parameters
T- Data type to be written.
Parameters
[in]dataset- Dataset id.
[in]position- Position in the dataset.
[in]size- Size of the hyperslab.
[in]data- Data to be written.
Exceptions
ios::failure- If error happens.
Warning
Limited to float and size_t data types.

Write a hyperslab into the dataset, float version.

Write a hyperslab into the dataset, float version explicit instance.

Write a hyperslab into the dataset, index version explicit instance.

Definition at line 335 of file Hdf5File.cpp.

◆ writeMatrixDataType()

void Hdf5File::writeMatrixDataType ( const hid_t  parentGroup,
MatrixName datasetName,
const MatrixDataType matrixDataType 
)
Parameters
[in]parentGroup- Where the dataset is.
[in]datasetName- Dataset name.
[in]matrixDataType- Matrix data type in the file.
Exceptions
ios::failure- If error happens.

Write matrix data type into the dataset at a specified place in the file tree.

Definition at line 793 of file Hdf5File.cpp.

◆ writeMatrixDomainType()

void Hdf5File::writeMatrixDomainType ( const hid_t  parentGroup,
MatrixName datasetName,
const MatrixDomainType matrixDomainType 
)
Parameters
[in]parentGroup- Where the dataset is.
[in]datasetName- Dataset name.
[in]matrixDomainType- Matrix domain type.
Exceptions
ios::failure- If error happens.

Write matrix data type into the dataset at a specified place in the file tree.

Definition at line 807 of file Hdf5File.cpp.

◆ writeScalarValue()

template<class T >
template void Hdf5File::writeScalarValue< size_t > ( const hid_t  parentGroup,
MatrixName datasetName,
const T  value 
)

No chunks and no compression is used.

Template Parameters
T- Data type to be written.
Parameters
[in]parentGroup- Where to link the scalar dataset.
[in]datasetName- HDF5 dataset name.
[in]value- data to be written.
Exceptions
ios::failure- If error happens.
Warning
Limited to float and size_t data types

Write a scalar value at a specified place in the file tree.

Write the scalar value under a specified group, float value explicit instance.

Write the scalar value under a specified group, index value explicit instance.

Definition at line 559 of file Hdf5File.cpp.

◆ writeSensorByMaskToHyperSlab()

void Hdf5File::writeSensorByMaskToHyperSlab ( const hid_t  dataset,
const DimensionSizes hyperslabPosition,
const size_t  indexSensorSize,
const size_t *  indexSensorData,
const DimensionSizes matrixDimensions,
const float *  matrixData 
)

Write sensor data selected by the sensor mask. The routine picks elements from the MatixData based on the Sensor Data and store them into a single hyperslab of size [Nsens, 1, 1]

Parameters
[in]dataset- Dataset to write MaatrixData into
[in]hyperslabPosition- 3D position in the dataset (hyperslab)
[in]indexSensorSize- Size of the index based sensor mask
[in]indexSensorData- Index based sensor mask
[in]matrixDimensions- Size of the sampled matrix
[in]matrixData- Matrix data
Warning
- very slow at this version of HDF5 for orthogonal planes-> DO NOT USE

Write sensor data selected by the sensor mask.

Definition at line 492 of file Hdf5File.cpp.

◆ writeStringAttribute()

void Hdf5File::writeStringAttribute ( const hid_t  parentGroup,
MatrixName datasetName,
MatrixName attributeName,
const std::string &  value 
)
Parameters
[in]parentGroup- Where the dataset is.
[in]datasetName- Dataset name.
[in]attributeName- Attribute name.
[in]value- Data to write.
Exceptions
ios::failure- If error happens.

Write integer attribute at a specified place in the file tree.

Definition at line 876 of file Hdf5File.cpp.


The documentation for this class was generated from the following files: