bes  Updated for version 3.20.6
FONcStr.cc
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 <Str.h>
33 
34 #include <BESInternalError.h>
35 #include <BESDebug.h>
36 
37 #include "FONcStr.h"
38 #include "FONcUtils.h"
39 #include "FONcAttributes.h"
40 
41 using namespace libdap;
42 
51 FONcStr::FONcStr(BaseType *b) :
52  FONcBaseType(), _str(0), _dimid(0), _data(0)
53 {
54  _str = dynamic_cast<Str *>(b);
55  if (!_str)
56  throw BESInternalError("File out netcdf, FONcStr was passed a variable that is not a DAP Str", __FILE__, __LINE__);
57 }
58 
67 {
68  delete _data;
69 }
70 
79 void FONcStr::define(int ncid)
80 {
81  if (!_defined) {
82  BESDEBUG("fonc", "FONcStr::define - defining " << _varname << endl);
83 
84  _varname = FONcUtils::gen_name(_embed, _varname, _orig_varname);
85  _data = new string;
86  _str->buf2val((void**) &_data);
87  int size = _data->size() + 1;
88 
89  string dimname = _varname + "_len";
90  int stax = nc_def_dim(ncid, dimname.c_str(), size, &_dimid);
91  if (stax != NC_NOERR) {
92  string err = (string) "fileout.netcdf - " + "Failed to define dim " + dimname + " for " + _varname;
93  FONcUtils::handle_error(stax, err, __FILE__, __LINE__);
94  }
95 
96  int var_dims[1]; // variable shape
97  var_dims[0] = _dimid;
98  stax = nc_def_var(ncid, _varname.c_str(), NC_CHAR, 1, var_dims, &_varid);
99  if (stax != NC_NOERR) {
100  string err = (string) "fileout.netcdf - " + "Failed to define var " + _varname;
101  FONcUtils::handle_error(stax, err, __FILE__, __LINE__);
102  }
103 
104  _defined = true;
105 
106  FONcAttributes::add_variable_attributes(ncid, _varid, _str);
107  FONcAttributes::add_original_name(ncid, _varid, _varname, _orig_varname);
108 
109  BESDEBUG("fonc", "FONcStr::define - done defining " << _varname << endl);
110  }
111 }
112 
122 void FONcStr::write(int ncid)
123 {
124  BESDEBUG("fonc", "FONcStr::write for var " << _varname << endl);
125 
126  size_t var_start[1]; // variable start
127  size_t var_count[1]; // variable count
128 
129  var_count[0] = _data->size() + 1;
130  var_start[0] = 0;
131  int stax = nc_put_vara_text(ncid, _varid, var_start, var_count, _data->c_str());
132  if (stax != NC_NOERR) {
133  string err = (string) "fileout.netcdf - " + "Failed to write string data " + *_data + " for " + _varname;
134  delete _data;
135  _data = 0;
136  FONcUtils::handle_error(stax, err, __FILE__, __LINE__);
137  }
138  delete _data;
139  _data = 0;
140 
141  BESDEBUG("fonc", "FONcStr::done write for var " << _varname << endl);
142 }
143 
149 {
150  return _str->name();
151 }
152 
157 nc_type FONcStr::type()
158 {
159  return NC_CHAR;
160 }
161 
168 void FONcStr::dump(ostream &strm) const
169 {
170  strm << BESIndent::LMarg << "FONcStr::dump - (" << (void *) this << ")" << endl;
171  BESIndent::Indent();
172  strm << BESIndent::LMarg << "name = " << _str->name() << endl;
173  BESIndent::UnIndent();
174 }
175 
FONcStr::~FONcStr
virtual ~FONcStr()
Destructor that cleans up the str.
Definition: FONcStr.cc:66
FONcStr::define
virtual void define(int ncid)
Define the string variable in the netcdf file.
Definition: FONcStr.cc:79
FONcStr::write
virtual void write(int ncid)
Write the str out to the netcdf file.
Definition: FONcStr.cc:122
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
FONcAttributes::add_variable_attributes
static void add_variable_attributes(int ncid, int varid, BaseType *b)
Add the attributes for an OPeNDAP variable to the netcdf file.
Definition: FONcAttributes.cc:77
libdap
Definition: BESDapFunctionResponseCache.h:35
FONcStr::dump
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcStr.cc:168
FONcStr::type
virtual nc_type type()
returns the netcdf type of the DAP Str
Definition: FONcStr.cc:157
FONcStr::name
virtual string name()
returns the name of the DAP Str
Definition: FONcStr.cc:148
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
FONcAttributes::add_original_name
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.
Definition: FONcAttributes.cc:415
FONcBaseType
A DAP BaseType with file out netcdf information included.
Definition: FONcBaseType.h:58
FONcStr::FONcStr
FONcStr(libdap::BaseType *b)
Constructor for FONcStr that takes a DAP Str.
Definition: FONcStr.cc:51