/
conjunctions.py
296 lines (256 loc) · 11.7 KB
/
conjunctions.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
"""
Functions for performing conjunction searches
"""
import datetime
import humanize
import warnings
from typing import Dict, List, Optional, Union
from .classes.search import Search
from ..api import AuroraXRequest, urls
from ..requests import STANDARD_POLLING_SLEEP_TIME
# pdoc init
__pdoc__: Dict = {}
def search(start: datetime.datetime,
end: datetime.datetime,
distance: Union[int, float, Dict[str, Union[int, float]]],
ground: Optional[List[Dict[str, str]]] = [],
space: Optional[List[Dict[str, str]]] = [],
events: Optional[List[Dict[str, str]]] = [],
conjunction_types: Optional[List[str]] = [],
epoch_search_precision: Optional[int] = 60,
response_format: Optional[Dict[str, bool]] = None,
poll_interval: Optional[float] = STANDARD_POLLING_SLEEP_TIME,
return_immediately: Optional[bool] = False,
verbose: Optional[bool] = False) -> Search:
"""
Search for conjunctions between data sources
By default, this function will block and wait until the request completes and
all data is downloaded. If you don't want to wait, set the 'return_immediately`
value to True. The Search object will be returned right after the search has been
started, and you can use the helper functions as part of that object to get the
data when it's done.
Args:
start: start timestamp of the search (inclusive)
end: end timestamp of the search (inclusive)
distance: the maximum distance allowed between data sources when searching for
conjunctions. This can either be a number (int or float), or a dictionary
modified from the output of the "get_advanced_distances_combos()" function.
ground: list of ground instrument search parameters, defaults to []
Example:
[{
"programs": ["themis-asi"],
"platforms": ["gillam", "rabbit lake"],
"instrument_types": ["RGB"],
"ephemeris_metadata_filters": {
"logical_operator": "AND",
"expressions": [
{
"key": "calgary_apa_ml_v1",
"operator": "in",
"values": [ "classified as APA" ]
}
]
}
}]
space: list of one or more space instrument search parameters, defaults to []
Example:
[{
"programs": ["themis-asi", "swarm"],
"platforms": ["themisa", "swarma"],
"instrument_types": ["footprint"],
"ephemeris_metadata_filters": {
"logical_operator": "AND",
"expressions": [
{
"key": "nbtrace_region",
"operator": "in",
"values": [ "north auroral oval" ]
}
]
},
"hemisphere": [
"northern"
]
}]
events: list of one or more events search parameters, defaults to []
Example:
[{
"programs": [ "events" ],
"platforms": [ "toshi" ],
"instrument_types": [ "substorm onsets" ]
}]
conjunction_types: list of conjunction types, defaults to [] (meaning all conjunction
types). Options are in the pyaurorax.conjunctions module, or at the top level using
the pyaurorax.CONJUNCTION_TYPE_* variables.
epoch_search_precision: the time precision to which conjunctions are calculated. Can be
30 or 60 seconds. Defaults to 60 seconds. Note - this parameter is under active
development and still considered "alpha".
response_format: JSON representation of desired data response format
poll_interval: seconds to wait between polling calls, defaults to
pyaurorax.requests.STANDARD_POLLING_SLEEP_TIME
return_immediately: initiate the search and return without waiting for data to
be received, defaults to False
verbose: show the progress of the request using the request log, defaults
Returns:
a pyaurorax.conjunctions.Search object
"""
# create a Search object
s = Search(start,
end,
distance,
ground=ground,
space=space,
events=events,
conjunction_types=conjunction_types,
epoch_search_precision=epoch_search_precision,
response_format=response_format)
if (verbose is True):
print(f"[{datetime.datetime.now()}] Search object created")
# execute the search
s.execute()
if (verbose is True):
print("[%s] Request submitted" % (datetime.datetime.now()))
print("[%s] Request ID: %s" % (datetime.datetime.now(), s.request_id))
print("[%s] Request details available at: %s" % (datetime.datetime.now(),
s.request_url))
# return immediately if we wanted to
if (return_immediately is True):
return s
# wait for data
if (verbose is True):
print("[%s] Waiting for data ..." % (datetime.datetime.now()))
s.wait(poll_interval=poll_interval, verbose=verbose)
# get the data
if (verbose is True):
print("[%s] Retrieving data ..." % (datetime.datetime.now()))
s.get_data()
# return response with the data
if (verbose is True):
print("[%s] Retrieved %s of data containing %d records" % (datetime.datetime.now(),
humanize.filesize.naturalsize(
s.status["search_result"]["file_size"]),
s.status["search_result"]["result_count"]))
# return
return s
def search_async(start: datetime.datetime,
end: datetime.datetime,
distance: Union[int, float, Dict[str, Union[int, float]]],
ground: Optional[List[Dict[str, str]]] = [],
space: Optional[List[Dict[str, str]]] = [],
events: Optional[List[Dict[str, str]]] = [],
conjunction_types: Optional[List[str]] = [],
epoch_search_precision: Optional[int] = 60,
response_format: Optional[Dict[str, bool]] = None) -> Search:
"""
Submit a request for a conjunctions search, return immediately
The request will be done asynchronously by the API. Use the helper functions
as part of the Search object returned to check for data and/or download it.
If you don't want the search to return immediately and rather block until
all data is downloaded, please use the 'search' function instead.
.. deprecated::
This function is deprecated as of 0.9.0. Please use the 'search' function
with the 'return_immediately' flag set to True to get the same behaviour.
This function will be removed in a future release.
Args:
start: start timestamp of the search (inclusive)
end: end timestamp of the search (inclusive)
distance: the maximum distance allowed between data sources when searching for
conjunctions. This can either be a number (int or float), or a dictionary
modified from the output of the "get_advanced_distances_combos()" function.
ground: list of ground instrument search parameters, defaults to []
Example:
[{
"programs": ["themis-asi"],
"platforms": ["gillam", "rabbit lake"],
"instrument_types": ["RGB"],
"ephemeris_metadata_filters": {
"logical_operator": "AND",
"expressions": [
{
"key": "calgary_apa_ml_v1",
"operator": "in",
"values": [ "classified as APA" ]
}
]
}
}]
space: list of one or more space instrument search parameters, defaults to []
Example:
[{
"programs": ["themis-asi", "swarm"],
"platforms": ["themisa", "swarma"],
"instrument_types": ["footprint"],
"ephemeris_metadata_filters": {
"logical_operator": "AND",
"expressions": [
{
"key": "nbtrace_region",
"operator": "in",
"values": [ "north auroral oval" ]
}
]
},
"hemisphere": [
"northern"
]
}]
events: list of one or more events search parameters, defaults to []
Example:
[{
"programs": [ "events" ],
"platforms": [ "toshi" ],
"instrument_types": [ "substorm onsets" ]
}]
conjunction_types: list of conjunction types, defaults to [] (meaning all conjunction
types). Options are in the pyaurorax.conjunctions module, or at the top level using
the pyaurorax.CONJUNCTION_TYPE_* variables.
epoch_search_precision: the time precision to which conjunctions are calculated. Can be
30 or 60 seconds. Defaults to 60 seconds. Note - this parameter is under active
development and still considered "alpha".
response_format: JSON representation of desired data response format
Returns:
a pyaurorax.conjunctions.Search object
"""
warnings.warn("This function is deprecated and will be removed in a future release. Please "
"use the 'search' function with the 'return_immediately' flag to produce the "
"same behaviour.", DeprecationWarning, stacklevel=2)
s = Search(start,
end,
distance,
ground=ground,
space=space,
events=events,
conjunction_types=conjunction_types,
epoch_search_precision=epoch_search_precision,
response_format=response_format)
s.execute()
return s
def describe(search_obj: Search) -> str:
"""
Describe a conjunction search as an "SQL-like" string
Args:
search_obj: the conjunction search to describe
Returns:
the "SQL-like" string describing the conjunction search object
"""
# make request
req = AuroraXRequest(method="post",
url=urls.describe_conjunction_query,
body=search_obj.query)
res = req.execute()
# return
return res.data
def get_request_url(request_id: str) -> str:
"""
Get the conjunction search request URL for a given
request ID. This URL can be used for subsequent
pyaurorax.requests function calls. Primarily this method
facilitates delving into details about a set of already-submitted
conjunction searches.
Args:
request_id: the request identifier
Returns:
the request URL
"""
url = urls.conjunction_request_url.format(request_id)
return url