bes  Updated for version 3.20.6
HDFEOS2CFStr.cc
1 // This file is part of the hdf4 data handler for the OPeNDAP data server.
3 // It retrieves the HDF-EOS2 swath or grid DFNT_CHAR 1D array field and
4 // then send to DAP as DAP string for the CF option.
5 // Authors: MuQun Yang <myang6@hdfgroup.org>
6 // Copyright (c) 2010-2012 The HDF Group
8 
9 #ifdef USE_HDFEOS2_LIB
10 #include <iostream>
11 #include <sstream>
12 #include <cassert>
13 #include <debug.h>
14 #include "InternalErr.h"
15 #include <BESDebug.h>
16 #include <BESLog.h>
17 
18 #include "HDFCFUtil.h"
19 #include "HDFEOS2CFStr.h"
20 #include "HDF4RequestHandler.h"
21 
22 using namespace std;
23 using namespace libdap;
24 
25 HDFEOS2CFStr::HDFEOS2CFStr(const int gsfd,
26  const std::string &filename,
27  const std::string &objname,
28  const std::string &varname,
29  const std::string &varnewname,
30  int grid_or_swath)
31  :Str(varnewname,filename),
32  gsfd(gsfd),
33  filename(filename),
34  objname(objname),
35  varname(varname),
36  grid_or_swath(grid_or_swath)
37 {
38 }
39 
40 HDFEOS2CFStr::~HDFEOS2CFStr()
41 {
42 }
43 BaseType *HDFEOS2CFStr::ptr_duplicate()
44 {
45  return new HDFEOS2CFStr(*this);
46 }
47 
48 bool
49 HDFEOS2CFStr::read ()
50 {
51 
52  BESDEBUG("h4","Coming to HDFEOS2CFStr read "<<endl);
53 
54 #if 0
55  string check_pass_fileid_key_str="H4.EnablePassFileID";
56  bool check_pass_fileid_key = false;
57  check_pass_fileid_key = HDFCFUtil::check_beskeys(check_pass_fileid_key_str);
58 #endif
59  bool check_pass_fileid_key = HDF4RequestHandler::get_pass_fileid();
60 
61 
62  int32 (*openfunc) (char *, intn);
63  intn (*closefunc) (int32);
64  int32 (*attachfunc) (int32, char *);
65  intn (*detachfunc) (int32);
66  intn (*fieldinfofunc) (int32, char *, int32 *, int32 *, int32 *, char *);
67  intn (*readfieldfunc) (int32, char *, int32 *, int32 *, int32 *, void *);
68 
69 
70  // Define function pointers to handle the swath
71  if(grid_or_swath == 0) {
72  openfunc = GDopen;
73  closefunc = GDclose;
74  attachfunc = GDattach;
75  detachfunc = GDdetach;
76  fieldinfofunc = GDfieldinfo;
77  readfieldfunc = GDreadfield;
78 
79  }
80  else {
81  openfunc = SWopen;
82  closefunc = SWclose;
83  attachfunc = SWattach;
84  detachfunc = SWdetach;
85  fieldinfofunc = SWfieldinfo;
86  readfieldfunc = SWreadfield;
87  }
88 
89  int32 gfid = -1;
90  if (false == check_pass_fileid_key) {
91 
92  // Obtain the EOS object ID(either grid or swath)
93  gfid = openfunc (const_cast < char *>(filename.c_str ()), DFACC_READ);
94  if (gfid < 0) {
95  ostringstream eherr;
96  eherr << "File " << filename.c_str () << " cannot be open.";
97  throw InternalErr (__FILE__, __LINE__, eherr.str ());
98  }
99 
100  }
101  else
102  gfid = gsfd;
103 
104  int32 gsid = attachfunc (gfid, const_cast < char *>(objname.c_str ()));
105  if (gsid < 0) {
106  if(false == check_pass_fileid_key)
107  closefunc(gfid);
108  ostringstream eherr;
109  eherr << "Grid/Swath " << objname.c_str () << " cannot be attached.";
110  throw InternalErr (__FILE__, __LINE__, eherr.str ());
111  }
112 
113  // Initialize the temp. returned value.
114  intn r = 0;
115  int32 tmp_rank = 0;
116  char tmp_dimlist[1024];
117  int32 tmp_dims[1];
118  int32 field_dtype = 0;
119 
120  r = fieldinfofunc (gsid, const_cast < char *>(varname.c_str ()),
121  &tmp_rank, tmp_dims, &field_dtype, tmp_dimlist);
122  if (r != 0) {
123  detachfunc(gsid);
124  if(false == check_pass_fileid_key)
125  closefunc(gfid);
126  ostringstream eherr;
127  eherr << "Field " << varname.c_str () << " information cannot be obtained.";
128  throw InternalErr (__FILE__, __LINE__, eherr.str ());
129  }
130 
131 
132  vector<int32>offset32;
133  offset32.resize(1);
134  vector<int32>count32;
135  count32.resize(1);
136  vector<int32>step32;
137  step32.resize(1);
138  offset32[0] = 0;
139  count32[0] = tmp_dims[0];
140  step32[0] = 1;
141 
142  vector<char>val;
143  val.resize(count32[0]);
144 
145  r = readfieldfunc(gsid,const_cast<char*>(varname.c_str()),
146  &offset32[0], &step32[0], &count32[0], &val[0]);
147 
148  if (r != 0) {
149  detachfunc(gsid);
150  if(false == check_pass_fileid_key)
151  closefunc(gfid);
152  ostringstream eherr;
153  eherr << "swath or grid readdata failed.";
154  throw InternalErr (__FILE__, __LINE__, eherr.str ());
155  }
156 
157  string final_str(val.begin(),val.end());
158  set_value(final_str);
159  detachfunc(gsid);
160  if(false == check_pass_fileid_key)
161  closefunc(gfid);
162  return false;
163 }
164 
165 
166 #endif
HDFEOS2CFStr.h
This class provides a way to map HDFEOS2 1-D character array to DAP Str for the CF option.
libdap
Definition: BESDapFunctionResponseCache.h:35