-
Notifications
You must be signed in to change notification settings - Fork 5
/
lnacoudetasks.py
297 lines (240 loc) · 8.58 KB
/
lnacoudetasks.py
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
#!/usr/bin/env python
#
# A script with tasks to reduce data from the Coude Spectrograph
# at the 1.6m telescope of the Observatorio do Pico dos Dias - Brazil
# Load Python standard modules
import glob
# Load third-party modules
from pyraf import iraf
def checkfile(filename):
'''Print statistics and run open imexamine task'''
iraf.imstatistics.unlearn()
iraf.imexamine.unlearn()
print 'Check output file:'
iraf.imstatistics(filename)
print ' Running "imexamine" task..'
iraf.imexamine(filename, 1)
def masterbias(biasre, output='Zero', combine='median', reject='minmax',
ccdtype='', rdnoise='rdnoise', gain='gain'):
'''run the task ccdred.zerocombine with chosen parameters
Input:
-------
str biasre: regular expression to identify zero level images
Output:
-------
file Zero.fits: combined zerolevel images
'''
biaslist = glob.glob(biasre)
biasstring = ', '.join(biaslist)
# load packages
iraf.imred()
iraf.ccdred()
# unlearn settings
iraf.imred.unlearn()
iraf.ccdred.unlearn()
iraf.ccdred.ccdproc.unlearn()
iraf.ccdred.combine.unlearn()
iraf.ccdred.zerocombine.unlearn()
iraf.ccdred.setinstrument.unlearn()
# setup task
iraf.ccdred.zerocombine.output = output
iraf.ccdred.zerocombine.combine = combine
iraf.ccdred.zerocombine.reject = reject
iraf.ccdred.zerocombine.ccdtype = ccdtype
iraf.ccdred.zerocombine.rdnoise = rdnoise
iraf.ccdred.zerocombine.gain = gain
# run task
iraf.ccdred.zerocombine(input=biasstring)
def masterflat(flatre, output='Flat', combine='median', reject='sigclip',
scale='mode', rdnoise='rdnoise', gain='gain'):
'''combine flat images with the task ccdred.flatcombine
Input:
-------
str: flatre - regular expression to bias files in the current directory
Output:
-------
file: Flat.fits - combined flat field images
'''
flatlist = glob.glob(flatre)
flatstring = ', '.join(flatlist)
# load packages
iraf.imred()
iraf.ccdred()
# unlearn settings
iraf.imred.unlearn()
iraf.ccdred.unlearn()
iraf.ccdred.ccdproc.unlearn()
iraf.ccdred.combine.unlearn()
iraf.ccdred.flatcombine.unlearn()
iraf.ccdred.setinstrument.unlearn()
# setup task
iraf.ccdred.flatcombine.output = output
iraf.ccdred.flatcombine.combine = combine
iraf.ccdred.flatcombine.reject = reject
iraf.ccdred.flatcombine.ccdtype = ''
iraf.ccdred.flatcombine.process = 'no'
iraf.ccdred.flatcombine.subsets = 'yes'
iraf.ccdred.flatcombine.scale = scale
iraf.ccdred.flatcombine.rdnoise = rdnoise
iraf.ccdred.flatcombine.gain = gain
iraf.ccdred.flatcombine(input=flatstring)
def subzero(imagesre, zero='Zero'):
'''Run ccdproc remove Zero level noise'''
imageslist = glob.glob(imagesre)
imagesin = ', '.join(imageslist)
# Load packages
iraf.imred()
iraf.ccdred()
# Unlearn previouse settings
iraf.ccdred.ccdproc.unlearn()
iraf.ccdred.combine.unlearn()
# setup and run task
iraf.ccdred.ccdproc.ccdtype = ''
iraf.ccdred.ccdproc.noproc = False
iraf.ccdred.ccdproc.fixpix = False
iraf.ccdred.ccdproc.overscan = False
iraf.ccdred.ccdproc.darkcor = False
iraf.ccdred.ccdproc.illumcor = False
iraf.ccdred.ccdproc.fringecor = False
iraf.ccdred.ccdproc.readcor = False
iraf.ccdred.ccdproc.scancor = False
iraf.ccdred.ccdproc.trim = False
iraf.ccdred.ccdproc.trimsec = ''
iraf.ccdred.ccdproc.readaxis = 'line'
iraf.ccdred.ccdproc.zerocor = True
iraf.ccdred.ccdproc.zero = zero
iraf.ccdred.ccdproc.flatcor = False
iraf.ccdred.ccdproc.flat = ''
iraf.ccdred.ccdproc(images=imagesin)
def divflat(imagesre, flat='Flat'):
'''Run ccdproc task to images'''
imageslist = glob.glob(imagesre)
imagesin = ', '.join(imageslist)
# Load packages
iraf.imred()
iraf.ccdred()
# Unlearn settings
iraf.ccdred.ccdproc.unlearn()
iraf.ccdred.combine.unlearn()
# Setup and run task
iraf.ccdred.ccdproc.ccdtype = ''
iraf.ccdred.ccdproc.noproc = False
iraf.ccdred.ccdproc.fixpix = False
iraf.ccdred.ccdproc.overscan = False
iraf.ccdred.ccdproc.darkcor = False
iraf.ccdred.ccdproc.illumcor = False
iraf.ccdred.ccdproc.fringecor = False
iraf.ccdred.ccdproc.readcor = False
iraf.ccdred.ccdproc.scancor = False
iraf.ccdred.ccdproc.trim = False
iraf.ccdred.ccdproc.trimsec = ''
iraf.ccdred.ccdproc.readaxis = 'line'
iraf.ccdred.ccdproc.zerocor = False
iraf.ccdred.ccdproc.zero = ''
iraf.ccdred.ccdproc.flatcor = True
iraf.ccdred.ccdproc.flat = flat
iraf.ccdred.ccdproc(images=imagesin)
def correctimages(imagesre, zero='Zero', flat='nFlat'):
'''Run ccdproc task to correct images'''
imageslist = glob.glob(imagesre)
imagesin = ', '.join(imageslist)
trimsection = str(raw_input('Enter trim section (or Hit <Enter>): '))
trimquery = True
if trimsection == '':
trimquery = False
# Load Packages
iraf.imred()
iraf.ccdred()
# Unlearn Settings
iraf.ccdred.ccdproc.unlearn()
iraf.ccdred.combine.unlearn()
# Setup and run task
iraf.ccdred.ccdproc.ccdtype = ''
iraf.ccdred.ccdproc.noproc = False
iraf.ccdred.ccdproc.fixpix = False
iraf.ccdred.ccdproc.overscan = False
iraf.ccdred.ccdproc.darkcor = False
iraf.ccdred.ccdproc.illumcor = False
iraf.ccdred.ccdproc.fringecor = False
iraf.ccdred.ccdproc.readcor = False
iraf.ccdred.ccdproc.scancor = False
iraf.ccdred.ccdproc.trim = trimquery
iraf.ccdred.ccdproc.trimsec = trimsection
iraf.ccdred.ccdproc.readaxis = 'line'
iraf.ccdred.ccdproc.zerocor = True
iraf.ccdred.ccdproc.zero = zero
iraf.ccdred.ccdproc.flatcor = True
iraf.ccdred.ccdproc.flat = flat
iraf.ccdred.ccdproc(images=imagesin)
def runapall(imagesre, gain='gain', rdnoise='rdnoise'):
'''Extract aperture spectra for science images ...'''
imageslist = glob.glob(imagesre)
imagesin = ', '.join(imageslist)
# load packages
iraf.imred()
iraf.ccdred()
iraf.specred()
# unlearn previous settings
iraf.ccdred.combine.unlearn()
iraf.ccdred.ccdproc.unlearn()
iraf.specred.apall.unlearn()
# setup and run task
iraf.specred.apall.format = 'onedspec'
iraf.specred.apall.readnoise = rdnoise
iraf.specred.apall.gain = gain
iraf.specred.apall(input=imagesin)
def flatresponse(input='Flat', output='nFlat'):
''' normalize Flat to correct illumination patterns'''
iraf.imred()
iraf.ccdred()
iraf.specred()
iraf.ccdred.combine.unlearn()
iraf.ccdred.ccdproc.unlearn()
iraf.specred.response.unlearn()
iraf.specred.response.interactive = True
iraf.specred.response.function = 'chebyshev'
iraf.specred.response.order = 1
iraf.specred.response(calibration=input, normalization=input,
response=output)
def flatresponse2(input='Flat_zero', output='nFlat'):
''' normalize Flat to correct illumination patterns'''
iraf.boxcar(input, 'Flatboxcar', 10, 10)
iraf.imarith(input, '/', 'Flatboxcar', output)
def scalewavelenght(calspec):
''' Creates a wavelenght solution for 'calspec' '''
iraf.imred()
iraf.specred()
linelist = str(raw_input('Enter file with list of lines (linelists$thar.dat) : '))
if linelist == '':
linelist = 'linelists$thar.dat'
iraf.specred.identify.coordlist = linelist
iraf.specred.identify(images=calspec)
def applywavesolution(inputre, calspec):
''' apply calibration solution to science spectra '''
inputlist = glob.glob(inputre)
inputstring = ', '.join(inputlist)
outputstring = ', '.join([inp[:-5]+'_spec.fits' for inp in inputlist])
iraf.hedit.unlearn()
iraf.dispcor.unlearn()
iraf.hedit.fields = 'REFSPEC1'
iraf.hedit.value = calspec
iraf.hedit.add = True
iraf.hedit(images=inputstring)
iraf.dispcor(input=inputstring, output=outputstring)
def combinespecs(inputre, scale='exposure', rdnoise='rdnoise', gain='gain'):
''' combine two or more spectra tha matches the input regular expression
'''
specfiles = glob.glob(specre)
specstring = ', '.join(specfiles)
print 'The following spectra will be combined: '
print specfiles
specout = str(raw_input('Enter output file name: '))
iraf.scombine.unlearn()
iraf.scombine.scale = scale
iraf.scombine.rdnoise = rdnoise
iraf.scombine.gain = gain
iraf.scombine(input=specstring, output=specout)
ask = str(raw_input('Plot output with splot? Y/N: '))
if (ask == 'y') or (ask == 'Y'):
iraf.splot.unlearn()
iraf.splot(specout)