Ewindow.py 13.9 KB
Newer Older
1
# -*- coding: iso-8859-15 -*-
2
3
import Tkinter as tk
from PTE import *
Matheus Müller's avatar
Matheus Müller committed
4
from pylab import *
5
from layers import *
Matheus Müller's avatar
Matheus Müller committed
6
7
from numpy import zeros
import os
8
import tkFileDialog
9
import shutil
10
import sys
11

12
13
##############################################################################
# Control variables and lists
Matheus Müller's avatar
Matheus Müller committed
14
Edict = dict()
15
Ebuttons = list()
16
Labentrys = list()
17
Frames = list()
18
i=0
19
20
Econtrol = 0

Matheus Müller's avatar
Matheus Müller committed
21
##############################################################################
22
# Element callback
23
24
25
def elem_callback(x, edict , ebutton):
    global Labentrys
    PTable(edict[x] , ebutton[x], Labentrys)
26
27
28

##############################################################################
# Element selection
29
def elem_select(i, button):
30

31
    global Econtrol, Edict, Labentrys
32
    button[Econtrol]['relief']='raised'
33
    Econtrol = i
34
35
    button[i]['relief']='sunken'

36
37
38
39
40
41
    Labentrys[0].configure(text = '%s' %Edict[i]['name'])
    Labentrys[1].configure(text = 'Mass - %f' %Edict[i]['mass'])
    Labentrys[2].configure(text = 'Z - %f' %Edict[i]['Z'])
    Labentrys[3].configure(text = 'Symbol - %s' %Edict[i]['symbol'])
    Labentrys[4].configure(text = 'Line Shape α - %f' %Edict[i]['LineShape'])
    Labentrys[5].configure(text = 'Max Depht - %f' %Edict[i]['profundidademax'])
42

43
    for i in arange(6,12):
44
        Labentrys[i].delete (0, "end")
45

46
47
48
49
50
51
    Labentrys[6].insert(0, Edict[int(Econtrol)]['name'])
    Labentrys[7].insert(0, Edict[int(Econtrol)]['mass'])
    Labentrys[8].insert(0, int(Edict[int(Econtrol)]['Z']))
    Labentrys[9].insert(0, Edict[int(Econtrol)]['symbol'])
    Labentrys[10].insert(0, Edict[int(Econtrol)]['LineShape'])
    Labentrys[11].insert(0, Edict[int(Econtrol)]['profundidademax'])
52

Matheus Müller's avatar
Matheus Müller committed
53
##############################################################################
54
# Init
55
def ewin_build(window, OFlabentries, mainwindow, calcbutton, distvar, modevar, Labelframe):
56

57
58
##############################################################################
# Element addition
59
60
    def create():
        global i, Edict, Ebuttons
61
        Edict[i]=dict(name="New",symbol="Hf",mass=178.,Z=72,dist=zeros(int(10./float(OFlabentries[17].get()))),LineShape=200.,profundidademax=10.)
62
        Ebuttons.insert(i, tk.Button(Eframe, text=i, width=1, height=1, command = lambda i=i : elem_select(i,Ebuttons)) )
63
        Ebuttons[i].grid(column = ((len(Ebuttons)-1)%13), row = int(math.floor((len(Ebuttons)-1)/13)))
64
        i = i+1
65

66
67
##############################################################################
# Element deletion
68
    def remove():
69
        global i, Edict, Ebuttons, Econtrol
70
        if i > 1:
71
            i = i-1
72
73
74
            del Edict[i]
            Ebuttons[i].destroy()
            del Ebuttons[i]
75
76
77
            if Econtrol == i:
                Econtrol = Econtrol -1
                Ebuttons[Econtrol]['relief']='sunken'
78
        elem_select(0, Ebuttons)
79

Matheus Müller's avatar
Matheus Müller committed
80
##############################################################################
81
82
# Element Frames

Matheus Müller's avatar
Matheus Müller committed
83
84
    Labelprop = tk.LabelFrame(window, text = 'Properties', relief='raised', bd=2)
    Labelprop.pack(side = 'top')
85
    Label1 = tk.LabelFrame(window, text = 'Elements', relief='raised', bd=2)
Matheus Müller's avatar
Matheus Müller committed
86
    Label1.pack(side = 'top') 
87
    # For Button Creation
88
89
    Eframe = tk.Canvas(Label1)
    Eframe.pack(fill='both',expand=0)
Matheus Müller's avatar
Matheus Müller committed
90

91
92
93
##############################################################################
# Init
    create()
