bes Updated for version 3.20.13
FONcInt.cc
1// FONcInt.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/Int32.h>
35#include <libdap/UInt16.h>
36
37#include "FONcInt.h"
38#include "FONcUtils.h"
39#include "FONcAttributes.h"
40
49FONcInt::FONcInt( BaseType *b )
50 : FONcBaseType(), _bt( b )
51{
52 Int32 *i32 = dynamic_cast<Int32 *>(b) ;
53 if( !i32)
54 {
55 string s = (string)"File out netcdf, FONcInt was passed a "
56 + "variable that is not a DAP Int32 or UInt32" ;
57 throw BESInternalError( s, __FILE__, __LINE__ ) ;
58 }
59 _unsigned_short = false;
60}
61
62FONcInt::FONcInt( BaseType *b ,bool unsigned_short)
63 : FONcBaseType(), _bt( b ), _unsigned_short(unsigned_short)
64{
65
66 if(!_unsigned_short) {
67 Int32 *i32 = dynamic_cast<Int32 *>(b) ;
68 if( !i32)
69 {
70 string s = (string)"File out netcdf, FONcInt was passed a "
71 + "variable that is not a DAP Int32 " ;
72 throw BESInternalError( s, __FILE__, __LINE__ ) ;
73 }
74 }
75 else {
76 UInt16 *ui16 = dynamic_cast<UInt16 *>(b) ;
77 if( !ui16 )
78 {
79 string s = (string)"File out netcdf, FONcInt was passed a "
80 + "variable that is not a DAP UInt16" ;
81 throw BESInternalError( s, __FILE__, __LINE__ ) ;
82 }
83
84 }
85}
92{
93}
94
105void
107{
108 FONcBaseType::define( ncid ) ;
109
110 if( !_defined )
111 {
112 if(is_dap4) {
113 D4Attributes *d4_attrs = _bt->attributes();
114 updateD4AttrType(d4_attrs,NC_INT);
115 }
116 else {
117 AttrTable &attrs = _bt->get_attr_table();
118 updateAttrType(attrs,NC_INT);
119 }
120
121
122 FONcAttributes::add_variable_attributes( ncid, _varid, _bt ,isNetCDF4_ENHANCED(),is_dap4) ;
124 _varname, _orig_varname ) ;
125
126 _defined = true ;
127 }
128}
129
137void
138FONcInt::write( int ncid )
139{
140 BESDEBUG( "fonc", "FONcInt::write for var " << _varname << endl ) ;
141 size_t var_index[] = {0} ;
142 int stax = 0;
143
144 if (is_dap4)
145 _bt->intern_data();
146 else
147 _bt->intern_data(*get_eval(), *get_dds());
148
149 if(_unsigned_short) {
150 unsigned short * sdata = new unsigned short;
151 _bt->buf2val( (void**)&sdata ) ;
152 BESDEBUG( "fonc", "FONcInt::write for short value " << *sdata << endl ) ;
153 int temps = (int)(*sdata);
154 stax = nc_put_var1_int( ncid, _varid, var_index, &temps ) ;
155 delete sdata;
156 }
157 else {
158 int *data = new int ;
159 _bt->buf2val( (void**)&data ) ;
160 stax = nc_put_var1_int( ncid, _varid, var_index, data ) ;
161 delete data;
162 }
163 if( stax != NC_NOERR )
164 {
165 string err = (string)"fileout.netcdf - "
166 + "Failed to write int data for "
167 + _varname ;
168 FONcUtils::handle_error( stax, err, __FILE__, __LINE__ ) ;
169 }
170
171 BESDEBUG( "fonc", "FONcInt::done write for var " << _varname << endl ) ;
172}
173
178string
180{
181 return _bt->name() ;
182}
183
188nc_type
190{
191 return NC_INT ;
192}
193
200void
201FONcInt::dump( ostream &strm ) const
202{
203 strm << BESIndent::LMarg << "FONcInt::dump - ("
204 << (void *)this << ")" << endl ;
205 BESIndent::Indent() ;
206 strm << BESIndent::LMarg << "name = " << _bt->name() << endl ;
207 BESIndent::UnIndent() ;
208}
209
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:60
virtual void define(int ncid)
Define the variable in the netcdf file.
Definition: FONcBaseType.cc:60
virtual void define(int ncid)
define the DAP Int32 or UInt32 in the netcdf file
Definition: FONcInt.cc:106
virtual ~FONcInt()
Destructor that cleans up the instance.
Definition: FONcInt.cc:91
virtual string name()
returns the name of the DAP Int32 or UInt32
Definition: FONcInt.cc:179
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcInt.cc:201
virtual nc_type type()
returns the netcdf type of the DAP object
Definition: FONcInt.cc:189
virtual void write(int ncid)
Write the int out to the netcdf file.
Definition: FONcInt.cc:138
static void handle_error(int stax, const string &err, const string &file, int line)
handle any netcdf errors
Definition: FONcUtils.cc:424