forked from growth-astro/ztfrest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger_bot.py
267 lines (224 loc) · 9.66 KB
/
trigger_bot.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
# Import the relevant packages
import psycopg2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from astropy.table import Table
from astropy.time import Time
from astropy.io import ascii, fits
from astropy.coordinates import SkyCoord
from astropy.time import Time, TimeDelta
import astropy.units as u
from slack import RTMClient, WebClient
import numpy as np
import logging
import matplotlib.pyplot as plt
import io
import os
import sys
from astropy.time import Time
import traceback
import time
from penquins import Kowalski
from functions_misc import make_triplet, plot_triplet, get_cutouts
from functions_misc import get_dust_info, plot_lc
from functions_misc import get_xmatch_clu_glade, get_bgal_ebv
slack_token = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".slack_access_token.txt")
with open(slack_token, "r") as f:
access_token = f.read()
web_client = WebClient(token=access_token)
def run_on_event(channel_id, bypass=False,
lco_programs=None):
thread_ts = time.time()
#response = web_client.chat_postMessage(
# channel=channel_id,
# text='testing')
if not bypass:
try:
converations = web_client.conversations_list(
channel=channel_id
)
channel_slack_id = channel_id
for converation in converations:
for chan in converation.data["channels"]:
if chan["name"] == channel_id.replace("#",""):
channel_slack_id = chan["id"]
delay_thresh = 15.0
payload = web_client.conversations_history(
channel=channel_slack_id,
oldest=thread_ts-delay_thresh
)
except:
return
if len(payload["messages"]) == 0:
return
doTrigger, trigger_action, filts = False, 'trigger', 'gp,rp,ip,zs,Y'
for mess in payload["messages"]:
print(mess)
message_ts = float(mess["ts"])
if np.abs(message_ts - thread_ts) > delay_thresh:
continue
txt = mess['text']
txtsplit = list(filter(None,txt.split(" ")))
if len(txtsplit) == 0: continue
if txtsplit[0] == "trigger":
doTrigger = True
if len(txtsplit) == 2:
name = txtsplit[1]
elif len(txtsplit) == 3:
name = txtsplit[1]
trigger_action = txtsplit[2]
elif len(txtsplit) == 4:
name = txtsplit[1]
trigger_action = txtsplit[2]
filts = txtsplit[3]
user = mess['user']
if not doTrigger:
return
else:
user, message_ts = 'test', thread_ts
name, trigger_action, filts = 'ZTF20achzlyv', 'trigger', 'gp,rp,ip,zs,Y'
message = []
message.append("Hi <@{0}>! You are interested in ztfrest triggering, right? Let me get right on that for you.".format(user))
message.append('Received request %.1f seconds ago...' % (np.abs(message_ts - thread_ts)))
message.append("We are looking into %s for you" % name)
web_client.chat_postMessage(
channel=channel_id,
text="\n".join(message)
)
df = pd.read_sql_query(f"SELECT * from candidate WHERE name = '{name}'", con)
ra, dec = df["ra"].values[0], df["dec"].values[0]
alerts = pd.read_sql_query(f"SELECT jd, magpsf, sigmapsf, filter, programid FROM lightcurve WHERE name = '{name}'", con)
d_alerts = {'jd': alerts['jd'],
'mag': alerts['magpsf'],
'mag_unc': alerts['sigmapsf'],
'filter': alerts['filter'],
'limmag': np.ones(len(alerts))*99.0,
'forced': np.zeros(len(alerts)),
'programid': alerts['programid']}
lc_alerts = pd.DataFrame(data=d_alerts)
medmags = {}
message = []
for filt in ["g", "r", "i"]:
idx = np.where(lc_alerts["filter"] == filt)[0]
medmags[filt] = np.median(lc_alerts["mag"][idx])
message.append("Median magnitude for %s-band: %.3f" % (filt, medmags[filt]))
#web_client.chat_postMessage(
# channel=channel_id,
# text="\n".join(message)
#)
lco_programs = lco_programs.split(",")
status = {-1: "CANCELED",
0: "WINDOW_EXPIRED",
1: "PENDING",
2: "COMPLETED"}
message = []
if trigger_action in ["status", "delete"]:
print('Checking LCO for existing observations...')
# LCO sometime over next 2 weeks
tstart = Time.now()
tend = Time.now() + TimeDelta(14*u.day)
tstart = str(tstart.isot).replace("T"," ")
tend = str(tend.isot).replace("T"," ")
#Read the secrets
lco_secrets = ascii.read('../lco/secrets.csv', format = 'csv')
PROPOSAL_ID = lco_secrets['PROPOSAL_ID'][0]
API_TOKEN = lco_secrets['API_TOKEN'][0]
from lco import check_observations, delete_observation
obs = check_observations(API_TOKEN, lco_programs=lco_programs)
for key in obs.keys():
if key == name:
for obsid in obs[key].keys():
if trigger_action == "status":
message.append('Observations of %s...' % name)
message.append('Observation ID: %d...' % obs[key][obsid]["obsid"])
message.append('Completion: %s...' % status[obs[key][obsid]["completed"]])
message.append('Filters: %s' % (','.join(obs[key][obsid]["observations"])))
message.append('Program: %s' % obs[key][obsid]["program"])
elif trigger_action == "delete":
delete_observation(obs[key][obsid]["obsid"], API_TOKEN)
message.append('Deleted Observation ID: %d...' % obs[key][obsid]["obsid"])
elif trigger_action == "trigger":
print('Triggering LCO...')
# LCO sometime over next 2 weeks
tstart = Time.now()
tend = Time.now() + TimeDelta(2*u.day)
tstart = str(tstart.isot).replace("T"," ")
tend = str(tend.isot).replace("T"," ")
#Read the secrets
lco_secrets = ascii.read('../lco/secrets.csv', format = 'csv')
PROPOSAL_ID = lco_secrets['PROPOSAL_ID'][0]
API_TOKEN = lco_secrets['API_TOKEN'][0]
from lco import submit_photometric_observation
from lco import submit_spectroscopic_observation
from lco import check_observations, delete_observation
obs = check_observations(API_TOKEN, lco_programs=lco_programs)
trigger = True
for key in obs.keys():
if key == name:
for obsid in obs[key].keys():
if status[obs[key][obsid]["completed"]] == "PENDING":
trigger = False
break
if trigger is True:
obsid = submit_photometric_observation(name, ra, dec,
PROPOSAL_ID, API_TOKEN,
tstart=tstart, tend=tend,
exposure_time = 300,
filters=filts.split(","),
doSubmission=True)
message.append('View the observing request: https://observe.lco.global/requestgroups/{}/'.format(obsid))
else:
message.append('Sorry... %s already has a pending observation.' % name)
#submit_spectroscopic_observation(name, ra, dec,
# PROPOSAL_ID, API_TOKEN,
# tstart=tstart, tend=tend,
# exposure_time = 300,
# doSubmission=True)
web_client.chat_postMessage(
channel=channel_id,
text="\n".join(message)
)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--channel", type=str, default="partnership")
parser.add_argument("-d", "--debug", action="store_true", default=False)
parser.add_argument('--lco-programs', dest='lco_programs',
type=str, required=False,
default='NOAO2020B-005,TOM2020A-008')
cfg = parser.parse_args()
if cfg.channel == 'partnership':
channel = 'C01B3ME3GEQ'
program_ids = [1,2]
elif cfg.channel == 'caltech':
channel = 'C01AUCVUKTP'
program_ids = [1,2,3]
elif cfg.channel == 'test':
channel = 'G01A2AUV8Q2'
program_ids = [1,2,3]
else:
print('Sorry, I do not know that channel...')
exit(0)
# Read the database secrets
info = ascii.read('./db_access.csv', format='csv')
info_db = info[info['db'] == 'db_kn_rt_user']
db_kn = f"host={info_db['host'][0]} dbname={info_db['dbname'][0]} port={info_db['port'][0]} user={info_db['user'][0]} password={info_db['password'][0]}"
# Connect to the database
con = psycopg2.connect(db_kn)
cur = con.cursor()
print(f"Connected to the '{info_db['dbname'][0]}' database")
# Read the secrets for kowalski access
secrets = ascii.read('secrets.csv', format='csv')
username_kowalski = secrets['kowalski_user'][0]
password_kowalski = secrets['kowalski_pwd'][0]
if cfg.debug:
run_on_event(channel, bypass=True, lco_programs=cfg.lco_programs)
exit(0)
while True:
#try:
print('Looking for some triggering to do!')
run_on_event(channel, lco_programs=cfg.lco_programs)
#except:
# pass
time.sleep(5)