#!/usr/bin/wish
#-------------
#Version to include 31 bands for each of 4 windows.
#This is a very ugly, and clunky version because 
#speed was necessary and I'm not proud enough
#at TCL to worry about elegance... EM. 
#----------------
#HISTORY:
#
#First edition: EM. Some time around early Mayish - made for 2 windows per sub-band!
#Expanded to accomodate 15 windows per suband.   EM. 
#Jun 28 2006 	EM. Streamlined the code a little.. 
#Aug 19th: 	EM. Super-dupered up the display  to a 2x2 format.
#		EM. Added line colour changing to indicate near band edge
#		EM. Added display of distance in MHz to nearest band edge
#       -NB. This feature is now redundant - with interleave mode. Sept. 5th. EM. 
#Aug 22nd: 	EM. Added checkbutton facility
#Sept 5th:      EM. Expanded to accomodate 31 windows per sub band.
#Sept 9th:      EM. Tweaked band settings.. more options to follow..
#Sept 9th:      EM. Added red colour for bands
#Sept 20th:     EM. Re-sized the whole panel a bit.. 
#Apr 13th, 2007:EM. eliminated redunant loop - sped things up mucho.
#		Separated frequency window, re-organised buttons, streamlined 
#		code a bit (ver 3.3) 


#Code contains:
# CALCULATE - To respond to the 'calculate' button, and just about anything else. Does all plotting etc.
# MAIN - sets up the gui, fundamental paramters etc.
# SAVE - Save the current setup to a file
# RESTORE - Restore a previously saved setup from a file
# HELP - Open a new window with some fundamental info on how to use mops_vis



#current version:
set ver 3.3


