python3 - <<'PY'
import os, re, statistics, sys
from pathlib import Path

ROOT  = Path("/station/instruments/disdrodb/PAR_UNBC_TERRACE")
ID    = "PAR_UNBC_TERRACE"
MONTHS = []   # tweak or set to [] to auto-scan all months
LOW_FRAC = 0.60                # flag if < 60% of median
ABS_MIN  = 120*1024            # or < 120 KB

def month_dirs(root):
    if MONTHS:
        for m in MONTHS:
            yield root/ m[:4] / m
    else:
        for ydir in sorted((p for p in root.iterdir() if p.is_dir() and re.match(r"^\d{4}$", p.name))):
            for mdir in sorted((p for p in ydir.iterdir() if p.is_dir() and re.match(rf"^{ydir.name}\d{{2}}$", p.name))):
                yield mdir

def sizes_for_month(mdir):
    sizes=[]
    for p in mdir.glob(f"{ID}_*.nc"):
        try: sizes.append((p, p.stat().st_size))
        except Exception: pass
    return sorted(sizes, key=lambda x: x[1])

def fmtkb(b): return f"{b/1024:,.0f} KB"

any_bad=False
for mdir in month_dirs(ROOT):
    items = sizes_for_month(mdir)
    if not items: continue
    sizes = [s for _,s in items]
    med = statistics.median(sizes)
    thr = max(int(med*LOW_FRAC), ABS_MIN)
    print(f"\n== {mdir}  count={len(sizes)}  median={fmtkb(med)}  threshold={fmtkb(thr)}")
    for p,sz in items:
        flag = " !!" if sz < thr else ""
        print(f"{fmtkb(sz):>10}  {p.name}{flag}")
        if flag: any_bad=True

if not any_bad:
    print("\nNo anomalies detected by the thresholds.")
PY

