Field3D

Contains utility functions and classes for Hdf5 files. More...

Classes

class  H5Base
 Base class for all scoped Hdf5 util classes. More...
 
class  H5ScopedAget_space
 Scoped object - opens an attribute data space on creation and closes it on destruction. More...
 
class  H5ScopedAget_type
 Scoped object - opens an attribute data type on creation and closes it on destruction. More...
 
class  H5ScopedAopen
 Scoped object - Opens attribute by name and closes it on destruction. More...
 
class  H5ScopedAopenIdx
 Scoped object - Opens attribute by index and closes it on destruction. More...
 
class  H5ScopedDcreate
 Scoped object - creates a dataset on creation and closes it on destruction. More...
 
class  H5ScopedDget_space
 Scoped object - opens a dataset on creation and closes it on destruction. More...
 
class  H5ScopedDget_type
 Scoped object - opens a dataset on creation and closes it on destruction. More...
 
class  H5ScopedDopen
 Scoped object - opens a dataset on creation and closes it on destruction. More...
 
class  H5ScopedGcreate
 Scoped object - creates a group on creation and closes it on destruction. More...
 
class  H5ScopedGopen
 Scoped object - opens a group on creation and closes it on destruction. More...
 
class  H5ScopedScreate
 Scoped object - creates a dataspace on creation and closes it on destruction. More...
 
class  H5ScopedTget_native_type
 Scoped object - opens an native type id on creation and closes it on destruction. More...
 

Functions

FIELD3D_API bool checkHdf5Gzip ()
 Checks whether gzip is available in the current hdf5 library.
 
bool readAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, double &value)
 
bool readAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, float &value)
 
bool readAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, int &value)
 
bool readAttribute (hid_t location, const string &attrName, string &value)
 
bool readAttribute (hid_t location, const string &attrName, unsigned int attrSize, double &value)
 
bool readAttribute (hid_t location, const string &attrName, unsigned int attrSize, float &value)
 
bool readAttribute (hid_t location, const string &attrName, unsigned int attrSize, int &value)
 
bool writeAttribute (hid_t location, const string &attrName, const string &value)
 
bool writeAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, const double &value)
 
bool writeAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, const float &value)
 
bool writeAttribute (hid_t location, const string &attrName, std::vector< unsigned int > &attrSize, const int &value)
 
bool writeAttribute (hid_t location, const string &attrName, unsigned int attrSize, const double &value)
 
bool writeAttribute (hid_t location, const string &attrName, unsigned int attrSize, const float &value)
 
bool writeAttribute (hid_t location, const string &attrName, unsigned int attrSize, const int &value)
 
Read/write simple data to hdf5 location
template<typename T >
void writeSimpleData (hid_t location, const std::string &name, const std::vector< T > &data)
 Writes a simple linear data set to the given location.
 
template<typename T >
void readSimpleData (hid_t location, const std::string &name, std::vector< T > &data)
 Reads a simple linear data set from the given location.
 
Attribute reading
FIELD3D_API bool readAttribute (hid_t location, const std::string &attrName, std::string &value)
 Reads a string attribute.
 
FIELD3D_API bool readAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, int &value)
 Reads an int attribute of arbitrary size.
 
FIELD3D_API bool readAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, float &value)
 Reads a float attribute of arbitrary size.
 
FIELD3D_API bool readAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, double &value)
 Reads a double attribute of arbitrary size.
 
FIELD3D_API bool readAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, int &value)
 Reads a int attribute of arbitrary size and rank.
 
FIELD3D_API bool readAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, float &value)
 Reads a float attribute of arbitrary size and rank.
 
FIELD3D_API bool readAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, double &value)
 Reads a double attribute of arbitrary size and rank.
 
Attribute writing
FIELD3D_API bool writeAttribute (hid_t location, const std::string &attrName, const std::string &value)
 Writes a string attribute.
 
FIELD3D_API bool writeAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, const int &value)
 Writes an int attribute of arbitrary size.
 
FIELD3D_API bool writeAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, const float &value)
 Writes a float attribute of arbitrary size.
 
FIELD3D_API bool writeAttribute (hid_t location, const std::string &attrName, unsigned int attrSize, const double &value)
 Writes a double attribute of arbitrary size.
 
