source: trunk/addcmds.tcl @ 447

Last change on this file since 447 was 447, checked in by toby, 14 years ago

# on 2001/09/25 23:25:21, toby did:
add comments
improve error message display & try to show all exptool errors
dummy histogram support

  • Property rcs:author set to toby
  • Property rcs:date set to 2001/09/25 23:25:21
  • Property rcs:lines set to +350 -56
  • Property rcs:rev set to 1.22
  • Property rcs:state set to Exp
  • Property svn:keywords set to Author Date Revision Id
File size: 61.4 KB
Line 
1# $Id: addcmds.tcl 447 2009-12-04 23:06:19Z toby $
2
3#----------- Add Phase routines ----------------------------------------
4
5proc MakeAddPhaseBox {} {
6    global expmap expgui
7
8    set expgui(coordList) {}
9    set nextphase ""
10    foreach p {1 2 3 4 5 6 7 8 9} {
11        if {[lsearch $expmap(phaselist) $p] == -1} {
12            set nextphase $p
13            break
14        }
15    }
16
17    # no more room
18    if {$nextphase == ""} {
19        MyMessageBox -parent . -title "Add Phase Error" \
20                -message "There are already 9 phases. You cannot add more." \
21                -icon error
22        return
23    }
24
25    set np .newphase
26    catch {destroy $np}
27    toplevel $np
28    bind $np <Key-F1> "MakeWWWHelp expgui2.html addphase"
29
30    grid [label $np.l1 -text "Adding phase #$nextphase"] \
31            -column 0 -row 0 -sticky w
32    grid [label $np.l2 -text "Phase title:"] -column 0 -row 1 
33    grid [entry $np.t1 -width 68] -column 1 -row 1 -columnspan 8
34    grid [label $np.l3 -text "Space Group:"] -column 0 -row 2 
35    grid [entry $np.t2 -width 12] -column 1 -row 2 
36    grid [frame $np.f -bd 4 -relief groove] -column 3 -row 2 -columnspan 8
37    set col -1
38    foreach i {a b c} {
39        grid [label $np.f.l1$i -text " $i "] -column [incr col] -row 1
40        grid [entry $np.f.e1$i -width 12] -column [incr col]  -row 1
41    }
42    set col -1
43    foreach i {a b g} {
44        grid [label $np.f.l2$i -text $i] -column [incr col] -row 2
45        set font [$np.f.l2$i cget -font]
46        $np.f.l2$i config -font "Symbol [lrange $font 1 end]"
47        grid [entry $np.f.e2$i -width 12] -column [incr col]  -row 2
48        $np.f.e2$i insert 0 90.
49    }   
50   
51    grid [frame $np.bf] -row 3 -column 0 -columnspan 10 -sticky ew
52    grid [button $np.bf.b1 -text Add \
53            -command "addphase $np"] -column 2 -row 3
54    bind $np <Return> "addphase $np"
55    grid [button $np.bf.b2 -text Cancel \
56            -command "destroy $np"] -column 3 -row 3
57    grid columnconfig $np.bf 4 -weight 1
58    grid [button $np.bf.help -text Help -bg yellow \
59            -command "MakeWWWHelp expgui2.html addphase"] \
60            -column 4 -row 3
61
62    # get the input formats if not already defined
63    GetImportFormats
64    if {[llength $expgui(importFormatList)] > 0} {
65        grid [frame $np.bf.fr -bd 4 -relief groove] -column 5 -row 3
66        grid [button $np.bf.fr.b3 -text "Import phase from: " \
67                -command "ImportPhase \$expgui(importFormat) $np"] \
68                -column 0 -row 0 -sticky e
69        eval tk_optionMenu $np.bf.fr.b4 expgui(importFormat) \
70                $expgui(importFormatList)
71        grid $np.bf.fr.b4 -column 1 -row 0 -sticky w
72        grid rowconfig $np.bf.fr 0 -pad 10
73        grid columnconfig $np.bf.fr 0 -pad 10
74        grid columnconfig $np.bf.fr 1 -pad 10
75    }
76    wm title $np "add new phase"
77
78    # set grab, etc.
79    putontop $np
80   
81    tkwait window $np
82   
83    # fix grab...
84    afterputontop
85}
86
87proc addphase {np} {
88    global expgui expmap
89    # validate the input
90    set err {}
91    set title [$np.t1 get]
92    if {[string trim $title] == ""} {
93        append err "  Title cannot be blank\n"
94    }
95    set spg [$np.t2 get]
96    if {[string trim $spg] == ""} {
97        append err "  Space group cannot be blank\n"
98    }
99    foreach i {a b c} {
100        set cell($i) [$np.f.e1$i get]
101        if {[string trim $cell($i)] == ""} {
102            append err "  $i cannot be blank\n"
103        } elseif {[catch {expr $cell($i)}]} {
104            append err "  $i is not valid\n"
105        }
106    }
107
108    foreach i {a b g} lbl {alpha beta gamma} {
109        set cell($lbl) [$np.f.e2$i get]
110        if {[string trim $cell($lbl)] == ""} {
111            append err "  $lbl cannot be blank\n"
112        } elseif {[catch {expr $cell($lbl)}]} {
113            append err "  $lbl is not valid\n"
114        }
115    }
116
117    if {$err != ""} {
118        MyMessageBox -parent . -title "Add Phase Error" \
119                -message "The following error(s) were found in your input:\n$err" \
120                -icon error
121        set expgui(oldphaselist) -1
122        return
123    }
124
125    # check the space group
126    set fp [open spg.in w]
127    puts $fp "N"
128    puts $fp "N"
129    puts $fp $spg
130    puts $fp "Q"
131    close $fp
132    global tcl_platform
133    catch {
134        if {$tcl_platform(platform) == "windows"} {
135            exec [file join $expgui(gsasexe) spcgroup.exe] < spg.in >& spg.out
136        } else {
137            exec [file join $expgui(gsasexe) spcgroup] < spg.in >& spg.out
138        }
139    }
140    set fp [open spg.out r]
141    set out [read $fp]
142    close $fp
143    # attempt to parse out the output (fix up if parse did not work)
144    if {[regexp "space group symbol.*>(.*)Enter a new space group symbol" \
145            $out a b ] != 1} {set b $out}
146    if {[string first Error $b] != -1} {
147        # got an error, show it
148        ShowBigMessage \
149                 $np.error \
150                 "Error processing space group\nReview error message below" \
151                 $b OK "" 1
152        set expgui(oldphaselist) -1
153        return
154    } else {
155        # show the result and confirm
156        set opt [ShowBigMessage \
157                $np.check \
158                "Check the symmetry operators in the output below" \
159                $b \
160                {Continue Redo} ]
161        if {$opt > 1} {
162            set expgui(oldphaselist) -1
163            return
164        }
165    }
166    file delete spg.in spg.out
167   
168    # ok do it!
169    set fp [open exptool.in w]
170    puts $fp "P"
171    puts $fp $title
172    puts $fp $spg
173    puts $fp "$cell(a) $cell(b) $cell(c) $cell(alpha) $cell(beta) $cell(gamma)"
174    puts $fp "/"
175    close $fp
176    global tcl_platform
177    # Save the current exp file
178    savearchiveexp
179    # disable the file changed monitor
180    set expgui(expModifiedLast) 0
181    set expnam [file root [file tail $expgui(expfile)]]
182    # save the previous phase list
183    set expgui(oldphaselist) $expmap(phaselist)
184    catch {
185        if {$tcl_platform(platform) == "windows"} {
186            exec [file join $expgui(gsasexe) exptool.exe] $expnam \
187                    < exptool.in >& exptool.out
188        } else {
189            exec [file join $expgui(gsasexe) exptool] $expnam \
190                    < exptool.in >& exptool.out
191        }
192    } errmsg
193    # load the revised exp file
194    set oldphaselist $expmap(phaselist)
195    loadexp $expgui(expfile)
196    set fp [open exptool.out r]
197    set out [read $fp]
198    close $fp
199    destroy $np
200    set err 0
201    if {[llength $oldphaselist] == [llength $expmap(phaselist)]} {set err 1}
202    if {$errmsg != ""} {
203        set err 1
204        append errmsg "\n" $out
205    } else {
206        set errmsg $out
207    }
208    if {$expgui(showexptool) || $err} {
209        set msg "Please review the result from adding the phase" 
210        if {$err} {append msg "\nIt appears an error occurred!"}
211        ShowBigMessage $np $msg $errmsg OK "" $err
212    }
213    file delete exptool.in exptool.out
214    # now select the new phase
215    SelectOnePhase [lindex $expmap(phaselist) end]   
216}
217
218#----------- Add Histogram routines --------------------------------------
219proc MakeAddHistBox {} {
220    global expmap newhist
221
222    # --> should check here if room for another histogram, but only texture
223    # folks will ever need that
224
225    set np .newhist
226    catch {destroy $np}
227    toplevel $np
228    bind $np <Key-F1> "MakeWWWHelp expgui3.html AddHist"
229
230    grid [label $np.l0 -text "Adding a new histogram"] \
231            -column 0 -row 0 -sticky ew -columnspan 7
232    grid [checkbutton $np.d0 -text "Dummy Histogram" -variable newhist(dummy) \
233            -command "PostDummyOpts $np" \
234            ] -column 2 -row 0 -columnspan 99 -sticky e
235    grid [label $np.l1 -text "Data file:"] -column 0 -row 2
236    grid [label $np.t1 -textvariable newhist(rawfile) -bd 2 -relief ridge] \
237            -column 1 -row 2 -columnspan 3 -sticky ew
238    grid [button $np.b1 -text "Select File" \
239            -command "getrawfile $np" \
240            ] -column 4 -row 2
241
242    grid [label $np.lbank -text "Select bank" -anchor w] -column 1 -row 3 -sticky w
243    grid [frame $np.bank]  -column 2 -row 3 -columnspan 7 -sticky ew
244
245    grid [label $np.l2 -text "Instrument\nParameter file:"] -column 0 -row 5
246    grid [label $np.t2 -textvariable newhist(instfile) -bd 2 -relief ridge] \
247            -column 1 -row 5 -columnspan 3 -sticky ew
248    grid [button $np.b2 -text "Select File" \
249            -command "getinstfile $np" \
250            ] -column 4 -row 5
251
252    grid [label $np.lset -text "Select set" -anchor w] -column 1 -row 6 -sticky w
253    grid [frame $np.set]  -column 2 -row 6 -columnspan 7 -sticky ew
254
255    grid [button $np.f6a -text "Run\nRAWPLOT" -command RunRawplot] \
256            -column 4 -row 8 -rowspan 2
257    grid [label $np.l3 -text "Usable data limit:"] -column 0 -row 8 -rowspan 2 
258    grid [entry $np.e3 -width 12 -textvariable newhist(2tLimit) \
259            ] -column 1 -row 8 -rowspan 2 
260    grid [radiobutton $np.cb3 -text "D-min" -variable newhist(LimitMode) \
261            -value 0] -column 2 -row 8 -sticky w
262    grid [radiobutton $np.cb4 -textvariable newhist(limitLbl)  \
263            -variable newhist(LimitMode) -anchor w -justify l \
264            -value 1] -column 2 -row 9 -sticky w
265    set newhist(limitLbl) "TOF-min\n2-Theta Max"
266    # spacers
267    grid [frame $np.sp0 -bg white] \
268            -columnspan 20 -column 0 -row 1 -sticky nsew -ipady 2
269    grid [frame $np.sp1 -bg white] \
270            -columnspan 20 -column 0 -row 4 -sticky nsew -ipady 2
271    grid [frame $np.sp2 -bg white] \
272            -columnspan 20 -column 0 -row 7 -sticky nsew -ipady 2
273    grid [frame $np.sp3 -bg white] \
274            -columnspan 20 -column 0 -row 98 -sticky nsew -ipady 2
275    grid [frame $np.f6] -column 0 -row 99 -columnspan 5 -sticky ew
276    grid [button $np.f6.b6a -text Add \
277            -command "addhist $np"] -column 0 -row 0
278    bind $np <Return> "addhist $np"
279    grid [button $np.f6.b6b -text Cancel \
280            -command "destroy $np"] -column 1 -row 0
281    grid [button $np.f6.help -text Help -bg yellow \
282            -command "MakeWWWHelp expgui3.html AddHist"] \
283            -column 2 -row 0 -sticky e
284    grid columnconfigure $np.f6 2 -weight 1
285    grid columnconfigure $np 3 -weight 1
286
287    # dummy histogram stuff
288    frame $np.d1
289    grid [label $np.d1.l1 -text min] -col 1 -row 1
290    grid [label $np.d1.l2 -text max] -col 2 -row 1
291    grid [label $np.d1.l3 -text step] -col 3 -row 1
292    grid [label $np.d1.lu -text ""] -col 4 -row 1 -rowspan 2
293    grid [entry $np.d1.e1 -width 10 -textvariable newhist(tmin)] -col 1 -row 2
294    grid [entry $np.d1.e2 -width 10 -textvariable newhist(tmax)] -col 2 -row 2
295    grid [entry $np.d1.e3 -width 10 -textvariable newhist(tstep)] -col 3 -row 2
296    grid [label $np.d1.m1 -anchor w] -col 1 -row 3 -sticky ew
297    grid [label $np.d1.m2 -anchor w] -col 2 -row 3 -sticky ew
298    label $np.dl1 -text "Data range:"
299    label $np.dl2 -text "Allowed"
300    label $np.dl3 -text "\n" -justify left -fg blue
301    wm title $np "add new histogram"
302
303    set newhist(banknum) {}
304    set newhist(setnum) {}
305    if {[string trim $newhist(rawfile)] != {}} {
306        validaterawfile $np $newhist(rawfile)
307    }
308    if {[string trim $newhist(instfile)] != {}} {
309        validateinstfile $np $newhist(instfile)
310    }
311
312    PostDummyOpts $np
313    # set grab, etc.
314    putontop $np
315
316    tkwait window $np
317
318    # fix grab...
319    afterputontop
320}
321
322# convert a file to Win-95 direct access
323proc WinCvt {file win} {
324    global expgui
325    if ![file exists $file] {
326        MyMessageBox -parent $win -title "Convert Error" \
327                -message "File $file does not exist" -icon error
328        return
329    }
330
331    set tmpname "[file join [file dirname $file] tempfile.xxx]"
332    set oldname "[file rootname $file].org"
333    if [file exists $oldname] {
334        set ans [MyMessageBox -parent $win -title "OK to overwrite?" \
335                -message "File [file tail $oldname] exists in [file dirname $oldname]. OK to overwrite?" \
336                -icon question -type yesno -default yes]
337        if {$ans == "no"} return
338        catch {file delete $oldname}
339    }
340
341    if [catch {
342        set in [open $file r]
343        # needed to test under UNIX
344        set out [open $tmpname w]
345        fconfigure $out -translation crlf
346        set len [gets $in line]
347        if {$len > 160} {
348            # this is a UNIX file. Hope there are no control characters
349            set i 0
350            set j 79
351            while {$j < $len} {
352                puts $out [string range $line $i $j]
353                incr i 80
354                incr j 80
355            }
356        } else {
357            while {$len >= 0} {
358                append line "                                        "
359                append line "                                        "
360                set line [string range $line 0 79]
361                puts $out $line
362                set len [gets $in line]
363            }
364        }
365        close $in
366        close $out
367        file rename $file $oldname
368        file rename $tmpname $file
369    } errmsg] {
370        MyMessageBox -parent $win -title Notify \
371                -message "Error in conversion:\n$errmsg" -icon warning
372    }
373    return $file
374}
375
376proc getrawfile {np} {
377    global newhist tcl_platform
378    if {$tcl_platform(platform) == "windows"} {
379        set inp [
380        tk_getOpenFile -parent $np -initialfile $newhist(rawfile) -filetypes {
381            {"Data files" .GSAS} {"Data files" .GSA} 
382            {"Data files" .RAW}  {"All files" *}
383        }
384        ]
385    } else {
386        set inp [
387        tk_getOpenFile -parent $np -initialfile $newhist(rawfile) -filetypes {
388            {"Data files" .GSA*} {"Data files" .RAW} 
389            {"Data files" .gsa*} {"Data files" .raw} 
390            {"All files" *}
391        } 
392        ]
393    }
394    validaterawfile $np $inp
395}
396
397proc validaterawfile {np inp} {
398    global expgui newhist
399    if {$inp == ""} return
400    if [catch {set in [open $inp r]}] {
401        MyMessageBox -parent $np -title "Open error" \
402                -message "Unable to open file $inp" -icon error
403        return 
404    }
405    set newhist(banklist) {}
406    foreach child [winfo children $np.bank] {destroy $child}
407    # is this a properly formatted file?
408    # -- are lines the correct length & terminated with a CR-LF?   
409    fconfigure $in -translation lf
410    set i 0
411    while {[set len [gets $in line]] > 0} {
412        incr i
413        if {$len != 81 || [string range $line end end] != "\r"} {
414            set ans [MyMessageBox -parent $np -title "Convert?" \
415                    -message "File $inp is not in the correct format for GSAS.\nOK to convert?" \
416                    -icon warning -type {OK Quit} -default OK]
417            if {$ans == "ok"} {
418                # convert and reopen the file
419                close $in
420                WinCvt $inp $np
421                set i 0
422                set in [open $inp r]
423                fconfigure $in -translation lf
424                set line {}
425            } else {
426                return
427            }
428        }
429        # scan for BANK lines
430        if {[string first BANK $line] == 0} {
431            scan $line "BANK%d" num
432            lappend newhist(banklist) $num
433            # compute last point
434            set tmin 0
435            set tmax 0
436            catch {
437                scan $line "BANK%d%d%d%s%f%f" num nchan nrec rest start step
438                set tmin [expr $start/100.]
439                set tmax [expr ($start + $step*($nchan-1))/100.]
440            }
441            set newhist(tmin$num) $tmin
442            set newhist(tmax$num) $tmax
443        }
444        # check for "Instrument parameter file" line
445        if {$i == 2 && [string first "Instrument parameter" $line] == 0} {
446            validateinstfile $np \
447                    [file join [file dirname $inp] \
448                    [string trim [string range $line 26 end]]]
449        }
450    }
451    # were banks found?
452    if {$newhist(banklist) == ""} {
453        MyMessageBox -parent $np -title "Read error" \
454                -message "File $inp has no BANK lines.\nThis is not a valid GSAS data file." \
455                -icon warning
456        return
457    }
458    # don't use a full path unless needed
459    if {[pwd] == [file dirname $inp]} {
460        set newhist(rawfile) [file tail $inp]
461    } else {
462        set newhist(rawfile) $inp
463    }
464    set row 0
465    set col -1
466    foreach i $newhist(banklist) {
467        if {$col > 8} {
468            set col -1
469            incr row
470        }
471        grid [radiobutton $np.bank.$i -text $i -command SetTmax \
472                -variable newhist(banknum) -value $i] \
473                -column [incr col] -row $row -sticky w
474        # only 1 choice, so set it
475        if {[llength $newhist(banklist)] == 1} {
476            set newhist(banknum) $i
477            SetTmax
478        } else {
479            set newhist(2tLimit) {}
480            set newhist(LimitMode) {}
481        }
482    }
483}
484
485proc SetTmax {} {
486    global newhist
487    set num $newhist(banknum)
488    if {$newhist(insttype) == "TOF"} {
489        set newhist(2tLimit) $newhist(tmin$num)
490        if {[llength $newhist(banklist)] == $newhist(instbanks)} {
491            set newhist(setnum) $newhist(banknum)
492        }
493    } else {
494        set newhist(2tLimit) $newhist(tmax$num)
495    }
496    set newhist(LimitMode) 1
497
498}
499
500proc getinstfile {np} {
501    global newhist tcl_platform
502    if {$tcl_platform(platform) == "windows"} {
503        set inp [
504        tk_getOpenFile -parent $np -initialfile $newhist(instfile) -filetypes {
505            {"Inst files" .INST} {"Inst files" .INS} 
506            {"Inst files" .PRM} {"All files" *}
507        }
508        ]
509    } else {
510        set inp [
511        tk_getOpenFile -parent $np -initialfile $newhist(instfile) -filetypes {
512            {"Inst files" .INS*} {"Inst files" .ins*} 
513            {"Inst files" .PRM}  {"Inst files" .prm} 
514            {"All files" *}
515        }
516        ]
517    }
518    set newhist(setnum) {}
519    validateinstfile $np $inp
520}
521
522proc validateinstfile {np inp} {
523    global expgui newhist
524    if {$inp == ""} return
525    if [catch {set in [open $inp r]}] {
526        MyMessageBox -parent $np -title "Open error" \
527                -message "Unable to open file $inp" -icon error
528        return 
529    }
530    set newhist(instbanks) {}
531    foreach child [winfo children $np.set] {destroy $child}
532    # is this a properly formatted file?
533    # -- are lines the correct length & terminated with a CR-LF?   
534    fconfigure $in -translation lf
535    while {[set len [gets $in line]] > 0} {
536        if {$len != 81 || [string range $line end end] != "\r"} {
537            set ans [MyMessageBox -parent $np -title "Convert?" \
538                    -message "File $inp is not in the correct format for GSAS.\nOK to convert?" \
539                    -icon warning -type {OK Quit} -default OK]
540            if {$ans == "ok"} {
541                # convert and reopen the file
542                close $in
543                WinCvt $inp $np
544                set in [open $inp r]
545                fconfigure $in -translation lf
546                set line {}
547            } else {
548                return
549            }
550        }
551        # scan for the INS   BANK line
552        if {[string first "INS   BANK" $line] == 0} {
553            set newhist(instbanks) \
554                    [string trim [string range $line 12 end]]
555        }
556        # scan for the INS   BANK line
557        if {[string first "INS   HTYPE" $line] == 0} {
558            if {[string index [lindex $line 2] 2] == "T"} {
559                set newhist(insttype) TOF
560            } elseif {[string index [lindex $line 2] 2] == "E"} {
561                set newhist(insttype) ED
562            } else {
563                set newhist(insttype) CW
564            }
565        }
566        # scan for the instrument constants
567        if {[regexp {INS ([ 1-9][0-9]) ICONS(.*)} $line a b c]} {
568            set b [string trim $b]
569            set newhist(inst${b}ICONS) [string trim $c]
570        }
571        if {[regexp {INS ([ 1-9][0-9])I ITYP(.*)} $line a b c]} {
572            set b [string trim $b]
573            set newhist(inst${b}ITYP) [string trim $c]
574        }
575    }
576    # were banks found?
577    if {$newhist(instbanks) == ""} {
578        MyMessageBox -parent $np -title "Read error" -message \
579                "File $inp has no \"INS   BANK\" line.\nThis is not a valid GSAS Instrument Parameter file." \
580                -icon warning
581        return
582    }
583    # don't use a full path unless needed
584    if {[pwd] == [file dirname $inp]} {
585        set newhist(instfile) [file tail $inp]
586    } else {
587        set newhist(instfile) $inp
588    }
589    set col -1
590    set row 0
591    for {set i 1} {$i <= $newhist(instbanks)} {incr i} {
592        if {$col > 8} {
593            set col -1
594            incr row
595        }
596        grid [radiobutton $np.set.$i -text $i \
597                -command "PostDummyOpts $np; ValidateDummyHist $np" \
598                -variable newhist(setnum) -value $i] \
599                -column [incr col] -row $row -sticky w
600        if {$newhist(instbanks) == 1} {set newhist(setnum) $i}
601    }
602    if {$newhist(dummy)} {PostDummyOpts $np; ValidateDummyHist $np}
603}
604
605proc addhist {np} {
606    global expgui newhist tcl_platform expmap
607    if {$newhist(dummy)} {
608        AddDummyHist $np
609        return
610    }
611    # validate the input
612    set err {}
613    if {[string trim $newhist(rawfile)] == ""} {
614        append err "  No data file specified\n"
615    }
616    if {[string trim $newhist(instfile)] == ""} {
617        append err "  No instrument parameter file specified\n"
618    }
619    if {[string trim $newhist(banknum)] == ""} {
620            append err "  Bank number must be specified\n"
621    } elseif {[catch {expr $newhist(banknum)}]} {
622            append err "  Bank number is not valid\n"
623    }
624    if {[string trim $newhist(setnum)] == ""} {
625        append err "  Parameter set number must be specified\n"
626    } elseif {[catch {expr $newhist(setnum)}]} {
627        append err "  Parameter set number is not valid\n"
628    }
629    if {[string trim $newhist(2tLimit)] == ""} {
630        append err "  2Theta/d-space limit must be specified\n"
631    } elseif {[catch {expr $newhist(2tLimit)}]} {
632        append err "  The 2Theta/d-space limit is not valid\n"
633    }
634    if {[string trim $newhist(LimitMode)] == ""} {
635        append err "  Please choose between either a 2Theta or d-space limit\n"
636    }
637
638    if {$err != ""} {
639        MyMessageBox -parent $np -title  "Add Histogram Error" \
640                -message "The following error(s) were found in your input:\n$err" \
641                -icon error -type ok -default ok \
642                -helplink "expgui3.html AddHistErr"
643        return
644    }
645
646    # ok do it!
647    set fp [open exptool.in w]
648    puts $fp "H"
649    if {$tcl_platform(platform) == "windows"} {
650        puts $fp [file attributes $newhist(rawfile) -shortname]
651        puts $fp [file attributes $newhist(instfile) -shortname]
652    } else {
653        puts $fp $newhist(rawfile)
654        puts $fp $newhist(instfile)
655    }
656    puts $fp $newhist(banknum)
657    puts $fp $newhist(setnum)
658    if {$newhist(LimitMode)} {
659        puts $fp "T"
660    } else {
661        puts $fp "D"
662    }
663    puts $fp "$newhist(2tLimit)"
664    puts $fp "/"
665    puts $fp "X"
666    puts $fp "X"
667    close $fp
668    global tcl_platform
669    # Save the current exp file
670    savearchiveexp
671    # disable the file changed monitor
672    set expgui(expModifiedLast) 0
673    set expnam [file root [file tail $expgui(expfile)]]
674    catch {
675        if {$tcl_platform(platform) == "windows"} {
676            exec [file join $expgui(gsasexe) exptool.exe] $expnam \
677                    < exptool.in >& exptool.out
678        } else {
679            exec [file join $expgui(gsasexe) exptool] $expnam \
680                    < exptool.in >& exptool.out
681        }
682    } errmsg
683    # load the revised exp file
684    set oldpowderlist $expmap(powderlist)
685    loadexp $expgui(expfile)
686    set fp [open exptool.out r]
687    set out [read $fp]
688    close $fp
689    destroy $np
690    set err 0
691    if {[llength $oldpowderlist] == [llength $expmap(powderlist)]} {set err 1}
692    if {$errmsg != ""} {
693        append errmsg "\n" $out
694        set err 1
695    } else {
696        set errmsg $out
697    }
698    if {$expgui(showexptool) || $err} {
699        set msg "Please review the result from adding the histogram" 
700        if {$err} {append msg "\nIt appears an error occurred!"}
701        ShowBigMessage $np $msg $errmsg OK "" $err
702    }
703    file delete exptool.in exptool.out
704}
705
706proc RunRawplot {} {
707    global newhist tcl_platform
708    # for Windows put a message on top, in case file names must be shortened
709    if {$tcl_platform(platform) == "windows"} {
710        set f1 {}
711        catch {set f1 [file nativename \
712                    [file attributes $newhist(rawfile) -shortname]]}
713        set f2 {}
714        catch {set f2 [file nativename \
715                [file attributes $newhist(instfile) -shortname]]}
716        if {$f1 != "" || $f2 != ""} {
717            set msg "Note: input to RAWPLOT\n"
718            if {$f1 != ""} {append msg "data file: $f1\n"}
719            if {$f2 != ""} {append msg "instrument file: $f2"}
720            MyMessageBox -icon info -message $msg -parent .
721        }
722    }
723    # start RAWPLOT
724    runGSASprog rawplot 1
725}
726#--- Dummy histogram stuff
727proc PostDummyOpts {np} {
728    global newhist
729    if {$newhist(dummy)} {
730        trace variable newhist(tmin) w "ValidateDummyHist $np"
731        trace variable newhist(tmax) w "ValidateDummyHist $np"
732        trace variable newhist(tstep) w "ValidateDummyHist $np"
733        foreach w {l1 t1 lbank} {
734            $np.$w config -fg grey
735        }
736        $np.b1 config -state disabled
737        grid forget $np.l3 $np.e3 $np.cb3 $np.cb4  $np.bank $np.f6a
738        grid $np.dl1 -column 0 -row 8
739        grid $np.d1 -column 1 -row 8 -rowspan 2 -columnspan 4 -sticky e
740        grid $np.dl3 -column 0 -columnspan 99 -row 10 -sticky ew
741        if {$newhist(insttype) == "TOF"} {
742            $np.dl1 config -text "Data range:\n(TOF)"
743            $np.d1.lu config -text millisec
744            grid $np.dl2 -column 0 -row 9
745            catch {
746                foreach {x tmin tmax x} $newhist(inst${newhist(setnum)}ITYP) {}
747                $np.d1.m1 config -text $tmin
748                $np.d1.m2 config -text $tmax
749            }
750        } elseif {$newhist(insttype) == "CW"} {
751            $np.dl1 config -text "Data range:\n(2Theta)"
752            $np.d1.lu config -text degrees
753            #grid forget $np.dl2
754            $np.d1.m1 config -text >0.
755            $np.d1.m2 config -text <180.
756        } else {
757            $np.dl1 config -text "Data range:\n(Energy)"
758            $np.d1.lu config -text KeV
759            $np.d1.m1 config -text 1.
760            $np.d1.m2 config -text 200.
761            grid $np.dl2 -column 0 -row 9
762        }
763    } else {
764        foreach var {tmin tmax tstep} {
765            foreach v [ trace vinfo newhist($var)] {
766                eval trace vdelete newhist($var) $v
767            }
768        }
769        grid forget $np.dl1 $np.d1 $np.dl2 $np.dl3
770        foreach w {l1 t1 lbank} {
771            $np.$w config -fg black
772        }
773        $np.b1 config -state normal
774        grid $np.bank -column 2 -row 3 -columnspan 7 -sticky ew
775        grid $np.f6a -column 4 -row 8 -rowspan 2
776        grid $np.l3 -column 0 -row 8 -rowspan 2 
777        grid $np.e3 -column 1 -row 8 -rowspan 2 
778        grid $np.cb3 -column 2 -row 8 -sticky w
779        grid $np.cb4 -column 2 -row 9 -sticky w
780     }
781}
782
783proc ValidateDummyHist {np args} {
784    # validate input
785    global newhist
786    set msg {}
787    $np.dl3 config -text "\n"
788    foreach e {e1 e2 e3} v {tmin tmax tstep} {
789        if [catch {expr $newhist($v)}] {
790            $np.d1.$e config -fg red
791            append msg "Value of $newhist($v) is invalid for $v\n"
792        } else {
793            $np.d1.$e config -fg black
794        }
795    }
796    if {[catch {expr $newhist(setnum)}]} {
797        append msg "An instrument file bank number must be selected\n"
798    } elseif {$newhist(setnum) <= 0 || \
799            $newhist(setnum) > $newhist(instbanks)} {
800        append msg "An invalid instrument file bank has been selected\n"
801    }
802
803    if {$msg != ""} {return $msg}
804
805    if {$newhist(tmax) <= $newhist(tmin)} {
806        $np.d1.e1 config -fg red
807        $np.d1.e2 config -fg red
808        return "Tmax <= Tmin\n"
809    }
810
811
812    set dmin -1
813    set dmax -1
814    if {$newhist(insttype) == "TOF"} {
815        catch {
816            foreach {x tmin tmax x} $newhist(inst${newhist(setnum)}ITYP) {}
817            if {$newhist(tmin) <$tmin } {
818                $np.d1.e1 config -fg red
819                append msg "Min value of $newhist(tmin) msec is invalid.\n"
820            }
821            if {$newhist(tmax) >$tmax } {
822                $np.d1.e2 config -fg red
823                append msg "Max value of $newhist(tmax) msec is invalid.\n"
824            }
825            set dmin [expr {1000. * $newhist(tmin) / \
826                    [lindex $newhist(inst${newhist(setnum)}ICONS) 0]}]
827            set dmax [expr {1000. * $newhist(tmax) / \
828                    [lindex $newhist(inst${newhist(setnum)}ICONS) 0]}]
829        }
830    } elseif {$newhist(insttype) == "CW"} {
831        if {$newhist(tmin) <= 0 } {
832            $np.d1.e1 config -fg red
833            append msg "Min value of $newhist(tmin) degrees is invalid.\n"
834        }
835        if {$newhist(tmax) >=180 } {
836            $np.d1.e2 config -fg red
837            append msg "Max value of $newhist(tmax) degrees is invalid.\n"
838        }
839        catch {
840            set dmin [expr {[lindex $newhist(inst${newhist(setnum)}ICONS) 0]\
841                    * 0.5 / sin(acos(0.)*$newhist(tmax)/180.)}]
842            set dmax [expr {[lindex $newhist(inst${newhist(setnum)}ICONS) 0]\
843                    * 0.5 / sin(acos(0.)*$newhist(tmin)/180.)}]
844        }
845    } else {
846        if {$newhist(tmin) <1 } {
847            $np.d1.e1 config -fg red
848            append msg "Min value of $newhist(tmin) KeV is invalid.\n"
849        }
850        if {$newhist(tmax) >200 } {
851            $np.d1.e2 config -fg red
852            append msg "Max value of $newhist(tmax) KeV is invalid.\n"
853        }
854        catch {
855            set ang [lindex $newhist(inst${newhist(setnum)}ICONS) 0]
856            set dmin [expr {12.398/ (2.0*sin($ang*acos(0.)/180) * \
857                    $newhist(tmax))}]
858            set dmax [expr {12.398/ (2.0*sin($ang*acos(0.)/180) * \
859                    $newhist(tmin))}]
860        }
861    }
862    if {$msg != ""} {return $msg}
863    set pnts -1
864    catch {
865        set pnts [expr {1+int(($newhist(tmax) - $newhist(tmin))/$newhist(tstep))}]
866        set qmin [expr {4.*acos(0)/$dmax}]
867        set qmax [expr {4.*acos(0)/$dmin}]
868    }
869    if {$pnts <= 0} {
870        $np.d1.e3 config -fg red
871        append msg "Step value of $newhist(tstep) is invalid.\n"
872    }
873    if {$pnts >20000} {
874        $np.d1.e3 config -fg red
875        append msg "Step value of $newhist(tstep) is too small (>20000 points).\n"
876    }
877    if {$msg != ""} {return $msg}
878    if {$dmin > 0 && $dmax > 0} {
879        catch {
880            set msg [format \
881                    {  %d points.%s  D-space range: %.2f-%.2f A,  Q: %.2f-%.2f/A} \
882                    $pnts "\n" $dmin $dmax $qmin $qmax]
883            $np.dl3 config -text $msg
884        }
885    }
886    if {$msg != ""} {return ""}
887    $np.dl3 config -text [format {  %d points.%s  Range: ?} $pnts "\n"]
888    return "Invalid data range -- something is wrong!"
889}
890
891proc AddDummyHist {np} {
892    global newhist expgui expmap
893    global tcl_platform
894    set msg [ValidateDummyHist $np]
895    if {$msg != ""} {
896        MyMessageBox -parent $np -title  "Add Histogram Error" \
897                -message "The following error(s) were found in your input:\n$msg" \
898                -icon error -type ok -default ok \
899                -helplink "expgui3.html AddHistErr"
900        return
901    }
902    set fp [open exptool.in w]
903    puts $fp "D"
904    puts $fp $newhist(instfile)
905    puts $fp $newhist(setnum)
906    if {$newhist(insttype) == "TOF"} {
907        puts $fp "C"
908    }
909    puts $fp $newhist(tmin)
910    puts $fp $newhist(tmax)
911    puts $fp $newhist(tstep)
912    puts $fp "X"
913    puts $fp "0"
914    close $fp
915    # Save the current exp file
916    savearchiveexp
917    # disable the file changed monitor
918    set expgui(expModifiedLast) 0
919    set expnam [file root [file tail $expgui(expfile)]]
920    set err [catch {
921        if {$tcl_platform(platform) == "windows"} {
922            exec [file join $expgui(gsasexe) exptool.exe] $expnam \
923                    < exptool.in >& exptool.out
924        } else {
925            exec [file join $expgui(gsasexe) exptool] $expnam \
926                    < exptool.in >& exptool.out
927        }
928    } errmsg ]
929    # load the revised exp file
930    set oldpowderlist $expmap(powderlist)
931    loadexp $expgui(expfile)
932    set fp [open exptool.out r]
933    set out [read $fp]
934    close $fp
935    if {[llength $oldpowderlist] == [llength $expmap(powderlist)]} {set err 1}
936    if {$errmsg != ""} {
937        append errmsg "\n" $out
938    } else {
939        set errmsg $out
940    }
941    if {[regexp {\(P,H,A\)} $out]} {
942        set msg {You must upgrade the EXPTOOL program.}
943        append msg { This version cannot add dummy histograms.}
944        MyMessageBox -icon error -title "Old EXPTOOL program" \
945                -message $msg -parent $np \
946                -helplink "expguierr.html OldEXPTOOL"
947        # update the documentation & link
948        destroy $np
949    } elseif {$expgui(showexptool) || $err} {
950        set msg "Please review the result from adding the dummy histogram" 
951        if {$err} {append msg "\nIt appears an error occurred!"}
952        ShowBigMessage $np $msg $errmsg OK "" $err
953    } else {
954        destroy $np
955    }
956    file delete exptool.in exptool.out
957}
958
959
960
961#----------- Add Atoms routines ----------------------------------------
962proc MakeAddAtomsBox {phase "atomlist {}"} {
963    global expmap expgui
964
965    # is there room for more atoms? Well, we will check this someday
966    if {$phase == ""} return
967    if {[llength $phase] != 1} return
968
969    set top .newatoms
970    catch {destroy $top}
971    toplevel $top
972    bind $top <Key-F1> "MakeWWWHelp expgui2.html addatoms"
973
974    grid [label $top.l1 -relief groove -bd 4 -anchor center\
975            -text "Adding atoms to phase #$phase"] \
976            -column 0 -row 0 \
977            -sticky we -columnspan 10
978   
979    grid [canvas $top.canvas \
980            -scrollregion {0 0 5000 500} -width 0 -height 250 \
981            -yscrollcommand "$top.scroll set"] \
982            -column 0 -row 2 -columnspan 4 -sticky nsew
983    grid columnconfigure $top 3 -weight 1
984    grid rowconfigure $top 2 -weight 1
985    grid rowconfigure $top 1 -pad 5
986    scrollbar $top.scroll \
987            -command "$top.canvas yview"
988    frame $top.canvas.fr
989    $top.canvas create window 0 0 -anchor nw -window $top.canvas.fr
990
991    set np $top.canvas.fr
992    set row 0
993    set col 0
994    grid [label $np.l_${row}0 -text "  #  "] -column $col -row $row
995    foreach i {Atom\ntype Name x y z Occ Uiso} \
996            var {type name x y z occ uiso} {
997        grid [button $np.l_${row}$i -text $i -padx 0 -pady 0 \
998                -command "sortAddAtoms $phase $top $var"] \
999                -column [incr col] -row $row -sticky nsew
1000    }
1001    grid [label $np.l_${row}Use -text Use\nFlag] -column [incr col] -row $row
1002
1003    set expgui(SetAddAtomsScroll) 0
1004    set i [llength $atomlist]
1005    if {$i == 0} {incr i}
1006    for {set j 0} {$j < $i} {incr j} {
1007        MakeAddAtomsRow $top
1008    }
1009    set row 0
1010    foreach item $atomlist {
1011        incr row
1012        foreach val $item w {n x y z t o u} {
1013            if {$val != ""} {
1014                $np.e${row}$w delete 0 end
1015                $np.e${row}$w insert end $val
1016            }
1017        }
1018    }
1019    bind $top <Configure> "SetAddAtomsScroll $top"
1020    grid rowconfigure $top 3 -min 10
1021    grid [button $top.b1 -text "Add Atoms"\
1022            -command "addatom $phase $top"] -column 0 -row 5 -sticky w
1023    bind $top <Return> "addatom $phase $top"
1024    grid [button $top.b2 -text Cancel \
1025            -command "destroy $top"] -column 1 -row 5 -sticky w
1026    grid [button $top.help -text Help -bg yellow \
1027            -command "MakeWWWHelp expgui2.html addatoms"] \
1028            -column 0 -columnspan 2 -row 4
1029
1030    # get the input formats if not already defined
1031    GetImportFormats
1032    if {[llength $expgui(importFormatList)] > 0} {
1033        grid [frame $top.fr -bd 4 -relief groove] \
1034                -column 3 -row 5 -columnspan 2 -sticky e
1035        grid [button $top.fr.b3 -text "Import atoms from: " \
1036                -command "ImportAtoms \$expgui(importFormat) $top $phase"] \
1037                -column 0 -row 0 -sticky e
1038        eval tk_optionMenu $top.fr.b4 expgui(importFormat) \
1039                $expgui(importFormatList)
1040        grid $top.fr.b4 -column 1 -row 0 -sticky w
1041        grid rowconfig $top.fr 0 -pad 10
1042        grid columnconfig $top.fr 0 -pad 10
1043        grid columnconfig $top.fr 1 -pad 10
1044    }
1045
1046    grid [button $top.b3 -text  "More atom boxes" \
1047            -command "MakeAddAtomsRow $top"] -column 3 \
1048            -columnspan 2 -row 4 -sticky e
1049   
1050    wm title $top "add new atom"
1051
1052    # set grab, etc.
1053    putontop $top
1054
1055    tkwait window $top
1056
1057    # fix grab...
1058    afterputontop
1059}
1060
1061proc MakeAddAtomsRow {top} {
1062    set np $top.canvas.fr
1063    set col -1
1064    set row 1
1065    # find an empty row
1066    while {![catch {grid info $np.e${row}t}]} {incr row}
1067    grid [label $np.e${row}num -text $row] -column [incr col]  -row $row
1068    grid [entry $np.e${row}t -width 5] -column [incr col]  -row $row
1069    grid [entry $np.e${row}n -width 8] -column [incr col]  -row $row
1070    foreach i {x y z o u} {
1071        grid [entry $np.e${row}$i -width 9] -column [incr col] -row $row
1072    }
1073    grid [checkbutton $np.e${row}use -variable expgui(UseAtom$row)] \
1074            -column [incr col] -row $row
1075    # default occupancy
1076    $np.e${row}o delete 0 end
1077    $np.e${row}o insert end 1.0
1078    # default Uiso
1079    $np.e${row}u delete 0 end
1080    $np.e${row}u insert end 0.025
1081    # default label
1082    $np.e${row}n delete 0 end
1083    $np.e${row}n insert end (default)
1084    # use by default
1085    $np.e${row}use select
1086
1087    SetAddAtomsScroll $top
1088    return $row
1089}
1090
1091proc SetAddAtomsScroll {top} {
1092    global expgui
1093    if $expgui(SetAddAtomsScroll) return
1094    # prevent reentrance
1095    set expgui(SetAddAtomsScroll) 1
1096    update
1097    set sizes [grid bbox $top.canvas.fr]
1098    $top.canvas config -scrollregion $sizes -width [lindex $sizes 2]
1099    # use the scroll for BIG atom lists
1100    if {[lindex $sizes 3] > [winfo height $top.canvas]} {
1101        grid $top.scroll -sticky ns -column 4 -row 2
1102    } else {
1103        grid forget $top.scroll 
1104    }
1105    update
1106    set expgui(SetAddAtomsScroll) 0
1107}
1108
1109proc addatom {phase top} {
1110    global expgui env expmap
1111    set np $top.canvas.fr
1112    set row 0
1113    # loop over the defined rows
1114    set err {}
1115    set atomlist {}
1116    set validatmtypes {
1117        H H-1 H_1 H_2 H_3 HE HE_3 HE_4 LI LI+1 LI_6 LI_7 BE BE+2 B B_10
1118        B_11 C CV C_12 C_13 N N_14 N_15 O O-1 O_16 O_17 O_18 F F-1 F_19 NE
1119        NE_20 NE_21 NE_22 NA NA+1 NA_23 MG MG+2 MG_24 MG_25 MG_26 AL AL+3
1120        AL_27 SI SI+4 SIV SI_28 SI_29 SI_30 P P_31 S S_32 S_33 S_34 CL CL-1
1121        CL_35 CL_37 AR AR_36 AR_40 K K+1 K_39 K_41 CA CA+2 CA_40 CA_44 SC SC+3
1122        SC_45 TI TI+2 TI+3 TI+4 TI_46 TI_47 TI_48 TI_49 TI_50 V V+2 V+3 V+5
1123        V_51 CR CR+2 CR+3 CR_50 CR_52 CR_53 CR_54 MN MN+2 MN+3 MN+4 MN_55 FE
1124        FE+2 FE+3 FE_54 FE_56 FE_57 FE_58 CO CO+2 CO+3 CO_59 NI NI+2 NI+3
1125        NI_58 NI_60 NI_61 NI_62 NI_64 CU CU+1 CU+2 CU_63 CU_65 ZN ZN+2 ZN_64
1126        ZN_66 ZN_67 ZN_68 GA GA+3 GE GE+4 AS AS_75 SE BR BR-1 BR_79 BR_81 KR
1127        RB RB+1 SR SR+2 Y Y+3 Y_89 ZR ZR+4 NB NB+3 NB+5 NB_93 MO MO+3 MO+5
1128        MO+6 TC TC_98 RU RU+3 RU+4 RH RH+3 RH+4 RH_103 PD PD+2 PD+4 AG AG+1
1129        AG+2 CD CD+2 CD_112 CD_113 CD_114 CD_116 IN IN+3 IN_113 IN_115 SN SN+2
1130        SN+4 SB SB+3 SB+5 TE I I-1 I_127 XE CS CS+1 CS_133 BA BA+2 LA LA+3 CE
1131        CE+3 CE+4 PR PR+3 PR+4 PR_141 ND ND+3 PM PM+3 PM_147 SM SM+3 SM_152
1132        SM_154 EU EU+2 EU+3 EU_153 GD GD+3 GD_160 TB TB+3 TB_159 DY DY+3 HO
1133        HO+3 HO_165 ER ER+3 TM TM+3 TM_169 YB YB+2 YB+3 LU LU+3 HF HF+4 TA
1134        TA+5 TA_181 W W+6 RE OS OS+4 IR IR+3 IR+4 PT PT+2 PT+4 AU AU+1 AU+3
1135        AU_197 HG HG+1 HG+2 TL TL+1 TL+3 PB PB+2 PB+4 BI BI+3 BI+5 BI_209 PO
1136        PO_210 AT AT_210 RN RN_222 FR FR_223 RA RA+2 RA_226 AC AC+3 AC_227 TH
1137        TH+4 TH_232 PA PA_231 U U+3 U+4 U+6 U_235 U_238 NP NP+3 NP+4 NP+6
1138        NP_237 PU PU+3 PU+4 PU+6 PU_239 PU_240 PU_242 AM AM_243 CM CM_244 BK
1139        BK_247 CF CF_249
1140    }
1141    while {![catch {grid info $np.e[incr row]t}]} {
1142        if !{$expgui(UseAtom$row)} continue
1143        # ignore blank entries
1144        set line {}
1145        foreach i {t x y z} {
1146            append line [string trim [$np.e${row}$i get]]
1147        }
1148        if {$line == ""} continue
1149        # validate the input
1150        if {[set type [string trim [$np.e${row}t get]]] == ""} {
1151            append err "  line $row: No atom type specified\n"
1152        }
1153        if {[lsearch $validatmtypes [string toupper $type]] == -1} {
1154            append err "  line $row: Atom type $type is invalid for GSAS\n"
1155        }
1156        set name [string trim [$np.e${row}n get]]
1157        if {$name == "(default)"} {set name "/"}
1158        if {$name == ""} {set name "/"}
1159        foreach i {x y z o u} n {x y z Occ Uiso} {
1160            if {[set $i [string trim [$np.e${row}$i get]]] == ""} {
1161                append err "  line $row: No value specified for $n\n"
1162            } elseif {[catch {expr [set $i]}]} {
1163                append err "  line $row: The value for $n is invalid\n"
1164            }
1165        }
1166        lappend atomlist "$type $x $y $z $o $name I $u"
1167    }   
1168    if {$err != ""} {
1169        MyMessageBox -icon warning -message "Note Errors:\n$err" -parent $top
1170        return
1171    }
1172    if {[llength $atomlist] == 0} {
1173        MyMessageBox -icon warning -message "No atoms to load!" -parent $top
1174        return
1175    }
1176    # ok add the atoms!
1177    set fp [open exptool.in w]
1178    puts $fp "A"
1179    puts $fp $phase
1180    # number of atoms
1181    puts $fp [llength $atomlist]
1182    foreach atomline $atomlist {
1183        puts $fp $atomline
1184    }
1185    close $fp
1186    # needed in UNIX
1187    set env(ATOMDATA) [file join $expgui(gsasdir) data atmdata.dat]
1188    set env(gsas) [file nativename $expgui(gsasdir)]
1189    # needed in Windows
1190    set env(GSAS) [file nativename $expgui(gsasdir)]
1191
1192    global tcl_platform
1193    # Save the current exp file
1194    savearchiveexp
1195    # disable the file changed monitor
1196    set expgui(expModifiedLast) 0
1197    set expnam [file root [file tail $expgui(expfile)]]
1198    catch {
1199        if {$tcl_platform(platform) == "windows"} {
1200            exec [file join $expgui(gsasexe) exptool.exe] $expnam \
1201                    < exptool.in >& exptool.out
1202        } else {
1203            exec [file join $expgui(gsasexe) exptool] $expnam \
1204                    < exptool.in >& exptool.out
1205        }
1206    } errmsg
1207    # load the revised exp file
1208    set oldatomlist $expmap(atomlist_$phase)
1209    loadexp $expgui(expfile)
1210    set fp [open exptool.out r]
1211    set out [read $fp]
1212    close $fp
1213    destroy $top
1214    set err 0
1215    if {[llength $oldatomlist] == [llength $expmap(atomlist_$phase))]} {
1216        set err 1
1217    }
1218    if {$errmsg != ""} {
1219        append errmsg "\n" $out
1220        set err 1
1221    } else {
1222        set errmsg $out
1223    }
1224    if {$expgui(showexptool) || $err} {
1225        set msg "Please review the result from adding the atom(s)" 
1226        if {$err} {append msg "\nIt appears an error occurred!"}
1227        ShowBigMessage $top $msg $errmsg OK "" $err
1228    }
1229    file delete exptool.in exptool.out
1230}
1231
1232#---------------------------------------------------------------------------
1233# commands to modify a group of selected atoms
1234#---------------------------------------------------------------------------
1235
1236# make the dialog to choose an action
1237proc MakeXformAtomsBox {phase} {
1238    global expgui expmap
1239    set numberList {}
1240    set p $expgui(curPhase)
1241    foreach AtomIndex $expgui(selectedatomlist) {
1242        # get atom number & phase
1243        set tuple [lindex $expmap(atomlistboxcontents) $AtomIndex]
1244        lappend numberList [lindex $tuple 0]
1245    }
1246    if {$numberList == ""} return
1247    if {[llength $numberList] > 1} {
1248        set suffix s
1249        set suffixy "ies"
1250    } else {
1251        set suffix ""
1252        set suffixy "y"
1253    }
1254    set w .global
1255    catch {destroy $w}
1256    toplevel $w
1257    wm title $w "Edit Atomic Parameter -- phase #$phase"
1258    bind $w <Key-F1> "MakeWWWHelp expgui2.html xform"
1259    # this needs to track by phase
1260    grid [label $w.0 \
1261            -text "Modifying atom${suffix} [CompressList $numberList] Phase $phase" \
1262            -bg yellow -anchor center] -row 0 -column 0 -columnspan 10 \
1263            -sticky nsew
1264    grid rowconfigure $w 0 -pad 5
1265    grid rowconfigure $w 1 -minsize 2
1266
1267    grid [TitleFrame $w.1 -bd 6 -relief groove -text "Modify coordinates"] \
1268            -row 2 -column 0 -columnspan 10 -sticky news
1269    set w1 [$w.1 getframe]
1270    set row 0
1271    foreach v {x y z} {
1272        incr row
1273        set col -1
1274        grid [label $w1.l$v -text "new $v   =   "] -column [incr col] -row $row
1275        foreach o {x y z} {
1276            grid [entry $w1.e${v}${o} -width 6] -column [incr col] -row $row
1277            $w1.e${v}${o} delete 0 end
1278            if {$v == $o} {
1279                $w1.e${v}${o} insert end "1.0"
1280            } else {
1281                $w1.e${v}${o} insert end "0."
1282            }
1283            grid [label $w1.p${v}${o} -text " $o  +  "] \
1284                    -column [incr col] -row $row
1285        }
1286        grid [entry $w1.e${v} -width 6] -column [incr col] -row $row
1287        $w1.e${v} delete 0 end
1288        $w1.e${v} insert end "0."
1289    }
1290    grid [button $w1.do -text "Transform Coordinates" \
1291            -command "XformAtomsCoord $phase [list $numberList] $w1" \
1292            ] -row [incr row] -column 0 -columnspan 10
1293
1294    grid rowconfigure $w 3 -minsize 5
1295    grid [TitleFrame $w.4 -bd 6 -relief groove -text "Modify occupanc${suffixy}"] \
1296            -row 4 -column 0 -columnspan 10 -sticky news
1297    set w2 [$w.4 getframe]
1298    grid [label $w2.1 -text "Occupancy: "] -row 1 -column 0
1299    grid [entry $w2.e -width 10] -column 1 -row 1
1300    $w2.e delete 0 end
1301    $w2.e insert end 1.0
1302    grid columnconfigure $w2 2 -weight 1
1303    grid [button $w2.do -text "Set Occupanc${suffixy}" \
1304            -command "XformAtomsOcc $phase [list $numberList] $w2" \
1305            ] -row 2 -column 0 -columnspan 10
1306
1307    grid rowconfigure $w 5 -minsize 5
1308    grid [TitleFrame $w.6 -bd 6 -relief groove \
1309            -text "Modify Displacement Parameter$suffix"] \
1310            -row 6 -column 0 -columnspan 10 -sticky news
1311    set w2 [$w.6 getframe]
1312    grid [label $w2.1 -text "Uiso or Uequiv: "] -row 1 -column 0
1313    grid [entry $w2.e -width 10] -column 1 -row 1
1314    $w2.e delete 0 end
1315    $w2.e insert end 0.025
1316    grid columnconfigure $w2 2 -weight 1
1317    grid [button $w2.do -text "Set U" \
1318            -command "XformAtomsU $phase [list $numberList] $w2" \
1319            ] -row 2 -column 0 -columnspan 10
1320    grid [frame $w2.f] -row 3 -column 0 -columnspan 10
1321    grid [button $w2.f.iso -text "Set Isotropic" \
1322            -command "XformAtomsU $phase [list $numberList] iso" \
1323            ] -row 0 -column 0
1324    grid [button $w2.f.aniso -text "Set Anisotropic" \
1325            -command "XformAtomsU $phase [list $numberList] aniso" \
1326            ] -row 0 -column 1
1327
1328    grid rowconfigure $w 5 -minsize 5
1329    grid [TitleFrame $w.8 -bd 6 -relief groove \
1330            -text "Erase Atom$suffix"] \
1331            -row 8 -column 0 -columnspan 10 -sticky news
1332    set w2 [$w.8 getframe]
1333    grid [button $w2.do -text "Erase Atom${suffix}" \
1334            -command "EraseAtoms $phase [list $numberList] $w" \
1335            ] -row 2 -column 0 -columnspan 10
1336
1337
1338    grid rowconfigure $w 9 -minsize 5
1339    grid [frame $w.b] -row 10 -column 0 -columnspan 10 -sticky ew
1340    pack [button $w.b.3 -text Close -command "destroy $w"] -side left \
1341            -padx 5 -pady 5
1342    pack [button $w.b.help -text Help -bg yellow \
1343            -command "MakeWWWHelp expgui2.html xform"] -side right \
1344            -padx 5 -pady 5
1345    bind $w <Return> "destroy $w"
1346
1347    # force the window to stay on top
1348    putontop $w
1349    focus $w.b.3
1350    tkwait window $w
1351    afterputontop
1352    # if there are selected atoms, reset their display
1353    if {[llength $expgui(selectedatomlist)] != 0} editRecord
1354}
1355
1356# transform the coordinates
1357proc XformAtomsCoord {phase numberList w1} {
1358    global expgui
1359    # get the matrix
1360    foreach v {x y z} {
1361        foreach o {x y z} {
1362            set matrix(${v}${o}) [$w1.e${v}${o} get]
1363        }
1364        set matrix(${v}) [$w1.e${v} get]
1365    }
1366    foreach atom $numberList {
1367        foreach v {x y z} {
1368            set $v [atominfo $phase $atom $v]
1369        }
1370        foreach v {x y z} {
1371            set new$v $matrix(${v})
1372            foreach o {x y z} {
1373                set new$v [expr [set new$v] + $matrix(${v}${o})*[set $o]]
1374            }
1375            atominfo $phase $atom $v set [set new$v]
1376        }
1377        incr expgui(changed)
1378    }
1379    DisplayAllAtoms noreset
1380}
1381
1382# set the occupancies to a single value
1383proc XformAtomsOcc {phase numberList w2} {
1384    global expgui
1385    # get the value
1386    set val [$w2.e get]
1387    foreach atom $numberList {
1388        atominfo $phase $atom frac set $val
1389        incr expgui(changed)
1390    }
1391    DisplayAllAtoms noreset
1392}
1393
1394# transform Uiso or Uij; if anisotropic set Uequiv to Uij
1395proc XformAtomsU {phase numberList w2} {
1396    global expgui
1397    if {$w2 == "iso"} {
1398        foreach atom $numberList {
1399            if {[atominfo $phase $atom temptype] != "I"} {
1400                atominfo $phase $atom temptype set I
1401            }
1402        }
1403    } elseif {$w2 == "aniso"} {
1404        foreach atom $numberList {
1405            if {[atominfo $phase $atom temptype] == "I"} {
1406                atominfo $phase $atom temptype set A
1407            }
1408        }
1409    } else {
1410        # get the value
1411        set val [$w2.e get]
1412        foreach atom $numberList {
1413            if {[atominfo $phase $atom temptype] == "I"} {
1414                atominfo $phase $atom Uiso set $val
1415            } else {
1416                atominfo $phase $atom U11 set $val
1417                atominfo $phase $atom U22 set $val
1418                atominfo $phase $atom U33 set $val
1419                atominfo $phase $atom U12 set 0.0
1420                atominfo $phase $atom U13 set 0.0
1421                atominfo $phase $atom U23 set 0.0
1422            }
1423            incr expgui(changed)
1424        }
1425    }
1426    DisplayAllAtoms noreset
1427}
1428
1429# confirm and erase atoms
1430proc EraseAtoms {phase numberList w2} {
1431    global expgui
1432    if {[llength $numberList] <= 0} return
1433    # make a list of atoms
1434    foreach atom $numberList {
1435        append atomlist "\n\t$atom  [atominfo $phase $atom label]"
1436    }
1437    set msg "OK to remove the following [llength $numberList] atoms from phase $phase:$atomlist"
1438    set val [MyMessageBox -parent $w2 -type okcancel -icon warning \
1439            -default cancel -title "Confirm Erase" -message $msg]
1440    if {$val == "ok"} {
1441        foreach atom $numberList {
1442            EraseAtom $atom $phase
1443            incr expgui(changed)
1444        }
1445        mapexp
1446        DisplayAllAtoms
1447        destroy $w2
1448    }
1449}
1450
1451#----------- more Add Phase routines (import) -------------------------------
1452proc ImportPhase {format np} {
1453    global expgui
1454    foreach item $expgui(extensions_$format) {
1455        lappend typelist [list $format $item]
1456    }
1457    lappend typelist [list "All files" *]
1458    set file [tk_getOpenFile -parent $np -filetypes $typelist]
1459    if {![file exists $file]} return
1460    # read in the file
1461    set input [$expgui(proc_$format) $file]
1462    catch {
1463        $np.bf.b1 config -text "Continue" -command "addphase $np; AddAtomsList"
1464        bind $np <Return> "addphase $np; AddAtomsList"
1465    }
1466    catch {
1467        $np.t1 delete 0 end
1468        $np.t1 insert end "from $file"
1469    }
1470    $np.t2 delete 0 end
1471    $np.t2 insert end [lindex $input 0]
1472    foreach i {.e1a .e1b .e1c .e2a .e2b .e2g} val [lindex $input 1] {
1473        $np.f$i delete 0 end
1474        $np.f$i insert end $val
1475    }
1476    set expgui(coordList) [lindex $input 2]
1477    set msg [lindex $input 3]
1478    if {$msg != ""} {
1479        catch {destroy $np.msg}
1480        grid [label $np.msg -text $msg -fg red -anchor center -bd 4 -relief raised] \
1481                -column 0 -columnspan 99 -row 20 -sticky ew
1482    }
1483}
1484
1485proc ImportAtoms {format top phase} {
1486    global expgui
1487    foreach item $expgui(extensions_$format) {
1488        lappend typelist [list $format $item]
1489    }
1490    lappend typelist [list "All files" *]
1491    set file [tk_getOpenFile -parent $top -filetypes $typelist]
1492    if {![file exists $file]} return
1493    # read in the file
1494    set input [$expgui(proc_$format) $file]
1495    # add atoms to table
1496    foreach item [lindex $input 2] {
1497        set row [MakeAddAtomsRow $top]
1498        set np $top.canvas.fr
1499        foreach val $item w {n x y z t o u} {
1500            if {$val != ""} {
1501                $np.e${row}$w delete 0 end
1502                $np.e${row}$w insert end $val
1503            }
1504        }
1505    }
1506    # sort the atoms by number, so that empty entries are at the bottom
1507    sortAddAtoms $phase $top number
1508}
1509
1510proc AddAtomsList {} {
1511    global expgui expmap
1512    # skip if we aborted out of addphase
1513    if {$expgui(oldphaselist) == -1} return
1514    # find the new phase
1515    set phase {}
1516    foreach p $expmap(phaselist) {
1517        if {[lsearch $expgui(oldphaselist) $p] == -1} {
1518            set phase $p
1519            break
1520        }
1521    }
1522    if {$phase == ""} return
1523    MakeAddAtomsBox $phase $expgui(coordList)
1524}
1525
1526# get the input formats by sourcing files named import_*.tcl
1527proc GetImportFormats {} {
1528    global expgui tcl_platform
1529    # only needs to be done once
1530    if [catch {set expgui(importFormatList)}] {
1531        set filelist [glob -nocomplain [file join $expgui(scriptdir) import_*.tcl]]
1532        foreach file $filelist {
1533            source $file
1534            lappend expgui(importFormatList) $description
1535            if {$tcl_platform(platform) == "unix"} {
1536                set extensions "[string tolower $extensions] [string toupper $extensions]"
1537            }
1538            set expgui(extensions_$description) $extensions
1539            set expgui(proc_$description) $procname
1540        }
1541    }
1542}
1543
1544proc MakeReplacePhaseBox {} {
1545    global expmap expgui tcl_platform
1546
1547    set expgui(coordList) {}
1548    # ignore the command if no phase is selected
1549    foreach p {1 2 3 4 5 6 7 8 9} {
1550        if {[lsearch $expmap(phaselist) $expgui(curPhase)] == -1} {
1551            return
1552        }
1553    }
1554
1555    set top .newphase
1556    catch {destroy $top}
1557    toplevel $top
1558    bind $top <Key-F1> "MakeWWWHelp expgui2.html replacephase"
1559
1560    grid [label $top.l1 -text "Replacing phase #$expgui(curPhase)" \
1561            -bg yellow -anchor center] -column 0 -columnspan 8 -row 0 -sticky ew
1562    grid [label $top.l3a -text "Current Space Group: "] \
1563            -column 0 -row 2 -columnspan 2 -sticky e
1564    grid [label $top.l3b -text [phaseinfo $expgui(curPhase) spacegroup]\
1565            -bd 4 -relief groove] \
1566            -column 2 -row 2  -sticky ew
1567    grid [label $top.l4 -text "New Space Group: "] \
1568            -column 0 -row 3 -columnspan 2 -sticky e
1569    grid [entry $top.t2 -width 12] -column 2 -row 3 -sticky w
1570    grid [radiobutton $top.r1 -text "Reenter current atoms"\
1571            -variable expgui(DeleteAllAtoms) -value 0] \
1572            -column 1 -row 4 -columnspan 8 -sticky w
1573    grid [radiobutton $top.r2 -text "Delete current atoms" \
1574            -variable expgui(DeleteAllAtoms) -value 1] \
1575            -column 1 -row 5 -columnspan 8 -sticky w
1576   
1577    grid [frame $top.f -bd 4 -relief groove] \
1578            -column 3 -row 2 -columnspan 3 -rowspan 4
1579    set col -1
1580    foreach i {a b c} {
1581        grid [label $top.f.l1$i -text " $i "] -column [incr col] -row 1
1582        grid [entry $top.f.e1$i -width 12] -column [incr col]  -row 1
1583        $top.f.e1$i delete 0 end
1584        $top.f.e1$i insert 0 [phaseinfo $expgui(curPhase) $i]
1585    }
1586    set col -1
1587    foreach i {a b g} var {alpha beta gamma} {
1588        grid [label $top.f.l2$i -text $i] -column [incr col] -row 2
1589        set font [$top.f.l2$i cget -font]
1590        $top.f.l2$i config -font "Symbol [lrange $font 1 end]"
1591        grid [entry $top.f.e2$i -width 12] -column [incr col]  -row 2
1592        $top.f.e2$i delete 0 end
1593        $top.f.e2$i insert 0 [phaseinfo $expgui(curPhase) $var]
1594    } 
1595
1596    grid [button $top.b1 -text Continue \
1597            -command "replacephase1 $top $expgui(curPhase)"] \
1598            -column 0 -row 6 -sticky w
1599    bind $top <Return> "replacephase1 $top $expgui(curPhase)"
1600    grid [button $top.b2 -text Cancel \
1601            -command "destroy $top"] -column 1 -row 6 -sticky w
1602    grid [button $top.help -text Help -bg yellow \
1603            -command "MakeWWWHelp expgui2.html replacephase"] \
1604            -column 2 -row 6
1605
1606    # get the input formats if not already defined
1607    GetImportFormats
1608    if {[llength $expgui(importFormatList)] > 0} {
1609        grid [frame $top.fr -bd 4 -relief groove] \
1610                -column 2 -row 6 -columnspan 8 -sticky e
1611        grid [button $top.fr.b3 -text "Import phase from: " \
1612                -command "ImportPhase \$expgui(importFormat) $top"] \
1613                -column 0 -row 0 -sticky e
1614        eval tk_optionMenu $top.fr.b4 expgui(importFormat) \
1615                $expgui(importFormatList)
1616        grid $top.fr.b4 -column 1 -row 0 -sticky w
1617        grid rowconfig $top.fr 0 -pad 10
1618        grid columnconfig $top.fr 0 -pad 10
1619        grid columnconfig $top.fr 1 -pad 10
1620#       grid columnconfig $top 4 -weight 1
1621        grid columnconfig $top 2 -weight 1
1622    }
1623   
1624    wm title $top "Replace phase $expgui(curPhase)"
1625
1626    # set grab, etc.
1627    putontop $top
1628
1629    tkwait window $top
1630
1631    # fix grab...
1632    afterputontop
1633}
1634
1635proc replacephase1 {top phase} {
1636    # validate cell & space group & save to pass
1637    global expgui expmap
1638    set expgui(SetAddAtomsScroll) 0
1639    # validate the input
1640    set err {}
1641    set spg [$top.t2 get]
1642    if {[string trim $spg] == ""} {
1643        append err "  Space group cannot be blank\n"
1644    }
1645    set cell {}
1646    foreach i {a b c a b g} lbl {a b c alpha beta gamma} n {1 1 1 2 2 2} {
1647        set $lbl [$top.f.e${n}$i get]
1648        if {[string trim [set $lbl]] == ""} {
1649            append err "  $lbl cannot be blank\n"
1650        } elseif {[catch {expr [set $lbl]}]} {
1651            append err "  [set $lbl] is not valid for $lbl\n"
1652        }
1653        lappend cell [set $lbl]
1654    }
1655
1656    if {$err != ""} {
1657        MyMessageBox -parent $top -title "Replace Phase Error" -icon warning \
1658                -message "The following error(s) were found in your input:\n$err" 
1659        return
1660    }
1661
1662    # check the space group
1663    set fp [open spg.in w]
1664    puts $fp "N"
1665    puts $fp "N"
1666    puts $fp $spg
1667    puts $fp "Q"
1668    close $fp
1669    global tcl_platform
1670    catch {
1671        if {$tcl_platform(platform) == "windows"} {
1672            exec [file join $expgui(gsasexe) spcgroup.exe] < spg.in >& spg.out
1673        } else {
1674            exec [file join $expgui(gsasexe) spcgroup] < spg.in >& spg.out
1675        }
1676    }
1677    set fp [open spg.out r]
1678    set out [read $fp]
1679    close $fp
1680    # attempt to parse out the output (fix up if parse did not work)
1681    if {[regexp "space group symbol.*>(.*)Enter a new space group symbol" \
1682            $out a b ] != 1} {set b $out}
1683    if {[string first Error $b] != -1} {
1684        # got an error, show it
1685        ShowBigMessage \
1686                 $top.error \
1687                 "Error processing space group\nReview error message below" \
1688                 $b OK "" 1
1689        return
1690    } else {
1691        # show the result and confirm
1692        set opt [ShowBigMessage \
1693                $top.check \
1694                "Check the symmetry operators in the output below" \
1695                $b \
1696                {Continue Redo} ]
1697        if {$opt > 1} return
1698    }
1699    file delete spg.in spg.out
1700    # draw coordinates box
1701    eval destroy [winfo children $top]
1702    grid [label $top.l1 -relief groove -bd 4 -anchor center\
1703            -text "Atom list for phase #$phase"] \
1704            -column 0 -row 0 \
1705            -sticky we -columnspan 10
1706    grid [canvas $top.canvas \
1707            -scrollregion {0 0 5000 500} -width 0 -height 250 \
1708            -yscrollcommand "$top.scroll set"] \
1709            -column 0 -row 2 -columnspan 4 -sticky nsew
1710    grid columnconfigure $top 3 -weight 1
1711    grid rowconfigure $top 2 -weight 1
1712    grid rowconfigure $top 1 -pad 5
1713    scrollbar $top.scroll \
1714            -command "$top.canvas yview"
1715    frame $top.canvas.fr
1716    $top.canvas create window 0 0 -anchor nw -window $top.canvas.fr
1717
1718    set np $top.canvas.fr
1719    set row 0
1720    set col 0
1721    grid [label $np.l_${row}0 -text "  #  "] -column $col -row $row
1722    foreach i {Atom\ntype Name x y z Occ Uiso} \
1723            var {type name x y z occ uiso} {
1724        grid [button $np.l_${row}$i -text $i -padx 0 -pady 0 \
1725                -command "sortAddAtoms $phase $top $var"] \
1726                -column [incr col] -row $row -sticky nsew
1727    }
1728    grid [label $np.l_${row}Use -text Use\nFlag] -column [incr col] -row $row
1729
1730    # add the old atoms, if appropriate
1731    if {!$expgui(DeleteAllAtoms)} {
1732        # loop over all atoms
1733        foreach atom $expmap(atomlist_$phase) {
1734            set row [MakeAddAtomsRow $top]
1735            # add all atoms in the current phase to the list
1736            foreach w {n x y z t o} var {label x y z type frac} {
1737                $np.e${row}$w delete 0 end
1738                $np.e${row}$w insert end [atominfo $phase $atom $var]
1739            }
1740            $np.e${row}u delete 0 end
1741            if {[atominfo $phase $atom temptype] == "I"} {
1742                $np.e${row}u insert end [atominfo $phase $atom Uiso]
1743            } else {
1744                $np.e${row}u insert end [expr ( \
1745                        [atominfo $phase $atom U11] + \
1746                        [atominfo $phase $atom U22] + \
1747                        [atominfo $phase $atom U33]) / 3.]
1748            }
1749        }
1750    }
1751
1752    # add coordinates that have been read in, if any
1753    foreach item $expgui(coordList) {
1754        set row [MakeAddAtomsRow $top]
1755        foreach val $item w {n x y z t o u} {
1756            if {$val != ""} {
1757                $np.e${row}$w delete 0 end
1758                $np.e${row}$w insert end $val
1759            }
1760        }
1761    }
1762    # a blank spot in the table
1763    MakeAddAtomsRow $top
1764
1765    bind $top <Configure> "SetAddAtomsScroll $top"
1766    grid rowconfigure $top 3 -min 10
1767    grid [button $top.b1 -text "Continue"\
1768            -command "replacephase2 $phase $top [list $spg] [list $cell]"] \
1769            -column 0 -row 5 -sticky w
1770    bind $top <Return> "replacephase2 $phase $top [list $spg] [list $cell]"
1771    grid [button $top.b2 -text Cancel \
1772            -command "destroy $top"] -column 1 -row 5 -sticky w
1773    if {[llength $expgui(importFormatList)] > 0} {
1774        grid [frame $top.fr -bd 4 -relief groove] \
1775                -column 3 -row 5 -columnspan 2 -sticky e
1776        grid [button $top.fr.b3 -text "Import atoms from: " \
1777                -command "ImportAtoms \$expgui(importFormat) $top $phase"] \
1778                -column 0 -row 0 -sticky e
1779        eval tk_optionMenu $top.fr.b4 expgui(importFormat) \
1780                $expgui(importFormatList)
1781        grid $top.fr.b4 -column 1 -row 0 -sticky w
1782        grid rowconfig $top.fr 0 -pad 10
1783        grid columnconfig $top.fr 0 -pad 10
1784        grid columnconfig $top.fr 1 -pad 10
1785    }
1786
1787    grid [button $top.b3 -text  "More atom boxes" \
1788            -command "MakeAddAtomsRow $top"] -column 3 \
1789            -columnspan 2 -row 4 -sticky e
1790   
1791    wm title $top "Replacing phase: Enter atoms"
1792    SetAddAtomsScroll $top
1793
1794    # fix grab for old window
1795    afterputontop
1796    # set grab, etc.
1797    putontop $top
1798}
1799
1800proc replacephase2 {phase top spg cell} {
1801    global expgui expmap env
1802    # validate coordinates
1803    set np $top.canvas.fr
1804    set row 0
1805    # loop over the defined rows
1806    set err {}
1807    set atomlist {}
1808    set validatmtypes {
1809        H H-1 H_1 H_2 H_3 HE HE_3 HE_4 LI LI+1 LI_6 LI_7 BE BE+2 B B_10
1810        B_11 C CV C_12 C_13 N N_14 N_15 O O-1 O_16 O_17 O_18 F F-1 F_19 NE
1811        NE_20 NE_21 NE_22 NA NA+1 NA_23 MG MG+2 MG_24 MG_25 MG_26 AL AL+3
1812        AL_27 SI SI+4 SIV SI_28 SI_29 SI_30 P P_31 S S_32 S_33 S_34 CL CL-1
1813        CL_35 CL_37 AR AR_36 AR_40 K K+1 K_39 K_41 CA CA+2 CA_40 CA_44 SC SC+3
1814        SC_45 TI TI+2 TI+3 TI+4 TI_46 TI_47 TI_48 TI_49 TI_50 V V+2 V+3 V+5
1815        V_51 CR CR+2 CR+3 CR_50 CR_52 CR_53 CR_54 MN MN+2 MN+3 MN+4 MN_55 FE
1816        FE+2 FE+3 FE_54 FE_56 FE_57 FE_58 CO CO+2 CO+3 CO_59 NI NI+2 NI+3
1817        NI_58 NI_60 NI_61 NI_62 NI_64 CU CU+1 CU+2 CU_63 CU_65 ZN ZN+2 ZN_64
1818        ZN_66 ZN_67 ZN_68 GA GA+3 GE GE+4 AS AS_75 SE BR BR-1 BR_79 BR_81 KR
1819        RB RB+1 SR SR+2 Y Y+3 Y_89 ZR ZR+4 NB NB+3 NB+5 NB_93 MO MO+3 MO+5
1820        MO+6 TC TC_98 RU RU+3 RU+4 RH RH+3 RH+4 RH_103 PD PD+2 PD+4 AG AG+1
1821        AG+2 CD CD+2 CD_112 CD_113 CD_114 CD_116 IN IN+3 IN_113 IN_115 SN SN+2
1822        SN+4 SB SB+3 SB+5 TE I I-1 I_127 XE CS CS+1 CS_133 BA BA+2 LA LA+3 CE
1823        CE+3 CE+4 PR PR+3 PR+4 PR_141 ND ND+3 PM PM+3 PM_147 SM SM+3 SM_152
1824        SM_154 EU EU+2 EU+3 EU_153 GD GD+3 GD_160 TB TB+3 TB_159 DY DY+3 HO
1825        HO+3 HO_165 ER ER+3 TM TM+3 TM_169 YB YB+2 YB+3 LU LU+3 HF HF+4 TA
1826        TA+5 TA_181 W W+6 RE OS OS+4 IR IR+3 IR+4 PT PT+2 PT+4 AU AU+1 AU+3
1827        AU_197 HG HG+1 HG+2 TL TL+1 TL+3 PB PB+2 PB+4 BI BI+3 BI+5 BI_209 PO
1828        PO_210 AT AT_210 RN RN_222 FR FR_223 RA RA+2 RA_226 AC AC+3 AC_227 TH
1829        TH+4 TH_232 PA PA_231 U U+3 U+4 U+6 U_235 U_238 NP NP+3 NP+4 NP+6
1830        NP_237 PU PU+3 PU+4 PU+6 PU_239 PU_240 PU_242 AM AM_243 CM CM_244 BK
1831        BK_247 CF CF_249
1832    }
1833    while {![catch {grid info $np.e[incr row]t}]} {
1834        if !{$expgui(UseAtom$row)} continue
1835        # ignore blank entries
1836        set line {}
1837        foreach i {t x y z} {
1838            append line [string trim [$np.e${row}$i get]]
1839        }
1840        if {$line == ""} continue
1841        # validate the input
1842        if {[set type [string trim [$np.e${row}t get]]] == ""} {
1843            append err "  line $row: No atom type specified\n"
1844        }
1845        if {[lsearch $validatmtypes [string toupper $type]] == -1} {
1846            append err "  line $row: Atom type $type is invalid for GSAS\n"
1847        }
1848        set name [string trim [$np.e${row}n get]]
1849        if {$name == "(default)"} {set name "/"}
1850        if {$name == ""} {set name "/"}
1851        foreach i {x y z o u} n {x y z Occ Uiso} {
1852            if {[set $i [string trim [$np.e${row}$i get]]] == ""} {
1853                append err "  line $row: No value specified for $n\n"
1854            } elseif {[catch {expr [set $i]}]} {
1855                append err "  line $row: The value for $n is invalid\n"
1856            }
1857        }
1858        lappend atomlist "$type $x $y $z $o $name I $u"
1859    }   
1860    if {$err != ""} {
1861        MyMessageBox -icon warning -message "Note Errors:\n$err" -parent $top
1862        return
1863    }
1864    if {[llength $atomlist] == 0} {
1865        MyMessageBox -icon warning -message "No atoms to load!" -parent $top
1866        return
1867    }
1868
1869    pleasewait "updating phase"
1870    # replace spacegroup and cell
1871    phaseinfo $phase spacegroup set $spg
1872    foreach val $cell var {a b c alpha beta gamma} {
1873        phaseinfo $phase $var set $val
1874    }
1875    # delete all atoms
1876    foreach i $expmap(atomlist_$phase) {
1877        EraseAtom $i $phase
1878    }
1879    incr expgui(changed) 8
1880    # write new atoms from table as input to exptool
1881    set fp [open exptool.in w]
1882    puts $fp "A"
1883    puts $fp $phase
1884    # number of atoms
1885    puts $fp [llength $atomlist]
1886    foreach atomline $atomlist {
1887        puts $fp $atomline
1888    }
1889    close $fp
1890    # needed in UNIX
1891    set env(ATOMDATA) [file join $expgui(gsasdir) data atmdata.dat]
1892    set env(gsas) [file nativename $expgui(gsasdir)]
1893    # needed in Windows
1894    set env(GSAS) [file nativename $expgui(gsasdir)]
1895
1896    global tcl_platform
1897    # Save the current exp file
1898    savearchiveexp
1899    # disable the file changed monitor
1900    set expgui(expModifiedLast) 0
1901    set expnam [file root [file tail $expgui(expfile)]]
1902    catch {
1903        if {$tcl_platform(platform) == "windows"} {
1904            exec [file join $expgui(gsasexe) exptool.exe] $expnam \
1905                    < exptool.in >& exptool.out
1906        } else {
1907            exec [file join $expgui(gsasexe) exptool] $expnam \
1908                    < exptool.in >& exptool.out
1909        }
1910    } errmsg
1911    # load the revised exp file
1912    set oldatomlist $expmap(atomlist_$phase)
1913    loadexp $expgui(expfile)
1914    set fp [open exptool.out r]
1915    set out [read $fp]
1916    close $fp
1917    set err 0
1918    if {[llength $oldatomlist] == [llength $expmap(atomlist_$phase))]} {
1919        set err 1
1920    }
1921    if {$errmsg != ""} {
1922        append errmsg "\n" $out
1923        set err 1
1924    } else {
1925        set errmsg $out
1926    }
1927    donewait 
1928    if {$expgui(showexptool) || $err} {
1929        set msg "Please review the result from adding the atom(s)" 
1930        if {$err} {append msg "\nIt appears an error occurred!"}
1931        ShowBigMessage $top $msg $errmsg OK "" $err
1932    }
1933    file delete exptool.in exptool.out
1934    destroy $top
1935}
1936
1937proc sortAddAtoms {phase top sortvar} {
1938    global expgui
1939    set np $top.canvas.fr
1940    set validlist {}
1941    set invalidlist {}
1942    set row 0
1943    while {![catch {grid info $np.e[incr row]t}]} {
1944        set valid 1
1945        set line $row
1946        if !{$expgui(UseAtom$row)} {set valid 0}
1947        lappend line $expgui(UseAtom$row)
1948        if {[set type [string trim [$np.e${row}t get]]] == ""} {set valid 0}
1949        lappend line [string trim [$np.e${row}t get]]
1950        lappend line [string trim [$np.e${row}n get]]
1951        foreach i {x y z o u} {
1952            set tmp [string trim [$np.e${row}$i get]]
1953            lappend line $tmp
1954            if {$tmp == "" || [catch {expr $tmp}]} {set valid 0}
1955        }
1956        if {$valid} {
1957            lappend validlist $line
1958        } else {
1959            lappend invalidlist $line
1960        }
1961    }
1962    switch $sortvar {
1963        type {set sortlist [lsort -index 2 -dictionary $validlist]}
1964        name {set sortlist [lsort -index 3 -dictionary $validlist]}
1965        x {set sortlist [lsort -index 4 -real $validlist]}
1966        y {set sortlist [lsort -index 5 -real $validlist]}
1967        z {set sortlist [lsort -index 6 -real $validlist]}
1968        occ {set sortlist [lsort -index 7 -real $validlist]}
1969        uiso  {set sortlist [lsort -index 8 -real $validlist]}
1970        default {set sortlist $validlist}
1971    }
1972
1973    if {[llength $invalidlist] > 0} {append sortlist " $invalidlist"}
1974    set row 0
1975    foreach line $sortlist {
1976        incr row
1977        set expgui(UseAtom$row) [lindex $line 1]
1978        foreach item [lrange $line 2 end] \
1979                var {t n x y z o u} {
1980            $np.e${row}$var delete 0 end
1981            $np.e${row}$var insert end $item
1982        }
1983    }
1984}
1985# default
1986set newhist(insttype) CW
1987set newhist(dummy) 0
Note: See TracBrowser for help on using the repository browser.