
#
# $Log: not supported by cvs2svn $
# Revision 1.14  2011/10/14 20:17:05  shang
# added one more condition that injTime > 8.
#
# Revision 1.13  2011/10/14 20:09:29  shang
# changed the injection time to 12 instead of 10.
#
# Revision 1.12  2011/10/14 20:08:13  shang
# added APSPARScopeMeasTopUpKickersSetup proc for setup par topup kickers before collecting scope waveforms.
#
# Revision 1.11  2006/02/16 20:26:13  borland
# Added statusCallback option to several setup routines.  It isn't used but will be
# passed by new version of ScopeWaveformHistory.
#
# Revision 1.10  2005/09/23 15:30:46  soliday
# Updated to work with the new PRF:F:FBM PVs that used to be PRF:F:feedback PVs.
#
# Revision 1.9  2005/05/23 22:26:53  soliday
# Updated to work with new PRF:H:FBM PVs that used to be PRF:H:feedback PVs.
#
# Revision 1.8  2001/10/23 15:02:02  borland
# Now uses LPL SCR system instead of PLET.
#
# Revision 1.7  2001/09/10 17:48:41  soliday
# Commented out puts command that is outside of a procedure.
#
# Revision 1.6  2001/04/30 18:15:18  soliday
# Changed code to reflect the change in PAR RF llrf feedback PV names.
#
# Revision 1.5  1998/05/20 14:55:14  borland
# Now optionally prompts user for permission before changing any equipment
# configurations.
#
# Revision 1.4  1997/12/31 20:37:52  borland
# Various improvements.  Can now choose which PAR systems to shut down.
# No longer aborst when supplies don't have positive conditioning status
# after initiating rampdown.
#
# Revision 1.3  1997/01/24 17:04:38  borland
# Added restoration of a standard timing setup prior to measurements.
# Timing is then put back by the existing save/restore operation.
#
# Revision 1.2  1997/01/21 23:56:41  borland
# Changed data directory to oag area; increased kicker test setpoints to
# 25kV.
#
# Revision 1.1  1997/01/21 23:47:38  borland
# First version.
#
#

set parScopeDataDir /home/helios/oagData/par/scope/archiveData1
#puts stderr $parScopeDataDir

proc APSMpPARScopeMeasPMsInfo {} {
    return "This procedure measures waveforms for the PAR pulsed magnets using standard setpoints.  The magnets should be functional prior to execution."
}

proc APSMpPARScopeMeasPMs {args} {
    global status parScopeDataDir
    if [catch {APSMpDoHPScopeMeas -dataDir $parScopeDataDir \
                 -rootname PARScopeMeasPMs -statusCallback "APSSetVarAndUpdate status" \
                 -doPrompt 1 -SCRSystem LPL} result] {
        APSMpReturn error "$result"
    }
    APSMpReturn ok
}

proc APSMpPARScopeMeasRFInfo {} {
    return "This procedure measures waveforms for the PAR RF systems using standard setpoints.  The systems should be functional prior to execution."
}

proc APSMpPARScopeMeasRF {args} {
    global status parScopeDataDir
    if [catch {APSMpDoHPScopeMeas -dataDir $parScopeDataDir \
                 -rootname PARScopeMeasRF -statusCallback "APSSetVarAndUpdate status" \
                 -doPrompt 1 -SCRSystem LPL} result] {
        APSMpReturn error "$result"
    }
    APSMpReturn ok
}