FIELD3D_API bool writeAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, const int &value)
 Writes a float attribute of arbitrary size and rank.
 
FIELD3D_API bool writeAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, const float &value)
 Writes a float attribute of arbitrary size and rank.
 
FIELD3D_API bool writeAttribute (hid_t location, const std::string &attrName, std::vector< unsigned int > &attrSize, const double &value)
 Writes a double attribute of arbitrary size and rank.
 

Detailed Description

Contains utility functions and classes for Hdf5 files.

Function Documentation

◆ readAttribute() [1/7]

bool Hdf5Util::readAttribute ( hid_t location,
const string & attrName,
string & value )

Definition at line 78 of file Hdf5Util.cpp.

79{
81
85
86 if (H5Aexists(location, attrName.c_str()) < 1)
87 throw MissingAttributeException("Couldn't find attribute " + attrName);
88
89 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
92
93 if (H5Aget_info(attr, &attrInfo) < 0) {
94 throw MissingAttributeException("Couldn't get attribute info " + attrName);
95 } else {
96 strLen = attrInfo.data_size;
97 }
98
100
101 if (typeClass != H5T_STRING)
102 throw MissingAttributeException("Bad attribute type class for " + attrName);
103
104 H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
105
106 std::vector<char> tempString(strLen + 1);
107
108 if (H5Aread(attr, nativeType, &tempString[0]) < 0)
109 throw MissingAttributeException("Couldn't read attribute " + attrName);
110
111 value = string(&tempString[0]);
112
113 return true;
114
115}
boost::recursive_mutex g_hdf5Mutex
Definition Hdf5Util.cpp:67
boost::recursive_mutex::scoped_lock GlobalLock
Definition Hdf5Util.h:78
#define FIELD3D_MTX_T
Definition StdMathLib.h:99
Scoped object - opens an attribute data space on creation and closes it on destruction.
Definition Hdf5Util.h:288
Scoped object - opens an attribute data type on creation and closes it on destruction.
Definition Hdf5Util.h:311
Scoped object - Opens attribute by name and closes it on destruction.
Definition Hdf5Util.h:114

References FIELD3D_MTX_T, and g_hdf5Mutex.

◆ readAttribute() [2/7]

bool Hdf5Util::readAttribute ( hid_t location,
const string & attrName,
unsigned int attrSize,
int & value )

Definition at line 120 of file Hdf5Util.cpp.

122{
124
126
127 if (H5Aexists(location, attrName.c_str()) < 1)
128 throw MissingAttributeException("Couldn't find attribute " + attrName);
129
130 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
133
135 throw MissingAttributeException("Bad attribute rank for attribute " +
136 attrName);
137
138 hsize_t dims[1];
140
141 if (dims[0] != attrSize)
142 throw MissingAttributeException("Invalid attribute size for attribute " +
143 attrName);
144
146
147 if (typeClass != H5T_INTEGER)
148 throw MissingAttributeException("Bad attribute type class for " +
149 attrName);
150
152
153 if (H5Aread(attr, nativeType, &value) < 0)
154 throw MissingAttributeException("Couldn't read attribute " + attrName);
155
156 return true;
157
158}
Scoped object - opens an native type id on creation and closes it on destruction.
Definition Hdf5Util.h:334

References FIELD3D_MTX_T, and g_hdf5Mutex.

◆ readAttribute() [3/7]

bool Hdf5Util::readAttribute ( hid_t location,
const string & attrName,
unsigned int attrSize,
float & value )

Definition at line 163 of file Hdf5Util.cpp.

165{
167
169
170 if (H5Aexists(location, attrName.c_str()) < 1)
171 throw MissingAttributeException("Couldn't find attribute " + attrName);
172
173 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
176
178 throw MissingAttributeException("Bad attribute rank for attribute " +
179 attrName);
180
181 hsize_t dims[1];
183
184 if (dims[0] != attrSize)
185 throw MissingAttributeException("Invalid attribute size for attribute " +
186 attrName);
187
189
190 if (typeClass != H5T_FLOAT)
191 throw MissingAttributeException("Bad attribute type class for " +
192 attrName);
193
195
196 if (H5Aread(attr, nativeType, &value) < 0)
197 throw MissingAttributeException("Couldn't read attribute " + attrName);
198
199 return true;
200}

