"""The light curves data/external.json points at, which the audit searches again: every light
curve with a vetted event of 1e34 erg or more, of either sign, on the confirmatory stars not set
aside as fast rotators (the counted flares among them), and the 150 other light curves of the
confirmatory set's searched stars whose file names hash lowest.

python pick_audit.py <data dir> <run dir> <out.txt>
"""
import hashlib
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import analyze as an  # noqa: E402

data, run, out = sys.argv[1:4]
st = an.Study(data, run, 'main')
nonfast = set(st.stars.tic[~st.stars.tess_fast])
cands = an.coincidences(an.table(run, 'cands'))
ok = cands.vetted.astype(bool) & (cands.energy >= 1e34) & (cands.n_coincident <= an.COINCIDENCE_MAX) & cands.tic.isin(nonfast)
events = set(cands.file[ok])
others = sorted((hashlib.sha256(f.encode()).hexdigest(), f) for f in st.use.file if f not in events)[:150]
files = sorted(events) + sorted(f for _, f in others)
open(out, 'w').write('\n'.join(files) + '\n')
print(len(events), 'with vetted events,', len(others), 'others')
