#!/bin/sh
# \
  exec oagtclsh "$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)]

# Injection Septum feed-forward compensation
set usage "SeptumFFCompensation -gain <gain> -shift <tickValue> -doPlot \{0|1\}"
set doPlot 0
# some common values
set shift 0
set gain 1
# Individual settings
set S38Coeff 1
set S39Coeff 1
set S38Shift 1.5 
set S39Shift 3
set args $argv
APSStrictParseArguments {S38Coeff S39Coeff S38Shift S39Shift gain  doPlot}

set ffWaveFile /home/helios/srfbData/waveforms/s38s39ah3.vec
set upstreamName "S37FB:S38A:H3"
set downstreamName "S39FB:S39A:H3"

set upstreamWF "$upstreamName:feedForwardC"
set upstreamEN "$upstreamName:feedForwardEnC"
set downstreamWF "$downstreamName:feedForwardC"
set downstreamEN "$downstreamName:feedForwardEnC"

set tmpfile /tmp/[APSTmpString]

#  Apply common values
exec sddsprocess $ffWaveFile $tmpfile.up \
  "-redef=col,$upstreamName,$upstreamName $S38Coeff * $gain *" \
		  -reprint=para,WaveformPV,$upstreamName:feedForwardC \
		  -redef=col,Waveform,$upstreamName 

exec sddsprocess $ffWaveFile $tmpfile.down \
  "-redef=col,$downstreamName,$downstreamName $S39Coeff * $gain *"  \
		  -reprint=para,WaveformPV,$downstreamName:feedForwardC \
		  -redef=col,Waveform,$downstreamName 


if [catch {exec sdds2stream -rows $ffWaveFile  \
         } points] {
    puts stderr "$points"
    exit
}
set points [lindex $points 0]

if {$S38Shift} {
    set start [expr $S38Shift * -1]
    set end [expr $start + $points - 1]
    file copy -force -- ${tmpfile}.up $tmpfile
    if [catch {exec sddsprocess $tmpfile -pipe=out \
                 | sddsinterp -pipe \
                 -col=Index,$upstreamName \
                 -sequence=$points,$start,$end \
                 -aboveRange=value=0 -belowrange=value=0 \
                 | sddsprocess -pipe=in ${tmpfile}.up -noWarning \
		  -reprint=para,WaveformPV,$upstreamName:feedForwardC \
		  -redef=col,Waveform,$upstreamName \
                 "-redef=col,Index,i_row"
             } result] {
        puts stderr "$result"
        exit
    }
}
if {$S39Shift} {
    set start [expr $S39Shift * -1]
    set end [expr $start + $points - 1]
    file copy -force -- ${tmpfile}.down $tmpfile
    if [catch {exec sddsprocess $tmpfile -pipe=out \
                 | sddsinterp -pipe \
                 -col=Index,$downstreamName \
                 -sequence=$points,$start,$end \
                 -aboveRange=value=0 -belowrange=value=0 \
                 | sddsprocess -pipe=in ${tmpfile}.down -noWarning \
		  -reprint=para,WaveformPV,$downstreamName:feedForwardC \
		  -redef=col,Waveform,$downstreamName \
                 "-redef=col,Index,i_row"
             } result] {
        puts stderr "$result"
        exit
    }
}

if $doPlot {
    exec sddsplot -layout=1,2 -grap=line,vary \
      -col=Index,$upstreamName $ffWaveFile -leg=spec=original \
      -col=Index,$upstreamName ${tmpfile}.up -leg=spec=modified -endpanel \
      -col=Index,$downstreamName $ffWaveFile -leg=spec=original \
      -col=Index,$downstreamName ${tmpfile}.down -leg=spec=modified  &
}


# read waveforms from file
if [catch {exec sdds2stream $tmpfile.up -col=$upstreamName} upstreamData] {
    puts stderr "Error reading normalized vector: $upstreamData"
    return
}
if [catch {exec sdds2stream $tmpfile.down -col=$downstreamName} downstreamData] {
    puts stderr "Error reading normalized vector: $downstreamData"
    return
}


# download waveforms to corrector channels
if [catch {eval exec sddswput ${tmpfile}.up} result] {
    puts stderr "Error putting to waveform records 1: $result"
    return
}
if [catch {eval exec sddswput ${tmpfile}.down} result] {
    puts stderr "Error putting to waveform records 2: $result"
    return
}

# enable channels and push 'load'
if [catch {exec cavput -pend=15 -list=$upstreamEN=1,$downstreamEN=1} error ] {
    puts stderr $error; update
    puts stderr "Failure"
    return
}
if [catch {exec cavput -pend=15 -list=SRFB:GBL:loadBO=1,SRFB:GBLM:DSPLoadParamsBO=1} error] {
    puts stderr $error
    return
}

exit


# Local Variables:
# mode: tcl
# indent-tabs-mode: nil
# End:
