class DoseControl(MonitorListener):
    
    def __init__(self, name,pv):
        self.name=name
        self.currentpressure=0.0
        self.outcli=CAClient(pv)
        self.outcli.configure()
        sleep(1)
        self.monitor=self.outcli.camonitor(self)
        
    def waitForGreaterThan(self, target):
        while (self.getCurrentPressure() <= target):
            sleep(0.1)
        
    def getCurrentPressure(self):
        return self.currentpressure
    
    def setSystemPressure(self, sysP,target,flow):
        #SET STARTING SAMPLE PRESSURE
        caput("BL11I-EA-GIR-01:DVPC:SETPOINT:WR", sysP)
        caput("BL11I-EA-GIR-01:MFC1:SETPOINT:WR", flow)
        caput("BL11I-EA-GIR-01:BPR:SETPOINT:WR", target)
        self.waitForGreaterThan(target)
        caput("BL11I-EA-GIR-01:MFC1:SETPOINT:WR", 0)
    
    def setSamplePressure(self,SampleP, target, increment):
        # SET FINAL SAMPLE PRESSURE AND INCREMENTS
        while SampleP <= target:                                #final sample pressure in bar
            SampleP += increment                      #increments in bar
            caput("BL11I-EA-GIR-01:DVPC:SETPOINT:WR", SampleP)
            sleep(5)                      #wait time in seconds

    def monitorChanged(self, mevent):
        try:
            self.currentpressure = float(mevent.getDBR().getDoubleValue()[0])
        except:
            #do nothing
            print
            
示例#2
0
class StripChart(MonitorListener):
    '''
    plot y dataset against x dataset for a fixed length, new data added push oldest data out in the datasets.
    '''

    def __init__(self, xpv, ypv, controlpv, numberofpointstoplot=1000,save=False):
        '''
        Constructor
        '''
        self.xpv=xpv
        self.ypv=ypv
        self.xcli=CAClient(xpv)
        self.ycli=CAClient(ypv)
        self.control=CAClient(controlpv)
        self.x=array.array('d')
        self.y=array.array('d')
        self.plotsize=numberofpointstoplot
        self.isSave=save
        
    def start(self):
        if not self.xcli.isConfigured():
            self.xcli.configure()
        if not self.ycli.isConfigured():
            self.ycli.configure()
        if not self.control.isConfigured():
            self.control.configure()
        self._first=True
        self.xmonitor=self.xcli.camonitor(self)
        self.ymonitor=self.ycli.camonitor(self)
        self.control.caput(1)

    def stop(self):
        self.control.caput(0)
        self.xcli.removeMonitor(self.xmonitor)
        self.ycli.removeMonitor(self.ymonitor)
        if self.xcli.isConfigured():
            self.xcli.clearup()
        if self.ycli.isConfigured():
            self.ycli.clearup()
        if self.control.isConfigured():
            self.control.clearup()
        
    def monitorChanged(self, mevent):
        if self._first:
            self._first=False
            return
        if str(mevent.getSource().getName()).trim()==self.xpv:
            self.x.append(float(mevent.getDBR().getDoubleValue()[0]))
        if str(mevent.getSource().getName()).trim()==self.ypv:
            self.y.append(float(mevent.getDBR().getDoubleValue()[0]))
        if self.x.count() == self.y.count():
            if self.x.count() <= self.plotsize:
                dnp.plot.line(self.x, self.y, "Plot")
            else:
                dnp.plot.plot(self.x[-self.plotsize:], self.y[-self.plotsize:], "Plot")
        
    def writeData(self):
        self.stop()
        self.stattimestamp.removeMonitor(self.timemonitor)
        self.stattotal.removeMonitor(self.countmonitor)
        filename=nextDataFile()
        print "saving data to %s, please wait..." % filename
        #length=min(self.timedata.getData().size, self.countdata.getData().size)
        timedataset=self.timedata.getData()
        countdataset=self.countdata.getData()
        dnp.plot.plot(timedataset, countdataset, "Plot")
        outfile=open(filename,"w")
        for time, count in zip(timedataset.getData().tolist(), countdataset.getData().tolist()):
            outfile.write("%f\t%d\n"% (time, count))
        outfile.close()
        #dnp.io.save(filename, [timedataset, countdataset], format="text") # write 2 text fileseach with one dataset
        print "collection completed."
