#!/bin/sh  
# \
exec oagwish "$0" "$@"

set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSStandardSetup


set CVSRevisionAuthor "\$Revision: 1.2 $ \$Author: shang $"

proc SetStatus {text} {
    global status
    set status $text
    update
}

proc GetPresentLattice {} {
    global PresentLattice OAGGlobal
    set oldDir [pwd]
    cd $OAGGlobal(SRLatticesDirectory)
    set PresentLattice [file tail [file readlink default]]
    cd $oldDir
}

proc PlotTwissSectors {args} {
    global dataDir selectLattice selectSector thick pngFile psFile outputDir noDispersion plotOption printer
    set png ""
    set ps ""
    if {[info exist pngFile] && [string length [string trim $pngFile]]} {
        set png $outputDir/[string trim $pngFile]
    }
    if {[info exist psFile] && [string length [string trim $psFile]]} {
        set ps  $outputDir/[string trim $psFile]
    }
    set sectorList [split $selectSector "-"]
    set option $plotOption
    set len [llength $sectorList]
    if {$len==2} {
        set s1 [lindex $sectorList 0]
        set s2 [lindex $sectorList 1]
        if {$s1<$s2} {
            set start $s1
            set end $s2
        } else {
            set start $s2
            set end $s1
        }
        set start [expr $start - 1]
    } elseif {$len==1} {
        set sector [lindex $sectorList 0]
        set start [expr $sector - 1]
        set end $sector
    } else {
        SetStatus "Invalid sector provided, it has to be one sector (1-40) or a range of sector (e.g 5-7)"
        return
    }
    if {$start<0} {
        SetStatus "invalid sectors provided; the sector has to be from 1 to 40."
        return
    }
    append option [APSMakeSafeQualifierString " -scale=[expr $start*27.6],[expr $end*27.6],0,0"]
    if [string length [string trim $printer]] {
        append option " -printer $printer"
    }
    if [string length [string trim $ps]] {
        append option " -psFile $ps"
    }
    if [string length [string trim $png]] {
        append option " -pngFile $png"
    }
    set fileRoot $dataDir/$selectLattice/aps
    append option " -thick $thick -noDispersion $noDispersion -fileRoot $fileRoot"
    
    if {0} {
        catch {  exec plotTwiss $option & } result
    }
    
    APSExecLog .plot  -unixCommand "plotTwiss $option"
}

set dataDir $OAGGlobal(SRLatticesDirectory)
set latticeList [APSSRFindLattices]
GetPresentLattice

set selectLattice $PresentLattice 
set thick 1
set noDispersion 0
set printer ""
set psFile ""
set pngFile ""
set plotOption ""

set sector 1
APSApplication . -name "PlotTwissSectors" -version $CVSRevisionAuthor \
  -overview "GUI to launch plotTwiss commands for any of the lattices."

set status "Ready."
APSScrolledStatus .status -parent .userFrame -textVariable status \
    -width 70 -height 8

APSRadioButtonFrame .lattice -parent .userFrame -label "Select Lattice"  \
    -buttonList $latticeList -variable selectLattice -valueList $latticeList -limitPerRow 7

set selectSector 1
APSLabeledEntry .sector -parent .userFrame -label "Sector: " \
  -textVariable selectSector -width 30 -contextHelp "sector can be given in integer from 1 to 40, or in a range, for example 6-8; \n
  for sector=1, the length is 27.6m; otherwise, the sector length will be $sector*27.6 m; the sector length will be used\n
  as the x-axis range of the plot"

set outputDir .
APSLabeledEntry .dir -parent .userFrame -width 70 -textVariable outputDir -label "Save file directory:" \
   -contextHelp "provide the directory for saving  psFile, pngFile to"
APSLabeledEntry .option -parent .userFrame -width 70 -textVariable plotOption -label "Plot Option:" \
    -contextHelp "enter the plot option."
APSFrameGrid .grid -parent .userFrame -xList {x1 x2}
set w1 .userFrame.grid.x1
set w2 .userFrame.grid.x2
APSLabeledEntry .file1 -parent $w2 -width 30 -textVariable psFile -label "PS Filename:" \
    -contextHelp "provide the file name for saving in ps format"
APSLabeledEntry .file2 -parent $w2 -width 30 -textVariable pngFile -label "PNG Filename:" \
    -contextHelp "provide the file name for saving in png format"
APSRadioButtonFrame .thick -parent $w1 -label "Thick?         " -buttonList {Yes No} -orientation horizontal \
    -valueList {1 0} -variable thick -contextHelp "increase line thickness, recommended for png file generation"
APSRadioButtonFrame .dis -parent $w1 -variable noDispersion -buttonList {Yes No} -valueList {1 0} \
  -orientation horizontal -contextHelp "If yes, no dispersion will be plotted." -label "No dispersion? "
APSLabeledEntry .printer -parent $w2 -label "Printer:" -textVariable printer -width 30

APSButton .plot -parent .userFrame  -text "Plot" -command PlotTwissSectors -contextHelp "bring plotTwiss command."