Matheus Müller's avatar
Matheus Müller committed
94
    
95
96
97
##############################################################################
# Properties

98
    global Econtrol, Edict, Labentrys
99
    def update(i):
100
101
102
103
104
105
106
107
108
109
110
111
        Edict[i]['name'] = str(Labentrys[6].get())
        Labentrys[0].configure(text = '%s' %Edict[i]['name'])
        Edict[i]['mass'] = float(Labentrys[7].get())
        Labentrys[1].configure(text = 'Mass - %f' %Edict[i]['mass'])
        Edict[i]['Z'] = float(Labentrys[8].get())
        Labentrys[2].configure(text = 'Z - %f' %Edict[i]['Z'])
        Edict[i]['symbol'] = str(Labentrys[9].get())
        Labentrys[3].configure(text = 'Symbol - %s' %Edict[i]['symbol'])
        Edict[i]['LineShape'] = float(Labentrys[10].get())
        Labentrys[4].configure(text = 'Line Shape α - %f' %Edict[i]['LineShape'])
        Edict[i]['profundidademax'] = float(Labentrys[11].get())
        Labentrys[5].configure(text = 'Max Depht - %f' %Edict[i]['profundidademax'])
Matheus Müller's avatar
Matheus Müller committed
112
        Edict[i]['dist'] = np.loadtxt(Edict[i]['symbol']+".prof")
113

114
115
116
117
118
119
120
121
122
    for i in range(3):
        Frames.insert(i, tk.Frame(Labelprop) )

    Frames[0].pack(side='left')
    Frames[1].pack(side='top')
    Frames[2].pack(side='right')

    for i in arange(3, 5):
        Frames.insert(i, tk.Frame(Frames[0]) )
123

124
125
    Frames[3].pack(side='left')
    Frames[4].pack(side='right')
126

127
128
129
130
131
132
133
134
135
    for i in arange(5, 7):
        Frames.insert(i, tk.Frame(Frames[1]) )

    Frames[5].pack(side='left')
    Frames[6].pack(side='right')
    Labeldist = tk.LabelFrame(Frames[5], text = 'Distribution', relief='raised', bd=2)
    Labeldist.pack(side = 'bottom') 
    Frames.insert(7, tk.Frame(Labeldist) )
    Frames[7].pack(side='top')
136

137
138
139
140
141
142
143
    # Label // Entries numbers:
    # 0 // 6 = Name
    # 1 // 7 = Mass
    # 2 // 8 = Z (Atomic number)
    # 3 // 9 = Symbol
    # 4 // 10 = Line shape
    # 5 // 11 = Max Depht
144
145
146
147
148
149
    Labentrys.insert(0, tk.Label(Frames[3], width=23, pady=2, text='%s' %Edict[int(Econtrol)]['name']) )
    Labentrys.insert(1, tk.Label(Frames[3], width=23, pady=2, text='Mass - %f' %Edict[int(Econtrol)]['mass']) )
    Labentrys.insert(2, tk.Label(Frames[3], width=23, pady=2, text='Z - %f' %Edict[int(Econtrol)]['Z']) )
    Labentrys.insert(3, tk.Label(Frames[3], width=23, pady=2, text= 'Symbol - %s' %Edict[int(Econtrol)]['symbol']) )
    Labentrys.insert(4, tk.Label(Frames[3], width=23, pady=2, text='Line Shape α - %f' %Edict[int(Econtrol)]['LineShape']) )
    Labentrys.insert(5, tk.Label(Frames[3], width=23, pady=2, text='Max Depht - %f' %Edict[int(Econtrol)]['profundidademax']) )
150
151
152
153
154

    for i in range(6):
        Labentrys[i].pack()

    for i in arange(6, 12):
155
        Labentrys.insert(i, tk.Entry(Frames[4], width=17) )
156
157
        Labentrys[i].pack()

158
    # Declaration of interpolation and distribution method variables
159
160
161
    methodvar = tk.StringVar()
    methodvar.set('reta')

162

163
164
165
##############################################################################
# Load/Save sample
    def LOADSAMPLE():
166
167
168
169
170
171
172
173
174
        global i, Econtrol
        arquivo = tkFileDialog.askopenfile()
        for n in arange(9,18):
            OFlabentries[n].delete(0,len(OFlabentries[n].get()))
            OFlabentries[n].insert(0,float(arquivo.readline()))
        for n in arange(21,24):
            OFlabentries[n].delete(0,len(OFlabentries[n].get()))
            OFlabentries[n].insert(0,float(arquivo.readline()))
        f = loadtxt(arquivo.name + '.elm', dtype="string")
