34#include <libdap/InternalErr.h>
52 BESDEBUG(
"h5",
">get_data()" << endl);
55 if ((dtype = H5Dget_type(dset)) < 0) {
56 throw InternalErr(__FILE__, __LINE__,
"Failed to get the datatype of the dataset");
59 if ((dspace = H5Dget_space(dset)) < 0) {
61 throw InternalErr(__FILE__, __LINE__,
"Failed to get the data space of the dataset");
64 hid_t memtype = H5Tget_native_type(dtype, H5T_DIR_ASCEND);
68 throw InternalErr(__FILE__, __LINE__,
"failed to get memory type");
71 if (H5Dread(dset, memtype, dspace, dspace, H5P_DEFAULT, buf)
76 throw InternalErr(__FILE__, __LINE__,
"failed to read data");
79 if (H5Tclose(dtype) < 0){
82 throw InternalErr(__FILE__, __LINE__,
"Unable to release the dtype.");
85 if (H5Tclose(memtype) < 0){
87 throw InternalErr(__FILE__, __LINE__,
"Unable to release the memtype.");
90 if(H5Sclose(dspace)<0) {
91 throw InternalErr(__FILE__, __LINE__,
"Unable to release the data space.");
101 BESDEBUG(
"h5",
"<get_data()" << endl);
115void get_strdata(
int strindex,
char *allbuf,
char *buf,
int elesize)
117 char *tempvalue = allbuf;
119 BESDEBUG(
"h5",
">get_strdata(): "
120 <<
" strindex=" << strindex <<
" allbuf=" << allbuf << endl);
126 for (
int i = 0; i < strindex; i++) {
127 tempvalue = tempvalue + elesize;
130 tempvalue = tempvalue +strindex *elesize;
132 strncpy(buf, tempvalue, elesize);
149get_slabdata(hid_t dset,
const int *offset,
const int *step,
const int *count,
const int num_dim,
152 BESDEBUG(
"h5",
">get_slabdata() " << endl);
154 hid_t dtype = H5Dget_type(dset);
156 throw InternalErr(__FILE__, __LINE__,
"could not get data type");
159 hid_t memtype = H5Tget_native_type(dtype, H5T_DIR_ASCEND);
162 throw InternalErr(__FILE__, __LINE__,
"could not get memory type");
165 hid_t dspace = H5Dget_space(dset);
169 throw InternalErr(__FILE__, __LINE__,
"could not get data space");
173 vector<hsize_t>dyn_count;
174 vector<hsize_t>dyn_step;
175 vector<hssize_t>dyn_offset;
176 dyn_count.resize(num_dim);
177 dyn_step.resize(num_dim);
178 dyn_offset.resize(num_dim);
180 for (
int i = 0; i < num_dim; i++) {
181 dyn_count[i] = (hsize_t) (*count);
182 dyn_step[i] = (hsize_t) (*step);
183 dyn_offset[i] = (hssize_t) (*offset);
185 "count:" << dyn_count[i]
186 <<
" step:" << dyn_step[i]
187 <<
" offset:" << dyn_step[i]
194 if (H5Sselect_hyperslab(dspace, H5S_SELECT_SET,
195 (
const hsize_t *)dyn_offset.data(), dyn_step.data(),
196 dyn_count.data(),
nullptr) < 0) {
200 throw InternalErr(__FILE__, __LINE__,
"could not select hyperslab");
203 hid_t memspace = H5Screate_simple(num_dim, dyn_count.data(),
nullptr);
208 throw InternalErr(__FILE__, __LINE__,
"could not open space");
211 if (H5Dread(dset, memtype, memspace, dspace, H5P_DEFAULT,
217 throw InternalErr(__FILE__, __LINE__,
"could not get data");
220 if (H5Sclose(dspace) < 0){
224 throw InternalErr(__FILE__, __LINE__,
"Unable to close the dspace.");
226 if (H5Sclose(memspace) < 0){
229 throw InternalErr(__FILE__, __LINE__,
"Unable to close the memspace.");
231 if (H5Tclose(dtype) < 0){
233 throw InternalErr(__FILE__, __LINE__,
"Unable to close the dtype.");
236 if (H5Tclose(memtype) < 0){
237 throw InternalErr(__FILE__, __LINE__,
"Unable to close the memtype.");
240 BESDEBUG(
"h5",
"<get_slabdata() " << endl);
244bool read_vlen_string(hid_t dsetid,
const int nelms,
const hsize_t *hoffset,
const hsize_t *hstep,
const hsize_t *hcount,vector<string> &finstrval)
251 bool is_scalar =
false;
254 if ((dspace = H5Dget_space(dsetid))<0) {
255 throw InternalErr (__FILE__, __LINE__,
"Cannot obtain data space.");
258 if(H5S_SCALAR == H5Sget_simple_extent_type(dspace))
262 if (
false == is_scalar) {
263 if (H5Sselect_hyperslab(dspace, H5S_SELECT_SET,
265 hcount,
nullptr) < 0) {
267 throw InternalErr (__FILE__, __LINE__,
"Cannot generate the hyperslab of the HDF5 dataset.");
270 int d_num_dim = H5Sget_simple_extent_ndims(dspace);
273 throw InternalErr (__FILE__, __LINE__,
"Cannot obtain the number of dimensions of the data space.");
276 mspace = H5Screate_simple(d_num_dim, hcount,
nullptr);
279 throw InternalErr (__FILE__, __LINE__,
"Cannot create the memory space.");
284 if ((dtypeid = H5Dget_type(dsetid)) < 0) {
286 if (
false == is_scalar)
289 throw InternalErr (__FILE__, __LINE__,
"Cannot obtain the datatype.");
293 if ((memtype = H5Tget_native_type(dtypeid, H5T_DIR_ASCEND))<0) {
295 if (
false == is_scalar)
299 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain memory datatype.");
303 size_t ty_size = H5Tget_size(memtype);
305 if (
false == is_scalar)
310 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain the size of HDF5 string.");
313 vector <char> strval;
314 strval.resize(nelms*ty_size);
316 if (
true == is_scalar)
317 read_ret = H5Dread(dsetid,memtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,(
void*)strval.data());
319 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,(
void*)strval.data());
322 if (
false == is_scalar)
327 throw InternalErr (__FILE__, __LINE__,
"Fail to read the HDF5 variable length string dataset.");
331 char *temp_bp = strval.data();
332 for (
int i =0;i<nelms;i++) {
333 char *onestring = *(
char**)temp_bp;
334 if(onestring!=
nullptr )
335 finstrval[i] =string(onestring);
341 if (
false == strval.empty()) {
342 herr_t ret_vlen_claim;
343 if (
true == is_scalar)
344 ret_vlen_claim = H5Dvlen_reclaim(memtype,dspace,H5P_DEFAULT,(
void*)strval.data());
346 ret_vlen_claim = H5Dvlen_reclaim(memtype,mspace,H5P_DEFAULT,(
void*)strval.data());
347 if (ret_vlen_claim < 0){
348 if (
false == is_scalar)
353 throw InternalErr (__FILE__, __LINE__,
"Cannot reclaim the memory buffer of the HDF5 variable length string.");
358 if (
false == is_scalar)
368bool promote_char_to_short(H5T_class_t type_cls, hid_t type_id) {
370 bool ret_value =
false;
371 if(type_cls == H5T_INTEGER) {
372 size_t size = H5Tget_size(type_id);
373 int sign = H5Tget_sign(type_id);
374 if(size == 1 && sign == H5T_SGN_2)
382void get_vlen_str_data(
const char*temp_bp,
string &finalstr_val) {
384 char *onestring = *(
char**)temp_bp;
385 if(onestring!=
nullptr )
386 finalstr_val =string(onestring);
void get_data(hid_t dset, void *buf)
void get_strdata(int strindex, char *allbuf, char *buf, int elesize)