Changeset 13 for trunk/gui/scripts/table.tcl
- Timestamp:
- Jun 8, 2005, 5:37:19 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gui/scripts/table.tcl
r11 r13 2 2 # COMPONENT: table - extracts data from an XML description of a table 3 3 # 4 # This object represents one table in an XML description of a device.4 # This object represents one table in an XML description of a table. 5 5 # It simplifies the process of extracting data representing columns 6 6 # in the table. … … 11 11 # ====================================================================== 12 12 package require Itcl 13 package require BLT14 13 15 14 namespace eval Rappture { # forward declaration } 16 15 17 16 itcl::class Rappture::Table { 18 constructor { libobj path} { # defined below }17 constructor {xmlobj path} { # defined below } 19 18 destructor { # defined below } 20 19 21 20 public method rows {} 22 public method columns {{pattern *}} 23 public method vectors {{what -overall}} 21 public method columns {args} 22 public method values {args} 23 public method limits {col} 24 24 public method hints {{key ""}} 25 25 26 protected method _build {} 27 28 private variable _units "" ;# system of units for this table 29 private variable _limits ;# maps slab name => {z0 z1} limits 30 private variable _zmax 0 ;# length of the device 31 26 private variable _xmlobj "" ;# ref to lib obj with curve data 32 27 private variable _table "" ;# lib obj representing this table 33 private variable _tree "" ;# BLT tree used to contain table data 34 35 private common _counter 0 ;# counter for unique vector names 28 private variable _tuples "" ;# list of tuples with table data 36 29 } 37 30 … … 39 32 # CONSTRUCTOR 40 33 # ---------------------------------------------------------------------- 41 itcl::body Rappture::Table::constructor {libobj path} { 42 if {![Rappture::library isvalid $libobj]} { 43 error "bad value \"$libobj\": should be LibraryObj" 44 } 45 set _table [$libobj element -as object $path] 46 set _units [$_table get units] 47 48 # determine the overall size of the device 49 set z0 [set z1 0] 50 foreach elem [$_device children recipe] { 51 switch -glob -- $elem { 52 slab* - molecule* { 53 if {![regexp {[0-9]$} $elem]} { 54 set elem "${elem}0" 55 } 56 set tval [$_device get recipe.$elem.thickness] 57 set tval [Rappture::Units::convert $tval \ 58 -context um -to um -units off] 59 set z1 [expr {$z0+$tval}] 60 set _limits($elem) [list $z0 $z1] 61 62 set z0 $z1 63 } 64 } 65 } 66 set _zmax $z1 67 68 # build up vectors for various components of the table 69 _build 34 itcl::body Rappture::Table::constructor {xmlobj path} { 35 if {![Rappture::library isvalid $xmlobj]} { 36 error "bad value \"$xmlobj\": should be Rappture::library" 37 } 38 set _table [$xmlobj element -as object $path] 39 40 # 41 # Load data from the table and store in the tuples. 42 # 43 set _tuples [Rappture::Tuples ::#auto] 44 foreach cname [$_table children -type column] { 45 set label [$_table get $cname.label] 46 $_tuples column insert end -name $cname -label $label 47 } 48 49 set cols [llength [$_tuples column names]] 50 set nline 1 51 foreach line [split [$_table get data] \n] { 52 if {[llength $line] == 0} { 53 continue 54 } 55 if {[llength $line] != $cols} { 56 error "bad data at line $nline: expected $cols columns but got \"[string trim $line]\"" 57 } 58 $_tuples insert end $line 59 incr nline 60 } 70 61 } 71 62 … … 74 65 # ---------------------------------------------------------------------- 75 66 itcl::body Rappture::Table::destructor {} { 67 itcl::delete object $_tuples 76 68 itcl::delete object $_table 77 # don't destroy the _device! we don't own it! 78 79 foreach name [array names _comp2vecs] { 80 eval blt::vector destroy $_comp2vecs($name) 81 } 82 } 83 84 # ---------------------------------------------------------------------- 85 # USAGE: components ?<pattern>? 86 # 87 # Returns a list of names for the various components of this table. 88 # If the optional glob-style <pattern> is specified, then it returns 89 # only the component names matching the pattern. 90 # ---------------------------------------------------------------------- 91 itcl::body Rappture::Table::components {{pattern *}} { 69 # don't destroy the _xmlobj! we don't own it! 70 } 71 72 # ---------------------------------------------------------------------- 73 # USAGE: rows 74 # 75 # Returns the number of rows of information in this table. 76 # ---------------------------------------------------------------------- 77 itcl::body Rappture::Table::rows {} { 78 return [$_tuples size] 79 } 80 81 # ---------------------------------------------------------------------- 82 # USAGE: columns ?-component|-label|-units? ?<pos>? 83 # 84 # Returns information about the columns associated with this table. 85 # ---------------------------------------------------------------------- 86 itcl::body Rappture::Table::columns {args} { 87 Rappture::getopts args params { 88 flag switch -component 89 flag switch -label default 90 flag switch -units 91 } 92 if {[llength $args] == 0} { 93 set cols [llength [$_tuples column names]] 94 set plist "" 95 for {set i 0} {$i < $cols} {incr i} { 96 lappend plist $i 97 } 98 } elseif {[llength $args] == 1} { 99 set p [lindex $args 0] 100 if {[string is integer $p]} { 101 lappend plist $p 102 } else { 103 set pos [lsearch -exact [$_tuples column names] $p] 104 if {$pos < 0} { 105 error "bad column \"$p\": should be column name or integer index" 106 } 107 lappend plist $pos 108 } 109 } else { 110 error "wrong # args: should be \"columns ?-component|-label|-units? ?pos?\"" 111 } 112 92 113 set rlist "" 93 foreach name [array names _comp2vecs] { 94 if {[string match $pattern $name]} { 95 lappend rlist $name 114 switch -- $params(switch) { 115 -component { 116 set names [$_tuples column names] 117 foreach p $plist { 118 lappend rlist [lindex $names $p] 119 } 120 } 121 -label { 122 set names [$_tuples column names] 123 foreach p $plist { 124 set name [lindex $names $p] 125 catch {unset opts} 126 array set opts [$_tuples column info $name] 127 lappend rlist $opts(-label) 128 } 129 } 130 -units { 131 set names [$_tuples column names] 132 foreach p $plist { 133 set comp [lindex $names $p] 134 lappend rlist [$_table get $comp.units] 135 } 96 136 } 97 137 } … … 100 140 101 141 # ---------------------------------------------------------------------- 102 # USAGE: vectors ?<name>? 103 # 104 # Returns a list {xvec yvec} for the specified table component <name>. 105 # If the name is not specified, then it returns the vectors for the 106 # overall table (sum of all components). 107 # ---------------------------------------------------------------------- 108 itcl::body Rappture::Table::vectors {{what -overall}} { 109 if {[info exists _comp2vecs($what)]} { 110 return $_comp2vecs($what) 111 } 112 error "bad option \"$what\": should be [join [lsort [array names _comp2vecs]] {, }]" 142 # USAGE: values ?-row <index>? ?-column <index>? 143 # 144 # Returns a single value or a list of values for data in this table. 145 # If a particular -row and -column is specified, then it returns 146 # a single value for that row/column. If either the -row or the 147 # -column is specified, then it returns a list of values in that 148 # row or column. With no args, it returns all values in the table. 149 # ---------------------------------------------------------------------- 150 itcl::body Rappture::Table::values {args} { 151 Rappture::getopts args params { 152 value -row "" 153 value -column "" 154 } 155 if {[llength $args] > 0} { 156 error "wrong # args: should be \"values ?-row r? ?-column c?\"" 157 } 158 if {"" == $params(-row) && "" == $params(-column)} { 159 return [$_tuples get] 160 } elseif {"" == $params(-column)} { 161 return [lindex [$_tuples get $params(-row)] 0] 162 } 163 164 if {[string is integer $params(-column)]} { 165 set col [lindex [$_tuples column names] $params(-column)] 166 } else { 167 set col $params(-column) 168 if {"" == [$_tuples column names $col]} { 169 error "bad column name \"$col\": should be [join [$_tuples column names] {, }]" 170 } 171 } 172 173 if {"" == $params(-row)} { 174 # return entire column 175 return [$_tuples get -format $col] 176 } 177 # return a particular cell 178 return [$_tuples get -format $col $params(-row)] 179 } 180 181 # ---------------------------------------------------------------------- 182 # USAGE: limits <column> 183 # 184 # Returns the {min max} limits of the numerical values in the 185 # specified <column>, which can be either an integer index to 186 # a column or a column name. 187 # ---------------------------------------------------------------------- 188 itcl::body Rappture::Table::limits {column} { 189 set min "" 190 set max "" 191 foreach v [values -column $column] { 192 if {"" == $min} { 193 set min $v 194 set max $v 195 } else { 196 if {$v < $min} { set min $v } 197 if {$v > $max} { set max $v } 198 } 199 } 200 return [list $min $max] 113 201 } 114 202 … … 121 209 # ---------------------------------------------------------------------- 122 210 itcl::body Rappture::Table::hints {{keyword ""}} { 123 foreach key {label scale color units restrict} { 124 set str [$_table get $key] 211 foreach {key path} { 212 label about.label 213 color about.color 214 style about.style 215 } { 216 set str [$_table get $path] 125 217 if {"" != $str} { 126 218 set hints($key) $str … … 136 228 return [array get hints] 137 229 } 138 139 # ----------------------------------------------------------------------140 # USAGE: _build141 #142 # Used internally to build up the vector representation for the143 # table when the object is first constructed, or whenever the table144 # data changes. Discards any existing vectors and builds everything145 # from scratch.146 # ----------------------------------------------------------------------147 itcl::body Rappture::Table::_build {} {148 # discard any existing data149 foreach name [array names _comp2vecs] {150 eval blt::vector destroy $_comp2vecs($name)151 }152 catch {unset _comp2vecs}153 154 #155 # Scan through the components of the table and create156 # vectors for each part.157 #158 foreach cname [$_table children -type component] {159 set xv ""160 set yv ""161 162 set val [$_table get $cname.constant]163 if {$val != ""} {164 set domain [$_table get $cname.domain]165 if {$domain == "" || ![info exists _limits($domain)]} {166 set z0 0167 set z1 $_zmax168 } else {169 foreach {z0 z1} $_limits($domain) { break }170 }171 set xv [blt::vector create x$_counter]172 $xv append $z0 $z1173 174 if {$_units != ""} {175 set val [Rappture::Units::convert $val \176 -context $_units -to $_units -units off]177 }178 set yv [blt::vector create y$_counter]179 $yv append $val $val180 181 set zm [expr {0.5*($z0+$z1)}]182 } else {183 set xydata [$_table get $cname.xy]184 if {"" != $xydata} {185 set xv [blt::vector create x$_counter]186 set yv [blt::vector create y$_counter]187 188 foreach line [split $xydata \n] {189 if {[scan $line {%g %g} xval yval] == 2} {190 $xv append $xval191 $yv append $yval192 }193 }194 }195 }196 197 if {$xv != "" && $yv != ""} {198 set _comp2vecs($cname) [list $xv $yv]199 incr _counter200 }201 }202 }
Note: See TracChangeset
for help on using the changeset viewer.