-
Notifications
You must be signed in to change notification settings - Fork 41
/
engdb_oss_msgs.py
289 lines (231 loc) · 12.4 KB
/
engdb_oss_msgs.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
#!/usr/bin/env python
#
"""
This module contains a class that can be used to retrieve OSS event messages from the engineering DB.
The messages are initially retrieved between a user-supplied starting and ending date/time.
The messages can be further filtered by visit ID (e.g. "V02235003001").
For visits that include a target acquisition (TA) observation, details of the TA performace
can also be extracted from the relevant log entries.
Original script created by Marshall Perrin, based on engdb_visit_times.py by Jeff Valenti
Modified by M.Gennaro to return the TA centroid values. Converted into a class, and expanded
to return more TA info by Bryan Hilbert
"""
import os
import argparse
from csv import reader
from datetime import datetime, timedelta, timezone
from getpass import getpass
from requests import Session
from time import sleep
import numpy as np
class EventLog():
def __init__(self, startdate='2022-02-01T02:00:00.0', enddate='', mast_api_token='', verbose=False):
self.startdate = datetime.fromisoformat(startdate)
self.mast_api_token = mast_api_token
self.verbose = verbose
# set or interactively get mast token
if not mast_api_token:
self.mast_api_token = os.environ.get('MAST_API_TOKEN')
if self.mast_api_token is None:
raise ValueError("Must define MAST_API_TOKEN env variable or specify mast_api_token parameter")
if not enddate:
tz_utc = timezone(timedelta(hours=0))
self.enddate = datetime.now(tz=tz_utc)
else:
self.enddate = datetime.fromisoformat(enddate)
self.get_ictm_event_log()
def get_ictm_event_log(self):
"""Retrieve the log entries corresponding to the given starting and ending times
"""
# parameters
mnemonic = 'ICTM_EVENT_MSG'
# constants
base = 'https://mast.stsci.edu/jwst/api/v0.1/Download/file?uri=mast:jwstedb'
mastfmt = '%Y%m%dT%H%M%S'
colhead = 'theTime'
# establish MAST session
session = Session()
session.headers.update({'Authorization': f'token {self.mast_api_token}'})
startstr = self.startdate.strftime(mastfmt)
endstr = self.enddate.strftime(mastfmt)
filename = f'{mnemonic}-{startstr}-{endstr}.csv'
url = f'{base}/{filename}'
if self.verbose:
print(f"Retrieving {url}")
response = session.get(url)
if response.status_code == 401:
exit('HTTPError 401 - Check your MAST token and EDB authorization. May need to refresh your token if it expired.')
response.raise_for_status()
self.eventlog = response.content.decode('utf-8').splitlines()
def extract_oss_event_msgs_for_visit(self, selected_visit_id, ta_only=False, verbose=False):
"""Extract lines from self.eventlog that are for the given visit ID.
"""
# parse response (ignoring header line) and print new event messages
vid = ''
in_selected_visit = False
in_ta = False
self.visit_messages = []
if verbose:
print(f"\tSearching for visit: {selected_visit_id}")
for value in reader(self.eventlog, delimiter=',', quotechar='"'):
if in_selected_visit and ((not ta_only) or in_ta) :
#print(value[0][0:22], "\t", value[2])
self.visit_messages.append(f'{value[0][0:22]}\t{value[2]}')
if value[2][:6] == 'VISIT ':
if value[2][-7:] == 'STARTED':
vstart = 'T'.join(value[0].split())[:-3]
vid = value[2].split()[1]
if vid == selected_visit_id:
if verbose:
print(f"VISIT {selected_visit_id} START FOUND at {vstart}")
self.visit_start = vstart
in_selected_visit = True
if ta_only:
if verbose:
print("Only displaying TARGET ACQUISITION RESULTS:")
elif value[2][-5:] == 'ENDED' and in_selected_visit:
assert vid == value[2].split()[1]
assert selected_visit_id == value[2].split()[1]
vend = 'T'.join(value[0].split())[:-3]
if verbose:
print(f"VISIT {selected_visit_id} END FOUND at {vend}")
self.visit_end = vend
in_selected_visit = False
elif value[2][:31] == f'Script terminated: {vid}':
if value[2][-5:] == 'ERROR':
script = value[2].split(':')[2]
vend = 'T'.join(value[0].split())[:-3]
self.visit_end = vend
dur = datetime.fromisoformat(vend) - datetime.fromisoformat(vstart)
note = f'Halt in {script}'
in_selected_visit = False
elif in_selected_visit and value[2].startswith('*'): # this string is used to mark the start and end of TA sections
in_ta = not in_ta
def extract_oss_SAM(self, selected_visit_id, verbose=False):
# parse response (ignoring header line) and print new event messages
vid = ''
in_selected_visit = False
self.sam_info = {}
if verbose:
print(f"\tSearching for visit: {selected_visit_id}")
for value in reader(self.eventlog, delimiter=',', quotechar='"'):
if in_selected_visit :
if "TA SAM (x, y) = " in value[2]:
out = value[2].split('=')[1]
self.sam_info['SAM_XY'] = np.float_(out.split(','))
if "From guiding:" in value[2]:
out = value[2].split('ideal')[1:]
self.sam_info['XY_Idl'] = np.array([out[0].replace('Y',''),out[1]],dtype=np.float_)
self.sam_info['Guider'] = ((value[2].split('ideal')[0]).split('guider')[1]).replace('X','').strip()
if "target location (v2, v3)" in value[2]:
out = value[2].split('=')[1]
for c in "()'":
out = out.replace(c,'')
self.sam_info['Target (v2,v3)'] = np.float_(out.split(','))
if value[2][:6] == 'VISIT ':
if value[2][-7:] == 'STARTED':
vstart = 'T'.join(value[0].split())[:-3]
vid = value[2].split()[1]
if vid==selected_visit_id:
in_selected_visit = True
elif value[2][-5:] == 'ENDED' and in_selected_visit:
assert vid == value[2].split()[1]
assert selected_visit_id == value[2].split()[1]
vend = 'T'.join(value[0].split())[:-3]
print(f"VISIT {selected_visit_id} END FOUND at {vend}")
in_selected_visit = False
elif value[2][:31] == f'Script terminated: {vid}':
if value[2][-5:] == 'ERROR':
script = value[2].split(':')[2]
vend = 'T'.join(value[0].split())[:-3]
dur = datetime.fromisoformat(vend) - datetime.fromisoformat(vstart)
note = f'Halt in {script}'
in_selected_visit = False
def extract_oss_centroid(self, selected_visit_id, verbose=False):
"""Retrieve information about the TA object's centroid value
Parameters
----------
eventlog : list
List of log entries. (Output from get_ictm_event_log())
selected_visit_id : str
ID of the visit containing the TA exposure. e.g. 'V01275004001'
verbose : bool
If True, print more information to the screen
Returns
-------
"""
# parse response (ignoring header line) and print new event messages
vid = ''
in_selected_visit = False
in_ta = False
self.ta_info = {}
if verbose:
print(f"\tSearching for visit: {selected_visit_id}")
for value in reader(self.eventlog, delimiter=',', quotechar='"'):
if in_selected_visit and in_ta :
if "postage-stamp coord (colCentroid, rowCentroid)" in value[2]:
out = value[2].split('=')[1]
for c in "()'":
out = out.replace(c,'')
self.ta_info["aperture_centroid"] = np.float_(out.split(','))
if "detector coord (colCentroid, rowCentroid)" in value[2]:
out = value[2].split('=')[1]
for c in "()'":
out = out.replace(c,'')
self.ta_info["detector_centroid"] = np.float_(out.split(','))
if "postage-stamp coord (colPeak, rowPeak)" in value[2]:
out = value[2].split('=')[1]
for c in "()'":
out = out.replace(c,'')
self.ta_info["peak_pixel"] = np.float_(out.split(','))
if "(convergenceFlag, convergenceThres)" in value[2]:
out = value[2].split('=')[1]
for c in "()'":
out = out.replace(c,'')
convergence, conv_thresh = out.split(',')
self.ta_info["convergence"] = convergence
self.ta_info["conv_thresh"] = np.float_(conv_thresh)
if "(total, numIter)" in value[2]:
out = value[2].split('=')[1]
for c in "()'":
out = out.replace(c,'')
self.ta_info["peak_signal"] = np.float_(out.split(',')[0])
if "peakValue" in value[2]:
out = value[2].split('=')[1]
self.ta_info["peak_bkgd"] = np.float_(out)
if value[2][:6] == 'VISIT ':
if value[2][-7:] == 'STARTED':
vstart = 'T'.join(value[0].split())[:-3]
vid = value[2].split()[1]
if vid==selected_visit_id:
in_selected_visit = True
elif value[2][-5:] == 'ENDED' and in_selected_visit:
assert vid == value[2].split()[1]
assert selected_visit_id == value[2].split()[1]
vend = 'T'.join(value[0].split())[:-3]
in_selected_visit = False
elif value[2][:31] == f'Script terminated: {vid}':
if value[2][-5:] == 'ERROR':
script = value[2].split(':')[2]
vend = 'T'.join(value[0].split())[:-3]
dur = datetime.fromisoformat(vend) - datetime.fromisoformat(vstart)
note = f'Halt in {script}'
in_selected_visit = False
elif in_selected_visit and value[2].startswith('*'): # this string is used to mark the start and end of TA sections
in_ta = not in_ta
def main():
""" Main function for command line arguments """
parser = argparse.ArgumentParser(
description='Get OSS ICTM Event Messages from Eng DB'
)
parser.add_argument('visit_id', type=str, help='Visit ID as a string starting with V i.e. "V01234003001"')
parser.add_argument('-m', '--mast_api_token', help='MAST API token. Either set this parameter or define environment variable $MAST_API_TOKEN')
parser.add_argument('-s', '--start_date', help='Start date for search, as YYYY-MM-DD. Defaults to 2022-02-02 if not set. ', default='2022-02-02')
parser.add_argument('-e', '--end_date', help='End date for search, as YYYY-MM-DD. Defaults to today if not set. ', default='')
parser.add_argument('-t', '--ta_only', action='store_true', help='Only show the Target Acquisition set of log messages')
parser.add_argument('-v', '--verbose', action='store_true', help='Be more verbose for debugging')
args = parser.parse_args()
visit_info = EventLog(startdate=args.start_date, enddate=args.end_date, mast_api_token=args.mast_api_token, verbose=args.verbose)
visit_info.extract_oss_event_msgs_for_visit(args.visit_id, ta_only=args.ta_only, verbose=args.verbose)
if __name__=="__main__":
main()