bes  Updated for version 3.20.5
ugrid_utils.h
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 #ifndef _UgridUtilities_h
29 #define _UgridUtilities_h 1
30 
31 #include <gridfields/array.h>
32 
33 using namespace std;
34 //using namespace libdap;
35 
36 namespace {
37 class Array;
38 }
39 
40 namespace ugrid {
41 
45 #define CF_ROLE "cf_role"
46 #define CF_STANDARD_NAME "standard_name"
47 #define UGRID_MESH_TOPOLOGY "mesh_topology"
48 #define UGRID_NODE_COORDINATES "node_coordinates"
49 #define UGRID_FACE_NODE_CONNECTIVITY "face_node_connectivity"
50 
51 #define UGRID_TOPOLOGY_DIMENSION "topology_dimension"
52 #define UGRID_DIMENSION "dimension" // Old-style; still using in some ugrids. jhrg 5/19/15
53 #define UGRID_LOCATION "location"
54 #define UGRID_GRID_LOCATION "grid_location"
55 #define UGRID_NODE "node"
56 #define UGRID_EDGE "edge"
57 #define UGRID_FACE "face"
58 #define UGRID_MESH "mesh"
59 #define UGRID_START_INDEX "start_index"
60 
64 #define UGRID_EDGE_NODE_CONNECTIVITY "edge_node_connectivity"
65 
66 #define UGRID_FACE_COORDINATES "face_coordinates"
67 #define UGRID_EDGE_COORDINATES "edge_coordinates"
68 #define UGRID_FACE_EDGE_CONNECTIVITY "face_edge_connectivity"
69 #define UGRID_FACE_FACE_CONNECTIVITY "face_face_connectivity"
70 
71 GF::Array *extractGridFieldArray(libdap::Array *a, vector<int*> *sharedIntArrays, vector<float*> *sharedFloatArrays);
72 GF::Array *newGFIndexArray(string name, long size, vector<int*> *sharedIntArrays);
73 
74 string getAttributeValue(libdap::BaseType *bt, string aName);
75 bool matchesCfRoleOrStandardName(libdap::BaseType *bt, string aValue);
76 
77 bool checkAttributeValue(libdap::BaseType *bt, string aName, string aValue);
78 
79 vector<string> split(const string &s, char delim);
80 vector<string> &split(const string &s, char delim, vector<string> &elems);
81 
82 int getNfrom3byNArray(libdap::Array *array);
83 
84 libdap::Type getGridfieldsReturnType(libdap::Type type);
85 
91 template<typename DODS, typename T> T *extract_array_helper(libdap::Array *a)
92 {
93  int length = a->length();
94 
95  DODS *src = new DODS[length];
96 
97  a->value(src);
98 
99  T *dest = new T[length];
100 
101  for (int i = 0; i < length; ++i)
102  dest[i] = (T) src[i];
103 
104  delete[] src;
105 
106  return dest;
107 }
108 
126 template<typename T> T *extractArray(libdap::Array *a)
127 {
128 
129  // Simple types are Byte, ..., Float64, String and Url.
130  if ((a->type() == libdap::dods_array_c && !a->var()->is_simple_type()) || a->var()->type() == libdap::dods_str_c
131  || a->var()->type() == libdap::dods_url_c)
132  throw libdap::Error(malformed_expr, "The function requires a DAP numeric-type array argument.");
133 
134  a->read();
135 
136  // The types of arguments that the CE Parser will build for numeric
137  // constants are limited to Uint32, Int32 and Float64. See ce_expr.y.
138  // Expanded to work for any numeric type so it can be used for more than
139  // just arguments.
140  switch (a->var()->type()) {
141  case libdap::dods_byte_c:
142  return extract_array_helper<libdap::dods_byte, T>(a);
143 
144  case libdap::dods_uint16_c:
145  return extract_array_helper<libdap::dods_uint16, T>(a);
146 
147  case libdap::dods_int16_c:
148  return extract_array_helper<libdap::dods_int16, T>(a);
149 
150  case libdap::dods_uint32_c:
151  return extract_array_helper<libdap::dods_uint32, T>(a);
152 
153  case libdap::dods_int32_c:
154  return extract_array_helper<libdap::dods_int32, T>(a);
155 
156  case libdap::dods_float32_c:
157  // Added the following line. jhrg 8/7/12
158  return extract_array_helper<libdap::dods_float32, T>(a);
159 
160  case libdap::dods_float64_c:
161  return extract_array_helper<libdap::dods_float64, T>(a);
162 
163  default:
164  throw libdap::InternalErr(__FILE__, __LINE__,
165  "The argument list built by the CE parser contained an unsupported numeric type.");
166  }
167 }
168 
169 } // namespace ugrid
170 
171 #endif // _UgridUtilities_h
Type
Type
Type of JSON value.
Definition: rapidjson.h:603