@@ -304,28 +304,28 @@ def _choose_float_dtype(
304
304
# see https://github.com/pydata/xarray/issues/5597#issuecomment-879561954
305
305
scale_factor = mapping .get ("scale_factor" )
306
306
add_offset = mapping .get ("add_offset" )
307
- if scale_factor or add_offset :
307
+ if scale_factor is not None or add_offset is not None :
308
308
# get the type from scale_factor/add_offset to determine
309
309
# the needed floating point type
310
- if scale_factor :
310
+ if scale_factor is not None :
311
311
scale_type = type (scale_factor )
312
- if add_offset :
312
+ if add_offset is not None :
313
313
offset_type = type (add_offset )
314
314
# CF conforming, both scale_factor and add-offset are given and
315
- # of same floating point type
315
+ # of same floating point type (float32/64)
316
316
if (
317
- add_offset
318
- and scale_factor
317
+ add_offset is not None
318
+ and scale_factor is not None
319
319
and offset_type == scale_type
320
- and np .issubdtype ( scale_type , np .floating )
320
+ and scale_type in [ np .float32 , np .float64 ]
321
321
):
322
322
return np .dtype (scale_type ).type
323
323
# Not CF conforming and add_offset given:
324
324
# A scale factor is entirely safe (vanishing into the mantissa),
325
325
# but a large integer offset could lead to loss of precision.
326
326
# Sensitivity analysis can be tricky, so we just use a float64
327
327
# if there's any offset at all - better unoptimised than wrong!
328
- if add_offset :
328
+ if add_offset is not None :
329
329
return np .float64
330
330
# return float32 in other cases where only scale_factor is given
331
331
return np .float32
0 commit comments