bes  Updated for version 3.20.6
ugrid_utils.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5 
6 // Copyright (c) 2002,2003,2011,2012 OPeNDAP, Inc.
7 // Authors: Nathan Potter <ndp@opendap.org>
8 // James Gallagher <jgallagher@opendap.org>
9 // Scott Moe <smeest1@gmail.com>
10 // Bill Howe <billhowe@cs.washington.edu>
11 //
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 //
26 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
27 
28 //#define DODS_DEBUG
29 
30 #include "config.h"
31 
32 #include <vector>
33 #include <sstream>
34 
35 #include <gridfields/array.h>
36 
37 #include "BaseType.h"
38 #include "Array.h"
39 #include "debug.h"
40 #include "util.h"
41 
42 #include "BESDebug.h"
43 
44 #include "ugrid_utils.h"
45 
46 #ifdef NDEBUG
47 #undef BESDEBUG
48 #define BESDEBUG( x, y )
49 #endif
50 
51 using namespace std;
52 using namespace libdap;
53 
54 namespace ugrid {
55 
56 GF::e_Type getGridfieldsInternalTypeMap(Type type)
57 {
58 
59  switch (type) {
60  case dods_byte_c:
61  case dods_uint16_c:
62  case dods_int16_c:
63  case dods_uint32_c:
64  case dods_int32_c: {
65  return GF::INT;
66  break;
67  }
68  case dods_float32_c:
69  case dods_float64_c: {
70  return GF::FLOAT;
71  break;
72  }
73  default:
74  throw InternalErr(__FILE__, __LINE__,
75  "Unknown DAP type encountered when converting to gridfields internal type.");
76  }
77 }
78 
79 Type getGridfieldsReturnType(Type type)
80 {
81  GF::e_Type gfInternalType = getGridfieldsInternalTypeMap(type);
82 
83  Type retType;
84  switch (gfInternalType) {
85  case GF::INT: {
86  retType = dods_int32_c;
87  break;
88  }
89  case GF::FLOAT: {
90  retType = dods_float64_c;
91  break;
92  }
93  default:
94  throw InternalErr(__FILE__, __LINE__,
95  "Unknown GF::e_Type type encountered when resolving gridfields result type mapping for dap type "
96  + libdap::type_name(type));
97  }DBG(cerr << " getGridfieldsReturnType() - Return type for " << libdap::type_name(type) <<
98  " is " << libdap::type_name(retType) << endl);
99 
100  return retType;
101 }
102 
103 Type getGridfieldsReturnType(libdap::Array *a)
104 {
105  return getGridfieldsReturnType(a->var()->type());
106 }
107 
108 GF::Array *newGFIndexArray(string name, long size, vector<int*> *sharedIntArrays)
109 {
110  GF::Array *gfa = new GF::Array(name, GF::INT);
111  int *values = new int[size];
112  for (long i = 0; i < size; i++) {
113  values[i] = i;
114  }
115  gfa->shareIntData(values, size);
116  sharedIntArrays->push_back(values);
117  return gfa;
118 }
119 
130 GF::Array *extractGridFieldArray(libdap::Array *a, vector<int*> *sharedIntArrays, vector<float*> *sharedFloatArrays)
131 {
132  if ((a->type() == dods_array_c && !a->var()->is_simple_type()) || a->var()->type() == dods_str_c
133  || a->var()->type() == dods_url_c)
134  throw Error(malformed_expr, "The function requires a DAP numeric-type array argument.");
135 
136  DBG(cerr << "extract_gridfield_array() - " << "Reading data values into DAP Array '" << a->name() <<"'"<< endl);
137  a->set_send_p(true);
138  a->read();
139 
140  // Construct a GridField array from a DODS array
141  GF::Array *gfa;
142 
143  switch (a->var()->type()) {
144  case dods_byte_c: {
145  gfa = new GF::Array(a->var()->name(), GF::INT);
146  int *values = extract_array_helper<dods_byte, int>(a);
147  gfa->shareIntData(values, a->length());
148  sharedIntArrays->push_back(values);
149  break;
150  }
151  case dods_uint16_c: {
152  gfa = new GF::Array(a->var()->name(), GF::INT);
153  int *values = extract_array_helper<dods_uint16, int>(a);
154  gfa->shareIntData(values, a->length());
155  sharedIntArrays->push_back(values);
156  break;
157  }
158  case dods_int16_c: {
159  gfa = new GF::Array(a->var()->name(), GF::INT);
160  int *values = extract_array_helper<dods_int16, int>(a);
161  gfa->shareIntData(values, a->length());
162  sharedIntArrays->push_back(values);
163  break;
164  }
165  case dods_uint32_c: {
166  gfa = new GF::Array(a->var()->name(), GF::INT);
167  int *values = extract_array_helper<dods_uint32, int>(a);
168  gfa->shareIntData(values, a->length());
169  sharedIntArrays->push_back(values);
170  break;
171  }
172  case dods_int32_c: {
173  gfa = new GF::Array(a->var()->name(), GF::INT);
174  int *values = extract_array_helper<dods_int32, int>(a);
175  gfa->shareIntData(values, a->length());
176  sharedIntArrays->push_back(values);
177  break;
178  }
179  case dods_float32_c: {
180  gfa = new GF::Array(a->var()->name(), GF::FLOAT);
181  float *values = extract_array_helper<dods_float32, float>(a);
182  gfa->shareFloatData(values, a->length());
183  sharedFloatArrays->push_back(values);
184  break;
185  }
186  case dods_float64_c: {
187  gfa = new GF::Array(a->var()->name(), GF::FLOAT);
188  float *values = extract_array_helper<dods_float64, float>(a);
189  gfa->shareFloatData(values, a->length());
190  sharedFloatArrays->push_back(values);
191  break;
192  }
193  default:
194  throw InternalErr(__FILE__, __LINE__, "Unknown DAP type encountered when converting to gridfields array");
195  }
196  return gfa;
197 }
198 
203 vector<string> &split(const string &s, char delim, vector<string> &elems)
204 {
205  stringstream ss(s);
206  string item;
207  while (getline(ss, item, delim)) {
208  elems.push_back(item);
209  }
210  return elems;
211 }
212 
216 vector<string> split(const string &s, char delim)
217 {
218  vector<string> elems;
219  return split(s, delim, elems);
220 }
221 
222 // Returns the string value of the attribute called aName, 0 otherwise.
223 string getAttributeValue(BaseType *bt, string aName)
224 {
225  AttrTable &at = bt->get_attr_table();
226  DBG(cerr << "getAttributeValue() - " << "Checking to see if the variable " << bt->name()
227  << "' has an attribute '"<< aName << "'"<<endl);
228 
229  // Confirm that submitted variable has an attribute called aName and return its value.
230  AttrTable::Attr_iter loc = at.simple_find(aName);
231  if (loc != at.attr_end()) {
232  return at.get_attr(loc, 0);
233  }
234 
235  return "";
236 }
237 
243 bool matchesCfRoleOrStandardName(BaseType *bt, string aValue)
244 {
245  // Confirm that submitted variable has a 'cf_role' attribute whose value is "aValue".
246  if (!checkAttributeValue(bt, CF_ROLE, aValue)) {
247  // Missing the 'cf_role' attribute? Check for a 'standard_name' attribute whose value is "aValue".
248  if (!checkAttributeValue(bt, CF_STANDARD_NAME, aValue)) {
249  return false;
250  }
251  }
252 
253  return true;
254 }
255 
256 // Returns true iff the submitted BaseType variable has an attribute called aName attribute whose value is aValue.
257 bool checkAttributeValue(BaseType *bt, string aName, string aValue)
258 {
259 
260  AttrTable &at = bt->get_attr_table();
261  DBG(cerr << "checkAttributeValue() - " << "Checking to see if the variable " << bt->name()
262  << "' has an attribute '"<< aName << "' with value '" << aValue << "'"<<endl);
263 
264  // Confirm that submitted variable has an attribute called aName whose value is aValue.
265  AttrTable::Attr_iter loc = at.simple_find(aName);
266  if (loc != at.attr_end()) {
267  DBG(cerr << "checkAttributeValue() - " << "'" << bt->name() << "' has a attribute named '" << aName << "'"<< endl);
268  string value = at.get_attr(loc, 0);
269  DBG(cerr << "checkAttributeValue() - " << "Attribute '"<< aName <<"' has value of '" << value << "'"<< endl);
270  if (value != aValue) {
271  return false;
272  }
273  return true;
274  }
275  return false;
276 
277 }
278 
279 } // namespace ugrid
Type
Type
Type of JSON value.
Definition: cmr_module/rapidjson/rapidjson.h:603
libdap
Definition: BESDapFunctionResponseCache.h:35
Error