#!/usr/bin/env python3

import os
import argparse
import sys
import subprocess
import glob
import shutil


def combine_hourly(hourly_dir, fname_base):
    fname = os.path.join(hourly_dir, 'data', 'gnss', fname_base + '_all.T04')
    #if os.path.isfile(fname):
    #    return
    print(f"combine({hourly_dir}, {fname_base})")
    # List of files to concat
    flist = sorted(glob.glob(
        '*T04',
        root_dir=os.path.join(hourly_dir, 'data', 'gnss', fname_base)
    ))
    print(flist)

    #if fname in flist:
    #    index = flist.index(fname)
    #    del flist[index]

    with open(fname, 'wb') as wfd:
        for f in flist:
            f_path = os.path.join(hourly_dir, 'data', 'gnss', fname_base, f)
            print(f"    appending {f} to {fname}")
            with open(f_path, 'rb') as fd:
                shutil.copyfileobj(fd, wfd)


folders = [
    '240312_no_whips',
    '240313_whip_uhf',
    '240317_whip_halow',
    '240318_whip_both',
    '240319_900only_halow',
    '240320_900only_uhf',
    #_240311_both_whips_wrong_ant_model,
    #_240314_whip_halow_snow,
    #_240315_whip_halow_snow,
    ]
#os.makedirs('figures', exist_ok=True)


for folder in folders:
    combine_hourly(folder, 'P3ENCL')