示例#3
0
class CameraCount(DetectorBase):
    '''
    A scannable detector for diagnose camera providing Starndard EPICS area detector interface.
    By default it returns the total count of the camera when scanning. It also designed to record
    a time series of counts over specified period into a data file.
    Usage:
        to create an diagnose camera scannable object:
        >>>sd3cam=CameraCount("sd3cam","SD3","BL09J-MO-SD-03","counts", "%d")
        to collect time series for 30 seconds at exposure time of 0.1 second
        >>>sd3cam.collectFor(30, 0.1)
        to scan a motor with exposure time of 0.1 second
        >>>scan motor 1 100 1 sd3cam 0.1
    '''
    def __init__(self, name, epicsdevicename, pvroot, unitstring,
                 formatstring):
        '''
        Constructor
        '''
        self.setName(name)
        self.setInputNames([name])
        self.Units = [unitstring]
        self.setOutputFormat([formatstring])
        self.epicsdevicename = epicsdevicename
        self.pvroot = pvroot
        self.exposure = CAClient(pvroot + ":CAM:AcquireTime")
        self.imagemode = CAClient(pvroot + ":CAM:ImageMode")
        self.acquire = CAClient(pvroot + ":CAM:Acquire")
        self.roiport = CAClient(pvroot + ":ROI:NDArrayPort")
        self.roienablecallback = CAClient(pvroot + ":ROI:EnableCallbacks")
        self.roienablex = CAClient(pvroot + ":ROI:EnableX")
        self.roienabley = CAClient(pvroot + ":ROI:EnableY")
        self.roiminx = CAClient(pvroot + ":ROI:MinX")
        self.roiminy = CAClient(pvroot + ":ROI:MinY")
        self.roisizex = CAClient(pvroot + ":ROI:SizeX")
        self.roisizey = CAClient(pvroot + ":ROI:SizeY")
        self.statenablecallback = CAClient(pvroot + ":STAT:EnableCallbacks")
        self.statport = CAClient(pvroot + ":STAT:NDArrayPort")
        self.statcompute = CAClient(pvroot + ":STAT:ComputeStatistics")
        self.stattimestamp = CAClient(pvroot + ":STAT:TimeStamp_RBV")
        self.stattotal = CAClient(pvroot + ":STAT:Total_RBV")
        self.timestamp = []
        self.counts = []

    #override scannable APIs
    def atScanStart(self):
        if not self.exposure.isConfigured():
            self.exposure.configure()
        self.exposurevalue = float(self.exposure.caget())
        if not self.acquire.isConfigured():
            self.acquire.configure()
        self.acquirestate = int(self.acquire.caget())
        if not self.stattotal.isConfigured():
            self.stattotal.configure()
        self.acquire.caput(0)
        #set camera
        if not self.imagemode.isConfigured():
            self.imagemode.configure()
        self.imagemodedata = int(self.imagemode.caget())
        self.imagemode.caput(0)  #Single mode

    def atScanEnd(self):
        self.exposure.caput(self.exposurevalue)
        self.imagemode.caput(self.imagemodedata)
        self.acquire.caput(self.acquirestate)
        if self.exposure.isConfigured():
            self.exposure.clearup()
        if self.acquire.isConfigured():
            self.acquire.clearup()
        if self.stattotal.isConfigured():
            self.stattotal.clearup()
        if self.imagemode.isConfigured():
            self.imagemode.clearup()

    def stop(self):
        if not self.acquire.isConfigured():
            self.acquire.configure()
            self.acquire.caput(0)
            self.acquire.clearup()
        else:
            self.acquire.caput(0)

    #override Detector APIs
    def prepareForCollection(self):
        #set ROI
        if not self.roiport.isConfigured():
            self.roiport.configure()
        self.roiport.caput(self.epicsdevicename + ".CAM")
        if not self.roienablecallback.isConfigured():
            self.roienablecallback.configure()
        self.roienablecallback.caput(1)
        #set STAT
        if not self.statport.isConfigured():
            self.statport.configure()
        self.statport.caput(self.epicsdevicename + ".ROI")
        if not self.statenablecallback.isConfigured():
            self.statenablecallback.configure()
        self.statenablecallback.caput(1)
        if not self.statcompute.isConfigured():
            self.statcompute.configure()
        self.statcomputedata = int(self.statcompute.caget())
        self.statcompute.caput(1)

    def endCollection(self):
        #set ROI
        if self.roiport.isConfigured():
            self.roiport.clearup()
        if self.roienablecallback.isConfigured():
            self.roienablecallback.clearup()
        #set STAT
        if self.statport.isConfigured():
            self.statport.clearup()
        if self.statenablecallback.isConfigured():
            self.statenablecallback.clearup()
        self.statcompute.caput(self.statcomputedat)
        if self.statcompute.isConfigured():
            self.statcompute.clearup()

    def setCollectionTime(self, t):
        if not self.exposure.isConfigured():
            self.exposure.configure()
            self.exposure.caput(t)
            self.exposure.clearup()
        else:
            self.exposure.caput(t)

    def getCollectionTime(self):
        value = -1.0
        if not self.exposure.isConfigured():
            self.exposure.configure()
            value = float(self.exposure.caget())
            self.exposure.clearup()
        else:
            value = float(self.exposure.caget())
        return value

    def collectData(self):
        if not self.acquire.isConfigured():
            self.acquire.configure()
            self.acquire.caput(1)
            self.acquire.clearup()
        else:
            self.acquire.caput(1)

    def readout(self):
        ''' return current total count'''
        output = 0.0
        if not self.stattotal.isConfigured():
            self.stattotal.configure()
            output = float(self.stattotal.caget())
            self.stattotal.clearup()
        else:
            output = float(self.stattotal.caget())
        return float(output)

    def getStatus(self):
        status = -1
        if not self.acquire.isConfigured():
            self.acquire.configure()
            status = int(self.acquire.caget())
            self.acquire.clearup()
        else:
            status = self.acquire.caput(0)
        if status == 1:
            return Detector.BUSY
        return Detector.IDLE

    # Area Detector ROI interface
    def setROI(self, minx, miny, sizex, sizey):
        if not self.roiminx.isConfigured():
            self.roiminx.configure()
            self.roiminx.caput(minx)
            self.roiminx.clearup()
        else:
            self.roiminx.caput(minx)
        if not self.roiminy.isConfigured():
            self.roiminy.configure()
            self.roiminy.caput(miny)
            self.roiminy.clearup()
        else:
            self.roiminy.caput(miny)
        if not self.roisizex.isConfigured():
            self.roisizex.configure()
            self.roisizex.caput(sizex)
            self.roisizex.clearup()
        else:
            self.roisizex.caput(sizex)
        if not self.roisizey.isConfigured():
            self.roisizey.configure()
            self.roisizey.caput(sizey)
            self.roisizey.clearup()
        else:
            self.roisizey.caput(sizey)

    def enableROI(self):
        if not self.roienablex.isConfigured():
            self.roienablex.configure()
            self.roienablex.caput(1)
            self.roienablex.clearup()
        else:
            self.roienablex.caput(1)
        if not self.roienabley.isConfigured():
            self.roienabley.configure()
            self.roienabley.caput(1)
            self.roienabley.clearup()
        else:
            self.roienabley.caput(1)

    def disableROI(self):
        if not self.roienablex.isConfigured():
            self.roienablex.configure()
            self.roienablex.caput(0)
            self.roienablex.clearup()
        else:
            self.roienablex.caput(0)
        if not self.roienabley.isConfigured():
            self.roienabley.configure()
            self.roienabley.caput(0)
            self.roienabley.clearup()
        else:
            self.roienabley.caput(0)

    #time series
    def collectFor(self, t, exposure=1.0):
        #get camera ready
        self.stop()
        if not self.imagemode.isConfigured():
            self.imagemode.configure()
            self.imagemode.caput(2)  #Continuous mode
            self.imagemode.clearup()
        else:
            self.imagemode.caput(2)  #Continuous mode
        self.prepareForCollection()
        self.setCollectionTime(exposure)
        self.timedata = EpicsMonitor('time')
        if not self.stattimestamp.isConfigured():
            self.stattimestamp.configure()
        self.timemonitor = self.stattimestamp.camonitor(self.timedata)
        self.countdata = EpicsMonitor('count')
        if not self.stattotal.isConfigured():
            self.stattotal.configure()
        self.countmonitor = self.stattotal.camonitor(self.countdata)
        timer = Timer(t, self.writeData)
        self.collectData()
        timer.start()

    def writeData(self):
        self.stop()
        self.stattimestamp.removeMonitor(self.timemonitor)
        self.stattotal.removeMonitor(self.countmonitor)
        filename = nextDataFile()
        print "saving data to %s, please wait..." % filename
        #length=min(self.timedata.getData().size, self.countdata.getData().size)
        timedataset = self.timedata.getData()
        countdataset = self.countdata.getData()
        dnp.plot.plot(timedataset, countdataset, "Plot")
        outfile = open(filename, "w")
        for time, count in zip(timedataset.getData().tolist(),
                               countdataset.getData().tolist()):
            outfile.write("%f\t%d\n" % (time, count))
        outfile.close()
        #dnp.io.save(filename, [timedataset, countdataset], format="text") # write 2 text fileseach with one dataset
        print "collection completed."
