source: trunk/addcmds.tcl @ 378

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

# on 2001/04/03 19:16:41, toby did:
fix bug: if error occurs in adding a phase, don't go on to the add atoms page
check for valid atom types

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