175 vector<int64_t>offset;
176 vector<int64_t>count;
178 vector<hsize_t> hoffset;
179 vector<hsize_t>hcount;
180 vector<hsize_t>hstep;
184 throw InternalErr (__FILE__, __LINE__,
185 "The number of dimension of the variable is <=0 for an array.");
191 hoffset.resize(rank);
194 nelms = format_constraint (offset.data(), step.data(), count.data());
195 for (
int i = 0; i <rank; i++) {
196 hoffset[i] = (hsize_t) offset[i];
197 hcount[i] = (hsize_t) count[i];
198 hstep[i] = (hsize_t) step[i];
202 BESDEBUG(
"h5",
"after format_constraint "<<endl);
209 bool data_from_disk_cache =
false;
210 bool data_to_disk_cache =
false;
213 bool use_disk_cache = valid_disk_cache();
217 if(
true == use_disk_cache) {
218 BESDEBUG(
"h5",
"Coming to use disk cache "<<endl);
221 unsigned long long disk_cache_size = HDF5RequestHandler::get_disk_cache_size();
222 string diskcache_dir = HDF5RequestHandler::get_disk_cache_dir();
223 string diskcache_prefix = HDF5RequestHandler::get_disk_cachefile_prefix();
225 string cache_fname=HDF5CFUtil::obtain_cache_fname(diskcache_prefix,filename,varname);
226 cache_fpath = diskcache_dir +
"/"+ cache_fname;
228 int64_t temp_total_elems = 1;
229 for (
const auto &dimsize:dimsizes)
230 temp_total_elems = temp_total_elems*dimsize;
232 short dtype_size = HDF5CFUtil::H5_numeric_atomic_type_size(dtype);
234 if(is_dap4 ==
false && dtype==H5CHAR)
237 int64_t expected_file_size = dtype_size *temp_total_elems;
240 if(
true == disk_cache->get_data_from_cache(cache_fpath, expected_file_size,fd)) {
242 vector<size_t> offset_size_t;
243 offset_size_t.resize(rank);
244 for(
int i = 0; i <rank;i++)
245 offset_size_t[i] = (
size_t)offset[i];
246 size_t offset_1st = INDEX_nD_TO_1D(dimsizes,offset_size_t);
249 for (
int i = 0; i < rank; i++)
250 end[i] = offset[i] +(count[i]-1)*step[i];
251 size_t offset_last = INDEX_nD_TO_1D(dimsizes,end);
256 size_t total_read = dtype_size*(offset_last-offset_1st+1);
258 off_t fpos = lseek(fd,dtype_size*offset_1st,SEEK_SET);
266 data_from_disk_cache = obtain_cached_data(disk_cache,cache_fpath,fd, step,count,total_read,dtype_size);
270 if(
true == data_from_disk_cache)
273 data_to_disk_cache =
true;
279 bool pass_fileid = HDF5RequestHandler::get_pass_fileid();
280 if(
false == pass_fileid) {
281 if ((fileid = H5Fopen(filename.c_str(),H5F_ACC_RDONLY,H5P_DEFAULT))<0) {
283 eherr <<
"HDF5 File " << filename
284 <<
" cannot be opened. "<<endl;
285 throw InternalErr (__FILE__, __LINE__, eherr.str ());
289 if ((dsetid = H5Dopen(fileid,varname.c_str(),H5P_DEFAULT))<0) {
290 HDF5CFUtil::close_fileid(fileid,pass_fileid);
292 eherr <<
"HDF5 dataset " << varname
293 <<
" cannot be opened. "<<endl;
294 throw InternalErr (__FILE__, __LINE__, eherr.str ());
297 if ((dspace = H5Dget_space(dsetid))<0) {
300 HDF5CFUtil::close_fileid(fileid,pass_fileid);
302 eherr <<
"Space id of the HDF5 dataset " << varname
303 <<
" cannot be obtained. "<<endl;
304 throw InternalErr (__FILE__, __LINE__, eherr.str ());
307 if (H5Sselect_hyperslab(dspace, H5S_SELECT_SET,
308 hoffset.data(), hstep.data(),
309 hcount.data(),
nullptr) < 0) {
313 HDF5CFUtil::close_fileid(fileid,pass_fileid);
315 eherr <<
"The selection of hyperslab of the HDF5 dataset " << varname
317 throw InternalErr (__FILE__, __LINE__, eherr.str ());
320 mspace = H5Screate_simple(rank, hcount.data(),
nullptr);
324 HDF5CFUtil::close_fileid(fileid,pass_fileid);
326 eherr <<
"The creation of the memory space of the HDF5 dataset " << varname
328 throw InternalErr (__FILE__, __LINE__, eherr.str ());
332 if ((dtypeid = H5Dget_type(dsetid)) < 0) {
337 HDF5CFUtil::close_fileid(fileid,pass_fileid);
339 eherr <<
"Obtaining the datatype of the HDF5 dataset " << varname
341 throw InternalErr (__FILE__, __LINE__, eherr.str ());
345 if ((memtype = H5Tget_native_type(dtypeid, H5T_DIR_ASCEND))<0) {
351 HDF5CFUtil::close_fileid(fileid,pass_fileid);
353 eherr <<
"Obtaining the memory type of the HDF5 dataset " << varname
355 throw InternalErr (__FILE__, __LINE__, eherr.str ());
363 if(
true == add_mem_cache) {
369 HDF5CFUtil::close_fileid(fileid,pass_fileid);
370 throw InternalErr(__FILE__,__LINE__,
"The memory data cache buffer needs to be set");
372 read_ret= H5Dread(dsetid,memtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf);
378 HDF5CFUtil::close_fileid(fileid,pass_fileid);
379 throw InternalErr(__FILE__,__LINE__,
"Cannot read the data to the buffer.");
395 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,val.data());
403 HDF5CFUtil::close_fileid(fileid,pass_fileid);
405 eherr <<
"Cannot read the HDF5 dataset " << varname
406 <<
" with the type of H5T_NATIVE_CHAR "<<endl;
407 throw InternalErr (__FILE__, __LINE__, eherr.str ());
412 set_value_ll((dods_int8 *)val.data(),nelms);
416 newval.resize(nelms);
418 for (int64_t counter = 0; counter < nelms; counter++)
419 newval[counter] = (
short) (val[counter]);
421 set_value_ll(newval.data(), nelms);
424 if(
true == data_to_disk_cache) {
426 BESDEBUG(
"h5",
"writing data to disk cache "<<endl);
427 write_data_to_cache(dsetid,dspace,mspace,memtype,cache_fpath,2,val,nelms);
435 HDF5CFUtil::close_fileid(fileid,pass_fileid);
437 eherr <<
"write data to cache failed.";
438 throw InternalErr (__FILE__, __LINE__, eherr.str ());
459 size_t dtype_size = HDF5CFUtil::H5_numeric_atomic_type_size(dtype);
461 val.resize(nelms*dtype_size);
463 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,val.data());
470 HDF5CFUtil::close_fileid(fileid,pass_fileid);
472 eherr <<
"Cannot read the HDF5 dataset " << varname
473 <<
" with the type of H5T_NATIVE_UCHAR "<<endl;
474 throw InternalErr (__FILE__, __LINE__, eherr.str ());
478 BESDEBUG(
"h5",
"after H5Dread "<<endl);
480 BESDEBUG(
"h5",
"after val2buf "<<endl);
483 if(
true == data_to_disk_cache) {
484 BESDEBUG(
"h5",
"writing data to disk cache "<<endl);
486 write_data_to_cache(dsetid,dspace,mspace,memtype,cache_fpath,dtype_size,val,nelms);
494 HDF5CFUtil::close_fileid(fileid,pass_fileid);
496 eherr <<
"Write data to cache failed."
497 <<
"It is very possible the error is caused by the server failure"
498 <<
" such as filled disk partition at the server rather than Hyrax. Please contact "
499 <<
" the corresponding data center first. If the issue is not due to "
501 throw InternalErr (__FILE__, __LINE__, eherr.str ());
517 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,val.data());
525 HDF5CFUtil::close_fileid(fileid,pass_fileid);
528 eherr <<
"Cannot read the HDF5 dataset " << varname
529 <<
" with the type of H5T_NATIVE_SHORT "<<endl;
530 throw InternalErr (__FILE__, __LINE__, eherr.str ());
533 set_value ((dods_int16 *) val.data(), nelms);
540 vector<unsigned short> val;
542 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,val.data());
550 HDF5CFUtil::close_fileid(fileid,pass_fileid);
552 eherr <<
"Cannot read the HDF5 dataset " << varname
553 <<
" with the type of H5T_NATIVE_USHORT "<<endl;
554 throw InternalErr (__FILE__, __LINE__, eherr.str ());
557 set_value ((dods_uint16 *) val.data(), nelms);
566 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,val.data());
573 HDF5CFUtil::close_fileid(fileid,pass_fileid);
575 eherr <<
"Cannot read the HDF5 dataset " << varname
576 <<
" with the type of H5T_NATIVE_INT "<<endl;
577 throw InternalErr (__FILE__, __LINE__, eherr.str ());
580 set_value ((dods_int32 *) val.data(), nelms);
586 vector<unsigned int>val;
588 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,val.data());
595 HDF5CFUtil::close_fileid(fileid,pass_fileid);
597 eherr <<
"Cannot read the HDF5 dataset " << varname
598 <<
" with the type of H5T_NATIVE_UINT "<<endl;
599 throw InternalErr (__FILE__, __LINE__, eherr.str ());
602 set_value ((dods_uint32 *) val.data(), nelms);
612 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,val.data());
619 HDF5CFUtil::close_fileid(fileid,pass_fileid);
621 eherr <<
"Cannot read the HDF5 dataset " << varname
622 <<
" with the type of H5T_NATIVE_FLOAT "<<endl;
623 throw InternalErr (__FILE__, __LINE__, eherr.str ());
626 set_value ((dods_float32 *) val.data(), nelms);
636 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,val.data());
644 HDF5CFUtil::close_fileid(fileid,pass_fileid);
646 eherr <<
"Cannot read the HDF5 dataset " << varname
647 <<
" with the type of H5T_NATIVE_DOUBLE "<<endl;
648 throw InternalErr (__FILE__, __LINE__, eherr.str ());
651 set_value ((dods_float64 *) val.data(), nelms);
659 size_t ty_size = H5Tget_size(dtypeid);
666 HDF5CFUtil::close_fileid(fileid,pass_fileid);
668 eherr <<
"Cannot obtain the size of the fixed size HDF5 string of the dataset "
670 throw InternalErr (__FILE__, __LINE__, eherr.str ());
673 vector <char> strval;
674 strval.resize(nelms*ty_size);
675 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,(
void*)strval.data());
683 HDF5CFUtil::close_fileid(fileid,pass_fileid);
685 eherr <<
"Cannot read the HDF5 dataset " << varname
686 <<
" with the type of the fixed size HDF5 string "<<endl;
687 throw InternalErr (__FILE__, __LINE__, eherr.str ());
690 string total_string(strval.begin(),strval.end());
692 vector <string> finstrval;
693 finstrval.resize(nelms);
694 for (int64_t i = 0; i<nelms; i++)
695 finstrval[i] = total_string.substr(i*ty_size,ty_size);
702 if ((
true == HDF5RequestHandler::get_drop_long_string()) &&
703 ty_size > NC_JAVA_STR_SIZE_LIMIT) {
704 for (int64_t i = 0; i<nelms; i++)
707 set_value_ll(finstrval,nelms);
708 total_string.clear();
715 size_t ty_size = H5Tget_size(memtype);
722 HDF5CFUtil::close_fileid(fileid,pass_fileid);
724 eherr <<
"Cannot obtain the size of the fixed size HDF5 string of the dataset "
726 throw InternalErr (__FILE__, __LINE__, eherr.str ());
728 vector <char> strval;
729 strval.resize(nelms*ty_size);
730 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,(
void*)strval.data());
738 HDF5CFUtil::close_fileid(fileid,pass_fileid);
740 eherr <<
"Cannot read the HDF5 dataset " << varname
741 <<
" with the type of the HDF5 variable length string "<<endl;
742 throw InternalErr (__FILE__, __LINE__, eherr.str ());
745 vector<string>finstrval;
746 finstrval.resize(nelms);
747 char*temp_bp = strval.data();
748 char*onestring =
nullptr;
749 for (int64_t i =0;i<nelms;i++) {
750 onestring = *(
char**)temp_bp;
751 if(onestring!=
nullptr )
752 finstrval[i] =string(onestring);
759 if (
false == strval.empty()) {
760 herr_t ret_vlen_claim;
761 ret_vlen_claim = H5Dvlen_reclaim(memtype,mspace,H5P_DEFAULT,(
void*)strval.data());
762 if (ret_vlen_claim < 0){
768 HDF5CFUtil::close_fileid(fileid,pass_fileid);
770 eherr <<
"Cannot reclaim the memory buffer of the HDF5 variable length string of the dataset "
772 throw InternalErr (__FILE__, __LINE__, eherr.str ());
780 if (
true == HDF5RequestHandler::get_drop_long_string()) {
781 bool drop_long_str =
false;
782 for (int64_t i =0;i<nelms;i++) {
783 if(finstrval[i].size() >NC_JAVA_STR_SIZE_LIMIT){
784 drop_long_str =
true;
788 if (drop_long_str ==
true) {
789 for (int64_t i =0;i<nelms;i++)
793 set_value_ll(finstrval,nelms);
805 HDF5CFUtil::close_fileid(fileid,pass_fileid);
807 eherr <<
"Cannot read the HDF5 dataset " << varname
808 <<
" with the unsupported HDF5 datatype"<<endl;
809 throw InternalErr (__FILE__, __LINE__, eherr.str ());
818 HDF5CFUtil::close_fileid(fileid,pass_fileid);