Exemplo n.º 1
0
    def exportToFile(self, outputFileName = None):
        if not outputFileName:
            getSaveFileName(self)
                
            if not outputFileName:
                return # User cancelled

        self.exporter(outputFileName, self.elementDictionary, self.defaultBeamline)
Exemplo n.º 2
0
    def saveToSDDS(self, sddsFileName=None):
        if not sddsFileName:
            sddsFileName = getSaveFileName(self, "sdds")
            if not sddsFileName:
                return

        with open(sddsFileName, "w"):
            return

        # make sure the top-level parameters are up-to-date
        self.designMomentumEV = convertUnitsStringToNumber(self.ui.designMomentum.text(), "eV")
        self.totalCharge = convertUnitsStringToNumber(self.ui.totalCharge.text(), "C")

        # create local pointer to particle array
        tmp6 = self.myBunch.getDistribution6D().getPhaseSpace6D().getArray6D()

        mySDDS = sdds.SDDS(0)
        mySDDS.description[0] = "RadTrack"
        mySDDS.description[1] = "Copyright 2013-2014 by RadiaBeam Technologies. All rights reserved."
        mySDDS.parameterName = ["designMomentumEV", "totalCharge", "eMassEV"]
        mySDDS.parameterData = [[self.designMomentumEV], [self.totalCharge], [self.eMassEV]]
        mySDDS.parameterDefinition = [
            ["", "", "", "", mySDDS.SDDS_DOUBLE, ""],
            ["", "", "", "", mySDDS.SDDS_DOUBLE, ""],
            ["", "", "", "", mySDDS.SDDS_DOUBLE, ""],
        ]
        mySDDS.columnName = ["x", "xp", "y", "yp", "s", "dp"]
        mySDDS.columnData = [
            [list(tmp6[0, :])],
            [list(tmp6[1, :])],
            [list(tmp6[2, :])],
            [list(tmp6[3, :])],
            [list(tmp6[4, :])],
            [list(tmp6[5, :])],
        ]

        if False:
            print(" ")
            print(" Here is mySDDS.columnData[:]:")
            print(mySDDS.columnData)

        mySDDS.columnDefinition = [
            ["", "m", "", "", mySDDS.SDDS_DOUBLE, 0],
            ["", "rad", "", "", mySDDS.SDDS_DOUBLE, 0],
            ["", "m", "", "", mySDDS.SDDS_DOUBLE, 0],
            ["", "rad", "", "", mySDDS.SDDS_DOUBLE, 0],
            ["", "m", "", "", mySDDS.SDDS_DOUBLE, 0],
            ["", "rad", "", "", mySDDS.SDDS_DOUBLE, 0],
        ]
        mySDDS.save(sddsFileName)
Exemplo n.º 3
0
    def saveToCSV(self, fileName=None):
        if not fileName:
            fileName = getSaveFileName(self, "csv")
            if not fileName:
                return

        with open(fileName, "w"):
            return

        # make sure the top-level parameters are up-to-date
        self.designMomentumEV = convertUnitsStringToNumber(self.ui.designMomentum.text(), "eV")
        self.totalCharge = convertUnitsStringToNumber(self.ui.totalCharge.text(), "C")

        # create local pointer to particle array
        tmp6 = self.myBunch.getDistribution6D().getPhaseSpace6D().getArray6D()
        numParticles = tmp6.shape[1]

        # create a header to identify this as a RadTrack file
        h1 = "RadTrack,Copyright 2012-2014 by RadiaBeam Technologies LLC - All rights reserved (C)\n "
        # names of the top-level parameters
        h2 = "p0 [eV],Q [C],mass [eV]\n "
        # values of the top-level parameters
        h3 = str(self.designMomentumEV) + "," + str(self.totalCharge) + "," + str(self.eMassEV) + "\n "
        # label the columns
        h4 = "x,xp,y,yp,s,dp\n "
        # specify the units
        h5 = "[m],[rad],[m],[rad],[m],[rad]"
        # assemble the full header
        myHeader = h1 + h2 + h3 + h4 + h5
        # write particle data into the file
        #   The following ugliness is used to accommodate savetxt()
        #   There is probably a better way...
        f6 = np.zeros((numParticles, 6))
        f6[:, 0] = tmp6[0, :]
        f6[:, 1] = tmp6[1, :]
        f6[:, 2] = tmp6[2, :]
        f6[:, 3] = tmp6[3, :]
        f6[:, 4] = tmp6[4, :]
        f6[:, 5] = tmp6[5, :]
        np.savetxt(fileName, f6, fmt="%.12e", delimiter=",", comments="", header=myHeader)
