Re: NC_BYTE and NC_CHAR (signed and unsigned 8-bit integers)

On Feb 3, 11:54am, lombardi emanuele wrote:
> Dear netcdf people,
>
> I'm asking your help from Italy.
>
> I need to save has much space as possible in saving my data so I
> decided to normalize data into the range 0-255 so that it can be kept
> in a single byte.
> In netcdf there are 2 8-bit data types:  NC_CHAR and NC_BYTE.
>
> I started using NC_CHAR data type and
> all works fine, but many utilities (ncview, vcs...) do'nt display
> CHAR data. So I'm trying to save the normalized data as NC_BYTE which
> can be plotted much easier.

You should only use NC_CHAR for "text" data. From FORTRAN, this would
be type "character". Use the NC_BYTE type for numbers.

> BUT netcdf library returns NC_BYTE data as 8-bit signed
> integers (instead of 8-bit unsigned integers)
> so that NC_BYTE values go from 0 to 127 and then from -128 to -1
> instead of going from 0 to 255 as wanted.

This is not just a feature of the netcdf library.
For many (most?) of the FORTRAN implementations that
support an 8 bit integer type (integer*1, byte, ...), that
type is a signed integer. For java, the byte type is signed.

In netcdf-3 when we introduced the interfaces which perform
automatic conversion from external type to machine type, we had to
decide whether the NC_BYTE was signed or unsigned. We chose "signed"
for consistancy with the FORTRAN and java, and because it has greater
symmetry with the other numeric nctypes.

In C, we have a family of put and get functions which "do the right
thing" (what you want) with arguments of C language type "unsigned char".
Since the FORTRAN language doesn't have such a type, no such functions
exist in netcdf FORTRAN interface.

> That means that MIN and MAX values are all wrong and that
> lookup tables for colors are no more monotonics from 0 to 255
> but must be made of 2 separted parts: the one for numbers from 0 to 127
> and the one for numbers from -128 to -1.
>
> How can I manage it?
>
> I'm a FORTRAN programmer, not a C programmer unfortunately!

All is not lost, the bits are not getting altered.
I can't tell for sure how the situation is causing you problems.
Are you using a display utility that requires MIN and MAX attributes,
or requires that they be monotonic?

> Any help and suggestion will be very apreciated.
> Thanks from Italy,
>
> Emanuele
>
> --
>  Emanuele Lombardi
>  mail:  AMB-GEM-CLIM ENEA Casaccia
>         I-00060 S.M. di Galeria (RM)
>         ITALY
>  mailto:lele@xxxxxxxxxxxxxxxxxxxxxxxxx
>  tel  +39 6 30483366
>  fax  +39 6 30483591




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