175
176
177
178
179
180
        els = f[:, 0]
        eln = f[:, 1]
        elm = f[:, 2]
        elz = f[:, 3]
        LSs = f[:, 4]
        eld = f[:, 5]
181
182
183
184
185
186
        Edict.clear()
        for n in range(len(Ebuttons)):
            Ebuttons[n].destroy()
        del Ebuttons[:]
        i = 0
        Econtrol = 0
187
        for n in range(len(els)):
188
            create()           
189
190
191
192
193
194
195
            Edict[n]['LineShape'] = float(LSs[n])
            Edict[n]['symbol'] = str(els[n])
            Edict[n]['name'] = str(eln[n])
            Edict[n]['mass'] = float(elm[n])
            Edict[n]['Z'] = float(elz[n])
            Edict[n]['profundidademax'] = float(eld[n])
            Ebuttons[n]['text'] = str(els[n])
196
197
            shutil.copy(arquivo.name + '-' + Edict[n]['symbol'] + '.dat' , Edict[n]['symbol']+'.dat')
            shutil.copy(arquivo.name + '-' + Edict[n]['symbol'] + '.prof' , Edict[n]['symbol']+'.prof')
198
        elem_select(0,Ebuttons)
199
        arquivo.close()
200
201

    def SAVESAMPLE():
202
203
204
205
206
207
208
209
210
        config = tkFileDialog.asksaveasfile(mode='w')
        elist = open(config.name + '.elm', 'w+')
        for k in arange(9,18):
            config.write( (OFlabentries[k].get())+'\n' )
        for j in arange(21,24):
            config.write( (OFlabentries[j].get())+'\n' )
        config.write( '##############################################################################\n' )
        config.write( '# dƐ/dx\n# dω²/dx\n# θ out\n# θ in\n# FWHM\n# E min\n# E max\n# E step\n# Depth step\n# Ion energy\n# Ion mass\n# Ion Z\n' )
        for n in range(len(Edict)):
211
212
213
214
            elist.write(Edict[n]['symbol']+' ')
            elist.write(Edict[n]['name']+' ')
            elist.write(str(Edict[n]['mass'])+' ')
            elist.write(str(Edict[n]['Z'])+' ')
215
            elist.write(str(Edict[n]['LineShape'])+' ')
216
            elist.write(str(Edict[n]['profundidademax'])+'\n')
217
218
219
            shutil.copy(Edict[n]['symbol']+'.prof', config.name  + '-' + Edict[n]['symbol'] + '.prof')
            shutil.copy(Edict[n]['symbol']+'.dat', config.name  + '-' + Edict[n]['symbol'] + '.dat')
            shutil.copy('temp.sim', config.name  + '.sim')
220
        elist.close()
221
        config.close()
222

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
##############################################################################
# Menu
    def donothing():
        return 0

    menubar = tk.Menu(mainwindow, bg='grey', relief='flat', bd=0)
    filemenu = tk.Menu(menubar, tearoff=0)
    filemenu.add_command(label="Simulate", command=lambda i=int(Econtrol) :calcbutton.invoke())
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=sys.exit)
    menubar.add_cascade(label="File", menu=filemenu)

    samplemenu = tk.Menu(menubar, tearoff=0)
    samplemenu.add_command(label="Load Sample", command=lambda i=int(Econtrol) :LOADSAMPLE())
    samplemenu.add_command(label="Save Sample", command=lambda i=int(Econtrol) :SAVESAMPLE())
    menubar.add_cascade(label="Samples", menu=samplemenu)

    helpmenu = tk.Menu(menubar, tearoff=0)
    helpmenu.add_command(label="Help!", command=donothing)
    helpmenu.add_command(label="About...", command=donothing)
    menubar.add_cascade(label="Help", menu=helpmenu)
    mainwindow.config(menu=menubar)

246
247
248
##############################################################################
# Buttons

249
    but = tk.Button(Frames[3], command=create, text='Add element', bd=1, height=1,width=15, pady='4')
250
    but.pack()
251
    butrmv = tk.Button(Frames[4], command=remove, text='Remove element', bd=1, height=1,width=14, pady='4')
252
    butrmv.pack()
253
    butPTE = tk.Button(Frames[5], text='Select from PTE', bd=1, width=15, command=lambda i=i :elem_callback(Econtrol,Edict,Ebuttons) )
