Debian Codegraph @

asdf_astropy/converters/coordinates/tests/test_frame.py

raw
1 import importlib.metadata
2 import unittest.mock as mk
3
4 import asdf
5 import numpy as np
6 import pytest
7 from asdf.testing.helpers import yaml_to_asdf
8 from astropy import units as u
9 from astropy.coordinates import (
10 CIRS,
11 FK4,
12 FK5,
13 GCRS,
14 ICRS,
15 ITRS,
16 TEME,
17 TETE,
18 Angle,
19 CartesianRepresentation,
20 FK4NoETerms,
21 Galactic,
22 Galactocentric,
23 Latitude,
24 Longitude,
25 PrecessedGeocentric,
26 SkyCoord,
27 SphericalRepresentation,
28 )
29 from astropy.time import Time
30
31 from asdf_astropy.converters.coordinates.frame import FrameConverter, LegacyICRSConverter
32 from asdf_astropy.testing.helpers import assert_frame_equal
33
34 # skip TETE and TEME for old asdf-coordinates_schemas
35 if importlib.metadata.version("asdf-coordinates-schemas") < "0.5.0":
36 TETE_MARKS = [pytest.mark.skip]
37 else:
38 TETE_MARKS = []
39
40
41 def create_frames():
42 test_data = {
43 "ra": 1 * u.deg,
44 "dec": 2 * u.deg,
45 }
46
47 sky = SkyCoord(ra=10 * u.deg, dec=12 * u.deg, frame="icrs")
48
49 return [
50 CIRS(),
51 CIRS(**test_data),
52 CIRS(**test_data, obstime=Time("J2005")),
53 FK4(),
54 FK4(**test_data),
55 FK4(**test_data, obstime=Time("B1950")),
56 FK4NoETerms(),
57 FK4NoETerms(**test_data),
58 FK4NoETerms(**test_data, obstime=Time("J1975")),
59 FK5(),
60 FK5(**test_data),
61 FK5(**test_data, equinox="J2005"),
62 FK5(**test_data, equinox="2011-01-01T00:00:00"),
63 Galactic(l=47.37 * u.degree, b=+6.32 * u.degree),
64 Galactocentric(
65 x=np.linspace(-10.0, 10.0, 100) * u.kpc,
66 y=np.linspace(-10.0, 10.0, 100) * u.kpc,
67 z=np.zeros(100) * u.kpc,
68 z_sun=15 * u.pc,
69 ),
70 GCRS(),
71 GCRS(**test_data),
72 GCRS(**test_data, obsgeoloc=CartesianRepresentation([1, 2, 3], unit=u.m)),
73 ICRS(**test_data),
74 ICRS(ra=Longitude(25, unit=u.deg), dec=Latitude(45, unit=u.deg)),
75 ICRS(ra=Longitude(25, unit=u.deg, wrap_angle=Angle(1.5, unit=u.rad)), dec=Latitude(45, unit=u.deg)),
76 ICRS(ra=[0, 1, 2] * u.deg, dec=[3, 4, 5] * u.deg),
77 ITRS(SphericalRepresentation(lon=12.3 * u.deg, lat=45.6 * u.deg, distance=1 * u.km)),
78 PrecessedGeocentric(),
79 PrecessedGeocentric(**test_data),
80 PrecessedGeocentric(**test_data, equinox="B1975"),
81 pytest.param(TEME(obstime=Time("J2020")), marks=TETE_MARKS),
82 pytest.param(TEME(sky.represent_as("cartesian"), obstime=Time("J2020")), marks=TETE_MARKS),
83 pytest.param(TETE(), marks=TETE_MARKS),
84 pytest.param(TETE(**test_data, obstime=Time("J2020")), marks=TETE_MARKS),
85 ]
86
87
88 @pytest.mark.parametrize("frame", create_frames())
89 def test_serialization(frame, tmp_path):
90 file_path = tmp_path / "test.asdf"
91 with asdf.AsdfFile() as af:
92 af["frame"] = frame
93 af.write_to(file_path)
94
95 with asdf.open(file_path) as af:
96 assert_frame_equal(af["frame"], frame)
97
98
99 def test_tags():
100 converter = FrameConverter(["tag1", "tag2"], "test")
101 assert converter.tags == ["tag1", "tag2"]
102
103
104 def test_legacy_icrs_serialize():
105 converter = LegacyICRSConverter()
106
107 ra = 25
108 dec = 45
109
110 frame = ICRS(ra=Longitude(ra, unit=u.deg), dec=Latitude(dec, unit=u.deg))
111 node = converter.to_yaml_tree(frame, mk.MagicMock(), mk.MagicMock())
112
113 assert node["ra"]["value"] == ra
114 assert node["ra"]["unit"] == "deg"
115 assert node["ra"]["wrap_angle"] == 360 * u.deg
116
117 assert node["dec"]["value"] == dec
118 assert node["dec"]["unit"] == "deg"
119
120
121 def test_legacy_icrs_deseialize():
122 example = """!<tag:astropy.org:astropy/coordinates/frames/icrs-1.0.0>
123 ra:
124 value: 25
125 unit: deg
126 wrap_angle: !unit/quantity-1.1.0
127 value: 360
128 unit: deg
129 dec:
130 value: 45
131 unit: deg"""
132 truth = ICRS(ra=Longitude(25, unit=u.deg), dec=Latitude(45, unit=u.deg))
133
134 buff = yaml_to_asdf(f"example: {example.strip()}", version="1.5.0")
135 with asdf.open(buff) as af:
136 assert_frame_equal(af["example"], truth)