bes  Updated for version 3.20.10
FONcShort.cc
1 // FONcShort.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 #include <libdap/Int16.h>
35 #include <libdap/Byte.h>
36 
37 #include "FONcShort.h"
38 #include "FONcUtils.h"
39 #include "FONcAttributes.h"
40 
50  : FONcBaseType(), _bt(b)
51 {
52  Int16 *i16 = dynamic_cast<Int16 *>(b);
53  if (!i16) {
54  string s = (string) "File out netcdf, FONcShort was passed a "
55  + "variable that is not a DAP Int16";
56  throw BESInternalError(s, __FILE__, __LINE__);
57  }
58  _unsigned_int8 = false;
59 }
60 
61 FONcShort::FONcShort(BaseType *b, bool unsigned_int8)
62  : FONcBaseType(), _bt(b), _unsigned_int8(unsigned_int8)
63 {
64  if(!_unsigned_int8) {
65  Int16 *i16 = dynamic_cast<Int16 *>(b);
66  if (!i16) {
67  string s = (string) "File out netcdf, FONcShort was passed a "
68  + "variable that is not a DAP Int16";
69  throw BESInternalError(s, __FILE__, __LINE__);
70  }
71  }
72  else {
73  Byte *ui8 = dynamic_cast<Byte *>(b);
74  if (!ui8) {
75  string s = (string) "File out netcdf, FONcShort was passed a "
76  + "variable that is not a DAP Byte";
77  throw BESInternalError(s, __FILE__, __LINE__);
78  }
79  }
80 }
81 
88 {
89 }
90 
101 void
103 {
104  FONcBaseType::define(ncid);
105 
106  if( !_defined )
107  {
108 
109  if(is_dap4) {
110  D4Attributes *d4_attrs = _bt->attributes();
111  updateD4AttrType(d4_attrs,NC_SHORT);
112  }
113  else {
114  AttrTable &attrs = _bt->get_attr_table();
115  updateAttrType(attrs,NC_SHORT);
116  }
117 
118 
119  FONcAttributes::add_variable_attributes( ncid, _varid, _bt,isNetCDF4_ENHANCED(),is_dap4 ) ;
120  FONcAttributes::add_original_name( ncid, _varid,
121  _varname, _orig_varname ) ;
122 
123  _defined = true;
124  }
125 }
126 
136 void
138 {
139  BESDEBUG("fonc", "FONcShort::write for var " << _varname << endl);
140  size_t var_index[] = {0};
141  int stax = 0;
142 
143  if (is_dap4)
144  _bt->intern_data();
145  else
146  _bt->intern_data(*get_eval(), *get_dds());
147 
148  if(_unsigned_int8) {
149  unsigned char *data_8 = new unsigned char;
150  _bt->buf2val((void **) &data_8);
151  short temp_8 = (short)(*data_8);
152  stax = nc_put_var1_short(ncid, _varid, var_index, &temp_8);
153  delete data_8;
154  }
155  else {
156  short *data = new short;
157  _bt->buf2val((void **) &data);
158  stax = nc_put_var1_short(ncid, _varid, var_index, data);
159  delete data;
160  }
161  if (stax != NC_NOERR) {
162  string err = (string) "fileout.netcdf - "
163  + "Failed to write short data for "
164  + _varname;
165  FONcUtils::handle_error(stax, err, __FILE__, __LINE__);
166  }
167  BESDEBUG("fonc", "FONcShort::done write for var " << _varname << endl);
168 }
169 
174 string
176 {
177  return _bt->name();
178 }
179 
184 nc_type
186 {
187  return NC_SHORT;
188 }
189 
196 void
197 FONcShort::dump(ostream &strm) const
198 {
199  strm << BESIndent::LMarg << "FONcShort::dump - ("
200  << (void *) this << ")" << endl;
201  BESIndent::Indent();
202  strm << BESIndent::LMarg << "name = " << _bt->name() << endl;
203  BESIndent::UnIndent();
204 }
205 
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.
A DAP BaseType with file out netcdf information included.
Definition: FONcBaseType.h:64
virtual void define(int ncid)
Define the variable in the netcdf file.
Definition: FONcBaseType.cc:60
virtual string name()
returns the name of the DAP Int16 or UInt16
Definition: FONcShort.cc:175
FONcShort(BaseType *b)
Constructor for FOncShort that takes a DAP Int16 or UInt16.
Definition: FONcShort.cc:49
virtual void define(int ncid)
define the DAP Int16 or UInt16 in the netcdf file
Definition: FONcShort.cc:102
virtual nc_type type()
returns the netcdf type of the DAP object
Definition: FONcShort.cc:185
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcShort.cc:197
virtual ~FONcShort()
Destructor that cleans up the short.
Definition: FONcShort.cc:87
virtual void write(int ncid)
Write the short out to the netcdf file.
Definition: FONcShort.cc:137
static void handle_error(int stax, const string &err, const string &file, int line)
handle any netcdf errors
Definition: FONcUtils.cc:424