proc APSPARScopeMeasPMsSetup {args} {
    set mode ""
    set ID -1
    set doPrompt 0
    set statusCallback APSNoOp
    if {[APSStrictParseArguments {ID mode doPrompt statusCallback}] || $ID<0 || $mode==""} {
        return -code error "APSPARScopeMeasPMsSetup: bad arguments"
    }

    global parScopeDataDir
    set snapshotFile /tmp/PARScopeMeasPMsSetup.$ID
    set requestFile $parScopeDataDir/PARScopeMeasPMsStdSetup.req
    set timingFile $parScopeDataDir/PLET-StandardTiming.snap

    switch $mode {
        setup {
            if $doPrompt {
                if {![APSMultipleChoice [APSUniqueName .] -modal 1 \
                        -question "This procedure will change the timing and pulsed magnet parameters.  Do you want to proceed?" \
                        -labelList {No Yes} -returnList {0 1}]} {
                    return "APSPARScopeMeasRFSetup: aborted by user."
                }
            }

            # Make a backup file
            if [catch {exec burtrb -f $requestFile -o $snapshotFile} result] {
                return -code error "APSPARScopeMeasPMsSetup: $result"
                exit 1
            }

            # Set standard values for PAR pulsed magnets
            # and turn them on.  Assume they are operational.
            #
            for {set try 0} {$try<3} {incr try} {
                if {[catch {exec cavput -pend=15 -list=P -list=1,2 -list=IK:VoltageSetSendAO=25} result] || \
                      [catch {exec cavput -pend=15 -list=P -list=1,2,4 -list=EK:VoltageSetSendAO=25} result] || \
                      [catch {exec cavput -pend=15 -list=P: -list=I,E -list=SP:VoltageSetSendAO=450} result]} {
                    return -code error "APSPARScopeMeasPMsSetup: $result"
                }
                # sleep, then repeat.
                exec sleep 2
            }
            
            # restore timing data 
            if [catch {exec sddscasr -restore $timingFile} result] {
                return -code error "APSPARScopeMeasPMsSetup: $result"
            }

            # turn on kicker HV
            # turn on charge pulses
            if {[catch \
                   {exec cavput -pend=15 -list=It:Ddg2chan4.GATE=1,It:ER1:er.OTL2=1,It:Par:PSPchargeBO.VAL=1} \
                   result] || \
                  [catch {exec cavput -pend=15 -list=P -list=1I,2I,4E -list=K:OnBO=1} result]} {
                return -code error "APSPARScopeMeasPMsSetup: $result"
            }
            exec sleep 3

            foreach PS {P1IK P2IK P4EK P:ISP} {
                if [catch {exec cavget -pend=15 -list=$PS:StatusCALC} result] {
                    return -code error "APSPARScopeMeasPMsSetup: $result"
                }
                if {[string compare $result ?]==0 || $result} {
                    return -code error "APSPARScopeMeasPMsSetup: bad status for $PS: $result"
                }
            }

            foreach trigger {It:ParIKickerChargeStatCC.SEVR It:EG2:ParEKickerChargeStatCC.SEVR \
                               It:ParInjChargeStatCC.SEVR It:ParExtChargeStatCC.SEVR} {
                if [catch {exec cavget -pend=15 -list=$trigger -numerical} result] {
                    return -code error "APSPARScopeMeasPMsSetup: $result"
                }
                if {[string compare $result ?]==0 || $result} {
                    return -code error "APSPARScopeMeasPMsSetup: trigger not on: $trigger=$result"
                }
            }
        }
        cleanup {
            if {[catch \
                   {exec cavput -pend=15 -list=It:Ddg2chan4.GATE=0,It:ER1:er.OTL2=0,It:Par:PSPchargeBO.VAL=0} \
                   result] } {
                return -code error "APSPARScopeMeasPMsSetup: $result"
            }
            for {set try 0} {$try<3} {incr try} {
                if {[catch {exec sddscasr -restore $snapshotFile} result]} {
                    return -code error "APSPARScopeMeasPMsSetup: $result"
                }
            }
        }
        default {
            return -code error "APSPARScopeMeasPMsSetup: bad mode: $mode"
        }
    }
}


