bes  Updated for version 3.20.6
NCUInt32.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of nc_handler, a data handler for the OPeNDAP data
4 // server.
5 
6 // Copyright (c) 2002,2003 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
8 //
9 // This is free software; you can redistribute it and/or modify it under the
10 // terms of the GNU Lesser General Public License as published by the Free
11 // Software Foundation; either version 2.1 of the License, or (at your
12 // option) any later version.
13 //
14 // This software is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 // License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 
26 // (c) COPYRIGHT URI/MIT 1996
27 // Please read the full copyright statement in the file COPYRIGHT.
28 //
29 // Authors:
30 // jhrg,jimg James Gallagher (jgallagher@gso.uri.edu)
31 
32 #include "config_nc.h"
33 
34 static char rcsid[] not_used = { "$Id$" };
35 
36 #include <netcdf.h>
37 #include <InternalErr.h>
38 
39 #include "NCUInt32.h"
40 
41 NCUInt32::NCUInt32(const string &n, const string &d) :
42  UInt32(n, d)
43 {
44 }
45 
46 NCUInt32::NCUInt32(const NCUInt32 &rhs) :
47  UInt32(rhs)
48 {
49 }
50 
51 NCUInt32::~NCUInt32()
52 {
53 }
54 
55 NCUInt32 &
56 NCUInt32::operator=(const NCUInt32 &rhs)
57 {
58  if (this == &rhs)
59  return *this;
60 
61  dynamic_cast<NCUInt32&> (*this) = rhs;
62 
63  return *this;
64 }
65 
66 BaseType *
67 NCUInt32::ptr_duplicate()
68 {
69 
70  return new NCUInt32(*this);
71 }
72 
73 bool NCUInt32::read()
74 {
75  if (read_p()) // nothing to do
76  return true;
77 
78  int ncid, errstat;
79  errstat = nc_open(dataset().c_str(), NC_NOWRITE, &ncid); /* netCDF id */
80  if (errstat != NC_NOERR) {
81  string err = "Could not open the dataset's file (" + dataset() + ")";
82  throw Error(errstat, err);
83  }
84 
85  int varid; /* variable Id */
86  errstat = nc_inq_varid(ncid, name().c_str(), &varid);
87  if (errstat != NC_NOERR)
88  throw Error(errstat, "Could not get variable ID during read.");
89 
90 #if NETCDF_VERSION >= 4
91  int lng;
92  errstat = nc_get_var(ncid, varid, &lng);
93 #else
94  long int lng ;
95  size_t cor[MAX_NC_DIMS]; /* corner coordinates */
96  int num_dim; /* number of dim. in variable */
97  nc_type datatype; /* variable data type */
98  errstat = nc_inq_var(ncid, varid, (char *)0, &datatype, &num_dim, (int *)0,
99  (int *)0);
100  if( errstat != NC_NOERR )
101  {
102  throw Error(errstat,string("Could not read information about the variable `") + name() + string("'."));
103  }
104  if( datatype != NC_LONG )
105  {
106  throw InternalErr(__FILE__, __LINE__, "Entered NCUInt32::read() with non-Int32 variable!");
107  }
108 
109  for( int id = 0; id <= num_dim && id < MAX_NC_DIMS; id++ )
110  {
111  cor[id] = 0;
112  }
113 
114  errstat = nc_get_var1_long( ncid, varid, cor, &lng ) ;
115 #endif
116  if (errstat != NC_NOERR)
117  throw Error(errstat, string("Could not read the variable `") + name() + string("'."));
118 
119  set_read_p(true);
120 
121  dods_uint32 uintg32 = (dods_uint32) lng;
122  val2buf(&uintg32);
123 
124  if (nc_close(ncid) != NC_NOERR)
125  throw InternalErr(__FILE__, __LINE__, "Could not close the dataset!");
126 
127  return true;
128 }
NCUInt32
Definition: NCUInt32.h:46
Error