"""Pre-mainshock seismicity: ComCat M>=2.5 within the padded max radius, 10 years before each mainshock to 1 min before.
Writes raw/bg/<eid>.csv."""
import csv, os, time, urllib.request, collections
from concurrent.futures import ThreadPoolExecutor
exec(open('fetch_catalog.py').read().split('def go(eid):')[0])   # reuse km(), R, info
os.makedirs('raw/bg', exist_ok=True)
def go(eid):
    p = f'raw/bg/{eid}.csv'
    if os.path.exists(p): return
    lat, lon, t = info[eid]
    out = b''
    for yr in range(10):   # one-year chunks, each paged 20000 at a time
        t1 = t / 1000 - 60 - yr * 365.25 * 86400; t0 = t1 - 365.25 * 86400
        st = time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime(t0)); en = time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime(t1))
        url = (f'https://earthquake.usgs.gov/fdsnws/event/1/query?format=csv&latitude={lat}&longitude={lon}'
               f'&maxradiuskm={R[eid] + 1:.1f}&starttime={st}&endtime={en}&minmagnitude=2.5&orderby=time-asc&limit=20000')
        off = 1
        while True:
            data = None
            for i in range(5):
                try:
                    data = urllib.request.urlopen(url + f'&offset={off}', timeout=300).read(); break
                except Exception:
                    time.sleep(5 * (i + 1))
            if data is None: print('FAILED', eid, yr, off, flush=True); return
            lines = data.splitlines(True)
            out += data if not out else b''.join(lines[1:])
            if len(lines) - 1 < 20000: break
            off += 20000
    open(p, 'wb').write(out)
with ThreadPoolExecutor(3) as ex: list(ex.map(go, list(info)))
print(len(os.listdir('raw/bg')))
