CygnusRFI/rfi_plotter.py

92 lines
3.3 KiB
Python
Raw Normal View History

2019-12-28 06:03:06 -07:00
#!/usr/bin/env python
2020-02-21 19:53:50 -07:00
try:
2019-12-28 06:03:06 -07:00
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
2020-02-21 17:47:29 -07:00
plt.rcParams.update({'font.size': 32})
2019-12-28 06:03:06 -07:00
import argparse
from matplotlib.gridspec import GridSpec
2020-02-21 17:47:29 -07:00
parser = argparse.ArgumentParser()
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')
args = parser.parse_args()
2019-12-28 06:03:06 -07:00
def decibel(x):
2020-02-21 17:47:29 -07:00
return 10.0*np.log10(x)
#return x
2019-12-28 06:03:06 -07:00
if __name__ == "__main__":
#Observation parameters
exec(args.freq)
exec(args.samp_rate)
exec(args.nchan)
exec(args.nbin)
2020-02-21 17:47:29 -07:00
exec(args.n)
exec(args.dur)
exec(args.fminimum)
exec(args.fmaximum)
2019-12-28 06:03:06 -07:00
frequency=freq
#Load data
2020-02-21 17:47:29 -07:00
z=1000*(np.fromfile("0.dat", dtype="float32").reshape(-1, nchan)/nbin)
2019-12-28 06:03:06 -07:00
allarrays=[]
2020-02-21 19:53:50 -07:00
#N = amount of .dat files
2020-02-21 17:47:29 -07:00
for i in range(int(n)):
final = 1000*(np.fromfile(str(i)+".dat", dtype="float32").reshape(-1, nchan)/nbin)
2019-12-28 06:03:06 -07:00
allarrays.append(final)
z_final = np.concatenate(allarrays,axis=1)
#Number of sub-integrations
nsub = z_final.shape[0]
#Compute average spectrum
zmean = np.mean(z_final, axis=0)
#Compute time axis
tint = float(nbin*nchan)/samp_rate
t = tint*np.arange(nsub)
#Compute frequency axis (convert to MHz)
allfreq=[]
2020-02-21 17:47:29 -07:00
for i in range(int(n)):
2019-12-28 06:03:06 -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
allfreq.append(freq)
freq = np.concatenate(allfreq)
2020-02-21 19:53:50 -07:00
2019-12-28 06:03:06 -07:00
#Initialize plot
2020-02-21 17:47:29 -07:00
fig = plt.figure(figsize=(5*n,20.25))
2020-02-21 19:53:50 -07:00
gs = GridSpec(1,1)
2019-12-28 06:03:06 -07:00
#Plot average spectrum
ax1 = fig.add_subplot(gs[0,0])
2020-02-21 17:47:29 -07:00
ax1.plot(freq, decibel(zmean), '#3182bd')
ax1.fill_between(freq, decibel(zmean), color='#deebf7')
2019-12-28 06:03:06 -07:00
ax1.set_xlim(np.min(freq), np.max(freq)) #np.min(freq), np.max(freq)
2020-02-21 17:47:29 -07:00
ax1.set_ylim(np.amin(decibel(zmean))-0.5, np.amin(decibel(zmean))+15)
2019-12-28 06:03:06 -07:00
ax1.ticklabel_format(useOffset=False)
2020-02-21 17:47:29 -07:00
ax1.set_xlabel("Frequency (MHz)", labelpad=25)
ax1.set_ylabel("Relative Power (dB)", labelpad=25)
2020-03-02 16:50:12 -07:00
try:
ax1.set_title("\nAveraged RFI Spectrum", pad=25)
except:
ax1.set_title("\nAveraged RFI Spectrum")
2020-02-21 17:47:29 -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')
ax1.grid()
2019-12-28 06:03:06 -07:00
plt.tight_layout()
plt.savefig("rfi_plot.png")
2020-02-21 19:53:50 -07:00
except Exception as e:
print(e)
pass