"""Extract Ukraine CSVs from NASA FIRMS yearly country archives using HTTP range requests. Usage: python3 -I fetch_firms.py sensor: modis | viirs-snpp Retries on connection resets (the FIRMS server drops connections intermittently). """ import os import sys import time from remotezip import RemoteZip BASE = "https://firms.modaps.eosdis.nasa.gov/data/country/zips/{s}_{y}_all_countries.zip" def fetch(out_dir, sensor, year, tries=8): url = BASE.format(s=sensor, y=year) out = os.path.join(out_dir, f"{sensor}_{year}_Ukraine.csv") if os.path.exists(out) and os.path.getsize(out) > 0: return f"{sensor} {year}: cached" last = None for i in range(tries): try: with RemoteZip(url, timeout=60) as z: names = [n for n in z.namelist() if n.lower().endswith("_ukraine.csv")] if not names: return f"{sensor} {year}: no Ukraine file (sample: {z.namelist()[:3]})" data = z.read(names[0]) with open(out, "wb") as f: f.write(data) return f"{sensor} {year}: {names[0]} {len(data)} bytes" except Exception as e: # noqa: BLE001 last = e if "404" in str(e): return f"{sensor} {year}: not available (404)" time.sleep(2 + 2 * i) return f"{sensor} {year}: FAILED ({last})" if __name__ == "__main__": out_dir, sensor, y0, y1 = sys.argv[1], sys.argv[2], int(sys.argv[3]), int(sys.argv[4]) os.makedirs(out_dir, exist_ok=True) for y in range(y0, y1 + 1): print(fetch(out_dir, sensor, y), flush=True)