OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
HDFArray.cc
Go to the documentation of this file.
1 // This file is part of the hdf4 data handler for the OPeNDAP data server.
2 
3 // Copyright (c) 2005 OPeNDAP, Inc.
4 // Author: James Gallagher <jgallagher@opendap.org>
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 License
17 // along with this software; if not, write to the Free Software Foundation,
18 // 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 
23 // Copyright 1996, by the California Institute of Technology.
24 // ALL RIGHTS RESERVED. United States Government Sponsorship
25 // acknowledged. Any commercial use must be negotiated with the
26 // Office of Technology Transfer at the California Institute of
27 // Technology. This software may be subject to U.S. export control
28 // laws and regulations. By accepting this software, the user
29 // agrees to comply with all applicable U.S. export laws and
30 // regulations. User has the responsibility to obtain export
31 // licenses, or other export authority as may be required before
32 // exporting such information to foreign countries or providing
33 // access to foreign persons.
34 
35 // Author: Todd Karakashian, NASA/Jet Propulsion Laboratory
36 // Todd.K.Karakashian@jpl.nasa.gov
37 //
39 
40 #include "config_hdf.h"
41 //#define DODS_DEBUG 1
42 
43 #include <vector>
44 
45 // Include this on linux to suppres an annoying warning about multiple
46 // definitions of MIN and MAX.
47 #ifdef HAVE_SYS_PARAM_H
48 #include <sys/param.h>
49 #endif
50 #include <mfhdf.h>
51 
52 #include <hdfclass.h>
53 #include <hcstream.h>
54 
55 #include <escaping.h>
56 #include <Error.h>
57 #include <debug.h>
58 #include <BESDebug.h>
59 
60 #include "HDFArray.h"
61 #include "dhdferr.h"
62 
63 HDFArray::HDFArray(const string &n, const string &d, BaseType * v) :
64  Array(n, d, v) {
65 }
66 
68 }
69 
71  return new HDFArray(*this);
72 }
73 void LoadArrayFromSDS(HDFArray * ar, const hdf_sds & sds);
74 void LoadArrayFromGR(HDFArray * ar, const hdf_gri & gr);
75 
76 // Read in an Array from either an SDS or a GR in an HDF file.
78  int err = 0;
79  int status = read_tagref(-1, -1, err);
80 
81  if (err)
82  throw Error(unknown_error, "Could not read from dataset.");
83 
84  return status;
85 }
86 
87 // todo: refactor: get rid of the err value-result parameter; throw from
88 // within this method.
89 bool HDFArray::read_tagref(int32 tag, int32 ref, int &err) {
90  if (read_p())
91  return true;
92 
93  // get the HDF dataset name, SDS name
94  string hdf_file = dataset();
95  string hdf_name = this->name();
96 
97  // get slab constraint
98  vector<int> start, edge, stride;
99  bool isslab = GetSlabConstraint(start, edge, stride);
100 
101  bool foundsds = false;
102  hdf_sds sds;
103  if (tag == -1 || tag == DFTAG_NDG) {
104  if (SDSExists(hdf_file.c_str(), hdf_name.c_str())) {
105  hdfistream_sds sdsin(hdf_file.c_str());
106  if (ref != -1) {
107  BESDEBUG("h4", "sds seek with ref = " << ref << endl);
108  sdsin.seek_ref(ref);
109  } else {
110  BESDEBUG("h4", "sds seek with name = '" << hdf_name << "'" << endl);
111  sdsin.seek(hdf_name.c_str());
112  }
113  if (isslab)
114  sdsin.setslab(start, edge, stride, false);
115  sdsin >> sds;
116  sdsin.close();
117  foundsds = true;
118  }
119  }
120 
121  bool foundgr = false;
122  hdf_gri gr;
123  if (!foundsds && (tag == -1 || tag == DFTAG_VG)) {
124  if (GRExists(hdf_file.c_str(), hdf_name.c_str())) {
125  hdfistream_gri grin(hdf_file.c_str());
126  if (ref != -1)
127  grin.seek_ref(ref);
128  else
129  grin.seek(hdf_name.c_str());
130  if (isslab)
131  grin.setslab(start, edge, stride, false);
132  grin >> gr;
133  grin.close();
134  foundgr = true;
135  }
136  }
137 
138  // Todo: refactor: move this stuff up into the above if stmts.
139  if (foundsds)
140  LoadArrayFromSDS(this, sds);
141  else if (foundgr)
142  LoadArrayFromGR(this, gr);
143 
144  if (foundgr || foundsds) {
145  set_read_p(true); // Moved here; see bug 136
146  err = 0; // no error
147  return true;
148  } else {
149  err = 1;
150  return false;
151  }
152 }
153 
154 // Read the slab constraint parameters; the arrays start_array, edge_array,
155 // stride_array. Returns true if there is a slab constraint, false otherwise.
156 bool HDFArray::GetSlabConstraint(vector<int>&start_array,
157  vector<int>&edge_array, vector<int>&stride_array) {
158  int start = 0, stop = 0, stride = 0;
159  int edge = 0;
160 
161  start_array = vector<int> (0);
162  edge_array = vector<int> (0);
163  stride_array = vector<int> (0);
164 
165  for (Array::Dim_iter p = dim_begin(); p != dim_end(); ++p) {
166  start = dimension_start(p, true);
167  stride = dimension_stride(p, true);
168  stop = dimension_stop(p, true);
169  if (start == 0 && stop == 0 && stride == 0)
170  return false; // no slab constraint
171  if (start > stop)
173  edge = (int) ((stop - start) / stride) + 1;
174  if (start + edge > dimension_size(p))
176 
177  start_array.push_back(start);
178  edge_array.push_back(edge);
179  stride_array.push_back(stride);
180  }
181  return true;
182 }
183 
205 void HDFArray::transfer_attributes(AttrTable *at) {
206  BESDEBUG("h4","Transferring attributes for " << name() << endl);
207 
208  BaseType::transfer_attributes(at);
209 
210  BESDEBUG("h4","...Now looking for the " << name() << " _dim_n containers." << endl);
211 
212  // Here we should look for the *_dim_n where '*' is name() and n is 0, 1, ...
213  string dim_name_base = name() + "_dim_";
214 
215  AttrTable::Attr_iter a_p = at->attr_begin();
216  while (a_p != at->attr_end()) {
217  string::size_type i = at->get_name(a_p).find(dim_name_base);
218  // Found a matching container?
219  // To avoid matching both Month_dim_0 and DayOfMonth_dim_0, et c.,
220  // check that i == 0 and not just i != string::npos. jhrg 8/17/11
221  if (i == 0 && at->get_attr_type(a_p) == Attr_container) {
222  AttrTable *dim = at->get_attr_table(a_p);
223  try {
224  BESDEBUG("h4","Found a dimension container for " << name() << endl);
226  }
227  catch (Error &e) {
228  BESDEBUG("h4","Caught an error transferring dimension attribute " << dim->get_name() << " for variable " << name() << endl);
229  throw e;
230  }
231  }
232 
233  a_p++;
234  }
235 }
236 
238  // Mark the table as not global
239  dim->set_is_global_attribute(false);
240  // copy the table
241  AttrTable *at = new AttrTable(*dim);
242  // add it to this variable using just the 'dim_<digit>' part of the name
243  string name = at->get_name().substr(at->get_name().find("dim"));
244  get_attr_table().append_container(at, name);
245 }
246 
bool SDSExists(const char *filename, const char *sdsname)
Definition: hcutil.cc:94
virtual void transfer_dimension_attribute(AttrTable *dim)
Definition: HDFArray.cc:237
virtual bool read_tagref(int32 tag, int32 ref, int &error)
Definition: HDFArray.cc:89
virtual void seek_ref(int ref)
Definition: gri.cc:176
void LoadArrayFromGR(HDFArray *ar, const hdf_gri &gr)
Definition: hc2dap.cc:461
bool GRExists(const char *filename, const char *grname)
Definition: hcutil.cc:110
bool GetSlabConstraint(vector< int > &start_array, vector< int > &edge_array, vector< int > &stride_array)
Definition: HDFArray.cc:156
virtual void transfer_attributes(AttrTable *at_container)
Transfer attributes from a separately built DAS to the DDS.
Definition: HDFArray.cc:205
virtual ~HDFArray()
Definition: HDFArray.cc:67
#define THROW(x)
Definition: dhdferr.h:51
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
HDFArray(const string &n, const string &d, BaseType *v)
Definition: HDFArray.cc:63
void LoadArrayFromSDS(HDFArray *ar, const hdf_sds &sds)
Definition: hc2dap.cc:441
virtual bool read()
Definition: HDFArray.cc:77
virtual BaseType * ptr_duplicate()
Definition: HDFArray.cc:70