mmtl_compare.tcl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. proc mmtl_compare { file1 file2 log } {
  2. #--------------------------------------------------------------
  3. # Open the first first file for the camparison.
  4. #--------------------------------------------------------------
  5. if { [catch { set f1 [open $file1 r] } result ] } {
  6. puts stderr "Could not open $file1"
  7. puts $log "Could not open $file1"
  8. return 1
  9. }
  10. #--------------------------------------------------------------
  11. # Get the data from the file.
  12. #--------------------------------------------------------------
  13. set data1 [ read $f1]
  14. close $f1
  15. #--------------------------------------------------------------
  16. # Open the second file for the comparison.
  17. #--------------------------------------------------------------
  18. if { [catch { set f2 [open $file2 r] } result ] } {
  19. puts stderr "Could not open $file2"
  20. puts $log "Could not open $file2"
  21. return 1
  22. }
  23. #--------------------------------------------------------------
  24. # Get the data from the file.
  25. #--------------------------------------------------------------
  26. set data2 [read $f2]
  27. close $f2
  28. set isCS 0
  29. set isResult 0
  30. #-------------------------------------------------------
  31. # Check if this is a result file.
  32. #-------------------------------------------------------
  33. if { [ string first "result" $file1] > 1 } {
  34. set indx [string first "Farads/Meter" $data1]
  35. if { $indx < 1 } {
  36. puts stderr "The comparison failed!"
  37. puts $log "The comparison failed!"
  38. return 1
  39. }
  40. set data1 [string range $data1 [expr {$indx + 12}] \
  41. [string length $data1]]
  42. set indx [string first "Asymm" $data1]
  43. if { $indx > 0 } {
  44. set isResult 1
  45. set indx2 [string first "(Ohms)" $data1]
  46. set indx3 [string first "Far-End" $data1]
  47. set data1B [string range $data1 [expr {$indx2 + 7}] \
  48. $indx3]
  49. set data1 [string range $data1 0 $indx]
  50. }
  51. set indx [string first "Farads/Meter" $data2]
  52. if { $indx < 1 } {
  53. puts stderr "The comparison failed!"
  54. puts $log "The comparison failed!"
  55. return 1
  56. }
  57. set data2 [string range $data2 [expr {$indx + 12}] \
  58. [string length $data2]]
  59. set indx [string first "Asymm" $data2]
  60. if { $indx > 0 } {
  61. set indx2 [string first "(Ohms)" $data2]
  62. set indx3 [string first "Far-End" $data2]
  63. set data2B [string range $data2 [expr {$indx2 + 7}] \
  64. $indx3]
  65. set data2 [string range $data2 0 $indx]
  66. }
  67. }
  68. #-------------------------------------------------------
  69. # Check if this is a xsctn file.
  70. #-------------------------------------------------------
  71. if { [string first "xsctn" $file1] > 1 } {
  72. set isCS 1
  73. set indx [string first "require csdl" $data1]
  74. if { $indx < 1 } {
  75. puts stderr "The comparison failed!"
  76. puts $log "The comparison failed!"
  77. return 1
  78. }
  79. set data1 [string range $data1 [expr {$indx + 12}] \
  80. [string length $data1]]
  81. set indx [string first "require csdl" $data2]
  82. if { $indx < 1 } {
  83. puts stderr "The comparison failed!"
  84. puts $log "The comparison failed!"
  85. return 1
  86. }
  87. set data2 [string range $data2 [expr {$indx + 12}] \
  88. [string length $data2]]
  89. }
  90. #----------------------------------------------------------------------
  91. # Initialize.
  92. #----------------------------------------------------------------------
  93. set maxDiff 0
  94. set maxNum1 0
  95. set maxNum2 0
  96. set indx 0
  97. set total1 0
  98. set total2 0
  99. set cnt1 0
  100. foreach itm $data1 {
  101. #-------------------------------------------------------------------
  102. # Only process the items that are numbers.
  103. #-------------------------------------------------------------------
  104. if { [scan $itm {%f} num1 ] == 1 } {
  105. #---------------------------------------------------------------
  106. # Check that this index in the second data-set is also a number.
  107. #---------------------------------------------------------------
  108. if { [scan [lindex $data2 $indx] {%f} num2] < 1 } {
  109. puts stderr "not both numbers: $num1 [lindex $data2 $indx]"
  110. puts $log "not both numbers: $num1 [lindex $data2 $indx]"
  111. return 1
  112. break
  113. }
  114. if { $num1 != 0 } {
  115. set diff [expr { abs ( ($num1 - $num2) / $num1 ) * 100.0 }]
  116. set total1 [expr { $total1 + $diff }]
  117. incr cnt1
  118. #------------------------------------------------------------
  119. # Save the maximum diff from the comparisons.
  120. #------------------------------------------------------------
  121. if { $diff > $maxDiff } {
  122. set maxDiff $diff
  123. set maxNum1 $num1
  124. set maxNum2 $num2
  125. }
  126. }
  127. }
  128. incr indx
  129. }
  130. #-------------------------------------------------------
  131. # The next piece of code will only be processed if the comparison
  132. # is being done on result files and there are more than one conductor.
  133. #-------------------------------------------------------
  134. if { $isResult } {
  135. set indx 0
  136. set cnt2 0
  137. foreach itm $data1B {
  138. if { [scan $itm {%f} num1 ] == 1 } {
  139. if { [scan [lindex $data2B $indx] {%f} num2] < 1 } {
  140. puts stderr "not both numbers: $num1\
  141. [lindex $data2B $indx]"
  142. puts $log "not both numbers: $num1 [lindex $data2B $indx]"
  143. return 1
  144. break
  145. }
  146. if { $num1 != 0 } {
  147. set diff [expr { abs ( ($num1 - $num2) / $num1 ) * 100.0 }]
  148. set total2 [expr { $total2 + $diff }]
  149. incr cnt2
  150. if { $diff > $maxDiff } {
  151. set maxDiff $diff
  152. set maxNum1 $num1
  153. set maxNum2 $num2
  154. }
  155. }
  156. }
  157. incr indx
  158. }
  159. }
  160. set total1 [expr { $total1 / $cnt1 }]
  161. puts stderr "-------------------------------------------------------------"
  162. puts stderr "Comparing:\t$file1\n \t$file2"
  163. set strg [format " Max Diff: %7.3f%s (%s : %s)" $maxDiff "\%" \
  164. $maxNum1 $maxNum2]
  165. puts stderr $strg
  166. if { ! $isCS } {
  167. set strg [format " (B & L) Average Diff: %7.3f%s" $total1 "\%"]
  168. puts stderr $strg
  169. if { $isResult } {
  170. set total2 [expr { $total2 / $cnt2 }]
  171. set strg [format "(I - Rdc) Average Diff: %7.3f%s" $total2 "\%"]
  172. puts stderr $strg
  173. set strg [format " Total Average Diff: %7.3f%s" \
  174. [expr { $total1 + $total2 }] "\%"]
  175. puts stderr $strg
  176. }
  177. }
  178. puts $log "-------------------------------------------------------------"
  179. puts $log "Comparing:\t$file1\n \t$file2"
  180. set strg [format " Max Diff: %5.3f%s (%s : %s)" $maxDiff "\%" \
  181. $maxNum1 $maxNum2]
  182. puts $log $strg
  183. if { ! $isCS } {
  184. set strg [format " (B & L) Average Diff: %7.3f%s" $total1 "\%"]
  185. puts $log $strg
  186. if { $isResult } {
  187. set strg [format "(I - Rdc) Average Diff: %7.3f%s" $total2 "\%"]
  188. puts $log $strg
  189. set strg [format " Total Average Diff: %7.3f%s" \
  190. [expr { $total1 + $total2 }] "\%"]
  191. puts $log $strg
  192. }
  193. }
  194. return 0
  195. }