CFEL - ASG Software Suite  2.5.0
CASS
Public Member Functions | Private Member Functions | Private Attributes | List of all members
hdf5::Handler Class Reference

A handler for h5 files. More...

#include <hdf5_handle.hpp>

Public Member Functions

 Handler ()
 default constructor More...
 
 Handler (const std::string &filename, const std::string &mode="w")
 constructor opening the file More...
 
 ~Handler ()
 destructor More...
 
void close ()
 close the file used by this handler More...
 
void open (const std::string &filename, const std::string &mode="w", int size=-1)
 open a file More...
 
template<typename type >
void writeScalar (const type value, const std::string &valname)
 write an scalar value with a given name as part of a given group More...
 
template<typename type >
type readScalar (const std::string &valname)
 read an scalar value with a given name as part of a given group More...
 
template<typename type >
void writeArray (const std::vector< type > &array, const size_t arrayLength, const std::string &valname, int compressLevel=2)
 write a 1d array with a given name More...
 
template<typename type >
void readArray (std::vector< type > &array, size_t &arrayLength, const std::string &valname)
 read a array with a given name into a linearized array More...
 
template<typename type >
void writeMatrix (const std::vector< type > &matrix, std::pair< size_t, size_t > shape, const std::string &valname, int compressLevel=2)
 write a linearized matrix with a given name More...
 
template<typename type >
void readMatrix (std::vector< type > &matrix, std::pair< size_t, size_t > &shape, const std::string &valname)
 read a matrix with a given name into a linearized array More...
 
template<typename type >
void appendData (const std::vector< type > &data, shape_t shape, const std::string &valname, int compressLevel=2)
 create/append data to a multidimensional dataset More...
 
template<typename type >
void readMultiDim (std::vector< type > &data, shape_t &shape, const std::string &valname)
 read a multidimensional dataset with a given name into a linearized array More...
 
template<typename type >
void readPartialMultiDim (typename std::vector< type >::iterator data, const partiality_t &part, const std::string &valname)
 partially read a multidimensional dataset with a given name into a linearized array More...
 
void writeString (const std::string &string, const std::string &dsetName)
 write a string dataset More...
 
std::string readString (const std::string &dsetName)
 read a string dataset More...
 
template<typename type >
void writeScalarAttribute (const type value, const std::string &valname, const std::string &dsetName)
 write an float scalar attribute with a given name as part of a given dataset More...
 
template<typename type >
type readScalarAttribute (const std::string &valname, const std::string &dsetName)
 read an scalar attribute with a given name as part of a given dataset More...
 
std::string filename () const
 retrieve the filename of the file associated with the handler More...
 
shape_t shape (const std::string &valname) const
 get the shape of a dataset with a given name More...
 
size_t dimension (const std::string &valname) const
 get the dimension of a value with a given name More...
 
dsetList_t datasets () const
 get the list of datasets in the file More...
 
dsetList_t rootGroups () const
 get the list of groups of the root group in the file More...
 
size_t currentFileSize () const
 retrieve the size of the current file More...
 

Private Member Functions

 Handler (const Handler &)
 prevent copy construction More...
 
std::string reopenFile (int &filehandle, size_t maxsize, const std::string &currentfilename)
 check filesize and open new file if too big More...
 
bool groupExists (const std::string &groupname)
 check if a groups exists More...
 
void ensureGroupExists (const std::string &name)
 make sure that the requested group for the datset exists More...
 

Private Attributes

hid_t _fileid
 the file handle More...
 

Detailed Description

A handler for h5 files.

Author
Lutz Foucar

Definition at line 167 of file hdf5_handle.hpp.

Constructor & Destructor Documentation

hdf5::Handler::Handler ( const Handler )
inlineprivate

prevent copy construction

Definition at line 171 of file hdf5_handle.hpp.

hdf5::Handler::Handler ( )
inline

default constructor

Definition at line 177 of file hdf5_handle.hpp.

hdf5::Handler::Handler ( const std::string &  filename,
const std::string &  mode = "w" 
)
inline

constructor opening the file

Parameters
modeOpen the file in read "r" or write "w" mode. Default is "w"
filenamethe name of the file to open

Definition at line 184 of file hdf5_handle.hpp.

References open().

hdf5::Handler::~Handler ( )
inline

destructor

