def remove_distortion_keywords(hdr):
    """
    Remove WCS distortion related keywords from the input header
    """
    distortion_kws = ['TDDALPHA', 'TDDBETA', 'D2IMEXT', 'D2IMERR',
        'DGEOEXT', 'NPOLEXT']
    for kw in distortion_kws:
        if kw in hdr:
            try:
                del hdr[kw]
            except KeyError:
                pass
    if 'ctype1' in hdr and hdr['ctype1'].find('SIP') > -1:
        hdr['ctype1'] = hdr['ctype1'][:-4]
        hdr['ctype2'] = hdr['ctype2'][:-4]
    for k in list(hdr.items()):
        if k[0][:2] in ['A_', 'B_'] or k[0][:3] in ['IDC', 'SCD'] and k[0
            ] != 'IDCTAB' or k[0][:6] in ['SCTYPE', 'SCRVAL', 'SNAXIS',
            'SCRPIX']:
            try:
                del hdr[k[0]]
            except KeyError:
                pass
    for k in list(hdr.items()):
        if k[0][:2] == 'DP':
            try:
                del hdr[k[0] + '*']
                del hdr[k[0] + '.*']
                del hdr[k[0] + '.*.*']
            except:
                print(
                    "ERROR (bleandheaders.remove_distortion_keywords) trying to delete '{:s}' in the header."
                    .format(k[0] + '*'))
                pass
        if k[0][:2] == 'CP':
            try:
                del hdr[k[0]]
            except KeyError:
                pass
