summaryrefslogtreecommitdiff
path: root/wcsutil/__init__.py
blob: d21a2ecd047413ce9c71c4cc02420f8af66cf191 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#from .. pywcs.sip import SIP
from pywcs import WCS, DistortionLookupTable
import pyfits
import instruments
#from .. distortion import models
from updatewcs.distortion import models
import numpy as np
from pytools import fileutil
from pytools.fileutil import DEGTORAD, RADTODEG

#from .. mappings import inst_mappings, ins_spec_kw, DEGTORAD, RADTODEG, basic_wcs
import mappings
from mappings import inst_mappings, ins_spec_kw
from mappings import basic_wcs, prim_hdr_kw


__docformat__ = 'restructuredtext'
__version__ = '0.3'


class HSTWCS(WCS):
    """
    Purpose
    =======
    Create a WCS object based on the instrument.
    It has all basic WCS kw as attribbutes (set by pywcs).
    It also uses the primary and extension header to define 
    instrument specific attributes.
    """
    
    def __init__(self, fobj=None, ext=None, instrument=None, detector=None, minerr=0.0):
        """
        :Parameters:
        `fobj`: string or PyFITS HDUList object or None
                a file name, e.g j9irw4b1q_flt.fits
                a fully qualified filename[EXTNAME,EXTNUM], e.g. j9irw4b1q_flt.fits[sci,1]
                a pyfits file object, e.g pyfits.open('j9irw4b1q_flt.fits')
        `ext`:  int or None
                extension number
                if ext==None, it is assumed the data is in the primary hdu
        `instrument`: string
                one of 'ACS', 'NICMOS', 'WFPC2', 'STIS', 'WFC3' 
                Used only to define a default HSTWCS object, when fobj==None
        `detector`:  string
                for example 'WFC'
                If instrument and detector parameters are given, a default HSTWCS 
                instrument is created. Used only with fobj==None
        `minerr`: float
                minimum value a distortion correction must have in order to be applied. 
                If CPERRja, CQERRja are smaller than minerr, the corersponding 
                distortion is not applied.
        """
        
        self.inst_kw = ins_spec_kw
        self.minerr = minerr
        if instrument == None:
            filename, hdr0, ehdr, phdu = self.parseInput(f=fobj, ext=ext)
            self.filename = filename
            self.setHDR0kw(hdr0, ehdr)
            WCS.__init__(self, ehdr, fobj=phdu, minerr=minerr)
            # If input was a pyfits HDUList object, it's the user's 
            # responsibility to close it, otherwise, it's closed here.
            if not isinstance(fobj, pyfits.HDUList):
                phdu.close()
                
            self.setInstrSpecKw(hdr0, ehdr)
            self.setPscale()
            self.setOrient()
            self.readIDCCoeffs(ehdr)
            extname = ehdr.get('EXTNAME', "")
            extnum = ehdr.get('EXTVER', None)
            self.extname = (extname, extnum)
        else:
            # create a default HSTWCS object based on 
            #instrument and detector parameters
            self.instrument = instrument
            self.detector = detector
            self.setInstrSpecKw()
        
        
    def parseInput(self, f=None, ext=None):
        if isinstance(f, str):
            # create an HSTWCS object from a filename
            
            if ext != None:
                filename = f
                extnum = ext
            elif ext == None:
                filename, extname = fileutil.parseFilename(f)
                if extname == None:
                    #data may be in the primary array
                    extnum = 0
                else:
                    extnum = fileutil.parseExtn(extname)
            phdu = pyfits.open(filename)
            hdr0 = pyfits.getheader(filename)
            try:
                ehdr = pyfits.getheader(filename, ext=extnum)
            except IndexError:
                print 'Unable to get extension %d. /n' % ext
            
        elif isinstance(f, pyfits.HDUList):
            phdu = f
            if ext == None:
                extnum = 0
            else:
                extnum = ext
            ehdr = f[extnum].header
            hdr0 = f[0].header    
            filename = hdr0.get('FILENAME', "")
        
        return filename, hdr0, ehdr, phdu
            
    def setHDR0kw(self, primhdr, ehdr):
        # Set attributes from kw defined in the primary header.
        self.instrument = primhdr.get('INSTRUME', None)
        self.offtab = primhdr.get('OFFTAB', None) 
        self.idctab = primhdr.get('IDCTAB', None)
        self.date_obs = primhdr.get('DATE-OBS', None)
        self.ra_targ = primhdr.get('RA_TARG', None)
        self.dec_targ = primhdr.get('DEC_TARG', None)
        self.det2imfile = primhdr.get('D2IMFILE', None)
        self.det2imext = primhdr.get('D2IMEXT', None)
        self.axiscorr = primhdr.get('AXISCORR', None)
        self.d2imerr = primhdr.get('D2IMERR', 0.0)
        try:
            self.pav3 = primhdr['PA_V3']
        
        except KeyError:
            print 'Keyword PA_V3 not found in primary header.'
            print 'This is typical for some old files. Please retrieve the files from the archive again.'
            #print 'Quitting ...'
            #raise
        
    
    
    def readIDCCoeffs(self, header):
        """
        Reads in first order IDCTAB coefficients if present in the header
        """
        coeffs = ['ocx10', 'ocx11', 'ocy10', 'ocy11', 'idcscale']
        for c in coeffs:
            self.__setattr__(c, header.get(c, None))
            
    def setInstrSpecKw(self, prim_hdr=None, ext_hdr=None):
        # Based on the instrument kw creates an instalnce of an instrument WCS class
        # and sets attributes from instrument specific kw 
        if self.instrument in inst_mappings.keys():
            inst_kl = inst_mappings[self.instrument]
            inst_kl = instruments.__dict__[inst_kl]
            insobj = inst_kl(prim_hdr, ext_hdr)
            for key in self.inst_kw:
                try:
                    self.__setattr__(key, insobj.__getattribute__(key))
                except AttributeError:
                    # Some of the instrument's attributes are recorded in the primary header and
                    # were already set, (e.g. 'DETECTOR'), the code below is a check for that case.
                    if not self.__getattribute__(key):
                        raise
                    else:
                        pass
        else:
            raise KeyError, "Unsupported instrument - %s" %self.instrument
    
    def setPscale(self):
        # Calculates the plate scale from the cd matrix
       
        cd11 = self.wcs.cd[0][0]
        cd21 = self.wcs.cd[1][0]
        self.pscale = np.sqrt(np.power(cd11,2)+np.power(cd21,2)) * 3600.
    
    def setOrient(self):
        # Recompute ORIENTAT
        cd12 = self.wcs.cd[0][1]
        cd22 = self.wcs.cd[1][1]
        self.orientat = RADTODEG(np.arctan2(cd12,cd22))
    
    def updatePscale(self, pscale):
        """Given a plate scale, update the CD matrix"""
        old_pscale = self.pscale
        self.pscale = pscale
        self.wcs.cd = self.wcs.cd * pscale/old_pscale
        self.naxis1 = self.naxis1 * old_pscale/ pscale 
        self.naxis2 = self.naxis2 * old_pscale/ pscale 
        self.wcs.crpix = self.wcs.crpix *old_pscale/pscale
        
    def updateOrient(self, orient):
        """Given n angle update the CD matrix"""
        if self.orientat == orient:
            return
        old_orient = self.orientat
        self.orientat = orient
        angle = fileutil.DEGTORAD(orient)
        cd11 = -np.cos(angle)
        cd12 = np.sin(angle)
        cd21 = cd12
        cd22 = -cd11
        cdmat = np.array([[cd11, cd12],[cd21,cd22]])
        self.wcs.cd = cdmat * self.pscale/3600
            
            
    def readModel(self, update=False, header=None):
        """
        Purpose
        =======
        Read distortion model from idc table.
        Save some of the information as kw needed for interpreting the distortion
        If header is provided and update is True, some IDC model kw 
        will be recorded in the header.
        """
        if self.idctab == None or self.date_obs == None:
            print 'idctab or date_obs not available\n'
            self.idcmodel = None
            return
        if self.filter1 ==None and self.filter2 == None:
            'No filter information available\n'
            self.idcmodel = None
            return
        
        self.idcmodel = models.IDCModel(self.idctab,
                    chip=self.chip, direction='forward', date=self.date_obs,
                    filter1=self.filter1, filter2=self.filter2, 
                    offtab=self.offtab, binned=self.binned)
        
        if update:
            if header==None:
                print 'Update header with IDC model kw requested but header was not provided\n.'
                return
            else:
                self.updatehdr(header)
    """
    The next four methods overwrite the corresponding PyWCS methods
    If detector to image correction is specified in the primary science
    header and a correction extension exists (D2IMEXT keyword) then 
    this correction is applied before the corrsponding PyWCS method is called.
    """
    def all_pix2sky(self, *args, **kwargs):
        origin = self.get_origin(*args)
        if self.det2imext != None and self.d2imerr > self.minerr:
            return WCS.all_pix2sky(self, self.det2im(*args),origin )
        else:
            return WCS.all_pix2sky(self, *args)
    all_pix2sky.__doc__ = """
        Applies detector to image correction before it does the 
        following pixel transformations:
        
        %s
        """ % (WCS.all_pix2sky.__doc__)
        
    def pix2foc(self, *args, **kwargs):
        origin = self.get_origin(*args)
        if self.det2imext != None and self.d2imerr > self.minerr:
            return WCS.pix2foc(self, self.det2im(*args), origin)
        else:
            return WCS.pix2foc(self, *args)
        
    pix2foc.__doc__ =  """
        Applies detector to image correction before it does the 
        following pixel transformations:
        
        %s
        """ % (WCS.pix2foc.__doc__)
           
    def p4_pix2foc(self, *args, **kwargs):
        origin = self.get_origin(*args)
        if self.det2imext != None and self.d2imerr > self.minerr:
            return WCS.p4_pix2foc(self, self.det2im(*args), origin)
        else:
            return WCS.p4_pix2foc(self, *args)
    
    p4_pix2foc.__doc__ =  """
        Applies detector to image correction before it does the 
        following pixel transformations:
        
        %s
        """ % (WCS.p4_pix2foc.__doc__)
        
    def sip_pix2foc(self, *args, **kwargs):
        origin = self.get_origin(*args)
        if self.det2imext != None and self.d2imerr > self.minerr:
            return WCS.sip_pix2foc(self, self.det2im(*args), origin)
        else:
            return WCS.sip_pix2foc(self, *args)
    
    sip_pix2foc.__doc__ =  """
        Applies detector to image correction before it does the 
        following pixel transformations:
        
        %s
        """ % (WCS.sip_pix2foc.__doc__)
        
    def get_origin(self, *args):
        # Parse the arguments to get the origin of the 
        #transformation: 0 or 1
        if len(args) == 2:
            return args[1]
        elif len(args) == 3:
            return args[2]
        raise TypeError("Expected 2 or 3 arguments, %d given" % len(args))
        
    
   
    def det2im(self, *args):
        """
        Convert detector to image coordinates
        """
        cpdis = self.get_d2im_lookup()
        d2im_wcs = WCS()
        if self.axiscorr == 1:
            d2im_wcs.cpdis1 = cpdis
        else:
            d2im_wcs.cpdis2 = cpdis
        if len(args) == 2:
            xy, origin = args
            try:
                xy = np.asarray(xy)
                origin = int(origin)
            except:
                raise TypeError(
                    "When providing two arguments, they must be (xy, origin)")
            return d2im_wcs.p4_pix2foc(xy,origin)
        elif len(args) == 3:
            x, y, origin = args
            try:
                x = np.asarray(x)
                y = np.asarray(y)
                origin = int(origin)
            except:
                raise TypeError(
                    "When providing three arguments, they must be (x, y, origin)")
            if len(x) != len(y):
                raise ValueError("x and y arrays are not the same size")
            length = len(x)
            xy = np.hstack((x.reshape((length, 1)),
                            y.reshape((length, 1))))
     
            img = d2im_wcs.p4_pix2foc(xy,origin)
        
            return [img[:, i] for i in range(img.shape[1])]
    
    def get_d2im_lookup(self):
        """
        Create a paper IV type lookup table from a reference file
        """
        d2im_data = pyfits.getdata(self.filename, ext=('D2IMARR', 1))
        d2im_data = np.array([d2im_data])
        d2im_hdr = pyfits.getheader(self.filename, ext=('D2IMARR', 1))
        
        crpix = (d2im_hdr['CRPIX1'],d2im_hdr['CRPIX2'])
        crval = (d2im_hdr['CRVAL1'],d2im_hdr['CRVAL2'])
        cdelt = (d2im_hdr['CDELT1'],d2im_hdr['CDELT2'])
        
        return DistortionLookupTable(d2im_data, crpix, crval, cdelt)
    
    def restore(self, header=None):
        """
        Restore a WCS archive in memory and update the WCS object.
        Restored are the basic WCS kw as well as pscale and orient.
        """
        from pywcs import Wcsprm
        backup = {}
        if header == None:
            print 'Need a valid header in order to restore archive\n'
            return 
        
        for k in basic_wcs:
            try:
                nkw = ('O'+k)[:7]
                backup[k] = header[nkw]
            except KeyError:
                pass
        if backup == {}:
            print 'No archive was found\n'
            return
        cdl=pyfits.CardList()
        for item in backup.items():
            card = pyfits.Card(key=item[0], value=item[1])
            cdl.append(card)
            
        h = pyfits.Header(cdl)
        wprm = Wcsprm("".join([str(x) for x in h.ascardlist()]))
        self.wcs = wprm
        self.setPscale()
        self.setOrient()
        
    def updatehdr(self, ext_hdr, newkeywords=None):
        #kw2add : OCX10, OCX11, OCY10, OCY11 
        # record the model in the header for use by pydrizzle
        ext_hdr.update('OCX10', self.idcmodel.cx[1,0])
        ext_hdr.update('OCX11', self.idcmodel.cx[1,1])
        ext_hdr.update('OCY10', self.idcmodel.cy[1,0])
        ext_hdr.update('OCY11', self.idcmodel.cy[1,1])
        ext_hdr.update('IDCSCALE', self.idcmodel.refpix['PSCALE'])
        ext_hdr.update('IDCTHETA', self.idcmodel.refpix['THETA'])
        ext_hdr.update('IDCXREF', self.idcmodel.refpix['XREF'])
        ext_hdr.update('IDCYREF', self.idcmodel.refpix['YREF'])
        ext_hdr.update('IDCV2REF', self.idcmodel.refpix['V2REF'])
        ext_hdr.update('IDCV3REF', self.idcmodel.refpix['V3REF'])
        
    
def help():
    print 'How to create an HSTWCS object:\n\n'
    print """ \
    1. Using a pyfits HDUList object and an extension number \n
    Example:\n
    
    fobj = pyfits.open('some_file.fits')\n
    w = wcsutil.HSTWCS(fobj, 3)\n\n
    
    2. Create an HSTWCS object using a qualified file name. \n
    Example:\n
    w = wcsutil.HSTWCS('j9irw4b1q_flt.fits[sci,1]')\n\n
    
    3. Create an HSTWCS object using a file name and an extension number. \n
    Example:\n
    w = wcsutil.HSTWCS('j9irw4b1q_flt.fits', ext=2)\n\n
    
    4. Create a template HSTWCS object for an instrument/detector combination.]n
    Example:\n
    w = wcsutil.HSTWCS(instrument='ACS', detector='WFC'\n\n
    """