-
Notifications
You must be signed in to change notification settings - Fork 6
/
uvmodel.py
185 lines (143 loc) · 5.73 KB
/
uvmodel.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
"""
2013 May 13
Shane Bussmann
Purpose: Take an input uv fits file and replace or subtract the visibilities
with a model for the surface brightness map of the target.
Inputs:
sbmodelloc: location of fits image of model for surface brightness map
visdataloc: location of uv fits data for target
Outputs:
vismodel: uvfits data for model
visheader: uvfits header for model
"""
from __future__ import print_function
from astropy.io import fits
import numpy
#import pyximport
#pyximport.install(setup_args={"include_dirs":numpy.get_include()})
import sample_vis
import uvutil
import os
#import time
def writeVis(vis_complex, visdataloc, modelvisloc, miriad=False):
if miriad:
os.system('rm -rf ' + modelvisloc)
cmd = 'cp ' + visdataloc + ' ' + modelvisloc
os.system(cmd)
# get the real and imaginary components
real = numpy.real(vis_complex)
imag = numpy.imag(vis_complex)
# replace data visibilities with model visibilities
visfile = fits.open(modelvisloc, mode='update')
visibilities = visfile[0].data
visheader = visfile[0].header
if visheader['NAXIS'] == 7:
visibilities['DATA'][:, 0, 0, :, :, :, 0] = real
visibilities['DATA'][:, 0, 0, :, :, :, 1] = imag
elif visheader['NAXIS'] == 6:
visibilities['DATA'][:, 0, 0, :, :, 0] = real
visibilities['DATA'][:, 0, 0, :, :, 1] = imag
else:
print("Visibility dataset has >7 or <6 axes. I can't read this.")
#visibilities['DATA'][:, 0, 0, :, :, 2] = wgt
# replace the data visibilities with the model visibilities
visfile[0].data = visibilities
visfile.flush()
else:
from taskinit import tb
print("Writing visibility data to " + modelvisloc)
os.system('rm -rf ' + modelvisloc)
tb.open(visdataloc)
tb.copy(modelvisloc)
tb.close()
tb.open(modelvisloc, nomodify=False)
datashape = tb.getcol('DATA').shape
vis_complex = vis_complex.reshape(datashape)
tb.putcol('DATA', vis_complex)
tb.close()
def getVis(sbmodelloc, visdataloc):
#print(sbmodelloc, visdataloc)
# read in the surface brightness map of the model
modelimage = fits.getdata(sbmodelloc)
modelheader = fits.getheader(sbmodelloc)
# load the uv data, including the phase center of the data
uu, vv, ww = uvutil.uvload(visdataloc)
# load the uv data, including the phase center of the data
pcd = uvutil.pcdload(visdataloc)
# sample the uv visfile[0].data using the model
uushape = uu.shape
if len(uushape) == 2:
npol = uushape[0]
nrow = uushape[1]
uushape = (npol, 1, nrow)
uu = uu.flatten()
vv = vv.flatten()
model_complex = sample_vis.uvmodel(modelimage, modelheader, \
uu, vv, pcd)
vis_complex = model_complex.reshape(uushape)
return vis_complex
def replace(sbmodelloc, visdataloc, modelvisloc, miriad=False):
vis_model = getVis(sbmodelloc, visdataloc)
#sub_complex, vis_weight = uvutil.visload(visdataloc)
writeVis(vis_model, visdataloc, modelvisloc, miriad=miriad)
return
def subtract(sbmodelloc, visdataloc, modelvisloc, miriad=False):
vis_model = getVis(sbmodelloc, visdataloc)
# load the visibilities
vis_data, vis_weight = uvutil.visload(visdataloc)
vis_data -= vis_model
#print(visdataloc, modelvisloc, miriad)
writeVis(vis_data, visdataloc, modelvisloc, miriad=miriad)
return
"""
this will eventually be a routine to add arbitrary models to existing data.
def add(sbmodelloc, visdataloc, WeightByRMS=True, ExcludeChannels='none'):
# read in the surface brightness map of the model
modelimage = fits.getdata(sbmodelloc)
modelheader = fits.getheader(sbmodelloc)
# read in the uvfits data
visfile = fits.open(visdataloc)
visibilities = visfile[0].data
# load the uv data, including the phase center of the data
uu, vv, pcd = uvutil.uvload(visfile)
# get the number of visibilities, spws, frequencies, polarizations
if uu.ndim == 4:
nvis = uu[:, 0, 0, 0]
nspw = uu[0, :, 0, 0]
nfreq = uu[0, 0, :, 0]
npol = uu[0, 0, 0, :]
if uu.ndim == 3:
nvis = uu[:, 0, 0]
nspw = 0
nfreq = uu[0, :, 0]
npol = uu[0, 0, :]
# sample the uv visibilities using the model
uu = uu.flatten()
vv = vv.flatten()
model_complex = sample_vis.uvmodel(modelimage, modelheader, \
uu, vv, pcd)
if nspw > 0:
# get the real and imaginary components
real = numpy.real(model_complex).reshape(nvis, nspw, nfreq, npol)
imag = numpy.imag(model_complex).reshape(nvis, nspw, nfreq, npol)
uu = uu.reshape(nvis, nspw, nfreq, npol)
vv = vv.reshape(nvis, nspw, nfreq, npol)
# replace data visibilities with model visibilities
visibilities['DATA'][:, 0, 0, :, :, :, 0] = real_raw + real
visibilities['DATA'][:, 0, 0, :, :, :, 1] = imag_raw + imag
visibilities['DATA'][:, 0, 0, :, :, :, 2] = wgt
else:
# get the real and imaginary components
real = numpy.real(model_complex).reshape(nvis, nfreq, npol)
imag = numpy.imag(model_complex).reshape(nvis, nfreq, npol)
uu = uu.reshape(nvis, nfreq, npol)
vv = vv.reshape(nvis, nfreq, npol)
# replace data visibilities with model visibilities
visibilities['DATA'][:, 0, 0, :, :, 0] = real_raw + real
visibilities['DATA'][:, 0, 0, :, :, 1] = imag_raw + imag
visibilities['DATA'][:, 0, 0, :, :, 2] = wgt
# replace the data visibilities with the model visibilities
visfile[0].data = visibilities
#visfile[0].data = vismodel
return visfile
"""