bes  Updated for version 3.20.6
NCByte.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 1994-1996
27 // Please read the full copyright statement in the file COPYRIGHT.
28 //
29 // Authors:
30 // reza Reza Nekovei (reza@intcomm.net)
31 
32 // netCDF sub-class implementation for NCByte,...NCGrid.
33 // The files are patterned after the subcalssing examples
34 // Test<type>.c,h files.
35 //
36 // ReZa 1/12/95
37 
38 #include "config_nc.h"
39 
40 static char rcsid[] not_used = { "$Id$" };
41 
42 #include <netcdf.h>
43 
44 #include <InternalErr.h>
45 #include <util.h>
46 
47 #include "NCByte.h"
48 
49 // This `helper function' creates a pointer to the a NCByte and returns
50 // that pointer. It takes the same arguments as the class's ctor. If any of
51 // the variable classes are subclassed (e.g., to make a new Byte like
52 // HDFByte) then the corresponding function here, and in the other class
53 // definition files, needs to be changed so that it creates an instnace of
54 // the new (sub)class. Continuing the earlier example, that would mean that
55 // NewByte() would return a HDFByte, not a Byte.
56 //
57 // It is important that these function's names and return types do not change
58 // - they are called by the parser code (for the dds, at least) so if their
59 // names changes, that will break.
60 //
61 // The declarations for these fuctions (in util.h) should *not* need
62 // changing.
63 
64 
65 NCByte::NCByte(const string &n, const string &d) :
66  Byte(n, d)
67 {
68 }
69 
70 NCByte::NCByte(const NCByte &rhs) :
71  Byte(rhs)
72 {
73 }
74 
75 NCByte::~NCByte()
76 {
77 }
78 
79 NCByte &
80 NCByte::operator=(const NCByte &rhs)
81 {
82  if (this == &rhs)
83  return *this;
84 
85  dynamic_cast<NCByte&> (*this) = rhs;
86 
87  return *this;
88 }
89 
90 BaseType *
91 NCByte::ptr_duplicate()
92 {
93  return new NCByte(*this);
94 }
95 
96 bool NCByte::read() {
97  if (read_p()) // already done
98  return true;
99 
100  int ncid, errstat;
101  errstat = nc_open(dataset().c_str(), NC_NOWRITE, &ncid); /* netCDF id */
102  if (errstat != NC_NOERR) {
103  string err = "Could not open the dataset's file (" + dataset() + ")";
104  throw Error(errstat, err);
105  }
106 
107  int varid; /* variable Id */
108  errstat = nc_inq_varid(ncid, name().c_str(), &varid);
109  if (errstat != NC_NOERR)
110  throw InternalErr(__FILE__, __LINE__,
111  "Could not get variable ID for: " + name() + ". (error: " + long_to_string(errstat) + ").");
112 
113  dods_byte Dbyte;
114 #if NETCDF_VERSION >= 4
115  errstat = nc_get_var(ncid, varid, &Dbyte);
116 #else
117  size_t cor[MAX_NC_DIMS]; /* corner coordinates */
118  int num_dim; /* number of dim. in variable */
119  nc_type datatype; /* variable data type */
120  errstat = nc_inq_var(ncid, varid, (char *) 0, &datatype, &num_dim, (int *) 0, (int *) 0);
121  if (errstat != NC_NOERR) {
122  throw Error(errstat, string("Could not read information about the variable `") + name() + string("'."));
123  }
124  if (datatype != NC_BYTE) {
125  throw InternalErr(__FILE__, __LINE__, "Entered NCByte::read() with non-byte variable!");
126  }
127 
128  for (int id = 0; id <= num_dim && id < MAX_NC_DIMS; id++) {
129  cor[id] = 0;
130  }
131 
132  errstat = nc_get_var1_uchar(ncid, varid, cor, &Dbyte);
133 #endif
134 
135  if (errstat != NC_NOERR)
136  throw Error(errstat, string("Could not read the variable '") + name() + string("'."));
137 
138  set_read_p(true);
139 
140  val2buf(&Dbyte);
141 
142  if (nc_close(ncid) != NC_NOERR)
143  throw InternalErr(__FILE__, __LINE__, "Could not close the dataset!");
144 
145  return true;
146 }
147 
NCByte
Definition: NCByte.h:46
Error