254
    butPTE.pack()
255
    BUpdate = tk.Button(Frames[5], text ='Update properties', command=lambda i=int(Econtrol) :update(Econtrol), width=15)
256
257
    BUpdate.pack()

258
##############################################################################
259
260
261
262
263
264
265
266
# Distribution
 
    def changestate():
        if str(distvar.get()) == 'layer':
            BLayer['state']='normal'
            BUpdateD['state']='disabled'
            RS['state']='disabled'
            RL['state']='disabled'
267
268
            OFlabentries[9]['state']='disabled'
            OFlabentries[10]['state']='disabled'
269
270
271
272
273
274
275
276
277
            for i in arange(6, 12):
                Labentrys[i]['state']='disabled'
            but['state']='disabled'
            butrmv['state']='disabled'
            butPTE['state']='disabled'
            BUpdate['state']='disabled'
            for n in arange(0, len(Ebuttons)):
                Ebuttons[n]['state'] = 'disabled'

278
279
280
281
282
        elif str(distvar.get()) == 'drawing':
            BLayer['state']='disabled'
            BUpdateD['state']='normal'
            RS['state']='normal'
            RL['state']='normal'
283
284
            OFlabentries[9]['state']='normal'
            OFlabentries[10]['state']='normal'
285
286
287
288
289
290
291
292
            for i in arange(6, 12):
                Labentrys[i]['state']='normal'
            but['state']='normal'
            butrmv['state']='normal'
            butPTE['state']='normal'
            BUpdate['state']='normal'
            for n in arange(0, len(Ebuttons)):
                Ebuttons[n]['state'] = 'normal'
293
294
295
296
297
298
299
300
301
302
303
304

    def call_layers():
        layers()

    # By Layers
    BLayer = tk.Button(Labeldist, text ='Open Layers', command = lambda i = int(Econtrol) : call_layers(), width=15)
    BLayer.pack()
    # By Drawing
    BUpdateD = tk.Button(Labeldist, text = 'Element Distribution', command = lambda i = int(Econtrol) : os.system("python profiler.py %s %s %s %s &" %( Edict[int(Econtrol)]['symbol'], str(methodvar.get()), str(Labentrys[11].get()), str(OFlabentries[17].get()) ) ), width=15)
    BUpdateD.pack()
    # Interpolation methods
    RS = tk.Radiobutton(Labeldist, text="Step", variable=methodvar, value='step')
305
    RS.pack(side='left')
306
    RL = tk.Radiobutton(Labeldist, text="Lines", variable=methodvar, value='reta')
307
    RL.pack(side='right')
308
309
310
311
312
    # Distribution methods
    RLay = tk.Radiobutton(Frames[7], text="Layers", command=lambda i=int(Econtrol) :changestate(), variable=distvar, value='layer')
    RLay.pack(side='left')
    RDra = tk.Radiobutton(Frames[7], text="Drawing", command=lambda i=int(Econtrol) :changestate(), variable=distvar, value='drawing')
    RDra.pack(side='right')
313

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
##############################################################################
# Experiment variations

    def changemode():
        if str(modevar.get()) == 'IONS':
            for n in range(len(Ebuttons)):
                Ebuttons[n]['state']='normal'
            but['state'] = 'normal'
            butrmv['state'] = 'normal'
            butPTE['state'] = 'normal'
            RLay['state'] = 'normal'

        elif str(modevar.get()) == 'RRNA':
            RDra.invoke()
            elem_select(0,Ebuttons)
            but['state'] = 'disabled'
            butrmv['state'] = 'disabled'
            RLay['state'] = 'disabled'
            for n in arange(1, len(Ebuttons)):
                Ebuttons[n]['state'] = 'disabled'

    Rions = tk.Radiobutton(Labelframe, text="Ion Scattering", command=lambda i=i :changemode(), variable=modevar, value='IONS', width=18)
    Rions.pack()
    Rrrna = tk.Radiobutton(Labelframe, text="Resonant RNA", command=lambda i=i :changemode(), variable=modevar, value='RRNA', width=18)
    Rrrna.pack()
    Rions.select()

Matheus Müller's avatar
Matheus Müller committed
341
##############################################################################
Matheus Müller's avatar
Matheus Müller committed
342
# Init
343
344

    RDra.invoke()
345
    RS.select()
Matheus Müller's avatar
Matheus Müller committed
346
    Ebuttons[0].invoke()
347

Matheus Müller's avatar
Matheus Müller committed
348
##############################################################################
349