32 #include "config_hdf5.h"
38 #include "InternalErr.h"
47 BaseType *HDFEOS5CFMissLLArray::ptr_duplicate()
52 bool HDFEOS5CFMissLLArray::read()
55 BESDEBUG(
"h5",
"Coming to HDFEOS5CFMissLLArray read "<<endl);
56 if(NULL == HDF5RequestHandler::get_lrdata_mem_cache())
57 read_data_NOT_from_mem_cache(
false,NULL);
59 vector<string> cur_lrd_non_cache_dir_list;
60 HDF5RequestHandler::get_lrd_non_cache_dir_list(cur_lrd_non_cache_dir_list);
64 if( (cur_lrd_non_cache_dir_list.size() == 0) ||
65 (
"" == check_str_sect_in_list(cur_lrd_non_cache_dir_list,filename,
'/'))) {
67 vector<string> cur_cache_dlist;
68 HDF5RequestHandler::get_lrd_cache_dir_list(cur_cache_dlist);
69 string cache_dir = check_str_sect_in_list(cur_cache_dlist,filename,
'/');
71 cache_key = cache_dir + varname;
75 cache_key = filename + varname;
81 if(cvartype == CV_LAT_MISS)
82 handle_data_with_mem_cache(H5FLOAT32,(
size_t)ydimsize,cache_flag,cache_key);
84 handle_data_with_mem_cache(H5FLOAT32,(
size_t)xdimsize,cache_flag,cache_key);
87 read_data_NOT_from_mem_cache(
false,NULL);
92 void HDFEOS5CFMissLLArray::read_data_NOT_from_mem_cache(
bool add_cache,
void*buf){
94 BESDEBUG(
"h5",
"Coming to read_data_NOT_from_mem_cache "<<endl);
99 if(eos5_projcode == HE5_GCTP_GEO) {
100 read_data_NOT_from_mem_cache_geo(add_cache,buf);
110 throw InternalErr (__FILE__, __LINE__,
111 "The number of dimension of this variable should be greater than 0");
116 nelms = format_constraint (&offset[0], &step[0], &count[0]);
120 throw InternalErr (__FILE__, __LINE__,
121 "The number of elments is negative.");
123 vector<size_t>pos(rank,0);
124 for (
int i = 0; i< rank; i++)
127 vector<size_t>dimsizes;
128 dimsizes.push_back(ydimsize);
129 dimsizes.push_back(xdimsize);
130 int total_elms = xdimsize*ydimsize;
138 rows.resize(xdimsize*ydimsize);
139 cols.resize(xdimsize*ydimsize);
140 lon.resize(xdimsize*ydimsize);
141 lat.resize(xdimsize*ydimsize);
143 upleft[0] = point_left;
144 upleft[1] = point_upper;
145 lowright[0] = point_right;
146 lowright[1] = point_lower;
155 for (k = j = 0; j < ydimsize; ++j) {
156 for (i = 0; i < xdimsize; ++i) {
163 BESDEBUG(
"h5",
" Before calling GDij2ll, check all projection parameters. " << endl);
164 BESDEBUG(
"h5",
" eos5_projcode is " << eos5_projcode <<endl);
165 BESDEBUG(
"h5",
" eos5_zone is " << eos5_zone <<endl);
166 BESDEBUG(
"h5",
" eos5_params[0] is " << eos5_params[0] <<endl);
167 BESDEBUG(
"h5",
" eos5_params[1] is " << eos5_params[1] <<endl);
168 BESDEBUG(
"h5",
" eos5_sphere is " << eos5_sphere <<endl);
169 BESDEBUG(
"h5",
" xdimsize is " << xdimsize <<endl);
170 BESDEBUG(
"h5",
" ydimsize is " << ydimsize <<endl);
171 BESDEBUG(
"h5",
" eos5_pixelreg is " << eos5_pixelreg <<endl);
172 BESDEBUG(
"h5",
" eos5_origin is " << eos5_origin <<endl);
173 BESDEBUG(
"h5",
" upleft[0] is " << upleft[0] <<endl);
174 BESDEBUG(
"h5",
" upleft[1] is " << upleft[1] <<endl);
175 BESDEBUG(
"h5",
" lowright[0] is " << lowright[0] <<endl);
176 BESDEBUG(
"h5",
" lowright[1] is " << lowright[1] <<endl);
179 cerr<<
" eos5_params[0] is " << eos5_params[0] <<endl;
180 cerr<<
" eos5_params[1] is " << eos5_params[1] <<endl;
181 cerr<<
" eos5_sphere is " << eos5_sphere <<endl;
182 cerr<<
" eos5_zone is " << eos5_zone <<endl;
183 cerr<<
" Before calling GDij2ll, check all projection parameters. " << endl;
184 cerr<<
" eos5_zone is " << eos5_zone <<endl;
185 cerr<<
" eos5_params[0] is " << eos5_params[0] <<endl;
186 cerr<<
" eos5_params[1] is " << eos5_params[1] <<endl;
187 BESDEBUG(
"h5",
" xdimsize is " << xdimsize <<endl);
188 BESDEBUG(
"h5",
" ydimsize is " << ydimsize <<endl);
189 BESDEBUG(
"h5",
" eos5_pixelreg is " << eos5_pixelreg <<endl);
190 BESDEBUG(
"h5",
" eos5_origin is " << eos5_origin <<endl);
191 BESDEBUG(
"h5",
" upleft[0] is " << upleft[0] <<endl);
192 BESDEBUG(
"h5",
" upleft[1] is " << upleft[1] <<endl);
193 BESDEBUG(
"h5",
" lowright[0] is " << lowright[0] <<endl);
194 BESDEBUG(
"h5",
" lowright[1] is " << lowright[1] <<endl);
200 bool ll_read_from_cache =
true;
203 bool use_latlon_cache = HDF5RequestHandler::get_use_eosgeo_cachefile();
207 if(use_latlon_cache ==
true) {
211 string cache_fname = obtain_ll_cache_name();
214 long ll_disk_cache_size = HDF5RequestHandler::get_latlon_disk_cache_size();
215 string ll_disk_cache_dir = HDF5RequestHandler::get_latlon_disk_cache_dir();
216 string ll_disk_cache_prefix = HDF5RequestHandler::get_latlon_disk_cachefile_prefix();
219 int expected_file_size = 2*xdimsize*ydimsize*
sizeof(double);
222 ll_read_from_cache = ll_cache->get_data_from_cache(cache_fname, expected_file_size,fd);
224 if(ll_read_from_cache ==
true) {
226 BESDEBUG(
"h5",
" Read latitude and longitude from a disk cache. " <<endl);
227 size_t var_offset = 0;
229 if(CV_LON_MISS == cvartype)
230 var_offset = xdimsize*ydimsize*
sizeof(double);
232 vector<double> var_value;
233 var_value.resize(xdimsize*ydimsize);
236 off_t fpos = lseek(fd,var_offset,SEEK_SET);
238 throw InternalErr (__FILE__, __LINE__,
239 "Cannot seek the cached file offset.");
243 ll_cache->unlock_and_close(cache_fname);
246 if((-1 == ret_val) || ((
size_t)ret_val != (xdimsize*ydimsize*
sizeof(
double)))) {
247 ll_cache->purge_file(cache_fname);
248 ll_read_from_cache =
false;
252 if(total_elms == nelms)
253 set_value((dods_float64 *)&var_value[0],total_elms);
266 set_value((dods_float64 *)&val[0],nelms);
272 ll_read_from_cache =
false;
276 r = GDij2ll (eos5_projcode, eos5_zone, &eos5_params[0], eos5_sphere, xdimsize, ydimsize, upleft, lowright,
277 xdimsize * ydimsize, &rows[0], &cols[0], &lon[0], &lat[0], eos5_pixelreg, eos5_origin);
280 eherr <<
"cannot calculate grid latitude and longitude";
281 throw InternalErr (__FILE__, __LINE__, eherr.str ());
288 if(use_latlon_cache ==
true && ll_read_from_cache ==
false) {
289 string cache_fname = obtain_ll_cache_name();
290 long ll_disk_cache_size = HDF5RequestHandler::get_latlon_disk_cache_size();
291 string ll_disk_cache_dir = HDF5RequestHandler::get_latlon_disk_cache_dir();
292 string ll_disk_cache_prefix = HDF5RequestHandler::get_latlon_disk_cachefile_prefix();
294 BESDEBUG(
"h5",
" Write EOS5 grid latitude and longitude to a disk cache. " <<endl);
299 vector <double>latlon;
300 latlon.reserve(xdimsize*ydimsize*2);
301 latlon.insert(latlon.end(),lat.begin(),lat.end());
302 latlon.insert(latlon.end(),lon.begin(),lon.end());
303 ll_cache->write_cached_data(cache_fname,2*xdimsize*ydimsize*
sizeof(
double),latlon);
306 BESDEBUG(
"h5",
" The first value of lon is " << lon[0] <<endl);
307 BESDEBUG(
"h5",
" The first value of lat is " << lat[0] <<endl);
310 vector<size_t>pos(rank,0);
311 for (
int i = 0; i< rank; i++)
314 vector<size_t>dimsizes;
315 dimsizes.push_back(ydimsize);
316 dimsizes.push_back(xdimsize);
317 int total_elms = xdimsize*ydimsize;
321 if(CV_LON_MISS == cvartype) {
322 if(total_elms == nelms)
323 set_value((dods_float64 *)&lon[0],total_elms);
336 set_value((dods_float64 *)&val[0],nelms);
340 else if(CV_LAT_MISS == cvartype) {
342 if(total_elms == nelms)
343 set_value((dods_float64 *)&lat[0],total_elms);
356 set_value((dods_float64 *)&val[0],nelms);
363 string HDFEOS5CFMissLLArray::obtain_ll_cache_name() {
365 BESDEBUG(
"h5",
"Coming to obtain_ll_cache_name "<<endl);
370 string bescachedir = HDF5RequestHandler::get_latlon_disk_cache_dir();
371 string bescacheprefix = HDF5RequestHandler::get_latlon_disk_cachefile_prefix();
372 long cachesize = HDF5RequestHandler::get_latlon_disk_cache_size();
374 if((
"" == bescachedir)||(
""==bescacheprefix)||(cachesize <=0)){
375 throw InternalErr (__FILE__, __LINE__,
"Either the cached dir is empty or the prefix is NULL or the cache size is not set.");
379 if(stat(bescachedir.c_str(),&sb) !=0) {
380 string err_mesg=
"The cached directory " + bescachedir;
381 err_mesg = err_mesg +
" doesn't exist. ";
382 throw InternalErr(__FILE__,__LINE__,err_mesg);
386 if(
true == S_ISDIR(sb.st_mode)) {
387 if(access(bescachedir.c_str(),R_OK|W_OK|X_OK) == -1) {
388 string err_mesg=
"The cached directory " + bescachedir;
389 err_mesg = err_mesg +
" can NOT be read,written or executable.";
390 throw InternalErr(__FILE__,__LINE__,err_mesg);
394 string err_mesg=
"The cached directory " + bescachedir;
395 err_mesg = err_mesg +
" is not a directory.";
396 throw InternalErr(__FILE__,__LINE__,err_mesg);
401 string cache_fname=HDF5RequestHandler::get_latlon_disk_cachefile_prefix();
404 cache_fname +=HDF5CFUtil::get_int_str(eos5_projcode);
405 cache_fname +=HDF5CFUtil::get_int_str(eos5_zone);
406 cache_fname +=HDF5CFUtil::get_int_str(eos5_sphere);
407 cache_fname +=HDF5CFUtil::get_int_str(eos5_pixelreg);
408 cache_fname +=HDF5CFUtil::get_int_str(eos5_origin);
410 cache_fname +=HDF5CFUtil::get_int_str(ydimsize);
411 cache_fname +=HDF5CFUtil::get_int_str(xdimsize);
415 cache_fname +=HDF5CFUtil::get_double_str(point_left,17,6);
416 cache_fname +=HDF5CFUtil::get_double_str(point_upper,17,6);
417 cache_fname +=HDF5CFUtil::get_double_str(point_right,17,6);
418 cache_fname +=HDF5CFUtil::get_double_str(point_lower,17,6);
421 for(
int ipar = 0; ipar<13;ipar++) {
422 cache_fname+=HDF5CFUtil::get_double_str(eos5_params[ipar],17,6);
425 string cache_fpath = bescachedir +
"/"+ cache_fname;
429 void HDFEOS5CFMissLLArray::read_data_NOT_from_mem_cache_geo(
bool add_cache,
void*buf){
431 BESDEBUG(
"h5",
"Coming to read_data_NOT_from_mem_cache_geo "<<endl);
439 throw InternalErr (__FILE__, __LINE__,
440 "The number of dimension of this variable should be greater than 0");
446 nelms = format_constraint (&offset[0], &step[0], &count[0]);
450 throw InternalErr (__FILE__, __LINE__,
451 "The number of elments is negative.");
460 if (CV_LAT_MISS == cvartype) {
462 if (HE5_HDFE_GD_UL == eos5_origin || HE5_HDFE_GD_UR == eos5_origin) {
475 throw InternalErr (__FILE__, __LINE__,
476 "The number of elments should be greater than 0.");
478 float lat_step = (end - start) /ydimsize;
481 if ( HE5_HDFE_CENTER == eos5_pixelreg ) {
482 for (
int i = 0; i < nelms; i++)
483 val[i] = ((
float)(offset[0]+i*step[0] + 0.5F) * lat_step + start) / 1000000.0;
486 if(add_cache ==
true) {
487 vector<float>total_val;
488 total_val.resize(ydimsize);
489 for (
int total_i = 0; total_i < ydimsize; total_i++)
490 total_val[total_i] = ((
float)(total_i + 0.5F) * lat_step + start) / 1000000.0;
492 memcpy(buf,&total_val[0],4*ydimsize);
497 for (
int i = 0; i < nelms; i++)
498 val[i] = ((
float)(offset[0]+i * step[0])*lat_step + start) / 1000000.0;
501 if(add_cache ==
true) {
502 vector<float>total_val;
503 total_val.resize(ydimsize);
504 for (
int total_i = 0; total_i < ydimsize; total_i++)
505 total_val[total_i] = ((
float)(total_i) * lat_step + start) / 1000000.0;
507 memcpy(buf,&total_val[0],4*ydimsize);
513 if (CV_LON_MISS == cvartype) {
515 if (HE5_HDFE_GD_UL == eos5_origin || HE5_HDFE_GD_LL == eos5_origin) {
528 throw InternalErr (__FILE__, __LINE__,
529 "The number of elments should be greater than 0.");
530 float lon_step = (end - start) /xdimsize;
532 if (HE5_HDFE_CENTER == eos5_pixelreg) {
533 for (
int i = 0; i < nelms; i++)
534 val[i] = ((
float)(offset[0] + i *step[0] + 0.5F) * lon_step + start ) / 1000000.0;
537 if(add_cache ==
true) {
538 vector<float>total_val;
539 total_val.resize(xdimsize);
540 for (
int total_i = 0; total_i < xdimsize; total_i++)
541 total_val[total_i] = ((
float)(total_i+0.5F) * lon_step + start) / 1000000.0;
543 memcpy(buf,&total_val[0],4*xdimsize);
548 for (
int i = 0; i < nelms; i++)
549 val[i] = ((
float)(offset[0]+i*step[0]) * lon_step + start) / 1000000.0;
552 if(add_cache ==
true) {
553 vector<float>total_val;
554 total_val.resize(xdimsize);
555 for (
int total_i = 0; total_i < xdimsize; total_i++)
556 total_val[total_i] = ((
float)(total_i) * lon_step + start) / 1000000.0;
558 memcpy(buf,&total_val[0],4*xdimsize);
564 for (
int i =0; i <nelms; i++)
565 "h5",
"final data val "<< i <<
" is " << val[i] <<endl;
568 set_value ((dods_float32 *) &val[0], nelms);
578 HDFEOS5CFMissLLArray::format_constraint (
int *offset,
int *step,
int *count)
583 Dim_iter p = dim_begin ();
585 while (p != dim_end ()) {
587 int start = dimension_start (p,
true);
588 int stride = dimension_stride (p,
true);
589 int stop = dimension_stop (p,
true);
595 oss <<
"Array/Grid hyperslab start point "<< start <<
596 " is greater than stop point " << stop <<
".";
597 throw Error(malformed_expr, oss.str());
604 count[id] = ((stop - start) / stride) + 1;
608 "=format_constraint():"
609 <<
"id=" <<
id <<
" offset=" << offset[
id]
610 <<
" step=" << step[
id]
611 <<
" count=" << count[
id]