::catch  {};#\
@echo off
::catch {};#\
@"%prop_dir%\expect-5.21\bin\wish80.exe" %0 %1 %2
::catch {};#\
@goto eof
puts "argv0='argv0' argc=$argc argv='$argv' pwd='[pwd]'"
#!/usr/local/bin/expectk -f
#Splots Tk package for viewing data from small angle instruments
#Requires splots  from splots.f splotf.f + librlib.a libpgplot.a
#and monbug and pdqheads
#S.Fayers v1.1 14/11/95
#
#S. Fayers v1.2 24/11/95
# Buttons added to load default colours or greyscales.
#S. Fayers v1.21 27/3/96
# minor changes to functionality
#R. Ghosh April 1997
# minor edits to assume all spawned files are in $env(SAS_DIR)/
# Ch. Lacaille-Desse/R. Ghosh  version 2  June 1997
# normalisation options/treated data/windows added
#R. Ghosh October 1999
# reading compressed files added...
# help file corrected for WINDOWS December 2006 (Ron)
#May 2008 R.G.  mods for NEXT/PREVIOUS and deal with different terminators
#September 2010 R.G. mods to deal with mixed files in raw directory (v2.1)
global button

catch { package require expect } resc1
catch { package require Expect } resc2
# don't know which spelling is available per se


#----------LACAILLE----------------PROCEDURE NORMALIZE---------------------
proc normaliz { name } {
global therun theext norm
set therun 0 ; set theext 0 ;set norm x
regsub -all "\r" $name " " name
set s [lsearch $name "Normalisation"]
if { $s > 0 }   {
	    set s [expr $s +4] 
	    set therun [lindex $name $s]
	    set s [expr $s +2] 
	    set theext [lindex $name $s]
		}
toplevel .normalize ; wm geometry .normalize +490+290
frame .normalize.f 
pack .normalize.f 
frame .normalize.f1 

pack .normalize.f1 -in .normalize.f -side top
frame .normalize.f2 
pack .normalize.f2 -in .normalize.f -side top

label .normalize.normrun -text "Run Number ?" 
entry .normalize.runent -width 6 -textvariable therun
label .normalize.runext -text "Extension ?" 
entry .normalize.runextent -width 3 -textvariable theext
button .normalize.runaplly -text Apply  -width 15 \
    -command {
    exp_send "inn\r"
    expect "...EXT"
    .normalize.normnew configure -text "Normalize ?"
    .normalize.chknormy configure -state normal
    .normalize.chknormn configure -state normal
    set norm y
    set therun [.normalize.runent get]
    set theext [.normalize.runextent get]
    exp_send " $therun $theext\r"
      expect "Option, or Help" 
    regsub -all "\r" $expect_out(buffer) " " name
        if {[lsearch $name deviation] <0 } {
	    set therun 0 ; set theext 0
	    .normalize.normnew configure -text ""
	    .normalize.chknormy configure -state disabled
	    .normalize.chknormn configure -state disabled
	set returnval [tk_dialog .d {data not found} {No normalisation possible} error 0 \
	{Dismiss}]

#tk_dialog .d {Error Encountered} "An error occured whilst reading:\n\n
# $therun $theext" {ok}	    
	    expect "Option, or Help" 
				           } 
      	     }          

label .normalize.normnew -text "Normalize ?" 
radiobutton .normalize.chknormy  -text YES  -variable norm -value y \
 -command { set norm y ; exp_send "+nd\r" ; expect "Option, or Help"}
radiobutton .normalize.chknormn  -text NO  -variable norm -value n \
 -command { set norm n ;exp_send "~nd\r" ; expect "Option, or Help"}

button .normalize.dism -text Dismiss -width 15  -command {destroy .normalize}

pack .normalize.normnew .normalize.chknormy .normalize.chknormn \
-in .normalize.f -side left -expand 1 -fill both

pack .normalize.normrun .normalize.runent .normalize.runext \
.normalize.runextent -in .normalize.f1 -side left -expand 1 -fill both


pack  .normalize.runaplly .normalize.dism -in .normalize.f2 -side left -expand 1 -fill both

focus .normalize
grab .normalize
.normalize.normnew configure -text "" 

if {$therun == 0 } {
             .normalize.chknormy configure -state disabled
             .normalize.chknormn configure -state disabled
	           } else {
.normalize.chknormn configure -state normal
.normalize.chknormy configure -state normal
                          }

}
#----------------END NORMALIZE---------------------

