Debian Codegraph @

asdf_astropy/converters/unit/magunit.py

raw
1 from asdf.versioning import split_tag_version
2
3 from .unit import UnitConverter
4
5
6 class MagUnitConverter(UnitConverter):
7 tags = ("tag:astropy.org:astropy/units/magunit-*",)
8 types = ("astropy.units.function.logarithmic.MagUnit",)
9
10 def select_tag(self, obj, tags, ctx):
11 return tags[-1]
12
13 def to_yaml_tree(self, obj, tag, ctx):
14 _, version = split_tag_version(tag)
15
16 # version 1.0.0 uses a nested dictionary
17 if version == "1.0.0":
18 return {"unit": obj.physical_unit}
19
20 return super().to_yaml_tree(obj.physical_unit, tag, ctx)
21
22 def from_yaml_tree(self, node, tag, ctx):
23 from astropy.units import mag
24
25 _, version = split_tag_version(tag)
26
27 # version 1.0.0 uses a nested dictionary
28 if version == "1.0.0":
29 return mag(node["unit"])
30
31 return mag(super().from_yaml_tree(node, tag, ctx))