proc APSPARScopeMeasRFSetup {args} {
    set ID -1
    set mode ""
    set doPrompt 0
    set statusCallback APSNoOp
    if {[APSStrictParseArguments {ID mode doPrompt statusCallback}] || $ID<0 || $mode==""} {
        puts stderr "APSPARScopeMeasRFSetup: bad arguments"
        exit 1
    }

    global parScopeDataDir
    set snapshotFile /tmp/PARScopeMeasRFSetup.$ID
    set requestFile $parScopeDataDir/PARScopeMeasRFStdSetup.req
    set timingFile $parScopeDataDir/PLET-StandardTiming.snap

    switch $mode {
        setup {
            if $doPrompt {
                if {![APSMultipleChoice [APSUniqueName .] -modal 1 \
                        -question "This procedure will change the timing and rf parameters.  Do you want to proceed?" \
                        -labelList {No Yes} -returnList {0 1}]} {
                    return "APSPARScopeMeasRFSetup: aborted by user."
                }
            }
            # Make a backup file
            if [catch {exec burtrb -f $requestFile -o $snapshotFile} result] {
                return -code error "APSPARScopeMeasRFSetup: $result"
            }
            # restore timing data 
            if [catch {exec sddscasr -restore $timingFile} result] {
                return -code error "APSPARScopeMeasPMsSetup: $result"
            }
            # set up rf systems, assuming they are operational
            # rf12
            if [catch {exec cavput -pend=15 \
                         -list=PRF:H:FBM2:pidSpCh1AO=1.45,PRF:H:FBM1:pidSpCh0AO=-1,PRF:H:FBM1:pidSpCh1AO=0} \
                  result] {
                return -code error "APSPARScopeMeasRFSetup: problem setting up rf12: $result"
            }
            # rf1
            if [catch {exec cavput -pend=15 \
                         -list=PRF:F:FBM2:pidSpCh1AO=2.30,PRF:F:FBM1:pidSpCh0AO=-0.9,PRF:F:FBM1:pidSpCh1AO=0} \
                  result] {
                return -code error "APSPARScopeMeasRFSetup: problem setting up rf1: $result"
            }
            # ensure rf on for both
            if [catch {exec cavput -list=PAR:RF1:abBoA0C7S0=1,PAR:RF12:abBoA1C5S2=1} result] {
                return -code error "APSPARScopeMeasRFSetup: problem turning on rf: $result"
            }
        }
        cleanup {
            if [catch {exec cavput -list=PAR:RF1:abBoA0C7S0=0,PAR:RF12:abBoA1C5S2=0} result] {
                return -code error "APSPARScopeMeasRFSetup: problem turning off rf: $result"
            }
            for {set try 0} {$try<3} {incr try} {
                if {[catch {exec sddscasr -restore $snapshotFile} result]} {
                    return -code error "APSPARScopeMeasRFSetup: $result"
                }
            }
        }
        default {
            return -code error "APSPARScopeMeasRFSetup: bad mode: $mode"
        }
    }
}

proc APSPARScopeMeasTopUpKickersSetup {args} {
    set mode ""
    set ID -1
    set system ""
    set statusCallback APSNoOp
    set doPrompt 0 
    if {[APSStrictParseArguments {ID mode system statusCallback doPrompt}] \
	    || $ID<0 || $mode==""} {
        return -code error "APSSRScopeMeasTopUpKickersSetup: bad arguments"
    }
    
    global parScopeDataDir apsScopeWaveformAbort 
    
    switch $mode {
        setup {
            # All we do is wait for top-up and monitor abort variable
            if [catch {APScavget -list=Mt:TopUpAutoEnableC -numerical} enabled] {
                return -code error "APSSRScopeMeasTopUpKickersSetup: $enabled"
            }
            if {$enabled == "?"} {
                return -code error "Cannot read Mt:TopUpAutoEnableC PV"
            }
            if !$enabled {
                return -code error "Top-up is not enabled"
            }
	    while {1} {
		eval $statusCallback "{[clock format [clock seconds] -format %T]: Waiting for top-up event.}"
		for {set i 0} {$i<10} {incr i} {
		    if [catch {APScavget -list=Mt:TopUpTime2Inject.VAL -printErrors} injTime] {
			return -code error "Unable to read topup counts: $injTime"
		    }
		    if {$injTime<15 && $injTime>5} {
			eval $statusCallback "{[clock format [clock seconds] -format %T]: Top-up is coming...}"
			return
		    }
                    if $apsScopeWaveformAbort {
                        return -code ok "Abort"
                    }
                    APSWaitWithUpdate -waitSeconds 2 -abortVariable apsScopeWaveformAbort \
			-updateInterval 1
                }
            }
        } 
        default {
        }
    }
}
