bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
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 <sstream>
33#include <libdap/Str.h>
34
35#include <BESInternalError.h>
36#include <BESDebug.h>
37
38#include "FONcStr.h"
39#include "FONcDim.h"
40#include "FONcUtils.h"
41#include "FONcAttributes.h"
42
43using namespace libdap;
44
53FONcStr::FONcStr(BaseType *b) :
54 FONcBaseType(), _str(0), _dimid(0), _data(0)
55{
56 _str = dynamic_cast<Str *>(b);
57 if (!_str)
58 throw BESInternalError("File out netcdf, FONcStr was passed a variable that is not a DAP Str", __FILE__, __LINE__);
59}
60
69{
70 delete _data;
71}
72
81void FONcStr::define(int ncid)
82{
83 if (!d_defined) {
84 BESDEBUG("fonc", "FONcStr::define - defining " << d_varname << endl);
85
86 d_varname = FONcUtils::gen_name(d_embed, d_varname, d_orig_varname);
87
88 // The following code (the call to intern_data() and then buf2val()) moves the string data
89 // from the encapsulated libdap::Str instance to the _data member of this class. Note that
90 // libdap::Str::buf2val() works with a **string and not **void like the numeric types. jhrg 2/14/24
91 // FIXME This (d_is_dap4 || get_eval() == nullptr || get_dds() == nullptr) is a hack to get
92 // around the fact that the code moved away from setting the DDS and
93 // the ConstraintEvaluator in the constructor. This is a temporary fix. jhrg 2/14/24
94 if (d_is_dap4 || get_eval() == nullptr || get_dds() == nullptr)
95 _str->intern_data();
96 else
97 _str->intern_data(*get_eval(), *get_dds());
98
99 _data = new string;
100 _str->buf2val((void**) &_data);
101
102 string dimname;
103 // For DAP4, we need to ensure the dimension name is unique.
104 if(d_is_dap4_group == true) {
105 ostringstream dim_suffix_strm;
106 dim_suffix_strm <<"_len"<<FONcDim::DimNameNum +1;
107 FONcDim::DimNameNum++;
108 dimname = d_varname + dim_suffix_strm.str();
109 }
110 else
111 dimname = d_varname + "_len";
112
113 // FIXME In the following, for the third parameter, why not: dimname.size() + 1? jhrg 2/14/24
114 int stax = nc_def_dim(ncid, dimname.c_str(), _data->size() + 1, &_dimid);
115 if (stax != NC_NOERR) {
116 string err = (string) "fileout.netcdf - " + "Failed to define dim " + dimname + " for " + d_varname;
117 FONcUtils::handle_error(stax, err, __FILE__, __LINE__);
118 }
119
120 int var_dims[1]; // variable shape
121 var_dims[0] = _dimid;
122 stax = nc_def_var(ncid, d_varname.c_str(), NC_CHAR, 1, var_dims, &d_varid);
123 if (stax != NC_NOERR) {
124 string err = (string) "fileout.netcdf - " + "Failed to define var " + d_varname;
125 FONcUtils::handle_error(stax, err, __FILE__, __LINE__);
126 }
127
128 d_defined = true;
129
130 FONcAttributes::add_variable_attributes(ncid, d_varid, _str, isNetCDF4_ENHANCED(), d_is_dap4);
131 FONcAttributes::add_original_name(ncid, d_varid, d_varname, d_orig_varname);
132
133 BESDEBUG("fonc", "FONcStr::define - done defining " << d_varname << endl);
134 }
135}
136
146void FONcStr::write(int ncid)
147{
148 BESDEBUG("fonc", "FONcStr::write for var " << d_varname << endl);
149
150 size_t var_start[1]; // variable start
151 size_t var_count[1]; // variable count
152
153 var_count[0] = _data->size() + 1;
154 var_start[0] = 0;
155 int stax = nc_put_vara_text(ncid, d_varid, var_start, var_count, _data->c_str());
156 if (stax != NC_NOERR) {
157 string err = (string) "fileout.netcdf - " + "Failed to write string data " + *_data + " for " + d_varname;
158 delete _data;
159 _data = 0;
160 FONcUtils::handle_error(stax, err, __FILE__, __LINE__);
161 }
162 delete _data;
163 _data = 0;
164
165 BESDEBUG("fonc", "FONcStr::done write for var " << d_varname << endl);
166}
167
173{
174 return _str->name();
175}
176
182{
183 return NC_CHAR;
184}
185
192void FONcStr::dump(ostream &strm) const
193{
194 strm << BESIndent::LMarg << "FONcStr::dump - (" << (void *) this << ")" << endl;
195 BESIndent::Indent();
196 strm << BESIndent::LMarg << "name = " << _str->name() << endl;
197 BESIndent::UnIndent();
198}
199
exception thrown if internal error encountered
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.
static void add_variable_attributes(int ncid, int varid, BaseType *b, bool is_netCDF_enhanced, bool is_dap4)
Add the attributes for an OPeNDAP variable to the netcdf file.
virtual nc_type type()
returns the netcdf type of the DAP Str
Definition FONcStr.cc:181
virtual void define(int ncid)
Define the string variable in the netcdf file.
Definition FONcStr.cc:81
virtual ~FONcStr()
Destructor that cleans up the str.
Definition FONcStr.cc:68
virtual void write(int ncid)
Write the str out to the netcdf file.
Definition FONcStr.cc:146
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
Definition FONcStr.cc:192
virtual string name()
returns the name of the DAP Str
Definition FONcStr.cc:172
FONcStr(libdap::BaseType *b)
Constructor for FONcStr that takes a DAP Str.
Definition FONcStr.cc:53
static void handle_error(int stax, const string &err, const string &file, int line)
handle any netcdf errors
Definition FONcUtils.cc:429
static string gen_name(const vector< string > &embed, const string &name, string &original)
generate a new name for the embedded variable
Definition FONcUtils.cc:182
STL class.
STL class.
STL class.