| 1 |
import asdf |
| 2 |
import numpy as np |
| 3 |
import pytest |
| 4 |
from astropy import units as u |
| 5 |
from astropy.coordinates import Angle, EarthLocation, Latitude, Longitude, SkyCoord |
| 6 |
from astropy.utils.masked import Masked |
| 7 |
|
| 8 |
from asdf_astropy.tests.versions import ASTROPY_GE_7_1 |
| 9 |
|
| 10 |
if ASTROPY_GE_7_1: |
| 11 |
from astropy.utils.masked import get_data_and_mask |
| 12 |
else: |
| 13 |
pytest.skip(reason="MaskedQuantity support was added in astropy 7.1", allow_module_level=True) |
| 14 |
|
| 15 |
|
| 16 |
@pytest.mark.parametrize("angle_class", [Angle, Latitude, Longitude]) |
| 17 |
def test_masked_angle(tmp_path, angle_class): |
| 18 |
angle = [1, 2, 3] |
| 19 |
mask = [False, False, True] |
| 20 |
masked_class = Masked(angle_class) |
| 21 |
file_path = tmp_path / "test.asdf" |
| 22 |
with asdf.AsdfFile() as af: |
| 23 |
af["angle"] = Masked(angle_class(angle * u.deg), mask) |
| 24 |
af.write_to(file_path) |
| 25 |
|
| 26 |
with asdf.open(file_path) as af: |
| 27 |
assert isinstance(af["angle"], masked_class) |
| 28 |
out_data, out_mask = get_data_and_mask(af["angle"].to_value(u.deg)) |
| 29 |
np.testing.assert_array_equal(out_data, angle) |
| 30 |
np.testing.assert_array_equal(out_mask, mask) |
| 31 |
|
| 32 |
|
| 33 |
def test_masked_skycoord(tmp_path): |
| 34 |
ra_deg = [0.0, 1.0, 2.0] |
| 35 |
dec_deg = [2.0, 1.0, 0.0] |
| 36 |
mask = [False, False, True] |
| 37 |
file_path = tmp_path / "test.asdf" |
| 38 |
with asdf.AsdfFile() as af: |
| 39 |
af["coord"] = SkyCoord(Masked(ra_deg, mask), Masked(dec_deg, mask), unit=u.deg) |
| 40 |
af.write_to(file_path) |
| 41 |
|
| 42 |
with asdf.open(file_path) as af: |
| 43 |
assert af["coord"].ra.unit == u.deg |
| 44 |
assert af["coord"].dec.unit == u.deg |
| 45 |
|
| 46 |
out_data, out_mask = get_data_and_mask(af["coord"].ra.deg) |
| 47 |
np.testing.assert_array_equal(out_data, ra_deg) |
| 48 |
np.testing.assert_array_equal(out_mask, mask) |
| 49 |
out_data, out_mask = get_data_and_mask(af["coord"].dec.deg) |
| 50 |
np.testing.assert_array_equal(out_data, dec_deg) |
| 51 |
np.testing.assert_array_equal(out_mask, mask) |
| 52 |
|
| 53 |
|
| 54 |
def test_masked_earth_location(tmp_path): |
| 55 |
x = [0.0, 1.0, 2.0] |
| 56 |
y = [2.0, 1.0, 0.0] |
| 57 |
z = [1.0, 2.0, 0.0] |
| 58 |
mask = [False, False, True] |
| 59 |
file_path = tmp_path / "test.asdf" |
| 60 |
with asdf.AsdfFile() as af: |
| 61 |
af["loc"] = EarthLocation.from_geocentric(Masked(x * u.m, mask), Masked(y * u.m, mask), Masked(z * u.m, mask)) |
| 62 |
af.write_to(file_path) |
| 63 |
|
| 64 |
with asdf.open(file_path) as af: |
| 65 |
assert af["loc"].x.unit == u.m |
| 66 |
assert af["loc"].y.unit == u.m |
| 67 |
assert af["loc"].z.unit == u.m |
| 68 |
|
| 69 |
out_data, out_mask = get_data_and_mask(af["loc"].x.to_value(u.m)) |
| 70 |
np.testing.assert_array_equal(out_data, x) |
| 71 |
np.testing.assert_array_equal(out_mask, mask) |
| 72 |
out_data, out_mask = get_data_and_mask(af["loc"].y.to_value(u.m)) |
| 73 |
np.testing.assert_array_equal(out_data, y) |
| 74 |
np.testing.assert_array_equal(out_mask, mask) |
| 75 |
out_data, out_mask = get_data_and_mask(af["loc"].z.to_value(u.m)) |
| 76 |
np.testing.assert_array_equal(out_data, z) |
| 77 |
np.testing.assert_array_equal(out_mask, mask) |