#START HERE
#set up parameters and initialise variables.
proc calculate {} {
global low_0 low_1 high_1 high_0 freq dopvel x y xmarg xmore nlines bwid bpos

if {$freq == ""} {set freq 0}
if {$dopvel == ""} {set dopvel 0}
	
set usb 1
set sign 1
set b1 $low_0; set b2 $low_1; set b3 $high_1; set b4 $high_0
		    
#Re -calculate frequencies and ranges for bands
set minfrq1 [expr [lindex $low_0 1]+$freq]
set maxfrq1 [expr [lindex $low_0 123]+$freq]
set centb1 [expr ($maxfrq1+$minfrq1)/2]
set minfrq2 [expr [lindex $low_1 1]+$freq]
set maxfrq2 [expr [lindex $low_1 123]+$freq]
set centb2 [expr ($maxfrq2+$minfrq2)/2]
set minfrq3 [expr [lindex $high_1 1]+$freq]
set maxfrq3 [expr [lindex $high_1 123]+$freq]
set centb3 [expr ($maxfrq3+$minfrq3)/2]
set minfrq4 [expr [lindex $high_0 1]+$freq]
set maxfrq4 [expr [lindex $high_0 123]+$freq]
set centb4 [expr ($maxfrq4+$minfrq4)/2]
set pixperm [expr $x/($bwid)]
	
foreach c {1 2 3 4} {.fplotb$c delete all}


# Calculate dopplershifted frequencies, if set.
set cnt 1
	
#Check if any lines have been set, and make the doppler calculation
while {$cnt <= $nlines} {  
	global f$cnt ; upvar 1 f$cnt freq_1
	global f$cnt\dop; upvar 1 f$cnt\dop dop_1 
	global cb$cnt; upvar 1 cb$cnt check_set
		
	if { $freq_1 != "" } { 
		set f$cnt\dop [expr $freq_1*(1-($dopvel/3e+5))]
		foreach window {.fplotb1 .fplotb2 .fplotb3 .fplotb4} {
			switch $window {
			.fplotb1 { set linepos  \
			[expr (($dop_1-$centb1)*$pixperm+($x/2))+$xmarg] }
			.fplotb2 { set linepos  \
			[expr (($dop_1-$centb2)*$pixperm+($x/2))] }
			.fplotb3 { set linepos  \
			[expr (($dop_1-$centb3)*$pixperm+($x/2))+$xmarg] }
			.fplotb4 { set linepos  \
			[expr (($dop_1-$centb4)*$pixperm+($x/2))] }
			    }
			set line_name [.lines.frqlist$cnt cget -text]
			if { $line_name eq "(Select)" } { set line_name $cnt }
			if { $check_set } {
			$window create line \
			$linepos\m 1m $linepos\m $y\m -fill purple -width 2
			$window create text \
			[expr $linepos+5]m 3m -text $line_name -fill purple 
				}
			}
		}
	    
	    incr cnt
	    }    



    
set oset 5.0
set oset_num 2.5
set thisnum -59
set num 1
set rn 0.3

foreach {a b c d} $b1 {
	set rn [expr $rn+$oset]
	set fstart [expr $freq+$b] 
	set fend [expr $freq+$d]
	set p1 [expr ($fstart-$minfrq1)*$pixperm+$xmarg]
	set p2 [expr ($fend-$minfrq1)*$pixperm+$xmarg]


	.fplotb1 create line \
		$p1\m [expr $rn/2.]\m $xmarg\m  [expr $rn/2.]\m -fill grey -width 2
	.fplotb1 create line \
		$p1\m [expr $rn/2.]\m $p2\m [expr $rn/2.]\m -fill skyblue -width 11
	.fplotb1 create text \
		3m [expr $num*$oset_num]m -text $thisnum -fill black -font {-size 8}

	set cnt 1
	while {$cnt <= $nlines} {
		global f$cnt\dop; upvar 1 f$cnt\dop dop_1 
		global cb$cnt; upvar 1 cb$cnt check_set
		if { $dop_1 != "" } { 
		if { ($fstart < $dop_1) && ($fend > $dop_1) && ($check_set)} {
			.fplotb1 create line \
				$p1\m [expr $rn/2.]\m $p2\m [expr $rn/2.]\m -fill red -width 11
			.fplotb1 create line \
				$p1\m [expr $rn/2.]\m $xmarg\m  [expr $rn/2.]\m -fill red -width 2
		.fplotb1 create text \
			3m [expr $num*$oset_num]m -text $thisnum -fill red -font {-size 8}}
			}
		    incr cnt 
		    }


	.fplotb1 create text \
		[expr $p1+5]\m [expr $rn/2.]\m -text [expr $fstart+$bpos] -font {-size 6}
		incr thisnum
		incr num


	}



set thisnum -29
set num 1
set rn 0.3
foreach {a b c d} $b2 {
	set rn [expr $rn+$oset]
	set fstart [expr $freq+$b] 
	set fend [expr $freq+$d]
	set p1 [expr ($fstart-$minfrq2)*$pixperm]
	set p2 [expr ($fend-$minfrq2)*$pixperm]
	.fplotb2 create line \
		$p1\m [expr $rn/2.]\m $x\m  [expr $rn/2.]\m -fill grey -width 2
	.fplotb2 create line \
		$p1\m [expr $rn/2.]\m $p2\m [expr $rn/2.]\m -fill skyblue -width 11
	.fplotb2 create text \
		[expr $x-3+$xmarg]m [expr $num*$oset_num]m -text $thisnum -fill black -font {-size 8}
	set cnt 1
	while {$cnt <= $nlines} {
		global f$cnt\dop; upvar 1 f$cnt\dop dop_1 
		global cb$cnt; upvar 1 cb$cnt check_set
		if { $dop_1 != "" } { 
			if { ($fstart < $dop_1) && ($fend > $dop_1)  && ($check_set)} {
			.fplotb2 create line \
				$p1\m [expr $rn/2.]\m $x\m  [expr $rn/2.]\m -fill red -width 2
			.fplotb2 create line \
				$p1\m [expr $rn/2.]\m $p2\m [expr $rn/2.]\m -fill red -width 11
			.fplotb2 create text \
				[expr $x-3+$xmarg]m [expr $num*$oset_num]m -text $thisnum -fill red -font {-size 8}}
			}

			incr cnt 
		    }
	.fplotb2 create text \
		[expr $p1+5]\m [expr $rn/2.]\m -text [expr $fstart+$bpos] -font {-size 6}
		incr thisnum
		incr num
	}

    set thisnum -1
	set num 1
	set rn 0.3
	foreach {a b c d} $b3 {
	    set rn [expr $rn+$oset]
		set fstart [expr $freq+$b] 
		set fend [expr $freq+$d]
		set p1 [expr ($fstart-$minfrq3)*$pixperm+$xmarg]
		set p2 [expr ($fend-$minfrq3)*$pixperm+$xmarg]
		.fplotb3 create line $p1\m [expr $rn/2.]\m $xmarg\m  [expr $rn/2.]\m -fill grey -width 2
		.fplotb3 create line $p1\m [expr $rn/2.]\m $p2\m [expr $rn/2.]\m -fill skyblue -width 11
		.fplotb3 create text 3m [expr $num*$oset_num]m -text $thisnum -fill black -font {-size 8}


		set cnt 1
		while {$cnt <= $nlines} {
		    global f$cnt\dop; upvar 1 f$cnt\dop dop_1 
		global cb$cnt; upvar 1 cb$cnt check_set
			if { $dop_1 != "" } { 
			    if { ($fstart < $dop_1) && ($fend > $dop_1)  && ($check_set)} {
		.fplotb3 create line $p1\m [expr $rn/2.]\m $xmarg\m  [expr $rn/2.]\m -fill red -width 2
		.fplotb3 create line $p1\m [expr $rn/2.]\m $p2\m [expr $rn/2.]\m -fill red -width 11
		.fplotb3 create text 3m [expr $num*$oset_num]m -text $thisnum -fill red -font {-size 8}}
			}

		incr cnt 
		    }
		.fplotb3 create text [expr $p1+5]\m [expr $rn/2.]\m -text [expr $fstart+$bpos] -font {-size 6}
			incr thisnum
			incr num
	}

    set thisnum 29
	set num 1
	set rn 0.3
	foreach {a b c d} $b4 {
	    set rn [expr $rn+$oset]
		set fstart [expr $freq+$b] 
		set fend [expr $freq+$d]
		set p1 [expr ($fstart-$minfrq4)*$pixperm]
		set p2 [expr ($fend-$minfrq4)*$pixperm]
		.fplotb4 create line $p1\m [expr $rn/2.]\m $x\m  [expr $rn/2.]\m -fill grey -width 2
		.fplotb4 create line $p1\m [expr $rn/2.]\m $p2\m [expr $rn/2.]\m -fill skyblue -width 11
		.fplotb4 create text [expr $x-3+$xmarg]m [expr $num*$oset_num]m -text $thisnum -fill black -font {-size 8}
		set cnt 1

		while {$cnt <= $nlines} {
	    global f$cnt\dop; upvar 1 f$cnt\dop dop_1 
		global cb$cnt; upvar 1 cb$cnt check_set
			if { $dop_1 != "" } { 
			    if { ($fstart < $dop_1) && ($fend > $dop_1)  && ($check_set)} {
		.fplotb4 create line $p1\m [expr $rn/2.]\m $x\m  [expr $rn/2.]\m -fill red -width 2
		.fplotb4 create line $p1\m [expr $rn/2.]\m $p2\m [expr $rn/2.]\m -fill red -width 11
		.fplotb4 create text [expr $x-3+$xmarg]m [expr $num*$oset_num]m -text $thisnum -fill red -font {-size 8}}
			}

		incr cnt 
		    }
		.fplotb4 create text [expr $p1+5]\m [expr $rn/2.]\m -text [expr $fstart+$bpos] -font {-size 6}
			incr thisnum
			incr num
	}

    foreach c {2 4} {
	.fplotb$c create line 0m $y\m 0m 0m $xmore\m 0m -width 3
	    .fplotb$c create line $xmore\m 0m $xmore\m $y\m 0m $y\m -width 1
	    .fplotb$c create line [expr $x/2]m 1m [expr $x/2]m $y\m -fill grey
	    .fplotb$c create line [expr $x/4]m 1m [expr $x/4]m $y\m -fill grey
	    .fplotb$c create line [expr 3*$x/4]m 1m [expr 3*$x/4]m $y\m -fill grey
	    set oset 3.55
	}
    foreach c {1 3} {
	.fplotb$c create line 0m $y\m 0m 0m $xmore\m 0m -width 3
	    .fplotb$c create line $xmore\m 0m $xmore\m $y\m 0m $y\m -width 1
	    .fplotb$c create line [expr $x/4+$xmarg]m 1m [expr $x/4+$xmarg]m $y\m -fill grey
	    .fplotb$c create line [expr $x/2+$xmarg]m 1m [expr $x/2+$xmarg]m $y\m -fill grey
	    .fplotb$c create line [expr 3*$x/4+$xmarg]m 1m [expr 3*$x/4+$xmarg]m $y\m -fill grey
	    set oset 3.55
	}
    .fplotb1 create text [expr $x-15]m [expr $y/2]m -text "Band 1" -font {-size 15} -fill orange
	.fplotb2 create text 15m [expr $y/2]m -text "Band 2" -font {-size 15} -fill orange
	.fplotb3 create text [expr $x-15]m [expr $y/2]m -text "Band 3" -font {-size 15} -fill orange
	.fplotb4 create text 15m [expr $y/2]m -text "Band 4" -font {-size 15} -fill orange
	
.fplotb1 create text [expr $x/2+$xmarg]m [expr $y-1]m -text [format "%5.2f" $centb1] -font {-size 7}
.fplotb1 create text [expr $x/4+$xmarg]m [expr $y-1]m -text [format "%5.2f" [expr $centb1-($bwid/4)]]  -font {-size 7}
.fplotb1 create text [expr 3*$x/4+$xmarg]m [expr $y-1]m -text [format "%5.2f" [expr $centb1+($bwid/4)]] -font {-size 7}
    
.fplotb2 create text [expr $x/2]m [expr $y-1]m -text [format "%5.2f" $centb2] -font {-size 7}
.fplotb2 create text [expr $x/4]m [expr $y-1]m -text [format "%5.2f" [expr $centb2-($bwid/4)]]  -font {-size 7}
.fplotb2 create text [expr 3*$x/4]m [expr $y-1]m -text [format "%5.2f" [expr $centb2+($bwid/4)]] -font {-size 7}
    
.fplotb3 create text [expr $x/2+$xmarg]m [expr $y-1]m -text [format "%5.2f" $centb3] -font {-size 7}
.fplotb3 create text [expr $x/4+$xmarg]m [expr $y-1]m -text [format "%5.2f" [expr $centb3-($bwid/4)]]  -font {-size 7}
.fplotb3 create text [expr 3*$x/4+$xmarg]m [expr $y-1]m -text [format "%5.2f" [expr $centb3+($bwid/4)]] -font {-size 7}
    
.fplotb4 create text [expr $x/2]m [expr $y-1]m -text [format "%5.2f" $centb4] -font {-size 7}
.fplotb4 create text [expr $x/4]m [expr $y-1]m -text [format "%5.2f" [expr $centb4-($bwid/4)]]  -font {-size 7}
.fplotb4 create text [expr 3*$x/4]m [expr $y-1]m -text [format "%5.2f" [expr $centb4+($bwid/4)]] -font {-size 7}


}



