summaryrefslogtreecommitdiff
path: root/lib/stwcs/wcsutil/convertwcs.py
blob: a384eb19a1841efb65a5b0fa964345bc4794b064 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from __future__ import print_function
try:
    import stwcs
    from stwcs import wcsutil
except:
    stwcs = None

from stsci.tools import fileutil

OPUS_WCSKEYS = ['OCRVAL1','OCRVAL2','OCRPIX1','OCRPIX2',
                'OCD1_1','OCD1_2','OCD2_1','OCD2_2',
                'OCTYPE1','OCTYPE2']


def archive_prefix_OPUS_WCS(fobj,extname='SCI'):
    """ Identifies WCS keywords which were generated by OPUS and archived
        using a prefix of 'O' for all 'SCI' extensions in the file

        Parameters
        ----------
        fobj : str or `astropy.io.fits.HDUList`
            Filename or fits object of a file

    """
    if stwcs is None:
        print('=====================')
        print('The STWCS package is needed to convert an old-style OPUS WCS to an alternate WCS')
        print('=====================')
        raise ImportError


    closefits = False
    if isinstance(fobj,str):
        # A filename was provided as input
        fobj = fits.open(fobj,mode='update')
        closefits=True

    # Define the header
    ext = ('sci',1)
    hdr = fobj[ext].header

    numextn = fileutil.countExtn(fobj)
    extlist = []
    for e in range(1,numextn+1):
        extlist.append(('sci',e))

    # Insure that the 'O' alternate WCS is present
    if 'O' not in wcsutil.wcskeys(hdr):
        # if not, archive the Primary WCS as the default OPUS WCS
        wcsutil.archiveWCS(fobj,extlist, wcskey='O', wcsname='OPUS')

    # find out how many SCI extensions are in the image
    numextn = fileutil.countExtn(fobj,extname=extname)
    if numextn == 0:
        extname = 'PRIMARY'

    # create HSTWCS object from PRIMARY WCS
    wcsobj = wcsutil.HSTWCS(fobj,ext=ext,wcskey='O')
    # get list of WCS keywords
    wcskeys = list(wcsobj.wcs2header().keys())

    # For each SCI extension...
    for e in range(1,numextn+1):
        # Now, look for any WCS keywords with a prefix of 'O'
        for key in wcskeys:
            okey = 'O'+key[:7]
            hdr = fobj[(extname,e)].header
            if okey in hdr:
                # Update alternate WCS keyword with prefix-O OPUS keyword value
                hdr[key] = hdr[okey]

    if closefits:
        fobj.close()

def create_prefix_OPUS_WCS(fobj,extname='SCI'):
    """ Creates alternate WCS with a prefix of 'O' for OPUS generated WCS values
        to work with old MultiDrizzle.

        Parameters
        ----------
        fobj : str or `astropy.io.fits.HDUList`
            Filename or fits object of a file

        Raises
        ------
        IOError:
            if input FITS object was not opened in 'update' mode

    """
    # List of O-prefix keywords to create
    owcskeys = OPUS_WCSKEYS

    closefits = False
    if isinstance(fobj,str):
        # A filename was provided as input
        fobj = fits.open(fobj, mode='update')
        closefits=True
    else:
        # check to make sure this FITS obj has been opened in update mode
        if fobj.fileinfo(0)['filemode'] != 'update':
            print('File not opened with "mode=update". Quitting...')
            raise IOError

    # check for existance of O-prefix WCS
    if owcskeys[0] not in fobj['sci',1].header:

        # find out how many SCI extensions are in the image
        numextn = fileutil.countExtn(fobj,extname=extname)
        if numextn == 0:
            extname = ''
        for extn in range(1,numextn+1):
            hdr = fobj[(extname,extn)].header
            for okey in owcskeys:
                hdr[okey] = hdr[okey[1:]+'O']

    # Close FITS image if we had to open it...
    if closefits:
        fobj.close()