'Scale image data by using ``BSCALE`` and ``BZERO``.
Calling this method will scale ``self.data`` and update the keywords of
``BSCALE`` and ``BZERO`` in ``self._header`` and ``self._image_header``.
This method should only be used right before writing to the output
file, as the data will be scaled and is therefore not very usable after
the call.
Parameters
type : str, optional
destination data type, use a string representing a numpy dtype
name, (e.g. ``\'uint8\'``, ``\'int16\'``, ``\'float32\'`` etc.).  If is
`None`, use the current data type.
option : str, optional
how to scale the data: if ``"old"``, use the original ``BSCALE``
and ``BZERO`` values when the data was read/created. If
``"minmax"``, use the minimum and maximum of the data to scale.
The option will be overwritten by any user-specified bscale/bzero
values.
bscale, bzero : int, optional
user specified ``BSCALE`` and ``BZERO`` values.'

def scale(self, type=None, option='old', bscale=1, bzero=0):

    if (self.data is None):
        return
    if (type is None):
        type = BITPIX2DTYPE[self._bitpix]
    _type = getattr(np, type)
    if ((bscale != 1) or (bzero != 0)):
        _scale = bscale
        _zero = bzero
    elif (option == 'old'):
        _scale = self._orig_bscale
        _zero = self._orig_bzero
    elif (option == 'minmax'):
        if isinstance(_type, np.floating):
            _scale = 1
            _zero = 0
        else:
            _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)
                _scale = ((_max - _min) / ((2.0 ** (8 * _type.bytes)) - 2))
    if (_zero != 0):
        np.subtract(self.data, _zero, out=self.data, casting='unsafe')
        self.header['BZERO'] = _zero
    else:
        for header in (self.header, self._header):
            with suppress(KeyError):
                del header['BZERO']
    if (_scale != 1):
        self.data /= _scale
        self.header['BSCALE'] = _scale
    else:
        for header in (self.header, self._header):
            with suppress(KeyError):
                del header['BSCALE']
    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.header['BITPIX'] = self._bitpix
    self._update_header_data(self.header)
    self._orig_bitpix = self._bitpix
    self._orig_bzero = self._bzero
    self._orig_bscale = self._bscale
