import sys import zmq import numpy as np import time import pickle import alignment import matplotlib.pyplot as plt import threading import datetime import copy def histVec(v,oversample=1): v = np.atleast_1d(v) v = np.unique(v) vd = np.diff(v) vd = np.hstack([vd[0],vd]) #vv = np.hstack([v-vd/2,v[-1]+vd[-1]/2]) vv = np.hstack([v-vd/2.,v[-1]+vd[-1]/2.]) if oversample>1: vvo = [] for i in range(len(vv)-1): vvo.append(np.linspace(vv[i],vv[i+1],oversample+1)[:-1]) vvo.append(vv[-1]) vv = np.array(np.hstack(vvo)) return vv def subtractBkg(imgs,nPix=100,dKtype='corners'): """ Opals tend to have different backgroud for every quadrant """ if dKtype is 'corners': if imgs.ndim == 2: imgs = imgs[np.newaxis,:] imgs = imgs.astype(np.float) q1 = imgs[:,:nPix,:nPix].mean(-1).mean(-1) imgs[:,:512,:512]-=q1[:,np.newaxis,np.newaxis] q2 = imgs[:,:nPix,-nPix:].mean(-1).mean(-1) imgs[:,:512,-512:]-=q2[:,np.newaxis,np.newaxis] q3 = imgs[:,-nPix:,-nPix:].mean(-1).mean(-1) imgs[:,-512:,-512:]-=q3[:,np.newaxis,np.newaxis] q4 = imgs[:,-nPix:,:nPix].mean(-1).mean(-1) imgs[:,-512:,:512]-=q4[:,np.newaxis,np.newaxis] elif dKtype is 'stripes': if imgs.ndim == 2: imgs = imgs[np.newaxis,:] imgs = imgs.astype(np.float) s1 = imgs[:,:nPix,:].mean(-2) return np.squeeze(imgs) def getData(): t0 = time.time() context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect('tcp://daq-xpp-mon06:12322') #socket.setsockopt(zmq.SUBSCRIBE, b'') while True: socket.send(b"Request") ret = socket.recv() ret = pickle.loads(ret, encoding='latin1') print('received',ret.keys(),time.time()-t0) t0 = time.time() if __name__ == "__main__": getData() class SpecHandler(object): def __init__(self,connectString='tcp://daq-xpp-mon06:12322',spec1name='opal0',spec2name='opal1',roi=[0,1024,0,1024]): self.connectString = connectString self.spec1name = spec1name self.spec2name = spec2name self.surveyplot = Surveyplot(spec1name=spec1name,spec2name=spec2name) self.roi = roi self.projsimple = ProjSimple(spec1name=spec1name,spec2name=spec2name) self._rawContinuousTime = None self.lastDat = None self.openSocket() self.dataCollector = [] self.runningPlot = RunningPlot(self.dataCollector) def openSocket(self): context = zmq.Context() self.context = context self.socket = context.socket(zmq.REQ) self.socket.connect(self.connectString) #self.socket.setsockopt(zmq.SUBSCRIBE, b'') def closeSocket(self): del self.context del self.socket def getData(self): self.socket.send(b"Request") ret = self.socket.recv() ret = pickle.loads(ret, encoding='latin1') for sn in [self.spec1name,self.spec2name]: ret[sn] = np.squeeze(alignment.subtractBkg(ret[sn], nPix=100, bkg_type='line')) self.lastDat = ret return ret def getRaw(self,repoenConnection=False,doAlign=False,show=False,doFit=False): if doFit is True: doFit='iminuit' if repoenConnection: self.closeSocket() self.openSocket() dat = self.getData() im1 = dat[self.spec1name]; im2 = dat[self.spec2name] if doAlign: #t = np.load("gui_align_transform_xppl3716.npy").item() if hasattr(self,'transformer'): algn = self.transformer else: algn = alignment.loadAlignment('last_trafo.npy') t = algn['transform'] roi1 = algn['roi1'] roi2 = algn['roi2'] r = alignment.doShot( im1[roi1,:],im2[roi2,:],t, show=show, doFit=doFit) self.transformer = dict(transform=r.final_transform,roi1=roi1,roi2=roi2) alignment.saveAlignment('last_trafo.npy',r.final_transform,roi1,roi2) im1 = r.im1; im2 = r.im2 showDiff = True showRatio = True else: showDiff = False showRatio = False self.surveyplot.plotImages(im1,im2,showDiff=showDiff,showRatio=showRatio) self.projsimple.plotProfiles(im1,im2) if doAlign: thres = 0.05 im1[im10: times = np.asarray(([i['time'] for i in self.dataCollector])) foms = np.asarray(([i['fom'] for i in self.dataCollector])) im1Proj = np.asarray(([i['im1Proj'] for i in self.dataCollector])) im2Proj = np.asarray(([i['im2Proj'] for i in self.dataCollector])) if hasattr(self,'fomline'): self.fomline.set_xdata(times) self.fomline.set_ydata(foms) #self.axs[0].autoscale(enable=True,axis='x') else: self.fomline = self.axs.plot(times,foms,'o-')[0] #if hasattr(self,'ratioimg'): #self.ratioimg.set_data() #self.ratioimg.set_ydata(foms) #else: #self.axs[0].plot(times,foms,'o-') #def plotOrUpdate(img1,img2): #if hasattr(self,i1): #self.i1.set_data(img1) #else: #self.i1 = self.axs.imshow(img1,interpolate='none',origin='bottom') #if hasattr(self,i1): #self.i1.set_data(img1) #else: #self.i1 = self.axs.imshow(img1,interpolate='none',origin='bottom')