227 lines
8.7 KiB
Python
227 lines
8.7 KiB
Python
import sys
|
|
#import json
|
|
#import webbrowser
|
|
#import subprocess
|
|
from argparse import ArgumentParser
|
|
|
|
from PyQt5 import QtWidgets,QtGui
|
|
|
|
from ui.ElegantToolsGui import Ui_ElegantGUI
|
|
from distribution import Distribution
|
|
from plot import ElegantPlot
|
|
from model import Model
|
|
from track import Track
|
|
#from machine import Machine
|
|
#from reference import ReferenceManager
|
|
#from sandbox import Sandbox
|
|
#from matching import Matching
|
|
#from elegant import Elegant
|
|
|
|
class ElegantTools(QtWidgets.QMainWindow, Ui_ElegantGUI):
|
|
def __init__(self,phase=0):
|
|
super(ElegantTools, self).__init__()
|
|
self.setupUi(self)
|
|
|
|
# office = office== 1
|
|
self.version = '1.0.1'
|
|
self.setWindowIcon(QtGui.QIcon("rsc/Audrey_Icon.png"))
|
|
|
|
self.plot = ElegantPlot(parent=self)
|
|
self.plot.show()
|
|
self.model = Model(phase=phase,parent=self)
|
|
self.track = Track(parent=self)
|
|
|
|
|
|
self.dists={}
|
|
self.distMapping={}
|
|
self.preferredDist = None
|
|
self.UIDistLoad.clicked.connect(self.loadDist)
|
|
self.UIDistMatch.clicked.connect(self.matchDist)
|
|
self.UIDistSave.clicked.connect(self.saveDist)
|
|
self.UIDistBlurr.clicked.connect(self.addBlurr)
|
|
self.UIDistCenter.clicked.connect(self.centerDist)
|
|
self.UIDistCut.clicked.connect(self.cutDist)
|
|
self.UIDistInput.clicked.connect(self.setPreferredDist)
|
|
|
|
def setPreferredDist(self):
|
|
self.preferredDist = self.getDist()
|
|
self.UITrackInput.setText(self.UIDistList.currentText())
|
|
|
|
def getDist(self):
|
|
if self.UIDistList.count() == 0:
|
|
return None
|
|
tag = self.UIDistList.currentText()
|
|
return self.dists[self.distMapping[tag]]
|
|
|
|
def matchDist(self):
|
|
dist = self.getDist()
|
|
dist.matchDist()
|
|
|
|
def addBlurr(self):
|
|
dist = self.getDist()
|
|
dist.addBlurr()
|
|
|
|
def centerDist(self):
|
|
dist = self.getDist()
|
|
dist.centerDist()
|
|
|
|
def cutDist(self):
|
|
dist = self.getDist()
|
|
dist.cutDist()
|
|
|
|
|
|
def loadDist(self):
|
|
options = QtWidgets.QFileDialog.Options()
|
|
options |= QtWidgets.QFileDialog.DontUseNativeDialog
|
|
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open Elegant Distribution",
|
|
"ReferenceDistribution/Gauss_200pC_20A.out",
|
|
"Elegant Files(*.*)", options=options)
|
|
if not fileName:
|
|
return
|
|
name = fileName.split('/')[-1]
|
|
self.UIDistList.addItem(name)
|
|
self.distMapping[name] = fileName
|
|
self.dists[fileName]=Distribution(parent=self,filename=fileName)
|
|
|
|
|
|
def saveDist(self):
|
|
options = QtWidgets.QFileDialog.Options()
|
|
options |= QtWidgets.QFileDialog.DontUseNativeDialog
|
|
fileName, _ = QtWidgets.QFileDialog.getSaveFileName(self, "Save Elegant Distribution",
|
|
"ReferenceDistribution/dist.sdds",
|
|
"Elegant Files (*.*)", options=options)
|
|
if not fileName:
|
|
return
|
|
dist=self.getDist()
|
|
dist.saveDist(fileName)
|
|
|
|
|
|
|
|
# initialize online model
|
|
# self.model = Model(phase=phase,parent=self)
|
|
|
|
# if phase > 0:
|
|
# office = True
|
|
# self.machine = Machine(parent = True, office = office)
|
|
# self.machine.initPVs(self.model.getElements())
|
|
# self.sandbox = Sandbox(parent = self, machine = self.machine)
|
|
# self.elegant=Elegant(parent=self,model=self.model)
|
|
|
|
# title = "SwissFEL Optics Tools - Lattice %s (Phase %d)" % (self.model.getLatticeVersion(),phase)
|
|
# self.setWindowTitle(title)
|
|
|
|
# initialization
|
|
# self.loadSettingsdirect("Settings/ReferenceSetting.json")
|
|
# self.sandbox.updateSandbox()
|
|
# self.reference = ReferenceManager(parent=self)
|
|
# self.matching = Matching(parent=self, model=self.model, reference = self.reference)
|
|
|
|
# events handling
|
|
# self.actionOpen_2.triggered.connect(self.loadSettings)
|
|
# self.actionSave.triggered.connect(self.saveSettings)
|
|
# self.UIUpdateFromMachine.clicked.connect(self.fullUpdate)
|
|
# self.actionHelp.triggered.connect(self.openGit)
|
|
# self.actionAbout.triggered.connect(self.about)
|
|
# self.actionOpenMatch.triggered.connect(self.loadMatchingConfig)
|
|
# self.actionOpenMatchEditor.triggered.connect(self.editMatchingConfig)
|
|
# self.actionOpenScriptEditor.triggered.connect(self.editMatchingScript)
|
|
# self.actionExportElegant.triggered.connect(self.exportElegant)
|
|
|
|
def closeEvent(self, event):
|
|
self.plot.close()
|
|
event.accept()
|
|
|
|
def about(self):
|
|
QtWidgets.QMessageBox.about(self, "Elegant Tools",
|
|
"Version:%s\nContact: Sven Reiche\nEmail: sven.reiche@psi.ch" % self.version)
|
|
|
|
# def openGit(self):
|
|
# webbrowser.open("https://gitea.psi.ch/reiche/opticstool")
|
|
|
|
# def exportElegant(self):
|
|
# self.elegant.writeElegantFiles('Elegant','dummy','SATBD01')
|
|
|
|
# def loadMatchingConfig(self):
|
|
# options = QtWidgets.QFileDialog.Options()
|
|
# options |= QtWidgets.QFileDialog.DontUseNativeDialog
|
|
# fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open Matching Config File",
|
|
# "MatchingConfig/Reference.json",
|
|
# "Json Files (*.json)", options=options)
|
|
# if not fileName:
|
|
# return
|
|
# self.reference.loadReference(fileName)
|
|
|
|
# def editMatchingConfig(self):
|
|
# options = QtWidgets.QFileDialog.Options()
|
|
# options |= QtWidgets.QFileDialog.DontUseNativeDialog
|
|
# fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open Matching Config File",
|
|
# "MatchingConfig/Reference.json",
|
|
# "Json Files (*.json)", options=options)
|
|
# if not fileName:
|
|
# return
|
|
# subprocess.Popen(["emacs", fileName])
|
|
|
|
# def editMatchingScript(self):
|
|
# options = QtWidgets.QFileDialog.Options()
|
|
# options |= QtWidgets.QFileDialog.DontUseNativeDialog
|
|
# fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open Matching Script File",
|
|
# "Scripts/switchyard.madx",
|
|
# "MadX Files (*.madx)", options=options)
|
|
# if not fileName:
|
|
# return
|
|
# subprocess.Popen(["emacs", fileName])
|
|
|
|
# def saveSettings(self):
|
|
# options = QtWidgets.QFileDialog.Options()
|
|
# options |= QtWidgets.QFileDialog.DontUseNativeDialog
|
|
# fileName, _ = QtWidgets.QFileDialog.getSaveFileName(self, "Save Settings",
|
|
# "Settings/newSetting.json",
|
|
# "Json Files (*.json)", options=options)
|
|
# if not fileName:
|
|
# return
|
|
# settings=self.model.getSettings()
|
|
# with open(fileName, 'w', encoding='utf-8') as f:
|
|
# json.dump(settings, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
|
|
# def loadSettings(self):
|
|
# options = QtWidgets.QFileDialog.Options()
|
|
# options |= QtWidgets.QFileDialog.DontUseNativeDialog
|
|
# fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open Settings",
|
|
# "Settings/ReferenceSetting.json",
|
|
# "Json Files (*.json)", options=options)
|
|
# if not fileName:
|
|
# return
|
|
# self.loadSettingsdirect(fileName)
|
|
|
|
# def loadSettingsdirect(self,fileName):
|
|
# with open(fileName, 'r', encoding='utf-8') as f:
|
|
# settings = json.load(f)
|
|
# self.model.loadSettings(settings)
|
|
# self.status('Reference loaded')
|
|
|
|
# def fullUpdate(self):
|
|
# machine = self.machine.getMachineStatus()
|
|
# self.model.updateFromMachine(machine)
|
|
# self.sandbox.updateSandbox()
|
|
# self.status('Machine Settings')
|
|
|
|
# def status(self,msg=''):
|
|
# self.UIStatus.setText(msg)
|
|
# --------------------------------
|
|
# Main routine
|
|
|
|
|
|
if __name__ == '__main__':
|
|
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create("plastique"))
|
|
parser = ArgumentParser()
|
|
parser.add_argument('-phase', type=int, help='Phase of the online model (0:current, 1:planned, 2:future)', default=0)
|
|
args = parser.parse_args()
|
|
app = QtWidgets.QApplication(sys.argv)
|
|
|
|
main = ElegantTools(phase = args.phase)
|
|
if main:
|
|
main.show()
|
|
sys.exit(app.exec_())
|