-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlacos_spec.cl
322 lines (245 loc) · 8.37 KB
/
lacos_spec.cl
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
procedure lacos_spec(input,output,outmask)
# cosmic ray rejection script for long slit spectra
# by Pieter van Dokkum, April 2001
#
# info at http://www.astro.yale.edu/dokkum/lacosmic/
char input{"",prompt="input spectrum"}
char output{"",prompt="cosmic ray cleaned output spectrum"}
char outmask{"",prompt="output bad pixel map (.pl)"}
real gain{2.,prompt="gain (electrons/ADU)"}
real readn{6.,prompt="read noise (electrons)"}
int xorder{9,prompt="order of object fit (0=no fit)"}
int yorder{3,prompt="order of sky line fit (0=no fit)"}
real sigclip{4.5,prompt="detection limit for cosmic rays (sigma)"}
real sigfrac{0.5,prompt="fractional detection limit for neighbouring pixels"}
real objlim{1.,prompt="contrast limit between CR and underlying object"}
int niter{4,prompt="maximum number of iterations"}
bool verbose{yes}
begin
char blk,lapla,deriv2,med5,sub5,sub5abs,med3,med7,imstatout,inputmask
char noise,sigmap,firstsel,starreject,gfirstsel,finalsel
char oldoutput,sigima,galaxy,skymod
char kernel,gkernel
real midpt,skylev,sig,sigcliplow
char l1,l2
int i
bool stop
int previous,npix
if (!deftask("imcalc")) error(2,"Load package stsdas")
if (gain<=0) error(2,"Gain is required")
if (verbose) {
print("")
print("_______________________________________________________________________________")
print("")
print(" L.A.Cosmic: Laplacian cosmic ray removal")
print("")
print(" P. van Dokkum, 2001, PASP 113, 1420")
print("")
print(" Spectroscopy version 1.0 (April 2001)")
print("_______________________________________________________________________________")
print("")
}
# make temporary files
blk = mktemp("lacos")
lapla = mktemp("lacos")
deriv2 = mktemp("lacos")
kernel = mktemp("lacos")
gkernel=mktemp("lacos")
med3 = mktemp("lacos")
med5 = mktemp("lacos")
med7 = mktemp("lacos")
sub5 = mktemp("lacos")
sub5abs = mktemp("lacos")
imstatout = mktemp("lacos")
noise = mktemp("lacos")
sigmap = mktemp("lacos")
firstsel = mktemp("lacos")
starreject=mktemp("lacos")
gfirstsel = mktemp("lacos")
finalsel = mktemp("lacos")
inputmask = mktemp("lacos")
oldoutput = mktemp("lacos")
sigima = mktemp("lacos")
galaxy=mktemp("lacos")
skymod = mktemp("lacos")
# set some parameters in standard IRAF tasks
convolve.bilinear=no
convolve.radsym=no
# create Laplacian kernel
print("0 -1 0;",>> kernel)
print("-1 4 -1;",>> kernel)
print("0 -1 0;",>> kernel)
# create growth kernel
print("1 1 1;",>> gkernel)
print("1 1 1;",>> gkernel)
print("1 1 1;",>> gkernel)
# initialize loop
i=1
stop=no
previous=0
imcopy(input,oldoutput,verb-)
imcopy(input,outmask,verb-)
imreplace(outmask,0,upper=INDEF,lower=INDEF)
# subtract object spectra if desired
if (xorder>0) {
if (verbose) {
print("Subtracting object spectra")
print(" fit order = "//xorder)
print("")
}
fit1d(oldoutput,galaxy,"fit",axis=1,order=xorder,func="leg",low=4.,
high=4.,nav=1,inter-,sample="*",niter=3,grow=0,cursor="")
imarith(oldoutput,"-",galaxy,oldoutput)
}
else {
imcopy(oldoutput,galaxy,verb-)
imreplace(galaxy,0,lower=INDEF,upper=INDEF)
}
if (yorder>0) {
if (verbose) {
print("Subtracting sky lines")
print(" fit order = "//yorder)
print("")
}
fit1d(oldoutput,skymod,"fit",axis=2,order=yorder,func="leg",low=4.,high=4.,
inter-,sample="*",nav=1,niter=3,grow=0,cursor="")
imarith(oldoutput,"-",skymod,oldoutput)
}
else {
imcopy(oldoutput,skymod,verb-)
imreplace(skymod,0,lower=INDEF,upper=INDEF)
}
# add object spectra to sky model
imarith(skymod,"+",galaxy,skymod)
# start iterations
while(!stop) {
if (verbose) {
print("")
if (i<10) print("_______________________________ Iteration "//i//" ___________________________________")
if (i>9) print("_______________________________ Iteration "//i//"___________________________________")
print("")
print("")
}
# add median of residuals to sky model
median(oldoutput,med5,5,5,zlor=INDEF,zhir=INDEF,verb-)
imarith(skymod,"+",med5,med5)
# take second-order derivative (Laplacian) of input image
# kernel is convolved with subsampled image, in order to remove negative
# pattern around high pixels
if (verbose) {
print("Convolving with Laplacian kernel")
print("")
}
blkrep(oldoutput,blk,2,2)
convolve(blk,lapla,kernel)
imreplace(lapla,0,upper=0,lower=INDEF)
blkavg(lapla,deriv2,2,2,option="average")
if (verbose) {
print("Creating noise model using:")
print(" gain = "//gain//" electrons/ADU")
print(" readnoise = "//readn//" electrons")
print("")
}
# create noise model
imcalc(med5,noise,"sqrt(im1*"//gain//" + "//readn//"**2)/"//gain,verb-)
imreplace(med5,0.00001,upper=0,lower=INDEF)
# divide Laplacian by noise model
imarith(deriv2,"/",noise,sigmap)
# Laplacian of blkreplicated image counts edges twice:
imarith(sigmap,"/",2.,sigmap)
# removal of large structure (bright, extended objects)
imdel(med5)
median(sigmap,med5,5,5,zlo=INDEF,zhi=INDEF,verb-)
imarith(sigmap,"-",med5,sigmap)
# find all candidate cosmic rays
# this selection includes sharp features such as stars and HII regions
if (verbose) {
print("Selecting candidate cosmic rays")
print(" sigma limit = "//sigclip)
print("")
}
imcopy(sigmap,firstsel,verb-)
imreplace(firstsel,0,upper=sigclip,lower=INDEF)
imreplace(firstsel,1,lower=0.1,upper=INDEF)
# compare candidate CRs to median filtered image
# this step rejects bright, compact sources from the initial CR list
if (verbose) {
print("Removing suspected emission lines")
print(" selecting cosmic rays > "//objlim//" times object flux")
print("")
}
# subtract background and smooth component of objects
median(oldoutput,med3,3,3,zlo=INDEF,zhi=INDEF,verb-)
median(med3,med7,7,7,zlo=INDEF,zhi=INDEF,verb-)
imarith(med3,"-",med7,med3)
imarith(med3,"/",noise,med3)
imreplace(med3,0.01,upper=0.01,lower=INDEF)
# compare CR flux to object flux
imcalc(firstsel//","//sigmap//","//med3,starreject,"(im1*im2)/im3",verb-)
# discard if CR flux <= objlim * object flux
imreplace(starreject,0,upper=objlim,lower=INDEF)
imreplace(starreject,1,lower=0.5,upper=INDEF)
imarith(firstsel,"*",starreject,firstsel)
# grow CRs by one pixel and check in original sigma map
convolve(firstsel,gfirstsel,gkernel)
imreplace(gfirstsel,1,lower=0.5,upper=INDEF)
imarith(gfirstsel,"*",sigmap,gfirstsel)
imreplace(gfirstsel,0,upper=sigclip,lower=INDEF)
imreplace(gfirstsel,1,lower=0.1,upper=INDEF)
# grow CRs by one pixel and lower detection limit
sigcliplow = sigfrac * sigclip
if (verbose) {
print("Finding neighbouring pixels affected by cosmic rays")
print(" sigma limit = "//sigcliplow)
print("")
}
convolve(gfirstsel,finalsel,gkernel)
imreplace(finalsel,1,lower=0.5,upper=INDEF)
imarith(finalsel,"*",sigmap,finalsel)
imreplace(finalsel,0,upper=sigcliplow,lower=INDEF)
imreplace(finalsel,1,lower=0.1,upper=INDEF)
# determine number of CRs found in this iteration
imdel(gfirstsel)
imcalc(finalsel//","//outmask,gfirstsel,"(1-im2)*im1",verb-)
imstat(gfirstsel,fields="npix",lower=0.5,upper=INDEF,for-) | scan(npix)
# create cleaned output image; use 3x3 median with CRs excluded
if (verbose) {
print("Creating output:")
print(" bad pixel mask: "//outmask)
print(" cleaned image: "//output)
print("")
}
imdel(med5)
imarith(outmask,"+",finalsel,outmask)
imreplace(outmask,1,lower=1,upper=INDEF)
imcalc(outmask//","//oldoutput,inputmask,"(1-10000*im1)*im2",verb-)
median(inputmask,med5,5,5,zloreject=-9999,zhi=INDEF,verb-)
imarith(outmask,"*",med5,med5)
if (i>1) imdel(output)
imcalc(outmask//","//oldoutput//","//med5,output,"(1-im1)*im2+im3",verb-)
# add sky and object spectra back in
imdel(oldoutput)
imcopy(output,oldoutput,verb-)
imarith(output,"+",skymod,output)
# cleanup and get ready for next iteration
if (verbose) {
print("Cleaning up")
print("")
}
if (verbose) {
print("")
print(npix//" cosmic rays found in iteration "//i)
print("")
}
if (npix==0) stop=yes
i=i+1
if (i>niter) stop=yes
# delete temp files
imdel(blk//","//lapla//","//deriv2//","//med5)
imdel(med3//","//med7//","//noise//","//sigmap)
imdel(firstsel//","//starreject//","//gfirstsel)
imdel(finalsel//","//inputmask)
}
imdel(oldoutput//","//skymod//","//galaxy)
delete(kernel//","//gkernel)
end