/
find_radar_mod.py
executable file
·275 lines (229 loc) · 9.96 KB
/
find_radar_mod.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#! /usr/bin/env python
import sys
import os.path
import argparse
import warnings
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import infodata
DTYPE = 'float32'
def compute_maxpows(data, inf, winlen):
nblocks = int(np.ceil(inf.N*inf.dt/winlen))
nbin = inf.N/float(nblocks)
# Compute maximum Fourier power for each block of time series data
maxpows = np.zeros(nblocks)
for iblock in xrange(nblocks):
istart = int(np.round(iblock*nbin))
iend = int(np.round((iblock+1)*nbin))
powers = np.abs(np.fft.rfft(data[istart:iend]))**2
maxpows[iblock] = np.max(powers[1:])
imax = np.argmax(powers[1:])
maxpows[-1] = np.median(maxpows[:-1])
# Scale the maximum powers
maxpows /= np.median(maxpows)
#maxpows /=np.median(powers)
return maxpows
def compute_minpows(data, inf, winlen):
nblocks = int(np.ceil(inf.N*inf.dt/winlen))
nbin = inf.N/float(nblocks)
# Compute maximum Fourier power for each block of time series data
minpows = np.zeros(nblocks)
medpows = np.zeros(nblocks)
variance = np.zeros(nblocks)
for iblock in xrange(nblocks):
istart = int(np.round(iblock*nbin))
iend = int(np.round((iblock+1)*nbin))
#powers = np.abs(np.fft.rfft(data[istart:iend]))**2
powers = np.abs(data[istart:iend])
minpows[iblock] = np.min(powers[1:])
medpows[iblock] = np.median(powers[1:])
variance[iblock] = np.var(powers[1:])
imin = np.argmin(powers[1:])
minpows[-1] = np.median(minpows[:-1])
minpows -= medpows
# Scale the minimum powers
minpows /= np.median(minpows)
variance /= np.median(variance)
print minpows[:25]
print variance[:25]
#return minpows, medpows
return variance, medpows
def apply_mask_to_maxpows(data, maxpows, thresh):
nblocks = len(maxpows)
nbin = len(data)/float(nblocks)
# Generate a mask based on block-wise Fourier power
# being larger than a threshold
mask = np.zeros(len(data), dtype=bool)
for iblock in xrange(len(maxpows)):
istart = int(np.round(iblock*nbin))
iend = int(np.round((iblock+1)*nbin))
if maxpows[iblock] > thresh:
mask[istart:iend] = 1
masked = np.ma.masked_array(data, mask=mask, fill_value=np.median(data))
return masked
def apply_mask_to_minpows(data, minpows, medpows, thresh):
nblocks = len(minpows)
nbin = len(data)/float(nblocks)
# Generate a mask based on block-wise Fourier power
# being larger than a threshold
mask = np.zeros(len(data), dtype=bool)
block_to_mask = []
for iblock in xrange(len(minpows)):
istart = int(np.round(iblock*nbin))
iend = int(np.round((iblock+1)*nbin))
if minpows[iblock] > thresh:
mask[istart:iend] = 1
block_to_mask.append(iblock)
masked = np.ma.masked_array(data, mask=mask, fill_value=np.median(data))
return masked, mask, block_to_mask
def write_masked_dm0_timeseries(masked, outbasenm):
maskedfn = outbasenm+"_noradar.dat"
(masked.filled(fill_value=np.ma.median(masked))).tofile(maskedfn)
# Create soft link for inf file
inflink = maskedfn[:-4]+".inf"
if not os.path.exists(inflink):
os.symlink(outbasenm+".inf", inflink)
def write_radar_intervals(masked, outbasenm):
# Invert the mask
inverted = invert_mask(masked)
slices = np.ma.flatnotmasked_contiguous(inverted)
if slices:
towrite = np.empty((len(slices), 2))
for ii, badslice in enumerate(slices):
towrite[ii] = (badslice.start-1000, badslice.stop+1000)
#radar_samples = np.flatnonzero(np.ma.getmaskarray(data))
with open(outbasenm+"_radar_samples.txt", 'w') as ff:
ff.write("# Samples containing the radar\n")
ff.write("# Intervals are samples to remove 'start:stop' (inclusive!)\n")
ff.write("# First sample number is 0\n")
ff.write("# Number of data samples to remove: %d of %d (%.2g %%)\n" %
(inverted.count(), len(inverted), 100.0*inverted.count()/len(inverted)))
if slices:
np.savetxt(ff, towrite, "%d", delimiter=':')
return inverted
def invert_mask(masked):
return np.ma.masked_array(masked.data, mask=~masked.mask)
def validate_timeseries(inf):
if inf.bary:
raise ValueError("Radar removal should only be performed " \
"using topocentric time series as input!")
if inf.DM != 0:
raise ValueError("Radar removal should only be performed " \
"using a DM=0 pc/cc time series as input!")
def plot_timeseries_comparison(masked, inf):
fig = plt.figure(figsize=(16,4))
times = np.arange(len(masked))*inf.dt
warnings.warn("Only plotting every 10th point of time series.")
plt.plot(times[::10], masked.data[::10], 'k-', drawstyle='steps-post',
label='Time series', zorder=1)
inverted = invert_mask(masked)
slices = np.ma.flatnotmasked_contiguous(inverted)
if slices:
for ii, badslice in enumerate(slices):
if ii == 0:
label='Radar indentified'
else:
label="_nolabel"
tstart = inf.dt*(badslice.start)
tstop = inf.dt*(badslice.stop-1)
plt.axvspan(tstart, tstop, alpha=0.5,
fc='r', ec='none', zorder=0, label=label)
plt.figtext(0.02, 0.02,
"Frac. of data masked: %.2f %%" % ((len(masked)-masked.count())/float(len(masked))*100),
size='x-small')
plt.figtext(0.02, 0.05, inf.basenm, size='x-small')
plt.xlabel("Time (s)")
plt.ylabel("Intensity")
plt.xlim(0, times.max()+inf.dt)
plt.subplots_adjust(bottom=0.15, left=0.075, right=0.98)
def plot_powerspec_comparison(masked, inf):
fig = plt.figure(figsize=(16,4))
# Make diagnostic plots
freqs = np.fft.fftfreq(int(inf.N), inf.dt)
nfft = int(inf.N/2)
freqs = freqs[:nfft]
plt.plot(freqs, np.abs(np.fft.rfft(masked.data)[:nfft])**2, 'r-')
plt.plot(freqs, np.abs(np.fft.rfft(masked.filled(fill_value=np.ma.median(masked)))[:nfft])**2, 'k-')
plt.xlabel("Freq (Hz)")
plt.ylabel("Raw Power")
plt.xscale('log')
plt.yscale('log')
plt.figtext(0.02, 0.02,
"Frac. of data masked: %.2f %%" % ((len(masked)-masked.count())/float(len(masked))*100),
size='x-small')
plt.figtext(0.02, 0.05, inf.basenm, size='x-small')
plt.xlim(0.066, 2000)
plt.subplots_adjust(bottom=0.15, left=0.075, right=0.98)
def read_datfile(fn):
# Get input dat/inf file names
if not fn.endswith(".dat"):
raise ValueError("Input file must be a PRESTO '.dat' file!")
inffn = fn[:-4]+'.inf'
# Read inf/dat files
inf = infodata.infodata(inffn)
rawdata = np.fromfile(fn, dtype=DTYPE, count=inf.N)
#validate_timeseries(inf)
return rawdata, inf
def main():
fn = args.infn
if args.outbasenm is None:
outbasenm = fn[:-4] # Trim off '.dat'
else:
outbasenm = args.outbasenm
rawdata, inf = read_datfile(fn)
maxpows = compute_maxpows(rawdata, inf, args.winlen)
data = apply_mask_to_maxpows(rawdata, maxpows, args.maxthresh)
minpows, medpows = compute_minpows(data, inf, args.winlen)
data, mask, blocks_to_mask = apply_mask_to_minpows(data, minpows, medpows, args.minthresh)
print "Frac. of data masked: %.2f %%" % ((len(data)-data.count())/float(len(data))*100),
# Write out masked file
#write_masked_dm0_timeseries(data, outbasenm)
# Write out list of masked samples
write_radar_intervals(data, outbasenm)
if args.plot_timeseries:
plot_timeseries_comparison(data, inf)
try:
plt.savefig(outbasenm+"_timeseries_compare.png")
except:
pass
if args.plot_powerspec:
plot_powerspec_comparison(data, inf)
try:
plt.savefig(outbasenm+"_powerspec_compare.png")
except:
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Find radar in PALFA data.")
parser.add_argument("-o", "--outbasenm", dest='outbasenm', default=None,
help="Base name of the output file containing " \
"list of intervals to clip. " \
"(Default: <inputfn>+'_radar_samples.txt'")
parser.add_argument("-w", "--window-length", dest='winlen', default=0.25,
type=float,
help="Duration of window length in seconds. " \
"(Default: 0.25 s)")
parser.add_argument("-t", "--minthreshold", dest='minthresh', default=1.5,
type=float,
help="Threshold of (min power -median power) / median power " \
"above which an interval is considered " \
"to be contaminated by the dips in the time series. " \
"(Default: 1.5)")
parser.add_argument("-T", "--maxthreshold", dest='maxthresh', default=3,
type=float,
help="Threshold of max power / median power " \
"above which an interval is considered " \
"to be contaminated by the radar. " \
"(Default: 3)")
parser.add_argument("--no-timeseries-plot", dest="plot_timeseries", action="store_false",
help="Do not plot time series comparison. " \
"(Default: create time series plot)")
parser.add_argument("--no-powerspec-plot", dest="plot_powerspec", action="store_false",
help="Do not plot power spectrum comparison. " \
"(Default: create power spectrum plot)")
parser.add_argument("infn",
help="Topocentric time series at DM=0 pc/cc to use " \
"to identify intervals containing the radar.")
args = parser.parse_args()
main()