#This is really where the guts of the gui starts.

set low_0 [list \
	   w1_0 -4140.0	w1_1  -4002.0 \
	   w2_0 -4071.0	w2_1  -3933.0 \
	   w3_0 -4002.0	w3_1  -3864.0 \
	   w4_0 -3933.0	w4_1  -3795.0 \
	   w5_0 -3864.0	w5_1  -3726.0 \
	   w6_0 -3795.0	w6_1  -3657.0 \
	   w7_0 -3726.0	w7_1  -3588.0 \
	   w8_0 -3657.0	w8_1  -3519.0 \
	   w9_0 -3588.0	w9_1  -3450.0 \
	   w10_0 -3519.0 w10_1  -3381.0 \
	   w11_0 -3450.0 w11_1  -3312.0 \
	   w12_0 -3381.0 w12_1  -3243.0 \
	   w13_0 -3312.0 w13_1  -3174.0 \
	   w14_0 -3243.0 w14_1  -3105.0 \
	   w15_0 -3174.0 w15_1  -3036.0 \
	   w16_0 -3105.0 w16_1  -2967.0 \
	   w17_0 -3036.0 w17_1  -2898.0 \
	   w18_0 -2967.0 w18_1  -2829.0 \
	   w19_0 -2898.0 w19_1  -2760.0 \
	   w20_0 -2829.0 w20_1  -2691.0 \
	   w21_0 -2760.0 w21_1  -2622.0 \
	   w22_0 -2691.0 w22_1  -2553.0 \
	   w23_0 -2622.0 w23_1  -2484.0 \
	   w24_0 -2553.0 w24_1  -2415.0 \
	   w25_0 -2484.0 w25_1  -2346.0 \
	   w26_0 -2415.0 w26_1  -2277.0 \
	   w27_0 -2346.0 w27_1  -2208.0 \
	   w28_0 -2277.0 w28_1  -2139.0 \
	   w29_0 -2208.0 w29_1  -2070.0 \
	   w30_0 -2139.0 w30_1  -2001.0 \
	   w31_0 -2070.0 w31_1  -1932.0 \

]

