Unidata - To provide the data services, tools, and cyberinfrastructure leadership that advance Earth system science, enhance educational opportunities, and broaden participation. Unidata
         
  advanced  
 

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




 
 
  Contact Us     Site Map     Search     Terms and Conditions     Privacy Policy     Participation Policy
 
National Science Foundation (NSF) UCAR Community Programs   Unidata is a member of the UCAR Community Programs, is managed by the University Corporation for Atmospheric Research, and is sponsored by the National Science Foundation.
P.O. Box 3000     Boulder, CO 80307-3000 USA     Tel: 303-497-8643     Fax: 303-497-8690