-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_object_instances.py
76 lines (58 loc) · 2.39 KB
/
get_object_instances.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
import os
import argparse
from astropy.table import Table, vstack
from phrosty.utils import make_object_table, get_templates, get_science, get_roman_bands
infodir = os.getenv('SN_INFO_DIR', None)
assert infodir is not None, 'You need to set SN_INFO_DIR as an environment variable.'
def make_object_tables(oid,n_templates):
"""
Given an SN OID and number of template images, generate 2 text files:
one with the templates, and one with all the science images.
"""
objs = make_object_table(oid)
oid = str(oid)
savedir = os.path.join(infodir,oid)
if not os.path.exists(savedir):
os.mkdir(savedir)
savepath = os.path.join(savedir,f'{oid}_instances.csv')
if not os.path.exists(savepath):
objs.to_csv(savepath, sep=',', index=False)
else:
print(f'{savepath} exists. Skipping this step.')
template_tab = Table(names=('filter','pointing','sca'), dtype=(str,int,int))
science_tab = Table(names=('filter','pointing','sca'), dtype=(str,int,int))
for band in get_roman_bands():
sci = get_science(oid,band,infodir,returntype='table')
temp = get_templates(oid,band,infodir,n_templates=n_templates,returntype='table')
science_tab = vstack([science_tab,sci])
template_tab = vstack([template_tab,temp])
scipath = os.path.join(savedir,f'{oid}_instances_science.csv')
temppath = os.path.join(savedir,f'{oid}_instances_template_{n_templates}.csv')
if not os.path.exists(scipath):
science_tab.write(scipath,format='csv',overwrite=True)
else:
print(f'{scipath} exists. Skipping this step.')
if not os.path.exists(temppath):
template_tab.write(temppath,format='csv',overwrite=True)
else:
print(f'{temppath} exists. Skipping this step.')
def parse_and_run():
parser = argparse.ArgumentParser(
prog='get_object_instances',
description='Get all images that contain the RA/Dec of the input transient OID.'
)
parser.add_argument(
'oid',
type=int,
help='ID of transient. Used to look up information on transient.'
)
parser.add_argument(
"--n-templates",
type=int,
help='Number of template images to retrieve.'
)
args = parser.parse_args()
make_object_tables(args.oid,args.n_templates)
print(f'Tables for {args.oid} retrieved and saved!')
if __name__ == '__main__':
parse_and_run()