#-------LACAILLE----------------PROCEDURE SETWIN---------------------
proc setwin {  } {
global winx0 winx1 winy0 winy1 limx0 limx1 limy0 limy1 lrawls
exp_send "v\r"
expect "Option, or Help"	     
regsub -all "\r" $expect_out(buffer) "" name
set winx0 [lindex $name 21]  ;set limx0 [lindex $name 24] ; set limx0 [expr $winx0 ]
set winx1 [lindex $name 22]  ;set limx1 [lindex $name 26] ; set limx1 [expr $limx1 ]  
set winy0 [lindex $name 29]  ;set limy0 [lindex $name 32] ; set limy0 [expr $limy0 ] 
set winy1 [lindex $name 30]  ;set limy1 [lindex $name 34] ; set limy1 [expr $limy1 ]   
		    
toplevel .set_window ; wm geometry .set_window +490+290
frame .set_window.f ;pack .set_window.f  
frame .set_window.f1 ;pack .set_window.f1 
frame .set_window.f2 ;pack .set_window.f2 


label .set_window.setw -text "WINDOW SIZE  from X:"
entry .set_window.entx0 -width 5 -textvariable winx0
label .set_window.setx1 -text "to X:"
entry .set_window.entx1 -width 5 -textvariable winx1
label .set_window.sety0 -text "WINDOW SIZE  from Y:"
entry .set_window.enty0 -width 5 -textvariable winy0
label .set_window.sety1 -text "to Y:"
entry .set_window.enty1 -width 5 -textvariable winy1

button .set_window.wind -text "SET WINDOW"  -command {
	    set winx0 [.set_window.entx0 get] ; set winx0 [expr $winx0 ]
	    set winx1 [.set_window.entx1 get] ; set winx1 [expr $winx1 ]
	    set winy0 [.set_window.enty0 get] ; set winy0 [expr $winy0 ]
	    set winy1 [.set_window.enty1 get] ; set winy1 [expr $winy1 ]
	    if { $winx0 <= 0 || $winx0 >= $limx1 } { set winx0 $limx0 }
	    if { $winx1 <= $winx0 || $winx1 >= $limx1 } { set winx1 $limx1 }
	    if { $winy0 <= 0 || $winy0 >= $limy1 } { set winy0 $limy0 }
	    if { $winy1 <= $winy0  || $winy1 >= $limy1 } { set winy1 $limy1 }
	    exp_send "w\r"
	    exp_send "$winx0 $winx1 $winy0 $winy1\r"
	    expect "Option, or Help"	     
		                                      }
		                                     
button .set_window.windf -text "FULL WINDOW" -command {
	    exp_send "w\r"
	    exp_send "\r"
	    expect "Option, or Help"
	    regsub -all "\r" $expect_out(buffer) "" name
	    set winx0 $limx0
	    set winx1 $limx1
	    set winy0 $limy0
	    set winy1 $limy1
		                                      }

button .set_window.dism -text "Dismiss" -command {destroy .set_window}

pack .set_window.setw .set_window.entx0  .set_window.setx1 .set_window.entx1 -in .set_window.f -side left \
-expand 1 -fill both
pack  .set_window.sety0 \
.set_window.enty0 .set_window.sety1 .set_window.enty1   -in .set_window.f1 -side left -expand 1 -fill both
pack .set_window.wind .set_window.windf \
.set_window.dism -in .set_window.f2 -side left -expand 1 -fill both
focus .set_window
grab .set_window

}
#----------------END SETWIN---------------------


# dialog.tcl --
#
# This file defines the procedure tk_dialog, which creates a dialog
# box containing a bitmap, a message, and one or more buttons.
#
# $Header: /user6/ouster/wish/library/RCS/dialog.tcl,v 1.4 93/08/16 16:59:52 ouster Exp $ SPRITE (Berkeley)
#
# Copyright (c) 1992-1993 The Regents of the University of California.
# All rights reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in
# all copies of this software.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
# CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#

#
# tk_dialog:
#
# This procedure displays a dialog box, waits for a button in the dialog
# to be invoked, then returns the index of the selected button.
#
# Arguments:
# w -		Window to use for dialog top-level.
# title -	Title to display in dialog's decorative frame.
# text -	Message to display in dialog.
# bitmap -	Bitmap to display in dialog (empty string means none).
# default -	Index of button that is to display the default ring
#		(-1 means none).
# args -	One or more strings to display in buttons across the
#		bottom of the dialog box.
proc tk_dialog {w title text bitmap default args} {


    global tk_priv

    # 1. Create the top-level window and divide it into top
    # and bottom parts.

    catch {destroy $w}
    toplevel $w -class Dialog 
    wm title $w $title
    wm iconname $w Dialog
    frame $w.top -relief raised -bd 1
    pack $w.top -side top -fill both
    frame $w.bot -relief raised -bd 1
    pack $w.bot -side bottom -fill both

    # 2. Fill the top part with bitmap and message.

    message $w.msg -width 3i -text $text \
	    -font -Adobe-Times-Medium-R-Normal-*-180-*
    pack $w.msg -in $w.top -side right -expand 1 -fill both -padx 5m -pady 5m
    if {$bitmap != ""} {
	label $w.bitmap -bitmap $bitmap
	pack $w.bitmap -in $w.top -side left -padx 5m -pady 5m
    }

    # 3. Create a row of buttons at the bottom of the dialog.

    set i 0
    foreach but $args {
	button $w.button$i -text $but -command "set tk_priv(button) $i"
	if {$i == $default} {
	    frame $w.default -relief sunken -bd 1
	    raise $w.button$i $w.default
	    pack $w.default -in $w.bot -side left -expand 1 -padx 3m -pady 2m
	    pack $w.button$i -in $w.default -padx 2m -pady 2m \
		    -ipadx 2m -ipady 1m
	    bind $w <Return> "$w.button$i flash; set tk_priv(button) $i"
	} else {
	    pack $w.button$i -in $w.bot -side left -expand 1 \
		    -padx 3m -pady 3m -ipadx 2m -ipady 1m
	}
	incr i
    }

    # 4. Withdraw the window, then update all the geometry information
    # so we know how big it wants to be, then center the window in the
    # display and de-iconify it.

    wm withdraw $w
    update idletasks
    set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
	    - [winfo vrootx [winfo parent $w]]]
    set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
	    - [winfo vrooty [winfo parent $w]]]
	    set y [expr $y +100]
    wm geometry $w +$x+$y
    wm deiconify $w

    # 5. Set a grab and claim the focus too.

    set oldFocus [focus]
    grab $w
    focus $w

    # 6. Wait for the user to respond, then restore the focus and
    # return the index of the selected button.

    tkwait variable tk_priv(button)
    destroy $w
    focus $oldFocus
    return $tk_priv(button)
}
# -----------------------------------------------------------------------
# execute loguse and program name
# -----------------------------------------------------------------------
set version 2.1
catch {exec $env(SAS_DIR)/monbug XPLT $version } monfil
# -----------------------------------------------------------------------
# store current directory
set currentdirectory "./"
# -----------------------------------------------------------------------
#create the frames to put the buttons in

