| 1 |
from asdf.extension import Converter |
| 2 |
|
| 3 |
|
| 4 |
class EquivalencyConverter(Converter): |
| 5 |
tags = ("tag:astropy.org:astropy/units/equivalency-*",) |
| 6 |
types = ("astropy.units.equivalencies.Equivalency",) |
| 7 |
|
| 8 |
def to_yaml_tree(self, obj, tag, ctx): |
| 9 |
return [ |
| 10 |
{"name": name, "kwargs_names": list(kw.keys()), "kwargs_values": list(kw.values())} |
| 11 |
for name, kw in zip(obj.name, obj.kwargs) |
| 12 |
] |
| 13 |
|
| 14 |
def from_yaml_tree(self, node, tag, ctx): |
| 15 |
from astropy.cosmology.units import with_H0 |
| 16 |
from astropy.units import equivalencies |
| 17 |
|
| 18 |
components = [] |
| 19 |
for equivalency_node in node: |
| 20 |
name = equivalency_node["name"] |
| 21 |
equivalency_method = with_H0 if name == "with_H0" else getattr(equivalencies, name) |
| 22 |
|
| 23 |
kwargs = dict(zip(equivalency_node["kwargs_names"], equivalency_node["kwargs_values"])) |
| 24 |
# astropy 7.0 deprecated factor, if it's None, don't provide it to spectral_density |
| 25 |
if "factor" in kwargs and name == "spectral_density" and kwargs["factor"] is None: |
| 26 |
del kwargs["factor"] |
| 27 |
components.append(equivalency_method(**kwargs)) |
| 28 |
|
| 29 |
# The Equivalency class is a UserList that overrides __add__ to |
| 30 |
# provide special behavior when combined with another Equivalency. |
| 31 |
# We're using sum here to add each subsequent Equivalency to the |
| 32 |
# first so we end up with a single correctly combined Equivalency |
| 33 |
# object at the end. |
| 34 |
return sum(components[1:], components[0]) |