"""The registered analysis again, without the counted events whose SAP flux moved the opposite way
(code/sap_sign.py): every count, prediction, and interval of code/analyze.py, written to a folder
of its own beside the registered results.

python corrected.py <data dir> <run dir> <results dir> <sap_sign.csv> [--localization FILE]
"""
import os
import sys

import pandas as pd

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

data, run, out, signs = sys.argv[1:5]
rest = sys.argv[5:]
ev = pd.read_csv(signs)
drop = {(f, round(t, 6)) for f, t, ok in zip(ev.file, ev.tpk, ev.same_sign) if not ok}
_counted = an.Study.counted


def counted(self, kind, files, e_star=1e34, scale=None, localized=False):
    d = _counted(self, kind, files, e_star, scale, localized)
    return d[[(f, round(t, 6)) not in drop for f, t in zip(d.file, d.tpk)]]


an.Study.counted = counted
sys.argv = ['analyze.py', data, run, out, '--set', 'main'] + rest
an.main()
