| 1 |
import asdf |
| 2 |
import numpy as np |
| 3 |
import pytest |
| 4 |
from asdf.testing.helpers import yaml_to_asdf |
| 5 |
from astropy.io import fits |
| 6 |
from numpy.testing import assert_array_equal |
| 7 |
|
| 8 |
from asdf_astropy.testing.helpers import assert_hdu_list_equal |
| 9 |
|
| 10 |
|
| 11 |
def create_hduls(): |
| 12 |
hdul = fits.HDUList() |
| 13 |
header = fits.Header([("FOO", "BAR", "BAZ"), ("SOMENUM", "11.0"), ("EMPTY",)]) |
| 14 |
hdul.append(fits.PrimaryHDU(header=header)) |
| 15 |
hdul.append(fits.ImageHDU(data=np.arange(100))) |
| 16 |
|
| 17 |
hdul_with_table = fits.HDUList() |
| 18 |
hdul_with_table.append(fits.BinTableHDU.from_columns(np.array([(0, 1), (2, 3)], dtype=[("A", int), ("B", int)]))) |
| 19 |
|
| 20 |
return [hdul, hdul_with_table] |
| 21 |
|
| 22 |
|
| 23 |
@pytest.mark.parametrize("hdul", create_hduls()) |
| 24 |
def test_serialization(hdul, tmp_path): |
| 25 |
file_path = tmp_path / "test.asdf" |
| 26 |
with asdf.AsdfFile() as af: |
| 27 |
af["hdul"] = hdul |
| 28 |
af.write_to(file_path) |
| 29 |
|
| 30 |
with asdf.open(file_path) as af: |
| 31 |
assert_hdu_list_equal(af["hdul"], hdul) |
| 32 |
|
| 33 |
|
| 34 |
def test_asdf_tag(): |
| 35 |
yaml = """ |
| 36 |
hdul: !fits/fits-1.0.0 |
| 37 |
- header: |
| 38 |
- [SIMPLE, true, conforms to FITS standard] |
| 39 |
- [BITPIX, 8, array data type] |
| 40 |
- [NAXIS, 0, number of array dimensions] |
| 41 |
- [EXTEND, true] |
| 42 |
- [] |
| 43 |
- ['', Top Level MIRI Metadata] |
| 44 |
- [] |
| 45 |
- [DATE, '2013-08-30T10:49:55.070373', The date this file was created (UTC)] |
| 46 |
- [FILENAME, MiriDarkReferenceModel_test.fits, The name of the file] |
| 47 |
- [TELESCOP, JWST, The telescope used to acquire the data] |
| 48 |
- [] |
| 49 |
- ['', Information about the observation] |
| 50 |
- [] |
| 51 |
- [DATE-OBS, '2013-08-30T10:49:55.000000', The date the observation was made (UTC)] |
| 52 |
- data: !core/ndarray-1.0.0 |
| 53 |
data: [2, 3, 3, 4] |
| 54 |
datatype: float32 |
| 55 |
shape: [4] |
| 56 |
header: |
| 57 |
- [XTENSION, IMAGE, Image extension] |
| 58 |
- [BITPIX, -32, array data type] |
| 59 |
- [NAXIS, 4, number of array dimensions] |
| 60 |
- [NAXIS1, 4] |
| 61 |
- [NAXIS2, 3] |
| 62 |
- [NAXIS3, 3] |
| 63 |
- [NAXIS4, 2] |
| 64 |
- [PCOUNT, 0, number of parameters] |
| 65 |
- [GCOUNT, 1, number of groups] |
| 66 |
- [EXTNAME, SCI, extension name] |
| 67 |
- [BUNIT, DN, Units of the data array] |
| 68 |
- data: !core/ndarray-1.0.0 |
| 69 |
data: [5, 6, 7, 8] |
| 70 |
datatype: int64 |
| 71 |
shape: [4] |
| 72 |
header: |
| 73 |
- [XTENSION, IMAGE, Image extension] |
| 74 |
- [BITPIX, -32, array data type] |
| 75 |
- [NAXIS, 4, number of array dimensions] |
| 76 |
- [NAXIS1, 4] |
| 77 |
- [NAXIS2, 3] |
| 78 |
- [NAXIS3, 3] |
| 79 |
- [NAXIS4, 2] |
| 80 |
- [PCOUNT, 0, number of parameters] |
| 81 |
- [GCOUNT, 1, number of groups] |
| 82 |
- [EXTNAME, ERR, extension name] |
| 83 |
- [BUNIT, DN, Units of the error array] |
| 84 |
""" |
| 85 |
|
| 86 |
buff = yaml_to_asdf(yaml, version="1.5.0") |
| 87 |
with asdf.open(buff) as af: |
| 88 |
hdul = af["hdul"] |
| 89 |
assert len(hdul) == 3 # noqa: PLR2004 |
| 90 |
assert hdul[0].header["FILENAME"] == "MiriDarkReferenceModel_test.fits" |
| 91 |
assert hdul[0].data is None |
| 92 |
assert hdul[1].header["EXTNAME"] == "SCI" |
| 93 |
assert_array_equal(hdul[1].data, np.array([2, 3, 3, 4], dtype=np.float32)) |
| 94 |
assert hdul[2].header["EXTNAME"] == "ERR" |
| 95 |
assert_array_equal(hdul[2].data, np.array([5, 6, 7, 8], dtype=np.int64)) |