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

proc safetyEnvMonOverview {} {
    return "This utility reads control system data pertaining to beam current and evaluates it in light of the safety envelope.\n\
It displays the charge per cycle in the LTP, PTB, and BTS, as obtained from the controls system.  It then uses the known limiting energy and cycle repetition rates for each system to compute the maximum possible beam power.  This is compared to the safety envelope, and a warning light is displayed when a reading is over the safety envelope threshold.  (Note that this is not a violation of the safety envelope unless it continues for 1 hour or more.\nThe 'Context Help' feature of the Help menu may be used to get more information on the various quantities displayed."
}

APSApplication . -name "Safety Envelope Monitor" -version $CVSRevisionAuthor \
	 -overview [safetyEnvMonOverview]

set tcl_precision 2

APSFrameGrid .fg -parent .userFrame \
  -xList {system charge energy rate power limit warning} \
  -yList {header ltp ptb bts} 

label .userFrame.fg.system.header.label -text "System\n "
label .userFrame.fg.charge.header.label  -text "Charge\n(nC)"
label .userFrame.fg.energy.header.label  -text "Max. Energy\n(GeV)"
label .userFrame.fg.rate.header.label  -text "Rep. rate\n(Hz)"
label .userFrame.fg.power.header.label  -text "Max. Beam Power\n(W)"
label .userFrame.fg.limit.header.label -text "Safety Envelope\n(W)"
label .userFrame.fg.warning.header.label -text "Warning\nIndicator"
pack .userFrame.fg.system.header.label .userFrame.fg.charge.header.label .userFrame.fg.energy.header.label \
  .userFrame.fg.rate.header.label .userFrame.fg.power.header.label \
  .userFrame.fg.limit.header.label .userFrame.fg.warning.header.label 

label .userFrame.fg.system.ltp.label -text "LTP: " -pady 4
label .userFrame.fg.system.ptb.label -text "PTB: " -pady 4
label .userFrame.fg.system.bts.label -text "BTS: " -pady 4
pack .userFrame.fg.system.ltp.label .userFrame.fg.system.ptb.label .userFrame.fg.system.bts.label  -fill both

set LTPq 1.0
set PTBq 1.0
set BTSq 1.0
APSLabeledOutput .ltp.lab -parent .userFrame.fg.charge -label "" -textVariable LTPq \
  -width 10 \
  -contextHelp "Gives the readback of the charge per cycle in the LTP."
APSLabeledOutput .ptb.lab -parent .userFrame.fg.charge -label "" -textVariable PTBq \
  -width 10 \
  -contextHelp "Gives the readback of the charge per cycle in the PTB."
APSLabeledOutput .bts.lab -parent .userFrame.fg.charge -label "" -textVariable BTSq \
  -width 10 \
  -contextHelp "Gives the readback of the charge per cycle in the BTS."

set LTPenergy 0.5
set PTBenergy 0.5
set BTSenergy 7.7
APSLabeledOutput .ltp.lab -parent .userFrame.fg.energy -label "" \
  -textVariable LTPenergy -width 8 \
  -contextHelp "Gives the maximum possible energy of the beam in the LTP."
APSLabeledOutput .ptb.lab -parent .userFrame.fg.energy -label "" \
  -textVariable PTBenergy -width 8 \
  -contextHelp "Gives the maximum possible energy of the beam in the PTB."
APSLabeledOutput .bts.lab -parent .userFrame.fg.energy -label "" \
  -textVariable BTSenergy -width 8 \
  -contextHelp "Gives the maximum possible energy of the beam in the BTS."

set LTPrate 2
set PTBrate 2
set BTSrate 2
APSLabeledOutput .ltp.lab -parent .userFrame.fg.rate -label "" \
  -textVariable LTPrate -width 8 \
  -contextHelp "Gives the cycle repetition rate in the LTP. (Not the same as the LINAC repetition rate.)"
