-
Notifications
You must be signed in to change notification settings - Fork 3
/
smoothcal_a2256_nophasors.py
executable file
·167 lines (114 loc) · 4.9 KB
/
smoothcal_a2256_nophasors.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import pyrap.tables as pt
import os
import lofar.parmdb
import numpy as numpy
import math
#import lofar.expion.parmdbmain
import scipy
import scipy.signal
#import matplotlib.pyplot as plt
def median_smooth(ampl, half_window):
ampl_tot_copy = numpy.copy(ampl)
ndata = len(ampl)
flags = numpy.zeros(ndata, dtype=bool)
sol = numpy.zeros(ndata + 2 * half_window)
sol[half_window:half_window + ndata] = ampl
for i in range(0, half_window):
# Mirror at left edge.
idx = min(ndata - 1, half_window - i)
sol[i] = ampl[idx]
# Mirror at right edge
idx = max(0, ndata - 2 - i)
sol[ndata + half_window + i] = ampl[idx]
# fix oct 2012
median_array = scipy.signal.medfilt(sol, half_window * 2. - 1)
ampl_tot_copy = median_array[half_window:ndata + half_window]
return ampl_tot_copy
def median_window_filter(ampl, half_window, threshold):
ampl_tot_copy = numpy.copy(ampl)
ndata = len(ampl)
flags = numpy.zeros(ndata, dtype=bool)
sol = numpy.zeros(ndata+2*half_window)
sol[half_window:half_window+ndata] = ampl
for i in range(0, half_window):
# Mirror at left edge.
idx = min(ndata-1, half_window-i)
sol[i] = ampl[idx]
# Mirror at right edge
idx = max(0, ndata-2-i)
sol[ndata+half_window+i] = ampl[idx]
#fix oct 2012
median_array = scipy.signal.medfilt(sol,half_window*2-1)
sol_flag = numpy.zeros(ndata+2*half_window, dtype=bool)
sol_flag_val = numpy.zeros(ndata+2*half_window, dtype=bool)
for i in range(half_window, half_window + ndata):
# Compute median of the absolute distance to the median.
window = sol[i-half_window:i+half_window+1]
window_flag = sol_flag[i-half_window:i+half_window+1]
window_masked = window[~window_flag]
if len(window_masked) < math.sqrt(len(window)):
# Not enough data to get accurate statistics.
continue
median = numpy.median(window_masked)
q = 1.4826 * numpy.median(numpy.abs(window_masked - median))
# Flag sample if it is more than 1.4826 * threshold * the
# median distance away from the median.
if abs(sol[i] - median) > (threshold * q):
sol_flag[i] = True
idx = numpy.where(sol == 0.0) # to remove 1.0 amplitudes
#print idx
#print 'sol', sol
sol[idx] = True
mask = sol_flag[half_window:half_window + ndata]
for i in range(len(mask)):
if mask[i]:
ampl_tot_copy[i] = median_array[half_window+i] # fixed 2012
return ampl_tot_copy
msname = str(sys.argv[1])
instrument_name = str(sys.argv[2])
instrument_name_smoothed = str(sys.argv[3]) # msname +'.instrument_smoothed'
##### EDIT THESE PARAMETERS BELOW #####
pol_list = ['0:0','1:1']
gain = 'Gain'
# I suggest to leave the above untouched !!
output_median = True # if True the will be replaced with the median, using half_window2
# if False than only the ampltudes will be filtered for outliers
output_phasezero = True # if True the phases will be set to zero (for amplitude solutions transfer
# if False the phases will be left untouched
#######################################
pdb = lofar.parmdb.parmdb(instrument_name)
parms = pdb.getValuesGrid('*')
key_names = parms.keys()
print key_names
anttab = pt.table(msname + '/ANTENNA')
antenna_list = anttab.getcol('NAME')
anttab.close()
print 'Stations available:', antenna_list
window = 4
for pol in pol_list:
for antenna in antenna_list:
print 'smoothing [antenna, polarization]:', antenna, pol
#amp = numpy.copy(parms[gain + ':' + pol + ':Ampl:'+ antenna]['values'][:, 0])
real = numpy.copy(parms[gain + ':' + pol + ':Real:'+ antenna]['values'][:, 0])
imag = numpy.copy(parms[gain + ':' + pol + ':Imag:'+ antenna]['values'][:, 0])
phase = numpy.arctan2(imag,real)
amp = numpy.sqrt(imag**2 + real**2)
window_window = numpy.int(len(amp)/3.)
amp = numpy.log10(amp)
amp = median_window_filter(amp,window,6)
amp = median_window_filter(amp,window,6)
amp = median_window_filter(amp,7,6) # window of 7
amp = median_window_filter(amp,4,6) # window of 4
amp = median_window_filter(amp,3,6) # window of 3
amp = 10**amp
parms[gain + ':' + pol + ':Real:'+ antenna]['values'][:, 0] = amp*numpy.cos(phase)
parms[gain + ':' + pol + ':Imag:'+ antenna]['values'][:, 0] = amp*numpy.sin(phase)
print 'writing the new database:', instrument_name_smoothed
print 'check your results with: parmdbplot.py', instrument_name_smoothed
print 'compare with: parmdbplot.py', instrument_name
pdbnew = lofar.parmdb.parmdb(instrument_name_smoothed, create=True)
pdbnew.addValues(parms)
pdbnew.flush()