Skip to content

Commit 7f5f133

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

3 files changed

Lines changed: 9 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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ def test_scale_back_compressed(self):
354354
# Create a compressed version of the scaled image
355355
with fits.open(self.data("scale.fits"), do_not_scale_image_data=True) as hdul:
356356
chdu = fits.CompImageHDU(data=hdul[0].data, header=hdul[0].header)
357+
chdu.header["BZERO"] = hdul[0].header["BZERO"]
358+
chdu.header["BSCALE"] = hdul[0].header["BSCALE"]
357359
chdu.writeto(self.temp("scale.fits"))
358360

359361
with fits.open(self.temp("scale.fits"), mode="update", scale_back=True) as hdul:
@@ -1005,11 +1007,11 @@ def setup_method(self, tmp_path):
10051007
)
10061008

10071009
header2 = fits.Header()
1008-
header2["BSCALE"] = 2
1009-
header2["BZERO"] = 100
10101010
hdu2 = fits.CompImageHDU(
10111011
self.data, header2, compression_type="RICE_1", tile_shape=(5, 4, 5)
10121012
)
1013+
hdu2.header["BSCALE"] = 2
1014+
hdu2.header["BZERO"] = 100
10131015
hdulist = fits.HDUList([fits.PrimaryHDU(), hdu1, hdu2])
10141016
hdulist.writeto(tmp_path / "sections.fits")
10151017

@@ -1126,7 +1128,6 @@ def test_uint_option(tmp_path):
11261128
assert_allclose(hdulist[1].data, data)
11271129

11281130

1129-
@pytest.mark.xfail
11301131
def test_incorrect_bzero(tmp_path):
11311132
"""
11321133
Regression test for https://github.com/astropy/astropy/issues/5999 which is
@@ -1158,6 +1159,7 @@ def test_incorrect_bzero(tmp_path):
11581159
for hdu in hdulist_read:
11591160
assert hdu.header.get("BZERO") is None
11601161
assert hdu.header.get("BSCALE") is None
1162+
np.testing.assert_array_equal(hdulist_read[1].data, hdulist_read[2].data)
11611163

11621164

11631165
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)