From 489678e3218e82409ab07509a2c41b90e09fb764 Mon Sep 17 00:00:00 2001 From: Cees Bassa Date: Sat, 18 Jun 2022 18:01:38 +0200 Subject: [PATCH] Nicer plotting, proper use of datetime --- rfplot.py | 25 ++++++++++++++++++++++--- strf/rfio.py | 20 +++++++++++--------- strf/rfio.py~ | 37 ------------------------------------- 3 files changed, 33 insertions(+), 49 deletions(-) delete mode 100644 strf/rfio.py~ diff --git a/rfplot.py b/rfplot.py index 2940c54..e7630e7 100644 --- a/rfplot.py +++ b/rfplot.py @@ -3,6 +3,8 @@ import numpy as np from strf.rfio import Spectrogram import matplotlib.pyplot as plt +import matplotlib.dates as mdates + if __name__ == "__main__": # Settings @@ -17,8 +19,25 @@ if __name__ == "__main__": # Create plot vmin, vmax = np.percentile(s.z, (5, 99.95)) - fig, ax = plt.subplots() - ax.imshow(s.z, origin="lower", aspect="auto", interpolation="None", - vmin=vmin, vmax=vmax) + # Time limits + tmin, tmax = mdates.date2num(s.t[0]), mdates.date2num(s.t[-1]) + # Frequency limits + fcen = np.mean(s.freq) + fmin, fmax = (s.freq[0] - fcen) * 1e-6, (s.freq[-1] - fcen) * 1e-6 + + fig, ax = plt.subplots(figsize=(10, 6)) + ax.imshow(s.z, origin="lower", aspect="auto", interpolation="None", + vmin=vmin, vmax=vmax, + extent=[tmin, tmax, fmin, fmax]) + + ax.xaxis_date() + date_format = mdates.DateFormatter("%F\n%H:%M:%S") + ax.xaxis.set_major_formatter(date_format) + fig.autofmt_xdate(rotation=0, ha="center") + + ax.set_xlabel("Time (UTC)") + ax.set_ylabel(f"Frequency (MHz) - {fcen * 1e-6:g} MHz") + + plt.show() diff --git a/strf/rfio.py b/strf/rfio.py index c8be4cc..aff1aca 100644 --- a/strf/rfio.py +++ b/strf/rfio.py @@ -2,10 +2,10 @@ import os import re -from datetime import datetime - import numpy as np +from datetime import datetime, timedelta + class Spectrogram: """Spectrogram class""" @@ -13,32 +13,34 @@ class Spectrogram: """Define a spectrogram""" # Read first file to get number of channels - fname = os.path.join(path, "{:s}_{:06d}.bin".format(prefix, ifile)) + fname = os.path.join(path, f"{prefix}_{ifile:06d}.bin") with open(fname, "rb") as fp: header = parse_header(fp.read(256)) # Set frequencies - freq = np.linspace(-0.5*header["bw"], 0.5*header["bw"], header["nchan"], endpoint=False)+header["freq"]+0.5*header["bw"]/header["nchan"] + freq_cen, bw, nchan = header["freq"], header["bw"], header["nchan"] + freq_min, freq_max = -0.5 * bw, 0.5 * bw + freq = freq_cen + np.linspace(freq_min, freq_max, nchan, endpoint=False) + freq_max / nchan # Loop over subints and files zs = [] - mjds = [] + t = [] isub = 0; while isub