-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrite_context_primecam_mpi.py
459 lines (374 loc) · 14.7 KB
/
write_context_primecam_mpi.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
"""
python write_context_FYSTv3.py -h
for help
write_context_FYSTv3.py -- create context.yaml & support databases for SSO sims
Original script from Matthew Hasselfield / sotodlib team
Modified and Hotwired for FYST from Ankur Dev, March 19, 2024
Note: Major functionalities removed/re-wired to be compatible with present-day
h5 FYST files. We need to re-visit this.
This script indexes output in TOAST's native HDF5 format. You can
index any number of output directories
Invoke like this:
python write_context_FYSTv3.py ./relative-path/to-h5-dir/
Added, to test indexed dir and files:
python write_context_FYSTv3.py ./relative-path/to-h5-dir/ --dry-run
(Note the HDF data are expected to be in "data" subdir of each passed
argument.)
Last Updated: September 26, 2024
"""
import os
import glob
import h5py
import numpy as np
import yaml
import argparse
from mpi4py import MPI
from os.path import normpath, basename
from sotodlib.core import metadata
from sotodlib.io.metadata import write_dataset
from so3g.proj import quat
from scripts.helper_scripts.ccat_logger import get_ccat_logger
DEG = np.pi/180
obsfiledb = metadata.ObsFileDb()
obsdb = metadata.ObsDb()
obsdb.add_obs_columns([
## Standardized
'timestamp float',
'duration float',
'start_time float',
'stop_time float',
'type string',
'subtype string',
'telescope string',
'telescope_flavor string',
'tube_slot string',
'tube_flavor string',
'detector_flavor string',
## Standardizing soon
'wafer_slot_mask string',
'el_nom float',
'el_span float',
'az_nom float',
'az_span float',
'roll_nom float',
'roll_span float',
## Extensions
'wafer_slots string',
'type string',
'target string',
'toast_obs_name string',
'toast_obs_uid string',
])
detsets = {}
item_count = 0
#usually dont change this
#Hardwiring context_dir
context_dir = "ccat_datacenter_mock/context/"
if not os.path.exists(context_dir):
os.makedirs(context_dir, exist_ok=True)
def extract_detdb(hg, db=None):
"""
Extracting Telescope and Detector Data.
Modifying and removing few columns in created database table
to match present Prime-cam simulations.
#Ankur Dev; March 19, 2024
#This should be re-done later
"""
if db is None:
db = metadata.DetDb()
db.create_table('base', [
"`det_id_` text", # we can't use "det_id"; rename later
"`readout_id` text",
"`wafer_slot` text",
"`special_ID` text",
"`tel_type` text",
"`tube_type` text",
"`band` text",
# "`fcode` text", #Skipping for FYST for now
"`toast_band` text",
])
db.create_table('quat', [
"`r` float",
"`i` float",
"`j` float",
"`k` float",
])
existing = list(db.dets()['name'])
tel_type = hg['instrument'].attrs.get('telescope_name')
# if tel_type in ['FYST']:
# print(f" Telescope name is: {tel_type}")
# else:
# print(f"Telescope name is not 'FYST'. Check!")
# new toast has TELn_TUBE
# tel_type = tel_type.split('_')[0][:3]
# assert tel_type in ['LAT', 'SAT']
fp = hg['instrument']['focalplane']
for dv in fp:
v = dict([(_k, dv[_k].decode('ascii'))
for _k in ['wafer_slot', 'band', 'name']])
k = v.pop('name')
if k in existing:
continue
v['special_ID'] = int(dv['uid'])
v['toast_band'] = v['band']
v['band'] = v['toast_band'].split('_')[1]
# v['fcode'] = v['band'] #Skipping for FYST, for now
v['tel_type'] = tel_type
v['tube_type'] = "280_GHz_Module" #Hardcoding for now, FYST
v['det_id_'] = 'DET_' + k
v['readout_id'] = k
db.add_props('base', k, **v, commit=False)
db.add_props('quat', k, **{'r': dv['quat'][3],
'i': dv['quat'][0],
'j': dv['quat'][1],
'k': dv['quat'][2]})
db.conn.commit()
db.validate()
return db
def extract_obs_info(h):
t = np.asarray(h['shared']['times'])[[0,-1]]
az = np.asarray(h['shared']['azimuth'][()])
el = np.asarray(h['shared']['elevation'][()])
el_nom = (el.max() + el.min()) / 2
el_span = el.max() - el.min()
# Put az in a single branch ...
az_cut = az[0] - np.pi
az = (az - az_cut) % (2 * np.pi) + az_cut
az_span = az.max() - az.min()
az_nom = (az.max() + az.min()) / 2 % (2 * np.pi)
data = {
'toast_obs_name': h.attrs['observation_name'],
'toast_obs_uid': int(h.attrs['observation_uid']),
'target': h.attrs['observation_name'].split('-')[0].lower(),
'start_time': t[0],
'stop_time': t[1],
'timestamp': t[0],
'duration': t[1] - t[0],
'type': 'obs',
'subtype': 'survey',
'el_nom': el_nom / DEG,
'el_span': el_span / DEG,
'az_nom': az_nom / DEG,
'az_span': az_span / DEG,
'roll_nom': 0.,
'roll_span': 0.,
}
return data
def detdb_to_focalplane(db):
# Focalplane compatible with, like, planet mapper.
fp = metadata.ResultSet(keys=['dets:readout_id', 'xi', 'eta', 'gamma'])
for row in db.props(props=['readout_id', 'quat.r', 'quat.i', 'quat.j', 'quat.k']).rows:
q = quat.quat(*row[1:])
xi, eta, gamma = quat.decompose_xieta(q)
fp.rows.append((row[0], xi, eta, (gamma) % (2*np.pi)))
return fp
def update_obsdb(h5_fpath):
global detsets
global item_count
##Removing functionality of handled ...
# if any([os.path.samefile(filename, f) for f in handled]):
# print(f' -- skipping {filename} -- already bundled')
# continue
with h5py.File(h5_fpath, 'r') as h:
detdb = extract_detdb(h, db=None)
obs_info = extract_obs_info(h)
# This will be one band, one wafer.
props = detdb.props()
tel_type = props['tel_type'][0]
wafers = set(props['wafer_slot'])
tube_type = props['tube_type'][0]
###Removing functionality #Hotwire
# telescope, tube, slot_mask, all_wafers = guess_tube_simple(tel_type, wafers)
bands = list(set(props['band']))
base_wafer = list(wafers)[0]
base_band = bands[0]
all_bands = [base_band]
#Removing handling multiple wafers...
#wafers_found = []
#for wafer in all_wafers:
# print(wafer)
# both = True
# for band in all_bands:
# filename_d = filename\
# .replace(base_band, band)\
# .replace(base_wafer, wafer)
# if not os.path.exists(filename_d):
# both = False
# continue
# if filename_d == filename: # already loaded...
# continue
# with h5py.File(filename_d, 'r') as h:
# detdb_d = extract_detdb(h, db=None)
# obs_info_d = extract_obs_info(h)
# for _n, _p in zip(detdb_d.dets(), detdb_d.props()):
# _p1 = {k: v for k, v in _p.items() if not k.startswith('quat.')}
# _p2 = {k[5:]: v for k, v in _p.items() if k.startswith('quat.')}
# detdb.add_props('base', _n['name'], **_p1)
# detdb.add_props('quat', _n['name'], **_p2)
# if both:
# print(f' -- including {wafer} -- bands {all_bands}')
# wafers_found.append(wafer)
#-------------------------------------------#
# Convert detdb to ResultSet
props = detdb.props()
props.keys[props.keys.index('det_id_')] = 'det_id'
# hotwire (from sotodlib carryover)
# Merge in that telescope name
#Major issue, no telescope ###Proceeding ###Hotwire
# props.merge(metadata.ResultSet(
# keys=['telescope'], src=[(telescope,)] * len(props)))
# obs_info.update({'telescope': telescope,
# 'telescope_flavor': telescope[:3],
# 'detector_flavor': 'TES',
# 'tube_slot': tube,
# 'tube_flavor': props['tube_type'][0],
# 'wafer_slot_mask': '_' + slot_mask,
# 'wafer_slots': ','.join(wafers),
# })
#Skipping dichroic_sub
# In this format, all dets for the set of wafers and a
# single band are stored in one file. Create that detset
# name from the list of wafers + band(s).
detset = '_'.join(sorted(list(wafers)) + sorted(list(bands)))
if detset not in detsets:
fp = detdb_to_focalplane(detdb)
detsets[detset] = [props, fp]
obsfiledb.add_detset(detset, props['readout_id'])
obs_id = f'{int(obs_info["timestamp"])}_{tube_type}'
# path = h5_file
# practical_path = path
# if not path.startswith('/'):
# if context_dir.startswith('/'):
# practical_path = os.path.abspath(path)
# else:
# practical_path = os.path.relpath(path, context_dir)
# Convert to an absolute path directly, without considering context_dir
# practical_path = os.path.abspath(path)
# filename_tmp = os.path.join(practical_path, os.path.split(h5_fpath)[1])
#print(f"tmp filename: {filename_tmp}")
# Add the observation file to the obsfiledb
# obsfiledb.add_obsfile(filename_tmp,obs_id, detset, 0, 1)
abs_path = os.path.abspath(h5_fpath)
# Add the observation file to the obsfiledb
obsfiledb.add_obsfile(abs_path, obs_id, detset, 0, 1)
### --- obs info --- ###
obsdb.update_obs(obs_id, obs_info)
# print(f' added {obs_id}')
logger.ml_pipeline(f'Processed {basename(normpath(h5_fpath))}'
f' with obs_id: {obs_id}')
item_count += 1
def write_context():
global detsets
# detdb.to_file(f'{args.context_dir}/detdb.sqlite')
obsdb.to_file(f'{context_dir}/obsdb.sqlite')
obsfiledb.to_file(f'{context_dir}/obsfiledb.sqlite')
#
# metadata: det_info & focalplane
#
scheme = metadata.ManifestScheme()
scheme.add_exact_match('dets:detset')
scheme.add_data_field('dataset')
db1 = metadata.ManifestDb(scheme=scheme)
scheme = metadata.ManifestScheme()
scheme.add_exact_match('dets:detset')
scheme.add_data_field('dataset')
db2 = metadata.ManifestDb(scheme=scheme)
for detset, (props, fp) in detsets.items():
key = 'dets_' + detset
props.keys = ['dets:' + k for k in props.keys]
write_dataset(props, f'{context_dir}/metadata.h5', key, overwrite=True)
db1.add_entry({'dets:detset': detset, 'dataset': key},
filename='metadata.h5')
key = 'focalplane_' + detset
write_dataset(fp, f'{context_dir}/metadata.h5', key, overwrite=True)
db2.add_entry({'dets:detset': detset, 'dataset': key},
filename='metadata.h5')
db1.to_file(f'{context_dir}/det_info.sqlite')
db2.to_file(f'{context_dir}/focalplane.sqlite')
# And the context.yaml!
context = {
'tags': {'metadata_lib': './'},
'imports': ['sotodlib.io.metadata'],
'obsfiledb': '{metadata_lib}/obsfiledb.sqlite',
#'detdb': '{metadata_lib}/detdb.sqlite',
'obsdb': '{metadata_lib}/obsdb.sqlite',
'obs_loader_type': 'toast3-hdf',
'obs_colon_tags': ['wafer_slot', 'band'],
'metadata': [
{'db': "{metadata_lib}/det_info.sqlite",
'det_info': True},
{'db': "{metadata_lib}/focalplane.sqlite",
'name': "focal_plane"}]
}
#if args.absolute:
# context['tags']['metadata_lib'] = context_dir
open(f'{context_dir}/context.yaml', 'w').write(yaml.dump(context, sort_keys=False))
def process_h5_dir(h5_dir, dry_run):
# Get full paths of all .h5 files in the directory
files = glob.glob(os.path.join(h5_dir, '*.h5'))
logger.ml_pipeline(f'Processing h5 dir: '
f'Found {len(files)} h5 file(s) in '
f'{basename(normpath(h5_dir))} ...')
for h5_fpath in files:
if dry_run:
break
update_obsdb(h5_fpath)
###=============###
# Main Function
###=============###
def main():
# Create the parser with description
parser = argparse.ArgumentParser(
description="Makes Context DB when h5 Observation files are provided.",
epilog= (
"The data file path is hierarchical. The program shall process all dirs within.\n"
"Example: python write_context_primecam_mpi.py ./relative-path/to-h5-parent-dir/"
),
formatter_class=argparse.RawTextHelpFormatter
)
#parser.add_argument('--context-dir', default='context/')
#parser.add_argument('--absolute', action='store_true', help=
# "Work around for relative paths bug in sotodlib ...")
#parser.add_argument('--test', action='store_true', help=
# "Only process 5 items then exit.")
#parser.add_argument('--dichroic-sub', nargs=2, help=
# "Tack on second band by substitution, e.g. f090 f150")
# dry-run an optional argument, defaulting to False, and is activated with --dry-run
parser.add_argument('--dry-run', action='store_true',
help="Dry run through h5 Directories to check all files are found.")
parser.add_argument('h5_dirs', nargs='+',
help="Directory to search for HDF5 data.")
args = parser.parse_args()
tel_info_cachefile = os.path.join(context_dir, 'tels.h5')
# As we process data directories, data files from other data dirs
# can get pulled into coherent observations; record them here to
# not duplicate.
#handled = []
# Loop over data dirs...
# Opening each h5 file in each dir...
for h5_dir in args.h5_dirs:
# Check if h5_dir is a parent dir
subdirs = [dir for dir in os.listdir(h5_dir)
if os.path.isdir(os.path.join(h5_dir, dir))]
if subdirs:
parent_dir = h5_dir
logger.ml_pipeline(f"Indexing parent dir: {parent_dir}")
for subdir in subdirs:
full_subdir_path = os.path.join(parent_dir, subdir)
process_h5_dir(full_subdir_path, args.dry_run)
else:
# If no subdirectories, assume it's directly an h5_dir
# the h5_dir should be a dir containing .h5 files
process_h5_dir(h5_dir, args.dry_run)
# Write out the context
if not args.dry_run:
write_context()
logger.ml_pipeline(f'Wrote {item_count} TOD files to context.')
if __name__ == '__main__':
# Initialize MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
logger = get_ccat_logger(rank)
if rank == 0:
main()