forked from pjmaker/tma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tma.tcl
executable file
·3928 lines (3435 loc) · 115 KB
/
tma.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/sh
# -*-tcl-*- \
exec /opt/tma/bin/bltwish25 "$0" "$@"
#
# tma.tcl - a visualiser for time matters analyser, in particular irregularly
# sampled time series coming out of Powerwater ASIM, OSIsoft PI, ...
#
#
# Copyright 2013 Phil Maker <[email protected]>,<[email protected]>
#
# This file is part of Tma. Tma is free software: you can
# redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Tma is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tma. If not, see http://www.gnu.org/licenses/.
#
set ::VERSION "0.0 (purple butterfly)"
# 0.1, 0.14, 0.149, ...
proc get_version {} {
return "tma version $::VERSION"
}
# descriptions of commands, options and variables
proc describe {thing text} {
append ::descriptions($thing) $text
}
proc descriptions {{pat *}} {
set r {}
foreach t [lsort -dictionary [array names ::descriptions $pat]] {
append r $::descriptions($t)
}
return $r
}
# some helpers for configuration on different platforms
proc windows_only {c} {
if {$::tcl_platform(platform) eq "windows"} {
uplevel #0 $c
}
}
proc unix_only {c} {
if {$::tcl_platform(platform) eq "unix"} {
uplevel #0 $c
}
}
# process options from argv
describe options {
The following command line options can be passed via the command line, e.g.
by editing the tma.bat file or using the command line. Of particular note is
the <tt>-get file</tt> option that allows you to either get a file, e.g.
<tt>X.csv</tt> or a tma script <tt>X.tma</tt>.
<p>
}
# list of (option value description) for all options
foreach {o v d} {
-plot_format_time "%H:%M:%S %Z\n%d/%m/%Y" "X-axis time format"
-get {} "Get files matching this"
-fullscreen 0 "Full screen mode on startup"
-periods {
"&Autorange"
"1m" "10m" "&30m"
"1&h" "8h" "12h"
"1&d" "3d" "7d" "28d" "30d" "31d"
"1&y" "5y"
} "Periods in the top menu"
-pi_tags "R_*" "Only use tags matching this pattern, e.g. R_* in remote"
-pi_tag_reader 1 "Display the PI tag reader commands"
-pitag_file "Pitags.csv" "File of PI Tag attributes"
-pitag_exe "PItags.exe" "Program to read PI attributes from the server"
-pitool_exe "PItool.exe" "Program that connect to the PI Server"
-pi_server "172.16.12.140" "PI Server IP address/name"
-nc_reader "acep-csv" "Program to read ACEP NetCDF (.nc) files into CSV"
-check_log 1 "log checking information to a file"
-console 1 "Display the console on startup"
-console_lines 7 "How many lines we display on the console"
-console_safe 0 "Use a safe(TCL) interpreter for the console and .tma files"
-width 13i "Width of the plot"
-unix_gs gs "Executable for ghostscript under *NIX"
-windows_gs "c:/Program Files/gs/gs9.07/bin/gswin32.exe" "Path to ghostscript on windows"
-unix_pdf_reader evince "UNIX PDF reader"
-windows_pdf_reader "c:/Program Files/Adobe/Reader 9.0/Reader/AcroRd32.exe" "Path to PDF reader for windows."
-bltdebug 0 "Debug level, see blt::bltdebug"
-colors {
#ff0000 #00ff00 #0000ff #860000 #007b00 #8900cc #9c680b #e8c10b
#28e9fe #f900f8 #deff00 #78dc8b #00fea6 #a4b98c #d34c00 #005e9c
#d3007e #b9be4e #fcff00 #d17373 #9ed3a6
} "Default colors for plots"
} {
set ::options($o) $v
describe options "$o - $d (default: <tt>$v</tt>)<br>"
}
# workaround for windows bugs (ttt review, maybe just check and die)
windows_only {
array set ::options {
-console_safe 0
}
}
describe options "<p>Current options:<p>"
if {([llength $::argv] % 2) != 0} {
puts "$argv0: usage $argv [-option val]"
exit 1
}
foreach {o v} $::argv {
if {![string match -* $o]} {
puts "$argv0: $o should be an option"
exit 1
}
}
array set options $::argv
foreach v [lsort -dictionary [array names ::options]] {
describe options "<tt>$v $::options($v)</tt><br>"
}
# import required packages
package require Tk
package require Ttk
package require BLT
# string routines
# matches uses string match and allows choice |
proc match {pat list} {
set pats [split $pat |]
set r {}
foreach p $pats {
foreach pp [lsearch -all -inline -glob $list $p] {
lappend r $pp
}
}
set r [lsort -dictionary -unique $r]
return $r
}
# define and describe command definitions where commands are executable
# from .tma files, these are all in the ::command::* namespace along
# with the variables we use for modelling time and values.
describe commands "<h2>Commands for use in *.tma files</h2><p>"
namespace eval commands {}
# ttt add a trace mechanism at some time
proc command {name params description code} {
describe commands "<tt>$name $params</tt> - $description<br>"
uplevel #0 "proc ::commands::$name [list $params] [list $code]"
interp alias {} ::$name {} ::commands::$name
}
# timed var support - simple timed global variable system using BLT vectors
# to represent the what/when.
set ::commands::vars {} ;# list of all vars
array set ::commands::rename {} ;# rename rules for vars
command var {v args} {
create variable v with properties args, e.g. var ErrE or var E -dv 10.
Explain variable renaming. ttt
returns the variable name (which may have changed if rename command is used).
} {
msg "var $v $args"
foreach {p a} $args {
if {$p eq "-name"} {
msg "var $v -name $a"
set v $a
}
}
set v2 [string map [array get ::commands::rename] $v]
msg "var $v renamed to $v2"
set v $v2
blt::vector create ::commands::${v}_ ;# what
blt::vector create ::commands::${v}@ ;# when
# default values
array set ::commands::${v}= [default_properties]
# override individual properties
array set ::commands::${v}= $args ;# properties
if {[set ::commands::[set v]=(-color)] eq {}} {
set ::commands::${v}=(-color) [nextcolor]
}
set ::commands::${v}# 0 ;# number of samples
set ::commands::${v}* -1 ;# current index
set ::commands::${v} 0
set ::commands::${v}_b 0
lappend ::commands::vars ${v}
return $v
}
proc default_properties {} {
return [property_sort {
-dv 0
-dz 0
-mapy y
-color {}
-pixels 2
-symbol none
}]
}
proc property_sort {l} {
array set a $l
set ks [lsort -dictionary [array names a]]
set r {}
foreach k $ks {
lappend r $k $a($k)
}
return $r
}
command varrename {args} {
After this newly created variable names will be renamed using args,
e.g. varname R_A_PG "" will remove the R_A_PG string from all names.
} {
array set ::commands::rename $args
}
set ::nextcolor 0
proc nextcolor {} {
if {$::nextcolor < [llength $::options(-colors)]} {
set c [lindex $::options(-colors) $::nextcolor]
incr ::nextcolor
return $c
} else {
return [format "#%02X%02X%02X" \
[expr int(rand()*150)] \
[expr int(rand()*150)] \
[expr int(rand()*150)]]
}
}
command var- {args} {remove variables matching args} {
foreach pat $args {
foreach v [vars $pat] {
catch ".w element delete $v"
blt::vector destroy ::commands::${v}_
blt::vector destroy ::commands::${v}@
unset ::commands::${v}=
unset ::commands::${v}
set ::commands::vars [lsearch -inline -exact -all -not $::commands::vars $v]
}
}
return [vars]
}
command vars {{pat *}} {list of all variables} {
return [lsearch -all -inline $::commands::vars $pat]
}
command properties {var} {list of properties and attributes for var} {
return [property_sort [array get ::commands::$var=]]
}
command property {var prop {def nodefault}} {return the value for property p} {
if {[info exists ::commands::$var=($prop)]} {
return [set ::commands::${var}=($prop)]
} elseif {$def == "nodefault"} {
error "property $var $prop failed"
} else {
return $def
}
}
command let {v what when} {v has value what at time when} {
# puts "let $v $what [time::format $when] [time::format [st]]"
if {[samples $v] == 0} { # no data yet
let0 $v $what $when
} else { # > 0 samples
if {[expr abs($what) >= [property $v -dz]]} {
letn $v $what $when
} else {
# ignore it within -dz deadband
}
}
return $what
}
# let0 handles the case for no samples
proc let0 {v what when} {
if {$when < [st]} { # sample is before so save it, still no real data
set ::before_when($v) $when
set ::before_what($v) $what
} elseif {$when == [st]} { # at st so just put it in
let2 $v $what $when
} elseif {$when < [et]} { # after st but before et
if {[info exists ::before_what($v)]} {
let2 $v $::before_what($v) [expr max($::before_when($v),[st])]
}
let2 $v $what $when
} elseif {$when == [et]} {
let2 $v $what $when
} else {
msg "warning: $v didn't have data for the st..et"
}
}
proc letn {v what when} {
if {$when < [et]} {
let2 $v $what $when
} elseif {[::commands::${v}@ index end] != [et]} {
let2 $v $what [expr min([et], $when)]
} else {
# msg "let $v $what $when ignored"
}
}
proc let2 {v what when} {
# puts "let2 $v $what [time::format $when]"
if {[samples $v] > 0} {
set bt [::commands::${v}@ index end]
set bv [::commands::${v}_ index end]
if {abs($what - $bv) <= [set ::commands::${v}=(-dv)]} { # no change no save
return
}
} else {
set bt 0
}
::commands::${v}_ append $what
::commands::${v}@ append $when
incr ::commands::${v}#
set ::commands::$v $what
if {$when < $bt} {
msg "warning: timetravel let $v $what [time::format $when] before [time::format $bt]"
::commands::${v}@ sort ::commands::${v}_
}
# used by overtime code to see the current state
}
command samples {v} {the number of samples in v} {
return [set ::commands::${v}#]
}
command when# {v i} {the time for sample i in v} {
# N.B. index is used rather than ($i) because of a buglet in BLT
# which happens for vectors inside namespaces.
return [::commands::${v}@ index $i]
}
command setwhen# {v i t} {set the time for sample i in v} {
set ::commands::${v}@($i) $t
}
command what# {v i} {the value for sample i in v} {
return [::commands::${v}_ index $i]
}
command first# {v} {return the first sample for v} {
return [what# $v 0]
}
command last# {v} {return the last sample for v} {
return [what# $v [expr [sample $v] - 1]]
}
command let# {v what i} {v at offset i has value what} {
if {$i < 0 || $i >= [samples $v]} {
error "let# $v $what $i out of bounds"
}
::commands::[set v]_ index $i $what
}
# slow for now
command what {v t} {return value for v at time t} {
for {set i 0} {$i < [samples $v] - 1} {incr i} {
if {[when# $v $i] <= $t && $t < [when# $v [expr $i+1]]} {
return [what# $v $i]
}
}
return [what# $v [expr [samples $v] - 1]]
}
array set ::commands::file_to_varname {}
command file_to_varname {fn} { convert a filename to variable name} {
set s [file tail [file rootname $fn]]
set s [string map [array get ::commands::file_to_varname] $s]
return $s
}
# time
namespace eval time {}
proc time::scan {s} {
if {[string is double $s]} {
return $s
}
regexp {([^.]*)([.][0-9]+)?$} $s -> ts ms
if {[catch "clock scan $ts -format %Y-%m-%dT%H:%M:%S" t0]} {
set t0 [clock scan $ts]
}
set r [expr $t0 + 0$ms]
# puts "$s -> $r"
return $r
}
proc time::format {t} {
return [clock format [expr int($t)] \
-format %Y-%m-%dT%H:%M:%S][::format .%03d [expr int(1000*($t-int($t)))]]
}
array set ::monthnames {
1 Jan 2 Feb 3 Mar 4 Apr 5 May 6 Jun 7 Jul 8 Aug 9 Sep 10 Oct 11 Nov 12 Dec
}
command monthname {n} {
translate a month number <tt>n</tt> in 1..13 to a name, e.g. (2=Feb, note 1 or 13 are <em>both</em> Jan)
} {
require {[string is int $n] && 1<= $n && $n <= 13} ;# 13=Jan
if {$n == 13} {
set n 1
}
ensure {[info exists ::monthnames($n)]}
return $::monthnames($n)
}
command month:next {n} {map month n to the next month (1->2,.., 13->1)} {
incr n
if {$n > 12} {
set n 1
}
return $n
}
# start/end time commands which limit the period we are interested
# in by causing let to ignore the data
set ::st [expr [clock seconds] - [expr 30 * 24 * 60 * 60]]
set ::st [expr 7 * 24 * 60 * 60]; # >0 to make PI happy
set ::st_f [time::format $::st]
command st {{t {}}} {
set start time to <tt>t</tt>, if no t returns current start time
} {
if {$t ne {}} {
set ::st [time::scan $t]
set ::et [expr max($::st,$::et)]
set ::st_f [time::format $::st]
set ::et_f [time::format $::et]
msg "st [time::format $::st]"
}
return $::st
}
set ::et [clock seconds]
set ::et_f [time::format $::et]
command et {{t {}}} {
set end time to <tt>t</tt>, if not <tt>t</tt> returns end time
} {
if {$t ne {}} {
set ::et [time::scan $t]
set ::st [expr min($::st,$::et)]
set ::st_f [time::format $::st]
set ::et_f [time::format $::et]
msg "et [time::format $::et]"
}
return $::et
}
# PDF driver using ghostscript - this is pretty horrible since it has
# to work in different coordinate systems and munge the postscript
# in order to generate things in the right place.
namespace eval pdf {
variable filename
variable psfilename
variable fd "no such file"
variable gsopts \
"-q -dBATCH -sDEVICE=pdfwrite -dNOPAUSE -dSAFER -sPAPERSIZE=a4"
variable height [winfo fpixels . 11i]
variable pagex
variable pagey
variable default_file "tma-default.pdf"
}
proc pdf::new {file} {
variable filename
variable psfilename
variable fd
variable height
variable pagex
variable pagey
set filename $file
set psfilename [file rootname $filename].ps
set fd [open $psfilename w]
set pagex [winfo fpixels . 0.5i]
set pagey [expr $height - [winfo fpixels . 0.5i]]
}
proc pdf::default {} {
variable fd
variable default_file
if {[catch [list tell $fd]]} { # not open so start the default
msg "pdf::default opening $default_file"
pdf::new $default_file
} else {
# msg "pdf::default already open"
}
}
proc pdf::show {} {
variable fd
variable filename
variable tmpfilename
variable gsopts
variable psfilename
newpage
puts $fd "%EOF"
close $fd
if {$::tcl_platform(platform) eq "windows"} {
exec $::options(-windows_gs) {*}$gsopts \
-sOutputFile=$filename -f $psfilename
file delete $psfilename
exec $::options(-windows_pdf_reader) $filename &
} else {
exec $::options(-unix_gs) {*}$gsopts \
-sOutputFile=$filename -f $psfilename
file delete $psfilename
exec $::options(-unix_pdf_reader) $filename &
}
}
proc pdf::newpage {} {
pdf::default
variable pagex
variable pagey
variable height
set pagex [winfo fpixels . 0.5i]
set pagey [expr $height - [winfo fpixels . 0.5i]]
variable fd
puts $fd "showpage"
}
proc pdf::pixels2i {p} {
return [expr ($p/[winfo fpixels . 1i])]
}
proc pdf::vbox {d} {
variable pagex
variable pagey
if {$pagey - [winfo fpixels . $d] < [winfo fpixels . 1i]} {
newpage
}
set r "-pagex [pixels2i ${pagex}]i -pagey [pixels2i ${pagey}]i"
set pagey [expr $pagey - [winfo fpixels . $d]]
return $r
}
proc pdf::vboxp {d} {
variable pagex
variable pagey
variable height
if {$pagey - [winfo fpixels . $d] < [winfo fpixels . 0.5i]} {
newpage
}
set y [expr $height - $pagey]
set r "-padx \{[pixels2i ${pagex}]i 0\} -pady \{[pixels2i ${y}]i 0\}"
append r " -height $d"
set pagey [expr $pagey - [winfo fpixels . $d]]
return $r
}
proc pdf::adjust {s} {
set r {}
set l 0
for {set i 0} {$i < [string length $s]} {incr i} {
set c [string index $s $i]
if {$c eq "\n"} {
append r $c
set l 0
} elseif {$l < 90} {
append r $c
incr l
} elseif {$l >= 97 && [string is space $c]} {
append r "\n"
set l 0
} elseif {$l >= 80} {
append r "$c\n"
set l 0
} else {
append r $c
incr l
}
}
return $r
}
proc pdf::text {s font} {
pdf::default
# first break fmt s so it fits
set s [pdf::adjust $s]
# and then build up the PDF
variable fd
toplevel .pdftext
wm attributes .pdftext -fullscreen 1
set w .pdftext.t
canvas $w -width 16i -height 10i
pack $w
set nlines [llength [split $s \n]]
set linespace [font metrics $font -linespace]
set h [pixels2i [expr $nlines * $linespace]]
$w create text 1m 1m -text $s -anchor nw -font $font
update
after 20
update
after 50
puts $fd [string map {%EOF {} showpage {}} \
[$w postscript -height ${h}i -pageanchor nw \
{*}[vbox ${h}i]]]
update
destroy .pdftext
}
array set pdf::plotopts {
-center 0
-colormode color
-decorations 0
-landscape 0
-maxpect 1
-paperwidth 7.0i
-paperheight 11.4i
-width 7i
}
proc pdf::plot {w h args} {
update
variable fd
variable plotopts
array set opts [array get plotopts]
array set opts $args
report:text \n ;# ttt hack but it works
puts -nonewline $fd [string map {%EOF {} showpage {}} \
[$w postscript output \
{*}[array get opts] \
{*}[vboxp $h]]]
}
# report generation
command report:new {filename} {
Start a new report to <tt>filename</tt>
} {
pdf::new $filename
}
command report:text {t {font {Helvetica 12 normal}}} {
Add text <tt>t</tt> in font <tt>font</tt> to report.
Note that variables and commands are expanded.
} {
pdf::text [uplevel 1 [list subst $t]] $font
}
command report:plaintext {t {font {Courier 12 italic}}} {
Add text <tt>t</tt> in font <tt>font</tt> to report.
Note that variables and commands are not expanded.
} {
pdf::text $t $font
}
command report:plot {h} {
Add a plot with height <tt>h</tt>.
} {
pdf::plot .w $h
}
command report:fullplot {} {
Add a full page plot with height <tt>h</tt>.
} {
pdf::plot .w 8i -landscape 1 -width 12i
}
command report:header {t} {
Generate a report header
} {
report:text "$t" {Helvetica 14 bold}
}
command report:newpage {} {
Generate a new page in the report
} {
pdf::newpage
}
command report:show {} {
show the final report to the user
} {
update
update idletasks
pdf::show
}
command title {t} {use title for the plot} {
.w configure -title $t
}
command scale {v s} {scale v by s, e.g. 'scale FedP 2' doubles FedP} {
::commands::${v}_ set [blt::vector expr "::commands::${v}_ * $s"]
return "scaled $v by $s"
}
command scalet {v s} {scale v timebase by s, e.g. 'scalet FedP 2' slow down FedP} {
::commands::${v}@ set [blt::vector expr "::commands::${v}@ * $s"]
return "scalet $v time by $s"
}
command offset {v o} {offset v by o, e.g. 'offset FedP 2' adds 2} {
::commands::${v}_ set [blt::vector expr "::commands::${v}_ + $o"]
}
command offsett {v o} {offset v timebase by o, e.g. 'offsett FedP 2' adds 2s} {
::commands::${v}@ set [blt::vector expr "::commands::${v}@ + $o"]
}
command offset_remove {v} {remove offset for v by subtracting first value} {
if {[samples $v] > 0} {
offset $v [expr - [what# $v 0]]
} else {
msg "offset_remove $v - warning no samples to offset"
}
}
# pi tags interface to metadata on the PI server
array set tags_descr {}
array set tags_units {}
array set tags_all_units {}
array set tags_all_sites {}
command tags:clear {} {clear all tag info} {
array set ::tags_descr {}
array set ::tags_units {}
}
# use the server program
command tags:getserver {} {get tag info from the server -- this is slow} {
msg "tags:getserver - reading tag info from server $::options(-pi_server)"
msg "tags:getserver - warning this is very slow"
set cmd [list open "|$::options(-pitag_exe) $::options(-pi_server)" r]
if {[catch $cmd fd]} {
msg "tags:readserver failed with $fd error"
return 0
}
set n 0
while {[gets $fd line] >= 0} {
set fields [split $line ,]
set ::tags_descr([lindex $fields 0]) [lindex $fields 1]
set ::tags_units([lindex $fields 0]) [lindex $fields 2]
set ::tags_all_units([lindex $field 2]) 1
if {($n % 1000) == 0} {
msg "$n read"
}
incr n
}
close $fd
return 1
}
command tags:put {file} {put current list of tags into a file} {
set fd [open $file w]
foreach t [pi:tags *] {
puts $fd "$t,$::tags_descr($t),$::tags_units($t)"
}
close $fd
}
command tags:get {file} {get current list of tags from a file} {
set fd [open $file r]
while {[gets $fd line] >= 0} {
set fields [split $line ,]
set tag [lindex $fields 0]
if {![string match $::options(-pi_tags) $tag]} {
continue
}
set description [lindex $fields 1]
set units [lindex $fields 2]
set units [string map {" " {}} $units]
set ::tags_descr($tag) $description
set ::tags_units($tag) $units
set ::tags_all_units($units) 1
if {[string match "R_*" $tag]} {
set ::tags_all_sites([pi:site $tag]) 1
}
}
close $fd
}
command tags:only {tagpat} {remove any not matching tagpat} {
foreach tag [pi:tags *] {
if {[string match $tagpat $tag]} {
continue
}
array unset ::tags_descr $tag
array unset ::tags_units $tag
}
array unset ::tags_all_units *
foreach {t u} [array get ::tags_units] {
set ::tags_all_units($u) 1
}
}
proc tags:init {} {
if {[catch "tags:get pi.tags"]} {
msg "tags:init cannot read pi.tags file, recreating it from server"
tags:getserver
tags:put pi.tags
} else {
# we've just read it in from pi.tags above
}
}
command pi:tags {{pat *}} {return all pi tags matching pat} {
return [match $pat [array names ::tags_descr]]
}
command pi:description {tag} {return the description for tag} {
return $::tags_descr($tag)
}
command pi:units {tag} {return the units for the tag} {
return $::tags_units($tag)
}
command pi:all_units {} {return all the units} {
return [lsort -dictionary [array names ::tags_all_units]]
}
command pi:all_sites {} {return all the sites} {
return [array names ::tags_all_sites]
}
command pi:pwcregion {tag} {return the region for a PWC tag} {
array set r {
D Darwin
A "Alice Springs"
T "Tennant Creek"
K "Katherine"
}
return $r([lindex [split $tag _] 1])
}
command pi:site {tag} {return the community id for a PWC tag} {
return [lindex [split $tag _] 3]
}
command pi:part {tag n} {return part of the tagname at n using _ as a sep} {
return [lindex [split $tag _] $n]
}
# linear ramping functions
command ramp:triangle {v rate} {generate a triangle wave} {
var $v
display $v
set y 0.0
set dir 1.0
for {set t [st]} {$t <= [et]} {set t [expr $t + 1.0]} {
let $v $y $t
set y [expr min(1.0,max(0.0,$y + $dir*$rate))]
if {$y == 0.0 || $y == 1.0} {
set dir [expr -$dir]
}
}
}
command filter:ramplimit {v up down} {ramp limit v upwards by up/s and down/s} {
for {set i 1} {$i < [samples $v]} {incr i} {
set dt [expr [when# $v $i] - [when# $v [expr $i-1]]]
set bv [what# $v [expr $i-1]]
set val [what# $v $i]
set vmax [expr $bv + ($up*$dt)]
set vmin [expr $bv - ($down*$dt)]
let# $v [expr max($vmin,min($vmax,$val))] $i
}
}
command random:flat {v {low 0} {high 1}} {random numbers low..high} {
var $v
display $v
for {set t [st]} {$t <= [et]} {incr t} {
let $v [expr rand()] $t
scale $v [expr $high - $low]
offset $v $low
}
}
command random:boundedwalk {v {pru 0.01} {up 0.01} {prd 0.01} {down 0.01}} {generate a random walk on 0..1} {
var $v -dv 0.02
display $v
set w 0.0
for {set t [st]} {$t <= [et]} {set t [expr $t + 1]} {
if {[expr rand() < 0.5]} {
if {[expr rand() <= $pru]} {
set w [expr $w + $up]
}
} else {
if {[expr rand() <= $prd]} {
set w [expr $w - $down]
}
}
set w [expr min(1.0,max(0.0,$w))]
let $v $w $t
}
}
command random:unboundedwalk {v {pru 0.01} {up 0.01} {prd 0.01} {down 0.01}} {generate an unbounded random walk} {
var $v -dv 0.02
display $v
set w 0.0
for {set t [st]} {$t <= [et]} {set t [expr $t + 1]} {
if {[expr rand() < 0.5]} {
if {[expr rand() <= $pru]} {
set w [expr $w + $up]
}
} else {
if {[expr rand() <= $prd]} {
set w [expr $w - $down]
}
}
let $v $w $t
if {[expr ($t % 10000)] == 0} {
update
}
}
}
# ttt this doesn't do delta compression since it writes lots
# of repeats
command filter:deadband {v db} {filter using a +/- db deadband} {
set val [what# $v 0]
for {set i 1} {$i < [samples $v]} {incr i} {
set cval [what# $v $i]
if {abs($cval - $val) > $db} {
set val $cval
}
let# $v $val $i
}
}
# ttt some problem as deadband
command filter:quantise {v s} {keep within quantised steps of size s} {
for {set i 0} {$i < [samples $v]} {incr i} {
set val [what# $v $i]
let# $v [expr ($s*round($val/$s)) +0.5*$s] $i
}
}
command filter:magictkln {v s} {tkln magic filter} {
for {set i 1} {$i < [samples $v]} {incr i} {
set pv [what# $v [expr $i-1]]
let# $v [expr ($pv + [what# $v $i])/2.0] $i
}
set cval [what# $v 0]
for {set i 1} {$i < [samples $v]} {incr i} {
set val [what# $v $i]
set err [expr abs($cval - $val)]
if {$err > 30} {
set cval [expr 15+(30.0*floor($val/30.0))]
}
let# $v $cval $i
}
}
command display {v args} {display v on main plot with options args} {
msg "display $v $args"
plot::display .w $v \