'This is an internal implementation of the `scale` method, which
also supports handling BLANK properly.
TODO: This is only needed for fixing #3865 without introducing any
public API changes.  We should support BLANK better when rescaling
data, and when that is added the need for this internal interface
should go away.
Note: the default of ``blank=0`` merely reflects the current behavior,
and is not necessarily a deliberate choice (better would be to disallow
conversion of floats to ints without specifying a BLANK if there are
NaN/inf values).'

def _scale_internal(self, type=None, option='old', bscale=None, bzero=None, blank=0):

    if (self.data is None):
        return
    if (type is None):
        type = BITPIX2DTYPE[self._bitpix]
    _type = getattr(np, type)
    if ((bscale is not None) and (bzero is not None)):
        _scale = bscale
        _zero = bzero
    elif (bscale is not None):
        _scale = bscale
        _zero = 0
    elif (bzero is not None):
        _scale = 1
        _zero = bzero
    elif ((option == 'old') and (self._orig_bscale is not None) and (self._orig_bzero is not None)):
        _scale = self._orig_bscale
        _zero = self._orig_bzero
    elif ((option == 'minmax') and (not issubclass(_type, np.floating))):
        min = np.minimum.reduce(self.data.flat)
        max = np.maximum.reduce(self.data.flat)
        if (_type == np.uint8):
            _zero = min
            _scale = ((max - min) / ((2.0 ** 8) - 1))
        else:
            _zero = ((max + min) / 2.0)
            nbytes = (8 * _type().itemsize)
            _scale = ((max - min) / ((2.0 ** nbytes) - 2))
    else:
        _scale = 1
        _zero = 0
    if (_zero != 0):
        np.add(self.data, (- _zero), out=self.data, casting='unsafe')
        self._header['BZERO'] = _zero
    else:
        try:
            del self._header['BZERO']
        except KeyError:
            pass
    if (_scale and (_scale != 1)):
        self.data = (self.data / _scale)
        self._header['BSCALE'] = _scale
    else:
        try:
            del self._header['BSCALE']
        except KeyError:
            pass
    if ((blank is not None) and issubclass(_type, np.integer)):
        self.data[np.isnan(self.data)] = blank
        self._header['BLANK'] = blank
    if (self.data.dtype.type != _type):
        self.data = np.array(np.around(self.data), dtype=_type)
    self._bitpix = DTYPE2BITPIX[self.data.dtype.name]
    self._bzero = self._header.get('BZERO', 0)
    self._bscale = self._header.get('BSCALE', 1)
    self._blank = blank
    self._header['BITPIX'] = self._bitpix
    self._orig_bitpix = self._bitpix
    self._orig_bzero = self._bzero
    self._orig_bscale = self._bscale
    self._orig_blank = self._blank
