7#include "HDFSPArray_RealField.h"
11#include <libdap/debug.h>
14#include <libdap/InternalErr.h>
16#include "BESInternalError.h"
18#include "HDF4RequestHandler.h"
25#define SIGNED_BYTE_TO_INT32 1
32 BESDEBUG(
"h4",
"Coming to HDFSPArray_RealField read "<<endl);
45 int nelms = format_constraint (offset.data(), step.data(), count.data());
51 bool data_from_cache =
false;
52 bool data_to_cache =
false;
55 if (-1 == dtype_size) {
56 string err_mesg =
"Wrong data type size for the variable ";
58 throw InternalErr(__FILE__,__LINE__,err_mesg);
62 if (
true == HDF4RequestHandler::get_enable_data_cachefile()) {
71 string bescachedir = HDF4RequestHandler::get_cache_latlon_path();
72 string bescacheprefix = HDF4RequestHandler::get_cache_latlon_prefix();
73 long cachesize = HDF4RequestHandler::get_cache_latlon_size();
75 if ((
"" == bescachedir)||(
""==bescacheprefix)||(cachesize <=0))
76 throw InternalErr (__FILE__, __LINE__,
"Either the cached dir is empty or the prefix is NULL or the cache size is not set.");
79 if (stat(bescachedir.c_str(),&sb) !=0) {
80 string err_mesg=
"The cached directory " + bescachedir;
81 err_mesg = err_mesg +
" doesn't exist. ";
82 throw InternalErr(__FILE__,__LINE__,err_mesg);
85 if (
true == S_ISDIR(sb.st_mode)) {
86 if (access(bescachedir.c_str(),R_OK|W_OK|X_OK) == -1) {
87 string err_mesg=
"The cached directory " + bescachedir;
88 err_mesg = err_mesg +
" can NOT be read,written or executable.";
89 throw InternalErr(__FILE__,__LINE__,err_mesg);
93 string err_mesg=
"The cached directory " + bescachedir;
94 err_mesg = err_mesg +
" is not a directory.";
95 throw InternalErr(__FILE__,__LINE__,err_mesg);
100 string cache_fname=HDFCFUtil::obtain_cache_fname(bescacheprefix,filename,name());
101 cache_fpath = bescachedir +
"/"+ cache_fname;
104 for (
unsigned int i = 0; i <dimsizes.size();i++)
105 total_elems = total_elems*dimsizes[i];
107 if (-1 == dtype_size) {
108 string err_mesg =
"Wrong data type size for the variable ";
110 throw InternalErr(__FILE__,__LINE__,err_mesg);
112 int expected_file_size = dtype_size *total_elems;
114 if (
true == llcache->get_data_from_cache(cache_fpath, expected_file_size,fd)) {
116 vector<int32>offset32;
117 offset32.resize(offset.size());
118 for (
int i = 0; i< rank;i++)
119 offset32[i] = offset[i];
120 int offset_1st = INDEX_nD_TO_1D(dimsizes,offset32);
123 for (
int i = 0; i < rank; i++)
124 end[i] = offset[i] +(count[i]-1)*step[i];
125 int offset_last = INDEX_nD_TO_1D(dimsizes,end);
126 size_t total_read = dtype_size*(offset_last-offset_1st+1);
128 off_t fpos = lseek(fd,dtype_size*offset_1st,SEEK_SET);
136 data_from_cache = obtain_cached_data(llcache,cache_fpath,fd, step,count,total_read,dtype_size);
140 if (
true == data_from_cache)
143 data_to_cache =
true;
147 bool check_pass_fileid_key = HDF4RequestHandler::get_pass_fileid();
150 vector<int32>offset32;
151 offset32.resize(rank);
152 vector<int32>count32;
153 count32.resize(rank);
158 for (
int i = 0; i < rank; i++) {
159 offset32[i] = (int32) offset[i];
160 count32[i] = (int32) count[i];
161 step32[i] = (int32) step[i];
167 if (
false == check_pass_fileid_key) {
168 sdid = SDstart (
const_cast < char *
>(filename.c_str ()), DFACC_READ);
171 eherr <<
"File " << filename.c_str () <<
" cannot be open.";
172 throw InternalErr (__FILE__, __LINE__, eherr.str ());
179 if (
true == HDF4RequestHandler::get_enable_metadata_cachefile()) {
181 sdid = SDstart (
const_cast < char *
>(filename.c_str ()), DFACC_READ);
184 eherr <<
"File " << filename.c_str () <<
" cannot be open.";
185 throw InternalErr (__FILE__, __LINE__, eherr.str ());
188 check_pass_fileid_key =
false;
199 int32 sdsindex = SDreftoindex (sdid, (int32) fieldref);
200 if (sdsindex == -1) {
203 eherr <<
"SDS index " << sdsindex <<
" is not right.";
204 throw InternalErr (__FILE__, __LINE__, eherr.str ());
208 sdsid = SDselect (sdid, sdsindex);
212 eherr <<
"SDselect failed.";
213 throw InternalErr (__FILE__, __LINE__, eherr.str ());
226 r = SDreaddata (sdsid, offset32.data(), step32.data(), count32.data(), buf.data());
231 eherr <<
"SDreaddata failed.";
232 throw InternalErr (__FILE__, __LINE__, eherr.str ());
235#ifndef SIGNED_BYTE_TO_INT32
240 newval.resize(nelms);
241 for (
int counter = 0; counter < nelms; counter++)
242 newval[counter] = (int32) (buf[counter]);
244 set_value ((dods_int32 *) newval.data(), nelms);
246 if (
true == data_to_cache) {
248 write_data_to_cache(sdsid,cache_fpath,dtype_size,buf,nelms);
254 eherr <<
"write data to cache failed.";
255 throw InternalErr (__FILE__, __LINE__, eherr.str ());
289 buf.resize(nelms*dtype_size);
291 r = SDreaddata (sdsid, offset32.data(), step32.data(), count32.data(), buf.data());
296 eherr <<
"SDreaddata failed";
297 throw InternalErr (__FILE__, __LINE__, eherr.str ());
300 if((basename(filename).size() >=7) && ((basename(filename)).compare(0,7,
"MCD43GF")==0)) {
301 if (fieldname==
"Latitude" && dtype == DFNT_FLOAT32) {
303 char *temp_buf = buf.data()+nelms*dtype_size-dtype_size;
304 float last_val = *((
float*)(temp_buf));
305 if (last_val <-90.0) {
307 vector<char>last_val_buf(dtype_size);
308 memcpy(last_val_buf.data(),(
void*)&last_val,dtype_size);
309 for (
unsigned int i = 0; i<dtype_size;i++)
310 buf[i+nelms*dtype_size-dtype_size] = last_val_buf[i];
313 else if (fieldname==
"Longitude" && dtype == DFNT_FLOAT32) {
314 char *temp_buf = buf.data()+nelms*dtype_size-dtype_size;
315 float last_val = *((
float*)(temp_buf));
316 if (last_val > 180.0) {
318 vector<char>last_val_buf(dtype_size);
319 memcpy(last_val_buf.data(),(
void*)&last_val,dtype_size);
320 for (
unsigned int i = 0; i<dtype_size;i++)
321 buf[i+nelms*dtype_size-dtype_size] = last_val_buf[i];
332 if (
true == data_to_cache) {
334 write_data_to_cache(sdsid,cache_fpath,dtype_size,buf,nelms);
340 eherr <<
"write data to cache failed.";
341 throw InternalErr (__FILE__, __LINE__, eherr.str ());
350 throw InternalErr (__FILE__, __LINE__,
"unsupported data type.");
354 r = SDendaccess (sdsid);
357 eherr <<
"SDendaccess failed.";
358 throw InternalErr (__FILE__, __LINE__, eherr.str ());
365bool HDFSPArray_RealField::obtain_cached_data(
BESH4Cache *llcache,
const string & cache_fpath,
int fd,vector<int> &cd_step, vector<int>&cd_count,
size_t total_read,
short dtype_size) {
367 ssize_t ret_read_val = -1;
369 buf.resize(total_read);
370 ret_read_val = HDFCFUtil::read_buffer_from_file(fd,(
void*)buf.data(),total_read);
372 if ((-1 == ret_read_val) || (ret_read_val != (ssize_t)total_read)) {
377 unsigned int nele_to_read = 1;
378 for(
int i = 0; i<rank;i++)
379 nele_to_read *=cd_count[i];
381 if (nele_to_read == (total_read/dtype_size)) {
387 vector<int>cd_start(rank,0);
388 vector<int32>cd_pos(rank,0);
389 int nelms_to_send = 1;
390 for(
int i = 0; i <rank; i++)
391 nelms_to_send = nelms_to_send*cd_count[i];
397#ifndef SIGNED_BYTE_TO_INT32
398 vector<int8>total_val;
399 total_val.resize(total_read/dtype_size);
400 memcpy(total_val.data(),(
void*)buf.data(),total_read);
402 vector<int8>final_val;
415 set_value((dods_byte*)final_val.data(),nelms_to_send);
418 vector<int32>total_val2;
419 total_val2.resize(total_read/dtype_size);
420 memcpy(total_val2.data(),(
void*)buf.data(),total_read);
422 vector<int32>final_val2;
435 set_value((dods_int32*)final_val2.data(),nelms_to_send);
444 vector<uint8>total_val;
445 total_val.resize(total_read/dtype_size);
446 memcpy(total_val.data(),(
void*)buf.data(),total_read);
448 vector<uint8>final_val;
462 set_value ((dods_byte *) final_val.data(), nelms_to_send);
468 vector<int16>total_val;
469 total_val.resize(total_read/dtype_size);
470 memcpy(total_val.data(),(
void*)buf.data(),total_read);
472 vector<int16>final_val;
486 set_value ((dods_int16 *) final_val.data(), nelms_to_send);
492 vector<uint16>total_val;
493 total_val.resize(total_read/dtype_size);
494 memcpy(total_val.data(),(
void*)buf.data(),total_read);
496 vector<uint16>final_val;
510 set_value ((dods_uint16 *) final_val.data(), nelms_to_send);
516 vector<int32>total_val;
517 total_val.resize(total_read/dtype_size);
518 memcpy(total_val.data(),(
void*)buf.data(),total_read);
520 vector<int32>final_val;
534 set_value ((dods_int32 *) final_val.data(), nelms_to_send);
540 vector<uint32>total_val;
541 total_val.resize(total_read/dtype_size);
542 memcpy(total_val.data(),(
void*)buf.data(),total_read);
544 vector<uint32>final_val;
558 set_value ((dods_uint32 *) final_val.data(), nelms_to_send);
564 vector<float32>total_val;
565 total_val.resize(total_read/dtype_size);
566 memcpy(total_val.data(),(
void*)buf.data(),total_read);
568 vector<float32>final_val;
582 set_value ((dods_float32 *) final_val.data(), nelms_to_send);
587 vector<float64>total_val;
588 total_val.resize(total_read/dtype_size);
589 memcpy(total_val.data(),(
void*)buf.data(),total_read);
591 vector<float64>final_val;
604 set_value ((dods_float64 *) final_val.data(), nelms_to_send);
608 throw InternalErr (__FILE__, __LINE__,
"unsupported data type.");
617HDFSPArray_RealField::write_data_to_cache(int32 sdsid,
const string& cache_fpath,
short dtype_size,
const vector<char> &buf,
int nelms) {
620 vector<int32>woffset32(rank,0);
621 vector<int32>wstep32(rank,1);
622 vector<int32>wcount32;
623 wcount32.resize(rank);
625 for(
int i = 0; i <rank; i++){
626 wcount32[i] = (int32)dimsizes[i];
627 total_nelem = total_nelem*dimsizes[i];
630 if (DFNT_INT8 == dtype) {
632#ifndef SIGNED_BYTE_TO_INT32
633 if (total_nelem == nelms)
634 llcache->write_cached_data2(cache_fpath,dtype_size*total_nelem,(
const void*)buf.data());
636 val.resize(dtype_size*total_nelem);
637 if (SDreaddata (sdsid, woffset32.data(), wstep32.data(), wcount32.data(), val.data())<0)
638 throw InternalErr (__FILE__, __LINE__,
"Cannot read the whole SDS for cache.");
639 llcache->write_cached_data2(cache_fpath,dtype_size*total_nelem,(
const void*)val.data());
644 newval.resize(total_nelem);
645 if (total_nelem == nelms) {
646 for (
int i = 0; i < total_nelem;i++)
647 newval[i] = (
int)buf[i];
648 llcache->write_cached_data2(cache_fpath,dtype_size*total_nelem,(
const void*)newval.data());
652 val2.resize(total_nelem);
653 if (SDreaddata (sdsid, woffset32.data(), wstep32.data(), wcount32.data(), val2.data())<0)
654 throw InternalErr (__FILE__, __LINE__,
"Cannot read the whole SDS for cache.");
655 for (
int i = 0; i < total_nelem;i++)
656 newval[i] = (
int)val2[i];
657 llcache->write_cached_data2(cache_fpath,dtype_size*total_nelem,(
const void*)newval.data());
662 if (total_nelem == nelms) {
663 llcache->write_cached_data2(cache_fpath,dtype_size*total_nelem,(
const void*)buf.data());
666 val.resize(dtype_size*total_nelem);
667 if (SDreaddata (sdsid, woffset32.data(), wstep32.data(), wcount32.data(), val.data())<0)
668 throw InternalErr (__FILE__, __LINE__,
"Cannot read the whole SDS for cache.");
670 llcache->write_cached_data2(cache_fpath,dtype_size*total_nelem,(
const void*)val.data());
677HDFSPArray_RealField::format_constraint (
int *offset,
int *step,
int *count)
682 Dim_iter p = dim_begin ();
683 while (p != dim_end ()) {
685 int start = dimension_start (p,
true);
686 int stride = dimension_stride (p,
true);
687 int stop = dimension_stop (p,
true);
692 oss <<
"Array/Grid hyperslab start point "<< start <<
693 " is greater than stop point " << stop <<
".";
694 throw Error(malformed_expr, oss.str());
699 count[id] = ((stop - start) / stride) + 1;
703 "=format_constraint():"
704 <<
"id=" <<
id <<
" offset=" << offset[
id]
705 <<
" step=" << step[
id]
706 <<
" count=" << count[
id]
728template<
typename T>
int
729HDFSPArray_RealField::subset(
734 vector<int> & stride,
736 std::vector<T> *poutput,
740 for(
int k=0; k<edge[index]; k++)
742 pos[index] = start[index] + k*stride[index];
744 subset(input, sf_rank, dim, start, stride, edge, poutput,pos,index+1);
745 if (index==sf_rank-1)
747 poutput->push_back(input[INDEX_nD_TO_1D( dim, pos)]);
virtual void unlock_and_close(const std::string &target)
virtual void purge_file(const std::string &file)
Purge a single file from the cache.
static BESH4Cache * get_instance()
static short obtain_type_size(int32)
Obtain datatype size.
static void close_fileid(int32 sdfd, int32 file_id, int32 gridfd, int32 swathfd, bool pass_fileid_key)