| 1 |
import asdf |
| 2 |
import astropy.units as u |
| 3 |
import numpy as np |
| 4 |
import pytest |
| 5 |
from astropy.coordinates import FK4, ICRS, Galactic, Longitude, SkyCoord |
| 6 |
|
| 7 |
from asdf_astropy.testing.helpers import assert_sky_coord_equal |
| 8 |
|
| 9 |
|
| 10 |
def create_sky_coords(): |
| 11 |
# These are cribbed directly from the Examples section of |
| 12 |
# https://docs.astropy.org/en/stable/api/astropy.coordinates.SkyCoord.html |
| 13 |
return [ |
| 14 |
# Defaults to ICRS frame |
| 15 |
SkyCoord(10, 20, unit="deg"), |
| 16 |
# Vector of 3 coords |
| 17 |
SkyCoord([1, 2, 3], [-30, 45, 8], frame="icrs", unit="deg"), |
| 18 |
# FK4 frame |
| 19 |
SkyCoord( |
| 20 |
["1:12:43.2 +1:12:43", "1 12 43.2 +1 12 43"], |
| 21 |
frame=FK4, |
| 22 |
unit=(u.deg, u.hourangle), |
| 23 |
obstime="J1992.21", |
| 24 |
), |
| 25 |
# Galactic frame |
| 26 |
SkyCoord("1h12m43.2s +1d12m43s", frame=Galactic), |
| 27 |
SkyCoord(frame="galactic", l="1h12m43.2s", b="+1d12m43s"), |
| 28 |
# With ra and dec |
| 29 |
SkyCoord( |
| 30 |
Longitude([1, 2, 3], unit=u.deg), |
| 31 |
np.array([4.5, 5.2, 6.3]) * u.deg, |
| 32 |
frame="icrs", |
| 33 |
), |
| 34 |
SkyCoord( |
| 35 |
frame=ICRS, |
| 36 |
ra=Longitude([1, 2, 3], unit=u.deg), |
| 37 |
dec=np.array([4.5, 5.2, 6.3]) * u.deg, |
| 38 |
obstime="2001-01-02T12:34:56", |
| 39 |
), |
| 40 |
# With overridden frame defaults |
| 41 |
SkyCoord(FK4(1 * u.deg, 2 * u.deg), obstime="J2010.11", equinox="B1965"), |
| 42 |
# Cartesian |
| 43 |
SkyCoord(w=0, u=1, v=2, unit="kpc", frame="galactic", representation_type="cartesian"), |
| 44 |
# Vector frame |
| 45 |
SkyCoord([ICRS(ra=1 * u.deg, dec=2 * u.deg), ICRS(ra=3 * u.deg, dec=4 * u.deg)]), |
| 46 |
# 2D obstime |
| 47 |
SkyCoord([1, 2], [3, 4], [5, 6], unit="deg,deg,m", frame="fk4", obstime=["J1990.5", "J1991.5"]), |
| 48 |
# Radial velocity |
| 49 |
SkyCoord(ra=1 * u.deg, dec=2 * u.deg, radial_velocity=10 * u.km / u.s), |
| 50 |
# Proper motion |
| 51 |
SkyCoord(ra=1 * u.deg, dec=2 * u.deg, pm_ra_cosdec=2 * u.mas / u.yr, pm_dec=1 * u.mas / u.yr), |
| 52 |
] |
| 53 |
|
| 54 |
|
| 55 |
@pytest.mark.parametrize("coord", create_sky_coords()) |
| 56 |
def test_serialization(coord, tmp_path): |
| 57 |
file_path = tmp_path / "test.asdf" |
| 58 |
|
| 59 |
with asdf.AsdfFile() as af: |
| 60 |
af["coord"] = coord |
| 61 |
af.write_to(file_path) |
| 62 |
|
| 63 |
with asdf.open(file_path) as af: |
| 64 |
assert_sky_coord_equal(af["coord"], coord) |
| 65 |
|
| 66 |
|
| 67 |
def test_serializaton_extra_attribute(tmp_path): |
| 68 |
coord = SkyCoord(10 * u.deg, 20 * u.deg, equinox="2011-01-01T00:00", frame="fk4").transform_to("icrs") |
| 69 |
|
| 70 |
file_path = tmp_path / "test.asdf" |
| 71 |
|
| 72 |
with asdf.AsdfFile() as af: |
| 73 |
af["coord"] = coord |
| 74 |
af.write_to(file_path) |
| 75 |
|
| 76 |
with asdf.open(file_path) as af: |
| 77 |
assert_sky_coord_equal(af["coord"], coord) |
| 78 |
assert hasattr(af["coord"], "equinox") |