-
Notifications
You must be signed in to change notification settings - Fork 10
/
H5Easy.h
1032 lines (999 loc) · 33.9 KB
/
H5Easy.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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Steven Walton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* Class functions for an easier h5 read and write method.
* Created by: Steven Walton
* Email: walton.stevenj@gmail.com
* Version: 1.2
*
* If you wish to contribute to this endeavour please email me or fork my branch from
* https://github.com/stevenwalton/H5Easy
*
*/
#ifndef H5RW_H
#define H5RW_H
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <typeinfo>
#include "H5Cpp.h"
using namespace H5;
class WriteH5
{
private:
std::string variable;
std::string filename;
public:
// sets our filename and our variable name
void setFileName ( std::string name ) {filename = name;};
void setVarName ( std::string name ) {variable = name;};
// Functions to be overloaded
template<typename T>
void writeData(const std::vector<T>&);
template<typename T>
void writeData(const std::vector<std::vector<T> >&);
template<typename T>
void writeData(const T&);
void createGroup(std::string);
};
class LoadH5
{
private:
std::string variable;
std::string filename;
public:
// sets our filename and our variable name
void setFileName(std::string name) {filename = name;};
void setVarName(std::string name) {variable = name;};
// Read functions
int getDataint() const;
float getDatafloat() const;
double getDatadouble() const;
std::vector<int> getDataVint() const;
std::vector<float> getDataVfloat() const;
std::vector<double> getDataVDouble() const;
std::vector<std::vector<int> > getData2Dint() const;
std::vector<std::vector<float> > getData2Dfloat() const;
std::vector<std::vector<double> > getData2Ddouble() const;
// Return the size of the data
// Note that for multi-dim arrays that it gets the total size and not the size of a single row.
int getSize() const;
// We now make a proxy class so that we can overload the return type and use a single
// function to get data whether int or float. This could be made more advanced by
// adding more data types (such as double).
class Proxy
{
private:
LoadH5 const* myOwner;
public:
Proxy( const LoadH5* owner ) : myOwner( owner ) {}
operator int() const
{
return myOwner->getDataint();
}
operator float() const
{
return myOwner->getDatafloat();
}
operator double() const
{
return myOwner->getDatadouble();
}
operator std::vector<int>() const
{
return myOwner->getDataVint();
}
operator std::vector<float>() const
{
return myOwner->getDataVfloat();
}
operator std::vector<double>() const
{
return myOwner->getDataVDouble();
}
operator std::vector<std::vector<int> >() const
{
return myOwner->getData2Dint();
}
operator std::vector<std::vector<float> >() const
{
return myOwner->getData2Dfloat();
}
operator std::vector<std::vector<double> >() const
{
return myOwner->getData2Ddouble();
}
};
// Here we use the Proxy class to have a single getData function
Proxy getData() const {return Proxy(this);}
};
/*
* ************************************************************************************************
* ************************************ Write Functions *******************************************
* ************************************************************************************************
*/
// Numeric implementation of our write data function
// Only accepts numerical values. Integers, floats, or doubles
template<typename T>
void WriteH5::writeData(const T &data)
{
Exception::dontPrint();
uint itr = 0;
auto *a = new T{data};
char* type = (char*)(typeid(T).name());
int vrank = 0;
hsize_t dims[1];
dims[0] = 1;
const H5std_string FILE_NAME(WriteH5::filename);
H5std_string DATASET_NAME(WriteH5::variable);
while (true)
{
try
{
H5File file(FILE_NAME, H5F_ACC_RDWR);
DataSpace dsp = DataSpace(vrank, dims);
// int
if ( type == (char*)typeid(int).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::STD_I32LE, dsp);
dset.write(a, PredType::STD_I32LE);
dset.close();
}
// float
else if ( type == (char*)typeid(float).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::IEEE_F32LE, dsp);
dset.write(a, PredType::IEEE_F32LE);
dset.close();
}
else if ( type == (char*)typeid(double).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::IEEE_F64LE, dsp);
dset.write(a, PredType::IEEE_F64LE);
dset.close();
}
else
{
std::cout << "Unknown data type! EXITING" << std::endl;
exit(1);
}
dsp.close();
file.close();
delete a;
break;
}
catch (FileIException error)
{
H5File file(FILE_NAME, H5F_ACC_TRUNC);
file.close();
itr++;
if ( itr > 3)
{
std::cout << "We've tried too many times in the Int writing sequence" << std::endl;
break;
}
}
}
}
template<typename T>
void WriteH5::writeData(const std::vector<T> &data)
{
Exception::dontPrint();
uint itr = 0; // Used to ensure we don't get stuck in an infinite loop
uint npts = data.size(); // size of our data
auto *a = new T[npts]; // convert to an array
char* type = (char*)(typeid(a[0]).name());
int vrank = 1; // since we are using std::vectors we are storing everything in one dimension
// convert std::vector to array. H5 does not seem to like the pointer implementation
for (size_t i = 0; i < npts; ++i)
a[i] = data[i];
// conventional syntax for H5 data writing
hsize_t dims[1];
dims[0] = npts;
// Let's make sure we are doing what we want and output it to the std output
// We need to set these parameters for the H5 data file writing
const H5std_string FILE_NAME(WriteH5::filename);
H5std_string DATASET_NAME(WriteH5::variable);
// loop here will check if the file exists.
while (true)
{
// This assumes that the file already exists and will then write to the file
try
{
H5File file(FILE_NAME, H5F_ACC_RDWR);
DataSpace dsp = DataSpace(vrank,dims);
// int
if ( type == (char*)typeid(int).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::STD_I32LE, dsp);
dset.write(a, PredType::STD_I32LE);
dset.close();
}
// uint
else if ( type == (char*)typeid(uint).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::STD_U32LE, dsp);
dset.write(a, PredType::STD_U32LE);
dset.close();
}
// float
else if ( type == (char*)typeid(float).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::IEEE_F32LE, dsp);
dset.write(a, PredType::IEEE_F32LE);
dset.close();
}
// double
else if ( type == (char*)typeid(double).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::IEEE_F64LE, dsp);
dset.write(a, PredType::IEEE_F64LE);
dset.close();
}
else
{
std::cout << "Unknown data type! EXITING" << std::endl;
exit(1);
}
// remember to close everything and delete our arrays
dsp.close();
file.close();
delete[] a;
break;
}
// Here we are catching if the file does not exist. We will then create a new file and return
// back to the try statement
catch (FileIException error)
{
H5File file(FILE_NAME, H5F_ACC_TRUNC);
file.close();
// Just some warning that we have gone through this catch
itr++;
// This is to prevent us from getting caught in an infinite loop. While (true) loops
// are useful, but they can be dangerous. Always ensure some escape sequence. Could
// just use a for loop
if ( itr > 3)
{
std::cout << "We've tried too many times in the Int writing sequence" << std::endl;
break;
}
}
}
}
template<typename T>
void WriteH5::writeData(const std::vector<std::vector<T> > &data)
{
Exception::dontPrint();
uint itr = 0; // Used to ensure we don't get stuck in an infinite loop
uint dim1 = data.size(); // size of our data
uint dim2 = data[0].size();
auto a = new T[dim1*dim2]; // convert to an array
auto md= new T*[dim1];
for ( size_t i = 0; i < dim1; ++i )
md[i] = a + i*dim2;
int vrank = 2; // since we are using std::vectors we are storing everything in one dimension
// convert std::vector to array. H5 does not seem to like the pointer implementation
for (size_t i = 0; i < dim1; ++i)
for ( size_t j = 0; j < dim2; ++j )
md[i][j] = data[i][j];
// conventional syntax for H5 data writing
hsize_t dims[2];
dims[0] = (int)dim1;
dims[1] = (int)dim2;
//hid_t memspace_id = H5Screate_simple(vrank, dims, NULL);
// Let's make sure we are doing what we want and output it to the std output
// We need to set these parameters for the H5 data file writing
const H5std_string FILE_NAME(WriteH5::filename);
H5std_string DATASET_NAME(WriteH5::variable);
// loop here will check if the file exists.
while (true)
{
// This assumes that the file already exists and will then write to the file
try
{
H5File file(FILE_NAME, H5F_ACC_RDWR);
DataSpace dsp = DataSpace(vrank,dims);
// int
if ( typeid(T).name()== typeid(int).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::STD_I32LE, dsp);
dset.write(a, PredType::STD_I32LE);
dset.close();
}
// uint
else if ( typeid(T).name() == typeid(uint).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::STD_U32LE, dsp);
dset.write(a, PredType::STD_U32LE);
dset.close();
}
// float
else if ( typeid(T).name()== typeid(float).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::IEEE_F32LE, dsp);
dset.write(a, PredType::IEEE_F32LE);
dset.close();
}
// double
else if ( typeid(T).name()== typeid(double).name() )
{
DataSet dset = file.createDataSet(DATASET_NAME, PredType::IEEE_F64LE, dsp);
dset.write(a, PredType::IEEE_F64LE);
dset.close();
}
else
{
std::cout << "Unknown data type! EXITING" << std::endl;
exit(1);
}
// remember to close everything and delete our arrays
dsp.close();
file.close();
delete[] md;
delete a;
break;
}
// Here we are catching if the file does not exist. We will then create a new file and return
// back to the try statement
catch (FileIException error)
{
H5File file(FILE_NAME, H5F_ACC_TRUNC);
file.close();
// Just some warning that we have gone through this catch
itr++;
// This is to prevent us from getting caught in an infinite loop. While (true) loops
// are useful, but they can be dangerous. Always ensure some escape sequence. Could
// just use a for loop
if ( itr > 3)
{
std::cout << "We've tried too many times in the Int writing sequence" << std::endl;
break;
}
}
}
}
void WriteH5::createGroup(std::string groupName)
{
try
{
H5std_string FILE_NAME(WriteH5::filename);
std::istringstream ss(groupName);
std::string token;
std::vector<std::string> groupSections;
while ( std::getline(ss, token, '/') )
groupSections.push_back(token);
std::string mygroup;
for ( size_t i = 0; i < groupSections.size(); ++i )
{
mygroup.append("/");
mygroup.append(groupSections[i]);
if ( mygroup != "/" )
{
H5File file(FILE_NAME, H5F_ACC_RDWR);
Group group(file.createGroup(mygroup));
group.close();
file.close();
}
}
}
catch (FileIException error)
{
error.printError();
}
catch (GroupIException error)
{
error.printError();
}
}
/*
* ************************************************************************************************
* ************************************ Read Functions ********************************************
* ************************************************************************************************
*/
int LoadH5::getSize() const
{
try
{
Exception::dontPrint();
H5std_string FILE_NAME(LoadH5::filename);
H5File file(FILE_NAME, H5F_ACC_RDONLY);
DataSet dataset = file.openDataSet(LoadH5::variable);
DataSpace dataspace = dataset.getSpace();
const int npts = dataspace.getSimpleExtentNpoints();
return npts;
}
catch (FileIException error)
{
error.printError();
int err = -1;
return err;
}
catch (GroupIException error)
{
error.printError();
int err = -1;
return err;
}
}
int LoadH5::getDataint() const
{
try
{
Exception::dontPrint();
H5std_string FILE_NAME(LoadH5::filename);
H5File file(FILE_NAME, H5F_ACC_RDONLY); // Only reads
DataSet dataset = file.openDataSet(LoadH5::variable);
DataType datatype = dataset.getDataType();
DataSpace dataspace = dataset.getSpace();
H5T_class_t classt = datatype.getClass();
if ( classt != 0 )
{
std::cout << LoadH5::variable << " is not an int... you can't save this as an int." << std::endl;
exit(1);
}
int *data = new int;
IntType itype = dataset.getIntType();
H5std_string order_string;
FloatType ftype = dataset.getFloatType();
H5T_order_t order = ftype.getOrder( order_string);
size_t size = ftype.getSize();
if ( order == 0 && size == 1 )
dataset.read(data, PredType::STD_I8LE); // Our standard integer
else if ( order== 0 && size == 2 )
dataset.read(data, PredType::STD_I16LE); // Our standard integer
else if ( order== 0 && size == 4 )
dataset.read(data, PredType::STD_I32LE); // Our standard integer
else if ( order== 0 && size == 8 )
dataset.read(data, PredType::STD_I64LE);
else if ( order== 1 && size == 1 )
dataset.read(data, PredType::STD_I8BE); // Our standard integer
else if ( order== 1 && size == 2 )
dataset.read(data, PredType::STD_I16BE); // Our standard integer
else if ( order== 1 && size == 4 )
dataset.read(data, PredType::STD_I32BE);
else if ( order== 1 && size == 8 )
dataset.read(data, PredType::STD_I64BE);
else
std::cout << "Did not find data type" << std::endl;
// Manage our memory properly
int v = *data;
delete data;
dataspace.close();
datatype.close();
dataset.close();
file.close();
return v;
}
catch (FileIException error)
{
error.printError();
int err = -1;
return err;
}
catch (GroupIException error)
{
error.printError();
int err = -1;
return err;
}
}
float LoadH5::getDatafloat() const
{
try
{
Exception::dontPrint();
//std::cout << "Getting float data" << std::endl;
H5std_string FILE_NAME(LoadH5::filename);
H5File file(FILE_NAME, H5F_ACC_RDONLY);
DataSet dataset = file.openDataSet(LoadH5::variable);
DataType datatype = dataset.getDataType();
DataSpace dataspace = dataset.getSpace();
H5T_class_t classt = datatype.getClass();
if ( classt != 1 )
{
std::cout << LoadH5::variable << " is not a float... you can't save this as a float." << std::endl;
exit(1);
}
FloatType ftype = dataset.getFloatType();
H5std_string order_string;
H5T_order_t order = ftype.getOrder( order_string);
size_t size = ftype.getSize();
float *data = new float;
if ( order == 0 && size == 4 )
dataset.read(data, PredType::IEEE_F32LE); // Our standard integer
else if ( order == 0 && size == 8 )
{
dataset.read((float*)data, PredType::IEEE_F64LE);
std::cout << "NOTE: This is actually double data. We are casting to float" << std:: endl;
}
else if ( order == 1 && size == 4 )
dataset.read(data, PredType::IEEE_F32BE);
else if ( (order_string == "Big endian byte order_stringing (1)" || order == 1) && size == 8 )
{
std::cout << "NOTE: This is actually double data. We are casting to float" << std:: endl;
dataset.read((float*)data, PredType::IEEE_F64BE);
}
else
std::cout << "Did not find data type" << std::endl;
float v = *data;
delete data;
dataspace.close();
datatype.close();
dataset.close();
file.close();
return v;
}
catch (FileIException error)
{
error.printError();
float err = -1.;
return err;
}
catch (GroupIException error)
{
error.printError();
float err = -1.;
return err;
}
}
double LoadH5::getDatadouble() const
{
try
{
Exception::dontPrint();
H5std_string FILE_NAME(LoadH5::filename);
H5File file(FILE_NAME, H5F_ACC_RDONLY);
DataSet dataset = file.openDataSet(LoadH5::variable);
DataType datatype = dataset.getDataType();
DataSpace dataspace = dataset.getSpace();
H5T_class_t classt = datatype.getClass();
if ( classt != 1 )
{
std::cout << LoadH5::variable << " is not a float... you can't save this as a float." << std::endl;
exit(1);
}
FloatType ftype = dataset.getFloatType();
H5std_string order_string;
H5T_order_t order = ftype.getOrder( order_string);
size_t size = ftype.getSize();
double *data = new double;
if ( order==0 && size == 4 )
{
std::cout << "NOTE: This is actually float data. We are casting to double" << std:: endl;
dataset.read((double*)data, PredType::IEEE_F32LE); // Our standard integer
}
else if ( order == 0 && size == 8 )
dataset.read(data, PredType::IEEE_F64LE);
else if ( order == 1 && size == 4 )
{
std::cout << "NOTE: This is actually float data. We are casting to double" << std:: endl;
dataset.read((double*)data, PredType::IEEE_F32BE);
}
else if ( order ==1 && size == 8 )
dataset.read((double*)data, PredType::IEEE_F64BE);
else
std::cout << "Did not find data type" << std::endl;
float v = *data;
delete data;
dataspace.close();
datatype.close();
dataset.close();
file.close();
return v;
}
catch (FileIException error)
{
error.printError();
double err = -1.;
return err;
}
catch (GroupIException error)
{
error.printError();
double err = -1.;
return err;
}
}
// Our int loading function
std::vector<int> LoadH5::getDataVint() const
{
try
{
Exception::dontPrint();
//std::cout << "Getting int data" << std::endl;
H5std_string FILE_NAME(LoadH5::filename);
H5File file(FILE_NAME, H5F_ACC_RDONLY); // Only reads
DataSet dataset = file.openDataSet(LoadH5::variable);
DataType datatype = dataset.getDataType();
DataSpace dataspace = dataset.getSpace();
const int npts = dataspace.getSimpleExtentNpoints(); // Gets length of data
H5T_class_t classt = datatype.getClass(); // Gets the data type of the data
// Let's make a quick error check
if ( classt != 0 )
{
std::cout << LoadH5::variable << " is not an int... you can't save this as an int." << std::endl;
exit(1);
}
int *data = new int[npts]; // allocate at run time what the size will be
IntType itype = dataset.getIntType();
H5std_string order_string;
H5T_order_t order = itype.getOrder( order_string );
size_t size = itype.getSize();
if ( order == 0 && size == 1 )
dataset.read(data, PredType::STD_I8LE); // Our standard integer
else if ( order== 0 && size == 2 )
dataset.read(data, PredType::STD_I16LE); // Our standard integer
else if ( order== 0 && size == 4 )
dataset.read(data, PredType::STD_I32LE); // Our standard integer
else if ( order== 0 && size == 8 )
dataset.read(data, PredType::STD_I64LE);
else if ( order== 1 && size == 1 )
dataset.read(data, PredType::STD_I8BE); // Our standard integer
else if ( order== 1 && size == 2 )
dataset.read(data, PredType::STD_I16BE); // Our standard integer
else if ( order== 1 && size == 4 )
dataset.read(data, PredType::STD_I32BE);
else if ( order== 1 && size == 8 )
dataset.read(data, PredType::STD_I64BE);
else
std::cout << "Did not find data type" << std::endl;
std::vector<int> v(data, data + npts); // Arrays are nice, but vectors are better
// Manage our memory properly
delete[] data;
dataspace.close();
datatype.close();
dataset.close();
file.close();
return v;
}
catch (FileIException error)
{
error.printError();
std::vector<int> err{1,-1};
return err;
}
catch (GroupIException error)
{
error.printError();
std::vector<int> err{1,-1};
return err;
}
}
// Same as our int function, but with float. Uses IEEE_F32BE
std::vector<float> LoadH5::getDataVfloat() const
{
try
{
Exception::dontPrint();
//std::cout << "Getting float data" << std::endl;
H5std_string FILE_NAME(LoadH5::filename);
H5File file(FILE_NAME, H5F_ACC_RDONLY);
DataSet dataset = file.openDataSet(LoadH5::variable);
DataType datatype = dataset.getDataType();
DataSpace dataspace = dataset.getSpace();
const int npts = dataspace.getSimpleExtentNpoints();
H5T_class_t classt = datatype.getClass();
if ( classt != 1 )
{
std::cout << LoadH5::variable << " is not a float... you can't save this as a float." << std::endl;
exit(1);
}
FloatType ftype = dataset.getFloatType();
H5std_string order_string;
H5T_order_t order = ftype.getOrder( order_string);
size_t size = ftype.getSize();
float *data = new float[npts];
if ( order == 0 && size == 4 )
dataset.read(data, PredType::IEEE_F32LE); // Our standard integer
else if ( order == 0 && size == 8 )
{
dataset.read((float*)data, PredType::IEEE_F64LE);
std::cout << "NOTE: This is actually double data. We are casting to float" << std:: endl;
}
else if ( order == 1 && size == 4 )
dataset.read(data, PredType::IEEE_F32BE);
else if ( (order_string == "Big endian byte order_stringing (1)" || order == 1) && size == 8 )
{
std::cout << "NOTE: This is actually double data. We are casting to float" << std:: endl;
dataset.read((float*)data, PredType::IEEE_F64BE);
}
else
std::cout << "Did not find data type" << std::endl;
std::vector<float> v(data, data + npts);
delete[] data;
dataspace.close();
datatype.close();
dataset.close();
file.close();
return v;
}
catch (FileIException error)
{
error.printError();
std::vector<float> err{1,-1.};
return err;
}
catch (GroupIException error)
{
error.printError();
std::vector<float> err{1,-1.};
return err;
}
}
// Same as our int function, but with double
std::vector<double> LoadH5::getDataVDouble() const
{
try
{
Exception::dontPrint();
H5std_string FILE_NAME(LoadH5::filename);
H5File file(FILE_NAME, H5F_ACC_RDONLY);
DataSet dataset = file.openDataSet(LoadH5::variable);
DataType datatype = dataset.getDataType();
DataSpace dataspace = dataset.getSpace();
const int npts = dataspace.getSimpleExtentNpoints();
H5T_class_t classt = datatype.getClass();
if ( classt != 1 )
{
std::cout << LoadH5::variable << " is not a float... you can't save this as a float." << std::endl;
exit(1);
}
FloatType ftype = dataset.getFloatType();
H5std_string order_string;
H5T_order_t order = ftype.getOrder( order_string);
size_t size = ftype.getSize();
double *data = new double[npts];
if ( order==0 && size == 4 )
{
std::cout << "NOTE: This is actually float data. We are casting to double" << std:: endl;
dataset.read((double*)data, PredType::IEEE_F32LE); // Our standard integer
}
else if ( order == 0 && size == 8 )
dataset.read(data, PredType::IEEE_F64LE);
else if ( order == 1 && size == 4 )
{
std::cout << "NOTE: This is actually float data. We are casting to double" << std:: endl;
dataset.read((double*)data, PredType::IEEE_F32BE);
}
else if ( order ==1 && size == 8 )
dataset.read((double*)data, PredType::IEEE_F64BE);
else
std::cout << "Did not find data type" << std::endl;
std::vector<double> v(data, data + npts);
delete[] data;
dataspace.close();
datatype.close();
dataset.close();
file.close();
return v;
}
catch (FileIException error)
{
error.printError();
std::vector<double> err{1,-1.};
return err;
}
catch (GroupIException error)
{
error.printError();
std::vector<double> err{1,-1.};
return err;
}
}
//
// 2-D VECTORS
//
// Same as our int function, but with double
// INT
std::vector<std::vector<int> > LoadH5::getData2Dint() const
{
try
{
Exception::dontPrint();
H5std_string FILE_NAME(LoadH5::filename);
H5File file(FILE_NAME, H5F_ACC_RDONLY);
DataSet dataset = file.openDataSet(LoadH5::variable);
DataType datatype = dataset.getDataType();
DataSpace dataspace = dataset.getSpace();
int rank = dataspace.getSimpleExtentNdims();
hsize_t dims[rank];
dataspace.getSimpleExtentDims(dims);
FloatType ftype = dataset.getFloatType();
H5std_string order_string;
H5T_order_t order = ftype.getOrder( order_string);
size_t size = ftype.getSize();
size_t dim1 = dims[0];
size_t dim2 = dims[1];
auto data = new int[dim1*dim2];
auto md = new int*[dim1];
for ( size_t i = 0; i < dim1; ++i )
md[i] = data + i*dim2;
if ( order==0 && size == 1 )
dataset.read(data, PredType::STD_I8LE); // Our standard integer
else if ( order == 1 && size == 1 )
dataset.read(data, PredType::STD_I8BE);
else if ( order == 0 && size == 2 )
dataset.read(data, PredType::STD_I16LE);
else if ( order == 1 && size == 2 )
dataset.read(data, PredType::STD_I16BE);
else if ( order == 0 && size == 4 )
dataset.read(data, PredType::STD_I32LE);
else if ( order == 1 && size == 4 )
dataset.read(data, PredType::STD_I32BE);
else if ( order == 0 && size == 8 )
dataset.read(data, PredType::STD_I64LE);
else if ( order == 1 && size == 8 )
dataset.read(data, PredType::STD_I64BE);
else
std::cout << "Did not find data type" << std::endl;
std::vector<std::vector<int> > v(dim1, std::vector<int>(dim2,0));//data, data + npts);
// Assign 2D vector
for ( size_t i = 0; i < dim1; ++i )
for ( size_t j = 0; j < dim2; ++j )
v[i][j] = md[i][j];
delete[] md;
delete data;
dataspace.close();
datatype.close();
dataset.close();
file.close();
return v;
}
catch (FileIException error)
{
error.printError();
std::vector<std::vector<int> > err{1,std::vector<int>(1,-1)};
return err;
}
catch (GroupIException error)
{
error.printError();
std::vector<std::vector<int> > err{1,std::vector<int>(1,-1)};
return err;
}
}
// FLOAT
std::vector<std::vector<float> > LoadH5::getData2Dfloat() const
{
try
{
Exception::dontPrint();
H5std_string FILE_NAME(LoadH5::filename);
H5File file(FILE_NAME, H5F_ACC_RDONLY);
DataSet dataset = file.openDataSet(LoadH5::variable);
DataType datatype = dataset.getDataType();
DataSpace dataspace = dataset.getSpace();
int rank = dataspace.getSimpleExtentNdims();
hsize_t dims[rank];
dataspace.getSimpleExtentDims(dims);
FloatType ftype = dataset.getFloatType();
H5std_string order_string;
H5T_order_t order = ftype.getOrder( order_string);
size_t size = ftype.getSize();
size_t dim1 = dims[0];
size_t dim2 = dims[1];
//float data[dim1][dim2];
auto data = new float[dim1*dim2];
auto md = new float*[dim1];
for ( size_t i = 0; i < dim1; ++i )
md[i] = data + i*dim2;
if ( order==0 && size == 4 )
dataset.read(data, PredType::IEEE_F32LE); // Our standard integer
else if ( order == 0 && size == 8 )
dataset.read(data, PredType::IEEE_F64LE);
else if ( order == 1 && size == 4 )
dataset.read(data, PredType::IEEE_F32BE);
else if ( order ==1 && size == 8 )
dataset.read(data, PredType::IEEE_F64BE);
else
std::cout << "Did not find data type" << std::endl;
std::vector<std::vector<float> > v(dim1, std::vector<float>(dim2,0));//data, data + npts);
// Assign 2D vector
for ( size_t i = 0; i < dim1; ++i )
for ( size_t j = 0; j < dim2; ++j )
v[i][j] = md[i][j];
delete[] md;
delete data;
dataspace.close();
datatype.close();
dataset.close();
file.close();
return v;
}
catch (FileIException error)
{
error.printError();
std::vector<std::vector<float> > err{1,std::vector<float>(1,-1.)};
return err;
}
catch (GroupIException error)
{
error.printError();
std::vector<std::vector<float> > err{1,std::vector<float>(1,-1.)};
return err;
}
}
// DOUBLE
std::vector<std::vector<double> > LoadH5::getData2Ddouble() const
{
try
{
Exception::dontPrint();
H5std_string FILE_NAME(LoadH5::filename);
H5File file(FILE_NAME, H5F_ACC_RDONLY);
DataSet dataset = file.openDataSet(LoadH5::variable);
DataType datatype = dataset.getDataType();
DataSpace dataspace = dataset.getSpace();
int rank = dataspace.getSimpleExtentNdims();
hsize_t dims[rank];
dataspace.getSimpleExtentDims(dims);
FloatType ftype = dataset.getFloatType();
H5std_string order_string;
H5T_order_t order = ftype.getOrder( order_string);
size_t size = ftype.getSize();
size_t dim1 = dims[0];
size_t dim2 = dims[1];
//double data[dim1][dim2];
auto data = new double[dim1*dim2];
auto md = new double*[dim1];
for ( size_t i = 0; i < dim1; ++i )
md[i] = data + i*dim2;
if ( order==0 && size == 4 )
dataset.read(data, PredType::IEEE_F32LE); // Our standard integer
else if ( order == 0 && size == 8 )
dataset.read(data, PredType::IEEE_F64LE);
else if ( order == 1 && size == 4 )
dataset.read(data, PredType::IEEE_F32BE);