-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSeeKAT_multi-epoch.py
214 lines (178 loc) · 7.84 KB
/
SeeKAT_multi-epoch.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
import argparse
import numpy as np
import math
import matplotlib.pyplot as plt
from astropy.coordinates import SkyCoord
import astropy.units as u
import sys
#sys.path.append('/raid/tbez/SeeKAT/') #REMOVE
import SK.plotting as Splot
import SK.utils as ut
import SK.coordinates as co
import SeeKAT as SK
def parseOptions(parser):
'''Options:
-f Input files with each line a different CB detection.
Should have 3 columns: RA (h:m:s), Dec (d:m:s), S/N
-p PSF of a CB in fits format
--o Fractional sensitivity level at which CBs are tiled to overlap
--r Resolution of PSF in units of arcseconds per pixel
--n Number of beams to consider when creating overlap contours. Will
pick the specified number of beams with the highest S/N values.
--nsig Sets the number of standard deviation contours drawn.
--s Draws known coordinates onto the plot for comparison.
--scalebar Sets the length of the scale bar on the plot in arcseconds.
--ticks Sets the spacing of ticks on the localisation plot.
--clip Sets level below which CB PSF is set equal to zero.
--zoom Automatically zooms in on the TABs.
'''
parser.add_argument('-f', dest='files',
nargs = '+',
type = str,
help="Detections files",
required=True)
parser.add_argument('-p',dest='psfs',
nargs='+',
type=str,
help="PSF file",
required=True)
parser.add_argument('-c', dest='config',
nargs = 1,
type = str,
help="Configuration (json) file",
required=False)
parser.add_argument('--o', dest='overlaps',
nargs='+',
type = float,
help = "Fractional sensitivity level at which the coherent beams overlap",
default = 0.25,
required = False)
parser.add_argument('--r', dest='res',
nargs = 1,
type=float,
help="Distance in arcseconds represented by one pixel of the PSF",
default = 1,
required = True)
parser.add_argument('--n',dest='npairs',
nargs = 1,
type = int,
help='Number of beams to use',
default = [1000000])
parser.add_argument('--nsig',dest='nsig',
nargs = 1,
type = int,
help='Draws uncertainty contours up to this number of standard deviations.',
default = [2])
parser.add_argument('--s', dest='source',
nargs = 1,
type=str,
help="Draws given coordinate location (format: hms,dms) on localisation plot",
required = False)
parser.add_argument('--scalebar', dest='sb',
nargs = 1,
type = float,
help = "Length of scale bar on the localisation plot in arcseconds. Set to 0 to omit altogether",
default = [10],
required = False)
parser.add_argument('--ticks', dest='tickspacing',
nargs = 1,
type = float,
help = "Sets the number of pixels between ticks on the localisation plot",
default = [100],
required = False)
parser.add_argument('--clip', dest='clipping',
nargs = 1,
type = float,
help = "Sets values of the PSF below this number to zero. Helps minimise the influence of low-level sidelobes",
default = [0.08],
required = False)
parser.add_argument('--zoom', dest='autozoom',
help = "Automatically zooms the localisation plot in on the TABs",
action = 'store_true')
parser.add_argument('--fits', dest='fitsOut',
help = "Outputs .fits file of localisation region",
action = 'store_true')
options= parser.parse_args()
return options
def readCoordsME(options):
"""
Checks that arguments make sense, and if so returns data read from input file.
"""
RAs = []
Decs = []
data = np.concatenate([np.loadtxt(f,
delimiter=' ',
dtype=str,
encoding="ascii") for f in options.files])
# Sorting beams by S/N, so that Gaussian assumption holds better.
# You generally want LOW/HIGH S/N beam pairs.
data = data[np.argsort(data[:,2])[::-1]]
c = SkyCoord(data[:,0], data[:,1], frame="icrs", unit=(u.hourangle, u.deg))
best_cand = np.argsort(data[:,2])[-1]
# Calculate number of beam pairs
n_comb = math.factorial(len(data))/(math.factorial(2)*
math.factorial(len(data)-2))
if options.source:
[ra,dec] = options.source[0].split(',')
b = SkyCoord(ra, dec, frame='icrs',unit=(u.hourangle, u.deg))
boresightCoord = [b.ra.deg,b.dec.deg]
else:
bs_c = SkyCoord(data[:,0][best_cand],data[:,1][best_cand], frame='icrs', unit=(u.hourangle, u.deg))
boresightCoord = [bs_c.ra.deg,bs_c.dec.deg]
if options.overlap > 1.0 or options.overlap < 0:
print("The OVERLAP parameter must be between 0 and 1")
exit()
elif options.npairs[0] > n_comb:
options.npairs[0] = n_comb
return data, c, boresightCoord
else:
return data, c, boresightCoord
def deg2pixME(w, c, psf, boresight, res):
"""
Converts coordinates from degrees to pixels
c must be a SkyCoord object and psf a numpy array.
"""
coordsDeg = []
for i in range(0, len(c)):
coordsDeg.append([c.ra.deg[i], c.dec.deg[i]])
### Convert deg -> pix
px = w.all_world2pix(coordsDeg, 1)
c.ra.px = px[:, 0]
c.dec.px = px[:, 1]
return c
if __name__ == "__main__":
parser = argparse.ArgumentParser()
options = parseOptions(parser)
f, ax = plt.subplots(figsize=(10,10))
psf_ar = ut.readPSF(options.psfs[0], options.clipping[0])
options.overlap = options.overlaps[0]
data, c, boresight = readCoordsME(options)
c, w, array_width, array_height = co.deg2pix(c, psf_ar, boresight, options.res[0])
for i in enumerate(options.files):
options.file = [i[1]]
print('\n' + options.file[0])
options.psf = [options.psfs[i[0]]]
if len(options.overlaps)==len(options.files):
options.overlap = options.overlaps[i[0]]
else:
options.overlap = options.overlaps[0]
data, c1, _ = ut.readCoords(options)
psf_ar = ut.readPSF(options.psf[0], options.clipping[0])
c1 = deg2pixME(w, c1, psf_ar, boresight, options.res[0])
loglikelihood = np.zeros((array_height,array_width))
options.npairs[0] = math.factorial(len(c1))/(math.factorial(2)*
math.factorial(len(c1)-2))
loglikelihood += SK.make_map(array_height, array_width,
c1, psf_ar, options, data)
if options.source:
Splot.plot_known(w, options.source[0])
Splot.make_ticks(ax, array_width, array_height,
w, fineness=options.tickspacing[0])
Splot.likelihoodPlot(f, ax, w, loglikelihood, options)
if options.autozoom == True:
ut.autozoom(ax, c, options)
if options.fitsOut == True:
likelihood = ut.norm_likelihood(loglikelihood)
ut.write2fits(w, likelihood)
plt.savefig(options.file[0]+'.png',dpi=300)
plt.show()