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