set low_1 [list \
	   w1_0 -2070.0	w1_1  -1932.0 \
	   w2_0 -2001.0	w2_1  -1863.0 \
	   w3_0 -1932.0	w3_1  -1794.0 \
	   w4_0 -1863.0	w4_1  -1725.0 \
	   w5_0 -1794.0	w5_1  -1656.0 \
	   w6_0 -1725.0	w6_1  -1587.0 \
	   w7_0 -1656.0	w7_1  -1518.0 \
	   w8_0 -1587.0	w8_1  -1449.0 \
	   w9_0 -1518.0	w9_1  -1380.0 \
	   w10_0 -1449.0 w10_1  -1311.0 \
	   w11_0 -1380.0 w11_1  -1242.0 \
	   w12_0 -1311.0 w12_1  -1173.0 \
	   w13_0 -1242.0 w13_1  -1104.0 \
	   w14_0 -1173.0 w14_1  -1035.0 \
	   w15_0 -1104.0 w15_1  -966.0 \
	   w16_0 -1035.0 w16_1  -897.0 \
	   w17_0 -966.0	w17_1  -828.0 \
	   w18_0 -897.0	w18_1  -759.0 \
	   w19_0 -828.0	w19_1  -690.0 \
	   w20_0 -759.0	w20_1  -621.0 \
	   w21_0 -690.0	w21_1  -552.0 \
	   w22_0 -621.0	w22_1  -483.0 \
	   w23_0 -552.0	w23_1  -414.0 \
	   w24_0 -483.0	w24_1  -345.0 \
	   w25_0 -414.0	w25_1  -276.0 \
	   w26_0 -345.0	w26_1  -207.0 \
	   w27_0 -276.0	w27_1  -138.0 \
	   w28_0 -207.0	w28_1  -69.0 \
	   w29_0 -138.0	w29_1  0.0 \
           w30_0 -69.0 w30_1  69.0 \
	   w31_0 0.0 w31_1  138.0 \
]

