#!/bin/sh -f
# \
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)]

#
# $Log: not supported by cvs2svn $
# Revision 1.12  2013/11/24 H. Shang
# modified to obtain the x/y start/stop range from injection controllaw offsets.
# 
# Revision 1.11  2003/09/30 17:37:32  sereno
# Added setup for bpms used in booster vertical injection controllaw.
#
# Revision 1.10  2003/07/28 21:49:12  sereno
# Added default averaging and offset parameters for operations bpm B2C8P2 and
# spare bpm B3C2P1.
#
# Revision 1.9  2003/07/28 21:21:47  sereno
# Changed the longitudinal peak search stop from 60 to 45.  This elminates
# detection of the second harmonic synchrotron peak when the synchrotron
# peak becomes less than the 2nd harmonic.
#
# Revision 1.8  2003/07/28 21:16:40  sereno
# Added second peak search start, stop and threshold PVs for betatron frequency
# peak search.  Added optional input arguments for the waveform offset and
# average factor.
#
# Revision 1.7  2003/07/17 19:32:22  sereno
# Changed default waveform averaging from 4 to 6.
#
# Revision 1.6  2002/01/23 17:36:09  sereno
# Changed setup command to set bpm backup trigger on.  N. Sereno.
#
# Revision 1.5  2002/01/19 00:10:12  sereno
# Added setup for time-domain waveform processing parameters offset, function,
# average number window type and frequency domain peak search start/stop
# channels and peak search threshold.
#
# Revision 1.4  2002/01/18 23:20:00  sereno
# Fixed cavput command that turned the BPM Force Free Run feature on instead
# of off when setting up the bpm for longitudinal correction.  N. Sereno
#
# Revision 1.3  2002/01/10 23:29:24  sereno
# Removed unneeded command line arguments.  N. Sereno.
#
# Revision 1.2  2002/01/10 23:26:46  sereno
# Added an error message if the bpm namestring has the wrong form.  N. Sereno.
#
# Revision 1.1  2002/01/10 22:24:53  sereno
# Booster bpm/beam history setup for use with longitudinal correction controllaw.
# This script replaces the BoosterBeamHistorySetup and BoosterBeamHistoryRestore
# scripts which have been cvs deleted.  N. Sereno
#
#

set args $argv
set lattice ""
APSParseArguments {lattice}

set tCardNumber 45_46
if ![string length $lattice] {
    if [catch {exec cavget -list=BOO:LatticeCALC -pend=10 -printErrors} lattice] {
        puts stderr "Unable to read booster lattice: $lattice"
    }
}

set delta 50
switch $lattice {
    92 {
        set xPeakStartP 15
        set xPeakStopP 40
        set offsetFile /home/helios/oagData/controllaw/BOOSTER-92nm/injTune/injTune.offset
        #set xPeakStart2P 150
        #set xPeakStop2P 300
        #set yPeakStartP 380
        #set yPeakStopP 500
    }
    132 {
        set xPeakStartP 15
        set xPeakStopP 40
        set offsetFile /home/helios/oagData/controllaw/BOOSTER-132nm/injTune/injTune.offset
        #set xPeakStart2P 300
        #set xPeakStop2P 500
        #set yPeakStartP 150
        #set yPeakStopP 250
    }
    default {
        #do not do anything for other lattice
        exit 0
    }
}
#changed the offset file to use current injection controllaw directory, 7/28/2014 H. Shang
set offsetFile /home/helios/oagData/controllaw/BOOSTER/injTune/injTune.offset
set parNames [exec sddsquery -par $offsetFile]
set putList ""
foreach par $parNames {
    set val [format %.0f [exec sdds2stream -par=$par $offsetFile]]
    lappend putList $par=$val
}
set xTuneOffset [exec sddsprocess $offsetFile -pipe=out -match=col,ControlName=B3C2P1:* | sdds2stream -pipe -col=Offset]
set yTuneOffset [exec sddsprocess $offsetFile -pipe=out -match=col,ControlName=B3C2P2:* | sdds2stream -pipe -col=Offset]

set sPeakStartP 15 
set sPeakStopP 40 
set xPeakStartP [format %.0f [expr ($xTuneOffset - $delta)*1024/815]]
set xPeakStopP [format %.0f [expr ($xTuneOffset + $delta)*1024/815]]
set yPeakStartP [format %.0f [expr ($yTuneOffset - $delta)*1024/815]]
set yPeakStopP [format %.0f [expr ($yTuneOffset + $delta)*1024/815]]