APSLabeledOutput .ptb.lab -parent .userFrame.fg.rate -label "" \
  -textVariable PTBrate -width 8 \
  -contextHelp "Gives the cycle repetition rate in the PTB."
APSLabeledOutput .bts.lab -parent .userFrame.fg.rate -label "" \
  -textVariable BTSrate -width 8 \
  -contextHelp "Gives the cycle repetition rate in the BTS."

set LTPpower 0
set PTBpower 0
set BTSpower 0
APSLabeledOutput .ltp.lab -parent .userFrame.fg.power -label "" \
  -textVariable LTPpower \
  -width 14 \
  -contextHelp "Gives the beam power in the LTP for the measured charge and the maximum energy."
APSLabeledOutput .ptb.lab -parent .userFrame.fg.power -label "" \
  -textVariable PTBpower \
  -width 14 \
  -contextHelp "Gives the beam power in the PTB for the measured charge and the maximum energy."
APSLabeledOutput .bts.lab -parent .userFrame.fg.power -label "" \
  -textVariable BTSpower \
  -width 14 \
  -contextHelp "Gives the beam power in the BTS for the measured charge and the maximum energy."

set LTPlimit 20
set PTBlimit 20
set BTSlimit 308
APSLabeledOutput .ltp.lab -parent .userFrame.fg.limit -label "" \
  -textVariable LTPlimit -width 14 \
  -contextHelp "Gives the beam power at the safety limit in the LTP."
APSLabeledOutput .ptb.lab -parent .userFrame.fg.limit -label "" \
  -textVariable PTBlimit -width 14 \
  -contextHelp "Gives the beam power at the safety limit in the PTB."
APSLabeledOutput .bts.lab -parent .userFrame.fg.limit -label "" \
  -textVariable BTSlimit -width 14 \
  -contextHelp "Gives the beam power at the safety limit in the BTS."


set LTPwarning 0
set PTBwarning 0
set BTSwarning 0
checkbutton .userFrame.fg.warning.ltp.cb \
  -variable LTPwarning -text " "
checkbutton .userFrame.fg.warning.ptb.cb \
  -variable PTBwarning -text " "
checkbutton .userFrame.fg.warning.bts.cb \
  -variable BTSwarning -text " "
pack .userFrame.fg.warning.ltp.cb .userFrame.fg.warning.ptb.cb \
  .userFrame.fg.warning.bts.cb

if {[pv linkw {LTPq PTBq BTSq} \
       {LTP:FCM:qTotalAI PTB:CM:qTotalAI BTS:CM:q}] != 0} {
    APSAlertBox .err[APSTmpString] -errorMessage "Unable to make channel access connections.\n$errorCode" 
    exit
}

if {[pv mon {LTPq PTBq BTSq} RefreshValues] != 0} {
    APSAlertBox .err[APSTmpString] -errorMessage "Unable to establish channel access monitors.\n$errorCode" 
    exit
}

proc RefreshValues {} {
    global LTPq PTBq BTSq
    global LTPenergy PTBenergy BTSenergy
    global LTPrate PTBrate BTSrate
    global LTPpower PTBpower BTSpower
    global LTPlimit PTBlimit BTSlimit
    global LTPwarning PTBwarning BTSwarning

    set LTPq $LTPq
    set PTBq $PTBq
    set BTSq $BTSq

    set LTPpower [expr abs($LTPq*$LTPrate*$LTPenergy)]
    set PTBpower [expr abs($PTBq*$PTBrate*$PTBenergy)]
    set BTSpower [expr abs($BTSq*$BTSrate*$BTSenergy)]

    set LTPwarning [expr $LTPpower>$LTPlimit?1:0]
    set PTBwarning [expr $PTBpower>$PTBlimit?1:0]
    set BTSwarning [expr $BTSpower>$BTSlimit?1:0]
    if {$LTPwarning || $PTBwarning || $BTSwarning} { bell }
    global tk_version
    if {[expr $tk_version<8.0]} update
}

RefreshValues
