40static char rcsid[] not_used = {
"$Id$" };
45#include <libdap/InternalErr.h>
48#include <libdap/debug.h>
50NCStr::NCStr(
const string &n,
const string &d) :
55NCStr::NCStr(
const NCStr &rhs) :
65NCStr::operator=(
const NCStr &rhs)
70 dynamic_cast<NCStr&
> (*this) = rhs;
78 return new NCStr(*
this);
90 errstat = nc_open(dataset().c_str(), NC_NOWRITE, &ncid);
92 if (errstat != NC_NOERR) {
93 string err =
"Could not open the dataset's file (" + dataset() +
")";
94 throw Error(errstat, err);
98 errstat = nc_inq_varid(ncid, name().c_str(), &varid);
99 if (errstat != NC_NOERR)
100 throw Error(errstat,
"Could not get variable ID.");
104 errstat = nc_inq_var(ncid, varid, (
char *) 0, &datatype, &num_dim, (
int *) 0, (
int *) 0);
105 if (errstat != NC_NOERR)
106 throw Error(errstat,
string(
"Could not read information about the variable `") + name() +
string(
"'."));
108#if NETCDF_VERSION == 3
111 if (datatype != NC_CHAR)
112 throw InternalErr(__FILE__, __LINE__,
"Entered String read method with non-string/char variable!");
119 errstat = nc_inq_vardimid(ncid, varid, &dim_id);
120 if (errstat != NC_NOERR)
121 throw Error(errstat,
string(
"Could not read the dimension id of `") + name() +
string(
"'."));
123 errstat = nc_inq_dimlen(ncid, dim_id, &dim_size);
124 if (errstat != NC_NOERR)
125 throw Error(errstat,
string(
"Could not read the dimension size of `") + name() +
string(
"'."));
127 char *charbuf =
new char[dim_size + 1];
129 size_t cor[1] = { 0 };
133 errstat = nc_get_vara_text(ncid, varid, cor, edg, charbuf);
134 if (errstat != NC_NOERR) {
136 throw Error(errstat,
string(
"Could not read data from the variable `") + name() +
string(
"'."));
139 charbuf[dim_size] =
'\0';
141 set_value(
string(charbuf));
145 else if (num_dim == 0) {
146 char *charbuf =
new char[2];
148 errstat = nc_get_var_text(ncid, varid, charbuf);
149 if (errstat != NC_NOERR) {
151 throw Error(errstat,
string(
"Could not read data from the variable `") + name() +
string(
"'."));
156 set_value(
string(charbuf));
161 throw Error(
string(
"Multidimensional character array found in string class while reading '") + name() +
string(
"'."));
164#if NETCDF_VERSION >= 4
166 size_t cor[MAX_NC_DIMS];
167 for (
int id = 0;
id <= num_dim &&
id < MAX_NC_DIMS;
id++)
172 vector<char*> strpp(1);
175 errstat = nc_get_var1_string(ncid, varid, cor, strpp.data());
176 if (errstat != NC_NOERR) {
177 throw Error(errstat,
string(
"Could not read data from the variable `") + name() +
string(
"'."));
182 set_value(
string(strpp[0]));
184 nc_free_string(1, strpp.data());
190 throw InternalErr(__FILE__, __LINE__,
"Entered String read method with an unrecognized datatype!");