From a48338d4016f70fe1951afc5e90a4d6a73626125 Mon Sep 17 00:00:00 2001 From: jswhit Date: Wed, 23 Aug 2023 14:04:14 -0400 Subject: [PATCH 1/3] fix for issue #1271 --- src/netCDF4/_netCDF4.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/netCDF4/_netCDF4.pyx b/src/netCDF4/_netCDF4.pyx index 8ec816505..1349ff3c4 100644 --- a/src/netCDF4/_netCDF4.pyx +++ b/src/netCDF4/_netCDF4.pyx @@ -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 - @@ -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): From 3d0cd655a439f55e4e52ebef5769754eca6c5d78 Mon Sep 17 00:00:00 2001 From: jswhit Date: Wed, 23 Aug 2023 14:21:40 -0400 Subject: [PATCH 2/3] add test --- test/tst_masked.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/tst_masked.py b/test/tst_masked.py index 4ae43e187..d2d411fdc 100644 --- a/test/tst_masked.py +++ b/test/tst_masked.py @@ -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)) @@ -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',)) @@ -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) @@ -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() From cd0f5dadd55ead744eec5ae76340c11e49eecfbf Mon Sep 17 00:00:00 2001 From: jswhit Date: Wed, 23 Aug 2023 14:25:52 -0400 Subject: [PATCH 3/3] add Changelog entry, bump version number --- Changelog | 4 ++++ src/netCDF4/_netCDF4.pyx | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Changelog b/Changelog index 1efad0da0..ac1271c90 100644 --- a/Changelog +++ b/Changelog @@ -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 diff --git a/src/netCDF4/_netCDF4.pyx b/src/netCDF4/_netCDF4.pyx index 1349ff3c4..eeed0a9e9 100644 --- a/src/netCDF4/_netCDF4.pyx +++ b/src/netCDF4/_netCDF4.pyx @@ -1,5 +1,5 @@ """ -Version 1.6.4 +Version 1.6.5 ------------- # Introduction @@ -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