source: trunk/addcmds.tcl @ 487

Last change on this file since 487 was 487, checked in by toby, 13 years ago

# on 2001/11/08 16:00:44, toby did:
fix update flag after Uiso/Uaniso? conversion

  • Property rcs:author set to toby
  • Property rcs:date set to 2001/11/08 16:00:44
  • Property rcs:lines set to +3 -1
  • Property rcs:rev set to 1.25
  • Property rcs:state set to Exp
  • Property svn:keywords set to Author Date Revision Id
File size: 61.7 KB
Line 
1# $Id: addcmds.tcl 487 2009-12-04 23:06:59Z 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.d1.m1 config -text {}
737        $np.d1.m2 config -text {}
738        $np.b1 config -state disabled
739        grid forget $np.l3 $np.e3 $np.cb3 $np.cb4  $np.bank $np.f6a
740        grid $np.dl1 -column 0 -row 8
741        grid $np.d1 -column 1 -row 8 -rowspan 2 -columnspan 4 -sticky e
742        grid $np.dl3 -column 0 -columnspan 99 -row 10 -sticky ew
743        if {$newhist(insttype) == "TOF"} {
744            $np.dl1 config -text "Data range:\n(TOF)"
745            $np.d1.lu config -text millisec
746            grid $np.dl2 -column 0 -row 9
747            catch {
748                set s $newhist(setnum)
749                foreach {x tmin tmax x} $newhist(inst${s}ITYP) {}
750                $np.d1.m1 config -text $tmin
751                $np.d1.m2 config -text $tmax
752            }
753        } elseif {$newhist(insttype) == "CW"} {
754            $np.dl1 config -text "Data range:\n(2Theta)"
755            $np.d1.lu config -text degrees
756            #grid forget $np.dl2
757            $np.d1.m1 config -text >0.
758            $np.d1.m2 config -text <180.
759        } elseif {$newhist(insttype) == "ED"} {
760            $np.dl1 config -text "Data range:\n(Energy)"
761            $np.d1.lu config -text KeV
762            $np.d1.m1 config -text 1.
763            $np.d1.m2 config -text 200.
764            grid $np.dl2 -column 0 -row 9
765        } else {
766            $np.dl1 config -text "No file\nselected"
767            $np.d1.lu config -text {}
768        }
769    } else {
770        foreach var {tmin tmax tstep} {
771            foreach v [ trace vinfo newhist($var)] {
772                eval trace vdelete newhist($var) $v
773            }
774        }
775        grid forget $np.dl1 $np.d1 $np.dl2 $np.dl3
776        foreach w {l1 t1 lbank} {
777            $np.$w config -fg black
778        }
779        $np.b1 config -state normal
780        grid $np.bank -column 2 -row 3 -columnspan 7 -sticky ew
781        grid $np.f6a -column 4 -row 8 -rowspan 2
782        grid $np.l3 -column 0 -row 8 -rowspan 2 
783        grid $np.e3 -column 1 -row 8 -rowspan 2 
784        grid $np.cb3 -column 2 -row 8 -sticky w
785        grid $np.cb4 -column 2 -row 9 -sticky w
786     }
787}
788
789proc ValidateDummyHist {np args} {
790    # validate input
791    global newhist
792    set msg {}
793    $np.dl3 config -text "\n"
794    foreach e {e1 e2 e3} v {tmin tmax tstep} {
795        if [catch {expr $newhist($v)}] {
796            $np.d1.$e config -fg red
797            append msg "Value of $newhist($v) is invalid for $v\n"
798        } else {
799            $np.d1.$e config -fg black
800        }
801    }
802    if {[catch {expr $newhist(setnum)}]} {
803        append msg "An instrument file bank number must be selected\n"
804    } elseif {$newhist(setnum) <= 0 || \
805            $newhist(setnum) > $newhist(instbanks)} {
806        append msg "An invalid instrument file bank has been selected\n"
807    }
808
809    if {$msg != ""} {return $msg}
810
811    if {$newhist(tmax) <= $newhist(tmin)} {
812        $np.d1.e1 config -fg red
813        $np.d1.e2 config -fg red
814        return "Tmax <= Tmin\n"
815    }
816
817
818    set dmin -1
819    set dmax -1
820    if {$newhist(insttype) == "TOF"} {
821        catch {
822            set s $newhist(setnum)
823            foreach {x tmin tmax x} $newhist(inst${s}ITYP) {}
824            if {$newhist(tmin) <$tmin } {
825                $np.d1.e1 config -fg red
826                append msg "Min value of $newhist(tmin) msec is invalid.\n"
827            }
828            if {$newhist(tmax) >$tmax } {
829                $np.d1.e2 config -fg red
830                append msg "Max value of $newhist(tmax) msec is invalid.\n"
831            }
832            set s $newhist(setnum)
833            set dmin [expr {1000. * $newhist(tmin) / \
834                    [lindex $newhist(inst${s}ICONS) 0]}]
835            set dmax [expr {1000. * $newhist(tmax) / \
836                    [lindex $newhist(inst${s}ICONS) 0]}]
837        }
838    } elseif {$newhist(insttype) == "CW"} {
839        if {$newhist(tmin) <= 0 } {
840            $np.d1.e1 config -fg red
841            append msg "Min value of $newhist(tmin) degrees is invalid.\n"
842        }
843        if {$newhist(tmax) >=180 } {
844            $np.d1.e2 config -fg red
845            append msg "Max value of $newhist(tmax) degrees is invalid.\n"
846        }
847        catch {
848            set s $newhist(setnum)
849            set dmin [expr {[lindex $newhist(inst${s}ICONS) 0]\
850                    * 0.5 / sin(acos(0.)*$newhist(tmax)/180.)}]
851            set dmax [expr {[lindex $newhist(inst${s}ICONS) 0]\
852                    * 0.5 / sin(acos(0.)*$newhist(tmin)/180.)}]
853        }
854    } else {
855        if {$newhist(tmin) <1 } {
856            $np.d1.e1 config -fg red
857            append msg "Min value of $newhist(tmin) KeV is invalid.\n"
858        }
859        if {$newhist(tmax) >200 } {
860            $np.d1.e2 config -fg red
861            append msg "Max value of $newhist(tmax) KeV is invalid.\n"
862        }
863        catch {
864            set s $newhist(setnum)
865            set ang [lindex $newhist(inst${s}ICONS) 0]
866            set dmin [expr {12.398/ (2.0*sin($ang*acos(0.)/180) * \
867                    $newhist(tmax))}]
868            set dmax [expr {12.398/ (2.0*sin($ang*acos(0.)/180) * \
869                    $newhist(tmin))}]
870        }
871    }
872    if {$msg != ""} {return $msg}
873    set pnts -1
874    catch {
875        set pnts [expr {1+int(($newhist(tmax) - $newhist(tmin))/$newhist(tstep))}]
876        set qmin [expr {4.*acos(0)/$dmax}]
877        set qmax [expr {4.*acos(0)/$dmin}]
878    }
879    if {$pnts <= 0} {
880        $np.d1.e3 config -fg red
881        append msg "Step value of $newhist(tstep) is invalid.\n"
882    }
883    if {$pnts >20000} {
884        $np.d1.e3 config -fg red
885        append msg "Step value of $newhist(tstep) is too small (>20000 points).\n"
886    }
887    if {$msg != ""} {return $msg}
888    if {$dmin > 0 && $dmax > 0} {
889        catch {
890            set msg [format \
891                    {  %d points.%s  D-space range: %.2f-%.2f A,  Q: %.2f-%.2f/A} \
892                    $pnts "\n" $dmin $dmax $qmin $qmax]
893            $np.dl3 config -text $msg
894        }
895    }
896    if {$msg != ""} {return ""}
897    $np.dl3 config -text [format {  %d points.%s  Range: ?} $pnts "\n"]
898    return "Invalid data range -- something is wrong!"
899}
900
901proc AddDummyHist {np} {
902    global newhist expgui expmap
903    global tcl_platform
904    set msg [ValidateDummyHist $np]
905    if {$msg != ""} {
906        MyMessageBox -parent $np -title  "Add Histogram Error" \
907                -message "The following error(s) were found in your input:\n$msg" \
908                -icon error -type ok -default ok \
909                -helplink "expgui3.html AddHistErr"
910        return
911    }
912    set fp [open exptool.in w]
913    puts $fp "D"
914    puts $fp $newhist(instfile)
915    puts $fp $newhist(setnum)
916    if {$newhist(insttype) == "TOF"} {
917        puts $fp "C"
918    }
919    puts $fp $newhist(tmin)
920    puts $fp $newhist(tmax)
921    puts $fp $newhist(tstep)
922    puts $fp "X"
923    puts $fp "0"
924    close $fp
925    # Save the current exp file
926    savearchiveexp
927    # disable the file changed monitor
928    set expgui(expModifiedLast) 0
929    set expnam [file root [file tail $expgui(expfile)]]
930    set err [catch {
931        if {$tcl_platform(platform) == "windows"} {
932            exec [file join $expgui(gsasexe) exptool.exe] $expnam \
933                    < exptool.in >& exptool.out
934        } else {
935            exec [file join $expgui(gsasexe) exptool] $expnam \
936                    < exptool.in >& exptool.out
937        }
938    } errmsg ]
939    # load the revised exp file
940    set oldpowderlist $expmap(powderlist)
941    loadexp $expgui(expfile)
942    set fp [open exptool.out r]
943    set out [read $fp]
944    close $fp
945    if {[llength $oldpowderlist] == [llength $expmap(powderlist)]} {set err 1}
946    if {$errmsg != ""} {
947        append errmsg "\n" $out
948    } else {
949        set errmsg $out
950    }
951    if {[regexp {\(P,H,A\)} $out]} {
952        set msg {You must upgrade the EXPTOOL program.}
953        append msg { This version cannot add dummy histograms.}
954        MyMessageBox -icon error -title "Old EXPTOOL program" \
955                -message $msg -parent $np \
956                -helplink "expguierr.html OldEXPTOOL"
957        # update the documentation & link
958        destroy $np
959    } elseif {$expgui(showexptool) || $err} {
960        set msg "Please review the result from adding the dummy histogram" 
961        if {$err} {append msg "\nIt appears an error occurred!"}
962        ShowBigMessage $np $msg $errmsg OK "" $err
963    } else {
964        destroy $np
965    }
966    file delete exptool.in exptool.out
967}
968
969
970
971#----------- Add Atoms routines ----------------------------------------
972proc MakeAddAtomsBox {phase "atomlist {}"} {
973    global expmap expgui
974
975    # is there room for more atoms? Well, we will check this someday
976    if {$phase == ""} return
977    if {[llength $phase] != 1} return
978
979    set top .newatoms
980    catch {destroy $top}
981    toplevel $top
982    bind $top <Key-F1> "MakeWWWHelp expgui2.html addatoms"
983
984    grid [label $top.l1 -relief groove -bd 4 -anchor center\
985            -text "Adding atoms to phase #$phase"] \
986            -column 0 -row 0 \
987            -sticky we -columnspan 10
988   
989    grid [canvas $top.canvas \
990            -scrollregion {0 0 5000 500} -width 0 -height 250 \
991            -yscrollcommand "$top.scroll set"] \
992            -column 0 -row 2 -columnspan 4 -sticky nsew
993    grid columnconfigure $top 3 -weight 1
994    grid rowconfigure $top 2 -weight 1
995    grid rowconfigure $top 1 -pad 5
996    scrollbar $top.scroll \
997            -command "$top.canvas yview"
998    frame $top.canvas.fr
999    $top.canvas create window 0 0 -anchor nw -window $top.canvas.fr
1000
1001    set np $top.canvas.fr
1002    set row 0
1003    set col 0
1004    grid [label $np.l_${row}0 -text "  #  "] -column $col -row $row
1005    foreach i {Atom\ntype Name x y z Occ Uiso} \
1006            var {type name x y z occ uiso} {
1007        grid [button $np.l_${row}$i -text $i -padx 0 -pady 0 \
1008                -command "sortAddAtoms $phase $top $var"] \
1009                -column [incr col] -row $row -sticky nsew
1010    }
1011    grid [label $np.l_${row}Use -text Use\nFlag] -column [incr col] -row $row
1012
1013    set expgui(SetAddAtomsScroll) 0
1014    set i [llength $atomlist]
1015    if {$i == 0} {incr i}
1016    for {set j 0} {$j < $i} {incr j} {
1017        MakeAddAtomsRow $top
1018    }
1019    set row 0
1020    foreach item $atomlist {
1021        incr row
1022        foreach val $item w {n x y z t o u} {
1023            if {$val != ""} {
1024                $np.e${row}$w delete 0 end
1025                $np.e${row}$w insert end $val
1026            }
1027        }
1028    }
1029    bind $top <Configure> "SetAddAtomsScroll $top"
1030    grid rowconfigure $top 3 -min 10
1031    grid [button $top.b1 -text "Add Atoms"\
1032            -command "addatom $phase $top"] -column 0 -row 5 -sticky w
1033    bind $top <Return> "addatom $phase $top"
1034    grid [button $top.b2 -text Cancel \
1035            -command "destroy $top"] -column 1 -row 5 -sticky w
1036    grid [button $top.help -text Help -bg yellow \
1037            -command "MakeWWWHelp expgui2.html addatoms"] \
1038            -column 0 -columnspan 2 -row 4
1039
1040    # get the input formats if not already defined
1041    GetImportFormats
1042    if {[llength $expgui(importFormatList)] > 0} {
1043        grid [frame $top.fr -bd 4 -relief groove] \
1044                -column 3 -row 5 -columnspan 2 -sticky e
1045        grid [button $top.fr.b3 -text "Import atoms from: " \
1046                -command "ImportAtoms \$expgui(importFormat) $top $phase"] \
1047                -column 0 -row 0 -sticky e
1048        eval tk_optionMenu $top.fr.b4 expgui(importFormat) \
1049                $expgui(importFormatList)
1050        grid $top.fr.b4 -column 1 -row 0 -sticky w
1051        grid rowconfig $top.fr 0 -pad 10
1052        grid columnconfig $top.fr 0 -pad 10
1053        grid columnconfig $top.fr 1 -pad 10
1054    }
1055
1056    grid [button $top.b3 -text  "More atom boxes" \
1057            -command "MakeAddAtomsRow $top"] -column 3 \
1058            -columnspan 2 -row 4 -sticky e
1059   
1060    wm title $top "add new atom"
1061
1062    # set grab, etc.
1063    putontop $top
1064
1065    tkwait window $top
1066
1067    # fix grab...
1068    afterputontop
1069}
1070
1071proc MakeAddAtomsRow {top} {
1072    set np $top.canvas.fr
1073    set col -1
1074    set row 1
1075    # find an empty row
1076    while {![catch {grid info $np.e${row}t}]} {incr row}
1077    grid [label $np.e${row}num -text $row] -column [incr col]  -row $row
1078    grid [entry $np.e${row}t -width 5] -column [incr col]  -row $row
1079    grid [entry $np.e${row}n -width 8] -column [incr col]  -row $row
1080    foreach i {x y z o u} {
1081        grid [entry $np.e${row}$i -width 9] -column [incr col] -row $row
1082    }
1083    grid [checkbutton $np.e${row}use -variable expgui(UseAtom$row)] \
1084            -column [incr col] -row $row
1085    # default occupancy
1086    $np.e${row}o delete 0 end
1087    $np.e${row}o insert end 1.0
1088    # default Uiso
1089    $np.e${row}u delete 0 end
1090    $np.e${row}u insert end 0.025
1091    # default label
1092    $np.e${row}n delete 0 end
1093    $np.e${row}n insert end (default)
1094    # use by default
1095    $np.e${row}use select
1096
1097    SetAddAtomsScroll $top
1098    return $row
1099}
1100
1101proc SetAddAtomsScroll {top} {
1102    global expgui
1103    if $expgui(SetAddAtomsScroll) return
1104    # prevent reentrance
1105    set expgui(SetAddAtomsScroll) 1
1106    update
1107    set sizes [grid bbox $top.canvas.fr]
1108    $top.canvas config -scrollregion $sizes -width [lindex $sizes 2]
1109    # use the scroll for BIG atom lists
1110    if {[lindex $sizes 3] > [winfo height $top.canvas]} {
1111        grid $top.scroll -sticky ns -column 4 -row 2
1112    } else {
1113        grid forget $top.scroll 
1114    }
1115    update
1116    set expgui(SetAddAtomsScroll) 0
1117}
1118
1119proc addatom {phase top} {
1120    global expgui env expmap
1121    set np $top.canvas.fr
1122    set row 0
1123    # loop over the defined rows
1124    set err {}
1125    set atomlist {}
1126    set validatmtypes {
1127        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
1128        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
1129        NE_20 NE_21 NE_22 NA NA+1 NA_23 MG MG+2 MG_24 MG_25 MG_26 AL AL+3
1130        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
1131        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
1132        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
1133        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
1134        FE+2 FE+3 FE_54 FE_56 FE_57 FE_58 CO CO+2 CO+3 CO_59 NI NI+2 NI+3
1135        NI_58 NI_60 NI_61 NI_62 NI_64 CU CU+1 CU+2 CU_63 CU_65 ZN ZN+2 ZN_64
1136        ZN_66 ZN_67 ZN_68 GA GA+3 GE GE+4 AS AS_75 SE BR BR-1 BR_79 BR_81 KR
1137        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
1138        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
1139        AG+2 CD CD+2 CD_112 CD_113 CD_114 CD_116 IN IN+3 IN_113 IN_115 SN SN+2
1140        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
1141        CE+3 CE+4 PR PR+3 PR+4 PR_141 ND ND+3 PM PM+3 PM_147 SM SM+3 SM_152
1142        SM_154 EU EU+2 EU+3 EU_153 GD GD+3 GD_160 TB TB+3 TB_159 DY DY+3 HO
1143        HO+3 HO_165 ER ER+3 TM TM+3 TM_169 YB YB+2 YB+3 LU LU+3 HF HF+4 TA
1144        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
1145        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
1146        PO_210 AT AT_210 RN RN_222 FR FR_223 RA RA+2 RA_226 AC AC+3 AC_227 TH
1147        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
1148        NP_237 PU PU+3 PU+4 PU+6 PU_239 PU_240 PU_242 AM AM_243 CM CM_244 BK
1149        BK_247 CF CF_249
1150    }
1151    while {![catch {grid info $np.e[incr row]t}]} {
1152        if !{$expgui(UseAtom$row)} continue
1153        # ignore blank entries
1154        set line {}
1155        foreach i {t x y z} {
1156            append line [string trim [$np.e${row}$i get]]
1157        }
1158        if {$line == ""} continue
1159        # validate the input
1160        if {[set type [string trim [$np.e${row}t get]]] == ""} {
1161            append err "  line $row: No atom type specified\n"
1162        }
1163        if {[lsearch $validatmtypes [string toupper $type]] == -1} {
1164            append err "  line $row: Atom type $type is invalid for GSAS\n"
1165        }
1166        set name [string trim [$np.e${row}n get]]
1167        if {$name == "(default)"} {set name "/"}
1168        if {$name == ""} {set name "/"}
1169        foreach i {x y z o u} n {x y z Occ Uiso} {
1170            if {[set $i [string trim [$np.e${row}$i get]]] == ""} {
1171                append err "  line $row: No value specified for $n\n"
1172            } elseif {[catch {expr [set $i]}]} {
1173                append err "  line $row: The value for $n is invalid\n"
1174            }
1175        }
1176        lappend atomlist "$type $x $y $z $o $name I $u"
1177    }   
1178    if {$err != ""} {
1179        MyMessageBox -icon warning -message "Note Errors:\n$err" -parent $top
1180        return
1181    }
1182    if {[llength $atomlist] == 0} {
1183        MyMessageBox -icon warning -message "No atoms to load!" -parent $top
1184        return
1185    }
1186    # ok add the atoms!
1187    set fp [open exptool.in w]
1188    puts $fp "A"
1189    puts $fp $phase
1190    # number of atoms
1191    puts $fp [llength $atomlist]
1192    foreach atomline $atomlist {
1193        puts $fp $atomline
1194    }
1195    close $fp
1196    # needed in UNIX
1197    set env(ATOMDATA) [file join $expgui(gsasdir) data atmdata.dat]
1198    set env(gsas) [file nativename $expgui(gsasdir)]
1199    # needed in Windows
1200    set env(GSAS) [file nativename $expgui(gsasdir)]
1201
1202    global tcl_platform
1203    # Save the current exp file
1204    savearchiveexp
1205    # disable the file changed monitor
1206    set expgui(expModifiedLast) 0
1207    set expnam [file root [file tail $expgui(expfile)]]
1208    catch {
1209        if {$tcl_platform(platform) == "windows"} {
1210            exec [file join $expgui(gsasexe) exptool.exe] $expnam \
1211                    < exptool.in >& exptool.out
1212        } else {
1213            exec [file join $expgui(gsasexe) exptool] $expnam \
1214                    < exptool.in >& exptool.out
1215        }
1216    } errmsg
1217    # load the revised exp file
1218    set oldatomlist $expmap(atomlist_$phase)
1219    loadexp $expgui(expfile)
1220    set fp [open exptool.out r]
1221    set out [read $fp]
1222    close $fp
1223    destroy $top
1224    set err 0
1225    if {[llength $oldatomlist] == [llength $expmap(atomlist_$phase))]} {
1226        set err 1
1227    }
1228    if {$errmsg != ""} {
1229        append errmsg "\n" $out
1230        set err 1
1231    } else {
1232        set errmsg $out
1233    }
1234    if {$expgui(showexptool) || $err} {
1235        set msg "Please review the result from adding the atom(s)" 
1236        if {$err} {append msg "\nIt appears an error occurred!"}
1237        ShowBigMessage $top $msg $errmsg OK "" $err
1238    }
1239    file delete exptool.in exptool.out
1240}
1241
1242#---------------------------------------------------------------------------
1243# commands to modify a group of selected atoms
1244#---------------------------------------------------------------------------
1245
1246# make the dialog to choose an action
1247proc MakeXformAtomsBox {phase} {
1248    global expgui expmap
1249    set numberList {}
1250    set p $expgui(curPhase)
1251    foreach AtomIndex $expgui(selectedatomlist) {
1252        # get atom number & phase
1253        set tuple [lindex $expmap(atomlistboxcontents) $AtomIndex]
1254        lappend numberList [lindex $tuple 0]
1255    }
1256    if {$numberList == ""} return
1257    if {[llength $numberList] > 1} {
1258        set suffix s
1259        set suffixy "ies"
1260    } else {
1261        set suffix ""
1262        set suffixy "y"
1263    }
1264    set w .global
1265    catch {destroy $w}
1266    toplevel $w
1267    wm title $w "Edit Atomic Parameter -- phase #$phase"
1268    bind $w <Key-F1> "MakeWWWHelp expgui2.html xform"
1269    # this needs to track by phase
1270    grid [label $w.0 \
1271            -text "Modifying atom${suffix} [CompressList $numberList] Phase $phase" \
1272            -bg yellow -anchor center] -row 0 -column 0 -columnspan 10 \
1273            -sticky nsew
1274    grid rowconfigure $w 0 -pad 5
1275    grid rowconfigure $w 1 -minsize 2
1276
1277    grid [TitleFrame $w.1 -bd 6 -relief groove -text "Modify coordinates"] \
1278            -row 2 -column 0 -columnspan 10 -sticky news
1279    set w1 [$w.1 getframe]
1280    set row 0
1281    foreach v {x y z} {
1282        incr row
1283        set col -1
1284        grid [label $w1.l$v -text "new $v   =   "] -column [incr col] -row $row
1285        foreach o {x y z} {
1286            grid [entry $w1.e${v}${o} -width 6] -column [incr col] -row $row
1287            $w1.e${v}${o} delete 0 end
1288            if {$v == $o} {
1289                $w1.e${v}${o} insert end "1.0"
1290            } else {
1291                $w1.e${v}${o} insert end "0."
1292            }
1293            grid [label $w1.p${v}${o} -text " $o  +  "] \
1294                    -column [incr col] -row $row
1295        }
1296        grid [entry $w1.e${v} -width 6] -column [incr col] -row $row
1297        $w1.e${v} delete 0 end
1298        $w1.e${v} insert end "0."
1299    }
1300    grid [button $w1.do -text "Transform Coordinates" \
1301            -command "XformAtomsCoord $phase [list $numberList] $w1" \
1302            ] -row [incr row] -column 0 -columnspan 10
1303
1304    grid rowconfigure $w 3 -minsize 5
1305    grid [TitleFrame $w.4 -bd 6 -relief groove -text "Modify occupanc${suffixy}"] \
1306            -row 4 -column 0 -columnspan 10 -sticky news
1307    set w2 [$w.4 getframe]
1308    grid [label $w2.1 -text "Occupancy: "] -row 1 -column 0
1309    grid [entry $w2.e -width 10] -column 1 -row 1
1310    $w2.e delete 0 end
1311    $w2.e insert end 1.0
1312    grid columnconfigure $w2 2 -weight 1
1313    grid [button $w2.do -text "Set Occupanc${suffixy}" \
1314            -command "XformAtomsOcc $phase [list $numberList] $w2" \
1315            ] -row 2 -column 0 -columnspan 10
1316
1317    grid rowconfigure $w 5 -minsize 5
1318    grid [TitleFrame $w.6 -bd 6 -relief groove \
1319            -text "Modify Displacement Parameter$suffix"] \
1320            -row 6 -column 0 -columnspan 10 -sticky news
1321    set w2 [$w.6 getframe]
1322    grid [label $w2.1 -text "Uiso or Uequiv: "] -row 1 -column 0
1323    grid [entry $w2.e -width 10] -column 1 -row 1
1324    $w2.e delete 0 end
1325    $w2.e insert end 0.025
1326    grid columnconfigure $w2 2 -weight 1
1327    grid [button $w2.do -text "Set U" \
1328            -command "XformAtomsU $phase [list $numberList] $w2" \
1329            ] -row 2 -column 0 -columnspan 10
1330    grid [frame $w2.f] -row 3 -column 0 -columnspan 10
1331    grid [button $w2.f.iso -text "Set Isotropic" \
1332            -command "XformAtomsU $phase [list $numberList] iso" \
1333            ] -row 0 -column 0
1334    grid [button $w2.f.aniso -text "Set Anisotropic" \
1335            -command "XformAtomsU $phase [list $numberList] aniso" \
1336            ] -row 0 -column 1
1337
1338    grid rowconfigure $w 5 -minsize 5
1339    grid [TitleFrame $w.8 -bd 6 -relief groove \
1340            -text "Erase Atom$suffix"] \
1341            -row 8 -column 0 -columnspan 10 -sticky news
1342    set w2 [$w.8 getframe]
1343    grid [button $w2.do -text "Erase Atom${suffix}" \
1344            -command "EraseAtoms $phase [list $numberList] $w" \
1345            ] -row 2 -column 0 -columnspan 10
1346
1347
1348    grid rowconfigure $w 9 -minsize 5
1349    grid [frame $w.b] -row 10 -column 0 -columnspan 10 -sticky ew
1350    pack [button $w.b.3 -text Close -command "destroy $w"] -side left \
1351            -padx 5 -pady 5
1352    pack [button $w.b.help -text Help -bg yellow \
1353            -command "MakeWWWHelp expgui2.html xform"] -side right \
1354            -padx 5 -pady 5
1355    bind $w <Return> "destroy $w"
1356
1357    # force the window to stay on top
1358    putontop $w
1359    focus $w.b.3
1360    tkwait window $w
1361    afterputontop
1362    # if there are selected atoms, reset their display
1363    if {[llength $expgui(selectedatomlist)] != 0} editRecord
1364}
1365
1366# transform the coordinates
1367proc XformAtomsCoord {phase numberList w1} {
1368    global expgui
1369    # get the matrix
1370    foreach v {x y z} {
1371        foreach o {x y z} {
1372            set matrix(${v}${o}) [$w1.e${v}${o} get]
1373        }
1374        set matrix(${v}) [$w1.e${v} get]
1375    }
1376    foreach atom $numberList {
1377        foreach v {x y z} {
1378            set $v [atominfo $phase $atom $v]
1379        }
1380        foreach v {x y z} {
1381            set new$v $matrix(${v})
1382            foreach o {x y z} {
1383                set new$v [expr [set new$v] + $matrix(${v}${o})*[set $o]]
1384            }
1385            atominfo $phase $atom $v set [set new$v]
1386        }
1387        incr expgui(changed)
1388    }
1389    DisplayAllAtoms noreset
1390}
1391
1392# set the occupancies to a single value
1393proc XformAtomsOcc {phase numberList w2} {
1394    global expgui
1395    # get the value
1396    set val [$w2.e get]
1397    foreach atom $numberList {
1398        atominfo $phase $atom frac set $val
1399        incr expgui(changed)
1400    }
1401    DisplayAllAtoms noreset
1402}
1403
1404# transform Uiso or Uij; if anisotropic set Uequiv to Uij
1405proc XformAtomsU {phase numberList w2} {
1406    global expgui
1407    if {$w2 == "iso"} {
1408        foreach atom $numberList {
1409            if {[atominfo $phase $atom temptype] != "I"} {
1410                atominfo $phase $atom temptype set I
1411                incr expgui(changed)
1412            }
1413        }
1414    } elseif {$w2 == "aniso"} {
1415        foreach atom $numberList {
1416            if {[atominfo $phase $atom temptype] == "I"} {
1417                atominfo $phase $atom temptype set A
1418                incr expgui(changed)
1419            }
1420        }
1421    } else {
1422        # get the value
1423        set val [$w2.e get]
1424        foreach atom $numberList {
1425            if {[atominfo $phase $atom temptype] == "I"} {
1426                atominfo $phase $atom Uiso set $val
1427            } else {
1428                atominfo $phase $atom U11 set $val
1429                atominfo $phase $atom U22 set $val
1430                atominfo $phase $atom U33 set $val
1431                atominfo $phase $atom U12 set 0.0
1432                atominfo $phase $atom U13 set 0.0
1433                atominfo $phase $atom U23 set 0.0
1434            }
1435            incr expgui(changed)
1436        }
1437    }
1438    DisplayAllAtoms noreset
1439}
1440
1441# confirm and erase atoms
1442proc EraseAtoms {phase numberList w2} {
1443    global expgui
1444    if {[llength $numberList] <= 0} return
1445    # make a list of atoms
1446    foreach atom $numberList {
1447        append atomlist "\n\t$atom  [atominfo $phase $atom label]"
1448    }
1449    set msg "OK to remove the following [llength $numberList] atoms from phase $phase:$atomlist"
1450    set val [MyMessageBox -parent $w2 -type okcancel -icon warning \
1451            -default cancel -title "Confirm Erase" -message $msg]
1452    if {$val == "ok"} {
1453        foreach atom $numberList {
1454            EraseAtom $atom $phase
1455            incr expgui(changed)
1456        }
1457        mapexp
1458        DisplayAllAtoms
1459        destroy $w2
1460    }
1461}
1462
1463#----------- more Add Phase routines (import) -------------------------------
1464proc ImportPhase {format np} {
1465    global expgui
1466    foreach item $expgui(extensions_$format) {
1467        lappend typelist [list $format $item]
1468    }
1469    lappend typelist [list "All files" *]
1470    set file [tk_getOpenFile -parent $np -filetypes $typelist]
1471    if {![file exists $file]} return
1472    # read in the file
1473    set input [$expgui(proc_$format) $file]
1474    catch {
1475        $np.bf.b1 config -text "Continue" -command "addphase $np; AddAtomsList"
1476        bind $np <Return> "addphase $np; AddAtomsList"
1477    }
1478    catch {
1479        $np.t1 delete 0 end
1480        $np.t1 insert end "from $file"
1481    }
1482    $np.t2 delete 0 end
1483    $np.t2 insert end [lindex $input 0]
1484    foreach i {.e1a .e1b .e1c .e2a .e2b .e2g} val [lindex $input 1] {
1485        $np.f$i delete 0 end
1486        $np.f$i insert end $val
1487    }
1488    set expgui(coordList) [lindex $input 2]
1489    set msg [lindex $input 3]
1490    if {$msg != ""} {
1491        catch {destroy $np.msg}
1492        grid [label $np.msg -text $msg -fg red -anchor center -bd 4 -relief raised] \
1493                -column 0 -columnspan 99 -row 20 -sticky ew
1494    }
1495}
1496
1497proc ImportAtoms {format top phase} {
1498    global expgui
1499    foreach item $expgui(extensions_$format) {
1500        lappend typelist [list $format $item]
1501    }
1502    lappend typelist [list "All files" *]
1503    set file [tk_getOpenFile -parent $top -filetypes $typelist]
1504    if {![file exists $file]} return
1505    # read in the file
1506    set input [$expgui(proc_$format) $file]
1507    # add atoms to table
1508    foreach item [lindex $input 2] {
1509        set row [MakeAddAtomsRow $top]
1510        set np $top.canvas.fr
1511        foreach val $item w {n x y z t o u} {
1512            if {$val != ""} {
1513                $np.e${row}$w delete 0 end
1514                $np.e${row}$w insert end $val
1515            }
1516        }
1517    }
1518    # sort the atoms by number, so that empty entries are at the bottom
1519    sortAddAtoms $phase $top number
1520}
1521
1522proc AddAtomsList {} {
1523    global expgui expmap
1524    # skip if we aborted out of addphase
1525    if {$expgui(oldphaselist) == -1} return
1526    # find the new phase
1527    set phase {}
1528    foreach p $expmap(phaselist) {
1529        if {[lsearch $expgui(oldphaselist) $p] == -1} {
1530            set phase $p
1531            break
1532        }
1533    }
1534    if {$phase == ""} return
1535    MakeAddAtomsBox $phase $expgui(coordList)
1536}
1537
1538# get the input formats by sourcing files named import_*.tcl
1539proc GetImportFormats {} {
1540    global expgui tcl_platform
1541    # only needs to be done once
1542    if [catch {set expgui(importFormatList)}] {
1543        set filelist [glob -nocomplain [file join $expgui(scriptdir) import_*.tcl]]
1544        foreach file $filelist {
1545            source $file
1546            lappend expgui(importFormatList) $description
1547            if {$tcl_platform(platform) == "unix"} {
1548                set extensions "[string tolower $extensions] [string toupper $extensions]"
1549            }
1550            set expgui(extensions_$description) $extensions
1551            set expgui(proc_$description) $procname
1552        }
1553    }
1554}
1555
1556proc MakeReplacePhaseBox {} {
1557    global expmap expgui tcl_platform
1558
1559    set expgui(coordList) {}
1560    # ignore the command if no phase is selected
1561    foreach p {1 2 3 4 5 6 7 8 9} {
1562        if {[lsearch $expmap(phaselist) $expgui(curPhase)] == -1} {
1563            return
1564        }
1565    }
1566
1567    set top .newphase
1568    catch {destroy $top}
1569    toplevel $top
1570    bind $top <Key-F1> "MakeWWWHelp expgui2.html replacephase"
1571
1572    grid [label $top.l1 -text "Replacing phase #$expgui(curPhase)" \
1573            -bg yellow -anchor center] -column 0 -columnspan 8 -row 0 -sticky ew
1574    grid [label $top.l3a -text "Current Space Group: "] \
1575            -column 0 -row 2 -columnspan 2 -sticky e
1576    grid [label $top.l3b -text [phaseinfo $expgui(curPhase) spacegroup]\
1577            -bd 4 -relief groove] \
1578            -column 2 -row 2  -sticky ew
1579    grid [label $top.l4 -text "New Space Group: "] \
1580            -column 0 -row 3 -columnspan 2 -sticky e
1581    grid [entry $top.t2 -width 12] -column 2 -row 3 -sticky w
1582    grid [radiobutton $top.r1 -text "Reenter current atoms"\
1583            -variable expgui(DeleteAllAtoms) -value 0] \
1584            -column 1 -row 4 -columnspan 8 -sticky w
1585    grid [radiobutton $top.r2 -text "Delete current atoms" \
1586            -variable expgui(DeleteAllAtoms) -value 1] \
1587            -column 1 -row 5 -columnspan 8 -sticky w
1588   
1589    grid [frame $top.f -bd 4 -relief groove] \
1590            -column 3 -row 2 -columnspan 3 -rowspan 4
1591    set col -1
1592    foreach i {a b c} {
1593        grid [label $top.f.l1$i -text " $i "] -column [incr col] -row 1
1594        grid [entry $top.f.e1$i -width 12] -column [incr col]  -row 1
1595        $top.f.e1$i delete 0 end
1596        $top.f.e1$i insert 0 [phaseinfo $expgui(curPhase) $i]
1597    }
1598    set col -1
1599    foreach i {a b g} var {alpha beta gamma} {
1600        grid [label $top.f.l2$i -text $i] -column [incr col] -row 2
1601        set font [$top.f.l2$i cget -font]
1602        $top.f.l2$i config -font "Symbol [lrange $font 1 end]"
1603        grid [entry $top.f.e2$i -width 12] -column [incr col]  -row 2
1604        $top.f.e2$i delete 0 end
1605        $top.f.e2$i insert 0 [phaseinfo $expgui(curPhase) $var]
1606    } 
1607
1608    grid [button $top.b1 -text Continue \
1609            -command "replacephase1 $top $expgui(curPhase)"] \
1610            -column 0 -row 6 -sticky w
1611    bind $top <Return> "replacephase1 $top $expgui(curPhase)"
1612    grid [button $top.b2 -text Cancel \
1613            -command "destroy $top"] -column 1 -row 6 -sticky w
1614    grid [button $top.help -text Help -bg yellow \
1615            -command "MakeWWWHelp expgui2.html replacephase"] \
1616            -column 2 -row 6
1617
1618    # get the input formats if not already defined
1619    GetImportFormats
1620    if {[llength $expgui(importFormatList)] > 0} {
1621        grid [frame $top.fr -bd 4 -relief groove] \
1622                -column 2 -row 6 -columnspan 8 -sticky e
1623        grid [button $top.fr.b3 -text "Import phase from: " \
1624                -command "ImportPhase \$expgui(importFormat) $top"] \
1625                -column 0 -row 0 -sticky e
1626        eval tk_optionMenu $top.fr.b4 expgui(importFormat) \
1627                $expgui(importFormatList)
1628        grid $top.fr.b4 -column 1 -row 0 -sticky w
1629        grid rowconfig $top.fr 0 -pad 10
1630        grid columnconfig $top.fr 0 -pad 10
1631        grid columnconfig $top.fr 1 -pad 10
1632#       grid columnconfig $top 4 -weight 1
1633        grid columnconfig $top 2 -weight 1
1634    }
1635   
1636    wm title $top "Replace phase $expgui(curPhase)"
1637
1638    # set grab, etc.
1639    putontop $top
1640
1641    tkwait window $top
1642
1643    # fix grab...
1644    afterputontop
1645}
1646
1647proc replacephase1 {top phase} {
1648    # validate cell & space group & save to pass
1649    global expgui expmap
1650    set expgui(SetAddAtomsScroll) 0
1651    # validate the input
1652    set err {}
1653    set spg [$top.t2 get]
1654    if {[string trim $spg] == ""} {
1655        append err "  Space group cannot be blank\n"
1656    }
1657    set cell {}
1658    foreach i {a b c a b g} lbl {a b c alpha beta gamma} n {1 1 1 2 2 2} {
1659        set $lbl [$top.f.e${n}$i get]
1660        if {[string trim [set $lbl]] == ""} {
1661            append err "  $lbl cannot be blank\n"
1662        } elseif {[catch {expr [set $lbl]}]} {
1663            append err "  [set $lbl] is not valid for $lbl\n"
1664        }
1665        lappend cell [set $lbl]
1666    }
1667
1668    if {$err != ""} {
1669        MyMessageBox -parent $top -title "Replace Phase Error" -icon warning \
1670                -message "The following error(s) were found in your input:\n$err" 
1671        return
1672    }
1673
1674    # check the space group
1675    set fp [open spg.in w]
1676    puts $fp "N"
1677    puts $fp "N"
1678    puts $fp $spg
1679    puts $fp "Q"
1680    close $fp
1681    global tcl_platform
1682    catch {
1683        if {$tcl_platform(platform) == "windows"} {
1684            exec [file join $expgui(gsasexe) spcgroup.exe] < spg.in >& spg.out
1685        } else {
1686            exec [file join $expgui(gsasexe) spcgroup] < spg.in >& spg.out
1687        }
1688    }
1689    set fp [open spg.out r]
1690    set out [read $fp]
1691    close $fp
1692    # attempt to parse out the output (fix up if parse did not work)
1693    if {[regexp "space group symbol.*>(.*)Enter a new space group symbol" \
1694            $out a b ] != 1} {set b $out}
1695    if {[string first Error $b] != -1} {
1696        # got an error, show it
1697        ShowBigMessage \
1698                 $top.error \
1699                 "Error processing space group\nReview error message below" \
1700                 $b OK "" 1
1701        return
1702    } else {
1703        # show the result and confirm
1704        set opt [ShowBigMessage \
1705                $top.check \
1706                "Check the symmetry operators in the output below" \
1707                $b \
1708                {Continue Redo} ]
1709        if {$opt > 1} return
1710    }
1711    file delete spg.in spg.out
1712    # draw coordinates box
1713    eval destroy [winfo children $top]
1714    grid [label $top.l1 -relief groove -bd 4 -anchor center\
1715            -text "Atom list for phase #$phase"] \
1716            -column 0 -row 0 \
1717            -sticky we -columnspan 10
1718    grid [canvas $top.canvas \
1719            -scrollregion {0 0 5000 500} -width 0 -height 250 \
1720            -yscrollcommand "$top.scroll set"] \
1721            -column 0 -row 2 -columnspan 4 -sticky nsew
1722    grid columnconfigure $top 3 -weight 1
1723    grid rowconfigure $top 2 -weight 1
1724    grid rowconfigure $top 1 -pad 5
1725    scrollbar $top.scroll \
1726            -command "$top.canvas yview"
1727    frame $top.canvas.fr
1728    $top.canvas create window 0 0 -anchor nw -window $top.canvas.fr
1729
1730    set np $top.canvas.fr
1731    set row 0
1732    set col 0
1733    grid [label $np.l_${row}0 -text "  #  "] -column $col -row $row
1734    foreach i {Atom\ntype Name x y z Occ Uiso} \
1735            var {type name x y z occ uiso} {
1736        grid [button $np.l_${row}$i -text $i -padx 0 -pady 0 \
1737                -command "sortAddAtoms $phase $top $var"] \
1738                -column [incr col] -row $row -sticky nsew
1739    }
1740    grid [label $np.l_${row}Use -text Use\nFlag] -column [incr col] -row $row
1741
1742    # add the old atoms, if appropriate
1743    if {!$expgui(DeleteAllAtoms)} {
1744        # loop over all atoms
1745        foreach atom $expmap(atomlist_$phase) {
1746            set row [MakeAddAtomsRow $top]
1747            # add all atoms in the current phase to the list
1748            foreach w {n x y z t o} var {label x y z type frac} {
1749                $np.e${row}$w delete 0 end
1750                $np.e${row}$w insert end [atominfo $phase $atom $var]
1751            }
1752            $np.e${row}u delete 0 end
1753            if {[atominfo $phase $atom temptype] == "I"} {
1754                $np.e${row}u insert end [atominfo $phase $atom Uiso]
1755            } else {
1756                $np.e${row}u insert end [expr ( \
1757                        [atominfo $phase $atom U11] + \
1758                        [atominfo $phase $atom U22] + \
1759                        [atominfo $phase $atom U33]) / 3.]
1760            }
1761        }
1762    }
1763
1764    # add coordinates that have been read in, if any
1765    foreach item $expgui(coordList) {
1766        set row [MakeAddAtomsRow $top]
1767        foreach val $item w {n x y z t o u} {
1768            if {$val != ""} {
1769                $np.e${row}$w delete 0 end
1770                $np.e${row}$w insert end $val
1771            }
1772        }
1773    }
1774    # a blank spot in the table
1775    MakeAddAtomsRow $top
1776
1777    bind $top <Configure> "SetAddAtomsScroll $top"
1778    grid rowconfigure $top 3 -min 10
1779    grid [button $top.b1 -text "Continue"\
1780            -command "replacephase2 $phase $top [list $spg] [list $cell]"] \
1781            -column 0 -row 5 -sticky w
1782    bind $top <Return> "replacephase2 $phase $top [list $spg] [list $cell]"
1783    grid [button $top.b2 -text Cancel \
1784            -command "destroy $top"] -column 1 -row 5 -sticky w
1785    if {[llength $expgui(importFormatList)] > 0} {
1786        grid [frame $top.fr -bd 4 -relief groove] \
1787                -column 3 -row 5 -columnspan 2 -sticky e
1788        grid [button $top.fr.b3 -text "Import atoms from: " \
1789                -command "ImportAtoms \$expgui(importFormat) $top $phase"] \
1790                -column 0 -row 0 -sticky e
1791        eval tk_optionMenu $top.fr.b4 expgui(importFormat) \
1792                $expgui(importFormatList)
1793        grid $top.fr.b4 -column 1 -row 0 -sticky w
1794        grid rowconfig $top.fr 0 -pad 10
1795        grid columnconfig $top.fr 0 -pad 10
1796        grid columnconfig $top.fr 1 -pad 10
1797    }
1798
1799    grid [button $top.b3 -text  "More atom boxes" \
1800            -command "MakeAddAtomsRow $top"] -column 3 \
1801            -columnspan 2 -row 4 -sticky e
1802   
1803    wm title $top "Replacing phase: Enter atoms"
1804    SetAddAtomsScroll $top
1805
1806    # fix grab for old window
1807    afterputontop
1808    # set grab, etc.
1809    putontop $top
1810}
1811
1812proc replacephase2 {phase top spg cell} {
1813    global expgui expmap env
1814    # validate coordinates
1815    set np $top.canvas.fr
1816    set row 0
1817    # loop over the defined rows
1818    set err {}
1819    set atomlist {}
1820    set validatmtypes {
1821        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
1822        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
1823        NE_20 NE_21 NE_22 NA NA+1 NA_23 MG MG+2 MG_24 MG_25 MG_26 AL AL+3
1824        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
1825        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
1826        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
1827        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
1828        FE+2 FE+3 FE_54 FE_56 FE_57 FE_58 CO CO+2 CO+3 CO_59 NI NI+2 NI+3
1829        NI_58 NI_60 NI_61 NI_62 NI_64 CU CU+1 CU+2 CU_63 CU_65 ZN ZN+2 ZN_64
1830        ZN_66 ZN_67 ZN_68 GA GA+3 GE GE+4 AS AS_75 SE BR BR-1 BR_79 BR_81 KR
1831        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
1832        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
1833        AG+2 CD CD+2 CD_112 CD_113 CD_114 CD_116 IN IN+3 IN_113 IN_115 SN SN+2
1834        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
1835        CE+3 CE+4 PR PR+3 PR+4 PR_141 ND ND+3 PM PM+3 PM_147 SM SM+3 SM_152
1836        SM_154 EU EU+2 EU+3 EU_153 GD GD+3 GD_160 TB TB+3 TB_159 DY DY+3 HO
1837        HO+3 HO_165 ER ER+3 TM TM+3 TM_169 YB YB+2 YB+3 LU LU+3 HF HF+4 TA
1838        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
1839        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
1840        PO_210 AT AT_210 RN RN_222 FR FR_223 RA RA+2 RA_226 AC AC+3 AC_227 TH
1841        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
1842        NP_237 PU PU+3 PU+4 PU+6 PU_239 PU_240 PU_242 AM AM_243 CM CM_244 BK
1843        BK_247 CF CF_249
1844    }
1845    while {![catch {grid info $np.e[incr row]t}]} {
1846        if !{$expgui(UseAtom$row)} continue
1847        # ignore blank entries
1848        set line {}
1849        foreach i {t x y z} {
1850            append line [string trim [$np.e${row}$i get]]
1851        }
1852        if {$line == ""} continue
1853        # validate the input
1854        if {[set type [string trim [$np.e${row}t get]]] == ""} {
1855            append err "  line $row: No atom type specified\n"
1856        }
1857        if {[lsearch $validatmtypes [string toupper $type]] == -1} {
1858            append err "  line $row: Atom type $type is invalid for GSAS\n"
1859        }
1860        set name [string trim [$np.e${row}n get]]
1861        if {$name == "(default)"} {set name "/"}
1862        if {$name == ""} {set name "/"}
1863        foreach i {x y z o u} n {x y z Occ Uiso} {
1864            if {[set $i [string trim [$np.e${row}$i get]]] == ""} {
1865                append err "  line $row: No value specified for $n\n"
1866            } elseif {[catch {expr [set $i]}]} {
1867                append err "  line $row: The value for $n is invalid\n"
1868            }
1869        }
1870        lappend atomlist "$type $x $y $z $o $name I $u"
1871    }   
1872    if {$err != ""} {
1873        MyMessageBox -icon warning -message "Note Errors:\n$err" -parent $top
1874        return
1875    }
1876    if {[llength $atomlist] == 0} {
1877        MyMessageBox -icon warning -message "No atoms to load!" -parent $top
1878        return
1879    }
1880
1881    pleasewait "updating phase"
1882    # replace spacegroup and cell
1883    phaseinfo $phase spacegroup set $spg
1884    foreach val $cell var {a b c alpha beta gamma} {
1885        phaseinfo $phase $var set $val
1886    }
1887    # delete all atoms
1888    foreach i $expmap(atomlist_$phase) {
1889        EraseAtom $i $phase
1890    }
1891    incr expgui(changed) 8
1892    # write new atoms from table as input to exptool
1893    set fp [open exptool.in w]
1894    puts $fp "A"
1895    puts $fp $phase
1896    # number of atoms
1897    puts $fp [llength $atomlist]
1898    foreach atomline $atomlist {
1899        puts $fp $atomline
1900    }
1901    close $fp
1902    # needed in UNIX
1903    set env(ATOMDATA) [file join $expgui(gsasdir) data atmdata.dat]
1904    set env(gsas) [file nativename $expgui(gsasdir)]
1905    # needed in Windows
1906    set env(GSAS) [file nativename $expgui(gsasdir)]
1907
1908    global tcl_platform
1909    # Save the current exp file
1910    savearchiveexp
1911    # disable the file changed monitor
1912    set expgui(expModifiedLast) 0
1913    set expnam [file root [file tail $expgui(expfile)]]
1914    catch {
1915        if {$tcl_platform(platform) == "windows"} {
1916            exec [file join $expgui(gsasexe) exptool.exe] $expnam \
1917                    < exptool.in >& exptool.out
1918        } else {
1919            exec [file join $expgui(gsasexe) exptool] $expnam \
1920                    < exptool.in >& exptool.out
1921        }
1922    } errmsg
1923    # load the revised exp file
1924    set oldatomlist $expmap(atomlist_$phase)
1925    loadexp $expgui(expfile)
1926    set fp [open exptool.out r]
1927    set out [read $fp]
1928    close $fp
1929    set err 0
1930    if {[llength $oldatomlist] == [llength $expmap(atomlist_$phase))]} {
1931        set err 1
1932    }
1933    if {$errmsg != ""} {
1934        append errmsg "\n" $out
1935        set err 1
1936    } else {
1937        set errmsg $out
1938    }
1939    donewait 
1940    if {$expgui(showexptool) || $err} {
1941        set msg "Please review the result from adding the atom(s)" 
1942        if {$err} {append msg "\nIt appears an error occurred!"}
1943        ShowBigMessage $top $msg $errmsg OK "" $err
1944    }
1945    file delete exptool.in exptool.out
1946    destroy $top
1947}
1948
1949proc sortAddAtoms {phase top sortvar} {
1950    global expgui
1951    set np $top.canvas.fr
1952    set validlist {}
1953    set invalidlist {}
1954    set row 0
1955    while {![catch {grid info $np.e[incr row]t}]} {
1956        set valid 1
1957        set line $row
1958        if !{$expgui(UseAtom$row)} {set valid 0}
1959        lappend line $expgui(UseAtom$row)
1960        if {[set type [string trim [$np.e${row}t get]]] == ""} {set valid 0}
1961        lappend line [string trim [$np.e${row}t get]]
1962        lappend line [string trim [$np.e${row}n get]]
1963        foreach i {x y z o u} {
1964            set tmp [string trim [$np.e${row}$i get]]
1965            lappend line $tmp
1966            if {$tmp == "" || [catch {expr $tmp}]} {set valid 0}
1967        }
1968        if {$valid} {
1969            lappend validlist $line
1970        } else {
1971            lappend invalidlist $line
1972        }
1973    }
1974    switch $sortvar {
1975        type {set sortlist [lsort -index 2 -dictionary $validlist]}
1976        name {set sortlist [lsort -index 3 -dictionary $validlist]}
1977        x {set sortlist [lsort -index 4 -real $validlist]}
1978        y {set sortlist [lsort -index 5 -real $validlist]}
1979        z {set sortlist [lsort -index 6 -real $validlist]}
1980        occ {set sortlist [lsort -index 7 -real $validlist]}
1981        uiso  {set sortlist [lsort -index 8 -real $validlist]}
1982        default {set sortlist $validlist}
1983    }
1984
1985    if {[llength $invalidlist] > 0} {append sortlist " $invalidlist"}
1986    set row 0
1987    foreach line $sortlist {
1988        incr row
1989        set expgui(UseAtom$row) [lindex $line 1]
1990        foreach item [lrange $line 2 end] \
1991                var {t n x y z o u} {
1992            $np.e${row}$var delete 0 end
1993            $np.e${row}$var insert end $item
1994        }
1995    }
1996}
1997# default
1998set newhist(insttype) {}
1999set newhist(dummy) 0
Note: See TracBrowser for help on using the repository browser.