#
# $Log: not supported by cvs2svn $
 
proc APSMpBCMSetBaselineInfo {args} {
  return "This procedure is used for setting bunch current motinor (BCM) baseline."
}

#This procedure has been placed in the attic
proc APSMpBCMSetBaseline {args} {
    APSMpStep "Read SR beam current and attenuators."
    if [catch {exec cavget -list=S35DCCT:currentCC -pend=30} current] {
        APSMpReturn error "APSMpBBCMSetBaseline: $current"
    }
    if [catch {exec cavget -list=BNCHI:AttenuatorC.VAL -pend=30} origValue] {
        APSMPReturn error "APSMpBBCMSetBaseline: $origValue"
    }
    if {$current=="?"} {
        APSMpReturn error "APSMpBBCMSetBaseline: can not read value of S35DCCT:currentCC"
    }
    if {$origValue=="?"} {
        APSMpReturn error "APSMpBBCMSetBaseline: can not read value of BNCHI:AttenuatorC.VAL"
    }
    if {$current<0.1} {
        #no SR beam
        APSMpStep "Set bunch current monitor baseline."
        if [catch {exec cavput "-list=BNCHI:AttenuatorC.VAL=33 dB" -pend=30} result] {
            APSMPReturn error "APSMpBBCMSetBaseline: $result"
        }
        after 3000
        if [catch {exec cavput -list=BNCHI:DoBaselineBO.VAL=1 -pend=30} result] {
            APSMPReturn error "APSMpBBCMSetBaseline: $result"
        }
    } else {
        #with SR beam
        APSMpStep "Read the bunch pattern."
        if [catch {exec cavget -list=SR:bunchPatternSO -pend=30} bunchPattern] {
            APSMPReturn error "APSMpBBCMSetBaseline: $bunchPattern"
        }
        regsub -all {\"} $bunchPattern {} bunchPattern
        if [catch {exec sddsprocess /home/helios/oagData/sr/bunchPatterns/presetPatterns \
                       -match=col,Label=$bunchPattern -pipe=out \
                       | sdds2stream -pipe -col=Attenuator} refValue] {
            APSMpReturn error "APSMpBBCMSetBaseline: $refValue"
        }
        set refValue [join $refValue]
        if {$refValue!=$origValue} {
            set answer [APSMultipleChoice [APSUniqueName .] -modal 1 -question \
                            "The current attenutator is $origValue, different from the reference value - $refValue, would you like to\n\n set the attentuator to $refValue and continue\n OR \n continue anyway \n OR \nabort?" \
                            -labelList {"Set and Continue" "Continue Anyway" "Abort"} \
                            -returnList {0 1 2}]
            switch $answer {
                0 {
                    regsub -all {\"} $refValue {} putValue
                    if [catch {exec cavput "-list=BNCHI:AttenuatorC=$putValue" -pend=30} result] {
                        APSMPReturn error "APSMpBBCMSetBaseline: $result"
                    }
                }
                1 {}
                2 {
                    APSMpReturn error "The current attenuator setting ($origValue) is different from reference ($refValue). The baseline is not set."
                }
            }
        }
        if [regexp {1296} $bunchPattern] {
            APSMpStep "set the attenuator to 44 dB"
            if [catch {exec cavput "-list=BNCHI:AttenuatorC.VAL=44 dB" -pend=30} result] {
                APSMPReturn error "APSMpBBCMSetBaseline: $result"
            }
            after 3000
        }
        APSMpStep "Read bunch current raw waveform"
        set tmpfile /tmp/[APSTmpString]
        APSAddToTmpFileList -ID 1 -fileList "$tmpfile $tmpfile.proc"
        if [catch {exec sddswget $tmpfile -pvnames=ADC750:DataWF} result] {
            APSMpReturn error "APSMpBBCMSetBaseline: $result"
        }
        if [catch {exec sddsprocess $tmpfile $tmpfile.proc \
                       -process=Waveform,baselevel,Baseline,functionOf=Index } result] {
            APSMpReturn error "APSMpBBCMSetBaseline: $result"
        }
        APSMpStep "Set the bunch current baseline."
        set baseline [exec sdds2stream $tmpfile.proc -par=Baseline]
        if [catch {exec cavput -list=BNCHI:BaseLineAO.VAL=$baseline -pend=30} result] {
            APSMPReturn error "APSMpBBCMSetBaseline: $result"
        }
        if [regexp {1296} $bunchPattern] {
            after 3000
            APSMpStep "Restore attenuator value."
            regsub -all {\"} $origValue {} origValue
            if [catch {exec cavput "-list=BNCHI:AttenuatorC.VAL=$origValue" -pend=30} result] {
                APSMPReturn error "APSMpBBCMSetBaseline: $result"
            }
        }
    }
    APSMpReturn ok
}
