34 #include <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);
115 void 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);
123 for (
int i = 0; i < strindex; i++) {
124 tempvalue = tempvalue + elesize;
127 strncpy(buf, tempvalue, elesize);
144 get_slabdata(hid_t dset,
int *offset,
int *step,
int *count,
int num_dim,
147 BESDEBUG(
"h5",
">get_slabdata() " << endl);
149 hid_t dtype = H5Dget_type(dset);
151 throw InternalErr(__FILE__, __LINE__,
"could not get data type");
154 hid_t memtype = H5Tget_native_type(dtype, H5T_DIR_ASCEND);
157 throw InternalErr(__FILE__, __LINE__,
"could not get memory type");
160 hid_t dspace = H5Dget_space(dset);
164 throw InternalErr(__FILE__, __LINE__,
"could not get data space");
168 vector<hsize_t>dyn_count;
169 vector<hsize_t>dyn_step;
170 vector<hssize_t>dyn_offset;
171 dyn_count.resize(num_dim);
172 dyn_step.resize(num_dim);
173 dyn_offset.resize(num_dim);
175 for (
int i = 0; i < num_dim; i++) {
176 dyn_count[i] = (hsize_t) (*count);
177 dyn_step[i] = (hsize_t) (*step);
178 dyn_offset[i] = (hssize_t) (*offset);
180 "count:" << dyn_count[i]
181 <<
" step:" << dyn_step[i]
182 <<
" offset:" << dyn_step[i]
189 if (H5Sselect_hyperslab(dspace, H5S_SELECT_SET,
190 (
const hsize_t *)&dyn_offset[0], &dyn_step[0],
191 &dyn_count[0], NULL) < 0) {
195 throw InternalErr(__FILE__, __LINE__,
"could not select hyperslab");
198 hid_t memspace = H5Screate_simple(num_dim, &dyn_count[0], NULL);
203 throw InternalErr(__FILE__, __LINE__,
"could not open space");
206 if (H5Dread(dset, memtype, memspace, dspace, H5P_DEFAULT,
212 throw InternalErr(__FILE__, __LINE__,
"could not get data");
215 if (H5Sclose(dspace) < 0){
219 throw InternalErr(__FILE__, __LINE__,
"Unable to close the dspace.");
221 if (H5Sclose(memspace) < 0){
224 throw InternalErr(__FILE__, __LINE__,
"Unable to close the memspace.");
226 if (H5Tclose(dtype) < 0){
228 throw InternalErr(__FILE__, __LINE__,
"Unable to close the dtype.");
231 if (H5Tclose(memtype) < 0){
232 throw InternalErr(__FILE__, __LINE__,
"Unable to close the memtype.");
235 BESDEBUG(
"h5",
"<get_slabdata() " << endl);
239 bool read_vlen_string(hid_t dsetid,
int nelms, hsize_t *hoffset, hsize_t *hstep, hsize_t *hcount,vector<string> &finstrval)
246 bool is_scalar =
false;
249 if ((dspace = H5Dget_space(dsetid))<0) {
250 throw InternalErr (__FILE__, __LINE__,
"Cannot obtain data space.");
253 if(H5S_SCALAR == H5Sget_simple_extent_type(dspace))
257 if (
false == is_scalar) {
258 if (H5Sselect_hyperslab(dspace, H5S_SELECT_SET,
262 throw InternalErr (__FILE__, __LINE__,
"Cannot generate the hyperslab of the HDF5 dataset.");
265 int d_num_dim = H5Sget_simple_extent_ndims(dspace);
268 throw InternalErr (__FILE__, __LINE__,
"Cannot obtain the number of dimensions of the data space.");
271 mspace = H5Screate_simple(d_num_dim, hcount,NULL);
274 throw InternalErr (__FILE__, __LINE__,
"Cannot create the memory space.");
279 if ((dtypeid = H5Dget_type(dsetid)) < 0) {
281 if (
false == is_scalar)
284 throw InternalErr (__FILE__, __LINE__,
"Cannot obtain the datatype.");
288 if ((memtype = H5Tget_native_type(dtypeid, H5T_DIR_ASCEND))<0) {
290 if (
false == is_scalar)
294 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain memory datatype.");
298 size_t ty_size = H5Tget_size(memtype);
300 if (
false == is_scalar)
305 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain the size of HDF5 string.");
308 vector <char> strval;
309 strval.resize(nelms*ty_size);
311 if (
true == is_scalar)
312 read_ret = H5Dread(dsetid,memtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,(
void*)&strval[0]);
314 read_ret = H5Dread(dsetid,memtype,mspace,dspace,H5P_DEFAULT,(
void*)&strval[0]);
317 if (
false == is_scalar)
322 throw InternalErr (__FILE__, __LINE__,
"Fail to read the HDF5 variable length string dataset.");
326 char*temp_bp = &strval[0];
327 char*onestring = NULL;
328 for (
int i =0;i<nelms;i++) {
329 onestring = *(
char**)temp_bp;
331 finstrval[i] =string(onestring);
337 if (
false == strval.empty()) {
338 herr_t ret_vlen_claim;
339 if (
true == is_scalar)
340 ret_vlen_claim = H5Dvlen_reclaim(memtype,dspace,H5P_DEFAULT,(
void*)&strval[0]);
342 ret_vlen_claim = H5Dvlen_reclaim(memtype,mspace,H5P_DEFAULT,(
void*)&strval[0]);
343 if (ret_vlen_claim < 0){
344 if (
false == is_scalar)
349 throw InternalErr (__FILE__, __LINE__,
"Cannot reclaim the memory buffer of the HDF5 variable length string.");
354 if (
false == is_scalar)
364 bool promote_char_to_short(H5T_class_t type_cls, hid_t type_id) {
366 bool ret_value =
false;
367 if(type_cls == H5T_INTEGER) {
368 size_t size = H5Tget_size(type_id);
369 int sign = H5Tget_sign(type_id);
370 if(size == 1 && sign == H5T_SGN_2)
378 void get_vlen_str_data(
char*temp_bp,
string &finalstr_val) {
380 char*onestring = NULL;
381 onestring = *(
char**)temp_bp;
383 finalstr_val =string(onestring);