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

#
# $Log: not supported by cvs2svn $
# Revision 1.2  1996/06/26 02:02:38  borland
# New versions per N. Sereno.  (Added averaging to cavput, plus other
# improvements.)
#
# Revision 1.1  1996/06/23  15:20:54  borland
# Moved to the right location in the repository.
#
# Revision 1.1  1996/06/23  15:17:56  borland
# First versions per N. Sereno.
#
#
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)]

set bpmFile /home/helios/oagData/booster/setupFiles/XsingleTurnBPM.def
set bpmList [join [exec sdds2stream -col=ControlName $bpmFile]]
set bpmVarList [join [exec sdds2stream -col=SymbolicName $bpmFile]]

# Set PTB BPM setpoints to the current BPM readback values.  Used in conjunction with controlLaw to steer
# to an arbitrary trajectory.

set ControlStatus "Ready."

proc MakePTBBPMSetpointConfigMenuAndStatus {} {

        APSApplication . -name "BoosterSingleTurnBPMSetpointConfig" -version "1.0" \
                         -overview "This is an application to set the Booster single turn X BPM setpoints to the current BPM readbacks"

        APSMenubarAddMenu .controlLaw -parent .menu -text "ControlLaw"

                .menu.controlLaw.menu add command -label "Single Turn ControlLaw" \
                        -command {exec controllaw -controlList "booster" &}

        APSScrolledStatus .status -parent .userFrame -textVariable ControlStatus \
                          -height 8 -width 65 -packOption {-side top}
}

# Make checkbuttons for PTB BPMs and execution button.
proc SelectPTB {args} {
    set all 0
    APSParseArguments {all}
    
    global bpmVarList
    eval global $bpmVarList

    foreach var $bpmVarList {
        if [regexp {PTB} $var] {
            set ${var} $all
        }
    }
}

proc SelectBooster {args} {
    set all 0
    APSParseArguments {all}
    
    global bpmVarList
    eval global $bpmVarList

    foreach var $bpmVarList {
        if ![regexp {PTB} $var] {
            set ${var} $all
        }
    }
}

proc MakePTBBPMCheckbuttonAndExecuteFrame {widget args} {

    global bpmList bpmVarList

    set parent ""
    APSParseArguments {parent}
    
    APSFrame $widget -parent $parent -label "Booster Single Turn X BPM Selection" \
        -packOption {-side left -expand 1 -fill x} \
        -contextHelp "Selection panel for PTB BPMs."
    
    set w $parent$widget.frame
    
    APSCheckButtonFrame .bpmHButtons -parent $w \
        -label "Horizontal\nBooster Single Turn/PTB BPMs" -buttonList $bpmVarList \
          -variableList "$bpmVarList" -allNone 1 \
        -packOption {-side left -expand 1} \
        -contextHelp "Horizontal Booster single turn BPM selection panel." 
    
    APSButton .allB -text "All Booster" -parent $w.bpmHButtons.frame  -size small -command "SelectBooster -all 1"
    APSButton .nB -text "None Booster"  -parent $w.bpmHButtons.frame  -size small -command "SelectBooster -all 0"
    APSButton .allPTB -text "All PTB" -parent $w.bpmHButtons.frame  -size small -command "SelectPTB -all 1"
    APSButton .nonPTB -text "None PTB" -parent $w.bpmHButtons.frame  -size small -command "SelectPTB -all 0"
    
    APSButton .transferButton -parent $w \
        -text "Transfer Readbacks To\nSingle Turn Booster ControlLaw Setpoints" \
        -command {SetBPMSetpoints -statusCallback SetStatus} \
        -packOption {-side left -expand 1} \
        -contextHelp "This button transfers current PTB BPM readbacks to controllaw setpoints"
    
       
}

# Status line update procedure

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

# procedure to set PTB bpm steering setpoints to bpm readback values.

proc SetBPMSetpoints {args} {
    
    global bpmList bpmVarList bpmFile
    eval global $bpmVarList
    
    APSParseArguments {statusCallback}

    set selectedList ""
    foreach var $bpmVarList {
        if [set $var] {
            lappend selectedList $var
        }
    }
    if ![llength $selectedList] {
        if {$statusCallback!=""} {
            $statusCallback "No BPMs selected."
        }
        bell
    } else {
        set tmpRoot /tmp/[APSTmpString]
        
        if {$statusCallback!=""} {
            $statusCallback "Getting 10 BPM readbacks for selected BPMs and averaging..."
        }
        if [catch {exec sddsmakedataset -pipe=out -col=SymbolicName,type=string -data=[join $selectedList ,] \
                     | sddsxref -pipe=in $bpmFile -take=ControlName -match=SymbolicName $tmpRoot.mon } result] {
            return -code error "Error create monitor file: $result"
        }
        if [catch {exec sddsmonitor $tmpRoot.mon -steps=10 -interval=1 $tmpRoot.sdds -erase } result] {
            return -code error "Error reading readbacks: $result"
        }
        if {$statusCallback!=""} {
            $statusCallback "transferring readbacks to setpoints..."
        }
        if [catch {exec sddsprocess $tmpRoot.sdds -process=*,ave,%s -pipe=out \
                     | sddscollapse -pipe \
                     | sddsconvert -pipe -retain=col,B*P*,PTB* \
                     | sddstranspose -pipe -root=Offset -oldColumnName=ControlName  \
                     | tee $tmpRoot.offset \
                     | sddsprocess -pipe -print=col,ValueString,%lf,Offset \
                     -reedit=col,ControlName,%/:x/:SetpointAO/%/Xposition/XsetpointAO/ \
                     | tee $tmpRoot.setpoint \
                     | sddscasr -pipe=in -restore } result] {
            return -code error "Error transferring readbacks to setpoints: $result"
        }
        
        set files [glob $tmpRoot.*]
        APSAddToTmpFileList -ID booster -fileList $files
        if {$statusCallback!=""} {
            $statusCallback "done."
        }
    }
}


# Build Application

MakePTBBPMSetpointConfigMenuAndStatus
MakePTBBPMCheckbuttonAndExecuteFrame .bpmFrame -parent .userFrame
