-
Notifications
You must be signed in to change notification settings - Fork 11
/
desi-daily.py
210 lines (182 loc) · 7.51 KB
/
desi-daily.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from astrometry.util.fits import text_table_fields, fits_table, merge_tables
import sys
import os
import numpy as np
import fitsio
from glob import glob
import tempfile
from astrometry.libkd.spherematch import tree_build
basedir = 'data/desi-spectro-daily'
# Create tile kd-tree
if True:
from astropy.table import Table
TT = []
# for surv,fn in [('main', '/global/cfs/cdirs/desi/survey/ops/surveyops/trunk/ops/tiles-main.ecsv'),
# ('sv1', '/global/cfs/cdirs/desi/survey/ops/surveyops/trunk/ops/tiles-sv1.ecsv'),
# ('sv2', '/global/cfs/cdirs/desi/survey/ops/surveyops/trunk/ops/tiles-sv2.ecsv'),
# ('sv3', '/global/cfs/cdirs/desi/survey/ops/surveyops/trunk/ops/tiles-sv3.ecsv'),
#
for fn in ['/global/cfs/cdirs/desi/spectro/redux/daily/tiles-daily.csv']:
t1 = Table.read(fn)
t1.write('/tmp/t.fits', overwrite=True)
T = fits_table('/tmp/t.fits')
#T.about()
#T.survey = np.array([surv] * len(T))
TT.append(T)
T = merge_tables(TT, columns='fillzero')
#T.tilera = T.ra
#T.tiledec = T.dec
T.ra = T.tilera
T.dec = T.tiledec
for i,(prog,faprog) in enumerate(zip(T.program, T.faprgrm)):
if prog.strip() == '':
T.program[i] = faprog
# ts = '%06i' % tileid
# fn = 'data/desi-tiles/%s/fiberassign-%s.fits.gz' % (ts[:3], ts)
# T.found_tile[itile] = True
# T.cut(T.found_tile)
outfn = os.path.join(basedir, 'tiles2.fits')
T.writeto(outfn)
kdfn = os.path.join(basedir, 'tiles2.kd.fits')
cmd = 'startree -i %s -R tilera -D tiledec -PTk -o %s' % (outfn, kdfn)
os.system(cmd)
print('Wrote tile kd-tree')
# Create redshift catalog kd-tree
if True:
allzbest = []
# Cached files & dates
for date in ['202310']: #'202110', '202110-missing', '202202', '202302']:
cachedfn = os.path.join(basedir, 'allzbest-%s.fits' % date)
print('Reading cached spectra from', cachedfn, '...')
T = fits_table(cachedfn)
T.rename('ra', 'target_ra')
T.rename('dec', 'target_dec')
allzbest.append(T)
#T.about()
#cache_cutoff = '20211100'
#cache_cutoff = '20220300'
#cache_cutoff = '20230300'
cache_cutoff = '20231100'
print('Finding zbest(redrock) files...')
tiles = glob('/global/cfs/cdirs/desi/spectro/redux/daily/tiles/cumulative/*')
# sort numerically
nt = np.array([int(f.split('/')[-1]) for f in tiles])
I = np.argsort(nt)
tiles = [tiles[i] for i in I]
fns = []
for tile in tiles:
dates = glob(tile + '/20*')
if len(dates) == 0:
continue
dates.sort()
date = dates[-1]
justdate = os.path.basename(date)
#print('Date:', justdate)
if cache_cutoff is not None:
if justdate <= cache_cutoff:
print('Skipping (cached):', date)
continue
#fns.extend(glob(date + '/zbest-*.fits'))
thisfns = glob(date + '/redrock-*.fits')
thisfns.sort()
fns.extend(thisfns)
print('Adding', len(thisfns), 'files from', date)
# tiles = glob('/global/cfs/cdirs/desi/spectro/redux/daily/attic/rerunarchive-20211029/tiles/cumulative/*')
# for tile in tiles:
# dates = glob(tile + '/*')
# if len(dates) == 0:
# continue
# dates.sort()
# date = dates[-1]
# justdate = os.path.basename(date)
# #print('Date:', justdate)
# if cache_cutoff is not None:
# if justdate <= cache_cutoff:
# print('Skipping (cached):', date)
# continue
# thisfns = glob(date + '/redrock-*.fits')
# fns.extend(thisfns)
# print('Adding', len(thisfns), 'files from', date)
# Are we producing a cache file?
caching = False
if caching:
#cachedate = '20211100'
#cachedate_name = '202110'
cachedate = '20220300'
cachedate_name = '202202'
# print('Dates:')
# for fn in fns:
# print(' ', fn)
# print(' date', os.path.basename(os.path.dirname(fn)))
# print(' date before cachedate:', os.path.basename(os.path.dirname(fn)) <= cachedate)
fns = [fn for fn in fns if os.path.basename(os.path.dirname(fn)) <= cachedate]
if True:
for fn in fns:
T = fits_table(fn)
# Don't cut until the end because other tables are row-aligned!!
#T.cut(T.targetid >= 0)
#T.cut(T.npixels > 0)
#if len(T) == 0:
#if np.all(T.npixels == 0):
# continue
print(len(T), 'from', fn)
#'NIGHT', 'EXPID',
RD = fits_table(fn, hdu=2, columns=['TARGETID','TARGET_RA','TARGET_DEC',
'TILEID', 'FIBER',
'COADD_EXPTIME',])
rdmap = dict([(t,i) for i,t in enumerate(RD.targetid)])
I = np.array([rdmap[t] for t in T.targetid])
RD = RD[I]
T.target_ra = RD.target_ra
T.target_dec = RD.target_dec
#T.night = RD.night
#T.expid = RD.expid
T.tileid = RD.tileid
T.fiber = RD.fiber
T.coadd_exptime = RD.coadd_exptime
# TSNR2 values
cols = ['TSNR2_BGS', 'TSNR2_LRG', 'TSNR2_ELG', 'TSNR2_QSO', 'TSNR2_LYA']
SNR = fits_table(fn, hdu=4, columns=cols)
for c in SNR.get_columns():
T.set(c, SNR.get(c))
exp = fits_table(fn, hdu=3, columns=['TARGETID', 'MJD', 'FIBERSTATUS'])
# keep exposures where fibers were not bad
exp.cut(exp.fiberstatus == 0)
# update in order of increasing MJD to end up with the max.
target_maxmjd = {}
I = np.argsort(exp.mjd)
for tid,mjd in zip(exp.targetid[I], exp.mjd[I]):
target_maxmjd[tid] = mjd
target_minmjd = {}
# Reverse order for min MJD
I = I[-1::-1]
for tid,mjd in zip(exp.targetid[I], exp.mjd[I]):
target_minmjd[tid] = mjd
T.minmjd = np.array([target_minmjd.get(tid, 0) for tid in T.targetid])
T.maxmjd = np.array([target_maxmjd.get(tid, 0) for tid in T.targetid])
for c in ['chi2', 'coeff', 'deltachi2', 'z', 'zerr']:
T.set(c, T.get(c).astype(np.float32))
for c in ['ncoeff', 'npixels', 'zwarn']:
T.set(c, T.get(c).astype(np.int16))
#T.cut(T.npixels > 0)
allzbest.append(T)
allzbest = merge_tables(allzbest, columns='fillzero')
allzbest.cut(allzbest.npixels > 0)
allzbest.rename('target_ra', 'ra')
allzbest.rename('target_dec', 'dec')
if caching:
allzbest.writeto(os.path.join(basedir, 'allzbest-%s.fits' % cachedate_name))
sys.exit(0)
fitsfn = os.path.join(basedir, 'allzbest.fits')
allzbest.writeto(fitsfn)
fitsfn = os.path.join(basedir, 'allzbest.fits')
outfn = os.path.join(basedir, 'allzbest.kd.fits')
from desi_spectro_kdtree import create_desi_spectro_kdtree
create_desi_spectro_kdtree(fitsfn, outfn)
obsfn = os.path.join(basedir, 'desi-obs.fits')
subcols = ['targetid', 'zwarn', 'npixels', 'tileid', 'fiber',
'tsnr2_bgs', 'tsnr2_lrg', 'tsnr2_elg', 'tsnr2_qso', 'tsnr2_lya',
'minmjd', 'maxmjd', 'ra', 'dec', 'coadd_exptime']
allzbest.writeto(obsfn, columns=subcols)
obskd = os.path.join(basedir, 'desi-obs.kd.fits')
create_desi_spectro_kdtree(obsfn, obskd)