source: trunk/readexp.tcl

Last change on this file was 1251, checked in by toby, 10 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: 144.8 KB
Line 
1# $Id: readexp.tcl 1251 2014-03-10 22:17:29Z toby $
2# Routines to deal with the .EXP "data structure"
3set expmap(Revision) {$Revision: 1251 $ $Date: 2014-03-10 22:17:29 +0000 (Mon, 10 Mar 2014) $}
4
5#  The GSAS data is read from an EXP file.
6#   ... reading an EXP file into an array
7# returns -1 on error
8# returns 0 if the file is old-style UNIX format (no CR/LF)
9# returns 1 if the file is 80 char/line + cr/lf
10# returns 2 if the file is sequential but not fixed-record length
11proc expload {expfile "ns {}"} {
12    # expfile is the path to the data file.
13    # ns is the namespace to place the output array (default is global)
14    if {$ns != ""} {
15        namespace eval $ns {}
16    }
17    if [catch {set fil [open "$expfile" r]}] {
18        tk_dialog .expFileErrorMsg "File Open Error" \
19                "Unable to open file $expfile" error 0 "Exit" 
20        return -1
21    }
22    fconfigure $fil -translation lf
23    set len [gets $fil line]
24    if {[string length $line] != $len} {
25        tk_dialog .expConvErrorMsg "old tcl" \
26                "You are using an old version of Tcl/Tk and your .EXP file has binary characters; run convstod or upgrade" \
27                error 0 "Exit"
28        return -1
29    }
30    catch {
31        unset ${ns}::exparray
32    }
33    if {$len > 160} {
34        set fmt 0
35        # a UNIX-type file
36        set i1 0
37        set i2 79
38        while {$i2 < $len} {
39            set nline [string range $line $i1 $i2]
40            incr i1 80
41            incr i2 80
42            set key [string range $nline 0 11]
43            set ${ns}::exparray($key) [string range $nline 12 end]
44        }
45    } else {
46        set fmt 1
47        while {$len > 0} {
48            set key [string range $line 0 11]
49            set ${ns}::exparray($key) [string range $line 12 79]
50            if {$len != 81 || [string range $line end end] != "\r"} {set fmt 2}
51            set len [gets $fil line]
52        }
53    }
54    close $fil
55    return $fmt
56}
57
58proc createexp {expfile title} {
59    global exparray expmap
60    catch {unset exparray}
61    foreach key   {"     VERSION" "      DESCR" "ZZZZZZZZZZZZ" " EXPR NPHAS"} \
62            value {"   6"         ""            "  Last EXP file record" ""} {
63        # truncate long keys & pad short ones
64        set key [string range "$key        " 0 11]
65        set exparray($key) $value
66    }
67    expinfo title set $title
68    exphistory add " created readexp.tcl [lindex $expmap(Revision) 1] [clock format [clock seconds] -format %Y-%m-%dT%T]"
69    expwrite $expfile
70}
71
72# get information out from an EXP file
73#   creates the following entries in global array expmap
74#     expmap(phaselist)     gives a list of defined phases
75#     expmap(phasetype)     gives the phase type for each defined phase
76#                           =1 nuclear; 2 mag+nuc; 3 mag; 4 macro
77#     expmap(atomlist_$p)   gives a list of defined atoms in phase $p
78#     expmap(htype_$n)      gives the GSAS histogram type for histogram (all)
79#     expmap(powderlist)    gives a list of powder histograms in use
80#     expmap(phaselist_$n)  gives a list of phases used in histogram $n
81#     expmap(nhst)          the number of GSAS histograms
82#
83proc mapexp {} {
84    global expgui expmap exparray
85    # clear out the old array
86    set expmap_Revision $expmap(Revision)
87    unset expmap
88    set expmap(Revision) $expmap_Revision
89    # apply any updates to the .EXP file
90    updateexpfile
91    # get the defined phases
92    set line [readexp " EXPR NPHAS"]
93#    if {$line == ""} {
94#       set msg "No EXPR NPHAS entry. This is an invalid .EXP file"
95#       tk_dialog .badexp "Error in EXP" $msg error 0 Exit
96#       destroy .
97#    }
98    set expmap(phaselist) {}
99    set expmap(phasetype) {}
100    # loop over phases
101    foreach iph {1 2 3 4 5 6 7 8 9} {
102        set i5s [expr {($iph - 1)*5}]
103        set i5e [expr {$i5s + 4}]
104        set flag [string trim [string range $line $i5s $i5e]]
105        if {$flag == ""} {set flag 0}
106        if $flag {
107            lappend expmap(phaselist) $iph
108            lappend expmap(phasetype) $flag
109        }
110    }
111    # get the list of defined atoms for each phase
112    foreach iph $expmap(phaselist) {
113        set expmap(atomlist_$iph) {}
114        if {[lindex $expmap(phasetype) [expr {$iph - 1}]] != 4} {
115            foreach key [array names exparray "CRS$iph  AT*A"] {
116                regexp { AT *([0-9]+)A} $key a num
117                lappend expmap(atomlist_$iph) $num
118            }
119        } else {
120            foreach key [array names exparray "CRS$iph  AT*"] {
121                scan [string range $key 8 11] %x atm
122                lappend expmap(atomlist_$iph) $atm
123            }
124        }
125        # note that sometimes an .EXP file contains more atoms than are actually defined
126        # drop the extra ones
127        set expmap(atomlist_$iph) [lsort -integer $expmap(atomlist_$iph)]
128        set natom [phaseinfo $iph natoms]
129        if {$natom != [llength $expmap(atomlist_$iph)]} {
130            set expmap(atomlist_$iph) [lrange $expmap(atomlist_$iph) 0 [expr {$natom-1}]]
131        }
132    }
133    # now get the histogram types
134    set expmap(nhst) [string trim [readexp { EXPR  NHST }]]
135    set n 0
136    set expmap(powderlist) {}
137    for {set i 0} {$i < $expmap(nhst)} {incr i} {
138        set ihist [expr {$i + 1}]
139        if {[expr {$i % 12}] == 0} {
140            incr n
141            set line [readexp " EXPR  HTYP$n"]
142            if {$line == ""} {
143                set msg "No HTYP$n entry for Histogram $ihist. This is an invalid .EXP file"
144                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
145            }
146            set j 0
147        } else {
148            incr j
149        }
150        set expmap(htype_$ihist) [string range $line [expr 2+5*$j] [expr 5*($j+1)]]
151        # is this a dummy histogram?
152        if {$ihist <=9} {
153            set key "HST  $ihist DUMMY"
154        } else {
155            set key "HST $ihist DUMMY"
156        }
157        # at least for now, ignore non-powder histograms
158        if {[string range $expmap(htype_$ihist) 0 0] == "P" && \
159                [string range $expmap(htype_$ihist) 3 3] != "*"} {
160            if {[existsexp $key]} {
161                set expmap(htype_$ihist) \
162                        [string range $expmap(htype_$ihist) 0 2]D
163            }
164            lappend expmap(powderlist) $ihist
165        }
166    }
167
168    # now process powder histograms
169    foreach ihist $expmap(powderlist) {
170        # make a 2 digit key -- hh
171        if {$ihist < 10} {
172            set hh " $ihist"
173        } else {
174            set hh $ihist
175        }
176        set line [readexp "HST $hh NPHAS"]
177        if {$line == ""} {
178            set msg "No NPHAS entry for Histogram $ihist. This is an invalid .EXP file"
179            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
180        }
181        set expmap(phaselist_$ihist) {}
182        # loop over phases
183        foreach iph {1 2 3 4 5 6 7 8 9} {
184            set i5s [expr {($iph - 1)*5}]
185            set i5e [expr {$i5s + 4}]
186            set flag [string trim [string range $line $i5s $i5e]]
187            if {$flag == ""} {set flag 0}
188            if $flag {lappend expmap(phaselist_$ihist) $iph}
189        }
190    }
191    # load the constrained parameters
192    atom_constraint_load
193    # construct tables of mapped atoms in rigid bodies
194    foreach phase $::expmap(phaselist) {
195        set expmap(rbatoms_$phase) {}
196        foreach bodnum [RigidBodyList] {
197            set natoms [llength [lindex [lindex [lindex [ReadRigidBody $bodnum] 1] 0] 3]]
198            foreach mapnum [RigidBodyMappingList $phase $bodnum] {
199                set atomnum [lindex [ReadRigidBodyMapping $phase $bodnum $mapnum] 0]
200                set st [lsearch $::expmap(atomlist_$phase) $atomnum]
201                set en [expr {$st+$natoms-1}]
202                set atoms [lrange $::expmap(atomlist_$phase) $st $en]
203                set expmap(rbatoms_$phase) [concat $expmap(rbatoms_$phase) $atoms]
204            }
205        }
206    }
207    set expgui(mapstat) 1
208}
209
210# this routine is called to update changes in the .EXP file
211proc updateexpfile {} {
212    global exparray
213    # change "CRSx  ODFxxx" records to "CRSx  OD xxx" records
214    # needed by the June 8, 2005 GSAS release
215    set ODFlist [array names exparray "CRS?  ODF*"]
216    if {[llength $ODFlist] > 0} {
217        catch {incr ::expgui(changed)}
218        foreach key $ODFlist {
219            regsub "ODF" $key "OD " newkey
220            set exparray($newkey) $exparray($key)
221            unset exparray($key)
222        }
223    }
224}
225
226
227# return the value for a ISAM key
228proc readexp {key} {
229    global exparray
230    # truncate long keys & pad short ones
231    set key [string range "$key        " 0 11]
232    if [catch {set val $exparray($key)}] {
233        global expgui
234        if $expgui(debug) {puts "Error accessing record $key"}
235        return ""
236    }
237    return $val
238}
239
240# return the number of records matching ISAM key (may contain wildcards)
241proc existsexp {key} {
242    global exparray
243    # key can contain wild cards so don't pad
244    return [llength [array names exparray  $key]]
245}
246
247
248# replace a section of the exparray with $value
249#   replace $char characters starting at character $start (numbered from 1)
250proc setexp {key value start chars} {
251    global exparray
252    # truncate long keys & pad short ones
253    set key [string range "$key        " 0 11]
254    if [catch {set exparray($key)}] {
255        global expgui
256        if $expgui(debug) {puts "Error accessing record $key"}
257        return ""
258    }
259
260    # pad value to $chars
261    set l0 [expr {$chars - 1}]
262    set value [string range "$value                                           " 0 $l0]
263
264    if {$start == 1} {
265        set ret {}
266        set l1 $chars
267    } else {
268        set l0 [expr {$start - 2}]
269        set l1 [expr {$start + $chars - 1}]
270        set ret [string range $exparray($key) 0 $l0]
271    }
272    append ret $value [string range $exparray($key) $l1 end]
273    set exparray($key) $ret
274}
275
276proc makeexprec {key} {
277    global exparray
278    # truncate long keys & pad short ones
279    set key [string range "$key        " 0 11]
280    if [catch {set exparray($key)}] {
281        # set to 68 blanks
282        set exparray($key) [format %68s " "]
283    }
284}
285
286# delete an exp record
287# returns 1 if OK; 0 if not found
288proc delexp {key} {
289    global exparray
290    # truncate long keys & pad short ones
291    set key [string range "$key        " 0 11]
292    if [catch {unset exparray($key)}] {
293        return 0
294    }
295    return 1
296}
297
298# test an argument if it is a valid number; reform the number to fit
299proc validreal {val length decimal} {
300    upvar $val value
301    # is this a number?
302    if [catch {expr {$value}}] {return 0}
303    if [catch {
304        # how many digits are needed to the left of the decimal?
305        set sign 0
306        if {$value > 0} {
307            set digits [expr {1 + int(log10($value))}]
308        } elseif {$value < 0} {
309            set digits [expr {1 + int(log10(-$value))}]
310            set sign 1
311        } else {
312            set digits 1
313        }
314        if {$digits <= 0} {set digits 1}
315        if {$digits + $sign >= $length} {
316            # the number is much too big -- use exponential notation
317            set decimal [expr {$length - 6 - $sign}]
318            # drop more decimal places, as needed
319            set tmp [format "%${length}.${decimal}E" $value]
320            while {[string length $tmp] > $length && $decimal >= 0} {
321                incr decimal -1
322                set tmp [format "%${length}.${decimal}E" $value]
323            }
324        } elseif {$digits + $sign >= $length - $decimal} {
325            # we will have to trim the number of decimal digits
326            set decimal [expr {$length - $digits - $sign - 1}]
327            set tmp [format "%#.${decimal}f" $value]
328        } elseif {abs($value) < pow(10,2-$decimal) && $length > 6 && $value != 0} {
329            # for small values, switch to exponential notation (2-$decimal -> three sig figs.)
330            set decimal [expr {$length - 6 - $sign}]
331            # drop more decimal places, as needed
332            set tmp [format "%${length}.${decimal}E" $value]
333            while {[string length $tmp] > $length && $decimal >= 0} {
334                incr decimal -1
335                set tmp [format "%${length}.${decimal}E" $value]
336            }
337        } else {
338            # use format as specified
339            set tmp [format "%${length}.${decimal}f" $value]
340        }
341        set value $tmp
342    } errmsg] {return 0}
343    return 1
344}
345
346# test an argument if it is a valid integer; reform the number into
347# an integer, if appropriate -- be sure to pass the name of the variable not the value
348proc validint {val length} {
349    upvar $val value
350    # FORTRAN type assumption: blank is 0
351    if {$value == ""} {set value 0}
352    if [catch {
353        set tmp [expr {round($value)}]
354        if {$tmp != $value} {return 0}
355        set value [format "%${length}d" $tmp]
356    }] {return 0}
357    return 1
358}
359
360# process history information
361#    action == last
362#       returns number and value of last record
363#    action == add
364#
365proc exphistory {action "value 0"} {
366    global exparray
367    if {$action == "last"} {
368        set key [lindex [lsort -decreasing [array names exparray *HSTRY*]] 0]
369        if {$key == ""} {return ""}
370        return [list [string trim [string range $key 9 end]] $exparray($key)]
371    } elseif {$action == "add"} {
372        set key [lindex [lsort -decreasing [array names exparray *HSTRY*]] 0]
373        if {$key == ""} {
374            set index 1
375        } else {
376            set index [string trim [string range $key 9 end]]
377            if {$index != "***"} {
378                if {$index < 999} {incr index}
379                set key [format "    HSTRY%3d" $index]
380                set exparray($key) $value
381            }
382        }
383        set key [format "    HSTRY%3d" $index]
384        set exparray($key) $value
385    }
386}
387# get overall info
388#   parm:
389#     print     -- GENLES print option (*)
390#     cycles    -- number of GENLES cycles (*)
391#     title     -- the overall title (*)
392#     convg     -- convergence criterion: -200 to 200 (*)
393#     marq      -- Marquardt damping factor: 1.0 to 9.99 (*)
394#     mbw       -- LS matrix bandwidth; =0 for full matrix (*)
395proc expinfo {parm "action get" "value {}"} {
396    switch ${parm}-$action {
397        title-get {
398            return [string trim [readexp "      DESCR"]]
399        }
400        title-set {
401            setexp "      DESCR" "  $value" 2 68
402        }
403        cycles-get {
404            return [string trim [cdatget MXCY]]
405        }
406        cycles-set {
407            if ![validint value 1] {return 0}
408            cdatset MXCY [format %4d $value]
409        }
410        cyclesrun-get {
411            set cycle -1
412            regexp {.*cycles run *([0-9]*) } [readexp "  GNLS  RUN"] x cycle
413            return $cycle
414        }
415        print-get {
416            set print [string trim [cdatget PRNT]]
417            if {$print != ""} {return $print}
418            return 0
419        }
420        print-set {
421            if ![validint value 1] {return 0}
422            cdatset PRNT [format %4d $value]
423        }
424        convg-get {
425            set cvg [string trim [cdatget CVRG]]
426            if {$cvg == ""} {return -200}
427            if [catch {expr {$cvg}}] {return -200}
428            return $cvg
429        }
430        convg-set {
431            if ![validint value 1] {return 0}
432            set value [expr {-200>$value?-200:$value}]
433            set value [expr {200<$value?200:$value}]
434            cdatset CVRG [format %4d $value]
435        }
436        marq-get {
437            set mar [string trim [cdatget MARQ]]
438            if {$mar == ""} {return 1.0}
439            if [catch {expr $mar}] {return 1.}
440            return $mar
441        }
442        marq-set {
443            if [catch {
444                set value [expr {1.0>$value?1.0:$value}]
445                set value [expr {9.99<$value?9.99:$value}]
446            }] {return 0}
447            if ![validreal value 4 2] {return 0}
448            cdatset MARQ $value
449        }
450        mbw-get {
451            set mbw [string trim [cdatget MBW]]
452            if {$mbw == ""} {return 0}
453            if [catch {expr $mbw}] {return 0}
454            return $mbw
455        }
456        mbw-set {
457            if ![validint value 1] {return 0}
458            if {$value < 0} {return 0}
459            cdatset MBW [format %5d $value]
460        }
461        default {
462            set msg "Unsupported expinfo access: parm=$parm action=$action"
463            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
464        }
465    }
466    return 1
467}
468
469proc cdatget {key} {
470    foreach i {1 2 3 4 5 6 7 8 9} {
471        if {[existsexp "  GNLS CDAT$i"] == 0} break
472        set line [readexp "  GNLS CDAT$i"]
473        if {$line == {}} break
474        foreach i1 {2 10 18 26 34 42 50 58 66} \
475                i2 {9 17 25 33 41 49 57 65 73} {
476            set item [string range $line $i1 $i2]
477            if {[string trim $item] == {}} continue
478            if [regexp "${key}(.*)" $item a b] {return $b}
479        }
480    }
481    return {}
482}
483
484proc cdatset {key value} {
485    # round 1 see if we can find the string
486    foreach i {1 2 3 4 5 6 7 8 9} {
487        set line [readexp "  GNLS CDAT$i"]
488        if {$line == {}} break
489        foreach i1 {2 10 18 26 34 42 50 58 66} \
490                i2 {9 17 25 33 41 49 57 65 73} {
491            set item [string range $line $i1 $i2]
492            if {[string trim $item] == {}} continue
493            if [regexp "${key}(.*)" $item a b] {
494                # found it now replace it
495                incr i1
496                setexp "  GNLS CDAT$i" "${key}${value}" $i1 8
497                return
498            }
499        }
500    }
501    # not found, take the 1st blank space, creating a card if needed
502    foreach i {1 2 3 4 5 6 7 8 9} {
503        set line [readexp "  GNLS CDAT$i"]
504        if {$line == {}} {makeexprec "  GNLS CDAT$i"}
505        foreach i1 {2 10 18 26 34 42 50 58 66} \
506                i2 {9 17 25 33 41 49 57 65 73} {
507            set item [string range $line $i1 $i2]
508            if {[string trim $item] == {}} {
509                # found a blank space: now replace it
510                incr i1
511                setexp "  GNLS CDAT$i" "${key}${value}" $i1 8
512                return
513            }
514        }
515    }
516    return {}
517}
518
519proc disagldat_get {phase} {
520    set key "  DSGL CDAT$phase"
521    if {[existsexp $key] == 0} {return "{none} {none}"}
522    set line [readexp $key]
523    set i1 2
524    # read atom-atom distance parameter
525    set dist {}
526    set item [string range $line $i1 [expr {$i1+3}]]
527    if {$item == "DMAX"} {
528        set val [string range $line [expr {$i1+4}] [expr {$i1+11}]]
529        set dist [string trim $val]
530        incr i1 13
531    } else {
532        set dist "radii"
533        incr i1 5
534    }
535    # read atom-atom-atom angle parameter
536    set ang {}
537    set item [string range $line $i1 [expr {$i1+3}]]
538    if {$item == "DAGL"} {
539        set val [string range $line [expr {$i1+4}] [expr {$i1+11}]]
540        set ang [string trim $val]
541        incr i1 13
542    } else {
543        set ang "radii"
544        incr i1 5
545    }
546    # note there are two more parameters, NOFO/FORA & ONCR/DFLT, but they are not being processed yet
547    return "$dist $ang"
548}
549
550# get phase information: phaseinfo phase parm action value
551#   phase: 1 to 9 (as defined)
552#   parm:
553#     name -- phase name
554#     natoms -- number of atoms (*)
555#     a b c alpha beta gamma -- cell parameters (*)
556#     cellref -- refinement flag for the unit cell(*)
557#     celldamp  -- damping for the unit cell refinement (*)
558#     spacegroup -- space group symbol (*)
559#     ODForder -- spherical harmonic order (*)
560#     ODFsym   -- sample symmetry (0-3) (*)
561#     ODFdampA -- damping for angles (*)
562#     ODFdampC -- damping for coefficients (*)
563#     ODFomega -- omega oriention angle (*)
564#     ODFchi -- chi oriention angle (*)
565#     ODFphi -- phi oriention angle (*)
566#     ODFomegaRef -- refinement flag for omega (*)
567#     ODFchiRef -- refinement flag for chi (*)
568#     ODFphiRef -- refinement flag for phi (*)
569#     ODFterms -- a list of the {l m n} values for each ODF term (*)
570#     ODFcoefXXX -- the ODF coefficient for for ODF term XXX (*)
571#     ODFRefcoef -- refinement flag for ODF terms (*)
572#     DistCalc   -- returns "radii", "none" or a number (*)
573#                   none: no distance or angle computation for the phase
574#                   radii: computation will be done by sums of radii
575#                          (see AtmTypInfo and DefAtmTypInfo)
576#                   other: a distance specifing the maximum distance
577#     AngCalc    -- returns "radii", "none" or a number (*)
578#                   none: no distance or angle computation for the phase
579#                   radii: computation will be done by sums of radii
580#                          (see AtmTypInfo and DefAtmTypInfo)
581#                   other: a distance specifing the maximum distance
582#  action: get (default) or set
583#  value: used only with set
584#  * =>  read+write supported
585proc phaseinfo {phase parm "action get" "value {}"} {
586    switch -glob ${parm}-$action {
587
588        name-get {
589            return [string trim [readexp "CRS$phase    PNAM"]]
590        }
591
592        name-set {
593            setexp "CRS$phase    PNAM" " $value" 2 68
594        }
595
596        spacegroup-get {
597            return [string trim [readexp "CRS$phase  SG SYM"]]
598        }
599
600        spacegroup-set {
601            setexp "CRS$phase  SG SYM" " $value" 2 68
602        }
603
604        natoms-get {
605            return [string trim [readexp "CRS$phase   NATOM"]]     
606        }
607
608        natoms-set {
609            if ![validint value 5] {return 0}
610            setexp "CRS$phase   NATOM" $value 1 5
611        }
612
613        a-get {
614           return [string trim [string range [readexp "CRS$phase  ABC"] 0 9]]
615        }
616        b-get {
617           return [string trim [string range [readexp "CRS$phase  ABC"] 10 19]]
618        }
619        c-get {
620           return [string trim [string range [readexp "CRS$phase  ABC"] 20 29]]
621        }
622        alpha-get {
623           return [string trim [string range [readexp "CRS$phase  ANGLES"] 0 9]]
624        }
625        beta-get {
626           return [string trim [string range [readexp "CRS$phase  ANGLES"] 10 19]]
627        }
628        gamma-get {
629           return [string trim [string range [readexp "CRS$phase  ANGLES"] 20 29]]
630        }
631
632        a-set {
633            if ![validreal value 10 6] {return 0}
634            setexp "CRS$phase  ABC" $value 1 10             
635        }
636        b-set {
637            if ![validreal value 10 6] {return 0}
638            setexp "CRS$phase  ABC" $value 11 10           
639        }
640        c-set {
641            if ![validreal value 10 6] {return 0}
642            setexp "CRS$phase  ABC" $value 21 10           
643        }
644        alpha-set {
645            if ![validreal value 10 4] {return 0}
646            setexp "CRS$phase  ANGLES" $value 1 10         
647        }
648        beta-set {
649            if ![validreal value 10 4] {return 0}
650            setexp "CRS$phase  ANGLES" $value 11 10         
651        }
652        gamma-set {
653            if ![validreal value 10 4] {return 0}
654            setexp "CRS$phase  ANGLES" $value 21 10         
655        }
656        cellref-get {
657            if {[string toupper [string range [readexp "CRS$phase  ABC"] 34 34]] == "Y"} {
658                return 1
659            }
660            return 0
661        }
662        cellref-set {
663            if $value {
664                setexp "CRS$phase  ABC" "Y" 35 1
665            } else {
666                setexp "CRS$phase  ABC" "N" 35 1
667            }       
668        }
669        celldamp-get {
670            set val [string range [readexp "CRS$phase  ABC"] 39 39]
671            if {$val == " "} {return 0}
672            return $val
673        }
674        celldamp-set {
675            setexp "CRS$phase  ABC" $value 40 1
676        }
677
678        ODForder-get {
679            set val [string trim [string range [readexp "CRS$phase  OD "] 0 4]]
680            if {$val == " "} {return 0}
681            return $val
682        }
683        ODForder-set {
684            if ![validint value 5] {return 0}
685            setexp "CRS$phase  OD " $value 1 5
686        }
687        ODFsym-get {
688            set val [string trim [string range [readexp "CRS$phase  OD "] 10 14]]
689            if {$val == " "} {return 0}
690            return $val
691        }
692        ODFsym-set {
693            if ![validint value 5] {return 0}
694            setexp "CRS$phase  OD " $value 11 5
695        }
696        ODFdampA-get {
697            set val [string range [readexp "CRS$phase  OD "] 24 24]
698            if {$val == " "} {return 0}
699            return $val
700        }
701        ODFdampA-set {
702            setexp "CRS$phase  OD " $value 25 1
703        }
704        ODFdampC-get {
705            set val [string range [readexp "CRS$phase  OD "] 29 29]
706            if {$val == " "} {return 0}
707            return $val
708        }
709        ODFdampC-set {
710            setexp "CRS$phase  OD " $value 30 1
711        }
712        ODFomegaRef-get {
713            if {[string toupper [string range [readexp "CRS$phase  OD "] 16 16]] == "Y"} {
714                return 1
715            }
716            return 0
717        }
718        ODFomegaRef-set {
719            if $value {
720                setexp "CRS$phase  OD " "Y" 17 1
721            } else {
722                setexp "CRS$phase  OD " "N" 17 1
723            }       
724        }
725        ODFchiRef-get {
726            if {[string toupper [string range [readexp "CRS$phase  OD "] 17 17]] == "Y"} {
727                return 1
728            }
729            return 0
730        }
731        ODFchiRef-set {
732            if $value {
733                setexp "CRS$phase  OD " "Y" 18 1
734            } else {
735                setexp "CRS$phase  OD " "N" 18 1
736            }       
737        }
738        ODFphiRef-get {
739            if {[string toupper [string range [readexp "CRS$phase  OD "] 18 18]] == "Y"} {
740                return 1
741            }
742            return 0
743        }
744        ODFphiRef-set {
745            if $value {
746                setexp "CRS$phase  OD " "Y" 19 1
747            } else {
748                setexp "CRS$phase  OD " "N" 19 1
749            }       
750        }
751        ODFcoef*-get {
752            regsub ODFcoef $parm {} term
753            set k [expr {($term+5)/6}]
754            if {$k <= 9} {
755                set k "  $k"
756            } elseif {$k <= 99} {
757                set k " $k"
758            }
759            set j [expr {(($term-1) % 6)+1}]
760            set lineB [readexp "CRS$phase  OD${k}B"]
761            set j0 [expr { ($j-1) *10}]
762            set j1 [expr {$j0 + 9}]
763            set val [string trim [string range $lineB $j0 $j1]]
764            if {$val == ""} {return 0.0}
765            return $val
766        }
767        ODFcoef*-set {
768            regsub ODFcoef $parm {} term
769            if ![validreal value 10 3] {return 0}
770            set k [expr {($term+5)/6}]
771            if {$k <= 9} {
772                set k "  $k"
773            } elseif {$k <= 99} {
774                set k " $k"
775            }
776            set j [expr {(($term-1) % 6)+1}]
777            set col [expr { ($j-1)*10 + 1}]
778            setexp "CRS$phase  OD${k}B" $value $col 10
779        }
780        ODFRefcoef-get {
781            if {[string toupper [string range [readexp "CRS$phase  OD "] 19 19]] == "Y"} {
782                return 1
783            }
784            return 0
785        }
786        ODFRefcoef-set {
787            if $value {
788                setexp "CRS$phase  OD " "Y" 20 1
789            } else {
790                setexp "CRS$phase  OD " "N" 20 1
791            }       
792        }
793        ODFomega-get {
794           return [string trim [string range [readexp "CRS$phase  OD "] 30 39]]
795        }
796        ODFchi-get {
797           return [string trim [string range [readexp "CRS$phase  OD "] 40 49]]
798        }
799        ODFphi-get {
800           return [string trim [string range [readexp "CRS$phase  OD "] 50 59]]
801        }
802        ODFomega-set {
803            if ![validreal value 10 4] {return 0}
804            setexp "CRS$phase  OD " $value 31 10
805        }
806        ODFchi-set {
807            if ![validreal value 10 4] {return 0}
808            setexp "CRS$phase  OD " $value 41 10
809        }
810        ODFphi-set {
811            if ![validreal value 10 4] {return 0}
812            setexp "CRS$phase  OD " $value 51 10
813        }
814
815        ODFterms-get {
816            set vallist {}
817            set val [string trim [string range [readexp "CRS$phase  OD "] 5 9]]
818            for {set i 1} {$i <= $val} {incr i 6} {
819                set k [expr {1+($i-1)/6}]
820                if {$k <= 9} {
821                    set k "  $k"
822                } elseif {$k <= 99} {
823                    set k " $k"
824                }
825                set lineA [readexp "CRS$phase  OD${k}A"]
826                set k 0
827                for {set j $i} {$j <= $val && $j < $i+6} {incr j} {
828                    set j0 [expr {($k)*10}]
829                    set j1 [expr {$j0 + 9}]
830                    lappend vallist [string trim [string range $lineA $j0 $j1]]
831                    incr k
832                }
833            }
834            return $vallist
835        }
836        ODFterms-set {
837            set key "CRS$phase  OD    "
838            if {![existsexp $key]} {
839                makeexprec $key
840                set oldlen 0
841            } else {
842                set oldlen [string trim [string range [readexp $key] 5 9]]
843            }
844            set len [llength $value]
845            if ![validint len 5] {return 0}
846            setexp $key $len 6 5
847            set j 0
848            set k 0
849            foreach item $value {
850                incr j
851                if {$j % 6 == 1} {
852                    incr k
853                    if {$k <= 9} {
854                        set k "  $k"
855                    } elseif {$k <= 99} {
856                        set k " $k"
857                    }
858                    set col 1
859                    set keyA "CRS$phase  OD${k}A"
860                    set keyB "CRS$phase  OD${k}B"
861                    if {![existsexp $keyA]} {
862                        makeexprec $keyA
863                        makeexprec $keyB
864                    }
865                }
866                set col1 [expr {$col + 1}]
867                foreach n [lrange $item 0 2] {
868                    if ![validint n 3] {return 0}
869                    setexp $keyA $n $col1 3
870                    incr col1 3
871                }
872                incr col 10
873            }
874            for {incr j} {$j <= $oldlen} {incr j} {
875                if {$j % 6 == 1} {
876                    incr k
877                    if {$k <= 9} {
878                        set k "  $k"
879                    } elseif {$k <= 99} {
880                        set k " $k"
881                    }
882                    set col 1
883                    set keyA "CRS$phase  OD${k}A"
884                    set keyB "CRS$phase  OD${k}B"
885                    delexp $keyA
886                    delexp $keyB
887                }
888                if {[existsexp $keyA]} {
889                    setexp $keyA "          " $col 10
890                    setexp $keyB "          " $col 10
891                }
892                incr col 10
893            }
894        }
895        DistCalc-get {
896            set val [disagldat_get $phase]
897            return [lindex $val 0]
898        }
899        DistCalc-set {
900            set key "  DSGL CDAT$phase"
901            # for none delete the record & thats all folks
902            if {$value == "none"} {
903                catch {unset ::exparray($key)}
904                return
905            }
906            if {[existsexp $key] == 0} {
907                makeexprec $key
908            }
909            set line [readexp $key]
910            if {[string trim $line] == ""} {
911                # blank set to defaults
912                set line [string replace $line 2 15 "DRAD ARAD NOFO"]
913            }
914            if {$value == "radii"} {
915                if {[string range $line 2 5] == "DMAX"} {
916                    set line [string replace $line 2 13 "DRAD"]
917                } else {
918                    set line [string replace $line 2 5 "DRAD"]
919                }
920            } else {
921                if ![validreal value 8 2] {return 0}
922                if {[string range $line 2 5] == "DMAX"} {
923                    set line [string replace $line 6 13 $value]
924                } else {
925                    set line [string replace $line 2 5 "DMAX"]
926                    set line [string replace $line 6 6 "$value "]
927                }
928            }
929            setexp $key $line 0 68
930        }
931        AngCalc-get {
932            set val [disagldat_get $phase]
933            return [lindex $val 1]
934        }
935        AngCalc-set {
936            set key "  DSGL CDAT$phase"
937            # for none delete the record & thats all folks
938            if {$value == "none"} {
939                catch {unset ::exparray($key)}
940                return
941            }
942            if {[existsexp $key] == 0} {
943                makeexprec $key
944            }
945            set line [readexp $key]
946            if {[string trim $line] == ""} {
947                # blank set to defaults
948                set line [string replace $line 2 15 "DRAD ARAD NOFO"]
949            }
950            if {[string range $line 2 5] == "DMAX"} {
951                set i2 8
952            } else {
953                set i2 0
954            }
955            if {$value == "radii"} {
956                if {[string range $line [expr {$i2+7}] [expr {$i2+10}]] == "DAGL"} {
957                    set line [string replace $line [expr {$i2+7}] [expr {$i2+18}] "ARAD"]
958                } else {
959                    set line [string replace $line [expr {$i2+7}] [expr {$i2+10}] "ARAD"]
960                }
961            } else {
962                if ![validreal value 8 2] {return 0}
963                if {[string range $line [expr {$i2+7}] [expr {$i2+10}]] == "DAGL"} {
964                    set line [string replace $line [expr {$i2+11}] [expr {$i2+18}] $value]
965                } else {
966                    set line [string replace $line [expr {$i2+7}] [expr {$i2+10}] "DAGL"]
967                    set line [string replace $line [expr {$i2+11}] [expr {$i2+11}] "$value "]
968                }
969            }
970            setexp $key $line 0 68
971        }
972        default {
973            set msg "Unsupported phaseinfo access: parm=$parm action=$action"
974            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
975        }
976    }
977    return 1
978}
979
980
981
982# get atom information: atominfo phase atom parm action value
983#   phase: 1 to 9 (as defined)
984#   atom: a valid atom number [see expmap(atomlist_$phase)]
985#      Note that atom and phase can be paired lists, but if there are extra
986#      entries in the atoms list, the last phase will be repeated.
987#      so that atominfo 1 {1 2 3} xset 1
988#               will set the xflag for atoms 1-3 in phase 1
989#      but atominfo {1 2 3} {1 1 1} xset 1
990#               will set the xflag for atoms 1 in phase 1-3
991#   parm:
992#     type -- element code
993#     mult -- atom multiplicity
994#     label -- atom label (*)
995#     x y z -- coordinates (*)
996#     frac --  occupancy (*)
997#     temptype -- I or A for Isotropic/Anisotropic (*)
998#     Uiso  -- Isotropic temperature factor (*)
999#     U11  -- Anisotropic temperature factor (*)
1000#     U22  -- Anisotropic temperature factor (*)
1001#     U33  -- Anisotropic temperature factor (*)
1002#     U12  -- Anisotropic temperature factor (*)
1003#     U13  -- Anisotropic temperature factor (*)
1004#     U23  -- Anisotropic temperature factor (*)
1005#     xref/xdamp -- refinement flag/damping value for the coordinates (*)
1006#     uref/udamp -- refinement flag/damping value for the temperature factor(s)  (*)
1007#     fref/fdamp -- refinement flag/damping value for the occupancy (*)
1008#  action: get (default) or set
1009#  value: used only with set
1010#  * =>  read+write supported
1011proc atominfo {phaselist atomlist parm "action get" "value {}"} {
1012    foreach phase $phaselist atom $atomlist {
1013        if {$phase == ""} {set phase [lindex $phaselist end]}
1014        if {$atom < 10} {
1015            set key "CRS$phase  AT  $atom"
1016        } elseif {$atom < 100} {
1017            set key "CRS$phase  AT $atom"
1018        } else {
1019            set key "CRS$phase  AT$atom"
1020        }
1021        switch -glob ${parm}-$action {
1022            type-get {
1023                return [string trim [string range [readexp ${key}A] 2 9] ]
1024            }
1025            mult-get {
1026                return [string trim [string range [readexp ${key}A] 58 61] ]
1027            }
1028            label-get {
1029                return [string trim [string range [readexp ${key}A] 50 57] ]
1030            }
1031            label-set {
1032                setexp ${key}A $value 51 8
1033            }
1034            temptype-get {
1035                return [string trim [string range [readexp ${key}B] 62 62] ]
1036            }
1037            temptype-set {
1038                if {$value == "A"} {
1039                    setexp ${key}B A 63 1
1040                    # copy the Uiso to the diagonal terms
1041                    set Uiso [string range [readexp ${key}B] 0 9]
1042                    foreach value [CalcAniso $phase $Uiso] \
1043                            col {1 11 21 31 41 51} {
1044                        validreal value 10 6
1045                        setexp ${key}B $value $col 10
1046                    }
1047                } else {
1048                    setexp ${key}B I 63 1
1049                    set value 0.0
1050                    catch {
1051                        # get the trace
1052                        set value [expr {( \
1053                                [string range [readexp ${key}B] 0 9] + \
1054                                [string range [readexp ${key}B] 10 19] + \
1055                                [string range [readexp ${key}B] 20 29])/3.}]
1056                    }
1057                    validreal value 10 6
1058                    setexp ${key}B $value 1 10
1059                    # blank out the remaining terms
1060                    set value " "
1061                    setexp ${key}B $value 11 10
1062                    setexp ${key}B $value 21 10
1063                    setexp ${key}B $value 31 10
1064                    setexp ${key}B $value 41 10
1065                    setexp ${key}B $value 51 10
1066                }
1067            }
1068            x-get {
1069                return [string trim [string range [readexp ${key}A] 10 19] ]
1070            }
1071            x-set {
1072                if ![validreal value 10 6] {return 0}
1073                setexp ${key}A $value 11 10
1074            }
1075            y-get {
1076                return [string trim [string range [readexp ${key}A] 20 29] ]
1077            }
1078            y-set {
1079                if ![validreal value 10 6] {return 0}
1080                setexp ${key}A $value 21 10
1081            }
1082            z-get {
1083                return [string trim [string range [readexp ${key}A] 30 39] ]
1084            }
1085            z-set {
1086                if ![validreal value 10 6] {return 0}
1087                setexp ${key}A $value 31 10
1088            }
1089            frac-get {
1090                return [string trim [string range [readexp ${key}A] 40 49] ]
1091            }
1092            frac-set {
1093                if ![validreal value 10 6] {return 0}
1094                setexp ${key}A $value 41 10
1095            }
1096            U*-get {
1097                regsub U $parm {} type
1098                if {$type == "iso" || $type == "11"} {
1099                    return [string trim [string range [readexp ${key}B] 0 9] ]
1100                } elseif {$type == "22"} {
1101                    return [string trim [string range [readexp ${key}B] 10 19] ]
1102                } elseif {$type == "33"} {
1103                    return [string trim [string range [readexp ${key}B] 20 29] ]
1104                } elseif {$type == "12"} {
1105                    return [string trim [string range [readexp ${key}B] 30 39] ]
1106                } elseif {$type == "13"} {
1107                    return [string trim [string range [readexp ${key}B] 40 49] ]
1108                } elseif {$type == "23"} {
1109                    return [string trim [string range [readexp ${key}B] 50 59] ]
1110                }
1111            }
1112            U*-set {
1113                if ![validreal value 10 6] {return 0}
1114                regsub U $parm {} type
1115                if {$type == "iso" || $type == "11"} {
1116                    setexp ${key}B $value 1 10
1117                } elseif {$type == "22"} {
1118                    setexp ${key}B $value 11 10
1119                } elseif {$type == "33"} {
1120                    setexp ${key}B $value 21 10
1121                } elseif {$type == "12"} {
1122                    setexp ${key}B $value 31 10
1123                } elseif {$type == "13"} {
1124                    setexp ${key}B $value 41 10
1125                } elseif {$type == "23"} {
1126                    setexp ${key}B $value 51 10
1127                }
1128            }
1129            xref-get {
1130                if {[string toupper [string range [readexp ${key}B] 64 64]] == "X"} {
1131                    return 1
1132                }
1133                return 0
1134            }
1135            xref-set {
1136                if $value {
1137                    setexp ${key}B "X" 65 1
1138                } else {
1139                    setexp ${key}B " " 65 1
1140                }           
1141            }
1142            xdamp-get {
1143                set val [string range [readexp ${key}A] 64 64]
1144                if {$val == " "} {return 0}
1145                return $val
1146            }
1147            xdamp-set {
1148                setexp ${key}A $value 65 1
1149            }
1150            fref-get {
1151                if {[string toupper [string range [readexp ${key}B] 63 63]] == "F"} {
1152                    return 1
1153                }
1154                return 0
1155            }
1156            fref-set {
1157                if $value {
1158                    setexp ${key}B "F" 64 1
1159                } else {
1160                    setexp ${key}B " " 64 1
1161                }           
1162            }
1163            fdamp-get {
1164                set val [string range [readexp ${key}A] 63 63]
1165                if {$val == " "} {return 0}
1166                return $val
1167            }
1168            fdamp-set {
1169                setexp ${key}A $value 64 1
1170            }
1171
1172            uref-get {
1173                if {[string toupper [string range [readexp ${key}B] 65 65]] == "U"} {
1174                    return 1
1175                }
1176                return 0
1177            }
1178            uref-set {
1179                if $value {
1180                    setexp ${key}B "U" 66 1
1181                } else {
1182                    setexp ${key}B " " 66 1
1183                }           
1184            }
1185            udamp-get {
1186                set val [string range [readexp ${key}A] 65 65]
1187                if {$val == " "} {return 0}
1188                return $val
1189            }
1190            udamp-set {
1191                setexp ${key}A $value 66 1
1192            }
1193            default {
1194                set msg "Unsupported atominfo access: parm=$parm action=$action"
1195                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
1196            }
1197        }
1198    }
1199    return 1
1200}
1201
1202# get macromolecular atom information: mmatominfo phase atom parm action value
1203#   phase: 1 (at present only one mm phase can be defined)
1204#   atom: a valid atom number [see expmap(atomlist_$phase)]
1205#      Note that atoms can be lists
1206#      so that mmatominfo 1 {1 2 3} xset 1
1207#               will set the xflag for atoms 1-3 in phase 1
1208#   parm:
1209#     type -- element code
1210#     frac --  occupancy (*)
1211#     x y z -- coordinates (*)
1212#     Uiso  -- Isotropic temperature factor (*)
1213#     label -- atom label (*)
1214#     residue -- residue label (*)
1215#     group -- group label (*)
1216#     resnum -- residue number (*)
1217#     xref/xdamp -- refinement flag/damping value for the coordinates (*)
1218#     uref/udamp -- refinement flag/damping value for the temperature factor(s)  (*)
1219#     fref/fdamp -- refinement flag/damping value for the occupancy (*)
1220#  action: get (default) or set
1221#  value: used only with set
1222#  * =>  read+write supported
1223proc mmatominfo {phaselist atomlist parm "action get" "value {}"} {
1224    foreach phase $phaselist atom $atomlist {
1225        if {$phase == ""} {set phase [lindex $phaselist end]}
1226        set num [string toupper [format %.4x $atom]]
1227        set key "CRS$phase  AT$num"
1228        switch -glob ${parm}-$action {
1229            type-get {
1230                return [string trim [string range [readexp ${key}] 2 9] ]
1231            }
1232            frac-get {
1233                return [string trim [string range [readexp ${key}] 10 15] ]
1234            }
1235            frac-set {
1236                if ![validreal value 6 4] {return 0}
1237                setexp ${key} $value 11 6
1238            }
1239            x-get {
1240                return [string trim [string range [readexp ${key}] 16 23] ]
1241            }
1242            x-set {
1243                if ![validreal value 8 5] {return 0}
1244                setexp ${key} $value 17 8
1245            }
1246            y-get {
1247                return [string trim [string range [readexp ${key}] 24 31] ]
1248            }
1249            y-set {
1250                if ![validreal value 8 5] {return 0}
1251                setexp ${key} $value 25 8
1252            }
1253            z-get {
1254                return [string trim [string range [readexp ${key}] 32 39] ]
1255            }
1256            z-set {
1257                if ![validreal value 8 5] {return 0}
1258                setexp ${key} $value 33 8
1259            }
1260            Uiso-get {
1261                return [string trim [string range [readexp ${key}] 40 45] ]
1262            }
1263            Uiso-set {
1264                if ![validreal value 6 4] {return 0}
1265                setexp ${key} $value 41 6
1266            }
1267            label-get {
1268                return [string trim [string range [readexp ${key}] 46 50] ]
1269            }
1270            label-set {
1271                setexp ${key} $value 47 5
1272            }
1273            residue-get {
1274                return [string range [readexp ${key}] 51 53]
1275            }
1276            residue-set {
1277                setexp ${key} $value 52 3
1278            }
1279            group-get {
1280                return [string range [readexp ${key}] 54 55]
1281            }
1282            group-set {
1283                setexp ${key} $value 55 2
1284            }
1285            resnum-get {
1286                return [string trim [string range [readexp ${key}] 56 59] ]
1287            }
1288            resnum-set {
1289                if ![validint value 4] {return 0}
1290                setexp "${key} EPHAS" $value 57 4
1291            }
1292            fref-get {
1293                if {[string toupper [string range [readexp $key] 60 60]] == "F"} {
1294                    return 1
1295                }
1296                return 0
1297            }
1298            fref-set {
1299                if $value {
1300                    setexp $key "F" 61 1
1301                } else {
1302                    setexp $key " " 61 1
1303                }           
1304            }
1305            xref-get {
1306                if {[string toupper [string range [readexp $key] 61 61]] == "X"} {
1307                    return 1
1308                }
1309                return 0
1310            }
1311            xref-set {
1312                if $value {
1313                    setexp $key "X" 62 1
1314                } else {
1315                    setexp ${key}B " " 62 1
1316                }           
1317            }
1318            uref-get {
1319                if {[string toupper [string range [readexp $key] 62 62]] == "U"} {
1320                    return 1
1321                }
1322                return 0
1323            }
1324            uref-set {
1325                if $value {
1326                    setexp $key "U" 63 1
1327                } else {
1328                    setexp $key " " 63 1
1329                }           
1330            }
1331
1332            fdamp-get {
1333                set val [string range [readexp ${key}] 63 63]
1334                if {$val == " "} {return 0}
1335                return $val
1336            }
1337            fdamp-set {
1338                setexp ${key} $value 64 1
1339            }
1340            xdamp-get {
1341                set val [string range [readexp ${key}] 64 64]
1342                if {$val == " "} {return 0}
1343                return $val
1344            }
1345            xdamp-set {
1346                setexp ${key} $value 65 1
1347            }
1348
1349            udamp-get {
1350                set val [string range [readexp ${key}] 65 65]
1351                if {$val == " "} {return 0}
1352                return $val
1353            }
1354            udamp-set {
1355                setexp ${key} $value 66 1
1356            }
1357            default {
1358                set msg "Unsupported mmatominfo access: parm=$parm action=$action"
1359                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
1360            }
1361        }
1362    }
1363    return 1
1364}
1365
1366
1367
1368# get histogram information: histinfo histlist parm action value
1369# histlist is a list of histogram numbers
1370# parm:
1371#     title
1372#     file  -- file name of raw data for histogram (*)
1373#     scale (*)
1374#     sref/sdamp -- refinement flag/damping value for the scale factor (*)
1375#     lam1, lam2 (*)
1376#     ttref refinement flag for the 2theta (ED Xray) (*)
1377#     wref refinement flag for the wavelength (*)
1378#     ratref refinement flag for the wavelength ratio (*)
1379#     difc, difa -- TOF calibration constants (*)
1380#     dcref,daref -- refinement flag for difc, difa (*)
1381#     zero (*)
1382#     zref refinement flag for the zero correction (*)
1383#     ipola (*)
1384#     pola (*)
1385#     pref refinement flag for the polarization (*)
1386#     kratio (*)
1387#     ddamp -- damping value for the diffractometer constants (*)
1388#     backtype -- background function number *
1389#     backterms -- number of background terms *
1390#     bref/bdamp -- refinement flag/damping value for the background (*)
1391#     bterm$n -- background term #n (*)
1392#     bank -- Bank number
1393#     tofangle -- detector angle (TOF only)
1394#     foextract  -- Fobs extraction flag (*)
1395#     LBdamp  -- LeBail damping value (*)
1396#     tmin/tmax -- minimum & maximum usable 2theta/TOF/energy
1397#     excl -- excluded regions (*)
1398#     dmin -- minimum d-space for reflection generation (*)
1399#     use  -- use flag; 1 = use; 0 = do not use (*)
1400#     dstart -- dummy histogram starting tmin/emin/2theta (*)
1401#     dstep -- dummy histogram step size tmin/emin/2theta (*)
1402#     dpoints -- dummy histogram number of points (*)
1403#     dtype   -- dummy histogram type (CONST or SLOG)
1404#     abscor1 -- 1st absorption correction (*)
1405#     abscor2 -- 2nd absorption correction (*)
1406#     abstype -- absorption correction type (*)
1407#     absdamp -- damping for absorption refinement (*)
1408#     absref -- refinement damping for absorption refinement (*)
1409#     proftbl -- returns number of profile table terms
1410#     anomff -- returns a list of elements, f' and f"
1411#   parameters transferred from the instrument parameter file:
1412#     ITYP    -- returns the contents of the ITYP record
1413proc histinfo {histlist parm "action get" "value {}"} {
1414    global expgui
1415    foreach hist $histlist {
1416        if {$hist < 10} {
1417            set key "HST  $hist"
1418        } else {
1419            set key "HST $hist"
1420        }
1421        switch -glob ${parm}-$action {
1422            foextract-get {
1423                set line [readexp "${key} EPHAS"]
1424                # add a EPHAS if not exists
1425                if {$line == {}} {
1426                    makeexprec "${key} EPHAS"
1427                    # expedt defaults this to "F", but I think "T" is better
1428                    setexp "${key} EPHAS" "Y" 50 1
1429                    if $expgui(debug) {puts "Warning: creating a ${key} EPHAS record"}
1430                }
1431                if {[string toupper [string range $line 49 49]] == "T"} {
1432                    return 1
1433                }
1434                # the flag has changed to "Y/N" in the latest versions
1435                # of GSAS
1436                if {[string toupper [string range $line 49 49]] == "Y"} {
1437                    return 1
1438                }
1439                return 0
1440            }
1441            foextract-set {
1442                # the flag has changed to "Y/N" in the latest versions
1443                # of GSAS
1444                if $value {
1445                    setexp "${key} EPHAS" "Y" 50 1
1446                } else {
1447                    setexp "${key} EPHAS" "N" 50 1
1448                }
1449            }
1450            LBdamp-get {
1451                set v [string trim [string range [readexp "${key} EPHAS"] 54 54]]
1452                if {$v == ""} {return 0}
1453                return $v
1454            }
1455            LBdamp-set {
1456                if ![validint value 5] {return 0}
1457                setexp "${key} EPHAS" $value 51 5
1458            }
1459            title-get {
1460                return [string trim [readexp "${key}  HNAM"] ]
1461            }
1462            scale-get {
1463                if {![existsexp ${key}HSCALE]} {
1464                    # fix missing scale factor record
1465                    makeexprec ${key}HSCALE
1466                    set value 1.0
1467                    validreal value 15 6
1468                    setexp ${key}HSCALE $value 1 15
1469                    catch {incr ::expgui(changed)}
1470                    setexp ${key}HSCALE "N" 20 1
1471                }
1472                return [string trim [string range [readexp ${key}HSCALE] 0 14]]
1473            }
1474            scale-set {
1475                if ![validreal value 15 6] {return 0}
1476                setexp ${key}HSCALE $value 1 15
1477            }
1478            sref-get {
1479                if {[string toupper [string range [readexp ${key}HSCALE] 19 19]] == "Y"} {
1480                    return 1
1481                }
1482                return 0
1483            }
1484            sref-set {
1485                if $value {
1486                    setexp ${key}HSCALE "Y" 20 1
1487                } else {
1488                    setexp ${key}HSCALE "N" 20 1
1489                }           
1490            }
1491            sdamp-get {
1492                set val [string range [readexp ${key}HSCALE] 24 24]
1493                if {$val == " "} {return 0}
1494                return $val
1495            }
1496            sdamp-set {
1497                setexp ${key}HSCALE $value 25 1
1498            }
1499
1500            difc-get -
1501            lam1-get {
1502                return [string trim [string range [readexp "${key} ICONS"] 0 9]]
1503            }
1504            difc-set -
1505            lam1-set {
1506                if ![validreal value 10 7] {return 0}
1507                setexp "${key} ICONS" $value 1 10
1508                # set the powpref warning (1 = suggested)
1509                catch {
1510                    global expgui
1511                    if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
1512                    set msg "Diffractometer constants" 
1513                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
1514                        append expgui(needpowpref_why) "\t$msg were changed\n"
1515                    }
1516                }
1517            }
1518            difa-get -
1519            lam2-get {
1520                return [string trim [string range [readexp "${key} ICONS"] 10 19]]
1521            }
1522            difa-set -
1523            lam2-set {
1524                if ![validreal value 10 7] {return 0}
1525                setexp "${key} ICONS" $value 11 10
1526                # set the powpref warning (1 = suggested)
1527                catch {
1528                    global expgui
1529                    if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
1530                    set msg "Diffractometer constants" 
1531                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
1532                        append expgui(needpowpref_why) "\t$msg were changed\n"
1533                    }
1534                }
1535            }
1536            zero-get {
1537                return [string trim [string range [readexp "${key} ICONS"] 20 29]]
1538            }
1539            zero-set {
1540                if ![validreal value 10 5] {return 0}
1541                setexp "${key} ICONS" $value 21 10
1542                # set the powpref warning (1 = suggested)
1543                catch {
1544                    global expgui
1545                    if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
1546                    set msg "Diffractometer constants" 
1547                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
1548                        append expgui(needpowpref_why) "\t$msg were changed\n"
1549                    }
1550                }
1551            }
1552            ipola-get {
1553                return [string trim [string range [readexp "${key} ICONS"] 54 54]]
1554            }
1555            ipola-set {
1556                if ![validint value 1] {return 0}
1557                setexp "${key} ICONS" $value 55 1
1558            }
1559            pola-get {
1560                return [string trim [string range [readexp "${key} ICONS"] 40 49]]
1561            }
1562            pola-set {
1563                if ![validreal value 10 5] {return 0}
1564                setexp "${key} ICONS" $value 41 10
1565            }
1566            kratio-get {
1567                set val [string trim [string range [readexp "${key} ICONS"] 55 64]]
1568                if {$val == ""} {set val 0}
1569                # N.B. this code is used w/CW, where Kratio may not be 0.0
1570                set lam2 [string trim [string range [readexp "${key} ICONS"] 10 19]]
1571                if {$lam2 == ""} {set lam2 0}
1572                # Change kratio & flag the change (this is rather kludged)
1573                if {$val == 0 && $lam2 != 0} {
1574                    set val 0.5
1575                    validreal val 10 5
1576                    setexp "${key} ICONS" $val 56 10
1577                    catch {incr ::expgui(changed)}
1578                }
1579                return $val
1580            }
1581            kratio-set {
1582                if ![validreal value 10 5] {return 0}
1583                setexp "${key} ICONS" $value 56 10
1584            }
1585
1586            wref-get {
1587            #------------------------------------------------------
1588            # col 33: refine flag for lambda, difc, ratio and theta
1589            #------------------------------------------------------
1590                if {[string toupper [string range \
1591                        [readexp "${key} ICONS"] 32 32]] == "L"} {
1592                    return 1
1593                }
1594                return 0
1595            }
1596            wref-set {
1597                if $value {
1598                    setexp "${key} ICONS" "L" 33 1
1599                } else {
1600                    setexp "${key} ICONS" " " 33 1
1601                }           
1602            }
1603            ratref-get {
1604                if {[string toupper [string range \
1605                        [readexp "${key} ICONS"] 32 32]] == "R"} {
1606                    return 1
1607                }
1608                return 0
1609            }
1610            ratref-set {
1611                if $value {
1612                    setexp "${key} ICONS" "R" 33 1
1613                } else {
1614                    setexp "${key} ICONS" " " 33 1
1615                }           
1616            }
1617            dcref-get {
1618                if {[string toupper [string range \
1619                        [readexp "${key} ICONS"] 32 32]] == "C"} {
1620                    return 1
1621                }
1622                return 0
1623            }
1624            dcref-set {
1625                if $value {
1626                    setexp "${key} ICONS" "C" 33 1
1627                } else {
1628                    setexp "${key} ICONS" " " 33 1
1629                }           
1630            }
1631            ttref-get {
1632                if {[string toupper [string range \
1633                        [readexp "${key} ICONS"] 32 32]] == "T"} {
1634                    return 1
1635                }
1636                return 0
1637            }
1638            ttref-set {
1639                if $value {
1640                    setexp "${key} ICONS" "T" 33 1
1641                } else {
1642                    setexp "${key} ICONS" " " 33 1
1643                }           
1644            }
1645
1646
1647            pref-get {
1648            #------------------------------------------------------
1649            # col 34: refine flag for POLA & DIFA
1650            #------------------------------------------------------
1651                if {[string toupper [string range \
1652                        [readexp "${key} ICONS"] 33 33]] == "P"} {
1653                    return 1
1654                }
1655                return 0
1656            }
1657            pref-set {
1658                if $value {
1659                    setexp "${key} ICONS" "P" 34 1
1660                } else {
1661                    setexp "${key} ICONS" " " 34 1
1662                }           
1663            }
1664            daref-get {
1665                if {[string toupper [string range \
1666                        [readexp "${key} ICONS"] 33 33]] == "A"} {
1667                    return 1
1668                }
1669                return 0
1670            }
1671            daref-set {
1672                if $value {
1673                    setexp "${key} ICONS" "A" 34 1
1674                } else {
1675                    setexp "${key} ICONS" " " 34 1
1676                }           
1677            }
1678
1679            zref-get {
1680            #------------------------------------------------------
1681            # col 34: refine flag for zero correction
1682            #------------------------------------------------------
1683                if {[string toupper [string range [readexp "${key} ICONS"] 34 34]] == "Z"} {
1684                    return 1
1685                }
1686                return 0
1687            }
1688            zref-set {
1689                if $value {
1690                    setexp "${key} ICONS" "Z" 35 1
1691                } else {
1692                    setexp "${key} ICONS" " " 35 1
1693                }           
1694            }
1695
1696            ddamp-get {
1697                set val [string range [readexp "${key} ICONS"] 39 39]
1698                if {$val == " "} {return 0}
1699                return $val
1700            }
1701            ddamp-set {
1702                setexp "${key} ICONS" $value 40 1
1703            }
1704
1705            backtype-get {
1706                set val [string trim [string range [readexp "${key}BAKGD "] 0 4]]
1707                if {$val == " "} {return 0}
1708                return $val
1709            }
1710            backtype-set {
1711                if ![validint value 5] {return 0}
1712                setexp "${key}BAKGD " $value 1 5
1713            }
1714            backterms-get {
1715                set val [string trim [string range [readexp "${key}BAKGD "] 5 9]]
1716                if {$val == " "} {return 0}
1717                return $val
1718            }
1719            backterms-set {
1720                # this takes a bit of work -- if terms are added, add lines as needed to the .EXP
1721                set oldval [string trim [string range [readexp "${key}BAKGD "] 5 9]]
1722                if ![validint value 5] {return 0}
1723                if {$oldval < $value} {
1724                    set line1  [expr {2 + ($oldval - 1) / 4}]
1725                    set line2  [expr {1 + ($value - 1) / 4}]
1726                    for {set i $line1} {$i <= $line2} {incr i} {
1727                        # create a blank entry if needed
1728                        makeexprec ${key}BAKGD$i
1729                    }
1730                    incr oldval
1731                    for {set num $oldval} {$num <= $value} {incr num} {
1732                        set f1 [expr {15*(($num - 1) % 4)}]
1733                        set f2 [expr {15*(1 + ($num - 1) % 4)-1}]
1734                        set line  [expr {1 + ($num - 1) / 4}]
1735                        if {[string trim [string range [readexp ${key}BAKGD$line] $f1 $f2]] == ""} {
1736                            set f1 [expr {15*(($num - 1) % 4)+1}]
1737                            setexp ${key}BAKGD$line 0.0 $f1 15                 
1738                        }
1739                    }
1740                }
1741                setexp "${key}BAKGD " $value 6 5
1742
1743            }
1744            bref-get {
1745                if {[string toupper [string range [readexp "${key}BAKGD"] 14 14]] == "Y"} {
1746                    return 1
1747                }
1748                return 0
1749            }
1750            bref-set {
1751                if $value {
1752                    setexp "${key}BAKGD "  "Y" 15 1
1753                } else {
1754                    setexp "${key}BAKGD "  "N" 15 1
1755                }
1756            }
1757            bdamp-get {
1758                set val [string range [readexp "${key}BAKGD "] 19 19]
1759                if {$val == " "} {return 0}
1760                return $val
1761            }
1762            bdamp-set {
1763                setexp "${key}BAKGD " $value 20 1
1764            }
1765            bterm*-get {
1766                regsub bterm $parm {} num
1767                set f1 [expr {15*(($num - 1) % 4)}]
1768                set f2 [expr {15*(1 + ($num - 1) % 4)-1}]
1769                set line  [expr {1 + ($num - 1) / 4}]
1770                return [string trim [string range [readexp ${key}BAKGD$line] $f1 $f2] ]
1771            }
1772            bterm*-set {
1773                regsub bterm $parm {} num
1774                if ![validreal value 15 6] {return 0}
1775                set f1 [expr {15*(($num - 1) % 4)+1}]
1776                set line  [expr {1 + ($num - 1) / 4}]
1777                setexp ${key}BAKGD$line $value $f1 15
1778            }
1779            bank-get {
1780                return [string trim [string range [readexp "${key} BANK"] 0 4]]
1781            }
1782            tofangle-get {
1783                return [string trim [string range [readexp "${key}BNKPAR"] 10 19]]
1784            }
1785            tmin-get {
1786                return [string trim [string range [readexp "${key} TRNGE"] 0 9]]
1787            }
1788            tmax-get {
1789                return [string trim [string range [readexp "${key} TRNGE"] 10 19]]
1790            }
1791            excl-get {
1792                set n [string trim [string range [readexp "${key} NEXC"] 0 4]]
1793                set exlist {}
1794                for {set i 1} {$i <= $n} {incr i} {
1795                    set line [readexp [format "${key}EXC%3d" $i]]
1796                    lappend exlist [list \
1797                            [string trim [string range $line  0  9]] \
1798                            [string trim [string range $line 10 19]]]
1799                }
1800                return $exlist
1801            }
1802            excl-set {
1803                set n [llength $value]
1804                if ![validint n 5] {return 0}
1805                setexp "${key} NEXC" $n 1 5
1806                set i 0
1807                foreach p $value {
1808                    incr i
1809                    foreach {r1 r2} $p {}
1810                    validreal r1 10 3
1811                    validreal r2 10 3
1812                    set k [format "${key}EXC%3d" $i]
1813                    if {![existsexp $k]} {
1814                        makeexprec $k
1815                    }
1816                    setexp $k ${r1}${r2} 1 20
1817                }
1818                # set the powpref warning (2 = required)
1819                catch {
1820                    global expgui
1821                    set expgui(needpowpref) 2
1822                    set msg "Excluded regions" 
1823                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
1824                        append expgui(needpowpref_why) "\t$msg were changed\n"
1825                    }
1826                }
1827            }
1828            file-get {
1829                return [string trim [readexp "${key}  HFIL"] ]
1830            }
1831            file-set {
1832                setexp "${key}  HFIL" $value 3 65
1833            }
1834            bank-get {
1835                return [string trim [string range [readexp "${key} BANK"] 0 4]]
1836            }
1837            dmin-get {
1838                return [string trim [string range [readexp "${key} NREF"] 5 14]]
1839            }
1840            dmin-set {
1841                if ![validreal value 10 4] {return 0}
1842                setexp "${key} NREF" $value 6 10
1843                # set the powpref warning (2 = required)
1844                catch {
1845                    global expgui
1846                    set expgui(needpowpref) 2
1847                    set msg "Dmin (reflection range)" 
1848                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
1849                        append expgui(needpowpref_why) "\t$msg was changed\n"
1850                    }
1851                }
1852            }
1853            use-get {
1854                set k [expr {($hist+11)/12}]
1855                set line [readexp " EXPR  HTYP$k"]
1856                set j [expr {((($hist-1) % 12)+1)*5}]
1857                if {[string range $line $j $j] == "*"} {return 0}
1858                return 1
1859            }
1860            use-set {
1861                set k [expr {($hist+11)/12}]
1862                set line [readexp " EXPR  HTYP$k"]
1863                set j [expr {((($hist-1) % 12)+1)*5+1}]
1864                if {$value} {
1865                    setexp " EXPR  HTYP$k" " " $j 1
1866                } else {
1867                    setexp " EXPR  HTYP$k" "*" $j 1
1868                }
1869                # set the powpref warning (2 = required)
1870                catch {
1871                    global expgui
1872                    set expgui(needpowpref) 2
1873                    set msg "Histogram use flags" 
1874                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
1875                        append expgui(needpowpref_why) "\t$msg were changed\n"
1876                    }
1877                }
1878            }
1879            dstart-get {
1880                return [string trim [string range [readexp "${key} DUMMY"] 20 29]]
1881            }
1882            dstart-set {
1883                if ![validreal value 10 3] {return 0}
1884                setexp "${key} DUMMY" $value 21 10
1885                # set the powpref warning (1 = suggested)
1886                catch {
1887                    global expgui
1888                    if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
1889                    set msg "Dummy histogram parameters" 
1890                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
1891                        append expgui(needpowpref_why) "\t$msg were changed\n"
1892                    }
1893                }
1894            }
1895            dstep-get {
1896                return [string trim [string range [readexp "${key} DUMMY"] 30 39]]
1897            }
1898            dstep-set {
1899                if ![validreal value 10 3] {return 0}
1900                setexp "${key} DUMMY" $value 31 10
1901                catch {
1902                    global expgui
1903                    if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
1904                    set msg "Dummy histogram parameters" 
1905                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
1906                        append expgui(needpowpref_why) "\t$msg were changed\n"
1907                    }
1908                }
1909            }
1910            dpoints-get {
1911                return [string trim [string range [readexp "${key} DUMMY"] 0 9]]
1912            }
1913            dpoints-set {
1914                if ![validint value 10] {return 0}
1915                setexp "${key} DUMMY" $value 1 10
1916                catch {
1917                    global expgui
1918                    if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
1919                    set msg "Dummy histogram parameters" 
1920                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
1921                        append expgui(needpowpref_why) "\t$msg were changed\n"
1922                    }
1923                }
1924            }
1925            dtype-get {
1926                return [string trim [string range [readexp "${key} DUMMY"] 10 19]]
1927            }
1928            abscor1-get {
1929                return [string trim [string range [readexp "${key}ABSCOR"] 0 14]]
1930            }
1931            abscor1-set {
1932                if ![validreal value 15 7] {return 0}
1933                setexp "${key}ABSCOR" $value 1 15
1934            }
1935            abscor2-get {
1936                return [string trim [string range [readexp "${key}ABSCOR"] 15 29]]
1937            }
1938            abscor2-set {
1939                # this must have a decimal as the 5th character, so that we end up with a
1940                # decimal point in column 20.
1941                set tmp $value
1942                if ![validreal tmp 12 7] {return 0}
1943                set pos [string first "." $tmp]
1944                while {$pos < 4} {
1945                    set tmp " $tmp"
1946                    set pos [string first "." $tmp]
1947                }
1948                if {$pos == 4} {
1949                    setexp "${key}ABSCOR" $tmp 16 15
1950                    return
1951                }
1952                catch {
1953                    set tmp [format "%12.6E" $value]
1954                    set pos [string first "." $tmp]
1955                    while {$pos < 4} {
1956                        set tmp " $tmp"
1957                        set pos [string first "." $tmp]
1958                    }
1959                    if {$pos == 4} {
1960                        setexp "${key}ABSCOR" $tmp 16 15
1961                        return
1962                    }
1963                }
1964                return 0
1965            }
1966            abstype-get {
1967                set val [string trim [string range [readexp "${key}ABSCOR"] 40 44]]
1968                if {$val == ""} {set val 0}
1969                return $val
1970            }
1971            abstype-set {
1972                if ![validint value 5] {return 0}
1973                setexp "${key}ABSCOR" $value 41 5
1974            }
1975            absdamp-get {
1976                set val [string range [readexp "${key}ABSCOR"] 39 39]
1977                if {$val == " "} {return 0}
1978                return $val
1979            }
1980            absdamp-set {
1981                if ![validint value 5] {return 0}
1982                setexp "${key}ABSCOR" $value 36 5
1983            }
1984            absref-get {
1985                if {[string toupper \
1986                        [string range [readexp "${key}ABSCOR"] 34 34]] == "Y"} {
1987                    return 1
1988                }
1989                return 0
1990            }
1991            absref-set {
1992                if $value {
1993                    setexp "${key}ABSCOR" "    Y" 31 5
1994                } else {
1995                    setexp "${key}ABSCOR" "    N" 31 5
1996                }
1997            }
1998            ITYP-get {
1999                return [string trim [readexp "${key}I ITYP"]]
2000            }
2001            proftbl-get {
2002                set line [readexp "${key}PAB3"]
2003                if {$line == ""} {return 0}
2004                set val [string trim [string range $line 0 4]]
2005                if {$val == ""} {return 0}
2006                return $val
2007            }
2008            anomff-get {
2009                set l {}
2010                foreach i {1 2 3 4 5 6 7 8 9} {
2011                    if {![existsexp "${key}FFANS$i"]} continue
2012                    set line [readexp "${key}FFANS$i"]
2013                    set elem [string trim [string range $line 2 9]]
2014                    set fp [string trim [string range $line 10 19]]
2015                    set fpp [string trim [string range $line 20 29]]
2016                    lappend l [list $elem $fp $fpp]
2017                }
2018                return $l
2019            }
2020            anomff-set {
2021                # match up input against elements in list.
2022                # change elements included, return any elements that are
2023                # not found.
2024                set errorlist {}
2025                foreach triplet $value {
2026                    foreach {e fp fpp} $triplet {}               
2027                    foreach i {1 2 3 4 5 6 7 8 9} {
2028                        if {![existsexp "${key}FFANS$i"]} continue
2029                        # note that the element name is not used or validated
2030                        set elem [string trim [string range \
2031                                                   [readexp "${key}FFANS$i"] 2 9]]
2032                        if {[string match -nocase $e $elem]} {
2033                            # seems to be a bug in GSAS if anom FF use too many digits,
2034                            # many digits, or perhaps due to exponential notation. Restricting
2035                            # the length to use the last 6 digits on output is a work around.
2036                            if ![validreal fp 6 3] {return 0}
2037                            setexp "${key}FFANS$i" "    $fp" 11 10
2038                            if ![validreal fpp 6 3] {return 0}
2039                            setexp "${key}FFANS$i" "    $fpp" 21 10
2040                            set e {}
2041                            break
2042                        }
2043                    }
2044                    if {$e != ""} {
2045                        # oops, no match
2046                        lappend errorlist $e
2047                    }
2048                }
2049                if {$errorlist != ""} {return [list 0 $errorlist]}
2050            }
2051            default {
2052                set msg "Unsupported histinfo access: parm=$parm action=$action"
2053                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
2054            }
2055        }
2056    }
2057    return 1
2058}
2059
2060proc add_anomff {histlist type {fp 0} {fpp 0}} {
2061    global expgui
2062    foreach hist $histlist {
2063        if {$hist < 10} {
2064            set key "HST  $hist"
2065        } else {
2066            set key "HST $hist"
2067        }
2068        if ![validreal fp 10 3] {return 0}
2069        if ![validreal fpp 10 3] {return 0}
2070        foreach i {1 2 3 4 5 6 7 8 9} {
2071            if {![existsexp "${key}FFANS$i"]} {
2072                makeexprec "${key}FFANS$i"
2073                setexp "${key}FFANS$i" [string trim $type] 3 8
2074                setexp "${key}FFANS$i" $fp 11 10
2075                setexp "${key}FFANS$i" $fpp 21 10
2076                setexp "${key}FFANS$i" "NN" 34 2
2077                return 1
2078            }
2079        }
2080        return 0
2081    }
2082}
2083
2084# read the information that differs by both histogram and phase (profile & phase fraction)
2085# use: hapinfo hist phase parm action value
2086
2087#     frac -- phase fraction (*)
2088#     frref/frdamp -- refinement flag/damping value for the phase fraction (*)
2089#     proftype -- profile function number (*)
2090#     profterms -- number of profile terms (*)
2091#     pdamp -- damping value for the profile (*)
2092#     pcut -- cutoff value for the profile (*)
2093#     pterm$n -- profile term #n (*)
2094#     pref$n -- refinement flag value for profile term #n (*)
2095#     extmeth -- Fobs extraction method (*)
2096#     POnaxis -- number of defined M-D preferred axes
2097proc hapinfo {histlist phaselist parm "action get" "value {}"} {
2098    foreach phase $phaselist hist $histlist {
2099        if {$phase == ""} {set phase [lindex $phaselist end]}
2100        if {$hist == ""} {set hist [lindex $histlist end]}
2101        if {$hist < 10} {
2102            set hist " $hist"
2103        }
2104        set key "HAP${phase}${hist}"
2105        switch -glob ${parm}-$action {
2106            extmeth-get {
2107                set i1 [expr {($phase - 1)*5}]
2108                set i2 [expr {$i1 + 4}]
2109                return [string trim [string range [readexp "HST $hist EPHAS"] $i1 $i2]]
2110            }
2111            extmeth-set {
2112                set i1 [expr {($phase - 1)*5 + 1}]
2113                if ![validint value 5] {return 0}
2114                setexp "HST $hist EPHAS" $value $i1 5
2115            }
2116            frac-get {
2117                return [string trim [string range [readexp ${key}PHSFR] 0 14]]
2118            }
2119            frac-set {
2120                if ![validreal value 15 6] {return 0}
2121                setexp ${key}PHSFR $value 1 15
2122            }
2123            frref-get {
2124                if {[string toupper [string range [readexp ${key}PHSFR] 19 19]] == "Y"} {
2125                    return 1
2126                }
2127                return 0
2128            }
2129            frref-set {
2130                if $value {
2131                    setexp ${key}PHSFR "Y" 20 1
2132                } else {
2133                    setexp ${key}PHSFR "N" 20 1
2134                }           
2135            }
2136            frdamp-get {
2137                set val [string range [readexp ${key}PHSFR] 24 24]
2138                if {$val == " "} {return 0}
2139                return $val
2140            }
2141            frdamp-set {
2142                setexp ${key}PHSFR $value 25 1
2143            }
2144            proftype-get {
2145                set val [string range [readexp "${key}PRCF "] 0 4]
2146                if {$val == " "} {return 0}
2147                return $val
2148            }
2149            proftype-set {
2150                if ![validint value 5] {return 0}
2151                setexp "${key}PRCF " $value 1 5
2152                # set the powpref warning (1 = suggested)
2153                catch {
2154                    global expgui
2155                    if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
2156                    set msg "Profile parameters" 
2157                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
2158                        append expgui(needpowpref_why) "\t$msg were changed\n"
2159                    }
2160                }
2161            }
2162            profterms-get {
2163                set val [string range [readexp "${key}PRCF "] 5 9]
2164                if {$val == " "} {return 0}
2165                return $val
2166            }
2167            profterms-set {
2168                if ![validint value 5] {return 0}
2169                setexp "${key}PRCF " $value 6 5
2170                # now check that all needed entries exist
2171                set lines [expr {1 + ($value - 1) / 4}]
2172                for {set i 1} {$i <= $lines} {incr i} {
2173                    makeexprec "${key}PRCF $i"
2174                }
2175                # set the powpref warning (1 = suggested)
2176                catch {
2177                    global expgui
2178                    if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
2179                    set msg "Profile parameters" 
2180                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
2181                        append expgui(needpowpref_why) "\t$msg were changed\n"
2182                    }
2183                }
2184            }
2185            pcut-get {
2186                return [string trim [string range [readexp "${key}PRCF "] 10 19]]
2187            }
2188            pcut-set {
2189                if ![validreal value 10 5] {return 0}
2190                setexp "${key}PRCF " $value 11 10
2191                # set the powpref warning (1 = suggested)
2192                catch {
2193                    global expgui
2194                    if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
2195                    set msg "Profile parameters" 
2196                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
2197                        append expgui(needpowpref_why) "\t$msg were changed\n"
2198                    }
2199                }
2200            }
2201            pdamp-get {
2202                set val [string range [readexp "${key}PRCF "] 24 24]
2203                if {$val == " "} {return 0}
2204                return $val
2205            }
2206            pdamp-set {
2207                setexp "${key}PRCF   " $value 25 1
2208            }
2209            pterm*-get {
2210                regsub pterm $parm {} num
2211                set f1 [expr {15*(($num - 1) % 4)}]
2212                set f2 [expr {15*(1 + ($num - 1) % 4)-1}]
2213                set line  [expr {1 + ($num - 1) / 4}]
2214                return [string trim [string range [readexp "${key}PRCF $line"] $f1 $f2] ]
2215            }
2216            pterm*-set {
2217                if ![validreal value 15 6] {return 0}
2218                regsub pterm $parm {} num
2219                set f1 [expr {1+ 15*(($num - 1) % 4)}]
2220                set line  [expr {1 + ($num - 1) / 4}]
2221                setexp "${key}PRCF $line" $value $f1 15
2222                # set the powpref warning (1 = suggested)
2223                catch {
2224                    global expgui
2225                    if {$expgui(needpowpref) == 0} {set expgui(needpowpref) 1}
2226                    set msg "Profile parameters" 
2227                    if {[string first $msg $expgui(needpowpref_why)] == -1} {
2228                        append expgui(needpowpref_why) "\t$msg were changed\n"
2229                    }
2230                }
2231            }
2232            pref*-get {
2233                regsub pref $parm {} num
2234                set f [expr {24+$num}]
2235                if {[string toupper [string range [readexp "${key}PRCF  "] $f $f]] == "Y"} {
2236                    return 1
2237                }
2238                return 0
2239            }
2240            pref*-set {
2241                regsub pref $parm {} num
2242                set f [expr {25+$num}]
2243                if $value {
2244                    setexp ${key}PRCF "Y" $f 1
2245                } else {
2246                    setexp ${key}PRCF "N" $f 1
2247                }           
2248            }
2249            POnaxis-get {
2250                set val [string trim \
2251                        [string range [readexp "${key}NAXIS"] 0 4]]
2252                if {$val == ""} {return 0}
2253                return $val
2254            }
2255            POnaxis-set {
2256                if ![validint value 5] {return 0}
2257                # there should be a NAXIS record, but if not make one
2258                if {![existsexp "${key}NAXIS"]} {
2259                    makeexprec "${key}NAXIS"
2260                }
2261                setexp "${key}NAXIS  " $value 1 5
2262            }
2263            default {
2264                set msg "Unsupported hapinfo access: parm=$parm action=$action"
2265                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
2266            }
2267        }
2268    }
2269    return 1
2270}
2271
2272#  read fixed constraints for a phase
2273proc atom_constraint_read {phase} {
2274    set fixlist ""
2275    foreach k {1 2 3 4 5 6 7 8 9} {
2276        set key [format "LEQV HOLD%1d%2d" $phase $k]
2277        set line [readexp $key]
2278        foreach j {2 10 18 26 34 42 50 58} {
2279            set fix_param [string range $line $j [expr $j+7]]
2280            if {[string trim $fix_param] == ""} {return $fixlist}
2281            lappend fixlist $fix_param
2282        }
2283    }
2284    return $fixlist
2285}
2286
2287# load all atom constraints into global array fix_param
2288proc atom_constraint_load { } {
2289    catch {unset ::fix_param}
2290    foreach i $::expmap(phaselist) {
2291        set temp [atom_constraint_read $i]
2292        foreach j $temp {
2293            set atomnum [string trim [string range $j 2 3]]
2294            set param [string trim [string range $j 4 6]]
2295            set ::fix_param($i,$atomnum,$param) 1   
2296        }
2297    }
2298}
2299
2300# returns 1 if the specified variable is fixed
2301proc atom_constraint_get {phase atom type} {
2302    if {[array names ::fix_param "$phase,$atom,$type"] == ""} {
2303        return 0
2304    }
2305    return 1
2306}
2307
2308proc atom_constraint_set {phase atomlist type mode} {
2309    foreach atom $atomlist {
2310        set key "$phase,$atom,$type"
2311        if {$mode} {
2312            set ::fix_param($key) 1
2313        } else {
2314            array unset ::fix_param $key
2315        }
2316    } 
2317    set fixlist {}
2318    foreach key [array names ::fix_param "$phase,*"] {
2319        foreach {j atom parm} [split $key ","] {}
2320        lappend fixlist \
2321            [format "%1s %+2s%-4s" $phase $atom $parm]
2322    }
2323    foreach key [array names ::exparray "LEQV HOLD$phase*"] {
2324        delexp $key
2325    }
2326    set k 0
2327    set j 1
2328    set line ""
2329    foreach fix $fixlist {
2330        incr k
2331        append line $fix
2332        if {$k == 8} {
2333            set key [format "LEQV HOLD%1d%2d" $phase $j]
2334            makeexprec $key
2335            setexp $key $line 3 [expr ($k * 8) + 2]
2336            set k 0
2337            incr j
2338            set line ""
2339        }
2340    }
2341    if {$line != ""} {
2342        set key [format "LEQV HOLD%1d%2d" $phase $j]
2343        makeexprec $key
2344        setexp $key $line 3 [expr ($k * 8) + 2]
2345    }   
2346}
2347
2348
2349#  get a logical constraint
2350#
2351#  type action
2352#  -----------
2353#  atom get  number        returns a list of constraints.
2354#   "   set  number value  replaces a list of constraints
2355#                          (value is a list of constraints)
2356#   "   add  number value  inserts a new list of constraints
2357#                          (number is ignored)
2358#   "   delete number      deletes a set of constraint entries
2359# Each item in the list of constraints is composed of 4 items:
2360#              phase, atom, variable, multiplier
2361# If variable=UISO atom can be ALL, otherwise atom is a number
2362# legal variable names: FRAC, X, Y, Z, UISO, U11, U22, U33, U12, U23, U13,
2363#                       MX, MY, MZ
2364#
2365#  type action
2366#  -----------
2367#  profileXX get number         returns a list of constraints for term XX=1-36
2368#                               use number=0 to get # of defined
2369#                                  constraints for term XX
2370#   "        set number value   replaces a list of constraints
2371#                               (value is a list of constraints)
2372#   "        add number value   inserts a new list of constraints
2373#                               (number is ignored)
2374#   "        delete number      deletes a set of constraint entries
2375# Each item in the list of constraints is composed of 3 items:
2376#              phase-list, histogram-list, multiplier
2377# Note that phase-list and/or histogram-list can be ALL
2378#
2379#  type action
2380#  -----------
2381#  absorbX get number         returns a list of constraints for term X=1 or 2
2382#   returns a the number of constraints for number = 0
2383#   returns a list of lists {{hist mult} {hist mult} ...}
2384
2385#  absorbX set number value   replaces a list of constraints
2386#      number corresponds to a specific constraint see "absorbX get 0"
2387#      value is a list of lists {{hist mult} {hist mult} ...}
2388#  absorbX add number value   inserts a new list of constraints
2389#                               (number is ignored)
2390#  absorbX  delete number      deletes a set of constraint entries and renumbers
2391# note that hist can be:
2392#      a histogram number (such as 2) or
2393#      range of histograms (such as 1:10 or 11:99, etc.) or
2394#      the string "ALL"
2395
2396proc constrinfo {type action number "value {}"} {
2397    global expmap
2398    if {[lindex $expmap(phasetype) 0] == 4} {
2399        set mm 1
2400    } else {
2401        set mm 0
2402    }
2403    switch -glob ${type}-$action {
2404        atom-get {
2405            # does this constraint exist?
2406            set key [format "LNCN%4d%4d" $number 1]
2407            if {![existsexp $key]} {return -1}
2408            set clist {}
2409            for {set i 1} {$i < 999} {incr i} {
2410                set key [format "LNCN%4d%4d" $number $i]
2411                if {![existsexp $key]} break
2412                set line [readexp $key]
2413                set j1 2
2414                set j2 17
2415                set seg [string range $line $j1 $j2]
2416                while {[string trim $seg] != ""} {
2417                    set p [string range $seg 0 0]
2418                    if {$p == 1 && $mm} {
2419                        set atom [string trim [string range $seg 1 4]]
2420                        set var [string trim [string range $seg 5 7]]
2421                        if {$atom == "ALL"} {
2422                            set var UIS
2423                        } else {
2424                            scan $atom %x atom
2425                        }
2426                        lappend clist [list $p $atom $var \
2427                                [string trim [string range $seg 8 end]]]
2428                    } else {
2429                        lappend clist [list $p \
2430                                [string trim [string range $seg 1 3]] \
2431                                [string trim [string range $seg 4 7]] \
2432                                [string trim [string range $seg 8 end]]]
2433                    }
2434                    incr j1 16
2435                    incr j2 16
2436                    set seg [string range $line $j1 $j2]
2437                }
2438            }
2439            return $clist
2440        }
2441        atom-set {
2442            # delete records for current constraint
2443            for {set i 1} {$i < 999} {incr i} {
2444                set key [format "LNCN%4d%4d" $number $i]
2445                if {![existsexp $key]} break
2446                delexp $key
2447            }
2448            set line {}
2449            set i 1
2450            foreach tuple $value {
2451                set p [lindex $tuple 0]
2452                if {$p == 1 && $mm && \
2453                        [string toupper [lindex $tuple 1]] == "ALL"} {
2454                    set seg [format %1dALL UIS%8.4f \
2455                            [lindex $tuple 0] \
2456                            [lindex $tuple 3]]
2457                } elseif {$p == 1 && $mm} {
2458                    set seg [eval format %1d%.4X%-3s%8.4f $tuple]
2459                } elseif {[string toupper [lindex $tuple 1]] == "ALL"} {
2460                    set seg [format %1dALL%-4s%8.4f \
2461                            [lindex $tuple 0] \
2462                            [lindex $tuple 2] \
2463                            [lindex $tuple 3]]
2464                } else {
2465                    set seg [eval format %1d%3d%-4s%8.4f $tuple]
2466                }
2467                append line $seg
2468                if {[string length $line] > 50} {
2469                    set key  [format "LNCN%4d%4d" $number $i]
2470                    makeexprec $key
2471                    setexp $key $line 3 68
2472                    set line {}
2473                    incr i
2474                }
2475            }
2476            if {$line != ""} {
2477                set key  [format "LNCN%4d%4d" $number $i]
2478                makeexprec $key
2479                setexp $key $line 3 68
2480            }
2481            return
2482        }
2483        atom-add {
2484            # loop over defined constraints
2485            for {set j 1} {$j < 9999} {incr j} {
2486                set key [format "LNCN%4d%4d" $j 1]
2487                if {![existsexp $key]} break
2488            }
2489            set number $j
2490            # save the constraint
2491            set line {}
2492            set i 1
2493            foreach tuple $value {
2494                set p [lindex $tuple 0]
2495                if {$p == 1 && $mm && \
2496                        [string toupper [lindex $tuple 1]] == "ALL"} {
2497                    set seg [format %1dALL UIS%8.4f \
2498                            [lindex $tuple 0] \
2499                            [lindex $tuple 3]]
2500                } elseif {$p == 1 && $mm} {
2501                    set seg [eval format %1d%.4X%-3s%8.4f $tuple]
2502                } elseif {[string toupper [lindex $tuple 1]] == "ALL"} {
2503                    set seg [format %1dALL%-4s%8.4f \
2504                            [lindex $tuple 0] \
2505                            [lindex $tuple 2] \
2506                            [lindex $tuple 3]]
2507                } else {
2508                    set seg [eval format %1d%3d%-4s%8.4f $tuple]
2509                }
2510                append line $seg
2511                if {[string length $line] > 50} {
2512                    set key  [format "LNCN%4d%4d" $number $i]
2513                    makeexprec $key
2514                    setexp $key $line 3 68
2515                    set line {}
2516                    incr i
2517                }
2518            }
2519            if {$line != ""} {
2520                set key  [format "LNCN%4d%4d" $number $i]
2521                makeexprec $key
2522                setexp $key $line 3 68
2523            }
2524            return
2525        }
2526        atom-delete {
2527            for {set j $number} {$j < 9999} {incr j} {
2528                # delete records for current constraint
2529                for {set i 1} {$i < 999} {incr i} {
2530                    set key [format "LNCN%4d%4d" $j $i]
2531                    if {![existsexp $key]} break
2532                    delexp $key
2533                }
2534                # now copy records, from the next entry, if any
2535                set j1 $j
2536                incr j1
2537                set key1 [format "LNCN%4d%4d" $j1 1]
2538                # if there is no record, there is nothing to copy -- done
2539                if {![existsexp $key1]} return
2540                for {set i 1} {$i < 999} {incr i} {
2541                    set key1 [format "LNCN%4d%4d" $j1 $i]
2542                    if {![existsexp $key1]} break
2543                    set key  [format "LNCN%4d%4d" $j  $i]
2544                    makeexprec $key
2545                    setexp $key [readexp $key1] 1 68
2546                }
2547            }
2548        }
2549        profile*-delete {
2550            regsub profile $type {} term
2551            if {$term < 10} {
2552                set term " $term"
2553            }
2554            set key "LEQV PF$term   "
2555            # return nothing if no term exists
2556            if {![existsexp $key]} {return 0}
2557
2558            # number of constraint terms
2559            set nterms [string trim [string range [readexp ${key}] 0 4] ]
2560            # don't delete a non-existing entry
2561            if {$number > $nterms} {return 0}
2562            set val [expr {$nterms - 1}]
2563            validint val 5
2564            setexp $key $val 1 5
2565            for {set i1 $number} {$i1 < $nterms} {incr i1} {
2566                set i2 [expr {1 + $i1}]
2567                # move the contents of constraint #i2 -> i1
2568                if {$i1 > 9} {
2569                    set k1 [expr {($i1+1)/10}]
2570                    set l1 $i1
2571                } else {
2572                    set k1 " "
2573                    set l1 " $i1"
2574                }
2575                set key1 "LEQV PF$term  $k1"
2576                # number of constraint lines for #i1
2577                set n1 [string trim [string range [readexp ${key1}] \
2578                        [expr {($i1%10)*5}] [expr {4+(($i1%10)*5)}]] ]
2579                if {$i2 > 9} {
2580                    set k2 [expr {($i2+1)/10}]
2581                    set l2 $i2
2582                } else {
2583                    set k2 " "
2584                    set l2 " $i2"
2585                }
2586                set key2 "LEQV PF$term  $k2"
2587                # number of constraint lines for #i2
2588                set n2 [string trim [string range [readexp ${key2}] \
2589                        [expr {($i2%10)*5}] [expr {4+(($i2%10)*5)}]] ]
2590                set val $n2
2591                validint val 5
2592                # move the # of terms
2593                setexp $key1 $val [expr {1+(($i1%10)*5)}] 5
2594                # move the terms
2595                for {set j 1} {$j <= $n2} {incr j 1} {
2596                    set key "LEQV PF${term}${l1}$j"
2597                    makeexprec $key
2598                    setexp $key [readexp "LEQV PF${term}${l2}$j"] 1 68
2599                }
2600                # delete any remaining lines
2601                for {set j [expr {$n2+1}]} {$j <= $n1} {incr j 1} {
2602                    delexp "LEQV PF${term}${l1}$j"
2603                }
2604            }
2605
2606            # clear the last term
2607            if {$nterms > 9} {
2608                set i [expr {($nterms+1)/10}]
2609            } else {
2610                set i " "
2611            }
2612            set key "LEQV PF$term  $i"
2613            set cb [expr {($nterms%10)*5}]
2614            set ce [expr {4+(($nterms%10)*5)}]
2615            set n2 [string trim [string range [readexp ${key}] $cb $ce] ]
2616            incr cb
2617            setexp $key "     " $cb 5
2618            # delete any remaining lines
2619            for {set j 1} {$j <= $n2} {incr j 1} {
2620                delexp "LEQV PF${term}${nterms}$j"
2621            }
2622        }
2623        profile*-set {
2624            regsub profile $type {} term
2625            if {$term < 10} {
2626                set term " $term"
2627            }
2628            set key "LEQV PF$term   "
2629            # get number of constraint terms
2630            set nterms [string trim [string range [readexp ${key}] 0 4] ]
2631            # don't change a non-existing entry
2632            if {$number > $nterms} {return 0}
2633            if {$number > 9} {
2634                set k1 [expr {($number+1)/10}]
2635                set l1 $number
2636            } else {
2637                set k1 " "
2638                set l1 " $number"
2639            }
2640            set key1 "LEQV PF$term  $k1"
2641            # old number of constraint lines
2642            set n1 [string trim [string range [readexp ${key1}] \
2643                    [expr {($number%10)*5}] [expr {4+(($number%10)*5)}]] ]
2644            # number of new constraints
2645            set j2 [llength $value]
2646            # number of new constraint lines
2647            set val [set n2 [expr {($j2 + 2)/3}]]
2648            # store the new # of lines
2649            validint val 5
2650            setexp $key1 $val [expr {1+(($number%10)*5)}] 5
2651
2652            # loop over the # of lines in the old or new, whichever is greater
2653            set v0 0
2654            for {set j 1} {$j <= [expr {($n1 > $n2) ? $n1 : $n2}]} {incr j 1} {
2655                set key "LEQV PF${term}${l1}$j"
2656                # were there more lines in the old?
2657                if {$j > $n2} {
2658                    # this line is not needed
2659                    if {$j % 3 == 1} {
2660                        delexp $key
2661                    }
2662                    continue
2663                }
2664                # are we adding new lines?
2665                if {$j > $n1} {
2666                    makeexprec $key
2667                }
2668                # add the three constraints to the line
2669                foreach s {3 23 43} \
2670                        item [lrange $value $v0 [expr {2+$v0}]] {
2671                    if {$item != ""} {
2672                        set val [format %-10s%9.3f \
2673                                [lindex $item 0],[lindex $item 1] \
2674                                [lindex $item 2]]
2675                        setexp $key $val $s 19
2676                    } else {
2677                        setexp $key " " $s 19
2678                    }
2679                }
2680                incr v0 3
2681            }
2682        }
2683        profile*-add {
2684            regsub profile $type {} term
2685            if {$term < 10} {
2686                set term " $term"
2687            }
2688            set key "LEQV PF$term   "
2689            if {![existsexp $key]} {makeexprec $key}
2690            set nterms [string trim [string range [readexp ${key}] 0 4] ]
2691            if {$nterms == ""} {
2692                set nterms 1
2693            } elseif {$nterms >= 99} {
2694                return 0
2695            } else {
2696                incr nterms
2697            }
2698            # store the new # of constraints
2699            set val $nterms
2700            validint val 5
2701            setexp $key $val 1 5
2702
2703            if {$nterms > 9} {
2704                set k1 [expr {($nterms+1)/10}]
2705                set l1 $nterms
2706            } else {
2707                set k1 " "
2708                set l1 " $nterms"
2709            }
2710            set key1 "LEQV PF$term  $k1"
2711
2712            # number of new constraints
2713            set j2 [llength $value]
2714            # number of new constraint lines
2715            set val [set n2 [expr {($j2 + 2)/3}]]
2716            # store the new # of lines
2717            validint val 5
2718            setexp $key1 $val [expr {1+(($nterms%10)*5)}] 5
2719
2720            # loop over the # of lines to be added
2721            set v0 0
2722            for {set j 1} {$j <= $n2} {incr j 1} {
2723                set key "LEQV PF${term}${l1}$j"
2724                makeexprec $key
2725                # add the three constraints to the line
2726                foreach s {3 23 43} \
2727                        item [lrange $value $v0 [expr {2+$v0}]] {
2728                    if {$item != ""} {
2729                        set val [format %-10s%9.3f \
2730                                [lindex $item 0],[lindex $item 1] \
2731                                [lindex $item 2]]
2732                        setexp $key $val $s 19
2733                    } else {
2734                        setexp $key " " $s 19
2735                    }
2736                }
2737                incr v0 3
2738            }
2739        }
2740        profile*-get {
2741            regsub profile $type {} term
2742            if {$term < 10} {
2743                set term " $term"
2744            }
2745            if {$number > 9} {
2746                set i [expr {($number+1)/10}]
2747            } else {
2748                set i " "
2749            }
2750            set key "LEQV PF$term  $i"
2751            # return nothing if no term exists
2752            if {![existsexp $key]} {return 0}
2753            # number of constraint lines
2754           
2755            set numline [string trim [string range [readexp ${key}] \
2756                    [expr {($number%10)*5}] [expr {4+(($number%10)*5)}]] ]
2757            if {$number == 0} {return $numline}
2758            set clist {}
2759            if {$number < 10} {
2760                set number " $number"
2761            }
2762            for {set i 1} {$i <= $numline} {incr i} {
2763                set key "LEQV PF${term}${number}$i"
2764                set line [readexp ${key}]
2765                foreach s {1 21 41} e {20 40 60} {
2766                    set seg [string range $line $s $e]
2767                    if {[string trim $seg] == ""} continue
2768                    # parse the string segment
2769                    set parse [regexp { *([0-9AL]+),([0-9AL]+) +([0-9.]+)} \
2770                            $seg junk phase hist mult]
2771                    # was parse successful
2772                    if {!$parse} {continue}
2773                    lappend clist [list $phase $hist $mult]
2774                }
2775            }
2776            return $clist
2777        }
2778        absorb*-delete {
2779            regsub absorb $type {} term
2780            set key "LEQV ABS$term   "
2781            if {! [existsexp $key]} {return 0}
2782            # current number of constraints
2783            set nterm [string trim [string range [readexp $key] 0 5]]
2784            if {$nterm == ""} {return 0}
2785            # does the entry exist?
2786            if {$nterm < $number} {
2787                puts "deleted!"
2788                return $nterm
2789            }
2790            for {set target $number} {$target < $nterm} {incr target} {
2791                set source [expr {$target + 1}]
2792                set recs [GetAbsCount $term $source]
2793                SetAbsCount $term $target [expr {3*$recs}]
2794                validint source 2
2795                validint target 2
2796                for {set i 1} {$i <= $recs} {incr i} {
2797                    set keyin "LEQV ABS${term}${source}$i"
2798                    set keyout "LEQV ABS${term}${target}$i"
2799                    set ::exparray($keyout) $::exparray($keyin)
2800                }
2801            }
2802            SetAbsCount $term $nterm 0
2803            # delete the last entry
2804            validint nterm 2
2805            foreach i {1 2 3 4 5 6 7 8 9} {
2806                set key "LEQV ABS${term}${nterm}$i"
2807                delexp $key
2808            }
2809            # decrease the count by one
2810            set nterm [expr {[string trim $nterm] - 1}]
2811            if {$nterm == 0} {
2812                delexp "LEQV ABS$term   "
2813            } else {
2814                validint nterm 5
2815                setexp "LEQV ABS$term   " $nterm 1 5                   
2816            }
2817            return [string trim $nterm]
2818
2819            if {$term < 10} {
2820                set term " $term"
2821            }
2822            set key "LEQV PF$term   "
2823            # return nothing if no term exists
2824            if {![existsexp $key]} {return 0}
2825
2826            # number of constraint terms
2827            set nterms [string trim [string range [readexp ${key}] 0 4] ]
2828            # don't delete a non-existing entry
2829            if {$number > $nterms} {return 0}
2830            set val [expr {$nterms - 1}]
2831            validint val 5
2832            setexp $key $val 1 5
2833            for {set i1 $number} {$i1 < $nterms} {incr i1} {
2834                set i2 [expr {1 + $i1}]
2835                # move the contents of constraint #i2 -> i1
2836                if {$i1 > 9} {
2837                    set k1 [expr {($i1+1)/10}]
2838                    set l1 $i1
2839                } else {
2840                    set k1 " "
2841                    set l1 " $i1"
2842                }
2843                set key1 "LEQV PF$term  $k1"
2844                # number of constraint lines for #i1
2845                set n1 [string trim [string range [readexp ${key1}] \
2846                        [expr {($i1%10)*5}] [expr {4+(($i1%10)*5)}]] ]
2847                if {$i2 > 9} {
2848                    set k2 [expr {($i2+1)/10}]
2849                    set l2 $i2
2850                } else {
2851                    set k2 " "
2852                    set l2 " $i2"
2853                }
2854                set key2 "LEQV PF$term  $k2"
2855                # number of constraint lines for #i2
2856                set n2 [string trim [string range [readexp ${key2}] \
2857                        [expr {($i2%10)*5}] [expr {4+(($i2%10)*5)}]] ]
2858                set val $n2
2859                validint val 5
2860                # move the # of terms
2861                setexp $key1 $val [expr {1+(($i1%10)*5)}] 5
2862                # move the terms
2863                for {set j 1} {$j <= $n2} {incr j 1} {
2864                    set key "LEQV PF${term}${l1}$j"
2865                    makeexprec $key
2866                    setexp $key [readexp "LEQV PF${term}${l2}$j"] 1 68
2867                }
2868                # delete any remaining lines
2869                for {set j [expr {$n2+1}]} {$j <= $n1} {incr j 1} {
2870                    delexp "LEQV PF${term}${l1}$j"
2871                }
2872            }
2873
2874            # clear the last term
2875            if {$nterms > 9} {
2876                set i [expr {($nterms+1)/10}]
2877            } else {
2878                set i " "
2879            }
2880            set key "LEQV PF$term  $i"
2881            set cb [expr {($nterms%10)*5}]
2882            set ce [expr {4+(($nterms%10)*5)}]
2883            set n2 [string trim [string range [readexp ${key}] $cb $ce] ]
2884            incr cb
2885            setexp $key "     " $cb 5
2886            # delete any remaining lines
2887            for {set j 1} {$j <= $n2} {incr j 1} {
2888                delexp "LEQV PF${term}${nterms}$j"
2889            }
2890        }
2891        absorb*-set {
2892            regsub absorb $type {} term
2893            if {$number < 1} return   
2894            # delete old records
2895            set l [GetAbsCount $term $number]
2896            set num $number
2897            validint num 2
2898            for {set i 1} {$i <= $l} {incr i} {
2899                delexp "LEQV ABS${term}${num}$i"
2900            }
2901            # record the new number of records
2902            SetAbsCount $term $number [llength $value]
2903            # save the new records
2904            set i 1
2905            set offh 2
2906            set offm 14
2907            foreach set $value {
2908                set hist [string trim [lindex $set 0]]
2909                set mult [string trim [lindex $set 1]]
2910                validreal mult 8 4
2911                set key "LEQV ABS${term}${num}$i"
2912                if {$offh == 2} {
2913                    makeexprec $key
2914                }
2915                setexp $key $hist [expr {$offh+1}] 11
2916                setexp $key $mult [expr {$offm+1}] 8
2917                incr offh 21
2918                incr offm 21
2919                if {$offm > 67} {
2920                    incr i
2921                    set offh 2
2922                    set offm 14
2923                }
2924            }
2925            return
2926        }
2927        absorb*-add {
2928            regsub absorb $type {} term
2929            set key "LEQV ABS$term   "
2930            if {! [existsexp $key]} {makeexprec $key}
2931            # current number of constraints
2932            set nterm [string trim [string range [readexp $key] 0 5]]
2933            if {$nterm == ""} {set nterm 0}
2934            if {$nterm >= 99} {
2935                return $nterm
2936            }
2937            incr nterm
2938            validint nterm 5
2939            setexp $key $nterm 1 5
2940            constrinfo $type set [string trim $nterm] $value
2941            return [string trim $nterm]
2942        }
2943        absorb*-get {
2944            regsub absorb $type {} term
2945            # no constraints, return blank
2946            set key "LEQV ABS$term   "
2947            if {! [existsexp $key]} {return ""}
2948            # requesting number of constraints
2949            if {$number == 0} {
2950                set l [string trim [string range [readexp ${key}] 0 5]]
2951                if {$l == ""} {return 0}
2952                return $l
2953            }
2954            #
2955            if {$number > 9} {
2956                set num $number
2957                set i [expr {($number+1)/10}]
2958                set off [expr {5*($number % 10)}]
2959                set key "LEQV ABS$term  $i"
2960            } else {
2961                set num " $number"
2962                set i " "
2963                set off [expr {5*($number % 10)}]
2964            }
2965            set off1 [expr {$off + 5}]
2966            set l [string trim [string range [readexp ${key}] $off $off1]]
2967            if {$l == ""} {return {}}
2968            # now look up those records
2969            set res {}
2970            for {set i 1} {$i <= $l} {incr i} {
2971                set key "LEQV ABS${term}${num}$i"
2972                for {set j 0} {$j < 3} {incr j} {
2973                    set off [expr {2 + 21*$j}]
2974                    set off1 [expr {$off + 11}]
2975                    set hist [string trim [string range [readexp ${key}] $off $off1]]
2976                    set off [expr {14 + 21*$j}]
2977                    set off1 [expr {$off + 7}]
2978                    set mult [string trim [string range [readexp ${key}] $off $off1]]
2979                    if {$mult == ""} break
2980                    lappend res [list $hist $mult]
2981                }
2982            }
2983            return $res
2984        }
2985        default {
2986            set msg "Unsupported constrinfo access: type=$type action=$action"
2987            tk_dialog .badexp "Error in readexp access" $msg error 0 OK
2988        }
2989
2990    }
2991}
2992proc GetAbsCount {term number} {
2993    if {$number > 9} {
2994        set num $number
2995        set off [expr {5*($number % 10)}]
2996        set i [expr {($number+1)/10}]
2997        set key "LEQV ABS$term  $i"
2998    } else {
2999        set num " $number"
3000        set off [expr {5*($number % 10)}]
3001        set key "LEQV ABS$term   "
3002    }
3003    set off1 [expr {$off + 5}]
3004    set l [string trim [string range [readexp ${key}] $off $off1]]
3005    if {$l == ""} {set l 0}
3006    return $l
3007}
3008proc SetAbsCount {term number len} {
3009    if {$number > 9} {
3010        set num $number
3011        set off [expr {1 + 5*($number % 10)}]
3012        set i [expr {($number+1)/10}]
3013        set key "LEQV ABS$term  $i"
3014    } else {
3015        set num " $number"
3016        set off [expr {1 + 5*($number % 10)}]
3017        set key "LEQV ABS$term   "
3018    }
3019    set l [expr {($len + 2)/3}]
3020    set val $l
3021    validint val 5
3022    setexp $key $val $off 5
3023}
3024
3025# read the default profile information for a histogram
3026# use: profdefinfo hist set# parm action
3027
3028#     proftype -- profile function number
3029#     profterms -- number of profile terms
3030#     pdamp -- damping value for the profile (*)
3031#     pcut -- cutoff value for the profile (*)
3032#     pterm$n -- profile term #n
3033#     pref$n -- refinement flag value for profile term #n (*)
3034
3035proc profdefinfo {hist set parm "action get"} {
3036    global expgui
3037    if {$hist < 10} {
3038        set key "HST  $hist"
3039    } else {
3040        set key "HST $hist"
3041    }
3042    switch -glob ${parm}-$action {
3043        proftype-get {
3044            set val [string range [readexp "${key}PRCF$set"] 0 4]
3045            if {$val == " "} {return 0}
3046            return $val
3047        }
3048        profterms-get {
3049            set val [string range [readexp "${key}PRCF$set"] 5 9]
3050            if {$val == " "} {return 0}
3051            return $val
3052        }
3053        pcut-get {
3054            return [string trim [string range [readexp "${key}PRCF$set"] 10 19]]
3055        }
3056        pdamp-get {
3057                set val [string range [readexp "${key}PRCF$set"] 24 24]
3058            if {$val == " "} {return 0}
3059            return $val
3060        }
3061        pterm*-get {
3062            regsub pterm $parm {} num
3063            set f1 [expr {15*(($num - 1) % 4)}]
3064            set f2 [expr {15*(1 + ($num - 1) % 4)-1}]
3065            set line  [expr {1 + ($num - 1) / 4}]
3066            return [string trim [string range [\
3067                        readexp "${key}PRCF${set}$line"] $f1 $f2] ]
3068        }
3069        pref*-get {
3070            regsub pref $parm {} num
3071            set f [expr {24+$num}]
3072            if {[string toupper [string range [readexp "${key}PRCF$set"] $f $f]] == "Y"} {
3073                return 1
3074            }
3075            return 0
3076        }
3077        default {
3078            set msg "Unsupported profdefinfo access: parm=$parm action=$action"
3079            tk_dialog .badexp "Code Error" $msg error 0 Exit
3080        }
3081    }
3082}
3083
3084# get March-Dollase preferred orientation information
3085# use MDprefinfo hist phase axis-number parm action value
3086#    ratio    -- ratio of xtallites in PO direction vs random (>1 for more)
3087#    fraction -- fraction in this direction, when more than one axis is used
3088#    h k & l  -- indices of P.O. axis
3089#    ratioref -- flag to vary ratio
3090#    fracref  -- flag to vary fraction
3091#    damp     -- damping value
3092#    type     -- model type (0 = P.O. _|_ to beam, 1 = || to beam)
3093#    new      -- creates a new record with default values (set only)
3094proc MDprefinfo {histlist phaselist axislist parm "action get" "value {}"} {
3095    foreach phase $phaselist hist $histlist axis $axislist {
3096        if {$phase == ""} {set phase [lindex $phaselist end]}
3097        if {$hist == ""} {set hist [lindex $histlist end]}
3098        if {$axis == ""} {set axis [lindex $axislist end]}
3099        if {$hist < 10} {
3100            set hist " $hist"
3101        }
3102        if {$axis > 9} {
3103            set axis "0"
3104        }
3105        set key "HAP${phase}${hist}PREFO${axis}"
3106        switch -glob ${parm}-$action {
3107            ratio-get {
3108                return [string trim [string range [readexp $key] 0 9]]
3109            }
3110            ratio-set {
3111                if ![validreal value 10 6] {return 0}
3112                setexp $key $value 1 10
3113            }
3114            fraction-get {
3115                return [string trim [string range [readexp $key] 10 19]]
3116            }
3117            fraction-set {
3118                if ![validreal value 10 6] {return 0}
3119                setexp $key $value 11 10
3120            }
3121            h-get {
3122                set h [string trim [string range [readexp $key] 20 29]]
3123                # why not allow negative h values?
3124                #               if {$h < 1} {return 0}
3125                return $h
3126            }
3127            h-set {
3128                if ![validreal value 10 2] {return 0}
3129                setexp $key $value 21 10
3130            }
3131            k-get {
3132                set k [string trim [string range [readexp $key] 30 39]]
3133                #               if {$k < 1} {return 0}
3134                return $k
3135            }
3136            k-set {
3137                if ![validreal value 10 2] {return 0}
3138                setexp $key $value 31 10
3139            }
3140            l-get {
3141                set l [string trim [string range [readexp $key] 40 49]]
3142                #if {$l < 1} {return 0}
3143                return $l
3144            }
3145            l-set {
3146                if ![validreal value 10 2] {return 0}
3147                setexp $key $value 41 10
3148            }
3149            ratioref-get {
3150                if {[string toupper \
3151                        [string range [readexp $key] 53 53]] == "Y"} {
3152                    return 1
3153                }
3154                return 0
3155            }
3156            ratioref-set {
3157                if $value {
3158                    setexp $key "Y" 54 1
3159                } else {
3160                    setexp $key "N" 54 1
3161                }
3162            }
3163            fracref-get {
3164                if {[string toupper \
3165                        [string range [readexp $key] 54 54]] == "Y"} {
3166                    return 1
3167                }
3168                return 0
3169            }
3170            fracref-set {
3171                if $value {
3172                    setexp $key "Y" 55 1
3173                } else {
3174                    setexp $key "N" 55 1
3175              }
3176            }
3177            damp-get {
3178                set val [string trim [string range [readexp $key] 59 59]]
3179                if {$val == " "} {return 0}
3180                return $val
3181            }
3182            damp-set {
3183                setexp $key $value 60 1
3184            }
3185            type-get {
3186                set val [string trim [string range [readexp $key] 64 64]]
3187                if {$val == " "} {return 0}
3188                return $val
3189            }
3190            type-set {
3191                # only valid settings are 0 & 1
3192                if {$value != "0" && $value != "1"} {set value "0"}
3193                setexp $key $value 65 1
3194            }
3195            new-set {
3196                makeexprec $key
3197                setexp $key \
3198                        {  1.000000  1.000000  0.000000  0.000000  1.000000   NN    0    0} \
3199                        1 68
3200            }
3201            default {
3202                set msg "Unsupported MDprefinfo access: parm=$parm action=$action"
3203                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
3204            }
3205
3206        }
3207
3208    }
3209}
3210
3211# get list of defined atom types
3212proc AtmTypList {} {
3213    set natypes [readexp " EXPR  NATYP"]
3214    if {$natypes == ""} return
3215    set j 0
3216    set typelist {}
3217    for {set i 1} {$i <= $natypes} {incr i} {
3218        set key {this should never be matched}
3219        while {![existsexp $key]} {
3220            incr j
3221            if {$j > 99} {
3222                return $typelist
3223            } elseif {$j <10} {
3224                set key " EXPR ATYP $j"
3225            } else {
3226                set key " EXPR ATYP$j"
3227            }
3228        }
3229        lappend typelist [string trim [string range $::exparray($key) 2 9]]
3230    }
3231    return $typelist
3232}
3233
3234# read information about atom types
3235#     distrad    atomic distance search radius (get/set)
3236#     angrad     atomic angle search radius (get/set)
3237proc AtmTypInfo {parm atmtype "action get" "value {}"} {
3238    # first, search through the records to find the record matching the type
3239    set natypes [readexp " EXPR  NATYP"]
3240    if {$natypes == ""} return
3241    set j 0
3242    set typelist {}
3243    for {set i 1} {$i <= $natypes} {incr i} {
3244        set key {this should never be matched}
3245        while {![existsexp $key]} {
3246            incr j
3247            if {$j > 99} {
3248                return $typelist
3249            } elseif {$j <10} {
3250                set key " EXPR ATYP $j"
3251            } else {
3252                set key " EXPR ATYP$j"
3253            }
3254        }
3255        if {[string toupper $atmtype] == \
3256                [string toupper [string trim [string range $::exparray($key) 2 9]]]} break
3257        set key {}
3258    }
3259    if {$key == ""} {
3260        # atom type not found
3261        return {}
3262    }
3263    switch -glob ${parm}-$action {
3264        distrad-get {
3265            return [string trim [string range [readexp $key] 15 24]]
3266        }
3267        distrad-set {
3268            if ![validreal value 10 2] {return 0}
3269            setexp $key $value 16 10
3270        }
3271        angrad-get {
3272            return [string trim [string range [readexp $key] 25 34]]
3273        }
3274        angrad-set {
3275            if ![validreal value 10 2] {return 0}
3276            setexp $key $value 26 10
3277        }
3278        default {
3279            set msg "Unsupported AtmTypInfo access: parm=$parm action=$action"
3280            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
3281        }
3282    }
3283}
3284# read default information about atom types (records copied to the .EXP file
3285# from the gsas/data/atomdata.dat file as AFAC ...
3286#     distrad returns a list of atom types (one or two letters) and
3287#                the corresponding distance
3288# note that these values are read only (no set option)
3289proc DefAtmTypInfo {parm} {
3290    set keys [array names ::exparray " AFAC *_SIZ"]
3291    set elmlist {}
3292    if {[llength $keys] <= 0} {return ""}
3293    foreach key $keys {
3294        lappend elmlist [string trim [string range $key 6 7]]
3295    }
3296    switch -glob ${parm} {
3297        distrad {
3298            set out {}
3299            foreach key $keys elm $elmlist {
3300                set val [string range $::exparray($key) 0 9]
3301                lappend out "$elm [string trim $val]"
3302            }
3303            return $out
3304        }
3305        angrad {
3306            set out {}
3307            foreach key $keys elm $elmlist {
3308                set val [string range $::exparray($key) 10 19]
3309                lappend out "$elm [string trim $val]"
3310            }
3311            return $out
3312        }
3313        default {
3314            set msg "Unsupported DefAtmTypInfo access: parm=$parm"
3315            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
3316        }
3317    }
3318}
3319# write the .EXP file
3320proc expwrite {expfile} {
3321    global exparray
3322    set blankline \
3323     "                                                                        "
3324    set fp [open ${expfile} w]
3325    fconfigure $fp -translation crlf -encoding ascii
3326    set keylist [lsort [array names exparray]]
3327    # reorder the keys so that VERSION comes 1st
3328    set pos [lsearch -exact $keylist {     VERSION}]
3329    set keylist "{     VERSION} [lreplace $keylist $pos $pos]"
3330    foreach key $keylist {
3331        puts $fp [string range \
3332                "$key$exparray($key)$blankline" 0 79]
3333    }
3334    close $fp
3335}
3336
3337# history commands -- delete all but last $keep history records,
3338# renumber if $renumber is true
3339proc DeleteHistory {keep renumber} {
3340    global exparray
3341    foreach y [lrange [lsort -decreasing \
3342            [array names exparray {    HSTRY*}]] $keep end] {
3343        unset exparray($y)
3344    }
3345    if !$renumber return
3346    # renumber
3347    set i 0
3348    foreach y [lsort -increasing \
3349            [array names exparray {    HSTRY*}]] {
3350        set key [format "    HSTRY%3d" [incr i]]
3351        set exparray($key) $exparray($y)
3352        unset exparray($y)
3353    }
3354    # list all history
3355    #    foreach y [lsort -decreasing [array names exparray {    HSTRY*}]] {puts "$y $exparray($y)"}
3356}
3357
3358proc CountHistory {} {
3359    global exparray
3360    return [llength [array names exparray {    HSTRY*}]]
3361}
3362
3363# set the phase flags for histogram $hist to $plist
3364proc SetPhaseFlag {hist plist} {
3365    # make a 2 digit key -- hh
3366    if {$hist < 10} {
3367        set hh " $hist"
3368    } else {
3369        set hh $hist
3370    }
3371    set key "HST $hh NPHAS"
3372    set str {}
3373    foreach iph {1 2 3 4 5 6 7 8 9} {
3374        if {[lsearch $plist $iph] != -1} {
3375            append str {    1}
3376        } else {
3377            append str {    0}     
3378        }
3379    }
3380    setexp $key $str 1 68
3381}
3382
3383# erase atom $atom from phase $phase
3384# update the list of atom types, erasing the record if not needed.
3385proc EraseAtom {atom phase} {
3386    set type [atominfo $phase $atom type]
3387    if {$type == ""} return
3388    if {$atom < 10} {
3389        set key "CRS$phase  AT  $atom"
3390    } elseif {$atom < 100} {
3391        set key "CRS$phase  AT $atom"
3392    } else {
3393        set key "CRS$phase  AT$atom"
3394    }
3395    # delete the records for the atom
3396    global exparray
3397    foreach k [array names exparray ${key}*] {
3398        delexp $k
3399    }
3400    # change the number of atoms in the phase
3401    phaseinfo $phase natoms set [expr {[phaseinfo $phase natoms] -1}]
3402
3403    # now adjust numbers in "EXPR ATYP" records and delete, if needed.
3404    set natypes [readexp " EXPR  NATYP"]
3405    if {$natypes == ""} return
3406    set j 0
3407    for {set i 1} {$i <= $natypes} {incr i} {
3408        incr j
3409        if {$j <10} {
3410            set key " EXPR ATYP $j"
3411        } else {
3412            set key " EXPR ATYP$j"
3413        }
3414        while {![existsexp $key]} {
3415            incr j
3416            if {$j > 99} {
3417                return
3418            } elseif {$j <10} {
3419                set key " EXPR ATYP $j"
3420            } else {
3421                set key " EXPR ATYP$j"
3422            }
3423        }
3424        set keytype [string trim [string range $exparray($key) 2 9]]
3425        if {$type == $keytype} {
3426            # found the type record
3427            set val [string trim [string range $exparray($key) 10 14]]
3428            incr val -1
3429            # if this is the last reference, remove the record,
3430            # otherwise, decrement the counter
3431            if {$val <= 0} {
3432                incr natypes -1 
3433                validint natypes 5
3434                setexp " EXPR  NATYP" $natypes 1 5
3435                delexp $key
3436            } else {
3437                validint val 5
3438                setexp $key $val 11 5
3439            }
3440            return
3441        }
3442    }
3443}
3444
3445# compute equivalent anisotropic temperature factor for Uequiv
3446proc CalcAniso {phase Uequiv} {
3447    foreach var {a b c alpha beta gamma} {
3448        set $var [phaseinfo $phase $var]
3449    }
3450
3451    set G(1,1) [expr {$a * $a}]
3452    set G(2,2) [expr {$b * $b}]
3453    set G(3,3) [expr {$c * $c}]
3454    set G(1,2) [expr {$a * $b * cos($gamma*0.017453292519943)}]
3455    set G(2,1) $G(1,2)
3456    set G(1,3) [expr {$a * $c * cos($beta *0.017453292519943)}]
3457    set G(3,1) $G(1,3)
3458    set G(2,3) [expr {$b * $c * cos($alpha*0.017453292519943)}]
3459    set G(3,2) $G(2,3)
3460
3461    # Calculate the volume**2
3462    set v2 0.0
3463    foreach i {1 2 3} {
3464        set J [expr {($i%3) + 1}]
3465        set K [expr {(($i+1)%3) + 1}]
3466        set v2 [expr {$v2+ $G(1,$i)*($G(2,$J)*$G(3,$K)-$G(3,$J)*$G(2,$K))}]
3467    }
3468    if {$v2 > 0} {
3469        set v [expr {sqrt($v2)}]
3470        foreach i {1 2 3} {
3471            set i1 [expr {($i%3) + 1}]
3472            set i2 [expr {(($i+1)%3) + 1}]
3473            foreach j {1 2 3} {
3474                set j1 [expr {($j%3) + 1}]
3475                set j2 [expr {(($j+1)%3) + 1}]
3476                set C($j,$i) [expr {(\
3477                        $G($i1,$j1) * $G($i2,$j2) - \
3478                        $G($i1,$j2)  * $G($i2,$j1)\
3479                        )/ $v}]
3480            }
3481        }
3482        set A(1,2) [expr {0.5 * ($C(1,2)+$C(2,1)) / sqrt( $C(1,1)* $C(2,2) )}]
3483        set A(1,3) [expr {0.5 * ($C(1,3)+$C(3,1)) / sqrt( $C(1,1)* $C(3,3) )}]
3484        set A(2,3) [expr {0.5 * ($C(2,3)+$C(3,2)) / sqrt( $C(2,2)* $C(3,3) )}]
3485        foreach i {1 1 2} j {2 3 3} {
3486            set A($i,$j) [expr {0.5 * ($C($i,$j) + $C($j,$i)) / \
3487                    sqrt( $C($i,$i)* $C($j,$j) )}]
3488            # clean up roundoff
3489            if {abs($A($i,$j)) < 1e-5} {set A($i,$j) 0.0}
3490        }
3491    } else {
3492        set A(1,2) 0.0
3493        set A(1,3) 0.0
3494        set A(2,3) 0.0
3495    }
3496    return "$Uequiv $Uequiv $Uequiv \
3497            [expr {$Uequiv * $A(1,2)}] \
3498            [expr {$Uequiv * $A(1,3)}] \
3499            [expr {$Uequiv * $A(2,3)}]"
3500}
3501
3502# read/edit soft (distance) restraint info
3503#  parm:
3504#    weight -- histogram weight (factr) *
3505#    restraintlist -- list of restraints *
3506#  action: get (default) or set
3507#  value: used only with set
3508#  * =>  read+write supported
3509proc SoftConst {parm "action get" "value {}"} {
3510    set HST {}
3511    # look for RSN record
3512    set n 0
3513    for {set i 0} {$i < $::expmap(nhst)} {incr i} {
3514        set ihist [expr {$i + 1}]
3515        if {[expr {$i % 12}] == 0} {
3516            incr n
3517            set line [readexp " EXPR  HTYP$n"]
3518            if {$line == ""} {
3519                set msg "No HTYP$n entry for Histogram $ihist. This is an invalid .EXP file"
3520                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
3521            }
3522            set j 0
3523        } else {
3524            incr j
3525        }
3526        if {[string range $line [expr 2+5*$j] [expr 5*($j+1)]] == "RSN "} {
3527            set HST $ihist
3528        }
3529    }
3530    if {$HST <=9} {
3531        set key "HST  $HST"
3532    } else {
3533        set key "HST $HST"
3534    }
3535    if {$HST == "" && $action == "set"} {
3536        # no RSN found need to add the soft constr. histogram
3537        # increment number of histograms
3538        set hst [string trim [string range [readexp { EXPR  NHST }] 0 4]]
3539        incr hst
3540        set HST $hst
3541        if ![validint hst 5] {return 0}
3542        setexp  { EXPR  NHST } $hst 1 5
3543        # add to EXPR HTYPx rec, creating if needed
3544        set n [expr { 1+ (($HST - 1) / 12) }]
3545        set key " EXPR  HTYP$n"
3546        if {[array names ::exparray $key] == ""} {
3547            makeexprec $key
3548        }
3549        setexp $key "RSN " [expr 3 + 5*(($HST-1) % 12)] 5
3550        # create other HST  xx recs
3551        if {$HST <=9} {
3552            set key "HST  $HST"
3553        } else {
3554            set key "HST $HST"
3555        }
3556        makeexprec "$key  HNAM"
3557        setexp "$key  HNAM" "Bond distance restraints" 3 24
3558        makeexprec "$key FACTR"
3559        makeexprec "$key NBNDS"
3560        mapexp
3561    } elseif {$HST == ""} {
3562        if $::expgui(debug) {puts "no restraints"}
3563        return "1"
3564    }
3565
3566    switch -glob ${parm}-$action {
3567        weight-get {
3568            return [string trim [string range [readexp "$key FACTR"] 0 14]]
3569        }
3570        weight-set {
3571            # update FACTR
3572            if ![validreal value 15 6] {return 0}
3573            setexp "$key FACTR" $value 1 15
3574        }
3575        restraintlist-get {
3576            set ncons [string trim [string range [readexp "$key NBNDS"] 0 4]]
3577            set conslist {}
3578            for {set i 1} {$i <= $ncons} {incr i} {
3579                set fi [string toupper [format %.4x $i]]
3580                set line [readexp "${key}BD$fi"]
3581                set const {}
3582                foreach len {3 5 5 3 3 3 3 3 6 6} {
3583                  set lenm1 [expr {$len - 1}]
3584                  lappend const [string trim [string range $line 0 $lenm1]]
3585                  set line [string range $line $len end]
3586                }
3587                lappend conslist $const
3588            }
3589            return $conslist
3590        }
3591        restraintlist-set {
3592            set num [llength $value]
3593            if ![validint num 5] {return 0}
3594            setexp "$key NBNDS" $num 1 5
3595            # delete all old records
3596            foreach i [array names ::exparray "${key}BD*"] {unset ::exparray($i)}
3597            set i 0
3598            foreach cons $value {
3599                incr i
3600                set fi [string toupper [format %.4x $i]]
3601                makeexprec "${key}BD$fi"
3602                set pos 1
3603                foreach num $cons len {3 5 5 3 3 3 3 3 -6 -6} {
3604                    if {$len > 0} {
3605                        validint num $len
3606                        setexp "${key}BD$fi" $num $pos $len
3607                    } else {
3608                        set len [expr abs($len)]
3609                        validreal num $len 3
3610                        setexp "${key}BD$fi" $num $pos $len
3611                    }
3612                    incr pos $len
3613                }
3614            }
3615        }
3616        default {
3617            set msg "Unsupported phaseinfo access: parm=$parm action=$action"
3618            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
3619        }
3620    return 1
3621    }
3622}
3623
3624# read/edit chemical restraint info
3625#  parm:
3626#    weight -- histogram weight (factr) *
3627#    restraintlist -- list of restraints *
3628#  action: get (default) or set
3629#  value: used only with set
3630#      value is a list of constraints
3631#      each constrain contains {sum esd cons1 cons2...}
3632#      each consN contains {phase atomnum multiplier}
3633#  * =>  read+write supported
3634# Examples:
3635#
3636#ChemConst restraintlist set { {10 1.1 {1 1 2} {2 2 3}} {0 1 {1 1 1} {1 2 -2}} }
3637#
3638#ChemConst restraintlist get
3639#{10.00000 1.10000 {1 1 2.00000} {2 2 3.00000}} {0.00000 1.00000 {1 1 1.00000} {1 2 -2.00000}}
3640# constraint one 2*(1:1) + 3*(2:2) = 10(1.1)
3641# constraint two 1*(1:1) - 2*(1:2) = 0(1)
3642#   where (1:2) is the total number of atoms (multiplicity*occupancy) for atom 2 in phase 1
3643
3644proc ChemConst {parm "action get" "value {}"} {
3645    set HST {}
3646    # look for CMP record
3647    set n 0
3648    for {set i 0} {$i < $::expmap(nhst)} {incr i} {
3649        set ihist [expr {$i + 1}]
3650        if {[expr {$i % 12}] == 0} {
3651            incr n
3652            set line [readexp " EXPR  HTYP$n"]
3653            if {$line == ""} {
3654                set msg "No HTYP$n entry for Histogram $ihist. This is an invalid .EXP file"
3655                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
3656            }
3657            set j 0
3658        } else {
3659            incr j
3660        }
3661        if {[string range $line [expr 2+5*$j] [expr 5*($j+1)]] == "CMP "} {
3662            set HST $ihist
3663        }
3664    }
3665    if {$HST <=9} {
3666        set key "HST  $HST"
3667    } else {
3668        set key "HST $HST"
3669    }
3670    if {$HST == "" && $action == "set"} {
3671        # no CMP found need to add the soft constr. histogram
3672        # increment number of histograms
3673        set hst [string trim [string range [readexp { EXPR  NHST }] 0 4]]
3674        incr hst
3675        set HST $hst
3676        if ![validint hst 5] {return 0}
3677        setexp  { EXPR  NHST } $hst 1 5
3678        # add to EXPR HTYPx rec, creating if needed
3679        set n [expr { 1+ (($HST - 1) / 12) }]
3680        set key " EXPR  HTYP$n"
3681        if {[array names ::exparray $key] == ""} {
3682            makeexprec $key
3683        }
3684        setexp $key "CMP " [expr 3 + 5*(($HST-1) % 12)] 5
3685        # create other HST  xx recs
3686        if {$HST <=9} {
3687            set key "HST  $HST"
3688        } else {
3689            set key "HST $HST"
3690        }
3691        makeexprec "$key  HNAM"
3692        setexp "$key  HNAM" "Chemical composition restraints" 3 31
3693        makeexprec "$key FACTR"
3694#       makeexprec "$key NBNDS"
3695        makeexprec "$key NCMPS"
3696        mapexp
3697    } elseif {$HST == ""} {
3698        if $::expgui(debug) {puts "no restraints"}
3699        return "1"
3700    }
3701
3702    switch -glob ${parm}-$action {
3703        weight-get {
3704            return [string trim [string range [readexp "$key FACTR"] 0 14]]
3705        }
3706        weight-set {
3707            # update FACTR
3708            if ![validreal value 15 6] {return 0}
3709            setexp "$key FACTR" $value 1 15
3710        }
3711        restraintlist-get {
3712            set ncons [string trim [string range [readexp "$key NCMPS"] 0 4]]
3713            set conslist {}
3714            for {set i 1} {$i <= $ncons} {incr i} {
3715                set const {}
3716                set line [readexp "${key} CM$i  "]
3717                # number of terms
3718                set nterm [string trim [string range $line 0 4]]
3719                if {$nterm == ""} {set nterm 0}
3720                # chemical sum and esd
3721                lappend const [string trim [string range $line 5 14]]
3722                lappend const [string trim [string range $line 15 24]]
3723                for {set j 1} {$j <= $nterm} {incr j} {
3724                    set n [expr {($j + 2)/3}]
3725                    set o1 [expr {20*(($j-1)%3)}]
3726                    set o2 [expr {19 + 20*(($j-1)%3)}]
3727                    validint n 2
3728                    if {$o1 == 0} {
3729                        set line [readexp "${key} CM${i}${n}"]
3730                    }
3731                    set frag [string range $line $o1 $o2]                   
3732                    lappend const [list \
3733                                       [string trim [string range $frag 0 4]] \
3734                                       [string trim [string range $frag 5 9]] \
3735                                       [string trim [string range $frag 10 19]] \
3736                                   ]
3737                }
3738                lappend conslist $const
3739            }
3740            return $conslist
3741        }
3742        restraintlist-set {
3743            set num [llength $value]
3744            if ![validint num 5] {return 0}
3745            setexp "$key NCMPS" $num 1 5
3746            # delete all old records
3747            foreach i [array names ::exparray "${key} CM*"] {
3748                unset ::exparray($i)
3749            }
3750            set i 0
3751            foreach cons $value {
3752                incr i
3753                set sum [lindex $cons 0]
3754                set esd [lindex $cons 1]
3755                set terms [lrange $cons 2 end]
3756                set nterms [llength $terms]
3757                validint nterms 5
3758                validreal sum 10 5
3759                validreal esd 10 5
3760                makeexprec "${key} CM$i  " 
3761                setexp "${key} CM$i  " "${nterms}${sum}${esd}" 1 25
3762                set j 0
3763                set str {}
3764                foreach term $terms {
3765                    incr j
3766                    set n [expr {($j + 2)/3}]
3767                    if {$n > 99} break
3768                    validint n 2
3769                    foreach {phase atom mult} $term {}
3770                    validint phase 5
3771                    validint atom 5
3772                    validreal mult 10 5
3773                    append str "${phase}${atom}${mult}"
3774                    if {[expr {$j%3}] == 0} {
3775                        #puts [readexp "${key} CM${i}${n}"]
3776                        makeexprec "${key} CM${i}${n}"
3777                        setexp "${key} CM${i}${n}" $str 1 60
3778                        set str {}
3779                    }
3780                }
3781                if {[string length $str] > 0} {
3782                    makeexprec "${key} CM${i}${n}"
3783                    setexp "${key} CM${i}${n}" $str 1 60
3784                }
3785            }
3786        }
3787        default {
3788            set msg "Unsupported phaseinfo access: parm=$parm action=$action"
3789            puts $msg
3790            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
3791        }
3792    return 1
3793    }
3794}
3795
3796#======================================================================
3797# conversion routines
3798#======================================================================
3799
3800# convert x values to d-space
3801proc tod {xlist hst} {
3802    global expmap
3803    if {[string range $expmap(htype_$hst) 2 2] == "T"} {
3804        return [toftod $xlist $hst]
3805    } elseif {[string range $expmap(htype_$hst) 2 2] == "C"} {
3806        return [tttod $xlist $hst]
3807    } elseif {[string range $expmap(htype_$hst) 2 2] == "E"} {
3808        return [engtod $xlist $hst]
3809    } else {
3810        return {}
3811    }
3812}
3813
3814# convert tof to d-space
3815proc toftod {toflist hst} {
3816    set difc [expr {[histinfo $hst difc]/1000.}]
3817    set difc2 [expr {$difc*$difc}]
3818    set difa [expr {[histinfo $hst difa]/1000.}]
3819    set zero [expr {[histinfo $hst zero]/1000.}]
3820    set ans {}
3821    foreach tof $toflist {
3822        if {$tof == 0.} {
3823            lappend ans 0.
3824        } elseif {$tof == 1000.} {
3825            lappend ans 1000.
3826        } else {
3827            set td [expr {$tof-$zero}]
3828            lappend ans [expr {$td*($difc2+$difa*$td)/ \
3829                    ($difc2*$difc+2.0*$difa*$td)}]
3830        }
3831    }
3832    return $ans
3833}
3834
3835# convert two-theta to d-space
3836proc tttod {twotheta hst} {
3837    set lamo2 [expr {0.5 * [histinfo $hst lam1]}]
3838    set zero [expr [histinfo $hst zero]/100.]
3839    set ans {}
3840    set cnv [expr {acos(0.)/180.}]
3841    foreach tt $twotheta {
3842        if {$tt == 0.} {
3843            lappend ans 99999.
3844        } elseif {$tt == 1000.} {
3845            lappend ans 0.
3846        } else {
3847            lappend ans [expr {$lamo2 / sin($cnv*($tt-$zero))}]
3848        }
3849    }
3850    return $ans
3851}
3852
3853# convert energy (edx-ray) to d-space
3854# (note that this ignores the zero correction)
3855proc engtod {eng hst} {
3856    set lam [histinfo $hst lam1]
3857    set zero [histinfo $hst zero]
3858    set ans {}
3859    set v [expr {12.398/(2.0*[sind[expr ($lam/2.0)]])}]
3860    foreach e $eng {
3861        if {$e == 0.} {
3862            lappend ans 1000.
3863        } elseif {$e == 1000.} {
3864            lappend ans 0.
3865        } else {
3866            lappend ans [expr {$v/$e}]
3867        }
3868    }
3869    return $ans
3870}
3871
3872# convert x values to Q
3873proc toQ {xlist hst} {
3874    global expmap
3875    if {[string range $expmap(htype_$hst) 2 2] == "T"} {
3876        return [toftoQ $xlist $hst]
3877    } elseif {[string range $expmap(htype_$hst) 2 2] == "C"} {
3878        return [tttoQ $xlist $hst]
3879    } elseif {[string range $expmap(htype_$hst) 2 2] == "E"} {
3880        return [engtoQ $xlist $hst]
3881    } else {
3882        return {}
3883    }
3884}
3885# convert tof to Q
3886proc toftoQ {toflist hst} {
3887    set difc [expr {[histinfo $hst difc]/1000.}]
3888    set difc2 [expr {$difc*$difc}]
3889    set difa [expr {[histinfo $hst difa]/1000.}]
3890    set zero [expr {[histinfo $hst zero]/1000.}]
3891    set 2pi [expr {4.*acos(0.)}]
3892    set ans {}
3893    foreach tof $toflist {
3894        if {$tof == 0.} {
3895            lappend ans 99999.
3896        } elseif {$tof == 1000.} {
3897            lappend ans 0.
3898        } else {
3899            set td [expr {$tof-$zero}]
3900            lappend ans [expr {$2pi * \
3901                    ($difc2*$difc+2.0*$difa*$td)/($td*($difc2+$difa*$td))}]
3902        }
3903    }
3904    return $ans
3905}
3906
3907# convert two-theta to Q
3908proc tttoQ {twotheta hst} {
3909    set lamo2 [expr {0.5 * [histinfo $hst lam1]}]
3910    set zero [expr [histinfo $hst zero]/100.]
3911    set ans {}
3912    set cnv [expr {acos(0.)/180.}]
3913    set 2pi [expr {4.*acos(0.)}]
3914    foreach tt $twotheta {
3915        if {$tt == 0.} {
3916            lappend ans 0.
3917        } elseif {$tt == 1000.} {
3918            lappend ans 1000.
3919        } else {
3920            lappend ans [expr {$2pi * sin($cnv*($tt-$zero)) / $lamo2}]
3921        }
3922    }
3923    return $ans
3924}
3925# convert energy (edx-ray) to Q
3926# (note that this ignores the zero correction)
3927proc engtoQ {eng hst} {
3928    set lam [histinfo $hst lam1]
3929    set zero [histinfo $hst zero]
3930    set ans {}
3931    set v [expr {12.398/(2.0*[sind[expr ($lam/2.0)]])}]
3932    set 2pi [expr {4.*acos(0.)}]
3933    foreach e $eng {
3934        if {$e == 0.} {
3935            lappend ans 0.
3936        } elseif {$e == 1000.} {
3937            lappend ans 1000.
3938        } else {
3939            lappend ans [expr {$2pi * $e / $v}]
3940        }
3941    }
3942    return $ans
3943}
3944proc sind {angle} {
3945    return [expr {sin($angle*acos(0.)/90.)}]
3946}
3947
3948# convert d-space values to 2theta, TOF or KeV
3949proc fromd {dlist hst} {
3950    global expmap
3951    if {[string range $expmap(htype_$hst) 2 2] == "T"} {
3952        set difc [expr {[histinfo $hst difc]/1000.}]
3953        set difa [expr {[histinfo $hst difa]/1000.}]
3954        set zero [expr {[histinfo $hst zero]/1000.}]
3955        set ans {}
3956        foreach d $dlist {
3957            if {$d == 0.} {
3958                lappend ans 0.
3959            } elseif {$d == 1000.} {
3960                lappend ans 1000.
3961            } else {
3962                lappend ans [expr {$difc*$d + $difa*$d*$d + $zero}]
3963            }
3964        }
3965        return $ans
3966    } elseif {[string range $expmap(htype_$hst) 2 2] == "C"} {
3967        set lamo2 [expr {0.5 * [histinfo $hst lam1]}]
3968        set zero [expr [histinfo $hst zero]/100.]
3969        set ans {}
3970        set cnv [expr {180./acos(0.)}]
3971        foreach d $dlist {
3972            if {$d == 99999.} {
3973                lappend ans 0
3974            } elseif {$d == 0.} {
3975                lappend ans 1000.
3976            } else {
3977                lappend ans [expr {$cnv*asin($lamo2/$d) + $zero}]
3978            }
3979        }
3980        return $ans
3981    } elseif {[string range $expmap(htype_$hst) 2 2] == "E"} {
3982        set lam [histinfo $hst lam1]
3983        set zero [histinfo $hst zero]
3984        set v [expr {12.398/(2.0*[sind[expr ($lam/2.0)]])}]
3985        set ans {}
3986        foreach d $dlist {
3987            if {$d == 1000.} {
3988                lappend ans 0
3989            } elseif {$d == 0.} {
3990                lappend ans 1000.
3991            } else {
3992                lappend ans [expr {$v/$d}]
3993            }
3994        }
3995        return $ans
3996    } else {
3997        return {}
3998    }
3999}
4000
4001# convert Q values to 2theta, TOF or KeV
4002proc fromQ {Qlist hst} {
4003    global expmap
4004    if {[string range $expmap(htype_$hst) 2 2] == "T"} {
4005        set difc [expr {[histinfo $hst difc]/1000.}]
4006        set difa [expr {[histinfo $hst difa]/1000.}]
4007        set zero [expr {[histinfo $hst zero]/1000.}]
4008        set ans {}
4009        foreach Q $Qlist {
4010            if {$Q == 0.} {
4011                lappend ans 1000.
4012            } elseif {$Q == 99999.} {
4013                lappend ans 1000.
4014            } else {
4015                set d [expr {4.*acos(0.)/$Q}]
4016                lappend ans [expr {$difc*$d + $difa*$d*$d + $zero}]
4017            }
4018        }
4019        return $ans
4020    } elseif {[string range $expmap(htype_$hst) 2 2] == "C"} {
4021        set lamo4pi [expr {[histinfo $hst lam1]/(8.*acos(0.))}]
4022        set zero [expr [histinfo $hst zero]/100.]
4023        set ans {}
4024        set cnv [expr {180./acos(0.)}]
4025        foreach Q $Qlist {
4026            if {$Q == 0.} {
4027                lappend ans 0
4028            } elseif {$Q == 1000.} {
4029                lappend ans 1000.
4030            } else {
4031                lappend ans [expr {$cnv*asin($Q*$lamo4pi) + $zero}]
4032            }
4033        }
4034        return $ans
4035    } elseif {[string range $expmap(htype_$hst) 2 2] == "E"} {
4036        set lam [histinfo $hst lam1]
4037        set zero [histinfo $hst zero]
4038        set v [expr {12.398/(2.0*[sind[expr ($lam/2.0)]])}]
4039        set ans {}
4040        set 2pi [expr {4.*acos(0.)}]
4041        foreach Q $Qlist {
4042            if {$Q == 1000.} {
4043                lappend ans 0
4044            } elseif {$Q == 0.} {
4045                lappend ans 1000.
4046            } else {
4047                lappend ans [expr {$Q * $v/$2pi}]
4048            }
4049        }
4050        return $ans
4051    } else {
4052        return {}
4053    }
4054}
4055#============================================================================
4056# rigid body EXP editing routines (to move into readexp.tcl)
4057# RigidBodyList -- returns a list of the defined rigid body types
4058# SetRigidBodyVar -- set variables and damping for rigid body type multipliers
4059# ReadRigidBody  -- # of times a body is mapped, scaling factors, var #s & coordinates
4060# RigidBodyMappingList - return a list instances where a RB is mapped in phase
4061# RigidBodyEnableTLS -- Enable or Disable TLS use for a rigid body mapping
4062# RigidBodySetTLS  -- change the TLS values for a rigid body mapping
4063# RigidBodySetDamp -- change the damping values for a rigid body mapping
4064# RigidBodyVary    -- set refinement variable numbers for a rigid body mapping
4065# RigidBodyTLSVary -- set TLS refinement variable nums for a rigid body mapping
4066# AddRigidBody -- defines a new rigid body type
4067# DeleteRigidBody -- remove a rigid body definition
4068# ReplaceRigidBody -- replaces a previous rigid body type
4069# ReadRigidBodyMapping  -- get parameters for a rigid body mapping
4070# MapRigidBody -- map a rigid body type into a phase
4071# EditRigidBodyMapping -- change the parameters in a rigid body mapping
4072# UnMapRigidBody --remove a rigid body constraint by removing a RB "instance"
4073#----- note that these older routines should not be used ------
4074# RigidBodyCount -- returns the number of defined rigid bodies (body types)
4075#    use RigidBodyList instead
4076# RigidBodyMappingCount -- # of times a rigid body is mapped in phase
4077#    use RigidBodyMappingList instead
4078#============================================================================
4079# returns the number of defined rigid bodies
4080proc RigidBodyCount {} {
4081    set n [string trim [readexp "RGBD  NRBDS"]]
4082    if {$n == ""} {
4083        set n 0
4084    }
4085    return $n
4086}
4087
4088# returns a list of the defined rigid body types
4089proc RigidBodyList {} {
4090    set n [string trim [readexp "RGBD  NRBDS"]]
4091    if {$n == ""} {
4092        set n 0
4093    }
4094    set rblist {}
4095    foreach rbnum {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15} {
4096        set value $rbnum
4097        validint value 2
4098        set key "RGBD${value}"
4099        if {[existsexp "$key NATR "]} {
4100            lappend rblist $rbnum
4101        }
4102        if {[llength $rblist] == $n} break
4103    }
4104    return $rblist
4105}
4106
4107# ReadRigidBody provides all information associated with a rigid body type
4108#  rbnum is the rigid body type number
4109# it returns two items:
4110#   the number of times the rigid body is mapped
4111#   a list containing an element for each scaling factor in rigid body #rbnum.
4112# in each element there are four items:
4113#    a multiplier value for the rigid body coordinates
4114#    a damping value (0-9) for the refinement of the multiplier
4115#    a variable number if the multiplier will be refined
4116#    a list of cartesian coordinates coordinates
4117# each cartesian coordinate contains 4 items: x,y,z and a label
4118#  note that the label is present only when the RB is created in EXPGUI and is
4119#  not used in GSAS.
4120proc ReadRigidBody {rbnum} {
4121    if {[lsearch [RigidBodyList] $rbnum] == -1} {
4122        return ""
4123    }
4124    set value $rbnum
4125    validint value 2
4126    set key "RGBD${value}"
4127    set n [string trim [string range [readexp "$key NATR"] 0 4]]
4128    set used [string trim [string range [readexp "$key NBDS"] 0 4]]
4129    set nmult [string trim [string range [readexp "$key NSMP"] 0 4]]
4130    set out {}
4131    for {set i 1} {$i <= $nmult} {incr i} {
4132        set line [readexp "${key}${i}PARM"]
4133        set mult [string trim [string range $line 0 9]]
4134        set var [string trim [string range $line 10 14]]
4135        set damp [string trim [string range $line 15 19]]
4136        set coordlist {}
4137        for {set j 1} {$j <= $n} {incr j} {
4138            set value $j
4139            validint value 3
4140            set line [readexp "${key}${i}SC$value"]
4141            set x [string trim [string range $line 0 9]]
4142            set y [string trim [string range $line 10 19]]
4143            set z [string trim [string range $line 20 29]]
4144            set lbl [string trim [string range $line 30 39]]
4145            lappend coordlist [list $x $y $z $lbl]
4146        }
4147        lappend out [list $mult $damp $var $coordlist]
4148    }
4149    return [list $used $out]
4150}
4151
4152# SetRigidBodyVar
4153#   rbnum is the rigid body type number
4154#   varnumlist is a list of variable numbers
4155#      note that if this list is shorter than the number of actual multipliers
4156#      for the body, the unspecified variable will not be changed
4157#   damplist   is a list of damping values (0-9)
4158#      note that if the damplist is shorter than the number of actual multipliers
4159#      the unspecified values are not changed
4160#  SetRigidBodVar 2 {1 2 3} {}
4161#       will vary the (first 3) translations in body #3 and will not change the
4162#       damping values
4163#  SetRigidBodVar 3 {} {0 0 0}
4164#       will not change variable settings but will change the (first 3) damping values
4165#  SetRigidBodVar 4 {11 11} {8 8}
4166#      changes both variable numbers and damping at the same time
4167# Nothing is returned
4168proc SetRigidBodyVar {rbnum varnumlist damplist} {
4169    if {[lsearch [RigidBodyList] $rbnum] == -1} {
4170        return ""
4171    }
4172    set value $rbnum
4173    validint value 2
4174    set key "RGBD${value}"
4175    set nmult [string trim [string range [readexp "$key NSMP"] 0 4]]
4176    for {set i 1} {$i <= $nmult} {incr i} {
4177        set j $i
4178        incr j -1
4179        set var [lindex $varnumlist $j]
4180        if {$var != ""} {
4181            validint var 5
4182            setexp "${key}${i}PARM" $var 11 15
4183        }
4184        set damp [lindex $damplist $j]
4185        if {$damp != ""} {
4186            if {$damp > 9} {set damp 9}
4187            if {$damp < 0} {set damp 0}
4188            validint damp 5
4189        }
4190        setexp "${key}${i}PARM" $damp 16 20
4191    }
4192}
4193
4194
4195# return the number of times rigid body $bodytyp is mapped in phase $phase
4196proc RigidBodyMappingCount {phase bodytyp} {
4197    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1]"
4198    if {! [existsexp "$key  NBDS"]} {return 0}
4199    set n [string trim [readexp "$key  NBDS"]]
4200    if {$n == ""} {
4201        set n 0
4202    }
4203    return $n
4204}
4205# return a list of the instances where rigid body $bodytyp is mapped in phase $phase
4206proc RigidBodyMappingList {phase bodytyp} {
4207    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1]"
4208    if {! [existsexp "$key  NBDS"]} {return {}}
4209    set n [string trim [readexp "$key  NBDS"]]
4210    if {$n == ""} {
4211        set n 0
4212    }
4213    set rblist {}
4214    foreach rbnum {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15} {
4215        set value $rbnum
4216        validint value 2
4217        set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $rbnum 1]"
4218        if {[existsexp "$key  NDA"]} {
4219            lappend rblist $rbnum
4220        }
4221        if {[llength $rblist] == $n} break
4222    }
4223    return $rblist
4224}
4225
4226
4227
4228# reads rigid body mapping parameters for phase ($phase), body type # ($bodytyp) and instance # ($num)
4229# returns a list of items (most lists) as follows:
4230#   1) sequence # of first atom in body
4231#   2) origin of body in fractional coordinates (3 elements)
4232#   3) Euler angles as 6 pairs of numbers (see below)
4233#   4) variable numbers for the 9 position variables (origin followed by rotations)
4234#   5) damping vals for the 9 position variables (origin followed by rotations)
4235#   6) the TLS values, in order below (empty list if TLS is not in use)
4236#   7) the variable numbers for each TLS values, in order below (or empty)
4237#   8) three damping values for the T, L and S terms.
4238# returns an empty list if no such body exists.
4239#
4240# Euler angles are a list of axes and angles to rotate:
4241#   { {axis1 angle1} {axis2 angle2} ...}
4242# where axis1,... can be 1, 2 or 3 corresponding to the cartesian X, Y or Z axes
4243#
4244# The 20 TLS terms are ordered:
4245#    T11, T22, T33, T12, T13, T23
4246#    L11, L22, L33, L12, L13, L23
4247#    S12, S13, S21, S23, S31, S32, SAA, SBB
4248#
4249proc ReadRigidBodyMapping {phase bodytyp num} {
4250    if {[lsearch [RigidBodyMappingList $phase $bodytyp] $num] == -1} {
4251        return ""
4252    }
4253    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $num 1]"
4254    set first [string trim [string range [readexp "$key  NDA"] 0 4]]
4255    set line [readexp "$key BDFL"]
4256    set varlist {}
4257    set damplist {}
4258    foreach i {0 1 2 3 4 5 6 7 8} {
4259        lappend varlist [string trim [string range $line [expr {5*$i}] [expr {4 + 5*$i}] ]]
4260        lappend damplist [string trim [string range $line [expr {45 + $i}] [expr {45 + $i}] ]]
4261    }
4262    set TLSdamplist {}
4263    foreach i {54 55 56} {
4264        lappend TLSdamplist [string trim [string range $line $i $i ]]
4265    }
4266    set line [readexp "${key} BDLC"]
4267    set x [string trim [string range $line 0 9]]
4268    set y [string trim [string range $line 10 19]]
4269    set z [string trim [string range $line 20 29]]
4270    set origin [list $x $y $z]
4271    set line [readexp "${key} BDOR"]
4272    set rotations {}
4273    foreach i {0 10 20 30 40 50} {
4274        set angle [string trim [string range $line $i [expr {$i+7}]]]
4275        set axis [string trim [string range $line [expr {$i+8}] [expr {$i+9}]]]
4276        lappend rotations [list $angle $axis]
4277    }
4278    set TLS [string trim [string range [readexp "${key} LSTF"] 0 4]]
4279    set tlsvars {}
4280    set tlsvals {}
4281    if {$TLS != 0} {
4282        set line [readexp "${key}TLSF1"]
4283        for {set j 0} {$j < 20} {incr j} {
4284            set var [string trim [string range $line [expr {3*$j}] [expr {3*$j+2}]]]
4285            if {$var == ""} {set var 0}
4286            lappend tlsvars $var
4287        }
4288        for {set j 0} {$j < 20} {incr j} {
4289            set i 0
4290            if {$j == 0} {
4291                set i 1
4292            } elseif {$j == 8} {
4293                set i 2
4294            } elseif {$j == 16} {
4295                set i 3
4296            }
4297            if {$i != 0} {
4298                set line [readexp "${key}TLSP$i"]
4299                set i 0
4300                set j1 0
4301                set j2 7
4302            } else {
4303                incr j1 8
4304                incr j2 8
4305            }
4306            set val [string trim [string range $line $j1 $j2]]
4307            if {$val == ""} {set val 0}
4308            lappend tlsvals $val
4309        }
4310    }
4311    return [list $first $origin $rotations $varlist $damplist $tlsvals $tlsvars $TLSdamplist]
4312}
4313
4314# Control TLS representation for phase, body # and instance number of a Rigid body mapping
4315#   for mapping with phase ($phase), body type # ($bodytyp) and instance # ($num)
4316# Enable TLS use if TLS is non-zero (true). Disable if zero
4317proc RigidBodyEnableTLS {phase bodytyp num TLS} {
4318    if {[lsearch [RigidBodyMappingList $phase $bodytyp] $num] == -1} {
4319        return ""
4320    }
4321    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $num 1]"
4322    if {$TLS} {
4323        setexp "${key} LSTF" [format "%5d" 1] 1 5
4324        if {![existsexp "${key}TLSF1"]} {makeexprec "${key}TLSF1"}
4325        if {![existsexp "${key}TLSP1"]} {
4326            makeexprec "${key}TLSP1"
4327            set str {}
4328            foreach v {.01 .01 .01 0 0 0 0 0} d {4 4 4 4 4 4 2 2} {
4329                validreal v 8 $d
4330                append str $v
4331            }
4332            setexp "${key}TLSP1" $str 1 64
4333        }
4334        if {![existsexp "${key}TLSP2"]} {
4335            makeexprec "${key}TLSP2"
4336            set str {}
4337            set v 0
4338            foreach d {2 2 2 2 4 4 4 4} {
4339                validreal v 8 $d
4340                append str $v
4341            }
4342            setexp "${key}TLSP2" $str 1 64
4343        }
4344        if {![existsexp "${key}TLSP3"]} {
4345            makeexprec "${key}TLSP3"
4346            set str {}
4347            set v 0
4348            foreach d {4 4 4 4} {
4349                validreal v 8 $d
4350                append str $v
4351            }
4352            setexp "${key}TLSP3" $str 1 64
4353        }
4354    } else {
4355        setexp "${key} LSTF" [format "%5d" 0] 1 5
4356    }
4357    return 1
4358}
4359
4360# Control the TLS values for Rigid body mapping for mapping with
4361#    phase ($phase), body type # ($bodytyp) and instance # ($num)
4362# set the 20 TLS values to the values in TLSvals
4363# There must be exactly 20 TLS terms, which are ordered:
4364#    T11, T22, T33, T12, T13, T23
4365#    L11, L22, L33, L12, L13, L23
4366#    S12, S13, S21, S23, S31, S32, SAA, SBB
4367proc RigidBodySetTLS {phase bodytyp num TLSvals} {
4368    if {[lsearch [RigidBodyMappingList $phase $bodytyp] $num] == -1} {
4369        return ""
4370    }
4371    if {[llength $TLSvals] != 20} {return ""}
4372    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $num 1]"
4373    set TLS [string trim [string range [readexp "${key} LSTF"] 0 4]]
4374    if {$TLS == 0} {return ""}
4375    if {![existsexp "${key}TLSF1"]} {makeexprec "${key}TLSF1"}
4376    foreach n {1 2 3} {
4377        if {![existsexp "${key}TLSP$n"]} {makeexprec "${key}TLSP$n"}
4378    }
4379    set str {}
4380    set n 1
4381    set i 0
4382    foreach v $TLSvals d {4 4 4 4 4 4 2 2 2 2 2 2 4 4 4 4 4 4 4 4} {
4383        incr i
4384        validreal v 8 $d
4385        append str $v
4386        if {$i == 8} {
4387            set i 0
4388            setexp "${key}TLSP$n" $str 1 64
4389            incr n
4390            set str {}
4391        }
4392    }
4393    setexp "${key}TLSP$n" $str 1 64
4394    return 1
4395}
4396
4397# set damping values for a Rigid body mapping
4398#   for mapping with phase ($phase), body type # ($bodytyp) and instance # ($num)
4399# there must be 9 damping values in RBdamp for the 9 position variables (origin followed by rotations)
4400# Use of TLSdamp is optional, but to be used, TLS representation must be enabled and there must be
4401# three damping terms (for all T terms; for all L terms and for all S terms)
4402proc RigidBodySetDamp {phase bodytyp num RBdamp "TLSdamp {}"} {
4403    if {[lsearch [RigidBodyMappingList $phase $bodytyp] $num] == -1} {
4404        return ""
4405    }
4406    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $num 1]"
4407    if {[llength $RBdamp] != 9} {return ""}
4408    set str {}
4409    foreach v $RBdamp {
4410        if {[validint v 1] != 1} {set v " "}
4411        append str $v
4412    }
4413    setexp "$key BDFL" $str 46 9
4414    set TLS [string trim [string range [readexp "${key} LSTF"] 0 4]]
4415    if {$TLS != 0 &&  [llength $TLSdamp] == 3} {
4416        set str {}
4417        foreach v $TLSdamp {
4418        if {[validint v 1] != 1} {set v " "}
4419            append str $v
4420        }
4421        setexp "$key BDFL" $str 55 3
4422    }
4423    return 1
4424}
4425
4426# set refinement variable numbers for a Rigid body mapping
4427#   for mapping with phase ($phase), body type # ($bodytyp) and instance # ($num)
4428# there must be 9 variable values in RBvar for the 9 position variables (origin followed by rotations)
4429# note that the variable values should be unique integers
4430proc RigidBodyVary {phase bodytyp num RBvar} {
4431    if {[lsearch [RigidBodyMappingList $phase $bodytyp] $num] == -1} {
4432        return ""
4433    }
4434    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $num 1]"
4435    if {[llength $RBvar] != 9} {return ""}
4436    set str {}
4437    foreach v $RBvar {
4438        if {[validint v 5] != 1} {set v " "}
4439        append str $v
4440    }
4441    setexp "$key BDFL" $str 1 45   
4442}
4443
4444# set TLS refinement variable numbers for a Rigid body mapping
4445#   for mapping with phase ($phase), body type # ($bodytyp) and instance # ($num)
4446# there must be 20 variable values in TLSvar for the 20 parameters:
4447#    T11, T22, T33, T12, T13, T23
4448#    L11, L22, L33, L12, L13, L23
4449#    S12, S13, S21, S23, S31, S32, SAA, SBB
4450# note that the variable values should be unique integers
4451proc RigidBodyTLSVary {phase bodytyp num TLSvar} {
4452    if {[lsearch [RigidBodyMappingList $phase $bodytyp] $num] == -1} {
4453        return ""
4454    }
4455    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $num 1]"
4456    if {[llength $TLSvar] != 20} {return ""}
4457    set TLS [string trim [string range [readexp "${key} LSTF"] 0 4]]
4458    if {$TLS == 0} {return ""}
4459    set str {}
4460    foreach v $TLSvar {
4461        if {[validint v 3] != 1} {set v " "}
4462        append str $v
4463    }
4464    setexp "${key}TLSF1" $str 1 60
4465
4466# AddRigidBody: add a new rigid body definition into the .EXP file
4467# arguments are:
4468#   multlist: defines a list of multipliers for each set of coordinates. In the
4469#             simplest case this will be {1}
4470#   coordlist: a nested list of coordinates such as { { {0 0 0} {.1 .1 .1} {.2 .2 .2} } }
4471# note that when the length of multlist > 1 then coordlist must have the same length.
4472# for input where
4473#     multlist = {s1 s2} and
4474#     coordlist = { { {0 0 0} {1 1 0} {.0 .0 .0} ...}
4475#                     {0 0 0} {1 1 0} {2 1 2} ...}
4476#                 }
4477# the cartesian coordinates are defined from the input as
4478#    atom 1 = s1 * (0,0,0) + s2*(0,0,0) [ = (0,0,0)]
4479#    atom 2 = s1 * (1,1,0) + s2*(1,1,0) [ = (s1+s2) * (1,1,0)]
4480#    atom 3 = s1 * (0,0,0) + s2*(2,1,2) [ = s2 * (2,1,2)]
4481#    ...
4482# Returns the number of the rigid body that has been created
4483proc AddRigidBody {multlist coordlist} {
4484    # find the first unused body #
4485    foreach rbnum {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} {
4486        set value $rbnum
4487        validint value 2
4488        set key "RGBD${value}"
4489        if {! [existsexp "$key NATR "]} {break}
4490    }
4491    # did we go too far?
4492    if {$rbnum == 16} {return ""}
4493    # increment the RB counter
4494    set n [string trim [readexp "RGBD  NRBDS"]]
4495    if {$n == ""} {
4496        makeexprec "RGBD  NRBDS"
4497        set n 0
4498    }
4499    incr n
4500    validint n 5
4501    setexp "RGBD  NRBDS" $n 1 5
4502    SetRigidBody $rbnum $multlist $coordlist
4503    return $rbnum
4504}
4505
4506# DeleteRigidBody: remove a rigid body definition from the .EXP file
4507# The body may not be mapped. I am not sure if GSAS allows more than 9 bodies,
4508# but if it does, the simplifed approach used here will fail, so this
4509# is not allowed.
4510# Input:
4511#   Rigid body number
4512# Returns:
4513#   1 on success
4514#   -1 if the body number is 11 or greater
4515#   -2 if the body is mapped
4516#   -3 if the body is not defined
4517proc DeleteRigidBody {rbnum} {
4518    # can't delete bodies with numbers higher than 10, since the key prefix
4519    # (RGBD11... will overlap with rigid body instance records, which would be
4520    # deleted below
4521    if {$rbnum > 10} {
4522        return -1
4523    }
4524    set value $rbnum
4525    validint value 2
4526    set key "RGBD${value}"
4527    if {![existsexp "$key NATR "]} {
4528        return -2
4529    }
4530    # make sure the body is not mapped
4531    if {[string trim [string range [readexp "$key NBDS"] 0 4]] != 0} {
4532        return -3
4533    }
4534    # delete the records starting with "RGBD x" or "RGBD10"
4535    foreach key [array names ::exparray "${key}*"] {
4536        #puts $key
4537        delexp $key
4538    }
4539    # decrement the RB counter
4540    set n [string trim [readexp "RGBD  NRBDS"]]
4541    if {$n == ""} {
4542        set n 0
4543    }
4544    incr n -1
4545    validint n 5
4546    if {$n > 0} {
4547        setexp "RGBD  NRBDS" $n 1 5
4548    } else {
4549        delexp "RGBD  NRBDS"
4550    }
4551    return 1
4552}
4553
4554# ReplaceRigidBody: replace all the information for rigid body #rbnum
4555# Works the sames as AddRigidBody (see above) except that the rigid body is replaced rather
4556# than added.
4557# Note that count of the # of times the body is used is preserved
4558proc ReplaceRigidBody {rbnum multlist coordlist {varlist ""} {damplist ""}} {
4559    set value $rbnum
4560    validint value 2
4561    set key "RGBD${value}"
4562    set line [readexp "$key NBDS"]
4563    foreach key [array names ::exparray "${key}*"] {
4564        #puts $key
4565        delexp $key
4566    }
4567    SetRigidBody $rbnum $multlist $coordlist $varlist $damplist
4568    setexp "$key NBDS" $line 1 68
4569}
4570
4571# Edit the parameters for rigid body #rbnum
4572# (normally called from ReplaceRigidBody or AddRigidBody)
4573proc SetRigidBody {rbnum multlist coordlist {varlist ""} {damplist ""}} {
4574    set value $rbnum
4575    validint value 2
4576    set key "RGBD${value}"
4577    # number of atoms
4578    set value [llength [lindex $coordlist 0]]
4579    validint value 5
4580    makeexprec "$key NATR"
4581    setexp "$key NATR" $value 1 5
4582    # number of times used
4583    set value 0
4584    validint value 5
4585    makeexprec "$key NBDS"
4586    setexp "$key NBDS" $value 1 5
4587    # number of coordinate matrices
4588    set value [llength $multlist]
4589    validint value 5
4590    makeexprec "$key NSMP"
4591    setexp "$key NSMP" $value 1 5
4592    set i 0
4593    foreach mult $multlist coords $coordlist {
4594        set var [lindex $varlist $i]
4595        if {$var == ""} {set var 0}
4596        set damp [lindex $damplist $i]
4597        if {$damp == ""} {set damp 0}
4598        incr i
4599        makeexprec "${key}${i}PARM"
4600        setexp "${key}${i}PARM" [format "%10.5f%5d%5d" $mult $var $damp] 1 20
4601        set j 0
4602        foreach item $coords {
4603            #puts $item
4604            incr j
4605            set value $j
4606            validint value 3
4607            makeexprec "${key}${i}SC$value"
4608            if {[llength $item] == 4} {
4609                setexp "${key}${i}SC$value" [eval format "%10.6f%10.6f%10.6f%10s" $item] 1 40
4610            } elseif {[llength $item] == 3} {
4611                setexp "${key}${i}SC$value" [eval format "%10.6f%10.6f%10.6f" $item] 1 30
4612            } else {
4613                return -code 3 "Invalid number of coordinates"
4614            }
4615        }
4616    }
4617}
4618
4619# convert a decimal to the GSAS hex encoding with a field $digits long.
4620proc ToHex {num digits} {
4621    return [string toupper [format "%${digits}x" $num]]
4622}
4623
4624# convert a GSAS hex encoding to a decimal integer
4625proc FromHex {hex} {
4626    return [scan $hex "%x"]
4627}
4628
4629# MapRigidBody: define an "instance" of a rigid body: meaning that the coordinates
4630# (and optionally U values) for a set of atoms will be generated from the rigid body
4631# arguments:
4632#   phase: phase number (1-9)
4633#   bodytyp: number of rigid body (1-15) as returned from AddRigidBody
4634#   firstatom: sequence number of the first atom in phase (note that atoms may
4635#              not be numbered sequentially)
4636#   position: list of three fractional coordinates for the origin of the rigid body coordinates
4637#   angles: list of 3 angles to rotate the rigid body coordinates around x, y, z of the
4638#           cartesian system before the body is translated to position.
4639# returns the instance # (number of times body $bodytyp has been used in phase $phase)
4640proc MapRigidBody {phase bodytyp firstatom position angles} {
4641    # find the first unused body # for this phase & type
4642    foreach rbnum {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} {
4643        set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $rbnum 1]"
4644        if {! [existsexp "$key  NDA"]} {break}
4645    }
4646    # did we go too far?
4647    if {$rbnum == 16} {return ""}
4648    # increment number of mapped bodies of this type overall
4649    set value $bodytyp
4650    validint value 2
4651    set key "RGBD${value}"
4652    set used [string trim [string range [readexp "$key NBDS"] 0 4]]
4653    incr used
4654    set value $used
4655    validint value 5
4656    setexp "$key NBDS" $value 1 5
4657    # increment number of mapped bodies of this type in this phase
4658    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1]"
4659    if {[existsexp "$key  NBDS"]} {
4660        set used [string trim [string range [readexp "$key  NBDS"] 0 4]]
4661    } else {
4662        makeexprec "$key  NBDS"
4663        set used 0
4664    }
4665    incr used
4666    set value $used
4667    validint value 5
4668    setexp "$key  NBDS" $value 1 5
4669    # now write the mapping parameters
4670    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $rbnum 1]"
4671    set value $firstatom
4672    validint value 5
4673    makeexprec "$key  NDA"
4674    setexp "$key  NDA" $value 1 5
4675    set l1 {}
4676    set l2 {}
4677    for {set i 0} {$i < 9} {incr i} {
4678        append l1 [format %5d 0]
4679        append l2 [format %1d 0]
4680    }
4681    makeexprec "$key BDFL"
4682    setexp "$key BDFL" $l1$l2 1 54
4683    makeexprec "${key} BDLC"
4684    setexp "${key} BDLC" [eval format "%10.6f%10.6f%10.6f" $position] 1 30
4685    makeexprec "${key} BDOR"
4686    set l1 {}
4687    foreach val "$angles 0 0 0" dir "1 2 3 1 1 1" {
4688        append l1 [format "%8.2f%2d" $val $dir]
4689    }
4690    setexp "${key} BDOR" $l1 1 60
4691    makeexprec "${key} LSTF"
4692    setexp "${key} LSTF" [format "%5d" 0] 1 5
4693    # turn off the X refinement flags for the new body
4694    set st [lsearch $::expmap(atomlist_$phase) $firstatom]
4695    set natoms [llength [lindex [lindex [lindex [ReadRigidBody $bodytyp] 1] 0] 3]]
4696    set en [expr {$st+$natoms-1}]
4697    set atomlist [lrange $::expmap(atomlist_$phase) $st $en]
4698    atominfo $phase $atomlist xref set 0
4699    # redo the mapping to capture the newly mapped atoms
4700    mapexp
4701    return $rbnum
4702}
4703
4704# EditRigidBodyMapping: edit parameters that define an "instance" of a rigid body (see MapRigidBody)
4705# arguments:
4706#   phase: phase number (1-9)
4707#   bodytyp: number of rigid body (1-15) as returned from AddRigidBody
4708#   bodynum: instance number, as returned by MapRigidBody
4709#   position: list of three fractional coordinates for the origin of the rigid body coordinates
4710#   angles: list of 3 angles to rotate the rigid body coordinates around x, y, z of the
4711#           cartesian system before the body is translated to position.
4712#
4713proc EditRigidBodyMapping {phase bodytyp bodynum position angles} {
4714    # number of bodies of this type in this phase
4715    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $bodynum 1]"
4716    setexp "${key} BDLC" [eval format "%10.6f%10.6f%10.6f" $position] 1 30
4717    set l1 {}
4718    foreach val "$angles 0 0 0" dir "1 2 3 1 1 1" {
4719        append l1 [format "%8.2f%2d" $val $dir]
4720    }
4721    setexp "${key} BDOR" $l1 1 60
4722}
4723
4724# UnMapRigidBody: remove a rigid body constraint by removing a RB "instance"
4725# (undoes MapRigidBody)
4726# arguments:
4727#   phase: phase number (1-9)
4728#   bodytyp: number of rigid body (1-15) as returned from AddRigidBody
4729#   bodynum: instance number, as returned by MapRigidBody
4730proc UnMapRigidBody {phase bodytyp mapnum} {
4731    if {[lsearch [RigidBodyMappingList $phase $bodytyp] $mapnum] == -1} {
4732        return ""
4733    }
4734    # decrement number of mapped bodies of this type overall
4735    set value $bodytyp
4736    validint value 2
4737    set key "RGBD${value}"
4738    set used [string trim [string range [readexp "$key NBDS"] 0 4]]
4739    incr used -1
4740    set value $used
4741    validint value 5
4742    setexp "$key NBDS" $value 1 5
4743    # decrement number of mapped bodies of this type in this phase
4744    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1]"
4745    if {[existsexp "$key  NBDS"]} {
4746        set used [string trim [string range [readexp "$key  NBDS"] 0 4]]
4747    } else {
4748        set used 0
4749    }
4750    incr used -1
4751    set value $used
4752    validint value 5
4753    if {$used > 0} {
4754        setexp "$key  NBDS" $value 1 5
4755    } else {
4756        delexp "$key  NBDS"
4757    }
4758    # now delete the mapping parameter records
4759    set key "RGBD[ToHex $phase 1][ToHex $bodytyp 1][ToHex $mapnum 1]"
4760    foreach key [array names ::exparray "${key}*"] {
4761        delexp $key
4762    }
4763    return $used
4764}
4765
4766# return a list of defined Fourier maps
4767proc listFourier {} {
4768    set l {}
4769    foreach i {1 2 3 4 5 6 7 8 9} {
4770        if {[existsexp "  FOUR CDAT$i"]} {
4771            lappend l $i
4772        }
4773    }
4774    return $l
4775}
4776
4777# read a Fourier map entry
4778# returns five values:
4779#   0: type of map (DELF,FCLC,FOBS,*FDF,PTSN,DPTS)
4780#   1: section (X,Y or Z)
4781#   2: phase (1-9)
4782#   3: DMIN (usually 0.0)
4783#   4: DMAX (usually 999.99)
4784proc readFourier {num} {
4785    set key "  FOUR CDAT$num"
4786    if {![existsexp $key]} {
4787        return {}
4788    }
4789    set vals {}
4790    # 0: type of map (DELF,FCLC,FOBS,[2-9]FDF,PTSN,DPTS)
4791    lappend vals [string trim [string range [readexp $key] 2 6]]
4792    # 1: section (X,Y or Z)
4793    lappend vals [string trim [string range [readexp $key] 7 8]]
4794    # 2: phase (1-9)
4795    lappend vals [string trim [string range [readexp $key] 8 13]]
4796    # 3: DMIN (usually 0.0)
4797    lappend vals [string trim [string range [readexp $key] 18 25]]
4798    # 4: DMAX (usually 999.99)
4799    lappend vals [string trim [string range [readexp $key] 30 37]]
4800    return $vals
4801}
4802
4803# add a new Fourier map computation type
4804#   arguments:
4805#      phase: (1-9)
4806#      type: type of map (DELF,FCLC,FOBS,*FDF,PTSN,DPTS) - default DELF
4807#      section: (X,Y or Z) - default Z
4808#   returns the number of the map that is added
4809proc addFourier {phase {type "DELF"} {section "Z"}} {
4810    set num {}
4811    foreach i {1 2 3 4 5 6 7 8 9} {
4812        set key "  FOUR CDAT$i"
4813        if {! [existsexp "  FOUR CDAT$i"]} {
4814            set num $i
4815            break
4816        }
4817    }
4818    if {$num == ""} {return {}}
4819    set key "  FOUR CDAT$num"
4820    makeexprec $key
4821    setexp $key $type 3 4
4822    setexp $key $section 8 1
4823    validint phase 5
4824    setexp $key $phase 9 5
4825    setexp $key "NOPR   0.00      999.99" 15 23
4826    return $num
4827}
4828
4829# delete all Fourier map computations
4830proc delFourier {} {
4831    foreach i {1 2 3 4 5 6 7 8 9} {
4832        set key "  FOUR CDAT$i"
4833        delexp $key
4834    }
4835}
4836
4837# read/set a Fourier computation value
4838# use: Fourierinfo num parm
4839#  or: Fourierinfo num parm set value
4840#
4841#  num is the Fourier entry
4842#  parm is one of the following
4843#     type    -- type of map (DELF,FCLC,FOBS,*FDF,PTSN,DPTS)
4844#     section -- last running map direction (X,Y or Z)
4845#     phase   -- phase (1-9)
4846#     dmin    -- d-space for highest order reflection to use (usually 0.0)
4847#     dmax    -- d-space for lowest order reflection to use (usually 999.99)
4848# all parameters may be read or set
4849proc Fourierinfo {num parm "action get" "value {}"} {
4850    set key "  FOUR CDAT$num"
4851    if {![existsexp $key]} {
4852        return {}
4853    }
4854    switch -glob ${parm}-$action {
4855        type-get {
4856            # type of map (DELF,FCLC,FOBS,*FDF,PTSN,DPTS)
4857            return [string trim [string range [readexp $key] 2 6]]
4858        }
4859        type-set {
4860            set found 0
4861            foreach val {DELF FCLC FOBS 2FDF 3FDF 4FDF 5FDF 6FDF 7FDF 8FDF 9FDF PTSN DPTS} {
4862                if {$val == $value} {
4863                    set found 1
4864                    break
4865                }
4866            }
4867            if $found {
4868                setexp $key $value 3 4
4869            }
4870        }
4871        section-get {
4872            # section (X,Y or Z)
4873            return [string range [readexp $key] 7 8]
4874        }
4875        section-set {
4876            set found 0
4877            foreach val {X Y Z} {
4878                if {$val == $value} {
4879                    set found 1
4880                    break
4881                }
4882            }
4883            if $found {
4884                setexp $key $value 8 1
4885            }
4886        }
4887        phase-get {
4888            # phase (1-9)
4889            return [string trim [string range [readexp $key] 8 13]]
4890        }
4891        phase-set {
4892            validint value 5
4893            setexp $key $value 9 5
4894        }
4895        dmin-get {
4896            # DMIN (usually 0.0)
4897            return [string trim [string range [readexp $key] 18 25]]
4898        }
4899        dmin-set {
4900            validreal value 7 2
4901            setexp $key $value 19 7
4902        }
4903        dmax-get {
4904            # DMAX (usually 999.99)
4905            return [string trim [string range [readexp $key] 30 37]]
4906        }
4907        dmax-set {
4908            validreal value 7 2
4909            setexp $key $value 31 7
4910        }
4911        default {
4912            set msg "Unsupported Fourierinfo access: parm=$parm action=$action"
4913            puts $msg
4914            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
4915        }
4916    }
4917}
4918
4919# set histograms used in Fourier computation
4920#  use:
4921#     FourierHists $phase
4922#     FourierHists $phase set {4 3 2 1}
4923# returns a list of histograms to be used to compute that phase's Fourier map
4924# or sets a list of histograms to be used to compute that phase's Fourier map
4925#
4926# Note that the histograms are loaded in the order specified with reflections in
4927# the last histogram overwriting those in earlier ones, where a reflection
4928# occurs in more than one place
4929proc FourierHists {phase "action get" "value {}"} {
4930    # note that in theory one can have more than one CRSm  FMHSTn record
4931    # if more than 22 histograms are used but we will ignore this
4932    set key "CRS$phase  FMHST1"
4933    if {![existsexp $key]} {
4934        makeexprec $key
4935    }
4936    if {$action == "get"} {
4937        return [string trim [readexp $key]]
4938    } else {
4939        set hlist {}
4940        foreach hist $value {
4941            validint hist 3
4942            append hlist $hist
4943        }
4944        setexp $key $hlist 0 67
4945    }
4946}
4947# get the Fourier map computation step and limits
4948# returns 4 lists:
4949#   {stepx stepy stepz} : step size in Angstroms
4950#   {xmin xmax} : min and max x in fractional coordinates
4951#   {ymin ymax} : min and max y in fractional coordinates
4952#   {zmin zmax} : min and max z in fractional coordinates
4953proc getFourierLimits {phase} {
4954    set key "CRS$phase  FMPCTL"
4955    if {![existsexp $key]} {
4956        setFourierLimits $phase
4957    }
4958    set i 0
4959    set line [readexp $key]
4960    foreach v {x y z} cell {a b c} {
4961        set cell_$v [phaseinfo $phase $cell]
4962    }
4963    foreach typ {step min max} {
4964        foreach v {x y z} {
4965            set val [string trim [string range $line $i [expr $i+5]]]
4966            if {$val == ""} {set val 0}
4967            set ${typ}_${v} $val
4968            incr i 5
4969        }           
4970    }
4971    set steps {}
4972    if {[catch {
4973        foreach v {x y z} {
4974            set range_$v {}
4975            lappend steps [expr {[set cell_$v] / [set step_$v]}]
4976            lappend range_$v [expr {[set min_$v] * 1. / [set step_$v] }]
4977            lappend range_$v [expr {[set max_$v] * 1. / [set step_$v] }]
4978        }
4979    }]} {
4980        return [list {.2 .2 .2} {0 1} {0 1} {0 1}]
4981    } else {
4982        return [list $steps $range_x $range_y $range_z]
4983    }
4984}
4985
4986# set the Fourier map computation step and limits
4987#   Asteps contains {stepx stepy stepz} : step size in Angstroms
4988#   range_x contains {xmin xmax} : min and max x in fractional coordinates
4989#   range_y contains {ymin ymax} : min and max y in fractional coordinates
4990#   range_z contains {zmin zmax} : min and max z in fractional coordinates
4991proc setFourierLimits {phase \
4992                           {Asteps {.2 .2 .2}} \
4993                           {range_x {0 1}} \
4994                           {range_y {0 1}} \
4995                           {range_z {0 1}} } {
4996    set key "CRS$phase  FMPCTL"
4997    if {![existsexp $key]} {
4998        makeexprec $key
4999    }
5000    set i 1
5001    # steps across map
5002    foreach v {x y z} cell {a b c} As $Asteps {
5003        set s [expr {1 + int([phaseinfo $phase $cell] / $As)}]
5004        set s [expr {$s + ($s % 2)}]
5005        set step_$v $s
5006        lappend steps [set step_$v]
5007        validint s 5
5008        setexp $key $s $i 5
5009        incr i 5
5010    }
5011    # x,y,z min in steps
5012    foreach v {x y z} {
5013        foreach {min max} [set range_$v] {}
5014        set s [expr {int($min * [set step_$v]-.5)}]
5015        validint s 5
5016        setexp $key $s $i 5
5017        incr i 5
5018    }
5019    # x,y,z max in steps
5020    foreach v {x y z} {
5021        foreach {min max} [set range_$v] {}
5022        set s [expr {int($max * [set step_$v]+.5)}]
5023        validint s 5
5024        setexp $key $s $i 5
5025        incr i 5
5026    }
5027}
Note: See TracBrowser for help on using the repository browser.