2023-01-17 12:41:46 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
try:
|
|
|
|
import matplotlib
|
2023-01-21 10:59:08 -07:00
|
|
|
|
|
|
|
matplotlib.use("Agg")
|
|
|
|
|
2023-01-17 12:41:46 -07:00
|
|
|
import numpy as np
|
|
|
|
import matplotlib.pyplot as plt
|
2023-01-21 10:59:08 -07:00
|
|
|
|
|
|
|
plt.rcParams.update({"font.size": 32})
|
2023-01-17 12:41:46 -07:00
|
|
|
import argparse
|
|
|
|
from matplotlib.gridspec import GridSpec
|
2023-01-21 10:59:08 -07:00
|
|
|
|
2023-01-17 12:41:46 -07:00
|
|
|
parser = argparse.ArgumentParser()
|
2023-01-21 10:59:08 -07:00
|
|
|
parser.add_argument("freq")
|
|
|
|
parser.add_argument("samp_rate")
|
|
|
|
parser.add_argument("nchan")
|
|
|
|
parser.add_argument("nbin")
|
|
|
|
parser.add_argument("n")
|
|
|
|
parser.add_argument("dur")
|
|
|
|
parser.add_argument("fminimum")
|
|
|
|
parser.add_argument("fmaximum")
|
2023-01-17 12:41:46 -07:00
|
|
|
args = parser.parse_args()
|
2023-01-21 10:59:08 -07:00
|
|
|
|
2023-01-17 12:41:46 -07:00
|
|
|
def decibel(x):
|
2023-01-21 10:59:08 -07:00
|
|
|
return 10.0 * np.log10(x)
|
|
|
|
# return x
|
|
|
|
|
2023-01-17 12:41:46 -07:00
|
|
|
if __name__ == "__main__":
|
2023-01-21 10:59:08 -07:00
|
|
|
# Observation parameters
|
2023-01-17 12:41:46 -07:00
|
|
|
exec(args.freq)
|
|
|
|
exec(args.samp_rate)
|
|
|
|
exec(args.nchan)
|
|
|
|
exec(args.nbin)
|
|
|
|
exec(args.n)
|
|
|
|
exec(args.dur)
|
|
|
|
exec(args.fminimum)
|
|
|
|
exec(args.fmaximum)
|
2023-01-21 10:59:08 -07:00
|
|
|
|
|
|
|
frequency = freq
|
|
|
|
|
|
|
|
# Load data
|
|
|
|
z = 1000 * (np.fromfile("0.dat", dtype="float32").reshape(-1, nchan) / nbin)
|
|
|
|
allarrays = []
|
|
|
|
|
|
|
|
# N = amount of .dat files
|
2023-01-17 12:41:46 -07:00
|
|
|
for i in range(int(n)):
|
2023-01-21 10:59:08 -07:00
|
|
|
final = 1000 * (
|
|
|
|
np.fromfile(str(i) + ".dat", dtype="float32").reshape(-1, nchan) / nbin
|
|
|
|
)
|
2023-01-17 12:41:46 -07:00
|
|
|
allarrays.append(final)
|
2023-01-21 10:59:08 -07:00
|
|
|
z_final = np.concatenate(allarrays, axis=1)
|
|
|
|
|
|
|
|
# Number of sub-integrations
|
2023-01-17 12:41:46 -07:00
|
|
|
nsub = z_final.shape[0]
|
2023-01-21 10:59:08 -07:00
|
|
|
|
|
|
|
# Compute average spectrum
|
2023-01-17 12:41:46 -07:00
|
|
|
zmean = np.mean(z_final, axis=0)
|
2023-01-21 10:59:08 -07:00
|
|
|
|
|
|
|
# Compute time axis
|
|
|
|
tint = float(nbin * nchan) / samp_rate
|
|
|
|
t = tint * np.arange(nsub)
|
|
|
|
|
|
|
|
# Compute frequency axis (convert to MHz)
|
|
|
|
allfreq = []
|
2023-01-17 12:41:46 -07:00
|
|
|
for i in range(int(n)):
|
2023-01-21 10:59:08 -07:00
|
|
|
freq = (
|
|
|
|
np.linspace(
|
|
|
|
(frequency + samp_rate * i) - 0.5 * samp_rate,
|
|
|
|
(frequency + samp_rate * i) + 0.5 * samp_rate,
|
|
|
|
nchan,
|
|
|
|
endpoint=False,
|
|
|
|
)
|
|
|
|
* 1e-6
|
|
|
|
)
|
2023-01-17 12:41:46 -07:00
|
|
|
allfreq.append(freq)
|
|
|
|
freq = np.concatenate(allfreq)
|
2023-01-21 10:59:08 -07:00
|
|
|
|
|
|
|
# Initialize plot
|
|
|
|
fig = plt.figure(figsize=(5 * n, 20.25))
|
|
|
|
gs = GridSpec(1, 1)
|
|
|
|
|
|
|
|
# Plot average spectrum
|
|
|
|
ax1 = fig.add_subplot(gs[0, 0])
|
|
|
|
ax1.plot(freq, decibel(zmean), "#3182bd")
|
|
|
|
ax1.fill_between(freq, decibel(zmean), color="#deebf7")
|
|
|
|
ax1.set_xlim(np.min(freq), np.max(freq)) # np.min(freq), np.max(freq)
|
|
|
|
ax1.set_ylim(np.amin(decibel(zmean)) - 0.5, np.amin(decibel(zmean)) + 15)
|
2023-01-17 12:41:46 -07:00
|
|
|
ax1.ticklabel_format(useOffset=False)
|
|
|
|
ax1.set_xlabel("Frequency (MHz)", labelpad=25)
|
|
|
|
ax1.set_ylabel("Relative Power (dB)", labelpad=25)
|
|
|
|
try:
|
|
|
|
ax1.set_title("\nAveraged RFI Spectrum", pad=25)
|
|
|
|
except:
|
|
|
|
ax1.set_title("\nAveraged RFI Spectrum")
|
2023-01-21 10:59:08 -07:00
|
|
|
ax1.annotate(
|
|
|
|
"Frequency range scanned: "
|
|
|
|
+ str(float(fminimum) / 1000000)
|
|
|
|
+ "-"
|
|
|
|
+ str(float(fmaximum) / 1000000)
|
|
|
|
+ " MHz ($\\Delta\\nu$ = "
|
|
|
|
+ str(float((fmaximum) - float(fminimum)) / 1000000)
|
|
|
|
+ " MHz)\nBandwidth per spectrum: "
|
|
|
|
+ str(float(samp_rate) / 1000000)
|
|
|
|
+ " MHz\nIntegration time per spectrum: "
|
|
|
|
+ str(dur)
|
|
|
|
+ " sec\nNumber of channels per spectrum (FFT size): "
|
|
|
|
+ str(nchan),
|
|
|
|
xy=(17, 1179),
|
|
|
|
xycoords="axes points",
|
|
|
|
size=32,
|
|
|
|
ha="left",
|
|
|
|
va="top",
|
|
|
|
color="brown",
|
|
|
|
)
|
2023-01-17 12:41:46 -07:00
|
|
|
ax1.grid()
|
2023-01-21 10:59:08 -07:00
|
|
|
|
2023-01-17 12:41:46 -07:00
|
|
|
plt.tight_layout()
|
|
|
|
plt.savefig("rfi_plot.png")
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
pass
|