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

#
# $Log: not supported by cvs2svn $
# Revision 1.5  1998/10/22 18:01:54  borland
# Removed polarity switch.
#
# Revision 1.4  1998/10/14 22:10:26  borland
# Added button to control expected polarity.  Default is electrons.
#
# Revision 1.3  1997/03/31 17:07:58  borland
# Made the scrolled area wider.
#
# Revision 1.2  1997/03/31 17:05:09  borland
# Added longer scrolled area plus count-down of samples.
#
# Revision 1.1  1997/03/31 16:59:19  borland
# First version.
#
#

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.6 $ \$Author: borland $"

APSApplication . -name PAREfficiency -version $CVSRevisionAuthor -overview \
    "Reads LTP, PAR, and PTB current monitors to compute efficiency of PAR."

set tcl_precision 4

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

proc RefreshValues {} {
    global LTPq PTBq samples sample polarity

    if $samples<0 {
        set samples 1
    }

    set LTPsum 0
#    set PTBsum 0
    set PARsum 0
    for {set sample $samples} {$sample>0} {incr sample -1} {
        if {[pv getw {LTPq PTBq}]!=0} {
            APSAlertBox .err[APSTmpString] -errorMessage "Unable to get values for charge.\n$errorCode" 
            exit
        }
        set LTPsum [expr $LTPsum+$LTPq]
#        set PARsum [expr $PARsum+$PARq]
        set PTBsum [expr $PTBsum+$PTBq]
        after 500
        update
    }
    set LTPave [expr $LTPsum/$samples]
#    set PARave [expr $PARsum/$samples]
    set PTBave [expr $PTBsum/$samples]
    set InjEff 0
    set ExtEff 0
    set totalEff 0
    if $LTPave>0 {
#        set InjEff [expr $PARave/$LTPave*100]
        set totalEff [expr $PTBave/$LTPave*100]
    } 
#    if $PARave>0 {
#        set ExtEff [expr $PTBave/$PARave*100]
#    }
    global status
    set status [format \
                  "LTP:%4.2fnC PAR:%4.2fnC PTB:%4.2fnC  Inj:%2.0f%% Ext:%2.0f%% Total:%2.0f%%" \
                  $LTPave $PARave $PTBave $InjEff $ExtEff $totalEff]
    set status [format \
                  "LTP:%4.2fnC PTB:%4.2fnC  Total:%2.0f%%" \
                  $LTPave $PTBave $totalEff]
    update
}

set status ""
set samples 10
set sample 0
set stopMeasurement 0
APSScrolledStatus .status -parent .userFrame -width 80 \
    -textVariable status -height 15
APSLabeledOutput .sample -parent .userFrame -width 10 \
    -label "Sample : " -textVariable sample -contextHelp \
    "Counts up the sample number during measurements."
APSLabeledEntry .samples -parent .userFrame -width 10 \
    -label "Samples: " -textVariable samples -contextHelp \
    "Sets the number of readings to average for each computation.  Samples are taken every 0.5s ."
APSButton .run -parent .userFrame -text Run -command RunMeasurement \
    -contextHelp "Begins the computations."
APSButton .stop -parent .userFrame -text Stop -command \
    "set stopMeasurement 1" -contextHelp "Stops the computations."

update

proc RunMeasurement {} {
    global stopMeasurement  status
    set status "Running..."
    update

    set stopMeasurement 0
    while {!$stopMeasurement} {
        RefreshValues
        update
    }
    set status "Stopped."
    set stopMeasurement 0
}
