31 #include "config_hdf5.h"
35 #include <libdap/InternalErr.h>
47 : Structure(n, d),var_path(vpath)
51 HDF5Structure::~HDF5Structure()
63 dynamic_cast < Structure &
>(*this) = rhs;
73 ">read() dataset=" << dataset()<<endl);
78 hid_t file_id = H5Fopen(dataset().c_str(),H5F_ACC_RDONLY,H5P_DEFAULT);
80 throw InternalErr(__FILE__,__LINE__,
"Fail to obtain the HDF5 file ID .");
85 dset_id = H5Dopen2(file_id,var_path.c_str(),H5P_DEFAULT);
87 dset_id = H5Dopen2(file_id,name().c_str(),H5P_DEFAULT);
91 throw InternalErr(__FILE__,__LINE__,
"Fail to obtain the datatype .");
94 hid_t dtypeid = H5Dget_type(dset_id);
98 throw InternalErr(__FILE__,__LINE__,
"Fail to obtain the datatype .");
101 do_structure_read(dset_id,dtypeid,values,
false,0);
118 void HDF5Structure::do_structure_read(hid_t dsetid, hid_t dtypeid,vector <char> &values,
bool has_values,
int values_offset) {
123 if ((memtype = H5Tget_native_type(dtypeid, H5T_DIR_ASCEND))<0) {
124 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain memory datatype.");
127 if(
false == has_values) {
129 if((mspace = H5Dget_space(dsetid))<0) {
130 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain memory datatype.");
133 size_t ty_size = H5Tget_size(memtype);
136 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain the size of HDF5 compound datatype.");
139 values.resize(ty_size);
141 read_ret = H5Dread(dsetid,memtype,mspace,mspace,H5P_DEFAULT,(
void*)&values[0]);
144 throw InternalErr (__FILE__, __LINE__,
"Fail to read the HDF5 compound datatype dataset.");
151 H5T_class_t memb_cls = H5T_NO_CLASS;
153 size_t memb_offset = 0;
155 char* memb_name = NULL;
158 if((nmembs = H5Tget_nmembers(memtype)) < 0) {
159 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain number of HDF5 compound datatype.");
162 for(u = 0; u < (unsigned)nmembs; u++) {
164 if((memb_id = H5Tget_member_type(memtype, u)) < 0)
165 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain the datatype of an HDF5 compound datatype member.");
168 if((memb_cls = H5Tget_member_class (memtype, u)) < 0)
169 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain the datatype class of an HDF5 compound datatype member.");
172 memb_offset= H5Tget_member_offset(memtype,u);
175 memb_name = H5Tget_member_name(memtype,u);
176 if(memb_name == NULL)
177 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain the name of an HDF5 compound datatype member.");
179 if (memb_cls == H5T_COMPOUND) {
181 memb_h5s.do_structure_read(dsetid,memb_id,values,has_values,memb_offset+values_offset);
183 else if(memb_cls == H5T_ARRAY) {
186 int at_ndims = H5Tget_array_ndims(memb_id);
188 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain number of dimensions of the array datatype.");
191 vector<int> at_offset(at_ndims,0);
192 vector<int> at_count(at_ndims,0);
193 vector<int> at_step(at_ndims,0);
195 int at_nelms = h5_array_type.format_constraint(&at_offset[0],&at_step[0],&at_count[0]);
198 h5_array_type.do_h5_array_type_read(dsetid, memb_id,values,has_values,memb_offset+values_offset,
199 at_nelms,&at_offset[0],&at_count[0],&at_step[0]);
202 else if(memb_cls == H5T_INTEGER || memb_cls == H5T_FLOAT) {
203 if(
true == promote_char_to_short(memb_cls,memb_id)) {
204 void *src = (
void*)(&values[0] + values_offset +memb_offset);
206 memcpy(&val_int8,src,1);
207 short val_short=(short)val_int8;
208 var(memb_name)->val2buf(&val_short);
211 var(memb_name)->val2buf(&values[0] + values_offset +memb_offset);
215 else if(memb_cls == H5T_STRING) {
218 if(
true == H5Tis_variable_str(memb_id)) {
220 void *src = (
void*)(&values[0]+values_offset + memb_offset);
221 char*temp_bp = (
char*)src;
222 string final_str =
"";
223 get_vlen_str_data(temp_bp,final_str);
224 var(memb_name)->val2buf((
void*)&final_str);
229 void *src = (
void*)(&values[0]+values_offset + memb_offset);
230 vector<char> str_val;
231 size_t memb_size = H5Tget_size(memb_id);
232 if (memb_size == 0) {
235 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain the size of HDF5 compound datatype.");
237 str_val.resize(memb_size);
238 memcpy(&str_val[0],src,memb_size);
239 string temp_string(str_val.begin(),str_val.end());
240 var(memb_name)->val2buf(&temp_string);
253 throw InternalErr (__FILE__, __LINE__,
254 "Only support the field of compound datatype when the field type class is integer, float, string, array or compound..");
259 var(memb_name)->set_read_p(
true);
266 if((memtype != -1) && (mspace !=-1)) {
267 if(H5Dvlen_reclaim(memtype,mspace,H5P_DEFAULT,(
void*)&values[0])<0)
268 throw InternalErr(__FILE__, __LINE__,
"Unable to reclaim the compound datatype array.");
278 if(memb_name != NULL)
283 if((memtype != -1) && (mspace !=-1)) {
284 if(H5Dvlen_reclaim(memtype,mspace,H5P_DEFAULT,(
void*)&values[0])<0)
285 throw InternalErr(__FILE__, __LINE__,
"Unable to reclaim the compound datatype array.");
This class converts HDF5 compound type into DAP structure for the default option.
virtual bool read()
Reads HDF5 structure data by calling each member's read method in this structure.
HDF5Structure(const std::string &n, const std::string &vpath, const std::string &d)
Constructor.
HDF5Structure & operator=(const HDF5Structure &rhs)
Assignment operator for dynamic cast into generic Structure.
virtual libdap::BaseType * ptr_duplicate()
Data structure and retrieval processing header for the default option.