diff --git a/CHANGES.rst b/CHANGES.rst index 783c65c54..24df41925 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -19,6 +19,9 @@ ``tag_to_schema_resolver`` property for ``AsdfFile`` and ``AsdfExtensionList``. [#399] +- Fix bug that caused serialized FITS tables to be duplicated in embedded ASDF + HDU. [#411] + 1.3.2 (unreleased) ------------------ diff --git a/asdf/fits_embed.py b/asdf/fits_embed.py index c20a5bf03..e2b0353dd 100644 --- a/asdf/fits_embed.py +++ b/asdf/fits_embed.py @@ -95,7 +95,9 @@ def find_or_create_block_for_array(self, arr, ctx): if not isinstance(arr, ndarray.NDArrayType): base = util.get_array_base(arr) for hdu in self._hdulist: - if base is hdu.data: + if hdu.data is None: + continue + if base is util.get_array_base(hdu.data): return _FitsBlock(hdu) return super( diff --git a/asdf/tests/test_fits_embed.py b/asdf/tests/test_fits_embed.py index 6ddd2c0bd..337a21b23 100644 --- a/asdf/tests/test_fits_embed.py +++ b/asdf/tests/test_fits_embed.py @@ -13,6 +13,7 @@ astropy = pytest.importorskip('astropy') from astropy.io import fits +from astropy.table import Table from astropy.tests.helper import catch_warnings from .. import asdf @@ -259,3 +260,21 @@ def test_version_mismatch_file(): with fits_embed.AsdfInFits.open(testfile) as fits_handle: assert fits_handle.tree['a'] == complex(0j) assert len(w) == 0, display_warnings(w) + +def test_serialize_table(tmpdir): + tmpfile = str(tmpdir.join('table.fits')) + + data = np.random.random((10, 10)) + table = Table(data) + + hdu = fits.BinTableHDU(table) + hdulist = fits.HDUList() + hdulist.append(hdu) + + tree = {'my_table': hdulist[1].data} + with fits_embed.AsdfInFits(hdulist, tree) as ff: + ff.write_to(tmpfile) + + with asdf.AsdfFile.open(tmpfile) as ff: + data = ff.tree['my_table'] + assert data._source.startswith('fits:')