@@ -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
0 commit comments