12#ifndef BELFEM_HDF5_TOOLS_HPP
13#define BELFEM_HDF5_TOOLS_HPP
41 return H5Lexists( aLoc, aLabel.c_str(), tDataSet );
58 return H5Lexists( aLoc, aLabel.c_str(), tGroup );
79#if ! H5_VERSION_GE( 1, 12, 0 )
80#error "BELFEM requires HDF5 1.12 or newer ( hdf5_tools.hpp uses H5Literate2 and H5Oget_info_by_name3 )."
87 const H5L_info2_t * aInfo,
90 auto * tLabels =
static_cast<std::vector<std::string> *
>( aData );
98 if( aInfo !=
nullptr && aInfo->type != H5L_TYPE_HARD )
110 if( H5Oget_info_by_name3( aLoc, aLabel, & tInfo,
111 H5O_INFO_BASIC, H5P_DEFAULT ) < 0 )
118 if( tInfo.type != H5O_TYPE_GROUP )
123 tLabels->emplace_back( aLabel );
133 inline Cell< std::string >
145 const herr_t tStatus = H5Literate2(
158 "H5Literate2 failed while listing the groups of an HDF5 location." );
174 template <
typename T >
179 return H5Tget_size(
datatype<T>() ) ==
sizeof( T );
207 template <
typename T >
213 "datatype class mismatch reading '%s': the file holds class %i, but %s is class %i.\n"
214 "HDF5 can convert between widths, not between classes - this file does not hold what the reader expects.",
216 (
int ) H5Tget_class( aFileType ),
233 template <
typename T >
237 const std::string & aLabel,
245 "Dataset %s of type %s does already exist.",
251 "Error in datatype size of type %s.",
263 = H5Screate_simple( 1, tDims,
nullptr );
266 hid_t tDataSet = H5Dcreate(
285 H5Sclose( tDataSpace );
286 H5Tclose( tDataType );
287 H5Dclose( tDataSet );
291 "Something went wrong while trying to store scalar %s of type %s",
298 template <
typename T >
302 const std::string & aLabel,
310 "Dataset %s of type %s does not exist.",
316 "Error in datatype size of type %s.",
320 hid_t tDataSet = H5Dopen1( aLoc, aLabel.c_str() );
323 hid_t tDataType = H5Dget_type( tDataSet );
330 hid_t tDataSpace = H5Dget_space( tDataSet );
342 H5Tclose( tDataType );
343 H5Dclose( tDataSet );
344 H5Sclose( tDataSpace );
348 "Something went wrong while trying to load scalar %s of type %s",
360 const std::string & aLabel,
369 const std::string & aLabel,
378 const std::string & aLabel,
379 const std::string & aValue,
387 const std::string & aLabel,
388 std::string & aValue,
392 template <
typename T >
396 const std::string & aLabel,
404 "Dataset %s of type Vector<%s> does already exist.",
410 "Error in datatype size of type %s.",
419 tDims[ 0 ] = aLength;
423 = H5Screate_simple( 1, tDims,
nullptr );
426 hid_t tDataSet = H5Dcreate(
449 H5Sclose( tDataSpace );
450 H5Tclose( tDataType );
451 H5Dclose( tDataSet );
455 "Something went wrong while trying to store vector %s of type %s",
466 const std::string & aLabel );
470 template <
typename T >
474 const std::string & aLabel,
483 "Dataset %s of type Vector<%s> does not exist.",
489 "Error in datatype size of type %s.",
493 hid_t tDataSet = H5Dopen1( aLoc, aLabel.c_str() );
496 hid_t tDataType = H5Dget_type( tDataSet );
503 hid_t tDataSpace = H5Dget_space( tDataSet );
509 aStatus = H5Sget_simple_extent_dims( tDataSpace, tDims,
nullptr );
512 BELFEM_ERROR( tDims[ 0 ] == aLength,
"Lengths do not match: is %lu, expect %lu.",
513 (
long unsigned int ) tDims[ 0 ], (
long unsigned int ) aLength );
534 H5Tclose( tDataType );
535 H5Dclose( tDataSet );
536 H5Sclose( tDataSpace );
540 "Something went wrong while trying to load vector %s of type %s",
547 template <
typename T >
551 const std::string & aLabel,
559 "Dataset %s of type Vector<%s> does already exist.",
565 "Error in datatype size of type %s.",
576 tDims[ 0 ] = tLength;
580 = H5Screate_simple( 1, tDims,
nullptr );
583 tDataSet = H5Dcreate(
596 T* tData = ( T* ) malloc( tLength *
sizeof( T ) );
602 tLength *
sizeof( T ) );
618 H5Sclose( tDataSpace );
619 H5Tclose( tDataType );
620 H5Dclose( tDataSet );
624 "Something went wrong while trying to store vector %s of type %s",
632 template <
typename T >
636 const std::string & aLabel,
643 "Dataset %s of type Vector<%s> does not exist.",
649 "Error in datatype size of type %s.",
653 hid_t tDataSet = H5Dopen1( aLoc, aLabel.c_str() );
656 hid_t tDataType = H5Dget_type( tDataSet );
663 hid_t tDataSpace = H5Dget_space( tDataSet );
669 aStatus = H5Sget_simple_extent_dims( tDataSpace, tDims,
nullptr );
681 T* tData = ( T* ) malloc( tLength *
sizeof( T ) );
696 tLength *
sizeof( T ) );
708 H5Tclose( tDataType );
709 H5Dclose( tDataSet );
710 H5Sclose( tDataSpace );
714 "Something went wrong while trying to load vector %s of type %s",
721 template <
typename T >
725 const std::string & aLabel,
728 const bool aTranspose =
false )
733 "Dataset %s of type Matrix<%s> does already exist.",
739 "Error in datatype size of type %s.",
752 tDims[ 0 ] = aMatrix.
n_cols();
753 tDims[ 1 ] = aMatrix.
n_rows();
758 tDims[ 0 ] = aMatrix.
n_rows();
759 tDims[ 1 ] = aMatrix.
n_cols();
764 = H5Screate_simple( 2, tDims,
nullptr );
767 tDataSet = H5Dcreate(
777 if( tDims[ 0 ]*tDims[ 1 ] > 0 )
780 T** tData = ( T** ) malloc( tDims[ 0 ] *
sizeof( T * ) );
781 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 );
802 for (
hsize_t i = 0; i < tDims[ 0 ]; ++i )
804 for (
hsize_t j = 0; j < tDims[ 1 ]; ++j )
806 tData[ i ][ j ] = aMatrix( i, j );
825 H5Sclose( tDataSpace );
826 H5Tclose( tDataType );
827 H5Dclose( tDataSet );
831 "Something went wrong while trying to store matrix %s of type %s",
839 template <
typename T >
843 const std::string & aLabel,
846 const bool aTranspose =
false )
852 "Dataset %s of type Matrix<%s> does not exist.",
858 "Error in datatype size of type %s.",
863 hid_t tDataSet = H5Dopen1( aLoc, aLabel.c_str() );
866 hid_t tDataType = H5Dget_type( tDataSet );
875 "Dataset %s does not seem to be %s.",
880 hid_t tDataSpace = H5Dget_space( tDataSet );
886 aStatus = H5Sget_simple_extent_dims( tDataSpace, tDims,
nullptr );
891 aMatrix.
set_size( tDims[ 1 ], tDims[ 0 ] );
895 aMatrix.
set_size( tDims[ 0 ], tDims[ 1 ] );
899 if( tDims[ 0 ]*tDims[ 1 ] > 0 )
903 malloc( tDims[ 0 ]*
sizeof( T* ) );
907 malloc( tDims[ 0 ]* tDims[ 1 ] *
sizeof( T ) );
910 for(
hsize_t i=1; i<tDims[ 0 ]; ++i )
912 tData[ i ] = tData[ 0 ]+ i*tDims[ 1 ];
929 for (
hsize_t i = 0; i < tDims[ 0 ]; ++i )
931 for (
hsize_t j = 0; j < tDims[ 1 ]; ++j )
933 aMatrix( j, i ) = tData[ i ][ j ];
939 for (
hsize_t j = 0; j < tDims[ 1 ]; ++j )
941 for (
hsize_t i = 0; i < tDims[ 0 ]; ++i )
943 aMatrix( i, j ) = tData[ i ][ j ];
951 else if( aStatus == 2 )
958 H5Tclose( tDataType );
959 H5Dclose( tDataSet );
960 H5Sclose( tDataSpace );
964 "Something went wrong while trying to load matrix %s of type %s",
972 const std::string & aLabel,
980 hsize_t tDim[ 1 ] = { tNumStrings };
981 hid_t tDataSpace = H5Screate_simple( 1, tDim, NULL );
984 hid_t tType = H5Tcopy( H5T_C_S1 );
985 H5Tset_size( tType, H5T_VARIABLE );
988 hid_t tDataset = H5Dcreate2(
997 BELFEM_ERROR( tDataset > -1,
"Error creating dataset" );
1001 for (
hsize_t i=0; i<tNumStrings; ++i )
1003 tBuffer( i ) = aCell( i ).c_str();
1016 H5Dclose( tDataset );
1018 H5Sclose( tDataSpace );
1024 const std::string & aLabel,
1033 "Dataset %s does not exist at expected location.", aLabel.c_str() );
1036 hid_t tDataset = H5Dopen1( aLoc, aLabel.c_str() );
1039 hid_t tDataSpace = H5Dget_space( tDataset );
1043 BELFEM_ERROR( H5Sget_simple_extent_dims( tDataSpace, tDim,
nullptr ) > -1 ,
1044 "Error getting dataspace dimensions" );
1047 hsize_t tNumStrings = tDim[ 0 ];
1057 hid_t tType = H5Tcopy( H5T_C_S1 );
1058 H5Tset_size( tType, H5T_VARIABLE );
1060 hid_t tFileType = H5Dget_type( tDataset );
1061 H5Tset_cset( tType, H5Tget_cset( tFileType ) );
1062 H5Tclose( tFileType );
1066 char ** tBuffer =
new char *[ tNumStrings ];
1069 BELFEM_ERROR( H5Dread( tDataset, tType, H5S_ALL, H5S_ALL, H5P_DEFAULT, tBuffer ) == 0,
1070 "Failed to read strings from file." );
1076 for (
hsize_t i=0; i<tNumStrings; ++i )
1078 aCell( i ) = std::string( tBuffer[ i ] );
1082 H5Dvlen_reclaim( tType, tDataSpace, H5P_DEFAULT, tBuffer );
1086 H5Sclose( tDataSpace );
1087 H5Dclose( tDataset );
1109 "Group %s does not exist.",
1125 return H5Gclose( aGroup );
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
Cell is a wrapper around the standard vector.
Definition cl_Cell.hpp:42
void set_size(const size_t aSize)
Definition cl_Cell.hpp:189
size_t size() const
return the size of the Cell
Definition cl_Cell.hpp:181
void clear()
clear the memory
Definition cl_Cell.hpp:240
std::vector< T > & vector_data()
Definition cl_Cell.hpp:135
T * data()
Definition cl_Cell.hpp:120
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:186
size_t n_rows() const
Definition cl_AR_Matrix.hpp:205
size_t n_cols() const
Definition cl_AR_Matrix.hpp:213
Column vector.
Definition cl_BZ_Vector.hpp:41
T * data()
expose the underlying raw pointer ( writable version )
Definition cl_AR_Vector.hpp:182
void set_size(const size_t aNumRows)
change the size of the vector
Definition cl_AR_Vector.hpp:237
size_t length() const
get the length of the vector
Definition cl_AR_Vector.hpp:257
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:549
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:54
void save_string_to_file(hid_t &aFileID, const std::string &aLabel, const std::string &aValue, herr_t &aStatus)
Definition hdf5_tools.cpp:140
void load_scalar_from_file(hid_t &aLoc, const std::string &aLabel, T &aValue, herr_t &aStatus)
Definition hdf5_tools.hpp:300
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:394
void load_bool_from_file(hid_t &aFileID, const std::string &aLabel, bool &aValue, herr_t &aStatus)
Definition hdf5_tools.cpp:83
void save_strings_to_file(hid_t &aLoc, const std::string &aLabel, const Cell< string > &aCell, herr_t &aStatus)
Definition hdf5_tools.hpp:971
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:235
hid_t open_group(const std::string &aLabel, hid_t aParent)
Definition hdf5_tools.hpp:1093
void load_strings_from_file(hid_t &aLoc, const std::string &aLabel, Cell< string > &aCell, herr_t &aStatus)
Definition hdf5_tools.hpp:1023
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:841
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:723
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:176
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:134
hid_t filetype()
Definition hdf5_types.hpp:56
hsize_t get_array_size(hid_t &aFileID, const std::string &aLabel)
Definition hdf5_tools.cpp:264
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:37
string create_tree(const Cell< string > &aTree, const string &aLabel)
Definition hdf5_tools.cpp:305
void load_vector_from_file(hid_t &aLoc, const std::string &aLabel, Vector< T > &aVector, herr_t &aStatus)
Definition hdf5_tools.hpp:634
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:209
void load_string_from_file(hid_t &aFileID, const std::string &aLabel, std::string &aValue, herr_t &aStatus)
Definition hdf5_tools.cpp:206
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:472
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:1122
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