'Generate an `astropy.io.fits.Header` object with the basic WCS
and SIP information stored in this object.  This should be
logically identical to the input FITS file, but it will be
normalized in a number of ways.
.. warning::
This function does not write out FITS WCS `distortion
paper`_ information, since that requires multiple FITS
header data units.  To get a full representation of
everything in this object, use `to_fits`.
Parameters
relax : bool or int, optional
Degree of permissiveness:
- `False` (default): Write all extensions that are
considered to be safe and recommended.
- `True`: Write all recognized informal extensions of the
WCS standard.
- `int`: a bit field selecting specific extensions to
write.  See :ref:`relaxwrite` for details.
If the ``relax`` keyword argument is not given and any
keywords were omitted from the output, an
`~astropy.utils.exceptions.AstropyWarning` is displayed.
To override this, explicitly pass a value to ``relax``.
key : str
The name of a particular WCS transform to use.  This may be
either ``\' \'`` or ``\'A\'``-``\'Z\'`` and corresponds to the ``"a"``
part of the ``CTYPEia`` cards.
Returns
header : `astropy.io.fits.Header`
Notes
The output header will almost certainly differ from the input in a
number of respects:
1. The output header only contains WCS-related keywords.  In
particular, it does not contain syntactically-required
keywords such as ``SIMPLE``, ``NAXIS``, ``BITPIX``, or
``END``.
2. Deprecated (e.g. ``CROTAn``) or non-standard usage will
be translated to standard (this is partially dependent on
whether ``fix`` was applied).
3. Quantities will be converted to the units used internally,
basically SI with the addition of degrees.
4. Floating-point quantities may be given to a different decimal
precision.
5. Elements of the ``PCi_j`` matrix will be written if and
only if they differ from the unit matrix.  Thus, if the
matrix is unity then no elements will be written.
6. Additional keywords such as ``WCSAXES``, ``CUNITia``,
``LONPOLEa`` and ``LATPOLEa`` may appear.
7. The original keycomments will be lost, although
`to_header` tries hard to write meaningful comments.
8. Keyword order may be changed.'

def to_header(self, relax=None, key=None):

    precision = WCSHDO_P14
    display_warning = False
    if (relax is None):
        display_warning = True
        relax = False
    if (relax not in (True, False)):
        do_sip = (relax & WCSHDO_SIP)
        relax &= (~ WCSHDO_SIP)
    else:
        do_sip = relax
        relax = (WCSHDO_all if (relax is True) else WCSHDO_safe)
    relax = (precision | relax)
    if (self.wcs is not None):
        if (key is not None):
            orig_key = self.wcs.alt
            self.wcs.alt = key
        header_string = self.wcs.to_header(relax)
        header = fits.Header.fromstring(header_string)
        keys_to_remove = [u'', u'    ', u'COMMENT']
        for kw in keys_to_remove:
            if (kw in header):
                del header[kw]
    else:
        header = fits.Header()
    if (do_sip and (self.sip is not None)):
        if ((self.wcs is not None) and any(((not ctyp.endswith(u'-SIP')) for ctyp in self.wcs.ctype))):
            self._fix_ctype(header, add_sip=True)
        for (kw, val) in self._write_sip_kw().items():
            header[kw] = val
    if ((not do_sip) and (self.wcs is not None) and any(self.wcs.ctype) and (self.sip is not None)):
        header = self._fix_ctype(header, add_sip=False)
    if display_warning:
        full_header = self.to_header(relax=True, key=key)
        missing_keys = []
        for (kw, val) in full_header.items():
            if (kw not in header):
                missing_keys.append(kw)
        if len(missing_keys):
            warnings.warn(u'Some    non-standard    WCS    keywords    were    excluded:    {0}    Use    the    ``relax``    kwarg    to    control    this.'.format(u',    '.join(missing_keys)), AstropyWarning)
        if (any(self.wcs.ctype) and (self.sip is not None)):
            header = self._fix_ctype(header, add_sip=False, log_message=False)
    if (key is not None):
        self.wcs.alt = orig_key
    return header
