-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathunit.py
49 lines (37 loc) · 1.44 KB
/
unit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import warnings
from asdf.extension import Converter
class UnitConverter(Converter):
tags = (
"tag:stsci.edu:asdf/unit/unit-*",
"tag:astropy.org:astropy/units/unit-*",
)
types = (
"astropy.units.core.CompositeUnit",
"astropy.units.core.IrreducibleUnit",
"astropy.units.core.NamedUnit",
"astropy.units.core.PrefixUnit",
"astropy.units.core.Unit",
"astropy.units.core.UnitBase",
"astropy.units.core.UnrecognizedUnit",
"astropy.units.function.mixin.IrreducibleFunctionUnit",
"astropy.units.function.mixin.RegularFunctionUnit",
)
def select_tag(self, obj, tags, ctx):
from astropy.units import UnitsError, UnitsWarning
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=UnitsWarning)
try:
obj.to_string(format="vounit")
except (UnitsError, ValueError):
return next(t for t in tags if "astropy.org" in t)
return next(t for t in tags if "stsci.edu" in t)
def to_yaml_tree(self, obj, tag, ctx):
if "stsci.edu" in tag:
return obj.to_string(format="vounit")
return obj.to_string()
def from_yaml_tree(self, node, tag, ctx):
from astropy.units import Unit
kwargs = {"parse_strict": "silent"}
if "stsci.edu" in tag:
kwargs["format"] = "vounit"
return Unit(node, **kwargs)