OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
HDF5UInt16.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 
34 
35 
36 #include "config_hdf5.h"
37 #include "debug.h"
38 #include <string>
39 #include <ctype.h>
40 
41 #include "InternalErr.h"
42 
43 #include "h5dds.h"
44 #include "HDF5UInt16.h"
45 #include "HDF5Structure.h"
46 
47 typedef struct s2_uint16_t {
49  dods_int16 a;
50 } s2_uint16_t;
51 
52 
53 HDF5UInt16::HDF5UInt16(const string & n, const string &d) : UInt16(n, d)
54 {
55  ty_id = -1;
56  dset_id = -1;
57 
58 }
59 
61 {
62 
63  return new HDF5UInt16(*this);
64 }
65 
67 {
68  if (read_p())
69  return true;
70 
71  if (get_dap_type(ty_id) == "UInt16") {
72  dods_uint16 buf;
73  get_data(dset_id, (void *) &buf);
74  set_read_p(true);
75  set_value(buf);
76 
77  // Release the handles.
78  if (H5Tclose(ty_id) < 0) {
79  throw InternalErr(__FILE__, __LINE__, "Unable to close the datatype.");
80  }
81  if (H5Dclose(dset_id) < 0) {
82  throw InternalErr(__FILE__, __LINE__, "Unable to close the dset.");
83  }
84 
85  }
86 
87  if (get_dap_type(ty_id) == "Structure") {
88 
89  BaseType *q = get_parent();
90  if (!q)
91  throw InternalErr(__FILE__, __LINE__, "null pointer");
92  HDF5Structure &p = static_cast<HDF5Structure &> (*q);
93 
94 #ifdef DODS_DEBUG
95  int i = H5Tget_nmembers(ty_id);
96  if (i < 0) {
97  throw InternalErr(__FILE__, __LINE__, "H5Tget_nmembers() failed.");
98  }
99 #endif
100  int j = 0;
101  int k = 0;
102 
103  hid_t s1_tid = H5Tcreate(H5T_COMPOUND, sizeof(s2_uint16_t));
104  hid_t stemp_tid;
105 
106  if (s1_tid < 0) {
107  throw InternalErr(__FILE__, __LINE__, "cannot create a new datatype");
108  }
109 
110  vector<s2_uint16_t> buf(p.get_entire_array_size());
111  string myname = name();
112  string parent_name;
113 
114  DBG(cerr
115  << "=read() ty_id=" << ty_id
116  << " name=" << myname << " no of members =" << i << endl);
117  while (q != NULL) {
118 
119  if (q->is_constructor_type()) { // Grid, structure or sequence
120  if (k == 0) {
121  // Bottom level structure
122  DBG(cerr << "=read() my_name " << myname.
123  c_str() << endl);
124  if (H5Tinsert(s1_tid, myname.c_str(), HOFFSET(s2_uint16_t, a), H5T_NATIVE_UINT16) < 0) {
125  throw InternalErr(__FILE__, __LINE__, "Unable to add datatype.");
126  }
127  }
128  else {
129  DBG(cerr << k << "=read() parent_name=" << parent_name
130  << endl);
131 
132  stemp_tid = H5Tcreate(H5T_COMPOUND, sizeof(s2_uint16_t));
133  if (stemp_tid < 0) {
134  throw InternalErr(__FILE__, __LINE__, "cannot create a new datatype");
135  }
136  if (H5Tinsert(stemp_tid, parent_name.c_str(), 0, s1_tid) < 0) {
137  throw InternalErr(__FILE__, __LINE__, "Unable to add 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 
157  if (H5Dread(dset_id, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buf[0]) < 0) {
158  // this should not be called here. The exception on
159  // the next line is thrown and caught below. In the
160  // catch block the buf is deleted. pcw Mar 18, 2009
161  //delete[] buf;
162  throw InternalErr(__FILE__, __LINE__, "hdf5_dods server failed when getting int32 data for structure");
163  // string
164  // (
165  // + Msgi);
166  }
167 
168  set_read_p(true);
169  DBG(cerr << "index " << j << endl);
170  set_value(buf[j].a);
171  } // In case of structure
172 
173  return true;
174 }
175 
176 void HDF5UInt16::set_did(hid_t dset)
177 {
178  dset_id = dset;
179 }
180 
181 void HDF5UInt16::set_tid(hid_t type)
182 {
183  ty_id = type;
184 }
185 
187 {
188  return dset_id;
189 }
190 
192 {
193  return ty_id;
194 }
virtual BaseType * ptr_duplicate()
Clone this instance.
Definition: HDF5UInt16.cc:60
void set_tid(hid_t type)
remembers HDF5 datatype id.
Definition: HDF5UInt16.cc:181
This class converts HDF5 compound type into DAP structure for the default option. ...
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.
void set_did(hid_t dset)
remembers HDF5 dataset id.
Definition: HDF5UInt16.cc:176
Data structure and retrieval processing header for the default option.
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
struct s2_uint16_t s2_uint16_t
#define NULL
Definition: wcsUtil.h:65
This class provides a way to map unsigned HDF5 16 bit integer to DAP UInt16 for the default option...
hid_t get_tid()
returns HDF5 datatype id.
Definition: HDF5UInt16.cc:191
hid_t get_did()
returns HDF5 dataset id.
Definition: HDF5UInt16.cc:186
HDF5UInt16(const string &n, const string &d)
Constructor.
Definition: HDF5UInt16.cc:53
int get_entire_array_size()
returns the entire array size of this Structure if it's a part of array of structures.
virtual bool read()
Reads HDF5 unsigned 16-bit integer data into local buffer.
Definition: HDF5UInt16.cc:66