-
Notifications
You must be signed in to change notification settings - Fork 3
/
HDF5Array.cc
1859 lines (1539 loc) · 67.1 KB
/
HDF5Array.cc
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
// This file is part of hdf5_handler a HDF5 file handler for the OPeNDAP
// data server.
// Author: Hyo-Kyung Lee <hyoklee@hdfgroup.org> and Muqun Yang
// <myang6@hdfgroup.org>
// Copyright (c) 2009-2016 The HDF Group, Inc. and OPeNDAP, Inc.
//
// This is H5free_memory software; you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your
// option) any later version.
//
// This software is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
// You can contact The HDF Group, Inc. at 1800 South Oak Street,
// Suite 203, Champaign, IL 61820
/// \file HDF5Array.cc
/// \brief This file contains the implementation on data array reading for the default option.
///
/// It includes the methods to read data array into DAP buffer from an HDF5 dataset.
/// \author Hyo-Kyung Lee (hyoklee@hdfgroup.org)
/// \author Kent Yang (myang6@hdfgroup.org)
/// \author James Gallagher (jgallagher@opendap.org)
#include "config_hdf5.h"
#include <iostream>
#include <memory>
#include <sstream>
#include <algorithm>
#include <ctype.h>
#include <BESDebug.h>
#include <libdap/Error.h>
#include <libdap/InternalErr.h>
#include "HDF5Array.h"
#include "HDF5Structure.h"
#include "HDF5Str.h"
using namespace std;
using namespace libdap;
BaseType *HDF5Array::ptr_duplicate() {
return new HDF5Array(*this);
}
HDF5Array::HDF5Array(const string & n, const string &d, BaseType * v) :
Array(n, d, v) {
d_num_dim = 0;
d_num_elm = 0;
d_memneed = 0;
}
HDF5Array::~HDF5Array() {
}
int HDF5Array::format_constraint(int *offset, int *step, int *count) {
// For the 0-length array case, just return 0.
if(length() == 0)
return 0;
//long nels = 1;
int nels = 1;
int id = 0;
Dim_iter p = dim_begin();
while (p != dim_end()) {
int start = dimension_start(p, true);
int stride = dimension_stride(p, true);
int stop = dimension_stop(p, true);
// Check for empty constraint
if (start > stop) {
ostringstream oss;
oss << "Array/Grid hyperslab start point "<< start <<
" is greater than stop point " << stop <<".";
throw Error(malformed_expr, oss.str());
}
offset[id] = start;
step[id] = stride;
count[id] = ((stop - start) / stride) + 1; // count of elements
nels *= count[id]; // total number of values for variable
BESDEBUG("h5",
"=format_constraint():"
<< "id=" << id << " offset=" << offset[id]
<< " step=" << step[id]
<< " count=" << count[id]
<< endl);
id++;
p++;
}
return nels;
}
bool HDF5Array::read()
{
BESDEBUG("h5",
">read() dataset=" << dataset()
<< " dimension=" << d_num_dim
<< " data_size=" << d_memneed << " length=" << length()
<< endl);
hid_t file_id = H5Fopen(dataset().c_str(),H5F_ACC_RDONLY,H5P_DEFAULT);
BESDEBUG("h5","after H5Fopen "<<endl);
BESDEBUG("h5","variable name is "<<name() <<endl);
BESDEBUG("h5","variable path is "<<var_path <<endl);
hid_t dset_id = -1;
if(true == is_dap4())
dset_id = H5Dopen2(file_id,var_path.c_str(),H5P_DEFAULT);
else
dset_id = H5Dopen2(file_id,name().c_str(),H5P_DEFAULT);
BESDEBUG("h5","after H5Dopen2 "<<endl);
// Leave the following code. We may replace the struct DS and DSattr(see hdf5_handler.h)
#if 0
hid_t dspace_id = H5Dget_space(dset_id);
if(dspace_id < 0) {
H5Dclose(dset_id);
H5Fclose(file_id);
throw InternalErr(__FILE__,__LINE__, "Fail to obtain the dataspace .");
}
int num_dim = H5Sget_simple_extent_ndims(dspace_id);
if(num_dim < 0) {
H5Sclose(dspace_id);
H5Dclose(dset_id);
H5Fclose(file_id);
throw InternalErr(__FILE__,__LINE__, "Fail to obtain the datatype .");
}
H5Sclose(dspace_id);
#endif
hid_t dtype_id = H5Dget_type(dset_id);
if(dtype_id < 0) {
H5Dclose(dset_id);
H5Fclose(file_id);
throw InternalErr(__FILE__,__LINE__, "Fail to obtain the datatype .");
}
vector<int> offset(d_num_dim);
vector<int> count(d_num_dim);
vector<int> step(d_num_dim);
int nelms = format_constraint(&offset[0], &step[0], &count[0]); // Throws Error.
vector<char>values;
// We only map the reference to URL when the dataset is an array of reference.
if (get_dap_type(dtype_id,is_dap4()) == "Url") {
bool ret_ref = false;
try {
ret_ref = m_array_of_reference(dset_id,dtype_id);
H5Tclose(dtype_id);
H5Dclose(dset_id);
H5Fclose(file_id);
}
catch(...) {
H5Tclose(dtype_id);
H5Dclose(dset_id);
H5Fclose(file_id);
throw;
}
return ret_ref;
}
try {
do_array_read(dset_id,dtype_id,values,false,0,nelms,&offset[0],&count[0],&step[0]);
}
catch(...) {
H5Tclose(dtype_id);
H5Dclose(dset_id);
H5Fclose(file_id);
throw;
}
H5Tclose(dtype_id);
H5Dclose(dset_id);
H5Fclose(file_id);
return true;
}
void HDF5Array::do_array_read(hid_t dset_id,hid_t dtype_id,vector<char>&values,bool has_values,int values_offset,
int nelms,int* offset,int* count, int* step)
{
H5T_class_t tcls = H5Tget_class(dtype_id);
if(H5T_COMPOUND == tcls)
m_array_of_structure(dset_id,values,has_values,values_offset,nelms,offset,count,step);
else if(H5T_INTEGER == tcls || H5T_FLOAT == tcls || H5T_STRING == tcls)
m_array_of_atomic(dset_id,dtype_id,nelms,offset,count,step);
else {
throw InternalErr(__FILE__,__LINE__,"Fail to read the data for Unsupported datatype.");
}
}
void HDF5Array:: m_array_of_atomic(hid_t dset_id, hid_t dtype_id,
int nelms,int* offset,int* count, int* step)
{
hid_t memtype = -1;
if((memtype = H5Tget_native_type(dtype_id, H5T_DIR_ASCEND))<0) {
throw InternalErr (__FILE__, __LINE__, "Fail to obtain memory datatype.");
}
// First handle variable-length string
if (H5Tis_variable_str(memtype) && H5Tget_class(memtype) == H5T_STRING) {
vector<hsize_t> hoffset;
vector<hsize_t>hcount;
vector<hsize_t>hstep;
hoffset.resize(d_num_dim);
hcount.resize(d_num_dim);
hstep.resize(d_num_dim);
for (int i = 0; i <d_num_dim; i++) {
hoffset[i] = (hsize_t) offset[i];
hcount[i] = (hsize_t) count[i];
hstep[i] = (hsize_t) step[i];
}
vector<string>finstrval;
finstrval.resize(nelms);
try {
read_vlen_string(dset_id, nelms, &hoffset[0], &hstep[0], &hcount[0],finstrval);
}
catch(...) {
H5Tclose(memtype);
throw InternalErr(__FILE__,__LINE__,"Fail to read variable-length string.");
}
set_value(finstrval,nelms);
H5Tclose(memtype);
return ;
}
try {
if (nelms == d_num_elm) {
vector<char> convbuf(d_memneed);
get_data(dset_id, (void *) &convbuf[0]);
// Check if a Signed Byte to Int16 conversion is necessary, this is only valid for DAP2.
if(false == is_dap4()) {
if (1 == H5Tget_size(memtype) && H5T_SGN_2 == H5Tget_sign(memtype))
{
vector<short> convbuf2(nelms);
for (int i = 0; i < nelms; i++) {
convbuf2[i] = (signed char) (convbuf[i]);
BESDEBUG("h5", "convbuf[" << i << "]="
<< (signed char)convbuf[i] << endl);
BESDEBUG("h5", "convbuf2[" << i << "]="
<< convbuf2[i] << endl)
;
}
// Libdap will generate the wrong output.
m_intern_plain_array_data((char*) &convbuf2[0],memtype);
}
else
m_intern_plain_array_data(&convbuf[0],memtype);
}
else
m_intern_plain_array_data(&convbuf[0],memtype);
} // end of "if (nelms == d_num_elm)"
else {
size_t data_size = nelms * H5Tget_size(memtype);
if (data_size == 0) {
throw InternalErr(__FILE__, __LINE__, "get_size failed");
}
vector<char> convbuf(data_size);
get_slabdata(dset_id, &offset[0], &step[0], &count[0], d_num_dim, &convbuf[0]);
// Check if a Signed Byte to Int16 conversion is necessary.
if(false == is_dap4()){
if (1 == H5Tget_size(memtype) && H5T_SGN_2 == H5Tget_sign(memtype)) {
vector<short> convbuf2(data_size);
for (int i = 0; i < (int)data_size; i++) {
convbuf2[i] = static_cast<signed char> (convbuf[i]);
}
m_intern_plain_array_data((char*) &convbuf2[0],memtype);
}
else {
m_intern_plain_array_data(&convbuf[0],memtype);
}
}
else
m_intern_plain_array_data(&convbuf[0],memtype);
}
H5Tclose(memtype);
}
catch (...) {
H5Tclose(memtype);
throw;
}
}
bool HDF5Array::m_array_of_structure(hid_t dsetid, vector<char>&values,bool has_values,int values_offset,
int nelms,int* offset,int* count, int* step) {
BESDEBUG("h5", "=read() Array of Structure length=" << length() << endl);
hid_t mspace = -1;
hid_t memtype = -1;
hid_t dtypeid = -1;
size_t ty_size = -1;
if((dtypeid = H5Dget_type(dsetid)) < 0)
throw InternalErr (__FILE__, __LINE__, "Cannot obtain the datatype.");
if((memtype = H5Tget_native_type(dtypeid, H5T_DIR_ASCEND))<0) {
H5Tclose(dtypeid);
throw InternalErr (__FILE__, __LINE__, "Fail to obtain memory datatype.");
}
ty_size = H5Tget_size(memtype);
if (ty_size == 0) {
H5Tclose(memtype);
H5Tclose(dtypeid);
throw InternalErr (__FILE__, __LINE__,"Fail to obtain the size of HDF5 compound datatype.");
}
if(false == has_values) {
hid_t dspace = -1;
if ((dspace = H5Dget_space(dsetid))<0) {
H5Tclose(memtype);
H5Tclose(dtypeid);
throw InternalErr (__FILE__, __LINE__, "Cannot obtain data space.");
}
d_num_dim = H5Sget_simple_extent_ndims(dspace);
if(d_num_dim < 0) {
H5Tclose(memtype);
H5Tclose(dtypeid);
H5Sclose(dspace);
throw InternalErr (__FILE__, __LINE__, "Cannot obtain the number of dimensions of the data space.");
}
vector<hsize_t> hoffset;
vector<hsize_t>hcount;
vector<hsize_t>hstep;
hoffset.resize(d_num_dim);
hcount.resize(d_num_dim);
hstep.resize(d_num_dim);
for (int i = 0; i <d_num_dim; i++) {
hoffset[i] = (hsize_t) offset[i];
hcount[i] = (hsize_t) count[i];
hstep[i] = (hsize_t) step[i];
}
if (H5Sselect_hyperslab(dspace, H5S_SELECT_SET,
&hoffset[0], &hstep[0],
&hcount[0], NULL) < 0) {
H5Tclose(memtype);
H5Tclose(dtypeid);
H5Sclose(dspace);
throw InternalErr (__FILE__, __LINE__, "Cannot generate the hyperslab of the HDF5 dataset.");
}
mspace = H5Screate_simple(d_num_dim, &hcount[0],NULL);
if (mspace < 0) {
H5Sclose(dspace);
H5Tclose(memtype);
H5Tclose(dtypeid);
throw InternalErr (__FILE__, __LINE__, "Cannot create the memory space.");
}
values.resize(nelms*ty_size);
hid_t read_ret = -1;
read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,(void*)&values[0]);
if (read_ret < 0) {
H5Tclose(memtype);
H5Tclose(dtypeid);
H5Sclose(dspace);
throw InternalErr (__FILE__, __LINE__, "Fail to read the HDF5 compound datatype dataset.");
}
H5Sclose(dspace);
has_values = true;
} // end of "if(false == has_values)" block
HDF5Structure *h5s = NULL;
hid_t memb_id = -1;
char* memb_name = NULL;
try {
// Loop through all the elements in this compound datatype array.
for (int element = 0; element < nelms; ++element) {
h5s = dynamic_cast<HDF5Structure*>(var()->ptr_duplicate());
H5T_class_t memb_cls = H5T_NO_CLASS;
int nmembs = 0;
size_t memb_offset = 0;
if((nmembs = H5Tget_nmembers(memtype)) < 0)
throw InternalErr (__FILE__, __LINE__, "Fail to obtain number of HDF5 compound datatype.");
for(unsigned int u = 0; u < (unsigned)nmembs; u++) {
// Get member type ID
if((memb_id = H5Tget_member_type(memtype, u)) < 0)
throw InternalErr (__FILE__, __LINE__, "Fail to obtain the datatype of an HDF5 compound datatype member.");
// Get member type class
if((memb_cls = H5Tget_member_class (memtype, u)) < 0)
throw InternalErr (__FILE__, __LINE__, "Fail to obtain the datatype class of an HDF5 compound datatype member.");
// Get member offset,H5Tget_member_offset only fails
// when H5Tget_memeber_class fails. Sinc H5Tget_member_class
// is checked above. So no need to check the return value.
memb_offset= H5Tget_member_offset(memtype,u);
// Get member name
memb_name = H5Tget_member_name(memtype,u);
if(memb_name == NULL)
throw InternalErr (__FILE__, __LINE__, "Fail to obtain the name of an HDF5 compound datatype member.");
BaseType *field = h5s->var(memb_name);
if (memb_cls == H5T_COMPOUND) {
HDF5Structure &memb_h5s = dynamic_cast<HDF5Structure&>(*field);
memb_h5s.do_structure_read(dsetid, memb_id,values,has_values,memb_offset+values_offset+ty_size*element);
}
else if(memb_cls == H5T_ARRAY) {
// memb_id, obtain the number of dimensions
int at_ndims = H5Tget_array_ndims(memb_id);
if(at_ndims <= 0)
throw InternalErr (__FILE__, __LINE__, "Fail to obtain number of dimensions of the array datatype.");
#if 0
//HDF5Array &h5_array_type = dynamic_cast<HDF5Array&>(*h5s->var(memb_name));
#endif
HDF5Array &h5_array_type = dynamic_cast<HDF5Array&>(*field);
vector<int> at_offset(at_ndims,0);
vector<int> at_count(at_ndims,0);
vector<int> at_step(at_ndims,0);
int at_nelms = h5_array_type.format_constraint(&at_offset[0],&at_step[0],&at_count[0]);
// Read the array data
h5_array_type.do_h5_array_type_read(dsetid,memb_id,values,has_values,memb_offset+values_offset+ty_size*element,
at_nelms,&at_offset[0],&at_count[0],&at_step[0]);
}
else if(memb_cls == H5T_INTEGER || memb_cls == H5T_FLOAT) {
if(true == promote_char_to_short(memb_cls,memb_id)) {
void *src = (void*)(&values[0] + (element*ty_size) + values_offset +memb_offset);
char val_int8;
memcpy(&val_int8,src,1);
short val_short=(short)val_int8;
field->val2buf(&val_short);
}
else {
field->val2buf(&values[0] + (element*ty_size) + values_offset +memb_offset);
}
}
else if(memb_cls == H5T_STRING) {
// distinguish between variable length and fixed length
if(true == H5Tis_variable_str(memb_id)) {
void *src = (void*)(&values[0]+(element*ty_size)+values_offset + memb_offset);
string final_str;
get_vlen_str_data((char*)src,final_str);
field->val2buf(&final_str);
}
else {// Obtain fixed-size string value
void *src = (void*)(&values[0]+(element*ty_size)+values_offset + memb_offset);
vector<char> str_val;
size_t memb_size = H5Tget_size(memb_id);
if (memb_size == 0) {
H5Tclose(memb_id);
H5free_memory(memb_name);
delete h5s;
throw InternalErr (__FILE__, __LINE__,"Fail to obtain the size of HDF5 compound datatype.");
}
str_val.resize(memb_size);
memcpy(&str_val[0],src,memb_size);
string temp_string(str_val.begin(),str_val.end());
field->val2buf(&temp_string);
}
}
else {
H5free_memory(memb_name);
H5Tclose(memb_id);
delete h5s;
throw InternalErr (__FILE__, __LINE__,
"Only support the field of compound datatype when the field type class is integer, float, string, array or compound..");
}
// Close member type ID
H5Tclose(memb_id);
H5free_memory(memb_name);
field->set_read_p(true);
} // end "for(unsigned u = 0)"
h5s->set_read_p(true);
set_vec(element,h5s);
delete h5s;
} // end "for (int element=0"
if(true == has_values) {
if(-1 == mspace)
throw InternalErr(__FILE__, __LINE__, "memory type and memory space for this compound datatype should be valid.");
if(H5Dvlen_reclaim(memtype,mspace,H5P_DEFAULT,(void*)&values[0])<0)
throw InternalErr(__FILE__, __LINE__, "Unable to reclaim the compound datatype array.");
H5Sclose(mspace);
}
H5Tclose(dtypeid);
H5Tclose(memtype);
}
catch(...) {
if(memb_id != -1)
H5Tclose(memb_id);
if(memb_name != NULL)
H5free_memory(memb_name);
if(h5s != NULL)
delete h5s;
if(true == has_values) {
if(H5Dvlen_reclaim(memtype,mspace,H5P_DEFAULT,(void*)(&values[0]))<0) {
H5Tclose(memtype);
H5Sclose(mspace);
}
H5Sclose(mspace);
}
H5Tclose(memtype);
H5Tclose(dtypeid);
throw;
}
set_read_p(true);
return false;
}
// Haven't checked the codes and comments
// Haven't added the close handles routines for error handlings yet. KY 2011-11-18
bool HDF5Array::m_array_of_reference(hid_t dset_id,hid_t dtype_id)
{
#if (H5_VERS_MAJOR == 1 && (H5_VERS_MINOR == 10 || H5_VERS_MINOR == 8 || H5_VERS_MINOR == 6))
hid_t d_dset_id = dset_id;
hdset_reg_ref_t *rbuf = NULL;
try {
vector<int> offset(d_num_dim);
vector<int> count(d_num_dim);
vector<int> step(d_num_dim);
int nelms = format_constraint(&offset[0], &step[0], &count[0]); // Throws Error.
vector<string> v_str(nelms);
BESDEBUG("h5", "=read() URL type is detected. "
<< "nelms=" << nelms << " full_size=" << d_num_elm << endl);
// Handle regional reference.
if (H5Tequal(dtype_id, H5T_STD_REF_DSETREG) < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Tequal() failed");
}
if (H5Tequal(dtype_id, H5T_STD_REF_DSETREG) > 0) {
BESDEBUG("h5", "=read() Got regional reference. " << endl);
// Vector doesn't work for this case. somehow it doesn't support the type.
rbuf = new hdset_reg_ref_t[d_num_elm];
if(rbuf == NULL){
throw InternalErr(__FILE__, __LINE__, "new() failed.");
}
if (H5Dread(d_dset_id, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, &rbuf[0]) < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Dread() failed.");
}
for (int i = 0; i < nelms; i++) {
// Let's assume that URL array is always 1 dimension.
BESDEBUG("h5", "=read() rbuf[" << i << "]" <<
rbuf[offset[0] + i * step[0]] << endl);
if (rbuf[offset[0] + i * step[0]][0] != '\0') {
char r_name[DODS_NAMELEN];
hid_t did_r = H5RDEREFERENCE(d_dset_id, H5R_DATASET_REGION, rbuf[offset[0] + i * step[0]]);
if (did_r < 0) {
throw InternalErr(__FILE__, __LINE__, "H5RDEREFERENCE() failed.");
}
if (H5Iget_name(did_r, (char *) r_name, DODS_NAMELEN) < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Iget_name() failed.");
}
BESDEBUG("h5", "=read() dereferenced name is " << r_name
<< endl);
string varname(r_name);
hid_t space_id = H5Rget_region(did_r, H5R_DATASET_REGION, rbuf[offset[0] + i * step[0]]);
if (space_id < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Rget_region() failed.");
}
int ndim = H5Sget_simple_extent_ndims(space_id);
if (ndim < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Sget_simple_extent_ndims() failed.");
}
BESDEBUG("h5", "=read() dim is " << ndim << endl);
string expression;
switch (H5Sget_select_type(space_id)) {
case H5S_SEL_NONE:
BESDEBUG("h5", "=read() None selected." << endl);
break;
case H5S_SEL_POINTS: {
BESDEBUG("h5", "=read() Points selected." << endl);
hssize_t npoints = H5Sget_select_npoints(space_id);
if (npoints < 0) {
throw InternalErr(__FILE__, __LINE__,
"Cannot determine number of elements in the dataspace selection");
}
BESDEBUG("h5", "=read() npoints are " << npoints
<< endl);
vector<hsize_t> buf(npoints * ndim);
if (H5Sget_select_elem_pointlist(space_id, 0, npoints, &buf[0]) < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Sget_select_elem_pointlist() failed.");
}
#if 0
for (int j = 0; j < npoints * ndim; j++) {
"h5", "=read() npoints buf[0] =" << buf[j] <<endl;
}
#endif
for (int j = 0; j < (int) npoints; j++) {
// Name of the dataset.
expression.append(varname);
for (int k = 0; k < ndim; k++) {
ostringstream oss;
oss << "[" << (int) buf[j * ndim + k] << "]";
expression.append(oss.str());
}
if (j != (int) (npoints - 1)) {
expression.append(",");
}
}
v_str[i].append(expression);
break;
}
case H5S_SEL_HYPERSLABS: {
vector<hsize_t> start(ndim);
vector<hsize_t> end(ndim);
vector<hsize_t>stride(ndim);
vector<hsize_t>count(ndim);
vector<hsize_t>block(ndim);
BESDEBUG("h5", "=read() Slabs selected." << endl);
BESDEBUG("h5", "=read() nblock is " <<
H5Sget_select_hyper_nblocks(space_id) << endl);
#if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8)
if (H5Sget_select_bounds(space_id, &start[0], &end[0]) < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Sget_select_bounds() failed.");
}
#else
if (H5Sget_regular_hyperslab(space_id, &start[0], &stride[0], &count[0], &block[0]) < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Sget_regular_hyperslab() failed.");
}
#endif
for (int j = 0; j < ndim; j++) {
ostringstream oss;
BESDEBUG("h5", "start " << start[j]
<< "stride "<<stride[j]
<< "count "<< count[j]
<< "block "<< block[j]
<<endl);
// Map from HDF5's start,stride,count,block to DAP's start,stride,end.
end[j] = start[j] + stride[j]*(count[j]-1)+(block[j]-1);
BESDEBUG("h5", "=read() start is " << start[j]
<< "=read() end is " << end[j] << endl);
oss << "[" << start[j] << ":" << stride[j] << ":" << end[j] << "]";
expression.append(oss.str());
BESDEBUG("h5", "=read() expression is "
<< expression << endl)
;
}
v_str[i] = varname;
if (!expression.empty()) {
v_str[i].append(expression);
}
// Constraint expression. [start:1:end]
break;
}
case H5S_SEL_ALL:
BESDEBUG("h5", "=read() All selected." << endl);
break;
default:
BESDEBUG("h5", "Unknown space type." << endl);
break;
}
}
else {
v_str[i] = "";
}
}
delete[] rbuf;
}
// Handle object reference.
if (H5Tequal(dtype_id, H5T_STD_REF_OBJ) < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Tequal() failed.");
}
if (H5Tequal(dtype_id, H5T_STD_REF_OBJ) > 0) {
BESDEBUG("h5", "=read() Got object reference. " << endl);
vector<hobj_ref_t> orbuf;
orbuf.resize(d_num_elm);
if (H5Dread(d_dset_id, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &orbuf[0]) < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Dread failed()");
}
for (int i = 0; i < nelms; i++) {
// Let's assume that URL array is always 1 dimension.
hid_t did_r = H5RDEREFERENCE(d_dset_id, H5R_OBJECT, &orbuf[offset[0] + i * step[0]]);
if (did_r < 0) {
throw InternalErr(__FILE__, __LINE__, "H5RDEREFERENCE() failed.");
}
char r_name[DODS_NAMELEN];
if (H5Iget_name(did_r, (char *) r_name, DODS_NAMELEN) < 0) {
throw InternalErr(__FILE__, __LINE__, "H5Iget_name() failed.");
}
// Shorten the dataset name
string varname(r_name);
BESDEBUG("h5", "=read() dereferenced name is " << r_name <<endl);
v_str[i] = varname;
}
}
set_value(&v_str[0], nelms);
return false;
}
catch (...) {
if(rbuf!= NULL)
delete[] rbuf;
throw;
}
#else
return m_array_of_reference_new_h5_apis(dset_id,dtype_id);
#endif
}
bool HDF5Array::m_array_of_reference_new_h5_apis(hid_t dset_id,hid_t dtype_id) {
#if (H5_VERS_MAJOR == 1 && (H5_VERS_MINOR == 10 || H5_VERS_MINOR == 8 || H5_VERS_MINOR == 6))
throw InternalErr(__FILE__, __LINE__,
"The HDF5 handler compiled with earlier version (<=110)of the HDF5 library should not call method that uses new reference APIs");
return false;
#else
H5R_ref_t *rbuf = NULL;
hid_t mem_space_id;
hid_t file_space_id;
try {
// First we need to read the reference data from DAP's hyperslab selection.
vector<int> offset(d_num_dim);
vector<int> count(d_num_dim);
vector<int> step(d_num_dim);
vector<hsize_t> hoffset(d_num_dim);
vector<hsize_t>hcount(d_num_dim);
vector<hsize_t>hstep(d_num_dim);
int nelms = format_constraint(&offset[0], &step[0], &count[0]);
for (int i = 0; i <d_num_dim; i++) {
hoffset[i] = (hsize_t) offset[i];
hcount[i] = (hsize_t) count[i];
hstep[i] = (hsize_t) step[i];
}
BESDEBUG("h5", "=read() URL type is detected. "
<< "nelms=" << nelms << endl);
rbuf = new H5R_ref_t[nelms];
file_space_id = H5Dget_space(dset_id);
if(file_space_id < 0)
throw InternalErr(__FILE__, __LINE__, "Fail to obtain reference dataset file space.");
if (H5Sselect_hyperslab(file_space_id, H5S_SELECT_SET,
&hoffset[0], &hstep[0],
&hcount[0], NULL) < 0)
throw InternalErr (__FILE__, __LINE__, "Fail to select the hyperslab for reference dataset.");
mem_space_id = H5Screate_simple(d_num_dim,&hcount[0],NULL);
if(mem_space_id < 0)
throw InternalErr(__FILE__, __LINE__, "Fail to obtain reference dataset memory space.");
if(H5Dread(dset_id,H5T_STD_REF,mem_space_id,file_space_id,H5P_DEFAULT,&rbuf[0])<0)
throw InternalErr(__FILE__, __LINE__, "Fail to read hyperslab reference dataset.");
H5Sclose(mem_space_id);
H5Sclose(file_space_id);
// Now we need to retrieve the reference info. fot the nelms elements.
vector<string> v_str;
H5R_type_t ref_type = H5Rget_type((const H5R_ref_t *)&rbuf[0]);
// The referenced objects can only be either objects or dataset regions.
if(ref_type != H5R_OBJECT2 && ref_type !=H5R_DATASET_REGION2)
throw InternalErr(__FILE__, __LINE__, "Unsupported reference: neither object nor region references");
for (int i = 0; i < nelms; i++) {
hid_t obj_id = H5Ropen_object((H5R_ref_t *)&rbuf[i], H5P_DEFAULT, H5P_DEFAULT);
if(obj_id < 0)
throw InternalErr(__FILE__, __LINE__, "Cannot open the object the reference points to");
vector<char> objname;
ssize_t objnamelen = -1;
if ((objnamelen= H5Iget_name(obj_id,NULL,0))<=0) {
H5Oclose(obj_id);
throw InternalErr(__FILE__, __LINE__, "Cannot obtain the name length of the object the reference points to");
}
objname.resize(objnamelen+1);
if ((objnamelen= H5Iget_name(obj_id,&objname[0],objnamelen+1))<=0) {
H5Oclose(obj_id);
throw InternalErr(__FILE__, __LINE__, "Cannot obtain the name length of the object the reference points to");
}
string objname_str = string(objname.begin(),objname.end());
string trim_objname = objname_str.substr(0,objnamelen);
// For object references, we just need to save the object full path.
if(ref_type == H5R_OBJECT2)
v_str.push_back(trim_objname);
else {// Must be region reference.
H5O_type_t obj_type;
if(H5Rget_obj_type3((H5R_ref_t *)&rbuf[i], H5P_DEFAULT, &obj_type) < 0){
H5Oclose(obj_id);
throw InternalErr(__FILE__, __LINE__, "H5Rget_obj_type3() failed.");
}
if(obj_type != H5O_TYPE_DATASET) {
H5Oclose(obj_id);
throw InternalErr(__FILE__, __LINE__, "Region reference must point to a dataset.");
}
hid_t region_space_id = H5Ropen_region(&rbuf[i],H5P_DEFAULT,H5P_DEFAULT);
if (region_space_id < 0) {
H5Oclose(obj_id);
throw InternalErr(__FILE__, __LINE__, "Cannot obtain the space ID the reference points to");
}
int ndim = H5Sget_simple_extent_ndims(region_space_id);
if (ndim < 0) {
H5Sclose(region_space_id);
H5Oclose(obj_id);
throw InternalErr(__FILE__, __LINE__, "H5Sget_simple_extent_ndims() failed.");
}
string expression;
switch (H5Sget_select_type(region_space_id)) {
case H5S_SEL_NONE:
BESDEBUG("h5", "=read() None selected." << endl);
break;
case H5S_SEL_POINTS: {
BESDEBUG("h5", "=read() Points selected." << endl);
hssize_t npoints = H5Sget_select_npoints(region_space_id);
if (npoints < 0) {
H5Sclose(region_space_id);
H5Oclose(obj_id);
throw InternalErr(__FILE__, __LINE__,
"Cannot determine number of elements in the dataspace selection");
}
BESDEBUG("h5", "=read() npoints are " << npoints
<< endl);
vector<hsize_t> buf(npoints * ndim);
if (H5Sget_select_elem_pointlist(region_space_id, 0, npoints, &buf[0]) < 0) {
H5Sclose(region_space_id);
H5Oclose(obj_id);
throw InternalErr(__FILE__, __LINE__, "H5Sget_select_elem_pointlist() failed.");
}
#if 0
for (int j = 0; j < npoints * ndim; j++) {
"h5", "=read() npoints buf[0] =" << buf[j] <<endl;
}
#endif
for (int j = 0; j < (int) npoints; j++) {
// Name of the dataset.
expression.append(trim_objname);
for (int k = 0; k < ndim; k++) {
ostringstream oss;
oss << "[" << (int) buf[j * ndim + k] << "]";
expression.append(oss.str());
}
if (j != (int) (npoints - 1)) {
expression.append(",");
}
}
v_str.push_back(expression);
break;
}
case H5S_SEL_HYPERSLABS: {
vector<hsize_t> start(ndim);
vector<hsize_t> end(ndim);
vector<hsize_t>stride(ndim);
vector<hsize_t>count(ndim);
vector<hsize_t>block(ndim);
BESDEBUG("h5", "=read() Slabs selected." << endl);
BESDEBUG("h5", "=read() nblock is " <<
H5Sget_select_hyper_nblocks(region_space_id) << endl);
if (H5Sget_regular_hyperslab(region_space_id, &start[0], &stride[0], &count[0], &block[0]) < 0) {
H5Sclose(region_space_id);
H5Oclose(obj_id);
throw InternalErr(__FILE__, __LINE__, "H5Sget_regular_hyperslab() failed.");
}
expression.append(trim_objname);
for (int j = 0; j < ndim; j++) {
ostringstream oss;
BESDEBUG("h5", "start " << start[j]
<< "stride "<<stride[j]
<< "count "<< count[j]
<< "block "<< block[j]
<<endl);
// Map from HDF5's start,stride,count,block to DAP's start,stride,end.
end[j] = start[j] + stride[j]*(count[j]-1)+(block[j]-1);
BESDEBUG("h5", "=read() start is " << start[j]
<< "=read() end is " << end[j] << endl);
oss << "[" << start[j] << ":" << stride[j] << ":" << end[j] << "]";
expression.append(oss.str());
BESDEBUG("h5", "=read() expression is "
<< expression << endl)
;
}
v_str.push_back(expression);
// Constraint expression. [start:stride:end]
break;
}
case H5S_SEL_ALL:
BESDEBUG("h5", "=read() All selected." << endl);
break;
default: