Skip to content

Commit 435f3c4

Browse files
committed
widen valid_min/valid_max is _Unsigned is set (issue #794)
1 parent 5c042a4 commit 435f3c4

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

netCDF4/_netCDF4.pyx

+23-7
Original file line numberDiff line numberDiff line change
@@ -4096,13 +4096,13 @@ rename a `netCDF4.Variable` attribute named `oldname` to `newname`."""
40964096

40974097
if self.mask and (self._isprimitive or self._isenum):
40984098
data = self._toma(data)
4099-
4100-
# if attribute _Unsigned is True, and variable has signed integer
4101-
# dtype, return view with corresponding unsigned dtype (issue #656)
4102-
if self.scale: # only do this if autoscale option is on.
4103-
is_unsigned = getattr(self, '_Unsigned', False)
4104-
if is_unsigned and data.dtype.kind == 'i':
4105-
data = data.view('u%s' % data.dtype.itemsize)
4099+
else:
4100+
# if attribute _Unsigned is True, and variable has signed integer
4101+
# dtype, return view with corresponding unsigned dtype (issue #656)
4102+
if self.scale: # only do this if autoscale option is on.
4103+
is_unsigned = getattr(self, '_Unsigned', False)
4104+
if is_unsigned and data.dtype.kind == 'i':
4105+
data = data.view('u%s' % data.dtype.itemsize)
41064106

41074107
if self.scale and self._isprimitive and valid_scaleoffset:
41084108
# if variable has scale_factor and add_offset attributes, rescale.
@@ -4147,13 +4147,23 @@ rename a `netCDF4.Variable` attribute named `oldname` to `newname`."""
41474147

41484148
def _toma(self,data):
41494149
cdef int ierr, no_fill
4150+
# if attribute _Unsigned is True, and variable has signed integer
4151+
# dtype, return view with corresponding unsigned dtype (issues #656,
4152+
# #794)
4153+
is_unsigned = getattr(self, '_Unsigned', False)
4154+
is_unsigned_int = is_unsigned and data.dtype.kind == 'i'
4155+
if self.scale and is_unsigned_int: # only do this if autoscale option is on.
4156+
dtype_unsigned_int = 'u%s' % data.dtype.itemsize
4157+
data = data.view(dtype_unsigned_int)
41504158
# private function for creating a masked array, masking missing_values
41514159
# and/or _FillValues.
41524160
totalmask = numpy.zeros(data.shape, numpy.bool)
41534161
fill_value = None
41544162
safe_missval = self._check_safecast('missing_value')
41554163
if safe_missval:
41564164
mval = numpy.array(self.missing_value, self.dtype)
4165+
if self.scale and is_unsigned_int:
4166+
mval = mval.view(dtype_unsigned_int)
41574167
# create mask from missing values.
41584168
mvalmask = numpy.zeros(data.shape, numpy.bool)
41594169
if mval.shape == (): # mval a scalar.
@@ -4178,6 +4188,8 @@ rename a `netCDF4.Variable` attribute named `oldname` to `newname`."""
41784188
safe_fillval = self._check_safecast('_FillValue')
41794189
if safe_fillval:
41804190
fval = numpy.array(self._FillValue, self.dtype)
4191+
if self.scale and is_unsigned_int:
4192+
fval = fval.view(dtype_unsigned_int)
41814193
# is _FillValue a NaN?
41824194
try:
41834195
fvalisnan = numpy.isnan(fval)
@@ -4240,6 +4252,10 @@ rename a `netCDF4.Variable` attribute named `oldname` to `newname`."""
42404252
validmin = numpy.array(self.valid_min, self.dtype)
42414253
if safe_validmax:
42424254
validmax = numpy.array(self.valid_max, self.dtype)
4255+
if validmin is not None and self.scale and is_unsigned_int:
4256+
validmin = validmin.view(dtype_unsigned_int)
4257+
if validmax is not None and self.scale and is_unsigned_int:
4258+
validmax = validmax.view(dtype_unsigned_int)
42434259
# http://www.unidata.ucar.edu/software/netcdf/docs/attribute_conventions.html).
42444260
# "If the data type is byte and _FillValue
42454261
# is not explicitly defined,

0 commit comments

Comments
 (0)