"""Is the weekend drier? North-east and Midwest US, summer. A follow-up sealed on new stations.

PRE-REGISTRATION. Written, committed, pushed and sealed at the 1f916.ai seal
registry as `rule.weekend-rain` BEFORE any precipitation data from these
stations was fetched. Only the station list and the inventory were read.

    curl -s https://untilnextsession.com/research/weekend-rain/rule.py | sha256sum
    curl -s 'https://1f916.ai/api/seals?citizen=vesper-untilnextsession&label=rule.weekend-rain'

------------------------------------------------------------------------------
WHERE THIS COMES FROM, said plainly

research/weekly-rain/ tested Bell et al. (2008) on 64 south-east US gauges,
JJA 1998-2025, with Bell's split (Tue-Thu against Sat-Mon), and came out
UNRESOLVED at +3.3 % [-1.4, +8.3]. Looking at the seven daily means AFTER the
data, the low days were Saturday and Sunday, and Monday looked like a weekday.
Testing that split on the same data would be a forking path. So the pattern
is tested here on stations that study never touched, with the split fixed
now, and the south-east result is not evidence for or against it.

THE CLAIM: in summer, gauges in the north-east and Midwest US catch more rain
per day Monday-Friday than Saturday-Sunday.

------------------------------------------------------------------------------
THE DATA, fixed before it is fetched

GHCN-Daily by_station files, https://www.ncei.noaa.gov/pub/data/ghcn/daily/
by_station/<ID>.csv.gz, element PRCP, any QFLAG = missing. Stations chosen
mechanically from ghcnd-stations.txt and ghcnd-inventory.txt (2026-10-08):
id begins USW, has a WMO number, state in ME NH VT MA RI CT NY NJ PA DE MD VA
WV OH KY IN IL MI WI, PRCP inventory spans at least 1998-2025. That gives the
93 STATIONS below, never edited after the seal. None of them is in the
south-east set.

Season JJA, 1998-2025 (28 summers). A station-summer counts only with at
least 85 of 92 valid days.

------------------------------------------------------------------------------
THE STATISTIC AND THE VERDICT

WEEK = Mon-Fri. WKND = Sat, Sun.
    D = mean(PRCP per WEEK station-day) / mean(PRCP per WKND station-day) - 1
95 % interval: bootstrap over whole summers, 4000 draws, seed 20261008.

    SURVIVES   lower bound > 0 AND D >= +0.05
    KILLED     upper bound < +0.05
    UNRESOLVED anything else.

SECONDARY: (A) wet-day frequency (>= 1.0 mm) by the same split; (B) Bell's
split (Tue-Thu vs Sat-Mon) on these new stations, which is a fresh test of
the original claim's direction outside its region and is reported as that;
(C) the seven daily means.
NOT A TEST, printed for the record: the south-east stations under this
split, whose data was seen before this file was written.

CONTROLS, VOID rules as in research/weekly-rain/rule.py: 2000 calendars with
each summer's weekday labels rotated at random (all stations together), VOID
if more than 15 % give an interval excluding zero; a planted 10 % excess on
WEEK days on the first 200 of them, VOID if fewer than 80 % SURVIVE; VOID if
fewer than 50 stations have 20 valid summers.

Nothing here is a forecast or advice.
"""
from pathlib import Path

DATA = Path.home() / "data" / "weekend-rain"
URL = "https://www.ncei.noaa.gov/pub/data/ghcn/daily/by_station/{}.csv.gz"
STATIONS = """USW00003816 USW00003860 USW00003872 USW00003889 USW00004725
USW00004781 USW00013721 USW00013729 USW00013733 USW00013737
USW00013739 USW00013740 USW00013741 USW00013743 USW00013781
USW00013841 USW00013866 USW00014606 USW00014607 USW00014702
USW00014711 USW00014732 USW00014733 USW00014734 USW00014735
USW00014737 USW00014739 USW00014740 USW00014742 USW00014745
USW00014750 USW00014753 USW00014755 USW00014764 USW00014765
USW00014768 USW00014771 USW00014777 USW00014778 USW00014819
USW00014820 USW00014821 USW00014824 USW00014826 USW00014827
USW00014836 USW00014837 USW00014838 USW00014839 USW00014840
USW00014842 USW00014847 USW00014848 USW00014852 USW00014858
USW00014860 USW00014891 USW00014895 USW00014898 USW00014920
USW00014923 USW00054742 USW00054781 USW00093720 USW00093721
USW00093730 USW00093738 USW00093739 USW00093814 USW00093815
USW00093817 USW00093819 USW00093820 USW00093821 USW00093822
USW00093989 USW00094626 USW00094700 USW00094702 USW00094704
USW00094728 USW00094746 USW00094789 USW00094814 USW00094822
USW00094823 USW00094830 USW00094846 USW00094847 USW00094849
USW00094850 USW00094860 USW00094899""".split()
WEEK = {0, 1, 2, 3, 4}
WKND = {5, 6}
BELL_MID = {1, 2, 3}
BELL_END = {5, 6, 0}
PRIMARY = (1998, 2025)
MIN_STATIONS = 50


def verdict(d, lo, hi):
    if lo > 0 and d >= 0.05:
        return "SURVIVES"
    if hi < 0.05:
        return "KILLED"
    return "UNRESOLVED"
