forked from OPENDAP/hdf4_handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHDFSP.cc
More file actions
6459 lines (5300 loc) · 238 KB
/
HDFSP.cc
File metadata and controls
6459 lines (5300 loc) · 238 KB
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 the hdf4 data handler for the OPeNDAP data server.
/// HDFSP.h and HDFSP.cc include the core part of retrieving HDF-SP Grid and Swath
/// metadata info and translate them into DAP DDS and DAS.
///
/// It currently provides the CF-compliant support for the following NASA HDF4 products.
/// Other HDF4 products can still be mapped to DAP but they are not CF-compliant.
/// TRMM version 6 Level2 1B21,2A12,2B31,2A25
/// TRMM version 6 Level3 3B42,3B43,3A46,CSH
/// CERES CER_AVG_Aqua-FM3-MODIS,CER_AVG_Terra-FM1-MODIS
/// CERES CER_ES4_Aqua-FM3_Edition1-CV
/// CERES CER_ISCCP-D2like-Day_Aqua-FM3-MODIS
/// CERES CER_ISCCP-D2like-GEO_
/// CERES CER_SRBAVG3_Aqua-
/// CERES CER_SYN_Aqua-
/// CERES CER_ZAVG_
/// OBPG SeaWIFS,OCTS,CZCS,MODISA,MODIST Level2
/// OBPG SeaWIFS,OCTS,CZCS,MODISA,MODIST Level3 m
/// KY 2010-8-12
///
///
/// @author Kent Yang <myang6@hdfgroup.org>
///
/// Copyright (C) 2010-2012 The HDF Group
///
/// All rights reserved.
/////////////////////////////////////////////////////////////////////////////
#include <sstream>
#include <algorithm>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include<libgen.h>
#include "HDFCFUtil.h"
#include "HDFSP.h"
#include "dodsutil.h"
#include "HDF4RequestHandler.h"
const char *_BACK_SLASH= "/";
using namespace HDFSP;
using namespace std;
#define ERR_LOC1(x) #x
#define ERR_LOC2(x) ERR_LOC1(x)
#define ERR_LOC __FILE__ " : " ERR_LOC2(__LINE__)
// Convenient function to handle exceptions
template < typename T, typename U, typename V, typename W, typename X > static void
_throw5 (const char *fname, int line, int numarg,
const T & a1, const U & a2, const V & a3, const W & a4, const X & a5)
{
std::ostringstream ss;
ss << fname << ":" << line << ":";
for (int i = 0; i < numarg; ++i) {
ss << " ";
switch (i) {
case 0:
ss << a1;
break;
case 1:
ss << a2;
break;
case 2:
ss << a3;
break;
case 3:
ss << a4;
break;
case 4:
ss << a5;
break;
default:
ss <<" Argument number is beyond 5";
}
}
throw Exception (ss.str ());
}
/// The followings are convenient functions to throw exceptions with different
// number of arguments.
/// We assume that the maximum number of arguments is 5.
#define throw1(a1) _throw5(__FILE__, __LINE__, 1, a1, 0, 0, 0, 0)
#define throw2(a1, a2) _throw5(__FILE__, __LINE__, 2, a1, a2, 0, 0, 0)
#define throw3(a1, a2, a3) _throw5(__FILE__, __LINE__, 3, a1, a2, a3, 0, 0)
#define throw4(a1, a2, a3, a4) _throw5(__FILE__, __LINE__, 4, a1, a2, a3, a4, 0)
#define throw5(a1, a2, a3, a4, a5) _throw5(__FILE__, __LINE__, 5, a1, a2, a3, a4, a5)
#define assert_throw0(e) do { if (!(e)) throw1("assertion failure"); } while (false)
#define assert_range_throw0(e, ge, l) assert_throw0((ge) <= (e) && (e) < (l))
// Convenient function to release resources.
struct delete_elem
{
template < typename T > void operator () (T * ptr)
{
delete ptr;
}
};
// Class File destructor
File::~File ()
{
// Release SD resources
if (this->sdfd != -1) {
if (sd != NULL)
delete sd;
// No need to close SD interface since for performance reasons
// it is handled(opened/closed) at the top level(HDF4RequestHandler.cc)
// KY 2014-02-18
//SDend (this->sdfd);
}
// Close V interface IDs and release vdata resources
if (this->fileid != -1) {
for (vector < VDATA * >::const_iterator i = this->vds.begin ();
i != this->vds.end (); ++i) {
delete *i;
}
for (vector < AttrContainer * >::const_iterator i = this->vg_attrs.begin ();
i != this->vg_attrs.end (); ++i) {
delete *i;
}
Vend (this->fileid);
// No need to close H interface since for performance reasons
// it is handled(opened/closed) at the top level(HDF4RequestHandler.cc)
//Hclose (this->fileid);
}
}
// Destructor to release vdata resources
VDATA::~VDATA ()
{
// Release vdata field pointers
std::for_each (this->vdfields.begin (), this->vdfields.end (),
delete_elem ());
// Release vdata attributes
std::for_each (this->attrs.begin (), this->attrs.end (), delete_elem ());
}
// Destructor to release SD resources
SD::~SD ()
{
// Release vdata attributes
std::for_each (this->attrs.begin (), this->attrs.end (), delete_elem ());
// Release SD field pointers
std::for_each (this->sdfields.begin (), this->sdfields.end (),
delete_elem ());
}
// Destructor to release SD field resources
SDField::~SDField ()
{
// Release dimension resources
std::for_each (this->dims.begin (), this->dims.end (), delete_elem ());
// Release corrected dimension resources
std::for_each (this->correcteddims.begin (), this->correcteddims.end (),
delete_elem ());
// Release attribute container dims_info resources(Only apply for the OTHERHDF case)
std::for_each (this->dims_info.begin (), this->dims_info.end (), delete_elem ());
}
// Vdata field constructors, nothing needs to do here. We don't provide vdata dimensions.
// Only when mapping to DDS (at hdfdesc.cc,search VDFDim0), we add the dimension info. to DDS. The addition
// may not be in a good place, however, the good part is that we don't need to allocate dimension resources
// for vdata.
VDField::~VDField ()
{
}
// We only need to release attributes since that's shared for both Vdata fields and SDS fields.
Field::~Field ()
{
std::for_each (this->attrs.begin (), this->attrs.end (), delete_elem ());
}
// Release attribute container resources. This should only apply to the OTHERHDF case.
AttrContainer::~AttrContainer()
{
std::for_each (this->attrs.begin (), this->attrs.end (), delete_elem ());
}
// Retrieve all the information from an HDF file; this is the same approach
// as the way to handle HDF-EOS2 files.
File *
File::Read (const char *path, int32 mysdid, int32 myfileid)
throw (Exception)
{
// Allocate a new file object.
File *file = new File (path);
#if 0
int32 mysdid = -1;
// Obtain the SD ID.
if ((mysdid =
SDstart (const_cast < char *>(file->path.c_str ()),
DFACC_READ)) == -1) {
delete file;
throw2 ("SDstart", path);
}
#endif
// Old comments just for reminders(KY 2014-02-18)
// A strange compiling bug was found if we don't pass the file id to this fuction.
// It will always give 0 number as the ID and the HDF4 library doesn't complain!!
// Will try dumplicating the problem and submit a bug report. KY 2010-7-14
file->sdfd = mysdid;
file->fileid = myfileid;
if(myfileid != -1) {
// Start V interface
int32 status = Vstart (file->fileid);
if (status == FAIL) {
delete file;
throw2 ("Cannot start vdata/vgroup interface", path);
}
}
try {
// Read SDS info.
file->sd = SD::Read (file->sdfd, file->fileid);
// Handle lone vdatas, non-lone vdatas will be handled in Prepare().
// Read lone vdata.
if(myfileid != -1)
file->ReadLoneVdatas(file);
}
catch(...) {
delete file;
throw;
}
return file;
}
// Retrieve all the information from the additional SDS objects of an HDF file; this is the same approach
// as the way to handle other HDF4 files.
File *
File::Read_Hybrid (const char *path, int32 mysdid, int32 myfileid)
throw (Exception)
{
// New File
File *file = new File (path);
if(file == NULL)
throw1("Memory allocation for file class failed. ");
//cerr<<"File is opened for HDF4 "<<endl;
#if 0
// Obtain SD interface
int32 mysdid = -1;
if ((mysdid =
SDstart (const_cast < char *>(file->path.c_str ()),
DFACC_READ)) == -1) {
delete file;
throw2 ("SDstart", path);
}
#endif
// Old comments just for reminders. The HDF4 issue may still exist. KY 2014-02-18
// A strange compiling bug was found if we don't pass the file id to this fuction.
// It will always give 0 number as the ID and the HDF4 library doesn't complain!!
// Will try dumplicating the problem and submit a bug report. KY 2010-7-14
file->sdfd = mysdid;
file->fileid = myfileid;
// Start V interface
int status = Vstart (file->fileid);
if (status == FAIL) {
delete file;
throw2 ("Cannot start vdata/vgroup interface", path);
}
//if(file != NULL) {// Coverity doesn't recongize the throw macro, see if this makes it happy.
try {
// Retrieve extra SDS info.
file->sd = SD::Read_Hybrid(file->sdfd, file->fileid);
// Retrieve lone vdata info.(HDF-EOS2 doesn't support any lone vdata)
file->ReadLoneVdatas(file);
// Retrieve extra non-lone vdata in the hybrid HDF-EOS2 file
file->ReadHybridNonLoneVdatas(file);
}
catch(...) {
delete file;
throw;
}
//}
return file;
}
// Retrieve lone vdata info.
void
File::ReadLoneVdatas(File *file) throw(Exception) {
int status = -1;
// No need to start V interface again
#if 0
// Start V interface
int status = Vstart (file->fileid);
if (status == FAIL)
throw2 ("Cannot start vdata/vgroup interface", path);
#endif
// Obtain number of lone vdata.
int num_lone_vdata = VSlone (file->fileid, NULL, 0);
if (num_lone_vdata == FAIL)
throw2 ("Fail to obtain lone vdata number", path);
// Currently the vdata name buffer has to be static allocated according to HDF4 reference manual. KY 2010-7-14
// Now HDF4 provides a dynamic way to allocate the length of vdata_class, should update to use that in the future.
// Documented in a jira ticket HFRHANDLER-168.
// KY 2013-07-11
char vdata_class[VSNAMELENMAX];
char vdata_name[VSNAMELENMAX];
if (num_lone_vdata > 0) {
vector<int32>ref_array;
ref_array.resize(num_lone_vdata);
if (VSlone (file->fileid, &ref_array[0], num_lone_vdata) == FAIL) {
throw2 ("cannot obtain lone vdata reference arrays", path);
}
for (int i = 0; i < num_lone_vdata; i++) {
int32 vdata_id = -1;
vdata_id = VSattach (file->fileid, ref_array[i], "r");
if (vdata_id == FAIL) {
throw2 ("Fail to attach Vdata", path);
}
status = VSgetclass (vdata_id, vdata_class);
if (status == FAIL) {
VSdetach (vdata_id);
throw2 ("Fail to obtain Vdata class", path);
}
if (VSgetname (vdata_id, vdata_name) == FAIL) {
VSdetach (vdata_id);
throw3 ("Fail to obtain Vdata name", path, vdata_name);
}
// Ignore any vdata that is either an HDF4 attribute or is used
// to store internal data structures.
if (VSisattr (vdata_id) == TRUE
|| !strncmp (vdata_class, _HDF_CHK_TBL_CLASS,
strlen (_HDF_CHK_TBL_CLASS))
|| !strncmp (vdata_class, _HDF_SDSVAR, strlen (_HDF_SDSVAR))
|| !strncmp (vdata_class, _HDF_CRDVAR, strlen (_HDF_CRDVAR))
|| !strncmp (vdata_class, DIM_VALS, strlen (DIM_VALS))
|| !strncmp (vdata_class, DIM_VALS01, strlen (DIM_VALS01))
|| !strncmp (vdata_class, RIGATTRCLASS, strlen (RIGATTRCLASS))
|| !strncmp (vdata_name, RIGATTRNAME, strlen (RIGATTRNAME))) {
status = VSdetach (vdata_id);
if (status == FAIL) {
throw3 ("VSdetach failed ", "Vdata name ", vdata_name);
}
}
else {
VDATA*vdataobj = NULL;
try {
// Read vdata information
vdataobj = VDATA::Read (vdata_id, ref_array[i]);
}
catch (...) {
VSdetach(vdata_id);
throw;
}
// We want to map fields of vdata with more than 10 records to DAP variables
// and we need to add the path and vdata name to the new vdata field name
if (!vdataobj->getTreatAsAttrFlag ()) {
for (std::vector < VDField * >::const_iterator it_vdf =
vdataobj->getFields ().begin ();
it_vdf != vdataobj->getFields ().end (); it_vdf++) {
// vdata name conventions.
// "vdata"+vdata_newname+"_vdf_"+(*it_vdf)->newname
(*it_vdf)->newname =
"vdata_" + vdataobj->newname + "_vdf_" +
(*it_vdf)->name;
//Make sure the name is following CF, KY 2012-6-26
(*it_vdf)->newname = HDFCFUtil::get_CF_string((*it_vdf)->newname);
}
}
// Save this vdata info. in the file instance.
file->vds.push_back (vdataobj);
// THe following code should be replaced by using the VDField member functions in the future
// The code has largely overlapped with VDField member functions, but not for this release.
// KY 2010-8-11
// To know if the data product is CERES, we need to check Vdata CERE_metadata(CERE_META_NAME).
// One field name LOCALGRANULEID(CERE_META_FIELD_NAME) includes the product name.
// We want to assign the filetype of this CERES file based on the LOCALGRANULEID.
// Please note that CERES products we support to follow CF are pure HDF4 files.
// For hybrid HDF-EOS2 files, this if loop is simply skipped.
// When the vdata name indicates this is a CERES product, we need to do the following:
if (false == strncmp
(vdata_name, CERE_META_NAME, strlen (CERE_META_NAME))) {
char *fieldname = NULL;
// Obtain number of vdata fields
int num_field = VFnfields (vdata_id);
if (num_field == FAIL) {
VSdetach (vdata_id);
throw3 ("number of fields at Vdata ", vdata_name," is -1");
}
// Search through the number of vdata fields
for (int j = 0; j < num_field; j++) {
fieldname = VFfieldname (vdata_id, j);
if (fieldname == NULL) {
VSdetach (vdata_id);
throw5 ("vdata ", vdata_name, " field index ", j,
" field name is NULL.");
}
// If the field name matches CERES's specific field name"LOCALGRANULEID"
else if (!strcmp (fieldname, CERE_META_FIELD_NAME)) {
int32 fieldsize = -1;
int32 nelms = -1;
// Obtain field size
fieldsize = VFfieldesize (vdata_id, j);
if (fieldsize == FAIL) {
VSdetach (vdata_id);
throw5 ("vdata ", vdata_name, " field ",fieldname, " size is wrong.");
}
// Obtain number of elements
nelms = VSelts (vdata_id);
if (nelms == FAIL) {
VSdetach (vdata_id);
throw5 ("vdata ", vdata_name,
" number of field record ", nelms," is wrong.");
}
string err_msg;
bool data_buf_err = false;
bool VS_fun_err = false;
// Allocate data buf
char *databuf = (char *) malloc (fieldsize * nelms);
if (databuf == NULL) {
err_msg = string(ERR_LOC) + "No enough memory to allocate buffer.";
data_buf_err = true;
goto cleanFail;
}
// Initialize the seeking process
if (VSseek (vdata_id, 0) == FAIL) {
err_msg = string(ERR_LOC) + "VSseek failed";
VS_fun_err = true;
goto cleanFail;
}
// The field to seek is CERE_META_FIELD_NAME
if (VSsetfields (vdata_id, CERE_META_FIELD_NAME) == FAIL) {
err_msg = "VSsetfields failed";
VS_fun_err = true;
goto cleanFail;
}
// Read this vdata field value
if (VSread(vdata_id, (uint8 *) databuf, 1,FULL_INTERLACE)
== FAIL) {
err_msg = "VSread failed";
VS_fun_err = true;
goto cleanFail;
}
// Assign the corresponding special product indicator we supported for CF
if (!strncmp(databuf, CER_AVG_NAME,strlen (CER_AVG_NAME)))
file->sptype = CER_AVG;
else if (!strncmp
(databuf, CER_ES4_NAME,strlen(CER_ES4_NAME)))
file->sptype = CER_ES4;
else if (!strncmp
(databuf, CER_CDAY_NAME,strlen (CER_CDAY_NAME)))
file->sptype = CER_CDAY;
else if (!strncmp
(databuf, CER_CGEO_NAME,strlen (CER_CGEO_NAME)))
file->sptype = CER_CGEO;
else if (!strncmp
(databuf, CER_SRB_NAME,strlen (CER_SRB_NAME)))
file->sptype = CER_SRB;
else if (!strncmp
(databuf, CER_SYN_NAME,strlen (CER_SYN_NAME)))
file->sptype = CER_SYN;
else if (!strncmp
(databuf, CER_ZAVG_NAME,
strlen (CER_ZAVG_NAME)))
file->sptype = CER_ZAVG;
cleanFail:
if(data_buf_err == true || VS_fun_err == true) {
VSdetach(vdata_id);
if(data_buf_err == true)
throw1(err_msg);
else {
free(databuf);
throw5("vdata ",vdata_name,"field ",
CERE_META_FIELD_NAME,err_msg);
}
}
else
free(databuf);
}
}
}
VSdetach (vdata_id);
}
}
}
}
// Handle non-attribute non-lone vdata for Hybrid HDF-EOS2 files.
void
File::ReadHybridNonLoneVdatas(File *file) throw(Exception) {
int32 status = -1;
int32 file_id = -1;
int32 vgroup_id = -1;
int32 vdata_id = -1;
//int32 vgroup_ref = -1;
//int32 obj_index = -1;
//int32 num_of_vg_objs = -1;
int32 obj_tag = -1;
int32 obj_ref = -1;
int32 lone_vg_number = 0;
int32 num_of_lones = -1;
int32 num_gobjects = 0;
// This can be updated in the future with new HDF4 APIs that can provide the actual length of an object name.
// Documented in a jira ticket HFRHANDLER-168.
// KY 2013-07-11
char vdata_name[VSNAMELENMAX];
char vdata_class[VSNAMELENMAX];
char vgroup_name[VGNAMELENMAX*4];
char vgroup_class[VGNAMELENMAX*4];
// Full path of this vgroup
char *full_path = NULL;
// Copy of a full path of this vgroup
char *cfull_path = NULL;
// Obtain H interface ID
file_id = file->fileid;
// No need to start V interface again.
#if 0
// Start V interface
status = Vstart (file_id);
if (status == FAIL)
throw2 ("Cannot start vdata/vgroup interface", path);
#endif
// No NASA HDF4 files have the vgroup that forms a ring; so ignore this case.
// First, call Vlone with num_of_lones set to 0 to get the number of
// lone vgroups in the file, but not to get their reference numbers.
num_of_lones = Vlone (file_id, NULL, 0);
if (num_of_lones == FAIL)
throw3 ("Fail to obtain lone vgroup number", "file id is", file_id);
// if there are any lone vgroups,
if (num_of_lones > 0) {
// Use the num_of_lones returned to allocate sufficient space for the
// buffer ref_array to hold the reference numbers of all lone vgroups,
// Use vectors to avoid the clean-up of the memory
vector<int32>ref_array;
ref_array.resize(num_of_lones);
// Call Vlone again to retrieve the reference numbers into
// the buffer ref_array.
num_of_lones = Vlone (file_id, &ref_array[0], num_of_lones);
if (num_of_lones == FAIL) {
throw3 ("Cannot obtain lone vgroup reference arrays ",
"file id is ", file_id);
}
// Loop the lone vgroups.
for (lone_vg_number = 0; lone_vg_number < num_of_lones;
lone_vg_number++) {
// Attach to the current vgroup
vgroup_id = Vattach (file_id, ref_array[lone_vg_number], "r");
if (vgroup_id == FAIL) {
throw3 ("Vattach failed ", "Reference number is ",
ref_array[lone_vg_number]);
}
// Obtain the vgroup name.
status = Vgetname (vgroup_id, vgroup_name);
if (status == FAIL) {
Vdetach (vgroup_id);
throw3 ("Vgetname failed ", "vgroup_id is ", vgroup_id);
}
// Obtain the vgroup_class name.
status = Vgetclass (vgroup_id, vgroup_class);
if (status == FAIL) {
Vdetach (vgroup_id);
throw3 ("Vgetclass failed ", "vgroup_name is ", vgroup_name);
}
//Ignore internal HDF groups
if (strcmp (vgroup_class, _HDF_ATTRIBUTE) == 0
|| strcmp (vgroup_class, _HDF_VARIABLE) == 0
|| strcmp (vgroup_class, _HDF_DIMENSION) == 0
|| strcmp (vgroup_class, _HDF_UDIMENSION) == 0
|| strcmp (vgroup_class, _HDF_CDF) == 0
|| strcmp (vgroup_class, GR_NAME) == 0
|| strcmp (vgroup_class, RI_NAME) == 0) {
Vdetach(vgroup_id);
continue;
}
// Obtain number of objects under this vgroup
num_gobjects = Vntagrefs (vgroup_id);
if (num_gobjects < 0) {
Vdetach (vgroup_id);
throw3 ("Vntagrefs failed ", "vgroup_name is ", vgroup_name);
}
// STOP: error handling to avoid the false alarm from coverity scan or sonar cloud
string err_msg;
bool VS_or_mem_err = false;
// Allocate enough buffer for the full path
// MAX_FULL_PATH_LEN(1024) is long enough
// to cover any HDF4 object path for all NASA HDF4 products.
// We replace strcpy and strcat with strncpy and strncat as suggested. KY 2013-08-29
full_path = (char *) malloc (MAX_FULL_PATH_LEN);
if (full_path == NULL) {
err_msg = "No enough memory to allocate the buffer for full_path.";
VS_or_mem_err = true;
goto cleanFail;
//Vdetach (vgroup_id);
//throw;
//throw1 ("No enough memory to allocate the buffer.");
}
else
memset(full_path,'\0',MAX_FULL_PATH_LEN);
// Obtain the full path of this vgroup
strncpy (full_path,_BACK_SLASH,strlen(_BACK_SLASH));
strncat(full_path,vgroup_name,strlen(vgroup_name));
strncat(full_path,_BACK_SLASH,strlen(_BACK_SLASH));
// Make a copy the current vgroup full path since full path may be passed to a recursive routine
cfull_path = (char *) malloc (MAX_FULL_PATH_LEN);
if (cfull_path == NULL) {
//Vdetach (vgroup_id);
//free (full_path);
err_msg = "No enough memory to allocate the buffer for cfull_path.";
VS_or_mem_err = true;
goto cleanFail;
//throw;
//throw1 ("No enough memory to allocate the buffer.");
}
else
memset(cfull_path,'\0',MAX_FULL_PATH_LEN);
strncpy(cfull_path,full_path,strlen(full_path));
// Loop all vgroup objects
for (int i = 0; i < num_gobjects; i++) {
// Obtain the object tag/ref pair of an object
if (Vgettagref (vgroup_id, i, &obj_tag, &obj_ref) == FAIL) {
err_msg = "Vgettagref failed";
VS_or_mem_err = true;
goto cleanFail;
//Vdetach (vgroup_id);
//free (full_path);
//free (cfull_path);
//throw5 ("Vgettagref failed ", "vgroup_name is ",
// vgroup_name, " reference number is ", obj_ref);
}
// If the object is a vgroup,always pass the original full path to its decendant vgroup
// The reason to use a copy is because the full_path will be changed when it goes down to its descendant.
if (Visvg (vgroup_id, obj_ref) == TRUE) {
strncpy(full_path,cfull_path,strlen(cfull_path)+1);
full_path[strlen(cfull_path)]='\0';
obtain_vdata_path (file_id, full_path, obj_ref);
}
// If this object is vdata
else if (Visvs (vgroup_id, obj_ref)) {
// Obtain vdata ID
vdata_id = VSattach (file_id, obj_ref, "r");
if (vdata_id == FAIL) {
err_msg = "VSattach failed";
VS_or_mem_err = true;
goto cleanFail;
}
// Obtain vdata name
status = VSgetname (vdata_id, vdata_name);
if (status == FAIL) {
err_msg = "VSgetname failed";
VS_or_mem_err = true;
goto cleanFail;
}
// Obtain vdata class name
status = VSgetclass (vdata_id, vdata_class);
if (status == FAIL) {
err_msg = "VSgetclass failed";
VS_or_mem_err = true;
goto cleanFail;
}
// Ignore the vdata to store internal HDF structure and the vdata used as an attribute
if (VSisattr (vdata_id) == TRUE
|| !strncmp (vdata_class, _HDF_CHK_TBL_CLASS,
strlen (_HDF_CHK_TBL_CLASS))
|| !strncmp (vdata_class, _HDF_SDSVAR,
strlen (_HDF_SDSVAR))
|| !strncmp (vdata_class, _HDF_CRDVAR,
strlen (_HDF_CRDVAR))
|| !strncmp (vdata_class, DIM_VALS, strlen (DIM_VALS))
|| !strncmp (vdata_class, DIM_VALS01,
strlen (DIM_VALS01))
|| !strncmp (vdata_class, RIGATTRCLASS,
strlen (RIGATTRCLASS))
|| !strncmp (vdata_name, RIGATTRNAME,
strlen (RIGATTRNAME))) {
status = VSdetach (vdata_id);
if (status == FAIL) {
err_msg = "VSdetach failed in the if block to ignore the HDF4 internal attributes.";
VS_or_mem_err = true;
goto cleanFail;
}
}
// Now user-defined vdata
else {
VDATA *vdataobj = NULL;
try {
vdataobj = VDATA::Read (vdata_id, obj_ref);
}
catch(...) {
free (full_path);
free (cfull_path);
VSdetach(vdata_id);
Vdetach (vgroup_id);
throw;
}
if(full_path != NULL)//Make coverity happy since it doesn't understand the throw macro
vdataobj->newname = full_path +vdataobj->name;
//We want to map fields of vdata with more than 10 records to DAP variables
// and we need to add the path and vdata name to the new vdata field name
if (!vdataobj->getTreatAsAttrFlag ()) {
for (std::vector <VDField * >::const_iterator it_vdf =
vdataobj->getFields ().begin ();
it_vdf != vdataobj->getFields ().end ();
it_vdf++) {
// Change vdata name conventions.
// "vdata"+vdata_newname+"_vdf_"+(*it_vdf)->newname
(*it_vdf)->newname =
"vdata" + vdataobj->newname + "_vdf_" + (*it_vdf)->name;
//Make sure the name is following CF, KY 2012-6-26
(*it_vdf)->newname = HDFCFUtil::get_CF_string((*it_vdf)->newname);
}
}
// Make sure the name is following CF
vdataobj->newname = HDFCFUtil::get_CF_string(vdataobj->newname);
// Save back this vdata
this->vds.push_back (vdataobj);
status = VSdetach (vdata_id);
if (status == FAIL) {
err_msg = "VSdetach failed in the user-defined vdata block";
VS_or_mem_err = true;
goto cleanFail;
}
}
}
//Ignore the handling of SDS objects. They are handled elsewhere.
else{
}
}
// STOP: add error handling
cleanFail:
if(full_path != NULL)
free (full_path);
if(cfull_path != NULL)
free (cfull_path);
status = Vdetach (vgroup_id);
if (status == FAIL) {
throw3 ("Vdetach failed ", "vgroup_name is ", vgroup_name);
}
if(true == VS_or_mem_err)
throw3(err_msg,"vgroup_name is ",vgroup_name);
}//end of the for loop
}// end of the if loop
}
// Check if this is a special SDS(MOD08_M3) that needs the connection between CVs and dimension names.
// General algorithm:
// 1. Insert a set for fields' dimensions,
// 2. in the mean time, insert a set for 1-D field
// 3. For each dimension in the set, search if one can find the corresponding field that has the same dimension name in the set.
// Return false if non-found occurs.
// Else return true.
bool
File::Check_update_special(const string& grid_name) throw(Exception) {
set<string> dimnameset;
set<SDField*> fldset;
// Build up a dimension set and a 1-D field set.
// We already know that XDim and YDim should be in the dimension set. so inserting them.
// Hopefully by doing this, we can save some time since many variables have dimensions
// "XDim" and "YDim" and excluding "XDim" and "YDim" may save some time if there are many
// dimensions in the dimnameset.
string FullXDim;
string FullYDim;
FullXDim="XDim:" ;
FullYDim="YDim:";
FullXDim= FullXDim+grid_name;
FullYDim =FullYDim+grid_name;
for (vector < SDField * >::const_iterator i =
this->sd->getFields ().begin ();
i != this->sd->getFields ().end (); ++i) {
for (vector < Dimension * >::const_iterator k =
(*i)->getDimensions ().begin ();
k != (*i)->getDimensions ().end (); ++k) {
if((*k)->getName() !=FullXDim && (*k)->getName()!=FullYDim)
dimnameset.insert((*k)->getName());
}
if (1==(*i)->getRank())
fldset.insert(*i);
}
// Check if all dimension names in the dimension set can be found in the 1-D variable sets. Moreover, the size of a dimension
// should be smaller or the same as the size of 1-D variable.
// Plus XDim and YDim for number of dimensions
if (fldset.size() < (dimnameset.size()+2))
return false;
int total_num_dims = 0;
size_t grid_name_size = grid_name.size();
string reduced_dimname;
for(set<SDField*>::const_iterator j =
fldset.begin(); j!=fldset.end(); ++ j) {
size_t dim_size = ((*j)->getDimensions())[0]->getName().size();
if( dim_size > grid_name_size){
reduced_dimname = ((*j)->getDimensions())[0]->getName().substr(0,dim_size-grid_name_size-1);
if ((*j)->getName() == reduced_dimname)
total_num_dims++;
}
}
if((size_t)total_num_dims != (dimnameset.size()+2))
return false;
// Updated dimension names for all variables: removing the grid_name prefix.
for (vector < SDField * >::const_iterator i =
this->sd->getFields ().begin ();
i != this->sd->getFields ().end (); ++i) {
for (vector < Dimension * >::const_iterator k =
(*i)->getDimensions ().begin ();
k != (*i)->getDimensions ().end (); ++k) {
size_t dim_size = (*k)->getName().size();
if( dim_size > grid_name_size){
reduced_dimname = (*k)->getName().substr(0,dim_size-grid_name_size-1);
(*k)->name = reduced_dimname;
}
else // Here we enforce that the dimension name has the grid suffix. This can be lifted in the future. KY 2014-01-16
return false;
}
}
// Build up Dimensions for DDS and DAS.
for(std::set<SDField*>::const_iterator j =
fldset.begin(); j!=fldset.end(); ++ j) {
if ((*j)->getName() == ((*j)->getDimensions())[0]->getName()) {
if("XDim" == (*j)->getName()){
std::string tempunits = "degrees_east";
(*j)->setUnits (tempunits);
(*j)->fieldtype = 2;
}
else if("YDim" == (*j)->getName()){
std::string tempunits = "degrees_north";
(*j)->setUnits (tempunits);
(*j)->fieldtype = 1;
}
else if("Pressure_Level" == (*j)->getName()) {
std::string tempunits = "hPa";
(*j)->setUnits (tempunits);
(*j)->fieldtype = 3;
}
else {
std::string tempunits = "level";
(*j)->setUnits (tempunits);
(*j)->fieldtype = 3;
}
}
}
return true;
}
#if 0
// This routine is used to check if this grid is a special MOD08M3-like grid in DDS-build.
// Check_if_special is used when building DAS. The reason to separate is that we pass the
// File pointer from DAS to DDS to reduce the building time.
// How to check:
// 1)
bool
File::Check_if_special(const string& grid_name) throw(Exception) {
bool xdim_is_lon = false;
bool ydim_is_lat = false;
bool pre_unit_hpa = true;
for (vector < SDField * >::const_iterator i =
this->sd->getFields ().begin ();
i != this->sd->getFields ().end (); ++i) {
if (1==(*i)->getRank()) {
if(1 == ((*i)->fieldtype)) {
if("YDim" == (*j)->getName()