flushes and cloeses the file if it is open

Definition at line 194 of file hdf5_handle.hpp.

References close().

Member Function Documentation

template<typename type >
void hdf5::Handler::appendData ( const std::vector< type > &  data,
shape_t  shape,
const std::string &  valname,
int  compressLevel = 2 
)
inline

create/append data to a multidimensional dataset

The name can contain the group that the value should be written to

Template Parameters
typeThe type that should be written
Parameters
datathe matrix to be written
shapethe shape of the matrix (first is cols, second is rows)
valnamethe name of the value
compressLevelthe compression level of the matrix

turn off error output

ensure that the goup where the data will be appended to exists

get the dataset and check if the dataset exists

the dataset seems to not exist, so create it

create the file dataspace, where the first dim is unlimited

create the property list to be chunked

create the dataset

close the resources

get the filespace of the dataset to extract the current dimensions

get the nbr of dimensions and the dimensions of the dataset

release the old dataspace of the dataset, later we get the new extended filespace of the dataset

check if the dimensions fit the to be appended datas dimensions

create space in memory for the new data of the dataset

extent the dataset so that it can incldue the newly added data

get the filespace of the extended dataset

create hyperslab on the file dataspace where the data will be written to later on

Definition at line 572 of file hdf5_handle.hpp.

References _fileid, ensureGroupExists(), offset, and status.

void hdf5::Handler::close ( )
inline

close the file used by this handler

Definition at line 202 of file hdf5_handle.hpp.

References _fileid.

Referenced by ~Handler().

size_t hdf5::Handler::currentFileSize ( ) const
inline

retrieve the size of the current file

Returns
the size of the file

Definition at line 1189 of file hdf5_handle.hpp.

References _fileid, and status.

dsetList_t hdf5::Handler::datasets ( ) const
inline

get the list of datasets in the file

Returns
list of strings that point to valid datasets

Definition at line 1154 of file hdf5_handle.hpp.

References _fileid, hdf5::dataset_iterator_func(), and status.

size_t hdf5::Handler::dimension ( const std::string &  valname) const
inline

get the dimension of a value with a given name

retrieve the dataset with the given name, then the dataspace for the dataset. Judge by the type and the number of dimension what the dimensions are.

Returns
the dimension of the value
Parameters
valnamethe name of the value

Definition at line 1072 of file hdf5_handle.hpp.

References _fileid.

Referenced by cass::HDF5FileInput::runthis().

void hdf5::Handler::ensureGroupExists ( const std::string &  name)
inlineprivate

make sure that the requested group for the datset exists

strip off the datasetname and check if the group exists. If it doesn't create the group once. Do this with each subgroup until the full path is checked.

Parameters
nameabsolute path containing also the datasetname

prepend a '/' if its not there to ensure that it is a absolute path

Definition at line 1274 of file hdf5_handle.hpp.

References _fileid, and groupExists().

Referenced by appendData(), writeArray(), writeMatrix(), and writeScalar().

std::string hdf5::Handler::filename ( ) const
inline

retrieve the filename of the file associated with the handler

Returns
the name of the file

