-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdr19_copy_aspcap_dr17.py
58 lines (53 loc) · 1.35 KB
/
dr19_copy_aspcap_dr17.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from tqdm import tqdm
from astra import __version__
from astra.models.base import database
from astra.models import ASPCAP, ApogeeCoaddedSpectrumInApStar
from peewee import chunked
# Get all DR17 results for v_astra = 0.5.0
q = (
ASPCAP
.select()
.distinct(ASPCAP.spectrum_pk)
.join(
ApogeeCoaddedSpectrumInApStar,
on=(ApogeeCoaddedSpectrumInApStar.spectrum_pk == ASPCAP.spectrum_pk)
)
.where(
(ASPCAP.v_astra == "0.5.0")
& (ApogeeCoaddedSpectrumInApStar.release == "dr17")
)
.dicts()
)
rows = []
for r in tqdm(q):
r.pop("task_pk")
r["v_astra"] = __version__
# Remove any calibrations.
for k, v in r.items():
if k.startswith("raw_"):
r[k[4:]] = v
rows.append(r)
spectrum_pks = [e["spectrum_pk"] for e in rows]
# Check to make sure we are not duplicating...
q = (
ASPCAP
.select()
.where(
(ASPCAP.v_astra == __version__)
& ASPCAP.spectrum_pk.in_(spectrum_pks)
)
.first()
)
assert q is None
# Bulk insert
batch_size = 1000
with database.atomic():
with tqdm(desc="Inserting", total=len(rows)) as pb:
for chunk in chunked(rows, batch_size):
(
ASPCAP
.insert_many(chunk)
.execute()
)
pb.update(min(batch_size, len(chunk)))
pb.refresh()