| 1 |
from asdf.extension import Converter |
| 2 |
from asdf.tags.core.ndarray import NDArrayType |
| 3 |
|
| 4 |
|
| 5 |
class SpectralCoordConverter(Converter): |
| 6 |
tags = ("tag:astropy.org:astropy/coordinates/spectralcoord-*",) |
| 7 |
types = ("astropy.coordinates.spectral_coordinate.SpectralCoord",) |
| 8 |
|
| 9 |
def to_yaml_tree(self, obj, tag, ctx): |
| 10 |
node = { |
| 11 |
"value": obj.value, |
| 12 |
"unit": obj.unit, |
| 13 |
} |
| 14 |
|
| 15 |
if obj.observer is not None: |
| 16 |
node["observer"] = obj.observer |
| 17 |
|
| 18 |
if obj.target is not None: |
| 19 |
node["target"] = obj.target |
| 20 |
|
| 21 |
return node |
| 22 |
|
| 23 |
def from_yaml_tree(self, node, tag, ctx): |
| 24 |
from astropy.coordinates.spectral_coordinate import SpectralCoord |
| 25 |
|
| 26 |
value = node["value"] |
| 27 |
if isinstance(value, NDArrayType): |
| 28 |
# TODO: Why doesn't NDArrayType work? This needs some research |
| 29 |
# and documentation. See similar note in QuantityConverter. |
| 30 |
value = value._make_array() |
| 31 |
|
| 32 |
return SpectralCoord( |
| 33 |
value, |
| 34 |
unit=node["unit"], |
| 35 |
observer=node.get("observer"), |
| 36 |
target=node.get("target"), |
| 37 |
) |