-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHDFEOS2.cc
More file actions
4524 lines (3775 loc) · 186 KB
/
HDFEOS2.cc
File metadata and controls
4524 lines (3775 loc) · 186 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.
//
// Authors: MuQun Yang <myang6@hdfgroup.org> Choonghwan Lee
// Copyright (c) 2009 The HDF Group
/////////////////////////////////////////////////////////////////////////////
#ifdef USE_HDFEOS2_LIB
//#include <BESDEBUG.h> // Should provide BESDebug info. later
#include <sstream>
#include <algorithm>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include <cfloat>
#include <math.h>
#include <sys/stat.h>
#include "HDFCFUtil.h"
#include "HDFEOS2.h"
#include "HDF4RequestHandler.h"
// Names to be searched by
// get_geodim_x_name()
// get_geodim_y_name()
// get_latfield_name()
// get_lonfield_name()
// get_geogrid_name()
// Possible XDim names
const char *HDFEOS2::File::_geodim_x_names[] = {"XDim", "LonDim","nlon"};
// Possible YDim names.
const char *HDFEOS2::File::_geodim_y_names[] = {"YDim", "LatDim","nlat"};
// Possible latitude names.
const char *HDFEOS2::File::_latfield_names[] = {
"Latitude", "Lat","YDim", "LatCenter"
};
// Possible longitude names.
const char *HDFEOS2::File::_lonfield_names[] = {
"Longitude", "Lon","XDim", "LonCenter"
};
// For some grid products, latitude and longitude are put under a special geogrid.
// One possible name is "location".
const char *HDFEOS2::File::_geogrid_names[] = {"location"};
using namespace HDFEOS2;
using namespace std;
// The following is for generating exception messages.
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)
{
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 used in destructors.
struct delete_elem
{
template<typename T> void operator()(T *ptr)
{
delete ptr;
}
};
// Destructor for class File.
File::~File()
{
if(gridfd !=-1) {
for (vector<GridDataset *>::const_iterator i = grids.begin();
i != grids.end(); ++i){
delete *i;
}
// Grid file IDs will be closed in HDF4RequestHandler.cc.
}
if(swathfd !=-1) {
for (vector<SwathDataset *>::const_iterator i = swaths.begin();
i != swaths.end(); ++i){
delete *i;
}
}
for (vector<PointDataset *>::const_iterator i = points.begin();
i != points.end(); ++i){
delete *i;
}
}
/// Read all the information in this file from the EOS2 APIs.
File * File::Read(const char *path, int32 mygridfd, int32 myswathfd) throw(Exception)
{
File *file = new File(path);
if(file == NULL)
throw1("Memory allocation for file class failed. ");
file->gridfd = mygridfd;
file->swathfd = myswathfd;
#if 0
// Read information of all Grid objects in this file.
if ((file->gridfd = GDopen(const_cast<char *>(file->path.c_str()),
DFACC_READ)) == -1) {
delete file;
throw2("grid open", path);
}
#endif
vector<string> gridlist;
if (!Utility::ReadNamelist(file->path.c_str(), GDinqgrid, gridlist)) {
delete file;
throw1("Grid ReadNamelist failed.");
}
try {
for (vector<string>::const_iterator i = gridlist.begin();
i != gridlist.end(); ++i)
file->grids.push_back(GridDataset::Read(file->gridfd, *i));
}
catch(...) {
delete file;
throw1("GridDataset Read failed");
}
#if 0
// Read information of all Swath objects in this file
if ((file->swathfd = SWopen(const_cast<char *>(file->path.c_str()),
DFACC_READ)) == -1){
delete file;
throw2("swath open", path);
}
#endif
vector<string> swathlist;
if (!Utility::ReadNamelist(file->path.c_str(), SWinqswath, swathlist)){
delete file;
throw1("Swath ReadNamelist failed.");
}
try {
for (vector<string>::const_iterator i = swathlist.begin();
i != swathlist.end(); ++i)
file->swaths.push_back(SwathDataset::Read(file->swathfd, *i));
}
catch(...) {
delete file;
throw1("SwathDataset Read failed.");
}
// We only obtain the name list of point objects but not don't provide
// other information of these objects.
// The client will only get the name list of point objects.
vector<string> pointlist;
if (!Utility::ReadNamelist(file->path.c_str(), PTinqpoint, pointlist)){
delete file;
throw1("Point ReadNamelist failed.");
}
//See if I can make coverity happy because it doesn't understand throw macro.
for (vector<string>::const_iterator i = pointlist.begin();
i != pointlist.end(); ++i)
file->points.push_back(PointDataset::Read(-1, *i));
// If this is not an HDF-EOS2 file, returns exception as false.
if(file->grids.size() == 0 && file->swaths.size() == 0
&& file->points.size() == 0) {
Exception e("Not an HDF-EOS2 file");
e.setFileType(false);
delete file;
throw e;
}
return file;
}
// A grid's X-dimension can have different names: XDim, LatDim, etc.
// This function returns the name of X-dimension which is used in the given file.
// For better performance, we check the first grid or swath only.
string File::get_geodim_x_name()
{
if(!_geodim_x_name.empty())
return _geodim_x_name;
_find_geodim_names();
return _geodim_x_name;
}
// A grid's Y-dimension can have different names: YDim, LonDim, etc.
// This function returns the name of Y-dimension which is used in the given file.
// For better performance, we check the first grid or swath only.
string File::get_geodim_y_name()
{
if(!_geodim_y_name.empty())
return _geodim_y_name;
_find_geodim_names();
return _geodim_y_name;
}
// In some cases, values of latitude and longitude are stored in data fields.
// Since the latitude field and longitude field do not have unique names
// (e.g., latitude field can be "latitude", "Lat", ...),
// we need to retrieve the field name.
// The following two functions does this job.
// For better performance, we check the first grid or swath only.
string File::get_latfield_name()
{
if(!_latfield_name.empty())
return _latfield_name;
_find_latlonfield_names();
return _latfield_name;
}
string File::get_lonfield_name()
{
if(!_lonfield_name.empty())
return _lonfield_name;
_find_latlonfield_names();
return _lonfield_name;
}
// In some cases, a dedicated grid is used to store the values of
// latitude and longitude. The following function finds the name
// of the geo grid.
string File::get_geogrid_name()
{
if(!_geogrid_name.empty())
return _geogrid_name;
_find_geogrid_name();
return _geogrid_name;
}
// Internal function used by
// get_geodim_x_name and get_geodim_y_name functions.
// This function is not intended to be used outside the
// get_geodim_x_name and get_geodim_y_name functions.
void File::_find_geodim_names()
{
set<string> geodim_x_name_set;
for(size_t i = 0; i<sizeof(_geodim_x_names) / sizeof(const char *); i++)
geodim_x_name_set.insert(_geodim_x_names[i]);
set<string> geodim_y_name_set;
for(size_t i = 0; i<sizeof(_geodim_y_names) / sizeof(const char *); i++)
geodim_y_name_set.insert(_geodim_y_names[i]);
// The following code is only used for grid. It also causes the coverity unhappy
// for the code block for(size_t i=0; ;i++), so simplify it after this code block.
#if 0
const size_t gs = grids.size();
const size_t ss = swaths.size();
for(size_t i=0; ;i++)
{
Dataset *dataset=NULL;
if(i<gs)
dataset = static_cast<Dataset*>(grids[i]);
else if(i < gs + ss)
dataset = static_cast<Dataset*>(swaths[i-gs]);
else
break;
const vector<Dimension *>& dims = dataset->getDimensions();
for(vector<Dimension*>::const_iterator it = dims.begin();
it != dims.end(); ++it)
{
if(geodim_x_name_set.find((*it)->getName()) != geodim_x_name_set.end())
_geodim_x_name = (*it)->getName();
else if(geodim_y_name_set.find((*it)->getName()) != geodim_y_name_set.end())
_geodim_y_name = (*it)->getName();
}
// For performance, we're checking this for the first grid or swath
break;
}
#endif
const size_t gs = grids.size();
// For performance, we're checking this for the first grid
if(gs >0)
{
Dataset *dataset=NULL;
dataset = static_cast<Dataset*>(grids[0]);
const vector<Dimension *>& dims = dataset->getDimensions();
for(vector<Dimension*>::const_iterator it = dims.begin();
it != dims.end(); ++it)
{
// Essentially this code will grab any dimension names that is
// NOT predefined "XDim","LonDim","nlon" for geodim_x_name;
// any dimension names that is NOT predefined "YDim","LatDim","nlat"
// for geodim_y_name. This is in theory not right. Given the
// fact that this works with the current HDF-EOS2 products and there
// will be no more HDF-EOS2 products. We will leave the code this way.
if(geodim_x_name_set.find((*it)->getName()) != geodim_x_name_set.end())
_geodim_x_name = (*it)->getName();
else if(geodim_y_name_set.find((*it)->getName()) != geodim_y_name_set.end())
_geodim_y_name = (*it)->getName();
}
}
if(_geodim_x_name.empty())
_geodim_x_name = _geodim_x_names[0];
if(_geodim_y_name.empty())
_geodim_y_name = _geodim_y_names[0];
}
// Internal function used by
// get_latfield_name and get_lonfield_name functions.
// This function is not intended to be used outside
// the get_latfield_name and get_lonfield_name functions.
void File::_find_latlonfield_names()
{
set<string> latfield_name_set;
for(size_t i = 0; i<sizeof(_latfield_names) / sizeof(const char *); i++)
latfield_name_set.insert(_latfield_names[i]);
set<string> lonfield_name_set;
for(size_t i = 0; i<sizeof(_lonfield_names) / sizeof(const char *); i++)
lonfield_name_set.insert(_lonfield_names[i]);
const size_t gs = grids.size();
const size_t ss = swaths.size();
// KY: converity structurally dead code i++ is never reached
// i++ is unreachable,so comment out this one
//for(size_t i=0; ;i++)
for(size_t i=0;i<1 ;i++)
{
Dataset *dataset = NULL;
SwathDataset *sw = NULL;
if(i<gs)
dataset = static_cast<Dataset*>(grids[i]);
else if(i < gs + ss)
{
sw = swaths[i-gs];
dataset = static_cast<Dataset*>(sw);
}
else
break;
const vector<Field *>& fields = dataset->getDataFields();
for(vector<Field*>::const_iterator it = fields.begin();
it != fields.end(); ++it)
{
if(latfield_name_set.find((*it)->getName()) != latfield_name_set.end())
_latfield_name = (*it)->getName();
else if(lonfield_name_set.find((*it)->getName()) != lonfield_name_set.end())
_lonfield_name = (*it)->getName();
}
if(sw)
{
const vector<Field *>& geofields = dataset->getDataFields();
for(vector<Field*>::const_iterator it = geofields.begin();
it != geofields.end(); ++it)
{
if(latfield_name_set.find((*it)->getName()) != latfield_name_set.end())
_latfield_name = (*it)->getName();
else if(lonfield_name_set.find((*it)->getName()) != lonfield_name_set.end())
_lonfield_name = (*it)->getName();
}
}
// For performance, we're checking this for the first grid or swath
// comment out to fix coverity issues
//break;
}
if(_latfield_name.empty())
_latfield_name = _latfield_names[0];
if(_lonfield_name.empty())
_lonfield_name = _lonfield_names[0];
}
// Internal function used by
// the get_geogrid_name function.
// This function is not intended to be used outside the get_geogrid_name function.
void File::_find_geogrid_name()
{
set<string> geogrid_name_set;
for(size_t i = 0; i<sizeof(_geogrid_names) / sizeof(const char *); i++)
geogrid_name_set.insert(_geogrid_names[i]);
const size_t gs = grids.size();
const size_t ss = swaths.size();
for(size_t i=0; ;i++)
{
Dataset *dataset;
if(i<gs)
dataset = static_cast<Dataset*>(grids[i]);
else if(i < gs + ss)
dataset = static_cast<Dataset*>(swaths[i-gs]);
else
break;
if(geogrid_name_set.find(dataset->getName()) != geogrid_name_set.end())
_geogrid_name = dataset->getName();
}
if(_geogrid_name.empty())
_geogrid_name = "location";
}
// Check if we have the dedicated lat/lon grid.
void File::check_onelatlon_grids() {
// 0. obtain "Latitude","Longitude" and "location" set.
string LATFIELDNAME = this->get_latfield_name();
string LONFIELDNAME = this->get_lonfield_name();
// Now only there is only one geo grid name "location", so don't call it know.
string GEOGRIDNAME = "location";
//only one lat/lon and it is under GEOGRIDNAME
int onellcount = 0;
// Check if lat/lon is found under other grids.
int morellcount = 0;
// Loop through all grids
for(vector<GridDataset *>::const_iterator i = this->grids.begin();
i != this->grids.end(); ++i){
// Loop through all fields
for(vector<Field *>::const_iterator j =
(*i)->getDataFields().begin();
j != (*i)->getDataFields().end(); ++j) {
if((*i)->getName()==GEOGRIDNAME){
if((*j)->getName()==LATFIELDNAME){
onellcount++;
(*i)->latfield = *j;
}
if((*j)->getName()==LONFIELDNAME){
onellcount++;
(*i)->lonfield = *j;
}
if(onellcount == 2)
break;//Finish this grid
}
else {// Here we assume that lat and lon are always in pairs.
if(((*j)->getName()==LATFIELDNAME)||((*j)->getName()==LONFIELDNAME)){
(*i)->ownllflag = true;
morellcount++;
break;
}
}
}
}
if(morellcount ==0 && onellcount ==2)
this->onelatlon = true;
}
// For one grid, need to handle the third-dimension(both existing and missing) coordinate variables
void File::handle_one_grid_zdim(GridDataset* gdset) {
// Obtain "XDim","YDim"
string DIMXNAME = this->get_geodim_x_name();
string DIMYNAME = this->get_geodim_y_name();
bool missingfield_unlim_flag = false;
Field *missingfield_unlim = NULL;
// This is a big assumption, it may be wrong since not every 1-D field
// with the third dimension(name and size) is a coordinate
// variable. We have to watch the products we've supported. KY 2012-6-13
// Unique 1-D field's dimension name list.
set<string> tempdimlist;
pair<set<string>::iterator,bool> tempdimret;
for (vector<Field *>::const_iterator j =
gdset->getDataFields().begin();
j != gdset->getDataFields().end(); ++j) {
//We only need to search those 1-D fields
if ((*j)->getRank()==1){
// DIMXNAME and DIMYNAME correspond to latitude and longitude.
// They should NOT be treated as dimension names missing fields. It will be handled differently.
if(((*j)->getDimensions())[0]->getName()!=DIMXNAME && ((*j)->getDimensions())[0]->getName()!=DIMYNAME){
tempdimret = tempdimlist.insert(((*j)->getDimensions())[0]->getName());
// Kent: The following implementation may not be always right. This essentially is the flaw of the
// data product if a file encounters this case. Only unique 1-D third-dimension field should be provided.
// Only pick up the first 1-D field that the third-dimension
if(tempdimret.second == true) {
HDFCFUtil::insert_map(gdset->dimcvarlist, ((*j)->getDimensions())[0]->getName(),
(*j)->getName());
(*j)->fieldtype = 3;
if((*j)->getName() == "Time")
(*j)->fieldtype = 5;// IDV can handle 4-D fields when the 4th dim is Time.
}
}
}
}
// Add the missing Z-dimension field.
// Some dimension name's corresponding fields are missing,
// so add the missing Z-dimension fields based on the dimension names. When the real
// data is read, nature number 1,2,3,.... will be filled!
// NOTE: The latitude and longitude dim names are not handled yet.
// The above handling is also based on a big assumption. This is the best the
// handler can do without having the extra information outside the HDF-EOS2 file. KY 2012-6-12
// Loop through all dimensions of this grid.
for (vector<Dimension *>::const_iterator j =
gdset->getDimensions().begin(); j!= gdset->getDimensions().end();++j){
// Don't handle DIMXNAME and DIMYNAME yet.
if((*j)->getName()!=DIMXNAME && (*j)->getName()!=DIMYNAME){
// This dimension needs a field
if((tempdimlist.find((*j)->getName())) == tempdimlist.end()){
// Need to create a new data field vector element with the name and dimension as above.
Field *missingfield = new Field();
missingfield->name = (*j)->getName();
missingfield->rank = 1;
//This is an HDF constant.the data type is always integer.
missingfield->type = DFNT_INT32;
// Dimension of the missing field
Dimension *dim = new Dimension((*j)->getName(),(*j)->getSize());
// only 1 dimension
missingfield->dims.push_back(dim);
// Provide information for the missing data, since we need to calculate the data, so
// the information is different than a normal field.
//int missingdatarank =1;
//int missingdatatypesize = 4;
//int missingdimsize[1]; //unused variable. SBL 2/7/20
//missingdimsize[0]= (*j)->getSize(); //no purpose
if(0 == (*j)->getSize()) {
missingfield_unlim_flag = true;
missingfield_unlim = missingfield;
}
// added Z-dimension coordinate variable with nature number
missingfield->fieldtype = 4;
// input data is empty now. We need to review this approach in the future.
// The data will be retrieved in HDFEOS2ArrayMissGeoField.cc. KY 2013-06-14
#if 0
// LightVector<char>inputdata;
// missingfield->data = NULL;
//missingfield->data = new MissingFieldData(missingdatarank,missingdatatypesize,missingdimsize,inputdata);
// The data will be handled separately, we don't need to provide data.
#endif
gdset->datafields.push_back(missingfield);
HDFCFUtil::insert_map(gdset->dimcvarlist, (missingfield->getDimensions())[0]->getName(),
missingfield->name);
}
}
}
//Correct the unlimited dimension size.
bool temp_missingfield_unlim_flag = missingfield_unlim_flag;
if(true == temp_missingfield_unlim_flag) {
for (vector<Field *>::const_iterator i =
gdset->getDataFields().begin();
i != gdset->getDataFields().end(); ++i) {
for (vector<Dimension *>::const_iterator j =
gdset->getDimensions().begin(); j!= gdset->getDimensions().end();++j){
if((*j)->getName() == (missingfield_unlim->getDimensions())[0]->getName()) {
if((*j)->getSize()!= 0) {
Dimension *dim = missingfield_unlim->getDimensions()[0];
// The unlimited dimension size is updated.
dim->dimsize = (*j)->getSize();
missingfield_unlim_flag = false;
break;
}
}
}
if(false == missingfield_unlim_flag)
break;
}
}
}
// For one grid, need to handle lat/lon(both existing lat/lon and calculated lat/lon from EOS2 APIs)
void File::handle_one_grid_latlon(GridDataset* gdset) throw(Exception)
{
// Obtain "XDim","YDim","Latitude","Longitude"
string DIMXNAME = this->get_geodim_x_name();
string DIMYNAME = this->get_geodim_y_name();
string LATFIELDNAME = this->get_latfield_name();
string LONFIELDNAME = this->get_lonfield_name();
// This grid has its own latitude/longitude
if(gdset->ownllflag) {
// Searching the lat/lon field from the grid.
for (vector<Field *>::const_iterator j =
gdset->getDataFields().begin();
j != gdset->getDataFields().end(); ++j) {
if((*j)->getName() == LATFIELDNAME) {
// set the flag to tell if this is lat or lon.
// The unit will be different for lat and lon.
(*j)->fieldtype = 1;
// Latitude rank should not be greater than 2.
// Here I don't check if the rank of latitude is the same as the longitude.
// Hopefully it never happens for HDF-EOS2 cases.
// We are still investigating if Java clients work
// when the rank of latitude and longitude is greater than 2.
if((*j)->getRank() > 2)
throw3("The rank of latitude is greater than 2",
gdset->getName(),(*j)->getName());
if((*j)->getRank() != 1) {
// Obtain the major dim. For most cases, it is YDim Major.
// But still need to watch out the rare cases.
(*j)->ydimmajor = gdset->getCalculated().isYDimMajor();
// If the 2-D lat/lon can be condensed to 1-D.
// For current HDF-EOS2 files, only GEO and CEA can be condensed.
// To gain performance,
// we don't check the real latitude values.
int32 projectioncode = gdset->getProjection().getCode();
if(projectioncode == GCTP_GEO || projectioncode ==GCTP_CEA) {
(*j)->condenseddim = true;
}
// Now we want to handle the dim and the var lists.
// If the lat/lon can be condensed to 1-D array,
// COARD convention needs to be followed.
// Since COARD requires that the dimension name of lat/lon is the same as the field name of lat/lon,
// we still need to handle this case in the later step(in function handle_grid_coards).
// If the 2-D array can be condensed to a 1-D array.
if((*j)->condenseddim) {
// Regardless of dimension major, always lat->YDim, lon->XDim;
// We don't need to adjust the dimension rank.
for (vector<Dimension *>::const_iterator k =
(*j)->getDimensions().begin(); k!= (*j)->getDimensions().end();++k){
if((*k)->getName() == DIMYNAME) {
HDFCFUtil::insert_map(gdset->dimcvarlist, (*k)->getName(), (*j)->getName());
}
}
}
// 2-D lat/lon case. Since dimension order doesn't matter, so we always assume lon->XDim, lat->YDim.
else {
for (vector<Dimension *>::const_iterator k =
(*j)->getDimensions().begin(); k!= (*j)->getDimensions().end();++k){
if((*k)->getName() == DIMYNAME) {
HDFCFUtil::insert_map(gdset->dimcvarlist, (*k)->getName(), (*j)->getName());
}
}
}
}
// This is the 1-D case, just inserting the dimension, field pair.
else {
HDFCFUtil::insert_map(gdset->dimcvarlist, (((*j)->getDimensions())[0])->getName(),
(*j)->getName());
}
}
else if ((*j)->getName() == LONFIELDNAME) {
// set the flag to tell if this is lat or lon. The unit will be different for lat and lon.
(*j)->fieldtype = 2;
// longitude rank should not be greater than 2.
// Here I don't check if the rank of latitude and longitude is the same.
// Hopefully it never happens for HDF-EOS2 cases.
// We are still investigating if Java clients work when the rank of latitude and longitude is greater than 2.
if((*j)->getRank() >2)
throw3("The rank of Longitude is greater than 2",gdset->getName(),(*j)->getName());
if((*j)->getRank() != 1) {
// Obtain the major dim. For most cases, it is YDim Major. But still need to check for rare cases.
(*j)->ydimmajor = gdset->getCalculated().isYDimMajor();
// If the 2-D lat/lon can be condensed to 1-D.
//For current HDF-EOS2 files, only GEO and CEA can be condensed. To gain performance,
// we don't check with real values.
int32 projectioncode = gdset->getProjection().getCode();
if(projectioncode == GCTP_GEO || projectioncode ==GCTP_CEA) {
(*j)->condenseddim = true;
}
// Now we want to handle the dim and the var lists.
// If the lat/lon can be condensed to 1-D array, COARD convention needs to be followed.
// Since COARD requires that the dimension name of lat/lon is the same as the field name of lat/lon,
// we still need to handle this case at last.
// Can be condensed to 1-D array.
if((*j)->condenseddim) {
// Regardless of dimension major, the EOS convention is always lat->YDim, lon->XDim;
// We don't need to adjust the dimension rank.
for (vector<Dimension *>::const_iterator k =
(*j)->getDimensions().begin(); k!= (*j)->getDimensions().end();++k){
if((*k)->getName() == DIMXNAME) {
HDFCFUtil::insert_map(gdset->dimcvarlist, (*k)->getName(), (*j)->getName());
}
}
}
// 2-D lat/lon case. Since dimension order doesn't matter, so we always assume lon->XDim, lat->YDim.
else {
for (vector<Dimension *>::const_iterator k =
(*j)->getDimensions().begin(); k!= (*j)->getDimensions().end();++k){
if((*k)->getName() == DIMXNAME) {
HDFCFUtil::insert_map(gdset->dimcvarlist, (*k)->getName(), (*j)->getName());
}
}
}
}
// This is the 1-D case, just inserting the dimension, field pair.
else {
HDFCFUtil::insert_map(gdset->dimcvarlist, (((*j)->getDimensions())[0])->getName(),
(*j)->getName());
}
}
}
}
else { // this grid's lat/lon has to be calculated.
// Latitude and Longitude
Field *latfield = new Field();
Field *lonfield = new Field();
latfield->name = LATFIELDNAME;
lonfield->name = LONFIELDNAME;
// lat/lon is a 2-D array
latfield->rank = 2;
lonfield->rank = 2;
// The data type is always float64. DFNT_FLOAT64 is the equivalent float64 type.
latfield->type = DFNT_FLOAT64;
lonfield->type = DFNT_FLOAT64;
// Latitude's fieldtype is 1
latfield->fieldtype = 1;
// Longitude's fieldtype is 2
lonfield->fieldtype = 2;
// Check if YDim is the major order.
// Obtain the major dim. For most cases, it is YDim Major. But some cases may be not. Still need to check.
latfield->ydimmajor = gdset->getCalculated().isYDimMajor();
lonfield->ydimmajor = latfield->ydimmajor;
// Obtain XDim and YDim size.
int xdimsize = gdset->getInfo().getX();
int ydimsize = gdset->getInfo().getY();
// Add dimensions. If it is YDim major,the dimension name list is "YDim XDim", otherwise, it is "XDim YDim".
// For LAMAZ projection, Y dimension is always supposed to be major for calculating lat/lon,
// but for dimension order, it should be consistent with data fields. (LD -2012/01/16
bool dmajor=(gdset->getProjection().getCode()==GCTP_LAMAZ)? gdset->getCalculated().DetectFieldMajorDimension()
: latfield->ydimmajor;
if(dmajor) {
Dimension *dimlaty = new Dimension(DIMYNAME,ydimsize);
latfield->dims.push_back(dimlaty);
Dimension *dimlony = new Dimension(DIMYNAME,ydimsize);
lonfield->dims.push_back(dimlony);
Dimension *dimlatx = new Dimension(DIMXNAME,xdimsize);
latfield->dims.push_back(dimlatx);
Dimension *dimlonx = new Dimension(DIMXNAME,xdimsize);
lonfield->dims.push_back(dimlonx);
}
else {
Dimension *dimlatx = new Dimension(DIMXNAME,xdimsize);
latfield->dims.push_back(dimlatx);
Dimension *dimlonx = new Dimension(DIMXNAME,xdimsize);
lonfield->dims.push_back(dimlonx);
Dimension *dimlaty = new Dimension(DIMYNAME,ydimsize);
latfield->dims.push_back(dimlaty);
Dimension *dimlony = new Dimension(DIMYNAME,ydimsize);
lonfield->dims.push_back(dimlony);
}
// Obtain info upleft and lower right for special longitude.
float64* upleft;
float64* lowright;
upleft = const_cast<float64 *>(gdset->getInfo().getUpLeft());
lowright = const_cast<float64 *>(gdset->getInfo().getLowRight());
// SOme special longitude is from 0 to 360.We need to check this case.
int32 projectioncode = gdset->getProjection().getCode();
if(((int)lowright[0]>180000000) && ((int)upleft[0]>-1)) {
// We can only handle geographic projection now.
// This is the only case we can handle.
if(projectioncode == GCTP_GEO) {// Will handle when data is read.
lonfield->speciallon = true;
// When HDF-EOS2 cache is involved, we have to also set the
// speciallon flag for the latfield since the cache file
// includes both lat and lon fields, and even the request
// is only to generate the lat field, the lon field also needs to
// be updated to write the proper cache. KY 2016-03-16
if(HDF4RequestHandler::get_enable_eosgeo_cachefile() == true)
latfield->speciallon = true;
}
}
// Some MODIS MCD files don't follow standard format for lat/lon (DDDMMMSSS);
// they simply represent lat/lon as -180.0000000 or -90.000000.
// HDF-EOS2 library won't give the correct value based on these value.
// These need to be remembered and resumed to the correct format when retrieving the data.
// Since so far we haven't found region of satellite files is within 0.1666 degree(1 minute)
// so, we divide the corner coordinate by 1000 and see if the integral part is 0.
// If it is 0, we know this file uses special lat/lon coordinate.
if(((int)(lowright[0]/1000)==0) &&((int)(upleft[0]/1000)==0)
&& ((int)(upleft[1]/1000)==0) && ((int)(lowright[1]/1000)==0)) {
if(projectioncode == GCTP_GEO){
lonfield->specialformat = 1;
latfield->specialformat = 1;
}
}
// Some TRMM CERES Grid Data have "default" to be set for the corner coordinate,
// which they really mean for the whole globe(-180 - 180 lon and -90 - 90 lat).
// We will remember the information and change
// those values when we read the lat and lon.
if(((int)(lowright[0])==0) &&((int)(upleft[0])==0)
&& ((int)(upleft[1])==0) && ((int)(lowright[1])==0)) {
if(projectioncode == GCTP_GEO){
lonfield->specialformat = 2;
latfield->specialformat = 2;
gdset->addfvalueattr = true;
}
}
//One MOD13C2 file doesn't provide projection code
// The upperleft and lowerright coordinates are all -1
// We have to calculate lat/lon by ourselves.
// Since it doesn't provide the project code, we double check their information
// and find that it covers the whole globe with 0.05 degree resolution.
// Lat. is from 90 to -90 and Lon is from -180 to 180.
if(((int)(lowright[0])==-1) &&((int)(upleft[0])==-1)
&& ((int)(upleft[1])==-1) && ((int)(lowright[1])==-1)) {
lonfield->specialformat = 3;
latfield->specialformat = 3;
lonfield->condenseddim = true;
latfield->condenseddim = true;
}
// We need to handle SOM projection in a different way.
if(GCTP_SOM == projectioncode) {
lonfield->specialformat = 4;
latfield->specialformat = 4;
}
// Check if the 2-D lat/lon can be condensed to 1-D.
//For current HDF-EOS2 files, only GEO and CEA can be condensed. To gain performance,
// we just check the projection code, don't check with real values.
if(projectioncode == GCTP_GEO || projectioncode ==GCTP_CEA) {
lonfield->condenseddim = true;
latfield->condenseddim = true;
}
#if 0
// Cache
// Check if a BES key H4.EnableEOSGeoCacheFile is true, if yes, we will check
// if a lat/lon cache file exists for this lat/lon.
string check_eos_geo_cache_key = "H4.EnableEOSGeoCacheFile";
bool enable_eos_geo_cache_key = false;
enable_eos_geo_cache_key = HDFCFUtil::check_beskeys(check_eos_geo_cache_key);
if(true == enable_eos_geo_cache_key) {
// Build the cache file name based on the projection parameters.
string cache_fname;
// Projection code, zone,sphere,pix,origin
cache_fname =HDFCFUtil::get_int_str(gdset->getProjection().getCode());
cache_fname +=HDFCFUtil::get_int_str(gdset->getProjection().getZone());
cache_fname +=HDFCFUtil::get_int_str(gdset->getProjection().getSphere());
cache_fname +=HDFCFUtil::get_int_str(gdset->getProjection().getPix());
cache_fname +=HDFCFUtil::get_int_str(gdset->getProjection().getOrigin());
// Xdim size, ydim size
if(dmajor) {
cache_fname +=HDFCFUtil::get_int_str(ydimsize);
cache_fname +=HDFCFUtil::get_int_str(xdimsize);
}
else {
cache_fname +=HDFCFUtil::get_int_str(xdimsize);
cache_fname +=HDFCFUtil::get_int_str(ydimsize);
}
// upleft,lowright
cache_fname +=HDFCFUtil::get_double_str(upleft[0],17,6);
cache_fname +=HDFCFUtil::get_double_str(upleft[1],17,6);
cache_fname +=HDFCFUtil::get_double_str(lowright[0],17,6);
cache_fname +=HDFCFUtil::get_double_str(lowright[1],17,6);
// obtain param
float64* params;
params = const_cast<float64 *>(gdset->getProjection().getParam());
// According to HDF-EOS2 document, only 13 parameters are used.
//for(int ipar = 0; ipar<16;ipar++)
for(int ipar = 0; ipar<13;ipar++)
cache_fname+=HDFCFUtil::get_double_str(params[ipar],17,6);
//cerr<<"cache_fname is "<<cache_fname <<endl;
// Check if this cache file exists and the file size, then set the flag.
// 0: The file doesn't exist. 1: The file size is not the same as the lat/lon size.
// 2: The file size is the same as the lat/lon size.
// Check the status of the file
struct stat st;
string cache_fpath = "/tmp/"+cache_fname;
int result = stat(cache_fpath.c_str(), &st);
if(result == 0){
int actual_file_size = st.st_size;
cerr<<"HDF-EOS2 actual_file_size is "<<actual_file_size <<endl;
int expected_file_size = 0;
if(gdset->getProjection().getCode() == GCTP_SOM)
expected_file_size = xdimsize*ydimsize*2*sizeof(double)*NBLOCK;
else if(gdset->getProjection().getCode() == GCTP_CEA ||
gdset->getProjection().getCode() == GCTP_GEO)
expected_file_size = (xdimsize+ydimsize)*sizeof(double);
else
expected_file_size = xdimsize*ydimsize*2*sizeof(double);
cerr<<"HDF-EOS2 expected_file_size is "<<expected_file_size <<endl;
if(actual_file_size != expected_file_size){
cerr<<"field_cache is 1 "<<endl;
lonfield->field_cache = 1;
latfield->field_cache = 1;
}
else {
cerr<<"field cache is 2 "<<endl;
lonfield->field_cache = 2;
latfield->field_cache = 2;
}
}
//FILE* pFile;
//pFile = fopen(cache_fname.c_str(),"rb");
// struct stat st;
// int result = stat(filename, &st);