#!/bin/sh
# \
exec oagwish -f "$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.8 $ \$Author: borland $"

proc FillMonitorOverview {} {
    return "This utility helps to evaluate the storage ring filling rate.  It monitors charge levels in the injector, and also displays the incremental current in the storage ring."
}

APSApplication . -name "FillMonitor" -version $CVSRevisionAuthor -overview [FillMonitorOverview]

set tcl_precision 4

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

set deltaSRcurrent 0
set deltaSRcharge 0
set lastSRcurrent  0
proc RefreshValues {} {
    global LTPq PTBq BTSq SRcurrent deltaSRcurrent lastSRcurrent deltaSRcharge
    global BTSCPV2sum BTSRatio
    set lastSRcurrent $SRcurrent

    if {[pv getw {LTPq PTBq BTSq SRcurrent BTSCPV2sum}]!=0} {
        APSAlertBox .err[APSTmpString] -errorMessage "Unable to get values for charge and current.\n$errorCode" 
        exit
    }
    set LTPq $LTPq
    set PTBq $PTBq
    set BTSq $BTSq
    set SRcurrent $SRcurrent
    set BTSCPV2sum $BTSCPV2sum
    if {$BTSq==0} {
        set BTSRatio ?
    } else {
        set BTSRatio [expr $BTSCPV2sum/$BTSq]
}
    # compute equivalent changes for 1/2 s
    # the numbers are read every 1s
    set deltaSRcurrent [expr ($SRcurrent-$lastSRcurrent)/2.0]
    set deltaSRcharge [expr $deltaSRcurrent*3.68]
}

RefreshValues 

APSLabeledOutput .ltp -parent .userFrame -textVariable LTPq -label "LTP (nC): " \
        -width 14 \
        -contextHelp "Charge in the LTP for the present sample."
APSLabeledOutput .ptb -parent .userFrame -textVariable PTBq -label "PTB (nC): " \
        -width 14 \
        -contextHelp "Charge in the PTB for the present sample."
APSLabeledOutput .bts -parent .userFrame -textVariable BTSq -label "BTS (nC): " \
        -width 14 \
        -contextHelp "Charge in the BTS for the present sample."
APSLabeledOutput .btscpv2 -parent .userFrame -textVariable BTSCPV2sum -label "BTS CPV2 (V): " \
        -width 14 \
        -contextHelp "Voltage on the last BPM in the BPS for the present sample."
APSLabeledOutput .btscpv2r -parent .userFrame -textVariable BTSRatio -label "BTS CPV2/BTS CM (V/nC): " \
        -width 14 \
        -contextHelp "Voltage on the last BPM in the BPS for the present sample."
APSLabeledOutput .srdcharge -parent .userFrame -textVariable deltaSRcharge \
        -label "delta SR (nC): " \
        -width 14 \
        -contextHelp "Change in charge in the storage ring from the last sample."
APSLabeledOutput .srcurr -parent .userFrame -textVariable SRcurrent -label "SR (mA): " \
        -width 14 \
        -contextHelp "Current in the storage ring for the present sample."
APSLabeledOutput .srdcurr -parent .userFrame -textVariable deltaSRcurrent \
        -label "delta SR (mA): " \
        -width 14 \
        -contextHelp "Change in current in the storage ring from the last sample."

while 1 {
    after 1000
    RefreshValues
    update
}



