Skip to content

Commit 70f616b

Browse files
committed
Fix creating CompImageHDU from header with bscale/bzero
Fix astropy#5999
1 parent 5c2259b commit 70f616b

3 files changed

Lines changed: 11 additions & 18 deletions

File tree

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,6 @@ def __init__(
325325
else:
326326
# Create at least a skeleton HDU that matches the input
327327
# header and data (if any were input)
328-
329-
if header is not None:
330-
bscale = header.get("BSCALE")
331-
bzero = header.get("BZERO")
332-
simple = header.get("SIMPLE")
333-
334328
super().__init__(
335329
data=data,
336330
header=header or Header(),
@@ -340,15 +334,8 @@ def __init__(
340334
scale_back=scale_back,
341335
)
342336

343-
if header is not None:
344-
if bscale is not None:
345-
self.header["BSCALE"] = bscale
346-
347-
if bzero is not None:
348-
self.header["BZERO"] = bzero
349-
350-
if simple is not None:
351-
self.header["SIMPLE"] = simple
337+
if header is not None and "SIMPLE" in header:
338+
self.header["SIMPLE"] = header["SIMPLE"]
352339

353340
self.compression_type = compression_type
354341
self.tile_shape = _validate_tile_shape(

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ def test_open_scaled_in_update_mode_compressed(self):
239239
# it
240240
with fits.open(self.data("scale.fits"), do_not_scale_image_data=True) as hdul:
241241
chdu = fits.CompImageHDU(data=hdul[0].data, header=hdul[0].header)
242+
chdu.header["BZERO"] = hdul[0].header["BZERO"]
243+
chdu.header["BSCALE"] = hdul[0].header["BSCALE"]
242244
chdu.writeto(self.temp("scale.fits"))
243245
mtime = os.stat(self.temp("scale.fits")).st_mtime
244246

@@ -354,6 +356,8 @@ def test_scale_back_compressed(self):
354356
# Create a compressed version of the scaled image
355357
with fits.open(self.data("scale.fits"), do_not_scale_image_data=True) as hdul:
356358
chdu = fits.CompImageHDU(data=hdul[0].data, header=hdul[0].header)
359+
chdu.header["BZERO"] = hdul[0].header["BZERO"]
360+
chdu.header["BSCALE"] = hdul[0].header["BSCALE"]
357361
chdu.writeto(self.temp("scale.fits"))
358362

359363
with fits.open(self.temp("scale.fits"), mode="update", scale_back=True) as hdul:
@@ -1005,11 +1009,11 @@ def setup_method(self, tmp_path):
10051009
)
10061010

10071011
header2 = fits.Header()
1008-
header2["BSCALE"] = 2
1009-
header2["BZERO"] = 100
10101012
hdu2 = fits.CompImageHDU(
10111013
self.data, header2, compression_type="RICE_1", tile_shape=(5, 4, 5)
10121014
)
1015+
hdu2.header["BSCALE"] = 2
1016+
hdu2.header["BZERO"] = 100
10131017
hdulist = fits.HDUList([fits.PrimaryHDU(), hdu1, hdu2])
10141018
hdulist.writeto(tmp_path / "sections.fits")
10151019

@@ -1126,7 +1130,6 @@ def test_uint_option(tmp_path):
11261130
assert_allclose(hdulist[1].data, data)
11271131

11281132

1129-
@pytest.mark.xfail
11301133
def test_incorrect_bzero(tmp_path):
11311134
"""
11321135
Regression test for https://github.com/astropy/astropy/issues/5999 which is
@@ -1158,6 +1161,7 @@ def test_incorrect_bzero(tmp_path):
11581161
for hdu in hdulist_read:
11591162
assert hdu.header.get("BZERO") is None
11601163
assert hdu.header.get("BSCALE") is None
1164+
np.testing.assert_array_equal(hdulist_read[1].data, hdulist_read[2].data)
11611165

11621166

11631167
def test_custom_extname():
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix creating CompImageHDU from header with BSCALE/BZERO: keywords are now
2+
ignored, as done in ImageHDU.

0 commit comments

Comments
 (0)