-
Notifications
You must be signed in to change notification settings - Fork 2
/
fink.py
409 lines (374 loc) · 13.2 KB
/
fink.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Copyright (c) 2021 Julien Peloton
#
# This file is part of TOM Toolkit
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from tom_alerts.alerts import GenericAlert, GenericBroker, GenericQueryForm
from tom_targets.models import Target
from tom_fink import __version__ as fink_version
from django import forms
from crispy_forms.layout import Fieldset, HTML, Layout
import requests
import markdown as md
import numpy as np
from astropy.time import Time
FINK_URL = "https://fink-portal.org"
COLUMNS = 'i:candid,d:rf_snia_vs_nonia,i:ra,i:dec,i:jd,i:magpsf,i:objectId,d:cdsxmatch'
class FinkQueryForm(GenericQueryForm):
""" Class to organise the Query Form for Fink.
It currently contains forms for
* ObjectId search
"""
help_objectid = """
Enter a valid object ID to access its data, e.g. try:
- ZTF19acmdpyr, ZTF19acnjwgm, ZTF17aaaabte, ZTF20abqehqf, ZTF18acuajcr
This query will return all matching alerts.
"""
objectId = forms.CharField(
required=False,
label='ZTF Object ID',
widget=forms.TextInput(
attrs={'placeholder': 'enter a valid ZTF object ID'}
),
help_text=md.markdown(
help_objectid
),
)
help_conesearch = """
Perform a conesearch around a position on the sky given by (RA, Dec, radius).
The initializer for RA/Dec is very flexible and supports inputs provided in a number of convenient formats.
The following ways of initializing a conesearch are all equivalent (radius in arcsecond):
- 271.3914265, 45.2545134, 5
- 271d23m29.135s, 45d15m16.25s, 5
- 18h05m33.942s, +45d15m16.25s, 5
- 18 05 33.942, +45 15 16.25, 5
- 18:05:33.942, 45:15:16.25, 5
Note that the maximum radius is 60 arcseconds. This query will return all matching objects (not individual alerts).
"""
conesearch = forms.CharField(
required=False,
label='Cone Search',
help_text=md.markdown(
help_conesearch
),
widget=forms.TextInput(
attrs={
'placeholder': 'RA, Dec, radius'
}
)
)
help_datesearch = """
Choose a starting date and a time window to see all processed alerts in this period.
Dates are in UTC, and the time window in minutes.
Among several, you can choose YYYY-MM-DD hh:mm:ss, Julian Date, or Modified Julian Date.
Example of valid search with a window of 2 minutes:
- 2019-11-03 02:40:00, 2
- 2458790.61111, 2
- 58790.11111, 2
Maximum window is 180 minutes (query can be very long!). This query will return all matching objects (not individual
alerts).
"""
datesearch = forms.CharField(
required=False,
label='Date Search',
help_text=md.markdown(
help_datesearch
),
widget=forms.TextInput(
attrs={
'placeholder': 'startdate, window'
}
)
)
help_classsearch = f"""
Choose a class of interest from {FINK_URL}/api/v1/classes
to see the `n_alert` latest alerts processed by Fink. Example
- Early SN candidate, 10
- EB*, 10
- AGN, 15
- Solar System, 50
"""
classsearch = forms.CharField(
required=False,
label='Class Search by number',
help_text=md.markdown(
help_classsearch
),
widget=forms.TextInput(
attrs={
'placeholder': 'class, n_alert'
}
)
)
help_classsearchdate = f"""
Choose a class of interest from {FINK_URL}/api/v1/classes
to see the alerts processed by Fink in the last `n_days_in_past` days. Example
- Early SN Ia the last day: Early SN candidate, 1
- Early SN Ia the last 10 days: Early SN candidate, 10
Note that n_days_in_past_max = 15.
"""
classsearchdate = forms.CharField(
required=False,
label='Class Search by date',
help_text=md.markdown(
help_classsearchdate
),
widget=forms.TextInput(
attrs={
'placeholder': 'class, n_days_in_past'
}
)
)
help_ssosearch = f"""
The list of arguments for retrieving SSO data can be found at {FINK_URL}/api/v1/sso.
The numbers or designations are taken from the MPC archive.
When searching for a particular asteroid or comet, it is best to use the IAU number,
as in 4209 for asteroid "4209 Briggs". You can also try for numbered comet (e.g. 10P),
or interstellar object (none so far...). If the number does not yet exist, you can search for designation.
Here are some examples of valid queries:
* Asteroids by number (default)
* Asteroids (Main Belt): 4209, 1922
* Asteroids (Hungarians): 18582, 77799
* Asteroids (Jupiter Trojans): 4501, 1583
* Asteroids (Mars Crossers): 302530
* Asteroids by designation (if number does not exist yet)
* 2010JO69, 2017AD19, 2012XK111
* Comets by number (default)
* 10P, 249P, 124P
* Comets by designation (if number does no exist yet)
* C/2020V2, C/2020R2
Note for designation, you can also use space (2010 JO69 or C/2020 V2).
"""
ssosearch = forms.CharField(
required=False,
label='Solar System Objects Search',
help_text=md.markdown(
help_ssosearch
),
widget=forms.TextInput(
attrs={
'placeholder': 'sso_name'
}
)
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper.layout = Layout(
HTML(f'''
<p>
<em>
TOM Toolkit Broker Module
(<a target="_blank" href="https://github.com/TOMToolkit/tom_fink">tom_fink</a>)
version {fink_version}
</em>
</p>
<p>
Please see the <a href="{FINK_URL}/api" target="_blank">Fink API homepage</a>
for a detailed description of this broker.
</p>
'''),
self.common_layout,
Fieldset(
None,
'objectId',
'conesearch',
'datesearch',
'classsearchdate',
'ssosearch'
),
)
class FinkBroker(GenericBroker):
"""
The ``FinkBroker`` is the interface to the Fink alert broker.
For information regarding Fink and its available
filters for querying, please see http://134.158.75.151:24000/api
"""
name = 'Fink'
form = FinkQueryForm
def fetch_alerts(self, parameters: dict) -> iter:
""" Call the Fink API based on parameters from the Query Form.
Parameters
----------
parameters: dict
Dictionary that contains query parameters defined in the Form
Example: {
'query_name': 'toto',
'broker': 'Fink',
'objectId': 'ZTF19acnjwgm'
}
Returns
----------
out: iter
Iterable on alert data (list of dictionary). Alert data is in
the form {column name: value}.
"""
# Check the user fills only one query form
allowed_search = [
'objectId', 'conesearch', 'datesearch',
'classsearch', 'classsearchdate', 'ssosearch'
]
nquery = np.sum([len(parameters[i].strip()) > 0 for i in allowed_search])
if nquery > 1:
msg = """
You must fill only one query form at a time! Edit your query to choose
only one query among: ZTF Object ID, Cone Search, Date Search, Class Search, Solar System Objects Search
"""
raise NotImplementedError(msg)
elif nquery == 0:
msg = """
You must fill at least one query form! Edit your query to choose
one query among: ZTF Object ID, Cone Search, Date Search, Class Search, Solar System Objects Search
"""
raise NotImplementedError(msg)
if len(parameters['objectId'].strip()) > 0:
r = requests.post(
FINK_URL + '/api/v1/objects',
json={
'objectId': parameters['objectId'].strip(),
'columns': COLUMNS
}
)
elif len(parameters['conesearch'].strip()) > 0:
try:
ra, dec, radius = parameters['conesearch'].split(',')
except ValueError:
raise
r = requests.post(
FINK_URL + '/api/v1/explorer',
json={
'ra': ra,
'dec': dec,
'radius': radius
}
)
elif len(parameters['datesearch'].strip()) > 0:
try:
startdate, window = parameters['datesearch'].split(',')
except ValueError:
raise
r = requests.post(
FINK_URL + '/api/v1/explorer',
json={
'startdate': startdate,
'window': window
}
)
elif len(parameters['classsearch'].strip()) > 0:
try:
class_name, n_alert = parameters['classsearch'].split(',')
except ValueError:
raise
r = requests.post(
FINK_URL + '/api/v1/latests',
json={
'class': class_name,
'n': n_alert
}
)
elif len(parameters['classsearchdate'].strip()) > 0:
try:
class_name, n_days_in_past = parameters['classsearchdate'].split(',')
except ValueError:
raise
now = Time.now()
start = Time(now.jd - float(n_days_in_past), format='jd').iso
end = now.iso
r = requests.post(
FINK_URL + '/api/v1/latests',
json={
'class': class_name,
'n': 1000,
'startdate': start,
'stopdate': end
}
)
elif len(parameters['ssosearch'].strip()) > 0:
r = requests.post(
FINK_URL + '/api/v1/sso',
json={
'n_or_d': parameters['ssosearch'].strip(),
'columns': COLUMNS
}
)
else:
msg = """
You need to enter one of the query field! Choose among:
search by ZTF objectId, conesearch, search by date, search by class,
search by SSO name.
"""
raise NotImplementedError(msg)
r.raise_for_status()
data = r.json()
return iter(data)
def fetch_alert(self, id: str):
""" Call the Fink API based on parameters from the Query Form.
Parameters
----------
parameters: str
Returns
----------
out: iter
Iterable on alert data (list of dictionary). Alert data is in
the form {column name: value}.
"""
r = requests.post(
FINK_URL + '/api/v1/explorer',
json={
'objectId': id,
'columns': COLUMNS
}
)
r.raise_for_status()
data = r.json()
return data
def process_reduced_data(self, target, alert=None):
pass
def to_target(self, alert: dict) -> Target:
""" Redirect query result to a Target
Parameters
----------
alert: dict
GenericAlert instance
"""
target = Target.objects.create(
name=alert.name,
type='SIDEREAL',
ra=alert.ra,
dec=alert.dec,
)
return target
def to_generic_alert(self, alert):
""" Extract relevant parameters from the Fink alert to the TOM interface
Parameters
----------
alert: dict
Dictionary containing alert data: {column name: value}. See
`self.fetch_alerts` for more information.
Returns
----------
out: GenericAlert
Alert columns to be displayed on the TOM interface
"""
# This URL points to the objectId page in the Fink Science Portal
url = '{}/{}'.format(FINK_URL, alert['i:objectId'])
return GenericAlert(
timestamp=alert['i:jd'],
id=alert['i:candid'],
url=url,
name=alert['i:objectId'],
ra=alert['i:ra'],
dec=alert['i:dec'],
mag=alert['i:magpsf'],
score=alert.get('d:rf_snia_vs_nonia', 0)
)