source: trunk/readexp.tcl @ 619

Last change on this file since 619 was 559, checked in by toby, 16 years ago

# on 2002/01/25 21:30:39, toby did:
more absorption correction terms

  • Property rcs:author set to toby
  • Property rcs:date set to 2002/01/25 21:30:39
  • Property rcs:lines set to +39 -3
  • Property rcs:rev set to 1.35
  • Property rcs:state set to Exp
  • Property svn:keywords set to Author Date Revision Id
File size: 69.5 KB
Line 
1# $Id: readexp.tcl 559 2009-12-04 23:08:12Z toby $
2# Routines to deal with the .EXP "data structure"
3set expmap(Revision) {$Revision: 559 $ $Date: 2009-12-04 23:08:12 +0000 (Fri, 04 Dec 2009) $}
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} {
12    global exparray tcl_platform
13    # $expfile is the path to the data file.
14
15    if [catch {set fil [open "$expfile" r]}] {
16        tk_dialog .expFileErrorMsg "File Open Error" \
17                "Unable to open file $expfile" error 0 "Exit" 
18        return -1
19    }
20    fconfigure $fil -translation lf
21    set len [gets $fil line]
22    if {[string length $line] != $len} {
23        tk_dialog .expConvErrorMsg "old tcl" \
24                "You are using an old version of Tcl/Tk and your .EXP file has binary characters; run convstod or upgrade" \
25                error 0 "Exit"
26        return -1
27    }
28    catch {
29        unset exparray
30    }
31    if {$len > 160} {
32        set fmt 0
33        # a UNIX-type file
34        set i1 0
35        set i2 79
36        while {$i2 < $len} {
37            set nline [string range $line $i1 $i2]
38            incr i1 80
39            incr i2 80
40            set key [string range $nline 0 11]
41            set exparray($key) [string range $nline 12 end]
42        }
43    } else {
44        set fmt 1
45        while {$len > 0} {
46            set key [string range $line 0 11]
47            set exparray($key) [string range $line 12 79]
48            if {$len != 81 || [string range $line end end] != "\r"} {set fmt 2}
49            set len [gets $fil line]
50        }
51    }
52    close $fil
53    return $fmt
54}
55
56proc createexp {expfile title} {
57    global exparray expmap
58    catch {unset exparray}
59    foreach key   {"     VERSION" "      DESCR" "ZZZZZZZZZZZZ" " EXPR NPHAS"} \
60            value {"   6"         ""            "  Last EXP file record" ""} {
61        # truncate long keys & pad short ones
62        set key [string range "$key        " 0 11]
63        set exparray($key) $value
64    }
65    expinfo title set $title
66    exphistory add " created readexp.tcl [lindex $expmap(Revision) 1] [clock format [clock seconds]]"
67    expwrite $expfile
68}
69
70# get information out from an EXP file
71#   creates the following entries in global array expmap
72#     expmap(phaselist)     gives a list of defined phases
73#     expmap(phasetype)     gives the phase type for each defined phase
74#                           =1 nuclear; 2 mag+nuc; 3 mag; 4 macro
75#     expmap(atomlist_$p)   gives a list of defined atoms in phase $p
76#     expmap(htype_$n)      gives the GSAS histogram type for histogram (all)
77#     expmap(powderlist)    gives a list of powder histograms in use
78#     expmap(phaselist_$n)  gives a list of phases used in histogram $n
79#     expmap(nhst)          the number of GSAS histograms
80#
81proc mapexp {} {
82    global expmap exparray
83    # clear out the old array
84    set expmap_Revision $expmap(Revision)
85    unset expmap
86    set expmap(Revision) $expmap_Revision
87    # get the defined phases
88    set line [readexp " EXPR NPHAS"]
89#    if {$line == ""} {
90#       set msg "No EXPR NPHAS entry. This is an invalid .EXP file"
91#       tk_dialog .badexp "Error in EXP" $msg error 0 Exit
92#       destroy .
93#    }
94    set expmap(phaselist) {}
95    set expmap(phasetype) {}
96    # loop over phases
97    foreach iph {1 2 3 4 5 6 7 8 9} {
98        set i5s [expr {($iph - 1)*5}]
99        set i5e [expr {$i5s + 4}]
100        set flag [string trim [string range $line $i5s $i5e]]
101        if {$flag == ""} {set flag 0}
102        if $flag {
103            lappend expmap(phaselist) $iph
104            lappend expmap(phasetype) $flag
105        }
106    }
107    # get the list of defined atoms for each phase
108    foreach iph $expmap(phaselist) {
109        set expmap(atomlist_$iph) {}
110        if {[lindex $expmap(phasetype) [expr {$iph - 1}]] != 4} {
111            foreach key [array names exparray "CRS$iph  AT*A"] {
112                regexp { AT *([0-9]+)A} $key a num
113                lappend expmap(atomlist_$iph) $num
114            }
115        } else {
116            foreach key [array names exparray "CRS$iph  AT*"] {
117                lappend expmap(atomlist_$iph) [scan [string range $key 8 11] %x]
118            }
119        }
120        # note that sometimes an .EXP file contains more atoms than are actually defined
121        # drop the extra ones
122        set expmap(atomlist_$iph) [lsort -integer $expmap(atomlist_$iph)]
123        set natom [phaseinfo $iph natoms]
124        if {$natom != [llength $expmap(atomlist_$iph)]} {
125            set expmap(atomlist_$iph) [lrange $expmap(atomlist_$iph) 0 [expr {$natom-1}]]
126        }
127    }
128    # now get the histogram types
129    set expmap(nhst) [string trim [readexp { EXPR  NHST }]]
130    set n 0
131    set expmap(powderlist) {}
132    for {set i 0} {$i < $expmap(nhst)} {incr i} {
133        set ihist [expr {$i + 1}]
134        if {[expr {$i % 12}] == 0} {
135            incr n
136            set line [readexp " EXPR  HTYP$n"]
137            if {$line == ""} {
138                set msg "No HTYP$n entry for Histogram $ihist. This is an invalid .EXP file"
139                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
140            }
141            set j 0
142        } else {
143            incr j
144        }
145        set expmap(htype_$ihist) [string range $line [expr 2+5*$j] [expr 5*($j+1)]]
146        # is this a dummy histogram?
147        if {$ihist <=9} {
148            set key "HST  $ihist DUMMY"
149        } else {
150            set key "HST $ihist DUMMY"
151        }
152        # at least for now, ignore non-powder histograms
153        if {[string range $expmap(htype_$ihist) 0 0] == "P" && \
154                [string range $expmap(htype_$ihist) 3 3] != "*"} {
155            if {[existsexp $key]} {
156                set expmap(htype_$ihist) \
157                        [string range $expmap(htype_$ihist) 0 2]D
158            }
159            lappend expmap(powderlist) $ihist
160        }
161    }
162
163    # now process powder histograms
164    foreach ihist $expmap(powderlist) {
165        # make a 2 digit key -- hh
166        if {$ihist < 10} {
167            set hh " $ihist"
168        } else {
169            set hh $ihist
170        }
171        set line [readexp "HST $hh NPHAS"]
172        if {$line == ""} {
173            set msg "No NPHAS entry for Histogram $ihist. This is an invalid .EXP file"
174            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
175        }
176        set expmap(phaselist_$ihist) {}
177        # loop over phases
178        foreach iph {1 2 3 4 5 6 7 8 9} {
179            set i5s [expr {($iph - 1)*5}]
180            set i5e [expr {$i5s + 4}]
181            set flag [string trim [string range $line $i5s $i5e]]
182            if {$flag == ""} {set flag 0}
183            if $flag {lappend expmap(phaselist_$ihist) $iph}
184        }
185    }
186}
187
188# return the value for a ISAM key
189proc readexp {key} {
190    global exparray
191    # truncate long keys & pad short ones
192    set key [string range "$key        " 0 11]
193    if [catch {set val $exparray($key)}] {
194        global expgui
195        if $expgui(debug) {puts "Error accessing record $key"}
196        return ""
197    }
198    return $val
199}
200
201# return the number of records matching ISAM key (may contain wildcards)
202proc existsexp {key} {
203    global exparray
204    # key can contain wild cards so don't pad
205    return [llength [array names exparray  $key]]
206}
207
208
209# replace a section of the exparray with $value
210#   replace $char characters starting at character $start (numbered from 1)
211proc setexp {key value start chars} {
212    global exparray
213    # truncate long keys & pad short ones
214    set key [string range "$key        " 0 11]
215    if [catch {set exparray($key)}] {
216        global expgui
217        if $expgui(debug) {puts "Error accessing record $key"}
218        return ""
219    }
220
221    # pad value to $chars
222    set l0 [expr {$chars - 1}]
223    set value [string range "$value                                           " 0 $l0]
224
225    if {$start == 1} {
226        set ret {}
227        set l1 $chars
228    } else {
229        set l0 [expr {$start - 2}]
230        set l1 [expr {$start + $chars - 1}]
231        set ret [string range $exparray($key) 0 $l0]
232    }
233    append ret $value [string range $exparray($key) $l1 end]
234    set exparray($key) $ret
235}
236
237proc makeexprec {key} {
238    global exparray
239    # truncate long keys & pad short ones
240    set key [string range "$key        " 0 11]
241    if [catch {set exparray($key)}] {
242        # set to 68 blanks
243        set exparray($key) [format %68s " "]
244    }
245}
246
247# delete an exp record
248# returns 1 if OK; 0 if not found
249proc delexp {key} {
250    global exparray
251    # truncate long keys & pad short ones
252    set key [string range "$key        " 0 11]
253    if [catch {unset exparray($key)}] {
254        return 0
255    }
256    return 1
257}
258# test an argument if it is a valid number; reform the number to fit
259proc validreal {val length decimal} {
260    upvar $val value
261    if [catch {expr {$value}}] {return 0}
262    if [catch {
263        # for small values, switch to exponential notation
264        set pow [expr .2 - $decimal]
265        if {abs($value) < pow(10,$pow) && $length > 6} {
266            set tmp [format "%${length}.${decimal}E" $value]
267        } else {
268            set tmp [format "%${length}.${decimal}f" $value]
269        }
270        # if the string will not fit, use scientific notation & drop
271        # digits, as needed
272        while {[string length $tmp] > $length && $decimal >= 0} {
273            set tmp [format "%${length}.${decimal}E" $value]
274            incr decimal -1
275        }
276        set value $tmp
277    }] {return 0}
278    return 1
279}
280
281# test an argument if it is a valid integer; reform the number into
282# an integer, if appropriate -- be sure to pass the name of the variable not the value
283proc validint {val length} {
284    upvar $val value
285    # FORTRAN type assumption: blank is 0
286    if {$value == ""} {set value 0}
287    if [catch {
288        set tmp [expr {round($value)}]
289        if {$tmp != $value} {return 0}
290        set value [format "%${length}d" $tmp]
291    }] {return 0}
292    return 1
293}
294
295# process history information
296#    action == last
297#       returns number and value of last record
298#    action == add
299#
300proc exphistory {action "value 0"} {
301    global exparray
302    if {$action == "last"} {
303        set key [lindex [lsort -decreasing [array names exparray *HSTRY*]] 0]
304        if {$key == ""} {return ""}
305        return [list [string trim [string range $key 9 end]] $exparray($key)]
306    } elseif {$action == "add"} {
307        set key [lindex [lsort -decreasing [array names exparray *HSTRY*]] 0]
308        if {$key == ""} {
309            set index 1
310        } else {
311            set index [string trim [string range $key 9 end]]
312            if {$index != "***"} {
313                if {$index < 999} {incr index}
314                set key [format "    HSTRY%3d" $index]
315                set exparray($key) $value
316            }
317        }
318        set key [format "    HSTRY%3d" $index]
319        set exparray($key) $value
320    }
321}
322# get overall info
323#   parm:
324#     print     -- GENLES print option (*)
325#     cycles    -- number of GENLES cycles (*)
326#     title     -- the overall title (*)
327#     convg     -- convergence criterion: -200 to 200 (*)
328#     marq      -- Marquardt damping factor: 1.0 to 9.99 (*)
329proc expinfo {parm "action get" "value {}"} {
330    switch ${parm}-$action {
331        title-get {
332            return [string trim [readexp "      DESCR"]]
333        }
334        title-set {
335            setexp "      DESCR" "  $value" 2 68
336        }
337        cycles-get {
338            return [string trim [cdatget MXCY]]
339        }
340        cycles-set {
341            if ![validint value 1] {return 0}
342            cdatset MXCY [format %4d $value]
343        }
344        cyclesrun-get {
345            set cycle -1
346            regexp {.*cycles run *([0-9]*) } [readexp "  GNLS  RUN"] x cycle
347            return $cycle
348        }
349        print-get {
350            set print [string trim [cdatget PRNT]]
351            if {$print != ""} {return $print}
352            return 0
353        }
354        print-set {
355            if ![validint value 1] {return 0}
356            cdatset PRNT [format %3d $value]
357        }
358        convg-get {
359            set cvg [string trim [cdatget CVRG]]
360            if {$cvg == ""} {return -200}
361            if [catch {expr {$cvg}}] {return -200}
362            return $cvg
363        }
364        convg-set {
365            if ![validint value 1] {return 0}
366            set value [expr {-200>$value?-200:$value}]
367            set value [expr {200<$value?200:$value}]
368            cdatset CVRG [format %4d $value]
369        }
370        marq-get {
371            set mar [string trim [cdatget MARQ]]
372            if {$mar == ""} {return 1.0}
373            if [catch {expr $mar}] {return 1.}
374            return $mar
375        }
376        marq-set {
377            if [catch {
378                set value [expr {1.0>$value?1.0:$value}]
379                set value [expr {9.99<$value?9.99:$value}]
380            }] {return 0}
381            if ![validreal value 4 2] {return 0}
382            cdatset MARQ $value
383        }
384        default {
385            set msg "Unsupported expinfo access: parm=$parm action=$action"
386            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
387        }
388    }
389    return 1
390}
391
392proc cdatget {key} {
393    foreach i {1 2 3 4 5 6 7 8 9} {
394        if {[existsexp "  GNLS CDAT$i"] == 0} break
395        set line [readexp "  GNLS CDAT$i"]
396        if {$line == {}} break
397        foreach i1 {2 10 18 26 34 42 50 58 66} \
398                i2 {9 17 25 33 41 49 57 65 73} {
399            set item [string range $line $i1 $i2]
400            if {[string trim $item] == {}} continue
401            if [regexp "${key}(.*)" $item a b] {return $b}
402        }
403    }
404    return {}
405}
406
407proc cdatset {key value} {
408    # round 1 see if we can find the string
409    foreach i {1 2 3 4 5 6 7 8 9} {
410        set line [readexp "  GNLS CDAT$i"]
411        if {$line == {}} break
412        foreach i1 {2 10 18 26 34 42 50 58 66} \
413                i2 {9 17 25 33 41 49 57 65 73} {
414            set item [string range $line $i1 $i2]
415            if {[string trim $item] == {}} continue
416            if [regexp "${key}(.*)" $item a b] {
417                # found it now replace it
418                incr i1
419                setexp "  GNLS CDAT$i" "${key}${value}" $i1 8
420                return
421            }
422        }
423    }
424    # not found, take the 1st blank space, creating a card if needed
425    foreach i {1 2 3 4 5 6 7 8 9} {
426        set line [readexp "  GNLS CDAT$i"]
427        if {$line == {}} {makeexprec "  GNLS CDAT$i"}
428        foreach i1 {2 10 18 26 34 42 50 58 66} \
429                i2 {9 17 25 33 41 49 57 65 73} {
430            set item [string range $line $i1 $i2]
431            if {[string trim $item] == {}} {
432                # found a blank space: now replace it
433                incr i1
434                setexp "  GNLS CDAT$i" "${key}${value}" $i1 8
435                return
436            }
437        }
438    }
439    return {}
440}
441
442# get phase information: phaseinfo phase parm action value
443#   phase: 1 to 9 (as defined)
444#   parm:
445#     name -- phase name
446#     natoms -- number of atoms (*)
447#     a b c alpha beta gamma -- cell parameters (*)
448#     cellref -- refinement flag for the unit cell(*)
449#     celldamp  -- damping for the unit cell refinement (*)
450#     spacegroup -- space group symbol (*)
451#     ODForder -- spherical harmonic order (*)
452#     ODFsym   -- sample symmetry (0-3) (*)
453#     ODFdampA -- damping for angles (*)
454#     ODFdampC -- damping for coefficients (*)
455#     ODFomega -- omega oriention angle (*)
456#     ODFchi -- chi oriention angle (*)
457#     ODFphi -- phi oriention angle (*)
458#     ODFomegaRef -- refinement flag for omega (*)
459#     ODFchiRef -- refinement flag for chi (*)
460#     ODFphiRef -- refinement flag for phi (*)
461#     ODFterms -- a list of the {l m n} values for each ODF term (*)
462#     ODFcoefXXX -- the ODF coefficient for for ODF term XXX (*)
463#     ODFRefcoef -- refinement flag for ODF terms (*)
464#  action: get (default) or set
465#  value: used only with set
466#  * =>  read+write supported
467proc phaseinfo {phase parm "action get" "value {}"} {
468    switch -glob ${parm}-$action {
469
470        name-get {
471            return [string trim [readexp "CRS$phase    PNAM"]]
472        }
473
474        name-set {
475            setexp "CRS$phase    PNAM" " $value" 2 68
476        }
477
478        spacegroup-get {
479            return [string trim [readexp "CRS$phase  SG SYM"]]
480        }
481
482        spacegroup-set {
483            setexp "CRS$phase  SG SYM" " $value" 2 68
484        }
485
486        natoms-get {
487            return [string trim [readexp "CRS$phase   NATOM"]]     
488        }
489
490        natoms-set {
491            if ![validint value 5] {return 0}
492            setexp "CRS$phase   NATOM" $value 1 5
493        }
494
495        a-get {
496           return [string trim [string range [readexp "CRS$phase  ABC"] 0 9]]
497        }
498        b-get {
499           return [string trim [string range [readexp "CRS$phase  ABC"] 10 19]]
500        }
501        c-get {
502           return [string trim [string range [readexp "CRS$phase  ABC"] 20 29]]
503        }
504        alpha-get {
505           return [string trim [string range [readexp "CRS$phase  ANGLES"] 0 9]]
506        }
507        beta-get {
508           return [string trim [string range [readexp "CRS$phase  ANGLES"] 10 19]]
509        }
510        gamma-get {
511           return [string trim [string range [readexp "CRS$phase  ANGLES"] 20 29]]
512        }
513
514        a-set {
515            if ![validreal value 10 6] {return 0}
516            setexp "CRS$phase  ABC" $value 1 10             
517        }
518        b-set {
519            if ![validreal value 10 6] {return 0}
520            setexp "CRS$phase  ABC" $value 11 10           
521        }
522        c-set {
523            if ![validreal value 10 6] {return 0}
524            setexp "CRS$phase  ABC" $value 21 10           
525        }
526        alpha-set {
527            if ![validreal value 10 4] {return 0}
528            setexp "CRS$phase  ANGLES" $value 1 10         
529        }
530        beta-set {
531            if ![validreal value 10 4] {return 0}
532            setexp "CRS$phase  ANGLES" $value 11 10         
533        }
534        gamma-set {
535            if ![validreal value 10 4] {return 0}
536            setexp "CRS$phase  ANGLES" $value 21 10         
537        }
538        cellref-get {
539            if {[string toupper [string range [readexp "CRS$phase  ABC"] 34 34]] == "Y"} {
540                return 1
541            }
542            return 0
543        }
544        cellref-set {
545            if $value {
546                setexp "CRS$phase  ABC" "Y" 35 1
547            } else {
548                setexp "CRS$phase  ABC" "N" 35 1
549            }       
550        }
551        celldamp-get {
552            set val [string range [readexp "CRS$phase  ABC"] 39 39]
553            if {$val == " "} {return 0}
554            return $val
555        }
556        celldamp-set {
557            setexp "CRS$phase  ABC" $value 40 1
558        }
559
560        ODForder-get {
561            set val [string trim [string range [readexp "CRS$phase  ODF"] 0 4]]
562            if {$val == " "} {return 0}
563            return $val
564        }
565        ODForder-set {
566            if ![validint value 5] {return 0}
567            setexp "CRS$phase  ODF" $value 1 5
568        }
569        ODFsym-get {
570            set val [string trim [string range [readexp "CRS$phase  ODF"] 10 14]]
571            if {$val == " "} {return 0}
572            return $val
573        }
574        ODFsym-set {
575            if ![validint value 5] {return 0}
576            setexp "CRS$phase  ODF" $value 11 5
577        }
578        ODFdampA-get {
579            set val [string range [readexp "CRS$phase  ODF"] 24 24]
580            if {$val == " "} {return 0}
581            return $val
582        }
583        ODFdampA-set {
584            setexp "CRS$phase  ODF" $value 25 1
585        }
586        ODFdampC-get {
587            set val [string range [readexp "CRS$phase  ODF"] 29 29]
588            if {$val == " "} {return 0}
589            return $val
590        }
591        ODFdampC-set {
592            setexp "CRS$phase  ODF" $value 30 1
593        }
594        ODFomegaRef-get {
595            if {[string toupper [string range [readexp "CRS$phase  ODF"] 16 16]] == "Y"} {
596                return 1
597            }
598            return 0
599        }
600        ODFomegaRef-set {
601            if $value {
602                setexp "CRS$phase  ODF" "Y" 17 1
603            } else {
604                setexp "CRS$phase  ODF" "N" 17 1
605            }       
606        }
607        ODFchiRef-get {
608            if {[string toupper [string range [readexp "CRS$phase  ODF"] 17 17]] == "Y"} {
609                return 1
610            }
611            return 0
612        }
613        ODFchiRef-set {
614            if $value {
615                setexp "CRS$phase  ODF" "Y" 18 1
616            } else {
617                setexp "CRS$phase  ODF" "N" 18 1
618            }       
619        }
620        ODFphiRef-get {
621            if {[string toupper [string range [readexp "CRS$phase  ODF"] 18 18]] == "Y"} {
622                return 1
623            }
624            return 0
625        }
626        ODFphiRef-set {
627            if $value {
628                setexp "CRS$phase  ODF" "Y" 19 1
629            } else {
630                setexp "CRS$phase  ODF" "N" 19 1
631            }       
632        }
633        ODFcoef*-get {
634            regsub ODFcoef $parm {} term
635            set k [expr {($term+5)/6}]
636            if {$k <= 9} {set k " $k"}
637            set j [expr {(($term-1) % 6)+1}]
638            set lineB [readexp "CRS$phase  ODF${k}B"]
639            set j0 [expr { ($j-1) *10}]
640            set j1 [expr {$j0 + 9}]
641            set val [string trim [string range $lineB $j0 $j1]]
642            if {$val == ""} {return 0.0}
643            return $val
644        }
645        ODFcoef*-set {
646            regsub ODFcoef $parm {} term
647            if ![validreal value 10 3] {return 0}
648            set k [expr {($term+5)/6}]
649            if {$k <= 9} {set k " $k"}
650            set j [expr {(($term-1) % 6)+1}]
651            set col [expr { ($j-1)*10 + 1}]
652            setexp "CRS$phase  ODF${k}B" $value $col 10
653        }
654        ODFRefcoef-get {
655            if {[string toupper [string range [readexp "CRS$phase  ODF"] 19 19]] == "Y"} {
656                return 1
657            }
658            return 0
659        }
660        ODFRefcoef-set {
661            if $value {
662                setexp "CRS$phase  ODF" "Y" 20 1
663            } else {
664                setexp "CRS$phase  ODF" "N" 20 1
665            }       
666        }
667        ODFomega-get {
668           return [string trim [string range [readexp "CRS$phase  ODF"] 30 39]]
669        }
670        ODFchi-get {
671           return [string trim [string range [readexp "CRS$phase  ODF"] 40 49]]
672        }
673        ODFphi-get {
674           return [string trim [string range [readexp "CRS$phase  ODF"] 50 59]]
675        }
676        ODFomega-set {
677            if ![validreal value 10 4] {return 0}
678            setexp "CRS$phase  ODF" $value 31 10
679        }
680        ODFchi-set {
681            if ![validreal value 10 4] {return 0}
682            setexp "CRS$phase  ODF" $value 41 10
683        }
684        ODFphi-set {
685            if ![validreal value 10 4] {return 0}
686            setexp "CRS$phase  ODF" $value 51 10
687        }
688
689        ODFterms-get {
690            set vallist {}
691            set val [string trim [string range [readexp "CRS$phase  ODF"] 5 9]]
692            for {set i 1} {$i <= $val} {incr i 6} {
693                set k [expr {1+($i-1)/6}]
694                if {$k <= 9} {set k " $k"}
695                set lineA [readexp "CRS$phase  ODF${k}A"]
696                set k 0
697                for {set j $i} {$j <= $val && $j < $i+6} {incr j} {
698                    set j0 [expr {($k)*10}]
699                    set j1 [expr {$j0 + 9}]
700                    lappend vallist [string trim [string range $lineA $j0 $j1]]
701                    incr k
702                }
703            }
704            return $vallist
705        }
706        ODFterms-set {
707            set key "CRS$phase  ODF   "
708            if {![existsexp $key]} {
709                makeexprec $key
710                set oldlen 0
711            } else {
712                set oldlen [string trim [string range [readexp $key] 5 9]]
713            }
714            set len [llength $value]
715            if ![validint len 5] {return 0}
716            setexp $key $len 6 5
717            set j 0
718            set k 0
719            foreach item $value {
720                incr j
721                if {$j % 6 == 1} {
722                    incr k
723                    if {$k <= 9} {set k " $k"}
724                    set col 1
725                    set keyA "CRS$phase  ODF${k}A"
726                    set keyB "CRS$phase  ODF${k}B"
727                    if {![existsexp $keyA]} {
728                        makeexprec $keyA
729                        makeexprec $keyB
730                    }
731                }
732                set col1 [expr {$col + 1}]
733                foreach n [lrange $item 0 2] {
734                    if ![validint n 3] {return 0}
735                    setexp $keyA $n $col1 3
736                    incr col1 3
737                }
738                incr col 10
739            }
740            for {incr j} {$j <= $oldlen} {incr j} {
741                if {$j % 6 == 1} {
742                    incr k
743                    if {$k <= 9} {set k " $k"}
744                    set col 1
745                    set keyA "CRS$phase  ODF${k}A"
746                    set keyB "CRS$phase  ODF${k}B"
747                    delexp $keyA
748                    delexp $keyB
749                }
750                if {[existsexp $keyA]} {
751                    setexp $keyA "          " $col 10
752                    setexp $keyB "          " $col 10
753                }
754                incr col 10
755            }
756        }
757
758        default {
759            set msg "Unsupported phaseinfo access: parm=$parm action=$action"
760            tk_dialog .badexp "Error in readexp" $msg error 0 Exit
761        }
762    }
763    return 1
764}
765
766
767# get atom information: atominfo phase atom parm action value
768#   phase: 1 to 9 (as defined)
769#   atom: a valid atom number [see expmap(atomlist_$phase)]
770#      Note that atom and phase can be paired lists, but if there are extra
771#      entries in the atoms list, the last phase will be repeated.
772#      so that atominfo 1 {1 2 3} xset 1
773#               will set the xflag for atoms 1-3 in phase 1
774#      but atominfo {1 2 3} {1 1 1} xset 1
775#               will set the xflag for atoms 1 in phase 1-3
776#   parm:
777#     type -- element code
778#     mult -- atom multiplicity
779#     label -- atom label (*)
780#     x y z -- coordinates (*)
781#     frac --  occupancy (*)
782#     temptype -- I or A for Isotropic/Anisotropic (*)
783#     Uiso  -- Isotropic temperature factor (*)
784#     U11  -- Anisotropic temperature factor (*)
785#     U22  -- Anisotropic temperature factor (*)
786#     U33  -- Anisotropic temperature factor (*)
787#     U12  -- Anisotropic temperature factor (*)
788#     U13  -- Anisotropic temperature factor (*)
789#     U23  -- Anisotropic temperature factor (*)
790#     xref/xdamp -- refinement flag/damping value for the coordinates (*)
791#     uref/udamp -- refinement flag/damping value for the temperature factor(s)  (*)
792#     fref/fdamp -- refinement flag/damping value for the occupancy (*)
793#  action: get (default) or set
794#  value: used only with set
795#  * =>  read+write supported
796proc atominfo {phaselist atomlist parm "action get" "value {}"} {
797    foreach phase $phaselist atom $atomlist {
798        if {$phase == ""} {set phase [lindex $phaselist end]}
799        if {$atom < 10} {
800            set key "CRS$phase  AT  $atom"
801        } elseif {$atom < 100} {
802            set key "CRS$phase  AT $atom"
803        } else {
804            set key "CRS$phase  AT$atom"
805        }
806        switch -glob ${parm}-$action {
807            type-get {
808                return [string trim [string range [readexp ${key}A] 2 9] ]
809            }
810            mult-get {
811                return [string trim [string range [readexp ${key}A] 58 61] ]
812            }
813            label-get {
814                return [string trim [string range [readexp ${key}A] 50 57] ]
815            }
816            label-set {
817                setexp ${key}A $value 51 8
818            }
819            temptype-get {
820                return [string trim [string range [readexp ${key}B] 62 62] ]
821            }
822            temptype-set {
823                if {$value == "A"} {
824                    setexp ${key}B A 63 1
825                    # copy the Uiso to the diagonal terms
826                    set Uiso [string range [readexp ${key}B] 0 9]
827                    foreach value [CalcAniso $phase $Uiso] \
828                            col {1 11 21 31 41 51} {
829                        validreal value 10 6
830                        setexp ${key}B $value $col 10
831                    }
832                } else {
833                    setexp ${key}B I 63 1
834                    set value 0.0
835                    catch {
836                        # get the trace
837                        set value [expr {( \
838                                [string range [readexp ${key}B] 0 9] + \
839                                [string range [readexp ${key}B] 10 19] + \
840                                [string range [readexp ${key}B] 20 29])/3.}]
841                    }
842                    validreal value 10 6
843                    setexp ${key}B $value 1 10
844                    # blank out the remaining terms
845                    set value " "
846                    setexp ${key}B $value 11 10
847                    setexp ${key}B $value 21 10
848                    setexp ${key}B $value 31 10
849                    setexp ${key}B $value 41 10
850                    setexp ${key}B $value 51 10
851                }
852            }
853            x-get {
854                return [string trim [string range [readexp ${key}A] 10 19] ]
855            }
856            x-set {
857                if ![validreal value 10 6] {return 0}
858                setexp ${key}A $value 11 10
859            }
860            y-get {
861                return [string trim [string range [readexp ${key}A] 20 29] ]
862            }
863            y-set {
864                if ![validreal value 10 6] {return 0}
865                setexp ${key}A $value 21 10
866            }
867            z-get {
868                return [string trim [string range [readexp ${key}A] 30 39] ]
869            }
870            z-set {
871                if ![validreal value 10 6] {return 0}
872                setexp ${key}A $value 31 10
873            }
874            frac-get {
875                return [string trim [string range [readexp ${key}A] 40 49] ]
876            }
877            frac-set {
878                if ![validreal value 10 6] {return 0}
879                setexp ${key}A $value 41 10
880            }
881            U*-get {
882                regsub U $parm {} type
883                if {$type == "iso" || $type == "11"} {
884                    return [string trim [string range [readexp ${key}B] 0 9] ]
885                } elseif {$type == "22"} {
886                    return [string trim [string range [readexp ${key}B] 10 19] ]
887                } elseif {$type == "33"} {
888                    return [string trim [string range [readexp ${key}B] 20 29] ]
889                } elseif {$type == "12"} {
890                    return [string trim [string range [readexp ${key}B] 30 39] ]
891                } elseif {$type == "13"} {
892                    return [string trim [string range [readexp ${key}B] 40 49] ]
893                } elseif {$type == "23"} {
894                    return [string trim [string range [readexp ${key}B] 50 59] ]
895                }
896            }
897            U*-set {
898                if ![validreal value 10 6] {return 0}
899                regsub U $parm {} type
900                if {$type == "iso" || $type == "11"} {
901                    setexp ${key}B $value 1 10
902                } elseif {$type == "22"} {
903                    setexp ${key}B $value 11 10
904                } elseif {$type == "33"} {
905                    setexp ${key}B $value 21 10
906                } elseif {$type == "12"} {
907                    setexp ${key}B $value 31 10
908                } elseif {$type == "13"} {
909                    setexp ${key}B $value 41 10
910                } elseif {$type == "23"} {
911                    setexp ${key}B $value 51 10
912                }
913            }
914            xref-get {
915                if {[string toupper [string range [readexp ${key}B] 64 64]] == "X"} {
916                    return 1
917                }
918                return 0
919            }
920            xref-set {
921                if $value {
922                    setexp ${key}B "X" 65 1
923                } else {
924                    setexp ${key}B " " 65 1
925                }           
926            }
927            xdamp-get {
928                set val [string range [readexp ${key}A] 64 64]
929                if {$val == " "} {return 0}
930                return $val
931            }
932            xdamp-set {
933                setexp ${key}A $value 65 1
934            }
935            fref-get {
936                if {[string toupper [string range [readexp ${key}B] 63 63]] == "F"} {
937                    return 1
938                }
939                return 0
940            }
941            fref-set {
942                if $value {
943                    setexp ${key}B "F" 64 1
944                } else {
945                    setexp ${key}B " " 64 1
946                }           
947            }
948            fdamp-get {
949                set val [string range [readexp ${key}A] 63 63]
950                if {$val == " "} {return 0}
951                return $val
952            }
953            fdamp-set {
954                setexp ${key}A $value 64 1
955            }
956
957            uref-get {
958                if {[string toupper [string range [readexp ${key}B] 65 65]] == "U"} {
959                    return 1
960                }
961                return 0
962            }
963            uref-set {
964                if $value {
965                    setexp ${key}B "U" 66 1
966                } else {
967                    setexp ${key}B " " 66 1
968                }           
969            }
970            udamp-get {
971                set val [string range [readexp ${key}A] 65 65]
972                if {$val == " "} {return 0}
973                return $val
974            }
975            udamp-set {
976                setexp ${key}A $value 66 1
977            }
978            default {
979                set msg "Unsupported atominfo access: parm=$parm action=$action"
980                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
981            }
982        }
983    }
984    return 1
985}
986
987# get macromolecular atom information: mmatominfo phase atom parm action value
988#   phase: 1 (at present only one mm phase can be defined)
989#   atom: a valid atom number [see expmap(atomlist_$phase)]
990#      Note that atoms can be lists
991#      so that mmatominfo 1 {1 2 3} xset 1
992#               will set the xflag for atoms 1-3 in phase 1
993#   parm:
994#     type -- element code
995#     frac --  occupancy (*)
996#     x y z -- coordinates (*)
997#     Uiso  -- Isotropic temperature factor (*)
998#     label -- atom label (*)
999#     residue -- residue label (*)
1000#     group -- group label (*)
1001#     resnum -- residue number (*)
1002#     xref/xdamp -- refinement flag/damping value for the coordinates (*)
1003#     uref/udamp -- refinement flag/damping value for the temperature factor(s)  (*)
1004#     fref/fdamp -- refinement flag/damping value for the occupancy (*)
1005#  action: get (default) or set
1006#  value: used only with set
1007#  * =>  read+write supported
1008proc mmatominfo {phaselist atomlist parm "action get" "value {}"} {
1009    foreach phase $phaselist atom $atomlist {
1010        if {$phase == ""} {set phase [lindex $phaselist end]}
1011        set num [string toupper [format %.4x $atom]]
1012        set key "CRS$phase  AT$num"
1013        switch -glob ${parm}-$action {
1014            type-get {
1015                return [string trim [string range [readexp ${key}] 2 9] ]
1016            }
1017            frac-get {
1018                return [string trim [string range [readexp ${key}] 10 15] ]
1019            }
1020            frac-set {
1021                if ![validreal value 6 4] {return 0}
1022                setexp ${key} $value 11 6
1023            }
1024            x-get {
1025                return [string trim [string range [readexp ${key}] 16 23] ]
1026            }
1027            x-set {
1028                if ![validreal value 8 5] {return 0}
1029                setexp ${key} $value 17 8
1030            }
1031            y-get {
1032                return [string trim [string range [readexp ${key}] 24 31] ]
1033            }
1034            y-set {
1035                if ![validreal value 8 5] {return 0}
1036                setexp ${key} $value 25 8
1037            }
1038            z-get {
1039                return [string trim [string range [readexp ${key}] 32 39] ]
1040            }
1041            z-set {
1042                if ![validreal value 8 5] {return 0}
1043                setexp ${key} $value 33 8
1044            }
1045            Uiso-get {
1046                return [string trim [string range [readexp ${key}] 40 45] ]
1047            }
1048            Uiso-set {
1049                if ![validreal value 6 4] {return 0}
1050                setexp ${key} $value 41 6
1051            }
1052            label-get {
1053                return [string trim [string range [readexp ${key}] 46 50] ]
1054            }
1055            label-set {
1056                setexp ${key} $value 47 5
1057            }
1058            residue-get {
1059                return [string range [readexp ${key}] 51 53]
1060            }
1061            residue-set {
1062                setexp ${key} $value 52 3
1063            }
1064            group-get {
1065                return [string range [readexp ${key}] 54 55]
1066            }
1067            group-set {
1068                setexp ${key} $value 55 2
1069            }
1070            resnum-get {
1071                return [string trim [string range [readexp ${key}] 56 59] ]
1072            }
1073            resnum-set {
1074                if ![validint value 4] {return 0}
1075                setexp "${key} EPHAS" $value 57 4
1076            }
1077            fref-get {
1078                if {[string toupper [string range [readexp $key] 60 60]] == "F"} {
1079                    return 1
1080                }
1081                return 0
1082            }
1083            fref-set {
1084                if $value {
1085                    setexp $key "F" 61 1
1086                } else {
1087                    setexp $key " " 61 1
1088                }           
1089            }
1090            xref-get {
1091                if {[string toupper [string range [readexp $key] 61 61]] == "X"} {
1092                    return 1
1093                }
1094                return 0
1095            }
1096            xref-set {
1097                if $value {
1098                    setexp $key "X" 62 1
1099                } else {
1100                    setexp ${key}B " " 62 1
1101                }           
1102            }
1103            uref-get {
1104                if {[string toupper [string range [readexp $key] 62 62]] == "U"} {
1105                    return 1
1106                }
1107                return 0
1108            }
1109            uref-set {
1110                if $value {
1111                    setexp $key "U" 63 1
1112                } else {
1113                    setexp $key " " 63 1
1114                }           
1115            }
1116
1117            fdamp-get {
1118                set val [string range [readexp ${key}] 63 63]
1119                if {$val == " "} {return 0}
1120                return $val
1121            }
1122            fdamp-set {
1123                setexp ${key} $value 64 1
1124            }
1125            xdamp-get {
1126                set val [string range [readexp ${key}] 64 64]
1127                if {$val == " "} {return 0}
1128                return $val
1129            }
1130            xdamp-set {
1131                setexp ${key} $value 65 1
1132            }
1133
1134            udamp-get {
1135                set val [string range [readexp ${key}] 65 65]
1136                if {$val == " "} {return 0}
1137                return $val
1138            }
1139            udamp-set {
1140                setexp ${key} $value 66 1
1141            }
1142            default {
1143                set msg "Unsupported mmatominfo access: parm=$parm action=$action"
1144                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
1145            }
1146        }
1147    }
1148    return 1
1149}
1150
1151
1152
1153# get histogram information: histinfo histlist parm action value
1154# histlist is a list of histogram numbers
1155# parm:
1156#     title
1157#     file  -- file name of raw data for histogram (*)
1158#     scale (*)
1159#     sref/sdamp -- refinement flag/damping value for the scale factor (*)
1160#     lam1, lam2 (*)
1161#     ttref refinement flag for the 2theta (ED Xray) (*)
1162#     wref refinement flag for the wavelength (*)
1163#     ratref refinement flag for the wavelength ratio (*)
1164#     difc, difa -- TOF calibration constants (*)
1165#     dcref,daref -- refinement flag for difc, difa (*)
1166#     zero (*)
1167#     zref refinement flag for the zero correction (*)
1168#     ipola (*)
1169#     pola (*)
1170#     pref refinement flag for the polarization (*)
1171#     kratio (*)
1172#     ddamp -- damping value for the diffractometer constants (*)
1173#     backtype -- background function number *
1174#     backterms -- number of background terms *
1175#     bref/bdamp -- refinement flag/damping value for the background (*)
1176#     bterm$n -- background term #n (*)
1177#     bank -- Bank number
1178#     tofangle -- detector angle (TOF only)
1179#     foextract  -- Fobs extraction flag (*)
1180#     LBdamp  -- LeBail damping value (*)
1181#     tmin/tmax -- minimum & maximum usable 2theta/TOF/energy
1182#     excl -- excluded regions (*)
1183#     dmin -- minimum d-space for reflection generation (*)
1184#     use  -- use flag; 1 = use; 0 = do not use (*)
1185#     dstart -- dummy histogram starting tmin/emin/2theta (*)
1186#     dstep -- dummy histogram step size tmin/emin/2theta (*)
1187#     dpoints -- dummy histogram number of points (*)
1188#     dtype   -- dummy histogram type (CONST or SLOG)
1189#     abscor1 -- 1st absorption correction (*)
1190#     abscor2 -- 2nd absorption correction (*)
1191#     abstype -- absorption correction type (*)
1192#     absdamp -- damping for absorption refinement (*)
1193#     absref -- refinement damping for absorption refinement (*)
1194#   parameters transferred from the instrument parameter file:
1195#     ITYP    -- returns the contents of the ITYP record
1196proc histinfo {histlist parm "action get" "value {}"} {
1197    global expgui
1198    foreach hist $histlist {
1199        if {$hist < 10} {
1200            set key "HST  $hist"
1201        } else {
1202            set key "HST $hist"
1203        }
1204        switch -glob ${parm}-$action {
1205            foextract-get {
1206                set line [readexp "${key} EPHAS"]
1207                # add a EPHAS if not exists
1208                if {$line == {}} {
1209                    makeexprec "${key} EPHAS"
1210                    # expedt defaults this to "F", but I think "T" is better
1211                    setexp "${key} EPHAS" "Y" 50 1
1212                    if $expgui(debug) {puts "Warning: creating a ${key} EPHAS record"}
1213                }
1214                if {[string toupper [string range $line 49 49]] == "T"} {
1215                    return 1
1216                }
1217                # the flag has changed to "Y/N" in the latest versions
1218                # of GSAS
1219                if {[string toupper [string range $line 49 49]] == "Y"} {
1220                    return 1
1221                }
1222                return 0
1223            }
1224            foextract-set {
1225                # the flag has changed to "Y/N" in the latest versions
1226                # of GSAS
1227                if $value {
1228                    setexp "${key} EPHAS" "Y" 50 1
1229                } else {
1230                    setexp "${key} EPHAS" "N" 50 1
1231                }
1232            }
1233            LBdamp-get {
1234                set v [string trim [string range [readexp "${key} EPHAS"] 54 54]]
1235                if {$v == ""} {return 0}
1236                return $v
1237            }
1238            LBdamp-set {
1239                if ![validint value 5] {return 0}
1240                setexp "${key} EPHAS" $value 51 5
1241            }
1242            title-get {
1243                return [string trim [readexp "${key}  HNAM"] ]
1244            }
1245            scale-get {
1246                return [string trim [string range [readexp ${key}HSCALE] 0 14]]
1247            }
1248            scale-set {
1249                if ![validreal value 15 6] {return 0}
1250                setexp ${key}HSCALE $value 1 15
1251            }
1252            sref-get {
1253                if {[string toupper [string range [readexp ${key}HSCALE] 19 19]] == "Y"} {
1254                    return 1
1255                }
1256                return 0
1257            }
1258            sref-set {
1259                if $value {
1260                    setexp ${key}HSCALE "Y" 20 1
1261                } else {
1262                    setexp ${key}HSCALE "N" 20 1
1263                }           
1264            }
1265            sdamp-get {
1266                set val [string range [readexp ${key}HSCALE] 24 24]
1267                if {$val == " "} {return 0}
1268                return $val
1269            }
1270            sdamp-set {
1271                setexp ${key}HSCALE $value 25 1
1272            }
1273
1274            difc-get -
1275            lam1-get {
1276                return [string trim [string range [readexp "${key} ICONS"] 0 9]]
1277            }
1278            difc-set -
1279            lam1-set {
1280                if ![validreal value 10 7] {return 0}
1281                setexp "${key} ICONS" $value 1 10
1282            }
1283            difa-get -
1284            lam2-get {
1285                return [string trim [string range [readexp "${key} ICONS"] 10 19]]
1286            }
1287            difa-set -
1288            lam2-set {
1289                if ![validreal value 10 7] {return 0}
1290                setexp "${key} ICONS" $value 11 10
1291            }
1292            zero-get {
1293                return [string trim [string range [readexp "${key} ICONS"] 20 29]]
1294            }
1295            zero-set {
1296                if ![validreal value 10 5] {return 0}
1297                setexp "${key} ICONS" $value 21 10
1298            }
1299            ipola-get {
1300                return [string trim [string range [readexp "${key} ICONS"] 54 54]]
1301            }
1302            ipola-set {
1303                if ![validint value 1] {return 0}
1304                setexp "${key} ICONS" $value 55 1
1305            }
1306            pola-get {
1307                return [string trim [string range [readexp "${key} ICONS"] 40 49]]
1308            }
1309            pola-set {
1310                if ![validreal value 10 5] {return 0}
1311                setexp "${key} ICONS" $value 41 10
1312            }
1313            kratio-get {
1314                return [string trim [string range [readexp "${key} ICONS"] 55 64]]
1315            }
1316            kratio-set {
1317                if ![validreal value 10 5] {return 0}
1318                setexp "${key} ICONS" $value 56 10
1319            }
1320
1321            wref-get {
1322            #------------------------------------------------------
1323            # col 33: refine flag for lambda, difc, ratio and theta
1324            #------------------------------------------------------
1325                if {[string toupper [string range \
1326                        [readexp "${key} ICONS"] 32 32]] == "L"} {
1327                    return 1
1328                }
1329                return 0
1330            }
1331            wref-set {
1332                if $value {
1333                    setexp "${key} ICONS" "L" 33 1
1334                } else {
1335                    setexp "${key} ICONS" " " 33 1
1336                }           
1337            }
1338            ratref-get {
1339                if {[string toupper [string range \
1340                        [readexp "${key} ICONS"] 32 32]] == "R"} {
1341                    return 1
1342                }
1343                return 0
1344            }
1345            ratref-set {
1346                if $value {
1347                    setexp "${key} ICONS" "R" 33 1
1348                } else {
1349                    setexp "${key} ICONS" " " 33 1
1350                }           
1351            }
1352            dcref-get {
1353                if {[string toupper [string range \
1354                        [readexp "${key} ICONS"] 32 32]] == "C"} {
1355                    return 1
1356                }
1357                return 0
1358            }
1359            dcref-set {
1360                if $value {
1361                    setexp "${key} ICONS" "C" 33 1
1362                } else {
1363                    setexp "${key} ICONS" " " 33 1
1364                }           
1365            }
1366            ttref-get {
1367                if {[string toupper [string range \
1368                        [readexp "${key} ICONS"] 32 32]] == "T"} {
1369                    return 1
1370                }
1371                return 0
1372            }
1373            ttref-set {
1374                if $value {
1375                    setexp "${key} ICONS" "T" 33 1
1376                } else {
1377                    setexp "${key} ICONS" " " 33 1
1378                }           
1379            }
1380
1381
1382            pref-get {
1383            #------------------------------------------------------
1384            # col 34: refine flag for POLA & DIFA
1385            #------------------------------------------------------
1386                if {[string toupper [string range \
1387                        [readexp "${key} ICONS"] 33 33]] == "P"} {
1388                    return 1
1389                }
1390                return 0
1391            }
1392            pref-set {
1393                if $value {
1394                    setexp "${key} ICONS" "P" 34 1
1395                } else {
1396                    setexp "${key} ICONS" " " 34 1
1397                }           
1398            }
1399            daref-get {
1400                if {[string toupper [string range \
1401                        [readexp "${key} ICONS"] 33 33]] == "A"} {
1402                    return 1
1403                }
1404                return 0
1405            }
1406            daref-set {
1407                if $value {
1408                    setexp "${key} ICONS" "A" 34 1
1409                } else {
1410                    setexp "${key} ICONS" " " 34 1
1411                }           
1412            }
1413
1414            zref-get {
1415            #------------------------------------------------------
1416            # col 34: refine flag for zero correction
1417            #------------------------------------------------------
1418                if {[string toupper [string range [readexp "${key} ICONS"] 34 34]] == "Z"} {
1419                    return 1
1420                }
1421                return 0
1422            }
1423            zref-set {
1424                if $value {
1425                    setexp "${key} ICONS" "Z" 35 1
1426                } else {
1427                    setexp "${key} ICONS" " " 35 1
1428                }           
1429            }
1430
1431            ddamp-get {
1432                set val [string range [readexp "${key} ICONS"] 39 39]
1433                if {$val == " "} {return 0}
1434                return $val
1435            }
1436            ddamp-set {
1437                setexp "${key} ICONS" $value 40 1
1438            }
1439
1440            backtype-get {
1441                set val [string trim [string range [readexp "${key}BAKGD "] 0 4]]
1442                if {$val == " "} {return 0}
1443                return $val
1444            }
1445            backtype-set {
1446                if ![validint value 5] {return 0}
1447                setexp "${key}BAKGD " $value 1 5
1448            }
1449            backterms-get {
1450                set val [string trim [string range [readexp "${key}BAKGD "] 5 9]]
1451                if {$val == " "} {return 0}
1452                return $val
1453            }
1454            backterms-set {
1455                # this takes a bit of work -- if terms are added, add lines as needed to the .EXP
1456                set oldval [string trim [string range [readexp "${key}BAKGD "] 5 9]]
1457                if ![validint value 5] {return 0}
1458                if {$oldval < $value} {
1459                    set line1  [expr {2 + ($oldval - 1) / 4}]
1460                    set line2  [expr {1 + ($value - 1) / 4}]
1461                    for {set i $line1} {$i <= $line2} {incr i} {
1462                        # create a blank entry if needed
1463                        makeexprec ${key}BAKGD$i
1464                    }
1465                    incr oldval
1466                    for {set num $oldval} {$num <= $value} {incr num} {
1467                        set f1 [expr {15*(($num - 1) % 4)}]
1468                        set f2 [expr {15*(1 + ($num - 1) % 4)-1}]
1469                        set line  [expr {1 + ($num - 1) / 4}]
1470                        if {[string trim [string range [readexp ${key}BAKGD$line] $f1 $f2]] == ""} {
1471                            set f1 [expr {15*(($num - 1) % 4)+1}]
1472                            setexp ${key}BAKGD$line 0.0 $f1 15                 
1473                        }
1474                    }
1475                }
1476                setexp "${key}BAKGD " $value 6 5
1477
1478            }
1479            bref-get {
1480                if {[string toupper [string range [readexp "${key}BAKGD"] 14 14]] == "Y"} {
1481                    return 1
1482                }
1483                return 0
1484            }
1485            bref-set {
1486                if $value {
1487                    setexp "${key}BAKGD "  "Y" 15 1
1488                } else {
1489                    setexp "${key}BAKGD "  "N" 15 1
1490                }
1491            }
1492            bdamp-get {
1493                set val [string range [readexp "${key}BAKGD "] 19 19]
1494                if {$val == " "} {return 0}
1495                return $val
1496            }
1497            bdamp-set {
1498                setexp "${key}BAKGD " $value 20 1
1499            }
1500            bterm*-get {
1501                regsub bterm $parm {} num
1502                set f1 [expr {15*(($num - 1) % 4)}]
1503                set f2 [expr {15*(1 + ($num - 1) % 4)-1}]
1504                set line  [expr {1 + ($num - 1) / 4}]
1505                return [string trim [string range [readexp ${key}BAKGD$line] $f1 $f2] ]
1506            }
1507            bterm*-set {
1508                regsub bterm $parm {} num
1509                if ![validreal value 15 6] {return 0}
1510                set f1 [expr {15*(($num - 1) % 4)+1}]
1511                set line  [expr {1 + ($num - 1) / 4}]
1512                setexp ${key}BAKGD$line $value $f1 15
1513            }
1514            bank-get {
1515                return [string trim [string range [readexp "${key} BANK"] 0 4]]
1516            }
1517            tofangle-get {
1518                return [string trim [string range [readexp "${key}BNKPAR"] 10 19]]
1519            }
1520            tmin-get {
1521                return [string trim [string range [readexp "${key} TRNGE"] 0 9]]
1522            }
1523            tmax-get {
1524                return [string trim [string range [readexp "${key} TRNGE"] 10 19]]
1525            }
1526            excl-get {
1527                set n [string trim [string range [readexp "${key} NEXC"] 0 4]]
1528                set exlist {}
1529                for {set i 1} {$i <= $n} {incr i} {
1530                    lappend exlist \
1531                            [string trim [readexp [format "${key}EXC%3d" $i]]]
1532                }
1533                return $exlist
1534            }
1535            excl-set {
1536                set n [llength $value]
1537                if ![validint n 5] {return 0}
1538                setexp "${key} NEXC" $n 1 5
1539                set i 0
1540                foreach p $value {
1541                    incr i
1542                    foreach {r1 r2} $p {}
1543                    validreal r1 10 3
1544                    validreal r2 10 3
1545                    set k [format "${key}EXC%3d" $i]
1546                    if {![existsexp $k]} {
1547                        makeexprec $k
1548                    }
1549                    setexp $k ${r1}${r2} 1 20
1550                }
1551            }
1552            file-get {
1553                return [string trim [readexp "${key}  HFIL"] ]
1554            }
1555            file-set {
1556                setexp "${key}  HFIL" $value 3 65
1557            }
1558            bank-get {
1559                return [string trim [string range [readexp "${key} BANK"] 0 4]]
1560            }
1561            dmin-get {
1562                return [string trim [string range [readexp "${key} NREF"] 5 14]]
1563            }
1564            dmin-set {
1565                if ![validreal value 10 4] {return 0}
1566                setexp "${key} NREF" $value 6 10
1567            }
1568            use-get {
1569                set k [expr {($hist+11)/12}]
1570                set line [readexp " EXPR  HTYP$k"]
1571                set j [expr {((($hist-1) % 12)+1)*5}]
1572                if {[string range $line $j $j] == "*"} {return 0}
1573                return 1
1574            }
1575            use-set {
1576                set k [expr {($hist+11)/12}]
1577                set line [readexp " EXPR  HTYP$k"]
1578                set j [expr {((($hist-1) % 12)+1)*5+1}]
1579                if {$value} {
1580                    setexp " EXPR  HTYP$k" " " $j 1
1581                } else {
1582                    setexp " EXPR  HTYP$k" "*" $j 1
1583                }
1584            }
1585            dstart-get {
1586                return [string trim [string range [readexp "${key} DUMMY"] 20 29]]
1587            }
1588            dstart-set {
1589                if ![validreal value 10 3] {return 0}
1590                setexp "${key} DUMMY" $value 21 10
1591            }
1592            dstep-get {
1593                return [string trim [string range [readexp "${key} DUMMY"] 30 39]]
1594            }
1595            dstep-set {
1596                if ![validreal value 10 3] {return 0}
1597                setexp "${key} DUMMY" $value 31 10
1598            }
1599            dpoints-get {
1600                return [string trim [string range [readexp "${key} DUMMY"] 0 9]]
1601            }
1602            dpoints-set {
1603                if ![validint value 10] {return 0}
1604                setexp "${key} DUMMY" $value 1 10
1605            }
1606            dtype-get {
1607                return [string trim [string range [readexp "${key} DUMMY"] 10 19]]
1608            }
1609            abscor1-get {
1610                return [string trim [string range [readexp "${key}ABSCOR"] 0 14]]
1611            }
1612            abscor1-set {
1613                if ![validreal value 15 8] {return 0}
1614                setexp "${key}ABSCOR" $value 1 15
1615            }
1616            abscor2-get {
1617                return [string trim [string range [readexp "${key}ABSCOR"] 15 29]]
1618            }
1619            abscor2-set {
1620                if ![validreal value 15 8] {return 0}
1621                setexp "${key}ABSCOR" $value 16 15
1622            }
1623            abstype-get {
1624                return [string trim [string range [readexp "${key}ABSCOR"] 40 44]]
1625            }
1626            abstype-set {
1627                if ![validint value 5] {return 0}
1628                setexp "${key}ABSCOR" $value 41 5
1629            }
1630            absdamp-get {
1631                set val [string range [readexp "${key}ABSCOR"] 39 39]
1632                if {$val == " "} {return 0}
1633                return $val
1634            }
1635            absdamp-set {
1636                setexp "${key}ABSCOR" $value 40 1
1637            }
1638            absref-get {
1639                if {[string toupper \
1640                        [string range [readexp "${key}ABSCOR"] 34 34]] == "Y"} {
1641                    return 1
1642                }
1643                return 0
1644            }
1645            absref-set {
1646                if $value {
1647                    setexp "${key}ABSCOR" "Y" 35 1
1648                } else {
1649                    setexp "${key}ABSCOR" "N" 35 1
1650                }
1651            }
1652            ITYP-get {
1653                return [string trim [readexp "${key}I ITYP"]]
1654            }
1655            default {
1656                set msg "Unsupported histinfo access: parm=$parm action=$action"
1657                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
1658            }
1659        }
1660    }
1661    return 1
1662}
1663
1664# read the information that differs by both histogram and phase (profile & phase fraction)
1665# use: hapinfo hist phase parm action value
1666
1667#     frac -- phase fraction (*)
1668#     frref/frdamp -- refinement flag/damping value for the phase fraction (*)
1669#     proftype -- profile function number (*)
1670#     profterms -- number of profile terms (*)
1671#     pdamp -- damping value for the profile (*)
1672#     pcut -- cutoff value for the profile (*)
1673#     pterm$n -- profile term #n (*)
1674#     pref$n -- refinement flag value for profile term #n (*)
1675#     extmeth -- Fobs extraction method (*)
1676#     POnaxis -- number of defined M-D preferred axes
1677proc hapinfo {histlist phaselist parm "action get" "value {}"} {
1678    foreach phase $phaselist hist $histlist {
1679        if {$phase == ""} {set phase [lindex $phaselist end]}
1680        if {$hist == ""} {set hist [lindex $histlist end]}
1681        if {$hist < 10} {
1682            set hist " $hist"
1683        }
1684        set key "HAP${phase}${hist}"
1685        switch -glob ${parm}-$action {
1686            extmeth-get {
1687                set i1 [expr {($phase - 1)*5}]
1688                set i2 [expr {$i1 + 4}]
1689                return [string trim [string range [readexp "HST $hist EPHAS"] $i1 $i2]]
1690            }
1691            extmeth-set {
1692                set i1 [expr {($phase - 1)*5 + 1}]
1693                if ![validint value 5] {return 0}
1694                setexp "HST $hist EPHAS" $value $i1 5
1695            }
1696            frac-get {
1697                return [string trim [string range [readexp ${key}PHSFR] 0 14]]
1698            }
1699            frac-set {
1700                if ![validreal value 15 6] {return 0}
1701                setexp ${key}PHSFR $value 1 15
1702            }
1703            frref-get {
1704                if {[string toupper [string range [readexp ${key}PHSFR] 19 19]] == "Y"} {
1705                    return 1
1706                }
1707                return 0
1708            }
1709            frref-set {
1710                if $value {
1711                    setexp ${key}PHSFR "Y" 20 1
1712                } else {
1713                    setexp ${key}PHSFR "N" 20 1
1714                }           
1715            }
1716            frdamp-get {
1717                set val [string range [readexp ${key}PHSFR] 24 24]
1718                if {$val == " "} {return 0}
1719                return $val
1720            }
1721            frdamp-set {
1722                setexp ${key}PHSFR $value 25 1
1723            }
1724            proftype-get {
1725                set val [string range [readexp "${key}PRCF "] 0 4]
1726                if {$val == " "} {return 0}
1727                return $val
1728            }
1729            proftype-set {
1730                if ![validint value 5] {return 0}
1731                setexp "${key}PRCF " $value 1 5
1732            }
1733            profterms-get {
1734                set val [string range [readexp "${key}PRCF "] 5 9]
1735                if {$val == " "} {return 0}
1736                return $val
1737            }
1738            profterms-set {
1739                if ![validint value 5] {return 0}
1740                setexp "${key}PRCF " $value 6 5
1741                # now check that all needed entries exist
1742                set lines [expr {1 + ($value - 1) / 4}]
1743                for {set i 1} {$i <= $lines} {incr i} {
1744                    makeexprec "${key}PRCF $i"
1745                }
1746            }
1747            pcut-get {
1748                return [string trim [string range [readexp "${key}PRCF "] 10 19]]
1749            }
1750            pcut-set {
1751                if ![validreal value 10 5] {return 0}
1752                setexp "${key}PRCF " $value 11 10
1753            }
1754            pdamp-get {
1755                set val [string range [readexp "${key}PRCF "] 24 24]
1756                if {$val == " "} {return 0}
1757                return $val
1758            }
1759            pdamp-set {
1760                setexp "${key}PRCF   " $value 25 1
1761            }
1762            pterm*-get {
1763                regsub pterm $parm {} num
1764                set f1 [expr {15*(($num - 1) % 4)}]
1765                set f2 [expr {15*(1 + ($num - 1) % 4)-1}]
1766                set line  [expr {1 + ($num - 1) / 4}]
1767                return [string trim [string range [readexp "${key}PRCF $line"] $f1 $f2] ]
1768            }
1769            pterm*-set {
1770                if ![validreal value 15 6] {return 0}
1771                regsub pterm $parm {} num
1772                set f1 [expr {1+ 15*(($num - 1) % 4)}]
1773                set line  [expr {1 + ($num - 1) / 4}]
1774                setexp "${key}PRCF $line" $value $f1 15
1775            }
1776            pref*-get {
1777                regsub pref $parm {} num
1778                set f [expr {24+$num}]
1779                if {[string toupper [string range [readexp "${key}PRCF  "] $f $f]] == "Y"} {
1780                    return 1
1781                }
1782                return 0
1783            }
1784            pref*-set {
1785                regsub pref $parm {} num
1786                set f [expr {25+$num}]
1787                if $value {
1788                    setexp ${key}PRCF "Y" $f 1
1789                } else {
1790                    setexp ${key}PRCF "N" $f 1
1791                }           
1792            }
1793            POnaxis-get {
1794                set val [string trim \
1795                        [string range [readexp "${key}NAXIS"] 0 4]]
1796                if {$val == ""} {return 0}
1797                return $val
1798            }
1799            POnaxis-set {
1800                if ![validint value 5] {return 0}
1801                # there should be a NAXIS record, but if not make one
1802                if {![existsexp "${key}NAXIS"]} {
1803                    makeexprec "${key}NAXIS"
1804                }
1805                setexp "${key}NAXIS  " $value 1 5
1806            }
1807            default {
1808                set msg "Unsupported hapinfo access: parm=$parm action=$action"
1809                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
1810            }
1811        }
1812    }
1813    return 1
1814}
1815
1816#  get a logical constraint
1817#
1818#  type action
1819#  -----------
1820#  atom get  number        returns a list of constraints.
1821#   "   set  number value  replaces a list of constraints
1822#                          (value is a list of constraints)
1823#   "   add  number value  inserts a new list of constraints
1824#                          (number is ignored)
1825#   "   delete number      deletes a set of constraint entries
1826# Each item in the list of constraints is composed of 4 items:
1827#              phase, atom, variable, multiplier
1828# If variable=UISO atom can be ALL, otherwise atom is a number
1829# legal variable names: FRAC, X, Y, Z, UISO, U11, U22, U33, U12, U23, U13,
1830#                       MX, MY, MZ
1831#
1832#  type action
1833#  -----------
1834#  profileXX get number         returns a list of constraints for term XX=1-36
1835#                               use number=0 to get # of defined
1836#                                  constraints for term XX
1837#   "        set number value   replaces a list of constraints
1838#                               (value is a list of constraints)
1839#   "        add number value   inserts a new list of constraints
1840#                               (number is ignored)
1841#   "        delete number      deletes a set of constraint entries
1842# Each item in the list of constraints is composed of 3 items:
1843#              phase-list, histogram-list, multiplier
1844# Note that phase-list and/or histogram-list can be ALL
1845
1846proc constrinfo {type action number "value {}"} {
1847    global expmap
1848    if {[lindex $expmap(phasetype) 0] == 4} {
1849        set mm 1
1850    } else {
1851        set mm 0
1852    }
1853    switch -glob ${type}-$action {
1854        atom-get {
1855            # does this constraint exist?
1856            set key [format "LNCN%4d%4d" $number 1]
1857            if {![existsexp $key]} {return -1}
1858            set clist {}
1859            for {set i 1} {$i < 999} {incr i} {
1860                set key [format "LNCN%4d%4d" $number $i]
1861                if {![existsexp $key]} break
1862                set line [readexp $key]
1863                set j1 2
1864                set j2 17
1865                set seg [string range $line $j1 $j2]
1866                while {[string trim $seg] != ""} {
1867                    set p [string range $seg 0 0]
1868                    if {$p == 1 && $mm} {
1869                        set atom [string trim [string range $seg 1 4]]
1870                        set var [string trim [string range $seg 5 7]]
1871                        if {$atom == "ALL"} {
1872                            set var UIS
1873                        } else {
1874                            scan $atom %x atom
1875                        }
1876                        lappend clist [list $p $atom $var \
1877                                [string trim [string range $seg 8 end]]]
1878                    } else {
1879                        lappend clist [list $p \
1880                                [string trim [string range $seg 1 3]] \
1881                                [string trim [string range $seg 4 7]] \
1882                                [string trim [string range $seg 8 end]]]
1883                    }
1884                    incr j1 16
1885                    incr j2 16
1886                    set seg [string range $line $j1 $j2]
1887                }
1888            }
1889            return $clist
1890        }
1891        atom-set {
1892            # delete records for current constraint
1893            for {set i 1} {$i < 999} {incr i} {
1894                set key [format "LNCN%4d%4d" $number $i]
1895                if {![existsexp $key]} break
1896                delexp $key
1897            }
1898            set line {}
1899            set i 1
1900            foreach tuple $value {
1901                set p [lindex $tuple 0]
1902                if {$p == 1 && $mm && \
1903                        [string toupper [lindex $tuple 1]] == "ALL"} {
1904                    set seg [format %1dALL UIS%8.4f \
1905                            [lindex $tuple 0] \
1906                            [lindex $tuple 3]]
1907                } elseif {$p == 1 && $mm} {
1908                    set seg [eval format %1d%.4X%-3s%8.4f $tuple]
1909                } elseif {[string toupper [lindex $tuple 1]] == "ALL"} {
1910                    set seg [format %1dALL%-4s%8.4f \
1911                            [lindex $tuple 0] \
1912                            [lindex $tuple 2] \
1913                            [lindex $tuple 3]]
1914                } else {
1915                    set seg [eval format %1d%3d%-4s%8.4f $tuple]
1916                }
1917                append line $seg
1918                if {[string length $line] > 50} {
1919                    set key  [format "LNCN%4d%4d" $number $i]
1920                    makeexprec $key
1921                    setexp $key $line 3 68
1922                    set line {}
1923                    incr i
1924                }
1925            }
1926            if {$line != ""} {
1927                set key  [format "LNCN%4d%4d" $number $i]
1928                makeexprec $key
1929                setexp $key $line 3 68
1930            }
1931            return
1932        }
1933        atom-add {
1934            # loop over defined constraints
1935            for {set j 1} {$j < 9999} {incr j} {
1936                set key [format "LNCN%4d%4d" $j 1]
1937                if {![existsexp $key]} break
1938            }
1939            set number $j
1940            # save the constraint
1941            set line {}
1942            set i 1
1943            foreach tuple $value {
1944                set p [lindex $tuple 0]
1945                if {$p == 1 && $mm && \
1946                        [string toupper [lindex $tuple 1]] == "ALL"} {
1947                    set seg [format %1dALL UIS%8.4f \
1948                            [lindex $tuple 0] \
1949                            [lindex $tuple 3]]
1950                } elseif {$p == 1 && $mm} {
1951                    set seg [eval format %1d%.4X%-3s%8.4f $tuple]
1952                } elseif {[string toupper [lindex $tuple 1]] == "ALL"} {
1953                    set seg [format %1dALL%-4s%8.4f \
1954                            [lindex $tuple 0] \
1955                            [lindex $tuple 2] \
1956                            [lindex $tuple 3]]
1957                } else {
1958                    set seg [eval format %1d%3d%-4s%8.4f $tuple]
1959                }
1960                append line $seg
1961                if {[string length $line] > 50} {
1962                    set key  [format "LNCN%4d%4d" $number $i]
1963                    makeexprec $key
1964                    setexp $key $line 3 68
1965                    set line {}
1966                    incr i
1967                }
1968            }
1969            if {$line != ""} {
1970                set key  [format "LNCN%4d%4d" $number $i]
1971                makeexprec $key
1972                setexp $key $line 3 68
1973            }
1974            return
1975        }
1976        atom-delete {
1977            for {set j $number} {$j < 9999} {incr j} {
1978                # delete records for current constraint
1979                for {set i 1} {$i < 999} {incr i} {
1980                    set key [format "LNCN%4d%4d" $j $i]
1981                    if {![existsexp $key]} break
1982                    delexp $key
1983                }
1984                # now copy records, from the next entry, if any
1985                set j1 $j
1986                incr j1
1987                set key1 [format "LNCN%4d%4d" $j1 1]
1988                # if there is no record, there is nothing to copy -- done
1989                if {![existsexp $key1]} return
1990                for {set i 1} {$i < 999} {incr i} {
1991                    set key1 [format "LNCN%4d%4d" $j1 $i]
1992                    if {![existsexp $key1]} break
1993                    set key  [format "LNCN%4d%4d" $j  $i]
1994                    makeexprec $key
1995                    setexp $key [readexp $key1] 1 68
1996                }
1997            }
1998        }
1999        profile*-delete {
2000            regsub profile $type {} term
2001            if {$term < 10} {
2002                set term " $term"
2003            }
2004            set key "LEQV PF$term   "
2005            # return nothing if no term exists
2006            if {![existsexp $key]} {return 0}
2007
2008            # number of constraint terms
2009            set nterms [string trim [string range [readexp ${key}] 0 4] ]
2010            # don't delete a non-existing entry
2011            if {$number > $nterms} {return 0}
2012            set val [expr {$nterms - 1}]
2013            validint val 5
2014            setexp $key $val 1 5
2015            for {set i1 $number} {$i1 < $nterms} {incr i1} {
2016                set i2 [expr {1 + $i1}]
2017                # move the contents of constraint #i2 -> i1
2018                if {$i1 > 9} {
2019                    set k1 [expr {($i1+1)/10}]
2020                    set l1 $i1
2021                } else {
2022                    set k1 " "
2023                    set l1 " $i1"
2024                }
2025                set key1 "LEQV PF$term  $k1"
2026                # number of constraint lines for #i1
2027                set n1 [string trim [string range [readexp ${key1}] \
2028                        [expr {($i1%10)*5}] [expr {4+(($i1%10)*5)}]] ]
2029                if {$i2 > 9} {
2030                    set k2 [expr {($i2+1)/10}]
2031                    set l2 $i2
2032                } else {
2033                    set k2 " "
2034                    set l2 " $i2"
2035                }
2036                set key2 "LEQV PF$term  $k2"
2037                # number of constraint lines for #i2
2038                set n2 [string trim [string range [readexp ${key2}] \
2039                        [expr {($i2%10)*5}] [expr {4+(($i2%10)*5)}]] ]
2040                set val $n2
2041                validint val 5
2042                # move the # of terms
2043                setexp $key1 $val [expr {1+(($i1%10)*5)}] 5
2044                # move the terms
2045                for {set j 1} {$j <= $n2} {incr j 1} {
2046                    set key "LEQV PF${term}${l1}$j"
2047                    makeexprec $key
2048                    setexp $key [readexp "LEQV PF${term}${l2}$j"] 1 68
2049                }
2050                # delete any remaining lines
2051                for {set j [expr {$n2+1}]} {$j <= $n1} {incr j 1} {
2052                    delexp "LEQV PF${term}${l1}$j"
2053                }
2054            }
2055
2056            # clear the last term
2057            if {$nterms > 9} {
2058                set i [expr {($nterms+1)/10}]
2059            } else {
2060                set i " "
2061            }
2062            set key "LEQV PF$term  $i"
2063            set cb [expr {($nterms%10)*5}]
2064            set ce [expr {4+(($nterms%10)*5)}]
2065            set n2 [string trim [string range [readexp ${key}] $cb $ce] ]
2066            incr cb
2067            setexp $key "     " $cb 5
2068            # delete any remaining lines
2069            for {set j 1} {$j <= $n2} {incr j 1} {
2070                delexp "LEQV PF${term}${nterms}$j"
2071            }
2072        }
2073        profile*-set {
2074            regsub profile $type {} term
2075            if {$term < 10} {
2076                set term " $term"
2077            }
2078            set key "LEQV PF$term   "
2079            # get number of constraint terms
2080            set nterms [string trim [string range [readexp ${key}] 0 4] ]
2081            # don't change a non-existing entry
2082            if {$number > $nterms} {return 0}
2083            if {$number > 9} {
2084                set k1 [expr {($number+1)/10}]
2085                set l1 $number
2086            } else {
2087                set k1 " "
2088                set l1 " $number"
2089            }
2090            set key1 "LEQV PF$term  $k1"
2091            # old number of constraint lines
2092            set n1 [string trim [string range [readexp ${key1}] \
2093                    [expr {($number%10)*5}] [expr {4+(($number%10)*5)}]] ]
2094            # number of new constraints
2095            set j2 [llength $value]
2096            # number of new constraint lines
2097            set val [set n2 [expr {($j2 + 2)/3}]]
2098            # store the new # of lines
2099            validint val 5
2100            setexp $key1 $val [expr {1+(($number%10)*5)}] 5
2101
2102            # loop over the # of lines in the old or new, whichever is greater
2103            set v0 0
2104            for {set j 1} {$j <= [expr {($n1 > $n2) ? $n1 : $n2}]} {incr j 1} {
2105                set key "LEQV PF${term}${l1}$j"
2106                # were there more lines in the old?
2107                if {$j > $n2} {
2108                    # this line is not needed
2109                    if {$j % 3 == 1} {
2110                        delexp %key
2111                    }
2112                    continue
2113                }
2114                # are we adding new lines?
2115                if {$j > $n1} {
2116                    makeexprec $key
2117                }
2118                # add the three constraints to the line
2119                foreach s {3 23 43} \
2120                        item [lrange $value $v0 [expr {2+$v0}]] {
2121                    if {$item != ""} {
2122                        set val [format %-10s%9.3f \
2123                                [lindex $item 0],[lindex $item 1] \
2124                                [lindex $item 2]]
2125                        setexp $key $val $s 19
2126                    } else {
2127                        setexp $key " " $s 19
2128                    }
2129                }
2130                incr v0 3
2131            }
2132        }
2133        profile*-add {
2134            regsub profile $type {} term
2135            if {$term < 10} {
2136                set term " $term"
2137            }
2138            set key "LEQV PF$term   "
2139            if {![existsexp $key]} {makeexprec $key}
2140            set nterms [string trim [string range [readexp ${key}] 0 4] ]
2141            if {$nterms == ""} {
2142                set nterms 1
2143            } elseif {$nterms >= 99} {
2144                return 0
2145            } else {
2146                incr nterms
2147            }
2148            # store the new # of constraints
2149            set val $nterms
2150            validint val 5
2151            setexp $key $val 1 5
2152
2153            if {$nterms > 9} {
2154                set k1 [expr {($nterms+1)/10}]
2155                set l1 $nterms
2156            } else {
2157                set k1 " "
2158                set l1 " $nterms"
2159            }
2160            set key1 "LEQV PF$term  $k1"
2161
2162            # number of new constraints
2163            set j2 [llength $value]
2164            # number of new constraint lines
2165            set val [set n2 [expr {($j2 + 2)/3}]]
2166            # store the new # of lines
2167            validint val 5
2168            setexp $key1 $val [expr {1+(($nterms%10)*5)}] 5
2169
2170            # loop over the # of lines to be added
2171            set v0 0
2172            for {set j 1} {$j <= $n2} {incr j 1} {
2173                set key "LEQV PF${term}${l1}$j"
2174                makeexprec $key
2175                # add the three constraints to the line
2176                foreach s {3 23 43} \
2177                        item [lrange $value $v0 [expr {2+$v0}]] {
2178                    if {$item != ""} {
2179                        set val [format %-10s%9.3f \
2180                                [lindex $item 0],[lindex $item 1] \
2181                                [lindex $item 2]]
2182                        setexp $key $val $s 19
2183                    } else {
2184                        setexp $key " " $s 19
2185                    }
2186                }
2187                incr v0 3
2188            }
2189        }
2190        profile*-get {
2191            regsub profile $type {} term
2192            if {$term < 10} {
2193                set term " $term"
2194            }
2195            if {$number > 9} {
2196                set i [expr {($number+1)/10}]
2197            } else {
2198                set i " "
2199            }
2200            set key "LEQV PF$term  $i"
2201            # return nothing if no term exists
2202            if {![existsexp $key]} {return 0}
2203            # number of constraint lines
2204           
2205            set numline [string trim [string range [readexp ${key}] \
2206                    [expr {($number%10)*5}] [expr {4+(($number%10)*5)}]] ]
2207            if {$number == 0} {return $numline}
2208            set clist {}
2209            if {$number < 10} {
2210                set number " $number"
2211            }
2212            for {set i 1} {$i <= $numline} {incr i} {
2213                set key "LEQV PF${term}${number}$i"
2214                set line [readexp ${key}]
2215                foreach s {1 21 41} e {20 40 60} {
2216                    set seg [string range $line $s $e]
2217                    if {[string trim $seg] == ""} continue
2218                    # parse the string segment
2219                    set parse [regexp { *([0-9AL]+),([0-9AL]+) +([0-9.]+)} \
2220                            $seg junk phase hist mult]
2221                    # was parse successful
2222                    if {!$parse} {continue}
2223                    lappend clist [list $phase $hist $mult]
2224                }
2225            }
2226            return $clist
2227        }
2228        default {
2229            set msg "Unsupported constrinfo access: type=$type action=$action"
2230            tk_dialog .badexp "Error in readexp access" $msg error 0 OK
2231        }
2232
2233    }
2234}
2235
2236# read the default profile information for a histogram
2237# use: profdefinfo hist set# parm action
2238
2239#     proftype -- profile function number
2240#     profterms -- number of profile terms
2241#     pdamp -- damping value for the profile (*)
2242#     pcut -- cutoff value for the profile (*)
2243#     pterm$n -- profile term #n
2244#     pref$n -- refinement flag value for profile term #n (*)
2245
2246proc profdefinfo {hist set parm "action get"} {
2247    global expgui
2248    if {$hist < 10} {
2249        set key "HST  $hist"
2250    } else {
2251        set key "HST $hist"
2252    }
2253    switch -glob ${parm}-$action {
2254        proftype-get {
2255            set val [string range [readexp "${key}PRCF$set"] 0 4]
2256            if {$val == " "} {return 0}
2257            return $val
2258        }
2259        profterms-get {
2260            set val [string range [readexp "${key}PRCF$set"] 5 9]
2261            if {$val == " "} {return 0}
2262            return $val
2263        }
2264        pcut-get {
2265            return [string trim [string range [readexp "${key}PRCF$set"] 10 19]]
2266        }
2267        pdamp-get {
2268                set val [string range [readexp "${key}PRCF$set"] 24 24]
2269            if {$val == " "} {return 0}
2270            return $val
2271        }
2272        pterm*-get {
2273            regsub pterm $parm {} num
2274            set f1 [expr {15*(($num - 1) % 4)}]
2275            set f2 [expr {15*(1 + ($num - 1) % 4)-1}]
2276            set line  [expr {1 + ($num - 1) / 4}]
2277            return [string trim [string range [\
2278                        readexp "${key}PRCF${set}$line"] $f1 $f2] ]
2279        }
2280        pref*-get {
2281            regsub pref $parm {} num
2282            set f [expr {24+$num}]
2283            if {[string toupper [string range [readexp "${key}PRCF$set"] $f $f]] == "Y"} {
2284                return 1
2285            }
2286            return 0
2287        }
2288        default {
2289            set msg "Unsupported profdefinfo access: parm=$parm action=$action"
2290            tk_dialog .badexp "Code Error" $msg error 0 Exit
2291        }
2292    }
2293}
2294
2295# get March-Dollase preferred orientation information
2296# use MDprefinfo hist phase axis-number parm action value
2297#    ratio    -- ratio of xtallites in PO direction vs random (>1 for more)
2298#    fraction -- fraction in this direction, when more than one axis is used
2299#    h k & l  -- indices of P.O. axis
2300#    ratioref -- flag to vary ratio
2301#    fracref  -- flag to vary fraction
2302#    damp     -- damping value
2303#    type     -- model type (0 = P.O. _|_ to beam, 1 = || to beam)
2304#    new      -- creates a new record with default values (set only)
2305proc MDprefinfo {histlist phaselist axislist parm "action get" "value {}"} {
2306    foreach phase $phaselist hist $histlist axis $axislist {
2307        if {$phase == ""} {set phase [lindex $phaselist end]}
2308        if {$hist == ""} {set hist [lindex $histlist end]}
2309        if {$axis == ""} {set axis [lindex $axislist end]}
2310        if {$hist < 10} {
2311            set hist " $hist"
2312        }
2313        if {$axis > 9} {
2314            set axis "0"
2315        }
2316        set key "HAP${phase}${hist}PREFO${axis}"
2317        switch -glob ${parm}-$action {
2318            ratio-get {
2319                return [string trim [string range [readexp $key] 0 9]]
2320            }
2321            ratio-set {
2322                if ![validreal value 10 6] {return 0}
2323                setexp $key $value 1 10
2324            }
2325            fraction-get {
2326                return [string trim [string range [readexp $key] 10 19]]
2327            }
2328            fraction-set {
2329                if ![validreal value 10 6] {return 0}
2330                setexp $key $value 11 10
2331            }
2332            h-get {
2333                set h [string trim [string range [readexp $key] 20 29]]
2334                # why not allow negative h values?
2335                #               if {$h < 1} {return 0}
2336                return $h
2337            }
2338            h-set {
2339                if ![validreal value 10 2] {return 0}
2340                setexp $key $value 21 10
2341            }
2342            k-get {
2343                set k [string trim [string range [readexp $key] 30 39]]
2344                #               if {$k < 1} {return 0}
2345                return $k
2346            }
2347            k-set {
2348                if ![validreal value 10 2] {return 0}
2349                setexp $key $value 31 10
2350            }
2351            l-get {
2352                set l [string trim [string range [readexp $key] 40 49]]
2353                #if {$l < 1} {return 0}
2354                return $l
2355            }
2356            l-set {
2357                if ![validreal value 10 2] {return 0}
2358                setexp $key $value 41 10
2359            }
2360            ratioref-get {
2361                if {[string toupper \
2362                        [string range [readexp $key] 53 53]] == "Y"} {
2363                    return 1
2364                }
2365                return 0
2366            }
2367            ratioref-set {
2368                if $value {
2369                    setexp $key "Y" 54 1
2370                } else {
2371                    setexp $key "N" 54 1
2372                }
2373            }
2374            fracref-get {
2375                if {[string toupper \
2376                        [string range [readexp $key] 54 54]] == "Y"} {
2377                    return 1
2378                }
2379                return 0
2380            }
2381            fracref-set {
2382                if $value {
2383                    setexp $key "Y" 55 1
2384                } else {
2385                    setexp $key "N" 55 1
2386              }
2387            }
2388            damp-get {
2389                set val [string trim [string range [readexp $key] 59 59]]
2390                if {$val == " "} {return 0}
2391                return $val
2392            }
2393            damp-set {
2394                setexp $key $value 60 1
2395            }
2396            type-get {
2397                set val [string trim [string range [readexp $key] 64 64]]
2398                if {$val == " "} {return 0}
2399                return $val
2400            }
2401            type-set {
2402                # only valid settings are 0 & 1
2403                if {$value != "0" && $value != "1"} {set value "0"}
2404                setexp $key $value 65 1
2405            }
2406            new-set {
2407                makeexprec $key
2408                setexp $key \
2409                        {  1.000000  1.000000  0.000000  0.000000  1.000000   NN    0    0} \
2410                        1 68
2411            }
2412            default {
2413                set msg "Unsupported MDprefinfo access: parm=$parm action=$action"
2414                tk_dialog .badexp "Error in readexp" $msg error 0 Exit
2415            }
2416
2417        }
2418
2419    }
2420}
2421
2422# write the .EXP file
2423proc expwrite {expfile} {
2424    global exparray
2425    set blankline \
2426     "                                                                        "
2427    set fp [open ${expfile} w]
2428    fconfigure $fp -translation crlf
2429    set keylist [lsort [array names exparray]]
2430    # reorder the keys so that VERSION comes 1st
2431    set pos [lsearch -exact $keylist {     VERSION}]
2432    set keylist "{     VERSION} [lreplace $keylist $pos $pos]"
2433    foreach key $keylist {
2434        puts $fp [string range \
2435                "$key$exparray($key)$blankline" 0 79]
2436    }
2437    close $fp
2438}
2439
2440# history commands -- delete all but last $keep history records,
2441# renumber if $renumber is true
2442proc DeleteHistory {keep renumber} {
2443    global exparray
2444    foreach y [lrange [lsort -decreasing \
2445            [array names exparray {    HSTRY*}]] $keep end] {
2446        unset exparray($y)
2447    }
2448    if !$renumber return
2449    # renumber
2450    set i 0
2451    foreach y [lsort -increasing \
2452            [array names exparray {    HSTRY*}]] {
2453        set key [format "    HSTRY%3d" [incr i]]
2454        set exparray($key) $exparray($y)
2455        unset exparray($y)
2456    }
2457    # list all history
2458    #    foreach y [lsort -decreasing [array names exparray {    HSTRY*}]] {puts "$y $exparray($y)"}
2459}
2460
2461proc CountHistory {} {
2462    global exparray
2463    return [llength [array names exparray {    HSTRY*}]]
2464}
2465
2466# set the phase flags for histogram $hist to $plist
2467proc SetPhaseFlag {hist plist} {
2468    # make a 2 digit key -- hh
2469    if {$hist < 10} {
2470        set hh " $hist"
2471    } else {
2472        set hh $hist
2473    }
2474    set key "HST $hh NPHAS"
2475    set str {}
2476    foreach iph {1 2 3 4 5 6 7 8 9} {
2477        if {[lsearch $plist $iph] != -1} {
2478            append str {    1}
2479        } else {
2480            append str {    0}     
2481        }
2482    }
2483    setexp $key $str 1 68
2484}
2485
2486# erase atom $atom from phase $phase
2487# update the list of atom types, erasing the record if not needed.
2488proc EraseAtom {atom phase} {
2489    set type [atominfo $phase $atom type]
2490    if {$type == ""} return
2491    if {$atom < 10} {
2492        set key "CRS$phase  AT  $atom"
2493    } elseif {$atom < 100} {
2494        set key "CRS$phase  AT $atom"
2495    } else {
2496        set key "CRS$phase  AT$atom"
2497    }
2498    # delete the records for the atom
2499    global exparray
2500    foreach k [array names exparray ${key}*] {
2501        delexp $k
2502    }
2503    # change the number of atoms in the phase
2504    phaseinfo $phase natoms set [expr {[phaseinfo $phase natoms] -1}]
2505
2506    # now adjust numbers in "EXPR ATYP" records and delete, if needed.
2507    set natypes [readexp " EXPR  NATYP"]
2508    if {$natypes == ""} return
2509    set j 0
2510    for {set i 1} {$i <= $natypes} {incr i} {
2511        incr j
2512        if {$j <10} {
2513            set key " EXPR ATYP $j"
2514        } else {
2515            set key " EXPR ATYP$j"
2516        }
2517        while {![existsexp $key]} {
2518            incr j
2519            if {$j > 99} {
2520                return
2521            } elseif {$j <10} {
2522                set key " EXPR ATYP $j"
2523            } else {
2524                set key " EXPR ATYP$j"
2525            }
2526        }
2527        set keytype [string trim [string range $exparray($key) 2 9]]
2528        if {$type == $keytype} {
2529            # found the type record
2530            set val [string trim [string range $exparray($key) 10 14]]
2531            incr val -1
2532            # if this is the last reference, remove the record,
2533            # otherwise, decrement the counter
2534            if {$val <= 0} {
2535                incr natypes -1 
2536                validint natypes 5
2537                setexp " EXPR  NATYP" $natypes 1 5
2538                delexp $key
2539            } else {
2540                validint val 5
2541                setexp $key $val 11 5
2542            }
2543            return
2544        }
2545    }
2546}
2547
2548# compute equivalent anisotropic temperature factor for Uequiv
2549proc CalcAniso {phase Uequiv} {
2550    foreach var {a b c alpha beta gamma} {
2551        set $var [phaseinfo $phase $var]
2552    }
2553
2554    set G(1,1) [expr {$a * $a}]
2555    set G(2,2) [expr {$b * $b}]
2556    set G(3,3) [expr {$c * $c}]
2557    set G(1,2) [expr {$a * $b * cos($gamma*0.017453292519943)}]
2558    set G(2,1) $G(1,2)
2559    set G(1,3) [expr {$a * $c * cos($beta *0.017453292519943)}]
2560    set G(3,1) $G(1,3)
2561    set G(2,3) [expr {$b * $c * cos($alpha*0.017453292519943)}]
2562    set G(3,2) $G(2,3)
2563
2564    # Calculate the volume**2
2565    set v2 0.0
2566    foreach i {1 2 3} {
2567        set J [expr {($i%3) + 1}]
2568        set K [expr {(($i+1)%3) + 1}]
2569        set v2 [expr {$v2+ $G(1,$i)*($G(2,$J)*$G(3,$K)-$G(3,$J)*$G(2,$K))}]
2570    }
2571    if {$v2 > 0} {
2572        set v [expr {sqrt($v2)}]
2573        foreach i {1 2 3} {
2574            set i1 [expr {($i%3) + 1}]
2575            set i2 [expr {(($i+1)%3) + 1}]
2576            foreach j {1 2 3} {
2577                set j1 [expr {($j%3) + 1}]
2578                set j2 [expr {(($j+1)%3) + 1}]
2579                set C($j,$i) [expr {(\
2580                        $G($i1,$j1) * $G($i2,$j2) - \
2581                        $G($i1,$j2)  * $G($i2,$j1)\
2582                        )/ $v}]
2583            }
2584        }
2585        set A(1,2) [expr {0.5 * ($C(1,2)+$C(2,1)) / sqrt( $C(1,1)* $C(2,2) )}]
2586        set A(1,3) [expr {0.5 * ($C(1,3)+$C(3,1)) / sqrt( $C(1,1)* $C(3,3) )}]
2587        set A(2,3) [expr {0.5 * ($C(2,3)+$C(3,2)) / sqrt( $C(2,2)* $C(3,3) )}]
2588        foreach i {1 1 2} j {2 3 3} {
2589            set A($i,$j) [expr {0.5 * ($C($i,$j) + $C($j,$i)) / \
2590                    sqrt( $C($i,$i)* $C($j,$j) )}]
2591            # clean up roundoff
2592            if {abs($A($i,$j)) < 1e-5} {set A($i,$j) 0.0}
2593        }
2594    } else {
2595        set A(1,2) 0.0
2596        set A(1,3) 0.0
2597        set A(2,3) 0.0
2598    }
2599    return "$Uequiv $Uequiv $Uequiv \
2600            [expr {$Uequiv * $A(1,2)}] \
2601            [expr {$Uequiv * $A(1,3)}] \
2602            [expr {$Uequiv * $A(2,3)}]"
2603}
Note: See TracBrowser for help on using the repository browser.