Skip to content

Commit c4842c0

Browse files
committed
Fix writing non contiguous data to a compressed HDU.
Fixes astropy#2150 and astropy#9955.
1 parent f2d14e1 commit c4842c0

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎CHANGES.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ astropy.io.fits
357357
- Fix ``fitsdiff`` when its arguments are directories that contain other
358358
directories. [#9711]
359359

360+
- Fix writing non contiguous data to a compressed HDU. [#9958]
361+
360362
astropy.io.registry
361363
^^^^^^^^^^^^^^^^^^^
362364

‎astropy/io/fits/hdu/compressed.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,8 @@ def _update_compressed_data(self):
16541654
# First delete the original compressed data, if it exists
16551655
del self.compressed_data
16561656

1657+
self.data = np.ascontiguousarray(self.data)
1658+
16571659
# Compress the data.
16581660
# The current implementation of compress_hdu assumes the empty
16591661
# compressed data table has already been initialized in

‎astropy/io/fits/tests/test_image.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,31 @@ def test_compressed_integers(self, dtype):
17901790
new = fits.getdata(testfile)
17911791
np.testing.assert_array_equal(data, new)
17921792

1793+
def test_write_non_contiguous_data(self):
1794+
"""
1795+
Regression test for https://github.com/astropy/astropy/issues/2150
1796+
"""
1797+
orig = np.arange(100, dtype=float).reshape((10, 10), order='f')
1798+
primary = fits.PrimaryHDU()
1799+
hdu = fits.CompImageHDU(orig)
1800+
hdulist = fits.HDUList([primary, hdu])
1801+
hdulist.writeto(self.temp('test.fits'))
1802+
1803+
actual = fits.getdata(self.temp('test.fits'))
1804+
assert_equal(orig, actual)
1805+
1806+
def test_slice_and_write_comp_hdu(self):
1807+
"""
1808+
Regression test for https://github.com/astropy/astropy/issues/9955
1809+
"""
1810+
with fits.open(self.data('comp.fits')) as hdul:
1811+
hdul[1].data = hdul[1].data[:200, :100]
1812+
hdul[1].writeto(self.temp('test.fits'))
1813+
1814+
with fits.open(self.data('comp.fits')) as hdul1:
1815+
with fits.open(self.temp('test.fits')) as hdul2:
1816+
assert_equal(hdul1[1].data[:200, :100], hdul2[1].data)
1817+
17931818

17941819
def test_comphdu_bscale(tmpdir):
17951820
"""

0 commit comments

Comments
 (0)