OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
HDF5GMCFFillIndexArray.cc
Go to the documentation of this file.
1 // This file is part of the hdf5_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 
29 
33 
34 #include "config_hdf5.h"
35 #include <iostream>
36 #include <sstream>
37 #include <cassert>
38 #include <debug.h>
39 #include "InternalErr.h"
40 #include <BESDebug.h>
41 
42 #include "Str.h"
43 #include "HDF5GMCFFillIndexArray.h"
44 
45 
46 
48 {
49  return new HDF5GMCFFillIndexArray(*this);
50 }
51 
52 // Read in an HDF5 Array
54 {
55 
56  BESDEBUG("h5","Coming to HDF5GMCFFillIndexArray read "<<endl);
57  int nelms = 0;
58 
59 #if 0
60 cerr<<"coming to read function"<<endl;
61 cerr<<"file name " <<filename <<endl;
62 cerr <<"var name "<<varname <<endl;
63 #endif
64 
65  if (rank != 1)
66  throw InternalErr (__FILE__, __LINE__,
67  "Currently the rank of the dimension scale must be 1.");
68 
69  vector<int> offset;
70  offset.resize(rank);
71  vector<int>count;
72  count.resize(rank);
73  vector<int>step;
74  step.resize(rank);
75 
76  // Obtain the number of the subsetted elements
77  nelms = format_constraint (&offset[0], &step[0], &count[0]);
78 
79 
80  switch (dtype) {
81 
82  case H5UCHAR:
83 
84  {
85  vector<unsigned char> val;
86  val.resize(nelms);
87 
88  for (int i = 0; i < count[0]; i++)
89  val[i] = offset[0] + step[0] * i;
90 
91  set_value ((dods_byte *) &val[0], nelms);
92  } // case H5UCHAR
93  break;
94 
95 
96  // signed char maps to 16-bit integer in DAP2(HDF5 to DAP2 mapping document.)
97  case H5CHAR:
98  case H5INT16:
99  {
100 
101  vector<short>val;
102  val.resize(nelms);
103 
104  for (int i = 0; i < count[0]; i++)
105  val[i] = offset[0] + step[0] * i;
106 
107  set_value ((dods_int16 *) &val[0], nelms);
108  }// H5CHAR and H5INT16
109  break;
110 
111 
112  case H5UINT16:
113  {
114  vector<unsigned short> val;
115  val.resize(nelms);
116 
117  for (int i = 0; i < count[0]; i++)
118  val[i] = offset[0] + step[0] * i;
119 
120  set_value ((dods_uint16 *) &val[0], nelms);
121  } // H5UINT16
122  break;
123 
124 
125  case H5INT32:
126  {
127  vector<int>val;
128  val.resize(nelms);
129 
130  for (int i = 0; i < count[0]; i++)
131  val[i] = offset[0] + step[0] * i;
132 
133  set_value ((dods_int32 *) &val[0], nelms);
134  } // case H5INT32
135  break;
136 
137  case H5UINT32:
138  {
139  vector<unsigned int>val;
140  val.resize(nelms);
141 
142  for (int i = 0; i < count[0]; i++)
143  val[i] = offset[0] + step[0] * i;
144 
145  set_value ((dods_uint32 *) &val[0], nelms);
146  }
147  break;
148 
149  case H5FLOAT32:
150  {
151 
152  vector<float>val;
153  val.resize(nelms);
154 
155  for (int i = 0; i < count[0]; i++)
156  val[i] = offset[0] + step[0] * i;
157 
158  set_value ((dods_float32 *) &val[0], nelms);
159  }
160  break;
161 
162 
163  case H5FLOAT64:
164  {
165 
166  vector<double>val;
167  val.resize(nelms);
168 
169  for (int i = 0; i < count[0]; i++)
170  val[i] = offset[0] + step[0] * i;
171 
172  set_value ((dods_float64 *) &val[0], nelms);
173  } // case H5FLOAT64
174  break;
175 
176 
177  case H5FSTRING:
178  case H5VSTRING:
179  default:
180  {
181  ostringstream eherr;
182  eherr << "Currently the dimension scale datatype cannot be string"<<endl;
183  throw InternalErr (__FILE__, __LINE__, eherr.str ());
184  }
185  break;
186 
187  }
188 
189  return true;
190 }
191 
192 
193 // parse constraint expr. and make hdf5 coordinate point location.
194 // return number of elements to read.
195 int
196 HDF5GMCFFillIndexArray::format_constraint (int *offset, int *step, int *count)
197 {
198  long nels = 1;
199  int id = 0;
200 
201  Dim_iter p = dim_begin ();
202 
203  while (p != dim_end ()) {
204 
205  int start = dimension_start (p, true);
206  int stride = dimension_stride (p, true);
207  int stop = dimension_stop (p, true);
208 
209 
210  // Check for illegical constraint
211  if (stride < 0 || start < 0 || stop < 0 || start > stop) {
212  ostringstream oss;
213 
214  oss << "Array/Grid hyperslab indices are bad: [" << start <<
215  ":" << stride << ":" << stop << "]";
216  throw Error (malformed_expr, oss.str ());
217  }
218 
219  // Check for an empty constraint and use the whole dimension if so.
220  if (start == 0 && stop == 0 && stride == 0) {
221  start = dimension_start (p, false);
222  stride = dimension_stride (p, false);
223  stop = dimension_stop (p, false);
224  }
225 
226  offset[id] = start;
227  step[id] = stride;
228  count[id] = ((stop - start) / stride) + 1; // count of elements
229  nels *= count[id]; // total number of values for variable
230 
231  BESDEBUG ("h5",
232  "=format_constraint():"
233  << "id=" << id << " offset=" << offset[id]
234  << " step=" << step[id]
235  << " count=" << count[id]
236  << endl);
237 
238  id++;
239  p++;
240  }
241 
242  return nels;
243 }
244 
int format_constraint(int *cor, int *step, int *edg)
HDF5GMCFFillIndexArray(int rank, H5DataType dtype, const string &n="", BaseType *v=0)
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
This class includes the methods to read data array into DAP buffer from an HDF5 dataset for the CF op...
virtual BaseType * ptr_duplicate()