Exemplo n.º 4
0
    def savePreviewImage(self):
        imageSuffixes = ['png', 'jpg', 'bmp', 'ppm', 'tiff', 'xbm', 'xpm']
        fileName = getSaveFileName(self, imageSuffixes)
        if not fileName:
            return
        fileExtension = os.path.splitext(fileName)[1]
        self.parent.lastUsedDirectory = os.path.dirname(fileName)

        view = self.ui.graphicsView
        questionBox = rt_qt.QtGui.QMessageBox(rt_qt.QtGui.QMessageBox.Question, 'RadTrack', 'Render entire beamline or just the viewable portion?')
        responses = [questionBox.addButton(text , rt_qt.QtGui.QMessageBox.ActionRole) for text in ['Entire Beamline', 'Viewable Portion']]
        questionBox.exec_()

        if questionBox.clickedButton() == responses[0]:
            boundingRectangle = view.scene().itemsBoundingRect()
        else:
            boundingRectangle = self.visibleSceneRect()

        # reduce image size if either dimension is larger than maxImageDimension
        maxImageDimension = 2**14 - 1
        sceneSize = max([boundingRectangle.width(),
                         boundingRectangle.height()])*self.zoomScale
        scale = min([1.0, maxImageDimension/sceneSize])
        self.zoomScale = self.zoomScale*scale
        view.setTransformationAnchor(rt_qt.QtGui.QGraphicsView.AnchorViewCenter)
        view.scale(scale, scale)
        view.setTransformationAnchor(rt_qt.QtGui.QGraphicsView.NoAnchor)

        try:
            progress = rt_qt.QtGui.QProgressDialog('Creating Image ...', 'Cancel', 0, 6, self)
            progress.setMinimumDuration(0)
            progress.setValue(0)
            image = rt_qt.QtGui.QImage((boundingRectangle.size()*self.zoomScale).toSize(),
                    rt_qt.QtGui.QImage.Format_ARGB32_Premultiplied)
            if progress.wasCanceled():
                return
            progress.setValue(1)
            progress.setLabelText('Filling Background ...')
            if fileExtension in ['png', 'tiff', 'xpm']:
                image.fill(rt_qt.QtGui.QColor('transparent'))
            else:
                image.fill(rt_qt.QtGui.QColor('white'))
            if progress.wasCanceled():
                return

            progress.setValue(2)
            progress.setLabelText('Creating Painter ...')
            painter = rt_qt.QtGui.QPainter(image)
            if progress.wasCanceled():
                return

            progress.setValue(3)
            progress.setLabelText('Rendering Image ...')
            view.scene().render(painter, rt_qt.QtCore.QRectF(), boundingRectangle)
            if progress.wasCanceled():
                return

            progress.setValue(4)
            progress.setLabelText('Saving Image ...')
            if not image.save(fileName):
                rt_qt.QtGui.QMessageBox(rt_qt.QtGui.QMessageBox.Warning,
                                 'RadTrack', "Image (" + fileName + ") was not saved.").exec_()
                progress.reset()
                return

        finally:
            progress.setLabelText('Deleting Painter ...')
            progress.setValue(5)
            try:
                del painter
            except Exception:
                pass
            progress.setValue(6)
            self.drawLengthScale()