References FIELD3D_MTX_T, and g_hdf5Mutex.

◆ readAttribute() [4/7]

bool Hdf5Util::readAttribute ( hid_t location,
const string & attrName,
unsigned int attrSize,
double & value )

Definition at line 205 of file Hdf5Util.cpp.

207{
209
211
212 if (H5Aexists(location, attrName.c_str()) < 0)
213 throw MissingAttributeException("Couldn't find attribute " + attrName);
214
215 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
218
220 throw MissingAttributeException("Bad attribute rank for attribute " +
221 attrName);
222
223 hsize_t dims[1];
225
226 if (dims[0] != attrSize)
227 throw MissingAttributeException("Invalid attribute size for attribute " +
228 attrName);
229
231
232 if (typeClass != H5T_FLOAT)
233 throw MissingAttributeException("Bad attribute type class for " +
234 attrName);
235
237
238 if (H5Aread(attr, nativeType, &value) < 0)
239 throw MissingAttributeException("Couldn't read attribute " + attrName);
240
241 return true;
242}

References FIELD3D_MTX_T, and g_hdf5Mutex.

◆ readAttribute() [5/7]

bool Hdf5Util::readAttribute ( hid_t location,
const string & attrName,
std::vector< unsigned int > & attrSize,
int & value )

Definition at line 247 of file Hdf5Util.cpp.

249{
251
253 size_t rank = attrSize.size();
254
255 if (H5Aexists(location, attrName.c_str()) < 0)
256 throw MissingAttributeException("Couldn't find attribute " + attrName);
257
258 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
261
262
264 throw MissingAttributeException("Bad attribute rank for attribute " +
265 attrName);
266
267 boost::scoped_array<hsize_t> dims(new hsize_t[rank]);
269
270 for (size_t i=0; i < rank; i++) {
271 if (dims[i] != attrSize[i])
272 throw MissingAttributeException("Invalid attribute size for attribute " +
273 attrName);
274 }
275
277
278 if (typeClass != H5T_INTEGER)
279 throw MissingAttributeException("Bad attribute type class for " +
280 attrName);
281
282 H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
283
284 if (H5Aread(attr, nativeType, &value) < 0)
285 throw MissingAttributeException("Couldn't read attribute " + attrName);
286
287 return true;
288}

References FIELD3D_MTX_T, and g_hdf5Mutex.

◆ readAttribute() [6/7]

bool Hdf5Util::readAttribute ( hid_t location,
const string & attrName,
std::vector< unsigned int > & attrSize,
float & value )

Definition at line 293 of file Hdf5Util.cpp.

295{
297
299 size_t rank = attrSize.size();
300
301 if (H5Aexists(location, attrName.c_str()) < 0)
302 throw MissingAttributeException("Couldn't find attribute " + attrName);
303
304 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
307
308
310 throw MissingAttributeException("Bad attribute rank for attribute " +
311 attrName);
312
313 boost::scoped_array<hsize_t> dims(new hsize_t[rank]);
315
316 for (size_t i=0; i < rank; i++) {
317 if (dims[i] != attrSize[i])
318 throw MissingAttributeException("Invalid attribute size for attribute " +
319 attrName);
320 }
321
323
324 if (typeClass != H5T_FLOAT)
325 throw MissingAttributeException("Bad attribute type class for " +
326 attrName);
327
328 H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
329
330 if (H5Aread(attr, nativeType, &value) < 0)
331 throw MissingAttributeException("Couldn't read attribute " + attrName);
332
333 return true;
334}

References FIELD3D_MTX_T, and g_hdf5Mutex.

◆ readAttribute() [7/7]

bool Hdf5Util::readAttribute ( hid_t location,
const string & attrName,
std::vector< unsigned int > & attrSize,
double & value )

Definition at line 339 of file Hdf5Util.cpp.

