'Reads `SIP`_ header keywords and returns a `~astropy.wcs.Sip`
object.
If no `SIP`_ header keywords are found, ``None`` is returned.'

def _read_sip_kw(self, header, wcskey=u''):

    if isinstance(header, (six.text_type, six.binary_type)):
        return None
    if ((str(u'A_ORDER') in header) and (header[str(u'A_ORDER')] > 1)):
        if (str(u'B_ORDER') not in header):
            raise ValueError(u'A_ORDER    provided    without    corresponding    B_ORDER    keyword    for    SIP    distortion')
        m = int(header[str(u'A_ORDER')])
        a = np.zeros(((m + 1), (m + 1)), np.double)
        for i in range((m + 1)):
            for j in range(((m - i) + 1)):
                key = str(u'A_{0}_{1}').format(i, j)
                if (key in header):
                    a[(i, j)] = header[key]
                    del header[key]
        m = int(header[str(u'B_ORDER')])
        if (m > 1):
            b = np.zeros(((m + 1), (m + 1)), np.double)
            for i in range((m + 1)):
                for j in range(((m - i) + 1)):
                    key = str(u'B_{0}_{1}').format(i, j)
                    if (key in header):
                        b[(i, j)] = header[key]
                        del header[key]
        else:
            a = None
            b = None
        del header[str(u'A_ORDER')]
        del header[str(u'B_ORDER')]
        ctype = [header[u'CTYPE{0}{1}'.format(nax, wcskey)] for nax in range(1, (self.naxis + 1))]
        if any(((not ctyp.endswith(u'-SIP')) for ctyp in ctype)):
            message = u'\n                                                                Inconsistent    SIP    distortion    information    is    present    in    the    FITS    header    and    the    WCS    object:\n                                                                SIP    coefficients    were    detected,    but    CTYPE    is    missing    a    "-SIP"    suffix.\n                                                                astropy.wcs    is    using    the    SIP    distortion    coefficients,\n                                                                therefore    the    coordinates    calculated    here    might    be    incorrect.\n\n                                                                If    you    do    not    want    to    apply    the    SIP    distortion    coefficients,\n                                                                please    remove    the    SIP    coefficients    from    the    FITS    header    or    the\n                                                                WCS    object.        As    an    example,    if    the    image    is    already    distortion-corrected\n                                                                (e.g.,    drizzled)    then    distortion    components    should    not    apply    and    the    SIP\n                                                                coefficients    should    be    removed.\n\n                                                                While    the    SIP    distortion    coefficients    are    being    applied    here,    if    that    was    indeed    the    intent,\n                                                                for    consistency    please    append    "-SIP"    to    the    CTYPE    in    the    FITS    header    or    the    WCS    object.\n\n                                                                '
            log.info(message)
    elif ((str(u'B_ORDER') in header) and (header[str(u'B_ORDER')] > 1)):
        raise ValueError((u'B_ORDER    provided    without    corresponding    A_ORDER    ' + u'keyword    for    SIP    distortion'))
    else:
        a = None
        b = None
    if ((str(u'AP_ORDER') in header) and (header[str(u'AP_ORDER')] > 1)):
        if (str(u'BP_ORDER') not in header):
            raise ValueError(u'AP_ORDER    provided    without    corresponding    BP_ORDER    keyword    for    SIP    distortion')
        m = int(header[str(u'AP_ORDER')])
        ap = np.zeros(((m + 1), (m + 1)), np.double)
        for i in range((m + 1)):
            for j in range(((m - i) + 1)):
                key = str(u'AP_{0}_{1}').format(i, j)
                if (key in header):
                    ap[(i, j)] = header[key]
                    del header[key]
        m = int(header[str(u'BP_ORDER')])
        if (m > 1):
            bp = np.zeros(((m + 1), (m + 1)), np.double)
            for i in range((m + 1)):
                for j in range(((m - i) + 1)):
                    key = str(u'BP_{0}_{1}').format(i, j)
                    if (key in header):
                        bp[(i, j)] = header[key]
                        del header[key]
        else:
            ap = None
            bp = None
        del header[str(u'AP_ORDER')]
        del header[str(u'BP_ORDER')]
    elif ((str(u'BP_ORDER') in header) and (header[str(u'BP_ORDER')] > 1)):
        raise ValueError(u'BP_ORDER    provided    without    corresponding    AP_ORDER    keyword    for    SIP    distortion')
    else:
        ap = None
        bp = None
    if ((a is None) and (b is None) and (ap is None) and (bp is None)):
        return None
    if ((str(u'CRPIX1{0}'.format(wcskey)) not in header) or (str(u'CRPIX2{0}'.format(wcskey)) not in header)):
        raise ValueError(u'Header    has    SIP    keywords    without    CRPIX    keywords')
    crpix1 = header.get(u'CRPIX1{0}'.format(wcskey))
    crpix2 = header.get(u'CRPIX2{0}'.format(wcskey))
    return Sip(a, b, ap, bp, (crpix1, crpix2))
