Skip to content

Commit

Permalink
Merge pull request #1272 from Unidata/issue1271
Browse files Browse the repository at this point in the history
fix for issue #1271
  • Loading branch information
jswhit authored Aug 24, 2023
2 parents e1d3c1f + cd0f5da commit b12a517
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
version 1.6.5 (not yet released)
===============================
* fix for issue #1271 (mask ignored if bool MA assinged to uint8 var)

version 1.6.4 (tag v1.6.4rel)
===============================
* set path to SSL certificates internally, so https DAP URLs work with wheels
Expand Down
8 changes: 4 additions & 4 deletions src/netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Version 1.6.4
Version 1.6.5
-------------
# Introduction
Expand Down Expand Up @@ -1227,7 +1227,7 @@ from .utils import (_StartCountStride, _quantize, _find_dim, _walk_grps,
import sys
import functools

__version__ = "1.6.4"
__version__ = "1.6.5"

# Initialize numpy
import posixpath
Expand Down Expand Up @@ -5233,6 +5233,8 @@ rename a `Variable` attribute named `oldname` to `newname`."""
elif hasattr(self, 'add_offset'):
data = data - self.add_offset
if self.dtype.kind in 'iu': data = numpy.around(data)
if self.dtype != data.dtype:
data = data.astype(self.dtype) # cast data to var type, if necessary.
if ma.isMA(data):
# if underlying data in masked regions of masked array
# corresponds to missing values, don't fill masked array -
Expand Down Expand Up @@ -5263,8 +5265,6 @@ rename a `Variable` attribute named `oldname` to `newname`."""
data = numpy.array([fillval],self.dtype)
else:
data = data.filled(fill_value=fillval)
if self.dtype != data.dtype:
data = data.astype(self.dtype) # cast data to var type, if necessary.
return data

def _assign_vlen(self, elem, data):
Expand Down
20 changes: 20 additions & 0 deletions test/tst_masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# create an n1dim by n2dim random ranarr.
FILE_NAME = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
FILE_NAME2 = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
FILE_NAME3 = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
ndim = 10
ranarr = 100.*uniform(size=(ndim))
ranarr2 = 100.*uniform(size=(ndim))
Expand All @@ -41,6 +42,7 @@ class PrimitiveTypesTestCase(unittest.TestCase):
def setUp(self):
self.file = FILE_NAME
self.file2 = FILE_NAME2
self.file3 = FILE_NAME3
file = netCDF4.Dataset(self.file,'w')
file.createDimension('n', ndim)
foo = file.createVariable('maskeddata', 'f8', ('n',))
Expand Down Expand Up @@ -93,6 +95,19 @@ def setUp(self):
data = dataset['v'][:]
dataset.close()

# issue #1271 (mask is ignored when assigning bool array to uint8 var)
ds = netCDF4.Dataset(self.file3, "w")
dim = ds.createDimension('time', 48)
var = ds.createVariable('blaat', np.uint8, ('time',),
zlib=True, complevel=4, shuffle=True, fletcher32=True,
fill_value=240)
mask = np.full((48,), False)
for x in range(30):
mask[x] = True
mama = ma.array(np.full((48,), True), mask=mask)
var[:] = mama
ds.close()

def tearDown(self):
# Remove the temporary files
os.remove(self.file)
Expand Down Expand Up @@ -148,6 +163,11 @@ def runTest(self):
assert var1[:].mask.all()
assert var2[:].mask.any() == False
dataset.close()
# issue #1271
ds = netCDF4.Dataset(self.file3,"r")
var = ds['blaat']
assert np.count_nonzero(var[:].mask) == 30
ds.close()

if __name__ == '__main__':
unittest.main()

0 comments on commit b12a517

Please sign in to comment.