
#
# Control panel for maximum entropy tool
#
proc gui_mem {infile}  { 
    global viz vp mem 

    catch {destroy .mem}
    toplevel .mem -relief raised -bd 2
#
# Add buttons for other functions
    mkOkSave .mem.other mem {do_mem $datadir/$infile $mem(wp) $mem(res) $mem(order)} {Save MEM Spectrum as ...}
    button .mem.other.h -text Help  -command {InfoBox {This tool computes a Maximum Entropy Spectrum from your data and displays them using your favorite graphics package. You can set the order of the spectral estimate by adjusting the slidebar labeled "MEM Order".  You can also save the result in a file. }}
    pack append .mem.other .mem.other.h fill
    pack .mem.other -side right
#
# Create a widget for controlling the number of poles calculated by MEM
#
    scan [get_dims $infile] %d d1
    pack [label .mem.l0 -text "Maximum Entropy Method"] \
	[scalentry .mem.res -variable mem(res) -label "Resolution" -from 0 -to $d1 -length 250] \
	[scalentry .mem.order -variable mem(order) -label "MEM Order" -from 1 -to 200 -length 250] \
	[label .mem.t -height 1 -width 10]	-padx 5 -pady 5 -side top

#initialize the vizualization package, and make a spectrum
    set mem(wp) [window_$viz "Maximum Entropy Spectrum"]
#    update idletasks
    update
    set mem(res)  $d1
    set mem(order) 10
    if {$mem(launch)} {.mem.other.ok invoke}
}


proc do_mem { infile window nfreq npoles } {
    global viz
    .mem.t configure -text "Working..."  ;  Reverse .mem.t  ;  update
    scan [get_dims $infile] %d d1
    catch {exec spectrum $infile -p $npoles -f $nfreq} msg
    exec echo $msg >> spectra.log
    .mem.t configure -text Done  ;  Reverse .mem.t
    plot_$viz  $window linear-log {mem.out xy curve}
}


proc last2 {x} {
	set y 1
	while {$y < $x} {set y [expr 2*$y]}
	return [expr $y/2]
}
