Re: Conventions

Mark,

> Why is this exception made for byte data? Why not handle unsigned byte
> data the same as you would unsigned short data? Conversely, why not allow
> unsigned data for any integer data type?

All of the integer types for netCDF variables, including byte, are
interpreted as signed when converted into other numeric types, but the
C interface has functions (the _schar and _uchar interfaces) for
reading byte data into signed or unsigned char variables.  For reading
as char data in C, the bits are copied without modification between
NC_BYTE variables and the host system's "char" data type, which is
signed on some platforms and unsigned on others.  

There is no explicitly unsigned integer type in netCDF, because any
netCDF file must be readable from a Fortran program, and Fortran lacks
support for unsigned integers.  One implication of this is that data
written as unsigned chars in C using the nc_put_var_uchar() interface
can only be read as signed chars from Fortran, with nf_get_var_int1(),
for example.  But all the bits are preserved, so conversion will still
be possible.  (Java also lacks unsigned integer types as primitives.)

Treating byte data as unsigned is so common that some way to represent
this intent is needed for netCDF data.  The original way this was done
was the now-deprecated "signedness" variable attribute.  Using the
"valid_range" attribute to capture this intent is not entirely
satisfactory, but is currently the recommended way to represent this
information.

--Russ

_____________________________________________________________________

Russ Rew                                         UCAR Unidata Program
russ@xxxxxxxxxxxxxxxx                     http://www.unidata.ucar.edu




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