import pandas as pd
import matplotlib.pyplot as plt
import os

import viewdat_cno_lib as vdl

#files = [
#    'outputs/B7-986RTK_202502061200_d35-19.tsv',
#    'outputs/B8-EB81RTK202502061200.tsv',
#    'outputs/EB79_____202502061200.tsv',
#    'outputs/EB93RAIL__202502061200.tsv',
#    ]
#d35_19_files = [
#    'outputs/B7-986RTK_202502061200_d35-19.tsv',
#    'outputs/B8-EB81RTK202502061200_d35_19.tsv',
#    'outputs/EB79_____202502061200_d35_19.tsv',
#    'outputs/EB93RAIL__202502061200_d35_19.tsv',
#    ]
#t04_files = [
#    '250206/B7-986RTK_all.T04',
#    '250206/B8-EB81RTK_all.T04',
#    '250206/EB79_all.T04',
#    '250206/EB93rail_all.T04',
#    ]
t04_files = [
    '250206/B7-986RTK_short.T04',
    '250206/B8-EB81RTK_short.T04',
    '250206/EB93rail_short.T04',
    '250206/EB79_short.T04',
    ]

names = ['SPS986', 'EB81', 'EB93', 'EB79']
location = ['test track', 'test track', 'B1 Roof', 'bench inside']
ant_type = ['single', 'vsbl', 'vsbl', 'dual']
LEAP_SEC_OFFSET = 18
# ---------------------------------------------------------------------------
if True:
    fig, ax = plt.subplots(
            len(t04_files), 1,
            figsize=[20,10],
            constrained_layout=True,
            )
    for n, fname in enumerate(t04_files):
        f = os.path.split(fname)[1]
        csv_fname = os.path.join('outputs', f.replace('.T04', '_d35-2.csv'))
        description=f'{names[n]}, {location[n]}, {ant_type[n]}'
        print(n, f, csv_fname)

        if os.path.isfile(csv_fname):
            df = pd.read_csv(csv_fname)
        else:
            df = vdl.read_corr_age(fname)
            df.to_csv(csv_fname, index=False)
        #print(df)
        df['utc'] = (df.Week * 604800 + df.Time) + LEAP_SEC_OFFSET

        if n == 0:
            ax[n].set_title('Correction Age')

        m = df.corrAge.mean()
        ax[n].plot(df.utc, df.corrAge, label=description)
        ax[n].axhline(y=m, color='r', linestyle='--', label=f'mean: {m:0.2f}')
        ax[n].set_ylabel('Age')
        ax[n].set_ylim([0, 15])
        ax[n].legend()

    ax[len(t04_files)-1].set_xlabel('Time (s)')
    plt.savefig('results/compare_corr_age.png')
    plt.show()
    plt.close()


# ---------------------------------------------------------------------------
if True:
    fig, ax = plt.subplots(
            len(t04_files), 1,
            figsize=[20,10],
            constrained_layout=True,
            )
    for n, fname in enumerate(t04_files):

        f = os.path.split(fname)[1]
        csv_fname = os.path.join('outputs', f.replace('.T04', '_d98.csv'))
        description=f'{names[n]}, {location[n]}, {ant_type[n]}'
        print(n, fname, f, csv_fname)

        if os.path.isfile(csv_fname):
            df = pd.read_csv(csv_fname)
        else:
            df = vdl.read_cmr(fname)
            df.to_csv(csv_fname, index=False)
        df_group =  vdl.cmr_group_count(df, group_col='Time', col_out='packet_count')
        print(df_group)

        if n == 0:
            ax[n].set_title('Packet Counts per Second')

        m = df_group.packet_count.mean()
        ax[n].plot(df_group.seconds, df_group.packet_count, label=description)
        ax[n].plot(df_group.seconds, df_group.seconds.diff(), label='dt')
        #ax[n].axhline(y=m, color='r', linestyle='--', label=f'mean: {m:0.2f}')
        ax[n].set_ylabel('packet count')
        ax[n].set_ylim([0, 15])
        ax[n].legend()

    ax[len(t04_files)-1].set_xlabel('Time (s)')
    plt.savefig('results/compare_packet_counts.png')
    plt.show()
    plt.close()
