#!/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)]
set CVSRevisionAuthor "\$Revision: 1.13 $ \$Author: borland $"

APSApplication . -name "setCMOffsets" -version $CVSRevisionAuthor \
  -overview "This utility allows setting the current monitor offsets for the LTP, PAR, PTB, and BTS.  It will turn off the linac gun trigger for about 30 seconds in order to do this.\n\
The offsets are strongly affected by the pulsed magnets, particular those in PAR.  Hence, if you are going to be running the PAR, the pulsed magnets should be left running while you perform this procedure.  Basically, any pulsed equipment that will be on while you run beam should be on while you perform this procedure."

set mainStatus "Ready."
proc SetMainStatus {text} {
    global mainStatus
    set mainStatus "[exec date +%H:%M:%S:] $text"
    update
}

proc SetCMOffsets {args} {
    set PAR 1
    set PTB 1
    set LTP 1
    set BTS 1
    set turnGunOff 1
    set statusVariable ""
    APSParseArguments {PAR PTB LTP BTS turnGunOff statusVariable}
    if [catch {exec cavget -list=L1:RG1:KIK:chargeTrigC \
                    -cavputForm -numerical -pend=10 -printErrors} gunState1] {
        return -code error "SetCMOffsets1: error in reading RG1 gun state: $gunState1"
    }
    if [catch {exec cavget -list=L1:RG2:KIK:chargeTrigC \
                    -cavputForm -numerical -pend=10 -printErrors} gunState2] {
        return -code error "SetCMOffsets1: error in reading RG1 gun state: $gunState2"
    }
    if [catch {exec cavget  -list=It:ER1:er.OTL2 -pend=10 -num -printErrors} EKStart] {
        return -code error "SetCMOffsets2: error in reading EK start: $EKStart"
    }
    
    if [string length $statusVariable]  {
        global $statusVariable
    } else {
        set statusVariable dummyStatusVariable
    }
    APSSetVarAndUpdate $statusVariable "Running..."
    if $turnGunOff {
        if [catch {exec cavput -list=L1:RG1:KIK:chargeTrigC=0,L1:RG2:KIK:chargeTrigC=0 -pend=10} result] {
            return -code error "SetCMOffsets3: error in turn off RFG gun: $eresult"
        }
    } else {
        APSSetVarAndUpdate $statusVariable "NB: gun not turned off, by your request."
        APSSetVarAndUpdate $statusVariable "If there is really beam in LTP, "
        APSSetVarAndUpdate $statusVariable "then you should run again allowing "
        APSSetVarAndUpdate $statusVariable "this script to turn the gun off."
    }

    if $PAR {
        # dump PAR beam
        if [catch {exec cavput -list=It:ER1:er.OTL2=1 -pend=10} result] {
            return -code error "SetCMOffsets4: error in dump PAR beam: $result"
        }
    }
    after 500
    if [catch {exec cavput -list=PTB:CM,LTP:FCM,P:ACM,BTS:CM -list=:offsetAveRestartCC.PROC=1 -pend=20} result] {
        return -code error "SetCMOffsets5: error in starting current offset monitoring: $result"
    }
    after 2000
    if [catch {exec cavput -list=It:ER1:er.OTL2=$EKStart -pend=10} result] {
        return -code error "SetCMOffsets6: error in setting EK start state: $result"
    }
#    if $PAR {
#        APSSetVarAndUpdate $statusVariable "waiting for PAR offset monitor alarm to be gone..."
#        if [catch {exec cawait -interval=5 -pend=10 -wait=P:ACM:offsetAveSUB.SEVR,equal=0 } result] {
#            return -code error "SetCMOffsets7: error in waiting for PAR offset monitor alarm: $result"
#        }
#        if [catch {exec cavput -list=P:ACM:offsetCommitSQ.PROC=1 -pend=10} result] {
#            return -code error "SetCMOffsets8: error in starting PAR current offset monitor: $result"
#        }
#    }
#    if $LTP {
#        APSSetVarAndUpdate $statusVariable "waiting for LTP offset monitor alarm to be gone..."
#        if [catch {exec cawait -interval=5 -wait=LTP:FCM:offsetAveSUB.SEVR,equal=0 } result] {
#            return -code error "SetCMOffsets9: error in waiting for LTP current offset monitor alarm: $result"
#        }
#        if [catch {exec cavput -list=LTP:FCM:offsetCommitSQ.PROC=1 } result] {
#            return -code error "SetCMOffsets10: error in starting LTP current offset monitor: $result"
#        }
#    }
#    if $PTB {
#        APSSetVarAndUpdate $statusVariable "waiting for PTB offset monitor alarm to be gone..."
#        if [catch {exec cawait -interval=5 -wait=PTB:CM:offsetAveSUB.SEVR,equal=0} result] {
#            return -code error "SetCMOffsets11: error in waiting for PTB current offset monitor alarm: $result"
#        }
#        if [catch {exec cavput -list=PTB:CM:offsetCommitSQ.PROC=1} result] {
#            return -code error "SetCMOffsets12: error in starting PTB current offset monitor: $result"
#        }
#    }
    if $BTS {
        APSSetVarAndUpdate $statusVariable "waiting for BTS offset monitor alarm to be gone..."
        if [catch {exec cawait -interval=5 \
                     -wait=BTS:CM:offsetAveSUB.SEVR,equal=0 } result] {
            return -code error "SetCMOffsets13: error in waiting for BTS current offset monitor alarm: $result"
        }
        if [catch {exec cavput -list=BTS:CM:offsetCommitSQ.PROC=1 } result] {
            return -code error "SetCMOffsets14: error in starting BTS current offset monitor: $result"
        }
    }

    if $turnGunOff {
        if [catch {exec cavput -list=$gunState1} result] {
            return -code error "SetCMOffsets15: error in turning off RG1 gun: $result"
        }
        if [catch {exec cavput -list=$gunState2} result] {
            return -code error "SetCMOffsets15: error in turning off RG2 gun: $result"
        }
    }
    APSSetVarAndUpdate $statusVariable "Done."
    bell
}

APSScrolledStatus .status -parent .userFrame -textVariable mainStatus -width 40

set LTP 1
set PTB 1
set PAR 1
set BTS 1
set turnGunOff 1
APSCheckButtonFrame .cb -parent .userFrame \
  -label "Current monitors" \
  -orientation horizontal \
  -buttonList {LTP PAR PTB BTS} \
  -variableList {LTP PAR PTB BTS} -contextHelp \
  "Select the current monitors for which to set the offsets.  Setting the LTP and PAR offsets is fast, so if you don't want PTB or BTS, deselect them."

APSRadioButtonFrame .rb -parent .userFrame \
  -label "Turn gun off for measurement: " -orientation horizontal \
  -variable turnGunOff -buttonList {Yes No} -valueList {1 0} -contextHelp \
  "Select whether to turn the gun off when taking measurements.  This must be set to Yes if there is beam in the LTP!"

APSButton .go -text Go -parent .userFrame \
  -command {SetCMOffsets -PAR $PAR -BTS $BTS -PTB $PTB -LTP $LTP -turnGunOff $turnGunOff -statusVariable mainStatus} \
  -contextHelp "Press to initiate setting of current monitor offsets for LTP, PAR, PTB, and BTS, as requested.  Will disable gun trigger for a while."

