#!/opt/local/Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10
# -*- coding: utf-8 -*-

# Copyright 2019 Daniel Estevez <daniel@destevez.net>
#
# This file is part of gr-satellites
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

"""Plots SMOG-P/ATL-1 spectrum data previously saved to a file"""

import pathlib
import re
import sys

import matplotlib.pyplot as plt
import numpy as np


def usage():
    print(f'Usage: {sys.argv[0]} input_file', file=sys.stderr)
    sys.exit(1)


def main():
    if len(sys.argv) != 2:
        usage()

    filename = sys.argv[1]
    m = re.match(
        'spectrum_start_(?P<start>\\d+?)_step_(?P<step>\\d+?)_'
        'rbw_\\d+_measid_(?P<measid>\\d+?)\\Z',
        pathlib.Path(filename).name)
    start = int(m.group('start'))
    step = int(m.group('step'))
    measid = int(m.group('measid'))

    data = np.fromfile(filename, dtype='uint8')
    freq = start + step * np.arange(data.size)

    plt.figure()
    plt.plot(freq * 1e-6, data)
    plt.title(f'Spectrum measurement {measid}')
    plt.xlabel('Frequency (MHz)')
    plt.ylabel('PSD')
    plt.savefig(pathlib.Path(filename).parent / f'spectrum_{measid}.png')


if __name__ == '__main__':
    main()
