[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: negative value



>To: address@hidden
>From: "Chung Eui-Seok" <address@hidden>
>Subject: Re: 20020528: negative value
>Organization: .
>Keywords: signed byte data, unsigned byte data

Hi Eui-Seok,

> I am trying to convert netCDF format data to ASCII data using ncdump. 
> 
> When I read this byte data with ncdump, it is interpreted as signed
> (-128 to 127). I would like to obtain unsigned data(0 to 255). What
> I should do to obtain unsigned data?

The operation you want is to just add 256 to any negative value, but
there is no way to specify that ncdump output byte values as unsigned
without modifying the ncdump source and recompiling it.  In versions
of ncdump before the version released with netCDF 3.4, whether bytes
were output as signed or unsigned depended on whether the "char" type
in C was signed or unsigned, which was platform-specific.  For example
on SGI/IRIX systems, byte data printed as unsigned in versions of
ncdump from netCDF 3.3 and earlier.

But rather than get an old version of ncdump, it would be easier just
to recompile it with the following one-line change to the function
printbval in ncdump/vardata.c (about line 199):

    (void) sprintf(sout, fmt, val);
to
    (void) sprintf(sout, fmt, val < 0 ? val + 256 : val);

After making this change, you should be able to just invoke "make" in
that directory and get a new version of ncdump that will print byte
values as unsigned.  It should still be interoperable with ncgen,
since ncgen was written to accept input from ncdump versions that
printed byte values as unsigned.

--Russ

_____________________________________________________________________

Russ Rew                                         UCAR Unidata Program
address@hidden                     http://www.unidata.ucar.edu