#setup horizontal (BPM:  B3C2P1)
set bpm B3C2P1
# Unlock BPM.
lappend putList ${bpm}:lock_bo.VAL=0

# Placing the BPM into X-Only mode and setup the bpm timing
# to detect exactly the first (injection) turn.
# Turning Force Free Run/Self Test/ off;
# Turning Backup Trigger on;
# Setting Gain to Low;
# Setting Ring to Sync;
# Setting Resync to Inhibit;
# Setting 250 ns delay to disable;
# Setting P0 to 0;
# Enabling the timing card start ramp signal;
# Setting timing variables for the first 272 turns (N=0 and M=68);
# Setting the global timing variables to "best" values; 
# Setting memory scanner average weight to 2;
# Setting mode to Delay;
# Setting Resync To LocalB to Disable;
# Setting Resync Reset to Disable.

lappend putList ${bpm}:disablex_bo=0
lappend putList ${bpm}:disabley_bo=1
lappend putList ${bpm}:disablesum_bo=0
lappend putList ${bpm}:ffreerun_bo=0
lappend putList ${bpm}:test_bo=0
lappend putList ${bpm}:backuptrig_bo=0
lappend putList ${bpm}:normal_bo=0
lappend putList ${bpm}:ring_bo=0
lappend putList ${bpm}:resync_bo=1
lappend putList B:bpmT${tCardNumber}:maxDelay_bo=1
lappend putList B:bpmT${tCardNumber}:P0_delay_lo=0
lappend putList B:bpmT${tCardNumber}:ext_start_bo=0
lappend putList B:bpmT${tCardNumber}:mode_bo=1
lappend putList B:bpmT${tCardNumber}:LBresyncDISbo=1
lappend putList B:bpmT${tCardNumber}:resync_reset_bo=1
lappend putList B:bpmT${tCardNumber}:Delay_count_lo=0
lappend putList B:bpmT${tCardNumber}:length_lo=0
lappend putList B:bpmT${tCardNumber}:Cycle_count_lo=32
lappend putList It:Ddg3chan6.DLY=0.29588
lappend putList It:Ddg9chan4.DLY=0.29720



# Set up the beam history module.  
# Setting the proper BPM index for the bpm name string; 
# Setting the fifo resync mode to disable;
# Setting the Fifo fill mode to "Stop on Full"; 
# Setting the Fifo read mode to enable; 
# Setting the Fifo mode to "Continuous"; 
# Enabling waveform acquisition;
# Enable FFT waveform processing;
# Set the longitudinal peak search start channel to 20 (15.91 kHz);
# Set the longitudinal peak search end channel to 45 (35.80 kHz);
# Set the horizontal peak search start channel to 200 (159.11 kHz);
# Set the horizontal peak search end channel to 370 (294.36 kHz);
# Set the longitudinal peak search threshold to 0;
# Set the horizontal peak search threshold to 0;
# Set the time-domain window to blackman;
# Set the time-domain processing to Sub/Ave;
# Set the time-domain waveform offset to 6;
# Set the time-domain number to average to 6.

lappend putList ${bpm}:bh.BPMI=44,${bpm}:bh.FIR0=0
lappend putList ${bpm}:bh.FIM0=1
lappend putList ${bpm}:bh.BRDE=1
lappend putList ${bpm}:bh.BRDM=1
lappend putList ${bpm}:bh:wfDisable_bo=1
lappend putList ${bpm}:fftFftDisableC=0
lappend putList ${bpm}:fftPeakSearchStartP=$sPeakStartP
lappend putList ${bpm}:fftPeakSearchStopP=$sPeakStopP
lappend putList ${bpm}:fftPeakFindThresP=0
lappend putList ${bpm}:ffsPeakSearchStartP=$xPeakStartP
lappend putList ${bpm}:ffsPeakSearchStopP=$xPeakStopP
lappend putList ${bpm}:ffsPeakFindThresP=0
lappend putList ${bpm}:fftWindowTypeC=1
lappend putList ${bpm}:bh:x:FunctionP=3
lappend putList ${bpm}s:bh:x:FunctionP=3

