57 bool *ignore_attr_ptr)
63 *ignore_attr_ptr =
false;
65 if ((attrid = H5Aopen_by_idx(dset,
".", H5_INDEX_CRT_ORDER, H5_ITER_INC,(hsize_t)index, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
66 string msg =
"unable to open attribute by index ";
68 throw InternalErr(__FILE__, __LINE__, msg);
72 ssize_t name_size = H5Aget_name(attrid, 0,
NULL);
75 string msg =
"unable to obtain the size of the hdf5 attribute name ";
76 throw InternalErr(__FILE__, __LINE__, msg);
79 vector<char> attr_name;
80 attr_name.resize(name_size+1);
82 if ((H5Aget_name(attrid, name_size+1, &attr_name[0])) < 0) {
84 string msg =
"unable to obtain the hdf5 attribute name ";
85 throw InternalErr(__FILE__, __LINE__, msg);
90 if ((ty_id = H5Aget_type(attrid)) < 0) {
91 string msg =
"unable to obtain hdf5 datatype for the attribute ";
92 string attrnamestr(attr_name.begin(),attr_name.end());
95 throw InternalErr(__FILE__, __LINE__, msg);
98 H5T_class_t ty_class = H5Tget_class(ty_id);
100 string msg =
"cannot get hdf5 attribute datatype class for the attribute ";
101 string attrnamestr(attr_name.begin(),attr_name.end());
104 throw InternalErr(__FILE__, __LINE__, msg);
117 if ((ty_class == H5T_TIME) || (ty_class == H5T_BITFIELD)
118 || (ty_class == H5T_OPAQUE) || (ty_class == H5T_ENUM)
119 || (ty_class == H5T_REFERENCE) ||(ty_class == H5T_COMPOUND)
120 || (ty_class == H5T_VLEN) || (ty_class == H5T_ARRAY)
121 || ((ty_class == H5T_INTEGER) && (H5Tget_size(ty_id)== 8))) {
123 *ignore_attr_ptr =
true;
128 if ((aspace_id = H5Aget_space(attrid)) < 0) {
129 string msg =
"cannot get hdf5 dataspace id for the attribute ";
130 string attrnamestr(attr_name.begin(),attr_name.end());
133 throw InternalErr(__FILE__, __LINE__, msg);
141 int ndims = H5Sget_simple_extent_ndims(aspace_id);
143 string msg =
"cannot get hdf5 dataspace number of dimension for attribute ";
144 string attrnamestr(attr_name.begin(),attr_name.end());
148 throw InternalErr(__FILE__, __LINE__, msg);
153 string msg =
"number of dimensions exceeds allowed for attribute ";
154 string attrnamestr(attr_name.begin(),attr_name.end());
158 throw InternalErr(__FILE__, __LINE__, msg);
168 if (H5Sget_simple_extent_dims(aspace_id, size, maxsize)<0){
169 string msg =
"cannot obtain the dim. info for the attribute ";
170 string attrnamestr(attr_name.begin(),attr_name.end());
174 throw InternalErr(__FILE__, __LINE__, msg);
181 for (
int j = 0; j < ndims; j++)
186 size_t ty_size = H5Tget_size(ty_id);
188 string msg =
"cannot obtain the dtype size for the attribute ";
189 string attrnamestr(attr_name.begin(),attr_name.end());
193 throw InternalErr(__FILE__, __LINE__, msg);
197 size_t need = nelmts * H5Tget_size(ty_id);
200 hid_t memtype = H5Tget_native_type(ty_id, H5T_DIR_ASCEND);
203 string msg =
"cannot obtain the memory dtype for the attribute ";
204 string attrnamestr(attr_name.begin(),attr_name.end());
208 throw InternalErr(__FILE__, __LINE__, msg);
212 (*attr_inst_ptr).type = memtype;
213 (*attr_inst_ptr).ndims = ndims;
214 (*attr_inst_ptr).nelmts = nelmts;
215 (*attr_inst_ptr).need = need;
216 strncpy((*attr_inst_ptr).name, &attr_name[0], name_size+1);
218 for (
int j = 0; j < ndims; j++) {
219 (*attr_inst_ptr).size[j] = size[j];
241 DBG(cerr <<
">get_dap_type(): type=" << type << endl);
242 H5T_class_t class_t = H5Tget_class(type);
243 if (H5T_NO_CLASS == class_t)
244 throw InternalErr(__FILE__, __LINE__,
245 "The HDF5 datatype doesn't belong to any Class.");
250 size = H5Tget_size(type);
252 throw InternalErr(__FILE__, __LINE__,
253 "size of datatype is invalid");
256 sign = H5Tget_sign(type);
258 throw InternalErr(__FILE__, __LINE__,
259 "sign of datatype is invalid");
262 DBG(cerr <<
"=get_dap_type(): H5T_INTEGER" <<
263 " sign = " << sign <<
264 " size = " << size <<
267 if (sign == H5T_SGN_NONE)
274 if (sign == H5T_SGN_NONE)
281 if (sign == H5T_SGN_NONE)
290 size = H5Tget_size(type);
292 throw InternalErr(__FILE__, __LINE__,
293 "size of the datatype is invalid");
296 DBG(cerr <<
"=get_dap_type(): FLOAT size = " << size << endl);
305 DBG(cerr <<
"<get_dap_type(): H5T_STRING" << endl);
309 DBG(cerr <<
"<get_dap_type(): H5T_REFERENCE" << endl);
313 DBG(cerr <<
"<get_dap_type(): COMPOUND" << endl);
320 DBG(cerr <<
"<get_dap_type(): Unmappable Type" << endl);
321 return "Unmappable Type";
336 hid_t fileid = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
338 string msg =
"cannot open the HDF5 file ";
339 string filenamestr(filename);
341 throw InternalErr(__FILE__, __LINE__, msg);
358 if (H5Fclose(fid) < 0)
359 throw Error(unknown_error,
360 string(
"Could not close the HDF5 file."));
377 DBG(cerr <<
">get_dataset()" << endl);
381 if ((dset = H5Dopen(pid, dname.c_str(),H5P_DEFAULT)) < 0) {
382 string msg =
"cannot open the HDF5 dataset ";
384 throw InternalErr(__FILE__, __LINE__, msg);
389 if ((dtype = H5Dget_type(dset)) < 0) {
391 string msg =
"cannot get the the datatype of HDF5 dataset ";
393 throw InternalErr(__FILE__, __LINE__, msg);
397 H5T_class_t ty_class = H5Tget_class(dtype);
401 string msg =
"cannot get the datatype class of HDF5 dataset ";
403 throw InternalErr(__FILE__, __LINE__, msg);
409 if ((ty_class == H5T_TIME) || (ty_class == H5T_BITFIELD)
410 || (ty_class == H5T_OPAQUE) || (ty_class == H5T_ENUM) || (ty_class == H5T_VLEN)) {
411 string msg =
"unexpected datatype of HDF5 dataset ";
413 throw InternalErr(__FILE__, __LINE__, msg);
417 if ((dspace = H5Dget_space(dset)) < 0) {
420 string msg =
"cannot get the the dataspace of HDF5 dataset ";
422 throw InternalErr(__FILE__, __LINE__, msg);
430 int ndims = H5Sget_simple_extent_ndims(dspace);
435 string msg =
"cannot get hdf5 dataspace number of dimension for dataset ";
437 throw InternalErr(__FILE__, __LINE__, msg);
442 string msg =
"number of dimensions exceeds allowed for dataset ";
447 throw InternalErr(__FILE__, __LINE__, msg);
457 if (H5Sget_simple_extent_dims(dspace, size, maxsize)<0){
458 string msg =
"cannot obtain the dim. info for the dataset ";
463 throw InternalErr(__FILE__, __LINE__, msg);
469 for (
int j = 0; j < ndims; j++)
473 size_t dtype_size = H5Tget_size(dtype);
474 if (dtype_size == 0) {
475 string msg =
"cannot obtain the data type size for the dataset ";
480 throw InternalErr(__FILE__, __LINE__, msg);
483 size_t need = nelmts * dtype_size;
485 hid_t memtype = H5Tget_native_type(dtype, H5T_DIR_ASCEND);
487 string msg =
"cannot obtain the memory data type for the dataset ";
492 throw InternalErr(__FILE__, __LINE__, msg);
495 (*dt_inst_ptr).dset = dset;
496 (*dt_inst_ptr).dataspace = dspace;
497 (*dt_inst_ptr).type = memtype;
498 (*dt_inst_ptr).ndims = ndims;
499 (*dt_inst_ptr).nelmts = nelmts;
500 (*dt_inst_ptr).need = need;
501 strncpy((*dt_inst_ptr).name, dname.c_str(), dname.length());
502 (*dt_inst_ptr).name[dname.length()] =
'\0';
504 for (
int j = 0; j < ndims; j++) {
505 (*dt_inst_ptr).size[j] = size[j];
508 DBG(cerr <<
"<get_dataset() dimension=" << ndims <<
" elements=" <<
523 DBG(cerr <<
">get_data()" << endl);
526 if ((dtype = H5Dget_type(dset)) < 0) {
528 throw InternalErr(__FILE__, __LINE__,
"Failed to get the datatype of the dataset");
531 if ((dspace = H5Dget_space(dset)) < 0) {
534 throw InternalErr(__FILE__, __LINE__,
"Failed to get the data space of the dataset");
537 hid_t memtype = H5Tget_native_type(dtype, H5T_DIR_ASCEND);
542 throw InternalErr(__FILE__, __LINE__,
"failed to get memory type");
547 if (H5Dread(dset, memtype, dspace, dspace, H5P_DEFAULT, buf)
553 throw InternalErr(__FILE__, __LINE__,
"failed to read data");
560 if (memtype == H5T_STRING) {
561 DBG(cerr <<
"=get_data(): H5T_STRING type is detected." << endl);
563 if (H5Dread(dset, memtype, dspace, dspace, H5P_DEFAULT, buf)
566 throw InternalErr(__FILE__, __LINE__,
"failed to read data");
570 DBG(cerr <<
"=get_data(): H5T_STRING type is NOT detected." << endl);
571 if (H5Dread(dset, memtype, dspace, dspace, H5P_DEFAULT, buf)
574 throw InternalErr(__FILE__, __LINE__,
"failed to read data");
588 if (H5Sclose(dspace) < 0){
592 throw InternalErr(__FILE__, __LINE__,
"Unable to terminate the data space access.");
596 if (H5Tget_class(dtype) != H5T_STRING) {
599 if (H5Tclose(dtype) < 0){
602 throw InternalErr(__FILE__, __LINE__,
"Unable to release the dtype.");
605 if (H5Tclose(memtype) < 0){
607 throw InternalErr(__FILE__, __LINE__,
"Unable to release the memtype.");
618 DBG(cerr <<
"<get_data()" << endl);
632 void get_strdata(
int strindex,
char *allbuf,
char *buf,
int elesize)
634 char *tempvalue = allbuf;
636 DBG(cerr <<
">get_strdata(): "
637 <<
" strindex=" << strindex <<
" allbuf=" << allbuf << endl);
640 for (
int i = 0; i < strindex; i++) {
641 tempvalue = tempvalue + elesize;
644 strncpy(buf, tempvalue, elesize);
661 get_slabdata(hid_t dset,
int *offset,
int *step,
int *count,
int num_dim,
664 DBG(cerr <<
">get_slabdata() " << endl);
666 hid_t dtype = H5Dget_type(dset);
669 throw InternalErr(__FILE__, __LINE__,
"could not get data type");
672 hid_t memtype = H5Tget_native_type(dtype, H5T_DIR_ASCEND);
676 throw InternalErr(__FILE__, __LINE__,
"could not get memory type");
679 hid_t dspace = H5Dget_space(dset);
684 throw InternalErr(__FILE__, __LINE__,
"could not get data space");
687 hsize_t *dyn_count = 0;
688 hsize_t *dyn_step = 0;
689 hssize_t *dyn_offset = 0;
691 dyn_count =
new hsize_t[num_dim];
692 dyn_step =
new hsize_t[num_dim];
693 dyn_offset =
new hssize_t[num_dim];
695 for (
int i = 0; i < num_dim; i++) {
696 dyn_count[i] = (hsize_t) (*count);
697 dyn_step[i] = (hsize_t) (*step);
698 dyn_offset[i] = (hssize_t) (*offset);
700 <<
"count:" << dyn_count[i]
701 <<
" step:" << dyn_step[i]
702 <<
" offset:" << dyn_step[i]
709 if (H5Sselect_hyperslab(dspace, H5S_SELECT_SET,
710 (
const hsize_t *)dyn_offset, dyn_step,
711 dyn_count,
NULL) < 0) {
720 InternalErr(__FILE__, __LINE__,
"could not select hyperslab");
723 hid_t memspace = H5Screate_simple(num_dim, dyn_count,
NULL);
732 throw InternalErr(__FILE__, __LINE__,
"could not open space");
739 if (H5Dread(dset, memtype, memspace, dspace, H5P_DEFAULT,
746 throw InternalErr(__FILE__, __LINE__,
"could not get data");
749 if (H5Sclose(dspace) < 0){
754 throw InternalErr(__FILE__, __LINE__,
"Unable to close the dspace.");
756 if (H5Sclose(memspace) < 0){
760 throw InternalErr(__FILE__, __LINE__,
"Unable to close the memspace.");
762 if (H5Tclose(dtype) < 0){
765 throw InternalErr(__FILE__, __LINE__,
"Unable to close the dtype.");
768 if (H5Tclose(memtype) < 0){
770 throw InternalErr(__FILE__, __LINE__,
"Unable to close the memtype.");
782 if( dyn_count )
delete[] dyn_count;
783 if( dyn_step )
delete[] dyn_step;
784 if( dyn_offset )
delete[] dyn_offset;
788 DBG(cerr <<
"<get_slabdata() " << endl);
804 if (H5Tget_class(h5type) == H5T_STRING)
A structure for DDS generation.
void get_dataset(hid_t pid, const string &dname, DS_t *dt_inst_ptr)
obtain data information in a dataset datatype, dataspace(dimension sizes) and number of dimensions an...
#define DODS_MAX_RANK
Maximum number of dimensions in an array(default option only).
string get_dap_type(hid_t type)
returns the string representation of HDF5 type.
void get_data(hid_t dset, void *buf)
will get all data of a dset dataset and put it into buf.
bool check_h5str(hid_t h5type)
checks if type is HDF5 string type
static class NCMLUtil overview
A structure for DAS generation.
hid_t get_attr_info(hid_t dset, int index, DSattr_t *attr_inst_ptr, bool *ignore_attr_ptr)
void get_strdata(int strindex, char *allbuf, char *buf, int elesize)
will get an individual string data from all string data elements and put it into buf.
int get_slabdata(hid_t dset, int *offset, int *step, int *count, int num_dim, void *buf)
will get hyperslab data of a dataset and put it into buf.
void close_fileid(hid_t fid)
closes HDF5 file reffered by fid.
hid_t get_fileid(const char *filename)
gets HDF5 file id.
Helper functions to generate DDS/DAS/DODS for the default option.