File: spsamview.py

package info (click to toggle)
sixpack 0.57-4
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k
  • size: 1,272 kB
  • ctags: 587
  • sloc: python: 10,074; makefile: 63
file content (1467 lines) | stat: -rw-r--r-- 60,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
# Data Viewer

import xasinput
from spdirvars import *
from spglobalfuncs import *
from Numeric import *
from Tkinter import *
import Pmw
import Tix
import tkFileDialog
import tkMessageBox
import tkSimpleDialog
import os
import math
from string import *
import MLab
from Scientific.Functions.LeastSquares import leastSquaresFit

#root=Tix.Tk()
#root.title("SamView")
#Pmw.initialise(root)
if os.name=='nt':os.sep='/'
filepath=os.getcwd()+os.sep
#global lastsavedir,lastreaddir
#lastsavedir=''
#lastreaddir=''


#deatime model   SCA=kappa*ICR*exp(-ICR*tau) tau in usecs
def dteqn(pars,point):
    #return pars[0]*point*exp(-pars[1]/point)
    return pars[2]+pars[0]*point*exp(-pars[1]*point*1e-6)
   

###############################################################
##
## Main Program Class
##
###############################################################

class Main:
    global starte,ende
    global edge
    global lastsavedir,lastreaddir
    def __init__(self,master,root):
        self.vwww=vwww=master
        self.root=root
        #define scrollable frame
        if root.winfo_screenheight()<860:
            self.sf=Pmw.ScrolledFrame(vwww,usehullsize=1,hull_width=800,hull_height=720)
            self.vw=vw=self.sf.interior()
            self.sf.pack(side=TOP)
        else:
            self.vw=vw=vwww
        #GUI for data viewer window
        menubar=Pmw.MenuBar(vw)
        #file menu
        menubar.addmenu('File','')
        menubar.addmenuitem('File','command',label='Open Dataset',command=self.loaddatafile)
        menubar.addmenuitem('File','command',label='Open Multiple Datasets',command=self.loadmultdatafile)
        menubar.addmenuitem('File','separator')    
        menubar.addmenuitem('File','command',label='Clear Datasets',command=self.clearfilelist)
        menubar.addmenuitem('File','separator')
        menubar.addmenuitem('File','command',label='Edit Default Directories',command=self.callchangedefdirs)
        menubar.addmenuitem('File','separator')
        menubar.addmenuitem('File','command',label='Close',command=self.closemod)        
        menubar.addmenuitem('File','command',label='Quit',command=self.root.quit)
        menubar.addmenu('Copy','')
        menubar.addmenuitem('Copy','command',label='Copy Plot Data to Clipboard',command=self.graphtextport) 
        menubar.addmenu('Save','')
        menubar.addmenuitem('Save','command',label='Averaged Spectra',command=self.averagescans)
        menubar.addmenu('CSXAS','')
        menubar.addmenuitem('CSXAS','command',label='Change Data Spacing',command=self.changespacing)
        menubar.addmenu('Comments','')    
        menubar.addmenuitem('Comments','command',label='View File Comments',command=self.viewcomments)
        menubar.addmenu('FluoChans','')
        menubar.addmenuitem('FluoChans','command',label='Set all fluo channels to current',command=self.setallfluochannels)
        menubar.addmenuitem('FluoChans','command',label='Reset all fluo channels',command=self.resetallfluochannels)
        menubar.addmenu('Gains','')
        menubar.addmenuitem('Gains','command',label='Change I0 Gain',command=self.editi0gain)
        menubar.addmenu('Help','',side=RIGHT)
        menubar.addmenuitem('Help','command',label='About SixPack',command=self.callprogramabout)
        menubar.grid(row=0,columnspan=25,sticky=W+E)
        #Add project manager window
        pmwin=Frame(vw,relief=SUNKEN,bd=2)
        l=Label(pmwin,text='Project Files',relief=RAISED,bd=2)
        l.pack(side=TOP,fill=X,padx=2,pady=4)
        act=Frame(pmwin)
        w=16
        b=Button(act,text="Add File",width=w,command=self.loaddatafile,bg='steel blue',fg='white')
        b.pack(side=LEFT,padx=2,pady=2)
        b=Button(act,text="Add Many Files",width=w,command=self.loadmultdatafile,bg='darkblue',fg='white')
        b.pack(side=LEFT,padx=2,pady=2)
        act.pack(side=TOP,fill=X,padx=2)
        act=Frame(pmwin)
        b=Button(act,text="Remove File",width=w,command=self.removefile,bg='red',fg='white')
        b.pack(side=LEFT,padx=2,pady=2)
        b=Button(act,text="Clear All",width=w,command=self.clearfilelist,bg='brown',fg='white')
        b.pack(side=LEFT,padx=2,pady=2)    
        act.pack(side=TOP,fill=X,padx=2)
        #Add btree
        tree=Tix.ScrolledHList(pmwin)
        self.phlist=tree.hlist
        self.phlist.config(separator='-',width=25, drawbranch=0, indent=10,itemtype=Tix.TEXT,browsecmd=self.selectfile)
        tree.pack(side=TOP,fill=BOTH,expand=1,padx=2,pady=2)
        #Add average button
        b=Button(pmwin,text="Average Scans",command=self.averagescans,bg='darkgreen',fg='snow')
        b.pack(side=BOTTOM,fill=X,padx=2,pady=2)
        pmwin.grid(row=1,column=0,columnspan=5,rowspan=15,sticky=W+E+N+S)
        #Add graph window
        grwin=Frame(vw,relief=SUNKEN,bd=2)
        self.graph=Pmw.Blt.Graph(grwin,plotbackground='white')
        self.graph.bind(sequence="<ButtonPress>",   func=self.mouseDown)
        self.graph.bind(sequence="<ButtonRelease>", func=self.mouseUp  )
        self.graph.bind(sequence="<Motion>", func=self.coordreport)
        self.graph.pack(expand=1,fill='both')
        grwin.grid(row=1,column=5,columnspan=20,rowspan=15,sticky=W+E+N+S)
        #Add E0 Calibration Window
        e0win=Frame(vw,relief=SUNKEN,bd=2)
        l=Label(e0win,text='E0 Calibration',relief=RAISED,bd=2)
        l.pack(side=TOP,fill=X,padx=2,pady=2)
        f1=Frame(e0win)
        f1.pack(side=TOP,padx=5,pady=2)
# point to proper location for images (CUS)
#        b=Button(f1,text='prev',bitmap="@"+filepath+"xbms"+os.sep+"previous.xbm")
        b=Button(f1,text='prev',bitmap="@"+os.sep+"usr"+os.sep+"share"+os.sep+"SIXpack"+os.sep+"previous.xbm")
        b.bind("<Button-1>",self.e0moveline)
        b.pack(side=LEFT,padx=2,pady=5)
        b=Button(f1,text="Find Data E0",width=20,command=self.findenot,bg='darkblue',fg='snow')
        b.pack(side=LEFT,padx=5,pady=5)
