'Creates an HDUList object from a CCDData object.
Parameters
hdu_mask, hdu_uncertainty, hdu_flags : str or None, optional
If it is a string append this attribute to the HDUList as
`~astropy.io.fits.ImageHDU` with the string as extension name.
Flags are not supported at this time. If ``None`` this attribute
is not appended.
Default is ``\'MASK\'`` for mask, ``\'UNCERT\'`` for uncertainty and
``None`` for flags.
wcs_relax : bool
Value of the ``relax`` parameter to use in converting the WCS to a
FITS header using `~astropy.wcs.WCS.to_header`. The common
``CTYPE`` ``RA---TAN-SIP`` and ``DEC--TAN-SIP`` requires
``relax=True`` for the ``-SIP`` part of the ``CTYPE`` to be
preserved.
Raises
ValueError
- If ``self.mask`` is set but not a `numpy.ndarray`.
- If ``self.uncertainty`` is set but not a
`~astropy.nddata.StdDevUncertainty`.
- If ``self.uncertainty`` is set but has another unit then
``self.data``.
NotImplementedError
Saving flags is not supported.
Returns
hdulist : `~astropy.io.fits.HDUList`'

def to_hdu(self, hdu_mask=u'MASK', hdu_uncertainty=u'UNCERT', hdu_flags=None, wcs_relax=True):

    if isinstance(self.header, fits.Header):
        header = self.header.copy()
    else:
        dummy_ccd = CCDData([1], meta=fits.Header(), unit=u'adu')
        for (k, v) in self.header.items():
            dummy_ccd._insert_in_metadata_fits_safe(k, v)
        header = dummy_ccd.header
    if (self.unit is not u.dimensionless_unscaled):
        header[u'bunit'] = self.unit.to_string()
    if self.wcs:
        wcs_header = self.wcs.to_header(relax=wcs_relax)
        header.extend(wcs_header, useblanks=False, update=True)
    hdus = [fits.PrimaryHDU(self.data, header)]
    if (hdu_mask and (self.mask is not None)):
        if (not hasattr(self.mask, u'shape')):
            raise ValueError(u'only    a    numpy.ndarray    mask    can    be    saved.')
        hduMask = fits.ImageHDU(self.mask.astype(np.uint8), name=hdu_mask)
        hdus.append(hduMask)
    if (hdu_uncertainty and (self.uncertainty is not None)):
        if (self.uncertainty.__class__.__name__ != u'StdDevUncertainty'):
            raise ValueError(u'only    StdDevUncertainty    can    be    saved.')
        if (hasattr(self.uncertainty, u'unit') and (self.uncertainty.unit is not None) and (self.uncertainty.unit != self.unit)):
            raise ValueError(u'saving    uncertainties    with    a    unit    differingfrom    the    data    unit    is    not    supported.')
        hduUncert = fits.ImageHDU(self.uncertainty.array, name=hdu_uncertainty)
        hdus.append(hduUncert)
    if (hdu_flags and self.flags):
        raise NotImplementedError(u'adding    the    flags    to    a    HDU    is    not    supported    at    this    time.')
    hdulist = fits.HDUList(hdus)
    return hdulist
