12#ifndef BELFEM_HDF5_TOOLS_HPP
13#define BELFEM_HDF5_TOOLS_HPP
40 hid_t mDataSet = H5I_INVALID_HID ;
41 hid_t mDataType = H5I_INVALID_HID ;
42 hid_t mDataSpace = H5I_INVALID_HID ;
44 DatasetHandles() = default ;
47 DatasetHandles(
const DatasetHandles & ) = delete ;
48 DatasetHandles & operator=(
const DatasetHandles & ) = delete ;
49 DatasetHandles( DatasetHandles && ) = delete ;
50 DatasetHandles & operator=( DatasetHandles && ) = delete ;
56 H5Sclose( mDataSpace );
60 H5Tclose( mDataType );
81 return H5Lexists( aLoc, aLabel.c_str(), tDataSet );
98 return H5Lexists( aLoc, aLabel.c_str(), tGroup );
119#if ! H5_VERSION_GE( 1, 12, 0 )
120#error "BELFEM requires HDF5 1.12 or newer ( hdf5_tools.hpp uses H5Literate2 and H5Oget_info_by_name3 )."
127 const H5L_info2_t * aInfo,
130 auto * tLabels =
static_cast<std::vector<std::string> *
>( aData );
138 if( aInfo !=
nullptr && aInfo->type != H5L_TYPE_HARD )
150 if( H5Oget_info_by_name3( aLoc, aLabel, & tInfo,
151 H5O_INFO_BASIC, H5P_DEFAULT ) < 0 )
158 if( tInfo.type != H5O_TYPE_GROUP )
163 tLabels->emplace_back( aLabel );
173 inline Cell< std::string >
185 const herr_t tStatus = H5Literate2(
198 "H5Literate2 failed while listing the groups of an HDF5 location." );
213 template <
typename T >
218 return H5Tget_size(
datatype<T>() ) ==
sizeof( T );
241 template <
typename T >
247 "datatype class mismatch reading '%s': the file holds class %i, but %s is class %i.\n"
248 "HDF5 can convert between widths, not between classes - this file does not hold what the reader expects.",
250 (
int ) H5Tget_class( aFileType ),
267 template <
typename T >
271 const std::string & aLabel,
278 "Dataset %s of type %s does already exist.",
283 "Error in datatype size of type %s.",
293 = H5Screate_simple( 1, tDims,
nullptr );
295 hid_t tDataSet = H5Dcreate(
312 H5Sclose( tDataSpace );
313 H5Tclose( tDataType );
314 H5Dclose( tDataSet );
317 "Something went wrong while trying to store scalar %s of type %s",
324 template <
typename T >
328 const std::string & aLabel,
335 "Dataset %s of type %s does not exist.",
340 "Error in datatype size of type %s.",
343 DatasetHandles tHandles ;
345 tHandles.mDataSet = H5Dopen1( aLoc, aLabel.c_str() );
347 tHandles.mDataType = H5Dget_type( tHandles.mDataSet );
353 tHandles.mDataSpace = H5Dget_space( tHandles.mDataSet );
364 "Something went wrong while trying to load scalar %s of type %s",
376 const std::string & aLabel,
385 const std::string & aLabel,
394 const std::string & aLabel,
395 const std::string & aValue,
403 const std::string & aLabel,
404 std::string & aValue,
408 template <
typename T >
412 const std::string & aLabel,
419 "Dataset %s of type Vector<%s> does already exist.",
424 "Error in datatype size of type %s.",
432 tDims[ 0 ] = aLength;
435 = H5Screate_simple( 1, tDims,
nullptr );
437 hid_t tDataSet = H5Dcreate(
450 H5Sclose( tDataSpace );
451 H5Tclose( tDataType );
452 BELFEM_ERROR(
false,
"Could not create dataset %s", aLabel.c_str() );
468 H5Sclose( tDataSpace );
469 H5Tclose( tDataType );
470 H5Dclose( tDataSet );
473 "Something went wrong while trying to store vector %s of type %s",
484 const std::string & aLabel );
488 template <
typename T >
492 const std::string & aLabel,
500 "Dataset %s of type Vector<%s> does not exist.",
505 "Error in datatype size of type %s.",
508 DatasetHandles tHandles ;
510 tHandles.mDataSet = H5Dopen1( aLoc, aLabel.c_str() );
512 tHandles.mDataType = H5Dget_type( tHandles.mDataSet );
518 tHandles.mDataSpace = H5Dget_space( tHandles.mDataSet );
524 const int tRank = H5Sget_simple_extent_ndims( tHandles.mDataSpace );
526 BELFEM_ERROR( tRank == 1,
"Dataset %s is not a 1-D array (rank %i)",
527 aLabel.c_str(), tRank );
529 aStatus = H5Sget_simple_extent_dims( tHandles.mDataSpace, tDims,
nullptr );
531 BELFEM_ERROR( tDims[ 0 ] == aLength,
"Lengths do not match: is %lu, expect %lu.",
532 (
long unsigned int ) tDims[ 0 ], (
long unsigned int ) aLength );
550 "Something went wrong while trying to load vector %s of type %s",
557 template <
typename T >
561 const std::string & aLabel,
568 "Dataset %s of type Vector<%s> does already exist.",
573 "Error in datatype size of type %s.",
583 tDims[ 0 ] = tLength;
586 = H5Screate_simple( 1, tDims,
nullptr );
588 tDataSet = H5Dcreate(
601 H5Sclose( tDataSpace );
602 H5Tclose( tDataType );
603 BELFEM_ERROR(
false,
"Could not create dataset %s", aLabel.c_str() );
610 T* tData = ( T* ) malloc( tLength *
sizeof( T ) );
615 tLength *
sizeof( T ) );
628 H5Sclose( tDataSpace );
629 H5Tclose( tDataType );
630 H5Dclose( tDataSet );
633 "Something went wrong while trying to store vector %s of type %s",
641 template <
typename T >
645 const std::string & aLabel,
651 "Dataset %s of type Vector<%s> does not exist.",
656 "Error in datatype size of type %s.",
659 DatasetHandles tHandles ;
661 tHandles.mDataSet = H5Dopen1( aLoc, aLabel.c_str() );
663 tHandles.mDataType = H5Dget_type( tHandles.mDataSet );
669 tHandles.mDataSpace = H5Dget_space( tHandles.mDataSet );
675 const int tRank = H5Sget_simple_extent_ndims( tHandles.mDataSpace );
677 BELFEM_ERROR( tRank == 1,
"Dataset %s is not a 1-D array (rank %i)",
678 aLabel.c_str(), tRank );
680 aStatus = H5Sget_simple_extent_dims( tHandles.mDataSpace, tDims,
nullptr );
688 T* tData = ( T* ) malloc( tLength *
sizeof( T ) );
701 tLength *
sizeof( T ) );
711 "Something went wrong while trying to load vector %s of type %s",
718 template <
typename T >
722 const std::string & aLabel,
725 const bool aTranspose =
false )
729 "Dataset %s of type Matrix<%s> does already exist.",
734 "Error in datatype size of type %s.",
746 tDims[ 0 ] = aMatrix.
n_cols();
747 tDims[ 1 ] = aMatrix.
n_rows();
752 tDims[ 0 ] = aMatrix.
n_rows();
753 tDims[ 1 ] = aMatrix.
n_cols();
757 = H5Screate_simple( 2, tDims,
nullptr );
759 tDataSet = H5Dcreate(
772 H5Sclose( tDataSpace );
773 H5Tclose( tDataType );
774 BELFEM_ERROR(
false,
"Could not create dataset %s", aLabel.c_str() );
779 if( tDims[ 0 ]*tDims[ 1 ] > 0 )
781 T** tData = ( T** ) malloc( tDims[ 0 ] *
sizeof( T * ) );
782 tData[ 0 ] = ( T* ) malloc( tDims[ 0 ]*tDims[ 1 ] *
sizeof( T ) );
784 for(
hsize_t i=0; i<tDims[ 0 ]; ++i )
786 tData[ i ] = tData[ 0 ]+ i*tDims[ 1 ];
791 for (
hsize_t i = 0; i < tDims[ 0 ]; ++i )
793 for (
hsize_t j = 0; j < tDims[ 1 ]; ++j )
795 tData[ i ][ j ] = aMatrix( j, i );
801 for (
hsize_t i = 0; i < tDims[ 0 ]; ++i )
803 for (
hsize_t j = 0; j < tDims[ 1 ]; ++j )
805 tData[ i ][ j ] = aMatrix( i, j );
821 H5Sclose( tDataSpace );
822 H5Tclose( tDataType );
823 H5Dclose( tDataSet );
826 "Something went wrong while trying to store matrix %s of type %s",
834 template <
typename T >
838 const std::string & aLabel,
841 const bool aTranspose =
false )
846 "Dataset %s of type Matrix<%s> does not exist.",
851 "Error in datatype size of type %s.",
854 DatasetHandles tHandles ;
856 tHandles.mDataSet = H5Dopen1( aLoc, aLabel.c_str() );
858 tHandles.mDataType = H5Dget_type( tHandles.mDataSet );
864 tHandles.mDataSpace = H5Dget_space( tHandles.mDataSet );
868 aStatus = H5Sget_simple_extent_dims( tHandles.mDataSpace, tDims,
nullptr );
873 aMatrix.
set_size( tDims[ 1 ], tDims[ 0 ] );
877 aMatrix.
set_size( tDims[ 0 ], tDims[ 1 ] );
880 if( tDims[ 0 ]*tDims[ 1 ] > 0 )
883 malloc( tDims[ 0 ]*
sizeof( T* ) );
886 malloc( tDims[ 0 ]* tDims[ 1 ] *
sizeof( T ) );
888 for(
hsize_t i=1; i<tDims[ 0 ]; ++i )
890 tData[ i ] = tData[ 0 ]+ i*tDims[ 1 ];
904 for (
hsize_t i = 0; i < tDims[ 0 ]; ++i )
906 for (
hsize_t j = 0; j < tDims[ 1 ]; ++j )
908 aMatrix( j, i ) = tData[ i ][ j ];
914 for (
hsize_t j = 0; j < tDims[ 1 ]; ++j )
916 for (
hsize_t i = 0; i < tDims[ 0 ]; ++i )
918 aMatrix( i, j ) = tData[ i ][ j ];
925 else if( aStatus == 2 )
931 "Something went wrong while trying to load matrix %s of type %s",
939 const std::string & aLabel,
945 DatasetHandles tHandles ;
948 hsize_t tDim[ 1 ] = { tNumStrings };
949 tHandles.mDataSpace = H5Screate_simple( 1, tDim, NULL );
951 tHandles.mDataType = H5Tcopy( H5T_C_S1 );
952 H5Tset_size( tHandles.mDataType, H5T_VARIABLE );
954 tHandles.mDataSet = H5Dcreate2(
963 BELFEM_ERROR( tHandles.mDataSet > -1,
"Error creating dataset" );
966 for (
hsize_t i=0; i<tNumStrings; ++i )
968 tBuffer( i ) = aCell( i ).c_str();
985 const std::string & aLabel,
993 "Dataset %s does not exist at expected location.", aLabel.c_str() );
995 DatasetHandles tHandles ;
997 tHandles.mDataSet = H5Dopen1( aLoc, aLabel.c_str() );
999 tHandles.mDataSpace = H5Dget_space( tHandles.mDataSet );
1005 const int tRank = H5Sget_simple_extent_ndims( tHandles.mDataSpace );
1007 BELFEM_ERROR( tRank == 1,
"Dataset %s is not a 1-D array (rank %i)",
1008 aLabel.c_str(), tRank );
1010 BELFEM_ERROR( H5Sget_simple_extent_dims( tHandles.mDataSpace, tDim,
nullptr ) > -1 ,
1011 "Error getting dataspace dimensions" );
1013 hsize_t tNumStrings = tDim[ 0 ];
1022 tHandles.mDataType = H5Tcopy( H5T_C_S1 );
1023 H5Tset_size( tHandles.mDataType, H5T_VARIABLE );
1025 hid_t tFileType = H5Dget_type( tHandles.mDataSet );
1026 H5Tset_cset( tHandles.mDataType, H5Tget_cset( tFileType ) );
1027 H5Tclose( tFileType );
1037 const herr_t tReadStatus = H5Dread( tHandles.mDataSet, tHandles.mDataType,
1038 H5S_ALL, H5S_ALL, H5P_DEFAULT, tBuffer.
data() );
1040 if( tReadStatus == 0 )
1044 for (
hsize_t i=0; i<tNumStrings; ++i )
1046 aCell( i ) = std::string( tBuffer( i ) );
1050 if( tNumStrings > 0 )
1052 H5Dvlen_reclaim( tHandles.mDataType, tHandles.mDataSpace, H5P_DEFAULT, tBuffer.
data() );
1055 BELFEM_ERROR( tReadStatus == 0,
"Failed to read strings from file." );
1075 "Group %s does not exist.",
1091 return H5Gclose( aGroup );
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
A bounds-checked wrapper around std::vector.
Definition cl_Cell.hpp:45
void set_size(const size_t aSize)
New elements are value-initialized ( zero for arithmetic T, nullptr for pointers ).
Definition cl_Cell.hpp:199
size_t size() const
Definition cl_Cell.hpp:189
void clear()
Empties the Cell.
Definition cl_Cell.hpp:249
std::vector< T > & vector_data()
The underlying vector.
Definition cl_Cell.hpp:146
T * data()
Returns a borrowed pointer into contiguous storage, possibly nullptr when the Cell is empty ( test si...
Definition cl_Cell.hpp:129
Dense column-major matrix.
Definition cl_BZ_Matrix.hpp:28
void set_size(const size_t aNumRows, const size_t aNumCols)
Definition cl_AR_Matrix.hpp:168
size_t n_rows() const
Definition cl_AR_Matrix.hpp:187
size_t n_cols() const
Definition cl_AR_Matrix.hpp:195
Column vector.
Definition cl_BZ_Vector.hpp:42
T * data()
Returns a borrowed raw buffer under the layout contract stated in cl_Vector.hpp; may be nullptr when ...
Definition cl_AR_Vector.hpp:261
void set_size(const size_t aNumRows)
change the size of the vector
Definition cl_AR_Vector.hpp:313
size_t length() const
get the length of the vector
Definition cl_AR_Vector.hpp:333
Definition cl_HDF5_Dataset.hpp:21
void save_vector_to_file(hid_t &aLoc, const std::string &aLabel, const Vector< T > &aVector, herr_t &aStatus)
Definition hdf5_tools.hpp:559
bool group_exists(hid_t aLoc, const std::string &aLabel)
test if a group exists ( tests for a link of that name; the link type is not checked )
Definition hdf5_tools.hpp:94
void save_string_to_file(hid_t &aFileID, const std::string &aLabel, const std::string &aValue, herr_t &aStatus)
Definition hdf5_tools.cpp:121
void load_scalar_from_file(hid_t &aLoc, const std::string &aLabel, T &aValue, herr_t &aStatus)
Definition hdf5_tools.hpp:326
void save_array_to_file(hid_t &aLoc, const std::string &aLabel, const T *aData, const hsize_t &aLength, herr_t &aStatus)
Definition hdf5_tools.hpp:410
void load_bool_from_file(hid_t &aFileID, const std::string &aLabel, bool &aValue, herr_t &aStatus)
Definition hdf5_tools.cpp:75
void save_strings_to_file(hid_t &aLoc, const std::string &aLabel, const Cell< string > &aCell, herr_t &aStatus)
Definition hdf5_tools.hpp:938
void save_scalar_to_file(hid_t &aLoc, const std::string &aLabel, const T &aValue, herr_t &aStatus)
saves a scalar value to a file file must be open
Definition hdf5_tools.hpp:269
hid_t open_group(const std::string &aLabel, hid_t aParent)
Definition hdf5_tools.hpp:1061
void load_strings_from_file(hid_t &aLoc, const std::string &aLabel, Cell< string > &aCell, herr_t &aStatus)
Definition hdf5_tools.hpp:984
void load_matrix_from_file(hid_t &aLoc, const std::string &aLabel, Matrix< T > &aMatrix, herr_t &aStatus, const bool aTranspose=false)
Definition hdf5_tools.hpp:836
void save_matrix_to_file(hid_t &aLoc, const std::string &aLabel, const Matrix< T > &aMatrix, herr_t &aStatus, const bool aTranspose=false)
Definition hdf5_tools.hpp:720
bool check_datatye_size()
this function returns true of both the HDF5 datatype and the passed datatype have the same size
Definition hdf5_tools.hpp:215
hid_t datatype()
Definition hdf5_types.hpp:36
Cell< std::string > get_groups(hid_t aLoc)
list groups that exist in location
Definition hdf5_tools.hpp:174
hid_t filetype()
Definition hdf5_types.hpp:56
hsize_t get_array_size(hid_t &aFileID, const std::string &aLabel)
Definition hdf5_tools.cpp:223
bool dataset_exists(hid_t aLoc, const std::string &aLabel)
test if a dataset exists ( tests for a link of that name; the link type is not checked )
Definition hdf5_tools.hpp:77
string create_tree(const Cell< string > &aTree, const string &aLabel)
Definition hdf5_tools.cpp:258
void load_vector_from_file(hid_t &aLoc, const std::string &aLabel, Vector< T > &aVector, herr_t &aStatus)
Definition hdf5_tools.hpp:643
void check_read_datatype(const hid_t aFileType, const std::string &aLabel)
Guard for every read: the FILE datatype must be convertible into T.
Definition hdf5_tools.hpp:243
void load_string_from_file(hid_t &aFileID, const std::string &aLabel, std::string &aValue, herr_t &aStatus)
Definition hdf5_tools.cpp:178
void load_array_from_file(hid_t &aLoc, const std::string &aLabel, T *aData, const hsize_t aLength, herr_t &aStatus)
Definition hdf5_tools.hpp:490
void save_bool_to_file(hid_t &aFileID, const std::string &aLabel, const bool &aValue, herr_t &aStatus)
Definition hdf5_tools.cpp:21
herr_t close_group(hid_t aGroup)
Definition hdf5_tools.hpp:1088
USER GUIDES:
Definition cl_Capacitor.cpp:16
std::string datatype_string()
Definition stringtools.hpp:64
int hsize_t
Definition hdf5_types.hpp:22
int hid_t
Definition hdf5_types.hpp:20
int herr_t
Definition hdf5_types.hpp:21