"""Parse the official CPC/IRI ENSO probability table (9 overlapping 3-month seasons x La Nina/Neutral/El Nino)
from IRI quick-look pages raw/iri_ql/YYYY-month.html -> data/official_probs.csv
(issue = page month; season label; target season mid-month date; lead = months from issue month to season centre)."""
import re, html, glob, os, csv, datetime as dt
R = os.environ.get('ENSO_ROOT', '/work/enso-outlooks')
MON = 'january february march april may june july august september october november december'.split()
SEAS = ['DJF', 'JFM', 'FMA', 'MAM', 'AMJ', 'MJJ', 'JJA', 'JAS', 'ASO', 'SON', 'OND', 'NDJ']  # index = centre month - 1 (DJF centre Jan)
rows, skipped = [], []
for f in sorted(glob.glob(f'{R}/raw/iri_ql/*.html')):
    y, m = os.path.basename(f)[:-5].split('-'); y = int(y); mi = MON.index(m) + 1
    s = open(f, errors='replace').read(); got = None
    for t in re.findall(r'<table.*?</table>', s, re.S):
        cells = [' '.join(html.unescape(re.sub(r'<[^>]+>', '', c)).split()) for c in re.findall(r'<t[dh][^>]*>(.*?)</t[dh]>', t, re.S)]
        if len(cells) == 40 and cells[1].startswith('La Ni') and cells[3].startswith('El Ni'):
            got = cells; break
    if not got: skipped.append(f'{y}-{mi:02d}'); continue
    prev_c = None
    for k in range(9):
        lab, a, b, c = got[4 + 4 * k: 8 + 4 * k]
        sea = lab[:3].upper(); ci = SEAS.index(sea) + 1  # centre month number
        # target year: first season is the one centred on the issue month - 1 .. +1; walk forward
        lead = k  # provisional
        num = lambda x: float(x.rstrip('%')) if re.fullmatch(r'\d+(\.\d+)?%?', x) else None
        rows.append(dict(issue=f'{y}-{mi:02d}', k=k, season=sea, centre_month=ci, p_lan=num(a), p_neu=num(b), p_eln=num(c), label=lab))
with open(f'{R}/data/official_probs_raw.csv', 'w', newline='') as fh:
    w = csv.DictWriter(fh, fieldnames=list(rows[0])); w.writeheader(); w.writerows(rows)
print(len(rows) // 9, 'issues parsed; skipped', skipped)
