43 #pragma comment(lib, "Ws2_32.lib") 74 : mFile(H5I_BADID), mFileName(
"")
105 mFile = H5Fcreate(fileName.c_str(), flags, H5P_DEFAULT, H5P_DEFAULT);
120 const char* cFileName = fileName.c_str();
129 if (H5Fis_hdf5(cFileName) == 0)
134 mFile = H5Fopen(cFileName, flags, H5P_DEFAULT);
149 return (access(fileName.c_str(), F_OK) == 0);
153 return (_access_s(fileName.c_str(), 0) == 0 );
164 herr_t status = H5Fclose(
mFile);
181 hid_t group = H5Gcreate(parentGroup, groupName.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
184 if (group == H5I_INVALID_HID)
199 hid_t group = H5Gopen(parentGroup, groupName.c_str(), H5P_DEFAULT);
202 if (group == H5I_INVALID_HID)
227 hid_t dataset = H5Dopen(parentGroup, datasetName.c_str(), H5P_DEFAULT);
229 if (dataset == H5I_INVALID_HID)
246 const size_t compressionLevel)
248 const int rank = (dimensionSizes.
is3D()) ? 3 : 4;
254 if (dimensionSizes.
is3D())
256 dims[0] = dimensionSizes.
nz;
257 dims[1] = dimensionSizes.
ny;
258 dims[2] = dimensionSizes.
nx;
260 chunk[0] = chunkSizes.
nz;
261 chunk[1] = chunkSizes.
ny;
262 chunk[2] = chunkSizes.
nx;
266 dims[0] = dimensionSizes.
nt;
267 dims[1] = dimensionSizes.
nz;
268 dims[2] = dimensionSizes.
ny;
269 dims[3] = dimensionSizes.
nx;
271 chunk[0] = chunkSizes.
nt;
272 chunk[1] = chunkSizes.
nz;
273 chunk[2] = chunkSizes.
ny;
274 chunk[3] = chunkSizes.
nx;
280 hid_t dataspace = H5Screate_simple(rank, dims, NULL);
283 propertyList = H5Pcreate(H5P_DATASET_CREATE);
285 status = H5Pset_chunk(propertyList, rank, chunk);
292 status = H5Pset_deflate(propertyList, static_cast<unsigned int>(compressionLevel));
304 hid_t dataset = H5Dcreate(parentGroup,
312 if (dataset == H5I_INVALID_HID)
317 H5Pclose(propertyList);
341 hid_t filespace, memspace;
344 filespace = H5Dget_space(dataset);
345 const int rank = H5Sget_simple_extent_ndims(filespace);
354 nElement[0] = size.
nz;
355 nElement[1] = size.
ny;
356 nElement[2] = size.
nx;
358 offset[0] = position.
nz;
359 offset[1] = position.
ny;
360 offset[2] = position.
nx;
364 nElement[0] = size.
nt;
365 nElement[1] = size.
nz;
366 nElement[2] = size.
ny;
367 nElement[3] = size.
nx;
369 offset[0] = position.
nt;
370 offset[1] = position.
nz;
371 offset[2] = position.
ny;
372 offset[3] = position.
nx;
376 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, nElement, NULL);
383 memspace = H5Screate_simple(rank, nElement, NULL);
389 if (std::is_same<T, size_t>())
391 status = H5Dwrite(dataset, H5T_STD_U64LE, memspace, filespace, H5P_DEFAULT, data);
393 if (std::is_same<T, float>())
395 status = H5Dwrite(dataset, H5T_NATIVE_FLOAT, memspace, filespace, H5P_DEFAULT, data);
412 void Hdf5File::writeHyperSlab<float>(
const hid_t dataset,
421 void Hdf5File::writeHyperSlab<size_t>(
const hid_t dataset,
436 const float* matrixData)
439 hid_t filespace, memspace;
441 constexpr
int rank = 4;
445 hsize_t slabSize[rank] = {1, cuboidSize.
nz, cuboidSize.
ny, cuboidSize.
nx};
446 hsize_t offsetInDataset[rank] = {hyperslabPosition.
nt,
447 hyperslabPosition.
nz,
448 hyperslabPosition.
ny,
449 hyperslabPosition.
nx};
450 hsize_t offsetInMatrixData[] = {cuboidPosition.
nz, cuboidPosition.
ny, cuboidPosition.
nx};
451 hsize_t matrixSize [] = {matrixDimensions.
nz, matrixDimensions.
ny, matrixDimensions.
nx};
455 filespace = H5Dget_space(dataset);
456 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsetInDataset, NULL, slabSize, NULL);
464 memspace = H5Screate_simple(3, matrixSize, NULL);
465 status = H5Sselect_hyperslab(memspace,
477 status = H5Dwrite(dataset, H5T_NATIVE_FLOAT, memspace, filespace, H5P_DEFAULT, matrixData);
494 const size_t indexSensorSize,
495 const size_t* indexSensorData,
497 const float* matrixData)
500 hid_t filespace, memspace;
502 constexpr
int rank = 3;
506 hsize_t slabSize[rank] = {1, 1, indexSensorSize};
507 hsize_t offsetInDataset[rank] = {hyperslabPosition.
nz, hyperslabPosition.
ny, hyperslabPosition.
nx };
510 hsize_t matrixSize = matrixDimensions.
nz * matrixDimensions.
ny * matrixDimensions.
nx;
514 filespace = H5Dget_space(dataset);
515 status = H5Sselect_hyperslab(filespace,
527 memspace = H5Screate_simple(1, &matrixSize, NULL);
528 status = H5Sselect_elements(memspace,
531 ( hsize_t *) (indexSensorData));
538 status = H5Dwrite(dataset,
563 constexpr
int rank = 3;
564 const hsize_t dims[] = {1, 1, 1};
566 hid_t dataset = H5I_INVALID_HID;
567 hid_t dataspace = H5I_INVALID_HID;
568 hid_t datatype = H5I_INVALID_HID;
571 if (std::is_same<T, float>())
573 datatype = H5T_NATIVE_FLOAT;
576 if (std::is_same<T, size_t>())
578 datatype = H5T_STD_U64LE;
581 const char* cDatasetName = datasetName.c_str();
582 if (H5LTfind_dataset(parentGroup, cDatasetName) == 1)
588 dataspace = H5Screate_simple(rank, dims, NULL);
589 dataset = H5Dcreate(parentGroup,
599 if (dataset == H5I_INVALID_HID)
604 status = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &value);
610 if (std::is_same<T, float>())
614 if (std::is_same<T, size_t>())
627 void Hdf5File::writeScalarValue<float>
628 (
const hid_t parentGroup,
636 void Hdf5File::writeScalarValue<size_t>
637 (
const hid_t parentGroup,
658 void Hdf5File::readScalarValue<float>
659 (
const hid_t parentGroup,
668 void Hdf5File::readScalarValue<size_t>
669 (
const hid_t parentGroup,
682 {
const char* cDatasetName = datasetName.c_str();
690 if (std::is_same<T, float>())
692 status = H5LTread_dataset(parentGroup, cDatasetName, H5T_NATIVE_FLOAT, data);
694 if (std::is_same<T, size_t>())
696 status = H5LTread_dataset(parentGroup, cDatasetName, H5T_STD_U64LE, data);
710 void Hdf5File::readCompleteDataset<float>
711 (
const hid_t parentGroup,
720 void Hdf5File::readCompleteDataset<size_t>
721 (
const hid_t parentGroup,
735 hsize_t dims[4] = { 0, 0, 0, 0};
737 herr_t status = H5LTget_dataset_info(parentGroup, datasetName.c_str(), dims, NULL, NULL);
762 herr_t status = H5LTget_dataset_ndims(parentGroup, datasetName.c_str(), &dims);
778 hsize_t dims[3] = {0, 0, 0};
780 herr_t status = H5LTget_dataset_info(parentGroup, datasetName.c_str(), dims, NULL, NULL);
786 return dims[0] * dims[1] * dims[2];
838 paramValue.c_str()));
865 paramValue.c_str()));
881 herr_t status = H5LTset_attribute_string(parentGroup, datasetName.c_str(), attributeName.c_str(), value.c_str());
896 char value[256] =
"";
897 herr_t status = H5LTget_attribute_string(parentGroup, datasetName.c_str(), attributeName.c_str(), value);
void writeStringAttribute(const hid_t parentGroup, MatrixName &datasetName, MatrixName &attributeName, const std::string &value)
Write string attribute into the dataset under the root group.
hid_t openDataset(const hid_t parentGroup, MatrixName &datasetName)
Open a dataset at a specified place in the file tree.
ErrorMessage kErrFmtCannotCloseFile
HDF5 error message.
void create(const std::string &fileName, unsigned int flags=H5F_ACC_TRUNC)
Create the HDF5 file.
void writeScalarValue(const hid_t parentGroup, MatrixName &datasetName, const T value)
Write the scalar value under a specified group.
static const std::string kMatrixDomainTypeName
String representation of the Domain type in the HDF5 file.
ErrorMessage kErrFmtBadDimensionSizes
HDF5 error message.
size_t nz
Number of elements in the z direction.
ErrorMessage kErrFmtCannotReopenFile
HDF5 error message.
virtual ~Hdf5File()
Destructor.
ErrorMessage kErrFmtCannotReadDataset
HDF5 error message.
hid_t openGroup(const hid_t parentGroup, MatrixName &groupName)
Open a HDF5 group at a specified place in the file tree.
void open(const std::string &fileName, unsigned int flags=H5F_ACC_RDONLY)
Open the HDF5 file.
The header file containing the HDF5 related classes.
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.
size_t getDatasetNumberOfDimensions(const hid_t parentGroup, MatrixName &datasetName)
Get number of dimensions of the dataset under a specified group.
ErrorMessage kErrFmtCannotCreateGroup
HDF5 error message.
DimensionSizes getDatasetDimensionSizes(const hid_t parentGroup, MatrixName &datasetName)
Get dimension sizes of the dataset under a specified group.
std::string mFileName
File name.
hid_t mFile
HDF file handle.
ErrorMessage kErrFmtCannotRecreateFile
HDF5 error message.
ErrorMessage kErrFmtBadAttributeValue
HDF5 error message.
static bool canAccess(const std::string &fileName)
Can I access the file.
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! ...
The header file containing the parameters of the simulation.
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.
void closeDataset(const hid_t dataset)
Close dataset.
void readScalarValue(const hid_t parentGroup, MatrixName &datasetName, T &value)
Read the scalar value under a specified group.
size_t nt
Number of time steps (for time series datasets).
size_t getDatasetSize(const hid_t parentGroup, MatrixName &datasetName)
Get dataset element count at a specified place in the file tree.
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.
ErrorMessage kErrFmtCannotOpenGroup
HDF5 error message.
std::string readStringAttribute(const hid_t parentGroup, MatrixName &datasetName, MatrixName &attributeName)
Read string attribute from the dataset under the root group.
hid_t createGroup(const hid_t parentGroup, MatrixName &groupName)
Create a HDF5 group at a specified place in the file tree.
ErrorMessage kErrFmtCannotReadAttribute
HDF5 error message.
The header file containing a class responsible for printing out info and error messages (stdout...
bool is3D() const
Does the object include spatial dimensions only?
ErrorMessage kErrFmtCannotSetCompression
HDF5 error message.
static std::string formatMessage(const std::string &format, Args ... args)
C++-11 replacement for sprintf that works with std::string instead of char*.
void writeHyperSlab(const hid_t dataset, const DimensionSizes &position, const DimensionSizes &size, const T *data)
Write a hyperslab into the dataset.
const std::string MatrixName
Datatype for matrix names.
Structure with 4D dimension sizes (3 in space and 1 in time).
The matrix is stored in floating point 32b wide format.
ErrorMessage kErrFmtCannotWriteDataset
HDF5 error message.
MatrixDataType readMatrixDataType(const hid_t parentGroup, MatrixName &datasetName)
Read matrix data type from the dataset at a specified place in the file tree.
The matrix is stored in fixed point point 64b wide format.
static const std::string kMatrixDataTypeNames[]
String representation of different data types.
ErrorMessage kErrFmtCannotCreateFile
HDF5 error message.
size_t ny
Number of elements in the y direction.
void closeGroup(const hid_t group)
Close a group.
size_t nElements() const
Get the number of elements, in 3D only spatial domain, in 4D with time.
void close()
Close the HDF5 file.
MatrixDomainType
HDF5 Matrix domain type (real or complex).
ErrorMessage kErrFmtFileNotOpen
HDF5 error message.
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.
static const std::string kMatrixDataTypeName
String representation of the Data type in the HDF5 file.
static const std::string kMatrixDomainTypeNames[]
String representation of different domain types.
MatrixDataType
HDF5 matrix data type (float or uint64).
ErrorMessage kErrFmtCannotWriteAttribute
HDF5 error message.
size_t nx
Number of elements in the x direction.
Hdf5File()
Constructor of the class.
ErrorMessage kErrFmtNotHdf5File
HDF5 error message.
ErrorMessage kErrFmtCannotOpenDataset
HDF5 error message.
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).
bool isOpen() const
Is the file opened?
MatrixDomainType readMatrixDomainType(const hid_t parentGroup, MatrixName &datasetName)
Read matrix dataset domain type at a specified place in the file tree.
The matrix is defined on real domain.