CFEL - ASG Software Suite  2.5.0
CASS
hdf5_handle.hpp
Go to the documentation of this file.
1 //Copyright (C) 2013 Lutz Foucar
2 
3 /**
4  * @file hdf5_handle.hpp easier api for hdf5 file writing
5  *
6  * @author Lutz Foucar
7  */
8 
9 #include <vector>
10 #include <list>
11 #include <string>
12 #include <stdexcept>
13 #include <typeinfo>
14 #include <utility>
15 #include <iostream>
16 #include <sstream>
17 
18 #include <hdf5.h>
19 
20 
21 namespace hdf5
22 {
23 
24 /** Exception thrown when there is an error with the dataset
25  *
26  * @author Lutz Foucar
27  */
28 class DatasetError : public std::runtime_error
29 {
30 public:
31  /** explicit constructor
32  *
33  * @param message the error message
34  */
35  explicit DatasetError(const std::string & message)
36  : std::runtime_error(message)
37  {}
38 
39  virtual ~DatasetError() throw() {}
40 };
41 
42 /** define the shape type */
43 typedef std::vector<hsize_t> shape_t;
44 
45 /** define the list of dataset names */
46 typedef std::list<std::string> dsetList_t;
47 
48 /** define the partiality parameter container */
49 typedef struct
50 {
51  /** the dimensions of the partial dataset */
52  std::vector<hsize_t> dims;
53 
54  /** the offset of the partial data within the original data
55  *
56  * The offset or start array specifies the offset of the starting element of
57  * the specified hyperslab.
58  */
59  std::vector<hsize_t> offset;
60 
61  /** the stride
62  *
63  * The stride array allows you to sample elements along a dimension. For
64  * example, a stride of one (or NULL) will select every element along a
65  * dimension, a stride of two will select every other element, and a
66  * stride of three will select an element after every two elements.
67  */
68  std::vector<hsize_t> stride;
69 
70  /** the count
71  *
72  * The count array determines how many blocks to select from the dataspace
73  * in each dimension. If the block size for a dimension is one then the count
74  * is the number of elements along that dimension.
75  */
76  std::vector<hsize_t> count;
77 
78  /** the block
79  *
80  * The block array determines the size of the element block selected from a
81  * dataspace. If the block size is one or NULL then the block size is a
82  * single element in that dimension.
83  */
84  std::vector<hsize_t> block;
85 
86 } partiality_t;
87 
88 /** traits for matching a build in type with the corresponding h5 type
89  *
90  * Default @throw logic_error reporting that the type is not supported by the
91  * H5 handling.
92  */
93 template <typename T> inline hid_t H5Type()
94 {
95  throw std::logic_error(std::string("H5Type does not exist for '") +
96  typeid(T).name() + "'");
97 }
98 
99 /** trait implementation for float */
100 template <> inline hid_t H5Type<float>() {return H5T_NATIVE_FLOAT;}
101 
102 /** trait implementation for double */
103 template <> inline hid_t H5Type<double>() {return H5T_NATIVE_DOUBLE;}
104 
105 /** trait implementation for int */
106 template <> inline hid_t H5Type<int>() {return H5T_NATIVE_INT;}
107 
108 /** trait implementation for unsigned 64 bit int */
109 template <> inline hid_t H5Type<uint64_t>() {return H5T_NATIVE_UINT64;}
110 
111 /** trait implementation for unsigned 16 bit int */
112 template <> inline hid_t H5Type<uint32_t>() {return H5T_NATIVE_UINT32;}
113 
114 /** trait implementation for unsigned 16 bit int */
115 template <> inline hid_t H5Type<uint16_t>() {return H5T_NATIVE_UINT16;}
116 
117 /** trait implementation for unsigned 8 bit int */
118 template <> inline hid_t H5Type<uint8_t>() {return H5T_NATIVE_UINT8;}
119 
120 /** trait implementation for char */
121 template <> inline hid_t H5Type<char>() {return H5T_NATIVE_CHAR;}
122 
123 /** function to gather all datasets of the h5 file
124  *
125  * @param unused not used
126  * @param name name the name name to be added
127  * @param info the info of the object
128  * @param dlist pointer to the list that should be filled with the objects
129  */
130 inline
131 herr_t dataset_iterator_func(hid_t /*unused*/, const char * name,
132  const H5O_info_t *info, void *dlist)
133 {
134  using namespace std;
135  list<string>& dsetlist(*reinterpret_cast<list<string>*>(dlist));
136  if (info->type == H5O_TYPE_DATASET)
137  dsetlist.push_back(name);
138  return 0;
139 }
140 
141 /** function to gather groups of h5 file
142  *
143  * @param loc_id the location of the object
144  * @param name name the name to be added
145  * @param unused parameter not used
146  * @param slist pointer to the list that should be filled with groupnames
147  */
148 inline
149 herr_t group_iterator_func(hid_t loc_id, const char * name,
150  const H5L_info_t */*unused*/, void *slist)
151 {
152  using namespace std;
153  list<string>& stringlist(*reinterpret_cast<list<string>*>(slist));
154  H5O_info_t infobuf;
155  herr_t status = H5Oget_info_by_name (loc_id, name, &infobuf, H5P_DEFAULT);
156  if (status != 0)
157  return status;
158  if (infobuf.type == H5O_TYPE_GROUP)
159  stringlist.push_back(name);
160  return 0;
161 }
162 
163 /** A handler for h5 files
164  *
165  * @author Lutz Foucar
166  */
167 class Handler
168 {
169 private:
170  /** prevent copy construction */
171  Handler(const Handler& /*other*/) : _fileid(0) {}
172 
173 public:
174  /** default constructor
175  *
176  */
177  Handler() : _fileid(0) {}
178 
179  /** constructor opening the file
180  *
181  * @param mode Open the file in read "r" or write "w" mode. Default is "w"
182  * @param filename the name of the file to open
183  */
184  Handler(const std::string &filename, const std::string & mode="w")
185  : _fileid(0)
186  {
187  open(filename,mode);
188  }
189 
190  /** destructor
191  *
192  * flushes and cloeses the file if it is open
193  */
195  {
196  close();
197  }
198 
199  /** close the file used by this handler
200  *
201  */
202  void close()
203  {
204  using namespace std;
205  if (_fileid)
206  {
207  H5Fflush(_fileid,H5F_SCOPE_LOCAL);
208  H5Fclose(_fileid);
209  }
210  }
211 
212  /** open a file
213  *
214  * @throw logic_error when file could not be opened
215  *
216  * @param filename the name of the file to be opened
217  * @param mode Open the file in read "r", write "w" or in read write "rw" mode.
218  * Default is "w"
219  * @param size the size of the cache for chuncked data in MB
220  */
221  void open(const std::string &filename, const std::string & mode="w", int size=-1)
222  {
223  if (size > 0)
224  {
225  int nelemts; /* Dummy parameter in API, no longer used */
226  size_t nslots; /* Number of slots in the hash table */
227  size_t nbytes; /* Size of chunk cache in bytes */
228  double w0; /* Chunk preemption policy */
229 
230  /* File access property identifier */
231  hid_t fapl(H5Pcreate (H5P_FILE_ACCESS));
232  /* Retrieve default cache parameters */
233  H5Pget_cache(fapl, &nelemts, &nslots, &nbytes, &w0);
234  /* Set cache size to 3MBs and instruct the cache to discard the fully read chunk */
235  nbytes = size*1024*1024;
236  w0 = 1.;
237  H5Pset_cache(fapl, nelemts, nslots, nbytes, w0);
238  }
239 
240  if (_fileid)
241  throw std::logic_error("hdf5::Handler::open(): File '" + filename +
242  "'. Hander is already responsible for '" +
243  this->filename() + "'");
244 
245  if (mode == "w")
246  {
247  _fileid = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
248  }
249  else if (mode == "r")
250  {
251  _fileid = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
252  }
253  else if (mode == "rw")
254  {
255  _fileid = H5Fopen(filename.c_str(),H5F_ACC_RDWR, H5P_DEFAULT);
256  }
257  else
258  throw std::invalid_argument("hdf5::Handler::open(): Open mode '" +
259  mode + "' unknown");
260  if (_fileid < 0)
261  throw std::invalid_argument("hdf5::Handler::open(): File '" + filename +
262  "' could not be opened");
263  }
264 
265  /** write an scalar value with a given name as part of a given group
266  *
267  * create a dataspace and a dataset for writing the scalar value as part of
268  * the given group. Then write the value and close all resources later on.
269  *
270  * @tparam type The scalar type that should be written
271  * @param value the value to be written
272  * @param valname the name of the value
273  */
274  template <typename type>
275  void writeScalar(const type value, const std::string& valname)
276  {
277  using namespace std;
278  ensureGroupExists(valname);
279 
280  hid_t dataspace_id(H5Screate(H5S_SCALAR));
281  if (dataspace_id < 0)
282  throw runtime_error("writeScalar(float): Could not open the dataspace");
283 
284  hid_t dataset_id(H5Dcreate(_fileid, valname.c_str(),H5Type<type>(),
285  dataspace_id, H5P_DEFAULT, H5P_DEFAULT , H5P_DEFAULT));
286  if (dataset_id < 0)
287  throw DatasetError("writeScalar(float): Could not open the dataset '" + valname +"'");
288 
289  herr_t status(H5Dwrite(dataset_id, H5Type<type>(), H5S_ALL, H5S_ALL,
290  H5P_DEFAULT, &value));
291  if (status < 0)
292  throw runtime_error("writeScalar(): Could not write value");
293 
294  H5Dclose(dataset_id);
295  H5Sclose(dataspace_id);
296  }
297 
298  /** read an scalar value with a given name as part of a given group
299  *
300  * create a dataspace and a dataset for writing the scalar value as part of
301  * the given group. Then write the value and close all resources later on.
302  *
303  * @tparam type The scalar type that should be written
304  * @param valname the name of the value
305  */
306  template <typename type>
307  type readScalar(const std::string& valname)
308  {
309  using namespace std;
310 
311  /** turn off error output */
312  H5Eset_auto(H5E_DEFAULT,0,0);
313 
314  /** open the scalar dataset */
315  hid_t dataset_id(H5Dopen(_fileid,valname.c_str(),H5P_DEFAULT));
316  if (dataset_id < 0)
317  throw DatasetError("readScalar(): Could not open the dataset '" +
318  valname + "'");
319 
320  /** read the attribute and close the resources */
321  type value;
322  herr_t status(H5Dread(dataset_id, H5Type<type>(), H5S_ALL, H5S_ALL, H5P_DEFAULT,
323  &value));
324  if (status < 0)
325  throw logic_error("readScalar(): Could not read the scalar value '" + valname + "'");
326 
327  H5Dclose(dataset_id);
328 
329  return value;
330  }
331 
332  /** write a 1d array with a given name
333  *
334  * create a dataspace and a dataset for writing the value as part of the given
335  * group. Then write the value and close all resources later on.
336  *
337  * The name can contain the group that the value should be written to
338  *
339  * @todo check out whether the chunck size can be optimized.
340  *
341  * @tparam type The type that should be written
342  * @param array the array to be written
343  * @param arrayLength the length of the array to be written
344  * @param valname the name of the value
345  * @param compressLevel the compression level of the array
346  *
347  * @author Lutz Foucar
348  */
349  template<typename type>
350  void writeArray(const std::vector<type> &array, const size_t arrayLength,
351  const std::string& valname, int compressLevel=2)
352  {
353  using namespace std;
354  hsize_t dims[1] = {arrayLength};
355 
356  ensureGroupExists(valname);
357 
358  /** create space and dataset for storing the graph (1D hist) */
359  hid_t dataspace_id = H5Screate_simple(1, dims, NULL);
360  if (dataspace_id < 0)
361  throw runtime_error("writeArray(): Could not open the dataspace");
362 
363  /** set up the chunck size and the deflate options */
364  hid_t dataset_id;
365  if(compressLevel != 0)
366  {
367  // Create dataset creation property list, set the gzip compression filter
368  // and chunck size
369  hsize_t chunk[1] = {arrayLength};
370  hid_t dcpl (H5Pcreate (H5P_DATASET_CREATE));
371  H5Pset_deflate (dcpl, compressLevel);
372  H5Pset_chunk (dcpl, 1, chunk);
373  dataset_id = (H5Dcreate(_fileid, valname.c_str(), H5Type<type>(),
374  dataspace_id, H5P_DEFAULT, dcpl, H5P_DEFAULT));
375  }
376  else
377  dataset_id = (H5Dcreate(_fileid, valname.c_str(), H5Type<type>(),
378  dataspace_id, H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT));
379 
380  if (dataset_id < 0)
381  throw DatasetError("writeArray(): Could not open the dataset '"
382  + valname +"'");
383 
384  herr_t status(H5Dwrite(dataset_id, H5Type<type>(), H5S_ALL, H5S_ALL,
385  H5P_DEFAULT, &array.front()));
386  if (status < 0)
387  throw runtime_error("writeArray(): Could not write array");
388 
389  H5Dclose(dataset_id);
390  H5Sclose(dataspace_id);
391  }
392 
393  /** read a array with a given name into a linearized array
394  *
395  * reads a array from the h5 file. The dimensions of the matrix will be
396  * returned in the arrayLength and the vector will be resized to fit the
397  * data before copying the data into the vector.
398  *
399  * @tparam type The type that should be read
400  * @param[out] array the array that will be read
401  * @param[out] arrayLength the length of the array
402  * @param[in] valname the name of the value
403  */
404  template<typename type>
405  void readArray(std::vector<type> &array, size_t &arrayLength,
406  const std::string& valname)
407  {
408  using namespace std;
409  hsize_t dims[1];
410 
411  /** turn off error output */
412  H5Eset_auto(H5E_DEFAULT,0,0);
413 
414  hid_t dataset_id(H5Dopen(_fileid, valname.c_str(), H5P_DEFAULT));
415  if (dataset_id < 0)
416  throw DatasetError("readArray(): Could not open Dataset '"+ valname +"'");
417 
418  hid_t dataspace_id(H5Dget_space (dataset_id));
419  if (dataspace_id < 0)
420  throw logic_error("readArray(): Could not open the dataspace");
421 
422  int ndims(H5Sget_simple_extent_dims (dataspace_id, dims, NULL));
423  if (ndims < 0)
424  throw logic_error("readMatrix(): Could not read the dimensions");
425 
426  arrayLength = dims[0];
427 
428  array.resize(arrayLength,0);
429 
430  herr_t status(H5Dread(dataset_id, H5Type<type>(), H5S_ALL, H5S_ALL, H5P_DEFAULT,
431  &array.front()));
432  if (status < 0 )
433  throw logic_error("readArray: Something went wrong reading matrix data");
434 
435  H5Sclose(dataspace_id);
436  H5Dclose(dataset_id);
437  }
438 
439 
440  /** write a linearized matrix with a given name
441  *
442  * create a dataspace and a dataset for writing the matrix as part of the given
443  * group. Then write the matrix and close all resources later on.
444  *
445  * The name can contain the group that the value should be written to
446  *
447  * @todo check out whether the chunck size can be optimized.
448  *
449  * @tparam type The type that should be written
450  * @param matrix the matrix to be written
451  * @param shape the shape of the matrix (first is cols, second is rows)
452  * @param valname the name of the value
453  * @param compressLevel the compression level of the matrix
454  */
455  template<typename type>
456  void writeMatrix(const std::vector<type> &matrix, std::pair<size_t,size_t> shape,
457  const std::string& valname, int compressLevel=2)
458  {
459  using namespace std;
460  hsize_t dims[2] = {shape.second,shape.first};
461 
462  ensureGroupExists(valname);
463 
464  /** create space and dataset for storing the matrix */
465  hid_t dataspace_id = H5Screate_simple(2, dims, NULL);
466  if (dataspace_id < 0)
467  throw runtime_error("writeMatrix(): Could not open the dataspace");
468 
469  hid_t dataset_id;
470  if(compressLevel != 0)
471  {
472  // Create dataset creation property list, set the gzip compression filter
473  // and chunck size
474  // in case one of the dimensions is 0, then set it to 1
475  hsize_t slowestDim(shape.second?shape.second:1);
476  hsize_t fastestDim(shape.first?shape.first:1);
477  hsize_t chunk[2] = {slowestDim,fastestDim};
478  hid_t dcpl (H5Pcreate (H5P_DATASET_CREATE));
479  herr_t stat(H5Pset_deflate (dcpl, compressLevel));
480  if (stat < 0)
481  throw runtime_error("writeMatrix(): Couldn't set the compressionlevel for '"
482  + valname + "'");
483  stat = H5Pset_chunk (dcpl, 2, chunk);
484  if (stat < 0)
485  throw runtime_error("writeMatrix(): Couldn't set the chunk for '"
486  + valname + "'");
487  dataset_id = H5Dcreate(_fileid, valname.c_str(), H5Type<type>(),
488  dataspace_id, H5P_DEFAULT, dcpl, H5P_DEFAULT);
489  }
490  else
491  dataset_id = H5Dcreate(_fileid, valname.c_str(), H5Type<type>(),
492  dataspace_id, H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
493 
494  if (dataset_id < 0)
495  throw DatasetError("writeMatrix(): Could not open the dataset '"
496  + valname + "'");
497 
498  herr_t status(H5Dwrite(dataset_id, H5Type<type>(), H5S_ALL, H5S_ALL,
499  H5P_DEFAULT, &matrix.front()));
500  if (status < 0)
501  throw runtime_error("writeMatrix(): Could not write data");
502 
503  H5Dclose(dataset_id);
504  H5Sclose(dataspace_id);
505  }
506 
507  /** read a matrix with a given name into a linearized array
508  *
509  * reads a matrix from the h5 file. The dimensions of the matrix will be
510  * returned in the shape parameter and the vector will be resized to fit the
511  * data before copying the data into the vector.
512  *
513  * @tparam type The type that should be written
514  * @param matrix the matrix to be read
515  * @param shape the shape of the matrix
516  * @param valname the name of the value
517  */
518  template<typename type>
519  void readMatrix(std::vector<type> &matrix, std::pair<size_t,size_t> &shape,
520  const std::string& valname)
521  {
522  using namespace std;
523 
524  /** turn off error output */
525  H5Eset_auto(H5E_DEFAULT,0,0);
526 
527  hid_t dataset_id(H5Dopen (_fileid, valname.c_str(), H5P_DEFAULT));
528  if (dataset_id < 0)
529  throw DatasetError("readMatrix(): Could not open Dataset '"+ valname +"'");
530 
531  hid_t dataspace_id(H5Dget_space (dataset_id));
532  if (dataspace_id < 0)
533  throw logic_error("readMatrix(): Could not open the dataspace");
534 
535  const int ndims(H5Sget_simple_extent_ndims(dataspace_id));
536  if (ndims < 0)
537  throw logic_error("readMatrix(): Could not read the the number of dimensions");
538  if (ndims != 2)
539  throw logic_error("readMatrix(): The dataset doesn't have the 2 dimensions");
540 
541  hsize_t dims[ndims];
542 
543  int retNdims(H5Sget_simple_extent_dims (dataspace_id, dims, NULL));
544  if (retNdims != ndims)
545  throw logic_error("readMatrix(): Could not read the dimensions");
546 
547  shape.first = dims[1];
548  shape.second = dims[0];
549 
550  matrix.resize(shape.first*shape.second,0);
551 
552  herr_t status(H5Dread(dataset_id, H5Type<type>(), H5S_ALL, H5S_ALL, H5P_DEFAULT,
553  &matrix.front()));
554  if (status < 0 )
555  throw logic_error("readMatrix: Something went wrong reading matrix data");
556 
557  H5Dclose(dataset_id);
558  H5Sclose(dataspace_id);
559  }
560 
561  /** create/append data to a multidimensional dataset
562  *
563  * The name can contain the group that the value should be written to
564  *
565  * @tparam type The type that should be written
566  * @param data the matrix to be written
567  * @param shape the shape of the matrix (first is cols, second is rows)
568  * @param valname the name of the value
569  * @param compressLevel the compression level of the matrix
570  */
571  template<typename type>
572  void appendData(const std::vector<type> &data, shape_t shape,
573  const std::string& valname, int compressLevel=2)
574  {
575  using namespace std;
576 
577 // for (size_t i(0); i<shape.size();++i)
578 // cout << "shape["<<i<<"] = "<<shape[i]<<endl;
579  /** turn off error output */
580  H5Eset_auto(H5E_DEFAULT,0,0);
581 
582  /** ensure that the goup where the data will be appended to exists */
583  ensureGroupExists(valname);
584 
585  /** get the dataset and check if the dataset exists*/
586  hid_t dataset_id(H5Dopen(_fileid, valname.c_str(), H5P_DEFAULT));
587  if (dataset_id < 0)
588  {
589  /** the dataset seems to not exist, so create it */
590 
591  /** create the file dataspace, where the first dim is unlimited */
592  vector<hsize_t> dims(shape);
593  dims.insert(dims.begin(),0);
594 // for (size_t i(0); i<dims.size();++i)
595 // cout << "dims["<<i<<"] = "<<dims[i]<<endl;
596  vector<hsize_t> maxdims(shape);
597  maxdims.insert(maxdims.begin(),H5S_UNLIMITED);
598 // for (size_t i(0); i<maxdims.size();++i)
599 // cout << "max_dims["<<i<<"] = "<<maxdims[i]<<endl;
600  hid_t dataspace_id(H5Screate_simple(dims.size(), &(dims.front()),
601  &(maxdims.front())));
602  if (dataspace_id < 0)
603  throw logic_error("appenddata(): Could not open the dataspace");
604 
605  /** create the property list to be chunked */
606  hid_t property_id(H5Pcreate(H5P_DATASET_CREATE));
607  if (property_id < 0)
608  throw logic_error("appenddata(): Could not create property list");
609  herr_t status(H5Pset_layout(property_id, H5D_CHUNKED));
610  if (status < 0)
611  throw logic_error("appenddata(): Could not set the chuncked layout");
612  vector<hsize_t> chunk_dims(dims);
613  chunk_dims[0] = 1;
614 // for (size_t i(0); i<chunk_dims.size();++i)
615 // cout << "chuck_dim["<<i<<"] = "<<chunk_dims[i]<<endl;
616  status = H5Pset_chunk(property_id, chunk_dims.size(), &(chunk_dims.front()));
617  if (status < 0)
618  throw logic_error("appenddata(): Could not define the chunck size");
619  status = H5Pset_deflate(property_id, compressLevel);
620  if (status < 0)
621  throw logic_error("appenddata(): Could not define the compression level");
622 
623  /** create the dataset */
624  dataset_id = H5Dcreate(_fileid, valname.c_str(), H5Type<type>(),
625  dataspace_id, H5P_DEFAULT, property_id, H5P_DEFAULT);
626  if (dataset_id < 0)
627  throw logic_error("appenddata(): Could not create the dataset '"
628  + valname + "'");
629 
630  /** close the resources */
631  H5Pclose(property_id);
632  H5Sclose(dataspace_id);
633  }
634 
635  /** get the filespace of the dataset to extract the current dimensions */
636  hid_t dataspace_id(H5Dget_space(dataset_id));
637  if (dataspace_id < 0)
638  throw logic_error("appenddata(): Could not open the dataspace");
639 
640  /** get the nbr of dimensions and the dimensions of the dataset */
641  const hssize_t ndims(H5Sget_simple_extent_ndims(dataspace_id));
642  if (ndims < 0)
643  throw logic_error("appenddata(): Could not read the the number of dimensions");
644  if (static_cast<size_t>(ndims) != shape.size()+1)
645  throw logic_error("appenddata(): The number of dimensions do not fit");
646  vector<hsize_t> dims(ndims);
647  int retNdims(H5Sget_simple_extent_dims(dataspace_id, &dims.front(), NULL));
648  if (retNdims != ndims)
649  throw logic_error("appenddata(): Could not read the dimensions");
650 // for (size_t i(0); i<dims.size();++i)
651 // cout << "dims["<<i<<"] = "<<dims[i]<<endl;
652 
653  /** release the old dataspace of the dataset, later we get the new extended
654  * filespace of the dataset
655  */
656  H5Sclose(dataspace_id);
657 
658  /** check if the dimensions fit the to be appended datas dimensions */
659  for (size_t i(0);i<shape.size(); ++i)
660  if (dims[i+1] != shape[i])
661  throw logic_error("appenddata(): The dimensions are not equal");
662 
663  /** create space in memory for the new data of the dataset */
664  hsize_t origsize(dims[0]);
665  dims[0] = 1;
666 // for (size_t i(0); i<dims.size();++i)
667 // cout << "dims["<<i<<"] = "<<dims[i]<<endl;
668  hid_t memspace_id(H5Screate_simple(ndims, &dims.front(), NULL));
669  if (memspace_id < 0)
670  throw runtime_error("appendData(): Could not open the dataspace");
671 
672  /** extent the dataset so that it can incldue the newly added data */
673  dims[0] = origsize + 1;
674 // for (size_t i(0); i<dims.size();++i)
675 // cout << "dims["<<i<<"] = "<<dims[i]<<endl;
676  herr_t status(H5Dset_extent(dataset_id, &dims.front()));
677  if (status < 0)
678  throw runtime_error("appendData(): Could not extent the dataset");
679 
680  /** get the filespace of the extended dataset */
681  dataspace_id = H5Dget_space(dataset_id);
682  if (dataspace_id < 0)
683  throw logic_error("appenddata(): Could not open the extended dataspace");
684 
685  /** create hyperslab on the file dataspace where the data will be written
686  * to later on
687  */
688  vector<hsize_t> offset(ndims,0);
689  offset[0] = origsize;
690 // for (size_t i(0); i<offset.size();++i)
691 // cout << "offset["<<i<<"] = "<<offset[i]<<endl;
692  vector<hsize_t> count(dims);
693  count[0] = 1;
694 // for (size_t i(0); i<count.size();++i)
695 // cout << "count["<<i<<"] = "<<count[i]<<endl;
696  status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET,
697  &(offset.front()), NULL,
698  &(count.front()), NULL);
699  if (status < 0)
700  throw runtime_error("appenddata(): Could define hyperslab");
701 
702  status = H5Dwrite(dataset_id, H5Type<type>(), memspace_id, dataspace_id,
703  H5P_DEFAULT, &(data.front()));
704  if (status < 0)
705  {
706  stringstream ss;
707  ss << "appenddata(): Could not write data " <<status;
708  throw runtime_error(ss.str());
709  }
710 
711  H5Dclose(dataset_id);
712  H5Sclose(dataspace_id);
713  H5Sclose(memspace_id);
714  }
715 
716  /** read a multidimensional dataset with a given name into a linearized array
717  *
718  * reads a multidimensional dataset from the h5 file. The dimensions of the
719  * dataset will be returned as it is stored in the hdf5 file in the shape
720  * parameter and the vector will be resized to fit the data before copying
721  * the data into the vector.
722  *
723  * @tparam type The type that should be written
724  * @param data the multidimensional data that will be read
725  * @param shape the shape of the matrix
726  * @param valname the name of the value
727  */
728  template<typename type>
729  void readMultiDim(std::vector<type> &data, shape_t &shape,
730  const std::string& valname)
731  {
732  using namespace std;
733 
734  /** turn off error output */
735  H5Eset_auto(H5E_DEFAULT,0,0);
736 
737  hid_t dataset_id(H5Dopen (_fileid, valname.c_str(), H5P_DEFAULT));
738  if (dataset_id < 0)
739  throw DatasetError("readMultiDim(): Could not open Dataset '"+ valname +"'");
740 
741  hid_t dataspace_id(H5Dget_space (dataset_id));
742  if (dataspace_id < 0)
743  throw logic_error("readMultiDim(): Could not open the dataspace");
744 
745  const int ndims(H5Sget_simple_extent_ndims(dataspace_id));
746  if (ndims < 0)
747  throw logic_error("readMultiDim(): Could not read the the number of dimensions");
748 
749  hsize_t dims[ndims];
750 
751  int retNdims(H5Sget_simple_extent_dims (dataspace_id, dims, NULL));
752  if (retNdims != ndims)
753  throw logic_error("readMultiDim(): Could not read the dimensions");
754 
755  size_t completesize(1);
756  for (int i(0); i<ndims; ++i)
757  {
758  completesize *= dims[i];
759  shape.push_back(dims[i]);
760  }
761 
762  data.resize(completesize,0);
763 
764  herr_t status(H5Dread(dataset_id, H5Type<type>(), H5S_ALL, H5S_ALL, H5P_DEFAULT,
765  &data.front()));
766  if (status < 0)
767  throw logic_error("readMultiDim: Something went wrong reading matrix data");
768 
769  H5Dclose(dataset_id);
770  H5Sclose(dataspace_id);
771  }
772 
773 
774  /** partially read a multidimensional dataset with a given name into a
775  * linearized array
776  *
777  * reads a part of a multidimensional dataset from the h5 file.
778  *
779  * @tparam type The type that should be read
780  * @param data pointer to the space where the data will be written to.
781  * @param part the part that should be read
782  * @param valname the name of the value
783  */
784  template<typename type>
785  void readPartialMultiDim(typename std::vector<type>::iterator data,
786  const partiality_t &part,
787  const std::string& valname)
788  {
789  using namespace std;
790 
791  /** turn off error output */
792  H5Eset_auto(H5E_DEFAULT,0,0);
793 
794  hid_t dataset_id(H5Dopen (_fileid, valname.c_str(), H5P_DEFAULT));
795  if (dataset_id < 0)
796  throw DatasetError("readPartialMultiDim(): Could not open Dataset '" +
797  valname + "' in file '" + filename() + "'");
798 
799  hid_t dataspace_id(H5Dget_space(dataset_id));
800  if (dataspace_id < 0)
801  throw logic_error("readPartialMultiDim(): Could not open the dataspace");
802 
803  const int ndims(H5Sget_simple_extent_ndims(dataspace_id));
804  if (ndims < 0)
805  throw logic_error(string("readPartialMultiDim(): Could not read the ") +
806  "number of dimensions");
807 
808  hsize_t dims[ndims];
809  int retNdims(H5Sget_simple_extent_dims(dataspace_id, dims, NULL));
810  if (retNdims != ndims)
811  throw logic_error("readPartialMultiDim(): Could not read the dimensions");
812 
813  /** do some error checking (check if provided params are suitable */
814  if (ndims != static_cast<int>(part.dims.size()))
815  {
816  stringstream ss;
817  ss <<"readPartialMultiDim(): The provided size of the dimensions '" <<
818  part.dims.size() << "' does not match the dataset dimension size '" <<
819  ndims << "' for dataset '" << valname <<"' within '" <<
820  filename() <<"'";
821  throw DatasetError(ss.str());
822  }
823 
824  /** set up the memory and the hypserlab for the partial read*/
825  hid_t memspace_id(H5Screate_simple(part.dims.size(), &(part.dims.front()),
826  NULL));
827  if (dataset_id < 0)
828  throw logic_error("readPartialMultiDim(): Could not open the memspace.");
829 
830  herr_t status(H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET,
831  &(part.offset.front()),
832  &(part.stride.front()),
833  &(part.count.front()),
834  &(part.block.front())));
835  if (status < 0)
836  throw logic_error(string("readPartialMultiDim: Something went wrong ") +
837  "creating the hyperslab");
838 
839  /** read the partial data directly into the provided dataspace */
840  status = H5Dread(dataset_id, H5Type<type>(), memspace_id, dataspace_id,
841  H5P_DEFAULT, &(*data));
842  if (status < 0)
843  throw logic_error("readPartialMultiDim: Something went wrong reading partial data");
844 
845  H5Sclose(memspace_id);
846  H5Sclose(dataspace_id);
847  H5Dclose(dataset_id);
848  }
849 
850  /** write a string dataset
851  *
852  * @param string the string to write
853  * @param dsetName the name of the string dataset
854  */
855  void writeString(const std::string &string, const std::string &dsetName)
856  {
857  using namespace std;
858 
859  hid_t dataspace_id(H5Screate (H5S_SCALAR));
860  if (dataspace_id < 0 )
861  throw runtime_error("writeString(): Could not open the dataspace");
862 
863  hid_t datatype_id(H5Tcopy(H5T_C_S1));
864  if (datatype_id < 0 )
865  throw runtime_error("writeString(): Could not open the datatype ");
866 
867  hid_t status(H5Tset_size (datatype_id, H5T_VARIABLE));
868  if (status < 0 )
869  throw runtime_error("writeString(): Could not set the variable size to datatype");
870 
871  hid_t dataset_id(H5Dcreate(_fileid, dsetName.c_str(), datatype_id,
872  dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT));
873  if (dataset_id < 0 )
874  throw DatasetError("writeString(): Could not open the dataset '"
875  + dsetName + "'");
876 
877  const char *s(string.c_str());
878  status = H5Dwrite(dataset_id, datatype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, &s);
879  if (status < 0 )
880  throw runtime_error("writeString(): Could not write data");
881 
882  H5Sclose(dataspace_id);
883  H5Dclose(dataset_id);
884  }
885 
886  /** read a string dataset
887  *
888  * @return the string
889  * @param dsetName the name of the dataset that contains the string
890  */
891  std::string readString(const std::string &dsetName)
892  {
893  using namespace std;
894 
895  /** turn off error output */
896  H5Eset_auto(H5E_DEFAULT,0,0);
897 
898  hid_t dataset_id(H5Dopen(_fileid, dsetName.c_str(), H5P_DEFAULT));
899  if (dataset_id < 0)
900  throw DatasetError("readString(): Could not open Dataset '"+ dsetName +
901  "'");
902 
903  hid_t datatype_id(H5Dget_type(dataset_id));
904  if (datatype_id < 0)
905  throw runtime_error("readString(): Error retrieving the data type");
906 
907  hsize_t datasize(H5Dget_storage_size(dataset_id));
908  if (datasize == 0)
909  throw runtime_error("readString(): Error retrieving the data size");
910 
911  vector<char> buf(static_cast<int>(datasize+1),0);
912  herr_t status(H5Dread(dataset_id,datatype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,
913  &buf.front()));
914  if (status < 0 )
915  throw logic_error("readString: Something went wrong reading string data");
916 
917  return string(buf.begin(),buf.end());
918  }
919 
920  /** write an float scalar attribute with a given name as part of a given dataset
921  *
922  * @tparam type The type that should be written
923  * @param value the value to be written
924  * @param valname the name of the value
925  * @param dsetName the Name of the Dataset
926  */
927  template<typename type>
928  void writeScalarAttribute(const type value, const std::string& valname,
929  const std::string & dsetName)
930  {
931  using namespace std;
932 
933  /** open the dataset that the attribute should be added to */
934  hid_t dataset_id(H5Dopen(_fileid,dsetName.c_str(),H5P_DEFAULT));
935  if (dataset_id < 0)
936  throw DatasetError("writeScalarAttribute(): Could not open the dataset '" +
937  dsetName + "'");
938 
939  /** open the attribute space and attribute of the dataset */
940  hid_t attributespace_id(H5Screate(H5S_SCALAR));
941  if (attributespace_id < 0)
942  throw runtime_error("writeScalarAttribute(): Could not open the dataspace");
943  hid_t attribute_id(H5Acreate(dataset_id, valname.c_str(), H5Type<type>(),
944  attributespace_id, H5P_DEFAULT, H5P_DEFAULT));
945  if (attribute_id < 0)
946  throw runtime_error("writeScalarAttribute(): Could not open the attribute '"
947  + valname +"'");
948 
949  /** write the attribute and close the resources */
950  herr_t status(H5Awrite(attribute_id, H5Type<type>(), &value));
951  if (status < 0 )
952  throw logic_error("writeScalarAttribute: Something went wrong reading matrix data");
953 
954  H5Aclose(attribute_id);
955  H5Sclose(attributespace_id);
956  H5Dclose(dataset_id);
957  }
958 
959  /** read an scalar attribute with a given name as part of a given dataset
960  *
961  * @throws invalid_argument when the requested parameter is not present
962  *
963  * @tparam type The type of the scalar value
964  * @return the value of the scalar attribute
965  * @param valname the name of the value
966  * @param dsetName the Name of the Dataset
967  */
968  template<typename type>
969  type readScalarAttribute(const std::string& valname, const std::string & dsetName)
970  {
971  using namespace std;
972 
973  /** turn off error output */
974  H5Eset_auto(H5E_DEFAULT,0,0);
975 
976  /** open the dataset that the attribute should be added to */
977  hid_t dataset_id(H5Dopen(_fileid,dsetName.c_str(),H5P_DEFAULT));
978  if (dataset_id < 0)
979  throw DatasetError("readScalarAttribute(): Could not open the dataset '" +
980  dsetName + "'");
981 
982  /** attach to the scalar attribute of the dataset and read it */
983  hid_t attribute_id(H5Aopen(dataset_id, valname.c_str(), H5P_DEFAULT));
984  if (attribute_id < 0)
985  throw invalid_argument("readScalarAttribute(): Could not open the attribute '"
986  + valname +"'");
987 
988  /** read the attribute and close the resources */
989  type value;
990  herr_t status(H5Aread(attribute_id, H5Type<type>(), &value));
991  if (status < 0)
992  throw logic_error("readScalarAttribute(): Could read the attribute '"
993  + valname +"' of dataset '" + dsetName + "'");
994 
995  H5Aclose(attribute_id);
996  H5Dclose(dataset_id);
997 
998  return value;
999  }
1000 
1001  /** retrieve the filename of the file associated with the handler
1002  *
1003  * @return the name of the file
1004  */
1005  std::string filename() const
1006  {
1007  using namespace std;
1008  if (!_fileid)
1009  return "";
1010  /** determine the size of the filename (which is the return value of the
1011  * query function
1012  */
1013  int namesize(H5Fget_name(_fileid,NULL,0));
1014  if (namesize < 0)
1015  throw logic_error("filename(): Error when retrieving the size of the name");
1016  /** allocate a vector of chars to hold the filanme */
1017  vector<char> fn(namesize+1,' ');
1018  /** and retrieve the filename */
1019  herr_t status(H5Fget_name(_fileid,&fn[0],fn.size()));
1020  if (status < 0)
1021  throw logic_error("filename(): Error when retrieving the filename");
1022  /** use the iterator constructor of string to convert the vector of chars to
1023  * string
1024  */
1025  return string(fn.begin(),fn.end());
1026  }
1027 
1028  /** get the shape of a dataset with a given name
1029  *
1030  * retrieve the dataset with the given name, then the dataspace for the
1031  * dataset. The order will be that the first will be the slowest dimension,
1032  * then the second slowest and so on, then the last value will be the fastest
1033  * dimension.
1034  *
1035  * @return the shape of the dataset
1036  * @param valname the name of the dataset
1037  */
1038  shape_t shape(const std::string &valname) const
1039  {
1040  using namespace std;
1041  hid_t dataset_id(H5Dopen (_fileid, valname.c_str(), H5P_DEFAULT));
1042  if (dataset_id < 0)
1043  throw DatasetError("shape(): Could not open Dataset '"+ valname +"'");
1044 
1045  hid_t dataspace_id(H5Dget_space (dataset_id));
1046  if(H5Sget_simple_extent_type(dataspace_id) != H5S_SIMPLE)
1047  throw DatasetError("shape(): Dataset '"+ valname + "' is not of type " +
1048  "H5S_SIMPLE and therefore doesn't have a shape");
1049 
1050  const int ndims(H5Sget_simple_extent_ndims(dataspace_id));
1051  if (ndims < 0)
1052  throw logic_error("shape(): Could not read the the number of dimensions");
1053 
1054  hsize_t dims[ndims];
1055 
1056  int retNdims(H5Sget_simple_extent_dims(dataspace_id, dims, NULL));
1057  if (retNdims != ndims)
1058  throw logic_error("shape(): Could not read the shape");
1059 
1060  return shape_t(&dims[0],&dims[0] + ndims);
1061  }
1062 
1063  /** get the dimension of a value with a given name
1064  *
1065  * retrieve the dataset with the given name, then the dataspace for the
1066  * dataset. Judge by the type and the number of dimension what the dimensions
1067  * are.
1068  *
1069  * @return the dimension of the value
1070  * @param valname the name of the value
1071  */
1072  size_t dimension(const std::string &valname) const
1073  {
1074  using namespace std;
1075  size_t dimension;
1076  hid_t dataset_id(H5Dopen (_fileid, valname.c_str(), H5P_DEFAULT));
1077  if (dataset_id < 0)
1078  throw DatasetError("dimension(): Could not open Dataset '"+ valname +"'");
1079 
1080  hid_t dataspace_id(H5Dget_space (dataset_id));
1081  switch(H5Sget_simple_extent_type(dataspace_id))
1082  {
1083  case H5S_SCALAR:
1084  {
1085  hid_t datatype_id(H5Dget_type(dataset_id));
1086  int dtype = H5Tget_class(datatype_id);
1087  //cout<<"SCALAR type"<<endl;
1088  switch(dtype)
1089  {
1090  case H5T_STRING:
1091  //cout<<"string type"<<endl;
1092  dimension = 3;
1093  break;
1094  case H5T_INTEGER:
1095  //cout<<"integer type"<<endl;
1096  case H5T_FLOAT:
1097  //cout<<"float type"<<endl;
1098  dimension = 0;
1099  break;
1100  default:
1101  throw logic_error("dimension(): Datatype not supported");
1102  break;
1103  }
1104  break;
1105  }
1106  case H5S_SIMPLE:
1107  //cout<<"SIMPLE type"<<endl;
1108  switch(H5Sget_simple_extent_ndims(dataspace_id))
1109  {
1110  case 1:
1111  {
1112  hsize_t dims[1];
1113  H5Sget_simple_extent_dims(dataspace_id,dims,NULL);
1114  //cout<<"dimension 1: " <<dims[0]<<endl;
1115  if (dims[0] == 1)
1116  dimension = 0;
1117  else
1118  dimension = 1;
1119  break;
1120  }
1121  case 2:
1122  {
1123  hsize_t dims[2];
1124  H5Sget_simple_extent_dims(dataspace_id,dims,NULL);
1125  //cout<<"dimension 2: "<< dims[0]<<"x"<<dims[1]<<endl;
1126  if (dims[0] == 1 && dims[1] == 1)
1127  dimension = 0;
1128  else if (dims[1] == 1)
1129  dimension = 1;
1130  else
1131  dimension = 2;
1132  break;
1133  }
1134  default:
1135  throw logic_error("dimension(): Unkown dataspace dimension");
1136  break;
1137  }
1138  break;
1139  default:
1140  throw logic_error("dimension(): Unknown dataspace type");
1141  break;
1142  }
1143 
1144  H5Dclose(dataset_id);
1145  H5Sclose(dataspace_id);
1146 
1147  return dimension;
1148  }
1149 
1150  /** get the list of datasets in the file
1151  *
1152  * @return list of strings that point to valid datasets
1153  */
1154  dsetList_t datasets() const
1155  {
1156  using namespace std;
1157  dsetList_t dsetlist;
1158  if (_fileid)
1159  {
1160  hid_t status(H5Ovisit(_fileid,H5_INDEX_NAME,H5_ITER_NATIVE,
1161  dataset_iterator_func,&dsetlist));
1162  if (status < 0)
1163  throw logic_error("datasets(): Error when iterating through the h5 file");
1164  }
1165 
1166  return dsetlist;
1167  }
1168 
1169  /** get the list of groups of the root group in the file
1170  *
1171  * @return list of strings that point to groups
1172  */
1173  dsetList_t rootGroups() const
1174  {
1175  using namespace std;
1176  dsetList_t grouplist;
1177  hid_t status(H5Literate(_fileid,H5_INDEX_NAME,H5_ITER_INC,NULL,
1178  group_iterator_func,&grouplist));
1179  if (status < 0)
1180  throw logic_error("rootGroups(): Error when iterating through the h5 file");
1181 
1182  return grouplist;
1183  }
1184 
1185  /** retrieve the size of the current file
1186  *
1187  * @return the size of the file
1188  */
1189  size_t currentFileSize() const
1190  {
1191  using namespace std;
1192  hsize_t currentsize;
1193  herr_t status(H5Fget_filesize(_fileid,&currentsize));
1194  if (status < 0)
1195  throw logic_error("currentFileSize(): Error when retrieving the file size");
1196  return currentsize;
1197  }
1198 
1199 private:
1200  /** check filesize and open new file if too big
1201  *
1202  * check if the current size of the h5 file is bigger than the
1203  * user set maximum file size. When this is the case, close the
1204  * current file and open a new file with the same file name, but
1205  * with an increasing extension.
1206  *
1207  * @return new filename
1208  * @param filehandle the filehandle to the hdf5 file
1209  * @param maxsize the maximum size of the file before a new file
1210  * is opened
1211  * @param currentfilename the name of the current hdf5 file
1212  *
1213  * @author Lutz Foucar
1214  */
1215  std::string reopenFile(int & filehandle, size_t maxsize,
1216  const std::string& currentfilename)
1217  {
1218  using namespace std;
1219  hsize_t currentsize;
1220  H5Fget_filesize(filehandle,&currentsize);
1221  string newfilename(currentfilename);
1222  if (maxsize < currentsize)
1223  {
1224  H5Fflush(filehandle,H5F_SCOPE_LOCAL);
1225  H5Fclose(filehandle);
1226 
1227  size_t found = newfilename.rfind("__");
1228  if (found == string::npos)
1229  {
1230  newfilename.insert(newfilename.find_last_of("."),"__0001");
1231  }
1232  else
1233  {
1234 // int filenumber = atoi(newfilename.substr(found+3,found+6).c_str());
1235 // ++filenumber;
1236 // stringstream ss;
1237 // ss << currentfilename.substr(0,found+2)
1238 // <<setw(4)<< setfill('0')<<filenumber
1239 // << currentfilename.substr(found+6,currentfilename.length());
1240 // newfilename = ss.str();
1241  }
1242  filehandle = H5Fcreate(newfilename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
1243  }
1244  return newfilename;
1245  }
1246 
1247  /** check if a groups exists
1248  *
1249  * for now just checks if an absolute path exists in the file. Need to turn
1250  * off error output, since the command will issue an error when the group does
1251  * not exist.
1252  *
1253  * @todo iterate through everthing to get rid of the disabling of the error
1254  * messaging
1255  *
1256  * @return true when the group exists, false otherwise
1257  * @param groupname the name of the group in absolute path
1258  */
1259  bool groupExists(const std::string& groupname)
1260  {
1261  H5Eset_auto(H5E_DEFAULT,0,0);
1262  H5G_info_t dummy;
1263  return (!(H5Gget_info_by_name(_fileid, groupname.c_str(),&dummy,H5P_DEFAULT) < 0));
1264  }
1265 
1266  /** make sure that the requested group for the datset exists
1267  *
1268  * strip off the datasetname and check if the group exists. If it doesn't
1269  * create the group once. Do this with each subgroup until the full path
1270  * is checked.
1271  *
1272  * @param name absolute path containing also the datasetname
1273  */
1274  void ensureGroupExists(const std::string& name)
1275  {
1276  using namespace std;
1277  string wholename(name);
1278  /** prepend a '/' if its not there to ensure that it is a absolute path */
1279  wholename = "/" + name;
1280  string gname(wholename.substr(0, wholename.find_last_of('/')+1));
1281  for (int i=0; i < static_cast<int>(gname.length()); ++i)
1282  {
1283  if(gname[i] == '/')
1284  {
1285  string groupname(gname.substr(0,i));
1286  if (!groupname.empty() && !groupExists(groupname))
1287  {
1288  hid_t gh(H5Gcreate(_fileid, groupname.c_str() ,H5P_DEFAULT,
1289  H5P_DEFAULT, H5P_DEFAULT));
1290  H5Gclose(gh);
1291  }
1292  }
1293  }
1294  }
1295 
1296 
1297 private:
1298  /** the file handle */
1299  hid_t _fileid;
1300 };
1301 }//end namespace hdf5
~Handler()
destructor
void readArray(std::vector< type > &array, size_t &arrayLength, const std::string &valname)
read a array with a given name into a linearized array
Handler(const std::string &filename, const std::string &mode="w")
constructor opening the file
void open(const std::string &filename, const std::string &mode="w", int size=-1)
open a file
std::vector< hsize_t > count
the count
Definition: hdf5_handle.hpp:76
A handler for h5 files.
hid_t H5Type< uint8_t >()
trait implementation for unsigned 8 bit int
hid_t H5Type< uint64_t >()
trait implementation for unsigned 64 bit int
type readScalar(const std::string &valname)
read an scalar value with a given name as part of a given group
Handler()
default constructor
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
STL namespace.
void appendData(const std::vector< type > &data, shape_t shape, const std::string &valname, int compressLevel=2)
create/append data to a multidimensional dataset
things written only at end of run H5Dump ProcessorSummary size
void ensureGroupExists(const std::string &name)
make sure that the requested group for the datset exists
std::string reopenFile(int &filehandle, size_t maxsize, const std::string &currentfilename)
check filesize and open new file if too big
dsetList_t datasets() const
get the list of datasets in the file
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
hid_t H5Type< float >()
trait implementation for float
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 ...
additional info
shape_t shape(const std::string &valname) const
get the shape of a dataset with a given name
herr_t group_iterator_func(hid_t loc_id, const char *name, const H5L_info_t *, void *slist)
function to gather groups of h5 file
hid_t H5Type< uint32_t >()
trait implementation for unsigned 16 bit int
DatasetError(const std::string &message)
explicit constructor
Definition: hdf5_handle.hpp:35
Exception thrown when there is an error with the dataset.
Definition: hdf5_handle.hpp:28
hid_t H5Type< double >()
trait implementation for double
std::vector< hsize_t > offset
the offset of the partial data within the original data
Definition: hdf5_handle.hpp:59
std::vector< hsize_t > shape_t
define the shape type
Definition: hdf5_handle.hpp:43
hid_t _fileid
the file handle
hid_t H5Type()
traits for matching a build in type with the corresponding h5 type
Definition: hdf5_handle.hpp:93
herr_t dataset_iterator_func(hid_t, const char *name, const H5O_info_t *info, void *dlist)
function to gather all datasets of the h5 file
define the partiality parameter container
Definition: hdf5_handle.hpp:49
std::string filename() const
retrieve the filename of the file associated with the handler
void close()
close the file used by this handler
std::string readString(const std::string &dsetName)
read a string dataset
virtual ~DatasetError()
Definition: hdf5_handle.hpp:39
size_t dimension(const std::string &valname) const
get the dimension of a value with a given name
std::vector< hsize_t > block
the block
Definition: hdf5_handle.hpp:84
std::vector< hsize_t > stride
the stride
Definition: hdf5_handle.hpp:68
auxiliary data[Processor]
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
definitions of when a hit is found
dsetList_t rootGroups() const
get the list of groups of the root group in the file
void writeString(const std::string &string, const std::string &dsetName)
write a string dataset
std::vector< hsize_t > dims
the dimensions of the partial dataset
Definition: hdf5_handle.hpp:52
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
std::list< std::string > dsetList_t
define the list of dataset names
Definition: hdf5_handle.hpp:46
offset
bool groupExists(const std::string &groupname)
check if a groups exists
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
Handler(const Handler &)
prevent copy construction
hid_t H5Type< char >()
trait implementation for char
void writeScalar(const type value, const std::string &valname)
write an scalar value with a given name as part of a given group
type readScalarAttribute(const std::string &valname, const std::string &dsetName)
read an scalar attribute with a given name as part of a given dataset
hid_t H5Type< uint16_t >()
trait implementation for unsigned 16 bit int
size_t currentFileSize() const
retrieve the size of the current file
check if there is some light in the chamber based upon the GMD value
and the shutter status[Processor]
hid_t H5Type< int >()
trait implementation for int