#!/usr/local/bin/g -file
#
# stripchart.t, an example tclVogle script which implments a strip chart
# graphic
#

proc legendObject {title} {
    g_makeobj [set obj [g_genobj]]
      g_pushmatrix
	g_font small
	# to keep vogle from scaling the text
	g_viewport -100 100 -100 100
	g_move 0.0 -0.0090
        g_drawstr $title
      g_popmatrix
    g_closeobj
    return $obj
}

# load tmp data into a vogle object
proc tmp {arglist} {
    global tmp

    #set tmp(dataObjID) [g_genobj]
    # fbdStripChartObject $tmp(dataObjID) $arglist
    fbdStripChartObject - $arglist
}
proc spd {a} {tmp $a}


proc fbdStripChartObject {objID data {minVal ""} {maxVal ""}} {
    # if not supplied, determine the range to use for the y axis
    if {$minVal=="" || $maxVal==""} {
	foreach rec $data {
	    set p [lindex $rec 1]
	    if {$p==""} {continue}
	    if {$minVal==""} { set minVal [set maxVal $p]; continue }
	    if {$p<$minVal} {set minVal $p; continue}
	    if {$p>$maxVal} {set maxVal $p; continue}
	}
    }

    if {[set yScale [expr $maxVal-$minVal]]==0.0} {
	set yScale 1.0
    } else {
	set yScale [expr 1.0/$yScale]
    }
    set xScale [expr 1.0/([llength $data]-1)]

    # build an object out of the data points

    if {$objID != "-"} { g_makeobj $objID }
    g_pushmatrix
	g_font small
	g_color green
	g_move -1 -1; g_drawstr "$minVal"
	g_move -1 .8; g_drawstr $maxVal

	g_translate -1.0 -1.0
	g_scale 2 2
	g_scale [expr $xScale] [expr $yScale]
	g_translate 0.0 [expr -1.0*$minVal]

	set x 0.0
	g_move $x $minVal 
	foreach rec $data {
	    set p [lindex $rec 1]
	    if {$p==""} { 
		g_rdraw 1.0 0.0
		g_getgp xx yy
		g_color red
		g_drawstr "?"
		g_move $xx $yy
	    } else {
		g_getgp xx yy
		g_move $x $minVal 
		g_color white
		g_rdraw 0.0 $p
		g_move $xx $yy
		g_color yellow
		g_draw $x $p
	    }
	    set x [expr $x+1.0]
	}
    g_popmatrix
    if {$objID != "-"} { g_closeobj }
    return $objID
}
proc fbdGraphPaint {item} {
    global $item
    g_color black; g_clear
    source tmp.dat
    g_color red; g_callobj [set [set item](legendObjID)]
    # g_callobj [set [set item](dataObjID)]
    g_flush
}

# for each item we make a global assoc array
# which holds 
#	- a legend object XXX(legenObjID)
#
proc makeFbdGraph {parent itemlist} {
    global Name
    if {$parent=="."} { set prefix "" } else { set prefix $parent }

    foreach item $itemlist {
	global $item
	set [set item](legendObjID) [legendObject $item]

	graphic $prefix.$item \
	    -command "fbdGraphPaint $item" \
	    -squareaspect false \
	    -doublebuffered true \
	    -geometry 600x100 \
	    -relief raised -borderwidth 1 \
	    -font lucidasanstypewriter-12 \

	pack append $parent $prefix.$item {top fill expand}
	source tmp.dat
	# eval [exec /home/ads/bin/fbdtcl $Name $item]
    }
}
proc makeHeader {parent} {
    global Name
    if {$parent=="."} { set prefix "" } else { set prefix $parent }
    message $prefix.msg \
	-text "Station: $Name" \
	-aspect 8000 \
	-relief raised -borderwidth 2
    pack append $parent $prefix.msg {top fill}
}

# to force deferred initialization of vogle
g_output /dev/null
g_init postscript

wm minsize . 100 100
if {0} {
if {$argc!=1} {
    puts stderr "not enough arguments should be: fbdGraph <stnID>"
    exit 1
}
} else {
    set Name KPSX
}
set Name [lindex $argv 0]
makeHeader .
makeFbdGraph . {tmp spd}

wm title . "Querying FBD file for $Name..."
wm title . "fbdGraph" 
