Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FITS objects created with fromstring have problems? #2710

Merged
merged 1 commit into from Sep 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -260,6 +260,10 @@ Bug Fixes

- ``astropy.io.fits``

- Fixed a crash when reading scaled float data out of a FITS file that was
loaded from a string (using ``HDUList.fromfile``) rather than from a file.
[#2710]

- Fixed a crash when reading data from an HDU whose header contained in
invalid value for the BLANK keyword (eg. a string value instead of an
integer as required by the FITS Standard). Invalid BLANK keywords are now
Expand Down
5 changes: 4 additions & 1 deletion astropy/io/fits/hdu/image.py
Expand Up @@ -616,7 +616,10 @@ def _get_scaled_image_data(self, offset, shape):
if new_dtype is not None:
data = np.array(raw_data, dtype=new_dtype)
else: # floating point cases
if self._file.memmap:
if self._file is not None and self._file.memmap:
data = raw_data.copy()
elif not raw_data.flags.writeable:
# create a writeable copy if needed
data = raw_data.copy()
# if not memmap, use the space already in memory
else:
Expand Down
18 changes: 18 additions & 0 deletions astropy/io/fits/tests/test_image.py
Expand Up @@ -809,6 +809,24 @@ def test_invalid_blank(self):
msg = "Invalid 'BLANK' keyword"
assert msg in str(w[1].message)

def test_scaled_image_fromfile(self):
"""
Regression test for https://github.com/astropy/astropy/issues/2710
"""

# Make some sample data
a = np.arange(100, dtype=np.float32)

hdu = fits.PrimaryHDU(data=a.copy())
hdu.scale(bscale=1.1)
hdu.writeto(self.temp('test.fits'))

with open(self.temp('test.fits'), 'rb') as f:
file_data = f.read()

hdul = fits.HDUList.fromstring(file_data)
assert np.allclose(hdul[0].data, a)


class TestCompressedImage(FitsTestCase):
def test_empty(self):
Expand Down