-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHDFEOS2ArraySwathDimMapField.cc
More file actions
1636 lines (1383 loc) · 57.3 KB
/
HDFEOS2ArraySwathDimMapField.cc
File metadata and controls
1636 lines (1383 loc) · 57.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
/////////////////////////////////////////////////////////////////////////////
// Retrieves the latitude and longitude of the HDF-EOS2 Swath with dimension map
// Authors: MuQun Yang <myang6@hdfgroup.org>
// Copyright (c) 2010-2012 The HDF Group
/////////////////////////////////////////////////////////////////////////////
// Currently the handling of swath data fields with dimension maps is the same as
// other data fields(HDFEOS2Array_RealField.cc etc)
// The reason to keep it in separate is, in theory, that data fields with dimension map
// may need special handlings.
// So we will leave it this way for now. It may be removed in the future.
// HDFEOS2Array_RealField.cc may be used.
// KY 2014-02-19
#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 "HDFEOS2ArraySwathDimMapField.h"
#include "HDF4RequestHandler.h"
#define SIGNED_BYTE_TO_INT32 1
using namespace std;
bool
HDFEOS2ArraySwathDimMapField::read ()
{
BESDEBUG("h4","Coming to HDFEOS2ArraySwathDimMapField 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 = 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);
string datasetname;
if (swathname == "") {
throw InternalErr (__FILE__, __LINE__, "It should be either grid or swath.");
}
else if (gridname == "") {
openfunc = SWopen;
closefunc = SWclose;
attachfunc = SWattach;
detachfunc = SWdetach;
datasetname = swathname;
}
else {
throw InternalErr (__FILE__, __LINE__, "It should be either grid or swath.");
}
// Swath ID, swathid is actually in this case only the id of latitude and longitude.
int32 sfid = -1;
int32 swathid = -1;
if (true == isgeofile || false == check_pass_fileid_key) {
// Open, attach and obtain datatype information based on HDF-EOS2 APIs.
sfid = openfunc (const_cast < char *>(filename.c_str ()), DFACC_READ);
if (sfid < 0) {
ostringstream eherr;
eherr << "File " << filename.c_str () << " cannot be open.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
}
else
sfid = swfd;
swathid = attachfunc (sfid, const_cast < char *>(datasetname.c_str ()));
if (swathid < 0) {
close_fileid (sfid,-1);
ostringstream eherr;
eherr << "Grid/Swath " << datasetname.c_str () << " cannot be attached.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// dimmaps was set to be empty in hdfdesc.cc if the extra geolocation file also
// uses the dimension map
// This is because the dimmaps may be different in the MODIS geolocation file.
// So we cannot just pass
// the dimmaps to this class.
// Here we then obtain the dimension map info. in the geolocation file.
if(true == dimmaps.empty()) {
int32 nummaps = 0;
int32 bufsize = 0;
// Obtain number of dimension maps and the buffer size.
if ((nummaps = SWnentries(swathid, HDFE_NENTMAP, &bufsize)) == -1){
detachfunc(swathid);
close_fileid(sfid,-1);
throw InternalErr (__FILE__, __LINE__, "cannot obtain the number of dimmaps");
}
if (nummaps <= 0){
detachfunc(swathid);
close_fileid(sfid,-1);
throw InternalErr (__FILE__,__LINE__,
"Number of dimension maps should be greater than 0");
}
vector<char> namelist;
vector<int32> map_offset;
vector<int32> increment;
namelist.resize(bufsize + 1);
map_offset.resize(nummaps);
increment.resize(nummaps);
if (SWinqmaps(swathid, &namelist[0], &map_offset[0], &increment[0])
== -1) {
detachfunc(swathid);
close_fileid(sfid,-1);
throw InternalErr (__FILE__,__LINE__,"fail to inquiry dimension maps");
}
vector<string> mapnames;
HDFCFUtil::Split(&namelist[0], bufsize, ',', mapnames);
int map_count = 0;
for (vector<string>::const_iterator i = mapnames.begin();
i != mapnames.end(); ++i) {
vector<string> parts;
HDFCFUtil::Split(i->c_str(), '/', parts);
if (parts.size() != 2){
detachfunc(swathid);
close_fileid(sfid,-1);
throw InternalErr (__FILE__,__LINE__,"the dimmaps should only include two parts");
}
struct dimmap_entry tempdimmap;
tempdimmap.geodim = parts[0];
tempdimmap.datadim = parts[1];
tempdimmap.offset = map_offset[map_count];
tempdimmap.inc = increment[map_count];
//cerr<<"map_count is: "<<map_count <<endl;
//cerr<<"dimmap geodim is: "<<tempdimmap.geodim <<endl;
//cerr<<"dimmap datadim is: "<<tempdimmap.datadim <<endl;
//cerr<<"offset is: "<<tempdimmap.offset <<endl;
//cerr<<"inc is: "<<tempdimmap.inc <<endl;
dimmaps.push_back(tempdimmap);
++map_count;
}
}
#if 0
else {
for(int i = 0; i <dimmaps.size();i++) {
cerr<<"dimmap geodim is: "<<dimmaps[i].geodim <<endl;
cerr<<"dimmap datadim is: "<<dimmaps[i].datadim <<endl;
cerr<<"offset is: "<<dimmaps[i].offset <<endl;
cerr<<"inc is: "<<dimmaps[i].inc <<endl;
}
}
#endif
if (sotype!=DEFAULT_CF_EQU) {
if("MODIS_SWATH_Type_L1B" == swathname) {
string emissive_str = "Emissive";
string RefSB_str = "RefSB";
bool is_emissive_field = false;
bool is_refsb_field = false;
if(fieldname.find(emissive_str)!=string::npos) {
if(0 == fieldname.compare(fieldname.size()-emissive_str.size(),
emissive_str.size(),emissive_str))
is_emissive_field = true;
}
if(fieldname.find(RefSB_str)!=string::npos) {
if(0 == fieldname.compare(fieldname.size()-RefSB_str.size(),
RefSB_str.size(),RefSB_str))
is_refsb_field = true;
}
if ((true == is_emissive_field) || (true == is_refsb_field)) {
detachfunc(swathid);
close_fileid(sfid,-1);
throw InternalErr (__FILE__, __LINE__,
"Currently don't support MODIS Level 1B swath dim. map for data ");
}
}
}
bool is_modis1b = false;
if("MODIS_SWATH_Type_L1B" == swathname)
is_modis1b = true;
#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
try {
//if(true == turn_on_disable_scale_comp_key && false== is_modis1b)
if(true == HDF4RequestHandler::get_disable_scaleoffset_comp() && false== is_modis1b)
write_dap_data_disable_scale_comp(swathid,nelms,offset32,count32,step32);
else
write_dap_data_scale_comp(swathid,nelms,offset32,count32,step32);
}
catch(...) {
detachfunc(swathid);
close_fileid(sfid,-1);
throw;
}
intn r = 0;
r = detachfunc (swathid);
if (r != 0) {
close_fileid(sfid,-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 (sfid);
if (r != 0) {
ostringstream eherr;
eherr << "Grid/Swath " << filename.c_str () << " cannot be closed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
}
return false;
}
// Standard way of DAP handlers to pass the coordinates of the subsetted region to the handlers
// Return the number of elements to read.
int
HDFEOS2ArraySwathDimMapField::format_constraint (int *offset, int *step, int *count)
{
long 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 illegal 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 ("h4",
"=format_constraint():"
<< "id=" << id << " offset=" << offset[id]
<< " step=" << step[id]
<< " count=" << count[id]
<< endl);
id++;
p++;
}// while (p != dim_end ())
return nels;
}
// Get latitude and longitude fields.
// It will call expand_dimmap_field to interpolate latitude and longitude.
template < class T > int
HDFEOS2ArraySwathDimMapField::
GetFieldValue (int32 swathid, const string & geofieldname,
vector < struct dimmap_entry >&sw_dimmaps,
vector < T > &vals, vector<int32>&newdims)
{
int32 ret = -1;
int32 size = -1;
int32 sw_rank = -1;
int32 dims[130];
int32 type = -1;
// Two dimensions for lat/lon; each dimension name is < 64 characters,
// The dimension names are separated by a comma.
char dimlist[130];
ret = SWfieldinfo (swathid, const_cast < char *>(geofieldname.c_str ()),
&sw_rank, dims, &type, dimlist);
if (ret != 0)
return -1;
size = 1;
for (int i = 0; i <sw_rank; i++)
size *= dims[i];
vals.resize (size);
ret = SWreadfield (swathid, const_cast < char *>(geofieldname.c_str ()),
NULL, NULL, NULL, (void *) &vals[0]);
if (ret != 0)
return -1;
vector < string > dimname;
HDFCFUtil::Split (dimlist, ',', dimname);
for (int i = 0; i < sw_rank; i++) {
vector < struct dimmap_entry >::iterator it;
for (it = sw_dimmaps.begin (); it != sw_dimmaps.end (); it++) {
if (it->geodim == dimname[i]) {
//cerr<<"dimnames["<<i<<"]: " <<dimname[i]<<endl;
//cerr<<"offset is "<<it->offset<<endl;
//cerr<<"inc is "<<it->inc<<endl;
int32 ddimsize = SWdiminfo (swathid, (char *) it->datadim.c_str ());
if (ddimsize == -1)
return -1;
int r;
r = _expand_dimmap_field (&vals, sw_rank, dims, i, ddimsize, it->offset, it->inc);
if (r != 0)
return -1;
}
}
}
// dims[] are expanded already.
for (int i = 0; i < sw_rank; i++) {
//cerr<<"i "<< i << " "<< dims[i] <<endl;
if (dims[i] < 0)
return -1;
newdims[i] = dims[i];
}
return 0;
}
// expand the dimension map field.
template < class T > int
HDFEOS2ArraySwathDimMapField::_expand_dimmap_field (vector < T >
*pvals, int32 sw_rank,
int32 dimsa[],
int dimindex,
int32 ddimsize,
int32 offset,
int32 inc)
{
vector < T > orig = *pvals;
vector < int32 > pos;
vector < int32 > dims;
vector < int32 > newdims;
pos.resize (sw_rank);
dims.resize (sw_rank);
for (int i = 0; i < sw_rank; i++) {
pos[i] = 0;
dims[i] = dimsa[i];
}
newdims = dims;
newdims[dimindex] = ddimsize;
dimsa[dimindex] = ddimsize;
int newsize = 1;
for (int i = 0; i < sw_rank; i++) {
newsize *= newdims[i];
}
pvals->clear ();
pvals->resize (newsize);
for (;;) {
// if end
if (pos[0] == dims[0]) {
// we past then end
break;
}
else if (pos[dimindex] == 0) {
// extract 1D values
vector < T > v;
for (int i = 0; i < dims[dimindex]; i++) {
pos[dimindex] = i;
v.push_back (orig[INDEX_nD_TO_1D (dims, pos)]);
}
// expand them
vector < T > w;
for (int32 j = 0; j < ddimsize; j++) {
int32 i = (j - offset) / inc;
T f;
if (i * inc + offset == j) // perfect match
{
f = (v[i]);
}
else {
int32 i1 = 0;
int32 i2 = (i<=0)?1:0;
int32 j1 = 0;
int32 j2 = 0;
#if 0
if (i <= 0) {
//i1 = 0;
i2 = 1;
}
#endif
if ((unsigned int) i + 1 >= v.size ()) {
i1 = v.size () - 2;
i2 = v.size () - 1;
}
else {
i1 = i;
i2 = i + 1;
}
j1 = i1 * inc + offset;
j2 = i2 * inc + offset;
f = (((j - j1) * v[i2] + (j2 - j) * v[i1]) / (j2 - j1));
}
w.push_back (f);
pos[dimindex] = j;
(*pvals)[INDEX_nD_TO_1D (newdims, pos)] = f;
}
pos[dimindex] = 0;
}
// next pos
pos[sw_rank - 1]++;
for (int i = sw_rank - 1; i > 0; i--) {
if (pos[i] == dims[i]) {
pos[i] = 0;
pos[i - 1]++;
}
}
}
return 0;
}
template < class T >
bool HDFEOS2ArraySwathDimMapField::FieldSubset (T * outlatlon,
const vector<int32>&newdims,
T * latlon,
int32 * offset,
int32 * count,
int32 * step)
{
if (newdims.size() == 1)
Field1DSubset(outlatlon,newdims[0],latlon,offset,count,step);
else if (newdims.size() == 2)
Field2DSubset(outlatlon,newdims[0],newdims[1],latlon,offset,count,step);
else if (newdims.size() == 3)
Field3DSubset(outlatlon,newdims,latlon,offset,count,step);
else
throw InternalErr(__FILE__, __LINE__,
"Currently doesn't support rank >3 when interpolating with dimension map");
return true;
}
// Subset of 1-D field to follow the parameters from the DAP expression constraint
template < class T >
bool HDFEOS2ArraySwathDimMapField::Field1DSubset (T * outlatlon,
const int majordim,
T * latlon,
int32 * offset,
int32 * count,
int32 * step)
{
if (majordim < count[0])
throw InternalErr(__FILE__, __LINE__,
"The number of elements is greater than the total dimensional size");
for (int i = 0; i < count[0]; i++)
outlatlon[i] = latlon[offset[0]+i*step[0]];
return true;
}
// Subset of latitude and longitude to follow the parameters
// from the DAP expression constraint
template < class T >
bool HDFEOS2ArraySwathDimMapField::Field2DSubset (T * outlatlon,
const int /*majordim //unused SBL 2/7/20 */,
const int minordim,
T * latlon,
int32 * offset,
int32 * count,
int32 * step)
{
#if 0
T (*templatlonptr)[majordim][minordim] = (T *[][]) latlon;
#endif
int i = 0;
int j = 0;
// do subsetting
// Find the correct index
int dim0count = count[0];
int dim1count = count[1];
int dim0index[dim0count];
int dim1index[dim1count];
for (i = 0; i < count[0]; i++) // count[0] is the least changing dimension
dim0index[i] = offset[0] + i * step[0];
for (j = 0; j < count[1]; j++)
dim1index[j] = offset[1] + j * step[1];
// Now assign the subsetting data
int k = 0;
for (i = 0; i < count[0]; i++) {
for (j = 0; j < count[1]; j++) {
#if 0
outlatlon[k] = (*templatlonptr)[dim0index[i]][dim1index[j]];
#endif
outlatlon[k] = *(latlon + (dim0index[i] * minordim) + dim1index[j]);
k++;
}
}
return true;
}
// Subsetting the field to follow the parameters from the DAP expression constraint
template < class T >
bool HDFEOS2ArraySwathDimMapField::Field3DSubset (T * outlatlon,
const vector<int32>& newdims,
T * latlon,
int32 * offset,
int32 * count,
int32 * step)
{
if (newdims.size() !=3)
throw InternalErr(__FILE__, __LINE__,
"the rank must be 3 to call this function");
#if 0
T (*templatlonptr)[newdims[0]][newdims[1]][newdims[2]] = (T *[][][]) latlon;
#endif
int i = 0;
int j = 0;
int k = 0;
// do subsetting
// Find the correct index
int dim0count = count[0];
int dim1count = count[1];
int dim2count = count[2];
int dim0index[dim0count], dim1index[dim1count],dim2index[dim2count];
for (i = 0; i < count[0]; i++) // count[0] is the least changing dimension
dim0index[i] = offset[0] + i * step[0];
for (j = 0; j < count[1]; j++)
dim1index[j] = offset[1] + j * step[1];
for (k = 0; k < count[2]; k++)
dim2index[k] = offset[2] + k * step[2];
// Now assign the subsetting data
int l = 0;
for (i = 0; i < count[0]; i++) {
for (j = 0; j < count[1]; j++) {
for (k =0; k < count[2]; k++) {
#if 0
outlatlon[l] = (*templatlonptr)[dim0index[i]][dim1index[j]][dim2index[k]];
#endif
outlatlon[l] = *(latlon + (dim0index[i] * newdims[1] * newdims[2]) + (dim1index[j] * newdims[2])+ dim2index[k]);
l++;
}
}
}
return true;
}
int
HDFEOS2ArraySwathDimMapField::write_dap_data_scale_comp(int32 swathid,
int nelms,
vector<int32>& offset32,
vector<int32>& count32,
vector<int32>& step32) {
#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 *);
fieldinfofunc = SWfieldinfo;
int32 attrtype = -1;
int32 attrcount = -1;
int32 attrindex = -1;
int32 scale_factor_attr_index = -1;
int32 add_offset_attr_index =-1;
float scale=1;
float offset2=0;
float fillvalue = 0.;
if (sotype!=DEFAULT_CF_EQU) {
// 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) {
ostringstream eherr;
eherr << "Cannot Start the SD interface for the file " << filename <<endl;
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
}
else
sdfileid = sdfd;
int32 sdsindex = -1;
int32 sdsid = -1;
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 ());
}
char attrname[H4_MAX_NC_NAME + 1];
vector<char> attrbuf;
vector<char> attrbuf2;
scale_factor_attr_index = SDfindattr(sdsid, "scale_factor");
if(scale_factor_attr_index!=FAIL)
{
intn ret = 0;
ret = SDattrinfo(sdsid, scale_factor_attr_index, attrname, &attrtype, &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(attrtype)*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 ());
}
// Appears that the assumption for the datatype of scale_factor
// is either float or double
// for this type of MODIS files. So far we haven't found any problems.
// Maybe this is okay.
// KY 2013-12-19
switch(attrtype)
{
#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(FLOAT32, float);
GET_SCALE_FACTOR_ATTR_VALUE(FLOAT64, double);
default:
throw InternalErr(__FILE__,__LINE__,"unsupported data type.");
}
#undef GET_SCALE_FACTOR_ATTR_VALUE
}
add_offset_attr_index = SDfindattr(sdsid, "add_offset");
if(add_offset_attr_index!=FAIL)
{
intn ret = 0;
ret = SDattrinfo(sdsid, add_offset_attr_index, attrname, &attrtype, &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(attrtype)*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(attrtype)
{
#define GET_ADD_OFFSET_ATTR_VALUE(TYPE, CAST) \
case DFNT_##TYPE: \
{ \
CAST tmpvalue = *(CAST*)&attrbuf[0]; \
offset2 = (float)tmpvalue; \
} \
break;
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
}
attrindex = SDfindattr(sdsid, "_FillValue");
if(sotype!=DEFAULT_CF_EQU && attrindex!=FAIL)
{
intn ret = 0;
ret = SDattrinfo(sdsid, attrindex, attrname, &attrtype, &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(attrtype)*attrcount);
ret = SDreadattr(sdsid, 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(attrtype)
{
#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(INT16, int16);
GET_FILLVALUE_ATTR_VALUE(INT32, int32);
GET_FILLVALUE_ATTR_VALUE(UINT8, uint8);
GET_FILLVALUE_ATTR_VALUE(UINT16, uint16);
GET_FILLVALUE_ATTR_VALUE(UINT32, uint32);
// Float and double are not considered. Handle them later.
default:
;
// throw InternalErr(__FILE__,__LINE__,"unsupported data type.");
}
#undef GET_FILLVALUE_ATTR_VALUE
}
#if 0
// There is a controversy if we need to apply the valid_range to the data, for the time being comment this out.
// KY 2013-12-19
float orig_valid_min = 0.;
float orig_valid_max = 0.;
// 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.
attrindex = SDfindattr(sdsid, "valid_range");
if(attrindex!=FAIL)
{
intn ret;
ret = SDattrinfo(sdsid, attrindex, attrname, &attrtype, &attrcount);
if (ret==FAIL)
{
detachfunc(gridid);
closefunc(gfid);
SDendaccess(sdsid);
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(attrtype)*attrcount);
ret = SDreadattr(sdsid, attrindex, (VOIDP)&attrbuf[0]);
if (ret==FAIL)
{
detachfunc(gridid);
closefunc(gfid);
SDendaccess(sdsid);
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(attrtype) {
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)
throw InternalErr(__FILE__,__LINE__,"should find the separator ,");
if (found != found_from_end)
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());
}
break;
case DFNT_INT8:
{
if (2 == temp_attrcount) {
orig_valid_min = (float)attrbuf[0];
orig_valid_max = (float)attrbuf[1];
}
else
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)
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)
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)
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)
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]);
}
break;
case DFNT_UINT32:
{
if (temp_attrcount != 2)
throw InternalErr(__FILE__,__LINE__,"The number of attribute count should be 2 for the DFNT_UINT32 type.");
unsigned int* temp_valid_range = (unsigned int *)&attrbuf[0];
orig_valid_min = (float)(temp_valid_range[0]);
orig_valid_max = (float)(temp_valid_range[1]);
}
break;
case DFNT_FLOAT32:
{
if (temp_attrcount != 2)
throw InternalErr(__FILE__,__LINE__,"The number of attribute count should be 2 for the DFNT_FLOAT32 type.");
float* temp_valid_range = (float *)&attrbuf[0];
orig_valid_min = temp_valid_range[0];
orig_valid_max = temp_valid_range[1];
}
break;