OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
HDF5Byte.cc
Go to the documentation of this file.
1 // This file is part of hdf5_handler a HDF5 file handler for the OPeNDAP
2 // data server.
3 
4 // Author: Hyo-Kyung Lee <hyoklee@hdfgroup.org> and Muqun Yang
5 // <myang6@hdfgroup.org>
6 
7 // Copyright (c) 2009-2013 The HDF Group, Inc. and OPeNDAP, Inc.
8 //
9 // This is free software; you can redistribute it and/or modify it under the
10 // terms of the GNU Lesser General Public License as published by the Free
11 // Software Foundation; either version 2.1 of the License, or (at your
12 // option) any later version.
13 //
14 // This software is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 // License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 // You can contact The HDF Group, Inc. at 1800 South Oak Street,
25 // Suite 203, Champaign, IL 61820
26 
35 
36 
37 // #define DODS_DEBUG
38 #include <string>
39 #include <ctype.h>
40 
41 #include "config_hdf5.h"
42 #include "debug.h"
43 #include "InternalErr.h"
44 #include "h5dds.h"
45 #include "HDF5Byte.h"
46 #include "HDF5Structure.h"
47 
48 typedef struct s2_byte_t {
50  dods_byte a;
51 } s2_byte_t;
52 
53 
54 HDF5Byte::HDF5Byte(const string & n, const string &d):Byte(n, d)
55 {
56  ty_id = -1;
57  dset_id = -1;
58 }
59 
61 {
62  return new HDF5Byte(*this);
63 }
64 
66 {
67  if (read_p())
68  return true;
69 
70  if (get_dap_type(ty_id) == "Byte") {
71  dods_byte buf;
72  get_data(dset_id, (void *) &buf);
73  set_read_p(true);
74  set_value(buf);
75 
76  // Release the handles.
77  if (H5Tclose(ty_id) < 0) {
78  throw InternalErr(__FILE__, __LINE__, "Unable to close the datatype.");
79  }
80  if (H5Dclose(dset_id) < 0) {
81  throw InternalErr(__FILE__, __LINE__, "Unable to close the dset.");
82  }
83 
84  }
85 
86  if (get_dap_type(ty_id) == "Structure") {
87 
88  BaseType *q = get_parent();
89  if (!q)
90  throw InternalErr(__FILE__, __LINE__, "null pointer");
91  HDF5Structure &p = static_cast<HDF5Structure &> (*q);
92 
93  // char Msgi[256];
94 #ifdef DODS_DEBUG
95  int i = H5Tget_nmembers(ty_id);
96  if (i < 0) {
97  throw InternalErr(__FILE__, __LINE__, "Unable to retrieve the number of elements.");
98  }
99 #endif
100  int j = 0;
101  int k = 0;
102 
103  hid_t s1_tid = H5Tcreate(H5T_COMPOUND, sizeof(s2_byte_t));
104  hid_t stemp_tid;
105 
106  if (s1_tid < 0) {
107  throw InternalErr(__FILE__, __LINE__, "Cannot create a new datatype");
108  }
109  vector<s2_byte_t> buf(p.get_entire_array_size());
110  string myname = name();
111  string parent_name;
112 
113  DBG(cerr
114  << "=read() ty_id=" << ty_id
115  << " name=" << myname << " no of members =" << i << endl);
116  while (q != NULL) {
117 
118  if (q->is_constructor_type()) { // Grid, structure or sequence
119  if (k == 0) {
120  // Bottom level structure
121  DBG(cerr << "=read() my_name=" << myname.
122  c_str() << endl);
123  if (H5Tinsert(s1_tid, myname.c_str(), HOFFSET(s2_byte_t, a), H5T_NATIVE_CHAR) < 0) {
124  throw InternalErr(__FILE__, __LINE__, "Unable to add to datatype.");
125  }
126  }
127  else {
128  DBG(cerr << k << " parent_name=" << parent_name <<
129  endl);
130 
131  stemp_tid = H5Tcreate(H5T_COMPOUND, sizeof(s2_byte_t));
132  if (stemp_tid < 0) {
133  throw InternalErr(__FILE__, __LINE__, "cannot create a new datatype");
134  }
135 
136  if (H5Tinsert(stemp_tid, parent_name.c_str(), 0, s1_tid) < 0) {
137  throw InternalErr(__FILE__, __LINE__, "Unable to add to datatype.");
138  }
139  s1_tid = stemp_tid;
140 
141  }
142  // Remember the last parent name.
143  parent_name = q->name();
144  p = static_cast<HDF5Structure &> (*q);
145  // Remember the index of array from the last parent.
146  j = p.get_array_index();
147  q = q->get_parent();
148 
149  }
150  else {
151  q = NULL;
152  }
153  k++;
154  } // while ()
155 
156  if (H5Dread(dset_id, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buf[0]) < 0) {
157  // buf is deleted in the catch ... block below and
158  // should not be deleted here. pwest Mar 18, 2009
159  //delete[] buf;
160  throw InternalErr(__FILE__, __LINE__, "hdf5_dods server failed when getting int32 data for structure");
161  /*string
162  ("hdf5_dods server failed when getting int32 data for structure\n")
163  + Msgi); jhrg 3/16/11 */
164  }
165 
166  set_read_p(true);
167  DBG(cerr << "index " << j << endl);
168  set_value(buf[j].a);
169  } // In case of structure
170 
171  return true;
172 }
173 
174 void HDF5Byte::set_did(hid_t dset)
175 {
176  dset_id = dset;
177 }
178 
179 void HDF5Byte::set_tid(hid_t type)
180 {
181  ty_id = type;
182 }
183 
185 {
186  return dset_id;
187 }
188 
190 {
191  return ty_id;
192 }
hid_t get_did()
returns HDF5 dataset id.
Definition: HDF5Byte.cc:184
This class converts HDF5 compound type into DAP structure for the default option. ...
virtual bool read()
Reads HDF5 byte data into local buffer.
Definition: HDF5Byte.cc:65
struct s2_byte_t s2_byte_t
string get_dap_type(hid_t type)
returns the string representation of HDF5 type.
Definition: h5get.cc:237
int get_array_index()
returns the array index of this Structure if it's a part of array of structures.
HDF5Byte(const string &n, const string &d)
Constructor.
Definition: HDF5Byte.cc:54
Data structure and retrieval processing header for the default option.
virtual BaseType * ptr_duplicate()
Clone this instance.
Definition: HDF5Byte.cc:60
void get_data(hid_t dset, void *buf)
will get all data of a dset dataset and put it into buf.
Definition: h5get.cc:521
void set_did(hid_t dset)
remembers HDF5 dataset id.
Definition: HDF5Byte.cc:174
#define NULL
Definition: wcsUtil.h:65
void set_tid(hid_t type)
remembers HDF5 datatype id.
Definition: HDF5Byte.cc:179
This class provides a way to map HDF5 byte to DAP Byte for the default option.
int get_entire_array_size()
returns the entire array size of this Structure if it's a part of array of structures.
hid_t get_tid()
returns HDF5 datatype id.
Definition: HDF5Byte.cc:189