1 | |
---|
2 | from Tkinter import * |
---|
3 | from ttk import * |
---|
4 | |
---|
5 | import matplotlib |
---|
6 | matplotlib.use("TkAgg") |
---|
7 | from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg |
---|
8 | from matplotlib.figure import Figure |
---|
9 | from analysis import * |
---|
10 | import matplotlib.animation as animation |
---|
11 | from matplotlib import style |
---|
12 | style.use('ggplot') |
---|
13 | import numpy as np |
---|
14 | import tkMessageBox as mbox |
---|
15 | import os |
---|
16 | |
---|
17 | path='./data/' |
---|
18 | lowlim=103000 |
---|
19 | highlim=110000 |
---|
20 | Pause=1000 |
---|
21 | dateYMD='20170524' |
---|
22 | PositionDet=4000000; |
---|
23 | detN=[4,7,10] |
---|
24 | minElecSignal=1000 |
---|
25 | canvas2=0 |
---|
26 | canvas3=0 |
---|
27 | x0=5 |
---|
28 | Energy=44 |
---|
29 | RefP=False |
---|
30 | ClearP=False |
---|
31 | dateP=0; |
---|
32 | fig0 = Figure(figsize=(5,4), dpi=100) |
---|
33 | a0 = fig0.add_subplot(111) |
---|
34 | |
---|
35 | fig1 = Figure(figsize=(5,4), dpi=100) |
---|
36 | a1 = fig1.add_subplot(111) |
---|
37 | |
---|
38 | fig3 = Figure(figsize=(5,4), dpi=100) |
---|
39 | a3 = fig3.add_subplot(111) |
---|
40 | |
---|
41 | def position_plot(i): |
---|
42 | a0.clear() |
---|
43 | position,current,tmp, Amplitude,SigErr,date,Electron_sig,Electron_sig_std=takedata(dateYMD,lowlim,highlim) |
---|
44 | |
---|
45 | #check on correct values |
---|
46 | detX=[] |
---|
47 | for c in range(0,len(detN)): |
---|
48 | if (detN[c]>0)&(detN[c]<13): |
---|
49 | detX.append(detN[c]) |
---|
50 | |
---|
51 | for detectorN in detX: |
---|
52 | positionCUT=[] |
---|
53 | AmplitudeCUT=[] |
---|
54 | AmpDet=[row[detectorN-1] for row in Amplitude]; |
---|
55 | for c in range(1,len(date)): |
---|
56 | if (date[c]>lowlim)&(date[c]<highlim)&(Electron_sig[c]>minElecSignal)&(AmpDet[c]>0): |
---|
57 | positionCUT.append(position[c]/1.e+6) |
---|
58 | AmplitudeCUT.append(AmpDet[c]/Electron_sig[c]) |
---|
59 | a0.plot(positionCUT, AmplitudeCUT,'^', label="Detector #"+str(detectorN)) |
---|
60 | a0.set_ylim(ymax=0.04, ymin=0.) |
---|
61 | a0.legend() |
---|
62 | a0.set_xlabel('Position, [Msteps]') |
---|
63 | #print 'hello2' |
---|
64 | #def spectrum_plot(k): |
---|
65 | #position,current,tmp, Amplitude,SigErr,date,Electron_sig,Electron_sig_std=takedata(dateYMD,lowlim,highlim) |
---|
66 | Angle=[48, 55, 62, 69, 76, 83, 90, 97, 104, 111, 118, 125] |
---|
67 | AmplitudeCUT=[] |
---|
68 | AmplitudeCUT2=[] |
---|
69 | positionCUT=[] |
---|
70 | dateCUT=[] |
---|
71 | dateCUT2=[] |
---|
72 | for c in range(1,len(date)): |
---|
73 | if (date[c]>lowlim)&(date[c]<highlim)&(Electron_sig[c]>minElecSignal): |
---|
74 | AmplitudeCUT.append([x/Electron_sig[c] for x in Amplitude[c]]) |
---|
75 | positionCUT.append(position[c]) |
---|
76 | dateCUT.append(date[c]) |
---|
77 | #find nearest position to requested |
---|
78 | imin=1e+6 |
---|
79 | indx=0 |
---|
80 | for c in range(0,len(positionCUT)): |
---|
81 | if abs(positionCUT[c]-np.asarray(PositionDet))<=imin: |
---|
82 | indx=c |
---|
83 | imin=abs(positionCUT[c]-np.asarray(PositionDet)) |
---|
84 | #mbox.showinfo("Information", "Spectrum for position "+str(positionCUT[indx])+"is shown of figure") |
---|
85 | for c in range(0,len(positionCUT)): |
---|
86 | if positionCUT[c]==positionCUT[indx]: |
---|
87 | AmplitudeCUT2.append(AmplitudeCUT[c]) |
---|
88 | dateCUT2.append(dateCUT[c]) |
---|
89 | Spectrum=[0,0,0,0,0,0,0,0,0,0,0,0] |
---|
90 | indx2=0 |
---|
91 | global ClearP |
---|
92 | if ClearP: |
---|
93 | a1.clear() |
---|
94 | a3.clear() |
---|
95 | canvas2.show() |
---|
96 | canvas3.show() |
---|
97 | ClearP=False |
---|
98 | if (dateCUT2): |
---|
99 | indx2=np.argmax(dateCUT2) |
---|
100 | global RefP |
---|
101 | if not RefP: |
---|
102 | a1.clear() |
---|
103 | a3.clear() |
---|
104 | global dateP |
---|
105 | if not dateP==dateCUT2[indx2]: |
---|
106 | #a1.plot(Angle, np.mean(AmplitudeCUT2,axis=0),'-^')#mean!!! |
---|
107 | a1.plot(Angle, AmplitudeCUT2[indx2],'-^') |
---|
108 | a1.set_title("Position "+str(positionCUT[indx])) |
---|
109 | a1.set_ylim(ymax=0.04, ymin=0.) |
---|
110 | a1.set_xlabel('Angle, [deg.]') |
---|
111 | dateP=dateCUT2[indx2] |
---|
112 | canvas2.show() |
---|
113 | Spectrum=AmplitudeCUT2[np.argmax(dateCUT2)] |
---|
114 | #def animate3(): |
---|
115 | #position,current,tmp, Amplitude,SigErr,date,Electron_sig,Electron_sig_std=takedata(dateYMD,lowlim,highlim) |
---|
116 | #Angle=[48, 55, 62, 69, 76, 83, 90, 97, 104, 111, 118, 125] |
---|
117 | #detectorN=7 |
---|
118 | #positionCUT=[] |
---|
119 | # AmplitudeCUT=[] |
---|
120 | #AmpDet=[row[detectorN] for row in Amplitude]; |
---|
121 | #for c in range(1,len(date)): |
---|
122 | #if (date[c]>lowlim)&(date[c]<highlim)&(Electron_sig[c]>minElecSignal): |
---|
123 | #AmplitudeCUT.append(Amplitude[c]) |
---|
124 | |
---|
125 | |
---|
126 | pullData = open('./SEY/CLIO_'+str(Energy)+'MeV_x0'+str(x0)+'mm.txt','r').read() |
---|
127 | dataArray = pullData.split('\n') |
---|
128 | x=[] |
---|
129 | for eachLine in dataArray: |
---|
130 | if len(eachLine)>1: |
---|
131 | x.append([float(s) for s in eachLine.split() ]) |
---|
132 | Thet=[row[0] for row in x] |
---|
133 | SEY=[row[1] for row in x] |
---|
134 | FF=range(0,len(Angle)) |
---|
135 | Freq=range(0,len(Angle)) |
---|
136 | |
---|
137 | for cntA in range(0,len(Angle)): |
---|
138 | for cntT in range(0,len(Thet)): |
---|
139 | if Angle[cntA]==Thet[cntT]: |
---|
140 | FF[cntA]=Spectrum[cntA]/SEY[cntT]*1.6e-19 |
---|
141 | Freq[cntA]=0.5e+11/(1.-np.cos(Angle[cntA]/180.*3.1415926536))*1e-12 |
---|
142 | |
---|
143 | |
---|
144 | a3.plot(Freq, FF,'-^') |
---|
145 | a3.set_title('Energy='+str(Energy)+'MeV; x0='+str(x0)+'mm') |
---|
146 | a3.set_xlabel('Frequency, [THz]') |
---|
147 | a3.set_ylim( ymin=0.,ymax=0.02) |
---|
148 | canvas3.show() |
---|
149 | |
---|
150 | class Example(Frame): |
---|
151 | |
---|
152 | def __init__(self, parent): |
---|
153 | Frame.__init__(self, parent) |
---|
154 | |
---|
155 | self.parent = parent |
---|
156 | self.initUI() |
---|
157 | |
---|
158 | |
---|
159 | def initUI(self): |
---|
160 | |
---|
161 | self.parent.title("Spectrum analyzing tool | ETALON") |
---|
162 | self.style = Style() |
---|
163 | self.style.theme_use("default") |
---|
164 | self.pack(fill=BOTH, expand=True) |
---|
165 | |
---|
166 | |
---|
167 | menubar = Menu(self.parent) |
---|
168 | self.parent.config(menu=menubar) |
---|
169 | |
---|
170 | fileMenu = Menu(menubar) |
---|
171 | fileMenu.add_command(label="Pause", state=DISABLED) |
---|
172 | fileMenu.add_command(label="Open", state=DISABLED) |
---|
173 | fileMenu.add_command(label="Options", state=DISABLED) |
---|
174 | fileMenu.add_command(label="Exit", command=self.quit) |
---|
175 | |
---|
176 | aboutMenu = Menu(menubar) |
---|
177 | aboutMenu.add_command(label="Tutorial", command=DISABLED) |
---|
178 | aboutMenu.add_command(label="Info", command=self.about) |
---|
179 | |
---|
180 | menubar.add_cascade(label="Control", menu=fileMenu) |
---|
181 | menubar.add_cascade(label="About", menu=aboutMenu) |
---|
182 | |
---|
183 | stepZero = LabelFrame(self, text="Position:") |
---|
184 | stepZero.grid(row=0, columnspan=6, sticky='WE', padx=5, pady=5, ipadx=5, ipady=5) |
---|
185 | canvas1 = FigureCanvasTkAgg(fig0, stepZero) |
---|
186 | canvas1.show() |
---|
187 | canvas1.get_tk_widget().pack(padx=5,pady=10) |
---|
188 | Label(stepZero, text="Detector:").pack(side=LEFT, padx=5, pady=8) |
---|
189 | self.Detnum = Entry(stepZero) |
---|
190 | self.Detnum.pack(side=LEFT, padx=5, pady=8) |
---|
191 | self.Detnum.insert(1,'4, 7, 10') |
---|
192 | Button(stepZero, text="Update", command=self.SetDetector).pack(side=RIGHT, padx=5, pady=8) |
---|
193 | |
---|
194 | stepZero2 = LabelFrame(self, text="Spectrum:") |
---|
195 | stepZero2.grid(row=0,column=7, columnspan=6, sticky='WE', padx=5, pady=5, ipadx=5, ipady=5) |
---|
196 | global canvas2 |
---|
197 | canvas2 = FigureCanvasTkAgg(fig1, stepZero2) |
---|
198 | |
---|
199 | canvas2.show() |
---|
200 | canvas2.get_tk_widget().pack(padx=5,pady=10) |
---|
201 | #toolbar = NavigationToolbar2TkAgg(self.canvas2, stepZero2) |
---|
202 | #toolbar.update() |
---|
203 | #canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=True) |
---|
204 | Label(stepZero2, text="Postion:").pack(side=LEFT, padx=5, pady=8) |
---|
205 | self.Posnum = Entry(stepZero2) |
---|
206 | self.Posnum.pack(side=LEFT, padx=5, pady=8) |
---|
207 | self.Posnum.insert(1,'4000000') |
---|
208 | Button(stepZero2, text="Clear", command=self.SetClear).pack(side=RIGHT, padx=5, pady=8) |
---|
209 | Checkbutton(stepZero2, text="Set Reference", command=self.SetReference).pack(side=RIGHT, padx=5, pady=8) |
---|
210 | #Button(stepZero2, text="Set Ref", command=self.SetReference).pack(side=RIGHT, padx=5, pady=8) |
---|
211 | Button(stepZero2, text="Update", command=self.SetPosition).pack(side=LEFT, padx=5, pady=8) |
---|
212 | stepZero3 = LabelFrame(self, text="Form Factor:") |
---|
213 | stepZero3.grid(row=0,column=13, columnspan=6, sticky='WE', padx=5, pady=5, ipadx=5, ipady=5) |
---|
214 | global canvas3 |
---|
215 | canvas3 = FigureCanvasTkAgg(fig3, stepZero3) |
---|
216 | #animate3() |
---|
217 | canvas3.show() |
---|
218 | canvas3.get_tk_widget().pack(padx=5,pady=10) |
---|
219 | Label(stepZero3, text="Energy (MeV):").pack(side=LEFT, padx=5, pady=8) |
---|
220 | #self.inFileTxt = Entry(stepZero3) |
---|
221 | #self.inFileTxt.pack(side=LEFT, padx=5, pady=8) |
---|
222 | #self.inFileTxt.insert(1,'44.3') |
---|
223 | Eng=[] |
---|
224 | for c in range(34,45): |
---|
225 | Eng.append(str(c)) |
---|
226 | self.box = Combobox(stepZero3, value=Eng) |
---|
227 | self.box.bind("<<ComboboxSelected>>", self.newselection) |
---|
228 | self.box.current(10) |
---|
229 | self.box.pack(side=LEFT, padx=5, pady=8) |
---|
230 | Label(stepZero3, text="BGS (mm):").pack(side=LEFT, padx=5, pady=8) |
---|
231 | posX=[] |
---|
232 | for c in range(0,30,5): |
---|
233 | posX.append(str(c)) |
---|
234 | self.box2 = Combobox(stepZero3, value=posX) |
---|
235 | self.box2.bind("<<ComboboxSelected>>", self.newselection2) |
---|
236 | self.box2.current(1) |
---|
237 | self.box2.pack(side=LEFT, padx=5, pady=8) |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | #Button(stepZero3, text="Ok", state=DISABLED).pack(side=RIGHT, padx=5, pady=8) |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | stepOne = LabelFrame(self, text="Date/Time settings: ") |
---|
246 | stepOne.grid(row=1, columnspan=6, sticky='WE', padx=5, pady=5, ipadx=5, ipady=5) |
---|
247 | |
---|
248 | |
---|
249 | Label(stepOne, text="Start:").grid(row=0, column=0, sticky='E', padx=5, pady=2) |
---|
250 | |
---|
251 | self.inFileTxt = Entry(stepOne) |
---|
252 | self.inFileTxt.grid(row=0, column=1, sticky="WE", pady=3) |
---|
253 | self.inFileTxt.insert(1,str(lowlim)) |
---|
254 | Label(stepOne, text=' ').grid(row=0, column=2, sticky='E', padx=5, pady=2) |
---|
255 | Label(stepOne, text="End:").grid(row=0, column=3, sticky='E', padx=5, pady=2) |
---|
256 | |
---|
257 | self.outFileTxt = Entry(stepOne) |
---|
258 | self.outFileTxt.grid(row=0, column=4, sticky="WE", pady=2) |
---|
259 | self.outFileTxt.insert(2,str(highlim)) |
---|
260 | outFileBtn = Label(stepOne, text=' ') |
---|
261 | outFileBtn.grid(row=0, column=5, sticky='E', padx=5, pady=2) |
---|
262 | |
---|
263 | Label(stepOne, text="Data").grid(row=1, column=0, sticky='E', padx=5, pady=2) |
---|
264 | self.inEncTxt = Entry(stepOne) |
---|
265 | self.inEncTxt.grid(row=1, column=1, sticky='E', pady=2) |
---|
266 | self.inEncTxt.insert(3,str(dateYMD)) |
---|
267 | Label(stepOne, text=' ').grid(row=1, column=2, sticky='E', padx=5, pady=2) |
---|
268 | |
---|
269 | #Label(stepOne, text="Detector").grid(row=1, column=3, sticky='E', padx=5, pady=2) |
---|
270 | #self.outEncTxt = Entry(stepOne) |
---|
271 | #self.outEncTxt.grid(row=1, column=4, sticky='E', pady=2) |
---|
272 | #self.outEncTxt.insert(3,'7') |
---|
273 | |
---|
274 | outEncLbl = Button(stepOne, text="Set", command=self.on_button) |
---|
275 | outEncLbl.grid(row=1, column=5, padx=5, pady=2) |
---|
276 | |
---|
277 | |
---|
278 | def SetDetector(self): |
---|
279 | global detN |
---|
280 | detN=[int(x) for x in self.Detnum.get().split(',') if x.strip().isdigit()] |
---|
281 | #print detN |
---|
282 | def SetClear(self): |
---|
283 | global ClearP |
---|
284 | ClearP=True |
---|
285 | |
---|
286 | def SetReference(self): |
---|
287 | global RefP |
---|
288 | if (RefP==True): |
---|
289 | RefP=False |
---|
290 | else: |
---|
291 | RefP=True |
---|
292 | |
---|
293 | def SetPosition(self): |
---|
294 | global PositionDet |
---|
295 | PositionDet=[int(x) for x in self.Posnum.get().split(',') if x.strip().isdigit()] |
---|
296 | def newselection(self, event): |
---|
297 | global Energy |
---|
298 | Energy=int(self.box.get()) |
---|
299 | def newselection2(self, event): |
---|
300 | global x0 |
---|
301 | x0=(int(self.box2.get())) |
---|
302 | def about(self): |
---|
303 | pullData = open('Info.txt','r').read() |
---|
304 | mbox.showinfo("Information", pullData) |
---|
305 | |
---|
306 | def on_button(self): |
---|
307 | global lowlim |
---|
308 | lowlim=int(self.inFileTxt.get()) |
---|
309 | global highlim |
---|
310 | highlim=int(self.outFileTxt.get()) |
---|
311 | global dateYMD |
---|
312 | dateYMD=self.inEncTxt.get() |
---|
313 | if len(dateYMD)>8: |
---|
314 | mbox.showerror("Error", "Wrong date type! It should be YYYYMMDD, example 20170504") |
---|
315 | if (not (os.path.isdir(path+dateYMD+'/'))): |
---|
316 | mbox.showerror("Error", "This day with measurements is not exist!!! Chack the date!") |
---|
317 | |
---|
318 | |
---|
319 | |
---|
320 | |
---|
321 | |
---|
322 | def main(): |
---|
323 | |
---|
324 | root = Tk() |
---|
325 | root.geometry("1630x600+300+300") |
---|
326 | app = Example(root) |
---|
327 | ani = animation.FuncAnimation(fig0,position_plot, interval=1000) |
---|
328 | root.mainloop() |
---|
329 | |
---|
330 | |
---|
331 | if __name__ == '__main__': |
---|
332 | main() |
---|