OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
HDFCFStr.cc
Go to the documentation of this file.
1 // This file is part of the hdf4_handler implementing for the CF-compliant
2 // Copyright (c) 2011-2013 The HDF Group, Inc. and OPeNDAP, Inc.
3 //
4 // This is free software; you can redistribute it and/or modify it under the
5 // terms of the GNU Lesser General Public License as published by the Free
6 // Software Foundation; either version 2.1 of the License, or (at your
7 // option) any later version.
8 //
9 // This software is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 // License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 //
18 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
19 // You can contact The HDF Group, Inc. at 1800 South Oak Street,
20 // Suite 203, Champaign, IL 61820
21 
30 
31 
32 
33 #include <iostream>
34 #include <sstream>
35 
36 #include "InternalErr.h"
37 #include "HDFCFStr.h"
38 #include <BESDebug.h>
39 #include "HDFCFUtil.h"
40 
41 
42 HDFCFStr::HDFCFStr(const int h4fd, int32 field_ref,const string &filename,const string &varname,const string &varnewname, bool is_vdata)
43  : Str(varnewname, filename),
44  filename(filename),
45  varname(varname),
46  h4fd(h4fd),
47  field_ref(field_ref),
48  is_vdata(is_vdata)
49 {
50 }
51 
53 {
54 }
56 {
57  return new HDFCFStr(*this);
58 }
59 
61 {
62 
63  BESDEBUG("h4","Coming to HDFCFStr read "<<endl);
64  string check_pass_fileid_key_str="H4.EnablePassFileID";
65  bool check_pass_fileid_key = false;
66  check_pass_fileid_key = HDFCFUtil::check_beskeys(check_pass_fileid_key_str);
67 
68  // SDS
69  if(false == is_vdata) {
70 
71  int32 sdid = -1;
72  if(false == check_pass_fileid_key) {
73  sdid = SDstart (const_cast < char *>(filename.c_str ()), DFACC_READ);
74  if (sdid < 0) {
75  ostringstream eherr;
76  eherr << "File " << filename.c_str () << " cannot be open.";
77  throw InternalErr (__FILE__, __LINE__, eherr.str ());
78  }
79  }
80  else
81  sdid = h4fd;
82 
83  //int32 sdid = h4fd;
84  int32 sdsid = 0;
85 
86  int32 sdsindex = SDreftoindex (sdid, field_ref);
87  if (sdsindex == -1) {
88  HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
89  ostringstream eherr;
90  eherr << "SDS index " << sdsindex << " is not right.";
91  throw InternalErr (__FILE__, __LINE__, eherr.str ());
92  }
93 
94  // Obtain this SDS ID.
95  sdsid = SDselect (sdid, sdsindex);
96  if (sdsid < 0) {
97  HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
98  ostringstream eherr;
99  eherr << "SDselect failed.";
100  throw InternalErr (__FILE__, __LINE__, eherr.str ());
101  }
102 
103  int32 dim_sizes[H4_MAX_VAR_DIMS];
104  int32 sds_rank, data_type, n_attrs;
105  char name[H4_MAX_NC_NAME];
106 
107  int32 r = 0;
108  r = SDgetinfo (sdsid, name, &sds_rank, dim_sizes,
109  &data_type, &n_attrs);
110  if(r == FAIL) {
111  SDendaccess(sdsid);
112  HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
113  ostringstream eherr;
114  eherr << "SDgetinfo failed.";
115  throw InternalErr (__FILE__, __LINE__, eherr.str ());
116  }
117 
118  if(sds_rank != 1) {
119  SDendaccess(sdsid);
120  HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
121  ostringstream eherr;
122  eherr << "The rank of string doesn't match with the rank of character array";
123  throw InternalErr (__FILE__, __LINE__, eherr.str ());
124 
125  }
126 
127  vector<int32>offset32;
128  offset32.resize(1);
129  vector<int32>count32;
130  count32.resize(1);
131  vector<int32>step32;
132  step32.resize(1);
133  offset32[0] = 0;
134  count32[0] = dim_sizes[0];
135  step32[0] = 1;
136 
137  vector<char>val;
138  val.resize(count32[0]);
139 
140  r = SDreaddata (sdsid, &offset32[0], &step32[0], &count32[0], &val[0]);
141  if (r != 0) {
142  SDendaccess (sdsid);
143  HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
144  ostringstream eherr;
145  eherr << "SDreaddata failed.";
146  throw InternalErr (__FILE__, __LINE__, eherr.str ());
147  }
148 
149  string final_str(val.begin(),val.end());
150  set_value(final_str);
151  SDendaccess(sdsid);
152  HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
153  }
154  else {
155 
156  int32 file_id = -1;
157 
158  if(true == check_pass_fileid_key)
159  file_id = h4fd;
160  else {
161  // Open the file
162  file_id = Hopen (filename.c_str (), DFACC_READ, 0);
163  if (file_id < 0) {
164  ostringstream eherr;
165  eherr << "File " << filename.c_str () << " cannot be open.";
166  throw InternalErr (__FILE__, __LINE__, eherr.str ());
167  }
168  }
169 
170 
171  // Start the Vdata interface
172  if (Vstart (file_id) < 0) {
173  HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
174  ostringstream eherr;
175  eherr << "This file cannot be open.";
176  throw InternalErr (__FILE__, __LINE__, eherr.str ());
177  }
178 
179  // Attach the vdata
180  int32 vdref = field_ref;
181  int32 vdata_id = VSattach (file_id, vdref, "r");
182  if (vdata_id == -1) {
183  Vend (file_id);
184  HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
185  ostringstream eherr;
186  eherr << "Vdata cannot be attached.";
187  throw InternalErr (__FILE__, __LINE__, eherr.str ());
188  }
189 
190  int32 num_rec = VSelts(vdata_id);
191  if (num_rec == -1) {
192  VSdetach (vdata_id);
193  Vend (file_id);
194 
195  HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
196  ostringstream eherr;
197  eherr << "The number of elements from this vdata cannot be obtained.";
198  throw InternalErr (__FILE__, __LINE__, eherr.str ());
199  }
200 
201 
202  int32 r = -1;
203 
204  // Seek the position of the starting point
205  if (VSseek (vdata_id, 0) == -1) {
206  VSdetach (vdata_id);
207  Vend (file_id);
208  HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
209  ostringstream eherr;
210  eherr << "VSseek failed at " << 0;
211  throw InternalErr (__FILE__, __LINE__, eherr.str ());
212  }
213 
214  // Prepare the vdata field
215  if (VSsetfields (vdata_id, varname.c_str ()) == -1) {
216  VSdetach (vdata_id);
217  Vend (file_id);
218  HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
219  ostringstream eherr;
220  eherr << "VSsetfields failed with the name " << varname;
221  throw InternalErr (__FILE__, __LINE__, eherr.str ());
222  }
223  vector <char> val;
224  val.resize(num_rec);
225 
226  // Read the data
227  r = VSread (vdata_id, (uint8 *) &val[0], num_rec,
228  FULL_INTERLACE);
229 
230 
231  string final_str(val.begin(),val.end());
232  set_value(final_str);
233  if (VSdetach (vdata_id) == -1) {
234  Vend (file_id);
235  HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
236  ostringstream eherr;
237  eherr << "VSdetach failed.";
238  throw InternalErr (__FILE__, __LINE__, eherr.str ());
239  }
240 
241  if (Vend (file_id) == -1) {
242  HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
243  ostringstream eherr;
244  eherr << "VSdetach failed.";
245  throw InternalErr (__FILE__, __LINE__, eherr.str ());
246  }
247 
248  HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
249 //#endif
250  }
251  return true;
252 }
virtual bool read()
Definition: HDFCFStr.cc:60
static void close_fileid(int32 sdfd, int32 file_id, int32 gridfd, int32 swathfd, bool pass_fileid_key)
Close HDF4 and HDF-EOS2 file IDs. For performance reasons, we want to keep HDF-EOS2/HDF4 IDs open for...
Definition: HDFCFUtil.cc:2881
HDFCFStr(const int h4fd, int32 field_ref, const std::string &filename, const std::string &varname, const std::string &varnewname, bool is_vdata)
Definition: HDFCFStr.cc:42
virtual ~HDFCFStr()
Definition: HDFCFStr.cc:52
virtual BaseType * ptr_duplicate()
Definition: HDFCFStr.cc:55
This class provides a way to map HDF4 1-D character array to DAP Str for the CF option.
static bool check_beskeys(const std::string &key)
Check the BES key. This function will check a BES key specified at the file h4.conf.in. If the key's value is either true or yes. The handler claims to find a key and will do some operations. Otherwise, will do different operations. For example, One may find a line H4.EnableCF=true at h4.conf.in. That means, the HDF4 handler will handle the HDF4 files by following CF conventions.
Definition: HDFCFUtil.cc:17
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64