Ability to specify the desired dtype when decoding a variable with scale_factor and add_offset #9254
Replies: 1 comment 7 replies
-
The desired target dtype can be specified by setting The CF conventions explicitly mention this here http://cfconventions.org/Data/cf-conventions/cf-conventions-1.11/cf-conventions.html#packed-data _When packed data is written, the scale_factor and add_offset attributes must be of the same type as the unpacked data, which must be either float or double. Data of type float must be packed into one of these types: byte, unsigned byte, short, unsigned short. Data of type double must be packed into one of these types: byte, unsigned byte, short, unsigned short, int, unsigned int. When packed data is read, it should be unpacked to the type of the scale_factor and add_offset attributes, which must have the same type if both are present. For guidance only, we suggest that packed data which does not conform to the rules of this section regarding the types of the data variable and attributes should be unpacked to double type, in order to minimise the risk of loss of precision._ |
Beta Was this translation helpful? Give feedback.
-
Hello,
I create this discussion following a recent xarray update that changes the decoding of encoded data via attributes
scale_factor
andadd_offset
. Long story short, before, myuint16
was decoded to afloat32
, but now is decoded tofloat64
. Thefloat32
was desirable for my usecase.More details on this comment I made on the issue: #2304 (comment)
_choose_float_dtype
implementation in the associated PR: https://github.com/pydata/xarray/pull/8713/files#diff-ebbe6dc9f2f17dc4a9777193c923783413a3a6814f6e85d82ade7ca9f8ca65c9R396My question would be: is it possible to specify the desired target dtype? Either via a special encoding attribute (like
scale_factor
andadd_offset
, we could imagineencoded_dtype='uint16'
anddecoded_dtype='float32'
? Or via a special kwarg to pass toopen_dataset
, likeopen_dataset(decode_cf=True, decoding_mapping={'/path/to/my_variable': 'float32'}
?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions