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
|
import os
import pyfits
#from .. wcsutil import HSTWCS
from updatewcs.wcsutil import HSTWCS
#from .. mappings import allowed_corrections
from updatewcs import utils
import corrections, makewcs
import dgeo, det2im
from pytools import parseinput, fileutil
import apply_corrections
#Note: The order of corrections is important
__docformat__ = 'restructuredtext'
__version__ = '0.3'
def updatewcs(input, vacorr=True, tddcorr=True, dgeocorr=True, checkfiles=True, d2imcorr=True):
"""
Purpose
=======
Applies corrections to the WCS keywords.
Example
=======
>>>from hstwcs import updatewcs
>>>updatewcs.updatewcs(filename)
Dependencies
============
`pytools`
`pyfits`
`pywcs`
:Parameters:
`input`: a python list of file names or a string (wild card characters allowed)
input files may be in fits, geis or waiver fits format
`vacorr`: boolean
If True, vecocity aberration correction will be applied
`tddcorr`: boolean
If True, time dependent distortion correction will be applied
`checkfiles`: boolean
If True, the format of the input files will be checked,
geis and waiver fits files will be converted to MEF format.
Default value is True for standalone mode.
"""
files = parseinput.parseinput(input)[0]
if checkfiles:
files = checkFiles(files)
if not files:
print 'No valid input, quitting ...\n'
return
for f in files:
acorr = apply_corrections.setCorrections(f, vacorr=vacorr, \
tddcorr=tddcorr,dgeocorr=dgeocorr, d2imcorr=d2imcorr)
#restore the original WCS keywords
utils.restoreWCS(f)
makecorr(f, acorr)
return files
def makecorr(fname, allowed_corr):
"""
Purpose
=======
Applies corrections to the WCS of a single file
:Parameters:
`fname`: string
file name
`acorr`: list
list of corrections to be applied
"""
f = pyfits.open(fname, mode='update')
#Determine the reference chip and create the reference HSTWCS object
nrefchip, nrefext = getNrefchip(f)
ref_wcs = HSTWCS(fobj=f, ext=nrefext)
ref_wcs.readModel(update=True,header=f[nrefext].header)
utils.write_archive(f[nrefext].header)
if 'DET2IMCorr' in allowed_corr:
det2im.DET2IMCorr.updateWCS(f)
for i in range(len(f))[1:]:
# Perhaps all ext headers should be corrected (to be consistent)
extn = f[i]
if extn.header.has_key('extname') and extn.header['extname'].lower() == 'sci':
ref_wcs.restore(f[nrefext].header)
hdr = extn.header
utils.write_archive(hdr)
ext_wcs = HSTWCS(fobj=f, ext=i)
ext_wcs.readModel(update=True,header=hdr)
for c in allowed_corr:
if c != 'DGEOCorr' and c != 'DET2IMCorr':
corr_klass = corrections.__getattribute__(c)
kw2update = corr_klass.updateWCS(ext_wcs, ref_wcs)
for kw in kw2update:
hdr.update(kw, kw2update[kw])
if 'DGEOCorr' in allowed_corr:
kw2update = dgeo.DGEOCorr.updateWCS(f)
for kw in kw2update:
f[1].header.update(kw, kw2update[kw])
f.close()
def getNrefchip(fobj):
"""
This handles the fact that WFPC2 subarray observations
may not include chip 3 which is the default reference chip for
full observations. Also for subarrays chip 3 may not be the third
extension in a MEF file.
"""
Nrefext = 1
instrument = fobj[0].header['INSTRUME']
if instrument == 'WFPC2':
detectors = [img.header['DETECTOR'] for img in fobj[1:]]
if 3 not in detectors:
Nrefchip=detectors[0]
Nrefext = 1
else:
Nrefchip = 3
Nrefext = detectors.index(3) + 1
elif instrument == 'ACS':
detector = fobj[0].header['DETECTOR']
if detector == 'WCS':
Nrefchip =2
else:
Nrefchip = 1
elif instrument == 'NICMOS':
Nrefchip = fobj[0].header['CAMERA']
elif instrument == 'WFC3':
detector = fobj[0].header['DETECTOR']
if detector == 'UVIS':
Nrefchip =2
else:
Nrefchip = 1
else:
Nrefchip = 1
return Nrefchip, Nrefext
def checkFiles(input):
"""
Purpose
=======
Checks that input files are in the correct format.
Converts geis and waiver fits files to multiextension fits.
"""
from pytools.check_files import geis2mef, waiver2mef, checkFiles
removed_files = []
newfiles = []
for file in input:
try:
imgfits,imgtype = fileutil.isFits(file)
except IOError:
print "Warning: File %s could not be found\n" %file
print "Removing file %s from input list" %file
removed_files.append(file)
continue
# Check for existence of waiver FITS input, and quit if found.
# Or should we print a warning and continue but not use that file
if imgfits:
if imgtype == 'waiver':
newfilename = waiver2mef(file, convert_dq=True)
if newfilename == None:
print "Removing file %s from input list - could not convert waiver to mef" %file
removed_files.append(file)
else:
newfiles.append(newfilename)
else:
newfiles.append(file)
# If a GEIS image is provided as input, create a new MEF file with
# a name generated using 'buildFITSName()'
# Convert the corresponding data quality file if present
if not imgfits:
newfilename = geis2mef(file, convert_dq=True)
if newfilename == None:
print "Removing file %s from input list - could not convert geis to mef" %file
removed_files.append(file)
else:
newfiles.append(newfilename)
if removed_files:
print 'The following files will be removed from the list of files to be processed :\n'
for f in removed_files:
print f
newfiles = checkFiles(newfiles)[0]
return newfiles
|