#!/bin/bash
# Download SWPC RSGA (daily forecast) and events (daily event list) text files from the NCEI mirror, 1996-2026.
# Usage: bash fetch.sh  (resumable; skips files already present)
B=https://www.ngdc.noaa.gov/stp/space-weather/swpc-products/daily_reports
cd "${FLARE_ROOT:-/work/flare-forecasts}"
gen() {
  /opt/venv/bin/python - <<'PY'
import datetime as dt
d=dt.date(1996,1,1); end=dt.date(2026,10,2)
while d<=end:
    print(d.strftime('%Y %m %Y%m%d')); d+=dt.timedelta(1)
PY
}
gen | while read y m ymd; do
  [ -s raw/rsga/${ymd}RSGA.txt ] || echo "$B/reports_solar_geophysical_activity/$y/$m/${ymd}RSGA.txt raw/rsga/${ymd}RSGA.txt"
  [ -s raw/events/${ymd}events.txt ] || echo "$B/solar_event_reports/$y/$m/${ymd}events.txt raw/events/${ymd}events.txt"
done | xargs -P 10 -n 2 sh -c 'curl -s -f --max-time 60 --retry 2 -o "$1" "$0" || echo "MISS $0" >> raw/missing.log'
