-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeconvolve.pro
executable file
·354 lines (287 loc) · 9.9 KB
/
deconvolve.pro
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
; this idl program extracts images
; from one .evt file
; and deconvolves the image using a PSF from Marx.
; Note that this version smooths the image (3x3) before
; deconvolving it.
;
; 12/31/99: based on George Chartas' image.pro program, which
; combined several .evt files and deconvolved the images, then
; combined the deconvolved images.
;
; 7/20/2011: This version uses Marx 4.4 (EAH)
;
;
; 05/21/2014: Updated to handle providing source position
; as argument (non-interactive). To run
; non-interactively, must provide all arguments.
; It is also possible to provide some arguments and
; not others. Unspecified arguments will be
; prompted for. (KAF)
;
;
@eb_parameter
@eb_property
@eb_derived_property
@domain_dataset
PRO deconvolve,infile=infile,source_x=source_x,source_y=source_y,img_size=img_size
PRINT, "Deconvolving "+infile+"..."
PRINT, " "
; input parameters
fromimage=0
if fromimage eq 0 then begin
;dn2evfile = '/bulk/pkg/xray/idl_lib/pro/acis.dn2ev'
split_threshold = 14
IF N_ELEMENTS(infile) EQ 0 THEN BEGIN
evtfile = pickfile(/READ, TITLE='Choose events file...', /NOCONFIRM)
;print,'Input file: ', evtfile
print,'Starting ds9 so you can select desired subimage ...'
spawn,'ciao; ds9 ' + evtfile + '&'
ENDIF ELSE BEGIN
evtfile = infile
ENDELSE
; define rectangular event filter region
;;original code
;read,'Enter the physical X, Y coordinates for the lower left corner: ', xllc, yllc
;read,'Enter the physical X, Y coordinates for the upper right corner: ', xurc, yurc
; get coordinates if not specified
IF N_ELEMENTS(source_x) EQ 0 THEN BEGIN
read,'Enter the physical X coordinate of source: ', source_x
ENDIF
IF N_ELEMENTS(source_y) EQ 0 THEN BEGIN
read,'Enter the physical Y coordinate of source: ', source_y
ENDIF
IF N_ELEMENTS(img_size) EQ 0 THEN BEGIN
read,'Enter side length of output image: ', img_size
ENDIF
endif ;endif not fromimage
; define box
xllc = source_x - img_size/2.0
yllc = source_y - img_size/2.0
xurc = source_x + img_size/2.0
yurc = source_y + img_size/2.0
pixel_size = 0.125 ; output pixel size in arcseconds
dx = pixel_size/0.492 ; pixel width for deconvolution in units of original pix
dy = dx
if fromimage eq 0 then begin
; Get grating from event file header
evdata = readfits(infile,head,exten_no=1)
grat = fxpar(head,'GRATING')
outfile = evtfile + '.image'
;read,'Enter name for output raw image file: ', outfile
; Run dmcopy to make an image of the desired portion of the events file
; convert box size and steps into strings
sxllc = strtrim(string(xllc),2)
sxurc = strtrim(string(xurc),2)
sdx = strtrim(string(dx),2)
syllc = strtrim(string(yllc),2)
syurc = strtrim(string(yurc),2)
sdy = strtrim(string(dy),2)
print,'Spawning dmcopy to create the raw image FITS file ...'
spawn, 'ciao; dmcopy "' + evtfile + '[EVENTS][grade=0,2,3,4,6]' $
+ '[bin x=' + sxllc + ':' + sxurc + ':' + sdx $
+ ', y=' + syllc + ':' + syurc + ':' + sdy + ']" ' + $
outfile + ' option=image clobber=yes'
print, 'ciao; dmcopy "' + evtfile + '[EVENTS][grade=0,2,3,4,6]' $
+ '[bin x=' + sxllc + ':' + sxurc + ':' + sdx $
+ ', y=' + syllc + ':' + syurc + ':' + sdy + ']" ' + $
outfile + ' option=image clobber=yes'
; werkt vanaf hier met .image. Dus.. als ik zelf een puntbron toevoeg
; aan image, vanaf hier inlezen.. (google translation = "works
; from here with .image. So .. if I do add a point source; to image, from reading here")
endif else begin ;endif not fromimage
outfile=infile
endelse
print,'Reading and displaying raw image ...'
im0 = readfits(outfile, header)
im = smooth(im0,3)
;endif
;pixel_size = 0.125
;dx = pixel_size/0.492 ; pixel width for deconvolution in units of original pix
;dy = dx
;im = readfits(infile,header)
;evtfile=infile
s = size(im)
syz1 = s(1)
syz2 = s(2)
repl = fix(512/syz2) + 1
tvscl, rebin(im, syz1*repl, syz2*repl, /sample)
; Determine psf's using MARX
; first remove current marx parameter file and cp original one
; spawn, '/usr/bin/rm marx.par'
; spawn, 'cp /bulk/pkg/cxc/marx/marx.par .'
; spawn, 'cp /usr/astro/marx/marx.par .'
; spawn, 'cp /usr/astro/marx-dist-4.3.0/share/marx/pfiles/marx.par .'
spawn, 'cp /usr/astro/marx/share/marx/pfiles/marx.par .'
; Format the marx command line we need, and execute it:
f='("marx ExposureTime=",F0, " OutputDir=", A, " GratingType=", A, " DetectorType=", A, " DetOffsetX=",F0, " DetOffsetY=",F0, " DetOffsetZ=",F0, " DetIdeal=", A, " SourceFlux=",F0, " SpectrumType=", A, " SpectrumFile=", A, " SourceType=", A, " SourceOffsetZ=",F0, " SourceOffsetY=",F0, " S-GaussSigma=",F0)'
;ExposureTime = 100. ; exposure time sec - 0 ==> run MARX in ray
; generation mode ->
; apparently no longer used...(EAH)
;OutputDir ="/bulk/draco2/burrows/marxout1"
OutputDir ="./"
;GratingType = grat
GratingType = "NONE"
DetectorType ="ACIS-S"
dox = 0.0
doy = 0.0
doz = 0.0
sy = 0.0
sz = 0.0
DetOffsetX = dox
DetOffsetY = doy
DetOffsetZ = doz
DetIdeal="no"
SourceFlux = -1
SpectrumType = "FILE"
;SpectrumFile = "/bulk/draco2/burrows/knotmod.dat" ; wabs pow = 0.09 1.7 1
SpectrumFile = "./knotmod.dat"
;SpectrumFile = "../obs15810_spectrum_chart.dat"
SourceType="GAUSS"
SourceOffsetZ =sz
SourceOffsetY =sy
SGaussSigma = 0.17
nl=1
source_circle_xc = dblarr(nl)
source_circle_yc = dblarr(nl)
source_circle_xc(*) = 0
source_circle_yc(*) = 0
xc0 = source_circle_xc(0)
yc0 = source_circle_yc(0)
; Get psf for current obsid
;ypsf = read_marx_file('/bulk/draco2/burrows/marxout1/xpixel.dat')
;zpsf = read_marx_file('/bulk/draco2/burrows/marxout1/ypixel.dat')
;ypsf = read_marx_file('./xpixel.dat')
;zpsf = read_marx_file('./ypixel.dat')
ypsf = read_marx_file('/astro/research/kaf33/IDL_Programs/sn1987a/xpixel.dat')
zpsf = read_marx_file('/astro/research/kaf33/IDL_Programs/sn1987a/ypixel.dat')
;ypsf = read_marx_file('xpixel.dat')
;zpsf = read_marx_file('ypixel.dat')
; find centroid of psf and create a 499x499 psf array with the peak of
; the psf center on pixel 249,249
; center for zero offset is at 511, 250
ypsf_m = mean(ypsf)
zpsf_m = mean(zpsf)
;
; PSF image is 80 x 80; each pixel is 0.492*dx arcsec
;
psf = make_image(ypsf,zpsf,xbinsize=dx,ybinsize=dy,xrange=[ypsf_m-10,ypsf_m+10],yrange=[zpsf_m-10,zpsf_m+10])
;
; Now, select the 21 x 21 subarray containing the PSF peak
;
psf_max = max(psf,ind)
psf_index = ind
ymax= ind mod n_elements(psf(*,0))
zmax= ind/n_elements(psf(0,*))
psf = psf(ymax-10.:ymax+10.,zmax-10.:zmax+10.)
title='MARX-generated PSF used for image deconvolution!C!Cfor '$
+ strtrim(string(total(psf)),2) + ' photons'
xlabel = strtrim(string(pixel_size),2) + ' arcsecond pixels'
ylabel = xlabel
contour,psf,nlevels=10,title=title, xtitle=xlabel, ytitle=ylabel
surface,psf,title=title, xtitle=xlabel, ytitle=ylabel
;stop
; print out plots
prt = ''
;read,'Do you want printed output (Y/N)?', prt
prt = 'Y'
if (prt eq 'y' or prt eq 'Y') then begin
set_plot,'ps'
device,/landscape
contour,psf,nlevels=10,title=title, xtitle=xlabel, ytitle=ylabel
surface,psf,title=title, xtitle=xlabel, ytitle=ylabel
device,/close
; spawn,'lpr -r idl.ps'
spawn,'mv idl.ps idl1.ps'
set_plot,'X'
endif
; normalize psf
print,'Normalizing PSF ...'
norm = total(psf)
print, 'total(psf) = ',norm
writefits,'psf.fits',psf
psf = double(psf/norm)
;psf = smooth(psf,3)
; deconvolve image with maximum likelihood
print,'Deconvolving image with max likelihood ...'
deconv = im
s = size(deconv)
xdim = s(1)
ydim = s(2)
tvscl,rebin(deconv, xdim*repl, ydim*repl, /sample)
xyouts,1,1,'Iteration 0'
Niter = 20
cube = fltarr(xdim,ydim,Niter+1)
cube[*,*,0] = im
for i=1,Niter do begin
print,'Starting iteration #',i
Max_Likelihood, im, psf, deconv, FT_PSF=psf_ft
tvscl,rebin(deconv, xdim*repl, ydim*repl, /sample)
xyouts,1,1,'Iteration '+strtrim(string(i),2)
cube[*,*,i] = deconv
endfor
writefits,'deconvolved_images_cube.fits',cube
; Write out the deconvolved image as a FITS file
if fromimage eq 0 then outfile2 = evtfile + '.deconvolved' else outfile2 = outfile + '.deconvolved'
;read,'Enter output file name for deconvolved image: ', outfile2
writefits,outfile2,deconv, header
; print out plots
prt = ''
;read,'Do you want printed output (Y/N)?', prt
prt = 'Y'
if (prt eq 'y' or prt eq 'Y') then begin
set_plot,'ps'
device,/landscape
contour,im,nlevels=10,$
title='SN1987A: raw 0th order image',xtitle=xlabel,ytitle=ylabel
contour,deconv,nlevels=10,$
title='SN1987A: deconvolved 0th order image',$
xtitle=xlabel,ytitle=ylabel
device,/close
; spawn,'lpr -r idl.ps'
spawn,'mv idl.ps idl2.ps'
set_plot,'X'
endif
d1 = n_elements(im(*,0))
d2 = n_elements(im(0,*))
projx = dblarr(d1)
projy = dblarr(d2)
imx = dblarr(d1)
imy = dblarr(d2)
for i =0,d1-1 do projx(i) = total(deconv(i,*))
for i =0,d2-1 do projy(i) = total(deconv(*,i))
for i =0,d1-1 do imx(i) = total(im(i,*))
for i =0,d2-1 do imy(i) = total(im(*,i))
print,'Plotting X projection of deconvolved image and original image...'
xvals = findgen(d1)*dx + xllc
;function_1d, id, xvals, projx, /block, dataset_name='Deconvolved',$
; title='Projections onto X axis', xlabel='0.123 arcsecond pixels'
;function_1d, id, xvals, imx, /block, dataset_name='Data',linestyle=2
;xmanager
plot, xvals, projx,$
title='Projections onto X axis!CDeconvolved (solid) and Data (dashed)',$
xtitle=xtitle
oplot,xvals,imx,linestyle=2
prt = ''
;read,'Do you want printed output (Y/N)?', prt
prt = 'Y'
if (prt eq 'y' or prt eq 'Y') then begin
set_plot,'ps'
device,/landscape
;; Show numbers HERE
FOR pp=0,(d1-1) DO BEGIN
if (projx[pp] NE 0.) then print, xvals[pp], projx[pp]
ENDFOR
plot, xvals, projx,$
title='Projections onto X axis!CDeconvolved (solid) and Data (dashed)',$
xtitle=xtitle
oplot,xvals,imx,linestyle=2
device,/close
; spawn,'lpr -r idl.ps'
spawn,'mv idl.ps idl3.ps'
set_plot,'X'
endif
;plot,projy
;oplot,imy
ss_im = im
ss_deconv = deconv
end