bes  Updated for version 3.20.8
FONcUShort.cc
1 // FONcUShort.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 //
23 // Authors:
24 // kyang Kent Yang <myang6@hdfgroup.org>
25 // Note: The code follows FONcShort.cc.
26 
27 
28 #include <BESInternalError.h>
29 #include <BESDebug.h>
30 #include <UInt16.h>
31 
32 #include "FONcUShort.h"
33 #include "FONcUtils.h"
34 #include "FONcAttributes.h"
35 
44 FONcUShort::FONcUShort( BaseType *b )
45  : FONcBaseType(), _bt( b )
46 {
47  UInt16 *u16 = dynamic_cast<UInt16 *>(b) ;
48  if( !u16 )
49  {
50  string s = (string)"File out netcdf-4, FONcUShort was passed a "
51  + "variable that is not a DAP UInt16" ;
52  throw BESInternalError( s, __FILE__, __LINE__ ) ;
53  }
54 }
55 
62 {
63 }
64 
75 void
76 FONcUShort::define( int ncid )
77 {
78  FONcBaseType::define( ncid ) ;
79 
80  if( !_defined )
81  {
82  FONcAttributes::add_variable_attributes( ncid, _varid, _bt,isNetCDF4_ENHANCED(),is_dap4 ) ;
84  _varname, _orig_varname ) ;
85 
86  _defined = true ;
87  }
88 }
89 
99 void
100 FONcUShort::write( int ncid )
101 {
102  BESDEBUG( "fonc", "FONcUShort::write for var " << _varname << endl ) ;
103  size_t var_index[] = {0} ;
104  unsigned short *data = new unsigned short ;
105  _bt->buf2val( (void**)&data ) ;
106  int stax = nc_put_var1_ushort( ncid, _varid, var_index, data ) ;
107  if( stax != NC_NOERR )
108  {
109  string err = (string)"fileout.netcdf-4 - "
110  + "Failed to write short data for "
111  + _varname ;
112  FONcUtils::handle_error( stax, err, __FILE__, __LINE__ ) ;
113  }
114  delete data ;
115  BESDEBUG( "fonc", "FONcUShort::done write for var " << _varname << endl ) ;
116 }
117 
122 string
124 {
125  return _bt->name() ;
126 }
127 
132 nc_type
134 {
135  return NC_USHORT ;
136 }
137 
144 void
145 FONcUShort::dump( ostream &strm ) const
146 {
147  strm << BESIndent::LMarg << "FONcUShort::dump - ("
148  << (void *)this << ")" << endl ;
149  BESIndent::Indent() ;
150  strm << BESIndent::LMarg << "name = " << _bt->name() << endl ;
151  BESIndent::UnIndent() ;
152 }
153 
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:61
virtual void define(int ncid)
Define the variable in the netcdf file.
Definition: FONcBaseType.cc:54
virtual void define(int ncid)
define the DAP Int16 or UInt16 in the netcdf file
Definition: FONcUShort.cc:76
virtual nc_type type()
returns the netcdf type of the DAP object
Definition: FONcUShort.cc:133
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcUShort.cc:145
virtual string name()
returns the name of the DAP Int16 or UInt16
Definition: FONcUShort.cc:123
FONcUShort(BaseType *b)
Constructor for FOncShort that takes a DAP UInt16.
Definition: FONcUShort.cc:44
virtual ~FONcUShort()
Destructor that cleans up the short.
Definition: FONcUShort.cc:61
virtual void write(int ncid)
Write the ushort out to the netcdf file.
Definition: FONcUShort.cc:100
static void handle_error(int stax, const string &err, const string &file, int line)
handle any netcdf errors
Definition: FONcUtils.cc:399