@@ -4096,13 +4096,13 @@ rename a `netCDF4.Variable` attribute named `oldname` to `newname`."""
4096
4096
4097
4097
if self .mask and (self ._isprimitive or self ._isenum):
4098
4098
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)
4106
4106
4107
4107
if self .scale and self ._isprimitive and valid_scaleoffset:
4108
4108
# if variable has scale_factor and add_offset attributes, rescale.
@@ -4147,13 +4147,23 @@ rename a `netCDF4.Variable` attribute named `oldname` to `newname`."""
4147
4147
4148
4148
def _toma (self ,data ):
4149
4149
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)
4150
4158
# private function for creating a masked array, masking missing_values
4151
4159
# and/or _FillValues.
4152
4160
totalmask = numpy.zeros(data.shape, numpy.bool)
4153
4161
fill_value = None
4154
4162
safe_missval = self ._check_safecast(' missing_value' )
4155
4163
if safe_missval:
4156
4164
mval = numpy.array(self .missing_value, self .dtype)
4165
+ if self .scale and is_unsigned_int:
4166
+ mval = mval.view(dtype_unsigned_int)
4157
4167
# create mask from missing values.
4158
4168
mvalmask = numpy.zeros(data.shape, numpy.bool)
4159
4169
if mval.shape == (): # mval a scalar.
@@ -4178,6 +4188,8 @@ rename a `netCDF4.Variable` attribute named `oldname` to `newname`."""
4178
4188
safe_fillval = self ._check_safecast(' _FillValue' )
4179
4189
if safe_fillval:
4180
4190
fval = numpy.array(self ._FillValue, self .dtype)
4191
+ if self .scale and is_unsigned_int:
4192
+ fval = fval.view(dtype_unsigned_int)
4181
4193
# is _FillValue a NaN?
4182
4194
try :
4183
4195
fvalisnan = numpy.isnan(fval)
@@ -4240,6 +4252,10 @@ rename a `netCDF4.Variable` attribute named `oldname` to `newname`."""
4240
4252
validmin = numpy.array(self .valid_min, self .dtype)
4241
4253
if safe_validmax:
4242
4254
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)
4243
4259
# http://www.unidata.ucar.edu/software/netcdf/docs/attribute_conventions.html).
4244
4260
# "If the data type is byte and _FillValue
4245
4261
# is not explicitly defined,
0 commit comments