示例#4
0
class EpicsPVWithMonitorListener(PseudoDevice, MonitorListener, Runnable):
	'''create a scannable that monitors the EPICS PV value changes and update its value passively. 
	This value is used by a running thread to control the scan processing this object participates in.
	'''
	def __init__(self, name, pvstring, unitstring, formatstring):
		self.setName(name)
		self.setInputNames([name])
		self.Units=[unitstring]
		self.setOutputFormat([formatstring])
		self.setLevel(5)
		self.outcli=CAClient(pvstring)
		self.currenttemp=float(self.rawGetPosition())
		self.monitor=None
		self.thread=None
		self.runThread=False

	def atScanStart(self):
		'''prepare to start scan: creating channel, monitor, and start control thread'''
		if not self.outcli.isConfigured():
			self.outcli.configure()
			self.monitor=self.outcli.camonitor(self)
			self.thread=Thread(self,"Thread: "+self.getName())
			self.runThread=True
			self.thread.start()
	def atScanEnd(self):
		'''clean up after scan finished successfully: remove monitor, destroy channel, and stop control thread'''
		if self.outcli.isConfigured():
			self.outcli.removeMonitor(self.monitor)
			self.monitor=None
			self.outcli.clearup()
			self.runThread=False
			self.thread=None
			
	def rawGetPosition(self):
		''' return current position.'''
		output=0.0
		if not self.outcli.isConfigured():
			self.outcli.configure()
			output=float(self.outcli.caget())
			self.outcli.clearup()
		else:
			output=float(self.outcli.caget())
		return float(output)

	def rawAsynchronousMoveTo(self,position):
		'''Not implemented, this is only a monitoring object'''
		print "object " + self.getName()+" cannot be moved."
		return

	def rawIsBusy(self):
		'''monitoring object never busy'''
		return 0

	def monitorChanged(self, mevent):
		self.currenttemp = float(mevent.getDBR().getDoubleValue()[0])

	def run(self):
	#	print "Thread: " + self.getName() + " started"
		while (self.runThread):
			if (JythonServerFacade.getInstance().getScanStatus() == JythonStatus.RUNNING and self.currenttemp >= float(MAXTEMP)):
				JythonServerFacade.getInstance().pauseCurrentScan()	
				print "Scan paused as temperature " + self.getName() +" returns: "+str(self.currenttemp)
			elif (JythonServerFacade.getInstance().getScanStatus() == JythonStatus.PAUSED and self.currenttemp <= float(MINTEMP)):
				print "Scan resumed as temperature " + self.getName() +" returns: "+str(self.currenttemp)
				JythonServerFacade.getInstance().resumeCurrentScan()
			sleep(10)


	def stop(self):
		'''clean up after scan finished successfully: remove monitor, destroy channel, 
		and stop control thread on emergence stop or unexpected crash. If required, can be used to manually clean up the object.'''
		if not self.monitor == None:
			self.outcli.removeMonitor(self.monitor)
			self.monitor=None
		if self.outcli.isConfigured():	
			self.outcli.clearup()
		if not self.thread == None:
			self.runThread=False
			self.thread=None
示例#5
0
class EpicsPVWithMonitorListener(ScannableMotionBase, MonitorListener,
                                 Runnable):
    '''create a scannable that monitors the EPICS PV value changes and update its value passively. 
	This value is used by a running thread to control the scan processing this object participates in.
	'''
    def __init__(self, name, pvstring, unitstring, formatstring):
        self.setName(name)
        self.setInputNames([name])
        self.Units = [unitstring]
        self.setOutputFormat([formatstring])
        self.setLevel(5)
        self.outcli = CAClient(pvstring)
        self.currenttemp = float(self.rawGetPosition())
        self.monitor = None
        self.thread = None
        self.runThread = False

    def atScanStart(self):
        '''prepare to start scan: creating channel, monitor, and start control thread'''
        if not self.outcli.isConfigured():
            self.outcli.configure()
            self.monitor = self.outcli.camonitor(self)
            self.thread = Thread(self, "Thread: " + self.getName())
            self.runThread = True
            self.thread.start()

    def atScanEnd(self):
        '''clean up after scan finished successfully: remove monitor, destroy channel, and stop control thread'''
        if self.outcli.isConfigured():
            self.outcli.removeMonitor(self.monitor)
            self.monitor = None
            self.outcli.clearup()
            self.runThread = False
            self.thread = None

    def rawGetPosition(self):
        ''' return current position.'''
        output = 0.0
        if not self.outcli.isConfigured():
            self.outcli.configure()
            output = float(self.outcli.caget())
            self.outcli.clearup()
        else:
            output = float(self.outcli.caget())
        return float(output)

    def rawAsynchronousMoveTo(self, position):
        '''Not implemented, this is only a monitoring object'''
        print "object " + self.getName() + " cannot be moved."
        return

    def isBusy(self):
        '''monitoring object never busy'''
        return 0

    def monitorChanged(self, mevent):
        self.currenttemp = float(mevent.getDBR().getDoubleValue()[0])

    def run(self):
        #	print "Thread: " + self.getName() + " started"
        while (self.runThread):
            if (JythonServerFacade.getInstance().getScanStatus()
                    == JythonStatus.RUNNING
                    and self.currenttemp >= float(MAXTEMP)):
                JythonServerFacade.getInstance().pauseCurrentScan()
                print "Scan paused as temperature " + self.getName(
                ) + " returns: " + str(self.currenttemp)
            elif (JythonServerFacade.getInstance().getScanStatus()
                  == JythonStatus.PAUSED
                  and self.currenttemp <= float(MINTEMP)):
                print "Scan resumed as temperature " + self.getName(
                ) + " returns: " + str(self.currenttemp)
                JythonServerFacade.getInstance().resumeCurrentScan()
            sleep(10)

    def stop(self):
        '''clean up after scan finished successfully: remove monitor, destroy channel, 
		and stop control thread on emergence stop or unexpected crash. If required, can be used to manually clean up the object.'''
        if not self.monitor == None:
            self.outcli.removeMonitor(self.monitor)
            self.monitor = None
        if self.outcli.isConfigured():
            self.outcli.clearup()
        if not self.thread == None:
            self.runThread = False
            self.thread = None