#!/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.0 $ \$Author: shang $"

proc SetStatus {text} {
    global status 
    set status "[exec date] $text"
    update
}

set prevSector 0
proc UpdateCorrector {args} {
    global sector SFBValue DACValue prevSector errorCode
    if ![string length $sector] {
        return
    }
    if {$sector==$prevSector} {
        return
    }
    set pvList [list SFB:S${sector}C:H1:CurrentAO S${sector}C:H1:DacAI S${sector}C:BM:CurrentAO]
    set varList {SFBValue DACValue BMValue}
    if $prevSector {
        pv unlink $varList
    }
    SetStatus "Link pvs for $sector sector ..."
    if [pv linkw $varList $pvList] {
        return -code error "Error link $sector: $errorCode"
    }
    pv umon $varList
    set prevSector $sector
    SetStatus "done."
}

proc ApplyDelta {args} {
    set sign -1
    APSStrictParseArguments {sign}
    global sector delta factor
    set change [expr $sign * $delta]
    set wfFile /home/helios/oagData/sr/orbitControllaw/waveforms/hcorrInfo.sdds
    set runControlPV DP:S:OrbitControlLawXSDDS
    
    SetStatus "check if datapool is running..."
    if [catch {exec cavget -list=$runControlPV.RUN -pend=10 } running] {
        return -code error "Error reading $runControlPV.RUN: $running"
    }
    if !$running {
        SetStatus "Datapool is not running. can not apply changes to corrector."
        return
    }
    set tmpRoot /tmp/[APSTmpString]
    set corrName S${sector}C:H1
    if [catch {exec sddsprocess $wfFile  -reprint=par,WaveformPV,%s,DeltaWaveformPV \
                 "-redefine=col,Waveform,DeviceName \"$corrName\" streq ? $change : 0 $ " $tmpRoot.$corrName } result] {
        return -code error "error process datapool corrector: $result"
    }
    if [catch {exec sddswput $tmpRoot.$corrName 
        exec cavput -list=S${sector}C:BM:CurrentAO=[expr $factor * $change] -delta -pend=10  } result] {
        return -code error "Error change $corrName: $result"
    }
    SetStatus "$corrName changed by $change A."
}

# Needed for APSParseArguments
set status "Press return after you input corrector name"
APSApplication . -name CH1CorrectorPunchDown -version $CVSRevisionAuthor \
  -overview "pown down input corrector."

APSScrolledStatus .ss -parent .userFrame -textVariable status \
  -packOption "-side top -fill x" -height 4 -width 60 \
  -contextHelp "Provides execution status and operation hints."

set sectorList [join [exec sddsprocess /home/helios/oagData/sr/orbitControllaw/waveforms/hcorrInfo.sdds \
                        -match=col,DeviceName=*C:H1 -pipe=out -scan=col,Sector,DeviceName,S%ld,type=long  \
                        | sddsprocess -pipe "-filter=col,Sector,6,7,!" \
                        | sdds2stream -pipe -col=Sector]]
set delta 0.1 
APSLabeledOutputFrame .readback -parent .userFrame -label "C:H1 SFB/DAC readback and C:BM (A) " -variableList {SFBValue DACValue BMValue} \
  -orientation horizontal -width 12
APSLabeledEntry .step -parent .userFrame -label "Corrector change delta (A)" -textVariable delta -width 25
set factor 0.1
#the factor is calibrated to 0.1
set sector ""
APSLabeledEntry .factor -parent .userFrame -label "BM change factor" -textVariable factor -width 25
APSComboboxFrame .sector -parent .userFrame -label "CH1 Sector" -packOption "-fill x" \
  -textVariable sector \
  -itemList $sectorList \
  -width 24 \
  -editable 0 \
  -callback "UpdateCorrector -index"

#bind .userFrame.corr.entry <Return> "UpdateCorrector"
#bind .userFrame.corr.entry <Leave> "UpdateCorrector"
APSButton .plus -parent .userFrame  -text "Apply +"  -command "ApplyDelta -sign 1"
APSButton .minus -parent .userFrame -text "Apply -" -command "ApplyDelta -sign -1"
