-
Notifications
You must be signed in to change notification settings - Fork 6
/
calibration.jl
471 lines (391 loc) · 16.3 KB
/
calibration.jl
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# Copyright (c) 2015, 2016 Michael Eastwood
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
abstract Calibration
macro generate_calibration(name, jones)
quote
Base.@__doc__ immutable $name <: Calibration
jones :: Matrix{$jones}
flags :: Matrix{Bool}
end
function $name(Nant::Int, Nfreq::Int)
$name(ones($jones, Nant, Nfreq), zeros(Bool, Nant, Nfreq))
end
Base.similar(cal::$name) = $name(Nant(cal), Nfreq(cal))
end |> esc
end
"""
GainCalibration
*Description*
This type represents the gain calibration of an interferometer.
Each antenna and frequency channel receives a diagonal Jones matrix.
The diagonal terms of the Jones matrix are the complex gains of the `x`
and `y` polarization respectively. The off-diagonal terms represent the
polarization leakage from `x` to `y` and `y` to `x`. These off-diagonal
terms are assumed to be zero.
The `gaincal` routine is used to solve for the interferometer's
gain calibration.
*Fields*
* `jones` - an array of diagonal Jones matrices (one per antenna and frequency channel)
* `flags` - a corresponding list of flags
"""
@generate_calibration GainCalibration DiagonalJonesMatrix
"""
PolarizationCalibration
*Description*
This type represents the polarization calibration of an interferometer.
Each antenna and frequency channel receives a full Jones matrix.
The diagonal terms of the Jones matrix are the complex gains of the `x`
and `y` polarization respectively. The off-diagonal terms represent the
polarization leakage from `x` to `y` and `y` to `x`. All of these
terms are included in this calibration.
The `polcal` routine is used to solve for the interferometer's
polarization calibration.
*Fields*
* `jones` - an array of Jones matrices (one per antenna and frequency channel)
* `flags` - a corresponding list of flags
"""
@generate_calibration PolarizationCalibration JonesMatrix
Nant( cal::Calibration) = size(cal.jones, 1)
Nfreq(cal::Calibration) = size(cal.jones, 2)
write(filename, calibration::Calibration) = JLD.save(File(format"JLD", filename), "cal", calibration)
read(filename) = JLD.load(filename, "cal")
# The following functions are disabled until NPZ.jl is fixed on Julia v0.5. To re-enable:
# * uncomment these lines
# * add `using NPZ` to `src/TTCal.jl`
# * add `NPZ` to `REQUIRE`
# * uncomment `using NPZ` in `test/runtests.jl`
# * uncomment the corresponding tests in `test/calibration.jl`
#
#function write_for_python(filename, calibration::GainCalibration)
# gains = zeros(Complex128, 2, Nant(calibration), Nfreq(calibration))
# flags = zeros( Bool, Nant(calibration), Nfreq(calibration))
# for β = 1:Nfreq(calibration), ant = 1:Nant(calibration)
# gains[1,ant,β] = calibration.jones[ant,β].xx
# gains[2,ant,β] = calibration.jones[ant,β].yy
# flags[ ant,β] = calibration.flags[ant,β]
# end
# npzwrite(filename, Dict("gains" => gains, "flags" => flags))
#end
#
#function write_for_python(filename, calibration::PolarizationCalibration)
# gains = zeros(Complex128, 4, Nant(calibration), Nfreq(calibration))
# flags = zeros( Bool, Nant(calibration), Nfreq(calibration))
# for β = 1:Nfreq(calibration), ant = 1:Nant(calibration)
# gains[1,ant,β] = calibration.jones[ant,β].xx
# gains[2,ant,β] = calibration.jones[ant,β].xy
# gains[3,ant,β] = calibration.jones[ant,β].yx
# gains[4,ant,β] = calibration.jones[ant,β].yy
# flags[ ant,β] = calibration.flags[ant,β]
# end
# npzwrite(filename, Dict("gains" => gains, "flags" => flags))
#end
# corrupt / applycal
doc"""
corrupt!(visibilities, metadata, calibration)
*Description*
Corrupt the visibilities as if they were observed with the given calibration.
That is if we have the model visibility $V_{i,j}$ on baseline $i,j$, and the
corresponding Jones matrices $J_i$ and $J_j$ for antennas $i$ and $j$ compute
$$V_{i,j} \rightarrow J_i V_{i,j} J_j^*$$
*Arguments*
* `visibilities` - the list of visibilities to corrupt
* `metadata` - the metadata describing the interferometer
* `calibration` - the calibration in question
"""
function corrupt!(visibilities::Visibilities, meta::Metadata, calibration::Calibration)
for β = 1:Nfreq(meta), α = 1:Nbase(meta)
antenna1 = meta.baselines[α].antenna1
antenna2 = meta.baselines[α].antenna2
# If the calibration has only one frequency channel, then it is a wideband
# solution and we should apply it to every frequency channel. Otherwise we
# should use the calibration from the frequency channel in question.
if Nfreq(calibration) == 1
J₁ = calibration.jones[antenna1,1]
J₂ = calibration.jones[antenna2,1]
flag = calibration.flags[antenna1,1] | calibration.flags[antenna2,1]
else
J₁ = calibration.jones[antenna1,β]
J₂ = calibration.jones[antenna2,β]
flag = calibration.flags[antenna1,β] | calibration.flags[antenna2,β]
end
V = visibilities.data[α,β]
visibilities.data[α,β] = J₁*V*J₂'
visibilities.flags[α,β] = ifelse(flag, true, visibilities.flags[α,β])
end
visibilities
end
doc"""
applycal!(visibilities, metadata, calibration)
*Description*
Apply the calibration to the given visibilities.
That is if we have the model visibility $V_{i,j}$ on baseline $i,j$, and the
corresponding Jones matrices $J_i$ and $J_j$ for antennas $i$ and $j$ compute
$$V_{i,j} \rightarrow J_i^{-1} V_{i,j} (J_j^{-1})^*$$
*Arguments*
* `visibilities` - the list of visibilities to corrupt
* `metadata` - the metadata describing the interferometer
* `calibration` - the calibration in question
"""
function applycal!(visibilities::Visibilities, meta::Metadata, calibration::Calibration)
inverse_cal = invert(calibration)
corrupt!(visibilities, meta, inverse_cal)
visibilities
end
doc"""
invert(calibration)
Returns the inverse of the given calibration.
The Jones matrices $J$ of each antenna is set to $J^{-1}$.
"""
function invert(calibration::Calibration)
output = similar(calibration)
for i in eachindex( output.jones, output.flags,
calibration.jones, calibration.flags)
output.jones[i] = inv(calibration.jones[i])
output.flags[i] = calibration.flags[i]
end
output
end
# gaincal / polcal
"""
gaincal(visibilities, metadata, beam, sources)
gaincal(visibilities, metadata, model_visibilities)
*Description*
Solve for the interferometer's electronic gains.
*Arguments*
* `visibilities` - the visibilities measured by the interferometer
* `metadata` - the metadata describing the interferometer
* `beam` - the primary beam model
* `sources` - a list of sources comprising the sky model
* `model_visibilities` - alternatively the sky model visibilities can be provided
*Keyword Arguments*
* `maxiter` - the maximum number of iterations to take on each frequency channel (defaults to `20`)
* `tolerance` - the relative tolerance used to test for convergence (defaults to `1e-3`)
* `quiet` - suppresses printing if set to `true` (defaults to `false`)
"""
function gaincal{S<:Source}(visibilities::Visibilities, meta::Metadata, beam::BeamModel, sources::Vector{S};
maxiter = 20, tolerance = 1e-3, quiet = false)
frame = reference_frame(meta)
sources = abovehorizon(frame, sources)
model = genvis(meta, beam, sources)
gaincal(visibilities, meta, model, maxiter=maxiter, tolerance=tolerance, quiet=quiet)
end
function gaincal(visibilities::Visibilities, meta::Metadata, model::Visibilities;
maxiter = 20, tolerance = 1e-3, quiet = false)
calibration = GainCalibration(Nant(meta), Nfreq(meta))
solve!(calibration, visibilities, model, meta, maxiter, tolerance, quiet)
calibration
end
"""
polcal(visibilities, metadata, beam, sources)
polcal(visibilities, metadata, model_visibilities)
*Description*
Solve for the polarization properties of the interferometer.
*Arguments*
* `visibilities` - the visibilities measured by the interferometer
* `metadata` - the metadata describing the interferometer
* `sources` - a list of sources comprising the sky model
* `beam` - the primary beam model
* `model_visibilities` - alternatively the sky model visibilities can be provided
*Keyword Arguments*
* `maxiter` - the maximum number of iterations to take on each frequency channel (defaults to `20`)
* `tolerance` - the relative tolerance used to test for convergence (defaults to `1e-3`)
* `quiet` - suppresses printing if set to `true` (defaults to `false`)
"""
function polcal{S<:Source}(visibilities::Visibilities, meta::Metadata, beam::BeamModel, sources::Vector{S};
maxiter::Int = 20, tolerance::Float64 = 1e-3, quiet = false)
frame = reference_frame(meta)
sources = abovehorizon(frame, sources)
model = genvis(meta, beam, sources)
polcal(visibilities, meta, model, maxiter=maxiter, tolerance=tolerance, quiet=quiet)
end
function polcal(visibilities::Visibilities, meta::Metadata, model::Visibilities;
maxiter = 20, tolerance = 1e-3, quiet = false)
calibration = PolarizationCalibration(Nant(meta), Nfreq(meta))
solve!(calibration, visibilities, model, meta, maxiter, tolerance, quiet)
calibration
end
function solve!(calibration, measured_visibilities, model_visibilities, metadata, maxiter, tolerance, quiet)
println(2)
square_measured, square_model = makesquare(measured_visibilities, model_visibilities, metadata)
println(2)
quiet || (p = Progress(Nfreq(metadata), "Calibrating: "))
for β = 1:Nfreq(metadata)
@show β
solve_onechannel!(view(calibration.jones, :, β),
view(calibration.flags, :, β),
view(square_measured, :, :, β),
view(square_model, :, :, β),
maxiter, tolerance)
quiet || next!(p)
end
if !quiet
# Print a summary of the flags
flagged_channels = sum(all(calibration.flags, 1))
flagged_antennas = sum(all(calibration.flags, 2))
percentage = 100 * sum(calibration.flags) / length(calibration.flags)
@printf("(%d antennas flagged, %d channels flagged, %0.2f percent total)\n",
flagged_antennas, flagged_channels, percentage)
end
end
"Solve one channel at a time."
function solve_onechannel!(jones, flags, measured, model, maxiter, tolerance)
converged = iterate(RK4(stefcal_step), maxiter, tolerance, jones, measured, model)
flag_solution!(jones, flags, measured, model, converged)
jones
end
"Solve with one solution for all channels."
function solve_allchannels!(calibration, measured, model, metadata, maxiter, tolerance)
G = view(calibration.jones, :, 1)
F = view(calibration.flags, :, 1)
V, M = makesquare(measured, model, metadata)
converged = iterate(RK4(stefcal_step), maxiter, tolerance, G, V, M)
flag_solution!(G, F, V, M, converged)
calibration
end
doc"""
Pack the visibilities into a square Hermitian matrices such that
the visibilities are ordered as follows:
$$\begin{pmatrix}
V_{11} & V_{12} & V_{13} & & \\\\
V_{21} & V_{22} & V_{23} & & \\\\
V_{31} & V_{32} & V_{33} & & \\\\
& & & \ddots & \\\\
\end{pmatrix}$$
Auto-correlations and flagged cross-correlations are set to zero.
"""
function makesquare(measured, model, meta)
output_measured = zeros(JonesMatrix, Nant(meta), Nant(meta), Nfreq(meta))
output_model = zeros(JonesMatrix, Nant(meta), Nant(meta), Nfreq(meta))
for β = 1:Nfreq(meta), α = 1:Nbase(meta)
measured.flags[α,β] && continue
antenna1 = meta.baselines[α].antenna1
antenna2 = meta.baselines[α].antenna2
antenna1 == antenna2 && continue
output_measured[antenna1,antenna2,β] = measured.data[α,β]
output_measured[antenna2,antenna1,β] = measured.data[α,β]'
output_model[antenna1,antenna2,β] = model.data[α,β]
output_model[antenna2,antenna1,β] = model.data[α,β]'
end
output_measured, output_model
end
"""
Inspect the calibration solution and apply flags as necessary.
"""
function flag_solution!(jones, flags, measured, model, converged)
Nant = length(jones)
# Flag everything if the solution did not converge.
if !converged
flags[:] = true
return flags
end
# Find flagged antennas by looking for columns of zeros.
for j = 1:Nant
isflagged = true
for i = 1:Nant
if measured[i,j] != zero(JonesMatrix)
isflagged = false
break
end
end
if isflagged
flags[j] = true
end
end
flags
end
"""
fixphase!(calibration, reference_antenna)
**Description**
Set the phase of the reference antenna and polarization to zero.
*Arguments*
* `calibration` - the calibration that will have its phase adjusted
* `reference_antenna` - a string containing the antenna number and polarization
whose phase will be chosen to be zero (eg. "14y" or "62x")
"""
function fixphase!(cal::Calibration, reference_antenna)
regex = r"(\d+)(x|y)"
m = match(regex,reference_antenna)
refant = parse(Int,m.captures[1])
refpol = m.captures[2] == "x"? 1 : 2
for β = 1:Nfreq(cal)
ref = refpol == 1? cal.jones[refant,β].xx : cal.jones[refant,β].yy
factor = conj(ref) / abs(ref)
for ant = 1:Nant(cal)
cal.jones[ant,β] = cal.jones[ant,β]*factor
end
end
cal
end
# step functions
doc"""
stefcal_step(input, measured, model)
**Description**
Given the `measured` and `model` visibilities, and the current
guess for the Jones matrices, solve for `step` such
that the new value of the Jones matrices is `input+step`.
The update step is defined such that the new value of the
Jones matrices minimizes
$$\sum_{i,j}\|V_{i,j} - J_i M_{i,j} J_{j,\rm new}^*\|^2,$$
where $i$ and $j$ label the antennas, $V$ labels the measured
visibilities, $M$ labels the model visibilities, and $J$
labels the Jones matrices.
*References*
* Michell, D. et al. 2008, JSTSP, 2, 5.
* Salvini, S. & Wijnholds, S. 2014, A&A, 571, 97.
"""
function stefcal_step{T}(input::AbstractVector{T}, measured::Matrix, model::Matrix)
Nant = length(input) # number of antennas
step = similar(input)
@inbounds for j = 1:Nant
numerator = zero(T)
denominator = zero(T)
for i = 1:Nant
GM = input[i]*model[i,j]
V = measured[i,j]
numerator += inner_multiply(T, GM, V)
denominator += inner_multiply(T, GM, GM)
end
ok = abs(det(denominator)) > eps(Float64)
step[j] = ifelse(ok, (denominator\numerator)' - input[j], zero(T))
end
step
end
function stefcal_step{T}(input::AbstractVector{T}, measured, model)
# This version is called if `measured` and `model` have 3 dimensions
# indicating that we want to use multiple integrations or multiple
# frequency channels to solve for the calibration.
Nant = length(input) # number of antennas
Nint = size(measured, 3) # number of integrations
@show "stefcal", Nant, Nint
step = similar(input)
@inbounds for j = 1:Nant
numerator = zero(T)
denominator = zero(T)
for t = 1:Nint, i = 1:Nant
GM = input[i]*model[i,j,t]
V = measured[i,j,t]
numerator += inner_multiply(T, GM, V)
denominator += inner_multiply(T, GM, GM)
end
ok = abs(det(denominator)) > eps(Float64)
step[j] = ifelse(ok, (denominator\numerator)' - input[j], zero(T))
end
step
end
@inline function inner_multiply(::Type{DiagonalJonesMatrix}, X, Y)
DiagonalJonesMatrix(X.xx'*Y.xx + X.yx'*Y.yx,
X.xy'*Y.xy + X.yy'*Y.yy)
end
@inline inner_multiply(::Type{JonesMatrix}, X, Y) = X'*Y