#!/bin/sh
# This comment extends to the next line for tcl \
exec dbwish -f $0 $*

proc plot {db record} { 
    
    set db [lreplace $db 3 3 $record]
    if { [catch {set name [dbgetv $db 0 $record instrument.dfile]}] } { 
	set name [dbgetv $db 0 $record stage.dfile]
    }

    regsub -all {\.} $name "_" name
    global Done
    if { [info exists Done($name)] } { return } 
    set Done($name) 1

    set elements [.mag element names] 
    set nelements [llength $elements]
    if { $nelements > 0 } { 
	.phs legend configure -mapped 0 
	.mag legend configure -mapped 1 -position bottom -relief flat
	wm title . "Response Curves"
	wm iconname . Response
    } else { 
	.phs legend configure -mapped 0 
	.mag legend configure -mapped 0
	wm title . $name
	wm iconname . $name
    }

    set colors {slateblue blue    skyblue       darkturquoise cyan   aquamarine}
    set dashes {""        {50 10} {50 10 20 10} {10 10}       {20 4} { 50 10 20 10 20 10 }}
    set ncolors [llength $colors]
    set color [lindex $colors [expr $nelements%$ncolors]]
    set dash ""
    # set dash  [lindex $dashes [expr $nelements%$ncolors]]
    set offdash white
    set activecolor magenta
    set linewidth 2

    global ::$name.f
    global ::$name.m
    global ::$name.p

    blt::vector $name.f
    blt::vector $name.m
    blt::vector $name.p
    dbresp2gr $name $db
    set n [expr [$name.f length]-5]
    global Fmin Fmax
    set Fmin [min $Fmin [eval set ${name}.f(0)]]
    set Fmax [max $Fmax [eval set ${name}.f($n)]]
    .mag xaxis configure -min $Fmin -max $Fmax
    .mag x2axis configure -min $Fmin -max $Fmax
    .phs xaxis configure -min $Fmin -max $Fmax
    .phs x2axis configure -min $Fmin -max $Fmax
    
    .mag element create "$name" \
	-mapx both \
	-mapy both \
	-xdata $name.f \
	-ydata $name.m \
	-linewidth $linewidth  \
	-activecolor $activecolor \
	-activelinewidth [expr 2*$linewidth]  \
	-color $color \
	-dashes $dash \
	-offdash $offdash

    .phs element create $name \
	-mapx both \
	-mapy both \
	-xdata $name.f \
	-ydata $name.p \
	-linewidth $linewidth  \
	-activelinewidth [expr 2*$linewidth]  \
	-activecolor $activecolor \
	-color $color \
	-dashes $dash \
	-offdash $offdash
}

proc build_windows {} { 
    blt::graph .phs -title ""
	.phs xaxis configure -logscale 1 -loose 1 -mapped 1 -title frequency
	.phs x2axis configure -logscale 1 -loose 1 -mapped 1 -title "" -showticks 1
	.phs yaxis configure -min -180. -max 180.0 -stepsize 90.0 \
		-subdivisions 3 -loose 0 -mapped 1 -title Phase
	.phs y2axis configure -min -180. -max 180.0 -stepsize 90.0 \
		-subdivisions 3 -loose 0 -mapped 1 -title ""

    set Mmin .000001
    set Mmax 1.1
    blt::graph .mag -title ""
	.mag xaxis configure -logscale 1 -loose 1 -mapped 1 -title ""
	.mag x2axis configure -logscale 1 -loose 1 -mapped 1 -title "" -showticks 1
	.mag yaxis configure -logscale 1 -min $Mmin -max $Mmax -loose 1 -mapped 1 -title Magnitude
	.mag y2axis configure -logscale 1 -min $Mmin -max $Mmax -loose 1 -mapped 1 -title ""

    button .postscript \
	-command postscript \
	-text Postscript

    button .done -command "destroy ." -text Done

    blt::table . \
	.phs 1,0 -cspan 10 -fill x\
	.mag 2,0 -cspan 10 -fill x\
	.postscript 3,0 -cspan 5 -fill x \
	.done 3,5 -cspan 5 -fill x

   global Fmin Fmax 
   set Fmin 1
   set Fmax 1

   Blt_ZoomStack .mag 
   Blt_ZoomStack .phs
   Blt_ActiveLegend .mag 

}

proc postscript {} {
    if { ! [winfo exists .filebox] } { 
	FileBox .filebox \
	    -title "Postscript File" \
	    -open "Save"
    }

    set filename [.filebox choose]
    if { [clength $filename] == 0 } { return } 
    
    set phs [.phs postscript output -paperheight 3i]
    set mag [.mag postscript output -paperheight 9i]
    set file [open $filename w+]
    puts $file $phs
    puts $file $mag
    close $file 
}

proc usage {} {
    global argv0
    puts stderr "Usage: $argv0 database.table"
    exit 1 
}

set Major 3
set Minor 1
set Date "\$Date: 1997/04/20 19:13:26 $"
regsub -all \\$ $Date "" Date
regsub "Date: " $Date "" Date
set Program dberesp
set Version $Major.$Minor

lappend auto_path $env(DSAP)/data/tcl/library
source $blt_library/bltGraph.tcl

old_tk

if { $argc < 1 || $argc > 4 } {
    usage
}

set maxwidth [expr [winfo vrootwidth .]-50]
set maxheight [expr [winfo vrootheight .]-50]
wm maxsize . $maxwidth $maxheight

bind . <Configure> {
    if { "%W" == "." && %E == 0 }  {
	if { "%w" != "$W" || "%h" != "$H" } { 
	    set buttons [winfo reqheight .done]
	    set left [expr %h-$buttons]
	    set phase [expr $left/3]
	    set mag   [expr $left-$phase]
	    .phs config -height $phase
	    .mag config -height $mag
	    set W %w 
	    set H %h
	    } 
    }
}

set W 0 
set H 0

set database_table [lindex $argv 0]
set db [dbopen_table $database_table r]
if { [lindex $db 1] < 0 } {
    puts stderr "Please specify a table"
    usage
}

build_windows

set n [dbquery $db dbRECORD_COUNT]
loop i 0 $n { 
     plot $db $i
    }


     
proc BltActivateLegend { graph x y } {
    global bltActiveEntry
 
    set old $bltActiveEntry($graph)
    set new [$graph legend get @$x,$y]
    if { $old != $new } {
        if { $old != "-1" } {
            $graph legend deactivate $old
            $graph element deactivate $old
            .phs legend deactivate $old
            .phs element deactivate $old
        }
        if { $new != "" } { 
            $graph legend activate $new
            $graph element activate $new
            .phs legend activate $new
            .phs element activate $new
        }
    }
    set bltActiveEntry($graph) $new
}


# $Id: dberesp.sh,v 1.2 1997/04/20 19:13:26 danq Exp $ 
