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

#
#
set CVSRevisionAuthor "\$Revision: 1.0 $ \$Author: shang $"


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
APSStandardSetup

set wfDir /home/helios/oagData/sr/orbitControllaw/waveforms
set hcorrList [exec sdds2stream -col=DeviceName $wfDir/hcorrInfo.sdds]
set vcorrList [exec sdds2stream -col=DeviceName $wfDir/vcorrInfo.sdds]
set hvarList $hcorrList
set vvarList $vcorrList
foreach corr $hcorrList {
    set $corr 0
}
foreach corr $vcorrList {
    set $corr 0
}

set delta 1.0
set corrStatus ""

proc SetStatus {text} {
    global sconfigConfigStatus
    set sconfigConfigStatus $text
    update
}

proc ApplyDelta {args} {
    global delta waitTime
    set wfDir /home/helios/oagData/sr/orbitControllaw/waveforms

    set tmpRoot /tmp/[APSTmpString]
    set hitemList {A:H1 A:H2 A:H3 A:H4 B:H4 B:H3 B:H2 B:H1 C:H1}
    set vitemList {A:V1 A:V2 A:V3 A:V4 B:V4 B:V3 B:V2 B:V1 C:V1}
    set applied 0
    foreach plane {h v} Plane {H V} {
        set itemList [set ${plane}itemList]
        set wfFile $wfDir/${plane}corrInfo.sdds
        if [catch {exec cavget -list=DP:${Plane}CorrVectorModeC.VAL -pend=30 -printErrors} corrMode] {
            return -code error "Error reading $plane corrector mode: $corrMode"
        }
        SetStatus "$corrMode"
        for {set sector 1} {$sector<=40} {incr sector} {
            foreach item $itemList {
                set corr S${sector}$item
                set var sconfig$corr
                global $var
                if [set $var] {
                    SetStatus "apply $delta delta Amps to $corr  ..."
                    if [catch {exec sddsprocess $wfFile -pipe=out \
                                 "-define=col,Waveform,DeviceName \"$corr\" strmatch ? $delta : 0 $ " \
                                 | sddsconvert -pipe=in $tmpRoot.$corr -rename=par,DeltaWaveformPV=WaveformPV } result] {
                        return -code error "Error processing waveform: $result"
                    }
                    switch $corrMode {
                        Vector {
                            if [catch {exec sddswput $tmpRoot.$corr -pend=30} result] {
                                return -code error "Error setting corrector vector delta: $result"
                            }
                        } 
                        Scalar {
                            if [catch {exec cavput -list=SFB:, -list=${corr}:CurrentAO=$delta -deltaMode -pend=10 } result] {
                                return -code error "Error setting corrector delta: $result"
                            }
                        }
                    }
                    APSWaitWithUpdate -waitSeconds $waitTime
                    incr applied
                }
            }
            
        }
    }
    if $applied {
        SetStatus "$applied correctors were applied $delta A changes."
    } else {
        SetStatus "No corrector were selected."
    }
}

APSApplication . -name "Apply SR Corrector Delta"  -version $CVSRevisionAuthor \
  -overview "apply corrector delta one by one"

set widget .config
set parent .userFrame
set rootname sconfig
set h_activeList ""
set v_activeList ""
set sconfigConfigStatus ""
set waitTime 1
APSFrameGrid $widget -parent $parent -yList {status top bot down}
APSFrame .but -parent $parent$widget.top

APSScrolledStatus .scrolled -parent $parent$widget.status -textVariable sconfigConfigStatus

set w $parent$widget.top.but.frame
set missingList ""
set missingListVar ""
set cbOption "-side top"
set buttonOrientation horizontal

APSSRCorrectorCheckButtons .shbut -parent $w -rootname ${rootname} -plane H \
  -sectorControl 1 \
  -orientation $buttonOrientation -packOption $cbOption

APSSRCorrectorCheckButtons .svbut -parent $w -rootname ${rootname} -plane V \
  -sectorControl 1 \
  -orientation $buttonOrientation -packOption $cbOption
        
APSSetSRSectorButtons -mode all-off -rootname $rootname  \
  -sectorCount 40 -itemList  {A:H1 A:H2 A:H3 A:H4 B:H4 B:H3 B:H2 B:H1 C:H1} \
  -missingList $missingList  -missingListVar $missingListVar

APSSetSRSectorButtons -mode all-off -rootname $rootname  \
  -sectorCount 40 -itemList  {A:V1 A:V2 A:V3 A:V4 B:V4 B:V3 B:V2 B:V1 C:V1} \
  -missingList $missingList  -missingListVar $missingListVar



APSLabeledEntry .delta -parent .userFrame.config.down -label "Corrector delta (A):" -textVariable delta -width 25 \
  -packOption "-side left" -contextHelp "the delta apply to selected corrector when apply button is clicked"
APSLabeledEntry .time -parent .userFrame.config.down -label "Wait time (seconds):" -textVariable waitTime -width 25 \
  -packOption "-side left" \
  -contextHelp "The wait time between applying two corrector deltas."

APSButton .apply -parent .userFrame -text "Apply" -command "ApplyDelta"