set high_1 [list \
	   w1_0 -138.0 w1_1  0.0 \
	   w2_0 -69.0 w2_1  69.0 \
	   w3_0 0.0 w3_1  138.0 \
	   w4_0 69.0 w4_1  207.0 \
	   w5_0 138.0 w5_1  276.0 \
	   w6_0 207.0 w6_1  345.0 \
	   w7_0 276.0 w7_1  414.0 \
	   w8_0 345.0 w8_1  483.0 \
	   w9_0 414.0 w9_1  552.0 \
	   w10_0 483.0 w10_1  621.0 \
	   w11_0 552.0 w11_1  690.0 \
	   w12_0 621.0 w12_1  759.0 \
	   w13_0 690.0 w13_1  828.0 \
	   w14_0 759.0 w14_1  897.0 \
	   w15_0 828.0 w15_1  966.0 \
	   w16_0 897.0 w16_1  1035.0 \
	   w17_0 966.0 w17_1  1104.0 \
	   w18_0 1035.0	w18_1  1173.0 \
	   w19_0 1104.0	w19_1  1242.0 \
	   w20_0 1173.0	w20_1  1311.0 \
	   w21_0 1242.0	w21_1  1380.0 \
	   w22_0 1311.0	w22_1  1449.0 \
	   w23_0 1380.0	w23_1  1518.0 \
	   w24_0 1449.0	w24_1  1587.0 \
	   w25_0 1518.0	w25_1  1656.0 \
	   w26_0 1587.0	w26_1  1725.0 \
	   w27_0 1656.0	w27_1  1794.0 \
	   w28_0 1725.0	w28_1  1863.0 \
	   w29_0 1794.0	w29_1  1932.0 \
	   w30_0 1863.0	w30_1  2001.0 \
	   w31_0 1932.0	w31_1  2070.0 \
]


set high_0 [list \
	   w1_0 1932.0	w1_1  2070.0  \
	   w2_0 2001.0	w2_1  2139.0  \
	   w3_0 2070.0	w3_1  2208.0  \
	   w4_0 2139.0	w4_1  2277.0  \
	   w5_0 2208.0	w5_1  2346.0  \
	   w6_0 2277.0	w6_1  2415.0  \
	   w7_0 2346.0	w7_1  2484.0  \
	   w8_0 2415.0	w8_1  2553.0  \
	   w9_0 2484.0	w9_1  2622.0  \
	   w10_0 2553.0	w10_1  2691.0  \
	   w11_0 2622.0	w11_1  2760.0  \
	   w12_0 2691.0	w12_1  2829.0  \
	   w13_0 2760.0	w13_1  2898.0  \
	   w14_0 2829.0	w14_1  2967.0  \
	   w15_0 2898.0	w15_1  3036.0  \
	   w16_0 2967.0	w16_1  3105.0  \
	   w17_0 3036.0	w17_1  3174.0  \
	   w18_0 3105.0	w18_1  3243.0  \
	   w19_0 3174.0	w19_1  3312.0  \
	   w20_0 3243.0	w20_1  3381.0  \
	   w21_0 3312.0	w21_1  3450.0  \
	   w22_0 3381.0	w22_1  3519.0  \
	   w23_0 3450.0	w23_1  3588.0  \
	   w24_0 3519.0	w24_1  3657.0  \
	   w25_0 3588.0	w25_1  3726.0  \
	   w26_0 3657.0	w26_1  3795.0  \
	   w27_0 3726.0	w27_1  3864.0  \
	   w28_0 3795.0	w28_1  3933.0  \
	   w29_0 3864.0	w29_1  4002.0  \
	   w30_0 3933.0	w30_1  4071.0 \
	   w31_0 4002.0	w31_1  4140.0  \
] 

    set freq 115000
    set dopvel 0
    set nlines 20
    set bwid 2208.0

