""" Scan class Author : Olivier Tache' Date : 02-05-2006 CEA / DSM / DRECAM / SCM / LIONS """ import os.path import Numeric import Gnuplot NoParameter='' FilenameTemp='c:\\python\\temp.txt' GnuplotSeparator='\t' class Scan: """ class Scan we want to do some actions and measures synchronously """ ListAction=[] ListMeasure=[] DataFilename=FilenameTemp results=[] GnuplotInstance='' GnuplotParameters=''# with linespoints' GnuplotParametersFile='' GnuplotCommand='' OptionPlot=False OptionSave=False def __init__(self): self.ListAction=[] self.ListMeasure=[] def addAction(self,Action,Sequence,name='?'): """ Add an action (function) to process with parameters in Sequence ActionName is a string which represent the physical vallue >>>s.addAction(moveMotor,[1,2,3,4,5],'theta') Each action function must have the same numbers of parameters (no sense if different) except a function without parameters ex : >>>s.addAction(doSomething,[]) """ self.ListAction.append([Action,Sequence,name]) def addMeasure(self,function,name,*args): """ Add a measure action Measure : function to execute and which return a result *Parameters : parameters for the function ex : >>>def x2(x,y): return x*y >>>s.addMeasure(x2,'x2',1,2) """ self.ListMeasure.append([function, args,name]) def addPlot(plot): """ link to a gnuplot instance """ GnuplotInstance=plot def __saveLastResults(self): """ save lasts results """ f=open(self.DataFilename,'a') f.write(self.__ListToStr(self.results[len(self.results)-1])+'\n') f.close def __createHeader(self): header='#' for a in self.ListAction: header+=a[2]+GnuplotSeparator for m in self.ListMeasure: header+=m[2]+GnuplotSeparator return header def __saveHeader(self): """ save header of file create the file """ if os.path.isfile(self.DataFilename): print self.DataFilename+" exist : will be overwrite..." else : print self.DataFilename+" not exit : will be create..." f=open(self.DataFilename,'w') f.write(self.__createHeader()+'\n') f.close def __repr__(self): """ get a str representation of class """ st='' for a in self.ListAction: if a[2]!='?': st+=a[2] elif callable(a[2]): st+=a[0].__name__ else: st+='?' st+=GnuplotSeparator+str(a[1])+"\n" for m in self.ListMeasure: st+=m[2]+GnuplotSeparator+str(m[1])+"\n" return st def __str__(self): return self.__repr__() def __calculateGnuplotCommand(self): self.GnuplotCommand="plot " file='\''+self.DataFilename+'\'' for m in range(len(self.ListMeasure)): columnnumber=len(self.ListAction)+m+1 self.GnuplotCommand+= file+' using 1:'+str(columnnumber) self.GnuplotCommand+=" title \'"+self.ListMeasure[m][2]+"\'" self.GnuplotCommand+=self.GnuplotParameters+ "," self.GnuplotCommand=self.GnuplotCommand[:len(self.GnuplotCommand)-1]#remove the last , def __ListToStr(self,list): chaine='' for i in list: if i!='': chaine+=str(i)+GnuplotSeparator else: chaine+='-'+GnuplotSeparator return chaine def execute(self): """ execute action """ if len(self.ListAction)<=0 : print "Error : there's no action to execute !" return False else : print "Ready to start..." #--- Option Gnuplot ---------------------------- if self.OptionPlot and self.GnuplotInstance!='': #plot in gnuplot # OptionSave must be true if not self.OptionSave: self.OptionSave=True self.DataFilename=FilenameTemp print "Data will be save in "+self.DataFilename #calculate Gnuplot Command self.__calculateGnuplotCommand() #send command self.GnuplotInstance('load \''+self.GnuplotParametersFile+'\'') self.GnuplotInstance(self.GnuplotCommand) #--- Option save ------------------------------- if self.OptionSave: self.__saveHeader() #--- Starting ------------------------------- print self.__createHeader() NbIterations=len(self.ListAction[0][1])#how many iterations ? for i in range(NbIterations): #-------- Actions ! datas=[] for a in self.ListAction: NbParameters=len(a[1]) #param='' #valuetowrite=NoParameter if NbParameters>i: #execute with given parameters if callable(a[0]) :#is callable ? ex Move(10.0) a[0](a[1][i]) else: #not callable ex position=10.0 a[0]=a[1][i] datas.append(a[1][i]) elif NbParameters>0: #take the last parameter a[0](a[1][NbParameters-1]) datas.append(a[1][NbParameters-1]) elif NbParameters==0: #no parameter a[0]() datas.append(NoParameter) #Measurements ! for m in self.ListMeasure: datas.append(m[0](*m[1])) print self.__ListToStr(datas) self.results.append(datas) #--- Option save --------------------- if self.OptionSave: self.__saveLastResults() #--- Option Gnuplot ------------------ if self.OptionPlot and self.GnuplotInstance!='': self.GnuplotInstance(self.GnuplotCommand) print "Finished" def simpleScan(action,sequence,actionName,measure,measureName,*args): """ perform a simple scan : one action (action, sequence and actionName) one Measure(measure,arg and measureName) return : a gnuplot instance """ g=Gnuplot.Gnuplot() s=Scan() s.GnuplotInstance=g #s.GnuplotParametersFile='c:\python\default.txt' s.OptionPlot=True s.addAction(action,sequence,actionName) s.addMeasure(measure,measureName,*args) s.execute() return g if __name__=='__main__': import time import random def r(x,y): return x*5/2*6.335*y*random.random() def x2(x): return x*x def x3(x): return x*x*x def waitstop(): 2*2 return #example """g=Gnuplot.Gnuplot() s=Scan() s.GnuplotInstance=g s.GnuplotParametersFile='c:\python\default.txt' s.OptionPlot=True s.addAction(x2,Numeric.arange(0,3,0.1),'Nx2') s.addAction(x2,[1,2,3,4,5,6,7,8,9],'x2') s.addAction(x3,[1,2,3,4,5,6,7,8,9]) s.addAction(time.sleep,[0.2]) #function with 1 parameter s.addMeasure(r,'r',1,2) s.addMeasure(random.random,'random') s.OptionSave=True s.execute() """ simpleScan(x2,Numeric.arange(0,3,0.1),'Nx2',r,'r',1,2)