-
Notifications
You must be signed in to change notification settings - Fork 3
/
H5Utilities.h
899 lines (805 loc) · 25.8 KB
/
H5Utilities.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/* ============================================================================
* Copyright (c) 2007-2019 BlueQuartz Software, LLC
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of BlueQuartz Software, the US Air Force, nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The code contained herein was partially funded by the following contracts:
* United States Air Force Prime Contract FA8650-04-C-5229
* United States Air Force Prime Contract FA8650-15-D-5231
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
#pragma once
#include <array>
#include <iostream>
#include <list>
#include <string>
#include <hdf5.h>
#include "H5Fpublic.h"
#include "H5Support/H5Lite.h"
#include "H5Support/H5Support.h"
/**
* Define the libraries features and file compatibility that will be used when opening
* or creating a file
*/
#if(H5_VERS_MINOR == 8)
#define HDF5_VERSION_LIB_LOWER_BOUNDS H5F_LIBVER_18
#define HDF5_VERSION_LIB_UPPER_BOUNDS H5F_LIBVER_LATEST
#endif
#if(H5_VERS_MINOR == 10)
#define HDF5_VERSION_LIB_LOWER_BOUNDS H5F_LIBVER_V110
#define HDF5_VERSION_LIB_UPPER_BOUNDS H5F_LIBVER_V110
#endif
#if(H5_VERS_MINOR == 12)
#define HDF5_VERSION_LIB_LOWER_BOUNDS H5F_LIBVER_V110
#define HDF5_VERSION_LIB_UPPER_BOUNDS H5F_LIBVER_V18
#endif
#if(H5_VERS_MINOR == 14)
#define HDF5_VERSION_LIB_LOWER_BOUNDS H5F_LIBVER_V110
#define HDF5_VERSION_LIB_UPPER_BOUNDS H5F_LIBVER_V110
#endif
#ifndef HDF5_VERSION_LIB_LOWER_BOUNDS
#error HDF5_VERSION_LIB_LOWER_BOUNDS is not defined. Please check the version of HDF5 that you are compiling against
#endif
namespace H5Support {
#define ENABLE_BITMASK_OPERATORS(x) \
template <> \
struct EnableBitMaskOperators<x> \
{ \
static const bool enable = true; \
};
template <typename Enum>
struct EnableBitMaskOperators
{
static const bool enable = false;
};
template <typename Enum>
typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type operator|(Enum lhs, Enum rhs)
{
using underlying = typename std::underlying_type<Enum>::type;
return static_cast<Enum>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs));
}
/**
* @brief General Utilities for working with the HDF5 data files and API
*/
namespace H5Utilities
{
enum class CustomHDFDataTypes : int32_t
{
Group = 1,
Dataset = 2,
Type = 4,
Link = 8,
Any = 15
};
// -----------HDF5 File Operations
/**
* @brief Opens a H5 file at path filename. Can be made read only access. Returns the id of the file object.
* @param filename
* @param readOnly
* @return
*/
inline hid_t openFile(const std::string& filename, bool readOnly = false)
{
H5SUPPORT_MUTEX_LOCK()
HDF_ERROR_HANDLER_OFF
hid_t fileID = -1;
if(readOnly)
{
fileID = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
}
else
{
/* Create a file access property list */
hid_t fileAccessPropertyList = H5Pcreate(H5P_FILE_ACCESS);
/* Set the file access property list */
H5Pset_libver_bounds(fileAccessPropertyList, HDF5_VERSION_LIB_LOWER_BOUNDS, HDF5_VERSION_LIB_UPPER_BOUNDS);
fileID = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
/* Close the file access property list object */
H5Pclose(fileAccessPropertyList);
}
HDF_ERROR_HANDLER_ON
return fileID;
}
/**
* @brief Creates a H5 file at path filename. Returns the id of the file object.
* @param filename
* @return
*/
inline hid_t createFile(const std::string& filename)
{
H5SUPPORT_MUTEX_LOCK()
/* Create a file access property list */
hid_t fileAccessPropertyList = H5Pcreate(H5P_FILE_ACCESS);
/* Set the fapl */
H5Pset_libver_bounds(fileAccessPropertyList, HDF5_VERSION_LIB_LOWER_BOUNDS, HDF5_VERSION_LIB_UPPER_BOUNDS);
/* Create a file with this file access property list */
hid_t fileID = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, fileAccessPropertyList);
/* Close the file access property list object */
H5Pclose(fileAccessPropertyList);
return fileID;
}
/**
* @brief Closes the object id
* @param locId The object id to close
* @return Negative value is error.
*/
inline herr_t closeHDF5Object(hid_t objectID)
{
H5SUPPORT_MUTEX_LOCK()
if(objectID < 0) // Object was not valid.
{
return 0;
}
H5I_type_t objectType;
herr_t err = -1; // default to an error
std::array<char, 1024> name;
name.fill(0);
objectType = H5Iget_type(objectID);
ssize_t charsRead = H5Iget_name(objectID, name.data(), name.size());
if(charsRead < 0)
{
std::cout << "Error Trying to get the name of an hdf object that was not closed. This is probably pretty bad. " << __FILE__ << "(" << __LINE__ << ")" << std::endl;
return -1;
}
switch(objectType)
{
case H5I_FILE:
err = H5Fclose(objectID);
break;
case H5I_GROUP:
// std::cout << "H5 Group Object left open. Id=" << objectID << " Name='" << name << "'" << std::endl;
err = H5Gclose(objectID);
break;
case H5I_DATASET:
// std::cout << "H5 Dataset Object left open. Id=" << objectID << " Name='" << name << "'" << std::endl;
err = H5Dclose(objectID);
break;
case H5I_ATTR:
// std::cout << "H5 Attribute Object left open. Id=" << objectID << " Name='" << name << "'" << std::endl;
err = H5Aclose(objectID);
break;
case H5I_DATATYPE:
// std::cout << "H5 DataType Object left open. Id=" << objectID << " Name='" << name << "'" << std::endl;
err = H5Tclose(objectID);
break;
case H5I_DATASPACE:
// std::cout << "H5 Data Space Object left open. Id=" << objectID << " Name='" << name << "'" << std::endl;
err = H5Sclose(objectID);
break;
default:
// std::cout << "Error unknown HDF object for closing: " << " Name='" << name << "'" << " Object Type=" << obj_type << std::endl;
err = -1;
}
return err;
}
/**
* @brief Closes a H5 file object. Returns the H5 error code.
* @param fileID
* @return
*/
inline herr_t closeFile(hid_t& fileID)
{
H5SUPPORT_MUTEX_LOCK()
herr_t err = 1;
if(fileID < 0) // fileID isn't open
{
return 1;
}
// Get the number of open identifiers of all types
// except files
ssize_t numOpen = H5Fget_obj_count(fileID, H5F_OBJ_DATASET | H5F_OBJ_GROUP | H5F_OBJ_DATATYPE | H5F_OBJ_ATTR | H5F_OBJ_LOCAL);
if(numOpen > 0)
{
std::cout << "WARNING: Some IDs weren't closed. Closing them." << std::endl;
std::vector<hid_t> attributeIDs(numOpen, 0);
H5Fget_obj_ids(fileID, H5F_OBJ_DATASET | H5F_OBJ_GROUP | H5F_OBJ_DATATYPE | H5F_OBJ_ATTR, numOpen, attributeIDs.data());
std::array<char, 1024> name;
for(const auto& id : attributeIDs)
{
name.fill(0);
ssize_t charsRead = H5Iget_name(id, name.data(), name.size());
if(charsRead < 0)
{
std::cout << "Error Trying to get the name of an hdf object that was not closed. This is probably pretty bad. " << __FILE__ << "(" << __LINE__ << ")" << std::endl;
return -1;
}
std::cout << "H5 Object left open. Id=" << id << " Name='" << name.data() << "'" << std::endl;
H5Utilities::closeHDF5Object(id);
}
}
err = H5Fclose(fileID);
if(err < 0)
{
std::cout << "Error Closing HDF5 File. " << err << std::endl;
}
fileID = -1;
return err;
}
// -------------- HDF Indentifier Methods ----------------------------
/**
* @brief Returns the path to an object
* @param objectID The HDF5 id of the object
* @param trim set to False to trim the path
* @return The path to the object relative to the objectID
*/
inline std::string getObjectPath(hid_t locationID, bool trim = false)
{
H5SUPPORT_MUTEX_LOCK()
size_t nameSize = 1 + H5Iget_name(locationID, nullptr, 0);
std::vector<char> objectName(nameSize, 0);
H5Iget_name(locationID, objectName.data(), nameSize);
std::string objectPath(objectName.data());
if((objectPath != "/") && (objectPath.at(0) == '/'))
{
objectPath.erase(0, 1);
}
return objectPath;
}
/**
* @brief Returns the hdf object type
* @param objectID The hdf5 object id
* @param objectName The path to the data set
* @param objectType The type of the object
* @return Negative value on error
*/
inline herr_t getObjectType(hid_t objectID, const std::string& objectName, int32_t& objectType)
{
H5SUPPORT_MUTEX_LOCK()
herr_t error = 1;
H5O_info_t objectInfo{};
error = H5Oget_info_by_name(objectID, objectName.c_str(), &objectInfo, H5P_DEFAULT);
if(error < 0)
{
return error;
}
objectType = objectInfo.type;
return error;
}
/**
* @brief Retrieves the object name for a given index
* @param fileID The hdf5 object id
* @param index The index to retrieve the name for
* @param name The variable to store the name
* @return Negative value is error
*/
inline herr_t objectNameAtIndex(hid_t fileID, int32_t index, std::string& name)
{
H5SUPPORT_MUTEX_LOCK()
ssize_t error = -1;
// call H5Gget_objname_by_idx with name as nullptr to get its length
ssize_t nameSize = H5Lget_name_by_idx(fileID, ".", H5_INDEX_NAME, H5_ITER_NATIVE, static_cast<hsize_t>(index), nullptr, 0, H5P_DEFAULT);
if(nameSize < 0)
{
name.clear();
return -1;
}
std::vector<char> buffer(nameSize + 1, 0);
error = H5Lget_name_by_idx(fileID, ".", H5_INDEX_NAME, H5_ITER_NATIVE, static_cast<hsize_t>(index), buffer.data(), buffer.size(), H5P_DEFAULT);
if(error < 0)
{
std::cout << "Error Trying to get the dataset name for index " << index << std::endl;
name.clear(); // Make an empty string if this fails
}
else
{
name.append(buffer.data()); // Append the string to the given string
}
return static_cast<herr_t>(error);
}
/**
* @brief Returns the path to an object's parent
* @param objectPath The HDF5 path to the object
* @param trim set to False to trim the path
* @return The path to the object relative to the objectID
*/
inline std::string getParentPath(const std::string& objectPath)
{
std::string parentPath = objectPath;
size_t start = parentPath.find_last_of('/');
parentPath.erase(start, parentPath.size() - start);
return parentPath;
}
/**
* @brief Returns the path to an object's parent
* @param objectID The HDF5 id of the object
* @param trim set to False to trim the path
* @return The path to the object relative to the objectID
*/
inline std::string getParentPath(hid_t objectID)
{
std::string objectPath = getObjectPath(objectID);
return getParentPath(objectPath);
}
/**
* @brief Returns the object's name from object path
* @param objectPath The HDF5 path to the object
* @return The object name
*/
inline std::string getObjectNameFromPath(const std::string& objectPath)
{
std::string str = objectPath;
size_t end = str.find_last_of('/');
str.erase(0, end + 1);
return str;
}
/**
* @brief Returns if a given hdf5 object is a group
* @param objectID The hdf5 object that contains an object with name objectName
* @param objectName The name of the object to check
* @return True if the given hdf5 object id is a group
*/
inline bool isGroup(hid_t nodeID, const std::string& objectName)
{
H5SUPPORT_MUTEX_LOCK()
bool isGroup = true;
herr_t error = -1;
H5O_info_t objectInfo{};
error = H5Oget_info_by_name(nodeID, objectName.c_str(), &objectInfo, H5P_DEFAULT);
if(error < 0)
{
std::cout << "Error in methd H5Gget_objinfo" << std::endl;
return false;
}
switch(objectInfo.type)
{
case H5O_TYPE_GROUP:
isGroup = true;
break;
case H5O_TYPE_DATASET:
isGroup = false;
break;
case H5O_TYPE_NAMED_DATATYPE:
isGroup = false;
break;
default:
isGroup = false;
}
return isGroup;
}
/**
* @brief objectExists Returns true if an object with the name exists in the given group or file
* @param nodeID
* @param objectName
* @return True if an object with the name exists
*/
inline bool objectExists(hid_t nodeID, const std::string& objectName)
{
H5SUPPORT_MUTEX_LOCK()
htri_t err = H5Oexists_by_name(nodeID, objectName.c_str(), H5P_DEFAULT);
return (err > 0);
}
/**
* @brief Opens an HDF5 object for hdf5 operations
* @param locId the Object id of the parent
* @param objectPath The path of the object to open
* @return The hdf5 id of the opened object. Negative value is error.
*/
inline hid_t openHDF5Object(hid_t locationID, const std::string& objectName)
{
H5SUPPORT_MUTEX_LOCK()
int32_t objectType = 0;
hid_t objectID;
herr_t error = 0;
HDF_ERROR_HANDLER_OFF;
error = getObjectType(locationID, objectName, objectType);
if(error < 0)
{
// std::cout << "Error: Unable to get object type for object: " << objectName << std::endl;
HDF_ERROR_HANDLER_ON;
return -1;
}
switch(objectType)
{
case H5O_TYPE_GROUP:
objectID = H5Gopen(locationID, objectName.c_str(), H5P_DEFAULT);
break;
case H5O_TYPE_DATASET:
objectID = H5Dopen(locationID, objectName.c_str(), H5P_DEFAULT);
break;
default:
std::cout << "Unknonwn HDF Type: " << objectType << std::endl;
objectID = -1;
}
HDF_ERROR_HANDLER_ON;
return objectID;
}
/**
* @brief Returns the associated string for the given HDF class type.
* @param classType
* @return
*/
inline std::string HDFClassTypeAsStr(hid_t classType)
{
switch(classType)
{
case H5T_INTEGER:
return "H5T_INTEGER";
break;
case H5T_FLOAT:
return "H5T_FLOAT";
break;
case H5T_STRING:
return "H5T_STRING";
break;
case H5T_TIME:
return "H5T_TIME";
break;
case H5T_BITFIELD:
return "H5T_BITFIELD";
break;
case H5T_OPAQUE:
return "H5T_OPAQUE";
break;
case H5T_COMPOUND:
return "H5T_COMPOUND";
break;
case H5T_REFERENCE:
return "H5T_REFERENCE";
break;
case H5T_ENUM:
return "H5T_ENUM";
break;
case H5T_VLEN:
return "H5T_VLEN";
break;
case H5T_ARRAY:
return "H5T_ARRAY";
break;
default:
return "OTHER";
}
}
/**
* @brief prints the class type of the given class
* @param classT The Class Type to print
*/
inline void printHDFClassType(H5T_class_t classType)
{
switch(classType)
{
case H5T_INTEGER:
std::cout << "H5T_INTEGER" << std::endl;
break;
case H5T_FLOAT:
std::cout << "H5T_FLOAT" << std::endl;
break;
case H5T_STRING:
std::cout << "H5T_STRING" << std::endl;
break;
case H5T_TIME:
std::cout << "H5T_TIME" << std::endl;
break;
case H5T_BITFIELD:
std::cout << "H5T_BITFIELD" << std::endl;
break;
case H5T_OPAQUE:
std::cout << "H5T_OPAQUE" << std::endl;
break;
case H5T_COMPOUND:
std::cout << "H5T_COMPOUND" << std::endl;
break;
case H5T_REFERENCE:
std::cout << "H5T_REFERENCE" << std::endl;
break;
case H5T_ENUM:
std::cout << "H5T_ENUM" << std::endl;
break;
case H5T_VLEN:
std::cout << "H5T_VLEN" << std::endl;
break;
case H5T_ARRAY:
std::cout << "H5T_ARRAY" << std::endl;
break;
default:
std::cout << "OTHER" << std::endl;
}
}
// -------------- HDF Group Methods ----------------------------
/**
* @brief Returns a list of child hdf5 objects for a given object id
* @param locationID The parent hdf5 id
* @param typeFilter A filter to apply to the list
* @param names Variable to store the list
* @return
*/
inline herr_t getGroupObjects(hid_t locationID, CustomHDFDataTypes typeFilter, std::list<std::string>& names)
{
H5SUPPORT_MUTEX_LOCK()
herr_t error = 0;
hsize_t numObjects = 0;
H5G_info_t groupInfo{};
error = H5Gget_info(locationID, &groupInfo);
if(error < 0)
{
// std::cout << "Error getting number of objects for group: " << locationID << std::endl;
return error;
}
numObjects = groupInfo.nlinks;
if(numObjects <= 0)
{
return 0; // no objects in group
}
size_t size = 0;
H5O_type_t type = H5O_TYPE_NTYPES;
std::vector<char> name;
for(hsize_t i = 0; i < numObjects; i++)
{
size = 1 + H5Lget_name_by_idx(locationID, ".", H5_INDEX_NAME, H5_ITER_INC, i, nullptr, 0, H5P_DEFAULT);
name.resize(size, 0);
H5Lget_name_by_idx(locationID, ".", H5_INDEX_NAME, H5_ITER_INC, i, name.data(), size, H5P_DEFAULT);
if(typeFilter == CustomHDFDataTypes::Any)
{
std::string objectName(name.data());
names.push_back(objectName);
}
else
{
H5O_info_t objectInfo{};
error = H5Oget_info_by_name(locationID, name.data(), &objectInfo, H5P_DEFAULT);
if(error >= 0)
{
type = objectInfo.type;
if(((type == H5O_TYPE_GROUP) && ((static_cast<int32_t>(CustomHDFDataTypes::Group) & static_cast<int32_t>(typeFilter)) != 0)) ||
((type == H5O_TYPE_DATASET) && ((static_cast<int32_t>(CustomHDFDataTypes::Dataset) & static_cast<int32_t>(typeFilter)) != 0)))
{
std::string objectName(name.data());
names.push_back(objectName);
}
}
}
}
return error;
}
/**
* @brief Creates a HDF Group by checking if the group already exists. If the
* group already exists then that group is returned otherwise a new group is
* created.
* @param locationID The HDF unique id given to files or groups
* @param group The name of the group to create. Note that this group name should
* not be any sort of 'path'. It should be a single group.
*/
inline hid_t createGroup(hid_t locationID, const std::string& group)
{
H5SUPPORT_MUTEX_LOCK()
hid_t groupID = -1;
herr_t error = -1;
H5O_info_t objectInfo{};
HDF_ERROR_HANDLER_OFF
error = H5Oget_info_by_name(locationID, group.c_str(), &objectInfo, H5P_DEFAULT);
// std::cout << "H5Gget_objinfo = " << err << " for " << group << std::endl;
if(error == 0)
{
groupID = H5Gopen(locationID, group.c_str(), H5P_DEFAULT);
}
else
{
groupID = H5Gcreate(locationID, group.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
}
// Turn the HDF Error handlers back on
HDF_ERROR_HANDLER_ON
return groupID;
}
/**
* @brief Given a path relative to the Parent ID, this method will create all
* the intermediate groups if necessary.
* @param pathToCheck The path to either create or ensure exists.
* @param parent The HDF unique id for the parent
* @return Error Condition: Negative is error. Positive is success.
*/
inline hid_t createGroupsFromPath(const std::string& pathToCheck, hid_t parent)
{
H5SUPPORT_MUTEX_LOCK()
hid_t groupID = 1;
herr_t error = -1;
std::string first;
std::string second;
std::string path(pathToCheck); // make a copy of the input
if(parent <= 0)
{
std::cout << "Bad parent Id. Returning from createGroupsFromPath" << std::endl;
return -1;
}
// remove any front slash
std::string::size_type pos = path.find_first_of('/', 0);
if(0 == pos)
{
path = path.substr(1, path.size());
}
else if(pos == std::string::npos) // Path contains only one element
{
groupID = H5Utilities::createGroup(parent, path);
if(groupID < 0)
{
std::cout << "Error creating group: " << path << " err:" << groupID << std::endl;
return groupID;
}
error = H5Gclose(groupID);
if(error < 0)
{
std::cout << "Error closing group during group creation." << std::endl;
return error;
}
return error; // Now return here as this was a special case.
}
// Remove any trailing slash
pos = path.find_last_of('/');
if(pos == (path.size() - 1)) // slash was in the last position
{
path = path.substr(0, pos);
}
if(path.empty())
{
return -1; // The path that was passed in was only a slash..
}
pos = path.find_first_of('/', 0);
if(pos == std::string::npos) // Only one element in the path
{
groupID = H5Utilities::createGroup(parent, path);
if(groupID < 0)
{
std::cout << "Error creating group '" << path << "' for group id " << groupID << std::endl;
return groupID;
}
error = H5Gclose(groupID);
return error;
}
while(pos != std::string::npos)
{
first = path.substr(0, pos);
second = path.substr(pos + 1, path.length());
groupID = H5Utilities::createGroup(parent, first);
if(groupID < 0)
{
std::cout << "Error creating group:" << groupID << std::endl;
return groupID;
}
error = H5Gclose(groupID);
pos = path.find_first_of('/', pos + 1);
if(pos == std::string::npos)
{
first += "/" + second;
groupID = createGroup(parent, first);
if(groupID < 0)
{
std::cout << "Error creating group:" << groupID << std::endl;
return groupID;
}
error = H5Gclose(groupID);
}
}
return error;
}
/**
* @brief Given a path relative to the Parent ID, this method will create all
* the intermediate groups if necessary.
* @param datasetPath The path to the dataset that you want to make all the intermediate groups for
* @param parent The HDF unique id for the parent
* @return Error Condition: Negative is error. Positive is success.
*/
inline hid_t createGroupsForDataset(const std::string& datasetPath, hid_t parent)
{
H5SUPPORT_MUTEX_LOCK()
// Generate the internal HDF dataset path and create all the groups necessary to write the dataset
std::string::size_type pos = 0;
pos = datasetPath.find_last_of('/');
// std::string parentPath;
if(pos != 0 && pos != std::string::npos)
{
std::string parentPath(datasetPath.substr(0, pos));
return H5Utilities::createGroupsFromPath(parentPath, parent);
}
// Make sure all the intermediary groups are in place in the HDF5 File
return 1;
}
/**
* @brief Extracts the object name from a given path
* @param path The path which to extract the object name
* @return The name of the object
*/
inline std::string extractObjectName(const std::string& path)
{
std::string::size_type pos;
pos = path.find_last_of('/');
if(pos == std::string::npos || path == "/")
{
return path;
}
return path.substr(pos + 1);
}
// -------------- HDF Attribute Methods ----------------------------
/**
* @brief Looks for an attribute with a given name
* @param locationID The objects Parent id
* @param objectName The name of the object
* @param attributeName The attribute to look for (by name)
* @return True if the attribute exists.
*/
inline bool probeForAttribute(hid_t locationID, const std::string& objectName, const std::string& attributeName)
{
H5SUPPORT_MUTEX_LOCK()
herr_t error = 0;
hid_t rank;
HDF_ERROR_HANDLER_OFF
error = H5Lite::getAttributeNDims(locationID, objectName, attributeName, rank);
HDF_ERROR_HANDLER_ON
return error >= 0;
}
/**
* @brief Returns a list of all the attribute names
* @param objectID The parent object
* @param names Variable to hold the list of attribute names
* @return Negate value is error
*/
inline herr_t getAllAttributeNames(hid_t objectID, std::list<std::string>& results)
{
H5SUPPORT_MUTEX_LOCK()
if(objectID < 0)
{
return -1;
}
herr_t error = -1;
hsize_t numAttributes;
hid_t attributeID;
size_t nameSize;
H5O_info_t objectInfo{};
error = H5Oget_info(objectID, &objectInfo);
numAttributes = objectInfo.num_attrs;
std::vector<char> attributeName;
for(hsize_t i = 0; i < numAttributes; i++)
{
attributeID = H5Aopen_by_idx(objectID, ".", H5_INDEX_NAME, H5_ITER_INC, i, H5P_DEFAULT, H5P_DEFAULT);
nameSize = 1 + H5Aget_name(attributeID, 0, nullptr);
attributeName.resize(nameSize, 0);
H5Aget_name(attributeID, nameSize, attributeName.data());
results.emplace_back(attributeName.data());
error = H5Aclose(attributeID);
}
return error;
}
/**
* @brief Returns a list of all the attribute names
* @param objectID The parent object
* @param objectName The name of the object whose attribute names you want a list
* @param names Variable to hold the list of attribute names
* @return Negative value is error
*/
inline herr_t getAllAttributeNames(hid_t locationID, const std::string& objectName, std::list<std::string>& names)
{
hid_t objectID = -1;
herr_t error = -1;
names.clear();
objectID = openHDF5Object(locationID, objectName);
if(objectID < 0)
{
return static_cast<herr_t>(objectID);
}
error = getAllAttributeNames(objectID, names);
error = closeHDF5Object(objectID);
return error;
}
}; // namespace H5Utilities
ENABLE_BITMASK_OPERATORS(H5Utilities::CustomHDFDataTypes)
}; // namespace H5Support