worker: TESS SPOC light curves, parallel fetch
Browse files
tools/colab_scidata_worker.py
CHANGED
|
@@ -128,8 +128,81 @@ def pdb():
|
|
| 128 |
k += 1
|
| 129 |
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
WORK.mkdir(parents=True, exist_ok=True)
|
| 132 |
say("scidata worker start, free disk", round(free_gb()), "GB, batch", BATCH_GB, "GB")
|
| 133 |
pubchem()
|
|
|
|
| 134 |
pdb()
|
| 135 |
say("SCIDATA_DONE")
|
|
|
|
| 128 |
k += 1
|
| 129 |
|
| 130 |
|
| 131 |
+
TESS = "https://archive.stsci.edu/missions/tess/download_scripts/sector/"
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def fetch_all(pairs, d, workers=24):
|
| 135 |
+
"""Download (filename, url) pairs into d in parallel; failures are retried 3 times, then skipped and counted."""
|
| 136 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 137 |
+
|
| 138 |
+
def one(pair):
|
| 139 |
+
fn, url = pair
|
| 140 |
+
for k in range(3):
|
| 141 |
+
try:
|
| 142 |
+
with urllib.request.urlopen(url, timeout=120) as r, open(d / (fn + ".part"), "wb") as fo:
|
| 143 |
+
shutil.copyfileobj(r, fo, 1 << 20)
|
| 144 |
+
(d / (fn + ".part")).rename(d / fn)
|
| 145 |
+
return True
|
| 146 |
+
except Exception: # noqa: BLE001
|
| 147 |
+
time.sleep(5 * (k + 1))
|
| 148 |
+
return False
|
| 149 |
+
|
| 150 |
+
with ThreadPoolExecutor(workers) as ex:
|
| 151 |
+
ok = sum(ex.map(one, pairs))
|
| 152 |
+
say("fetched", ok, "of", len(pairs))
|
| 153 |
+
return ok
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def tess(first=1, last=200):
|
| 157 |
+
"""TESS SPOC 2-min light curves (NASA/MAST, public domain), one sector at a time, packed in ~5 GB tar shards."""
|
| 158 |
+
name = "space/tess_spoc_lightcurves"
|
| 159 |
+
scripts = listing(TESS, r'(tesscurl_sector_(\d+)_lc\.sh)')
|
| 160 |
+
sectors = sorted({int(n) for _, n in scripts if first <= int(n) <= last})
|
| 161 |
+
have = on_hub(name + "/") # file names only: MANIFEST_sNNNN.jsonl marks a finished sector
|
| 162 |
+
say("tess", len(sectors), "sectors,", sum(f"MANIFEST_s{s:04d}.jsonl" in have for s in sectors), "already on the Hub")
|
| 163 |
+
for s in sectors:
|
| 164 |
+
if f"MANIFEST_s{s:04d}.jsonl" in have:
|
| 165 |
+
continue
|
| 166 |
+
d, out = WORK / "tess_dl", WORK / "tess_out"
|
| 167 |
+
for p in (d, out):
|
| 168 |
+
shutil.rmtree(p, ignore_errors=True)
|
| 169 |
+
p.mkdir(parents=True)
|
| 170 |
+
sh = urllib.request.urlopen(f"{TESS}tesscurl_sector_{s}_lc.sh", timeout=120).read().decode()
|
| 171 |
+
pairs = []
|
| 172 |
+
for ln in sh.splitlines():
|
| 173 |
+
t = ln.split()
|
| 174 |
+
if t and t[0] == "curl" and "-o" in t:
|
| 175 |
+
fn = t[t.index("-o") + 1]
|
| 176 |
+
if re.fullmatch(r"[\w.-]+\.fits", fn):
|
| 177 |
+
pairs.append((fn, t[-1]))
|
| 178 |
+
fetch_all(pairs, d)
|
| 179 |
+
files = sorted(p for p in d.iterdir() if p.suffix == ".fits")
|
| 180 |
+
k, cur, tf, rows = 0, 0, None, []
|
| 181 |
+
for p in files:
|
| 182 |
+
if tf is None or cur >= 5e9:
|
| 183 |
+
if tf:
|
| 184 |
+
tf.close()
|
| 185 |
+
tf, cur = tarfile.open(out / f"s{s:04d}_part_{k:03d}.tar", "w"), 0
|
| 186 |
+
k += 1
|
| 187 |
+
nm = p.name
|
| 188 |
+
tf.add(p, arcname=f"sector_{s:04d}/{nm}")
|
| 189 |
+
cur += p.stat().st_size
|
| 190 |
+
rows.append({"file": nm, "bytes": p.stat().st_size, "shard": f"s{s:04d}_part_{k - 1:03d}.tar"})
|
| 191 |
+
if tf:
|
| 192 |
+
tf.close()
|
| 193 |
+
shutil.rmtree(d)
|
| 194 |
+
(out / f"MANIFEST_s{s:04d}.jsonl").write_text("".join(json.dumps(r) + "\n" for r in rows))
|
| 195 |
+
if s == sectors[0] and "SOURCE.json" not in have:
|
| 196 |
+
(out / "SOURCE.json").write_text(json.dumps({"name": name, "licence": "NASA/MAST public data (US Government "
|
| 197 |
+
"work); acknowledge the TESS mission and MAST", "url": TESS}))
|
| 198 |
+
upload(out, name, f"add {name} sector {s} ({len(rows)} light curves, {sum(r['bytes'] for r in rows) / 1e9:.1f} GB)")
|
| 199 |
+
say("tess sector", s, len(rows), "files", round(sum(r["bytes"] for r in rows) / 1e9, 1), "GB")
|
| 200 |
+
shutil.rmtree(WORK / "tess_out", ignore_errors=True)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
WORK.mkdir(parents=True, exist_ok=True)
|
| 204 |
say("scidata worker start, free disk", round(free_gb()), "GB, batch", BATCH_GB, "GB")
|
| 205 |
pubchem()
|
| 206 |
+
tess()
|
| 207 |
pdb()
|
| 208 |
say("SCIDATA_DONE")
|