-
Notifications
You must be signed in to change notification settings - Fork 41
/
edb_interface.py
217 lines (163 loc) · 6.03 KB
/
edb_interface.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
#! /usr/bin/env python
"""Module to interface the JWST DMS Engineering Database.
This module provides ``jwql`` with functions to interface and query the
JWST DMS Engineering Database. It is designed to have minimal
dependencies on non-builtin python packages.
Authors
-------
- Johannes Sahlmann
Use
---
This module can be imported and used with
::
from jwql.edb import edb_interface
edb_interface.query_single_mnemonic(mnemonic_identifier,
start_time, end_time)
Required arguments:
``mnemonic_identifier`` - String representation of a mnemonic name.
``start_time`` - astropy.time.Time instance
``end_time`` - astropy.time.Time instance
Notes
-----
This module is built on top of ``astroquery.mast`` and uses
JWST-specific MAST services.
The user has to provide a valid MAST authentication token
or be authenticated.
References
----------
The MAST JWST EDB web portal is located at
``https://mast.stsci.edu/portal/Mashup/Clients/JwstEdb/JwstEdb.html``
Dependencies
------------
- astropy
- astroquery
"""
from functools import lru_cache
from astropy.table import Table
from astropy.time import Time
from astroquery.mast import Mast
mast_edb_timeseries_service = 'Mast.JwstEdb.GetTimeseries.All'
mast_edb_dictionary_service = 'Mast.JwstEdb.Dictionary'
mast_edb_mnemonic_service = 'Mast.JwstEdb.Mnemonics'
def mast_authenticate(token=None):
"""Verify MAST authentication status, login if needed."""
if Mast.authenticated() is False:
if token is None:
raise ValueError('You are not authenticated in MAST. Please provide a valid token.')
else:
Mast.login(token=token)
def is_valid_mnemonic(mnemonic_identifier):
"""Determine if the given string is a valid EDB mnemonic.
Parameters
----------
mnemonic_identifier : str
The mnemonic_identifier string to be examined.
Returns
-------
bool
Is mnemonic_identifier a valid EDB mnemonic?
"""
inventory = mnemonic_inventory()[0]
if mnemonic_identifier in inventory['tlmMnemonic']:
return True
else:
return False
@lru_cache()
def mnemonic_inventory():
"""Return all mnemonics in the DMS engineering database.
No authentication is required, this information is public.
Since this is a rather large and quasi-static table (~15000 rows),
it is cached using functools.
Returns
-------
data : astropy.table.Table
Table representation of the mnemonic inventory.
meta : dict
Additional information returned by the query.
"""
out = Mast.service_request_async(mast_edb_mnemonic_service, {})
data, meta = process_mast_service_request_result(out)
# convert numerical ID to str for homogenity (all columns are str)
data['tlmIdentifier'] = data['tlmIdentifier'].astype(str)
return data, meta
def process_mast_service_request_result(result, data_as_table=True):
"""Parse the result of a MAST EDB query.
Parameters
----------
result : list of requests.models.Response instances
The object returned by a call to ``Mast.service_request_async``
data_as_table : bool
If True, return data as astropy table, else return as json
Returns
-------
data : astropy.table.Table
Table representation of the returned data.
meta : dict
Additional information returned by the query
"""
json_data = result[0].json()
if json_data['status'] != 'COMPLETE':
raise RuntimeError('Mnemonic query did not complete.\nquery status: {}\nmessage: {}'.format(
json_data['status'], json_data['msg']))\
try:
# timestamp-value pairs in the form of an astropy table
if data_as_table:
data = Table(json_data['data'])
else:
data = json_data['data'][0]
except KeyError:
raise RuntimeError('Query did not return any data.')
# collect meta data
meta = {}
for key in json_data.keys():
if key.lower() != 'data':
meta[key] = json_data[key]
return data, meta
def query_mnemonic_info(mnemonic_identifier, token=None):
"""Query the EDB to return the mnemonic description.
Parameters
----------
mnemonic_identifier : str
Telemetry mnemonic identifier, e.g. ``SA_ZFGOUTFOV``
token : str
MAST token
Returns
-------
info : dict
Object that contains the returned data
"""
mast_authenticate(token=token)
parameters = {"mnemonic": "{}".format(mnemonic_identifier)}
result = Mast.service_request_async(mast_edb_dictionary_service, parameters)
info = process_mast_service_request_result(result, data_as_table=False)[0]
return info
def query_single_mnemonic(mnemonic_identifier, start_time, end_time, token=None):
"""Query DMS EDB to get the mnemonic readings in a time interval.
Parameters
----------
mnemonic_identifier : str
Telemetry mnemonic identifier, e.g. 'SA_ZFGOUTFOV'
start_time : astropy.time.Time instance
Start time
end_time : astropy.time.Time instance
End time
token : str
MAST token
Returns
-------
data, meta, info : tuple
Table and two dictionaries with the results of the query
"""
mast_authenticate(token=token)
if not is_valid_mnemonic(mnemonic_identifier):
raise RuntimeError('Mnemonic identifier is invalid!')
if not isinstance(start_time, Time):
raise RuntimeError('Please specify a valid start time (instance of astropy.time.core.Time)')
if not isinstance(end_time, Time):
raise RuntimeError('Please specify a valid end time (instance of astropy.time.core.Time)')
parameters = {'mnemonic': mnemonic_identifier, 'start': start_time.iso, 'end': end_time.iso}
result = Mast.service_request_async(mast_edb_timeseries_service, parameters)
data, meta = process_mast_service_request_result(result)
# get auxiliary information (description, subsystem, ...)
info = query_mnemonic_info(mnemonic_identifier)
return data, meta, info