| 1 |
import numpy as np |
| 2 |
from asdf.extension import Converter |
| 3 |
from asdf.tags.core.ndarray import NDArrayType |
| 4 |
|
| 5 |
_GUESSABLE_FORMATS = {"iso", "byear", "jyear", "yday"} |
| 6 |
|
| 7 |
_ASTROPY_FORMAT_TO_ASDF_FORMAT = { |
| 8 |
"isot": "iso", |
| 9 |
"byear_str": "byear", |
| 10 |
"jyear_str": "jyear", |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
class TimeConverter(Converter): |
| 15 |
tags = ("tag:stsci.edu:asdf/time/time-*",) |
| 16 |
types = ("astropy.time.core.Time",) |
| 17 |
|
| 18 |
def to_yaml_tree(self, obj, tag, ctx): |
| 19 |
from astropy.time import Time |
| 20 |
|
| 21 |
base_format = obj.format |
| 22 |
if base_format == "byear": |
| 23 |
obj = Time(obj, format="byear_str") |
| 24 |
elif base_format == "jyear": |
| 25 |
obj = Time(obj, format="jyear_str") |
| 26 |
elif base_format in ("fits", "datetime", "plot_date", "ymdhms", "datetime64"): |
| 27 |
obj = Time(obj, format="isot") |
| 28 |
|
| 29 |
asdf_format = _ASTROPY_FORMAT_TO_ASDF_FORMAT.get(obj.format, obj.format) |
| 30 |
guessable_format = asdf_format in _GUESSABLE_FORMATS |
| 31 |
|
| 32 |
if obj.scale == "utc" and guessable_format and obj.isscalar and base_format == obj.format: |
| 33 |
return obj.value |
| 34 |
|
| 35 |
node = { |
| 36 |
"value": obj.value, |
| 37 |
} |
| 38 |
|
| 39 |
if not guessable_format: |
| 40 |
node["format"] = asdf_format |
| 41 |
|
| 42 |
if base_format != obj.format: |
| 43 |
node["base_format"] = base_format |
| 44 |
|
| 45 |
if obj.scale != "utc": |
| 46 |
node["scale"] = obj.scale |
| 47 |
|
| 48 |
if obj.location is not None: |
| 49 |
# The 1.0.0 and 1.1.0 tags differ in how location is represented. |
| 50 |
# In 1.0.0, there is a single "unit" property that is shared among |
| 51 |
# x, y, and z, and in 1.1.0 each is a quantity with its own unit. |
| 52 |
location = ( |
| 53 |
{ |
| 54 |
"x": obj.location.x.value, |
| 55 |
"y": obj.location.y.value, |
| 56 |
"z": obj.location.z.value, |
| 57 |
"unit": obj.location.unit, |
| 58 |
} |
| 59 |
if tag.endswith("1.0.0") |
| 60 |
else { |
| 61 |
"x": obj.location.x, |
| 62 |
"y": obj.location.y, |
| 63 |
"z": obj.location.z, |
| 64 |
} |
| 65 |
) |
| 66 |
|
| 67 |
node["location"] = location |
| 68 |
|
| 69 |
return node |
| 70 |
|
| 71 |
def from_yaml_tree(self, node, tag, ctx): |
| 72 |
from astropy import units |
| 73 |
from astropy.coordinates import EarthLocation |
| 74 |
from astropy.time import Time |
| 75 |
|
| 76 |
if isinstance(node, (str, list, np.ndarray, NDArrayType)): |
| 77 |
time = Time(node) |
| 78 |
asdf_format = _ASTROPY_FORMAT_TO_ASDF_FORMAT.get(time.format, time.format) |
| 79 |
if asdf_format not in _GUESSABLE_FORMATS: |
| 80 |
msg = f"ASDF time '{node}' is not one of the recognized implicit formats" |
| 81 |
raise ValueError(msg) |
| 82 |
|
| 83 |
return time |
| 84 |
|
| 85 |
location = node.get("location") |
| 86 |
if location is not None: |
| 87 |
# The 1.0.0 and 1.1.0 tags differ in how location is represented. |
| 88 |
# In 1.0.0, there is a single "unit" property that is shared among |
| 89 |
# x, y, and z, and in 1.1.0 each is a quantity with its own unit. |
| 90 |
if tag.endswith("1.0.0"): |
| 91 |
unit = location.get("unit", units.m) |
| 92 |
location = EarthLocation.from_geocentric( |
| 93 |
units.Quantity(location["x"], unit=unit), |
| 94 |
units.Quantity(location["y"], unit=unit), |
| 95 |
units.Quantity(location["z"], unit=unit), |
| 96 |
) |
| 97 |
else: |
| 98 |
location = EarthLocation.from_geocentric( |
| 99 |
location["x"], |
| 100 |
location["y"], |
| 101 |
location["z"], |
| 102 |
) |
| 103 |
|
| 104 |
time = Time( |
| 105 |
node["value"], |
| 106 |
format=node.get("format"), |
| 107 |
scale=node.get("scale"), |
| 108 |
location=location, |
| 109 |
) |
| 110 |
|
| 111 |
base_format = node.get("base_format") |
| 112 |
if base_format is not None and base_format != time.format: |
| 113 |
time.format = base_format |
| 114 |
|
| 115 |
return time |