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