-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHDFEOS2ArrayGridGeoField.cc
More file actions
2447 lines (2024 loc) · 91.1 KB
/
HDFEOS2ArrayGridGeoField.cc
File metadata and controls
2447 lines (2024 loc) · 91.1 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 Grid
// Authors: MuQun Yang <myang6@hdfgroup.org>
// Copyright (c) 2009-2012 The HDF Group
/////////////////////////////////////////////////////////////////////////////
#ifdef USE_HDFEOS2_LIB
#include "HDFEOS2ArrayGridGeoField.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <iostream>
#include <sstream>
#include <cassert>
#include <libdap/debug.h>
#include "HDFEOS2.h"
#include <libdap/InternalErr.h>
#include <BESDebug.h>
#include "HDFCFUtil.h"
#include "misrproj.h"
#include "errormacros.h"
#include <proj.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include "BESH4MCache.h"
#include "HDF4RequestHandler.h"
using namespace std;
using namespace libdap;
#define SIGNED_BYTE_TO_INT32 1
// These two functions are used to handle MISR products with the SOM projections.
extern "C" {
int inv_init(int insys, int inzone, double *inparm, int indatum, char *fn27, char *fn83, int *iflg, int (*inv_trans[])(double, double, double*, double*));
int sominv(double y, double x, double *lon, double *lat);
}
bool
HDFEOS2ArrayGridGeoField::read ()
{
BESDEBUG("h4","Coming to HDFEOS2ArrayGridGeoField 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();
// Currently The latitude and longitude rank from HDF-EOS2 grid must be either 1-D or 2-D.
// However, For SOM projection the final rank will become 3.
if (rank < 1 || rank > 2) {
throw InternalErr (__FILE__, __LINE__, "The rank of geo field is greater than 2, currently we don't support 3-D lat/lon cases.");
}
// MISR SOM file's final rank is 3. So declare a new variable.
int final_rank = -1;
if (true == condenseddim)
final_rank = 1;
else if(4 == specialformat)// For the SOM projection, the final output of latitude/longitude rank should be 3.
final_rank = 3;
else
final_rank = rank;
vector<int> offset;
offset.resize(final_rank);
vector<int> count;
count.resize(final_rank);
vector<int> step;
step.resize(final_rank);
int nelms = -1;
// Obtain the number of the subsetted elements
nelms = format_constraint (&offset[0], &step[0], &count[0]);
// Define function pointers to handle both grid and swath Note: in
// this code, we only handle grid, implementing this way is to
// keep the same style as the read functions in other files.
int32 (*openfunc) (char *, intn);
int32 (*attachfunc) (int32, char *);
intn (*detachfunc) (int32);
intn (*fieldinfofunc) (int32, char *, int32 *, int32 *, int32 *, char *);
intn (*readfieldfunc) (int32, char *, int32 *, int32 *, int32 *, void *);
string datasetname;
openfunc = GDopen;
attachfunc = GDattach;
detachfunc = GDdetach;
fieldinfofunc = GDfieldinfo;
readfieldfunc = GDreadfield;
datasetname = gridname;
int32 gfid = -1;
int32 gridid = -1;
/* Declare projection code, zone, etc grid parameters. */
int32 projcode = -1;
int32 zone = -1;
int32 sphere = -1;
float64 params[16];
int32 xdim = 0;
int32 ydim = 0;
float64 upleft[2];
float64 lowright[2];
string cache_fpath="";
bool use_cache = false;
// Check if passing file IDs to data
if(true == check_pass_fileid_key)
gfid = gridfd;
else {
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 ());
}
}
// Attach the grid id; make the grid valid.
gridid = attachfunc (gfid, const_cast < char *>(datasetname.c_str ()));
if (gridid < 0) {
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "Grid " << datasetname.c_str () << " cannot be attached.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if(false == llflag) {
// Cache
// Check if a BES key H4.EnableEOSGeoCacheFile is true, if yes, we will check
// if a lat/lon cache file exists and if we can read lat/lon from this file.
#if 0
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);
#endif
if(true == HDF4RequestHandler::get_enable_eosgeo_cachefile()) {
use_cache = true;
BESH4Cache *llcache = BESH4Cache::get_instance();
// Here we have a sanity check for the cached parameters:Cached directory,file prefix and cached directory size.
// Supposedly Hyrax BES cache feature should check this and the code exists. However, the
// current hyrax 1.9.7 doesn't provide this feature. KY 2014-10-24
#if 0
string bescachedir= llcache->getCacheDirFromConfig();
string bescacheprefix = llcache->getCachePrefixFromConfig();
unsigned long cachesize = llcache->getCacheSizeFromConfig();
#endif
//#if 0
string bescachedir = HDF4RequestHandler::get_cache_latlon_path();
string bescacheprefix = HDF4RequestHandler::get_cache_latlon_prefix();
long cachesize = HDF4RequestHandler::get_cache_latlon_size();
//#endif
if(("" == bescachedir)||(""==bescacheprefix)||(cachesize <=0)){
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw InternalErr (__FILE__, __LINE__, "Either the cached dir is empty or the prefix is NULL or the cache size is not set.");
}
else {
struct stat sb;
if(stat(bescachedir.c_str(),&sb) !=0) {
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
string err_mesg="The cached directory " + bescachedir;
err_mesg = err_mesg + " doesn't exist. ";
throw InternalErr(__FILE__,__LINE__,err_mesg);
}
else {
if(true == S_ISDIR(sb.st_mode)) {
if(access(bescachedir.c_str(),R_OK|W_OK|X_OK) == -1) {
//if(access(bescachedir.c_str(),R_OK) == -1)
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
string err_mesg="The cached directory " + bescachedir;
err_mesg = err_mesg + " can NOT be read,written or executable.";
throw InternalErr(__FILE__,__LINE__,err_mesg);
}
}
else {
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
string err_mesg="The cached directory " + bescachedir;
err_mesg = err_mesg + " is not a directory.";
throw InternalErr(__FILE__,__LINE__,err_mesg);
}
}
}
string cache_fname=HDF4RequestHandler::get_cache_latlon_prefix();
intn r = -1;
r = GDprojinfo (gridid, &projcode, &zone, &sphere, params);
if (r!=0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw InternalErr (__FILE__, __LINE__, "GDprojinfo failed");
}
// Retrieve dimensions and X-Y coordinates of corners
if (GDgridinfo(gridid, &xdim, &ydim, upleft,
lowright) == -1) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw InternalErr (__FILE__, __LINE__, "GDgridinfo failed");
}
// Retrieve pixel registration information
int32 pixreg = 0;
r = GDpixreginfo (gridid, &pixreg);
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "cannot obtain grid pixel registration info.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
//Retrieve grid pixel origin
int32 origin = 0;
r = GDorigininfo (gridid, &origin);
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "cannot obtain grid origin info.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Projection code,zone,sphere,pix,origin
cache_fname +=HDFCFUtil::get_int_str(projcode);
cache_fname +=HDFCFUtil::get_int_str(zone);
cache_fname +=HDFCFUtil::get_int_str(sphere);
cache_fname +=HDFCFUtil::get_int_str(pixreg);
cache_fname +=HDFCFUtil::get_int_str(origin);
// Xdimsize and ydimsize. Although it is rare, need to consider dim major.
// Whether latlon is ydim,xdim or xdim,ydim.
if(ydimmajor) {
cache_fname +=HDFCFUtil::get_int_str(ydim);
cache_fname +=HDFCFUtil::get_int_str(xdim);
}
else {
cache_fname +=HDFCFUtil::get_int_str(xdim);
cache_fname +=HDFCFUtil::get_int_str(ydim);
}
// upleft,lowright
// HDF-EOS upleft,lowright,params use DDDDMMMSSS.6 digits. So choose %17.6f.
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);
// According to HDF-EOS2 document, only 13 parameters are used.
for(int ipar = 0; ipar<13;ipar++)
cache_fname+=HDFCFUtil::get_double_str(params[ipar],17,6);
cache_fpath = bescachedir + "/"+ cache_fname;
#if 0
cerr<<"cache file path is "<<cache_fpath <<endl;
cerr<<"obtain file path from BESMCache "<<endl;
cerr<<"Name is "<<llcache->get_cache_file_name_h4(cache_fpath,false) <<endl;
int fd;
llcache->get_read_lock(cache_fpath,fd);
cerr<<"after testing get_read_lock"<<endl;
#endif
try {
do { // do while(0) is a trick to handle break; so ignore solarcloud's warning.
int expected_file_size = 0;
if(GCTP_CEA == projcode || GCTP_GEO == projcode)
expected_file_size = (xdim+ydim)*sizeof(double);
else if(GCTP_SOM == projcode)
expected_file_size = xdim*ydim*NBLOCK*2*sizeof(double);
else
expected_file_size = xdim*ydim*2*sizeof(double);
int fd = 0;
bool latlon_from_cache = llcache->get_data_from_cache(cache_fpath, expected_file_size,fd);
#if 0
if(true == latlon_from_cache)
cerr<<"the cached file exists: "<<endl;
else
cerr<<"the cached file doesn't exist "<< endl;
#endif
if(false == latlon_from_cache)
break;
// Get the offset of lat/lon in the cached file. Since lat is stored first and lon is stored second,
// so offset_1d for lat/lon is different.
// We still need to consider different projections. 1D,2D,3D reading.Need also to consider dim major and special format.
size_t offset_1d = 0;
// Get the count of the lat/lon from the cached file.
// Notice the data is read continuously. So starting from the offset point, we have to read all elements until the
// last points. The total count for the data point is bigger than the production of count and step.
int count_1d = 1;
if(GCTP_CEA == projcode|| GCTP_GEO== projcode) {
// It seems that for 1-D lat/lon, regardless of xdimmajor or ydimmajor. It is always Lat[YDim],Lon[XDim}, check getCorrectSubset
// So we don't need to consider the dimension major case.
offset_1d = (fieldtype == 1) ?offset[0] :(ydim+offset[0]);
#if 0
if(true == ydimmajor) {
offset_1d = (fieldtype == 1) ?offset[0] :(ydim*sizeof(double)+offset[0]);
}
else {
offset_1d = (fieldtype == 1) ?offset[0] :(xdim*sizeof(double)+offset[0]);
}
#endif
count_1d = 1+(count[0]-1)*step[0];
}
else if (GCTP_SOM == projcode) {
if(true == ydimmajor) {
offset_1d = (fieldtype == 1)?(offset[0]*xdim*ydim+offset[1]*xdim+offset[2])
:(offset[0]*xdim*ydim+offset[1]*xdim+offset[2]+expected_file_size/2/sizeof(double));
}
else {
offset_1d = (fieldtype == 1)?(offset[0]*xdim*ydim+offset[1]*ydim+offset[2])
:(offset[0]*xdim*ydim+offset[1]*ydim+offset[2]+expected_file_size/2/sizeof(double));
}
int total_count_dim0 = (count[0]-1)*step[0];
int total_count_dim1 = (count[1]-1)*step[1];
int total_count_dim2 = (count[2]-1)*step[2];
int total_dim1 = (true ==ydimmajor)?ydim:xdim;
int total_dim2 = (true ==ydimmajor)?xdim:ydim;
// Flatten the 3-D index to 1-D
// This calculation can be generalized from nD to 1D
// but since we only use it here. Just keep it this way.
count_1d = 1 + total_count_dim0*total_dim1*total_dim2 + total_count_dim1*total_dim2 + total_count_dim2;
}
else {// 2-D lat/lon case
if (true == ydimmajor)
offset_1d = (fieldtype == 1) ?(offset[0] * xdim + offset[1]):(expected_file_size/2/sizeof(double)+offset[0]*xdim+offset[1]);
else
offset_1d = (fieldtype == 1) ?(offset[0] * ydim + offset[1]):(expected_file_size/2/sizeof(double)+offset[0]*ydim+offset[1]);
// Flatten the 2-D index to 1-D
int total_count_dim0 = (count[0]-1)*step[0];
int total_count_dim1 = (count[1]-1)*step[1];
int total_dim1 = (true ==ydimmajor)?xdim:ydim;
count_1d = 1 + total_count_dim0*total_dim1 + total_count_dim1;
}
// Assign a vector to store lat/lon
vector<double> latlon_1d;
latlon_1d.resize(count_1d);
// Read lat/lon from the file.
//int fd;
//fd = open(cache_fpath.c_str(),O_RDONLY,0666);
// TODO: Use BESLog to report that the cached file cannot be read.
off_t fpos = lseek(fd,sizeof(double)*offset_1d,SEEK_SET);
if (-1 == fpos) {
llcache->unlock_and_close(cache_fpath);
llcache->purge_file(cache_fpath);
break;
}
ssize_t read_size = HDFCFUtil::read_vector_from_file(fd,latlon_1d,sizeof(double));
llcache->unlock_and_close(cache_fpath);
if((-1 == read_size) || ((size_t)read_size != count_1d*sizeof(double))) {
llcache->purge_file(cache_fpath);
break;
}
// Leave the debugging comments for the time being.
#if 0
// ONLY READ the subset
FILE *pFile;
pFile = fopen(cache_fpath.c_str(),"rb");
if(NULL == pFile)
break;
int ret_value = fseek(pFile,sizeof(double)*offset_1d,SEEK_SET);
if(ret_value != 0) {
// fall back to the original calculation.
fclose(pFile);
break;
}
cerr<<"field name is "<<fieldname <<endl;
cerr<<"fread is checked "<<endl;
cerr<<"offset_1d is "<<offset_1d <<endl;
cerr<<"count_1d is "<<count_1d <<endl;
ret_value = fread(&latlon_1d[0],sizeof(double),count_1d,pFile);
if(0 == ret_value) {
// fall back to the original calculation
cerr<<"fread fails "<<endl;
fclose(pFile);
break;
}
#endif
#if 0
for(int i =0;i<count_1d;i++)
cerr<<"latlon_1d["<<i<<"]"<<latlon_1d[i]<<endl;
#endif
int total_count = 1;
for (int i_rank = 0; i_rank<final_rank;i_rank++)
total_count = total_count*count[i_rank];
// We will see if there is a shortcut that the lat/lon is accessed with
// one-big-block. Actually this is the most common case. If we find
// such a case, we simply read the whole data into the latlon buffer and
// send it to BES.
if(total_count == count_1d) {
set_value((dods_float64*)&latlon_1d[0],nelms);
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
return false;
}
vector<double>latlon;
latlon.resize(total_count);
// Retrieve latlon according to the projection
if(GCTP_CEA == projcode|| GCTP_GEO== projcode) {
for (int i = 0; i <total_count;i++)
latlon[i] = latlon_1d[i*step[0]];
}
else if (GCTP_SOM == projcode) {
for (int i =0; i<count[0];i++)
for(int j =0;j<count[1];j++)
for(int k=0;k<count[2];k++)
latlon[i*count[1]*count[2]+j*count[2]+k]=(true == ydimmajor)
?latlon_1d[i*ydim*xdim*step[0]+j*xdim*step[1]+k*step[2]]
:latlon_1d[i*ydim*xdim*step[0]+j*ydim*step[1]+k*step[2]];
}
else {
for (int i =0; i<count[0];i++)
for(int j =0;j<count[1];j++)
latlon[i*count[1]+j]=(true == ydimmajor)
?latlon_1d[i*xdim*step[0]+j*step[1]]
:latlon_1d[i*ydim*step[0]+j*step[1]];
}
set_value((dods_float64*)&latlon[0],nelms);
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
return false;
} while (0);
}
catch(...) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw;
}
}
}
// In this step, if use_cache is true, we always need to write the lat/lon into the cache.
// SOM projection should be calculated differently. If turning on the lat/lon cache feature, it also needs to be handled differently.
if(specialformat == 4) {// SOM projection
try {
CalculateSOMLatLon(gridid, &offset[0], &count[0], &step[0], nelms,cache_fpath,use_cache);
}
catch(...) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw;
}
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
return false;
}
// We define offset,count and step in int32 datatype.
vector<int32>offset32;
offset32.resize(rank);
vector<int32>count32;
count32.resize(rank);
vector<int32>step32;
step32.resize(rank);
// Obtain offset32 with the correct rank, the rank of lat/lon of
// GEO and CEA projections in the file may be 2 instead of 1.
try {
getCorrectSubset (&offset[0], &count[0], &step[0], &offset32[0], &count32[0], &step32[0], condenseddim, ydimmajor, fieldtype, rank);
}
catch(...) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw;
}
// The following case handles when the lat/lon is not provided.
if (llflag == false) { // We have to calculate the lat/lon
vector<float64>latlon;
latlon.resize(nelms);
// If projection code etc. is not retrieved, retrieve them.
// When specialformat is 3, the file is a file of which the project code is set to -1, we need to skip it. KY 2014-09-11
if(projcode == -1 && specialformat !=3) {
intn r = 0;
r = GDprojinfo (gridid, &projcode, &zone, &sphere, params);
if (r!=0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw InternalErr (__FILE__, __LINE__, "GDprojinfo failed");
}
// Retrieve dimensions and X-Y coordinates of corners
if (GDgridinfo(gridid, &xdim, &ydim, upleft,
lowright) == -1) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw InternalErr (__FILE__, __LINE__, "GDgridinfo failed");
}
}
// Handle LAMAZ projection first.
if (GCTP_LAMAZ == projcode) {
try {
vector<double>latlon_all;
latlon_all.resize(xdim*ydim*2);
CalculateLAMAZLatLon(gridid, fieldtype, &latlon[0], &latlon_all[0],&offset[0], &count[0], &step[0], use_cache);
if(true == use_cache) {
BESH4Cache *llcache = BESH4Cache::get_instance();
llcache->write_cached_data(cache_fpath,xdim*ydim*2*sizeof(double),latlon_all);
}
}
catch(...) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw;
}
set_value ((dods_float64 *) &latlon[0], nelms);
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
return false;
}
// Aim to handle large MCD Grid such as 21600*43200 lat,lon
if (specialformat == 1) {
try {
vector<double>latlon_all;
latlon_all.resize(xdim+ydim);
CalculateLargeGeoLatLon(gridid, fieldtype,&latlon[0], &latlon_all[0],&offset[0], &count[0], &step[0], nelms,use_cache);
if(true == use_cache) {
BESH4Cache *llcache = BESH4Cache::get_instance();
llcache->write_cached_data(cache_fpath,(xdim+ydim)*sizeof(double),latlon_all);
#if 0
// if(HDFCFUtil::write_vector_to_file(cache_fpath,latlon_all,sizeof(double)) != ((xdim+ydim)))
if(HDFCFUtil::write_vector_to_file2(cache_fpath,latlon_all,sizeof(double)) != ((xdim+ydim)*sizeof(double))) {
if(remove(cache_fpath.c_str()) !=0) {
throw InternalErr(__FILE__,__LINE__,"Cannot remove the cached file.");
}
}
#endif
}
}
catch(...) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw;
}
set_value((dods_float64 *)&latlon[0],nelms);
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
return false;
}
// Now handle other cases,note the values will be written after the if-block
else if (specialformat == 3) {// Have to provide latitude and longitude by ourselves
try {
CalculateSpeLatLon (gridid, fieldtype, &latlon[0], &offset32[0], &count32[0], &step32[0]);
}
catch(...) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw;
}
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
}
else {// This is mostly general case, it will calculate lat/lon with GDij2ll.
// Cache: check the flag and decide whether to calculate the lat/lon.
vector<double>latlon_all;
if(GCTP_GEO == projcode || GCTP_CEA == projcode)
latlon_all.resize(xdim+ydim);
else
latlon_all.resize(xdim*ydim*2);
CalculateLatLon (gridid, fieldtype, specialformat, &latlon[0],&latlon_all[0],
&offset32[0], &count32[0], &step32[0], nelms,use_cache);
if(true == use_cache) {
// size_t num_item_to_write = HDFCFUtil::write_vector_to_file2(cache_fpath,latlon_all,sizeof(double));
size_t num_item_expected = 0;
if(GCTP_GEO == projcode || GCTP_CEA == projcode)
num_item_expected = xdim + ydim;
else
num_item_expected = xdim*ydim*2;
BESH4Cache *llcache = BESH4Cache::get_instance();
llcache->write_cached_data(cache_fpath,num_item_expected*sizeof(double),latlon_all);
}
// The longitude values changed in the cache file is implemented in CalculateLatLon.
// Some longitude values need to be corrected.
if (speciallon && fieldtype == 2)
CorSpeLon(&latlon[0], nelms);
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
}
set_value ((dods_float64 *) &latlon[0], nelms);
return false;
}
// Now lat and lon are stored as HDF-EOS2 fields. We need to read the lat and lon values from the fields.
int32 tmp_rank = -1;
vector<int32> tmp_dims;
tmp_dims.resize(rank);
char tmp_dimlist[1024];
int32 type = -1;
intn r = -1;
// Obtain field info.
r = fieldinfofunc (gridid, const_cast < char *>(fieldname.c_str ()),
&tmp_rank, &tmp_dims[0], &type, tmp_dimlist);
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "Field " << fieldname.c_str () << " information cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Retrieve dimensions and X-Y coordinates of corners
#if 0
//int32 xdim = 0;
//int32 ydim = 0;
//float64 upleft[2];
//float64 lowright[2];
#endif
r = GDgridinfo (gridid, &xdim, &ydim, upleft, lowright);
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "Grid " << datasetname.c_str () << " information cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Retrieve all GCTP projection information
//int32 projcode = -1;
//int32 zone = -1;
//int32 sphere = -1;
//float64 params[16];
r = GDprojinfo (gridid, &projcode, &zone, &sphere, params);
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "Grid " << datasetname.c_str () << " projection info. cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (projcode != GCTP_GEO) { // Just retrieve the data like other fields
// We have to loop through all datatype and read the lat/lon out.
switch (type) {
case DFNT_INT8:
{
vector<int8> val;
val.resize(nelms);
r = readfieldfunc (gridid,
const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], (void*)(&val[0]));
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// DAP2 requires the map of SIGNED_BYTE to INT32 if
// SIGNED_BYTE_TO_INT32 is defined.
#ifndef SIGNED_BYTE_TO_INT32
set_value ((dods_byte *) &val[0], nelms);
#else
vector<int32>newval;
newval.resize(nelms);
for (int counter = 0; counter < nelms; counter++)
newval[counter] = (int32) (val[counter]);
set_value ((dods_int32 *) &newval[0], nelms);
#endif
}
break;
case DFNT_UINT8:
case DFNT_UCHAR8:
{
vector<uint8> val;
val.resize(nelms);
r = readfieldfunc (gridid,
const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], (void*)(&val[0]));
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_byte *) &val[0], nelms);
}
break;
case DFNT_INT16:
{
vector<int16> val;
val.resize(nelms);
r = readfieldfunc (gridid,
const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], (void*)(&val[0]));
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_int16 *) &val[0], nelms);
}
break;
case DFNT_UINT16:
{
vector<uint16> val;
val.resize(nelms);
r = readfieldfunc (gridid,
const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], (void*)(&val[0]));
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_uint16 *) &val[0], nelms);
}
break;
case DFNT_INT32:
{
vector<int32> val;
val.resize(nelms);
r = readfieldfunc (gridid,
const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], (void*)(&val[0]));
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_int32 *) &val[0], nelms);
}
break;
case DFNT_UINT32:
{
vector<uint32> val;
val.resize(nelms);
r = readfieldfunc (gridid,
const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], (void*)(&val[0]));
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_uint32 *) &val[0], nelms);
}
break;
case DFNT_FLOAT32:
{
vector<float32> val;
val.resize(nelms);
r = readfieldfunc (gridid,
const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], (void*)(&val[0]));
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_float32 *) &val[0], nelms);
}
break;
case DFNT_FLOAT64:
{
vector<float64> val;
val.resize(nelms);
r = readfieldfunc (gridid,
const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], (void*)(&val[0]));
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_float64 *) &val[0], nelms);
}
break;
default:
{
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw InternalErr (__FILE__, __LINE__, "unsupported data type.");
}
}
}
else {// Only handle special cases for the Geographic Projection
// We find that lat/lon of the geographic projection in some
// files include fill values. So we recalculate lat/lon based
// on starting value,step values and number of steps.
// GDgetfillvalue will return 0 if having fill values.
// The other returned value indicates no fillvalue is found inside the lat or lon.
switch (type) {
case DFNT_INT8:
{
vector<int8> val;
val.resize(nelms);
int8 fillvalue = 0;
r = GDgetfillvalue (gridid,
const_cast < char *>(fieldname.c_str ()),
&fillvalue);
if (r == 0) {
int ifillvalue = fillvalue;
vector <int8> temp_total_val;
//The previous size doesn't make sense since num_elems = xdim*ydim
temp_total_val.resize(xdim*ydim);
//temp_total_val.resize(xdim*ydim*4);
r = readfieldfunc(gridid,
const_cast < char *>(fieldname.c_str ()),
NULL, NULL, NULL, (void *)(&temp_total_val[0]));
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
try {
// Recalculate lat/lon for the geographic projection lat/lon that has fill values
HandleFillLatLon(temp_total_val, (int8*)&val[0],ydimmajor,fieldtype,xdim,ydim,&offset32[0],&count32[0],&step32[0],ifillvalue);
}
catch(...) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
throw;
}
}
else {
r = readfieldfunc (gridid,
const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], (void*)(&val[0]));
if (r != 0) {
detachfunc(gridid);
HDFCFUtil::close_fileid(-1,-1,gfid,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
}
if (speciallon && fieldtype == 2)
CorSpeLon ((int8 *) &val[0], nelms);
#ifndef SIGNED_BYTE_TO_INT32
set_value ((dods_byte *) &val[0], nelms);
#else
vector<int32>newval;
newval.resize(nelms);
for (int counter = 0; counter < nelms; counter++)
newval[counter] = (int32) (val[counter]);
set_value ((dods_int32 *) &newval[0], nelms);
#endif
}
break;
case DFNT_UINT8:
case DFNT_UCHAR8:
{
vector<uint8> val;
val.resize(nelms);
uint8 fillvalue = 0;
r = GDgetfillvalue (gridid,
const_cast < char *>(fieldname.c_str ()),
&fillvalue);