Re: PyNIO signed/unsigned bytes

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Wed, 15 Jul 2009 15:48:44 -0600

Hi Tim,
Sorry for the delay in getting back to you on this issue. It is a
somewhat unfortunate historical 'feature' of the NIO library that
underlies PyNIO that the 'byte' type is treated as unsigned, whereas
in NetCDF it is signed. This is an issue that we are working on to
devise a backwards-compatible fix for. There is (as far as I can
tell) nothing wrong with the encoding of the data.

Fortunately, there is a pretty easy work-around using the NumPy
'astype' method:

>>> f.set_option('MaskedArrayMode', 'MaskedNever')
>>> v = f.variables['hgt_cref']
>>> v[:].astype('int8')
array([[[[-128, -128, -128, ..., -128, -128, -128],
          [-128, -128, -128, ..., -128, -128, -128],
          [-128, -128, -128, ..., -128, -128, -128],
          ...,
          [-128, -128, -128, ..., -128, -128, -128],
          [-128, -128, -128, ..., -128, -128, -128],
          [-128, -128, -128, ..., -128, -128, -128]]]], dtype=int8)
>>> v[:].astype('int8').min()
-128
>>> v[:].astype('int8').max()
127
>>> v.valid_min
array([129], dtype=uint8)
>>> v.valid_min.astype('int8')
array([-127], dtype=int8)
>>> v.valid_max.astype('int8')
array([127], dtype=int8)
>>> v._FillValue.astype('int8')
array([-128], dtype=int8)

Hope this helps.
  -dave

On Jul 8, 2009, at 10:09 AM, Supinie, Tim A. wrote:

> Hello,
>
> I'm using PyNIO 1.3.0b1 to read in some composite reflectivity
> data, which was encoded in signed bytes. When reading them in, I
> noticed the minimum valid value, which should be -127, is 129, and
> the maximum valid value is 127 (as it should be). The values in
> the data should also range from -127 to 127. I think PyNIO is
> reading all the signed bytes in as unsigned bytes, which presents
> some problems (the least of which is the fact that the minimum
> valid value is greater than the maximum valid value.) I'm curious
> as to whether that's a problem with the encoding of the file, if
> it's an option that can be set somehow, or whether it's a bug. I
> can provide a sample script and a data file if you need me to.
>
> Thanks,
> Tim Supinie
> _______________________________________________
> pyngl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/pyngl-talk

_______________________________________________
pyngl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/pyngl-talk
Received on Wed Jul 15 2009 - 15:48:44 MDT

This archive was generated by hypermail 2.2.0 : Sat Aug 01 2009 - 06:38:59 MDT