#puts "Booster bpm $bpm set to X-Only mode to detect first turn (injection) position."

# vertical bpm (B3C2P2)
set bpm B3C2P2

# If not a horizontal/longitudinal bpm, it must be one of the two vertical bpms.  Do a similar
# setup but don't need all the horizontal variable.

# Unlock BPM.
lappend putList ${bpm}:lock_bo.VAL=0

# Placing the BPM into Y-Only mode and setup the bpm timing
# to detect exactly the first (injection) turn.
# Turning Force Free Run/Self Test/ off;
# Turning Backup Trigger on;
# Setting Gain to Low;
# Setting Ring to Sync;
# Setting Resync to Inhibit;
# Setting 250 ns delay to disable;
# Setting P0 to 0;
# Enabling the timing card start ramp signal;
# Setting timing variables for the first 272 turns (N=0 and M=68);
# Setting the global timing variables to "best" values; 
# Setting memory scanner average weight to 2;
# Setting mode to Delay;
# Setting Resync To LocalB to Disable;
# Setting Resync Reset to Disable.

lappend putList ${bpm}:disablex_bo=1
lappend putList ${bpm}:disabley_bo=0
lappend putList ${bpm}:disablesum_bo=0
lappend putList ${bpm}:ffreerun_bo=0
lappend putList ${bpm}:test_bo=0
lappend putList ${bpm}:backuptrig_bo=0
lappend putList ${bpm}:normal_bo=0
lappend putList ${bpm}:ring_bo=0
lappend putList ${bpm}:resync_bo=1
lappend putList B:bpmT${tCardNumber}:maxDelay_bo=1
lappend putList B:bpmT${tCardNumber}:P0_delay_lo=0
lappend putList B:bpmT${tCardNumber}:ext_start_bo=0
lappend putList B:bpmT${tCardNumber}:mode_bo=1
lappend putList B:bpmT${tCardNumber}:LBresyncDISbo=1
lappend putList B:bpmT${tCardNumber}:resync_reset_bo=1
lappend putList B:bpmT${tCardNumber}:Delay_count_lo=0
lappend putList B:bpmT${tCardNumber}:length_lo=0
lappend putList B:bpmT${tCardNumber}:Cycle_count_lo=32
lappend putList It:Ddg3chan6.DLY=0.29588
lappend putList It:Ddg9chan4.DLY=0.29720 
# Set up the beam history module.  
# Setting the proper BPM index for the bpm name string; 
# Setting the fifo resync mode to disable;
# Setting the Fifo fill mode to "Stop on Full"; 
# Setting the Fifo read mode to enable; 
# Setting the Fifo mode to "Continuous"; 
# Enabling waveform acquisition;
# Enable FFT waveform processing;
# Set the vertical peak search start channel to 275 (109.39 kHz);
# Set the vertical peak search end channel to 370 (147.18 kHz);
# Set the vertical peak search threshold to 0;
# Set the time-domain window to blackman;
# Set the time-domain processing to Sub/Ave;
# Set the time-domain waveform offset to 6;
# Set the time-domain number to average to 6.

lappend putList ${bpm}:bh.BPMI=45
lappend putList ${bpm}:bh.FIR0=0
lappend putList ${bpm}:bh.FIM0=1
lappend putList ${bpm}:bh.BRDE=1
lappend putList ${bpm}:bh.BRDM=1
lappend putList ${bpm}:bh:wfDisable_bo=1
lappend putList ${bpm}:fftFftDisableC=0
lappend putList ${bpm}:fftPeakSearchStartP=$yPeakStartP
lappend putList ${bpm}:fftPeakSearchStopP=$yPeakStopP
lappend putList ${bpm}:fftPeakFindThresP=0
lappend putList ${bpm}:fftWindowTypeC=1
lappend putList ${bpm}:bh:y:FunctionP=3


if [catch {exec cavput -list=[join $putList ,] -pend=30} result] {
    puts stderr "Error setting pvs: $result"
}

if [catch {exec cavput -list=B3C2P1:fftWindowSUB.PROC=1,B3C2P1:ffsWindowSUB.PROC=1,B3C2P2:fftWindowSUB.PROC=1 -pend=10 } result] {
    puts stderr "Error setting compute button: $result"
}

#puts "Booster bpm $bpm set to Y-Only mode to detect first turns (injection) position."
 
exit 0
