37#include <BESInternalError.h>
41#include "DapFunctionUtils.h"
43#include "FONcAttributes.h"
81 BaseType *parent = b->get_parent();
85 if (
true != is_dap4 || parent->type() != dods_group_c)
86 FONcAttributes::add_variable_attributes_worker(ncid, varid, parent, emb_name, is_nc_enhanced, is_dap4);
91 BESDEBUG(
"dap",
"FONcAttributes::add_variable_attributes() after parent " << endl);
95 add_attributes(ncid, varid, b->get_attr_table(), b->name(),
"", is_nc_enhanced);
113void FONcAttributes::add_variable_attributes_worker(
int ncid,
int varid, BaseType *b,
string &emb_name,
114 bool is_nc_enhanced,
bool is_dap4) {
116 BaseType *parent = b->get_parent();
118 FONcAttributes::add_variable_attributes_worker(ncid, varid, parent, emb_name, is_nc_enhanced, is_dap4);
120 if (!emb_name.empty()) {
121 emb_name += FONC_EMBEDDED_SEPARATOR;
123 emb_name += b->name();
128 add_attributes(ncid, varid, b->get_attr_table(), b->name(), emb_name, is_nc_enhanced);
148 const string &prepend_attr,
bool is_nc_enhanced) {
150 unsigned int num_attrs = attrs.get_size();
152 AttrTable::Attr_iter i = attrs.attr_begin();
153 AttrTable::Attr_iter e = attrs.attr_end();
154 for (; i != e; i++) {
155 unsigned int num_vals = attrs.get_attr_num(i);
157 add_attributes_worker(ncid, varid, var_name, attrs, i, prepend_attr, is_nc_enhanced);
178 const string &prepend_attr,
bool is_nc_enhanced) {
180 BESDEBUG(
"dap",
"FONcAttributes::add_dap4_attributes() number of attributes " << d4_attrs << endl);
181 for (D4Attributes::D4AttributesIter ii = d4_attrs->attribute_begin(), ee = d4_attrs->attribute_end();
183 string name = (*ii)->name();
184 unsigned int num_vals = (*ii)->num_values();
185 if (num_vals || varid == NC_GLOBAL)
186 add_dap4_attributes_worker(ncid, varid, var_name, *ii, prepend_attr, is_nc_enhanced);
203void FONcAttributes::add_attributes_worker(
int ncid,
int varid,
const string &var_name,
204 AttrTable &attrs, AttrTable::Attr_iter &attr,
205 const string &prepend_attr,
bool is_nc_enhanced) {
207 AttrType attrType = attrs.get_attr_type(attr);
209 string attr_name = attrs.get_name(attr);
210 string new_attr_name(
"");
211 if (!prepend_attr.empty()) {
212 new_attr_name = prepend_attr + FONC_EMBEDDED_SEPARATOR + attr_name;
218 if (varid == NC_GLOBAL && attrType == Attr_container &&
BESUtil::endsWith(attr_name,
"_GLOBAL")) {
219 BESDEBUG(
"fonc",
"Suppressing global AttributeTable name '" << attr_name
220 <<
"' from inclusion in NetCDF attributes namespace chain." << endl);
224 new_attr_name = attr_name;
232 string new_name = new_attr_name;
233 if (!var_name.empty()) {
234 new_name = var_name + FONC_ATTRIBUTE_SEPARATOR + new_attr_name;
243 BESDEBUG(
"fonc",
"FONcAttributes name: " << new_name << endl);
244 BESDEBUG(
"fonc",
"FONcAttributes type: " << attrType << endl);
247 if (varid == NC_GLOBAL) {
248 BESDEBUG(
"fonc",
"FONcAttributes::add_attributes_worker() - Adding global attributes " << attr_name << endl);
251 BESDEBUG(
"fonc",
"FONcAttributes::add_attributes_worker() - Adding attribute " << new_name << endl);
255 if (is_nc_enhanced ==
true)
259 unsigned int attri = 0;
260 unsigned int num_vals = attrs.get_attr_num(attr);
262 case Attr_container: {
265 "Attribute " << attr_name <<
" is an attribute container. new_attr_name: \"" << new_attr_name
267 AttrTable *container = attrs.get_attr_table(attr);
269 add_attributes(ncid, varid, *container, var_name, new_attr_name, is_nc_enhanced);
278 vals.resize(num_vals);
279 for (attri = 0; attri < num_vals; attri++) {
280 string val = attrs.get_attr(attr, attri);
281 istringstream is(val);
282 unsigned int uival = 0;
284 vals[attri] = (short) uival;
286 stax = nc_put_att_short(ncid, varid, new_name.c_str(), NC_SHORT, num_vals, vals.data());
287 if (stax != NC_NOERR) {
288 string err = (string)
"File out netcdf, failed to write byte attribute " + new_name;
296 vals.resize(num_vals);
297 for (attri = 0; attri < num_vals; attri++) {
298 string val = attrs.get_attr(attr, attri);
299 istringstream is(val);
304 stax = nc_put_att_short(ncid, varid, new_name.c_str(), NC_SHORT, num_vals, vals.data());
305 if (stax != NC_NOERR) {
306 string err = (string)
"File out netcdf, " +
"failed to write short attribute " + new_name;
315 vals.resize(num_vals);
316 for (attri = 0; attri < num_vals; attri++) {
317 string val = attrs.get_attr(attr, attri);
318 istringstream is(val);
323 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, vals.data());
324 if (stax != NC_NOERR) {
325 string err = (string)
"File out netcdf, " +
"failed to write unsinged short attribute " + new_name;
333 vals.resize(num_vals);
334 for (attri = 0; attri < num_vals; attri++) {
335 string val = attrs.get_attr(attr, attri);
336 istringstream is(val);
341 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, vals.data());
342 if (stax != NC_NOERR) {
343 string err = (string)
"File out netcdf, " +
"failed to write int attribute " + new_name;
351 string err = (string)
"File out netcdf, " +
"failed to write unsigned int attribute " + new_name;
352 err = err +
" for classic model because of potential overflow. ";
353 err = err +
" Please use the netCDF4 enhanced model. ";
359 vals.resize(num_vals);
360 for (attri = 0; attri < num_vals; attri++) {
361 string val = attrs.get_attr(attr, attri);
362 istringstream is(val);
367 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals,
369 if (stax != NC_NOERR) {
370 string err = (string)
"File out netcdf, "
371 +
"failed to write unsigned int attribute " + new_name;
380 vals.resize(num_vals);
381 for (attri = 0; attri < num_vals; attri++) {
382 string val = attrs.get_attr(attr, attri);
383 const char *cval = val.c_str();
386 fval = strtod(cval, NULL);
390 stax = nc_put_att_float(ncid, varid, new_name.c_str(), NC_FLOAT, num_vals, vals.data());
391 if (stax != NC_NOERR) {
392 string err = (string)
"File out netcdf, " +
"failed to write float attribute " + new_name;
400 vals.resize(num_vals);
401 for (attri = 0; attri < num_vals; attri++) {
402 string val = attrs.get_attr(attr, attri);
403 const char *cval = val.c_str();
406 dval = strtod(cval, NULL);
410 stax = nc_put_att_double(ncid, varid, new_name.c_str(), NC_DOUBLE, num_vals, vals.data());
411 if (stax != NC_NOERR) {
412 string err = (string)
"File out netcdf, " +
"failed to write double attribute " + new_name;
422 string val = attrs.get_attr(attr, 0);
423 for (attri = 1; attri < num_vals; attri++) {
424 val +=
"\n" + attrs.get_attr(attr, attri);
426 if (attr_name != _FillValue) {
427 stax = nc_put_att_text(ncid, varid, new_name.c_str(), val.size(), val.c_str());
431 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
432 << val.c_str()[0] << endl);
433 stax = nc_put_att_text(ncid, varid, new_name.c_str(), 1, val.c_str());
434 if (stax == NC_NOERR) {
436 string new_name_fillvalue =
"Orig_FillValue";
438 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
439 << val.c_str() << endl);
443 attrs.append_attr(new_name_fillvalue,
"String", val);
445 stax = nc_put_att_text(ncid, varid, new_name_fillvalue.c_str(), val.size(), val.c_str());
449 if (stax != NC_NOERR) {
450 string err = (string)
"File out netcdf, " +
"failed to write string attribute " + new_name;
457 string err = (string)
"File out netcdf, " +
"failed to write unknown type of attribute " + new_name;
477void FONcAttributes::add_dap4_attributes_worker(
int ncid,
int varid,
const string &var_name,
479 const string &prepend_attr,
bool is_nc_enhanced) {
480 D4AttributeType d4_attr_type = attr->type();
482 string d4_attr_name = attr->name();
483 BESDEBUG(
"dap",
"FONcAttributes:: D4 attribute name is " << d4_attr_name << endl);
484 string new_attr_name(
"");
485 if (!prepend_attr.empty()) {
486 new_attr_name = prepend_attr + FONC_EMBEDDED_SEPARATOR + d4_attr_name;
487 BESDEBUG(
"dap",
"FONcAttributes:: D4 new attribute name is " << new_attr_name << endl);
494 if (varid == NC_GLOBAL && d4_attr_type == attr_container_c
497 BESDEBUG(
"fonc",
"Suppressing global AttributeTable name '" << d4_attr_name
498 <<
"' from inclusion in NetCDF attributes namespace chain." << endl);
502 new_attr_name = d4_attr_name;
511 string new_name = new_attr_name;
512 if (!var_name.empty()) {
513 new_name = var_name + FONC_ATTRIBUTE_SEPARATOR + new_attr_name;
524 if (varid == NC_GLOBAL) {
525 BESDEBUG(
"fonc",
"FONcAttributes::addattrs() - Adding global attributes " << d4_attr_name << endl);
528 BESDEBUG(
"fonc",
"FONcAttributes::addattrs() - Adding attribute " << new_name << endl);
533 if (is_nc_enhanced ==
true)
538 string attr_type =
"unknown";
539 unsigned int attri = 0;
541 unsigned int num_vals = attr->num_values();
542 switch (d4_attr_type) {
543 case attr_container_c: {
546 "Attribute " << d4_attr_name <<
" is an attribute container. new_attr_name: \""
549 D4Attributes *c_attributes = attr->attributes();
560 vector <int8_t> vals;
561 vals.resize(num_vals);
563 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
565 istringstream is(val);
568 vals[attri] = (int8_t) uival;
571 stax = nc_put_att_schar(ncid, varid, new_name.c_str(), NC_BYTE, num_vals, vals.data());
572 if (stax != NC_NOERR) {
573 string err = (string)
"File out netcdf-4 classic for DAP4,"
574 +
" failed to write signed 8-bit integer attribute " + new_name;
583 vals.resize(num_vals);
585 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
587 istringstream is(val);
590 unsigned int uival = 0;
592 vals[attri] = (short) uival;
595 stax = nc_put_att_short(ncid, varid, new_name.c_str(), NC_SHORT, num_vals, vals.data());
603 vals.resize(num_vals);
605 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
607 istringstream is(val);
614 stax = nc_put_att_short(ncid, varid, new_name.c_str(), NC_SHORT, num_vals, vals.data());
619 case attr_uint16_c: {
625 vals.resize(num_vals);
626 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
628 istringstream is(val);
635 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, vals.data());
643 vals.resize(num_vals);
645 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
647 istringstream is(val);
654 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, vals.data());
659 case attr_uint32_c: {
663 vals.resize(num_vals);
665 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
667 istringstream is(val);
674 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, vals.data());
679 case attr_float32_c: {
682 vals.resize(num_vals);
684 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
686 const char *cval = val.c_str();
689 sval = strtod(cval, NULL);
695 stax = nc_put_att_float(ncid, varid, new_name.c_str(), NC_FLOAT, num_vals, vals.data());
700 case attr_float64_c: {
703 vals.resize(num_vals);
705 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
707 const char *cval = val.c_str();
710 sval = strtod(cval, NULL);
715 stax = nc_put_att_double(ncid, varid, new_name.c_str(), NC_DOUBLE, num_vals, vals.data());
722 case attr_otherxml_c: {
723 attr_type =
"string";
724 D4Attribute::D4AttributeIter vi, ve;
725 vi = attr->value_begin();
726 ve = attr->value_end();
731 for (; vi != ve; vi++) {
735 if (d4_attr_name != _FillValue) {
736 stax = nc_put_att_text(ncid, varid, new_name.c_str(), val.size(), val.c_str());
740 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
741 << val.c_str()[0] << endl);
742 stax = nc_put_att_text(ncid, varid, new_name.c_str(), 1, val.c_str());
743 if (stax == NC_NOERR) {
745 string new_name_fillvalue =
"Orig_FillValue";
747 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
748 << val.c_str() << endl);
752 attrs.append_attr(new_name_fillvalue,
"String", val);
754 stax = nc_put_att_text(ncid, varid, new_name_fillvalue.c_str(), val.size(), val.c_str());
769 (string)
"File out netcdf, failed to write unknown/unsupported type of attribute " + new_name;
775 if (stax != NC_NOERR) {
776 string err = (string)
"File out netcdf, failed to write " + attr_type +
" attribute " + new_name;
797 const string &var_name,
const string &orig) {
798 if (var_name != orig) {
799 string attr_name = FONC_ORIGINAL_NAME;
800 int stax = nc_put_att_text(ncid, varid, attr_name.c_str(),
801 orig.size(), orig.c_str());
802 if (stax != NC_NOERR) {
803 string err = (string)
"File out netcdf, "
804 +
"failed to write change of name attribute for "
826 const string &var_attr_name, AttrTable attrs, AttrTable::Attr_iter &attr,
827 bool is_nc_enhanced) {
830 string attr_type =
"unknown";
831 AttrType attrType = attrs.get_attr_type(attr);
832 BESDEBUG(
"fonc",
"FONcAttributes write_attrs_for_nc4_type name: " << var_attr_name << endl);
833 BESDEBUG(
"fonc",
"FONcAttributes write_attrs_for_nc4_type type: " << attrType << endl);
834 unsigned int attri = 0;
835 unsigned int num_vals = attrs.get_attr_num(attr);
837 case Attr_container: {
839 BESDEBUG(
"fonc",
"This is an attribute container. attr_name: \"" << global_attr_name <<
"\"" << endl);
840 AttrTable *container = attrs.get_attr_table(attr);
842 add_attributes(ncid, varid, *container, var_name, global_attr_name, is_nc_enhanced);
849 vector<unsigned char> vals;
850 vals.resize(num_vals);
851 for (attri = 0; attri < num_vals; attri++) {
852 string val = attrs.get_attr(attr, attri);
853 istringstream is(val);
854 unsigned int uival = 0;
856 vals[attri] = (
unsigned char) uival;
858 stax = nc_put_att_uchar(ncid, varid, var_attr_name.c_str(), NC_UBYTE, num_vals, vals.data());
860 if (stax != NC_NOERR) {
861 string err = (string)
"File out netcdf, " +
"failed to write byte attribute " + var_attr_name;
871 vals.resize(num_vals);
872 for (attri = 0; attri < num_vals; attri++) {
873 string val = attrs.get_attr(attr, attri);
874 istringstream is(val);
879 stax = nc_put_att_short(ncid, varid, var_attr_name.c_str(), NC_SHORT, num_vals, vals.data());
881 if (stax != NC_NOERR) {
882 string err = (string)
"File out netcdf, " +
"failed to write short attribute " + var_attr_name;
894 vector<unsigned short> vals;
895 vals.resize(num_vals);
896 for (attri = 0; attri < num_vals; attri++) {
897 string val = attrs.get_attr(attr, attri);
898 istringstream is(val);
899 unsigned short ival = 0;
903 stax = nc_put_att_ushort(ncid, varid, var_attr_name.c_str(), NC_USHORT, num_vals, vals.data());
905 if (stax != NC_NOERR) {
906 string err = (string)
"File out netcdf, " +
"failed to write unsigned short attribute " + var_attr_name;
918 vals.resize(num_vals);
919 for (attri = 0; attri < num_vals; attri++) {
920 string val = attrs.get_attr(attr, attri);
921 istringstream is(val);
926 stax = nc_put_att_int(ncid, varid, var_attr_name.c_str(), NC_INT, num_vals, vals.data());
929 if (stax != NC_NOERR) {
930 string err = (string)
"File out netcdf, " +
"failed to write int attribute " + var_attr_name;
941 vector<unsigned int> vals;
942 vals.resize(num_vals);
943 for (attri = 0; attri < num_vals; attri++) {
944 string val = attrs.get_attr(attr, attri);
945 istringstream is(val);
946 unsigned int lval = 0;
950 stax = nc_put_att_uint(ncid, varid, var_attr_name.c_str(), NC_UINT, num_vals, vals.data());
952 if (stax != NC_NOERR) {
953 string err = (string)
"File out netcdf, " +
"failed to write byte attribute " + var_attr_name;
965 vals.resize(num_vals);
966 for (attri = 0; attri < num_vals; attri++) {
967 string val = attrs.get_attr(attr, attri);
968 const char *cval = val.c_str();
971 fval = strtod(cval, NULL);
975 stax = nc_put_att_float(ncid, varid, var_attr_name.c_str(), NC_FLOAT, num_vals, vals.data());
978 if (stax != NC_NOERR) {
979 string err = (string)
"File out netcdf, " +
"failed to write float attribute " + var_attr_name;
991 vals.resize(num_vals);
992 for (attri = 0; attri < num_vals; attri++) {
993 string val = attrs.get_attr(attr, attri);
994 const char *cval = val.c_str();
997 dval = strtod(cval, NULL);
1001 stax = nc_put_att_double(ncid, varid, var_attr_name.c_str(), NC_DOUBLE, num_vals, vals.data());
1004 if (stax != NC_NOERR) {
1005 string err = (string)
"File out netcdf, " +
"failed to write double attribute " + var_attr_name;
1015 case Attr_other_xml: {
1017 string val = attrs.get_attr(attr, 0);
1018 for (attri = 1; attri < num_vals; attri++) {
1019 val +=
"\n" + attrs.get_attr(attr, attri);
1021 string attr_name = attrs.get_name(attr);
1022 if (attr_name != _FillValue) {
1023 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), val.size(), val.c_str());
1027 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
1028 << val.c_str()[0] << endl);
1029 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 1, val.c_str());
1030 if (stax == NC_NOERR) {
1032 string var_attr_name_fillvalue =
"Orig_FillValue";
1034 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
1035 << val.c_str() << endl);
1040 attrs.append_attr(var_attr_name_fillvalue,
"String", val);
1042 stax = nc_put_att_text(ncid, varid, var_attr_name_fillvalue.c_str(), val.size(), val.c_str());
1047 if (stax != NC_NOERR) {
1048 string err = (string)
"File out netcdf, "
1049 +
"failed to write string attribute " + var_attr_name;
1060 string err = (string)
"File out netcdf, failed to write unknown type of attribute " + var_attr_name;
1066 if (stax != NC_NOERR) {
1067 string err = (string)
"File out netcdf, failed to write " + attr_type +
" attribute " + var_attr_name;
1087 const string &var_name,
1088 const string &global_attr_name,
1089 const string &var_attr_name,
1091 bool is_nc_enhanced) {
1093 D4AttributeType d4_attr_type = attr->type();
1094 int stax = NC_NOERR;
1095 unsigned int attri = 0;
1096 unsigned int num_vals = attr->num_values();
1097 switch (d4_attr_type) {
1098 case attr_container_c: {
1100 BESDEBUG(
"fonc",
"This is an attribute container. attr_name: \"" << global_attr_name <<
"\"" << endl);
1101 D4Attributes *c_attributes = attr->attributes();
1103 add_dap4_attributes(ncid, varid, c_attributes, var_name, global_attr_name, is_nc_enhanced);
1108 case attr_uint8_c: {
1110 vector<unsigned char> vals;
1111 vals.resize(num_vals);
1113 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1115 istringstream is(val);
1116 unsigned int uival = 0;
1118 vals[attri] = (
unsigned char) uival;
1121 stax = nc_put_att_uchar(ncid, varid, var_attr_name.c_str(), NC_UBYTE,
1122 num_vals, vals.data());
1123 if (stax != NC_NOERR) {
1124 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1125 +
"failed to write byte attribute " + var_attr_name;
1132 vector <int8_t> vals;
1133 vals.resize(num_vals);
1135 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1137 istringstream is(val);
1140 vals[attri] = (int8_t) uival;
1143 stax = nc_put_att_schar(ncid, varid, var_attr_name.c_str(), NC_BYTE,
1144 num_vals, vals.data());
1145 if (stax != NC_NOERR) {
1146 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1147 +
"failed to write signed 8-bit integer attribute " + var_attr_name;
1152 case attr_int16_c: {
1155 vals.resize(num_vals);
1157 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1159 istringstream is(val);
1166 stax = nc_put_att_short(ncid, varid, var_attr_name.c_str(), NC_SHORT,
1167 num_vals, vals.data());
1168 if (stax != NC_NOERR) {
1169 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1170 +
"failed to write short attribute " + var_attr_name;
1175 case attr_uint16_c: {
1178 vector<unsigned short> vals;
1179 vals.resize(num_vals);
1180 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1182 istringstream is(val);
1183 unsigned short ival = 0;
1189 stax = nc_put_att_ushort(ncid, varid, var_attr_name.c_str(), NC_USHORT, num_vals,
1191 if (stax != NC_NOERR) {
1192 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1193 +
"failed to write unsigned short attribute " + var_attr_name;
1198 case attr_int32_c: {
1200 vals.resize(num_vals);
1202 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1204 istringstream is(val);
1211 stax = nc_put_att_int(ncid, varid, var_attr_name.c_str(), NC_INT, num_vals,
1213 if (stax != NC_NOERR) {
1214 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1215 +
"failed to write int attribute " + var_attr_name;
1220 case attr_uint32_c: {
1222 vector<unsigned int> vals;
1223 vals.resize(num_vals);
1225 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1227 istringstream is(val);
1228 unsigned int sval = 0;
1234 stax = nc_put_att_uint(ncid, varid, var_attr_name.c_str(), NC_UINT, num_vals,
1236 if (stax != NC_NOERR) {
1237 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1238 +
"failed to write unsigned int attribute " + var_attr_name;
1243 case attr_int64_c: {
1244 vector<long long> vals;
1245 vals.resize(num_vals);
1247 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1249 istringstream is(val);
1256 stax = nc_put_att_longlong(ncid, varid, var_attr_name.c_str(), NC_INT64, num_vals,
1258 if (stax != NC_NOERR) {
1259 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1260 +
"failed to write 64-bit int attribute " + var_attr_name;
1265 case attr_uint64_c: {
1266 vector<unsigned long long> vals;
1267 vals.resize(num_vals);
1269 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1271 istringstream is(val);
1272 unsigned long long sval = 0;
1278 stax = nc_put_att_ulonglong(ncid, varid, var_attr_name.c_str(), NC_UINT64, num_vals,
1280 if (stax != NC_NOERR) {
1281 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1282 +
"failed to write unsigned 64-bit int attribute " + var_attr_name;
1288 case attr_float32_c: {
1290 vals.resize(num_vals);
1292 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1294 const char *cval = val.c_str();
1297 sval = strtod(cval, NULL);
1303 stax = nc_put_att_float(ncid, varid, var_attr_name.c_str(), NC_FLOAT,
1304 num_vals, vals.data());
1305 if (stax != NC_NOERR) {
1306 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1307 +
"failed to write float attribute " + var_attr_name;
1312 case attr_float64_c: {
1313 vector<double> vals;
1314 vals.resize(num_vals);
1316 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1318 const char *cval = val.c_str();
1321 sval = strtod(cval, NULL);
1326 stax = nc_put_att_double(ncid, varid, var_attr_name.c_str(), NC_DOUBLE,
1327 num_vals, vals.data());
1328 if (stax != NC_NOERR) {
1329 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1330 +
"failed to write double attribute " + var_attr_name;
1337 case attr_otherxml_c:
1340 if (attr->num_values() == 0)
1341 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 0,
"");
1344 D4Attribute::D4AttributeIter vi, ve;
1345 vi = attr->value_begin();
1346 ve = attr->value_end();
1352 for (; vi != ve; vi++) {
1356 if (var_attr_name != _FillValue) {
1357 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), val.size(), val.c_str());
1361 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
1362 << val.c_str()[0] << endl);
1363 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 1, val.c_str());
1364 if (stax == NC_NOERR) {
1366 string var_attr_name_fillvalue =
"Orig_FillValue";
1368 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
1369 << val.c_str() << endl);
1373 attrs.append_attr(var_attr_name_fillvalue,
"String", val);
1375 stax = nc_put_att_text(ncid, varid, var_attr_name_fillvalue.c_str(), val.size(), val.c_str());
1380 if (stax != NC_NOERR) {
1381 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1382 +
"failed to write string attribute " + var_attr_name;
1390 case attr_opaque_c: {
1391 string err = (string)
"File out netcdf, "
1392 +
"failed to write unknown type of attribute " + var_attr_name;
1403AttrType attrType = attrs.get_attr_type(attr);
1404unsigned int attri = 0;
1405unsigned int num_vals = attrs.get_attr_num(attr);
1407 case Attr_container: {
1409 BESDEBUG(
"fonc",
"This is an attribute container. attr_name: \"" << global_attr_name <<
"\"" << endl);
1410 AttrTable *container = attrs.get_attr_table(attr);
1412 add_attributes(ncid, varid, *container, var_name, global_attr_name, is_nc_enhanced);
1419 vector<unsigned char> vals;
1420 vals.resize(num_vals);
1421 for (attri = 0; attri < num_vals; attri++) {
1422 string val = attrs.get_attr(attr, attri);
1423 istringstream is(val);
1424 unsigned int uival = 0;
1426 vals[attri] = (
unsigned char) uival;
1428 stax = nc_put_att_uchar(ncid, varid, var_attr_name.c_str(), NC_UBYTE,
1429 num_vals, vals.data());
1430 if (stax != NC_NOERR) {
1431 string err = (string)
"File out netcdf, "
1432 +
"failed to write byte attribute " + var_attr_name;
1441 vals.resize(num_vals);
1442 for (attri = 0; attri < num_vals; attri++) {
1443 string val = attrs.get_attr(attr, attri);
1444 istringstream is(val);
1449 stax = nc_put_att_short(ncid, varid, var_attr_name.c_str(), NC_SHORT,
1450 num_vals, vals.data());
1451 if (stax != NC_NOERR) {
1452 string err = (string)
"File out netcdf, "
1453 +
"failed to write short attribute " + var_attr_name;
1462 vector<unsigned short> vals;
1463 vals.resize(num_vals);
1464 for (attri = 0; attri < num_vals; attri++) {
1465 string val = attrs.get_attr(attr, attri);
1466 istringstream is(val);
1467 unsigned short ival = 0;
1471 stax = nc_put_att_ushort(ncid, varid, var_attr_name.c_str(), NC_USHORT, num_vals,
1473 if (stax != NC_NOERR) {
1474 string err = (string)
"File out netcdf, "
1475 +
"failed to write unsinged short attribute " + var_attr_name;
1484 vals.resize(num_vals);
1485 for (attri = 0; attri < num_vals; attri++) {
1486 string val = attrs.get_attr(attr, attri);
1487 istringstream is(val);
1492 stax = nc_put_att_int(ncid, varid, var_attr_name.c_str(), NC_INT, num_vals,
1494 if (stax != NC_NOERR) {
1495 string err = (string)
"File out netcdf, "
1496 +
"failed to write int attribute " + var_attr_name;
1504 vector<unsigned int> vals;
1505 vals.resize(num_vals);
1506 for (attri = 0; attri < num_vals; attri++) {
1507 string val = attrs.get_attr(attr, attri);
1508 istringstream is(val);
1509 unsigned int lval = 0;
1513 stax = nc_put_att_uint(ncid, varid, var_attr_name.c_str(), NC_UINT, num_vals,
1515 if (stax != NC_NOERR) {
1516 string err = (string)
"File out netcdf, "
1517 +
"failed to write byte attribute " + var_attr_name;
1522 case Attr_float32: {
1526 vals.resize(num_vals);
1527 for (attri = 0; attri < num_vals; attri++) {
1528 string val = attrs.get_attr(attr, attri);
1529 const char *cval = val.c_str();
1532 fval = strtod(cval,NULL);
1536 stax = nc_put_att_float(ncid, varid, var_attr_name.c_str(), NC_FLOAT,
1537 num_vals, vals.data());
1538 if (stax != NC_NOERR) {
1539 string err = (string)
"File out netcdf, "
1540 +
"failed to write float attribute " + var_attr_name;
1545 case Attr_float64: {
1548 vector<double> vals;
1549 vals.resize(num_vals);
1550 for (attri = 0; attri < num_vals; attri++) {
1551 string val = attrs.get_attr(attr, attri);
1552 const char *cval = val.c_str();
1555 dval = strtod(cval,NULL);
1559 stax = nc_put_att_double(ncid, varid, var_attr_name.c_str(), NC_DOUBLE,
1560 num_vals, vals.data());
1561 if (stax != NC_NOERR) {
1562 string err = (string)
"File out netcdf, "
1563 +
"failed to write double attribute " + var_attr_name;
1570 case Attr_other_xml:
1573 string val = attrs.get_attr(attr, 0);
1574 for (attri = 1; attri < num_vals; attri++) {
1575 val +=
"\n" + attrs.get_attr(attr, attri);
1577 string attr_name = attrs.get_name(attr);
1578 if (attr_name != _FillValue) {
1579 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), val.size(), val.c_str());
1582 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
1583 << val.c_str()[0] << endl);
1584 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 1, val.c_str());
1585 if (stax == NC_NOERR) {
1587 string var_attr_name_fillvalue =
"Orig_FillValue";
1589 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
1590 << val.c_str() << endl);
1594 attrs.append_attr(var_attr_name_fillvalue,
"String", val);
1596 stax = nc_put_att_text(ncid, varid, var_attr_name_fillvalue.c_str(), val.size(), val.c_str());
1600 if (stax != NC_NOERR) {
1601 string err = (string)
"File out netcdf, "
1602 +
"failed to write string attribute " + var_attr_name;
1608 case Attr_unknown: {
1609 string err = (string)
"File out netcdf, "
1610 +
"failed to write unknown type of attribute " + var_attr_name;
static bool endsWith(std::string const &fullString, std::string const &ending)
static void add_dap4_attributes(int ncid, int varid, D4Attributes *d4_attrs, const string &var_name, const string &prepend_attr, bool is_netCDF_enhanced)
add_dap4_attributes
static void write_dap4_attrs_for_nc4_types(int ncid, int varid, const string &var_name, const string &global_attr_name, const string &var_attr_name, D4Attribute *attr, bool is_nc_enhanced)
writes out a single attribute that maps the dap4 datatype to netCDF-4
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 void add_attributes(int ncid, int varid, AttrTable &attrs, const string &var_name, const string &prepend_attr, bool is_netCDF_enhanced)
helper function for add_attributes
static void write_attrs_for_nc4_types(int ncid, int varid, const string &var_name, const string &global_attr_name, const string &var_attr_name, AttrTable attrs, AttrTable::Attr_iter &attr, bool is_nc_enhanced)
writes out a single attribute that maps the datatype to netCDF-4
static void add_variable_attributes(int ncid, int varid, BaseType *b, bool is_netCDF_enhanced, bool is_dap4)
Add the attributes for an OPeNDAP variable to the netcdf file.
static void handle_error(int stax, const string &err, const string &file, int line)
handle any netcdf errors
static string id2netcdf(string in)
convert the provided string to a netcdf allowed identifier.