forked from elacerda/stepgw-schedblock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepgw_sched.py
executable file
·205 lines (173 loc) · 5.52 KB
/
stepgw_sched.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
#!/usr/bin/env python3
import sys
import numpy as np
import astropy.units as u
from os.path import basename
from astropy.io import ascii
from os.path import splitext
from datetime import datetime
from astropy.coordinates import ICRS
__script_name__ = basename(sys.argv[0])
PI = 'Clecio R. Bom and Charles Kilpatrick'
NOW = datetime.now()
DATETIME = NOW.strftime('%Y%m%d%H%M%S')
def read_stepgw_input(filename):
"""
Reader for the STEPGW alert tiles generated by STEP monitor. Today is
only a wrapper for `astropy.io.ascii.read`. Is provides an output
:class:`astropy.table.Table` with the following data:
FieldName
FieldRA
FieldDec
Telscope
Filter
ExpTime
Priority
Status
A_lambda
Parameters
----------
filename : str
Path for the STEPGW alert file generated by STEP monitor.
Returns
-------
:class:`astropy.table.Table`
Output table.
"""
print('-Reading target list from %s ...' % filename)
fnoext = splitext(filename)[0]
pid, alert, skymap_pipeline = fnoext.split('_')
table = ascii.read(filename)
table.sort('Prob', reverse=True)
return table, pid, alert, skymap_pipeline
def write_stepgw_targets_file(tbl, pid, filename_suffix):
"""
Create the targets list file to run with chimera-robobs.py:
chimera-robobs.py --addTar targets_list.csv
Parameters
----------
tbl : :class:`astropy.table.Table`
Table read from `read_stepgw_input` function.
Returns
-------
str
Self-generated output filename following the rule::
targets_PID_DATETIME.csv
where::
DATETIME='YYMMDDHHMMSS'
.
"""
filename = '{}_tagets.csv'.format(filename_suffix)
iter_targets = zip(
tbl['Field_Name'],
tbl['RA'],
tbl['Dec']
)
alert = filename_suffix.split('_')[1]
names = []
with open(filename, 'w+') as f:
print('-Writting target list to {} ...'.format(filename))
f.write('PID,NAME,RA,DEC,EPOCH')
for i, (_name, ra, dec) in enumerate(iter_targets):
###
# Change the name here
# Ex:
# name = f'{pid}_{_name}'
###
name = alert + '_{:04d}'.format(i)
#name = _name
names.append(name)
f.write('\n{},{},{},{},2000'.format(pid, name, ra, dec))
return filename, names
def create_stepgw_blockfile(tbl, pid, filename_suffix, dithering=False):
"""
Creates scheduler block YAML file.
"""
header = """programs:
- name: "{NAME}"
pi: "{pi}"
actions:"""
point = """
- action: point
name: "{name}"
ra: "{ra}"
dec: "{dec}"
"""
autofocus = """
- action: autofocus
step: -1
"""
expose = """ - action: expose
filter: {filt}
frames: 1
exptime: {exptime}
imageType: OBJECT
compress_format: fits_rice
objectName: "{name}"
filename: "{pid}-$DATE-$TIME"
"""
dithering_east = """ - action: point
offset:
east: 10
"""
dithering_west = """ - action: point
offset:
west: 10
"""
iter_targets = zip(
tbl['Field_Name'],
tbl['RA'],
tbl['Dec'],
tbl['Filter'],
tbl['texp'],
)
alert = filename_suffix.split('_')[1]
filename = '{}_schedblock.yaml'.format(filename_suffix)
last_coord = None
with open(filename, 'w') as block:
block.write(header.replace('{NAME}', alert).replace('{pi}', PI))
for (name, ra, dec, filt, exptime) in iter_targets:
coords = ICRS(ra*u.deg, dec*u.deg)
_ra = coords.ra.to_string(u.hourangle, sep=':', precision=2)
_dec = coords.dec.to_string(u.deg, sep=':', precision=2)
_autofocus = autofocus
_point = point.replace('{name}', name).replace('{ra}', str(_ra))
_point = _point.replace('{dec}', str(_dec))
_expose = expose.replace('{name}', name).replace('{pid}', pid)
_expose = _expose.replace('{filt}', filt.upper())
_expose = _expose.replace('{exptime}', str(int(exptime)))
if dithering:
_tmp = dithering_east
_tmp += _expose
_tmp += dithering_west
_tmp += _expose
_expose += _tmp
if last_coord is not None:
last_ra, last_dec = last_coord
if (ra == last_ra) and (dec == last_dec):
_point = ''
_autofocus = ''
_action_block = _point + _autofocus + _expose
last_coord = (ra, dec)
block.write(_action_block)
print('-Scheduler block file', filename, 'is ready')
#def check_observation(tbl):
def main():
try:
filename = sys.argv[1]
except:
print(f'Usage: ./{__script_name__} FILENAME [DITHERING=0/1]')
sys.exit(1)
table, pid, alert, skymap_pipeline = read_stepgw_input(filename)
try:
dithering = bool(sys.argv[2])
print(f'{__script_name__}: Dithering enable')
except:
dithering = False
print(f'{__script_name__}: Dithering disable')
out_suffix = '{}_{}_{}_{}'.format(pid, alert, skymap_pipeline, DATETIME)
targets_filename, names = write_stepgw_targets_file(table, pid, out_suffix)
table.add_column(names, name='name')
create_stepgw_blockfile(table, pid, out_suffix, dithering)
if __name__ == '__main__':
main()