This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
object_properties_dialog.py
87 lines (80 loc) · 4.49 KB
/
object_properties_dialog.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
from astroquery.simbad import Simbad
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QMessageBox
from pyui.object_properties_dialog import Ui_ObjectPropertiesDialog
import re
from PyQt5.QtCore import Qt, QObject, pyqtSignal, QDateTime
from astropy.coordinates import SkyCoord
from astropy import units as u
from object_properties import ObjectProperties
Simbad.add_votable_fields('otype(V)')
Simbad.add_votable_fields('sptype')
class ObjectPropertiesDialog(QDialog):
def __init__(self, settings, object_properties):
super(ObjectPropertiesDialog, self).__init__()
self.ui = Ui_ObjectPropertiesDialog()
self.ui.setupUi(self)
self.settings = settings
self.object_properties = object_properties
enable_simbad_button = lambda: self.ui.simbad.setEnabled(len(self.ui.name.currentText()))
self.ui.name.editTextChanged.connect(lambda txt: enable_simbad_button())
self.ui.name.lineEdit().returnPressed.connect(self.simbad_query)
self.ui.simbad.clicked.connect(self.simbad_query)
self.accepted.connect(self.save_properties)
enable_simbad_button()
self.ui.name.setFocus()
self.ui.name.setEditText(object_properties.name)
self.ui.names.setText(object_properties.names)
self.ui.comment.setText(object_properties.comment)
self.ui.ra.setText(object_properties.ra_str())
self.ui.dec.setText(object_properties.dec_str())
self.ui.date.setDateTime(object_properties.date)
self.ui.type.setText(object_properties.type)
self.ui.sptype.setText(object_properties.sptype)
self.ui.observer.setText(object_properties.observer if object_properties.observer else settings.value('observer'))
self.ui.equipment.setText(object_properties.equipment if object_properties.observer else settings.value('equipment'))
self.ui.position.setText(object_properties.position if object_properties.observer else settings.value('position'))
def simbad_query(self):
search_name = self.ui.name.currentText()
try:
result = Simbad.query_object(search_name)
if not result:
QMessageBox.warning(self, 'Not Found', 'Identifier {} not recognized by Simbad'.format(search_name))
return
except Exception as e:
QMessageBox.critical(self, 'Query Error', 'Error running Simbad query: {}'.format(e))
return
row = result[0] # todo: handle more than one results
main_id = row['MAIN_ID'].decode()
names = [(name[0].decode(), re.sub('\s{2,}', ' ', re.sub('^\*+', '', name[0].decode())).replace('NAME ', '').strip()) for name in Simbad.query_objectids(main_id)]
names = [(n[0],n[1].title()) if n[0][0:4]=='NAME' else n for n in names]
self.ui.name.clear()
plain_names = [n[1] for n in names]
self.ui.names.setText(', '.join(plain_names))
self.ui.name.addItems(plain_names)
self.ui.name.setCurrentText([name[1] for name in names if main_id == name[0]][0])
searched_name = [n for n in plain_names if n.lower() == search_name.lower()]
if len(searched_name):
self.ui.name.setCurrentText(searched_name[0])
self.ui.ra.setText(row['RA'])
self.ui.dec.setText(row['DEC'])
self.ui.sptype.setText(row['SP_TYPE'].decode())
self.ui.type.setText(row['OTYPE_V'].decode())
def save_properties(self):
self.settings.setValue('observer', self.ui.observer.text())
self.settings.setValue('equipment', self.ui.equipment.text())
self.settings.setValue('position', self.ui.position.text())
self.object_properties.name = self.ui.name.currentText()
self.object_properties.names = self.ui.names.text()
self.object_properties.comment = self.ui.comment.text()
self.object_properties.date = self.ui.date.dateTime()
self.object_properties.coordinates = SkyCoord(ra=self.ui.ra.text(), dec=self.ui.dec.text(), unit=(u.hourangle, u.deg))
self.object_properties.type = self.ui.type.text()
self.object_properties.sptype = self.ui.sptype.text()
self.object_properties.observer = self.ui.observer.text()
self.object_properties.equipment = self.ui.equipment.text()
self.object_properties.position = self.ui.position.text()
self.object_properties.write()
def keyPressEvent(self, evt):
if evt.key() == Qt.Key_Enter or evt.key() == Qt.Key_Return:
return
QDialog.keyPressEvent(self.evt)