bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
HDF5CFInt16.cc
Go to the documentation of this file.
1// This file is part of hdf5_handler: an HDF5 file handler for the OPeNDAP
2// data server.
3
4// Copyright (c) 2011-2023 The HDF Group, Inc. and OPeNDAP, Inc.
5//
6// This is free software; you can redistribute it and/or modify it under the
7// terms of the GNU Lesser General Public License as published by the Free
8// Software Foundation; either version 2.1 of the License, or (at your
9// option) any later version.
10//
11// This software is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14// License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public
17// License along with this library; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
21// You can contact The HDF Group, Inc. at 410 E University Ave,
22// Suite 200, Champaign, IL 61820
23
32
33
34#include <memory>
35#include <libdap/InternalErr.h>
36#include "HDF5CFInt16.h"
37#include <BESDebug.h>
38#include "h5common.h"
39
40using namespace std;
41using namespace libdap;
42
43HDF5CFInt16::HDF5CFInt16(const string &n, const string &d) : Int16(n, d)
44{
45
46}
47
48HDF5CFInt16::HDF5CFInt16(const string &n, const string &d,const string &d_f) : Int16(n, d),filename(d_f)
49{
50}
51
52BaseType *HDF5CFInt16::ptr_duplicate()
53{
54 auto HDF5CFInt16_unique = make_unique<HDF5CFInt16>(*this);
55 return HDF5CFInt16_unique.release();
56}
57
58bool HDF5CFInt16::read()
59{
60 BESDEBUG("h5","Coming to HDF5CFInt16 read "<<endl);
61
62 if (read_p())
63 return true;
64
65 hid_t file_id = H5Fopen(filename.c_str(),H5F_ACC_RDONLY,H5P_DEFAULT);
66 if(file_id < 0) {
67 throw InternalErr(__FILE__,__LINE__, "Fail to obtain the HDF5 file ID .");
68 }
69
70 hid_t dset_id = -1;
71 dset_id = H5Dopen2(file_id,dataset().c_str(),H5P_DEFAULT);
72 if(dset_id < 0) {
73 H5Fclose(file_id);
74 throw InternalErr(__FILE__,__LINE__, "Fail to obtain the dataset .");
75 }
76
77 hid_t dtypeid = H5Dget_type(dset_id);
78 if(dtypeid < 0) {
79 H5Dclose(dset_id);
80 H5Fclose(file_id);
81 throw InternalErr(__FILE__,__LINE__, "Fail to obtain the datatype .");
82 }
83
84 hid_t memtype = H5Tget_native_type(dtypeid, H5T_DIR_ASCEND);
85 if (memtype < 0){
86 H5Tclose(dtypeid);
87 H5Dclose(dset_id);
88 H5Fclose(file_id);
89 throw InternalErr(__FILE__, __LINE__, "Cannot obtain the native datatype.");
90 }
91
92 try {
93
94 dods_int16 buf;
95 if(1 == H5Tget_size(memtype) && H5T_SGN_2 == H5Tget_sign(memtype)) {
96 signed char buf2;
97 get_data(dset_id,(void*)&buf2);
98 buf =(short)buf2;
99 }
100 else
101 get_data(dset_id, (void *) &buf);
102
103 set_read_p(true);
104 set_value(buf);
105
106 if(H5Tclose(memtype) < 0) {
107 throw InternalErr(__FILE__, __LINE__, "Unable to close the memory datatype.");
108 }
109 if(H5Tclose(dtypeid) < 0) {
110 throw InternalErr(__FILE__, __LINE__, "Unable to close the datatype id.");
111 }
112 // Release the handles.
113 if (H5Dclose(dset_id) < 0) {
114 throw InternalErr(__FILE__, __LINE__, "Unable to close the dset.");
115 }
116 H5Fclose(file_id);
117 }
118 catch(...) {
119 H5Tclose(memtype);
120 H5Tclose(dtypeid);
121 H5Dclose(dset_id);
122 H5Fclose(file_id);
123 throw;
124 }
125
126 return true;
127
128}
129
This class provides a way to map HDF5 int16 to DAP int16 for the CF option.
void get_data(hid_t dset, void *buf)