341{
343
345 size_t rank = attrSize.size();
346
347 if (H5Aexists(location, attrName.c_str()) < 0)
348 throw MissingAttributeException("Couldn't find attribute " + attrName);
349
350 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT);
353
354
356 throw MissingAttributeException("Bad attribute rank for attribute " +
357 attrName);
358
359 boost::scoped_array<hsize_t> dims(new hsize_t[rank]);
361
362 for (size_t i=0; i < rank; i++) {
363 if (dims[i] != attrSize[i])
364 throw MissingAttributeException("Invalid attribute size for attribute " +
365 attrName);
366 }
367
369
370 if (typeClass != H5T_FLOAT)
371 throw MissingAttributeException("Bad attribute type class for " +
372 attrName);
373
374 H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND);
375
376 if (H5Aread(attr, nativeType, &value) < 0)
377 throw MissingAttributeException("Couldn't read attribute " + attrName);
378
379 return true;
380}

References FIELD3D_MTX_T, and g_hdf5Mutex.

◆ writeAttribute() [1/7]

bool Hdf5Util::writeAttribute ( hid_t location,
const string & attrName,
const string & value )

Definition at line 385 of file Hdf5Util.cpp.

386{
388
389 hid_t attr = -1;
392
393 bool success = true;
394
396 if (attrSpace == -1)
397 success = false;
398
400 if (attrType == -1)
401 success = false;
402
403 if (value.size()) {
404 // if the string is null the following will return error
405 // which we don't want.
406 if (success && H5Tset_size(attrType, value.size()) == -1){
407 success = false;
408 }
409 }
410
411 if (success) {
413 attr = H5Acreate(location, attrName.c_str(), attrType, attrSpace,
415 }
416
417 if (attr == -1) {
418 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
419 success = false;
420 }
421
422 if (success && H5Awrite(attr, attrType, value.c_str()) == -1) {
423 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
424 success = false;
425 }
426
427 H5Aclose(attr);
430
431 return success;
432
433}
@ SevWarning
Definition Log.h:68
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity.
Definition Log.cpp:70

References FIELD3D_MTX_T, g_hdf5Mutex, Msg::print(), and Msg::SevWarning.

◆ writeAttribute() [2/7]

bool Hdf5Util::writeAttribute ( hid_t location,
const string & attrName,
unsigned int attrSize,
const int & value )

Definition at line 438 of file Hdf5Util.cpp.

440{
442
443 hid_t attr;
445 hsize_t dims[1];
446
447 dims[0] = attrSize;
448
450 if (attrSpace < 0)
451 return false;
452
454 return false;
455
456 attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_INT,
458 if (attr < 0) {
459 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
460 H5Aclose(attr);
462 return false;
463 }
464
465 if (H5Awrite(attr, H5T_NATIVE_INT, &value) < 0) {
466 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
467 H5Aclose(attr);
469 return false;
470 }
471
472 H5Aclose(attr);
474
475 return true;
476}

References FIELD3D_MTX_T, g_hdf5Mutex, Msg::print(), and Msg::SevWarning.

◆ writeAttribute() [3/7]

bool Hdf5Util::writeAttribute ( hid_t location,
const string & attrName,
unsigned int attrSize,
const float & value )

Definition at line 481 of file Hdf5Util.cpp.

483{
485
486 hid_t attr;
488 hsize_t dims[1];
489
490 dims[0] = attrSize;
491
493 if (attrSpace < 0)
494 return false;
495
497 return false;
498
499 attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_FLOAT,
501 if (attr < 0) {
502 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
503 H5Aclose(attr);
505 return false;
506 }
507
508 if (H5Awrite(attr, H5T_NATIVE_FLOAT, &value) < 0) {
509 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
510 H5Aclose(attr);
512 return false;
513 }
514
515 H5Aclose(attr);
517
518 return true;
519}

References FIELD3D_MTX_T, g_hdf5Mutex, Msg::print(), and Msg::SevWarning.

◆ writeAttribute() [4/7]

bool Hdf5Util::writeAttribute ( hid_t location,
const string & attrName,
unsigned int attrSize,
const double & value )

Definition at line 524 of file Hdf5Util.cpp.

526{
528
529 hid_t attr;
531 hsize_t dims[1];
532
533 dims[0] = attrSize;
534
536 if (attrSpace < 0)
537 return false;
538
540 return false;
541
542 attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_DOUBLE,
544 if (attr < 0) {
545 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
546 H5Aclose(attr);
548 return false;
549 }
550
551 if (H5Awrite(attr, H5T_NATIVE_DOUBLE, &value) < 0) {
552 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
553 H5Aclose(attr);
555 return false;
556 }
557
558 H5Aclose(attr);
560
561 return true;
562}

References FIELD3D_MTX_T, g_hdf5Mutex, Msg::print(), and Msg::SevWarning.

◆ writeAttribute() [5/7]

bool Hdf5Util::writeAttribute ( hid_t location,
const string & attrName,
std::vector< unsigned int > & attrSize,
const int & value )

Definition at line 568 of file Hdf5Util.cpp.

570{
572
573 hid_t attr;
575 size_t rank = attrSize.size();
576
577 boost::scoped_array<hsize_t> current_dims(new hsize_t[rank]);
578 boost::scoped_array<hsize_t> max_dims(new hsize_t[rank]);
579
580 for (size_t i=0; i < rank; i++)
582
583 for (size_t i=0; i < rank; i++)
585
587 if (attrSpace < 0)
588 return false;
589
591 current_dims.get(), max_dims.get()) < 0) {
592 return false;
593 }
594
595 attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_INT,
597 if (attr < 0) {
598 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
599 H5Aclose(attr);
601 return false;
602 }
603
604 if (H5Awrite(attr, H5T_NATIVE_INT, &value) < 0) {
605 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
606 H5Aclose(attr);
608 return false;
609 }
610
611 H5Aclose(attr);
613
614 return true;
615}

