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

set debug 0
# set 
set BTX 0
set verbose 0
set args $argv
set usage "setupBTSflagFrames \[-BTX 1]]\nWhen beam is present, determines the optimum frame out of 15 and makes saves in the Camera Setup Calibration file. setting BTX 1 will use only BTS:FS2 and BTX:FX, assuming that BTS:BX is on."
APSParseArguments {debug verbose BTX}


# Removed BTS:FS1 because it is blasted with booster dipole synchrotron 
# radiation.
set flagList ""
set muxList ""
set actuatorCommandList ""
#lappend flagList BTS:FS1
#lappend muxList [list {VID:ICR:muxOut1C 24} {VID:A014:muxOut8C 3}]
#lappend actuatorCommandList BTS:FS1:act_cont_bo
#lappend inOutCommandList {IN OUT}
lappend flagList BTS:FS2
lappend muxList [list {VID:ICR:muxOut1C 24} {VID:A014:muxOut8C 4}]
lappend actuatorCommandList BTS:FS2:act_cont_bo
lappend inOutCommandList {IN OUT}
if $BTX {
    lappend flagList BTX:FS
    lappend muxList [list {VID:ICR:muxOut1C 24} {VID:A014:muxOut8C 2}]
    lappend actuatorCommandList BTX:FS:act_cont_bo
    lappend inOutCommandList {IN OUT}
} else {
    lappend flagList BTS:FS3
    lappend muxList [list {VID:ICR:muxOut1C 24} {VID:A014:muxOut8C 5}]
    lappend actuatorCommandList BTS:FCC1:FS3:Act1
    lappend flagList BTS:FS4
    lappend inOutCommandList {In Out}
    lappend muxList [list {VID:ICR:muxOut1C 24} {VID:A014:muxOut8C 6}]
    lappend actuatorCommandList BTS:FCC1:FS4:Act1
    lappend inOutCommandList {In Out}
    lappend flagList BTS:FS5
    lappend muxList [list {VID:ICR:muxOut1C 24} {VID:A014:muxOut8C 7}]
    lappend actuatorCommandList BTS:FCC2:FS5:Act1
    lappend inOutCommandList {In Out}
}    
# start pulsing
if [catch {TogglePulsedMagnetEnables -location GuntoBoosterExt \
         } result] {
    puts stderr $result
}

set tmpfile /tmp/[APSTmpString]

exec caput LI:VD1:captureModeC 1
   
# unfortunately we have three type of databases for the BTS flag actuators.
foreach flag $flagList mux $muxList actuator $actuatorCommandList  inOut $inOutCommandList {
    if $verbose {
        puts stderr "Doing $flag..."
    }
    set cavputList ""
    foreach item $mux {
        if $verbose {
            puts stderr "cavputList [lindex $item 0]=[lindex $item 1]"
        }
        lappend cavputList [lindex $item 0]=[lindex $item 1]
    }

    exec cavput -list=[join $cavputList ,]

    # insert screen 
    exec cavput -list=${actuator}=[lindex $inOut 0]
    after 5000

    # set large ROI
    exec cavput -list=LI:VD1:roi -list=X,Y -list=StartC=150
    exec cavput -list=LI:VD1:roi -list=X,Y -list=SizeC=200
    
    after 1000
    
    if $debug {
        puts stderr "Looking at profile peak..."
    }
    # need to acquire two consecutive waveforms for full second.
    set tmpfile /tmp/[APSTmpString]
    if [catch {exec sddswmonitor -PV=LI:VD1:y2HzProfilePeaksWF -interval=0.5 -step=2 $tmpfile } result] {
        puts stderr "setupBTSflagFrames(0): $result"
    }
    if [catch {exec sddsprocess $tmpfile -pipe=out \
                 -proc=LI:VD1:y2HzProfilePeaksWF,maximum,FrameDouble,position,functionof=Index \
                 -proc=LI:VD1:y2HzProfilePeaksWF,maximum,Peak \
                 -redef=para,Frame,FrameDouble,type=long \
                 | tee $tmpfile.yprofile.${flag} \
                 | sdds2stream -pipe=in -para=Frame} frameList] {
        puts stderr "setupBTSflagFrames(1): $frameList"
        exit
    }
    # figure out from the two frames which one has the largest peak.
    if [catch {exec sdds2stream -para=Peak $tmpfile.yprofile.${flag} \
             } peakList] {
        puts stderr "setupBTSflagFrames(2):$peakList"
        exit
    }
    if {[lindex $peakList 0] > [lindex $peakList 1]} {
        set frame [lindex $frameList 0]
    } else {
        set frame [lindex $frameList 1]
    }
    puts stderr "peaks detected for flag $flag at frames: $frameList. Select $frame"
    # set correct frame delay
    exec cavput -list=LI:VD1:syncFrameDelayC=$frame
    exec cavput -list=LI:VD1:CAL:applyAndSaveC.PROC=1
    # remove screen
    exec cavput -list=${actuator}=[lindex $inOut 1]
    after 1000
}
   
# stop pulsing
if [catch {SetPulsedMagnetEnables -location BoosterExtSpecial -state 0 \
         } result] {
    puts stderr $result
}

exit


# **************************** Emacs Editing Sequences *****************
# Local Variables:
# mode: tcl
# End:
