#!/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

# modified to remove sector 38. The variable station is used here as
# the group of cavities (sector), not really the klystrons station,
# which is confusing.
# there is no CA to a klystron station.

# to test: run script SRGapVoltagePP.makePVs, which makes the file SRGapVoltagePP.pvs and
# actually launches an instance of sddspcas of that file/
# It seems that EPICS_CA_AUTO_ADDR_LIST should have value yes

set version 1.0
APSApplication . -name SRGapVoltagePP -version $version \
  -overview {This tools provides storage ring gap voltage calculations from probe power.}

set tcl_precision 4

frame .userFrame.outputs 
pack .userFrame.outputs -side top

proc MakeStationVoltageWidget {widget args} {
    set station ""
    set parent ""
    APSParseArguments {station parent}

    global S${station}C1GV S${station}C2GV S${station}C3GV \
      S${station}C4GV S${station}GV

    APSFrame $widget -parent $parent -label ""
    set w $parent$widget.frame
    APSLabeledOutputFrame .total -parent $w -label \
      "Station $station total (kV): " \
      -width 13 -variableList "S${station}GV"
    APSLabeledOutputFrame .indiv -parent $w -label \
      "Station $station cells (kV): " \
      -width 13 -variableList  \
    "S${station}C1GV S${station}C2GV S${station}C3GV S${station}C4GV"
}

APSMakeLabeledOutputs .userFrame.outputs.total "Total (kV): " \
  -ewidth 10 -lanchor w  SGV

#
# Build up tcl var names and pv names. Create display widgets.
#
set refreshPvList {}
set refreshTclVarList {}
foreach station {1 2 3} sector {40 36 37} {
    set tclVarList [list "S${station}C1PP" \
		      "S${station}C2PP" \
		      "S${station}C3PP" \
		      "S${station}C4PP"] 
    eval lappend refreshTclVarList [split $tclVarList " "]

    set pvList [list "SRF:S${sector}:ED1:Ch2Watt" \
		  "SRF:S${sector}:ED2:Ch2Watt" \
		  "SRF:S${sector}:ED3:Ch2Watt" \
		  "SRF:S${sector}:ED4:Ch2Watt"]
    eval lappend refreshPvList [split $pvList " "]

    MakeStationVoltageWidget .s$station -parent .userFrame.outputs \
      -station $station
}
#
# With complete tcl var and pv name lists in hand, link them all at once.
#    
if {[pv linkw $refreshTclVarList $refreshPvList] != 0} {
    puts stderr "$argv0: $errorCode"
    exit
}    

# shunt impedance with factor of 2 included per R. Kustom
set Rs [expr 2.0*5.0]

proc RefreshValues {} {
    global refreshTclVarList argv0 errorCode
    foreach s {1 2 3 4} {
	global S${s}C1GV S${s}C2GV S${s}C3GV S${s}C4GV S${s}GV
	global S${s}C1PP S${s}C2PP S${s}C3PP S${s}C4PP
    }
    global SGV
    global Rs

    if {[pv getw $refreshTclVarList] != 0} {
	puts stderr "argv0: $errorCode"
    }
    foreach station {1 2 3} {
        set sum 0.0
        foreach cavity {1 2 3 4} {
            set value [expr sqrt([subst \$S${station}C${cavity}PP]*$Rs)]
            set S${station}C${cavity}GV $value
            set sum [expr $sum+$value]
        }
        set S${station}GV $sum
    }
    set SGV [expr $S1GV+$S2GV+$S3GV]        
}

proc ScheduledUpdate {} {
    RefreshValues
    update idletasks
    after 1000 ScheduledUpdate
}

ScheduledUpdate


# Local Variables:
# mode: tcl
# End:
