Example #1
0
def loadImageIntoSFH(path, iFileLoader=None):
	if iFileLoader is None:
		iFileLoader = FILELOADERS[path.split('.')[-1].upper()]
#	print "loadIntoSfh loading: %s using %s" % (path, str(iFileLoader))
	sfh = ScanFileHolder()
	sfh.load(iFileLoader(path))
	return sfh
Example #2
0
class ImageUtility(object):
	def __init__(self, panelName="Data Vector", iFileLoader=None):
		self.panel = panelName;
		self.iFileLoader = iFileLoader;
		
		self.filename= None;
		self.data = ScanFileHolder();
		self.dataset = None;


	def loadDataFromFile(self, fileName, fileLoader):
		if fileLoader is None:
			import os
			fileLoader = GDA_FILELOADERS[os.path.splitext(fileName)[-1].upper()];

		print fileName
		self.data.load(fileLoader(fileName));

		return self.data;

	def loadDataset(self, fileName = None, fileLoader = None):
		self.dataset = None;
		
		if fileLoader == None:
			fileLoader = self.iFileLoader;
		
		if fileName != None:#Get data from file directly
			self.data = self.loadDataFromFile(fileName, fileLoader);
			if self.data is not None:
				self.filename = fileName;
#				self.dataset = self.data[0];
				self.dataset = self.data.getAxis(0);
		else: 
			print "Please give a full file name"
			return;

		return self.dataset;
			
	def loadFile(self, fileName = None, fileLoader = None):
		return self.loadDataset(fileName, fileLoader);

	def open(self, fileName):
		self.display(fileName);
		
	def display(self, fileName):
		if fileName is None:
			print "Please give a full file name to display";
			return;
		else:
			dataset = self.loadDataset(fileName)
				
		if self.panel:
			SDAPlotter.imagePlot(self.panel, dataset);
		else:
			print "No panel set to display"
			raise Exception("No panel_name set in %s. Set this or set %s.setAlive(False)" % (self.name,self.name));
Example #3
0
    def loadPNGFile(self, fileName):
        #To check file exist:
        if not self.fileExists(fileName, 5):
            print "File '" + fileName + "' does not exist!"
            raise Exception("No File Found Error")

        dataset = None
        sfh = ScanFileHolder()
        sfh.load(PNGLoader(fileName))
        dataset = sfh.getAxis(0)
        return dataset
Example #4
0
def getScanFileHolderXY(xName, xDataRaw, yName, yDataRaw):
    if len(xDataRaw) != len(yDataRaw):
        raise "len(xDataRaw) != len(yDataRaw)"
    #convert to list from array
    xData = []
    yData = []
    for i in range(len(yDataRaw)):
        xData.append(xDataRaw[i])
        yData.append(yDataRaw[i])
    scanFileHolder = ScanFileHolder()
    scanFileHolder.load(XYDataLoader(xName, xData, yName, yData))
    return scanFileHolder
Example #5
0
def getScanFileHolderXY(xName,xDataRaw,yName, yDataRaw):
    if len(xDataRaw) != len(yDataRaw):
        raise ValueError("len(xDataRaw) != len(yDataRaw)")
    #convert to list from array
    xData = []
    yData = []
    for i in range(len(yDataRaw)):
        xData.append(xDataRaw[i])
        yData.append(yDataRaw[i])
    scanFileHolder = ScanFileHolder()
    scanFileHolder.load(XYDataLoader(xName,xData,yName, yData))
    return scanFileHolder
Example #6
0
def loadSRSData(filename):
    '''Load SRS data file into a ScanFileHolder object, 
    supporting relative loading with respect to the current collected data (0)'''
    sfh = ScanFileHolder()
    try:
        if filename == None:
            #current file
            sfh.load(SRSLoader(_getCurrentFileName(0)))
        elif representsInt(filename):
            #past file - relative path
            sfh.load(SRSLoader(_getCurrentFileName(int(filename))))
        elif not filename.startswith(File.separator):
            #filename with extension
            sfh.load(SRSLoader(os.path.join(PathConstructor.createFromDefaultProperty(),filename)))
        else:
            #absolute file path
            sfh.load(SRSLoader(filename))
    except ScanFileHolderException, err:
        print "File loader failed. " + err
def loadMacData(filename):
    '''Load MAC data file into a ScanFileHolder object, supporting relative loading with respect to the current collected data (0)'''
    sfh = ScanFileHolder()
    try:
        if filename == None:
            #current file
            sfh.load(MACLoader(0))
        elif representsInt(filename):
            #past file - relative path
            sfh.load(MACLoader(int(filename)))
        else:
            #absolute file path
            sfh.load(MACLoader(filename))
    except ScanFileHolderException, err:
        print "File loader failed. " + err
Example #8
0
class UViewDetectorClass(DetectorBase):
	DETECTOR_STATUS_IDLE, DETECTOR_STATUS_BUSY, DETECTOR_STATUS_PAUSED, DETECTOR_STATUS_STANDBY, DETECTOR_STATUS_FAULT, DETECTOR_STATUS_MONITORING = range(6);
	ImageFileLoaders={  'TIF' : TIFFImageLoader,
						'TIFF': TIFFImageLoader,
						'JPG' : JPEGLoader,
						'JPEG': JPEGLoader,
						'PNG' : PNGLoader };

	def __init__(self, name, panelName, detector):
		self.setName(name);
		self.setInputNames([name]);
		self.setExtraNames([]);
#		self.Units=[strUnit];
		self.setLevel(7);

		self.panel = panelName;
		self.detector = detector;
		self.scanNumberTracker = NumTracker("tmp");

		self.fileName=None;
		self.data = ScanFileHolder()
		self.exposureTime = 0

		self.logScale = False;
		self.alive=True;

#		To setup the image folder according to I06 requirement
		self.scanImageDirectory();
		self.fastMode=False;
		self.protection=True;
#		self.protectCamera();

		self.verbose = False

	#DetectorBase Implementation
	def getPosition(self):
		return self.readout();

	def asynchronousMoveTo(self,newPos):
		self.setCollectionTime(newPos);
		self.collectData();

	def getCollectionTime(self):
		self.exposureTime=self.detector.getCollectionTime();
		return self.exposureTime;

	def setCollectionTime(self, newExpos):
		self.exposureTime = newExpos;
		self.detector.setCollectionTime(self.exposureTime);

	def collectData(self):
		#self.detector.setCollectionTime(self.exposureTime)
		self.detector.collectData();
		return;

	def prepareForCollection(self):
		#To put camera in the "camera mode"
		self.detector.setCameraInProgress(False);
		self.detector.prepareForCollection();

	def endCollection(self):
#		self.detector.endCollection();
		if self.protection:
			self.protectCamera();
		return;

	def readout(self):
		self.fileName  = self.detector.readout();
		if self.alive:
			self.display();
		return self.fileName;

	def getStatus(self):
		return self.detector.getStatus();

	def createsOwnFiles(self):
		return True;

	def toString(self):
		self.getPosition();
		return "Latest image file: " + self.getFullFileName();

	def isBusy(self):
		return self.detector.getStatus() == Detector.BUSY

## Extra Implementation

	def setFileFormat(self, fileExt, contents=2):
		'''
		fileExtension: 	dat: Raw data uncompressed
			 			png: PNG compressed
			 			tiff: TIFF compressed
			 			bmp: BMP uncompressed
			 			jpg: JPG compressed
			 			tif: TIFF uncompressed
		imagecontents: 	0: RGB 8+8+8 bits x,y,z, as seen on screen
			 			1: RGB 8+8+8 bits,x,y raw, z as seen on screen
			 			2: RAW 16 bits graylevel x, y, z raw data
		'''
		self.detector.setFileFormat(fileExt, contents);

	def setImageAverage(self, averageParameter):
		'''
		 averageParameter:	-1: Sliding average. Current does NOT work!
		 					0: No average
		 					1 ~ 99: Number of frames to be averaged
		'''
		self.detector.setImageAverageNumber(averageParameter);

	def getImageAverage(self):
		iv=self.detector.getImageAverageNumber();
		return iv;

	def protectCamera(self):
		#self.detector.setCollectionTime(0.1);
		self.detector.setCameraInProgress(True);
		if self.verbose:
			print "Camera is set to 'Recorder Mode' for protection.";
		return;


	def setAlive(self, newAlive=True):
		self.alive = newAlive;

	def setLogScale(self, newLogScale=True):
		self.logScale = newLogScale;

	def setPanel(self, newPanelName):
		self.panel = newPanelName;

	def getDetectorInfo(self):
		return self.detectorInfo;

	def singleFastShot(self, newExpos=None):
		if newExpos is not None: # New exposure time given
			self.exposureTime = newExpos;
			self.setCollectionTime(newExpos);
		return self.detector.shotSingleImage();


	def singleShot(self, newExpos=None):
		"""Shot single image with given exposure time""";

		#To put camera in the "camera mode"
		self.detector.setCameraInProgress(False);

		if self.getStatus() != Detector.IDLE:
			print 'Camera not available, please try later';
			return;

		if newExpos is not None: # New exposure time given
			self.exposureTime = newExpos;

		self.setCollectionTime(self.exposureTime);

		self.detector.setCameraSequentialMode(True);
		#self.setNumOfImages(1);
		self.collectData();

		sleep(self.exposureTime);
		while self.getStatus() != Detector.IDLE:
			sleep(self.exposureTime/10.0);

		if self.protection:
			self.protectCamera();

		return self.readout();

	def multiShot(self, numberOfImages, newExpos=None, newDir=True):
		"""Shot multiple images with given exposure time""";

		#To put camera in the "camera mode"
		self.detector.setCameraInProgress(False);

		if self.getStatus() != Detector.IDLE:
			print 'Camera not available, please try later';
			return;

		if newExpos is not None: # New exposure time given
			self.exposureTime = newExpos;

		self.setCollectionTime(self.exposureTime);

		if newDir:
			self.newImageDirectory();
			self.scanImageDirectory();
		else:
			self.detector.setCameraSequentialMode(True);


		temp=self.alive;
		self.alive=False;#To turn off the display

		if self.fastMode:
			fn=self.multiFastShot(numberOfImages);
		else:
			fn=self.multiSafeShot(numberOfImages);

		self.alive=temp; #To restoru the display setting

		if self.protection:
			self.protectCamera();

		return fn;

	def multiSafeShot(self, numberOfImages):
		fn=[];
		#self.setNumOfImages(numberOfImages);
		for n in range(numberOfImages):
			self.collectData();
			sleep(self.exposureTime);
			while self.getStatus() != Detector.IDLE:
				sleep(self.exposureTime/10.0);
			fn.append( self.readout() );

		return fn;

	def multiFastShot(self, numberOfImages):
		fn=[];
		#To take the first image
		self.collectData()
		sleep(self.exposureTime + 0.15);
		i=0;
		while i<numberOfImages-1:
			self.collectData();#To trigger a new acquisition
			t0=time.time();
			fn.append( self.readout() );#To save the previous picture
			t1=time.time();
			td=t1-t0;
			if td < self.exposureTime+0.15:#To wait the exposure time, plus a 150ms delay?
				sleep(self.exposureTime+0.15-td);
			i+=1;

		fn.append( self.readout() );#To save the last previous triggered picture
		return fn;

	def display(self,file=None):
		if file==None:
			file = self.getFullFileName()

		fileLoader = UViewDetectorClass.ImageFileLoaders[os.path.splitext(file)[-1].split('.')[-1].upper()];

		self.data.load( fileLoader(file) );

		dataset = self.data.getAxis(0);

		if self.panel:
			if self.logScale:
				RCPPlotter.imagePlot(self.panel, DatasetUtils.lognorm(dataset)); #For RCP GUI
			else:
				RCPPlotter.imagePlot(self.panel, dataset); #For RCP GUI
		else:
			print "No panel set to display"
			raise Exception("No panel_name set in %s. Set this or set %s.setAlive(False)" % (self.name,self.name));


	def setNumOfImages(self, number):
#		self.detector.setNumOfImages(number);
		return;

	def getNumOfImages(self):
		return 1
#		return self.detector.getNumOfImages();

#	To use the directory with scan number to save image, like what scan command does.
	def scanImageDirectory(self):
		#increase the scan number for one
		self.scanNumberTracker.incrementNumber();
		self.prepareForCollection();

#	To create a new directory following the PEEM image directory rules
	def newImageDirectory(self, newDir=None):
		if newDir is not None:
			self.detector.setImageDir(newDir);
		self.detector.prepare();

	def getFullFileName(self):
		"""Returns file path of the LAST CREATED image"""
		return self.fileName;
Example #9
0
class AnalyserDetectorClass(DetectorBase):

	def __init__(self, name, detector, processors=[], panelName="Area Detector", iFileLoader=None):
		self.detector = detector;
		self.processors = processors;
		self.panel = panelName;
		self.iFileLoader = iFileLoader;
		
		self.filename= None;
		self.data = ScanFileHolder();
		self.dataset = None;
		self.mask = None;
		self.result = None;

		self.alive = False;
		
		self.scannableSetup(name);

	def scannableSetup(self, name):
		self.setName(name);
		self.setInputNames([]);

		extraNames = ['ExposureTime', 'file'];
#		extraNames = ['file'];
#		extraNames = [];
		outputFormat = ['%f','%s'];
		if self.processors != None:
			for processor in self.processors:
				for label in processor.labelList:
					extraNames.append(self.name + '_' + label)
					outputFormat.append( '%f' );

#		self.extraNames = extraNames;
#		self.outputFormat = outputFormat;
		self.setExtraNames(extraNames);
		self.setOutputFormat(outputFormat);
		
		self.level = self.detector.getLevel() + 1;

	def setAlive(self, newAlive=True):
		self.alive = newAlive;
			
	def loadDataFromFile(self, fileName, fileLoader=None):
		if fileLoader is None:
			fileLoader = ImageFileLoaders[os.path.splitext(fileName)[-1].split('.')[-1].upper()];
			
		try:
			self.data.load(fileLoader(fileName));
		except IllegalArgumentException:
			raise IOError("Oh Dear. How to load this image file?");

		return self.data;


	def loadDataset(self, fileName = None, fileLoader = None):
		self.dataset = None;
		
		if fileLoader == None:
			fileLoader = self.iFileLoader;
		
		if fileName != None:#Get data from file directly
			self.data = self.loadDataFromFile(fileName, fileLoader);
			if self.data is not None:
				self.filename = fileName;
#				self.dataset = self.data[0];
				self.dataset = self.data.getAxis(0);
		else: #Get data from detector
			if self.detector.createsOwnFiles():
				fn=self.detector.readout();
				self.data = self.loadDataFromFile(fn, fileLoader);
				if self.data is not None:
					self.filename = fn;
#					self.dataset = self.data[0];
					self.dataset = self.data.getAxis(0);
			else:
				self.filename = None;
				ds = self.detector.readout();
				if isinstance(ds, DataSet):
					self.dataset = ds;
				else:
					self.dataset = None;
					raise Exception('For none file generating detector, a DataSet is needed for analysis.');

		#Apply mask if available:
		self.dataset = self.applyMask(self.mask, self.dataset);
		return self.dataset;

	def loadFile(self, fileName = None, fileLoader = None):
		return self.loadDataset(fileName, fileLoader);

	def getResult(self, processor, dataset):
		result = [];

		if dataset is None:
			print "Warning: None dataset";
			return result;
		
		twodDataSetResult = processor.process(dataset, 0, 0);
		d = twodDataSetResult.resultsDict
		for key in processor.labelList:
			result.append(d[key])
		return result
	

	def setResult(self, pos):
		self.result = {}
		for key, val in zip(list(self.getInputNames()) + list(self.getExtraNames()), pos):
			self.result[key] = val

	def getMask(self):
		return self.mask;

	def removeMask(self):
		self.mask = None;
	
	def createMask(self, low, high, dataset=None):
		if dataset is None:
			if self.dataset is None:
				print "No reference dataset to mask";
				return;
			else:
				dataset = self.dataset;
				
		maskMaker=MakeMask(low, high);
#		arrayListDataset = ArrayList(dataset.exec(maskMaker));
#		self.mask=DataSet(arrayListDataset.get(0));

		self.mask = ArrayList(dataset.exec(maskMaker)).get(0).cast(Dataset.FLOAT64);
		return self.mask;

	def applyMask(self, mask=None, dataset=None):
		if dataset is None:
			if self.dataset is None:
				print "No dataset to mask";
				return;
			else:
				dataset = self.dataset;

		if mask is None:
			if self.mask is None:
#				print "No mask provided, please create new mask";
				return dataset;
			else:
				mask = self.mask;
		
		maskedDataet = dataset * mask
#		if self.alive:
#			self.display(maskedDataet);
		return maskedDataet;

	def display(self, dataset=None):
		if dataset is None:
			if self.dataset is None:
				print "No dataset to display";
				return;
			else:
				dataset = self.dataset;
				
		if self.panel:
			RCPPlotter.imagePlot(self.panel, dataset);
		else:
			print "No panel set to display"
			raise Exception("No panel_name set in %s. Set this or set %s.setAlive(False)" % (self.name,self.name));
	

###	DetectorBase implementations:
	def createsOwnFiles(self):
		return False;

	def getCollectionTime(self):
		return self.detector.getCollectionTime();

	def setCollectionTime(self, newExpos):
		self.detector.setCollectionTime(newExpos);

	def collectData(self):
		self.dataset = None;
		self.result = None;
		self.detector.collectData();

	def readout(self):
		self.loadDataset();

		result = [self.detector.getCollectionTime(), self.filename];
		if self.processors != None:
			for processor in self.processors:
				result += list(self.getResult(processor, self.dataset));

		self.setResult(result);

		if self.alive:
			self.display();
		
		return result;

	def getStatus(self):
		return self.detector.getStatus();

	def getDescription(self):
		return "An Detector based analyser";

	def getDetectorID(self):
		return "dumbdumb-1";

	def getDetectorType(self):
		return "AnalyserDetectorClass";
Example #10
0
class DetectorAnalyserClass(ScannableMotionBase):
    def __init__(self,
                 name,
                 detector,
                 processors=[],
                 panelName="Area Detector",
                 iFileLoader=None):
        self.detector = detector
        self.processors = processors
        self.panel = panelName
        self.iFileLoader = iFileLoader

        self.filename = None
        self.data = ScanFileHolder()
        self.dataset = None
        self.mask = None
        self.result = None

        self.passive = False
        self.alive = False

        self.scannableSetup(name)

    def scannableSetup(self, name):
        self.setName(name)
        self.inputNames = [self.detector.getName() + 'Wrapper_' + name]

        extraNames = ['file']
        outputFormat = ['%f', '%s']
        if self.processors != None:
            for processor in self.processors:
                for label in processor.labelList:
                    extraNames.append(self.name + '_' + label)
                    outputFormat.append('%f')

        self.extraNames = extraNames
        self.outputFormat = outputFormat

        self.level = self.detector.getLevel() + 1

    def setPassive(self, newPassive=True):
        """
		Set the detector analyser to be Passive or Active
		Device Passive means this analyser will not drive its detector but only works with the latest detector data.
		Device NoPassive/Active means this analyser will drive its detector to get new data and then works with the new acquisition.
		"""
        self.passive = newPassive

    def setAlive(self, newAlive=True):
        self.alive = newAlive

    def loadDataFromFile(self, fileName, fileLoader):
        if fileLoader is None:
            fileLoader = GDA_FILELOADERS[os.path.splitext(fileName)
                                         [-1].upper()]

    #	print "loadIntoSfh loading: %s using %s" % (fileName, str(iFileLoader))
        print fileName
        self.data.load(fileLoader(fileName))

        return self.data

    def loadDataset(self, fileName=None, fileLoader=None):
        self.dataset = None

        if fileLoader == None:
            fileLoader = self.iFileLoader

        if fileName != None:  #Get data from file directly
            self.data = self.loadDataFromFile(fileName, fileLoader)
            if self.data is not None:
                self.filename = fileName
                #				self.dataset = self.data[0];
                self.dataset = self.data.getAxis(0)
        else:  #Get data from detector
            if self.detector.createsOwnFiles():
                fn = self.detector.readout()
                self.data = self.loadDataFromFile(fn, fileLoader)
                if self.data is not None:
                    self.filename = fn
                    #					self.dataset = self.data[0];
                    self.dataset = self.data.getAxis(0)
            else:
                self.filename = None
                ds = self.detector.readout()
                if isinstance(ds, DoubleDataset):
                    self.dataset = ds
                else:
                    self.dataset = None
                    raise Exception(
                        'For none file generating detector, a DataSet is needed for analysis.'
                    )

        #Apply mask if available:
        self.dataset = self.applyMask(self.mask, self.dataset)
        return self.dataset

    def loadFile(self, fileName=None, fileLoader=None):
        return self.loadDataset(fileName, fileLoader)

    def getResult(self, processor, dataset):
        result = []

        if dataset is None:
            print "Warning: None dataset"
            return result

        twodDataSetResult = processor.process(dataset, 0, 0)
        d = twodDataSetResult.resultsDict
        for key in processor.labelList:
            result.append(d[key])
        return result

    def setResult(self, pos):
        self.result = {}
        for key, val in zip(
                list(self.getInputNames()) + list(self.getExtraNames()), pos):
            self.result[key] = val

    def getMask(self):
        return self.mask

    def removeMask(self):
        self.mask = None

    def createMask(self, low, high, dataset=None):
        if dataset is None:
            if self.dataset is None:
                print "No reference dataset to mask"
                return
            else:
                dataset = self.dataset

        maskMaker = MakeMask(low, high)
        #		arrayListDataset = ArrayList(dataset.exec(maskMaker));
        #		self.mask=DataSet(arrayListDataset.get(0));

        self.mask = maskMaker.value(dataset).get(0).cast(Dataset.FLOAT64)
        return self.mask

    def applyMask(self, mask=None, dataset=None):
        if dataset is None:
            if self.dataset is None:
                print "No dataset to mask"
                return
            else:
                dataset = self.dataset

        if mask is None:
            if self.mask is None:
                print "No mask provided, please create new mask"
                return dataset
            else:
                mask = self.mask

        maskedDataet = dataset * mask
        #		if self.alive:
        #			self.display(maskedDataet);
        return maskedDataet

    def display(self, dataset=None):
        if dataset is None:
            if self.dataset is None:
                print "No dataset to display"
                return
            else:
                dataset = self.dataset

        if self.panel:
            RCPPlotter.imagePlot(self.panel, dataset)
        else:
            print "No panel set to display"
            raise Exception(
                "No panel_name set in %s. Set this or set %s.setAlive(False)" %
                (self.name, self.name))

    def rescan(self, scanNumber):
        """
		To re-do the data analysis based on the data set generated in the previous scan
		Input: scanNumber: the old scan that created a list of data files
		Output: a new SRS data file that re do the data analysis 
		"""
        self.data.loadSRS(scanNumber)
        self.data.info()

        #find the file name
        #load dataset from the file
        #do the analysis and get result
        #save into a new srs file

    def __getitem__(self, key):
        if self.result == None:
            self.getPosition()
        return self.result[key]

###	Psduco Device Interface method implementations:

    def asynchronousMoveTo(self, exposureTime):
        if self.passive:
            print "The Detector Analyser " + self.name + " only works with existing detector data, no new data will be collected."
            return

        self.dataset = None
        self.result = None
        self.detector.asynchronousMoveTo(exposureTime)
#		self.detector.setCollectionTime(t);
#		self.detector.collectData();

    def isBusy(self):
        if self.passive:
            return False
        else:
            return self.detector.getStatus() == Detector.BUSY

    def getPosition(self):
        self.loadDataset()

        result = [self.detector.getCollectionTime(), self.filename]
        if self.processors != None:
            for processor in self.processors:
                result += list(self.getResult(processor, self.dataset))

        self.setResult(result)

        if self.alive:
            self.display()

        return result
Example #11
0
    def ncdredconf(self, detector, **kwords):
        """
            if default None passed in no change is made
            default is everything off
        """
        
        if detector.lower() == "Waxs".lower():
            detector="Waxs"
        else:
            detector="Saxs"
            
        if not detector in self.settings:
            self.settings[detector] = {}
        for i in self.options:
            if not i in self.settings[detector]:
                self.settings[detector][i] = False
            if i in kwords:
                if kwords[i] == None:
                    self.settings[detector][i] = False
                else:
                    self.settings[detector][i] = kwords[i]
        
        realdet=ncdutils.getDetectorByType(self.detsystem, detector.upper())
        realdetname = realdet.getName()
        ncdutils.removeDetectorByName(self.detsystem, detector+" chain")
        
        saxsrc=ReductionChain(detector+" chain", realdetname)
        saxsrclist=saxsrc.getChain()
        
        panel=detector+" Plot"
        beanbag=SDAPlotter.getGuiBean(panel)
        if beanbag == None:
            beanbag = {}
        
        sroi = None
        if self.is2D(realdet):
            if GuiParameters.ROIDATA in beanbag:
                roi = beanbag[GuiParameters.ROIDATA]
                if isinstance(roi, SectorROI):
                    sroi = roi
                    radii = sroi.getRadii()
                    self.settings[detector]["length"]=int(math.ceil(radii[1]-radii[0]))
                    self.settings[detector]["disttobeamstop"]=radii[0]

        length =  self.settings[detector]["length"]
        slope =  self.settings[detector]["slope"]
        intercept =  self.settings[detector]["intercept"]
        if slope == 0 or slope == False:
            axisds=None
        else:
            axis=[]
            pis = realdet.getPixelSize()*1000
            d2b = self.settings[detector]["disttobeamstop"]
            for i in range(length):
                axis.append(float((i+d2b)*pis*slope+intercept))
            axisds=DataSet("qaxis", axis)
            
        mask =  self.settings[detector]["mask"]
        sect =  self.settings[detector]["sect"]
        if sroi != None:
            if sect or isinstance(axisds, DataSet):
                    start = sroi.getPoint()
                    realdet.setAttribute("beam_center_x", start[0])
                    realdet.setAttribute("beam_center_y", start[1])
            else:
                    realdet.setAttribute("beam_center_x", None)
                    realdet.setAttribute("beam_center_y", None)
                    
        cameralength = self.settings[detector]["cameralength"]
        if cameralength != False:
            realdet.setAttribute("distance", cameralength)
        else:
            realdet.setAttribute("distance", None)
            
        norm = self.settings[detector]["norm"]
        if (norm != False):
            saxsnorm=Normalisation(detector+"norm","ignored")
            saxsnorm.setCalibChannel(1)
            saxsnorm.setCalibName(ncdutils.getDetectorByType(self.detsystem, "CALIB").getName())
            saxsrclist.add(saxsnorm)
            
        bg =  self.settings[detector]["bg"]
        if bg != False: 
            if os.path.isfile(bg):
                saxsbgs=BackgroundSubtraction(detector+"bg","ignored")
                sfh=ScanFileHolder()
                if (norm>=0):
                    upstream=detector+"norm.data"
                else:
                    upstream=realdetname+".data"
                sfh.load(NexusLoader(bg,[upstream]))
                ds=sfh.getAxis(upstream)
                saxsbgs.setBackground(ds)
                saxsrclist.add(saxsbgs)
            else:
                print "background file \"%s\" does not exist." % bg
        

        if (sect):
            if sroi != None:
                    saxssect=SectorIntegration(detector+"sect","ignored")
                    saxssect.setROI(sroi)
                    if isinstance(axisds, DataSet):
                        saxssect.setqAxis(axisds)
                    if (mask):
                        if not GuiParameters.MASKING in beanbag:
                            print "no mask defined for %s" % panel
                        else:
                            mb=beanbag[GuiParameters.MASKING]
                            if not isinstance(mb, MaskingBean):
                                print "no mask defined for %s" % panel
                            else:
                                saxssect.setMask(mb.getMask())
                    saxsrclist.add(saxssect)
            else:
                print "sector integration requested but no ROI found"
                
        inv =  self.settings[detector]["inv"]
        if (inv):
            saxsinvariant=Invariant(detector+"invariant", "ignored")
            saxsrclist.add(saxsinvariant)
            
        aver =  self.settings[detector]["aver"]
        if (aver):
            saxsaver=Average(detector+"average", "ignored")
            saxsrclist.add(saxsaver)
        self.detsystem.addDetector(saxsrc)
Example #12
0
class DummyAreaDetectorClass(DetectorBase):
    def __init__(self, name, panelName, zippedImageSource, fileImageExtension):
        self.setName(name)
        #		self.setInputNames([]);
        self.setLevel(7)

        self.panel = panelName
        self.data = ScanFileHolder()
        self.triggerTime = 0

        self.filePath = None
        self.filePrefix = None
        self.logScale = False
        self.alive = True

        #		self.imageProducer=ZipImageProducerClass('/scratch/Dev/gdaDev/gda-config/i07/scripts/Diamond/Pilatus/images100K.zip', 'tif');
        self.imageProducer = ZipImageProducerClass(zippedImageSource,
                                                   fileImageExtension)
        self.fullImageFileName = None
# DetectorBase Implementation

    def collectData(self):
        self.triggerTime = time.time()
        self.fullImageFileName = self.imageProducer.getNextImage()
        return

    def readout(self):
        if self.alive:
            self.display()
        return self.fullImageFileName

    def getStatus(self):
        currenttime = time.time()

        if currenttime < self.triggerTime + self.getCollectionTime():
            return Detector.BUSY
        else:
            return Detector.IDLE

    def createsOwnFiles(self):
        return True

    def toString(self):
        self.getPosition()
        return "Latest image file: " + self.getFullFileName()

## Extra Implementation

    def setAlive(self, newAlive=True):
        self.alive = newAlive

    def setLogScale(self, newLogScale=True):
        self.logScale = newLogScale

    def setPanel(self, newPanelName):
        self.panel = newPanelName

    def singleShot(self, newExpos):
        """Shot single image with given exposure time"""
        if self.getStatus() != Detector.IDLE:
            print 'Area Detector not available, please try later'
            return

        self.setCollectionTime(newExpos)
        self.collectData()
        while self.getStatus() != Detector.IDLE:
            time.sleep(self.getCollectionTime() / 2.0)
        return self.readout()

    def multiShot(self, numberOfImages, newExpos=None):
        """Shot multiple images with given exposure time"""
        if self.getStatus() != Detector.IDLE:
            print 'Camera not available, please try later'
            return

        if newExpos is not None:  # New exposure time given
            exposureTime = newExpos
            self.setCollectionTime(exposureTime)

        exposureTime = self.getCollectionTime()
        fn = []
        #self.setNumOfImages(numberOfImages);
        for n in range(numberOfImages):
            self.collectData()
            time.sleep(exposureTime)
            while self.getStatus() != Detector.IDLE:
                time.sleep(exposureTime / 10.0)
            fn.append(self.readout())

        return fn

    def display(self, file=None):
        if file == None:
            file = self.getFullFileName()
#		self.data.loadPilatusData(file)
        self.data.load(PilatusTiffLoader(file))
        dataset = self.data.getAxis(0)

        if self.panel:
            if self.logScale:
                SDAPlotter.imagePlot(self.panel, DatasetUtils.lognorm(dataset))
                #For RCP GUI
            else:
                SDAPlotter.imagePlot(self.panel, dataset)
                #For RCP GUI
        else:
            print "No panel set to display"
            raise Exception(
                "No panel_name set in %s. Set this or set %s.setAlive(False)" %
                (self.name, self.name))

## Area Detector Implementation

    def setFile(self, subDir, newFilePrefix):
        """Set file path and name"""
        #		imagePath = PathConstructor.createFromProperty("gda.data.scan.datawriter.datadir");
        imagePath = PathConstructor.createFromDefaultProperty(
        ) + File.separator

        fullPath = os.path.join(imagePath, subDir)
        print "Note: Current Pilatus image path: " + fullPath

        self.imageProducer.setFilePath(fullPath)
        self.imageProducer.setFilePrefix(newFilePrefix)

#a.setFilePath("/scratch/temp")
#a.setFilePrefix("p100k")

    def getFilePath(self):
        return self.imageProducer.getFilePath()

    def getFilePrefix(self):
        return self.imageProducer.getFilePrefix()

    def getFileNumber(self):
        """Get filenumber"""
        self.fileNumber = self.imageProducer.getFileNumber()
        return self.fileNumber

    def getFullFileName(self):
        return self.fullImageFileName
Example #13
0
class PilatusPseudoDeviceClass(DetectorBase, ShutterDeviceClass):
    DETECTOR_STATUS_IDLE, DETECTOR_STATUS_BUSY, DETECTOR_STATUS_PAUSED, DETECTOR_STATUS_STANDBY, DETECTOR_STATUS_FAULT, DETECTOR_STATUS_MONITORING = range(
        6)

    def __init__(self, name, panelName, detectorName, shutterName=None):
        self.setName(name)
        self.setInputNames(['exposure'])
        self.setExtraNames([])
        #		self.setOutputFormat(['%.2f']);
        self.setLevel(7)

        self.panel = panelName
        self.detector = vars(gdamain)[detectorName]

        self.detectorInfo = None
        self.data = ScanFileHolder()
        self.exposureTime = 0
        self.sum = 0
        self.maxpix = 0

        #		self.filePath = None;
        #		self.filePrefix = None;
        #		self.fileFormat = "%s%s%4.4d.tif";
        #		self.fileNumber = self.detector.getFileNumber();
        self.logScale = False
        self.alive = True

        ShutterDeviceClass.__init__(self, shutterName)

# DetectorBase Implementation

    def getPosition(self):
        return self.readout()

#	def asynchronousMoveTo(self,newExpos):
#		self.setCollectionTime(newExpos)
#		self.collectData();

    def getCollectionTime(self):
        self.exposureTime = self.detector.getCollectionTime()
        return self.exposureTime

    def setCollectionTime(self, newExpos):
        self.exposureTime = newExpos
        self.detector.setCollectionTime(self.exposureTime)

    def collectData(self):
        self.openShutter()
        self.detector.collectData()
        return

    def readout(self):
        self.closeShutter()
        self.fileNumber = self.detector.readout()
        fileName = self.getFullFileName()

        if not self.fileExists(fileName, 100):
            logger.simpleLog("PilatusDetectorPseudoDevice.readout(): File '" +
                             fileName + "' does not exist!")
            raise Exception("No File Found Error")

        if self.alive:
            self.display()

        return fileName

    def getStatus(self):
        return self.detector.getStatus()

    def createsOwnFiles(self):
        return True

    def toString(self):
        self.getPosition()
        return "Latest image file: " + self.getFullFileName()

    def stop(self):
        self.closeShutter()

## Extra Implementation

    def setAlive(self, newAlive=True):
        self.alive = newAlive

    def setLogScale(self, newLogScale=True):
        self.logScale = newLogScale

    def setPanel(self, newPanelName):
        self.panel = newPanelName

    def getDetectorInfo(self):
        return self.detectorInfo

    def singleShot(self, newExpos):
        """Shot single image with given exposure time"""
        if self.getStatus() != Detector.IDLE:
            print 'Pilatus not available, please try later'
            return

        self.openShutter()
        self.setCollectionTime(newExpos)
        self.setNumOfImages(1)
        self.collectData()
        while self.getStatus() != Detector.IDLE:
            sleep(self.exposureTime / 2.0)
        return self.readout()

    def multiShot(self, newExposureTime, newExposurePeirod, newNumberOfImages):
        """Shot single image with given exposure time"""
        if self.getStatus() != Detector.IDLE:
            print 'Pilatus not available, please try later'
            return

        self.openShutter()
        self.setCollectionTime(newExposureTime)
        self.setExposurePeriod(newExposurePeirod)
        self.setNumOfImages(newNumberOfImages)

        self.collectData()

        while self.getStatus() != Detector.IDLE:
            sleep(self.exposureTime * newNumberOfImages / 5.0)
        self.closeShutter()
        print 'Done, file number is: ' + str(self.getFileNumber())
        return self.detector.getMultipleFullFileNames()

    def display(self, file=None):
        if file == None:
            file = self.getFullFileName()
#		self.data.loadPilatusData(file)
        self.data.load(PilatusTiffLoader(file))
        dataset = self.data.getAxis(0)

        if self.panel:
            if self.logScale:
                RCPPlotter.imagePlot(self.panel, DatasetMaths.lognorm(dataset))
                #For RCP GUI
            else:
                RCPPlotter.imagePlot(self.panel, dataset)
                #For RCP GUI
        else:
            print "No panel set to display"
            raise Exception(
                "No panel_name set in %s. Set this or set %s.setAlive(False)" %
                (self.name, self.name))

    def triggerOn(self, interval):
        #		self.detector.start();
        self.detector.trigger()
        self.triggerFlag = True

        while self.triggerFlag:
            sleep(interval)
            self.readout()

    def triggerOff(self):
        self.triggerFlag = False
        self.detector.stop()

## Pilatus Implementation

    def setFile(self, subDir, newFilePrefix):
        """Set file path and name"""
        pilatusDataDir = InterfaceProvider.getPathConstructor(
        ).createFromDefaultProperty()
        #		pilatusDataDir = InterfaceProvider.getPathConstructor().createFromProperty("gda.pilatus.datadir");

        fullPath = os.path.join(pilatusDataDir, subDir)

        if not os.path.exists(fullPath):
            print "Warning: Pilatus image path does not exist. Create new"
            print "OS Command: " + "umask 0002; mkdir -p " + fullPath
            os.system("umask 0002; mkdir -p " + fullPath)

#		Check the new path created.
        if not os.path.exists(fullPath):
            print "Error: Pilatus image path does not exist and can not be created"
            return

        print "Note: Current Pilatus image path: " + fullPath
        self.detector.setFilePath(fullPath)
        self.detector.setFilePrefix(newFilePrefix)

    def getFilePath(self):
        return self.detector.getFilePath()

    def getFilePrefix(self):
        """Get filename - not the path"""
        return self.detector.getFilePrefix()

    def getFileNumber(self):
        """Get filenumber"""
        self.fileNumber = self.detector.getFileNumber()
        return self.fileNumber

    def getFullFileName(self):
        """Returns file path of the LAST CREATED image"""
        return self.detector.getFullFileName()

    def setThreshold(self, newGain, newThresholdEnergy):
        self.detector.setThreshold(newGain, newThresholdEnergy)
        return

    def getThreshold(self):
        return self.detector.getThreshold()

    def setExposurePeriod(self, newExpp):
        self.detector.setExposurePeriod(newExpp)
        return

    def getExposurePeriod(self):
        return self.detector.getExposurePeriod()

    def setNumOfImages(self, number):
        self.detector.setNumOfImages(number)

    def getNumOfImages(self):
        return self.detector.getNumOfImages()

#Additional DLS Luster fix to deal with delay

    def fileExists(self, fileName, numberOfTry):
        result = False

        for i in range(numberOfTry):
            if (not os.path.exists(fileName)) or (
                    not os.path.isfile(fileName)):
                if i > 1:  #To make a note if tried more than twice
                    print "File does not exist on try " + str(i + 1)
                    logger.simpleLog(
                        "DetectorAnalyser: File does not exist on try " +
                        str(i + 1))
                #check again:
                sleep(1)
                os.system("ls -la " + fileName)
                #To try to update the file system
            else:
                #To touch the file will change the timestamps on the directory, which in turn causes the client to refetch the file's attributes.
                os.system("touch -c " + fileName)

                #To check the file size non zero
                fsize = os.stat(fileName)[stat.ST_SIZE]
                if i > 1:  #To make a note if tried more than twice
                    print "File exists on try " + str(
                        i + 1) + " with size " + str(fsize)
                    logger.simpleLog("DetectorAnalyser: File '" + fileName +
                                     "' exists on try " + str(i + 1) +
                                     " with size " + str(fsize))

                sizeP100K = 383956L
                sizeP2M = 9910196L
                if fsize != sizeP100K and fsize != sizeP2M:
                    print "Wrong file size: " + str(fsize)
                    logger.simpleLog("DetectorAnalyser: File '" + fileName +
                                     "' has wrong file size: " + str(fsize))
                    sleep(1)
                    continue
                #The file seems exist with some content inside. Double check to make sure it's available
                try:
                    #To touch the file will change the timestamps on the directory, which in turn causes the client to refetch the file's attributes.
                    os.system("touch -c " + fileName)
                    tf = open(fileName, "rb")
                    tf.close()
                    result = True
                    break
                except:
                    print "There are something wrong on the file system !!!"
                    logger.simpleLog(
                        "DetectorAnalyser: There are something wrong on the file system !!!"
                    )
        return result
Example #14
0
class DummyAreaDetectorClass(DetectorBase):
	def __init__(self, name, panelName, zippedImageSource, fileImageExtension):
		self.setName(name);
#		self.setInputNames([]);
		self.setLevel(7);
		
		self.panel = panelName;
		self.data = ScanFileHolder();
		self.triggerTime = 0;

		self.filePath = None;
		self.filePrefix = None;
		self.logScale = False;
		self.alive=True;

#		self.imageProducer=ZipImageProducerClass('/scratch/Dev/gdaDev/gda-config/i07/scripts/Diamond/Pilatus/images100K.zip', 'tif');
		self.imageProducer=ZipImageProducerClass(zippedImageSource, fileImageExtension);
		self.fullImageFileName = None;
# DetectorBase Implementation

	def collectData(self):
		self.triggerTime = time.time();
		self.fullImageFileName = self.imageProducer.getNextImage();
		return;


	def readout(self):
		if self.alive:
			self.display();
		return self.fullImageFileName;

	def getStatus(self):
		currenttime = time.time();
		
		if currenttime<self.triggerTime + self.getCollectionTime():
			return Detector.BUSY
		else:
			return Detector.IDLE;
	
	def createsOwnFiles(self):
		return True;
	
	def toString(self):
		self.getPosition();
		return "Latest image file: " + self.getFullFileName();

## Extra Implementation
	def setAlive(self, newAlive=True):
		self.alive = newAlive;
		
	def setLogScale(self, newLogScale=True):
		self.logScale = newLogScale;

	def setPanel(self, newPanelName):
		self.panel = newPanelName;

	def singleShot(self, newExpos):
		"""Shot single image with given exposure time""";
		if self.getStatus() != Detector.IDLE:
			print 'Area Detector not available, please try later';
			return;
		
		self.setCollectionTime(newExpos);
		self.collectData();
		while self.getStatus() != Detector.IDLE:
			time.sleep(self.getCollectionTime()/2.0);
		return self.readout();

	def multiShot(self, numberOfImages, newExpos=None):
		"""Shot multiple images with given exposure time""";
		if self.getStatus() != Detector.IDLE:
			print 'Camera not available, please try later';
			return;
		
		if newExpos is not None: # New exposure time given
			exposureTime=newExpos;
			self.setCollectionTime(exposureTime);
			
		exposureTime=self.getCollectionTime();
		fn=[];
		#self.setNumOfImages(numberOfImages);
		for n in range(numberOfImages):
			self.collectData();
			time.sleep(exposureTime);
			while self.getStatus() != Detector.IDLE:
				time.sleep(exposureTime/10.0);
			fn.append( self.readout() );

		return fn;


	def display(self,file=None):
		if file==None:
			file = self.getFullFileName()
#		self.data.loadPilatusData(file)
		self.data.load(PilatusTiffLoader(file));
		dataset = self.data.getAxis(0);

		if self.panel:
			if self.logScale:
				SDAPlotter.imagePlot(self.panel, DatasetUtils.lognorm(dataset)); #For RCP GUI
			else:
				SDAPlotter.imagePlot(self.panel, dataset); #For RCP GUI
		else:
			print "No panel set to display"
			raise Exception("No panel_name set in %s. Set this or set %s.setAlive(False)" % (self.name,self.name));


## Area Detector Implementation
	def setFile(self, subDir, newFilePrefix):
		"""Set file path and name"""
#		imagePath = PathConstructor.createFromProperty("gda.data.scan.datawriter.datadir");
		imagePath=PathConstructor.createFromDefaultProperty() + File.separator;
		
		fullPath = os.path.join(imagePath, subDir);
		print "Note: Current Pilatus image path: " + fullPath;
		
		self.imageProducer.setFilePath(fullPath);
		self.imageProducer.setFilePrefix(newFilePrefix);

#a.setFilePath("/scratch/temp")
#a.setFilePrefix("p100k")

	def getFilePath(self):
		return self.imageProducer.getFilePath();

	def getFilePrefix(self):
		return self.imageProducer.getFilePrefix();

	def getFileNumber(self):
		"""Get filenumber"""
		self.fileNumber = self.imageProducer.getFileNumber();
		return self.fileNumber

	def getFullFileName(self):
		return self.fullImageFileName;
Example #15
0
    def ncdredconf(self, detector, **kwords):
        """
            if default None passed in no change is made
            default is everything off
        """

        if detector.lower() == "Waxs".lower():
            detector = "Waxs"
        else:
            detector = "Saxs"

        if not detector in self.settings:
            self.settings[detector] = {}
        for i in self.options:
            if not i in self.settings[detector]:
                self.settings[detector][i] = False
            if i in kwords:
                if kwords[i] == None:
                    self.settings[detector][i] = False
                else:
                    self.settings[detector][i] = kwords[i]

        realdet = ncdutils.getDetectorByType(self.detsystem, detector.upper())
        realdetname = realdet.getName()
        ncdutils.removeDetectorByName(self.detsystem, detector + " chain")

        saxsrc = ReductionChain(detector + " chain", realdetname)
        saxsrclist = saxsrc.getChain()

        panel = detector + " Plot"
        beanbag = SDAPlotter.getGuiBean(panel)
        if beanbag == None:
            beanbag = {}

        sroi = None
        if self.is2D(realdet):
            if GuiParameters.ROIDATA in beanbag:
                roi = beanbag[GuiParameters.ROIDATA]
                if isinstance(roi, SectorROI):
                    sroi = roi
                    radii = sroi.getRadii()
                    self.settings[detector]["length"] = int(
                        math.ceil(radii[1] - radii[0]))
                    self.settings[detector]["disttobeamstop"] = radii[0]

        length = self.settings[detector]["length"]
        slope = self.settings[detector]["slope"]
        intercept = self.settings[detector]["intercept"]
        if slope == 0 or slope == False:
            axisds = None
        else:
            axis = []
            pis = realdet.getPixelSize() * 1000
            d2b = self.settings[detector]["disttobeamstop"]
            for i in range(length):
                axis.append(float((i + d2b) * pis * slope + intercept))
            axisds = DataSet("qaxis", axis)

        mask = self.settings[detector]["mask"]
        sect = self.settings[detector]["sect"]
        if sroi != None:
            if sect or isinstance(axisds, DataSet):
                start = sroi.getPoint()
                realdet.setAttribute("beam_center_x", start[0])
                realdet.setAttribute("beam_center_y", start[1])
            else:
                realdet.setAttribute("beam_center_x", None)
                realdet.setAttribute("beam_center_y", None)

        cameralength = self.settings[detector]["cameralength"]
        if cameralength != False:
            realdet.setAttribute("distance", cameralength)
        else:
            realdet.setAttribute("distance", None)

        norm = self.settings[detector]["norm"]
        if (norm != False):
            saxsnorm = Normalisation(detector + "norm", "ignored")
            saxsnorm.setCalibChannel(1)
            saxsnorm.setCalibName(
                ncdutils.getDetectorByType(self.detsystem, "CALIB").getName())
            saxsrclist.add(saxsnorm)

        bg = self.settings[detector]["bg"]
        if bg != False:
            if os.path.isfile(bg):
                saxsbgs = BackgroundSubtraction(detector + "bg", "ignored")
                sfh = ScanFileHolder()
                if (norm >= 0):
                    upstream = detector + "norm.data"
                else:
                    upstream = realdetname + ".data"
                sfh.load(NexusLoader(bg, [upstream]))
                ds = sfh.getAxis(upstream)
                saxsbgs.setBackground(ds)
                saxsrclist.add(saxsbgs)
            else:
                print "background file \"%s\" does not exist." % bg

        if (sect):
            if sroi != None:
                saxssect = SectorIntegration(detector + "sect", "ignored")
                saxssect.setROI(sroi)
                if isinstance(axisds, DataSet):
                    saxssect.setqAxis(axisds)
                if (mask):
                    if not GuiParameters.MASKING in beanbag:
                        print "no mask defined for %s" % panel
                    else:
                        mb = beanbag[GuiParameters.MASKING]
                        if not isinstance(mb, MaskingBean):
                            print "no mask defined for %s" % panel
                        else:
                            saxssect.setMask(mb.getMask())
                saxsrclist.add(saxssect)
            else:
                print "sector integration requested but no ROI found"

        inv = self.settings[detector]["inv"]
        if (inv):
            saxsinvariant = Invariant(detector + "invariant", "ignored")
            saxsrclist.add(saxsinvariant)

        aver = self.settings[detector]["aver"]
        if (aver):
            saxsaver = Average(detector + "average", "ignored")
            saxsrclist.add(saxsaver)
        self.detsystem.addDetector(saxsrc)