60 visit_obj_cb(hid_t o_id,
const char *name,
const H5O_info_t *oinfo,
64 static herr_t attr_info(hid_t loc_id,
const char *name,
const H5A_info_t *ainfo,
void *opdata);
84 bool *ignore_attr_ptr)
90 *ignore_attr_ptr =
false;
92 if ((attrid = H5Aopen_by_idx(dset,
".", H5_INDEX_CRT_ORDER, H5_ITER_INC,(hsize_t)index, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
93 string msg =
"unable to open attribute by index ";
94 throw InternalErr(__FILE__, __LINE__, msg);
98 ssize_t name_size = H5Aget_name(attrid, 0, NULL);
101 string msg =
"unable to obtain the size of the hdf5 attribute name ";
102 throw InternalErr(__FILE__, __LINE__, msg);
105 vector<char> attr_name;
106 attr_name.resize(name_size+1);
108 if ((H5Aget_name(attrid, name_size+1, &attr_name[0])) < 0) {
110 string msg =
"unable to obtain the hdf5 attribute name ";
111 throw InternalErr(__FILE__, __LINE__, msg);
116 if ((ty_id = H5Aget_type(attrid)) < 0) {
117 string msg =
"unable to obtain hdf5 datatype for the attribute ";
118 string attrnamestr(attr_name.begin(),attr_name.end());
121 throw InternalErr(__FILE__, __LINE__, msg);
124 H5T_class_t ty_class = H5Tget_class(ty_id);
126 string msg =
"cannot get hdf5 attribute datatype class for the attribute ";
127 string attrnamestr(attr_name.begin(),attr_name.end());
130 throw InternalErr(__FILE__, __LINE__, msg);
143 if ((ty_class == H5T_TIME) || (ty_class == H5T_BITFIELD)
144 || (ty_class == H5T_OPAQUE) || (ty_class == H5T_ENUM)
145 || (ty_class == H5T_REFERENCE) ||(ty_class == H5T_COMPOUND)
146 || (ty_class == H5T_VLEN) || (ty_class == H5T_ARRAY)){
148 *ignore_attr_ptr =
true;
155 if (
false == is_dap4) {
156 if((ty_class == H5T_INTEGER) && (H5Tget_size(ty_id)== 8)) {
157 *ignore_attr_ptr =
true;
163 hid_t aspace_id = -1;
164 if ((aspace_id = H5Aget_space(attrid)) < 0) {
165 string msg =
"cannot get hdf5 dataspace id for the attribute ";
166 string attrnamestr(attr_name.begin(),attr_name.end());
169 throw InternalErr(__FILE__, __LINE__, msg);
177 int ndims = H5Sget_simple_extent_ndims(aspace_id);
179 string msg =
"cannot get hdf5 dataspace number of dimension for attribute ";
180 string attrnamestr(attr_name.begin(),attr_name.end());
184 throw InternalErr(__FILE__, __LINE__, msg);
189 string msg =
"number of dimensions exceeds allowed for attribute ";
190 string attrnamestr(attr_name.begin(),attr_name.end());
194 throw InternalErr(__FILE__, __LINE__, msg);
202 if (H5Sget_simple_extent_dims(aspace_id, size, maxsize)<0){
203 string msg =
"cannot obtain the dim. info for the attribute ";
204 string attrnamestr(attr_name.begin(),attr_name.end());
208 throw InternalErr(__FILE__, __LINE__, msg);
214 for (
int j = 0; j < ndims; j++)
218 size_t ty_size = H5Tget_size(ty_id);
220 string msg =
"cannot obtain the dtype size for the attribute ";
221 string attrnamestr(attr_name.begin(),attr_name.end());
225 throw InternalErr(__FILE__, __LINE__, msg);
228 size_t need = nelmts * H5Tget_size(ty_id);
231 hid_t memtype = H5Tget_native_type(ty_id, H5T_DIR_ASCEND);
233 string msg =
"cannot obtain the memory dtype for the attribute ";
234 string attrnamestr(attr_name.begin(),attr_name.end());
238 throw InternalErr(__FILE__, __LINE__, msg);
242 (*attr_inst_ptr).type = memtype;
243 (*attr_inst_ptr).ndims = ndims;
244 (*attr_inst_ptr).nelmts = nelmts;
245 (*attr_inst_ptr).need = need;
246 strncpy((*attr_inst_ptr).name, &attr_name[0], name_size+1);
248 for (
int j = 0; j < ndims; j++) {
249 (*attr_inst_ptr).size[j] = size[j];
252 if(H5Sclose(aspace_id)<0) {
254 throw InternalErr(__FILE__,__LINE__,
"Cannot close HDF5 dataspace ");
276 BESDEBUG(
"h5",
">get_dap_type(): type=" << type << endl);
277 H5T_class_t class_t = H5Tget_class(type);
278 if (H5T_NO_CLASS == class_t)
279 throw InternalErr(__FILE__, __LINE__,
280 "The HDF5 datatype doesn't belong to any Class.");
285 size = H5Tget_size(type);
287 throw InternalErr(__FILE__, __LINE__,
288 "size of datatype is invalid");
291 sign = H5Tget_sign(type);
293 throw InternalErr(__FILE__, __LINE__,
294 "sign of datatype is invalid");
297 BESDEBUG(
"h5",
"=get_dap_type(): H5T_INTEGER" <<
298 " sign = " << sign <<
299 " size = " << size <<
303 if(
true == is_dap4) {
304 if (sign == H5T_SGN_NONE)
310 if (sign == H5T_SGN_NONE)
318 if (sign == H5T_SGN_NONE)
325 if (sign == H5T_SGN_NONE)
333 if (
true == is_dap4) {
334 if (sign == H5T_SGN_NONE)
346 size = H5Tget_size(type);
348 throw InternalErr(__FILE__, __LINE__,
349 "size of the datatype is invalid");
352 BESDEBUG(
"h5",
"=get_dap_type(): FLOAT size = " << size << endl);
361 BESDEBUG(
"h5",
"<get_dap_type(): H5T_STRING" << endl);
365 BESDEBUG(
"h5",
"<get_dap_type(): H5T_REFERENCE" << endl);
371 BESDEBUG(
"h5",
"<get_dap_type(): COMPOUND" << endl);
378 BESDEBUG(
"h5",
"<get_dap_type(): Unmappable Type" << endl);
379 return "Unmappable Type";
394 hid_t fileid = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
396 string msg =
"cannot open the HDF5 file ";
397 string filenamestr(filename);
399 throw InternalErr(__FILE__, __LINE__, msg);
416 if (H5Fclose(fid) < 0)
417 throw Error(unknown_error,
418 string(
"Could not close the HDF5 file."));
433 void get_dataset(hid_t pid,
const string &dname,
DS_t * dt_inst_ptr,
bool use_dimscale)
436 BESDEBUG(
"h5",
">get_dataset()" << endl);
440 if ((dset = H5Dopen(pid, dname.c_str(),H5P_DEFAULT)) < 0) {
441 string msg =
"cannot open the HDF5 dataset ";
443 throw InternalErr(__FILE__, __LINE__, msg);
448 if ((dtype = H5Dget_type(dset)) < 0) {
450 string msg =
"cannot get the the datatype of HDF5 dataset ";
452 throw InternalErr(__FILE__, __LINE__, msg);
456 H5T_class_t ty_class = H5Tget_class(dtype);
460 string msg =
"cannot get the datatype class of HDF5 dataset ";
462 throw InternalErr(__FILE__, __LINE__, msg);
468 if ((ty_class == H5T_TIME) || (ty_class == H5T_BITFIELD)
469 || (ty_class == H5T_OPAQUE) || (ty_class == H5T_ENUM) || (ty_class == H5T_VLEN)) {
470 string msg =
"unexpected datatype of HDF5 dataset ";
472 throw InternalErr(__FILE__, __LINE__, msg);
476 if ((dspace = H5Dget_space(dset)) < 0) {
479 string msg =
"cannot get the the dataspace of HDF5 dataset ";
481 throw InternalErr(__FILE__, __LINE__, msg);
489 int ndims = H5Sget_simple_extent_ndims(dspace);
494 string msg =
"cannot get hdf5 dataspace number of dimension for dataset ";
496 throw InternalErr(__FILE__, __LINE__, msg);
501 string msg =
"number of dimensions exceeds allowed for dataset ";
506 throw InternalErr(__FILE__, __LINE__, msg);
513 if (H5Sget_simple_extent_dims(dspace, size, maxsize)<0){
514 string msg =
"cannot obtain the dim. info for the dataset ";
519 throw InternalErr(__FILE__, __LINE__, msg);
525 for (
int j = 0; j < ndims; j++)
529 size_t dtype_size = H5Tget_size(dtype);
530 if (dtype_size == 0) {
531 string msg =
"cannot obtain the data type size for the dataset ";
536 throw InternalErr(__FILE__, __LINE__, msg);
539 size_t need = nelmts * dtype_size;
541 hid_t memtype = H5Tget_native_type(dtype, H5T_DIR_ASCEND);
543 string msg =
"cannot obtain the memory data type for the dataset ";
548 throw InternalErr(__FILE__, __LINE__, msg);
551 (*dt_inst_ptr).type = memtype;
552 (*dt_inst_ptr).ndims = ndims;
553 (*dt_inst_ptr).nelmts = nelmts;
554 (*dt_inst_ptr).need = need;
555 strncpy((*dt_inst_ptr).name, dname.c_str(), dname.length());
556 (*dt_inst_ptr).name[dname.length()] =
'\0';
557 for (
int j = 0; j < ndims; j++)
558 (*dt_inst_ptr).size[j] = size[j];
561 if(
true == use_dimscale) {
564 bool is_dimscale =
false;
572 herr_t ret = H5Aiterate2(dset, H5_INDEX_NAME, H5_ITER_INC, NULL, attr_info, &count);
574 string msg =
"cannot interate the attributes of the dataset ";
579 throw InternalErr(__FILE__, __LINE__, msg);
588 if(
true == is_dimscale) {
590 (*dt_inst_ptr).dimnames.push_back(dname.substr(dname.find_last_of(
"/")+1));
596 if(H5Tclose(dtype)<0) {
599 throw InternalErr(__FILE__, __LINE__,
"Cannot close the HDF5 datatype.");
602 if(H5Sclose(dspace)<0) {
604 throw InternalErr(__FILE__, __LINE__,
"Cannot close the HDF5 dataspace.");
607 if(H5Dclose(dset)<0) {
608 throw InternalErr(__FILE__, __LINE__,
"Cannot close the HDF5 dataset.");
623 if (H5Tget_class(h5type) == H5T_STRING)
647 unsigned short *tusp;
658 switch (H5Tget_class(type)) {
662 size_t size = H5Tget_size(type);
664 throw InternalErr(__FILE__, __LINE__,
665 "size of datatype is invalid");
668 H5T_sign_t sign = H5Tget_sign(type);
670 throw InternalErr(__FILE__, __LINE__,
671 "sign of datatype is invalid");
674 BESDEBUG(
"h5",
"=get_dap_type(): H5T_INTEGER" <<
675 " sign = " << sign <<
676 " size = " << size <<
686 if(sign == H5T_SGN_NONE) {
687 gp.ucp = (
unsigned char *) sm_buf;
688 unsigned char tuchar = *(gp.ucp + loc);
689 snprintf(&rep[0], 32,
"%u", tuchar);
693 gp.tcp = (
char *) sm_buf;
694 snprintf(&rep[0], 32,
"%d", *(gp.tcp + loc));
698 else if (size == 2) {
700 if(sign == H5T_SGN_NONE) {
701 gp.tusp = (
unsigned short *) sm_buf;
702 snprintf(&rep[0], 32,
"%hu", *(gp.tusp + loc));
706 gp.tsp = (
short *) sm_buf;
707 snprintf(&rep[0], 32,
"%hd", *(gp.tsp + loc));
712 else if (size == 4) {
714 if(sign == H5T_SGN_NONE) {
715 gp.tuip = (
unsigned int *) sm_buf;
716 snprintf(&rep[0], 32,
"%u", *(gp.tuip + loc));
720 gp.tip = (
int *) sm_buf;
721 snprintf(&rep[0], 32,
"%d", *(gp.tip + loc));
724 else if (size == 8) {
726 if(sign == H5T_SGN_NONE) {
727 gp.tulp = (
unsigned long *) sm_buf;
728 snprintf(&rep[0], 32,
"%lu", *(gp.tulp + loc));
731 gp.tlp = (
long *) sm_buf;
732 snprintf(&rep[0], 32,
"%ld", *(gp.tlp + loc));
736 throw InternalErr(__FILE__, __LINE__,
"Unsupported integer type, check the size of datatype.");
745 if (H5Tget_size(type) == 4) {
747 float attr_val = *(
float*)sm_buf;
748 bool is_a_fin = isfinite(attr_val);
751 gp.tfp = (
float *) sm_buf;
752 int ll = snprintf(gps, 30,
"%.10g", *(gp.tfp + loc));
756 if (!strchr(gps,
'.') && !strchr(gps,
'e') && !strchr(gps,
'E')
757 && (
true == is_a_fin)){
762 snprintf(&rep[0], 32,
"%s", gps);
764 else if (H5Tget_size(type) == 8) {
766 double attr_val = *(
double*)sm_buf;
767 bool is_a_fin = isfinite(attr_val);
768 gp.tdp = (
double *) sm_buf;
769 int ll = snprintf(gps, 30,
"%.17g", *(gp.tdp + loc));
773 if (!strchr(gps,
'.') && !strchr(gps,
'e')&& !strchr(gps,
'E')
774 && (
true == is_a_fin)) {
778 snprintf(&rep[0], 32,
"%s", gps);
780 else if (H5Tget_size(type) == 0){
781 throw InternalErr(__FILE__, __LINE__,
"H5Tget_size() failed.");
787 int str_size = H5Tget_size(type);
788 if(H5Tis_variable_str(type)>0) {
789 throw InternalErr(__FILE__, __LINE__,
790 "print_attr function doesn't handle variable length string, variable length string should be handled separately.");
793 throw InternalErr(__FILE__, __LINE__,
"H5Tget_size() failed.");
795 BESDEBUG(
"h5",
"=print_attr(): H5T_STRING sm_buf=" << (
char *) sm_buf
796 <<
" size=" << str_size << endl);
800 buf =
new char[str_size + 1];
801 strncpy(buf, (
char *) sm_buf, str_size);
802 buf[str_size] =
'\0';
804 rep.resize(str_size+3);
805 snprintf(&rep[0], str_size + 3,
"%s", buf);
806 rep[str_size + 2] =
'\0';
807 delete[] buf; buf = 0;
810 if( buf )
delete[] buf;
820 string rep_str(rep.begin(),rep.end());
824 D4AttributeType daptype_strrep_to_dap4_attrtype(std::string s){
828 else if (s ==
"Int8")
830 else if (s ==
"UInt8")
832 else if (s ==
"Int16")
834 else if (s ==
"UInt16")
835 return attr_uint16_c;
836 else if (s ==
"Int32")
838 else if (s ==
"UInt32")
839 return attr_uint32_c;
840 else if (s ==
"Int64")
842 else if (s ==
"UInt64")
843 return attr_uint64_c;
844 else if (s ==
"Float32")
845 return attr_float32_c;
846 else if (s ==
"Float64")
847 return attr_float64_c;
848 else if (s ==
"String")
873 BaseType *Get_bt(
const string &vname,
875 const string &dataset,
876 hid_t datatype,
bool is_dap4)
878 BaseType *btp = NULL;
882 BESDEBUG(
"h5",
">Get_bt varname=" << vname <<
" datatype=" << datatype
886 H5T_sign_t sign = H5T_SGN_ERROR;
887 switch (H5Tget_class(datatype)) {
891 size = H5Tget_size(datatype);
892 sign = H5Tget_sign(datatype);
893 BESDEBUG(
"h5",
"=Get_bt() H5T_INTEGER size = " << size <<
" sign = "
896 if (sign == H5T_SGN_ERROR) {
897 throw InternalErr(__FILE__, __LINE__,
"cannot retrieve the sign type of the integer");
900 throw InternalErr(__FILE__, __LINE__,
"cannot return the size of the datatype");
902 else if (size == 1) {
904 if (sign == H5T_SGN_2) {
905 if (
false == is_dap4)
906 btp =
new HDF5Int16(vname, vpath, dataset);
908 btp =
new HDF5Int8(vname,vpath,dataset);
911 btp =
new HDF5Byte(vname, vpath,dataset);
913 else if (size == 2) {
914 if (sign == H5T_SGN_2)
915 btp =
new HDF5Int16(vname, vpath,dataset);
919 else if (size == 4) {
920 if (sign == H5T_SGN_2){
921 btp =
new HDF5Int32(vname, vpath,dataset);
926 else if (size == 8) {
927 if(
true == is_dap4) {
928 if(sign == H5T_SGN_2)
929 btp =
new HDF5Int64(vname,vpath, dataset);
935 InternalErr(__FILE__, __LINE__,
936 string(
"Unsupported HDF5 64-bit Integer type:")
945 size = H5Tget_size(datatype);
946 BESDEBUG(
"h5",
"=Get_bt() H5T_FLOAT size = " << size << endl);
949 throw InternalErr(__FILE__, __LINE__,
"cannot return the size of the datatype");
951 else if (size == 4) {
954 else if (size == 8) {
961 btp =
new HDF5Str(vname, vpath,dataset);
971 "=Get_bt() H5T_ARRAY datatype = " << datatype
975 hid_t dtype_base = H5Tget_super(datatype);
976 ar_bt = Get_bt(vname, dataset, dtype_base);
977 btp =
new HDF5Array(vname, dataset, ar_bt);
978 delete ar_bt; ar_bt = 0;
981 int ndim = H5Tget_array_ndims(datatype);
982 size = H5Tget_size(datatype);
985 if (dtype_base < 0) {
986 throw InternalErr(__FILE__, __LINE__,
"cannot return the base datatype");
989 throw InternalErr(__FILE__, __LINE__,
"cannot return the rank of the array datatype");
992 throw InternalErr(__FILE__, __LINE__,
"cannot return the size of the datatype");
995 <<
"=Get_bt()" <<
" Dim = " << ndim
996 <<
" Size = " << size
1000 if(H5Tget_array_dims(datatype, size2) < 0){
1002 InternalErr(__FILE__, __LINE__,
1003 string(
"Could not get array dims for: ")
1009 for (
int dim_index = 0; dim_index < ndim; dim_index++) {
1010 h5_ar.append_dim(size2[dim_index]);
1011 BESDEBUG(
"h5",
"=Get_bt() " << size2[dim_index] << endl);
1012 nelement = nelement * size2[dim_index];
1015 h5_ar.set_did(dt_inst.dset);
1017 h5_ar.set_tid(datatype);
1021 h5_ar.set_length(nelement);
1022 h5_ar.d_type = H5Tget_class(dtype_base);
1023 if (h5_ar.d_type == H5T_NO_CLASS){
1024 throw InternalErr(__FILE__, __LINE__,
"cannot return the datatype class identifier");
1028 if( ar_bt )
delete ar_bt;
1029 if( btp )
delete btp;
1038 btp =
new HDF5Url(vname, vpath,dataset);
1042 throw InternalErr(__FILE__, __LINE__,
1043 string(
"Unsupported HDF5 type: ") + vname);
1047 if( btp )
delete btp;
1052 throw InternalErr(__FILE__, __LINE__,
1053 string(
"Could not make a DAP variable for: ")
1056 BESDEBUG(
"h5",
"<Get_bt()" << endl);
1078 Structure *Get_structure(
const string &varname,
const string &vpath,
1079 const string &dataset,
1080 hid_t datatype,
bool is_dap4)
1083 char* memb_name = NULL;
1084 hid_t memb_type = -1;
1086 BESDEBUG(
"h5",
">Get_structure()" << datatype << endl);
1088 if (H5Tget_class(datatype) != H5T_COMPOUND)
1089 throw InternalErr(__FILE__, __LINE__,
1090 string(
"Compound-to-structure mapping error for ")
1097 int nmembs = H5Tget_nmembers(datatype);
1098 BESDEBUG(
"h5",
"=Get_structure() has " << nmembs << endl);
1100 throw InternalErr(__FILE__, __LINE__,
"cannot retrieve the number of elements");
1102 for (
int i = 0; i < nmembs; i++) {
1103 memb_name = H5Tget_member_name(datatype, i);
1104 H5T_class_t memb_cls = H5Tget_member_class(datatype, i);
1105 memb_type = H5Tget_member_type(datatype, i);
1106 if (memb_name == NULL){
1107 throw InternalErr(__FILE__, __LINE__,
"cannot retrieve the name of the member");
1109 if ((memb_cls < 0) || (memb_type < 0)) {
1110 throw InternalErr(__FILE__, __LINE__,
1111 string(
"Type mapping error for ")
1112 +
string(memb_name) );
1115 if (memb_cls == H5T_COMPOUND) {
1116 Structure *s = Get_structure(memb_name, memb_name, dataset, memb_type,is_dap4);
1117 structure_ptr->add_var(s);
1120 else if(memb_cls == H5T_ARRAY) {
1122 BaseType *ar_bt = 0;
1125 hid_t dtype_base = 0;
1130 dtype_base = H5Tget_super(memb_type);
1133 int ndim = H5Tget_array_ndims(memb_type);
1134 size_t size = H5Tget_size(memb_type);
1137 if (dtype_base < 0) {
1138 throw InternalErr(__FILE__, __LINE__,
"cannot return the base memb_type");
1141 throw InternalErr(__FILE__, __LINE__,
"cannot return the rank of the array memb_type");
1144 throw InternalErr(__FILE__, __LINE__,
"cannot return the size of the memb_type");
1148 if(H5Tget_array_dims(memb_type, size2) < 0){
1150 InternalErr(__FILE__, __LINE__,
1151 string(
"Could not get array dims for: ")
1152 +
string(memb_name));
1155 H5T_class_t array_memb_cls = H5Tget_class(dtype_base);
1156 if(array_memb_cls == H5T_NO_CLASS) {
1157 throw InternalErr(__FILE__, __LINE__,
1158 string(
"cannot get the correct class for compound type member")
1159 +
string(memb_name));
1161 if(H5T_COMPOUND == array_memb_cls) {
1163 s = Get_structure(memb_name, memb_name,dataset, dtype_base,is_dap4);
1166 for (
int dim_index = 0; dim_index < ndim; dim_index++) {
1167 h5_ar->append_dim(size2[dim_index]);
1168 nelement = nelement * size2[dim_index];
1174 h5_ar->set_length(nelement);
1176 structure_ptr->add_var(h5_ar);
1180 else if (H5T_INTEGER == array_memb_cls || H5T_FLOAT == array_memb_cls || H5T_STRING == array_memb_cls) {
1181 ar_bt = Get_bt(memb_name, memb_name,dataset, dtype_base,is_dap4);
1184 for (
int dim_index = 0; dim_index < ndim; dim_index++) {
1185 h5_ar->append_dim(size2[dim_index]);
1186 nelement = nelement * size2[dim_index];
1192 h5_ar->set_length(nelement);
1194 structure_ptr->add_var(h5_ar);
1197 if( ar_bt )
delete ar_bt;
1198 if( btp )
delete btp;
1200 H5Tclose(dtype_base);
1204 if( ar_bt )
delete ar_bt;
1205 if( btp )
delete btp;
1207 H5Tclose(dtype_base);
1212 else if (memb_cls == H5T_INTEGER || memb_cls == H5T_FLOAT || memb_cls == H5T_STRING) {
1213 BaseType *bt = Get_bt(memb_name, memb_name,dataset, memb_type,is_dap4);
1214 structure_ptr->add_var(bt);
1220 throw InternalErr(__FILE__, __LINE__,
"unsupported field datatype inside a compound datatype");
1223 if(memb_name != NULL)
1229 delete structure_ptr;
1230 if(memb_name!= NULL)
1233 H5Tclose(memb_type);
1237 BESDEBUG(
"h5",
"<Get_structure()" << endl);
1239 return structure_ptr;
1260 bool check_dimscale(hid_t fileid) {
1262 bool ret_value =
false;
1263 herr_t ret_o= H5Ovisit(fileid, H5_INDEX_NAME, H5_ITER_INC, visit_obj_cb, NULL);
1265 throw InternalErr(__FILE__, __LINE__,
"H5Ovisit fails");
1267 ret_value =(ret_o >0)?
true:
false;
1273 visit_obj_cb(hid_t group_id,
const char *name,
const H5O_info_t *oinfo,
1277 if(oinfo->type == H5O_TYPE_DATASET) {
1280 dataset = H5Dopen2(group_id,name,H5P_DEFAULT);
1282 throw InternalErr(__FILE__, __LINE__,
"H5Dopen2 fails in the H5Ovisit call back function.");
1285 dspace = H5Dget_space(dataset);
1288 throw InternalErr(__FILE__, __LINE__,
"H5Dget_space fails in the H5Ovisit call back function.");
1292 if(H5Sget_simple_extent_ndims(dspace) == 1) {
1296 herr_t ret = H5Aiterate2(dataset, H5_INDEX_NAME, H5_ITER_INC, NULL, attr_info, &count);
1300 throw InternalErr(__FILE__, __LINE__,
"H5Aiterate2 fails in the H5Ovisit call back function.");
1334 attr_info(hid_t loc_id,
const char *name,
const H5A_info_t *ainfo,
void *opdata)
1336 int *count = (
int*)opdata;
1339 hid_t atype_id = -1;
1342 attr_id = H5Aopen(loc_id, name, H5P_DEFAULT);
1344 throw InternalErr(__FILE__, __LINE__,
"H5Aopen fails in the attr_info call back function.");
1347 atype_id = H5Aget_type(attr_id);
1350 throw InternalErr(__FILE__, __LINE__,
"H5Aget_type fails in the attr_info call back function.");
1356 if ((H5T_COMPOUND == H5Tget_class(atype_id)) && (strcmp(name,
"REFERENCE_LIST")==0)) {
1361 if ((H5T_STRING == H5Tget_class(atype_id)) && (strcmp(name,
"CLASS") == 0)) {
1363 H5T_str_t str_pad = H5Tget_strpad(atype_id);
1365 hid_t aspace_id = -1;
1366 aspace_id = H5Aget_space(attr_id);
1368 throw InternalErr(__FILE__, __LINE__,
"H5Aget_space fails in the attr_info call back function.");
1371 int ndims = H5Sget_simple_extent_ndims(aspace_id);
1377 vector<hsize_t> asize;
1378 vector<hsize_t> maxsize;
1379 asize.resize(ndims);
1380 maxsize.resize(ndims);
1386 if (H5Sget_simple_extent_dims(aspace_id, &asize[0], &maxsize[0])<0) {
1387 H5Sclose(aspace_id);
1388 throw InternalErr(__FILE__, __LINE__,
"Cannot obtain the dim. info in the H5Aiterate2 call back function.");
1392 for (
int j = 0; j < ndims; j++)
1396 size_t ty_size = H5Tget_size(atype_id);
1398 H5Sclose(aspace_id);
1399 throw InternalErr(__FILE__, __LINE__,
"Cannot obtain the type size in the H5Aiterate2 call back function.");
1402 size_t total_bytes = nelmts * ty_size;
1403 string total_vstring =
"";
1404 if(H5Tis_variable_str(atype_id) > 0) {
1407 vector<char> temp_buf;
1408 temp_buf.resize(total_bytes);
1410 if (H5Aread(attr_id, atype_id, &temp_buf[0]) < 0){
1411 H5Sclose(aspace_id);
1412 throw InternalErr(__FILE__,__LINE__,
"Cannot read the attribute in the H5Aiterate2 call back function");
1415 char *temp_bp = NULL;
1416 temp_bp = &temp_buf[0];
1417 char* onestring = NULL;
1419 for (
unsigned int temp_i = 0; temp_i <nelmts; temp_i++) {
1422 onestring =*(
char **)temp_bp;
1424 if(onestring!= NULL)
1425 total_vstring +=string(onestring);
1431 if ((&temp_buf[0]) != NULL) {
1433 if (H5Dvlen_reclaim(atype_id,aspace_id,H5P_DEFAULT,&temp_buf[0]) < 0) {
1434 H5Sclose(aspace_id);
1435 throw InternalErr(__FILE__,__LINE__,
"Cannot reclaim VL memory in the H5Aiterate2 call back function.");
1443 vector<char> temp_buf;
1444 temp_buf.resize(total_bytes);
1445 if (H5Aread(attr_id, atype_id, &temp_buf[0]) < 0){
1446 H5Sclose(aspace_id);
1447 throw InternalErr(__FILE__,__LINE__,
"Cannot read the attribute in the H5Aiterate2 call back function");
1449 string temp_buf_string(temp_buf.begin(),temp_buf.end());
1450 total_vstring = temp_buf_string.substr(0,total_bytes);
1453 if(str_pad != H5T_STR_ERROR)
1454 total_vstring = total_vstring.substr(0,total_vstring.size()-1);
1459 H5Sclose(aspace_id);
1460 if(total_vstring ==
"DIMENSION_SCALE"){
1496 htri_t has_dimension_list = -1;
1498 string dimlist_name =
"DIMENSION_LIST";
1499 has_dimension_list = H5Aexists(dset,dimlist_name.c_str());
1501 if(has_dimension_list > 0 && ndims > 0) {
1504 vector<hvl_t> vlbuf;
1505 vlbuf.resize(ndims);
1508 hid_t atype_id = -1;
1509 hid_t amemtype_id = -1;
1510 hid_t aspace_id = -1;
1511 hid_t ref_dset = -1;
1514 attr_id = H5Aopen(dset,dimlist_name.c_str(),H5P_DEFAULT);
1516 string msg =
"Cannot open the attribute " + dimlist_name +
" of HDF5 dataset "+ string(dt_inst_ptr->
name);
1517 throw InternalErr(__FILE__, __LINE__, msg);
1520 atype_id = H5Aget_type(attr_id);
1522 string msg =
"Cannot get the datatype of the attribute " + dimlist_name +
" of HDF5 dataset "+ string(dt_inst_ptr->
name);
1523 throw InternalErr(__FILE__, __LINE__, msg);
1526 amemtype_id = H5Tget_native_type(atype_id, H5T_DIR_ASCEND);
1527 if (amemtype_id < 0) {
1528 string msg =
"Cannot get the memory datatype of the attribute " + dimlist_name +
" of HDF5 dataset "+ string(dt_inst_ptr->
name);
1529 throw InternalErr(__FILE__, __LINE__, msg);
1533 if (H5Aread(attr_id,amemtype_id,&vlbuf[0]) <0) {
1534 string msg =
"Cannot obtain the referenced object for the variable " + string(dt_inst_ptr->
name);
1535 throw InternalErr(__FILE__, __LINE__, msg);
1538 vector<char> objname;
1541 for (
int i = 0; i < ndims; i++) {
1543 if(vlbuf[i].p == NULL) {
1544 stringstream sindex ;
1546 string msg =
"For variable " + string(dt_inst_ptr->
name) +
"; ";
1547 msg = msg +
"the dimension of which the index is "+ sindex.str() +
" doesn't exist. ";
1548 throw InternalErr(__FILE__, __LINE__, msg);
1551 rbuf =((hobj_ref_t*)vlbuf[i].p)[0];
1554 if ((ref_dset = H5RDEREFERENCE(attr_id, H5R_OBJECT, &rbuf)) < 0) {
1555 string msg =
"Cannot dereference from the DIMENSION_LIST attribute for the variable " + string(dt_inst_ptr->
name);
1556 throw InternalErr(__FILE__, __LINE__, msg);
1559 ssize_t objnamelen = -1;
1560 if ((objnamelen= H5Iget_name(ref_dset,NULL,0))<=0) {
1561 string msg =
"Cannot obtain the dimension name length for the variable " + string(dt_inst_ptr->
name);
1562 throw InternalErr(__FILE__,__LINE__,msg);
1565 objname.resize(objnamelen+1);
1566 if ((objnamelen= H5Iget_name(ref_dset,&objname[0],objnamelen+1))<=0) {
1568 string msg =
"Cannot obtain the dimension name for the variable " + string(dt_inst_ptr->
name);
1569 throw InternalErr(__FILE__,__LINE__,msg);
1572 string objname_str = string(objname.begin(),objname.end());
1575 string trim_objname = objname_str.substr(0,objnamelen);
1578 dt_inst_ptr->dimnames.push_back(trim_objname.substr(trim_objname.find_last_of(
"/")+1));
1580 if(H5Dclose(ref_dset)<0) {
1581 throw InternalErr(__FILE__,__LINE__,
"Cannot close the HDF5 dataset in the function obtain_dimnames().");
1585 if(vlbuf.size()!= 0) {
1587 if ((aspace_id = H5Aget_space(attr_id)) < 0) {
1588 string msg =
"Cannot close the HDF5 attribute space successfully for <DIMENSION_LIST> of the variable "+string(dt_inst_ptr->
name);
1589 throw InternalErr(__FILE__,__LINE__,msg);
1592 if (H5Dvlen_reclaim(amemtype_id,aspace_id,H5P_DEFAULT,(
void*)&vlbuf[0])<0) {
1593 throw InternalErr(__FILE__,__LINE__,
"Cannot reclaim the variable length memory in the function obtain_dimnames()");
1596 H5Sclose(aspace_id);
1601 H5Tclose(amemtype_id);
1611 if(amemtype_id != -1)
1612 H5Tclose(amemtype_id);
1615 H5Sclose(aspace_id);
1627 void write_vlen_str_attrs(hid_t attr_id,hid_t ty_id,
DSattr_t * attr_inst_ptr,D4Attribute *d4_attr, AttrTable* d2_attr,
bool is_dap4){
1629 BESDEBUG(
"h5",
"attribute name " << attr_inst_ptr->
name <<endl);
1630 BESDEBUG(
"h5",
"attribute size " <<attr_inst_ptr->
need <<endl);
1631 BESDEBUG(
"h5",
"attribute type size " <<(
int)(H5Tget_size(ty_id))<<endl);
1633 hid_t temp_space_id = H5Aget_space(attr_id);
1634 BESDEBUG(
"h5",
"attribute calculated size "<<(
int)(H5Tget_size(ty_id)) *(
int)(H5Sget_simple_extent_npoints(temp_space_id)) <<endl);
1635 if(temp_space_id <0) {
1638 throw InternalErr(__FILE__, __LINE__,
"unable to read HDF5 attribute data");
1641 vector<char> temp_buf;
1643 temp_buf.resize((
size_t)attr_inst_ptr->
need);
1645 if (H5Aread(attr_id, ty_id, &temp_buf[0]) < 0) {
1648 H5Sclose(temp_space_id);
1649 throw InternalErr(__FILE__, __LINE__,
"unable to read HDF5 attribute data");
1653 temp_bp = &temp_buf[0];
1655 for (
unsigned int temp_i = 0; temp_i <attr_inst_ptr->
nelmts; temp_i++) {
1658 onestring =*(
char **)temp_bp;
1661 if (onestring !=NULL) {
1662 string tempstring(onestring);
1664 d4_attr->add_value(tempstring);
1666 d2_attr->append_attr(attr_inst_ptr->
name,
"String",tempstring);
1669 temp_bp +=H5Tget_size(ty_id);
1671 if (temp_buf.empty() !=
true) {
1674 herr_t ret_vlen_claim;
1675 ret_vlen_claim = H5Dvlen_reclaim(ty_id,temp_space_id,H5P_DEFAULT,&temp_buf[0]);
1676 if(ret_vlen_claim < 0){
1679 H5Sclose(temp_space_id);
1680 throw InternalErr(__FILE__, __LINE__,
"Cannot reclaim the memory buffer of the HDF5 variable length string.");
1685 H5Sclose(temp_space_id);