#!/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 index ""
set outputDir .
set outputRootname ""
set description ""
set doPlots 0
set debug 0
set verbose 0
set BTX 0
set args $argv
set usage "takeBTSflagImages \[-BTX \{0|1\}\] -index <integer> \[-outputDir <string>\] -outputRootname <string> \[-description <string>\] \[-verbose 1\] \[-debug 1\]"
APSParseArguments {index outputDir outputRootname description doPlots debug verbose BTX}

if {$outputRootname==""} {
    puts stderr "No value given to outputRootname"
    exit
}
# 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 inOutCommandList {In Out}
    lappend flagList BTS:FS4
    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}}    

# assert the path for the image save. The PV has once been changed.
exec caput LI:VD1:imageFilePath /home/helios4/image_data/linac
exec cavput -list=LI:VD1:fileIndexRoot=BTSimage.sdds
exec cavput -list=LI:VD1:fileIndexEnableC=On

set root BTSimage
# 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 {
        lappend cavputList [lindex $item 0]=[lindex $item 1]
    }
    exec cavput -list=[join $cavputList ,]

    # insert screen and stop pulsing
    exec cavput -list=${actuator}=[lindex $inOut 0]
    if [catch {SetPulsedMagnetEnables -location BoosterExtSpecial -state 0 \
             } result] {
        puts stderr $result
    }
    after 1000
    exec cavput -list=LI:VD1:captureModeC=1
    exec cavput -list=LI:VD1:freezeFrameC=Freeze

    set generation [exec cavget -list=LI:VD1:fileIndexNum]
    set generation [format %04ld $generation]
    
    # read background
    exec cavput -list=LI:VD1:imageSourceC=Original
    after 2000
    exec cavput -list=LI:VD1:captureBkgndC=1
    after 2000

    if [catch {TogglePulsedMagnetEnables -location GuntoBoosterExt \
             } result] {
        puts stderr $result
    }
    # subtracted
    exec cavput -list=LI:VD1:imageSourceC=Subtracted
    after 5000

    # save file
    if $verbose {
        puts stderr "Saving image..."
    }
    exec cavput "-list=LI:VD1:saveRegionC=ROI Only,LI:VD1:saveImageToFileC=1"

    after 8000

    if ![string length outputRootname] {
        exec cp /home/helios4/image_data/linac/${root}-${generation}.sdds .
        set outputFile ./${root}-${generation}.sdds
    } else {
        if {$index==""} {
            set outputFile $outputDir/$outputRootname.$flag
            exec cp /home/helios4/image_data/linac/${root}-${generation}.sdds $outputFile

        } else {
            set outputFile $outputDir/$outputRootname.$flag.[format %03ld $index]
            exec cp /home/helios4/image_data/linac/${root}-${generation}.sdds $outputFile
        }
    }

    if {$description != ""} {
        exec sddsprocess -noWarning $outputFile \
          -reprint=para,Description,[APSMakeSafeQualifierString $description]
    }
    # remove screen
    exec cavput -list=${actuator}=[lindex $inOut 1]
    
    if $doPlots {
        exec sddscontour -shade=128 $outputFile \
          -col=Index,HLine* -equal \
          -ystring=sparse=50,edit=%/HLine// \
          &
    }
}

exit


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