-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTclean_grid.py
258 lines (227 loc) · 7.24 KB
/
Tclean_grid.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
import os
import sys
import astropy.units as u
from astropy.units import Quantity
import numpy as np
import re
from astropy.io import fits
from copy import deepcopy
from scipy.signal import medfilt2d
load_path_4scripts = os.environ['HOME'] + '/gitcommon/VisAlign/'
HOME = os.environ.get('HOME')
include_path = HOME + '/common/python/include'
sys.path.append(include_path)
from ImUtils.Cube2Im import slice0
from astropy.wcs import WCS
def apodize(hdu, FWHM=12., NMAX=16384): # 8192
hduapod = deepcopy(hdu)
#hdr=hdu[0].header
#hdrapod=hduapod[0].header
#size_in = hdr['NAXIS2'] * hdr['CDELT2'] * 3600.
#size_ideal = 4. * FWHM
#NIDEAL = size_ideal / (hdr['CDELT2'] * 3600.)
#NAPOD = NIDEAL
#if NAPOD > NMAX:
# NAPOD = NMAX
#else:
# NAPOD=2**(np.rint(np.log2(NIDEAL)))
#print("NAXIS2 = ", hdr['NAXIS2'], "NAPOD = ",NAPOD)
im = hduapod.data
hdr = hduapod.header
nx = hdr['NAXIS1']
ny = hdr['NAXIS2']
ivec = np.arange(0, nx)
jvec = np.arange(0, ny)
iis, jjs = np.meshgrid(ivec, jvec)
i0 = (float(nx) - 1) / 2.
j0 = (float(ny) - 1) / 2.
xxs = (iis - i0) * hdr['CDELT2'] * 3600.
yys = (jjs - j0) * hdr['CDELT2'] * 3600.
rrs = np.sqrt(xxs**2 + yys**2)
sigma = FWHM / (2. * np.sqrt(2. * np.log(2)))
atten = np.exp(-0.5 * rrs**2 / sigma**2)
im = im * atten
hduapod.data = im
return hduapod
def punch_vis(im, du, fileout, CRPIX1=1., CRPIX2=1.):
print("punching ", fileout)
nx, ny = im.shape
hdu = fits.PrimaryHDU()
hdu.data = im
hdr = hdu.header
CRPIX1 = (nx + 1.) / 2.
CRPIX2 = (ny + 1.) / 2.
hdr['CRPIX1'] = CRPIX1
hdr['CRVAL1'] = 0.
hdr['CDELT1'] = -du
hdr['CRPIX2'] = CRPIX2
hdr['CRVAL2'] = 0.
hdr['CDELT2'] = du
hdr['BUNIT'] = 'Jy'
hdu.header = hdr
hdu.writeto(fileout, overwrite=True)
def gridvis(
file_ms,
imsize=2048,
rmsnoise=3.6E-5, # Jy/beam noise
hermitian_symmetry=False,
dx=None,
outputdir='',
FWHM_apod=3., # arcsec
DoApod=True,
tcleanimagename=False):
gendirty(file_ms,
imsize=imsize,
tcleanimagename='dirty_tclean_' + os.path.basename(file_ms),
dx=dx,
outputdir=outputdir)
dx, gridded_visibilities_nat, gridded_weights_nat = getfft(
file_dirty=outputdir + 'dirty_tclean_' + os.path.basename(file_ms) +
'.image.fits',
file_psf=outputdir + 'dirty_tclean_' + os.path.basename(file_ms) +
'.psf.fits',
rmsnoise=rmsnoise, # Jy/beam noise
DoApod=DoApod,
outputdir=outputdir,
FWHM_apod=FWHM_apod)
dx = Quantity(dx, 'deg')
return dx, gridded_visibilities_nat, gridded_weights_nat.real
#def gridvis0(file_ms,
# imsize=2048,
# rmsnoise = 3.6E-5, # Jy/beam noise
# hermitian_symmetry=False,
# dx=None,
# outputdir='',
# FWHM_apod = 3., # arcsec
# tcleanimagename=False):
#
# print("processing: ", file_ms)
#
# cellsize = str(dx.to(u.arcsec).value) + 'arcsec'
#
# imagename = outputdir + tcleanimagename
# casacommand = '/usr/local/casareleases/casa-6.4.0-16/bin/casa --log2term --nogui -c ' + load_path_4scripts + 'tclean_gendirty.py' + ' ' + file_ms + ' ' + cellsize + ' ' + str(
# imsize) + ' ' + str(imsize) + ' ' + imagename
# print("casacommand", casacommand)
# os.system(casacommand)
#
# DoApod = True
# DoSpike = False
#
# file_in = imagename + '.psf.fits'
# hdupsf = slice0(file_in)
# hdrpsf = hdupsf.header
# nx = hdrpsf['NAXIS1']
# ny = hdrpsf['NAXIS2']
# if DoSpike:
# psf = hdupsf.data
# psf *= 0
# psf[int(hdrpsf['CRPIX1']) - 1, int(hdrpsf['CRPIX2']) - 1] = 1.
# elif DoApod:
# hdupsfapod = apodize(hdupsf, FWHM=FWHM_apod)
# psf = hdupsfapod.data
# fileout = re.sub('.fits', '.apod.fits', os.path.basename(file_in))
# hdupsfapod.writeto(outputdir+fileout, overwrite=True)
# else:
# psf = hdupsf.data
#
# file_in = imagename + '.image.fits'
# hdudirty = slice0(file_in)
# hdrdirty = hdudirty.header
# if DoApod:
# hdudirtyapod = apodize(hdudirty, FWHM=FWHM_apod)
# fileout = re.sub('.fits', '.apod.fits', os.path.basename(file_in))
# hdudirtyapod.writeto(outputdir+fileout, overwrite=True)
# dirty = hdudirtyapod.data
# else:
# dirty = hdudirty.data
#
# dirtyprep = np.fft.fftshift(dirty)
# VIB = np.fft.ifft2(dirtyprep)
# VIB = np.fft.ifftshift(VIB)
# #VIB /= (nx * ny) << not required because of internal ifft norm
#
# norm_Wk = 1 / rmsnoise**2 # Sum_k W_k
# VIB *= norm_Wk
#
#
# psfprep = np.fft.fftshift(psf)
# wVIB = np.fft.ifft2(psfprep)
# wVIB = np.fft.ifftshift(wVIB)
# #wVIB /= (nx * ny) << not required because of internal ifft norm
# wVIB *= norm_Wk
#
# VIB = VIB / wVIB
#
#
# dx = hdrpsf['CDELT2']
# dx_rad = dx * np.pi / (180.)
# imsize = hdrpsf['NAXIS1']
#
# du = (1 / (imsize * dx_rad))
#
#
# dx = Quantity(dx, 'deg')
# return dx, VIB, wVIB.real
def gendirty(file_ms,
imsize=2048,
dx=None,
outputdir='',
tcleanimagename=False):
cellsize = str(dx.to(u.arcsec).value) + 'arcsec'
imagename = outputdir + tcleanimagename
casacommand = 'casa --log2term --nogui -c ' + load_path_4scripts + 'tclean_gendirty.py' + ' ' + file_ms + ' ' + cellsize + ' ' + str(
imsize) + ' ' + str(imsize) + ' ' + imagename
print("casacommand", casacommand)
os.system(casacommand)
def getfft(
file_dirty,
file_psf,
rmsnoise=3.6E-5, # Jy/beam noise
hermitian_symmetry=False,
outputdir='',
DoApod=True,
DoSpike=False, # just a xcheck on fft
FWHM_apod=3.):
hdupsf = slice0(file_psf)
hdrpsf = hdupsf.header
nx = hdrpsf['NAXIS1']
ny = hdrpsf['NAXIS2']
if DoSpike:
psf = hdupsf.data
psf *= 0
psf[int(hdrpsf['CRPIX1']) - 1, int(hdrpsf['CRPIX2']) - 1] = 1.
elif DoApod:
hdupsfapod = apodize(hdupsf, FWHM=FWHM_apod)
psf = hdupsfapod.data
fileout = re.sub('.fits', '.apod.fits', os.path.basename(file_psf))
hdupsfapod.writeto(outputdir + fileout, overwrite=True)
else:
psf = hdupsf.data
hdudirty = slice0(file_dirty)
hdrdirty = hdudirty.header
if DoApod:
hdudirtyapod = apodize(hdudirty, FWHM=FWHM_apod)
fileout = re.sub('.fits', '.apod.fits', os.path.basename(file_dirty))
hdudirtyapod.writeto(outputdir + fileout, overwrite=True)
dirty = hdudirtyapod.data
else:
dirty = hdudirty.data
dirtyprep = np.fft.fftshift(dirty)
VIB = np.fft.ifft2(dirtyprep)
VIB = np.fft.ifftshift(VIB)
#VIB /= (nx * ny) << not required because of internal ifft norm
norm_Wk = 1 / rmsnoise**2 # Sum_k W_k
VIB *= norm_Wk
psfprep = np.fft.fftshift(psf)
wVIB = np.fft.ifft2(psfprep)
wVIB = np.fft.ifftshift(wVIB)
#wVIB /= (nx * ny) << not required because of internal ifft norm
wVIB *= norm_Wk
VIB = VIB / wVIB
dx = hdrpsf['CDELT2']
dx_rad = dx * np.pi / (180.)
imsize = hdrpsf['NAXIS1']
du = (1 / (imsize * dx_rad))
dx = Quantity(dx, 'deg')
return dx, VIB, wVIB.real