32#include "config_hdf5.h"
38#include <libdap/InternalErr.h>
47BaseType *HDFEOS5CFMissLLArray::ptr_duplicate()
52bool HDFEOS5CFMissLLArray::read()
55 BESDEBUG(
"h5",
"Coming to HDFEOS5CFMissLLArray read "<<endl);
56 if(
nullptr == HDF5RequestHandler::get_lrdata_mem_cache())
57 read_data_NOT_from_mem_cache(
false,
nullptr);
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.empty()) ||
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,
false);
84 handle_data_with_mem_cache(H5FLOAT32,(
size_t)xdimsize,cache_flag,cache_key,
false);
87 read_data_NOT_from_mem_cache(
false,
nullptr);
92void 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.data(), step.data(), count.data());
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;
152 for (
int k = j = 0; j < ydimsize; ++j) {
153 for (
int i = 0; i < xdimsize; ++i) {
160 BESDEBUG(
"h5",
" Before calling GDij2ll, check all projection parameters. " << endl);
161 BESDEBUG(
"h5",
" eos5_projcode is " << eos5_projcode <<endl);
162 BESDEBUG(
"h5",
" eos5_zone is " << eos5_zone <<endl);
163 BESDEBUG(
"h5",
" eos5_params[0] is " << eos5_params[0] <<endl);
164 BESDEBUG(
"h5",
" eos5_params[1] is " << eos5_params[1] <<endl);
165 BESDEBUG(
"h5",
" eos5_sphere is " << eos5_sphere <<endl);
166 BESDEBUG(
"h5",
" xdimsize is " << xdimsize <<endl);
167 BESDEBUG(
"h5",
" ydimsize is " << ydimsize <<endl);
168 BESDEBUG(
"h5",
" eos5_pixelreg is " << eos5_pixelreg <<endl);
169 BESDEBUG(
"h5",
" eos5_origin is " << eos5_origin <<endl);
170 BESDEBUG(
"h5",
" upleft[0] is " << upleft[0] <<endl);
171 BESDEBUG(
"h5",
" upleft[1] is " << upleft[1] <<endl);
172 BESDEBUG(
"h5",
" lowright[0] is " << lowright[0] <<endl);
173 BESDEBUG(
"h5",
" lowright[1] is " << lowright[1] <<endl);
176 cerr<<
" eos5_params[0] is " << eos5_params[0] <<endl;
177 cerr<<
" eos5_params[1] is " << eos5_params[1] <<endl;
178 cerr<<
" eos5_sphere is " << eos5_sphere <<endl;
179 cerr<<
" eos5_zone is " << eos5_zone <<endl;
180 cerr<<
" Before calling GDij2ll, check all projection parameters. " << endl;
181 cerr<<
" eos5_zone is " << eos5_zone <<endl;
182 cerr<<
" eos5_params[0] is " << eos5_params[0] <<endl;
183 cerr<<
" eos5_params[1] is " << eos5_params[1] <<endl;
184 BESDEBUG(
"h5",
" xdimsize is " << xdimsize <<endl);
185 BESDEBUG(
"h5",
" ydimsize is " << ydimsize <<endl);
186 BESDEBUG(
"h5",
" eos5_pixelreg is " << eos5_pixelreg <<endl);
187 BESDEBUG(
"h5",
" eos5_origin is " << eos5_origin <<endl);
188 BESDEBUG(
"h5",
" upleft[0] is " << upleft[0] <<endl);
189 BESDEBUG(
"h5",
" upleft[1] is " << upleft[1] <<endl);
190 BESDEBUG(
"h5",
" lowright[0] is " << lowright[0] <<endl);
191 BESDEBUG(
"h5",
" lowright[1] is " << lowright[1] <<endl);
197 bool ll_read_from_cache =
true;
200 bool use_latlon_cache = HDF5RequestHandler::get_use_eosgeo_cachefile();
204 if(use_latlon_cache ==
true) {
208 string cache_fname = obtain_ll_cache_name();
211 long ll_disk_cache_size = HDF5RequestHandler::get_latlon_disk_cache_size();
212 string ll_disk_cache_dir = HDF5RequestHandler::get_latlon_disk_cache_dir();
213 string ll_disk_cache_prefix = HDF5RequestHandler::get_latlon_disk_cachefile_prefix();
216 int expected_file_size = 2*xdimsize*ydimsize*
sizeof(double);
219 ll_read_from_cache = ll_cache->get_data_from_cache(cache_fname, expected_file_size,fd);
221 if(ll_read_from_cache ==
true) {
223 BESDEBUG(
"h5",
" Read latitude and longitude from a disk cache. " <<endl);
224 size_t var_offset = 0;
226 if(CV_LON_MISS == cvartype)
227 var_offset = xdimsize*ydimsize*
sizeof(double);
229 vector<double> var_value;
230 var_value.resize(xdimsize*ydimsize);
235 off_t fpos = lseek(fd,var_offset,SEEK_SET);
237 throw InternalErr (__FILE__, __LINE__,
238 "Cannot seek the cached file offset.");
242 ll_cache->unlock_and_close(cache_fname);
245 if((-1 == ret_val) || ((
size_t)ret_val != (xdimsize*ydimsize*
sizeof(
double)))) {
246 ll_cache->purge_file(cache_fname);
247 ll_read_from_cache =
false;
251 if(total_elms == nelms)
252 set_value((dods_float64 *)var_value.data(),total_elms);
265 set_value((dods_float64 *)val.data(),nelms);
271 ll_read_from_cache =
false;
275 r = GDij2ll (eos5_projcode, eos5_zone, eos5_params.data(), eos5_sphere, xdimsize, ydimsize, upleft, lowright,
276 xdimsize * ydimsize, rows.data(), cols.data(), lon.data(), lat.data(), eos5_pixelreg, eos5_origin);
279 eherr <<
"cannot calculate grid latitude and longitude";
280 throw InternalErr (__FILE__, __LINE__, eherr.str ());
287 if(use_latlon_cache ==
true && ll_read_from_cache ==
false) {
288 string cache_fname = obtain_ll_cache_name();
289 long ll_disk_cache_size = HDF5RequestHandler::get_latlon_disk_cache_size();
290 string ll_disk_cache_dir = HDF5RequestHandler::get_latlon_disk_cache_dir();
291 string ll_disk_cache_prefix = HDF5RequestHandler::get_latlon_disk_cachefile_prefix();
293 BESDEBUG(
"h5",
" Write EOS5 grid latitude and longitude to a disk cache. " <<endl);
298 vector <double>latlon;
299 latlon.reserve(xdimsize*ydimsize*2);
300 latlon.insert(latlon.end(),lat.begin(),lat.end());
301 latlon.insert(latlon.end(),lon.begin(),lon.end());
302 ll_cache->write_cached_data(cache_fname,2*xdimsize*ydimsize*
sizeof(
double),latlon);
305 BESDEBUG(
"h5",
" The first value of lon is " << lon[0] <<endl);
306 BESDEBUG(
"h5",
" The first value of lat is " << lat[0] <<endl);
309 vector<size_t>pos(rank,0);
310 for (
int i = 0; i< rank; i++)
313 vector<size_t>dimsizes;
314 dimsizes.push_back(ydimsize);
315 dimsizes.push_back(xdimsize);
316 int total_elms = xdimsize*ydimsize;
320 if(CV_LON_MISS == cvartype) {
321 if(total_elms == nelms)
322 set_value((dods_float64 *)lon.data(),total_elms);
335 set_value((dods_float64 *)val.data(),nelms);
339 else if(CV_LAT_MISS == cvartype) {
341 if(total_elms == nelms)
342 set_value((dods_float64 *)lat.data(),total_elms);
355 set_value((dods_float64 *)val.data(),nelms);
362string HDFEOS5CFMissLLArray::obtain_ll_cache_name() {
364 BESDEBUG(
"h5",
"Coming to obtain_ll_cache_name "<<endl);
369 string bescachedir = HDF5RequestHandler::get_latlon_disk_cache_dir();
370 string bescacheprefix = HDF5RequestHandler::get_latlon_disk_cachefile_prefix();
371 long cachesize = HDF5RequestHandler::get_latlon_disk_cache_size();
373 if((
"" == bescachedir)||(
""==bescacheprefix)||(cachesize <=0)){
374 throw InternalErr (__FILE__, __LINE__,
"Either the cached dir is empty or the prefix is nullptr or the cache size is not set.");
378 if(stat(bescachedir.c_str(),&sb) !=0) {
379 string err_mesg=
"The cached directory " + bescachedir;
380 err_mesg = err_mesg +
" doesn't exist. ";
381 throw InternalErr(__FILE__,__LINE__,err_mesg);
385 if(
true == S_ISDIR(sb.st_mode)) {
386 if(access(bescachedir.c_str(),R_OK|W_OK|X_OK) == -1) {
387 string err_mesg=
"The cached directory " + bescachedir;
388 err_mesg = err_mesg +
" can NOT be read,written or executable.";
389 throw InternalErr(__FILE__,__LINE__,err_mesg);
393 string err_mesg=
"The cached directory " + bescachedir;
394 err_mesg = err_mesg +
" is not a directory.";
395 throw InternalErr(__FILE__,__LINE__,err_mesg);
400 string cache_fname=HDF5RequestHandler::get_latlon_disk_cachefile_prefix();
403 cache_fname +=HDF5CFUtil::get_int_str(eos5_projcode);
404 cache_fname +=HDF5CFUtil::get_int_str(eos5_zone);
405 cache_fname +=HDF5CFUtil::get_int_str(eos5_sphere);
406 cache_fname +=HDF5CFUtil::get_int_str(eos5_pixelreg);
407 cache_fname +=HDF5CFUtil::get_int_str(eos5_origin);
409 cache_fname +=HDF5CFUtil::get_int_str(ydimsize);
410 cache_fname +=HDF5CFUtil::get_int_str(xdimsize);
414 cache_fname +=HDF5CFUtil::get_double_str(point_left,17,6);
415 cache_fname +=HDF5CFUtil::get_double_str(point_upper,17,6);
416 cache_fname +=HDF5CFUtil::get_double_str(point_right,17,6);
417 cache_fname +=HDF5CFUtil::get_double_str(point_lower,17,6);
420 for(
int ipar = 0; ipar<13;ipar++) {
421 cache_fname+=HDF5CFUtil::get_double_str(eos5_params[ipar],17,6);
424 string cache_fpath = bescachedir +
"/"+ cache_fname;
428void HDFEOS5CFMissLLArray::read_data_NOT_from_mem_cache_geo(
bool add_cache,
void*buf){
430 BESDEBUG(
"h5",
"Coming to read_data_NOT_from_mem_cache_geo "<<endl);
438 throw InternalErr (__FILE__, __LINE__,
439 "The number of dimension of this variable should be greater than 0");
445 nelms = format_constraint (offset.data(), step.data(), count.data());
449 throw InternalErr (__FILE__, __LINE__,
450 "The number of elments is negative.");
459 if (CV_LAT_MISS == cvartype) {
461 if (HE5_HDFE_GD_UL == eos5_origin || HE5_HDFE_GD_UR == eos5_origin) {
474 throw InternalErr (__FILE__, __LINE__,
475 "The number of elments should be greater than 0.");
477 float lat_step = (end - start) /ydimsize;
480 if ( HE5_HDFE_CENTER == eos5_pixelreg ) {
481 for (
int i = 0; i < nelms; i++)
482 val[i] = ((offset[0]+i*step[0] + 0.5F) * lat_step + start) / 1000000.0F;
485 if(add_cache ==
true) {
486 vector<float>total_val;
487 total_val.resize(ydimsize);
488 for (
int total_i = 0; total_i < ydimsize; total_i++)
489 total_val[total_i] = ((total_i + 0.5F) * lat_step + start) / 1000000.0F;
491 memcpy(buf,total_val.data(),4*ydimsize);
496 for (
int i = 0; i < nelms; i++)
497 val[i] = ((
float)(offset[0]+i * step[0])*lat_step + start) / 1000000.0F;
500 if(add_cache ==
true) {
501 vector<float>total_val;
502 total_val.resize(ydimsize);
503 for (
int total_i = 0; total_i < ydimsize; total_i++)
504 total_val[total_i] = ((
float)(total_i) * lat_step + start) / 1000000.0F;
506 memcpy(buf,total_val.data(),4*ydimsize);
512 if (CV_LON_MISS == cvartype) {
514 if (HE5_HDFE_GD_UL == eos5_origin || HE5_HDFE_GD_LL == eos5_origin) {
527 throw InternalErr (__FILE__, __LINE__,
528 "The number of elments should be greater than 0.");
529 float lon_step = (end - start) /xdimsize;
531 if (HE5_HDFE_CENTER == eos5_pixelreg) {
532 for (
int i = 0; i < nelms; i++)
533 val[i] = ((offset[0] + i *step[0] + 0.5F) * lon_step + start ) / 1000000.0F;
536 if(add_cache ==
true) {
537 vector<float>total_val;
538 total_val.resize(xdimsize);
539 for (
int total_i = 0; total_i < xdimsize; total_i++)
540 total_val[total_i] = ((total_i+0.5F) * lon_step + start) / 1000000.0F;
542 memcpy(buf,total_val.data(),4*xdimsize);
547 for (
int i = 0; i < nelms; i++)
548 val[i] = ((
float)(offset[0]+i*step[0]) * lon_step + start) / 1000000.0F;
551 if(add_cache ==
true) {
552 vector<float>total_val;
553 total_val.resize(xdimsize);
554 for (
int total_i = 0; total_i < xdimsize; total_i++)
555 total_val[total_i] = ((
float)(total_i) * lon_step + start) / 1000000.0F;
557 memcpy(buf,total_val.data(),4*xdimsize);
563for (
int i =0; i <nelms; i++)
564"h5",
"final data val "<< i <<
" is " << val[i] <<endl;
567 set_value ((dods_float32 *) val.data(), nelms);
577HDFEOS5CFMissLLArray::format_constraint (
int *offset,
int *step,
int *count)
582 Dim_iter p = dim_begin ();
584 while (p != dim_end ()) {
586 int start = dimension_start (p,
true);
587 int stride = dimension_stride (p,
true);
588 int stop = dimension_stop (p,
true);
594 oss <<
"Array/Grid hyperslab start point "<< start <<
595 " is greater than stop point " << stop <<
".";
596 throw Error(malformed_expr, oss.str());
603 count[id] = ((stop - start) / stride) + 1;
607 "=format_constraint():"
608 <<
"id=" <<
id <<
" offset=" << offset[
id]
609 <<
" step=" << step[
id]
610 <<
" count=" << count[
id]
include the entry functions to execute the handlers
This class specifies the retrieval of the missing lat/lon values for HDF-EOS5 products.
static HDF5DiskCache * get_instance(const long, const std::string &, const std::string &)
static ssize_t read_buffer_from_file(int fd, void *buf, size_t)
Getting a subset of a variable.