From 724128ad3be9515a70ac5f4359729038aa54c116 Mon Sep 17 00:00:00 2001 From: Dennis Heimbigner Date: Wed, 13 Feb 2019 14:36:14 -0700 Subject: [PATCH] Add hack to deal with DAP2 signed byte hack. re: issue https://github.com/Unidata/netcdf-c/issues/1316 The DAP2 data model does not have a signed byte type, but netcdf-3 does have (only) a signed byte type. So, when converting a netcdf-3 signed byte typed variable to a DAP2 unsigned byte, the following actions are taken by thredds: 1. The variable type is marked as DAP2 (unsigned) byte. 2. A special attribute, "_Unsigned=false" is set for the variable 3. The corresponding "_FillValue" attribute, if any, is up-converted to the DAP2 Int16 type in order to hold, with sign, any signed byte fill value. On the netcdf-c side, this looks like a fillvalue type mismatch and causes an error. Instead, the netcdf-c dap2 conversion code needs to recognize this hack and undo it locally. So this change looks for the above case, and if found, then it properly converts the _FillValue type to netcdf-3 signed byte. Since DAP2 supports both signed and unsigned integers of sizes 16 and 32 bits, this should be the only hack needed (famous last words). It may later be desirable for the thredds DAP2 converter to modify its behavior as well. --- libdap2/ncd2dispatch.c | 36 +++++++++++++++++++++++++++++++++++- oc2/ochttp.c | 1 + 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/libdap2/ncd2dispatch.c b/libdap2/ncd2dispatch.c index 6bc51100b3..226eef04c6 100644 --- a/libdap2/ncd2dispatch.c +++ b/libdap2/ncd2dispatch.c @@ -780,11 +780,45 @@ fprintf(stderr,"\n"); } var->ncid = varid; if(var->attributes != NULL) { + NCattribute* unsignedatt = NULL; + int unsignedval = 0; + /* See if variable has _Unsigned attribute */ for(j=0;jattributes);j++) { NCattribute* att = (NCattribute*)nclistget(var->attributes,j); + if(strcmp(att->name,"_Unsigned") == 0) { + char* value = nclistget(att->values,0); + unsignedatt = att; + if(value != NULL) { + if(strcasecmp(value,"false")==0 + || strcmp(value,"0")==0) + unsignedval = 0; + else + unsignedval = 1; + } + break; + } + } + for(j=0;jattributes);j++) { + NCattribute* att = (NCattribute*)nclistget(var->attributes,j); + char* val = NULL; /* Check for _FillValue/Variable mismatch */ if(strcmp(att->name,"_FillValue")==0) { - if(att->etype != var->etype) { + /* Special case var is byte, fillvalue is int16 and + unsignedattr == 0; + This exception is needed because DAP2 byte type + is equivalent to netcdf ubyte type. So passing + a signed byte thru DAP2 requires some type and + signedness hacking that we have to undo. + */ + if(var->etype == NC_UBYTE + && att->etype == NC_SHORT + && unsignedatt != NULL && unsignedval == 0) { + /* Forcibly change the attribute type and signedness */ + att->etype = NC_BYTE; + val = nclistremove(unsignedatt->values,0); + if(val) free(val); + nclistpush(unsignedatt->values,strdup("false")); + } else if(att->etype != var->etype) {/* other mismatches */ /* Log a message */ nclog(NCLOGERR,"_FillValue/Variable type mismatch: variable=%s",var->ncbasename); /* See if mismatch is allowed */ diff --git a/oc2/ochttp.c b/oc2/ochttp.c index e6ae849c5d..70701c0b3f 100644 --- a/oc2/ochttp.c +++ b/oc2/ochttp.c @@ -138,6 +138,7 @@ ocfetchurl(CURL* curl, const char* url, NCbytes* buf, long* filetime) fail: nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat)); switch (httpcode) { + case 400: stat = OC_EBADURL; break; case 401: stat = OC_EAUTH; break; case 404: stat = OC_ENOFILE; break; case 500: stat = OC_EDAPSVC; break;