OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
FONcStr.cc
Go to the documentation of this file.
1 // FONcStr.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 <BESInternalError.h>
33 #include <BESDebug.h>
34 
35 #include "FONcStr.h"
36 #include "FONcUtils.h"
37 #include "FONcAttributes.h"
38 
47 FONcStr::FONcStr( BaseType *b )
48  : FONcBaseType(), _str( 0 ), _dimid( 0 ), _data( 0 )
49 {
50  _str = dynamic_cast<Str *>(b) ;
51  if( !_str )
52  {
53  string s = (string)"File out netcdf, FONcStr was passed a "
54  + "variable that is not a DAP Str" ;
55  throw BESInternalError( s, __FILE__, __LINE__ ) ;
56  }
57 }
58 
67 {
68  if( _data ) delete _data ;
69 }
70 
79 void
80 FONcStr::define( int ncid )
81 {
82  if( !_defined )
83  {
84  BESDEBUG( "fonc", "FONcStr::define - defining "
85  << _varname << endl ) ;
86 
87  int var_dims[1] ; // variable shape
88  // size_t var_start[1] ; // variable start // Not used; jhrg 3/16/11
89  // size_t var_count[1] ; // variable count // Not used; jhrg 3/16/11
90 
92  _data = new string ;
93  _str->buf2val( (void**)&_data ) ;
94  int size = _data->size() + 1 ;
95 
96  string dimname = _varname + "_len" ;
97  int stax = nc_def_dim( ncid, dimname.c_str(), size, &_dimid ) ;
98  if( stax != NC_NOERR )
99  {
100  string err = (string)"fileout.netcdf - "
101  + "Failed to define dim " + dimname + " for "
102  + _varname ;
103  FONcUtils::handle_error( stax, err, __FILE__, __LINE__ ) ;
104  }
105 
106  var_dims[0] = _dimid ;
107  stax = nc_def_var( ncid, _varname.c_str(), NC_CHAR, 1,
108  var_dims, &_varid );
109  if( stax != NC_NOERR )
110  {
111  string err = (string)"fileout.netcdf - "
112  + "Failed to define var " + _varname ;
113  FONcUtils::handle_error( stax, err, __FILE__, __LINE__ ) ;
114  }
115 
116  _defined = true ;
117 
118  FONcAttributes::add_attributes( ncid, _varid, _str ) ;
121 
122  BESDEBUG( "fonc", "FONcStr::define - done defining "
123  << _varname << endl ) ;
124  }
125 }
126 
136 void
137 FONcStr::write( int ncid )
138 {
139  BESDEBUG( "fonc", "FONcStr::write for var " << _varname << endl ) ;
140 
141  size_t var_start[1] ; // variable start
142  size_t var_count[1] ; // variable count
143 
144  var_count[0] = _data->size() + 1 ;
145  var_start[0] = 0 ;
146  int stax = nc_put_vara_text( ncid, _varid, var_start, var_count,
147  _data->c_str() ) ;
148  if( stax != NC_NOERR )
149  {
150  string err = (string)"fileout.netcdf - "
151  + "Failed to write string data " + *_data + " for "
152  + _varname ;
153  delete _data ;
154  _data = 0 ;
155  FONcUtils::handle_error( stax, err, __FILE__, __LINE__ ) ;
156  }
157  delete _data ;
158  _data = 0 ;
159  BESDEBUG( "fonc", "FONcStr::done write for var " << _varname << endl ) ;
160 }
161 
166 string
168 {
169  return _str->name() ;
170 }
171 
176 nc_type
178 {
179  return NC_CHAR ;
180 }
181 
188 void
189 FONcStr::dump( ostream &strm ) const
190 {
191  strm << BESIndent::LMarg << "FONcStr::dump - ("
192  << (void *)this << ")" << endl ;
194  strm << BESIndent::LMarg << "name = " << _str->name() << endl ;
196 }
197 
exception thrown if inernal error encountered
FONcStr(BaseType *b)
Constructor for FONcStr that takes a DAP Str.
Definition: FONcStr.cc:47
virtual string name()
returns the name of the DAP Str
Definition: FONcStr.cc:167
virtual ~FONcStr()
Destructor that cleans up the str.
Definition: FONcStr.cc:66
static void Indent()
Definition: BESIndent.cc:38
static void add_original_name(int ncid, int varid, const string &var_name, const string &orig)
Adds an attribute for the variable if the variable name had to be modified in any way...
vector< string > _embed
Definition: FONcBaseType.h:54
string _orig_varname
Definition: FONcBaseType.h:53
string _varname
Definition: FONcBaseType.h:52
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
static void handle_error(int stax, string &err, const string &file, int line)
handle any netcdf errors
Definition: FONcUtils.cc:238
A DAP BaseType with file out netcdf information included.
Definition: FONcBaseType.h:48
virtual void define(int ncid)
Define the string variable in the netcdf file.
Definition: FONcStr.cc:80
virtual void write(int ncid)
Write the str out to the netcdf file.
Definition: FONcStr.cc:137
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
virtual nc_type type()
returns the netcdf type of the DAP Str
Definition: FONcStr.cc:177
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
static void UnIndent()
Definition: BESIndent.cc:44
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcStr.cc:189