Re: [netcdfgroup] nc_get_vara_float on short data changes data range?

There is an error, but probably not the one you think.
If you look at the DAP2 dds by pasting this in your browser
http://opendap.nmdc.eu/knmi/thredds/dodsC/radarprecipclim/RAD_NL21_RAC_MFBS_01H_NC.nc.dds
you will see that the type of image1_image_data is actually
an unsigned short (UInt16). Unfortunately netcdf-3 does not
support unsigned data types so it reports the type as signed short.
However internally, it is using the underlying unsigned short
so it is converting as that type. The attribute is handled
correctly because opendap is properly typeing it as signed short.
We have recently decided to tag such instances with a special
attribute "_Unsigned" to alert the caller of this situation, but it
is not yet in the distribution.
Since the underlying data being sent by the server is in fact
Unsigned, the conversion to float is technically correct.
=Dennis Heimbigner
 Unidata


plieger wrote:
Hi,

Here is a question about data-type conversion done by the netcdf-c library.

I have a file with short data, but I am trying to read the data as float using nc_get_vara_float. This will make it easier for me to apply scale and offset factors later on.

I noticed that the short value of -1 is changed to 65535 when reading it with the nc_get_vara_float function, while for attributes the value stays -1, when using the nc_get_att_float function.

Is this expected behaviour? Below a small test program which illustrates this:

#include <stdio.h>
#include <netcdf.h>

int main(){
  int status;
  int ncid;
  int varid;
  static size_t start[] = {0, 0, 0};
  static size_t count[] = {1, 1, 1};
  float varData[1];
  float attrData[1];

status = nc_open("http://opendap.nmdc.eu/knmi/thredds/dodsC/radarprecipclim/RAD_NL21_RAC_MFBS_01H_NC.nc";, 0, &ncid);
  if (status != NC_NOERR) {return 1;}

  status = nc_inq_varid (ncid, "image1_image_data", &varid);
  if (status != NC_NOERR) {return 2;}

  status = nc_get_vara_float(ncid, varid, start, count, varData);
  if (status != NC_NOERR) {return 3;}

  status = nc_get_att_float(ncid,varid,"_FillValue",attrData);
  if (status != NC_NOERR) {return 4;}

  nc_close(ncid);

  printf("Vardata : %f\t\nAttrdata: %f\n",varData[0],attrData[0]);
  return 0;
}

it returns:
Vardata : 65535.000000
Attrdata: -1.000000

I would expect that both values are -1?

Best regards,
Maarten Plieger




  • 2013 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: