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

APSStandardSetup

set CVSRevisionAuthor "Version 1.0, H. Shang"
# Needed for APSParseArguments

proc SetStatus {text} {
    global status
    set status "$text"
    update
}

proc BringUpADT {} {
    set oldDir [pwd]
    cd /home/helios/oagData/ADTFiles/srBpm
    exec adt -f sr.bpm.lowPass1s.error.pv &
    cd $oldDir
}


proc ChangeGain {knobName direction} {
    global knob
    set factor 10.0
    if {$direction=="down"} {
	set factor 0.1
    }
    set knob($knobName.gain) [format %0.3f [expr $knob($knobName.gain)*$factor]]
}

proc DisplayInfo {knobName} {
    global  dataDir
    APSFileDisplayWindow [APSUniqueName .] -fileName $dataDir/BMsteerKnob.${knobName}.print -width 130
}



set abort 0
proc Start {args} {

    global interval period  knob abort plane hAmp vAmp

    set t 0
    set amplitude [set ${plane}Amp]
    switch $plane {
	h {
	    set knob1 S:hSingVal1
	    set knob2 S:hSingVal2
	    #flip knobs for h planee
	   # set knob1 S:hSingVal2
	   # set knob2 S:hSingVal1
	}
	v {
	    set knob1 S:vSingVal1
	    set knob2 S:vSingVal2
	}
    }
    SetStatus "Start $plane orbit wave..."
    while {1} {
	if $abort {
	    SetStatus "$plane orbit wave abort."
	    set abort 0
	    return
	}
	set value1 [expr $amplitude * sin(2.0*3.14*$t/$period)]
	set value2 [expr $amplitude * cos(2.0*3.14*$t/$period)]
	
	
	foreach pv $knob($knob1.pvList) weight $knob($knob1.weightList) init $knob($knob1.initValue) {
	    set value [expr $knob($knob1.gain)*$weight*$value1]
	    set knob($pv.delta1) $value
	    set knob($pv.init) $init
	}
	set putList ""
	foreach pv $knob($knob2.pvList) weight $knob($knob2.weightList) init $knob($knob2.initValue) {
	    set value [expr $knob($knob2.gain)*$weight*$value2 ]
	    set knob($pv.delta2) $value
	    set knob($pv.init) $init
	    
	   
	    if [lsearch -exact $knob($knob1.pvList) $pv]>=0 {
		set value [expr $knob($pv.init) + $knob($pv.delta1) + $knob($pv.delta2)]
	    } else {
		set value [expr $knob($pv.init) + $knob($pv.detal2)]
	    }
	    
	    lappend putList $pv=$value
	}

	foreach pv $knob($knob1.pvList) {
	    if [lsearch -exact $knob($knob1.pvList) $pv]<0 {
		set value [expr $knob($pv.init) + $knob($pv.detal1)]
		lappend putList $pv=$value
	    }
	}
	
	SetStatus "setting $knob1=$value1, $knob2=$value2..."
	#puts $putList
	if [catch {APScavput -list=[join $putList ,] -pend=10 } result] {
	    return -code error "Error setting pvs: $result:
	}
	after [expr $interval * 1000]
	set t [expr $t+$interval]
    }
}
proc Abort {args} {
    global aborth abortv abort
    set plane ""
    APSParseArguments {plane}
    set abort$plane 1
    set abort 1
    SetStatus "$plane aborted."
}


proc Restore {args} {
    global dataDir
    foreach knob {S:hSingVal1 S:hSingVal2 S:vSingVal1 S:vSingVal2} {
	SetStatus "Restoring $knob correctors..."
	if [catch {exec sddscasr -restore $dataDir/BMsteerKnob.$knob}  result] {
	    return -code error "Error restore $knob1 correctors: $result"
	}
    }
    SetStatus "Restore correctors done."
}
#
# Set up graphical interface for tclKnobs
#
set status ""
set knobList ""
APSApplication . -name "TravelingOrbitWave" -version $CVSRevisionAuthor \
  -overview "create traveling orbit wavee with orbit knob." \
  -contextHelp "Assigns weighted vectors of process variables to tcl buttons."


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

set dataDir [APSGoToDailyDirectory -subdirectory BMSteer]
APSLabeledEntry .dir -parent .userFrame -width 70 -textVariable dataDir -label "Knob File dir:"

set interval 4
set hAmp 1.0
set vAmp 0.4
set period 30
APSLabeledEntry .interval -parent .userFrame -width 20 -textVariable interval -label "Interval:"
APSLabeledEntry .period -parent .userFrame -width 20 -textVariable period -label "Period:"
APSLabeledEntryFrame .amplitude -parent .userFrame -width 20 -variableList {hAmp vAmp} -label "H/V Amplitude:" \
    -orientation horizontal
APSLabel .note -parent .userFrame -text "Note that the above amplitude: 1.0 (h) and 0.4 (v) are when tune=0.1 for both planes,\n you need change the amplitude if the tune is different."

set plane h
APSRadioButtonFrame .plane -parent .userFrame -label "Plane:" -buttonList {h v} -valueList {h v} -variable plane -orientation horizontal \
    -variable plane


set knobFile  /home/helios/oagData/sr/knobs/orbit/lattices/default/orbit.cokn
foreach knobName {S:hSingVal1 S:hSingVal2 S:vSingVal1 S:vSingVal2} {
    if [catch {exec sddsprocess $knobFile  -pipe=out \
		   -match=par,ControlName=$knobName \
		   | sddscasr -pipe=in -save  $dataDir/BMsteerKnob.$knobName }  result] {
	return -code error "Error geting info of $knobName: $result"
    }
    if [catch {exec sddsprintout $dataDir/BMsteerKnob.$knobName $dataDir/BMsteerKnob.$knobName.print \
		   -par=ControlName -col=ControlName,format=%20s -col=Weight,format=%10.5f } result] {
	return -code error "Error printing $knobName: $result"
    }
    set knob($knobName.gain) [exec sdds2stream -par=Gain  $dataDir/BMsteerKnob.$knobName]
    set knob($knobName.pvList) [exec sdds2stream -col=ControlName $dataDir/BMsteerKnob.$knobName]
    set knob($knobName.weightList) [exec sdds2stream -col=Weight  $dataDir/BMsteerKnob.$knobName]
    set knob($knobName.initValue) [exec sdds2stream -col=ValueString $dataDir/BMsteerKnob.$knobName]
    
}

APSFrame .f2 -parent .userFrame -label ""
set w1 .userFrame.f2.frame
$w1 configure -bd 0
APSButton .info1 -parent $w1  -text "S:hSingVal1 Info" -command "DisplayInfo S:hSingVal1"
APSButton .info2 -parent $w1 -text "S:hSingVal2 Info" -command "DisplayInfo S:hSingVal2"
APSButton .info3 -parent  $w1 -text "S:vSingVal1 Info" -command "DisplayInfo S:vSingVal1"
APSButton .info4 -parent $w1 -text "S:vSingVal2 Info" -command "DisplayInfo S:vSingVal2"

APSFrame .f3 -parent .userFrame -label ""
set w1 .userFrame.f3.frame
$w1 configure -bd 0
APSButton .start -parent $w1 -text "Start" -command "Start"
APSButton .abort -parent $w1 -text "Abort" -command "set abort 1"
APSButton .restore -parent $w1 -text "Restore Correctors" -command "Restore"
APSButton .adt -parent $w1 -text "BPM ADT" -command BringUpADT

SetStatus "Ready."
