Improve bin file reading logic
parent
fe6e4fd120
commit
0342f65f87
|
@ -60,7 +60,6 @@ def main():
|
||||||
# Parse input arguments
|
# Parse input arguments
|
||||||
parser = argparse.ArgumentParser(description="rfplot: plot RF observations", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
parser = argparse.ArgumentParser(description="rfplot: plot RF observations", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||||
parser.add_argument("-p", "--path", help="Input path to parent directory /a/b/", type=str)
|
parser.add_argument("-p", "--path", help="Input path to parent directory /a/b/", type=str)
|
||||||
parser.add_argument("-P", "--prefix", help="Filename prefix c in c_??????.bin", type=str)
|
|
||||||
parser.add_argument("-s", "--start", type=int, default=0, help="Number of starting subintegration")
|
parser.add_argument("-s", "--start", type=int, default=0, help="Number of starting subintegration")
|
||||||
parser.add_argument("-l", "--length", type=int, default=3600, help="Number of subintegrations to plot")
|
parser.add_argument("-l", "--length", type=int, default=3600, help="Number of subintegrations to plot")
|
||||||
parser.add_argument("-C", "--site", type=int, help="Site ID", default=4171)
|
parser.add_argument("-C", "--site", type=int, help="Site ID", default=4171)
|
||||||
|
@ -84,11 +83,9 @@ def main():
|
||||||
if not os.path.exists(args.catalog):
|
if not os.path.exists(args.catalog):
|
||||||
print(f"TLE catalog not available under {args.catalog}")
|
print(f"TLE catalog not available under {args.catalog}")
|
||||||
|
|
||||||
print(site, args.freqlist, args.catalog)
|
|
||||||
|
|
||||||
# Read spectrogram
|
# Read spectrogram
|
||||||
if args.artifact is None:
|
if args.artifact is None:
|
||||||
s = Spectrogram(args.path, args.prefix, args.start, args.length, args.site)
|
s = Spectrogram(args.path, args.start, args.length, args.site)
|
||||||
timestamps = [ x.replace(tzinfo=utc) for x in s.t]
|
timestamps = [ x.replace(tzinfo=utc) for x in s.t]
|
||||||
range_rate_base = [ 0 for x in timestamps]
|
range_rate_base = [ 0 for x in timestamps]
|
||||||
site_location = wgs84.latlon(site["lat"], site["lon"], site["height"])
|
site_location = wgs84.latlon(site["lat"], site["lon"], site["height"])
|
||||||
|
|
51
strf/rfio.py
51
strf/rfio.py
|
@ -1,6 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
|
import glob
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
@ -9,11 +11,27 @@ from datetime import datetime, timedelta
|
||||||
class Spectrogram:
|
class Spectrogram:
|
||||||
"""Spectrogram class"""
|
"""Spectrogram class"""
|
||||||
|
|
||||||
def __init__(self, path, prefix, ifile, nsub, siteid):
|
def __init__(self, froot, ifile, nsub, siteid):
|
||||||
"""Define a spectrogram"""
|
"""Define a spectrogram"""
|
||||||
|
path, prefix = extract_path_and_prefix(froot)
|
||||||
|
|
||||||
# Read first file to get number of channels
|
# Read matching filenames
|
||||||
|
fnames = sorted(glob.glob(os.path.join(path, f"{prefix}_*.bin")))
|
||||||
|
|
||||||
|
# Start filename
|
||||||
fname = os.path.join(path, f"{prefix}_{ifile:06d}.bin")
|
fname = os.path.join(path, f"{prefix}_{ifile:06d}.bin")
|
||||||
|
|
||||||
|
if not fname in fnames:
|
||||||
|
# Exit on no matching files
|
||||||
|
if fnames == []:
|
||||||
|
print(f"Spectrogram is not available under {fname}")
|
||||||
|
sys.exit(1)
|
||||||
|
else:
|
||||||
|
print(f"Spectrogram is not available under {fname}\nUsing {fnames[0]} instead")
|
||||||
|
fname = fnames[0]
|
||||||
|
ifile = int(fname.split("_")[1].replace(".bin", ""))
|
||||||
|
|
||||||
|
# Read first header
|
||||||
with open(fname, "rb") as fp:
|
with open(fname, "rb") as fp:
|
||||||
header = parse_header(fp.read(256))
|
header = parse_header(fp.read(256))
|
||||||
|
|
||||||
|
@ -29,12 +47,22 @@ class Spectrogram:
|
||||||
while isub<nsub:
|
while isub<nsub:
|
||||||
# File name of file
|
# File name of file
|
||||||
fname = os.path.join(path, f"{prefix}_{ifile:06d}.bin")
|
fname = os.path.join(path, f"{prefix}_{ifile:06d}.bin")
|
||||||
|
|
||||||
|
# Exit on absent file
|
||||||
|
if not os.path.exists(fname):
|
||||||
|
break
|
||||||
|
|
||||||
|
print(f"Opened {fname}")
|
||||||
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)
|
||||||
t.append(header["utc_start"] + timedelta(seconds=0.5 * header["length"]))
|
t.append(header["utc_start"] + timedelta(seconds=0.5 * header["length"]))
|
||||||
zs.append(np.fromfile(fp, dtype=np.float32, count=nchan))
|
z = np.fromfile(fp, dtype=np.float32, count=nchan)
|
||||||
|
# Break on incomplete spectrum
|
||||||
|
if len(z) != nchan:
|
||||||
|
break
|
||||||
|
zs.append(z)
|
||||||
next_header = fp.read(256)
|
next_header = fp.read(256)
|
||||||
isub += 1
|
isub += 1
|
||||||
ifile += 1
|
ifile += 1
|
||||||
|
@ -43,11 +71,28 @@ class Spectrogram:
|
||||||
self.t = t
|
self.t = t
|
||||||
self.freq = freq
|
self.freq = freq
|
||||||
self.fcen = freq_cen
|
self.fcen = freq_cen
|
||||||
|
self.bw = bw
|
||||||
self.siteid = siteid
|
self.siteid = siteid
|
||||||
self.nchan = self.z.shape[0]
|
self.nchan = self.z.shape[0]
|
||||||
self.nsub = self.z.shape[1]
|
self.nsub = self.z.shape[1]
|
||||||
|
|
||||||
|
print(f"Read spectrogram\n{self.nchan} channels, {self.nsub} subints\nFrequency: {self.fcen * 1e-6:g} MHz\nBandwidth: {self.bw * 1e-6:g} MHz")
|
||||||
|
|
||||||
|
|
||||||
|
def extract_path_and_prefix(fname):
|
||||||
|
basename, dirname = os.path.basename(fname), os.path.dirname(fname)
|
||||||
|
|
||||||
|
pattern_with_extension = "_\d{6}.bin$"
|
||||||
|
pattern_without_extension = "_\d{6}$"
|
||||||
|
if re.findall(pattern_with_extension, basename):
|
||||||
|
prefix, _ = re.split(pattern_with_extension, basename)
|
||||||
|
elif re.findall(pattern_without_extension, basename):
|
||||||
|
prefix, _ = re.split(pattern_without_extension, basename)
|
||||||
|
else:
|
||||||
|
prefix = basename
|
||||||
|
|
||||||
|
return dirname, prefix
|
||||||
|
|
||||||
def parse_header(header_b):
|
def parse_header(header_b):
|
||||||
header_s = header_b.decode('ASCII').strip('\x00')
|
header_s = header_b.decode('ASCII').strip('\x00')
|
||||||
regex = r"^HEADER\nUTC_START (.*)\nFREQ (.*) Hz\nBW (.*) Hz\nLENGTH (.*) s\nNCHAN (.*)\nNSUB (.*)\nEND\n$"
|
regex = r"^HEADER\nUTC_START (.*)\nFREQ (.*) Hz\nBW (.*) Hz\nLENGTH (.*) s\nNCHAN (.*)\nNSUB (.*)\nEND\n$"
|
||||||
|
|
Loading…
Reference in New Issue