Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recognize _Unsigned attribute and return unsigned integer data (issue #656) #658

Merged
merged 12 commits into from
May 12, 2017
6 changes: 4 additions & 2 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
version 1.2.8a0 (tag XXXX)
===========================
version 1.2.8 (not yet released)
=================================
* recognize _Unsigned attribute used by netcdf-java to designate unsigned
integer data stored with a signed integer type in netcdf-3.
* add Dataset init memory parameter to allow loading a file from memory

version 1.2.7 (tag v1.2.7rel)
Expand Down
10,694 changes: 5,416 additions & 5,278 deletions netCDF4/_netCDF4.c

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Version 1.2.8a0
Version 1.2.8
-------------
- - -

Expand Down Expand Up @@ -936,7 +936,7 @@ except ImportError:
# python3: zip is already python2's itertools.izip
pass

__version__ = "1.2.8a0"
__version__ = "1.2.8"

# Initialize numpy
import posixpath
Expand Down Expand Up @@ -3792,6 +3792,13 @@ rename a `netCDF4.Variable` attribute named `oldname` to `newname`."""
# length 1.
if data.ndim != 0: data = numpy.asarray(data[0])

# if attribute _Unsigned is True, and variable has signed integer
# dtype, return view with corresponding unsigned dtype (issue #656)
if self.scale: # only do this if autoscale option is on.
is_unsigned = getattr(self, '_Unsigned', False)
if is_unsigned and data.dtype.kind == 'i':
data = data.view('u%s' % data.dtype.itemsize)

# if auto_scale mode set to True, (through
# a call to set_auto_scale or set_auto_maskandscale),
# perform automatic unpacking using scale_factor/add_offset.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def getnetcdfvers(libdirs):

setup(name = "netCDF4",
cmdclass = cmdclass,
version = "1.2.8a0",
version = "1.2.8",
long_description = "netCDF version 4 has many features not found in earlier versions of the library, such as hierarchical groups, zlib compression, multiple unlimited dimensions, and new data types. It is implemented on top of HDF5. This module implements most of the new features, and can read and write netCDF files compatible with older versions of the library. The API is modelled after Scientific.IO.NetCDF, and should be familiar to users of that module.\n\nThis project has a `Subversion repository <http://code.google.com/p/netcdf4-python/source>`_ where you may access the most up-to-date source.",
author = "Jeff Whitaker",
author_email = "jeffrey.s.whitaker@noaa.gov",
Expand Down
27 changes: 27 additions & 0 deletions test/tst_Unsigned.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
import netCDF4
from numpy.testing import assert_array_equal
import numpy as np

class Test_Unsigned(unittest.TestCase):
"""
Test autoconversion to unsigned ints when _Unsigned attribute is True.
This attribute is is set by netcdf-java to designate unsigned
integer data stored with a signed integer type in netcdf-3.
If _Unsigned=True, a view to the data as unsigned integers is returned.
set_autoscale can be used to turn this off (default is on)
See issue #656 (pull reqeust #658).
"""
def test_unsigned(self):
f = netCDF4.Dataset("ubyte.nc3")
data = f['ub'][:]
assert data.dtype.str[1:] == 'u1'
assert_array_equal(data,np.array([0,255],np.uint8))
f.set_auto_scale(False)
data2 = f['ub'][:]
assert data2.dtype.str[1:] == 'i1'
assert_array_equal(data2,np.array([0,-1],np.int8))
f.close()

if __name__ == '__main__':
unittest.main()
Binary file added test/ubyte.nc3
Binary file not shown.