#--------------
#Define Important buttons and frames
    frame .bandlist1
    frame .bandlist2
    frame .bandlist3
    frame .bandlist4
    frame .allbands
    frame .b1
    frame .b2
    frame .b3
    frame .b4
    frame .topstuff 
    frame .pmfreq
    frame .filestuff
    frame .bottomstuff
    frame .band_sel    
    toplevel .lines

button .quit_button -text "Exit!" -command exit -background pink
button .help_button -text "HELP!" -command help -background skyblue -width 20
button .calculate -text "Calculate All!" -command { calculate }  -background green -width 14
button .save -text "File Save" -command { save $fname}
button .restore -text "File Restore" -command { restore $fname ; calculate}
button .pl10 -text "+10 MHz" -command { set freq [expr $freq +10];calculate } -width 5
button .m10 -text "-10 MHz" -command { set freq [expr $freq -10];calculate } -width 5
entry .fname -text fname
grid .pl10 -in .pmfreq -column 1 -row 1
grid .m10 -in .pmfreq -column 2 -row 1

set bpos 69
radiobutton .bandmid -variable bpos -value 0 -text "Show window start freq"
radiobutton .bandstart -variable bpos -value 69 -text "Show window mid freq"
bind  .bandmid <ButtonRelease>  {calculate}
bind  .bandstart <ButtonRelease>  {calculate}
#-----------
#Define the rest of the gui band.
label .b1_lab1 -text "Band 1" -font {-size 7}
label .b1_lab2 -text "Band 2" -font {-size 7}
label .b1_lab3 -text "Band 3" -font {-size 7}
label .b1_lab4 -text "Band 4" -font {-size 7}
entry .freq_in -text freq -width 10
label .freq_lab -text "Central freq \[MHz\]"
#----------------
#Define the canvases.
set y 80
set x 150
set xmarg 6
set xmore [expr $x+$xmarg]
canvas .fplotb1 -height $y\m -width [expr $xmore]\m
canvas .fplotb2 -height $y\m -width [expr $xmore]\m
canvas .fplotb3 -height $y\m -width [expr $xmore]\m
canvas .fplotb4 -height $y\m -width [expr $xmore]\m
#----------------
#Make the optional frequency lines.
    
set cnt 1
set thisline 1
set maxentries 5
set colnum  1

