-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathixchel_command.py
2527 lines (2335 loc) · 109 KB
/
ixchel_command.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
The IxchelCommand module translates user commands in the Slack channel to actions.
These actions can be telescope commands or requesting information from APIs, like Weatherbit, ClearDarkSky, etc.
Telescope commands are defined by the TelescopeInterface module.
"""
import threading
import os
from xmlrpc.client import Boolean, boolean
import pathlib2
from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
import logging
import re
import requests
import time
import datetime
import pytz
from globals import doAbort
from telescope_interface import TelescopeInterface
from astropy.coordinates import SkyCoord, Angle, AltAz
import astropy.units as u
from astropy.time import Time
from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits
from sky import Satellite, Celestial, SolarSystem, Coordinate
import json
import random
import string
from pathlib import PurePosixPath
import numpy as np
import math
import matplotlib
matplotlib.use('Agg') # don't need display
find_format_string = \
"""[
{{
"type": "section",
"fields": [
{{
"type": "mrkdwn",
"text": "*{Index}*. `Name`: {Name}"
}},
{{
"type": "mrkdwn",
"text": "`Type:` {Type}"
}},
{{
"type": "mrkdwn",
"text": "`RA/DEC:` {RA}/{DEC}"
}},
{{
"type": "mrkdwn",
"text": "`Altitude:` {Altitude}"
}},
{{
"type": "mrkdwn",
"text": "`Azimuth:` {Azimuth}"
}},
{{
"type": "mrkdwn",
"text": "`Magnitude (V):` {V}"
}}
]
}}
]"""
class CommandThread:
def __init__(self, thread, command, user):
self.thread = thread
self.command = command
self.user = user
class IxchelCommand:
commands = []
configure_commands = []
skyObjects = []
threads = []
def __init__(self, ixchel):
self.logger = logging.getLogger('IxchelCommand')
self.ixchel = ixchel
self.config = ixchel.config
self.lock = ixchel.lock
self.channel = self.config.get('slack', 'channel_name')
self.bot_name = self.config.get('slack', 'bot_name')
self.slack = ixchel.slack
self.telescope = ixchel.telescope
self.image_dir = self.config.get(
'telescope', 'image_dir')
# session states to save
self.hdr = False
self.share = False
self.target = 'unknown'
self.preview = True
# build list of backslash commands
self.init_commands()
# init the Sky interface
self.satellite = Satellite(ixchel)
self.celestial = Celestial(ixchel)
self.solarSystem = SolarSystem(ixchel)
self.coordinate = Coordinate(ixchel)
def resetSession(self):
self.hdr = False
self.share = False
self.target = 'unknown'
self.preview = True
def set_target(self, target='unknown'):
self.target = target.strip().lower() # lower case
# replace non-alphanumerics
self.target = re.sub('[^A-Za-z0-9\+\-]', '_', self.target)
def parse(self, message):
text = message['text'].strip()
for cmd in self.commands:
command = re.search(cmd['regex'], text, re.IGNORECASE)
if command:
user = self.slack.get_user_by_id(message.get('user'))
self.logger.info('Received the command: %s from %s.' % (
command.group(0), user.get('name')))
try:
if 'lock' in cmd and cmd['lock'] == True and not self.is_locked_by(user) and not self.share:
self.slack.send_message(
'Please lock the telescope before calling this command.')
return
# clean up threads
self.threads = [
t for t in self.threads if t.thread.is_alive()]
# is this an abort command?
if cmd['function'] == self.abort:
# are there any threads to abort?
if len(self.threads) <= 0:
self.slack.send_message(
'No commands to abort.')
self.setDoAbort(False)
return
self.slack.send_message(
'Aborting current command (%s). Please wait...' % (self.threads[0].command))
self.setDoAbort(True) # signal the abort
return
if len(self.threads) > 0: # not an /abort, but there is another command running
self.slack.send_message(
'Please wait for the current command (%s) to complete.' % (self.threads[0].command))
return
# run this command in a thread
thread = threading.Thread(
target=cmd['function'], args=(command, user,), daemon=True)
commandThread = CommandThread(
thread, command.group(0), user.get('name'))
self.threads.append(commandThread)
thread.start()
# cmd['function'](command, user)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
return
self.slack.send_message(
'%s does not recognize your command (%s).' % (self.bot_name, text))
def handle_error(self, command, error):
self.logger.error('Command failed (%s). %s' % (command, error))
self.slack.send_message('Error. Command (%s) failed.' % command)
def _track(self, on_off):
try:
telescope_interface = TelescopeInterface('track')
telescope_interface.set_input_value('on_off', on_off)
# create a command that applies the specified values
self.telescope.track(telescope_interface)
except Exception as e:
raise Exception("Set track command failed.")
def track(self, command, user):
try:
# assign values
on_off = command.group(1)
self._track(on_off)
self.slack.send_message(
'Telescope tracking is %s.' % on_off.strip().lower())
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def _get_track(self):
try:
telescope_interface = TelescopeInterface('get_track')
self.telescope.get_track(telescope_interface)
# assign values
ha = telescope_interface.get_output_value('ha')
dec = telescope_interface.get_output_value('dec')
# create a command that applies the specified values
return (math.ceil(ha) != 0 or math.ceil(dec) != 0)
except Exception as e:
raise Exception("Get track command failed.")
def get_track(self, command, user):
try:
on_off = 'off'
isOn = self._get_track()
if(isOn):
on_off = 'on'
self.slack.send_message(
'Telescope tracking is %s.' % on_off.strip().lower())
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def offset(self, command, user):
try:
dRA = command.group(1).strip()
dDEC = command.group(2).strip()
self.slack.send_message('%s is offsetting the telescope by dRA=%s/dDEC=%s. Please wait...' %
(self.config.get('slack', 'bot_name'), dRA, dDEC))
telescope_interface = TelescopeInterface('offset')
# assign values
telescope_interface.set_input_value('dRA', dRA)
telescope_interface.set_input_value('dDEC', dDEC)
# create a command that applies the specified values
self.telescope.point(telescope_interface)
# send output to Slack
self.slack.send_message(
'Telescope successfully offset by dRA=%s/dDEC=%s.' % (dRA, dDEC))
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def point_ra_dec(self, command, user):
try:
self.set_target()
ra = command.group(1).strip()
dec = command.group(2).strip()
self.slack.send_message('%s is pointing the telescope to RA=%s/DEC=%s. Please wait...' %
(self.config.get('slack', 'bot_name'), ra, dec))
# turn on telescope tracking
telescope_interface = TelescopeInterface('track')
telescope_interface.set_input_value('on_off', 'on')
self.telescope.track(telescope_interface)
# point the telescope
telescope_interface = TelescopeInterface('point')
# assign values
telescope_interface.set_input_value('ra', ra)
telescope_interface.set_input_value('dec', dec)
# create a command that applies the specified values
self.telescope.point(telescope_interface)
# send output to Slack
self.slack.send_message(
'Telescope successfully pointed to RA=%s/DEC=%s.' % (ra, dec))
# regex to format RA/dec for filename
_ra = re.sub('^(\d{1,2}):(\d{2}):(\d{2}).+', r'\1h\2m\3s', ra)
_dec = re.sub('(\d{1,2}):(\d{2}):(\d{2}).+', r'\1d\2m\3s', dec)
self.set_target('%s%s' % (_ra, _dec))
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def point(self, command, user):
try:
# get object id; assume 1 if none
if command.group(1):
id = int(command.group(1).strip())
else:
id = 1
# ensure object id is valid
if id < 1 or id > len(self.skyObjects):
self.slack.send_message('%s does not recognize the object id (%d). Run \\find first!' % (
self.config.get('slack', 'bot_name'), id))
return
self.set_target()
# find corresponding object
skyObject = self.skyObjects[id-1]
self.slack.send_message('%s is pointing the telescope to "%s". Please wait...' % (
self.config.get('slack', 'bot_name'), skyObject.name))
# turn on telescope tracking
telescope_interface = TelescopeInterface('track')
telescope_interface.set_input_value('on_off', 'on')
self.telescope.track(telescope_interface)
# point the telescope
telescope_interface = TelescopeInterface('point')
# assign values
telescope_interface.set_input_value('ra', skyObject.ra)
telescope_interface.set_input_value('dec', skyObject.dec)
# create a command that applies the specified values
self.telescope.point(telescope_interface)
# send output to Slack
self.slack.send_message(
'Telescope successfully pointed to %s.' % skyObject.name)
self.set_target(skyObject.name)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def _pinpoint(self, _user, ra, dec, time, filter):
# turn off HDR mode
hdr = self.hdr
self.hdr = False
original_filter = filter
try:
# astrometry parameters
solve_field_path = self.config.get(
'pinpoint', 'solve_field_path', '/home/chultun/astrometry/bin/solve-field')
downsample = self.config.get('pinpoint', 'downsample', 2)
scale_low = self.config.get('pinpoint', 'scale_low', 0.55)
scale_high = self.config.get('pinpoint', 'scale_high', 2.00)
radius = self.config.get('pinpoint', 'radius', 50.0)
cpulimit = self.config.get('pinpoint', 'cpu_limit', 30)
max_ra_offset = float(self.config.get(
'pinpoint', 'max_ra_offset', 50.0))
max_dec_offset = float(self.config.get(
'pinpoint', 'max_dec_offset', 50.0))
min_ra_offset = float(self.config.get(
'pinpoint', 'min_ra_offset', 0.05))
min_dec_offset = float(self.config.get(
'pinpoint', 'min_dec_offset', 0.05))
max_tries = int(self.config.get('pinpoint', 'max_tries', 5))
bin = self.config.get('pinpoint', 'bin', 2)
user = self.slack.get_user_by_id(
_user['id']).get('name', _user['id'])
# name and path for pinpoint images
fname = self.get_fitsFname(
'pinpoint', filter, time, bin, user.lower(), 0, '')
path = self.get_fitsPath(user.lower())
ra_target = Angle(ra.replace(' ', ':'), unit=u.hour).degree
dec_target = Angle(dec.replace(' ', ':'), unit=u.deg).degree
# turn tracking on - this is redundant, but sometimes helpful
telescope_interface = TelescopeInterface('track')
telescope_interface.set_input_value('on_off', 'on')
self.telescope.track(telescope_interface)
# center dome - this is redundant, but sometimes helpful
telescope_interface = TelescopeInterface('center_dome')
self.telescope.center_dome(telescope_interface)
# point the telescope
# self.logger.info('Pointing to RA=%s, DEC=%s.' %
# (ra.replace(' ', ':'), dec.replace(' ', ':')))
self.slack.send_message('%s is pointing the telescope to RA=%s/DEC=%s. Please wait...' %
(self.config.get('slack', 'bot_name'), ra, dec))
telescope_interface = TelescopeInterface('point')
telescope_interface.set_input_value('ra', ra)
telescope_interface.set_input_value('dec', dec)
self.telescope.point(telescope_interface)
self.slack.send_message(
'Telescope successfully pointed to RA=%s/DEC=%s.' % (ra, dec))
# get current filter setting
original_filter = self._get_filter()
# change filter to 'filter_for_pinpoint' if not already set
if(original_filter != filter):
result = self._set_filter(filter)
self.logger.info('Filter changed from %s to %s.' %
(original_filter, result))
# start pinpoint iterations
iteration = 0
while(iteration < max_tries):
self.slack.send_message(
'Obtaining intermediate image (#%d) for pinpoint astrometry...' % (iteration+1))
success = self._get_image(time, bin, filter, path, fname)
if success:
self.slack_send_fits_file(path + fname, fname)
else:
self.hdr = hdr # restore HDR setting
# change filter back to original_filter
if(original_filter != filter):
result = self._set_filter(filter)
self.logger.info('Filter changed from %s to %s.' % (
original_filter, result))
self.logger.error(
'Error. Image command failed (%s).' % fname)
return False
telescope_interface = TelescopeInterface('pinpoint')
# assign values
telescope_interface.set_input_value(
'solve_field_path', solve_field_path)
telescope_interface.set_input_value('downsample', downsample)
telescope_interface.set_input_value('scale_low', scale_low)
telescope_interface.set_input_value('scale_high', scale_high)
telescope_interface.set_input_value('ra_target', ra_target)
telescope_interface.set_input_value('dec_target', dec_target)
telescope_interface.set_input_value('radius', radius)
telescope_interface.set_input_value('cpulimit', cpulimit)
telescope_interface.set_input_value('path', path)
telescope_interface.set_input_value('fname', fname)
self.telescope.pinpoint(telescope_interface)
# get field center for this image, if astrometry succeeded
ra_image = telescope_interface.get_output_value('ra_image')
dec_image = telescope_interface.get_output_value('dec_image')
ra_offset = float(ra_target)-float(ra_image)
if ra_offset > 350:
ra_offset -= 360.0
dec_offset = float(dec_target)-float(dec_image)
if(abs(ra_offset) <= min_ra_offset and abs(dec_offset) <= min_dec_offset):
self.hdr = hdr
# change filter back to original_filter
if(original_filter != filter):
result = self._set_filter(filter)
self.logger.info('Filter changed from %s to %s.' % (
original_filter, result))
return True
elif(abs(ra_offset) <= max_ra_offset and abs(dec_offset) <= max_dec_offset):
self.slack.send_message(
"Adjusting telescope pointing (dRA=%f deg, dDEC=%f deg)..." % (ra_offset, dec_offset))
telescope_interface = TelescopeInterface('offset')
telescope_interface.set_input_value('dRA', ra_offset)
telescope_interface.set_input_value('dDEC', dec_offset)
self.telescope.pinpoint(telescope_interface)
else:
self.logger.error("Calculated offsets too large (dRA=%f deg, dDEC=%f deg)! Pinpoint aborted." % (
ra_offset, dec_offset))
self.hdr = hdr
# change filter back to original_filter
if(original_filter != filter):
result = self._set_filter(filter)
self.logger.info('Filter changed from %s to %s.' % (
original_filter, result))
return False
iteration += 1
self.logger.error(
'Pinpoint exceeded maximum number of iterations (%d).' % max_tries)
self.hdr = hdr # restore HDR setting
# change filter back to original_filter
if(original_filter != filter):
result = self._set_filter(filter)
self.logger.info('Filter changed from %s to %s.' %
(original_filter, result))
return False
except Exception as e:
self.hdr = hdr
# change filter back to original_filter
if(original_filter != filter):
result = self._set_filter(filter)
self.logger.info('Filter changed from %s to %s.' % (
original_filter, result))
raise Exception("Failed to _pinpoint the target")
def pinpoint(self, command, user):
try:
# get object id; assume 1 if none
if command.group(1):
id = int(command.group(1).strip())
else:
id = 1
# get exposure time; assume config value if none
if command.group(2):
time = float(command.group(2).strip())
else:
time = self.config.get('pinpoint', 'time', 10)
# get filter; assume config value if none
if command.group(3):
filter = command.group(3).strip()
else:
filter = self.config.get('pinpoint', 'filter', 'clear')
# ensure object id is valid
if id < 1 or id > len(self.skyObjects):
self.slack.send_message('%s does not recognize the object id (%d). Run \\find first!' % (
self.config.get('slack', 'bot_name'), id))
return
self.set_target()
# find corresponding object
skyObject = self.skyObjects[id-1]
self.slack.send_message('%s is pinpointing the telescope to "%s". Please wait...' % (
self.config.get('slack', 'bot_name'), skyObject.name))
success = self._pinpoint(
user, skyObject.ra, skyObject.dec, time, filter)
if success:
self.slack.send_message(
'Telescope successfully pinpointed to %s.' % skyObject.name)
self.set_target(skyObject.name)
else:
self.slack.send_message(
'Telescope failed to pinpoint to %s.' % skyObject.name)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def pinpoint_ra_dec(self, command, user):
try:
self.set_target()
ra = command.group(1).strip()
dec = command.group(2).strip()
# get exposure time; assume config value if none
if command.group(3):
time = float(command.group(3).strip())
else:
time = self.config.get('pinpoint', 'time', 10)
# get filter; assume config value if none
if command.group(4):
filter = command.group(4).strip()
else:
filter = self.config.get('pinpoint', 'filter', 'clear')
self.slack.send_message('%s is pinpointing the telescope to RA=%s/DEC=%s. Please wait...' %
(self.config.get('slack', 'bot_name'), ra, dec))
success = self._pinpoint(user, ra, dec, time, filter)
if success:
self.slack.send_message(
'Telescope successfully pinpointed to RA=%s/DEC=%s.' % (ra, dec))
# regex to format RA/dec for filename
_ra = re.sub('^(\d{1,2}):(\d{2}):(\d{2}).+', r'\1h\2m\3s', ra)
_dec = re.sub('(\d{1,2}):(\d{2}):(\d{2}).+', r'\1d\2m\3s', dec)
self.set_target('%s%s' % (_ra, _dec))
else:
self.slack.send_message(
'Telescope successfully pinpointed to RA=%s/DEC=%s.' % (ra, dec))
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def plot_ra_dec(self, command, user):
ra = command.group(1)
dec = command.group(2)
self.slack.send_message('%s is calculating when RA=%s/DEC=%s is observable from your location. Please wait...' %
(self.config.get('slack', 'bot_name'), ra, dec))
self.coordinate.plot(ra, dec)
def plot(self, command, user):
# get object id; assume 1 if none
if command.group(1):
id = int(command.group(1).strip())
else:
id = 1
# ensure object id is valid
if id < 1 or id > len(self.skyObjects):
self.slack.send_message('%s does not recognize the object id (%d). Run \\find first!' % (
self.config.get('slack', 'bot_name'), id))
return
# find corresponding object
skyObject = self.skyObjects[id-1]
self.slack.send_message('%s is calculating when "%s" is observable from your location. Please wait...' % (
self.config.get('slack', 'bot_name'), skyObject.name))
if skyObject.type == 'Solar System':
self.solarSystem.plot(skyObject)
elif skyObject.type == "Celestial":
self.celestial.plot(skyObject)
elif skyObject.type == "Satellite":
self.satellite.plot(skyObject)
# self.slack.send_message('Name of object is %s.'%skyObject.name)
def find(self, command, user):
try:
search_string = command.group(1)
self.slack.send_message('%s is searching the cosmos for "%s". Please wait...' % (
self.config.get('slack', 'bot_name'), search_string))
satellites = self.satellite.find(search_string)
celestials = self.celestial.find(search_string)
solarSystems = self.solarSystem.find(search_string)
# process total search restults
self.skyObjects = satellites + celestials + solarSystems
telescope = self.ixchel.telescope.earthLocation
if len(self.skyObjects) > 0:
report = ''
index = 1
# calculate local time of observatory
telescope_now = Time(datetime.datetime.utcnow(), scale='utc')
self.slack.send_message('%s found %d match(es):' % (
self.config.get('slack', 'bot_name'), len(self.skyObjects)))
for skyObject in self.skyObjects:
# check for abort
if self.getDoAbort():
self.slack.send_message('Search aborted.')
self.setDoAbort(False)
return
# create SkyCoord instance from RA and DEC
c = SkyCoord(skyObject.ra, skyObject.dec,
unit=(u.hour, u.deg))
# transform RA,DEC to alt, az for this object from the observatory
altaz = c.transform_to(
AltAz(obstime=telescope_now, location=telescope))
# report += '%d.\t%s object (%s) found at RA=%s, DEC=%s, ALT=%f, AZ=%f, VMAG=%s.\n' % (
# index, skyObject.type, skyObject.name, skyObject.ra, skyObject.dec, altaz.alt.degree, altaz.az.degree, skyObject.vmag)
report = find_format_string.format(Index=str(index), Name=skyObject.name, Type=skyObject.type, RA=skyObject.ra,
DEC=skyObject.dec, Altitude='%.1f°' % altaz.alt.degree, Azimuth='%.1f°' % altaz.az.degree, V=skyObject.vmag)
self.slack.send_block_message(report)
index += 1
# don't trigger the Slack bandwidth threshold
time.sleep(1)
else:
self.slack.send_message(
'Sorry, %s knows all but *still* could not find "%s".' % (self.config.get('slack', 'bot_name'), search_string))
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def get_help(self, command, user):
slack_user = self.slack.get_user_by_id(
user['id']).get('name', user['id'])
help_message = self.config.get('slack', 'help_message').format(
bot_name=self.bot_name, user=slack_user) + '\n'
for cmd in sorted(self.commands, key=lambda i: i['regex']):
if not cmd['hide']:
help_message += '>%s\n' % cmd['description']
self.slack.send_message(help_message)
def get_where(self, command, user):
try:
telescope_interface = TelescopeInterface('get_where')
# query telescope
self.telescope.get_precipitation(telescope_interface)
# assign values
ra = telescope_interface.get_output_value('ra')
dec = telescope_interface.get_output_value('dec')
alt = telescope_interface.get_output_value('alt')
az = telescope_interface.get_output_value('az')
slewing = telescope_interface.get_output_value('slewing')
# send output to Slack
self.slack.send_message('Telescope Pointing:')
self.slack.send_message('>RA: %s' % ra)
self.slack.send_message('>DEC: %s' % dec)
self.slack.send_message(u'>Alt: %.1f°' % alt)
self.slack.send_message(u'>Az: %.1f°' % az)
if slewing == 1:
self.slack.send_message('>Slewing? Yes')
else:
self.slack.send_message('>Slewing? No')
# get a DSS image of this part of the sky
ra_decimal = Angle(ra + ' hours').hour
dec_decimal = Angle(dec + ' degrees').degree
url = self.config.get('misc', 'dss_url').format(
ra=ra_decimal, dec=dec_decimal)
self.slack.send_message(
"", [{"image_url": "%s" % url, "title": "Sky Position (DSS2):"}])
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def get_clouds(self, command, user):
try:
telescope_interface = TelescopeInterface('get_precipitation')
# query telescope
self.telescope.get_precipitation(telescope_interface)
# assign values
clouds = telescope_interface.get_output_value('clouds')
# send output to Slack
self.slack.send_message('Cloud cover is %d%%.' % int(clouds*100))
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def get_sun(self, command, user):
try:
telescope_interface = TelescopeInterface('get_sun')
# query telescope
self.telescope.get_precipitation(telescope_interface)
# assign values
alt = telescope_interface.get_output_value('alt')
# send output to Slack
self.slack.send_message('Sun:')
self.slack.send_message('>Altitude: %.1f°' % alt)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def get_dome(self, command, user):
try:
telescope_interface = TelescopeInterface('get_dome')
# query telescope
self.telescope.get_dome(telescope_interface)
# assign values
az = telescope_interface.get_output_value('az')
# send output to Slack
self.slack.send_message(
'The dome slit azimuth is %s°.' % az.strip())
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def _center_dome(self):
try:
telescope_interface = TelescopeInterface('center_dome')
# query telescope
self.telescope.center_dome(telescope_interface)
# assign values
return telescope_interface.get_output_value('az')
except:
self.logger.error('Failed to center the dome.')
raise
def center_dome(self, command, user):
try:
self.slack.send_message(
'Centering dome. Please wait...')
az = self._center_dome()
# send output to Slack
self.slack.send_message(
'The dome slit is centered (az=%s°).' % az.strip())
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def abort(self, command, user):
try:
self.logger.debug("You should never get here.")
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def home_dome(self, command, user):
try:
self.slack.send_message(
'Homing dome. Please wait...')
# right
telescope_interface = TelescopeInterface('home_domer')
# query telescope
self.telescope.home_domer(telescope_interface)
# assign values
az_hit = telescope_interface.get_output_value('az_hit')
rem = telescope_interface.get_output_value('rem')
# left
telescope_interface = TelescopeInterface('home_domel')
# query telescope
self.telescope.home_domel(telescope_interface)
# assign values
az_hit = telescope_interface.get_output_value('az_hit')
rem = telescope_interface.get_output_value('rem')
# send output to Slack
self.slack.send_message(
'The dome is homed.')
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def _get_lights(self):
try:
telescope_interface = TelescopeInterface('get_lights')
# query telescope
self.telescope.get_lights(telescope_interface)
# assign values
on_offs = []
on_offs.append(telescope_interface.get_output_value('1_on_off'))
on_offs.append(telescope_interface.get_output_value('2_on_off'))
on_offs.append(telescope_interface.get_output_value('3_on_off'))
on_offs.append(telescope_interface.get_output_value('4_on_off'))
on_offs.append(telescope_interface.get_output_value('5_on_off'))
on_offs.append(telescope_interface.get_output_value('6_on_off'))
on_offs.append(telescope_interface.get_output_value('7_on_off'))
on_offs.append(telescope_interface.get_output_value('8_on_off'))
return on_offs
except Exception as e:
raise Exception('Failed to get the statuses of the dome lights.')
def get_lights(self, command, user):
try:
on_offs = self._get_lights()
self.slack.send_message('Lights:')
lights = self.config.get('telescope', 'lights').split('\n')
for light in lights:
(light_name, light_num) = light.split('|', 2)
self.slack.send_message('>%s: %s' % (
light_name, on_offs[int(light_num)-1]))
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def get_light_names(self):
light_names = []
try:
lights = self.config.get('telescope', 'lights').split('\n')
for light in lights:
(light_name, light_num) = light.split('|', 2)
light_names.append(light_name)
return light_names
except Exception as e:
raise Exception('Failed to get the light names.')
def _set_lights(self, light_number, on_off):
try:
telescope_interface = TelescopeInterface('set_lights')
telescope_interface.set_input_value('light_number', light_number)
telescope_interface.set_input_value('on_off', on_off)
# query telescope
self.telescope.set_lights(telescope_interface)
except:
self.logger.error('Failed to turn the lights %s.' % on_off)
raise
def set_lights(self, command, user):
light_number_words = ['zero', 'one', 'two', 'three',
'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
try:
# assign input values
light_name = command.group(1).strip()
on_off = command.group(2).strip()
lights = self.config.get('telescope', 'lights').split('\n')
success = True
for light in lights:
(_light_name, light_num) = light.split('|', 2)
if(light_name == 'all' or light_name == _light_name):
self._set_lights(
light_number_words[int(light_num)], on_off)
on_offs = self._get_lights()
self.slack.send_message('Lights:')
for light in lights:
(_light_name, light_num) = light.split('|', 2)
self.slack.send_message('>%s: %s' % (
_light_name, on_offs[int(light_num)-1]))
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def get_mirror(self, command, user):
try:
telescope_interface = TelescopeInterface('get_mirror')
# query telescope
self.telescope.get_mirror(telescope_interface)
# assign values
open_close = telescope_interface.get_output_value('open_close')
# send output to Slack
self.slack.send_message(
'The mirror cover is %s.' % open_close.strip())
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def set_mirror(self, command, user):
try:
telescope_interface = TelescopeInterface('set_mirror')
# assign input values
open_close = command.group(1).strip()
telescope_interface.set_input_value('open_close', open_close)
# query telescope
self.telescope.set_mirror(telescope_interface)
# assign output values
open_closed = telescope_interface.get_output_value(
'open_close').strip()
success = (open_closed.find(open_close) >= 0)
# send output to Slack
if success:
self.slack.send_message(
'The mirror cover is %s.' % open_closed)
else:
self.slack.send_message(
'Failed to %s the mirror cover.' % open_close)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def get_slit(self, command, user):
try:
telescope_interface = TelescopeInterface('get_slit')
# query telescope
self.telescope.get_slit(telescope_interface)
# assign values
open_close = telescope_interface.get_output_value('open_close')
# send output to Slack
self.slack.send_message('The slit is %s.' % open_close.strip())
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def set_slit(self, command, user):
try:
telescope_interface = TelescopeInterface('set_slit')
# assign input values
open_close = command.group(1).strip()
telescope_interface.set_input_value('open_close', open_close)
# query telescope
self.telescope.set_slit(telescope_interface)
# assign output values
open_closed = telescope_interface.get_output_value(
'open_close').strip()
success = (open_closed.find(open_close) >= 0)
# send output to Slack
if success:
self.slack.send_message('The slit is %s.' % open_closed)
else:
self.slack.send_message(
'Failed to %s the slit.' % open_close)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def get_preview(self, command, user):
try:
if (self.preview):
self.slack.send_message('FITS preview is on.')
else:
self.slack.send_message('FITS preview is off.')
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def set_preview(self, command, user):
try:
on_off = command.group(1)
if (on_off == 'on'):
self.preview = True
else:
self.preview = False
self.get_preview(command, user)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def show_configuration_setting(self, setting):
try:
value = self.config.get('configuration', setting)
if value == 'True':
value = 'on'
elif value == 'False':
value = 'off'
self.slack.send_message('Configuration setting `%s` is %s.'%(setting, value))
except Exception as e:
self.logger.error('Failed to get the configuration setting (%s).' % setting)
raise
def get_hdr(self, command, user):
try:
if (self.hdr):
self.slack.send_message('HDR mode is on.')
else:
self.slack.send_message('HDR mode is off.')
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def set_hdr(self, command, user):
try:
on_off = command.group(1)
if (on_off == 'on'):
self.hdr = True
else:
self.hdr = False
self.get_hdr(command, user)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def set_shutterfix(self, command, user):
try:
setting = 'shutterfix'
on_off = command.group(2)
if (on_off == 'on'):
self.config.set('configuration', setting, True)
else:
self.config.set('configuration', setting, False)
self.show_configuration_setting(setting)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def share_lock(self, command, user):
try:
on_off = command.group(1)
if (on_off == 'on'):
self.share = True
else:
self.share = False
self.slack.send_message('Lock sharing is %s.' % on_off)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def get_ccd(self, command, user):
try:
telescope_interface = TelescopeInterface('get_ccd')
# query telescope
self.telescope.get_ccd(telescope_interface)
# assign values
ncol = telescope_interface.get_output_value('ncol')
nrow = telescope_interface.get_output_value('nrow')
name = telescope_interface.get_output_value('name')
tchip = telescope_interface.get_output_value('tchip')
setpoint = telescope_interface.get_output_value('setpoint')
drive = telescope_interface.get_output_value('drive')
# send output to Slack
self.slack.send_message('CCD:')
self.slack.send_message('>Type: %s' % name)
self.slack.send_message('>Pixels: %d x %d' % (nrow, ncol))
self.slack.send_message('>Temperature: %.1f° C' % tchip)
self.slack.send_message('>Set Point: %.1f° C' % setpoint)
self.slack.send_message('>Cooler Drive: %.1f' % drive)
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def set_ccd(self, command, user):
try:
telescope_interface = TelescopeInterface('set_ccd')
# assign input values
cool_warm = command.group(1)
telescope_interface.set_input_value('cool_warm', cool_warm)
setpoint = command.group(2)
telescope_interface.set_input_value('setpoint', setpoint)
# query telescope
self.telescope.set_ccd(telescope_interface)
# assign output values
success = telescope_interface.get_output_value('success')
# send output to Slack
if success:
self.slack.send_message(
'CCD is %sing (setpoint is %s°C). Use \ccd to monitor.' % (cool_warm, setpoint))
else:
self.slack.send_message(
'Failed to adjust CCD cooling settings.')
except Exception as e:
self.handle_error(command.group(0), 'Exception (%s).' % e)
def get_moon(self, command, user):
try:
telescope_interface = TelescopeInterface('get_moon')
# query telescope
self.telescope.get_precipitation(telescope_interface)
# assign values
alt = telescope_interface.get_output_value('alt')
phase = int(telescope_interface.get_output_value('phase')*100)
# send output to Slack
self.slack.send_message('Moon:')
self.slack.send_message('>Altitude: %.1f°' % alt)
self.slack.send_message('>Phase: %d%%' % phase)
except Exception as e: