/
calibrate_wavelength.py
324 lines (281 loc) · 9.88 KB
/
calibrate_wavelength.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
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
### Load python modules
from pyraf import iraf
from numpy import *
import string
import sys
import os
import pyfits
### Load functions script (located in the same folder)
import functions
### Load iraf moduels
iraf.noao()
iraf.imred()
iraf.imred.ccdred()
iraf.imred.kpnoslit()
iraf.noao.rv()
iraf.astutil()
###################
### Description ###
###################
### The stellar spectrum needs to be extracted and wavelength calibrated.
### Extraction is automatically performed on the brightest star
### unless interact is turned on.
### The arc spectrum is extracted along the same aperture as the
### stellar spectrum.
### If all went well, the distortion corrected fits image already
### has a wavelength solution. We use the arc spectra to check
### this wavelength calibration. We also perform cosmic ray removal
### and normalisation.
### Usage: calibrate_wavelength.py file_path file_name
########################
### Start of program ###
########################
### Set file_path
file_path = sys.argv[1]
file_path_temp = file_path + "temp/"
file_path_reduced = file_path + "reduced/"
file_name = sys.argv[2]
hdulist = pyfits.open(file_path + file_name)
object_mjd = hdulist[0].header['MJD-OBS']
hdulist.close()
camera = functions.read_config_file("CAMERA")
grating = functions.read_config_file("GRATING")
dichroic = functions.read_config_file("DICHROIC")
combine_aps = functions.read_config_file("COMBINE_APERTURES")
print "This script applies NeAr arc image to calibrate the object spectrum " +file_name
### Get slice numbers and arc images to use
arc_list = functions.read_ascii(file_path_temp + "arcs_to_use.txt")
image_slices = functions.read_ascii(file_path_temp + "stellar_apertures.txt")
### Calculate the fractional weight of each arc
arc_weight = []
for arc_name in arc_list:
hdulist = pyfits.open(file_path + arc_name)
arc_mjd = hdulist[0].header['MJD-OBS']
hdulist.close()
arc_weight.append(abs(arc_mjd - object_mjd))
arc_weight = array(arc_weight)
arc_weight = arc_weight / sum(arc_weight)
### Define linelist to use
linelist = grating + "_linelist.dat"
### Define template to use
template = "calibrated_" + grating +"_"+dichroic+ "_template.fits"
### Determine if the extraction process should be done interactively
interact = 0
interact = functions.read_config_file("INTERACT")
if interact == "true":
interact = 1
if interact == "false":
interact = 0
###################################
### Perform spectral extraction ###
###################################
program_dir = os.getcwd() + "/" #Save the current working directory
os.chdir(file_path_temp) #Change to ../temp/ dir
print "Using apall to extract the spectrum about the central line in the object image"
os.system("rm -f " + file_path_temp + "*" + string.split(file_name,".")[0] + "*ms*")
os.system("rm database/*" + string.split(file_name,".")[0] + "*")
os.system("rm -f " + file_path_temp + "nearraw_*" + file_name)
### Copy over the template files
os.system("cp -f " + program_dir + "cal_linelists/" + template + " .")
os.system("cp -f " + program_dir+"cal_linelists/database/idcalibrated_"+grating+"_"+dichroic+"_template " + "database/")
### Perform spectral extraction, one image slice at a time
### Then extract corresponding arc spectra from the arc images
### So we have n stellar spectra from n image slices
### And n x m arc spectra from n image slices and m arc images
for im_slice in image_slices:
iraf.apall(
input = "t"+im_slice+"_"+file_name, \
output = "",\
format = "multispec",\
references = "",\
apertures = 1,\
interactive = interact, \
find = 1,\
recenter = 1,\
resize = 1,\
edit =interact,\
trace = 1,\
fittrace = 0,\
extract = 1,\
extras = 1,\
review = 0,\
line = "INDEF",\
nsum = -100,\
lower = -10.0,\
upper = 10.0,\
b_sample = "-15:-8,8:15",\
b_order = 1,\
b_naverage = -5,\
b_niterate = 0,\
b_low_reject = 3.0,\
b_high_reject = 3.0,\
b_grow = 0.0,\
width = 5.0,\
radius = 10.0,\
threshold = 0.0,\
nfind = 1,\
ylevel = 0.3,\
peak = 1,\
bkg = 1,\
t_nsum = 40,\
t_step = 5,\
t_nlost = 3,\
t_function = "spline3",\
t_order = 5,\
t_naverage = -10,\
t_niterate = 5,\
t_low_reject = 2.0,\
t_high_reject = 2.0,\
t_grow = 0.0,\
background = "fit",\
weights = "variance",\
clean = 1,\
lsigma = 20.0,\
usigma = 3.0,\
saturation = 64000,\
readnoise = 3.8,\
gain = 0.9)
### Extract NeAr arc lamp spectrum
print "Using apall to extract the arc spectrum according to the same aperture as the object spectrum"
count = 1
for arc_name in arc_list:
### For each arc image, perform extraction at the same
### aperture as the stellar spectrum
os.system("rm database/*"+im_slice+"*" + string.split(arc_name,".")[0] + "*")
os.system("rm nearraw_" + im_slice + "_" + arc_name)
### Use apall to extract along the same lines
### as that used in the stellar extraction
iraf.apall(
input = "t" + im_slice + "_" + arc_name, \
output = "nearraw_" + im_slice + "_" + arc_name,\
references = "t"+im_slice+"_" + file_name,\
apertures = "",\
find = 0,\
recenter = 0,\
resize = 0,\
edit = 0,\
trace = 0,\
back = "none",\
nfind = "",\
interactive = 0)
### Using reidentify to find a dispersion solution
print "reidentifying the lines - to make sure of the dispersion solution"
iraf.reidentify(
reference = template, \
images = "nearraw_"+im_slice + "_" + arc_name,\
answer = "no",\
crval = "",\
cdelt = "",\
interactive = "no",\
section = "first line",\
newaps = 0,\
override = 1,\
refit = 1,\
trace = 0,\
step = "10",\
nsum = "10",\
shift = "INDEF",\
search = "INDEF",\
nlost = 2,\
cradius = 5.0,\
threshold = 5.0,\
addfeatures = 0,\
coordlist = program_dir + "cal_linelists/" + linelist,\
match = -2.0,\
maxfeatures = 60,\
minsep = 2.0,\
database = "database",\
plotfile = "",\
verbose = 1,\
graphics = "stdgraph",\
cursor = "",\
aidpars = "",\
mode = "ql")
### Apply dispersion solution to object spectrum
### First add REFSPEC to the object header using hedit
iraf.hedit(
images="t" + im_slice + "_" +string.split(file_name,".")[0]+".ms.fits",\
fields ="REFSPEC" + str(int(count)),\
value ="nearraw_" + im_slice+ "_" + arc_name+" "+str(arc_weight[count-1]),\
add = 1,\
verify = 0,\
show = 1,\
update = 1)
count = count + 1
### Run dispcor to calibrate the object spectrum according to the dispersion
### solution
print "Using dispcor to apply the dispersion solution to the object image"
os.system("rm dispcorr_" + im_slice + "_" + file_name)
iraf.dispcor(
input = "t" + im_slice + "_" + string.split(file_name,".")[0] + ".ms.fits",\
output = "dispcorr_" + im_slice + "_" + file_name,\
linearize = "no",\
database = "database",\
log = 0,\
flux = 1,\
samedisp = 0,\
ignoreaps = 0,\
confirm = 0,\
listonly = 0,\
verbose = 1)
iraf.hedit(images="dispcorr_" + im_slice + "_" + file_name,fields="SFIT",value="1",add=0,delete=1,verify=0,show=1,update=1)
iraf.hedit(images="dispcorr_" + im_slice + "_" + file_name,fields="SFITB",value="1",add=0,delete=1,verify=0,show=1,update=1)
### Apply high rejection to remove cosmic rays, dead pixels, etc.
print "Applying high rejection"
### Be more lenient if apertures will be combined
if combine_aps == "true":
lowrej = 30.0
highrej = 20.0
nit = 2
else:
lowrej = 50.0
highrej = 50.0
nit = 2
os.system("rm cray_" + im_slice + "_" + file_name)
iraf.continuum(
input = "dispcorr_" + im_slice + "_" + file_name,\
output = "cray_" + im_slice + "_" + file_name,\
ask = "no",\
lines = "*",\
bands = "*",\
type = "data",\
replace = 1,\
wavescale = 1,\
logscale = 0,\
override = 1,\
listonly = 0,\
logfiles = "logfile",\
interactive = 0,\
sample = "*",\
naverage = 1,\
function = "spline3",\
order = 15,\
low_reject = lowrej,\
high_reject = highrej,\
niterate = nit,\
grow = 1.0)
iraf.hedit(images="cray_" + im_slice + "_" + file_name,fields="SFIT",value="1",add=0,delete=1,verify=0,show=1,update=1)
iraf.hedit(images="cray_" + im_slice + "_" + file_name,fields="SFITB",value="1",add=0,delete=1,verify=0,show=1,update=1)
### Apply normalisation with continuum
print "Fitting continuum and normalising spectrum"
os.system("rm norm_" + im_slice + "_" + file_name)
iraf.continuum(
input = "cray_" + im_slice + "_" + file_name,\
output = "norm_" + im_slice + "_" + file_name,\
lines = "*",\
bands = "*",\
type = "ratio",\
replace = 0,\
wavescale = 1,\
logscale = 0,\
override = 1,\
listonly = 0,\
logfiles = "logfile",\
interactive = 0,\
function = "spline3",\
order = 15,\
low_reject = 2.0,\
high_reject = 3.0,\
niterate = 5,\
grow = 1,\
ask = "no",)