34 using std::istringstream;
39 #include <BESInternalError.h>
43 #include "DapFunctionUtils.h"
45 #include "FONcAttributes.h"
46 #include "FONcUtils.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);
113 void 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) {
181 unsigned int num_attrs = attrs.get_size();
183 AttrTable::Attr_iter i = attrs.attr_begin();
184 AttrTable::Attr_iter e = attrs.attr_end();
185 for (; i != e; i++) {
186 unsigned int num_vals = attrs.get_attr_num(i);
188 add_dap4_attributes_worker(ncid, varid, var_name, attrs, i, prepend_attr, is_nc_enhanced);
194 BESDEBUG(
"dap",
"FONcAttributes::add_dap4_attributes() number of attributes "<< d4_attrs <<endl);
195 for (D4Attributes::D4AttributesIter ii = d4_attrs->attribute_begin(), ee = d4_attrs->attribute_end(); ii != ee; ++ii) {
196 string name = (*ii)->name();
198 unsigned int num_vals = (*ii)->num_values();
201 if (num_vals || varid == NC_GLOBAL)
202 add_dap4_attributes_worker(ncid, varid, var_name, *ii, prepend_attr, is_nc_enhanced);
219 void FONcAttributes::add_attributes_worker(
int ncid,
int varid,
const string &var_name,
220 AttrTable &attrs, AttrTable::Attr_iter &attr,
221 const string &prepend_attr,
bool is_nc_enhanced) {
223 AttrType attrType = attrs.get_attr_type(attr);
225 string attr_name = attrs.get_name(attr);
226 string new_attr_name(
"");
227 if (!prepend_attr.empty()) {
228 new_attr_name = prepend_attr + FONC_EMBEDDED_SEPARATOR + attr_name;
234 if (varid == NC_GLOBAL && attrType == Attr_container &&
BESUtil::endsWith(attr_name,
"_GLOBAL")) {
236 "Suppressing global AttributeTable name '" << attr_name
237 <<
"' from inclusion in NetCDF attributes namespace chain."
241 new_attr_name = attr_name;
248 string new_name = new_attr_name;
249 if (!var_name.empty()) {
250 new_name = var_name + FONC_ATTRIBUTE_SEPARATOR + new_attr_name;
261 if (varid == NC_GLOBAL) {
262 BESDEBUG(
"fonc",
"FONcAttributes::addattrs() - Adding global attributes " << attr_name << endl);
264 BESDEBUG(
"fonc",
"FONcAttributes::addattrs() - Adding attribute " << new_name << endl);
268 if (is_nc_enhanced ==
true)
272 unsigned int attri = 0;
273 unsigned int num_vals = attrs.get_attr_num(attr);
275 case Attr_container: {
278 "Attribute " << attr_name <<
" is an attribute container. new_attr_name: \"" << new_attr_name
280 AttrTable *container = attrs.get_attr_table(attr);
282 add_attributes(ncid, varid, *container, var_name, new_attr_name, is_nc_enhanced);
288 vector<unsigned char>vals;
289 vals.resize(num_vals);
290 for (attri = 0; attri < num_vals; attri++) {
291 string val = attrs.get_attr(attr, attri);
292 istringstream is(val);
293 unsigned int uival = 0;
295 vals[attri] = (
unsigned char) uival;
297 stax = nc_put_att_uchar(ncid, varid, new_name.c_str(), NC_BYTE,
299 if (stax != NC_NOERR) {
300 string err = (string)
"File out netcdf, "
301 +
"failed to write byte attribute " + new_name;
309 vals.resize(num_vals);
310 for (attri = 0; attri < num_vals; attri++) {
311 string val = attrs.get_attr(attr, attri);
312 istringstream is(val);
317 stax = nc_put_att_short(ncid, varid, new_name.c_str(), NC_SHORT,
319 if (stax != NC_NOERR) {
320 string err = (string)
"File out netcdf, "
321 +
"failed to write short attribute " + new_name;
330 vals.resize(num_vals);
331 for (attri = 0; attri < num_vals; attri++) {
332 string val = attrs.get_attr(attr, attri);
333 istringstream is(val);
338 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals,
340 if (stax != NC_NOERR) {
341 string err = (string)
"File out netcdf, "
342 +
"failed to write unsinged short attribute " + new_name;
350 vals.resize(num_vals);
351 for (attri = 0; attri < num_vals; attri++) {
352 string val = attrs.get_attr(attr, attri);
353 istringstream is(val);
358 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals,
360 if (stax != NC_NOERR) {
361 string err = (string)
"File out netcdf, "
362 +
"failed to write int attribute " + new_name;
371 vals.resize(num_vals);
372 for (attri = 0; attri < num_vals; attri++) {
373 string val = attrs.get_attr(attr, attri);
374 istringstream is(val);
379 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals,
381 if (stax != NC_NOERR) {
382 string err = (string)
"File out netcdf, "
383 +
"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;
493 void 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;
525 string new_name = new_attr_name;
526 if (!var_name.empty()) {
527 new_name = var_name + FONC_ATTRIBUTE_SEPARATOR + new_attr_name;
538 if (varid == NC_GLOBAL) {
539 BESDEBUG(
"fonc",
"FONcAttributes::addattrs() - Adding global attributes " << d4_attr_name << endl);
541 BESDEBUG(
"fonc",
"FONcAttributes::addattrs() - Adding attribute " << new_name << endl);
546 if (is_nc_enhanced ==
true)
551 string attr_type =
"unknown";
552 unsigned int attri = 0;
554 unsigned int num_vals = attr->num_values();
555 switch (d4_attr_type) {
556 case attr_container_c: {
559 "Attribute " << d4_attr_name <<
" is an attribute container. new_attr_name: \""
562 D4Attributes *c_attributes = attr->attributes();
574 unsigned char vals[num_vals];
576 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
578 istringstream is(val);
579 unsigned int uival = 0;
581 vals[attri] = (
unsigned char) uival;
585 for (attri = 0; attri < num_vals; attri++) {
586 string val = attrs.get_attr(attr, attri);
587 istringstream is(val);
588 unsigned int uival = 0;
590 vals[attri] = (
unsigned char) uival;
593 stax = nc_put_att_uchar(ncid, varid, new_name.c_str(), NC_UBYTE, num_vals, vals);
601 short vals[num_vals];
603 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
605 istringstream is(val);
613 for (attri = 0; attri < num_vals; attri++) {
614 string val = attrs.get_attr(attr, attri);
615 istringstream is(val);
621 stax = nc_put_att_short(ncid, varid, new_name.c_str(), NC_SHORT, num_vals, vals);
626 case attr_uint16_c: {
629 attr_type =
"unsigned short";
632 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
634 istringstream is(val);
642 for (attri = 0; attri < num_vals; attri++) {
643 string val = attrs.get_attr(attr, attri);
644 istringstream is(val);
650 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, vals);
660 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
662 istringstream is(val);
670 for (attri = 0; attri < num_vals; attri++) {
671 string val = attrs.get_attr(attr, attri);
672 istringstream is(val);
678 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, vals);
683 case attr_uint32_c: {
686 attr_type =
"unsigned int";
689 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
691 istringstream is(val);
699 for (attri = 0; attri < num_vals; attri++) {
700 string val = attrs.get_attr(attr, attri);
701 istringstream is(val);
707 stax = nc_put_att_int(ncid, varid, new_name.c_str(), NC_INT, num_vals, vals);
712 case attr_float32_c: {
715 float vals[num_vals];
717 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
719 const char *cval = val.c_str();
722 sval = strtod(cval,NULL);
729 for (attri = 0; attri < num_vals; attri++) {
730 string val = attrs.get_attr(attr, attri);
731 istringstream is(val);
737 stax = nc_put_att_float(ncid, varid, new_name.c_str(), NC_FLOAT, num_vals, vals);
742 case attr_float64_c: {
744 attr_type =
"float64";
745 double vals[num_vals];
747 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
749 const char *cval = val.c_str();
752 sval = strtod(cval,NULL);
758 for (attri = 0; attri < num_vals; attri++) {
759 string val = attrs.get_attr(attr, attri);
760 istringstream is(val);
766 stax = nc_put_att_double(ncid, varid, new_name.c_str(), NC_DOUBLE, num_vals, vals);
773 case attr_otherxml_c: {
774 attr_type =
"string";
775 D4Attribute::D4AttributeIter vi, ve;
776 vi = attr->value_begin();
777 ve = attr->value_end();
782 for (; vi != ve; vi++) {
787 for (D4Attribute::D4AttributeIter vi = (*i)->value_begin(), ve = (*i)->value_end(); vi != ve; vi++) {
789 istringstream is(val);
790 unsigned int uival = 0;
792 vals[attri] = (
unsigned char) uival;
798 string val = attrs.get_attr(attr, 0);
799 for (attri = 1; attri < num_vals; attri++) {
800 val +=
"\n" + attrs.get_attr(attr, attri);
804 if (d4_attr_name != _FillValue) {
805 stax = nc_put_att_text(ncid, varid, new_name.c_str(), val.length(), val.c_str());
809 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
810 << val.c_str()[0] << endl);
811 stax = nc_put_att_text(ncid, varid, new_name.c_str(), 1, val.c_str());
812 if (stax == NC_NOERR) {
814 string new_name_fillvalue =
"Orig_FillValue";
816 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
817 << val.c_str() << endl);
821 attrs.append_attr(new_name_fillvalue,
"String", val);
823 stax = nc_put_att_text(ncid, varid, new_name_fillvalue.c_str(), val.length(), val.c_str());
838 string err = (string)
"File out netcdf, failed to write unknown/unsupported type of attribute " + new_name;
844 if (stax != NC_NOERR) {
845 string err = (string)
"File out netcdf, failed to write " + attr_type +
" attribute " + new_name;
866 const string &var_name,
const string &orig) {
867 if (var_name != orig) {
868 string attr_name = FONC_ORIGINAL_NAME;
869 int stax = nc_put_att_text(ncid, varid, attr_name.c_str(),
870 orig.length(), orig.c_str());
871 if (stax != NC_NOERR) {
872 string err = (string)
"File out netcdf, "
873 +
"failed to write change of name attribute for "
894 const string &var_attr_name, AttrTable attrs, AttrTable::Attr_iter &attr,
895 bool is_nc_enhanced) {
898 string attr_type =
"unknown";
899 AttrType attrType = attrs.get_attr_type(attr);
900 unsigned int attri = 0;
901 unsigned int num_vals = attrs.get_attr_num(attr);
903 case Attr_container: {
905 BESDEBUG(
"fonc",
"This is an attribute container. attr_name: \"" << global_attr_name <<
"\"" << endl);
906 AttrTable *container = attrs.get_attr_table(attr);
908 add_attributes(ncid, varid, *container, var_name, global_attr_name, is_nc_enhanced);
916 vector<unsigned char> vals;
917 vals.resize(num_vals);
918 for (attri = 0; attri < num_vals; attri++) {
919 string val = attrs.get_attr(attr, attri);
920 istringstream is(val);
921 unsigned int uival = 0;
923 vals[attri] = (
unsigned char) uival;
925 stax = nc_put_att_uchar(ncid, varid, var_attr_name.c_str(), NC_UBYTE,
928 if (stax != NC_NOERR) {
929 string err = (string)
"File out netcdf, "
930 +
"failed to write byte attribute " + var_attr_name;
941 vals.resize(num_vals);
942 for (attri = 0; attri < num_vals; attri++) {
943 string val = attrs.get_attr(attr, attri);
944 istringstream is(val);
949 stax = nc_put_att_short(ncid, varid, var_attr_name.c_str(), NC_SHORT, num_vals, &vals[0]);
951 if (stax != NC_NOERR) {
952 string err = (string)
"File out netcdf, "
953 +
"failed to write short attribute " + var_attr_name;
965 vector<unsigned short> vals;
966 vals.resize(num_vals);
967 for (attri = 0; attri < num_vals; attri++) {
968 string val = attrs.get_attr(attr, attri);
969 istringstream is(val);
970 unsigned short ival = 0;
974 stax = nc_put_att_ushort(ncid, varid, var_attr_name.c_str(), NC_USHORT, num_vals,
977 if (stax != NC_NOERR) {
978 string err = (string)
"File out netcdf, "
979 +
"failed to write unsinged short 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 istringstream is(val);
999 stax = nc_put_att_int(ncid, varid, var_attr_name.c_str(), NC_INT, num_vals, &vals[0]);
1002 if (stax != NC_NOERR) {
1003 string err = (string)
"File out netcdf, "
1004 +
"failed to write int attribute " + var_attr_name;
1015 vector<unsigned int> vals;
1016 vals.resize(num_vals);
1017 for (attri = 0; attri < num_vals; attri++) {
1018 string val = attrs.get_attr(attr, attri);
1019 istringstream is(val);
1020 unsigned int lval = 0;
1024 stax = nc_put_att_uint(ncid, varid, var_attr_name.c_str(), NC_UINT, num_vals,
1027 if (stax != NC_NOERR) {
1028 string err = (string)
"File out netcdf, "
1029 +
"failed to write byte attribute " + var_attr_name;
1037 case Attr_float32: {
1041 vals.resize(num_vals);
1042 for (attri = 0; attri < num_vals; attri++) {
1043 string val = attrs.get_attr(attr, attri);
1044 const char *cval = val.c_str();
1047 fval = strtod(cval,NULL);
1051 stax = nc_put_att_float(ncid, varid, var_attr_name.c_str(), NC_FLOAT, num_vals, &vals[0]);
1054 if (stax != NC_NOERR) {
1055 string err = (string)
"File out netcdf, "
1056 +
"failed to write float attribute " + var_attr_name;
1064 case Attr_float64: {
1067 vector<double> vals;
1068 vals.resize(num_vals);
1069 for (attri = 0; attri < num_vals; attri++) {
1070 string val = attrs.get_attr(attr, attri);
1071 const char *cval = val.c_str();
1074 dval = strtod(cval,NULL);
1078 stax = nc_put_att_double(ncid, varid, var_attr_name.c_str(), NC_DOUBLE, num_vals, &vals[0]);
1081 if (stax != NC_NOERR) {
1082 string err = (string)
"File out netcdf, "
1083 +
"failed to write double attribute " + var_attr_name;
1093 case Attr_other_xml: {
1095 string val = attrs.get_attr(attr, 0);
1096 for (attri = 1; attri < num_vals; attri++) {
1097 val +=
"\n" + attrs.get_attr(attr, attri);
1099 string attr_name = attrs.get_name(attr);
1100 if (attr_name != _FillValue) {
1101 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), val.length(), val.c_str());
1105 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
1106 << val.c_str()[0] << endl);
1107 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 1, val.c_str());
1108 if (stax == NC_NOERR) {
1110 string var_attr_name_fillvalue =
"Orig_FillValue";
1112 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
1113 << val.c_str() << endl);
1117 attrs.append_attr(var_attr_name_fillvalue,
"String", val);
1119 stax = nc_put_att_text(ncid, varid, var_attr_name_fillvalue.c_str(), val.length(), val.c_str());
1124 if (stax != NC_NOERR) {
1125 string err = (string)
"File out netcdf, "
1126 +
"failed to write string attribute " + var_attr_name;
1137 string err = (string)
"File out netcdf, failed to write unknown type of attribute " + var_attr_name;
1143 if (stax != NC_NOERR) {
1144 string err = (string)
"File out netcdf, failed to write " + attr_type +
" attribute " + var_attr_name;
1164 const string &var_name,
1165 const string &global_attr_name,
1166 const string &var_attr_name,
1168 bool is_nc_enhanced) {
1170 D4AttributeType d4_attr_type = attr->type();
1171 int stax = NC_NOERR;
1172 unsigned int attri = 0;
1173 unsigned int num_vals = attr->num_values();
1174 switch (d4_attr_type) {
1175 case attr_container_c: {
1177 BESDEBUG(
"fonc",
"This is an attribute container. attr_name: \"" << global_attr_name <<
"\"" << endl);
1178 D4Attributes *c_attributes = attr->attributes();
1180 add_dap4_attributes(ncid, varid, c_attributes, var_name, global_attr_name, is_nc_enhanced);
1187 vector<unsigned char> vals;
1188 vals.resize(num_vals);
1190 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1192 istringstream is(val);
1193 unsigned int uival = 0;
1195 vals[attri] = (
unsigned char) uival;
1198 stax = nc_put_att_uchar(ncid, varid, var_attr_name.c_str(), NC_UBYTE,
1199 num_vals, &vals[0]);
1200 if (stax != NC_NOERR) {
1201 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1202 +
"failed to write byte attribute " + var_attr_name;
1209 vector<int8_t> vals;
1210 vals.resize(num_vals);
1212 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1214 istringstream is(val);
1217 vals[attri] = (int8_t) uival;
1220 stax = nc_put_att_schar(ncid, varid, var_attr_name.c_str(), NC_BYTE,
1221 num_vals, &vals[0]);
1222 if (stax != NC_NOERR) {
1223 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1224 +
"failed to write signed 8-bit integer attribute " + var_attr_name;
1229 case attr_int16_c: {
1232 vals.resize(num_vals);
1234 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1236 istringstream is(val);
1243 stax = nc_put_att_short(ncid, varid, var_attr_name.c_str(), NC_SHORT,
1244 num_vals, &vals[0]);
1245 if (stax != NC_NOERR) {
1246 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1247 +
"failed to write short attribute " + var_attr_name;
1252 case attr_uint16_c: {
1255 vector<unsigned short>vals;
1256 vals.resize(num_vals);
1257 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1259 istringstream is(val);
1260 unsigned short ival = 0;
1266 stax = nc_put_att_ushort(ncid, varid, var_attr_name.c_str(), NC_USHORT, num_vals,
1268 if (stax != NC_NOERR) {
1269 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1270 +
"failed to write unsigned short attribute " + var_attr_name;
1275 case attr_int32_c: {
1277 vals.resize(num_vals);
1279 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1281 istringstream is(val);
1288 stax = nc_put_att_int(ncid, varid, var_attr_name.c_str(), NC_INT, num_vals,
1290 if (stax != NC_NOERR) {
1291 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1292 +
"failed to write int attribute " + var_attr_name;
1297 case attr_uint32_c: {
1299 vector<unsigned int> vals;
1300 vals.resize(num_vals);
1302 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1304 istringstream is(val);
1305 unsigned int sval = 0;
1311 stax = nc_put_att_uint(ncid, varid, var_attr_name.c_str(), NC_UINT, num_vals,
1313 if (stax != NC_NOERR) {
1314 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1315 +
"failed to write unsigned int attribute " + var_attr_name;
1320 case attr_int64_c: {
1321 vector<long long> vals;
1322 vals.resize(num_vals);
1324 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1326 istringstream is(val);
1333 stax = nc_put_att_longlong(ncid, varid, var_attr_name.c_str(), NC_INT64, num_vals,
1335 if (stax != NC_NOERR) {
1336 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1337 +
"failed to write 64-bit int attribute " + var_attr_name;
1342 case attr_uint64_c: {
1343 vector<unsigned long long> vals;
1344 vals.resize(num_vals);
1346 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1348 istringstream is(val);
1349 unsigned long long sval = 0;
1355 stax = nc_put_att_ulonglong(ncid, varid, var_attr_name.c_str(), NC_UINT64, num_vals,
1357 if (stax != NC_NOERR) {
1358 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1359 +
"failed to write unsigned 64-bit int attribute " + var_attr_name;
1365 case attr_float32_c: {
1367 vals.resize(num_vals);
1369 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1371 const char *cval = val.c_str();
1374 sval = strtod(cval,NULL);
1380 stax = nc_put_att_float(ncid, varid, var_attr_name.c_str(), NC_FLOAT,
1381 num_vals, &vals[0]);
1382 if (stax != NC_NOERR) {
1383 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1384 +
"failed to write float attribute " + var_attr_name;
1389 case attr_float64_c: {
1390 vector<double> vals;
1391 vals.resize(num_vals);
1393 for (D4Attribute::D4AttributeIter vi = attr->value_begin(), ve = attr->value_end(); vi != ve; vi++) {
1395 const char *cval = val.c_str();
1398 sval = strtod(cval,NULL);
1403 stax = nc_put_att_double(ncid, varid, var_attr_name.c_str(), NC_DOUBLE,
1404 num_vals, &vals[0]);
1405 if (stax != NC_NOERR) {
1406 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1407 +
"failed to write double attribute " + var_attr_name;
1414 case attr_otherxml_c:
1417 D4Attribute::D4AttributeIter vi,ve;
1418 vi = attr->value_begin();
1419 ve = attr->value_end();
1424 for (; vi != ve; vi++) {
1428 if (var_attr_name != _FillValue) {
1429 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), val.length(), val.c_str());
1432 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
1433 << val.c_str()[0] << endl);
1434 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 1, val.c_str());
1435 if (stax == NC_NOERR) {
1437 string var_attr_name_fillvalue =
"Orig_FillValue";
1439 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
1440 << val.c_str() << endl);
1444 attrs.append_attr(var_attr_name_fillvalue,
"String", val);
1446 stax = nc_put_att_text(ncid, varid, var_attr_name_fillvalue.c_str(), val.length(), val.c_str());
1450 if (stax != NC_NOERR) {
1451 string err = (string)
"File out netcdf-4 enhanced for DAP4, "
1452 +
"failed to write string attribute " + var_attr_name;
1462 string err = (string)
"File out netcdf, "
1463 +
"failed to write unknown type of attribute " + var_attr_name;
1472 int stax = NC_NOERR;
1473 AttrType attrType = attrs.get_attr_type(attr);
1474 unsigned int attri = 0;
1475 unsigned int num_vals = attrs.get_attr_num(attr);
1477 case Attr_container: {
1479 BESDEBUG(
"fonc",
"This is an attribute container. attr_name: \"" << global_attr_name <<
"\"" << endl);
1480 AttrTable *container = attrs.get_attr_table(attr);
1482 add_attributes(ncid, varid, *container, var_name, global_attr_name, is_nc_enhanced);
1489 vector<unsigned char> vals;
1490 vals.resize(num_vals);
1491 for (attri = 0; attri < num_vals; attri++) {
1492 string val = attrs.get_attr(attr, attri);
1493 istringstream is(val);
1494 unsigned int uival = 0;
1496 vals[attri] = (
unsigned char) uival;
1498 stax = nc_put_att_uchar(ncid, varid, var_attr_name.c_str(), NC_UBYTE,
1499 num_vals, &vals[0]);
1500 if (stax != NC_NOERR) {
1501 string err = (string)
"File out netcdf, "
1502 +
"failed to write byte attribute " + var_attr_name;
1511 vals.resize(num_vals);
1512 for (attri = 0; attri < num_vals; attri++) {
1513 string val = attrs.get_attr(attr, attri);
1514 istringstream is(val);
1519 stax = nc_put_att_short(ncid, varid, var_attr_name.c_str(), NC_SHORT,
1520 num_vals, &vals[0]);
1521 if (stax != NC_NOERR) {
1522 string err = (string)
"File out netcdf, "
1523 +
"failed to write short attribute " + var_attr_name;
1532 vector<unsigned short> vals;
1533 vals.resize(num_vals);
1534 for (attri = 0; attri < num_vals; attri++) {
1535 string val = attrs.get_attr(attr, attri);
1536 istringstream is(val);
1537 unsigned short ival = 0;
1541 stax = nc_put_att_ushort(ncid, varid, var_attr_name.c_str(), NC_USHORT, num_vals,
1543 if (stax != NC_NOERR) {
1544 string err = (string)
"File out netcdf, "
1545 +
"failed to write unsinged short attribute " + var_attr_name;
1554 vals.resize(num_vals);
1555 for (attri = 0; attri < num_vals; attri++) {
1556 string val = attrs.get_attr(attr, attri);
1557 istringstream is(val);
1562 stax = nc_put_att_int(ncid, varid, var_attr_name.c_str(), NC_INT, num_vals,
1564 if (stax != NC_NOERR) {
1565 string err = (string)
"File out netcdf, "
1566 +
"failed to write int attribute " + var_attr_name;
1574 vector<unsigned int> vals;
1575 vals.resize(num_vals);
1576 for (attri = 0; attri < num_vals; attri++) {
1577 string val = attrs.get_attr(attr, attri);
1578 istringstream is(val);
1579 unsigned int lval = 0;
1583 stax = nc_put_att_uint(ncid, varid, var_attr_name.c_str(), NC_UINT, num_vals,
1585 if (stax != NC_NOERR) {
1586 string err = (string)
"File out netcdf, "
1587 +
"failed to write byte attribute " + var_attr_name;
1592 case Attr_float32: {
1596 vals.resize(num_vals);
1597 for (attri = 0; attri < num_vals; attri++) {
1598 string val = attrs.get_attr(attr, attri);
1599 const char *cval = val.c_str();
1602 fval = strtod(cval,NULL);
1606 stax = nc_put_att_float(ncid, varid, var_attr_name.c_str(), NC_FLOAT,
1607 num_vals, &vals[0]);
1608 if (stax != NC_NOERR) {
1609 string err = (string)
"File out netcdf, "
1610 +
"failed to write float attribute " + var_attr_name;
1615 case Attr_float64: {
1618 vector<double> vals;
1619 vals.resize(num_vals);
1620 for (attri = 0; attri < num_vals; attri++) {
1621 string val = attrs.get_attr(attr, attri);
1622 const char *cval = val.c_str();
1625 dval = strtod(cval,NULL);
1629 stax = nc_put_att_double(ncid, varid, var_attr_name.c_str(), NC_DOUBLE,
1630 num_vals, &vals[0]);
1631 if (stax != NC_NOERR) {
1632 string err = (string)
"File out netcdf, "
1633 +
"failed to write double attribute " + var_attr_name;
1640 case Attr_other_xml:
1643 string val = attrs.get_attr(attr, 0);
1644 for (attri = 1; attri < num_vals; attri++) {
1645 val +=
"\n" + attrs.get_attr(attr, attri);
1647 string attr_name = attrs.get_name(attr);
1648 if (attr_name != _FillValue) {
1649 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), val.length(), val.c_str());
1652 "FONcAttributes::add_attributes_worker - Original attribute value is first character: "
1653 << val.c_str()[0] << endl);
1654 stax = nc_put_att_text(ncid, varid, var_attr_name.c_str(), 1, val.c_str());
1655 if (stax == NC_NOERR) {
1657 string var_attr_name_fillvalue =
"Orig_FillValue";
1659 "FONcAttributes::add_attributes_worker - New attribute value is original value: "
1660 << val.c_str() << endl);
1664 attrs.append_attr(var_attr_name_fillvalue,
"String", val);
1666 stax = nc_put_att_text(ncid, varid, var_attr_name_fillvalue.c_str(), val.length(), val.c_str());
1670 if (stax != NC_NOERR) {
1671 string err = (string)
"File out netcdf, "
1672 +
"failed to write string attribute " + var_attr_name;
1678 case Attr_unknown: {
1679 string err = (string)
"File out netcdf, "
1680 +
"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.