| 1 |
import asdf |
| 2 |
import numpy as np |
| 3 |
import pytest |
| 4 |
from astropy import units as u |
| 5 |
from astropy.utils.masked import Masked |
| 6 |
|
| 7 |
from asdf_astropy.tests.versions import ASTROPY_GE_7_1 |
| 8 |
|
| 9 |
if ASTROPY_GE_7_1: |
| 10 |
from astropy.utils.masked import get_data_and_mask |
| 11 |
else: |
| 12 |
pytest.skip(reason="MaskedQuantity support was added in astropy 7.1", allow_module_level=True) |
| 13 |
|
| 14 |
MaskedQuantity = Masked(u.Quantity) |
| 15 |
|
| 16 |
|
| 17 |
def test_masked_quantity(tmp_path): |
| 18 |
data = [1, 2, 3] |
| 19 |
mask = [False, False, True] |
| 20 |
file_path = tmp_path / "test.asdf" |
| 21 |
with asdf.AsdfFile() as af: |
| 22 |
af["quantity"] = Masked(data, mask) * u.yottamole |
| 23 |
af.write_to(file_path) |
| 24 |
|
| 25 |
with asdf.open(file_path) as af: |
| 26 |
assert isinstance(af["quantity"], MaskedQuantity) |
| 27 |
assert af["quantity"].unit == u.yottamole |
| 28 |
|
| 29 |
result_data, result_mask = get_data_and_mask(af["quantity"].value) |
| 30 |
np.testing.assert_array_equal(result_data, data) |
| 31 |
np.testing.assert_array_equal(result_mask, mask) |