wm title . "Xplots v$version     S.Fayers & Ch.Lacaille-Desse"
frame .left -bd 4 -relief groove
frame .right
frame .top -bd 4 -relief groove -height 1c
frame .bottom 
frame .bl  -bd 4 -relief groove
frame .br -bd 4 -relief groove
frame .bml -bd 4 -relief groove
frame .bmr -bd 4 -relief groove

#and pack them in a logical order
pack .left .right -side left -expand 1 -fill both
pack  .top  -in .right  -side top -fill x
pack .bottom -in .right -side top -expand 1 -fill both
pack .bl .bml .bmr .br -in .bottom  -expand 1 -fill both -side left

# -----------------------------------------------------------------------
# trace the output from splots.f for "Run Not Set"  and create a dialog
# box to warn the users of the situation
trace variable expect_out(buffer) w {

	set testvar $expect_out(buffer)
	if {[lsearch $testvar Run] >=0} {
	if {[lsearch $testvar Not] >=0} {
	if {[lsearch $testvar Set] >=0} {
	set returnval [tk_dialog .d {Run Numbers Not Set} {Either you haven't set the\
	run numbers, or the run set does not exist} error 0 {(Re)Set Run Number}\
	{Dismiss}]
	if {$returnval == 0} {focus .first}}
}}}

global selectdata run1 run2 comment norm
set selectdata r ;set comment ""
	
# -----------------------------------------------------------------------
#first and last run boxes with annotation
entry .first -width 6 -relief flat -textvariable first
entry .last -width 6 -relief flat -textvariable last
label .firstno -text "First Run  "
label .lastno -text "Last Run  "
# these are now not packed, but are still available
# pack .firstno .first .lastno .last -in .top -expand 1 -fill both -side left

# -----------------------------------------------------------------------
# create a change runs button, with appropriate functionality
button .changeruns -text "Change Run Range" -command {
# remove current selected runs from list
#.slctruns delete 0 [.slctruns index end]
# create a new window to select runs from
set runsindex [.slctruns index end]
toplevel .setruns ; wm geometry .setruns +500+70
wm title .setruns "Xplots Run Selector"
# ensure that the new window is in focus
focus .setruns
grab .setruns
frame .setruns.a 
frame .setruns.a1 
frame .setruns.a2 

frame .setruns.b
pack .setruns.a  .setruns.a1 .setruns.a2  .setruns.b -in .setruns 
set datapath $env(SAS_DATA_PATH)
# for Mac OSX setenv SAS_DECOMPRESS gunzip -c
# for Linux   setenv SAS_DECOMPRESS zcat
# for PC      set    SAS_DECOMPRESS=zcat
set zcat "nodecompress"
if [ catch { set zcat $env(SAS_DECOMPRESS) }  result ] {
                   puts stderr result
}

listbox .setruns.files -relief raised -width 35 -bd 2 -yscrollcommand ".setruns.scroll set"
scrollbar .setruns.scroll -command ".setruns.files yview"
pack .setruns.scroll -in .setruns.b -side right -fill y
# change to the current SAS data path and read the files, inserting them into the list

	    set mydir "./"

if {$selectdata == "r"} {
            .setruns.files delete 0 end 
            cd $datapath
# don't sort raw data - assume in sequential order
            foreach i  [glob -nocomplain \[0\-9\]?????] {
	    .setruns.files insert end $i 
                                                        }
            if {$zcat != "nodecompress" } {
# don't sort raw data - assume in sequential order
            foreach i  [glob -nocomplain \[0\-9\]?????.Z] {
            regexp {[0-9].....} $i ffff


	    .setruns.files insert end $ffff
                                                                }
            foreach i  [glob -nocomplain \[0\-9\]?????.gz] {
            regexp {[0-9].....} $i ffff


	    .setruns.files insert end $ffff
                                                                }
                                   }
                        }
if {$selectdata == "t"} {
            .setruns.files delete 0 end 
            foreach i [lsort [glob -nocomplain t\[0\-9\]?????.???] ] {
	    set i [string range $i 1 10] 
	     set i1 [string range $i 0 5] 
	     set i2 [string range $i 7 9]  
	   set i [concat $i1 $i2]
            .setruns.files insert end $i
	                                                      }
  
                        }

# place end of selector window viewable
.setruns.files see end


pack .setruns.files -in .setruns.b -expand 1 -side left -fill both
.setruns.files configure -height 25 -selectmode extended
label .setruns.info -text "The following runs are available. \
Select the ones of interest and click OK" -wraplength 320

set lraw [string toupper $env(SAS_INSTRUMENT)] 
set braw [concat "RAW data for SAS_INSTRUMENT :" $lraw]
radiobutton .setruns.r1 -text $braw  -variable selectdata   -anchor w -value r\
 -command { 
            set selectdata r
            .setruns.files delete 0 end
	    set mydir "./"
            cd $datapath
# don't sort raw data - assume in sequential order
            foreach i  [glob -nocomplain \[0\-9\]?????] {
	    .setruns.files insert end [concat $i 0] 
	                                                       }
            if { $zcat != "nodecompress" } {
# don't sort raw data - assume in sequential order
            foreach i [glob -nocomplain \[0\-9\]?????.*] {
            regexp {[0-9].....} $i ffff

	    .setruns.files insert end [concat $ffff 0]
                                                        }        
                                    }
	    cd $mydir						       

          }
radiobutton .setruns.r2 -text "TREATED data"   -anchor w -variable selectdata -value t \
 -command { 
            set selectdata t
            .setruns.files delete 0 end
            foreach i [lsort [glob -nocomplain t\[0\-9\]?????.???] ]  {
	    set i [string range $i 1 10] 
	    set i1 [string range $i 0 5]
	    set i2 [string range $i 7 9] 
	    set i [concat $i1 $i2]
            .setruns.files insert end $i
	                                                       }
          }

button .setruns.dismiss -text Dismiss -command {destroy .setruns} 


button .setruns.ook -text OK -width 25 -command {
set oneval [.setruns.files get anchor]  
set twoval [.setruns.files get active] 
# get list of highlighted runs
set runlist {}
foreach item [.setruns.files curselection] {
set runlist [linsert $runlist end [.setruns.files get $item]]
}

if {$runlist == ""} {return} ; #SI LA LISTE EST VIDE...

# destroy all the children of the setruns window and the window itself
eval destroy [winfo children .setruns]
destroy .setruns

update idletasks
cd $currentdirectory
# mark end of current run select list
set startsec [ .slctruns index end ]
incr startsec -1
# because we're going to give a next command for plotting
# now add to runlist with numor/ext/title for all runs selected
foreach run $runlist {

set run1 [lindex $run 0 ] ; set run2 [lindex $run 1]
set currenthead [exec $env(SAS_DIR)/pdqheads $run1 $run2]
# if pdqheads fails to open file and find title => ...ERROR..
if {[string first "ERROR" $currenthead] == -1} {
.slctruns insert end $currenthead
# the menu for data selection is a list of "$currenthead"s
# which is analysed to find run and ext number for retrieval
update idletasks
.slctruns see end
} else {
tk_dialog .d {Error Encountered} "An error occured whilst reading:\n\n $run\
\n \nThis run is not available to view. 
Data may not be from $lraw"\
 warning 0 { OK }
}}
# plot first run in new or added selection
set runsindex $startsec
.next invoke
# first time round set sqrt scales...
if {$temp98 == 0} {.rsqr invoke; set temp98 1}
}

pack .setruns.info    -in .setruns.a -side left

pack .setruns.r1 .setruns.r2 -in .setruns.a1 -side top -expand 1 -fill both
pack .setruns.ook .setruns.dismiss  -in .setruns.a2 -side left
}
pack .changeruns -in .top -ipady 2m -side left -expand 1 -fill both


frame .a
frame .b
pack .a .b -side top -in .left -expand 1 -fill both

listbox .slctruns -height 12 -selectmode single -yscrollcommand \
 ".slctscroll set"
	    scrollbar .slctscroll -command ".slctruns yview"
label .lab -text "Select a run by double-clicking with the left mouse \
button \n The next and back buttons in the main window will select the\
next and previous runs in this list" -wraplength 300
pack .lab -in .a
pack .slctscroll -in .b -side right -fill y -expand 1 
pack .slctruns -in .b -side left -expand 1 -fill x
#
# Double click in selector panel
#
bind .slctruns <Double-1> {
set runsindex [.slctruns index active]
set numor [lindex [.slctruns get active] 0]
# numor is in format nnnnnn/mmm 
         set run1_1 [string range $numor 0 5]
	 set run2_1 [string range $numor 7 9] 
	 if {$locked == 1} {
	 exp_send "n\r"
	 } else {
	 exp_send "NA\r"
	                   }
	 expect "*new run number*"
	 exp_send "$run1_1 $run2_1 0\r"
	 expect "*Option*" 
         if {$itype == 3} {set itype 0}
         exp_send "V\r"
         expect "*Option*" {
		if {$locked ==0} {
		set bottomlimit [lindex $expect_out(buffer) 15]
		set toplimit [lindex $expect_out(buffer) 18]}
	                         }
.slctruns selection anchor end
.slctruns see end 
#*********************************************************
}

# -----------------------------------------------------------------------
#create back, next and help buttons
set runsindex 0
# first time around...
button .next -text NEXT -command {
# increase the run number by one

##incr runsindex
##if {$runsindex > 0} {.slctruns selection clear [expr $runsindex -1]}
##if {$runsindex > [.slctruns index end]} {set runsindex [.slctruns index end]}
##.slctruns selection set $runsindex
set hhhh [.slctruns index end ]
incr hhhh -1
####### don't ask why PC-tk returns (end+1) ########
#set yyyy [.slctruns get $hhhh $hhhh ]
#dblocate " $runsindex  and end at $hhhh with ' $yyyy' "
.slctruns selection clear $runsindex
if {$runsindex < $hhhh } {incr runsindex }
.slctruns selection set $runsindex
#puts [ .slctruns curselection ]
.slctruns see [.slctruns curselection]
set numor [lindex [.slctruns get [.slctruns curselection]] 0]
         set run1_1 [string range $numor 0 5]
	 set run2_1 [string range $numor 7 9]
# numor is in format nnnnnn/mmm 
# send info to splots.f and get back data limits
	 if {$locked == 1} {
	 exp_send "n\r"
	 } else {
	 exp_send "NA\r"
	 }
	 expect "*new run number*"
	 exp_send "$run1_1 $run2_1 0\r"
	 expect "*Option*" 
        if {$itype == 3} {set itype 0}
        exp_send "V\r"
        expect "*Option*" {
		if {$locked ==0} {
		set bottomlimit [lindex $expect_out(buffer) 15]
		set toplimit [lindex $expect_out(buffer) 18]}
	}
#.slctruns see end
}
button .back -text PREVIOUS -command {
# decreases the run number by one


##incr runsindex -1
##if {$runsindex <0} {set runsindex 0}
##.slctruns selection clear [expr $runsindex +1]
##.slctruns selection set $runsindex

.slctruns selection clear $runsindex
if {$runsindex > 0 } {  incr runsindex -1 }
.slctruns selection set $runsindex

.slctruns see [.slctruns curselection]
set numor [lindex [.slctruns get [.slctruns curselection]] 0]
# numor is in format nnnnnn/mmm 
         set run1_1 [string range $numor 0 5]
	 set run2_1 [string range $numor 7 9] 
# send info to splots.f and get back data limits
	 if {$locked == 1} {
	 exp_send "n\r"
	 } else {
	 exp_send "NA\r"
	 }
	 expect "*new run number*"
	 exp_send "$run1_1 $run2_1 0\r"
	 expect "*Option*" 
        if {$itype == 3} {set itype 0}
        exp_send "V\r"
        expect "*Option*" {
		if {$locked ==0} {
		set bottomlimit [lindex $expect_out(buffer) 15]
		set toplimit [lindex $expect_out(buffer) 18]}
	}
}

button .shelp -text HELP!! -command {
set opwd [pwd]
cd $env(SAS_DIR)
exec htmltk.bat xplotshelp/xplots.htm &
cd $opwd
#exec $env(SAS_DIR)/htmltk.bat d:/SANS2006/sasprogdir/xplotshelp/xplots.htm
}



pack .back .next .shelp -in .top -ipady 2m -side left -expand 1 -fill both  
# -----------------------------------------------------------------------
#create buttons to select no per page, save, load cols and colour file
button .six -text "The `Six-Pack'"  \
 -anchor w -command {
	exp_send "SIX\r"
	expect "*Option*"
	}


button .storeops -text "Store/Delete/Retrieve" -anchor w -command {
exp_send "SIX\r"
expect "*Option*"
#create a new window
toplevel .storef ; wm geometry .storef +280+390 
wm title .storef "Xplots Store"
frame .storef.sbr
frame .storef.sbl
frame .storef.stl
frame .storef.str
frame .storef.stop
frame .storef.sbottom
pack .storef.stl .storef.str -in .storef.stop -side left -expand 1 -fill both
pack .storef.sbl .storef.sbr -in .storef.sbottom -side left -expand 1 -fill both
pack .storef.stop .storef.sbottom -in .storef -side left -expand 1 -fill both
# button to get rid of window when finished
button .storef.dismiss -text Dismiss -command {
focus . 
eval destroy [winfo children .storef]
destroy .storef
}
button .storef.da -text "Delete All" -command {
exp_send "DA\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
}
pack .storef.dismiss .storef.da -in .storef.sbr -expand 1 -fill both

#create the frames and buttons to store the currnt data
frame .storef.st1
frame .storef.st2
frame .storef.st3

button .storef.1 -text "Store 1" -anchor w -command {
exp_send "S1\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"

set messagetext "Stored"
}
button .storef.2 -text "Store 2" -anchor w -command {
exp_send "S2\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Stored"
}
button .storef.3 -text "Store 3" -anchor w -command {
exp_send "S3\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Stored"
}
button .storef.4 -text "Store 4" -anchor w -command {
exp_send "S4\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Stored"
}
button .storef.5 -text "Store 5" -anchor w -command {
exp_send "S5\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Stored"
}
button .storef.6 -text "Store 6" -anchor w -command {
exp_send "S6\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Stored"
}
pack .storef.st1 .storef.st2 .storef.st3 -in .storef.stl -expand 1 -fill both
pack .storef.1 .storef.2 -in .storef.st1 -side left -expand 1 -fill both
pack .storef.3 .storef.4 -in .storef.st2 -side left -expand 1 -fill both
pack .storef.5 .storef.6 -in .storef.st3 -side left -expand 1 -fill both

# create the buttons to delete data from the store
frame .storef.de1
frame .storef.de2
frame .storef.de3

button .storef.d1 -text "Delete 1" -anchor w -command {
exp_send "D1\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Deleted"
}
button .storef.d2 -text "Delete 2" -anchor w -command {
exp_send "D2\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Deleted"
}
button .storef.d3 -text "Delete 3" -anchor w -command {
exp_send "D3\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Deleted"
}
button .storef.d4 -text "Delete 4" -anchor w -command {
exp_send "D4\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Deleted"
}
button .storef.d5 -text "Delete 5" -anchor w -command {
exp_send "D5\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Deleted"
}
button .storef.d6 -text "Delete 6" -anchor w -command {
exp_send "D6\r"
expect "*Option*"
exp_send "SIX\r"
expect "*Option*"
set messagetext "Deleted"
}
pack .storef.de1 .storef.de2 .storef.de3 -in .storef.sbl -expand 1 -fill both
pack .storef.d1 .storef.d2 -in .storef.de1 -side left -expand 1 -fill both
pack .storef.d3 .storef.d4 -in .storef.de2 -side left -expand 1 -fill both
pack .storef.d5 .storef.d6 -in .storef.de3 -side left -expand 1 -fill both

# create buttons to reload frames
frame .storef.re1
frame .storef.re2
frame .storef.re3

button .storef.r1 -text "Reload 1" -anchor w -command {
exp_send "R1\r"
expect "*Option*"
set messagetext "Reloaded" ;.storef.dismiss invoke
}
button .storef.r2 -text "Reload 2" -anchor w -command {
exp_send "R2\r"
expect "*Option*"
set messagetext "Reloaded" ;.storef.dismiss invoke
}
button .storef.r3 -text "Reload 3" -anchor w -command {
exp_send "R3\r"
expect "*Option*"
set messagetext "Reloaded" ;.storef.dismiss invoke
}
button .storef.r4 -text "Reload 4" -anchor w -command {
exp_send "R4\r"
expect "*Option*"
set messagetext "Reloaded" ;.storef.dismiss invoke
}
button .storef.r5 -text "Reload 5" -anchor w -command {
exp_send "R5\r"
expect "*Option*"
set messagetext "Reloaded" ;.storef.dismiss invoke
}
button .storef.r6 -text "Reload 6" -anchor w -command {
exp_send "R6\r"
expect "*Option*"
set messagetext "Reloaded" ;.storef.dismiss invoke
}
pack .storef.re1 .storef.re2 .storef.re3 -in .storef.str -expand 1 -fill both
pack .storef.r1 .storef.r2 -in .storef.re1 -side left -expand 1 -fill both
pack .storef.r3 .storef.r4 -in .storef.re2 -side left -expand 1 -fill both
pack .storef.r5 .storef.r6 -in .storef.re3 -side left -expand 1 -fill both
}

# create the button to save the parameters
button .save -text "Save Colours\nand Levels" \
	-command {
	exp_send "S\r"
	expect "*file saved*"
	}
#create the button to load the colour table
button .colours -text "Change Colours" -command {
toplevel .cols ; wm geometry .cols +535+325
focus .cols ; grab .cols
button .cols.defcol -text "Default Colours" -command {
	exp_send "LDC\r"
	expect "*Option*"
	}
button .cols.defgry -text "Default Greys" -command {
	exp_send "LDG\r"
	expect "*Option*"
	}
button .cols.revcol -text "Reverse Colours" -command {
	exp_send "LRC\r"
	expect "*Option*"
	}
button .cols.revgry -text "Reverse Greys" -command {
	exp_send "LRG\r"
	expect "*Option*"
	}


button .cols.loadcols -text "Load from File" -command {
focus .
	exp_send "C\r"
	expect "*What filename*"
	exp_send "$colourfile \r"
	expect "*not found*" {set messagetext "Colour Map not found"}\
	       "*loaded*" {exp_send "RDW\r"; set messagetext "Colour Map loaded"}\
		"*Option*"
		}
label .cols.colfile -text "FileName"
entry .cols.colourfile -width 20 -relief sunken -textvariable colourfile
button .cols.quit -text "Return" -command {
eval destroy [winfo children .cols]
destroy .cols
focus .
}
pack .cols.defcol .cols.revcol .cols.defgry .cols.revgry .cols.loadcols \
  .cols.colfile .cols.colourfile .cols.quit -expand 1 -fill both
}

pack .six .storeops  -in .bl -fill x
pack .save .colours  -in .bl -expand 1 -fill both

set npp 1



# -----------------------------------------------------------------------
#create limit entries with annotation, and other buttons
label .topl -text "Top Limit:"
label .botl -text "Bottom Limit:"
entry .tle -width 10 -relief sunken -textvariable toplimit
entry .ble -width 10 -relief sunken -textvariable bottomlimit

#switch on and off auto limiting

# auto limit button sends splots.f parameter commmand and reads return
#create two buttons to lock and unlock limits
frame .lok 
radiobutton .lock -text "Limits Locked" -variable locked -value 1
radiobutton .unlock -text "Auto Limit" -variable locked -value 0

# the entry field for the top and bottom limits
bind .tle <Return> {
# ensure that the value is numeric
	if [expr $toplimit]  {
	if {$itype ==3} {
	set messagetext "Resetting to Linear"
	set itype 0
	exp_send "LIN\r"
	expect "*Option*"}
# check that the toplimit is above the bottomlimit
	if {$toplimit<$bottomlimit} {
	set messagetext "Toplimit below Bottomlimit"
	                } else {
	focus .
	exp_send "T\r"
	expect "*Top Limit*"
	exp_send "$toplimit \r"
	expect "*Option*"
	}} else {
	set messagetext "This value must be numeric and has been auto set"
	exp_send "V\r"
	expect  "*Option*"
	set toplimit [lindex $expect_out(buffer) 18]
	}
	           }
# entry field for bottom limit (same as top limit)
bind .ble <Return> {
	if [expr $bottomlimit] {
	if {$itype ==3} {
	set messagetext "Resetting to Linear"
	set itype 0
	exp_send "LIN\r"
	expect "*Option*"}
	if {$toplimit<$bottomlimit} {
	set messagetext "Bottomlimit above Toplimit"
	} else {
	focus .
	exp_send "B\r"
	expect "*Bottom Limit*"
	exp_send "$bottomlimit \r"
	expect "*Option*"
	}} else {
	set messagetext "This value must be numeric and has been auto set"
	exp_send "V\r"
	expect  "*Option*"
	set bottomlimit [lindex $expect_out(buffer) 15]
	}
	}
# -----------------------------------------------------------------------
# create manual setup button, with new windowing commands
button .msetup -text "Set Levels" -command {
exp_send "M\r"
expect "*Options*" {
# create a new window
toplevel .manual
wm title .manual "Xplots Set Manual Scale" ; wm geometry .manual +470+260
set textvar $expect_out(buffer)
frame .manual.base2
frame .manual.base3
pack  .manual.base2 .manual.base3 -side left -expand 1 -fill both
# create a frame and entry widget for each manual level
foreach frames {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
frame .manual.frm$frames
pack .manual.frm$frames -in .manual.base2 -side bottom
}

foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
entry .manual.lev$level -width 16 -relief groove -textvariable\
levl$level
set llevel $level
label .manual.lab$level -text $level -width 4
pack  .manual.lev$level .manual.lab$level -in .manual.frm$level \
-expand 1 -fill both -side right
set levl$level [lindex $textvar [expr 54-($level *3)]]
}

# for each entry field bind it to send the relative commands
# when the value is changed (There must be a shorter way
# of doing this !!!!!!!!.
bind .manual.lev1 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "1 [.manual.lev1 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]

}
}
bind .manual.lev2 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "2 [.manual.lev2 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev3 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "3 [.manual.lev3 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev4 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "4 [.manual.lev4 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev5 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "5 [.manual.lev5 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev6 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "6 [.manual.lev6 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev7 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "7 [.manual.lev7 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev8 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "8 [.manual.lev8 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev9 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "9 [.manual.lev9 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev10 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "10 [.manual.lev10 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev11 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "11 [.manual.lev11 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev12 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "12 [.manual.lev12 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev13 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "13 [.manual.lev13 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev14 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "14 [.manual.lev14 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev15 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "15 [.manual.lev15 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev16 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "16 [.manual.lev16 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
bind .manual.lev17 <Return> {
exp_send "E\r"
expect "*or 0 to exit*"
exp_send "17 [.manual.lev17 get] \r"
expect "*or 0 to exit*"
exp_send "0\r"
expect "*Options*"
set textvar $expect_out(buffer)
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
button .manual.create -text "Create Levels from Current Plot" -command {
exp_send "C\r"
expect "*Options*"
set textvar $expect_out(buffer)
# read values from parameters returned from splots.f
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
button .manual.fpar -text "Load Previous From File" -command {
exp_send "F\r"
expect "*Options*" 
set textvar $expect_out(buffer)
# read values from parameters returned from splots.par via splots.f
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
#set levl$level [lindex $textvar [expr 52-($level *3)]]
set levl$level [lindex $textvar [expr 63-($level *3)]]
}}

button .manual.zero -text "Initialise All Levels" -command {
exp_send "Z\r"
expect "*Options*"
set textvar $expect_out(buffer)
# read values from parameters returned from splots.f
foreach level {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17} {
set levl$level [lindex $textvar [expr 54-($level *3)]]
}}
button .manual.save -text "Save New Levels" -command {
exp_send "S\r"
expect "*Options*"
}
button .manual.return -text Return -command {
exp_send "R\r"
expect "*Option*"
eval destroy [winfo children .manual]
destroy .manual
}
pack .manual.save .manual.create .manual.fpar  .manual.zero .manual.return -in .manual.base3\
-side top -expand 1 -fill both -ipadx 5m
focus .manual
grab .manual
}}

# -----------------------------------------------------------------------
# create the scaling selector buttons
set lastitype 0
radiobutton .rlin -text Linear -variable itype -value 0 -anchor w\
-command {
exp_send "LIN\r"
expect "*OK - Linear*" {set lastitype 0;set messagetext "Linear OK"}\
       "*Invalid*" {set itype $lastitype; set messagetext "Run not set"}}
       
radiobutton .rlog -text "Log Scale" -variable itype -value 1 -anchor w\
-command {
exp_send "LOG\r"
expect "*OK - Log*" {set lastitype 1;set messagetext "Log OK"}\
       "*Invalid*" {set itype $lastitype; set messagetext "Run not set"}}

radiobutton .rsqr -text "Square Root" -variable itype -value 2 -anchor w\
-command {
exp_send "SQR\r"
expect "*OK - Sqr Roots*" {set lastitype 2;set messagetext "Square Root OK"}\
       "*Invalid*" {set itype $lastitype; set messagetext "Run not set"}}

radiobutton .rman -text "Manual Scale" -variable itype -value 3 -anchor w\
-command {
exp_send "MAN\r"
expect "*OK - manual*" {set lastitype 3;set messagetext "Manual OK"}\
       "*Invalid*" {set itype $lastitype; set messagetext "Run not set"}}



pack .rlin .rlog .rsqr .rman -in .bmr -expand 1 -fill both 
pack .msetup .lok -in .bml -expand 1 -fill both -side bottom
pack .lock .unlock -in .lok -expand 1 -fill \
both
set itype 0
pack .topl .tle .botl .ble -in .bml -fill x -side top

# create the postscript button which uses the current display
button .ps -text PostScript -command {
	exp_send "P\r"
	expect "*PostScript done*" {set messagetext "PostScript Printed"}}

#creates the general reset button - always useful
button .zz -text "General Reset" -command {
set messagetext "Reset"
set itype 0
set npp 6
set last $first
exp_send "z\r"
expect "*new run*"
exp_send "$first \r"
expect "*Option*"
}

# creates the exit button which closes splots.f cleanly, then closes splots.tk
button .exit -text EXIT -command {
exp_send "E\r"
expect "*PG*"
exit
}
pack .ps .zz .exit -in .br -expand 1 -fill both
bind .first <Return> {if {$first <0} {if {$last ==0} {set first 0} \
else {set first 1}}; if {$first>$last}\
{set last $first};.runnos config -from $first; .runnos set $first
focus .
exp_send "n\r"
expect "*new run*"
exp_send "$first \r"
expect "*Option*"
exp_send "V\r"
expect  "*Option*"
	set bottomlimit [lindex $expect_out(buffer) 15]
	set toplimit [lindex $expect_out(buffer) 18]
}
bind .last <Return> {if [regexp {^[0-9]*$} $last] {
if {$first <0} {set first 0}\
;.runnos config -to $last; focus .} else {
set messagetext "This value must be integer"
set last $first
}
}

frame .c
pack .c -in .right -side top -fill both
button .normlab -text "SET NORMALIZATION" -width 20 \
-command      {
		exp_send "v\r"
		expect "Option, or Help"
		normaliz $expect_out(buffer)
              }

button .wind -text "SET WINDOW"  -width 20 -command {setwin}
 
pack .normlab .wind  -in .c -side left

# start up splots.f
.unlock invoke
spawn splots
expect "*new run*"
exp_send "0\r"
.changeruns invoke
# to set to sqr to start off with, set a var, then when runs set, then
# send a SQr command after a run has been selected
set temp98 0

######################  dblocate     debug window

proc dblocate  W {
toplevel .dbl
frame .dbl.f
label .dbl.f.label -text " ... $W ..." -fg red
button .dbl.f.but -text "OK" -command {destroy .dbl }
pack .dbl.f.label  .dbl.f.but
pack .dbl.f
focus .dbl
grab set .dbl
tkwait window .dbl
}

#:eof
