
#
# Control panel for multitaper method tool
#
# $Id: mtm.tcl,v 1.2 1994/08/08 23:20:18 weibel Exp $
#
proc gui_mtm {infile}  { 
    global viz vp mtm 

    catch {destroy .mtm}
    toplevel .mtm -relief raised -bd 2
    
#
# Add buttons for other functions
    mkOkSave .mtm.other mtm {do_mtm $datadir/$infile $mtm(wp) [.mtm.nf get] \
	[set nwindows [.mtm.nw get]] [set bandw [.mtm.bw get]] [.mtm.th get]} \
	{Save MTM Spectrum as ...}
    button .mtm.other.h -text Help  -command {InfoBox {This tool computes a power spectrum from your data using multi-taper method (MTM) and displays them using your favorite graphics package.  You can also save the result in a file. }}
    pack append .mtm.other .mtm.other.h fill
    pack .mtm.other -padx 2m -side right
    
    pack [label .mtm.l0 -text "Multi-Taper Method"] \
	[scale .mtm.nf -orient horizontal -label "Number of frequencies" -from 1 -to 512 -length 250] \
	[scale .mtm.nw -orient horizontal -label "Number of windows" -from 0 -to 20 -length 250] \
	[scale .mtm.bw -orient horizontal -label "Bandwidth" -from 1 -to 21 -length 250]  \
	[scale .mtm.th -orient horizontal -label "Significance Threshold (%)" -from 1 -to 100 -length 250]  \
	[label .mtm.t -height 1 -width 10] -padx 5 -pady 3 -side top

#initialize the vizualization package, and make a spectrum
    set mtm(wp) [window_$viz "Multi-Taper Spectrum"]
    .mtm.nf set 256
    .mtm.nw set 10
    .mtm.bw set 10
    .mtm.th set 90
    if ($mtm(launch)) {.mtm.other.ok invoke}

    bind .mtm <Destroy> {exec rm -f mtm.out mtm.sig}
}

#
# Run the MultiTaper Method and plot the results
#
proc do_mtm {infile window nfreq nwindows bandw threshold} {
    global viz
    .mtm.t configure -text "Working..."  ;  Reverse .mtm.t
    update
    scan [get_dims $infile] %d d1
    set status [catch {exec mtm $infile -f $nfreq -b $bandw -s [expr $threshold/100.] -w $nwindows} msg]
    exec echo $msg >> spectra.log
    if {$status == 0} {
	exec cat mtm.sig >> spectra.log
	.mtm.t configure -text Done  ;  Reverse .mtm.t
	plot_$viz  $window linear {mtm.out xy curve} {mtm.sig xy scatter}
    } else {
	.mtm.t configure -text Error  ;  Reverse .mtm.t
	ErrorBox "MTM Error:\n$msg"
    }
    return $status
}
