Commit fdb5f458 authored by Tammo Jan Dijkema's avatar Tammo Jan Dijkema
Browse files

Initial commit

parents
.idea
.ipynb_checkpoints
__pycache__
#!/usr/bin/env python3
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QVBoxLayout, \
QGridLayout, QLineEdit, QLabel, QCheckBox
from PyQt5.QtCore import QTimer, Qt
from PyQt5 import QtCore
from PyQt5.QtGui import QColor, QFont, QIcon
import webbrowser
import os.path
import pickle
import numpy as np
import astropy.units as u
from astropy.units import Quantity
from astroplan import Observer
class HPIBGui(QWidget):
def __init__(self, argv, app: QApplication):
super().__init__()
self.lo_textbox = QLineEdit(self)
self.lo_textbox.setText("lo freq")
self.lo_textbox.returnPressed.connect(self.setLOFreq)
self.receiver_textbox = QLineEdit(self)
self.receiver_textbox.setText("rcv freq")
self.receiver_textbox.returnPressed.connect(self.setReceiverFreq)
self.doppler_checkbox = QCheckBox("Doppler tracking", self)
self.doppler_checkbox.stateChanged.connect(self.doppler_checkbox_changed)
app.focusChanged.connect(self.focus_changed)
self.help_label = QLabel(self)
self.help_label.setText("Press Enter to confirm or Escape to cancel")
self.left = 10
self.top = 10
self.title = 'HPIBGui'
self.width = 654
self.height = 200
self.init_ui()
self.timer_update = QTimer(self)
self.timer_update.timeout.connect(self.updateValues)
self.doppler_checkbox.setFocus()
self.timer_update.start(500)
self.i = 0
self.show()
def focus_changed(self, old, new):
if new in (self.lo_textbox, self.receiver_textbox):
self.help_label.setText("Press Enter to confirm or Escape to cancel")
else:
self.help_label.setText("")
def keyPressEvent(self, e):
if e.key() == Qt.Key_Escape:
if self.lo_textbox.hasFocus():
self.lo_textbox.clearFocus()
self.lo_textbox.setText("")
elif self.receiver_textbox.hasFocus():
self.receiver_textbox.clearFocus()
self.receiver_textbox.setText("")
def updateValues(self):
self.i += 1
if not self.lo_textbox.hasFocus():
self.lo_textbox.setText(str(self.i))
if not self.receiver_textbox.hasFocus():
self.receiver_textbox.setText(str(self.i))
def parse_input(self, new_freq_txt):
"""
Convert a string to Astropy quantity in Hz. If not possible,
display an error and raise the error.
Args:
quantity_string (str): string which may contain a unit, otherwise
it is interpreted as Hz
Returns:
Quantity: astropy quantity in Hz
"""
if new_freq_txt.isnumeric():
new_freq = int(new_freq_txt) * u.Hz
else:
try:
new_freq = Quantity(new_freq_txt)
except (ValueError, TypeError):
self.help_label.setText("Could not parse input")
raise
if not new_freq.unit.is_equivalent(u.Hz):
self.help_label.setText("New frequency should be in Hz")
raise ValueError("New frequency should be in Hz")
return new_freq
def setLOFreq(self):
try:
new_freq = self.parse_input(self.lo_textbox.text())
except (ValueError, TypeError):
return
try:
print("Set LO to {}".format(new_freq))
except ValueError:
self.help_label.setText("Could not set LO to {}".format(new_freq))
return
self.lo_textbox.clearFocus()
self.help_label.setText("")
def setReceiverFreq(self):
try:
new_freq = self.parse_input(self.lo_textbox.text())
except (ValueError, TypeError):
return
try:
print("Set receiver to {}".format(new_freq))
except ValueError:
self.help_label.setText("Could not set LO to {}".format(new_freq))
return
self.receiver_textbox.clearFocus()
self.help_label.setText("")
def doppler_checkbox_changed(self):
self.lo_textbox.setDisabled(self.doppler_checkbox.isChecked())
self.lo_textbox.clearFocus()
self.receiver_textbox.clearFocus()
def init_ui(self):
vbox = QVBoxLayout()
gridbox = QGridLayout()
gridbox.addWidget(QLabel("Local Oscillator"), 0, 0)
gridbox.addWidget(self.lo_textbox, 0, 1)
gridbox.addWidget(QLabel("Receiver"), 1, 0)
gridbox.addWidget(self.receiver_textbox, 1, 1)
vbox.addLayout(gridbox)
vbox.addWidget(self.doppler_checkbox)
vbox.addWidget(self.help_label)
self.setLayout(vbox)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = HPIBGui(sys.argv, app)
sys.exit(app.exec_())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment