bes  Updated for version 3.20.6
FONcUtils.cc
1 // FONcUtils.cc
2 
3 // This file is part of BES Netcdf File Out Module
4 
5 // Copyright (c) 2004,2005 University Corporation for Atmospheric Research
6 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 //
22 // You can contact University Corporation for Atmospheric Research at
23 // 3080 Center Green Drive, Boulder, CO 80301
24 
25 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
26 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
27 //
28 // Authors:
29 // pwest Patrick West <pwest@ucar.edu>
30 // jgarcia Jose Garcia <jgarcia@ucar.edu>
31 
32 #include "config.h"
33 
34 #include <cassert>
35 
36 #include "FONcUtils.h"
37 #include "FONcDim.h"
38 #include "FONcByte.h"
39 #include "FONcStr.h"
40 #include "FONcShort.h"
41 #include "FONcInt.h"
42 #include "FONcFloat.h"
43 #include "FONcDouble.h"
44 #include "FONcStructure.h"
45 #include "FONcGrid.h"
46 #include "FONcArray.h"
47 #include "FONcSequence.h"
48 
49 #include <BESInternalError.h>
50 
55 string FONcUtils::name_prefix = "";
56 
60 {
61  FONcArray::Dimensions.clear();
62  FONcGrid::Maps.clear();
63  FONcDim::DimNameNum = 0;
64 }
65 
75 string FONcUtils::id2netcdf(string in)
76 {
77  // string of allowed characters in netcdf naming convention
78  string allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+_.@";
79  // string of allowed first characters in netcdf naming
80  // convention
81  string first = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
82 
83  string::size_type i = 0;
84 
85  while ((i = in.find_first_not_of(allowed, i)) != string::npos) {
86  in.replace(i, 1, "_");
87  i++;
88  }
89 
90  if (first.find(in[0]) == string::npos) {
91  in = FONcUtils::name_prefix + in;
92  }
93 
94  return in;
95 }
96 
102 nc_type FONcUtils::get_nc_type(BaseType *element)
103 {
104  nc_type x_type = NC_NAT; // the constant ncdf uses to define simple type
105 
106  string var_type = element->type_name();
107  if (var_type == "Byte") // check this for dods type
108  x_type = NC_SHORT;
109  else if (var_type == "String")
110  x_type = NC_CHAR;
111  else if (var_type == "Int16")
112  x_type = NC_SHORT;
113  // The attribute of UInt16 maps to NC_INT, so we need to map UInt16
114  // to NC_INT for the variable so that end_def won't complain about
115  // the inconsistent datatype between fillvalue and the variable. KY 2012-10-25
116  //else if( var_type == "UInt16" )
117  // x_type = NC_SHORT ;
118  else if (var_type == "UInt16")
119  x_type = NC_INT;
120  else if (var_type == "Int32")
121  x_type = NC_INT;
122  else if (var_type == "UInt32")
123  x_type = NC_INT;
124  else if (var_type == "Float32")
125  x_type = NC_FLOAT;
126  else if (var_type == "Float64")
127  x_type = NC_DOUBLE;
128 
129  return x_type;
130 }
131 
148 string FONcUtils::gen_name(const vector<string> &embed, const string &name, string &original)
149 {
150  string new_name;
151  vector<string>::const_iterator i = embed.begin();
152  vector<string>::const_iterator e = embed.end();
153  bool first = true;
154  for (; i != e; i++) {
155  if (first)
156  new_name = (*i);
157  else
158  new_name += FONC_EMBEDDED_SEPARATOR + (*i);
159  first = false;
160  }
161  if (first)
162  new_name = name;
163  else
164  new_name += FONC_EMBEDDED_SEPARATOR + name;
165 
166  original = new_name;
167 
168  return FONcUtils::id2netcdf(new_name);
169 }
170 
181 FONcBaseType *
182 FONcUtils::convert(BaseType *v)
183 {
184  FONcBaseType *b = 0;
185  switch (v->type()) {
186  case dods_str_c:
187  case dods_url_c:
188  b = new FONcStr(v);
189  break;
190  case dods_byte_c:
191  b = new FONcByte(v);
192  break;
193  case dods_int16_c:
194  case dods_uint16_c:
195  b = new FONcShort(v);
196  break;
197  case dods_int32_c:
198  case dods_uint32_c:
199  b = new FONcInt(v);
200  break;
201  case dods_float32_c:
202  b = new FONcFloat(v);
203  break;
204  case dods_float64_c:
205  b = new FONcDouble(v);
206  break;
207  case dods_grid_c:
208  b = new FONcGrid(v);
209  break;
210  case dods_array_c:
211  b = new FONcArray(v);
212  break;
213  case dods_structure_c:
214  b = new FONcStructure(v);
215  break;
216  case dods_sequence_c:
217  b = new FONcSequence(v);
218  break;
219  default:
220  string err = (string) "file out netcdf, unable to " + "write unknown variable type";
221  throw BESInternalError(err, __FILE__, __LINE__);
222 
223  }
224  return b;
225 }
226 
245 void FONcUtils::handle_error(int stax, const string &err, const string &file, int line)
246 {
247  assert(stax != NC_NOERR); // This should not be called for NOERR
248 
249  throw BESInternalError(err + string(": ") + nc_strerror(stax), file, line);
250 }
251 
FONcByte
A class representing the DAP Byte class for file out netcdf.
Definition: FONcByte.h:48
FONcUtils::id2netcdf
static string id2netcdf(string in)
convert the provided string to a netcdf allowed identifier.
Definition: FONcUtils.cc:75
FONcArray
A DAP Array with file out netcdf information included.
Definition: FONcArray.h:54
FONcUtils::gen_name
static string gen_name(const vector< string > &embed, const string &name, string &original)
generate a new name for the embedded variable
Definition: FONcUtils.cc:148
FONcUtils::handle_error
static void handle_error(int stax, const string &err, const string &file, int line)
handle any netcdf errors
Definition: FONcUtils.cc:245
FONcUtils::get_nc_type
static nc_type get_nc_type(BaseType *element)
translate the OPeNDAP data type to a netcdf data type
Definition: FONcUtils.cc:102
FONcSequence
A DAP Sequence with file out netcdf information included.
Definition: FONcSequence.h:47
FONcGrid::Maps
static vector< FONcMap * > Maps
global list of maps that could be shared amongst the different grids
Definition: FONcGrid.h:74
FONcUtils::reset
static void reset()
Resets the FONc transformation for a new input and out file.
Definition: FONcUtils.cc:59
FONcStr
A class representing the DAP Str class for file out netcdf.
Definition: FONcStr.h:50
FONcInt
A DAP Int32 and UInt32 with file out netcdf information included.
Definition: FONcInt.h:47
FONcStructure
A DAP Structure with file out netcdf information included.
Definition: FONcStructure.h:48
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
FONcUtils::convert
static FONcBaseType * convert(BaseType *v)
Creates a FONc object for the given DAP object.
Definition: FONcUtils.cc:182
FONcBaseType
A DAP BaseType with file out netcdf information included.
Definition: FONcBaseType.h:58
FONcGrid
A DAP Grid with file out netcdf information included.
Definition: FONcGrid.h:57
FONcShort
A DAP Int16 and UInt16 with file out netcdf information included.
Definition: FONcShort.h:47
FONcDouble
A DAP Float64 with file out netcdf information included.
Definition: FONcDouble.h:47
FONcFloat
A DAP Float32 with file out netcdf information included.
Definition: FONcFloat.h:47
FONcUtils::name_prefix
static string name_prefix
If a variable name, dimension name, or attribute name begins with a character that is not supported b...
Definition: FONcUtils.h:56