Ewindow.py 13.8 KB
Newer Older
1
# -*- coding: iso-8859-15 -*-
2 3
import Tkinter as tk
from PTE import *
4
from pylab import *
5
from layers import *
6 7
from numpy import zeros
import os
8
import tkFileDialog
9
import shutil
10
import sys
11

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

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

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
Matheus Müller's avatar
Matheus Müller committed
61
        Edict[i]=dict(name="New Element",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

80
##############################################################################
81 82
# Element Frames

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)
86
    Label1.pack(side = 'top') 
87
    # For Button Creation
88 89
    Eframe = tk.Canvas(Label1)
    Eframe.pack(fill='both',expand=0)
90

91 92 93
##############################################################################
# Init
    create()
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'])
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
Matheus Müller's avatar
Matheus Müller committed
144
    ## 13 // 12 = Resonance energy (For RNA only)
145 146 147 148 149 150
    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']) )
151

Matheus Müller's avatar
Matheus Müller committed
152
    for i in arange(6, 13):
153
        Labentrys.insert(i, tk.Entry(Frames[4], width=17) )
Matheus Müller's avatar
Matheus Müller committed
154 155 156 157

    Labentrys.insert(13, tk.Label(Frames[3], width=23, pady=2, text='Resonance energy') )

    for i in range(14):
158 159
        Labentrys[i].pack()

160
    # Declaration of interpolation and distribution method variables
161
    methodvar = tk.StringVar()
162
    methodvar.set('step')
163

164

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

    def SAVESAMPLE():
204 205 206 207
        config = tkFileDialog.asksaveasfile(mode='w')
        elist = open(config.name + '.elm', 'w+')
        for k in arange(9,18):
            config.write( (OFlabentries[k].get())+'\n' )
208
        for j in arange(21,25):
209 210
            config.write( (OFlabentries[j].get())+'\n' )
        config.write( '##############################################################################\n' )
211
        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 # Dose\n' )
212
        for n in range(len(Edict)):
213 214 215 216
            elist.write(Edict[n]['symbol']+' ')
            elist.write(Edict[n]['name']+' ')
            elist.write(str(Edict[n]['mass'])+' ')
            elist.write(str(Edict[n]['Z'])+' ')
217
            elist.write(str(Edict[n]['LineShape'])+' ')
218
            elist.write(str(Edict[n]['profundidademax'])+'\n')
219 220 221
            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')
222
        elist.close()
223
        config.close()
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
##############################################################################
# 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)

248 249 250
##############################################################################
# Buttons

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

260
##############################################################################
261 262 263
# Distribution
 
    def changestate():
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
        BLayer['state']='normal'
        BUpdateD['state']='disabled'
        RS['state']='disabled'
        RL['state']='disabled'
        OFlabentries[9]['state']='disabled'
        OFlabentries[10]['state']='disabled'
        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'

    def changestate2():
        BLayer['state']='disabled'
        BUpdateD['state']='normal'
        RS['state']='normal'
        RL['state']='normal'
        OFlabentries[9]['state']='normal'
        OFlabentries[10]['state']='normal'
        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'
294 295 296 297 298 299 300 301 302 303 304 305

    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')
306
    RS.pack(side='left')
307
    RL = tk.Radiobutton(Labeldist, text="Lines", variable=methodvar, value='reta')
308
    RL.pack(side='right')
309
    # Distribution methods
310
    RLay = tk.Radiobutton(Frames[7], text="Layers", command=lambda i=int(Econtrol) :changestate(), variable=distvar, value=0)
311
    RLay.pack(side='left')
312
    RDra = tk.Radiobutton(Frames[7], text="Drawing", command=lambda i=int(Econtrol) :changestate2(), variable=distvar, value=1)
313
    RDra.pack(side='right')
314

315 316 317 318
##############################################################################
# Experiment variations

    def changemode():
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
        for n in range(len(Ebuttons)):
            Ebuttons[n]['state']='normal'
        but['state'] = 'normal'
        butrmv['state'] = 'normal'
        butPTE['state'] = 'normal'
        RLay['state'] = 'normal'
        Labentrys[12]['state'] = 'disabled'

    def changemode2():
        RDra.invoke()
        elem_select(0,Ebuttons)
        but['state'] = 'disabled'
        butrmv['state'] = 'disabled'
        RLay['state'] = 'disabled'
        Labentrys[12]['state'] = 'normal'
        for n in arange(1, len(Ebuttons)):
            Ebuttons[n]['state'] = 'disabled'

    Rions = tk.Radiobutton(Labelframe, text="Ion Scattering", variable=modevar, value=0, width=18, command=lambda i=i :changemode())
338
    Rions.pack()
339
    Rrrna = tk.Radiobutton(Labelframe, text="Resonant RNA", variable=modevar, value=1, width=18, command=lambda i=i :changemode2())
340 341
    Rrrna.pack()

342
##############################################################################
343
# Init
344

345
    Rions.invoke()
346
    RDra.invoke()
347
    RS.select()
348
    Ebuttons[0].invoke()
349

350
##############################################################################
351