References FIELD3D_MTX_T, g_hdf5Mutex, Msg::print(), and Msg::SevWarning.

◆ writeAttribute() [6/7]

bool Hdf5Util::writeAttribute ( hid_t location,
const string & attrName,
std::vector< unsigned int > & attrSize,
const float & value )

Definition at line 620 of file Hdf5Util.cpp.

622{
624
625 hid_t attr;
627 size_t rank = attrSize.size();
628
629 boost::scoped_array<hsize_t> current_dims(new hsize_t[rank]);
630 boost::scoped_array<hsize_t> max_dims(new hsize_t[rank]);
631
632 for (size_t i=0; i < rank; i++)
634
635 for (size_t i=0; i < rank; i++)
637
639 if (attrSpace < 0)
640 return false;
641
643 current_dims.get(), max_dims.get()) < 0) {
644 return false;
645 }
646
647 attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_FLOAT,
649 if (attr < 0) {
650 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
651 H5Aclose(attr);
653 return false;
654 }
655
656 if (H5Awrite(attr, H5T_NATIVE_FLOAT, &value) < 0) {
657 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
658 H5Aclose(attr);
660 return false;
661 }
662
663 H5Aclose(attr);
665
666 return true;
667}

References FIELD3D_MTX_T, g_hdf5Mutex, Msg::print(), and Msg::SevWarning.

◆ writeAttribute() [7/7]

bool Hdf5Util::writeAttribute ( hid_t location,
const string & attrName,
std::vector< unsigned int > & attrSize,
const double & value )

Definition at line 672 of file Hdf5Util.cpp.

674{
676
677 hid_t attr;
679 size_t rank = attrSize.size();
680 boost::scoped_array<hsize_t> current_dims(new hsize_t[rank]);
681 boost::scoped_array<hsize_t> max_dims(new hsize_t[rank]);
682
683 for (size_t i=0; i < rank; i++)
685
686 for (size_t i=0; i < rank; i++)
688
690 if (attrSpace < 0)
691 return false;
692
694 current_dims.get(), max_dims.get()) < 0) {
695 return false;
696 }
697
698 attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_DOUBLE,
700 if (attr < 0) {
701 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName);
702 H5Aclose(attr);
704 return false;
705 }
706
707 if (H5Awrite(attr, H5T_NATIVE_DOUBLE, &value) < 0) {
708 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName);
709 H5Aclose(attr);
711 return false;
712 }
713
714 H5Aclose(attr);
716
717 return true;
718}

References FIELD3D_MTX_T, g_hdf5Mutex, Msg::print(), and Msg::SevWarning.