OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
HDF5Int16.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 1901 South First Street,
25 // Suite C-2, Champaign, IL 61820
26 
27 // #define DODS_DEBUG
28 
37 
38 
39 #include "config_hdf5.h"
40 #include "debug.h"
41 #include <string>
42 #include <ctype.h>
43 
44 #include "InternalErr.h"
45 
46 #include "h5dds.h"
47 #include "HDF5Int16.h"
48 #include "HDF5Structure.h"
49 
50 typedef struct s2_int16_t {
52  dods_int16 a;
53 } s2_int16_t;
54 
55 
56 HDF5Int16::HDF5Int16(const string & n, const string &d) : Int16(n, d)
57 {
58 }
59 
61 {
62 
63  return new HDF5Int16(*this);
64 }
65 
67 {
68  if (read_p())
69  return true;
70  if (get_dap_type(ty_id) == "Int8") {
71  dods_int16 buf;
72  dods_byte buf2;
73  get_data(dset_id, (void *) &buf2);
74  buf = (signed char) buf2;
75  set_read_p(true);
76  set_value(buf);
77 
78  // Release the handles.
79  if (H5Tclose(ty_id) < 0) {
80  throw InternalErr(__FILE__, __LINE__, "Unable to close the datatype.");
81  }
82  if (H5Dclose(dset_id) < 0) {
83  throw InternalErr(__FILE__, __LINE__, "Unable to close the dset.");
84  }
85 
86  }
87 
88  if (get_dap_type(ty_id) == "Int16") {
89  dods_int16 buf;
90  get_data(dset_id, (void *) &buf);
91 
92  set_read_p(true);
93  set_value(buf);
94  // Release the handles.
95  if (H5Tclose(ty_id) < 0) {
96  throw InternalErr(__FILE__, __LINE__, "Unable to close the datatype.");
97  }
98  if (H5Dclose(dset_id) < 0) {
99  throw InternalErr(__FILE__, __LINE__, "Unable to close the dset.");
100  }
101 
102 
103  }
104 
105  if (get_dap_type(ty_id) == "Structure") {
106 
107  BaseType *q = get_parent();
108  if (!q)
109  throw InternalErr(__FILE__, __LINE__, "null pointer");
110  HDF5Structure &p = static_cast<HDF5Structure &> (*q);
111 
112 #ifdef DODS_DEBUG
113  int i = H5Tget_nmembers(ty_id);
114  if(i < 0) {
115  throw InternalErr(__FILE__, __LINE__, "H5Tget_nmembers() failed.");
116  }
117 #endif
118  int j = 0;
119  int k = 0;
120 
121  hid_t s1_tid = H5Tcreate(H5T_COMPOUND, sizeof(s2_int16_t));
122  hid_t stemp_tid;
123 
124  if (s1_tid < 0) {
125  throw InternalErr(__FILE__, __LINE__, "cannot create a new datatype ");
126  }
127 
128  vector<s2_int16_t> buf(p.get_entire_array_size());
129  string myname = name();
130  string parent_name;
131 
132  DBG(cerr
133  << "=read() ty_id=" << ty_id
134  << " name=" << myname << " no of members =" << i << endl);
135  while (q != NULL) {
136 
137  if (q->is_constructor_type()) { // Grid, structure or sequence
138  if (k == 0) {
139  // Bottom level structure
140  DBG(cerr << "=read() my_name " << myname.
141  c_str() << endl);
142  if (H5Tinsert(s1_tid, myname.c_str(), HOFFSET(s2_int16_t, a), H5T_NATIVE_INT16) < 0) {
143  throw InternalErr(__FILE__, __LINE__, "Unable to add datatype.");
144  }
145  }
146  else {
147  DBG(cerr << k << "=read() parent_name=" << parent_name
148  << endl);
149 
150  stemp_tid = H5Tcreate(H5T_COMPOUND, sizeof(s2_int16_t));
151  if (stemp_tid < 0) {
152  throw InternalErr(__FILE__, __LINE__, "cannot create a new datatype");
153  }
154  if (H5Tinsert(stemp_tid, parent_name.c_str(), 0, s1_tid) < 0) {
155  throw InternalErr(__FILE__, __LINE__, "Unable to add datatype.");
156  }
157  s1_tid = stemp_tid;
158 
159  }
160  // Remember the last parent name.
161  parent_name = q->name();
162  p = static_cast<HDF5Structure &> (*q);
163  // Remember the index of array from the last parent.
164  j = p.get_array_index();
165  q = q->get_parent();
166 
167  }
168  else {
169  q = NULL;
170  }
171  k++;
172  } // while ()
173 
174 
175  if (H5Dread(dset_id, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buf[0]) < 0) {
176  // buf is deleted in the catch ... block below and
177  // should not be deleted here. pwest Mar 18, 2009
178  //delete[] buf;
179  throw InternalErr(__FILE__, __LINE__, "hdf5_dods server failed when getting int32 data for structure");
180  // string
181  // ()
182  // + Msgi);
183  }
184 
185  set_read_p(true);
186  DBG(cerr << "index " << j << endl);
187 
188  set_value(buf[j].a);
189  } // In case of structure
190 
191  return true;
192 }
193 
194 void HDF5Int16::set_did(hid_t dset)
195 {
196  dset_id = dset;
197 }
198 
199 void HDF5Int16::set_tid(hid_t type)
200 {
201  ty_id = type;
202 }
203 
205 {
206  return dset_id;
207 }
208 
210 {
211  return ty_id;
212 }
A class for HDF5 signed 16 bit integer type.
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.
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
HDF5Int16(const string &n, const string &d)
Constructor.
Definition: HDF5Int16.cc:56
hid_t get_tid()
returns HDF5 datatype id.
Definition: HDF5Int16.cc:209
#define NULL
Definition: wcsUtil.h:65
hid_t get_did()
returns HDF5 dataset id.
Definition: HDF5Int16.cc:204
void set_did(hid_t dset)
remembers HDF5 dataset id.
Definition: HDF5Int16.cc:194
struct s2_int16_t s2_int16_t
virtual BaseType * ptr_duplicate()
Clone this instance.
Definition: HDF5Int16.cc:60
int get_entire_array_size()
returns the entire array size of this Structure if it's a part of array of structures.
void set_tid(hid_t type)
remembers HDF5 datatype id.
Definition: HDF5Int16.cc:199
virtual bool read()
Reads HDF5 16-bit integer data into local buffer.
Definition: HDF5Int16.cc:66