bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
NCUInt16.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// Reza Reza Nekovei (rnekovei@ieee.org)
31
32#include "config_nc.h"
33
34static char rcsid[] not_used = { "$Id$" };
35
36#include <netcdf.h>
37#include <libdap/InternalErr.h>
38
39#include "NCUInt16.h"
40
41NCUInt16::NCUInt16(const string &n, const string &d) :
42 UInt16(n, d)
43{
44}
45
46NCUInt16::NCUInt16(const NCUInt16 &rhs) :
47 UInt16(rhs)
48{
49}
50
51NCUInt16::~NCUInt16()
52{
53}
54
56NCUInt16::operator=(const NCUInt16 &rhs)
57{
58 if (this == &rhs)
59 return *this;
60
61 dynamic_cast<NCUInt16&> (*this) = rhs;
62
63 return *this;
64}
65
66BaseType *
67NCUInt16::ptr_duplicate()
68{
69
70 return new NCUInt16(*this);
71}
72
73bool NCUInt16::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.");
89
90 short sht;
91#if NETCDF_VERSION >= 4
92 errstat = nc_get_var(ncid, varid, &sht);
93#else
94 size_t cor[MAX_NC_DIMS]; /* corner coordinates */
95 int num_dim; /* number of dim. in variable */
96 nc_type datatype; /* variable data type */
97 errstat = nc_inq_var(ncid, varid, (char *)0, &datatype, &num_dim, (int *)0,
98 (int *)0);
99 if( errstat != NC_NOERR )
100 {
101 throw Error(errstat,string("Could not read information about the variable `") + name() + string("'."));
102 }
103 if( datatype != NC_SHORT )
104 {
105 throw InternalErr(__FILE__, __LINE__, "Entered NCUInt16::read() with non-UInt16 variable!");
106 }
107
108 for( int id = 0; id <= num_dim && id < MAX_NC_DIMS; id++ )
109 {
110 cor[id] = 0;
111 }
112
113 errstat = nc_get_var1_short( ncid, varid, cor, &sht ) ;
114#endif
115 if (errstat != NC_NOERR)
116 throw Error(errstat, string("Could not read the variable `") + name() + string("'."));
117
118 set_read_p(true);
119
120 dods_uint16 uintg16 = (dods_uint16) sht;
121 val2buf(&uintg16);
122
123 if (nc_close(ncid) != NC_NOERR)
124 throw InternalErr(__FILE__, __LINE__, "Could not close the dataset!");
125
126 return true;
127}