newmodulo.py 8.82 KB
Newer Older
1
2
3
4
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

import os
5
import pexpect
6
import Tkinter as tk
7
import tkMessageBox
8
9
import matplotlib.pyplot as plt
from pylab import *
10
import time
11
12
13
14
15

##############################################################################
##############################################################################
# Subprocess variable

16
17
18
child = pexpect.spawn('./N957Run')
time.sleep(3.3)
child.send('s')
19
20
21

##############################################################################
##############################################################################
22
# Histrogram
23

24
25
26
27
28
29
t = 2000
bmin = 0
bmax = 8192
Data=list()
for a in range(8192):
    Data.insert(a, 0)
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
xx = arange(0, 8192)       		  
temp, = plot(xx, xx*0, 'y.', lw=0.9)
plt.grid(True)
plt.xlabel('Channel')
plt.ylabel('Counts')

ax = subplot(111)
ax.xaxis.set_major_locator(MultipleLocator(1024))
ax.xaxis.set_minor_locator(MultipleLocator(128))

ax.xaxis.grid(True,'minor')
ax.xaxis.grid(True,'major',linewidth=2)

##############################################################################
##############################################################################

Interface = dict(frames=list(), buttons=list(), labels=list(), spinboxes=list(), entries=list(), labelframes=list())
Livetime = dict(ADC_Conversion='0' , Time='0' , LiveTime='0' )

def set_fator():
    global Interface, child, bmax
    if int(Interface['spinboxes'][0].get()) == 8192:
        child.send('a')
        bmax = 8192
    elif int(Interface['spinboxes'][0].get()) == 4096:
        child.send('b')
        bmax = 4096
    elif int(Interface['spinboxes'][0].get()) == 2048:
        child.send('c')
        bmax = 2048
    elif int(Interface['spinboxes'][0].get()) == 1024:
        child.send('d')
        bmax = 1024
    elif int(Interface['spinboxes'][0].get()) == 512:
        child.send('e')
        bmax = 512
    elif int(Interface['spinboxes'][0].get()) == 256:
        child.send('f')
        bmax = 256
    elif int(Interface['spinboxes'][0].get()) == 128:
        child.send('g')
        bmax = 128
    elif int(Interface['spinboxes'][0].get()) == 64:
        child.send('h')
        bmax = 64

def dump():
    global child
    child.send('i')
80
81

def graphic():
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    global child, Data, Interface, bmin, bmax
    for n in arange(0, int(Interface['spinboxes'][0].get())):
        Data[n] = int(child.readline().strip("\r\n"))
    temp.set_ydata(Data)
    #plt.fill_between(range(8192),Data,0,color='0.8')
    plt.xlim(bmin, bmax)
    plt.ylim(0, max(Data[bmin:bmax])*1.1)
    show()
 
def realtimeplot():
    global t
    dump()
    try:
        root.after(t, realtimeplot)
        graphic()
    except ValueError: 
        pass 

#############################################################################
101
102
103
104
##############################################################################
# Main window

root = tk.Tk()
105
root.minsize(400,350)
106
root.title('Modulo Multicanal')
107
#root.geometry('670x470+200+200')
108
109
110
111
112
113

##############################################################################
##############################################################################
# Control functions

def finish():
114
115
116
117
118
119
120
121
    global child
    child.send('j')
    Livetime['ADC_Conversion'] = child.readline().strip("\r\n")
    Livetime['Time'] = child.readline().strip("\r\n")
    Livetime['LiveTime'] = child.readline().strip("\r\n")
    Interface['labels'][0].configure(text='ADC Conversions: %s' %Livetime['ADC_Conversion'])
    Interface['labels'][1].configure(text='Total Time: %s' %Livetime['Time'])
    Interface['labels'][2].configure(text='Live Time: %s' %Livetime['LiveTime'])
122
123

def reset():
124
125
    global child
    child.send('r')
126
127

def pause():
128
    return 0
129
130

def restart():
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    global child
    child = pexpect.spawn('./N957Run')
    time.sleep(3.3)
    child.send('s')

def save():
    global child
    child.send('z')

def load():
    global child
    child.send('y')

def plotting():
    global bmin, bmax
    if Interface['entries'][0].get().isdigit() and int(Interface['entries'][0].get()) >= 0 and Interface['entries'][1].get().isdigit() and int(Interface['entries'][1].get()) <= int(Interface['spinboxes'][0].get()) and int(Interface['entries'][1].get()) > int(Interface['entries'][0].get()): 
        bmin = int(Interface['entries'][0].get())
        bmax = int(Interface['entries'][1].get())
        Interface['labelframes'][0].configure(text = 'Region of interest - %i to %i' %(bmin, bmax))
    else:
        tkMessageBox.showinfo("Error", "Invalid values")

