bes Updated for version 3.20.13
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-2016 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
27// #define DODS_DEBUG
28
37
38
39#include "config_hdf5.h"
40#include "BESDebug.h"
41#include <string>
42#include <ctype.h>
43
44#include <libdap/InternalErr.h>
45
46#include "h5dds.h"
47#include "HDF5Int16.h"
48
49using namespace std;
50using namespace libdap;
51
52#if 0
53typedef struct s2_int16_t {
55 dods_int16 a;
56} s2_int16_t;
57
58#endif
59
60HDF5Int16::HDF5Int16(const string & n, const string &vpath,const string &d) : Int16(n, d),var_path(vpath)
61{
62}
63
65{
66
67 return new HDF5Int16(*this);
68}
69
71{
72 if (read_p())
73 return true;
74
75 hid_t file_id = H5Fopen(dataset().c_str(),H5F_ACC_RDONLY,H5P_DEFAULT);
76 if(file_id < 0) {
77 throw InternalErr(__FILE__,__LINE__, "Fail to obtain the HDF5 file ID .");
78 }
79
80 hid_t dset_id = -1;
81 if(true == is_dap4())
82 dset_id = H5Dopen2(file_id,var_path.c_str(),H5P_DEFAULT);
83 else
84 dset_id = H5Dopen2(file_id,name().c_str(),H5P_DEFAULT);
85
86 if(dset_id < 0) {
87 H5Fclose(file_id);
88 throw InternalErr(__FILE__,__LINE__, "Fail to obtain the datatype .");
89 }
90
91 hid_t dtypeid = H5Dget_type(dset_id);
92 if(dtypeid < 0) {
93 H5Dclose(dset_id);
94 H5Fclose(file_id);
95 throw InternalErr(__FILE__,__LINE__, "Fail to obtain the datatype .");
96 }
97
98 hid_t memtype = H5Tget_native_type(dtypeid, H5T_DIR_ASCEND);
99
100 if (memtype < 0){
101 H5Tclose(dtypeid);
102 H5Dclose(dset_id);
103 H5Fclose(file_id);
104 throw InternalErr(__FILE__, __LINE__, "Cannot obtain the native datatype.");
105 }
106
107 try {
108 if(false == is_dap4()) {
109 if (1 == H5Tget_size(memtype) && H5T_SGN_2 == H5Tget_sign(memtype)) {
110 dods_int16 buf;
111 signed char buf2; // Needs to be corrected with signed int8 buffer.
112 get_data(dset_id, (void *) &buf2);
113 buf = (short) buf2;
114 set_read_p(true);
115 set_value(buf);
116
117 }
118
119 else if (get_dap_type(memtype,false) == "Int16") {
120 dods_int16 buf;
121 get_data(dset_id, (void *) &buf);
122
123 set_read_p(true);
124 set_value(buf);
125
126 }
127 }
128 else {
129 dods_int16 buf;
130 get_data(dset_id, (void *) &buf);
131
132 set_read_p(true);
133 set_value(buf);
134
135 }
136
137 // Release the handles.
138 if (H5Tclose(memtype) < 0) {
139 throw InternalErr(__FILE__, __LINE__, "Unable to close the datatype.");
140 }
141 H5Tclose(dtypeid);
142 if (H5Dclose(dset_id) < 0) {
143 throw InternalErr(__FILE__, __LINE__, "Unable to close the dset.");
144 }
145
146 H5Fclose(file_id);
147
148
149 }
150
151 catch(...) {
152 H5Tclose(memtype);
153 H5Tclose(dtypeid);
154 H5Dclose(dset_id);
155 H5Fclose(file_id);
156 throw;
157 }
158 return true;
159}
160
A class for HDF5 signed 16 bit integer type.
HDF5Int16(const std::string &n, const std::string &vpath, const std::string &d)
Constructor.
Definition: HDF5Int16.cc:60
bool read() override
Reads HDF5 16-bit integer data into local buffer.
Definition: HDF5Int16.cc:70
libdap::BaseType * ptr_duplicate() override
Definition: HDF5Int16.cc:64
void get_data(hid_t dset, void *buf)
Definition: h5common.cc:50
Data structure and retrieval processing header for the default option.
string get_dap_type(hid_t type, bool is_dap4)
Definition: h5get.cc:292