#!/usr/bin/env python3
"""Regression capture for the SSR QC IOD check (tests/data/qc_iod_261015.raw).

  ephemeris stream: each RTCM3 message of BCEP00BKG0 only at its first reception
                    (the stream repeats every data set every 5 s), START_EPH..END
  SSR streams:      complete, START_SSR..END

usage: mk_qc_iod.py <SSR QC capture of 15 Oct 2026 (bncSsrQc.raw_261015)> <out>
"""
import sys
sys.path.insert(0, '/home/andrea/tools/BNC/tests/tools')
import bncraw

EPH = 'BCEP00BKG0'
SSR = ('SSRA00WHU1', 'SSRA00GMV1', 'SSRA00CNE1')
START_EPH = '2026-10-15T00:00'
START_SSR = '2026-10-15T01:55'
END = '2026-10-15T02:10'


def chunks(raw):
    framer = bncraw.Rtcm3Framer()
    seen = set()
    for t, sta, fmt, data in bncraw.read_chunks(raw):
        if t >= END:
            return
        if sta == EPH and t >= START_EPH:
            new = [p for p in framer.feed(data) if p not in seen]
            seen.update(new)
            if new:
                yield t, sta, fmt, b''.join(bncraw.frame(p) for p in new)
        elif sta in SSR and t >= START_SSR:
            yield t, sta, fmt, data


bncraw.write_chunks(sys.argv[2], chunks(sys.argv[1]))
