-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHDFEOS2Array_RealField.cc
More file actions
2158 lines (1894 loc) · 86.3 KB
/
HDFEOS2Array_RealField.cc
File metadata and controls
2158 lines (1894 loc) · 86.3 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.
// It retrieves the real field values.
// Authors: MuQun Yang <myang6@hdfgroup.org> Eunsoo Seo
// Copyright (c) 2010-2012 The HDF Group
/////////////////////////////////////////////////////////////////////////////
#ifdef USE_HDFEOS2_LIB
#include "config.h"
#include "config_hdf.h"
#include <iostream>
#include <sstream>
#include <cassert>
#include <libdap/debug.h>
#include <libdap/InternalErr.h>
#include <BESDebug.h>
#include <BESLog.h>
#include "HDFCFUtil.h"
#include "HDFEOS2Array_RealField.h"
#include "dodsutil.h"
#include "HDF4RequestHandler.h"
using namespace std;
using namespace libdap;
#define SIGNED_BYTE_TO_INT32 1
bool
HDFEOS2Array_RealField::read ()
{
BESDEBUG("h4","Coming to HDFEOS2_Array_RealField read "<<endl);
if(length() == 0)
return true;
#if 0
string check_pass_fileid_key_str="H4.EnablePassFileID";
bool check_pass_fileid_key = false;
check_pass_fileid_key = HDFCFUtil::check_beskeys(check_pass_fileid_key_str);
#endif
bool check_pass_fileid_key = HDF4RequestHandler::get_pass_fileid();
// Declare offset, count and step
vector<int>offset;
offset.resize(rank);
vector<int>count;
count.resize(rank);
vector<int>step;
step.resize(rank);
// Obtain offset,step and count from the client expression constraint
int nelms = 0;
nelms = format_constraint (&offset[0], &step[0], &count[0]);
// Just declare offset,count and step in the int32 type.
vector<int32>offset32;
offset32.resize(rank);
vector<int32>count32;
count32.resize(rank);
vector<int32>step32;
step32.resize(rank);
// Just obtain the offset,count and step in the datatype of int32.
for (int i = 0; i < rank; i++) {
offset32[i] = (int32) offset[i];
count32[i] = (int32) count[i];
step32[i] = (int32) step[i];
}
// Define function pointers to handle both grid and swath
int32 (*openfunc) (char *, intn);
intn (*closefunc) (int32);
int32 (*attachfunc) (int32, char *);
intn (*detachfunc) (int32);
intn (*fieldinfofunc) (int32, char *, int32 *, int32 *, int32 *, char *);
string datasetname;
if (swathname == "") {
openfunc = GDopen;
closefunc = GDclose;
attachfunc = GDattach;
detachfunc = GDdetach;
fieldinfofunc = GDfieldinfo;
datasetname = gridname;
}
else if (gridname == "") {
openfunc = SWopen;
closefunc = SWclose;
attachfunc = SWattach;
detachfunc = SWdetach;
fieldinfofunc = SWfieldinfo;
datasetname = swathname;
}
else
throw InternalErr (__FILE__, __LINE__, "It should be either grid or swath.");
// Note gfid and gridid represent either swath or grid.
int32 gfid = 0;
int32 gridid = 0;
if (true == isgeofile || false == check_pass_fileid_key) {
// Obtain the EOS object ID(either grid or swath)
gfid = openfunc (const_cast < char *>(filename.c_str ()), DFACC_READ);
if (gfid < 0) {
ostringstream eherr;
eherr << "File " << filename.c_str () << " cannot be open.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
}
else
gfid = gsfd;
// Attach the EOS object ID
gridid = attachfunc (gfid, const_cast < char *>(datasetname.c_str ()));
if (gridid < 0) {
close_fileid(gfid,-1);
ostringstream eherr;
eherr << "Grid/Swath " << datasetname.c_str () << " cannot be attached.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
#if 0
string check_disable_scale_comp_key = "H4.DisableScaleOffsetComp";
bool turn_on_disable_scale_comp_key= false;
turn_on_disable_scale_comp_key = HDFCFUtil::check_beskeys(check_disable_scale_comp_key);
#endif
bool is_modis_l1b = false;
if("MODIS_SWATH_Type_L1B" == swathname)
is_modis_l1b = true;
bool is_modis_vip = false;
if ("VIP_CMG_GRID" == gridname)
is_modis_vip = true;
bool field_is_vdata = false;
// HDF-EOS2 swath maps 1-D field as vdata. So we need to check if this field is vdata or SDS.
// Essentially we only call SDS attribute routines to retrieve MODIS scale,offset and
// fillvalue attributes since we don't
// find 1-D MODIS field has scale,offset and fillvalue attributes. We may need to visit
// this again in the future to see if we also need to support the handling of
// scale,offset,fillvalue via vdata routines. KY 2013-07-15
if (""==gridname) {
int32 tmp_rank = 0;
char tmp_dimlist[1024];
int32 tmp_dims[rank];
int32 field_dtype = 0;
intn r = 0;
r = fieldinfofunc (gridid, const_cast < char *>(fieldname.c_str ()),
&tmp_rank, tmp_dims, &field_dtype, tmp_dimlist);
if (r != 0) {
detachfunc(gridid);
close_fileid(gfid,-1);
ostringstream eherr;
eherr << "Field " << fieldname.c_str () << " information cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (1 == tmp_rank)
field_is_vdata = true;
}
bool has_Key_attr = false;
if (false == field_is_vdata) {
// Obtain attribute values.
int32 sdfileid = -1;
if (true == isgeofile || false == check_pass_fileid_key) {
sdfileid = SDstart(const_cast < char *>(filename.c_str ()), DFACC_READ);
if (FAIL == sdfileid) {
detachfunc(gridid);
close_fileid(gfid,-1);
ostringstream eherr;
eherr << "Cannot Start the SD interface for the file " << filename <<endl;
}
}
else
sdfileid = sdfd;
int32 sdsindex = -1;
int32 sdsid = -1;
sdsindex = SDnametoindex(sdfileid, fieldname.c_str());
if (FAIL == sdsindex) {
detachfunc(gridid);
close_fileid(gfid,sdfileid);
ostringstream eherr;
eherr << "Cannot obtain the index of " << fieldname;
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
sdsid = SDselect(sdfileid, sdsindex);
if (FAIL == sdsid) {
detachfunc(gridid);
close_fileid(gfid,sdfileid);
ostringstream eherr;
eherr << "Cannot obtain the SDS ID of " << fieldname;
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Here we cannot check if SDfindattr fails since even SDfindattr fails it doesn't mean
// errors happen. If no such attribute can be found, SDfindattr still returns FAIL.
// The correct way is to use SDgetinfo and SDattrinfo to check if attributes
// "radiance_scales" etc exist.
// For the time being, I won't do this, due to the performance reason and code simplicity and also the
// very small chance of real FAIL for SDfindattr.
if(SDfindattr(sdsid, "Key")!=FAIL)
has_Key_attr = true;
// Close the interfaces
SDendaccess(sdsid);
if (true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
}
// USE a try-catch block to release the resources.
try {
if((false == is_modis_l1b) && (false == is_modis_vip)
&&(false == has_Key_attr) && (true == HDF4RequestHandler::get_disable_scaleoffset_comp()))
write_dap_data_disable_scale_comp(gridid,nelms,&offset32[0],&count32[0],&step32[0]);
else
write_dap_data_scale_comp(gridid,nelms,offset32,count32,step32);
}
catch(...) {
detachfunc(gridid);
close_fileid(gfid,-1);
throw;
}
int32 r = -1;
r = detachfunc (gridid);
if (r != 0) {
close_fileid(gfid,-1);
ostringstream eherr;
eherr << "Grid/Swath " << datasetname.c_str () << " cannot be detached.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if(true == isgeofile || false == check_pass_fileid_key) {
r = closefunc (gfid);
if (r != 0) {
ostringstream eherr;
eherr << "Grid/Swath " << filename.c_str () << " cannot be closed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
}
return false;
}
int
HDFEOS2Array_RealField::write_dap_data_scale_comp(int32 gridid,
int nelms,
vector<int32>& offset32,
vector<int32>& count32,
vector<int32>& step32) {
BESDEBUG("h4",
"coming to HDFEOS2Array_RealField write_dap_data_scale_comp "
<<endl);
#if 0
string check_pass_fileid_key_str="H4.EnablePassFileID";
bool check_pass_fileid_key = false;
check_pass_fileid_key = HDFCFUtil::check_beskeys(check_pass_fileid_key_str);
#endif
bool check_pass_fileid_key = HDF4RequestHandler::get_pass_fileid();
// Define function pointers to handle both grid and swath
intn (*fieldinfofunc) (int32, char *, int32 *, int32 *, int32 *, char *);
intn (*readfieldfunc) (int32, char *, int32 *, int32 *, int32 *, void *);
if (swathname == "") {
fieldinfofunc = GDfieldinfo;
readfieldfunc = GDreadfield;
}
else if (gridname == "") {
fieldinfofunc = SWfieldinfo;
readfieldfunc = SWreadfield;
}
else
throw InternalErr (__FILE__, __LINE__, "It should be either grid or swath.");
// tmp_rank and tmp_dimlist are two dummy variables that are only used when calling fieldinfo.
int32 tmp_rank = 0;
char tmp_dimlist[1024];
// field dimension sizes
int32 tmp_dims[rank];
// field data type
int32 field_dtype = 0;
// returned value of HDF4 and HDF-EOS2 APIs
intn r = 0;
// Obtain the field info. We mainly need the datatype information
// to allocate the buffer to store the data
r = fieldinfofunc (gridid, const_cast < char *>(fieldname.c_str ()),
&tmp_rank, tmp_dims, &field_dtype, tmp_dimlist);
if (r != 0) {
ostringstream eherr;
eherr << "Field " << fieldname.c_str () << " information cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// The following chunk of code until switch(field_dtype) handles MODIS level 1B,
// MOD29E1D Key and VIP products. The reason to keep the code this way is due to
// use of RECALCULATE macro. It is too much work to change it now. KY 2013-12-17
// MODIS level 1B reflectance and radiance fields have scale/offset arrays rather
// than one scale/offset value.
// So we need to handle these fields specially.
float *reflectance_offsets =NULL;
float *reflectance_scales =NULL;
float *radiance_offsets =NULL;
float *radiance_scales =NULL;
// Attribute datatype, reused for several attributes
int32 attr_dtype = 0;
// Number of elements for an attribute, reused
int32 temp_attrcount = 0;
// Number of elements in an attribute
int32 num_eles_of_an_attr = 0;
// Attribute(radiance_scales, reflectance_scales) index
int32 cf_modl1b_rr_attrindex = 0;
// Attribute (radiance_offsets) index
int32 cf_modl1b_rr_attrindex2 = 0;
// Attribute valid_range index
int32 cf_vr_attrindex = 0;
// Attribute fill value index
int32 cf_fv_attrindex = 0;
// Scale factor attribute index
int32 scale_factor_attr_index = 0;
// Add offset attribute index
int32 add_offset_attr_index = 0;
// Initialize scale
float scale = 1;
// Intialize field_offset
float field_offset = 0;
// Initialize fillvalue
float fillvalue = 0;
// Initialize the original valid_min
float orig_valid_min = 0;
// Initialize the original valid_max
float orig_valid_max = 0;
// Some NSIDC products use the "Key" attribute to identify
// the discrete valid values(land, cloud etc).
// Since the valid_range attribute in these products may treat values
// identified by the Key attribute as invalid,
// we need to handle them in a special way.So set a flag here.
bool has_Key_attr = false;
int32 sdfileid = -1;
if(sotype!=DEFAULT_CF_EQU) {
bool field_is_vdata = false;
// HDF-EOS2 swath maps 1-D field as vdata. So we need to check
// if this field is vdata or SDS.
// Essentially we only call SDS attribute routines to retrieve MODIS scale,
// offset and fillvalue
// attributes since we don't find 1-D MODIS field has scale,offset and
// fillvalue attributes.
// We may need to visit this again in the future to see
// if we also need to support the handling of scale,offset,fillvalue via
// vdata routines. KY 2013-07-15
if (""==gridname) {
r = fieldinfofunc (gridid, const_cast < char *>(fieldname.c_str ()),
&tmp_rank, tmp_dims, &field_dtype, tmp_dimlist);
if (r != 0) {
ostringstream eherr;
eherr << "Field " << fieldname.c_str ()
<< " information cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (1 == tmp_rank)
field_is_vdata = true;
}
/// DEL LATER
#if 0
r = fieldinfofunc (gridid, const_cast < char *>(fieldname.c_str ()),
&tmp_rank, tmp_dims, &field_dtype, tmp_dimlist);
if (r != 0) {
ostringstream eherr;
eherr << "Field " << fieldname.c_str () << " information cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
cerr<<"tmp_rank is "<<tmp_rank <<endl;
#endif
// For swath, we don't see any MODIS 1-D fields that have scale,offset
// and fill value attributes that need to be changed.
// So now we don't need to handle the vdata handling.
// Another reason is the possible change of the implementation
// of the SDS attribute handlings. That may be too costly.
// KY 2012-07-31
if( false == field_is_vdata) {
char attrname[H4_MAX_NC_NAME + 1];
vector<char> attrbuf;
// Obtain attribute values.
if(false == isgeofile || false == check_pass_fileid_key)
sdfileid = sdfd;
else {
sdfileid = SDstart(const_cast < char *>(filename.c_str ()), DFACC_READ);
if (FAIL == sdfileid) {
ostringstream eherr;
eherr << "Cannot Start the SD interface for the file "
<< filename;
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
}
int32 sdsindex = -1;
int32 sdsid;
sdsindex = SDnametoindex(sdfileid, fieldname.c_str());
if (FAIL == sdsindex) {
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
ostringstream eherr;
eherr << "Cannot obtain the index of " << fieldname;
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
sdsid = SDselect(sdfileid, sdsindex);
if (FAIL == sdsid) {
if (true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
ostringstream eherr;
eherr << "Cannot obtain the SDS ID of " << fieldname;
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
#if 0
char attrname[H4_MAX_NC_NAME + 1];
vector<char> attrbuf, attrbuf2;
// Here we cannot check if SDfindattr fails or not since even SDfindattr fails it doesn't mean
// errors happen. If no such attribute can be found, SDfindattr still returns FAIL.
// The correct way is to use SDgetinfo and SDattrinfo to check if attributes "radiance_scales" etc exist.
// For the time being, I won't do this, due to the performance reason and code simplity and also the
// very small chance of real FAIL for SDfindattr.
cf_general_attrindex = SDfindattr(sdsid, "radiance_scales");
cf_general_attrindex2 = SDfindattr(sdsid, "radiance_offsets");
// Obtain the values of radiance_scales and radiance_offsets if they are available.
if(cf_general_attrindex!=FAIL && cf_general_attrindex2!=FAIL)
{
intn ret = -1;
ret = SDattrinfo(sdsid, cf_general_attrindex, attrname, &attr_dtype, &temp_attrcount);
if (ret==FAIL)
{
SDendaccess(sdsid);
if(true == isgeofile)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'radiance_scales' in " << fieldname.c_str () << " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
attrbuf.clear();
attrbuf.resize(DFKNTsize(attr_dtype)*temp_attrcount);
ret = SDreadattr(sdsid, cf_general_attrindex, (VOIDP)&attrbuf[0]);
if (ret==FAIL)
{
release_mod1b_res(reflectance_scales,reflectance_offsets,radiance_scales,radiance_offsets);
SDendaccess(sdsid);
if (true == isgeofile)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'radiance_scales' in " << fieldname.c_str () << " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
ret = SDattrinfo(sdsid, cf_general_attrindex2, attrname, &attr_dtype, &temp_attrcount);
if (ret==FAIL)
{
release_mod1b_res(reflectance_scales,reflectance_offsets,radiance_scales,radiance_offsets);
SDendaccess(sdsid);
if(true == isgeofile)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'radiance_offsets' in " << fieldname.c_str () << " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
attrbuf2.clear();
attrbuf2.resize(DFKNTsize(attr_dtype)*temp_attrcount);
ret = SDreadattr(sdsid, cf_general_attrindex2, (VOIDP)&attrbuf2[0]);
if (ret==FAIL)
{
release_mod1b_res(reflectance_scales,reflectance_offsets,radiance_scales,radiance_offsets);
SDendaccess(sdsid);
if(true == isgeofile)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'radiance_offsets' in " << fieldname.c_str () << " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// The following macro will obtain radiance_scales and radiance_offsets.
// Although the code is compact, it may not be easy to follow. The similar macro can also be found later.
switch(attr_dtype)
{
#define GET_RADIANCE_SCALES_OFFSETS_ATTR_VALUES(TYPE, CAST) \
case DFNT_##TYPE: \
{ \
CAST *ptr = (CAST*)&attrbuf[0]; \
CAST *ptr2 = (CAST*)&attrbuf2[0]; \
radiance_scales = new float[temp_attrcount]; \
radiance_offsets = new float[temp_attrcount]; \
for(int l=0; l<temp_attrcount; l++) \
{ \
radiance_scales[l] = ptr[l]; \
radiance_offsets[l] = ptr2[l]; \
} \
} \
break;
GET_RADIANCE_SCALES_OFFSETS_ATTR_VALUES(FLOAT32, float);
GET_RADIANCE_SCALES_OFFSETS_ATTR_VALUES(FLOAT64, double);
}
#undef GET_RADIANCE_SCALES_OFFSETS_ATTR_VALUES
num_eles_of_an_attr = temp_attrcount; // Store the count of attributes.
}
// Obtain attribute values of reflectance_scales and reflectance_offsets if they are available.
cf_general_attrindex = SDfindattr(sdsid, "reflectance_scales");
cf_general_attrindex2 = SDfindattr(sdsid, "reflectance_offsets");
if(cf_general_attrindex!=FAIL && cf_general_attrindex2!=FAIL)
{
intn ret = -1;
ret = SDattrinfo(sdsid, cf_general_attrindex, attrname, &attr_dtype, &temp_attrcount);
if (ret==FAIL)
{
release_mod1b_res(reflectance_scales,reflectance_offsets,radiance_scales,radiance_offsets);
SDendaccess(sdsid);
if(true == isgeofile)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'reflectance_scales' in " << fieldname.c_str () << " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
attrbuf.clear();
attrbuf.resize(DFKNTsize(attr_dtype)*temp_attrcount);
ret = SDreadattr(sdsid, cf_general_attrindex, (VOIDP)&attrbuf[0]);
if (ret==FAIL)
{
release_mod1b_res(reflectance_scales,reflectance_offsets,radiance_scales,radiance_offsets);
SDendaccess(sdsid);
if(true == isgeofile)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'reflectance_scales' in " << fieldname.c_str () << " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
ret = SDattrinfo(sdsid, cf_general_attrindex2, attrname, &attr_dtype, &temp_attrcount);
if (ret==FAIL)
{
release_mod1b_res(reflectance_scales,reflectance_offsets,radiance_scales,radiance_offsets);
SDendaccess(sdsid);
if(true == isgeofile)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'reflectance_offsets' in " << fieldname.c_str () << " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
attrbuf2.clear();
attrbuf2.resize(DFKNTsize(attr_dtype)*temp_attrcount);
ret = SDreadattr(sdsid, cf_general_attrindex2, (VOIDP)&attrbuf2[0]);
if (ret==FAIL)
{
release_mod1b_res(reflectance_scales,reflectance_offsets,radiance_scales,radiance_offsets);
SDendaccess(sdsid);
if(true == isgeofile)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'reflectance_offsets' in " << fieldname.c_str () << " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
switch(attr_dtype)
{
#define GET_REFLECTANCE_SCALES_OFFSETS_ATTR_VALUES(TYPE, CAST) \
case DFNT_##TYPE: \
{ \
CAST *ptr = (CAST*)&attrbuf[0]; \
CAST *ptr2 = (CAST*)&attrbuf2[0]; \
reflectance_scales = new float[temp_attrcount]; \
reflectance_offsets = new float[temp_attrcount]; \
for(int l=0; l<temp_attrcount; l++) \
{ \
reflectance_scales[l] = ptr[l]; \
reflectance_offsets[l] = ptr2[l]; \
} \
} \
break;
GET_REFLECTANCE_SCALES_OFFSETS_ATTR_VALUES(FLOAT32, float);
GET_REFLECTANCE_SCALES_OFFSETS_ATTR_VALUES(FLOAT64, double);
}
#undef GET_REFLECTANCE_SCALES_OFFSETS_ATTR_VALUES
num_eles_of_an_attr = temp_attrcount; // Store the count of attributes.
}
#endif
// Obtain the value of attribute scale_factor.
scale_factor_attr_index = SDfindattr(sdsid, "scale_factor");
if(scale_factor_attr_index!=FAIL)
{
intn ret = -1;
ret = SDattrinfo(sdsid, scale_factor_attr_index, attrname,
&attr_dtype, &temp_attrcount);
if (ret==FAIL)
{
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'scale_factor' in "
<< fieldname.c_str () << " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
attrbuf.clear();
attrbuf.resize(DFKNTsize(attr_dtype)*temp_attrcount);
ret = SDreadattr(sdsid, scale_factor_attr_index, (VOIDP)&attrbuf[0]);
if (ret==FAIL)
{
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'scale_factor' in "
<< fieldname.c_str () << " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
switch(attr_dtype)
{
#define GET_SCALE_FACTOR_ATTR_VALUE(TYPE, CAST) \
case DFNT_##TYPE: \
{ \
CAST tmpvalue = *(CAST*)&attrbuf[0]; \
scale = (float)tmpvalue; \
} \
break;
GET_SCALE_FACTOR_ATTR_VALUE(INT8, int8);
GET_SCALE_FACTOR_ATTR_VALUE(CHAR,int8);
GET_SCALE_FACTOR_ATTR_VALUE(UINT8, uint8);
GET_SCALE_FACTOR_ATTR_VALUE(UCHAR,uint8);
GET_SCALE_FACTOR_ATTR_VALUE(INT16, int16);
GET_SCALE_FACTOR_ATTR_VALUE(UINT16, uint16);
GET_SCALE_FACTOR_ATTR_VALUE(INT32, int32);
GET_SCALE_FACTOR_ATTR_VALUE(UINT32, uint32);
GET_SCALE_FACTOR_ATTR_VALUE(FLOAT32, float);
GET_SCALE_FACTOR_ATTR_VALUE(FLOAT64, double);
default:
throw InternalErr(__FILE__,__LINE__,"unsupported data type.");
};
#undef GET_SCALE_FACTOR_ATTR_VALUE
}
// Obtain the value of attribute add_offset
add_offset_attr_index = SDfindattr(sdsid, "add_offset");
if(add_offset_attr_index!=FAIL)
{
intn ret;
ret = SDattrinfo(sdsid, add_offset_attr_index, attrname,
&attr_dtype, &temp_attrcount);
if (ret==FAIL)
{
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'add_offset' in " << fieldname.c_str ()
<< " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
attrbuf.clear();
attrbuf.resize(DFKNTsize(attr_dtype)*temp_attrcount);
ret = SDreadattr(sdsid, add_offset_attr_index, (VOIDP)&attrbuf[0]);
if (ret==FAIL)
{
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute 'add_offset' in " << fieldname.c_str ()
<< " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
switch(attr_dtype)
{
#define GET_ADD_OFFSET_ATTR_VALUE(TYPE, CAST) \
case DFNT_##TYPE: \
{ \
CAST tmpvalue = *(CAST*)&attrbuf[0]; \
field_offset = (float)tmpvalue; \
} \
break;
GET_ADD_OFFSET_ATTR_VALUE(INT8, int8);
GET_ADD_OFFSET_ATTR_VALUE(CHAR,int8);
GET_ADD_OFFSET_ATTR_VALUE(UINT8, uint8);
GET_ADD_OFFSET_ATTR_VALUE(UCHAR,uint8);
GET_ADD_OFFSET_ATTR_VALUE(INT16, int16);
GET_ADD_OFFSET_ATTR_VALUE(UINT16, uint16);
GET_ADD_OFFSET_ATTR_VALUE(INT32, int32);
GET_ADD_OFFSET_ATTR_VALUE(UINT32, uint32);
GET_ADD_OFFSET_ATTR_VALUE(FLOAT32, float);
GET_ADD_OFFSET_ATTR_VALUE(FLOAT64, double);
default:
throw InternalErr(__FILE__,__LINE__,"unsupported data type.");
};
#undef GET_ADD_OFFSET_ATTR_VALUE
}
// Obtain the value of the attribute _FillValue
cf_fv_attrindex = SDfindattr(sdsid, "_FillValue");
if(cf_fv_attrindex!=FAIL)
{
intn ret;
ret = SDattrinfo(sdsid, cf_fv_attrindex, attrname, &attr_dtype, &temp_attrcount);
if (ret==FAIL)
{
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute '_FillValue' in " << fieldname.c_str ()
<< " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
attrbuf.clear();
attrbuf.resize(DFKNTsize(attr_dtype)*temp_attrcount);
ret = SDreadattr(sdsid, cf_fv_attrindex, (VOIDP)&attrbuf[0]);
if (ret==FAIL)
{
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute '_FillValue' in " << fieldname.c_str ()
<< " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
switch(attr_dtype)
{
#define GET_FILLVALUE_ATTR_VALUE(TYPE, CAST) \
case DFNT_##TYPE: \
{ \
CAST tmpvalue = *(CAST*)&attrbuf[0]; \
fillvalue = (float)tmpvalue; \
} \
break;
GET_FILLVALUE_ATTR_VALUE(INT8, int8);
GET_FILLVALUE_ATTR_VALUE(CHAR, int8);
GET_FILLVALUE_ATTR_VALUE(INT16, int16);
GET_FILLVALUE_ATTR_VALUE(INT32, int32);
GET_FILLVALUE_ATTR_VALUE(UINT8, uint8);
GET_FILLVALUE_ATTR_VALUE(UCHAR, uint8);
GET_FILLVALUE_ATTR_VALUE(UINT16, uint16);
GET_FILLVALUE_ATTR_VALUE(UINT32, uint32);
default:
throw InternalErr(__FILE__,__LINE__,"unsupported data type.");
};
#undef GET_FILLVALUE_ATTR_VALUE
}
// Retrieve valid_range,valid_range is normally represented as (valid_min,valid_max)
// for non-CF scale and offset rules, the data is always float. So we only
// need to change the data type to float.
cf_vr_attrindex = SDfindattr(sdsid, "valid_range");
if(cf_vr_attrindex!=FAIL)
{
intn ret;
ret = SDattrinfo(sdsid, cf_vr_attrindex, attrname, &attr_dtype, &temp_attrcount);
if (ret==FAIL)
{
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute '_FillValue' in " << fieldname.c_str ()
<< " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
attrbuf.clear();
attrbuf.resize(DFKNTsize(attr_dtype)*temp_attrcount);
ret = SDreadattr(sdsid, cf_vr_attrindex, (VOIDP)&attrbuf[0]);
if (ret==FAIL)
{
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
ostringstream eherr;
eherr << "Attribute '_FillValue' in " << fieldname.c_str ()
<< " cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
string attrbuf_str(attrbuf.begin(),attrbuf.end());
switch(attr_dtype) {
case DFNT_CHAR:
{
// We need to treat the attribute data as characters or string.
// So find the separator.
size_t found = attrbuf_str.find_first_of(",");
size_t found_from_end = attrbuf_str.find_last_of(",");
if (string::npos == found){
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
throw InternalErr(__FILE__,__LINE__,"should find the separator ,");
}
if (found != found_from_end){
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
throw InternalErr(__FILE__,__LINE__,
"Only one separator , should be available.");
}
#if 0
//istringstream(attrbuf_str.substr(0,found))>> orig_valid_min;
//istringstream(attrbuf_str.substr(found+1))>> orig_valid_max;
#endif
orig_valid_min = atof((attrbuf_str.substr(0,found)).c_str());
orig_valid_max = atof((attrbuf_str.substr(found+1)).c_str());
}
break;
case DFNT_INT8:
{
// We find a special case that even valid_range is logically
// interpreted as two elements,
// but the count of attribute elements is more than 2. The count
// actually is the number of
// characters stored as the attribute value. So we need to find
// the separator "," and then
// change the string before and after the separator into float numbers.
//
if (temp_attrcount >2) {
size_t found = attrbuf_str.find_first_of(",");
size_t found_from_end = attrbuf_str.find_last_of(",");
if (string::npos == found){
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
throw InternalErr(__FILE__,__LINE__,"should find the separator ,");
}
if (found != found_from_end){
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
throw InternalErr(__FILE__,__LINE__,
"Only one separator , should be available.");
}
//istringstream(attrbuf_str.substr(0,found))>> orig_valid_min;
//istringstream(attrbuf_str.substr(found+1))>> orig_valid_max;
orig_valid_min = atof((attrbuf_str.substr(0,found)).c_str());
orig_valid_max = atof((attrbuf_str.substr(found+1)).c_str());
}
else if (2 == temp_attrcount) {
orig_valid_min = (float)attrbuf[0];
orig_valid_max = (float)attrbuf[1];
}
else {
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
throw InternalErr(__FILE__,__LINE__,
"The number of attribute count should be greater than 1.");
}
}
break;
case DFNT_UINT8:
case DFNT_UCHAR:
{
if (temp_attrcount != 2) {
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
throw InternalErr(__FILE__,__LINE__,
"The number of attribute count should be 2 for the DFNT_UINT8 type.");
}
unsigned char* temp_valid_range = (unsigned char *)&attrbuf[0];
orig_valid_min = (float)(temp_valid_range[0]);
orig_valid_max = (float)(temp_valid_range[1]);
}
break;
case DFNT_INT16:
{
if (temp_attrcount != 2) {
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
throw InternalErr(__FILE__,__LINE__,
"The number of attribute count should be 2 for the DFNT_INT16 type.");
}
short* temp_valid_range = (short *)&attrbuf[0];
orig_valid_min = (float)(temp_valid_range[0]);
orig_valid_max = (float)(temp_valid_range[1]);
}
break;
case DFNT_UINT16:
{
if (temp_attrcount != 2) {
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
throw InternalErr(__FILE__,__LINE__,
"The number of attribute count should be 2 for the DFNT_UINT16 type.");
}
unsigned short* temp_valid_range = (unsigned short *)&attrbuf[0];
orig_valid_min = (float)(temp_valid_range[0]);
orig_valid_max = (float)(temp_valid_range[1]);
}
break;
case DFNT_INT32:
{
if (temp_attrcount != 2) {
SDendaccess(sdsid);
if(true == isgeofile || false == check_pass_fileid_key)
SDend(sdfileid);
throw InternalErr(__FILE__,__LINE__,
"The number of attribute count should be 2 for the DFNT_INT32 type.");
}
int* temp_valid_range = (int *)&attrbuf[0];
orig_valid_min = (float)(temp_valid_range[0]);
orig_valid_max = (float)(temp_valid_range[1]);