# point to proper location for images (CUS)
#        b=Button(f1,text='next',bitmap="@"+filepath+"xbms"+os.sep+"next.xbm")
        b=Button(f1,text='next',bitmap="@"+os.sep+"usr"+os.sep+"share"+os.sep+"SIXpack"+os.sep+"next.xbm")
        b.bind("<Button-1>",self.e0moveline)
        b.pack(side=LEFT,padx=2,pady=5)
        self.e0app=Pmw.EntryField(e0win,labelpos='w',label_text='Apparent: ',value='0',validate='real')
        self.e0act=Pmw.EntryField(e0win,labelpos='w',label_text='Actual: ',value='0',validate='real',command=self.enotcalcshift)
        self.e0shift=Pmw.EntryField(e0win,labelpos='w',label_text='Shift: ',value='0',validate='real')
        e0fields=(self.e0app,self.e0act,self.e0shift)
        for fld in e0fields:
            fld.pack(side=TOP,fill=X,padx=10,pady=2)
        Pmw.alignlabels(e0fields)
        b=Button(e0win,text="Apply Shift to Data",width=20,command=self.enotapplyshift,bg='brown',fg='snow')
        b.pack(side=TOP,padx=2,pady=5)
        e0win.grid(row=16,column=0,columnspan=5,rowspan=6,sticky=W+E+N+S)    
        #Add Deatime Correction Window
        dtwin=Frame(vw,relief=SUNKEN,bd=2)
        l=Label(dtwin,text='Deadtime Corrections',relief=RAISED,bd=2)
        l.pack(side=TOP,fill=X,padx=2,pady=2)
        self.dodt=IntVar()
        cb=Checkbutton(dtwin,text="Do the deatime correction?",variable=self.dodt,anchor=W,command=self.dodeadtime)
        cb.pack(side=TOP,padx=2,pady=2)
        self.dtfile=Tix.FileEntry(dtwin,label="Deadtime File: ")
        self.dtfile.pack(side=TOP,padx=2,pady=2)
        b=Button(dtwin,text="New DT file",width=20,command=self.getnewdtfile,bg='darkseagreen4',fg='ivory')
        b.pack(side=TOP,padx=2,pady=5)
        dtwin.grid(row=22,column=0,columnspan=5,rowspan=6,sticky=W+E+N+S)    
        #Add Plot control window
        plotwin=Frame(vw,relief=SUNKEN,bd=2)
        l=Label(plotwin,text='Plot Control',relief=RAISED,bd=2)
        l.pack(side=TOP,fill=X,padx=2,pady=2)
        lhs=Frame(plotwin)
        rhs=Frame(plotwin)
        lhs.pack(side=LEFT,fill=BOTH,padx=1,pady=1)
        rhs.pack(side=LEFT,fill=BOTH,padx=1,pady=1)
        self.plotlist=('muF','muT1','muT2','Column')
        self.plottype=Pmw.ScrolledListBox(lhs,items=self.plotlist,labelpos=N,label_text='Plot Type',
                                        listbox_height=4,selectioncommand=self.selectnewplot,
                                        vscrollmode='static',listbox_exportselection=0)
        self.plottype.select_set(0)
        self.plottype.pack(side=TOP,fill=X,padx=2,pady=5)
        b=Button(lhs,text="Stack All Plots?",width=15,command=self.stackplot,bg='darkblue',fg='snow')
        b.pack(side=TOP,padx=2,pady=3)
        self.derivlist=('None','First','Second')
        self.deriv=Pmw.RadioSelect(lhs,labelpos=N,label_text='Derivative',buttontype='radiobutton',orient='vertical',pady=1)
        for but in self.derivlist:
            self.deriv.add(but)
        self.deriv.invoke('None')    
        self.deriv.pack(side=TOP,fill=X,padx=2,pady=30)      
        self.collist=('i0','i1','i2','iFsum','iFind','iFall','ICRind','ICRall')
        self.plotcol=Pmw.ScrolledListBox(rhs,items=self.collist,labelpos=N,label_text='Column Selection',
                                       listbox_height=6,selectioncommand=self.selectnewcolumn,
                                       vscrollmode='static',listbox_exportselection=0,
                                       listbox_selectbackground='grey',listbox_fg='grey')
        self.plotcol.select_set(0)
        self.plotcol.pack(side=TOP,fill=X,padx=2,pady=5)
        bb=Pmw.ButtonBox(rhs,orient='vertical',pady=5)
        bb.pack(side=TOP,padx=2,pady=20)
        bb.add("Replot",width=15,command=self.makeplot,bg='darkgreen',fg='ivory')
        bb.add("Rescale",width=15,command=self.rescaleplot,bg='brown',fg='ivory')
        self.smoothpts=Pmw.Counter(rhs,labelpos=N,label_text='# Pts to Smooth',entryfield_value=0,
                                   entryfield_validate={'validator':'integer','min':0},datatype='integer',
                                   entryfield_entry_width=2)
        self.smoothpts.pack(side=TOP,padx=2,pady=2)
        plotwin.grid(row=16,column=5,columnspan=10,rowspan=12,sticky=W+E+N+S)    
        #Add Fluorescence control window
        flwin=Frame(vw,relief=SUNKEN,bd=2)
        l=Label(flwin,text='Fluorescence Channels',relief=RAISED,bd=2)
        l.pack(side=TOP,fill=X,padx=2,pady=2)
        tree=Tix.ScrolledHList(flwin)
        self.fhlist=tree.hlist
        self.fhlist.config(separator='-',width=25, drawbranch=1, indent=30,itemtype=Tix.TEXT,browsecmd=self.makeplot)
        tree.pack(side=TOP,fill=BOTH,expand=1,padx=2,pady=2)
        bb=Pmw.ButtonBox(flwin,orient='horizontal',pady=5)
        bb.pack(side=TOP,padx=2,pady=5)
        bb.add("Zero Sel. Chan.",width=15,command=self.deleteffchan,bg='darkorange4',fg='snow')
        bb.add("Reload Channels",width=15,command=self.restoreffchan,bg='goldenrod',fg='snow')
        flwin.grid(row=16,column=15,columnspan=5,rowspan=12,sticky=W+E+N+S)    
        #Status Bar
        self.status=Label(vw,text="",bd=2,relief=RAISED,anchor=W,fg='blue')
        self.status.grid(row=30,columnspan=15,sticky=W+E)
        setstatus(self.status,"Ready")
        self.xcoord=Label(vw,text="X=      ",bd=2,relief=RIDGE,anchor=W,fg='red')
        self.ycoord=Label(vw,text="Y=      ",bd=2,relief=RIDGE,anchor=W,fg='red')
        self.xcoord.bind("<Double-Button-1>",self.opencoordwin)
        self.ycoord.bind("<Double-Button-1>",self.opencoordwin)
        self.xcoord.grid(row=30,column=15,columnspan=2,sticky=W+E)
        self.ycoord.grid(row=30,column=17,columnspan=2,sticky=W+E)
        #Coordinates window
        self.coordwin=Pmw.Dialog(vw,title='SV Coord')
        self.winxcoord=Label(self.coordwin.interior(),text="X=      ",bd=2,relief=RIDGE,anchor=W,fg='red')
        self.winycoord=Label(self.coordwin.interior(),text="Y=      ",bd=2,relief=RIDGE,anchor=W,fg='red')
        self.winxcoord.pack(side=LEFT,fill=X,padx=2,pady=2)
        self.winycoord.pack(side=LEFT,fill=X,padx=2,pady=2)
        self.coordwin.iconname('SV Coord')
        self.coordwin.withdraw()        
        #init new vars
        self.datfiles=[]
        self.datvars={}
        self.ffchan={}
        self.icrchan={}
        self.dtcvar={}
        self.plotstk=0
        self.curmax=0
        self.maxima=[]
        self.zoomstack=[]
        self.fluochannelsremoved={}

    def opencoordwin(self,args):
        self.coordwin.deiconify()

    def closemod(self):
        self.root.deiconify()
        self.vw.destroy()

    def viewcomments(self):
        #get data
        cur=self.phlist.info_selection()
        if len(cur)!=0:
            #show box
            self.dialog.activate()
        
    def selectnewplot(self):
        cur=self.plottype.getcurselection()[0]
        if cur=='Column':
            #return defaults
            self.plotcol.configure(listbox_selectbackground='midnight blue',listbox_fg='black')
        else:
            #turn colors to "off"
            self.plotcol.configure(listbox_selectbackground='grey',listbox_fg='grey')
        self.rescaleplot()

    def selectnewcolumn(self):
        cur=self.plottype.getcurselection()[0]
        if cur=='Column':
            self.rescaleplot()
            
    def clearfilelist(self):
        #reset all
        xasinput.PsCLEAR()
        self.datfiles=[]
        self.fflist=[]
        self.datvars={}
        self.ffchan={}
        self.fluochannelsremoved={}
        self.phlist.delete_all()
        self.fhlist.delete_all()
        #clear graph
        glist=self.graph.element_names()
        if glist != ():
            for g in glist:
                self.graph.element_delete(g)        

    def removefile(self):
        #clear graph
        glist=self.graph.element_names()
        if glist != ():
            for g in glist:
                self.graph.element_delete(g)
        #get current selection
        cur=self.phlist.info_selection()
        if cur != ():
            cur=cur[0]            
            #remove all associated vars
            ind=self.datfiles.index(cur)
            self.datfiles.remove(cur)
            self.phlist.delete_entry(cur)
            del self.datvars[cur]
            del self.fluochannelsremoved[cur]
            #reselect
            if len(self.datfiles)==0:
                self.fhlist.delete_all()
            elif ind != 0:
                new=self.datfiles[ind-1]
                self.phlist.selection_set(new)
                self.selectfile()            
            else:
                new=self.datfiles[0]
                self.phlist.selection_set(new)
                self.selectfile()            

    def loadmultdatafile(self):
        global lastsavedir,lastreaddir
        self.multifd=Tix.ExFileSelectDialog(self.root)
        if lastreaddir!='':
            lastreaddir=replace(lastreaddir,'/','\\')            
            self.multifd.fsbox.configure(directory=lastreaddir)
        self.multifd.fsbox.filelist.listbox.configure(selectmode=MULTIPLE)
        #setup ok button...
        self.multifd.fsbox.ok.configure(command=self.getmultilist)        
        self.multifd.popup()

    def getmultilist(self):
        global lastsavedir,lastreaddir
        multfn=self.multifd.fsbox.filelist.listbox.curselection()
        multdlist=self.multifd.fsbox.filelist.listbox.get(0,END)
        curdir=self.multifd.fsbox.dir.entry.get()
        lastreaddir=curdir
        self.multifd.popdown()
        if multfn !=():
            for sel in multfn:
                curfil=multdlist[int(sel)]
                fulpath=curdir+os.sep+curfil
                #get file data
                f=xasinput.XASget(fulpath,self.vw)
                if len(f.eV)==0:return
                f.restore=tuple(reshape(f.iF,(-1,)))
                f.restoreshape=shape(f.iF)
                self.datfiles.append(curfil)
                self.datvars.update({curfil:f})
                self.fluochannelsremoved.update({curfil:[]})
                self.phlist.add(curfil,text=curfil,state=NORMAL)
                self.phlist.selection_clear()
                self.phlist.selection_set(curfil)
                #call selection routine
                self.selectfile()                
                
    def loaddatafile(self):
        global lastsavedir,lastreaddir
        infile=ask_for_file([("all files","*"),("data files","*.dat")],lastreaddir)
        if infile !='':
            indir=rfind(infile,os.sep)
            lastreaddir=infile[:indir]
            #get file data
            f=xasinput.XASget(infile,self.vw)
            if len(f.eV)==0:return
            f.restore=tuple(reshape(f.iF,(-1,)))
            f.restoreshape=shape(f.iF)
            #append lists/update tree            
            treefile=trimdir(infile)        
            self.datfiles.append(treefile)
            self.datvars.update({treefile:f})
            self.fluochannelsremoved.update({treefile:[]})
            self.phlist.add(treefile,text=treefile,state=NORMAL)
            self.phlist.selection_clear()
            self.phlist.selection_set(treefile)
            #call selection routine
            self.selectfile()

    def selectfile(self, *arg):
        #get data
        cur=self.phlist.info_selection()[0]
        dat=self.datvars.get(cur)
        #update status
        npts=len(dat.i0)
        setstatus(self.status,"File "+cur+" contains "+str(npts)+" data points")
        #comment box
        self.dialog=Pmw.TextDialog(self.vw,title='File Comments',defaultbutton=0,scrolledtext_usehullsize=1,
                                   scrolledtext_hull_width=500,scrolledtext_hull_height=100)
        self.dialog.withdraw()
        self.dialog.clear()
        if dat.type[0]=='S':
            self.dialog.insert('end',dat.comment)
        else:
            self.dialog.insert('end','No comments available')
        #update fluobox
        #delete all children
        self.fhlist.delete_all()
        self.ffchan.clear()
        self.icrchan.clear()
        #add rootlevel
        self.fhlist.add(cur,text=cur+" SUM",state=NORMAL)
        #add children
        self.fflist=[]
        nfc=shape(dat.iF)
        if shape(nfc)[0]==1:
            #only one FF channel
            self.fhlist.add(cur+'-FF0',text='FF1',state=NORMAL)
            self.ffchan.update({cur+'-FF0':dat.iF})
            self.ffchan.update({cur:dat.iF})
            self.icrchan.update({cur:dat.ICR})
            self.icrchan.update({cur+'-FF0':dat.ICR})
            self.fflist.append('FF0')
        else:
            #multiple FF channels
            ncols=nfc[1]
            i=0
            while i<ncols:
                self.fflist.append('FF'+str(i+1))
                i=i+1
            i=0
            for ch in self.fflist:
                self.fhlist.add(cur+'-'+ch,text=ch,state=NORMAL)
                self.ffchan.update({cur+'-'+ch:dat.iF[:,i]})
                self.icrchan.update({cur+'-'+ch:dat.ICR[:,i]})
                i=i+1
            self.ffchan.update({cur:self.sumFFdata(dat.iF,dat.ICR,dat.rtc)})
        self.fhlist.selection_set(cur)
        #call plotting routine
        if self.plotstk!=-1:
            self.rescaleplot()

    def setallfluochannels(self):
        cur=self.phlist.info_selection()[0]
        masterlist=self.fluochannelsremoved.get(cur)
        for nextfil in self.datfiles:
            self.phlist.selection_clear()
            self.phlist.selection_set(nextfil)
            self.selectfile()
            #reset
            self.restoreffchan()
            #delete chans
            for fullkey in masterlist:
                goneind=rfind(fullkey,'-')
                goneend=fullkey[goneind:]
                gone=nextfil+goneend
                self.deletingchannelmain(gone,nextfil)
        self.phlist.selection_clear()
        self.phlist.selection_set(cur)
        self.selectfile()            

    def resetallfluochannels(self):
        cur=self.phlist.info_selection()[0]
        for nextfil in self.datfiles:
            self.phlist.selection_clear()
            self.phlist.selection_set(nextfil)
            self.selectfile()
            self.restoreffchan()
        self.phlist.selection_clear()
        self.phlist.selection_set(cur)
        self.selectfile()        

    def editi0gain(self):
        #get data
        cur=self.phlist.info_selection()[0]
        dat=self.datvars.get(cur)
        #get factor
        factor=tkSimpleDialog.askfloat('Gains','Multiply i0 by what factor?',parent=self.vw,initialvalue=1.0)
        dat.i0=dat.i0*factor
        self.selectfile()

    def deleteffchan(self):
        chdel=self.fhlist.info_selection()[0]
        cur=self.phlist.info_selection()[0]
        byebye=self.fluochannelsremoved.get(cur)
        byebye.append(chdel)
        self.fluochannelsremoved.update({cur:byebye})
        self.deletingchannelmain(chdel,cur)

    def deletingchannelmain(self,chdel,cur):        
        if chdel != cur:
            dat=self.datvars.get(cur)        
            #if dat.iF==dat.restore:print 'ok'
            ind=rfind(chdel,'F')+1
            cnum=int(chdel[ind:])-1
            #zero channel out
            z=zeros(shape(self.ffchan[chdel]),Float32)
            self.ffchan.update({chdel:z})
            #zero it in iF too
            npts=shape(dat.iF)[0]
            if shape(shape(dat.iF))[0] != 1:
                i=0
                while i<npts:
                    dat.iF[i,cnum]=0
                    i=i+1
                self.ffchan.update({cur:self.sumFFdata(dat.iF,dat.ICR,dat.rtc)})
            else:
                dat.iF=z
                self.ffchan.update({cur:z})
            #update lists
            #if dat.iF==dat.restore:print 'come again'
            self.datvars.update({cur:dat})
            #if dat.iF==dat.restore:print 'huh'
            #replot
            self.makeplot()
        
    def restoreffchan(self):
        cur=self.phlist.info_selection()[0]
        dat=self.datvars.get(cur)
        self.fluochannelsremoved.update({cur:[]})
        #if dat.iF==dat.restore: print 'what?'
        dat.iF=reshape(array(dat.restore),dat.restoreshape)
        self.selectfile()

    def rescaleplot(self):
        self.graph.xaxis_configure(min="",max="")
        self.graph.yaxis_configure(min="",max="")
        self.zoomstack=[]
        self.makeplot()

    def stackplot(self):
        self.plotstk=0
        #remember curent selection
        if self.phlist.info_selection()==():
            return
        if self.plottype.getcurselection()[0]=='Column':
            if self.plotcol.getcurselection()[0]=='iFall' or self.plotcol.getcurselection()[0]=='ICRall':
                return
        temp=self.phlist.info_selection()[0]
        #plot the first one
        #select first file
        self.phlist.selection_clear()
        self.phlist.selection_set(self.datfiles[0])
        self.selectfile()
        #plot the rest
        for nextfil in self.datfiles:
            if nextfil!=self.datfiles[0]:
                self.plotstk=self.plotstk+1
                self.phlist.selection_clear()
                self.phlist.selection_set(nextfil)
                self.selectfile()
        #restore selection
        self.plotstk=-1
        self.phlist.selection_clear()
        self.phlist.selection_set(temp)
        self.selectfile()
        self.plotstk=0        
       
    def makeplot(self, *args):
        #update graph
        #first remove current plot(s) if not stacking
        if self.plotstk==0:
            glist=self.graph.element_names()
            if glist != ():
                for g in glist:
                    self.graph.element_delete(g)
        if self.phlist.info_selection()==():
            return
        #get data
        colors=['red4','red','orange','yellow4','peru','darkseagreen4','green','darkgreen','cyan','steel blue','blue','blue violet','purple4']
        cur=self.phlist.info_selection()[0]
        dat=self.datvars.get(cur)
        #determine plot type
        pt=self.plottype.getcurselection()[0]
        mf=0
        endtext=cur[len(cur)-7:]
        if endtext[4:]=='dat':
            lt=endtext[:3]
        else:
            lt=endtext
        tt=pt
        if pt=='muF':
            #mu fluo-sum
            xd=tuple(dat.eV)
            yd=tuple(self.ffchan.get(cur)/dat.i0)
        if pt=='muT1':
            #mu I1/I0
            xd=tuple(dat.eV)
            yd=tuple(log(dat.i0/dat.i1))
        if pt=='muT2':
            #mu I2/I1
            xd=tuple(dat.eV)
            yd=tuple(log(dat.i1/dat.i2))
        if pt=='Column':
            #go to column selection
            ct=self.plotcol.getcurselection()[0]
            tt=ct
            if ct=='i0':
                #plot i0/rtc
                xd=tuple(dat.eV)
                yd=tuple(dat.i0/dat.rtc)
            if ct=='i1':
                #plot i1/rtc
                xd=tuple(dat.eV)
                yd=tuple(dat.i1/dat.rtc)
            if ct=='i2':
                #plot i2/rtc
                xd=tuple(dat.eV)
                yd=tuple(dat.i2/dat.rtc)
            if ct=='iFsum':
                #plot iFsum/rtc
                xd=tuple(dat.eV)
                yd=tuple(self.ffchan.get(cur)/dat.rtc)
            if ct=='iFall':
                #plot all iFF's/rtc
                mf=1
                xd=tuple(dat.eV)
                clen=len(colors)
                c=0
                for ch in self.fflist:
                    try:
                        tau=self.dtcvar[ch]
                    except:
                        tau=0
                    icrhz=self.icrchan[cur+'-'+ch]/dat.rtc
                    sca=self.ffchan.get(cur+'-'+ch)*exp(1e-6*tau*icrhz)
                    yd=tuple(sca/dat.rtc)
                    self.graph.line_create(ch,xdata=xd,ydata=yd,symbol='',color=colors[int(fmod(c,clen))])
                    c=c+1                
                self.graph.configure(title='All iF')
            if ct=='iFind':
                #plot FFselected/rtc
                xd=tuple(dat.eV)
                ffsel=self.fhlist.info_selection()[0]
                if ffsel != cur:
                    try:
                        tau=self.dtcvar[ffsel[rfind(ffsel,'-')+1:]]
                    except:
                        tau=0
                    icrhz=self.icrchan[ffsel]/dat.rtc
                    sca=self.ffchan.get(ffsel)*exp(1e-6*tau*icrhz)
                else:
                    sca=self.ffchan.get(ffsel)
                yd=tuple(sca/dat.rtc)
            if ct=='ICRind':
                #plot ICRselected/rtc
                xd=tuple(dat.eV)
                ffsel=self.fhlist.info_selection()[0]
                if ffsel==cur:
                    mf=1
                else:
                    yd=tuple(self.icrchan.get(ffsel)/dat.rtc)
            if ct=='ICRall':
                mf=1
                #plot all ICR channels/rtc
                xd=tuple(dat.eV)
                clen=len(colors)
                c=0
                for ch in self.fflist:
                    yd=tuple(self.icrchan.get(cur+'-'+ch)/dat.rtc)
                    self.graph.line_create('ICR'+ch[2:],xdata=xd,ydata=yd,symbol='',color=colors[int(fmod(c,clen))])
                    c=c+1                
                self.graph.configure(title='All ICR')
        if mf==0:
            #NOW... calculate derivative if needed!
            dsel=self.deriv.getcurselection()
            if dsel=='None':
                pxd=xd
                pyd=yd
            if dsel=='First':
                new=self.getderiv(xd,yd)
                pyd=tuple(new[1])
                pxd=tuple(new[0])
            if dsel=='Second':
                new1=self.getderiv(xd,yd)
                new=self.getderiv(new1[0],new1[1])
                pyd=tuple(new[1])
                pxd=tuple(new[0])
            numsmpts=int(self.smoothpts.get())
            pyd=tuple(self.datasmooth(pyd,numsmpts))
            if self.plotstk==0:
                pc='blue'
            else:
                pc=colors[int(fmod(self.plotstk,len(colors)))]
            try:
                self.graph.line_create(lt,xdata=pxd,ydata=pyd,symbol='',color=pc)
            except:
                lt=str(self.plotstk)+lt
                self.graph.line_create(lt,xdata=pxd,ydata=pyd,symbol='',color=pc)
            self.graph.configure(title=tt)

    def graphtextport(self):
        #export all data in tab delimited text to clipboard
        text=''
        datay=[]
        datax=[]
        allnames=self.graph.element_names()
        print allnames
        for name in allnames:
            temp=self.graph.element_configure(name,'xdata')
            datax.append(temp[4])
            temp=self.graph.element_configure(name,'ydata')
            datay.append(temp[4])
            text=text+name+'\t\t\t'
        text=text+'\n'
        #parse list now
        pdatax=[]
        pdatay=[]
        alllen=[]
        maxlen=0
        for i in range(len(datax)):
            temp=datax[i]
            pdatax.append(split(temp))
            temp=datay[i]
            temp2=split(temp)
            if maxlen<len(temp2):maxlen=len(temp2)
            alllen.append(len(temp2))
            pdatay.append(temp2)
        #setup text
        for j in range(maxlen):
            for i in range(len(pdatax)):
                if j<alllen[i]:
                    text=text+pdatax[i][j]+'\t'+pdatay[i][j]+'\t\t'
                else:
                    text=text+'\t\t\t'
            text=text+'\n'
        #export to clipboard
        self.root.clipboard_clear()
        self.root.clipboard_append(text)
        setstatus(self.status,"Graph data copied to clipboard")
                
    def getderiv(self,x,y):
        #returns deriv in ans(x,y)
        ans=[]
        difx=MLab.diff(x,n=1)
        dify=MLab.diff(y,n=1)
        ans.append(self.dxdata(x))
        ans.append(dify/difx)
        return ans

    def dxdata(self,x):
        i=0
        avgd=[]
        while i<len(x)-1:
            avgd.append((x[i]+x[i+1])/2)
            i=i+1
        return avgd

    def datasmooth(self,x,numpts):
        if numpts==0:
            return x
        else:
            i=0
            smoo=[]
            while i<len(x):
                if i<numpts:
                    sr=range(0,i+numpts+1,1)
                elif i>len(x)-numpts-1:
                    sr=range(i-numpts,len(x),1)
                else:
                    sr=range(i-numpts,i+numpts+1,1)
                temp=0
                for ind in sr:
                    temp=temp+x[ind]
                smoo.append(temp/len(sr))
                i=i+1
            return smoo

    def coordreport(self,event):
        (x,y)=self.graph.invtransform(event.x,event.y)
        xtext="X="+str(x)
        ytext="Y="+str(y)
        xtext=xtext[:12]
        ytext=ytext[:12]
        setstatus(self.xcoord,xtext)
        setstatus(self.ycoord,ytext)
        setstatus(self.winxcoord,xtext)
        setstatus(self.winycoord,ytext)
    
    def zoom(self, x0, y0, x1, y1):
        #add last to zoomstack
        a0=self.graph.xaxis_cget("min")
        a1=self.graph.xaxis_cget("max")
        b0=self.graph.yaxis_cget("min")
        b1=self.graph.yaxis_cget("max")        
        self.zoomstack.append((a0,a1,b0,b1))
        #configure
        self.graph.xaxis_configure(min=x0, max=x1)
        self.graph.yaxis_configure(min=y0, max=y1)

    def unzoom(self):
        #get last off stack
        if self.zoomstack==[]:
            return
        limit=self.zoomstack.pop()
        self.graph.xaxis_configure(min=limit[0],max=limit[1])
        self.graph.yaxis_configure(min=limit[2],max=limit[3])

    def mouseDrag(self,event):
        global x0, y0, x1, y1, druged
        druged = 1
        (x1, y1) = self.graph.invtransform(event.x, event.y)             
        self.graph.marker_configure("marking rectangle", 
            coords = (x0, y0, x1, y0, x1, y1, x0, y1, x0, y0))
        self.coordreport(event)
    
    def mouseUp(self,event):
        global dragging, druged
        global x0, y0, x1, y1        
        if dragging:
            self.graph.unbind(sequence="<Motion>")
            self.graph.bind(sequence="<Motion>", func=self.coordreport)
            self.graph.marker_delete("marking rectangle")           
            if event.num==1 and druged:
                if x0 <> x1 and y0 <> y1:   
                    # make sure the coordinates are sorted
                    if x0 > x1: x0, x1 = x1, x0
                    if y0 > y1: y0, y1 = y1, y0         
                    self.zoom(x0, y0, x1, y1) # zoom in
            else:
                self.unzoom() # zoom out
                           
    def mouseDown(self,event):
        global dragging, druged, x0, y0
        dragging = 0
        druged = 0
        if self.graph.inside(event.x, event.y):
            dragging = 1
            (x0, y0) = self.graph.invtransform(event.x, event.y)
            self.graph.marker_create("line", name="marking rectangle", dashes=(2, 2))
            self.graph.bind(sequence="<Motion>",  func=self.mouseDrag)        

    def averagescans(self):
        global lastsavedir,lastreaddir
        #average all active scans
        allscans=list(self.phlist.info_children())      
        nscans=len(allscans)
        #check lengths for each file 
        lenlist=[]
        for d in allscans:
            lenlist.append(len(self.datvars[d].i0))
        match=1
        maxlen=max(lenlist)
        maxlenind=lenlist.index(maxlen)
        for m in lenlist:
            if m != lenlist[0]:match=0
        if match==0:
            #ask if truncation, full averaging, or cancel of averaging is desired...
            mtext="Files not of equal length! \nTruncate, Continue or Cancel? \n"
            lengtherror=Pmw.MessageDialog(self.vw,message_text=mtext,buttonboxpos='s',iconpos='w',icon_bitmap='warning',
                                          buttons=('Truncate','Continue','Cancel'),defaultbutton='Cancel')
            lengtherror.iconname=('Averaging Error')
            lengthtype=lengtherror.activate()
            if lengthtype=='Cancel':return
            mtext="WARNING: All files MUST have same intial energy spacings! \n Continue?"
            lengthwarning=Pmw.MessageDialog(self.vw,message_text=mtext,buttonboxpos='s',iconpos='w',icon_bitmap='warning',
                                          buttons=('Continue','Cancel'),defaultbutton='Cancel')
            lengthwarning.iconname=('Averaging Error')
            lengthwtype=lengthwarning.activate()
            if lengthwtype=='Cancel':return
            #tkMessageBox.showwarning(title="Averaging Error",parent=self.vw,
            #                         message='Not implemented yet')
            #return
        if self.plottype.getcurselection()[0]=='Column':
            #trap for no selection of average type
            tkMessageBox.showwarning(title="Averaging Error",parent=self.vw,
                                     message='Please select mu type to average')
            return
        #do averaging all files same length
        if match==1:
            total=[]
            if self.plottype.getcurselection()[0]=='muF':
                atype='_fl'
                #put all data in a list...
                fchnlist=[]
                for d in allscans:
                    #calcualte sum
                    fdat=self.datvars[d].iF
                    nfc=shape(fdat)
                    if shape(nfc)[0]==1:
                        #only one FF channel
                        fchnlist.append(fdat)
                    else:
                        #multiple FF channels
                        fchnlist.append(self.sumFFdata(fdat,self.datvars[d].ICR,self.datvars[d].rtc))
                for pt in range(lenlist[0]):
                    temp=0
                    for d in allscans:
                        ind=allscans.index(d)
                        tiFsum=fchnlist[ind]
                        temp=temp+(tiFsum[pt]/self.datvars[d].i0[pt])
                    total.append(temp/nscans)
            if self.plottype.getcurselection()[0]=='muT1':
                atype='_tr'
                for pt in range(lenlist[0]):
                    temp=0
                    for d in allscans:
                        temp=temp+log(self.datvars[d].i0[pt]/self.datvars[d].i1[pt])
                    total.append(temp/nscans)
            if self.plottype.getcurselection()[0]=='muT2':
                atype='_t2'
                for pt in range(lenlist[0]):
                    temp=0
                    for d in allscans:
                        temp=temp+log(self.datvars[d].i1[pt]/self.datvars[d].i2[pt])
                    total.append(temp/nscans)
            energy=self.datvars[allscans[0]].eV
            total=array(total,Float32)
        else: #average scans for unequal lengths -- didn't want to mess with what worked above.
            #choose eV from longest scan
            energy=self.datvars[allscans[maxlenind]].eV
            temp=zeros((len(energy),len(allscans)),Float32)
            if self.plottype.getcurselection()[0]=='muF':
                atype='_fl'
                #put all data in a list...
                fchnlist=[]
                for d in allscans:
                    #calcualte sum
                    fdat=self.datvars[d].iF
                    nfc=shape(fdat)
                    if shape(nfc)[0]==1:
                        #only one FF channel
                        fchnlist.append(fdat)
                    else:
                        #multiple FF channels
                        fchnlist.append(self.sumFFdata(fdat,self.datvars[d].ICR,self.datvars[d].rtc))
                #for pt in range(lenlist[0]):
                for d in allscans:
                    for pt in range(len(self.datvars[d].i0)):
                        ind=allscans.index(d)
                        tiFsum=fchnlist[ind]
                        temp[pt,ind]=(tiFsum[pt]/self.datvars[d].i0[pt])
            if self.plottype.getcurselection()[0]=='muT1':
                atype='_tr'
                for d in allscans:
                    for pt in range(len(self.datvars[d].i0)):
                        temp[pt,allscans.index(d)]=log(self.datvars[d].i0[pt]/self.datvars[d].i1[pt])
            if self.plottype.getcurselection()[0]=='muT2':
                atype='_t2'
                for d in allscans:
                    for pt in range(len(self.datvars[d].i0)):
                        temp[pt,allscans.index(d)]=log(self.datvars[d].i0[pt]/self.datvars[d].i1[pt])
            #compile array
            noelems=[]
            for pt in range(len(energy)):
                i=0
                for d in allscans:
                    if temp[pt,allscans.index(d)]!=0:i=i+1
                noelems.append(i)
            #add columns
            temp=sum(temp,1)
            #divide by noelems
            total=temp/noelems
            #deal with truncation/averaging
            if lengthtype=='Truncate':
                minlen=min(lenlist)
                tlen=tkSimpleDialog.askinteger('Averaging','Truncate to which point?',parent=self.vw,initialvalue=minlen)
                if tlen==None:return
                energy=energy[0:tlen]
                total=total[0:tlen]
        #now plot the average
        #first remove current plot(s)
        glist=self.graph.element_names()
        if glist != ():
            for g in glist:
                self.graph.element_delete(g)
        self.graph.line_create('avg',xdata=tuple(energy),ydata=tuple(total),symbol='',color='blue')        
        #ask to save file...
        sf=tkMessageBox.askyesno(title='Continue',parent=self.vw,message='Save this average?')
        if sf:
            default=trimdirext(allscans[0])+atype+".avg"                
            fname=ask_save_file(default,lastsavedir)
            if fname != '':
                sdir=rfind(fname,os.sep)
                lastsavedir=fname[:sdir]
                #save two column ascii w/ brief header
                fid=open(fname,"w")
                fid.write("# Averaged Data "+fname+" created by SamView\n")
                try:
                    for pt in range(tlen): #lenlist[0]):
                        fid.write(str(energy[pt])+"\t"+str(total[pt])+"\n")
                except:
                    for pt in range(lenlist[0]):
                        fid.write(str(energy[pt])+"\t"+str(total[pt])+"\n")
                fid.close()

    def findenot(self):
        #find all local maxima of data at current derivative level
        dsel=self.deriv.getcurselection()
        #get data
        cur=self.phlist.info_selection()[0]
        dat=self.datvars.get(cur)
        #determine plot type
        pt=self.plottype.getcurselection()[0]
        xd=tuple(dat.eV)
        if pt=='muF':
            #mu fluo-sum
            yd=tuple(self.ffchan.get(cur)/dat.i0)
        if pt=='muT1':
            #mu I1/I0
            yd=tuple(log(dat.i0/dat.i1))
        if pt=='muT2':
            #mu I2/I1
            yd=tuple(log(dat.i1/dat.i2))
        if dsel=='None':
            #take first deriv
            new=self.getderiv(xd,yd)
            pyd=tuple(new[1])
            pxd=tuple(new[0])
        if dsel=='First':
            #take second deriv
            new1=self.getderiv(xd,yd)
            new=self.getderiv(new1[0],new1[1])
            pyd=tuple(new[1])
            pxd=tuple(new[0])
        if dsel=='Second':
            #take yet another deriv
            new2=self.getderiv(xd,yd)
            new1=self.getderiv(new2[0],new2[1])
            new=self.getderiv(new1[0],new1[1])
            pyd=tuple(new[1])
            pxd=tuple(new[0])
        numsmpts=int(self.smoothpts.get())
        pyd=tuple(self.datasmooth(pyd,numsmpts))        
        #find all zeros in pyd
        self.curmax=self.getalllocalmax(pxd,pyd)
        #add lines to plot
        self.drawe0lines()

    def e0moveline(self,event):
        dir=event.widget.cget('text')
        if self.maxima != []:
            if dir=='prev':self.curmax=self.curmax-1
            if dir=='next':self.curmax=self.curmax+1
            if self.curmax<0:self.curmax=len(self.maxima)-1
            if self.curmax>len(self.maxima)-1:self.curmax=0
            self.drawe0lines()

    def drawe0lines(self):
        enot=self.maxima[self.curmax]
        entry_replace_d(self.e0app,enot,5)
        if float(self.e0act.get()) != 0:self.enotcalcshift()
        #add lines to plot
        self.makeplot()
        (dmin,dmax)=self.graph.yaxis_limits()
        ind=0
        for ex in self.maxima:
            ind=ind+1
            if ex==self.maxima[self.curmax]:
                self.graph.line_create('e0',xdata=(ex,ex),ydata=(dmin,dmax),symbol='',color='red')              
            else:
                self.graph.line_create('max'+str(ind),xdata=(ex,ex),ydata=(dmin,dmax),symbol='',color='green')              
                
    def getalllocalmax(self,xd,yd):
        self.maxima=[]
        maxin=0
        maxdel=0
        s=1 #positive
        ind=0
        for p in yd:
            if ind>1 and ind<len(yd)-2:
                #compare to last point and next point... (make sure its not noise)
                sn=1
                if p<0:sn=0
                if s==1 and sn==0 and yd[ind-2]>0 and yd[ind+1]<0:
                    #cross a zero as a maxima
                    #calculate average of last two points
                    xcross=(xd[ind]+xd[ind-1])/2
                    #only collect first 10...
                    if len(self.maxima)<10:
                        self.maxima.append(xcross)
                        #save the index of the biggest...
                        delta=yd[ind-1]-yd[ind]
                        if delta>maxdel:
                            maxdel=delta
                            maxin=len(self.maxima)-1
                s=sn
            ind=ind+1
        return maxin

    def enotcalcshift(self):
        #calculate shift on entry
        app=float(self.e0app.get())
        act=float(self.e0act.get())
        shift=act-app
        entry_replace_d(self.e0shift,shift,4)

    def enotapplyshift(self):
        #apply energy shift
        #get data
        cur=self.phlist.info_selection()[0]
        dat=self.datvars.get(cur) 
        dat.eV=dat.eV+float(self.e0shift.get())
        self.makeplot()

    def changespacing(self):
        global chespcing,chkstart,chkspcing,chedge
        #routine to average CSXAS data points...
        chespcing=1
        chkstart=2
        chkspcing=0.05
        chedge=float(self.e0act.get())        
        #ask to do all data sets same?
        #get data
        cur=self.phlist.info_selection()[0]
        dat=self.datvars.get(cur)
        #need to know... e spacing, begin k, k spacing, edge
        chespcing=tkSimpleDialog.askfloat('Input for CS-XAS Change','Enter energy spacing (eV)',initialvalue=chespcing,parent=self.vw)
        chkstart=tkSimpleDialog.askfloat('Input for CS-XAS Change','Enter start of k range',initialvalue=chkstart,parent=self.vw)           
        chkspcing=tkSimpleDialog.askfloat('Input for CS-XAS Change','Enter k-spacing',initialvalue=chkspcing,parent=self.vw)
        chedge=tkSimpleDialog.askfloat('Input for CS-XAS Change','Enter edge (eV)',initialvalue=chedge,parent=self.vw)
        #if edge is 0 or not valid, then exit
        if chedge==0 or None in (chespcing,chkstart,chkspcing,chedge):
            tkMessageBox.showerror(title='CSXAS Operation Cancelled',parent=self.vw,message='Invalid Entries')
            return
        #start at first E...        
        datadelE=dat.eV[1]-dat.eV[0]
        #if spacing is wrong...
        if chespcing<abs(datadelE):
            tkMessageBox.showerror(title='CSXAS Operation Cancelled',parent=self.vw,message='Energy spacing is less than original data')
            return            
        #init new arrays
        # dat.rtc = clock normalizing counts
        # dat.eV, dat.i0, dat.i1, dat.i2, dat.iF, dat.ICR
        neweV=[]
        newi0=[]
        newi1=[]
        newi2=[]
        newiF=[]
        newICR=[]
        newrtc=[]
        #set markers for k-space
        kst=chkstart-chkspcing/2
        kestart=(kst/0.51233)**2+chedge
        curk=chkstart-chkspcing
        inkland=0
        #begin da rebin loop...
        lastind=len(dat.eV)
        firstind=0
        firste=dat.eV[firstind]
        nexte=firste+chespcing
        #find next index
        for i in range(firstind,lastind):
            if dat.eV[i]<nexte:
                nextind=i
        while nextind<lastind:
            #set up arrays
            trtc=dat.rtc[firstind:nextind+1]
            ti0=dat.i0[firstind:nextind+1]
            ti1=dat.i1[firstind:nextind+1]
            ti2=dat.i2[firstind:nextind+1]
            tif=dat.iF[firstind:nextind+1]
            ticr=dat.ICR[firstind:nextind+1]
            #add values to arrays
            newi0.append(sum(ti0))
            newi1.append(sum(ti1))
            newi2.append(sum(ti2))
            newiF.append(sum(tif))
            newICR.append(sum(ticr))            
            newrtc.append(sum(trtc))
            #new eV
            tev=(firste+nexte)/2
            neweV.append(tev)
            #advance to next point
            if nexte<kestart:
                firste=nexte
                nexte=nexte+chespcing
                firstind=nextind
            else:
                #in k-space land
                curk=curk+chkspcing
                firste=((curk-(chkspcing/2))/0.51233)**2+chedge
                nexte=((curk+(chkspcing/2))/0.51233)**2+chedge
                if inkland==0:
                    for i in range(0,lastind):
                        if dat.eV[i]<firste:
                            firstind=i
                else:
                    firstind=nextind
                inkland=1                    
            #find next index
            for i in range(firstind,lastind):
                if dat.eV[i]<nexte:
                    nextind=i
                if nextind==lastind-1:
                    nextind=lastind
        #now replace data...
        dat.eV=array(neweV)
        dat.i0=array(newi0)
        dat.i1=array(newi1)
        dat.i2=array(newi2)
        dat.iF=array(newiF)
        dat.ICR=array(newICR)
        dat.rtc=array(newrtc)
        #redraw...
        self.rescaleplot()
        print 'done'

    def dodeadtime(self):
        #do deattime corrections
        #check to see if file is present
        action=0
        dtf=self.dtfile.entry.get()
        if dtf=='':
            #put up message box
            tkMessageBox.showwarning(title="Deadtime Error",parent=self.vw,
                                     message='Choose deadtime correction file first')
            #reselect no dt
            self.dodt.set(0)
        else:
            if self.dodt.get()==1:
                #apply deadtimes
                #read first line of file -- make sure its valid
                fid=open(dtf,'r')
                lines=fid.read().split('\n')
                test=split(lines[0])
                if test[0] in ('FF','7','9'):
                    #read in channel values 
                    action=1
                    self.dtcvar={}
                    i=1
                    for line in lines:
                        if line[:2] not in ('FF',''):
                            self.dtcvar.update({'FF'+str(i):float(split(line)[1])})
                            i=i+1
                else:
                    tkMessageBox.showwarning(title="Deadtime Error",parent=self.vw,
                                             message='Invalid Deadtime File')
            else:
                #remove deadtimes
                action=1
                self.dtcvar={}
            #apply corrections to current data by reloading all
            if action==1:
                allscans=self.phlist.info_children()
                if allscans != ():
                    cur=self.phlist.info_selection()[0]
                    for d in allscans:
                        self.phlist.selection_clear()
                        self.phlist.selection_set(d)
                        self.selectfile()
                    self.phlist.selection_clear()
                    self.phlist.selection_set(cur)
                    self.selectfile()

    def sumFFdata(self,FF,ICR,rtc):
        #calculate a sum
        nfc=shape(FF)
        if shape(nfc)[0]==1:
            #only one FF channel
            try:
                tau=self.dtcvar['FF1']
            except:
                tau=0
            return FF*exp(1e-6*tau*ICR/rtc)
        else:
            #multiple FF channels
            ncols=nfc[1]
            temp=zeros(nfc[0])
            i=0
            while i<ncols:
                try:
                    tau=self.dtcvar['FF'+str(i+1)]
                except:
                    tau=0
                temp=temp+FF[:,i]*exp(1e-6*tau*ICR[:,i]/rtc)
                i=i+1
            return array(temp)
                        
    def getnewdtfile(self):
        GetNewDTFile(self.dtfile.entry,self.root)

    def callprogramabout(self):
        programabout(self.root)

    def callchangedefdirs(self):
        global lastsavedir,lastreaddir
        s=changedefdirs(self.vw)
        lastreaddir=s.newdir
        lastsavedir=s.newdir