button .lines.freq_clear -text "Clear Freqs" -background pink -command {
	set cnt 1
	while {$cnt <= $nlines} {
		set f$cnt ""
		set f$cnt\name ""
		set f$cnt\dop ""
		.lines.frqlist$cnt configure -text (Select)
		incr cnt
		}

}
while {$cnt <= $nlines} {
set "cb$cnt" 1

menubutton .lines.frqlist$cnt -text (Select) -menu .lines.frqlist$cnt\.menu_f$cnt -width 6
menu .lines.frqlist$cnt\.menu_f$cnt
    
	.lines.frqlist$cnt\.menu_f$cnt add command -label 12CO -command \
		"set f$cnt 115271.202;\
		.lines.frqlist$cnt configure -text 12CO;\
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label C17O -command \
		"set f$cnt 112358.988;\
		.lines.frqlist$cnt configure -text C17O;\
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label 13CO  -command \
		"set f$cnt 110201.353;\
		.lines.frqlist$cnt configure -text 13CO;\
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label HCCCN  -command \
		"set f$cnt 100076.385;\
		.lines.frqlist$cnt configure -text HCCCN; \
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label CH3OH  -command \
		"set f$cnt 84521.206;\
		.lines.frqlist$cnt configure -text CH3OH;\
		 calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label H13CN  -command \
		"set f$cnt 86340.167; \
		.lines.frqlist$cnt configure -text H13CN; \
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label C18O  -command \
		"set f$cnt 109782.173; \
		.lines.frqlist$cnt configure -text C18O; \
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label SO2  -command \
		"set f$cnt 109757.587; \
		.lines.frqlist$cnt configure -text SO2; \
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label CS -command \
		"set f$cnt 97980.953; \
		.lines.frqlist$cnt configure -text CS; \
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label N2H+ -command \
		"set f$cnt 93173.480; \
		.lines.frqlist$cnt configure -text N2H+; \
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label HNC -command \
		"set f$cnt 90663.572; \
		.lines.frqlist$cnt configure -text HNC; \
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label HCO+  -command \
		"set f$cnt 89188.526; \
		.lines.frqlist$cnt configure -text HCO+; \
		calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label HCN  -command \
		"set f$cnt 88631.8473; \
		.lines.frqlist$cnt configure -text HCN;\
		 calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label H13CO+ -command \
		"set f$cnt 86754.330; \
		.lines.frqlist$cnt configure -text H13CO+;\
		 calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label SiO -command \
		"set f$cnt 86243.440; \
		.lines.frqlist$cnt configure -text SiO;\
		 calculate"
	.lines.frqlist$cnt\.menu_f$cnt add command -label (Select) -command \
		".lines.frqlist$cnt configure -text (Select) -width 7;\
		set f$cnt \"\" ; set f$cnt\dop \"\";\
		 calculate"
	checkbutton .lines.f_cb$cnt -variable "cb$cnt" -width 1

	bind .lines.f_cb$cnt <ButtonRelease>  {calculate}
	label .lines.f$cnt\l -text "F $cnt" -foreground blue -width 4
    	label .lines.f0lab$cnt -text "f"
	entry .lines.f$cnt -width 8 -text f$cnt
	label .lines.f1lab$cnt -text "f\'"
	entry .lines.f$cnt\dop -width 8 -text f$cnt\dop
	grid .lines.f$cnt\l -in .lines -column $colnum -row $thisline -sticky w
	grid .lines.f_cb$cnt -in .lines -column [expr $colnum+2] -row $thisline
	grid .lines.frqlist$cnt -in .lines -column [expr $colnum+3] -row $thisline -sticky w
	grid .lines.f0lab$cnt -in .lines -column [expr $colnum+4] -row $thisline
	grid .lines.f$cnt -in .lines -column [expr $colnum+5] -row $thisline -sticky w
	grid .lines.f1lab$cnt -in .lines -column [expr $colnum+6] -row $thisline
	grid .lines.f$cnt\dop -in .lines -column [expr $colnum+7] -row $thisline
	incr cnt
	incr thisline
	if {$thisline > $maxentries} {
		set thisline 1
		label .lines.dum$cnt -width 1
		grid .lines.dum$cnt -in .lines -column [expr $colnum+8] -row $thisline
		set colnum [expr $colnum+9]
		}

	}




incr thisline
grid .lines.freq_clear -in .lines -column 1 -row [expr $maxentries+1]  -columnspan 5
#-----------
label .dum1 -width 20
label .dopvellab1 -text "Velocity (Topo)  \[km/s\]"
entry .dopvel -text dopvel -width 10
#--------------
#Grid it all up into frames.
    
#--Topstuff
    grid .freq_lab -in .topstuff -row 1 -column 1 -sticky w
    grid .freq_in -in .topstuff -row 1 -column 2 -sticky w
    grid .calculate -in .topstuff -row 2 -column 3 -sticky w
    grid .pmfreq -in .topstuff -row 1 -column 3 -sticky w
    grid .help_button -in .topstuff -row 2 -column 4 -sticky w
    grid .dopvellab1 -in .topstuff   -row 2 -column 1 -sticky w
    grid .dopvel -in .topstuff   -row 2 -column 2  -sticky w
    label .dum2 -width 5
    grid .dum2 -in .topstuff -row 1 -column 7 -sticky w
        
##--The band canvas and band list
    grid .bandlist1 -in .b1 -column 1 -row 1 -rowspan 2
    grid .fplotb1 -in .b1 -column 2 -row 2
    grid .bandlist2 -in .b2 -column 2 -row 1 -rowspan 2
    grid .fplotb2 -in .b2 -column 1 -row 2
    grid .bandlist3 -in .b3 -column 1 -row 1 -rowspan 2
    grid .fplotb3 -in .b3 -column 2 -row 2
    grid .bandlist4 -in .b4 -column 2 -row 1 -rowspan 2
    grid .fplotb4 -in .b4 -column 1 -row 2

    grid .b1 -in .allbands -row 1 -column 1
    grid .b2 -in .allbands -row 1 -column 2
    grid .b3 -in .allbands -row 2 -column 1
    grid .b4 -in .allbands -row 2 -column 2
