34 using std::istringstream;
39 #include <BESInternalError.h> 42 #include "DapFunctionUtils.h" 44 #include "FONcAttributes.h" 45 #include "FONcUtils.h" 79 BaseType *parent = b->get_parent();
81 FONcAttributes::add_variable_attributes_worker(ncid, varid, parent, emb_name);
101 void FONcAttributes::add_variable_attributes_worker(
int ncid,
int varid, BaseType *b,
string &emb_name) {
103 BaseType *parent = b->get_parent();
105 FONcAttributes::add_variable_attributes_worker(ncid, varid, parent, emb_name);
107 if (!emb_name.empty()) {
108 emb_name += FONC_EMBEDDED_SEPARATOR;
110 emb_name += b->name();
112 add_attributes(ncid, varid, b->get_attr_table(), b->name(), emb_name);
131 unsigned int num_attrs = attrs.get_size();
133 AttrTable::Attr_iter i = attrs.attr_begin();
134 AttrTable::Attr_iter e = attrs.attr_end();
135 for (; i != e; i++) {
136 unsigned int num_vals = attrs.get_attr_num(i);
138 add_attributes_worker(ncid, varid, var_name, attrs, i, prepend_attr);
158 void FONcAttributes::add_attributes_worker(
int ncid,
int varid,
const string &var_name,
159 AttrTable &attrs, AttrTable::Attr_iter &attr,
160 const string &prepend_attr) {
162 AttrType attrType = attrs.get_attr_type(attr);
164 string attr_name = attrs.get_name(attr);
165 string new_attr_name(
"");
166 if (!prepend_attr.empty()) {
167 new_attr_name = prepend_attr + FONC_EMBEDDED_SEPARATOR + attr_name;
174 if (varid == NC_GLOBAL && attrType==Attr_container &&
BESUtil::endsWith(attr_name,
"_GLOBAL")) {
176 "Suppressing global AttributeTable name '" << attr_name <<
"' from inclusion in NetCDF attributes namespace chain." << endl);
180 new_attr_name = attr_name;
187 string new_name = new_attr_name;
188 if (!var_name.empty()) {
189 new_name = var_name + FONC_ATTRIBUTE_SEPARATOR + new_attr_name;
200 if (varid == NC_GLOBAL) {
201 BESDEBUG(
"fonc",
"FONcAttributes::addattrs() - Adding global attributes " << attr_name << endl);
204 BESDEBUG(
"fonc",
"FONcAttributes::addattrs() - Adding attribute " << new_name << endl);
208 unsigned int attri = 0;
209 unsigned int num_vals = attrs.get_attr_num(attr);
211 case Attr_container: {
213 BESDEBUG(
"fonc",
"Attribute " << attr_name <<
" is an attribute container. new_attr_name: \"" << new_attr_name <<
"\"" << endl);
214 AttrTable *container = attrs.get_attr_table(attr);
222 unsigned char vals[num_vals];
223 for (attri = 0; attri < num_vals; attri++) {
224 string val = attrs.get_attr(attr, attri);
225 istringstream is(val);
226 unsigned int uival = 0;
228 vals[attri] = (
unsigned char) uival;
230 stax = nc_put_att_uchar(ncid, varid, new_name.c_str(), NC_BYTE,
232 if (stax != NC_NOERR) {
233 string err = (string)
"File out netcdf, " 234 +
"failed to write byte attribute " + new_name;
241 short vals[num_vals];
242 for (attri = 0; attri < num_vals; attri++) {
243 string val = attrs.get_attr(attr, attri);
244 istringstream is(val);
249 stax = nc_put_att_short(ncid, varid, new_name.c_str(), NC_SHORT,
251 if (stax != NC_NOERR) {
252 string err = (string)
"File out netcdf, " 253 +
"failed to write short attribute " + new_name;
262 for (attri = 0; attri < num_vals; attri++) {
263 string val = attrs.get_attr(attr, attri);
264 istringstream is(val);
269 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals,
271 if (stax != NC_NOERR) {
272 string err = (string)
"File out netcdf, " 273 +
"failed to write unsinged short attribute " + new_name;
281 for (attri = 0; attri < num_vals; attri++) {
282 string val = attrs.get_attr(attr, attri);
283 istringstream is(val);
288 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals,
290 if (stax != NC_NOERR) {
291 string err = (string)
"File out netcdf, " 292 +
"failed to write int attribute " + new_name;
301 for (attri = 0; attri < num_vals; attri++) {
302 string val = attrs.get_attr(attr, attri);
303 istringstream is(val);
308 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals,
310 if (stax != NC_NOERR) {
311 string err = (string)
"File out netcdf, " 312 +
"failed to write byte attribute " + new_name;
319 float vals[num_vals];
320 for (attri = 0; attri < num_vals; attri++) {
321 string val = attrs.get_attr(attr, attri);
322 istringstream is(val);
327 stax = nc_put_att_float(ncid, varid, new_name.c_str(), NC_FLOAT,
329 if (stax != NC_NOERR) {
330 string err = (string)
"File out netcdf, " 331 +
"failed to write float attribute " + new_name;
338 double vals[num_vals];
339 for (attri = 0; attri < num_vals; attri++) {
340 string val = attrs.get_attr(attr, attri);
341 istringstream is(val);
346 stax = nc_put_att_double(ncid, varid, new_name.c_str(), NC_DOUBLE,
348 if (stax != NC_NOERR) {
349 string err = (string)
"File out netcdf, " 350 +
"failed to write double attribute " + new_name;
360 string val = attrs.get_attr(attr, 0);
361 for (attri = 1; attri < num_vals; attri++) {
362 val +=
"\n" + attrs.get_attr(attr, attri);
364 if (attr_name != _FillValue) {
365 stax = nc_put_att_text(ncid, varid, new_name.c_str(), val.length(), val.c_str());
369 "FONcAttributes::add_attributes_worker - Original attribute value is first character: " << val.c_str()[0]<< endl);
370 stax = nc_put_att_text(ncid, varid, new_name.c_str(), 1, val.c_str());
371 if (stax == NC_NOERR) {
373 string new_name_fillvalue =
"Orig_FillValue";
375 "FONcAttributes::add_attributes_worker - New attribute value is original value: " << val.c_str() << endl);
376 attrs.append_attr(new_name_fillvalue,
"String", val);
377 stax = nc_put_att_text(ncid, varid, new_name_fillvalue.c_str(), val.length(), val.c_str());
381 if (stax != NC_NOERR) {
382 string err = (string)
"File out netcdf, " 383 +
"failed to write string attribute " + new_name;
390 string err = (string)
"File out netcdf, " 391 +
"failed to write unknown type of attribute " + new_name;
412 const string &var_name,
const string &orig) {
413 if (var_name != orig) {
414 string attr_name = FONC_ORIGINAL_NAME;
415 int stax = nc_put_att_text(ncid, varid, attr_name.c_str(),
416 orig.length(), orig.c_str());
417 if (stax != NC_NOERR) {
418 string err = (string)
"File out netcdf, " 419 +
"failed to write change of name attribute for " static void handle_error(int stax, const string &err, const string &file, int line)
handle any netcdf errors
static void add_variable_attributes(int ncid, int varid, BaseType *b)
Add the attributes for an OPeNDAP variable to the netcdf file.
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 bool endsWith(std::string const &fullString, std::string const &ending)
static void add_attributes(int ncid, int varid, AttrTable &attrs, const string &var_name, const string &prepend_attr)
helper function for add_attributes
static string id2netcdf(string in)
convert the provided string to a netcdf allowed identifier.