Skip to content

Commit 3651203

Browse files
committed
Delete scaling keywords from header when unsigned int data converted to another type
Closes astropy#4974
1 parent 079eed9 commit 3651203

2 files changed

Lines changed: 139 additions & 5 deletions

File tree

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

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,35 @@ def __init__(self, data=None, header=None, do_not_scale_image_data=False,
9999
self._uint = uint
100100
self._scale_back = scale_back
101101

102+
# Keep track of whether BZERO/BSCALE were set from the header so that
103+
# values for self._orig_bzero and self._orig_bscale can be set
104+
# properly, if necessary, once the data has been set.
105+
bzero_in_header = 'BZERO' in self._header
106+
bscale_in_header = 'BSCALE' in self._header
102107
self._bzero = self._header.get('BZERO', 0)
103108
self._bscale = self._header.get('BSCALE', 1)
104109

105110
# Save off other important values from the header needed to interpret
106111
# the image data
107112
self._axes = [self._header.get('NAXIS' + str(axis + 1), 0)
108113
for axis in range(self._header.get('NAXIS', 0))]
109-
self._bitpix = self._header.get('BITPIX', 8)
114+
115+
# Not supplying a default for BITPIX makes sense because BITPIX
116+
# is either in the header or should be determined from the dtype of
117+
# the data (which occurs when the data is set).
118+
self._bitpix = self._header.get('BITPIX')
110119
self._gcount = self._header.get('GCOUNT', 1)
111120
self._pcount = self._header.get('PCOUNT', 0)
112121
self._blank = None if ignore_blank else self._header.get('BLANK')
113122
self._verify_blank()
114123

115124
self._orig_bitpix = self._bitpix
125+
self._orig_blank = self._header.get('BLANK')
126+
127+
# These get set again below, but need to be set to sensible defaults
128+
# here.
116129
self._orig_bzero = self._bzero
117130
self._orig_bscale = self._bscale
118-
self._orig_blank = self._header.get('BLANK')
119131

120132
# Set the name attribute if it was provided (if this is an ImageHDU
121133
# this will result in setting the EXTNAME keyword of the header as
@@ -135,9 +147,31 @@ def __init__(self, data=None, header=None, do_not_scale_image_data=False,
135147
self._data_needs_rescale = True
136148
return
137149
else:
150+
# Setting data will set set _bitpix, _bzero, and _bscale to the
151+
# appropriate BITPIX for the data, and always sets _bzero=0 and
152+
# _bscale=1.
138153
self.data = data
139154
self.update_header()
140155

156+
# Check again for BITPIX/BSCALE/BZERO in case they changed when the
157+
# data was assigned. This can happen, for example, if the input
158+
# data is an unsigned int numpy array.
159+
self._bitpix = self._header.get('BITPIX')
160+
161+
# Do not provide default values for BZERO and BSCALE here because
162+
# the keywords will have been deleted in the header if appropriate
163+
# after scaling. We do not want to put them back in if they
164+
# should not be there.
165+
self._bzero = self._header.get('BZERO')
166+
self._bscale = self._header.get('BSCALE')
167+
168+
# Handle case where there was no BZERO/BSCALE in the initial header
169+
# but there should be a BSCALE/BZERO now that the data has been set.
170+
if not bzero_in_header:
171+
self._orig_bzero = self._bzero
172+
if bscale_in_header:
173+
self._orig_bscale = self._bscale
174+
141175
@classmethod
142176
def match_header(cls, header):
143177
"""
@@ -217,8 +251,10 @@ def data(self, data):
217251
return
218252
else:
219253
self._data_replaced = True
254+
was_unsigned = _is_pseudo_unsigned(self.__dict__['data'].dtype)
220255
else:
221256
self._data_replaced = True
257+
was_unsigned = False
222258

223259
if data is not None and not isinstance(data, np.ndarray):
224260
# Try to coerce the data into a numpy array--this will work, on
@@ -233,10 +269,11 @@ def data(self, data):
233269
self._modified = True
234270

235271
if isinstance(data, np.ndarray):
272+
# Set new values of bitpix, bzero, and bscale now, but wait to
273+
# revise original values until header is updated.
236274
self._bitpix = DTYPE2BITPIX[data.dtype.name]
237-
self._orig_bitpix = self._bitpix
238-
self._bscale = self._orig_bscale = 1
239-
self._bzero = self._orig_bzero = 0
275+
self._bscale = 1
276+
self._bzero = 0
240277
self._blank = None
241278
self._axes = list(data.shape)
242279
self._axes.reverse()
@@ -245,7 +282,26 @@ def data(self, data):
245282
else:
246283
raise ValueError('not a valid data array')
247284

285+
# Update the header, including adding BZERO/BSCALE if new data is
286+
# unsigned. Does not change the values of self._bitpix,
287+
# self._orig_bitpix, etc.
248288
self.update_header()
289+
if (data is not None and was_unsigned):
290+
self._update_header_scale_info(data.dtype)
291+
292+
# Keep _orig_bitpix as it was until header update is done, then
293+
# set it, to allow easier handling of the case of unsigned
294+
# integer data being converted to something else. Setting these here
295+
# is needed only for the case do_not_scale_image_data=True when
296+
# setting the data to unsigned int.
297+
298+
# If necessary during initialization, i.e. if BSCALE and BZERO were
299+
# not in the header but the data was unsigned, the attributes below
300+
# will be update in __init__.
301+
self._orig_bitpix = self._bitpix
302+
self._orig_bscale = self._bscale
303+
self._orig_bzero = self._bzero
304+
249305

250306
# returning the data signals to lazyproperty that we've already handled
251307
# setting self.__dict__['data']
@@ -313,11 +369,30 @@ def update_header(self):
313369
if 'BLANK' in self._header:
314370
self._blank = self._header['BLANK']
315371

372+
# Add BSCALE/BZERO to header if data is unsigned int.
316373
self._update_uint_scale_keywords()
317374

318375
self._modified = False
319376

320377
def _update_header_scale_info(self, dtype=None):
378+
"""
379+
Delete BSCALE/BZERO from header if necessary.
380+
"""
381+
382+
# Note that _dtype_for_bitpix determines the dtype based on the
383+
# "original" values of bitpix, bscale, and bzero, stored in
384+
# self._orig_bitpix, etc. It contains the logic for determining which
385+
# special cases of BZERO/BSCALE, if any, are auto-detected as following
386+
# the FITS unsigned int convention.
387+
388+
# Added original_was_unsigned with the intent of facilitating the
389+
# special case of do_not_scale_image_data=True and uint=True
390+
# eventually.
391+
if self._dtype_for_bitpix() is not None:
392+
original_was_unsigned = self._dtype_for_bitpix().kind == 'u'
393+
else:
394+
original_was_unsigned = False
395+
321396
if (self._do_not_scale_image_data or
322397
(self._orig_bzero == 0 and self._orig_bscale == 1)):
323398
return

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,65 @@ def test_uint_header_consistency(self):
627627
assert 'BZERO' in new_uint_hdu.header
628628
assert new_uint_hdu.header['BZERO'] == (2 ** (int_size - 1))
629629

630+
@pytest.mark.parametrize(('from_file'), (False, True))
631+
@pytest.mark.parametrize(('do_not_scale'), (False,))
632+
def test_uint_header_keywords_removed_after_bitpix_change(self,
633+
from_file,
634+
do_not_scale):
635+
"""
636+
Regression test for https://github.com/astropy/astropy/issues/4974
637+
638+
BZERO/BSCALE should be removed if data is converted to a floating
639+
point type.
640+
641+
Currently excluding the case where do_not_scale_image_data=True
642+
because it is not clear what the expectation should be.
643+
"""
644+
645+
arr = np.zeros(100, dtype='uint16')
646+
647+
if from_file:
648+
# To generate the proper input file we always want to scale the
649+
# data before writing it...otherwise when we open it will be
650+
# regular (signed) int data.
651+
tmp_uint = fits.PrimaryHDU(arr)
652+
filename = 'unsigned_int.fits'
653+
tmp_uint.writeto(self.temp(filename))
654+
f = fits.open(self.temp(filename),
655+
do_not_scale_image_data=do_not_scale)
656+
657+
uint_hdu = f[0]
658+
# Force a read before we close.
659+
_ = uint_hdu.data
660+
else:
661+
uint_hdu = fits.PrimaryHDU(arr,
662+
do_not_scale_image_data=do_not_scale)
663+
664+
# Make sure appropriate keywords are in the header. See
665+
# https://github.com/astropy/astropy/pull/3916#issuecomment-122414532
666+
# for discussion.
667+
assert 'BSCALE' in uint_hdu.header
668+
assert 'BZERO' in uint_hdu.header
669+
assert uint_hdu.header['BSCALE'] == 1
670+
assert uint_hdu.header['BZERO'] == 32768
671+
672+
# Convert data to floating point...
673+
uint_hdu.data = uint_hdu.data * 1.0
674+
675+
# ...bitpix should be negative.
676+
assert uint_hdu.header['BITPIX'] < 0
677+
678+
# BSCALE and BZERO should NOT be in header any more.
679+
# assert 'BSCALE' not in uint_hdu.header
680+
# assert 'BZERO' not in uint_hdu.header
681+
682+
# This is the main test...the data values should round trip
683+
# as zero.
684+
filename = 'test_uint_to_float.fits'
685+
uint_hdu.writeto(self.temp(filename))
686+
with fits.open(self.temp(filename)) as hdul:
687+
assert (hdul[0].data == 0).all()
688+
630689
def test_blanks(self):
631690
"""Test image data with blank spots in it (which should show up as
632691
NaNs in the data array.

0 commit comments

Comments
 (0)