#------------------
    grid .restore -in .filestuff -row 1 -column 1
    grid .fname -in .filestuff -row 1 -column 2
    grid .save -in .filestuff -row 1 -column 3
#------------------
    grid .bandmid -in .band_sel -row 1 -sticky w
    grid .bandstart -in .band_sel -row 2 -sticky w
#------------------
#Grid up the frames and important buttons
    grid .topstuff -in . -row 1 -column 1 -sticky w
    grid .allbands -in . -row 2 -column 1 -columnspan 5  -sticky w
    grid .filestuff -in . -row 1 -column 2 -sticky e
    grid .band_sel -in .topstuff -row 1 -column 4 -sticky w
    grid .bottomstuff -in . -row 6 -column 1 -sticky w
    grid .quit_button -in . -row 1 -column 3 -sticky e
#--------
#Grid up the canvas
    
    wm title . "Mops_vis v $ver"
    wm title .lines "Frequencies"

    calculate
    
#---------------------------------------------------------------------
# Other functions/processes follow
#---------------------------------------------------------------------
    
#----------------------------RESTORE INPUTS
proc restore {fname} {
	set fnameid [open $fname]
	global freq dopvel nlines
	gets $fnameid freq
	gets $fnameid dopvel
			
	set cnt 1
	set newval ""
	while {$newval != "end"} {
		global f$cnt
		gets $fnameid newval
		if {$newval!="end"} {set f$cnt $newval }
		incr cnt
		}
	set cnt 1
	while {$cnt <= $nlines} {
		gets $fnameid inp
		if { $inp == "" } { set inp (Select) }
		.lines.frqlist$cnt configure -text $inp
		incr cnt
		}
	close $fnameid
}



#----------------------------SAVE INPUTS
proc save {fname} {
set dont_write 0


if {[file exists $fname]} {
	set answer \
		[tk_messageBox -message "Oops!,\n $fname already exists!\n Do \
		you want to write\n over the existing file?"  -type okcancel]
	case $answer {
	    cancel {set dont_write 1}
	    }
}

if { $dont_write != 1 } { 
	set fnameid [open $fname w 0600]
	global freq nlines; puts $fnameid $freq 
	global dopvel; puts $fnameid $dopvel
	set cnt 1
	while {$cnt <= $nlines} {
		upvar 1 f$cnt freq_1
		global $freq_1
		puts $fnameid $freq_1
		incr cnt
		}
	puts $fnameid "end"
		set cnt 1
		while {$cnt <= $nlines} {
			puts $fnameid [.lines.frqlist$cnt cget -text]
 			incr cnt
			}
	tk_messageBox -message "$fname saved."  -type ok

	close $fnameid
    }
}

proc help {} {
global ver
    set helpmess \
"The new, super-duper Mops_vis $ver is designed to help optimise frequency and window 
selections for the Zoom mode on MOPS. It enables efficient optimisation of the operation for the 
MOPS narrowband multi-window mode, by visually displaying the positions of up to 20 molecular 
transitions,relative to a selected central tuning frequency.

Ver. $ver is in beta. Note that files from Mopsvis ver 2 are not compatable, this is because it 
will take more time for me to fix it. than it will for you to re-enter the values yourself.

Mops provides access to 8 GHz total simultaneous band, spanned by 4x2GHz bands, extending +/- 4
GHz centred on the user-specified  freqency in 'Central Freq \[MHz\]'. 

The 'Calculate!' button must be pressed after a new central Frequency is entered.

Each band has provision for up to 4 x 138 MHz 'windows', which may be positioned at any of the 
available 31 interleaved options (shown as blue bars). Modifying the central freq by +/- 10 
MHz is affected by clicking on '+10' or '-10', allowing the user to easily locate an optimal 
central freqency.

The window labelled 'Frequencies' enables the user to choose up to 20 preset, or user defined 
frequencies, which will be plotted on the display, in the appropriate band. The frequencies for 
transitions of interest can be entered into the entry fields at the bottom of the gui, 
Input lines can be selected or deselected by clicking on the checkbutton. 

A Systemic velocity can be entered into 'Calculate doppler.....'

Make a note of the windows which intersect these bands, for use in the schedule files.

Setups can be saved or loaded with 'File Save' and 'File Restore'"
    
    
toplevel .helppop 
grab .helppop
wm title .helppop "Help!"
message .helppop.msg -relief raised -bd 2 -text $helpmess -width 20c
button .helppop.okb -text OK -command {destroy .helppop ; return 0} -background green
pack .helppop.msg .helppop.okb -side top 
}


