34using std::istringstream;
39#include <BESInternalError.h>
43#include "DapFunctionUtils.h"
45#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);
147 const string &prepend_attr,
bool is_nc_enhanced) {
149 unsigned int num_attrs = attrs.get_size();
151 AttrTable::Attr_iter i = attrs.attr_begin();
152 AttrTable::Attr_iter e = attrs.attr_end();
153 for (; i != e; i++) {
154 unsigned int num_vals = attrs.get_attr_num(i);
156 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(); ii != ee; ++ii) {
182 string name = (*ii)->name();
184 unsigned int num_vals = (*ii)->num_values();
187 if (num_vals || varid == NC_GLOBAL)
188 add_dap4_attributes_worker(ncid, varid, var_name, *ii, prepend_attr, is_nc_enhanced);
205void FONcAttributes::add_attributes_worker(
int ncid,
int varid,
const string &var_name,
206 AttrTable &attrs, AttrTable::Attr_iter &attr,
207 const string &prepend_attr,
bool is_nc_enhanced) {
209 AttrType attrType = attrs.get_attr_type(attr);
211 string attr_name = attrs.get_name(attr);
212 string new_attr_name(
"");
213 if (!prepend_attr.empty()) {
214 new_attr_name = prepend_attr + FONC_EMBEDDED_SEPARATOR + attr_name;
220 if (varid == NC_GLOBAL && attrType == Attr_container &&
BESUtil::endsWith(attr_name,
"_GLOBAL")) {
222 "Suppressing global AttributeTable name '" << attr_name
223 <<
"' from inclusion in NetCDF attributes namespace chain."
227 new_attr_name = attr_name;
235 string new_name = new_attr_name;
236 if (!var_name.empty()) {
237 new_name = var_name + FONC_ATTRIBUTE_SEPARATOR + new_attr_name;
246 BESDEBUG(
"fonc",
"FONcAttributes name: " << new_name << endl);
247 BESDEBUG(
"fonc",
"FONcAttributes type: " << attrType << endl);
250 if (varid == NC_GLOBAL) {
251 BESDEBUG(
"fonc",
"FONcAttributes::add_attrbutes_worker() - Adding global attributes " << attr_name << endl);
253 BESDEBUG(
"fonc",
"FONcAttributes::add_attributes_worker() - Adding attribute " << new_name << endl);
257 if (is_nc_enhanced ==
true)
261 unsigned int attri = 0;
262 unsigned int num_vals = attrs.get_attr_num(attr);
264 case Attr_container: {
267 "Attribute " << attr_name <<
" is an attribute container. new_attr_name: \"" << new_attr_name
269 AttrTable *container = attrs.get_attr_table(attr);
271 add_attributes(ncid, varid, *container, var_name, new_attr_name, is_nc_enhanced);
280 vals.resize(num_vals);
281 for (attri = 0; attri < num_vals; attri++) {
282 string val = attrs.get_attr(attr, attri);
283 istringstream is(val);
284 unsigned int uival = 0;
286 vals[attri] = (short) uival;
288 stax = nc_put_att_short(ncid, varid, new_name.c_str(), NC_SHORT,
290 if (stax != NC_NOERR) {
291 string err = (string)
"File out netcdf, "
292 +
"failed to write byte attribute " + new_name;
300 vals.resize(num_vals);
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_short(ncid, varid, new_name.c_str(), NC_SHORT,
310 if (stax != NC_NOERR) {
311 string err = (string)
"File out netcdf, "
312 +
"failed to write short attribute " + new_name;
321 vals.resize(num_vals);
322 for (attri = 0; attri < num_vals; attri++) {
323 string val = attrs.get_attr(attr, attri);
324 istringstream is(val);
329 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals,
331 if (stax != NC_NOERR) {
332 string err = (string)
"File out netcdf, "
333 +
"failed to write unsinged short attribute " + new_name;
341 vals.resize(num_vals);
342 for (attri = 0; attri < num_vals; attri++) {
343 string val = attrs.get_attr(attr, attri);
344 istringstream is(val);
349 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals,
351 if (stax != NC_NOERR) {
352 string err = (string)
"File out netcdf, "
353 +
"failed to write int attribute " + new_name;
361 string err = (string)
"File out netcdf, "
362 +
"failed to write unsigned int attribute " + new_name;
363 err = err +
" for classic model because of potential overflow. ";
364 err = err +
" Please use the netCDF4 enhanced model. ";
370 vals.resize(num_vals);
371 for (attri = 0; attri < num_vals; attri++) {
372 string val = attrs.get_attr(attr, attri);
373 istringstream is(val);
378 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals,
380 if (stax != NC_NOERR) {
381 string err = (string)
"File out netcdf, "
382 +
"failed to write unsigned int attribute " + new_name;
391 vals.resize(num_vals);
392 for (attri = 0; attri < num_vals; attri++) {
393 string val = attrs.get_attr(attr, attri);
394 const char *cval = val.c_str();
397 fval = strtod(cval,NULL);
401 stax = nc_put_att_float(ncid, varid, new_name.c_str(), NC_FLOAT,
403 if (stax != NC_NOERR) {
404 string err = (string)
"File out netcdf, "
405 +
"failed to write float attribute " + new_name;
413 vals.resize(num_vals);
414 for (attri = 0; attri < num_vals; attri++) {
415 string val = attrs.get_attr(attr, attri);
416 const char *cval = val.c_str();
419 dval = strtod(cval,NULL);
423 stax = nc_put_att_double(ncid, varid, new_name.c_str(), NC_DOUBLE,
425 if (stax != NC_NOERR) {
426 string err = (string)
"File out netcdf, "
427 +
"failed to write double attribute " + new_name;
437 string val = attrs.get_attr(attr, 0);
438 for (attri = 1; attri < num_vals; attri++) {
439 val +=
"\n" + attrs.get_attr(attr, attri);
441 if (attr_name != _FillValue) {
442 stax = nc_put_att_text(ncid, varid, new_name.c_str(), val.length(), val.c_str());
445 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
446 << val.c_str()[0] << endl);
447 stax = nc_put_att_text(ncid, varid, new_name.c_str(), 1, val.c_str());
448 if (stax == NC_NOERR) {
450 string new_name_fillvalue =
"Orig_FillValue";
452 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
453 << val.c_str() << endl);
457 attrs.append_attr(new_name_fillvalue,
"String", val);
459 stax = nc_put_att_text(ncid, varid, new_name_fillvalue.c_str(), val.length(), val.c_str());
463 if (stax != NC_NOERR) {
464 string err = (string)
"File out netcdf, "
465 +
"failed to write string attribute " + new_name;
472 string err = (string)
"File out netcdf, "
473 +
"failed to write unknown type of attribute " + new_name;
493void FONcAttributes::add_dap4_attributes_worker(
int ncid,
int varid,
const string &var_name,
495 const string &prepend_attr,
bool is_nc_enhanced) {
496 D4AttributeType d4_attr_type = attr->type();
498 string d4_attr_name = attr->name();
499 BESDEBUG(
"dap",
"FONcAttributes:: D4 attribute name is "<<d4_attr_name <<endl);
500 string new_attr_name(
"");
501 if (!prepend_attr.empty()) {
502 new_attr_name = prepend_attr + FONC_EMBEDDED_SEPARATOR + d4_attr_name;
503 BESDEBUG(
"dap",
"FONcAttributes:: D4 new attribute name is "<<new_attr_name <<endl);
510 if (varid == NC_GLOBAL && d4_attr_type == attr_container_c && (
BESUtil::endsWith(d4_attr_name,
"_GLOBAL") ||
513 "Suppressing global AttributeTable name '" << d4_attr_name
514 <<
"' from inclusion in NetCDF attributes namespace chain."
518 new_attr_name = d4_attr_name;
527 string new_name = new_attr_name;
528 if (!var_name.empty()) {
529 new_name = var_name + FONC_ATTRIBUTE_SEPARATOR + new_attr_name;
540 if (varid == NC_GLOBAL) {
541 BESDEBUG(
"fonc",
"FONcAttributes::addattrs() - Adding global attributes " << d4_attr_name << endl);
543 BESDEBUG(
"fonc",
"FONcAttributes::addattrs() - Adding attribute " << new_name << endl);
548 if (is_nc_enhanced ==
true)
553 string attr_type =
"unknown";
554 unsigned int attri = 0;
556 unsigned int num_vals = attr->num_values();
557 switch (d4_attr_type) {
558 case attr_container_c: {
561 "Attribute " << d4_attr_name <<
" is an attribute container. new_attr_name: \""
564 D4Attributes *c_attributes = attr->attributes();
576 vals.resize(num_vals);
578 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
580 istringstream is(val);
583 vals[attri] = (int8_t) uival;
586 stax = nc_put_att_schar(ncid, varid, new_name.c_str(), NC_BYTE,
588 if (stax != NC_NOERR) {
589 string err = (string)
"File out netcdf-4 classic for DAP4, "
590 +
"failed to write signed 8-bit integer attribute " + new_name;
599 vals.resize(num_vals);
601 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
603 istringstream is(val);
606 unsigned int uival = 0;
608 vals[attri] = (short) uival;
611 stax = nc_put_att_short(ncid, varid, new_name.c_str(), NC_SHORT, num_vals, &vals[0]);
619 vals.resize(num_vals);
621 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
623 istringstream is(val);
630 stax = nc_put_att_short(ncid, varid, new_name.c_str(), NC_SHORT, num_vals, &vals[0]);
635 case attr_uint16_c: {
641 vals.resize(num_vals);
642 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
644 istringstream is(val);
651 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, &vals[0]);
659 vals.resize(num_vals);
661 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
663 istringstream is(val);
670 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, &vals[0]);
675 case attr_uint32_c: {
679 vals.resize(num_vals);
681 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
683 istringstream is(val);
690 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, &vals[0]);
695 case attr_float32_c: {
698 vals.resize(num_vals);
700 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
702 const char *cval = val.c_str();
705 sval = strtod(cval,NULL);
711 stax = nc_put_att_float(ncid, varid, new_name.c_str(), NC_FLOAT, num_vals, &vals[0]);
716 case attr_float64_c: {
719 vals.resize(num_vals);
721 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
723 const char *cval = val.c_str();
726 sval = strtod(cval,NULL);
731 stax = nc_put_att_double(ncid, varid, new_name.c_str(), NC_DOUBLE, num_vals, &vals[0]);
738 case attr_otherxml_c: {
739 attr_type =
"string";
740 D4Attribute::D4AttributeIter vi, ve;
741 vi = attr->value_begin();
742 ve = attr->value_end();
747 for (; vi != ve; vi++) {
751 if (d4_attr_name != _FillValue) {
752 stax = nc_put_att_text(ncid, varid, new_name.c_str(), val.length(), val.c_str());
756 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
757 << val.c_str()[0] << endl);
758 stax = nc_put_att_text(ncid, varid, new_name.c_str(), 1, val.c_str());
759 if (stax == NC_NOERR) {
761 string new_name_fillvalue =
"Orig_FillValue";
763 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
764 << val.c_str() << endl);
768 attrs.append_attr(new_name_fillvalue,
"String", val);
770 stax = nc_put_att_text(ncid, varid, new_name_fillvalue.c_str(), val.length(), val.c_str());
784 string err = (string)
"File out netcdf, failed to write unknown/unsupported type of attribute " + new_name;
790 if (stax != NC_NOERR) {
791 string err = (string)
"File out netcdf, failed to write " + attr_type +
" attribute " + new_name;
812 const string &var_name,
const string &orig) {
813 if (var_name != orig) {
814 string attr_name = FONC_ORIGINAL_NAME;
815 int stax = nc_put_att_text(ncid, varid, attr_name.c_str(),
816 orig.length(), orig.c_str());
817 if (stax != NC_NOERR) {
818 string err = (string)
"File out netcdf, "
819 +
"failed to write change of name attribute for "
840 const string &var_attr_name, AttrTable attrs, AttrTable::Attr_iter &attr,
841 bool is_nc_enhanced) {
844 string attr_type =
"unknown";
845 AttrType attrType = attrs.get_attr_type(attr);
846 BESDEBUG(
"fonc",
"FONcAttributes write_attrs_for_nc4_type name: " << var_attr_name << endl);
847 BESDEBUG(
"fonc",
"FONcAttributes write_attrs_for_nc4_type type: " << attrType << endl);
848 unsigned int attri = 0;
849 unsigned int num_vals = attrs.get_attr_num(attr);
851 case Attr_container: {
853 BESDEBUG(
"fonc",
"This is an attribute container. attr_name: \"" << global_attr_name <<
"\"" << endl);
854 AttrTable *container = attrs.get_attr_table(attr);
856 add_attributes(ncid, varid, *container, var_name, global_attr_name, is_nc_enhanced);
863 vector<unsigned char> vals;
864 vals.resize(num_vals);
865 for (attri = 0; attri < num_vals; attri++) {
866 string val = attrs.get_attr(attr, attri);
867 istringstream is(val);
868 unsigned int uival = 0;
870 vals[attri] = (
unsigned char) uival;
872 stax = nc_put_att_uchar(ncid, varid, var_attr_name.c_str(), NC_UBYTE,
875 if (stax != NC_NOERR) {
876 string err = (string)
"File out netcdf, "
877 +
"failed to write byte attribute " + var_attr_name;
887 vals.resize(num_vals);
888 for (attri = 0; attri < num_vals; attri++) {
889 string val = attrs.get_attr(attr, attri);
890 istringstream is(val);
895 stax = nc_put_att_short(ncid, varid, var_attr_name.c_str(), NC_SHORT, num_vals, &vals[0]);
897 if (stax != NC_NOERR) {
898 string err = (string)
"File out netcdf, "
899 +
"failed to write short attribute " + var_attr_name;
911 vector<unsigned short> vals;
912 vals.resize(num_vals);
913 for (attri = 0; attri < num_vals; attri++) {
914 string val = attrs.get_attr(attr, attri);
915 istringstream is(val);
916 unsigned short ival = 0;
920 stax = nc_put_att_ushort(ncid, varid, var_attr_name.c_str(), NC_USHORT, num_vals,
923 if (stax != NC_NOERR) {
924 string err = (string)
"File out netcdf, "
925 +
"failed to write unsinged short attribute " + var_attr_name;
937 vals.resize(num_vals);
938 for (attri = 0; attri < num_vals; attri++) {
939 string val = attrs.get_attr(attr, attri);
940 istringstream is(val);
945 stax = nc_put_att_int(ncid, varid, var_attr_name.c_str(), NC_INT, num_vals, &vals[0]);
948 if (stax != NC_NOERR) {
949 string err = (string)
"File out netcdf, "
950 +
"failed to write int attribute " + var_attr_name;
961 vector<unsigned int> vals;
962 vals.resize(num_vals);
963 for (attri = 0; attri < num_vals; attri++) {
964 string val = attrs.get_attr(attr, attri);
965 istringstream is(val);
966 unsigned int lval = 0;
970 stax = nc_put_att_uint(ncid, varid, var_attr_name.c_str(), NC_UINT, num_vals,
973 if (stax != NC_NOERR) {
974 string err = (string)
"File out netcdf, "
975 +
"failed to write byte attribute " + var_attr_name;
987 vals.resize(num_vals);
988 for (attri = 0; attri < num_vals; attri++) {
989 string val = attrs.get_attr(attr, attri);
990 const char *cval = val.c_str();
993 fval = strtod(cval,NULL);
997 stax = nc_put_att_float(ncid, varid, var_attr_name.c_str(), NC_FLOAT, num_vals, &vals[0]);
1000 if (stax != NC_NOERR) {
1001 string err = (string)
"File out netcdf, "
1002 +
"failed to write float attribute " + var_attr_name;
1010 case Attr_float64: {
1013 vector<double> vals;
1014 vals.resize(num_vals);
1015 for (attri = 0; attri < num_vals; attri++) {
1016 string val = attrs.get_attr(attr, attri);
1017 const char *cval = val.c_str();
1020 dval = strtod(cval,NULL);
1024 stax = nc_put_att_double(ncid, varid, var_attr_name.c_str(), NC_DOUBLE, num_vals, &vals[0]);
1027 if (stax != NC_NOERR) {
1028 string err = (string)
"File out netcdf, "
1029 +
"failed to write double attribute " + var_attr_name;
1039 case Attr_other_xml: {
1041 string val = attrs.get_attr(attr, 0);
1042 for (attri = 1; attri < num_vals; attri++) {
1043 val +=
"\n" + attrs.get_attr(attr, attri);
1045 string attr_name = attrs.get_name(attr);
1046 if (attr_name != _FillValue) {
1047 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), val.length(), val.c_str());
1051 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
1052 << val.c_str()[0] << endl);
1053 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 1, val.c_str());
1054 if (stax == NC_NOERR) {
1056 string var_attr_name_fillvalue =
"Orig_FillValue";
1058 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
1059 << val.c_str() << endl);
1064 attrs.append_attr(var_attr_name_fillvalue,
"String", val);
1066 stax = nc_put_att_text(ncid, varid, var_attr_name_fillvalue.c_str(), val.length(), val.c_str());
1071 if (stax != NC_NOERR) {
1072 string err = (string)
"File out netcdf, "
1073 +
"failed to write string attribute " + var_attr_name;
1084 string err = (string)
"File out netcdf, failed to write unknown type of attribute " + var_attr_name;
1090 if (stax != NC_NOERR) {
1091 string err = (string)
"File out netcdf, failed to write " + attr_type +
" attribute " + var_attr_name;
1111 const string &var_name,
1112 const string &global_attr_name,
1113 const string &var_attr_name,
1115 bool is_nc_enhanced) {
1117 D4AttributeType d4_attr_type = attr->type();
1118 int stax = NC_NOERR;
1119 unsigned int attri = 0;
1120 unsigned int num_vals = attr->num_values();
1121 switch (d4_attr_type) {
1122 case attr_container_c: {
1124 BESDEBUG(
"fonc",
"This is an attribute container. attr_name: \"" << global_attr_name <<
"\"" << endl);
1125 D4Attributes *c_attributes = attr->attributes();
1127 add_dap4_attributes(ncid, varid, c_attributes, var_name, global_attr_name, is_nc_enhanced);
1134 vector<unsigned char> vals;
1135 vals.resize(num_vals);
1137 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1139 istringstream is(val);
1140 unsigned int uival = 0;
1142 vals[attri] = (
unsigned char) uival;
1145 stax = nc_put_att_uchar(ncid, varid, var_attr_name.c_str(), NC_UBYTE,
1146 num_vals, &vals[0]);
1147 if (stax != NC_NOERR) {
1148 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1149 +
"failed to write byte attribute " + var_attr_name;
1156 vector<int8_t> vals;
1157 vals.resize(num_vals);
1159 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1161 istringstream is(val);
1164 vals[attri] = (int8_t) uival;
1167 stax = nc_put_att_schar(ncid, varid, var_attr_name.c_str(), NC_BYTE,
1168 num_vals, &vals[0]);
1169 if (stax != NC_NOERR) {
1170 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1171 +
"failed to write signed 8-bit integer attribute " + var_attr_name;
1176 case attr_int16_c: {
1179 vals.resize(num_vals);
1181 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1183 istringstream is(val);
1190 stax = nc_put_att_short(ncid, varid, var_attr_name.c_str(), NC_SHORT,
1191 num_vals, &vals[0]);
1192 if (stax != NC_NOERR) {
1193 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1194 +
"failed to write short attribute " + var_attr_name;
1199 case attr_uint16_c: {
1202 vector<unsigned short>vals;
1203 vals.resize(num_vals);
1204 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1206 istringstream is(val);
1207 unsigned short ival = 0;
1213 stax = nc_put_att_ushort(ncid, varid, var_attr_name.c_str(), NC_USHORT, num_vals,
1215 if (stax != NC_NOERR) {
1216 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1217 +
"failed to write unsigned short attribute " + var_attr_name;
1222 case attr_int32_c: {
1224 vals.resize(num_vals);
1226 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1228 istringstream is(val);
1235 stax = nc_put_att_int(ncid, varid, var_attr_name.c_str(), NC_INT, num_vals,
1237 if (stax != NC_NOERR) {
1238 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1239 +
"failed to write int attribute " + var_attr_name;
1244 case attr_uint32_c: {
1246 vector<unsigned int> vals;
1247 vals.resize(num_vals);
1249 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1251 istringstream is(val);
1252 unsigned int sval = 0;
1258 stax = nc_put_att_uint(ncid, varid, var_attr_name.c_str(), NC_UINT, num_vals,
1260 if (stax != NC_NOERR) {
1261 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1262 +
"failed to write unsigned int attribute " + var_attr_name;
1267 case attr_int64_c: {
1268 vector<long long> vals;
1269 vals.resize(num_vals);
1271 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1273 istringstream is(val);
1280 stax = nc_put_att_longlong(ncid, varid, var_attr_name.c_str(), NC_INT64, num_vals,
1282 if (stax != NC_NOERR) {
1283 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1284 +
"failed to write 64-bit int attribute " + var_attr_name;
1289 case attr_uint64_c: {
1290 vector<unsigned long long> vals;
1291 vals.resize(num_vals);
1293 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1295 istringstream is(val);
1296 unsigned long long sval = 0;
1302 stax = nc_put_att_ulonglong(ncid, varid, var_attr_name.c_str(), NC_UINT64, num_vals,
1304 if (stax != NC_NOERR) {
1305 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1306 +
"failed to write unsigned 64-bit int attribute " + var_attr_name;
1312 case attr_float32_c: {
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);
1327 stax = nc_put_att_float(ncid, varid, var_attr_name.c_str(), NC_FLOAT,
1328 num_vals, &vals[0]);
1329 if (stax != NC_NOERR) {
1330 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1331 +
"failed to write float attribute " + var_attr_name;
1336 case attr_float64_c: {
1337 vector<double> vals;
1338 vals.resize(num_vals);
1340 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1342 const char *cval = val.c_str();
1345 sval = strtod(cval,NULL);
1350 stax = nc_put_att_double(ncid, varid, var_attr_name.c_str(), NC_DOUBLE,
1351 num_vals, &vals[0]);
1352 if (stax != NC_NOERR) {
1353 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1354 +
"failed to write double attribute " + var_attr_name;
1361 case attr_otherxml_c:
1364 if(attr->num_values()==0)
1365 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 0,
"");
1368 D4Attribute::D4AttributeIter vi,ve;
1369 vi = attr->value_begin();
1370 ve = attr->value_end();
1376 for (; vi != ve; vi++) {
1380 if (var_attr_name != _FillValue) {
1381 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), val.length(), val.c_str());
1384 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
1385 << val.c_str()[0] << endl);
1386 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 1, val.c_str());
1387 if (stax == NC_NOERR) {
1389 string var_attr_name_fillvalue =
"Orig_FillValue";
1391 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
1392 << val.c_str() << endl);
1396 attrs.append_attr(var_attr_name_fillvalue,
"String", val);
1398 stax = nc_put_att_text(ncid, varid, var_attr_name_fillvalue.c_str(), val.length(), val.c_str());
1403 if (stax != NC_NOERR) {
1404 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1405 +
"failed to write string attribute " + var_attr_name;
1415 string err = (string)
"File out netcdf, "
1416 +
"failed to write unknown type of attribute " + var_attr_name;
1426 int stax = NC_NOERR;
1427 AttrType attrType = attrs.get_attr_type(attr);
1428 unsigned int attri = 0;
1429 unsigned int num_vals = attrs.get_attr_num(attr);
1431 case Attr_container: {
1433 BESDEBUG(
"fonc",
"This is an attribute container. attr_name: \"" << global_attr_name <<
"\"" << endl);
1434 AttrTable *container = attrs.get_attr_table(attr);
1436 add_attributes(ncid, varid, *container, var_name, global_attr_name, is_nc_enhanced);
1443 vector<unsigned char> vals;
1444 vals.resize(num_vals);
1445 for (attri = 0; attri < num_vals; attri++) {
1446 string val = attrs.get_attr(attr, attri);
1447 istringstream is(val);
1448 unsigned int uival = 0;
1450 vals[attri] = (
unsigned char) uival;
1452 stax = nc_put_att_uchar(ncid, varid, var_attr_name.c_str(), NC_UBYTE,
1453 num_vals, &vals[0]);
1454 if (stax != NC_NOERR) {
1455 string err = (string)
"File out netcdf, "
1456 +
"failed to write byte attribute " + var_attr_name;
1465 vals.resize(num_vals);
1466 for (attri = 0; attri < num_vals; attri++) {
1467 string val = attrs.get_attr(attr, attri);
1468 istringstream is(val);
1473 stax = nc_put_att_short(ncid, varid, var_attr_name.c_str(), NC_SHORT,
1474 num_vals, &vals[0]);
1475 if (stax != NC_NOERR) {
1476 string err = (string)
"File out netcdf, "
1477 +
"failed to write short attribute " + var_attr_name;
1486 vector<unsigned short> vals;
1487 vals.resize(num_vals);
1488 for (attri = 0; attri < num_vals; attri++) {
1489 string val = attrs.get_attr(attr, attri);
1490 istringstream is(val);
1491 unsigned short ival = 0;
1495 stax = nc_put_att_ushort(ncid, varid, var_attr_name.c_str(), NC_USHORT, num_vals,
1497 if (stax != NC_NOERR) {
1498 string err = (string)
"File out netcdf, "
1499 +
"failed to write unsinged short attribute " + var_attr_name;
1508 vals.resize(num_vals);
1509 for (attri = 0; attri < num_vals; attri++) {
1510 string val = attrs.get_attr(attr, attri);
1511 istringstream is(val);
1516 stax = nc_put_att_int(ncid, varid, var_attr_name.c_str(), NC_INT, num_vals,
1518 if (stax != NC_NOERR) {
1519 string err = (string)
"File out netcdf, "
1520 +
"failed to write int attribute " + var_attr_name;
1528 vector<unsigned int> vals;
1529 vals.resize(num_vals);
1530 for (attri = 0; attri < num_vals; attri++) {
1531 string val = attrs.get_attr(attr, attri);
1532 istringstream is(val);
1533 unsigned int lval = 0;
1537 stax = nc_put_att_uint(ncid, varid, var_attr_name.c_str(), NC_UINT, num_vals,
1539 if (stax != NC_NOERR) {
1540 string err = (string)
"File out netcdf, "
1541 +
"failed to write byte attribute " + var_attr_name;
1546 case Attr_float32: {
1550 vals.resize(num_vals);
1551 for (attri = 0; attri < num_vals; attri++) {
1552 string val = attrs.get_attr(attr, attri);
1553 const char *cval = val.c_str();
1556 fval = strtod(cval,NULL);
1560 stax = nc_put_att_float(ncid, varid, var_attr_name.c_str(), NC_FLOAT,
1561 num_vals, &vals[0]);
1562 if (stax != NC_NOERR) {
1563 string err = (string)
"File out netcdf, "
1564 +
"failed to write float attribute " + var_attr_name;
1569 case Attr_float64: {
1572 vector<double> vals;
1573 vals.resize(num_vals);
1574 for (attri = 0; attri < num_vals; attri++) {
1575 string val = attrs.get_attr(attr, attri);
1576 const char *cval = val.c_str();
1579 dval = strtod(cval,NULL);
1583 stax = nc_put_att_double(ncid, varid, var_attr_name.c_str(), NC_DOUBLE,
1584 num_vals, &vals[0]);
1585 if (stax != NC_NOERR) {
1586 string err = (string)
"File out netcdf, "
1587 +
"failed to write double attribute " + var_attr_name;
1594 case Attr_other_xml:
1597 string val = attrs.get_attr(attr, 0);
1598 for (attri = 1; attri < num_vals; attri++) {
1599 val +=
"\n" + attrs.get_attr(attr, attri);
1601 string attr_name = attrs.get_name(attr);
1602 if (attr_name != _FillValue) {
1603 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), val.length(), val.c_str());
1606 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
1607 << val.c_str()[0] << endl);
1608 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 1, val.c_str());
1609 if (stax == NC_NOERR) {
1611 string var_attr_name_fillvalue =
"Orig_FillValue";
1613 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
1614 << val.c_str() << endl);
1618 attrs.append_attr(var_attr_name_fillvalue,
"String", val);
1620 stax = nc_put_att_text(ncid, varid, var_attr_name_fillvalue.c_str(), val.length(), val.c_str());
1624 if (stax != NC_NOERR) {
1625 string err = (string)
"File out netcdf, "
1626 +
"failed to write string attribute " + var_attr_name;
1632 case Attr_unknown: {
1633 string err = (string)
"File out netcdf, "
1634 +
"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.