astropy/asdf-astropy

`dtype` is not preserved roundtrip by `Quantity`

Closed this issue · 0 comments

We currently do not preserve the dtype for Quantity objects roundtrip. E.G.

import asdf
import astropy.units as u

amp33 = u.Quantity(np.zeros((5, 5), dtype=np.uint16), dtype=np.uint16)
print(f"{amp33.dtype=}")

af = asdf.AsdfFile({"amp33": amp33})
af.write_to('test.asdf')

with asdf.open('test.asdf', mode="rw") as ff:
    new_amp33 = ff["amp33"]

print(f"{new_amp33.dtype=}")

returns

amp33.dtype=dtype('uint16')
new_amp33.dtype=dtype('float64')

So the dtype is not making it round trip. I believe this is because Quantity automatically casts the dtype to float64 unless otherwise specified. This means the QuantityConverter needs to add a dtype argument here:

return Quantity(value, unit=node["unit"], copy=False)
matching the dtype of value if it is a ndarray.