determine the size of the filename (which is the return value of the query function

allocate a vector of chars to hold the filanme

and retrieve the filename

use the iterator constructor of string to convert the vector of chars to string

Definition at line 1005 of file hdf5_handle.hpp.

References _fileid, and status.

Referenced by open(), and readPartialMultiDim().

bool hdf5::Handler::groupExists ( const std::string &  groupname)
inlineprivate

check if a groups exists

for now just checks if an absolute path exists in the file. Need to turn off error output, since the command will issue an error when the group does not exist.

Todo:
iterate through everthing to get rid of the disabling of the error messaging
Returns
true when the group exists, false otherwise
Parameters
groupnamethe name of the group in absolute path

Definition at line 1259 of file hdf5_handle.hpp.

References _fileid.

Referenced by ensureGroupExists().

void hdf5::Handler::open ( const std::string &  filename,
const std::string &  mode = "w",
int  size = -1 
)
inline

open a file

Exceptions
logic_errorwhen file could not be opened
Parameters
filenamethe name of the file to be opened
modeOpen the file in read "r", write "w" or in read write "rw" mode. Default is "w"
sizethe size of the cache for chuncked data in MB

Definition at line 221 of file hdf5_handle.hpp.

References _fileid, filename(), and size.

Referenced by Handler().

template<typename type >
void hdf5::Handler::readArray ( std::vector< type > &  array,
size_t &  arrayLength,
const std::string &  valname 
)
inline

read a array with a given name into a linearized array

reads a array from the h5 file. The dimensions of the matrix will be returned in the arrayLength and the vector will be resized to fit the data before copying the data into the vector.

Template Parameters
typeThe type that should be read
Parameters
[out]arraythe array that will be read
[out]arrayLengththe length of the array
[in]valnamethe name of the value

turn off error output

Definition at line 405 of file hdf5_handle.hpp.

References _fileid, and status.

Referenced by cass::HDF5FileInput::runthis().

template<typename type >
void hdf5::Handler::readMatrix ( std::vector< type > &  matrix,
std::pair< size_t, size_t > &  shape,
const std::string &  valname 
)
inline

read a matrix with a given name into a linearized array

reads a matrix from the h5 file. The dimensions of the matrix will be returned in the shape parameter and the vector will be resized to fit the data before copying the data into the vector.

Template Parameters
typeThe type that should be written
Parameters
matrixthe matrix to be read
shapethe shape of the matrix
valnamethe name of the value

turn off error output

Definition at line 519 of file hdf5_handle.hpp.

References _fileid, and status.

Referenced by cass::HDF5FileInput::runthis().

template<typename type >
void hdf5::Handler::readMultiDim ( std::vector< type > &  data,
shape_t shape,
const std::string &  valname 
)
inline

read a multidimensional dataset with a given name into a linearized array

reads a multidimensional dataset from the h5 file. The dimensions of the dataset will be returned as it is stored in the hdf5 file in the shape parameter and the vector will be resized to fit the data before copying the data into the vector.

Template Parameters
typeThe type that should be written
Parameters
datathe multidimensional data that will be read
shapethe shape of the matrix
valnamethe name of the value

turn off error output

Definition at line 729 of file hdf5_handle.hpp.

References _fileid, and status.

template<typename type >
void hdf5::Handler::readPartialMultiDim ( typename std::vector< type >::iterator  data,
const partiality_t part,
const std::string &  valname 
)
inline

partially read a multidimensional dataset with a given name into a linearized array

reads a part of a multidimensional dataset from the h5 file.

Template Parameters
typeThe type that should be read
Parameters
datapointer to the space where the data will be written to.
partthe part that should be read
valnamethe name of the value

turn off error output

do some error checking (check if provided params are suitable

set up the memory and the hypserlab for the partial read

read the partial data directly into the provided dataspace

Definition at line 785 of file hdf5_handle.hpp.

References _fileid, hdf5::partiality_t::block, hdf5::partiality_t::count, hdf5::partiality_t::dims, filename(), hdf5::partiality_t::offset, status, and hdf5::partiality_t::stride.

template<typename type >
type hdf5::Handler::readScalar ( const std::string &  valname)
inline

read an scalar value with a given name as part of a given group

create a dataspace and a dataset for writing the scalar value as part of the given group. Then write the value and close all resources later on.

Template Parameters
typeThe scalar type that should be written
Parameters
valnamethe name of the value

turn off error output

open the scalar dataset

read the attribute and close the resources

Definition at line 307 of file hdf5_handle.hpp.

References _fileid, status, and value.

Referenced by cass::HDF5FileInput::runthis().

template<typename type >
type hdf5::Handler::readScalarAttribute ( const std::string &  valname,
const std::string &  dsetName 
)
inline

read an scalar attribute with a given name as part of a given dataset

Exceptions
invalid_argumentwhen the requested parameter is not present
Template Parameters
typeThe type of the scalar value
Returns
the value of the scalar attribute
Parameters
valnamethe name of the value
dsetNamethe Name of the Dataset

turn off error output

open the dataset that the attribute should be added to

attach to the scalar attribute of the dataset and read it

read the attribute and close the resources

Definition at line 969 of file hdf5_handle.hpp.

References _fileid, status, and value.

std::string hdf5::Handler::readString ( const std::string &  dsetName)
inline

read a string dataset

Returns
the string
Parameters
dsetNamethe name of the dataset that contains the string

turn off error output

Definition at line 891 of file hdf5_handle.hpp.

References _fileid, and status.

std::string hdf5::Handler::reopenFile ( int &  filehandle,
size_t  maxsize,
const std::string &  currentfilename 
)
inlineprivate

check filesize and open new file if too big

check if the current size of the h5 file is bigger than the user set maximum file size. When this is the case, close the current file and open a new file with the same file name, but with an increasing extension.

Returns
new filename
Parameters
filehandlethe filehandle to the hdf5 file
maxsizethe maximum size of the file before a new file is opened
currentfilenamethe name of the current hdf5 file
Author
Lutz Foucar

Definition at line 1215 of file hdf5_handle.hpp.

References found.

dsetList_t hdf5::Handler::rootGroups ( ) const
inline

get the list of groups of the root group in the file

Returns
list of strings that point to groups

Definition at line 1173 of file hdf5_handle.hpp.

References _fileid, hdf5::group_iterator_func(), and status.

Referenced by cass::HDF5FileInput::runthis().

shape_t hdf5::Handler::shape ( const std::string &  valname) const
inline

get the shape of a dataset with a given name

retrieve the dataset with the given name, then the dataspace for the dataset. The order will be that the first will be the slowest dimension, then the second slowest and so on, then the last value will be the fastest dimension.

Returns
the shape of the dataset
Parameters
valnamethe name of the dataset

Definition at line 1038 of file hdf5_handle.hpp.

References _fileid.

template<typename type >
void hdf5::Handler::writeArray ( const std::vector< type > &  array,
const size_t  arrayLength,
const std::string &  valname,
int  compressLevel = 2 
)
inline

write a 1d array with a given name

create a dataspace and a dataset for writing the value as part of the given group. Then write the value and close all resources later on.

The name can contain the group that the value should be written to

Todo:
check out whether the chunck size can be optimized.
Template Parameters
typeThe type that should be written
Parameters
arraythe array to be written
arrayLengththe length of the array to be written
valnamethe name of the value
compressLevelthe compression level of the array
Author
Lutz Foucar

create space and dataset for storing the graph (1D hist)

set up the chunck size and the deflate options

Definition at line 350 of file hdf5_handle.hpp.

References _fileid, ensureGroupExists(), and status.

template<typename type >
void hdf5::Handler::writeMatrix ( const std::vector< type > &  matrix,
std::pair< size_t, size_t >  shape,
const std::string &  valname,
int  compressLevel = 2 
)
inline

write a linearized matrix with a given name

create a dataspace and a dataset for writing the matrix as part of the given group. Then write the matrix and close all resources later on.

The name can contain the group that the value should be written to

Todo:
check out whether the chunck size can be optimized.
Template Parameters
typeThe type that should be written
Parameters
matrixthe matrix to be written
shapethe shape of the matrix (first is cols, second is rows)
valnamethe name of the value
compressLevelthe compression level of the matrix

create space and dataset for storing the matrix

Definition at line 456 of file hdf5_handle.hpp.

References _fileid, ensureGroupExists(), and status.

template<typename type >
void hdf5::Handler::writeScalar ( const type  value,
const std::string &  valname 
)
inline

write an scalar value with a given name as part of a given group

create a dataspace and a dataset for writing the scalar value as part of the given group. Then write the value and close all resources later on.

Template Parameters
typeThe scalar type that should be written
Parameters
valuethe value to be written
valnamethe name of the value

Definition at line 275 of file hdf5_handle.hpp.

References _fileid, ensureGroupExists(), and status.

template<typename type >
void hdf5::Handler::writeScalarAttribute ( const type  value,
const std::string &  valname,
const std::string &  dsetName 
)
inline

write an float scalar attribute with a given name as part of a given dataset

Template Parameters
typeThe type that should be written
Parameters
valuethe value to be written
valnamethe name of the value
dsetNamethe Name of the Dataset

open the dataset that the attribute should be added to

open the attribute space and attribute of the dataset

write the attribute and close the resources

Definition at line 928 of file hdf5_handle.hpp.

References _fileid, and status.

void hdf5::Handler::writeString ( const std::string &  string,
const std::string &  dsetName 
)
inline

write a string dataset

Parameters
stringthe string to write
dsetNamethe name of the string dataset

Definition at line 855 of file hdf5_handle.hpp.

References _fileid, and status.

Member Data Documentation

hid_t hdf5::Handler::_fileid
private

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