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