Re: Type mismatch trying to save 8-bit integer

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Mon Aug 23 2010 - 14:00:32 MDT

Hi Sourish,

This problem derives from a historical artifact that we intend to
address in the near future. Basically the Nio library has only
supported the byte type as unsigned.
This is confusingly in contrast to the NetCDF 3.x interface which only
supported byte as a signed type. Both classic NetCDF and the Nio
library use the character
type for the unsigned or signed 8 bit integer type respectively. This
does not map well into NumPy however.

So currently when you create an Nio variable using either the 'b' or
the 'B' type specification you get an unsigned 8 bit type. The type
mismatch occurs when you
try to populate it with a signed int 8 type. Your code will work if
you just change the last statement to:

var[:] = array([1, 2, 3], uint8)

If you are storing 1's and 0's only this should be no problem. And if
you need to store negative int8 values you can always use the astype
method to recover the
actual values.

As far as a logical type is concerned, you can create a variable
supposedly of logical type using the '?' character as the type
specifier. Unfortunately this actually
turns into a 32 bit integer, which is not really useful for compact
storage.

Recently we have added new types 'int8' and 'uint8' to the Nio
library. Because of the limitations of the NetCDF3 interface, there
will still be difficulties supporting these types explicitly for
NetCDF3 files. However, we plan to have both types explicitly
supported for NetCDF 4 and HDF, although the details of this support
have not been completely worked out yet.
  -dave

On Aug 23, 2010, at 8:03 AM, Sourish Basu wrote:

> Hi all,
>
> I'm trying to save an array of 8-bit integers to a NetCDF/HDF
> file. Sample script:
>
> from numpy import *
> from Nio import open_file
> fid = open_file('test.nc','w')
> fid.create_dimension('dummy', 3)
> var = fid.create_variable('dummy_var', 'b', ('dummy',))
> var[:] = array([1, 2, 3], int8)
>
> at which point I get the following error (same for test.hdf):
>
> NIOError Traceback (most recent
> call last)
>
> /var/autofs/hd/data/GOSAT data/L2/<ipython console> in <module>()
>
> /usr/local/lib/python2.6/site-packages/PyNIO/Nio.pyc in
> __setitem__(self, xsel, value)
> 362
> 363 if not isinstance(xsel, xSelect) or xsel.isbasic:
> --> 364 self._obj[xsel] = value
> 365 else:
> 366 bb = xsel.bndbox()
>
> NIOError: type or dimensional mismatch writing to variable (dummy_var)
>
> I don't understand the source of this error. Why can't I save 8-
> bit integers? I'm using python 2.6.5, numpy 1.3.0 and PyNio 1.3.0b4.
>
> As an aside, is there a way to save a logical variable to a
> NetCDF/HDF file? The variable I want to save is just a yes/no (1/0)
> flag, so even using 8 bits seems like a waste.
>
> Thanks in advance,
>
> Sourish Basu
>
> <S_Basu.vcf>_______________________________________________
> 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 Mon Aug 23 14:00:48 2010

This archive was generated by hypermail 2.1.8 : Thu Sep 09 2010 - 15:43:50 MDT