Nicer plotting, proper use of datetime
parent
116153c784
commit
489678e321
25
rfplot.py
25
rfplot.py
|
@ -3,6 +3,8 @@ import numpy as np
|
||||||
from strf.rfio import Spectrogram
|
from strf.rfio import Spectrogram
|
||||||
|
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
import matplotlib.dates as mdates
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Settings
|
# Settings
|
||||||
|
@ -17,8 +19,25 @@ if __name__ == "__main__":
|
||||||
# Create plot
|
# Create plot
|
||||||
vmin, vmax = np.percentile(s.z, (5, 99.95))
|
vmin, vmax = np.percentile(s.z, (5, 99.95))
|
||||||
|
|
||||||
fig, ax = plt.subplots()
|
# Time limits
|
||||||
ax.imshow(s.z, origin="lower", aspect="auto", interpolation="None",
|
tmin, tmax = mdates.date2num(s.t[0]), mdates.date2num(s.t[-1])
|
||||||
vmin=vmin, vmax=vmax)
|
|
||||||
|
|
||||||
|
# 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()
|
plt.show()
|
||||||
|
|
20
strf/rfio.py
20
strf/rfio.py
|
@ -2,10 +2,10 @@
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
class Spectrogram:
|
class Spectrogram:
|
||||||
"""Spectrogram class"""
|
"""Spectrogram class"""
|
||||||
|
|
||||||
|
@ -13,32 +13,34 @@ class Spectrogram:
|
||||||
"""Define a spectrogram"""
|
"""Define a spectrogram"""
|
||||||
|
|
||||||
# Read first file to get number of channels
|
# 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:
|
with open(fname, "rb") as fp:
|
||||||
header = parse_header(fp.read(256))
|
header = parse_header(fp.read(256))
|
||||||
|
|
||||||
# Set frequencies
|
# 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
|
# Loop over subints and files
|
||||||
zs = []
|
zs = []
|
||||||
mjds = []
|
t = []
|
||||||
isub = 0;
|
isub = 0;
|
||||||
while isub<nsub:
|
while isub<nsub:
|
||||||
# File name of file
|
# File name of file
|
||||||
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:
|
with open(fname, "rb") as fp:
|
||||||
next_header = fp.read(256)
|
next_header = fp.read(256)
|
||||||
while next_header:
|
while next_header:
|
||||||
header = parse_header(next_header)
|
header = parse_header(next_header)
|
||||||
# mjds.append(Time(header["utc_start"], format="datetime", scale="utc").mjd+0.5*header["length"]/86400.0)
|
t.append(header["utc_start"] + timedelta(seconds=0.5 * header["length"]))
|
||||||
zs.append(np.fromfile(fp, dtype=np.float32, count=header["nchan"]))
|
zs.append(np.fromfile(fp, dtype=np.float32, count=nchan))
|
||||||
next_header = fp.read(256)
|
next_header = fp.read(256)
|
||||||
isub += 1
|
isub += 1
|
||||||
ifile += 1
|
ifile += 1
|
||||||
|
|
||||||
self.z = np.transpose(np.vstack(zs))
|
self.z = np.transpose(np.vstack(zs))
|
||||||
self.mjd = np.array(mjds)
|
self.t = t
|
||||||
self.freq = freq
|
self.freq = freq
|
||||||
self.siteid = siteid
|
self.siteid = siteid
|
||||||
self.nchan = self.z.shape[0]
|
self.nchan = self.z.shape[0]
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
class Spectrogram:
|
|
||||||
"""Spectrogram class"""
|
|
||||||
|
|
||||||
def __init__(self, path, prefix, ifile, nsub, siteid):
|
|
||||||
"""Define a spectrogram"""
|
|
||||||
|
|
||||||
# Read first file to get number of channels
|
|
||||||
fname = os.path.join(path, "{:s}_{:06d}.bin".format(prefix, ifile))
|
|
||||||
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"]
|
|
||||||
|
|
||||||
# Loop over subints and files
|
|
||||||
zs = []
|
|
||||||
mjds = []
|
|
||||||
isub = 0;
|
|
||||||
while isub<nsub:
|
|
||||||
# File name of file
|
|
||||||
fname = os.path.join(path, "{:s}_{:06d}.bin".format(prefix, ifile))
|
|
||||||
with open(fname, "rb") as fp:
|
|
||||||
next_header = fp.read(256)
|
|
||||||
while next_header:
|
|
||||||
header = parse_header(next_header)
|
|
||||||
mjds.append(Time(header["utc_start"], format="datetime", scale="utc").mjd+0.5*header["length"]/86400.0)
|
|
||||||
zs.append(np.fromfile(fp, dtype=np.float32, count=header["nchan"]))
|
|
||||||
next_header = fp.read(256)
|
|
||||||
isub += 1
|
|
||||||
ifile += 1
|
|
||||||
|
|
||||||
self.z = np.transpose(np.vstack(zs))
|
|
||||||
self.mjd = np.array(mjds)
|
|
||||||
self.freq = freq
|
|
||||||
self.siteid = siteid
|
|
||||||
self.nchan = self.z.shape[0]
|
|
||||||
self.nsub = self.z.shape[1]
|
|
Loading…
Reference in New Issue