class GetNewDTFile:
    def __init__(self,placement,root):
        self.root=root
        #start new deatime fitting procedure
        dt=self.root.dt=Toplevel(self.root)
        self.dt=dt
        dt.title("Deadtimes Fitting Dialog")
        dt.focus_set()
        menubar=Pmw.MenuBar(dt)
        #file menu
        menubar.addmenu('File','')
        menubar.addmenuitem('File','command',label='Open DT Dataset',command=self.loaddts)
        menubar.addmenuitem('File','separator')    
        menubar.addmenuitem('File','command',label='Fit Dataset',command=self.fitdt)
        menubar.addmenuitem('File','separator')
        menubar.addmenuitem('File','command',label='Close',command=self.root.dt.destroy)
        menubar.addmenu('Save','')
        menubar.addmenuitem('Save','command',label='Save Deadtime Fits',command=self.savefitresults)
        menubar.addmenu('Help','',side=RIGHT)
        menubar.addmenuitem('Help','command',label='About SamView',command=programabout)
        menubar.grid(row=0,columnspan=25,sticky=W+E)
        #file/FF control window
        fcwin=Frame(dt,relief=SUNKEN,bd=2)
        l=Label(fcwin,text='Deadtime Channels',relief=RAISED,bd=2)
        l.pack(side=TOP,fill=X,padx=2,pady=4)
        b=Button(fcwin,text="Load File",command=self.loaddts,bg='brown',fg='snow')
        b.pack(side=TOP,padx=20,pady=2,fill=X)
        b=Button(fcwin,text="Fit Deadtimes",command=self.fitdt,bg='darkgreen',fg='snow')
        b.pack(side=TOP,padx=20,pady=2,fill=X)  
        #Add btree
        tree=Tix.ScrolledHList(fcwin)
        self.dfhlist=tree.hlist
        self.dfhlist.config(separator='-',width=25, drawbranch=1, indent=30,itemtype=Tix.TEXT,browsecmd=self.plotdt)
        tree.pack(side=TOP,fill=BOTH,expand=1,padx=2,pady=2)
        #add ICR cutoff
        self.icrcutoff=Pmw.EntryField(fcwin,labelpos='n',label_text='ICR Cutoff Hz',value='400000',validate='numeric',command=self.reloaddts)
        self.icrcutoff.pack(side=BOTTOM,fill=X,padx=4,pady=2)
        self.icrbotoff=Pmw.EntryField(fcwin,labelpos='n',label_text='ICR Bottom Hz',value='0',validate='numeric',command=self.reloaddts)
        self.icrbotoff.pack(side=BOTTOM,fill=X,padx=4,pady=2)
        fcwin.grid(row=1,columnspan=5,rowspan=20,sticky=N+S+W+E)
        #Add graph window
        dgrwin=Frame(dt,relief=SUNKEN,bd=2)
        self.dgraph=Pmw.Blt.Graph(dgrwin,plotbackground='white')
        self.dgraph.xaxis_configure(title='ICR')
        self.dgraph.yaxis_configure(title='SCA')
        self.dgraph.pack(expand=1,fill='both')
        dgrwin.grid(row=1,column=5,columnspan=15,rowspan=15,sticky=W+E+N+S)                   
        #Add results window
        frwin=Frame(dt,relief=SUNKEN,bd=2)
        frwin.grid(row=16,column=5,columnspan=15,rowspan=5,sticky=W+E+N+S)
        l=Label(frwin,text='Fitting Results',relief=RAISED,bd=2)
        l.pack(side=TOP,fill=X,padx=2,pady=4)
        b=Button(frwin,text="Save\nDT\nFit\n",command=self.savefitresults,bg='darkorange4',fg='snow')
        b.pack(side=LEFT,fill=Y,padx=2,pady=10)
        self.restext=Pmw.ScrolledText(frwin,hscrollmode='none',vscrollmode='static',borderframe=1,
                                    text_font="Courier 9",usehullsize=1,hull_width=400,hull_height=150)
        self.restext.configure(text_state=DISABLED)
        self.restext.pack(fill='both',padx=2,pady=4)
        #init vars
        self.dffchan={}
        self.dicrchan={}
        self.dtfits={}
        self.dtfitvars={}
        self.dfflist=[]
        self.plc=placement
        
    def loaddts(self):
        global lastsavedir,lastreaddir
        #load deadtime file
        self.infile=ask_for_file([("all files","*")],lastreaddir)
        self.reloaddts()

    def reloaddts(self):        
        global lastsavedir,lastreaddir
        if self.infile !='':
            indir=rfind(self.infile,os.sep)
            lastreaddir=self.infile[:indir]
            #get file data
            dat=self.dat=xasinput.XASget(self.infile,self.dt)
            self.root.dt.focus_set()
            if len(dat.eV)==0:return
            #append lists/update tree            
            self.dfhlist.delete_all()
            cur=self.datfile=trimdir(self.infile)        
            self.dfhlist.add(cur,text=cur,state=NORMAL)
            self.dfhlist.selection_clear()
            self.dfhlist.selection_set(cur)
            #clear then load FF chans
            self.dffchan={}
            self.dicrchan={}
            self.dfflist=[]
            self.dtfits={}
            self.dtfitvars={}
            #add children
            nfc=shape(dat.iF)
            if shape(nfc)[0]==1:
                #only one FF channel
                self.dfhlist.add(cur+'-FF0',text='FF1',state=NORMAL)
                #edit for ICR cutoff
                tempf=[]
                tempi=[]
                cutoff=0
                for i in range(len(dat.iF)):
                    if dat.ICR[i]<int(self.icrcutoff.get()) and dat.ICR[i]>int(self.icrbotoff.get()) and cutoff==0:
                        tempf.append(dat.iF[i])
                        tempi.append(dat.ICR[i])
                    if dat.ICR[i]>int(self.icrcutoff.get()): cutoff=1
                dat.iF=array(tempf)
                dat.ICR=array(tempi)
                self.dffchan.update({cur+'-FF0':dat.iF})
                self.dicrchan.update({cur+'-FF0':dat.ICR})
                self.dfflist.append('FF0')
            else:
                #multiple FF channels
                ncols=nfc[1]
                i=0
                while i<ncols:
                    self.dfflist.append('FF'+str(i+1))
                    i=i+1
                i=0
                for ch in self.dfflist:
                    #edit for ICR cutoff
                    a=dat.iF[:,i]
                    b=dat.ICR[:,i]
                    tempf=[]
                    tempi=[]
                    cutoff=0
                    for j in range(len(a)):
                        if b[j]<int(self.icrcutoff.get()) and b[j]>int(self.icrbotoff.get()) and cutoff==0:
                            tempf.append(a[j])
                            tempi.append(b[j])
                        if b[j]>int(self.icrcutoff.get()): cutoff=1
                    a=array(tempf)
                    b=array(tempi)
                    self.dfhlist.add(cur+'-'+ch,text=ch,state=NORMAL)
                    self.dffchan.update({cur+'-'+ch:a})
                    self.dicrchan.update({cur+'-'+ch:b})
                    i=i+1
            self.dfhlist.selection_set(cur)
            self.plotdt()

    def plotdt(self, *arg):
        #plot dt SCA vs ICR and fits if present
        #first remove all plot elements
        glist=self.dgraph.element_names()
        if glist != ():
            for g in glist:
                self.dgraph.element_delete(g)
        #now get currently chosen element
        chan=self.dfhlist.info_selection()[0]
        #make plot of data
        if chan != self.datfile:
            flt=chan[rfind(chan,'-')+1:]
            self.dgraph.line_create(flt,xdata=tuple(self.dicrchan[chan]),ydata=tuple(self.dffchan[chan]),symbol='circle',color='blue',pixels=".05i",linewidth=0)
            #make plot of fit if it exists
            try:
                self.dgraph.line_create('Fit',xdata=tuple(self.dicrchan[chan]),ydata=tuple(self.dtfits[chan]),symbol='',color='red')        
            except:
                pass
        
    def fitdt(self):
        #fit the deadtime curves!  use SCA=kappa*ICR*exp(-ICR*tau) taus in usec
        self.dtfits={} #clear data fits
        self.dtfitvars={} #clear fit params
        cur=self.datfile
        self.restext.configure(text_state=NORMAL)
        self.restext.clear()
        self.restext.insert(END,'FF\ttau\t\toffset\t\tkappa\n')
        self.restext.configure(text_state=DISABLED)
        for ch in self.dfflist:
            #construct fitting parameters
            initguess=(0.5,1,0)  #kappa,tau(usec)
            passdata=[]
            for i in range(len(self.dffchan[cur+'-'+ch])):
                passdata.append((self.dicrchan[cur+'-'+ch][i],self.dffchan[cur+'-'+ch][i]))
            try:
                result=leastSquaresFit(dteqn,initguess,passdata)
                fp=result[0]
            except:
                fp=(0,0,0)
            self.dtfitvars.update({cur+'-'+ch:fp})
            f=dteqn(fp,self.dicrchan[cur+'-'+ch])
            self.dtfits.update({cur+'-'+ch:f})
            self.restext.configure(text_state=NORMAL)
            sca=ch[2:]
            self.restext.insert(END,sca+'\t'+str(fp[1])+'\t'+str(fp[2])+'\t'+str(fp[0])+'\n')
            self.restext.configure(text_state=DISABLED)
            self.restext.update_idletasks()
        self.plotdt()

    def savefitresults(self):
        global lastsavedir,lastreaddir
        #save fit window
        default="deadtime.dat"                
        savedname=ask_save_file(default,lastsavedir)
        self.root.dt.focus_set()
        if savedname != '':
            sdir=rfind(savedname,os.sep)
            lastsavedir=savedname[:sdir]
            self.restext.exportfile(savedname)
            self.plc.delete(0,END)
            self.plc.insert(END,savedname)
     
#start event handler
#Main(root)
#root.mainloop()