source: trunk/import_cif.tcl

Last change on this file was 1251, checked in by toby, 9 years ago

use svn ps svn:eol-style "native" * to change line ends

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Revision Id
File size: 5.8 KB
Line 
1# $Id: import_cif.tcl 1251 2014-03-10 22:17:29Z toby $
2
3#-------------------------------------------------
4# define info used in addcmds.tcl
5catch {
6    # make sure vars get set at global level
7    global CIF
8    source [file join $expgui(scriptdir) browsecif.tcl]
9    set description "Crystallographic Information File (CIF)"
10    set extensions .cif
11    set procname ReadCIFFile
12}
13#-------------------------------------------------
14
15proc ReadCIFFile {filename} {
16    global expgui CIF
17    set fp [open $filename r]
18    pleasewait "Reading CIF file"         
19
20    catch {destroy [set file .file]}
21    toplevel $file
22    #bind $file <Key-F1> "MakeWWWHelp gsas2cif.html filltemplate"
23    set CIF(txt) $file.t
24    grid [text $CIF(txt) -height 10 -width 80 -yscrollcommand "$file.s set"] \
25            -column 0 -row 0 -sticky news
26    grid [scrollbar $file.s -command "$CIF(txt) yview"] \
27            -column 1 -row 0 -sticky ns
28    grid columnconfig $file 0 -weight 1
29    grid rowconfig $file 0 -weight 1
30    # hide it
31    wm withdraw $file
32    set blocks [ParseCIF $CIF(txt) $filename]
33    if {$blocks == ""} {
34        donewait
35        MyMessageBox -parent . -type ok -icon warning \
36                -message "Note: no valid CIF blocks were read from file $filename"
37        return 
38    }
39    set allblocks {}
40    set coordblocks {}
41    # search each block for coordinate
42    for {set i 1} {$i <= $blocks} {incr i} {
43        lappend allblocks $i
44        global block$i
45        set flag 1
46        foreach id {_atom_site_fract_x _atom_site_fract_y _atom_site_fract_z} {
47            if {[array name block$i $id] == ""} {set flag 0}
48        }
49        if $flag {lappend coordblocks $i}
50    }
51    donewait
52    if {$coordblocks == ""} {
53        MyMessageBox -parent . -type ok -icon warning \
54                -message "Note: CIF $filename contains no coordinates"
55        return
56    }
57    set expgui(choose) [lindex $coordblocks 0]
58    # there is more than one appropriate block
59    if {[llength $coordblocks] > 1} {
60        catch {destroy .choose}
61        toplevel .choose
62        wm title .choose "Choose CIF Block"
63        bind .choose <Key-F1> "MakeWWWHelp expguierr.html ChooseCIF"
64        grid [label .choose.0 -text \
65                "More than one block in CIF $filename\ncontains coordinates.\n\nSelect the block to use" \
66                ] -row 0 -column 0 -columnspan 2
67        set row 0
68        foreach i $coordblocks {
69            incr row
70            set name ""
71            catch {set name [set block${i}(data_)]}
72            grid [radiobutton .choose.$row -value $i \
73                    -text "block $i ($name)" -variable expgui(choose)] \
74                    -row $row -column 0 -sticky w
75        }
76        grid [button .choose.browse -text CIF\nBrowser -command \
77                "CallBrowseCIF $CIF(txt) [list $allblocks] [list $coordblocks] .choose.cif" \
78                ] -row 1 -rowspan $row -column 1 
79        grid [button .choose.ok -text OK -command "destroy .choose"] \
80                -row [incr row] -column 0 -sticky w
81        grid [button .choose.help -text Help -bg yellow \
82            -command "MakeWWWHelp expguierr.html ChooseCIF"] \
83            -column 1 -row $row -sticky e
84        putontop .choose
85        tkwait window .choose
86        # fix grab...
87        afterputontop
88    }
89
90    set i $expgui(choose)
91    # get the space group
92    set spg {}
93    set sgnum {}
94    set msg {}
95    catch {
96        set spg [ValueFromCIF block${i} _symmetry_space_group_name_h-m]
97    }
98    set cell {}
99    foreach var {_cell_length_a _cell_length_b _cell_length_c \
100            _cell_angle_alpha _cell_angle_beta _cell_angle_gamma} {
101        # leave blank any unspecified data items
102        set val {}
103        catch {set val [ValueFromCIF block${i} $var]}
104        lappend cell [lindex [ParseSU $val] 0]
105    }
106   
107    set atomlist {}
108    set lbllist {}
109    catch {
110        set lbllist [ValueFromCIF block${i} _atom_site_label]
111    }
112    set uisolist {}
113    set Uconv 1
114    catch {
115        set uisolist [ValueFromCIF block${i} _atom_site_u_iso_or_equiv]
116    }
117    if {$uisolist == ""} {
118        catch {
119            set uisolist [ValueFromCIF block${i} _atom_site_b_iso_or_equiv]
120            set Uconv [expr 1/(8*3.14159*3.14159)]
121        }
122    }
123    set occlist {}
124    catch {
125        set occlist [ValueFromCIF block${i} _atom_site_occupancy]
126    }
127    set typelist {}
128    catch {
129        set typelist [ValueFromCIF block${i} _atom_site_type_symbol]
130    }
131    foreach x [ValueFromCIF block${i} _atom_site_fract_x] \
132            y [ValueFromCIF block${i} _atom_site_fract_y] \
133            z [ValueFromCIF block${i} _atom_site_fract_z] \
134            lbl $lbllist uiso $uisolist occ $occlist type $typelist {
135        # deal with defaults
136        if {$occ == "" || $occ == "."} {set occ 1.0}
137        if {$uiso == "" || $uiso == "."} {set uiso 0.025}
138        # should not be any quotes, but remove them, if there are
139        foreach var {lbl type} {
140            foreach char {' \"} {
141                set q {\\}
142                append q $char
143                set hidden [regsub -all $q [set $var] \200 $var]
144                if {[string index [set $var] 0] == $char} {
145                    regsub -all $char [set $var] {} $var
146                }
147                if {$hidden} {regsub -all \200 [set $var] $char $var}
148            }
149        }
150        # CIF specifies types as Cu2+; GSAS uses Cu+2
151        if {[regexp {([A-Za-z]+)([1-9])([+-])} $type junk elem sign val]} {
152            set type ${elem}${val}$sign
153        }
154        # if type is missing, attempt to parse an element in the label
155        if {$type == "" && $lbl != ""} {
156            regexp {[A-Za-z][A-Za-z]?} $lbl type
157        }
158        # get rid of standard uncertainies
159        foreach var {x y z occ uiso} {
160            catch {
161                set $var [lindex [ParseSU [set $var]] 0]
162            }
163        }
164        # convert Biso to Uiso (if needed)
165        if {$Uconv != 1} {
166            catch {set uiso [expr {$Uconv*$uiso}]} err
167        }
168        lappend atomlist [list $lbl $x $y $z $type $occ $uiso]
169    }
170
171    # clean up -- get rid of the CIF arrays & window
172    for {set i 1} {$i <= $blocks} {incr i} {
173        unset block$i
174    }
175    destroy $file
176    return "[list $spg] [list $cell] [list $atomlist] [list $msg]"
177}
178
179
180proc CallBrowseCIF {txt blocklist selected frame} {
181    # is there a defined list of dictionary files?
182    if {[catch {set ::CIF(dictfilelist)}]} {
183        set dictfilelist [glob -nocomplain \
184                              [file join $::expgui(gsasdir) data *.dic]]
185        foreach file $dictfilelist {
186            lappend ::CIF(dictfilelist) $file
187            set ::CIF(dict_$file) 1
188        }
189    }
190    # load the initial CIF dictionaries
191    LoadDictIndices
192   
193    BrowseCIF $txt $blocklist $selected $frame
194}
Note: See TracBrowser for help on using the repository browser.