def refreshrate():
    global t
    if Interface['entries'][2].get().isdigit() and int(Interface['entries'][2].get()) > 499:
        t = int(Interface['entries'][2].get())
        Interface['labelframes'][1].configure(text = 'Refresh Rate - %ims' %t)
    else:
        tkMessageBox.showinfo("Error", "Invalid value")
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

##############################################################################
##############################################################################
# Safe Exit

def shutdown():
    sys.exit    

##############################################################################
##############################################################################
# Interface elements

for i in range(2):
    Interface['frames'].insert(i, tk.Frame(root))
    Interface['frames'][i].pack()

176
177
178
179
Interface['labelframes'].insert(0,tk.LabelFrame(Interface['frames'][1], relief='raised', text='Region of interest - 0 to 8192', bd=2))
Interface['labelframes'].insert(1,tk.LabelFrame(Interface['frames'][1], relief='raised', text='Refresh Rate - 2000ms', bd=2))
Interface['labelframes'].insert(2,tk.LabelFrame(Interface['frames'][1], relief='raised', text='Bins', bd=2))
Interface['labelframes'].insert(3,tk.LabelFrame(Interface['frames'][1], relief='raised', text='Last Data', bd=2))
180

181
182
183
Interface['labels'].insert(0,tk.Label(Interface['labelframes'][3], text='ADC Conversions: %s' %Livetime['ADC_Conversion']))
Interface['labels'].insert(1,tk.Label(Interface['labelframes'][3], text='Total Time: %s' %Livetime['Time']))
Interface['labels'].insert(2,tk.Label(Interface['labelframes'][3], text='Live Time: %s' %Livetime['LiveTime']))
184

185
186
187
188
189
190
191
192
193
194
195
for i in range(3):
    Interface['labelframes'][i].pack()
    Interface['frames'].insert((i+2), tk.Frame(Interface['labelframes'][0]))
    Interface['frames'][(i+2)].pack(side='left')
    Interface['labels'][i].pack()

Interface['labelframes'][3].pack()
Interface['labels'].insert(3,tk.Label(Interface['frames'][3], text='to'))
Interface['labels'][3].pack()

Interface['spinboxes'].insert(0, tk.Spinbox(Interface['labelframes'][2], command = set_fator, values=[64,128,256,512,1024,2048,4096,8192]))
196
197
198
199
Interface['spinboxes'][0].pack()
Interface['spinboxes'][0].delete(0,len(Interface['spinboxes'][0].get()))
Interface['spinboxes'][0].insert(0,8192)

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
Interface['entries'].insert(0, tk.Entry(Interface['frames'][2], width=7))
Interface['entries'].insert(1, tk.Entry(Interface['frames'][4], width=7))
Interface['entries'].insert(2, tk.Entry(Interface['labelframes'][1], width=16))

for i in range(3):
    Interface['entries'][i].pack()

Interface['buttons'].insert(0, tk.Button(Interface['frames'][0],  text = 'Plot', command = lambda i=i: realtimeplot(), bd=2, width=11, height=1))
Interface['buttons'].insert(1, tk.Button(Interface['frames'][0],  text = 'Pause', command = lambda i=i: pause(), bd=2, width=11, height=1))
Interface['buttons'].insert(2, tk.Button(Interface['frames'][0],  text = 'Reset', command = lambda i=i: reset(), bd=2, width=11, height=1))
Interface['buttons'].insert(3, tk.Button(Interface['frames'][0],  text = 'Finish', command = lambda i=i: finish(), bd=2, width=11, height=1))
Interface['buttons'].insert(4, tk.Button(Interface['frames'][0],  text = 'Restart', command = lambda i=i: restart(), bd=2, width=11, height=1))
Interface['buttons'].insert(5, tk.Button(Interface['frames'][0],  text = 'Save', command = lambda i=i: save(), bd=2, width=11, height=1))
Interface['buttons'].insert(6, tk.Button(Interface['frames'][0],  text = 'Load', command = lambda i=i: load(), bd=2, width=11, height=1))
Interface['buttons'].insert(7, tk.Button(Interface['labelframes'][0],  text = 'Change', command = lambda i=i: plotting(), bd=2, width=7, height=1))
Interface['buttons'].insert(8, tk.Button(Interface['labelframes'][1],  text = 'Change', command = lambda i=i: refreshrate(), bd=2, width=13, height=1))

for i in range(9):
    Interface['buttons'][i].pack()

220
221
222
223
224
225
226
227
menubar = tk.Menu(root, bg='grey', relief='flat', bd=0)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command = sys.exit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)

##############################################################################
##############################################################################
228
229
# Paralel processing

230
root.mainloop()
231

232
233
234
##############################################################################
##############################################################################