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