OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
FONcUtils.cc
Go to the documentation of this file.
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 #include "FONcUtils.h"
34 #include "FONcByte.h"
35 #include "FONcStr.h"
36 #include "FONcShort.h"
37 #include "FONcInt.h"
38 #include "FONcFloat.h"
39 #include "FONcDouble.h"
40 #include "FONcStructure.h"
41 #include "FONcGrid.h"
42 #include "FONcArray.h"
43 #include "FONcSequence.h"
44 
45 #include <BESInternalError.h>
46 
51 string FONcUtils::name_prefix = "" ;
52 
55 void
57 {
58  FONcArray::Dimensions.clear() ;
59  FONcGrid::Maps.clear() ;
61 }
62 
72 string
74 {
75  // string of allowed characters in netcdf naming convention
76  string allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+_.@" ;
77  // string of allowed first characters in netcdf naming
78  // convention
79  string first = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_" ;
80 
81  string::size_type i = 0;
82 
83  while( (i = in.find_first_not_of( allowed, i ) ) != string::npos)
84  {
85  in.replace( i, 1, "_" ) ;
86  i++ ;
87  }
88 
89  if( first.find( in[0] ) == string::npos )
90  {
91  in = FONcUtils::name_prefix + in ;
92  }
93 
94  return in;
95 }
96 
102 nc_type
103 FONcUtils::get_nc_type( BaseType *element )
104 {
105  nc_type x_type = NC_NAT ; // the constant ncdf uses to define simple type
106 
107  string var_type = element->type_name() ;
108  if( var_type == "Byte" ) // check this for dods type
109  x_type = NC_SHORT ;
110  else if( var_type == "String" )
111  x_type = NC_CHAR ;
112  else if( var_type == "Int16" )
113  x_type = NC_SHORT ;
114  // The attribute of UInt16 maps to NC_INT, so we need to map UInt16
115  // to NC_INT for the variable so that end_def won't complain about
116  // the inconsistent datatype between fillvalue and the variable. KY 2012-10-25
117  //else if( var_type == "UInt16" )
118  // x_type = NC_SHORT ;
119  else if( var_type == "UInt16" )
120  x_type = NC_INT ;
121  else if( var_type == "Int32" )
122  x_type = NC_INT ;
123  else if( var_type == "UInt32" )
124  x_type = NC_INT ;
125  else if( var_type == "Float32" )
126  x_type = NC_FLOAT ;
127  else if( var_type == "Float64" )
128  x_type = NC_DOUBLE ;
129 
130  return x_type ;
131 }
132 
149 string
150 FONcUtils::gen_name( const vector<string> &embed, const string &name,
151  string &original )
152 {
153  string new_name ;
154  vector<string>::const_iterator i = embed.begin() ;
155  vector<string>::const_iterator e = embed.end() ;
156  bool first = true ;
157  for( ; i != e; i++ )
158  {
159  if( first ) new_name = (*i) ;
160  else new_name += FONC_EMBEDDED_SEPARATOR + (*i) ;
161  first = false ;
162  }
163  if( first ) new_name = name ;
164  else new_name += FONC_EMBEDDED_SEPARATOR + name ;
165 
166  original = new_name ;
167 
168  return FONcUtils::id2netcdf( new_name ) ;
169 }
170 
177 FONcBaseType *
178 FONcUtils::convert( BaseType *v )
179 {
180  FONcBaseType *b = 0 ;
181  switch( v->type() )
182  {
183  case dods_str_c:
184  case dods_url_c:
185  b = new FONcStr( v ) ;
186  break ;
187  case dods_byte_c:
188  b = new FONcByte( v ) ;
189  break ;
190  case dods_int16_c:
191  case dods_uint16_c:
192  b = new FONcShort( v ) ;
193  break ;
194  case dods_int32_c:
195  case dods_uint32_c:
196  b = new FONcInt( v ) ;
197  break ;
198  case dods_float32_c:
199  b = new FONcFloat( v ) ;
200  break ;
201  case dods_float64_c:
202  b = new FONcDouble( v ) ;
203  break ;
204  case dods_grid_c:
205  b = new FONcGrid( v ) ;
206  break ;
207  case dods_array_c:
208  b = new FONcArray( v ) ;
209  break ;
210  case dods_structure_c:
211  b = new FONcStructure( v ) ;
212  break ;
213  case dods_sequence_c:
214  b = new FONcSequence( v ) ;
215  break ;
216  default:
217  string err = (string)"file out netcdf, unable to "
218  + "write unknown variable type" ;
219  throw BESInternalError( err, __FILE__, __LINE__ ) ;
220 
221  }
222  return b ;
223 }
224 
237 void
238 FONcUtils::handle_error( int stax, string &err, const string &file, int line )
239 {
240  if( stax != NC_NOERR )
241  {
242  const char *nerr = nc_strerror( stax ) ;
243  if( nerr )
244  {
245  err += (string)": " + nerr ;
246  }
247  else
248  {
249  err += (string)": unknown error" ;
250  }
251  throw BESInternalError( err, file, line ) ;
252  }
253 }
254 
exception thrown if inernal error encountered
A DAP Int32 and UInt32 with file out netcdf information included.
Definition: FONcInt.h:47
A DAP Array with file out netcdf information included.
Definition: FONcArray.h:50
static int DimNameNum
Definition: FONcDim.h:68
A DAP Float64 with file out netcdf information included.
Definition: FONcDouble.h:47
#define FONC_EMBEDDED_SEPARATOR
Definition: FONcUtils.h:45
A DAP Grid with file out netcdf information included.
Definition: FONcGrid.h:57
static vector< FONcDim * > Dimensions
Definition: FONcArray.h:97
A class representing the DAP Str class for file out netcdf.
Definition: FONcStr.h:47
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:58
A DAP Structure with file out netcdf information included.
Definition: FONcStructure.h:48
static nc_type get_nc_type(BaseType *element)
translate the OPeNDAP data type to a netcdf data type
Definition: FONcUtils.cc:103
A class representing the DAP Byte class for file out netcdf.
Definition: FONcByte.h:48
static void handle_error(int stax, string &err, const string &file, int line)
handle any netcdf errors
Definition: FONcUtils.cc:238
static void reset()
Resets the FONc transformation for a new input and out file.
Definition: FONcUtils.cc:56
A DAP BaseType with file out netcdf information included.
Definition: FONcBaseType.h:48
A DAP Float32 with file out netcdf information included.
Definition: FONcFloat.h:47
static string gen_name(const vector< string > &embed, const string &name, string &original)
generate a new name for the embedded variable
Definition: FONcUtils.cc:150
static FONcBaseType * convert(BaseType *v)
creates a FONc object for the given DAP object
Definition: FONcUtils.cc:178
A DAP Int16 and UInt16 with file out netcdf information included.
Definition: FONcShort.h:47
A DAP Sequence with file out netcdf information included.
Definition: FONcSequence.h:47
static string id2netcdf(string in)
convert the provided string to a netcdf allowed identifier.
Definition: FONcUtils.cc:73
static vector< FONcMap * > Maps
global list of maps that could be shared amongst the different grids
Definition: FONcGrid.h:75