#
# $Log: not supported by cvs2svn $
# Revision 1.10  2006/02/16 23:09:10  borland
# Added a 1 second wait after detecting top-up to ensure that the scope waveform
# is updated.
#
# Revision 1.9  2006/02/16 21:12:33  borland
# Fixed typos in status callback commands.
#
# Revision 1.8  2006/02/16 20:29:44  borland
# Added APSSRScopeMeasTopUpKickersSetup, which is used basically to wait for
# a top-up event to occur in order to know when to acquire waveforms for
# kickers.
#
# Revision 1.7  2001/08/28 23:15:48  borland
# Now checks for existence of request file before using it.
#
# Revision 1.6  1998/05/20 15:08:34  borland
# Removed unused routines.  Added optional check of beam current.
#
# Revision 1.5  1998/05/20 14:56:19  borland
# Now optionally prompts user before doing anything that changes the machine
# state.
#
# Revision 1.4  1997/04/09 22:46:21  borland
# Kicker measurement script now checks for presence of beam and doesn't
# run if there is beam.
#
# Revision 1.3  1997/04/08 00:59:40  borland
# Added StoredBeam measuremnet.
#
# Revision 1.2  1997/03/12 20:55:18  borland
# Working version with general setup routine.
#
# Revision 1.1  1997/03/10 19:55:15  borland
# First version.  Not tested.
#
#

set srScopeDataDir /home/helios/oagData/sr/scope/archiveData1

proc APSSRScopeMeasSetup {args} {
    set mode ""
    set ID -1
    set system ""
    set doPrompt 0
    set changesSystem 0
    set abortIfBeamPresent 0
    if {[APSStrictParseArguments {ID mode system doPrompt abortIfBeamPresent changesSystem}] \
          || $ID<0 || $mode=="" || ![string length $system]} {
        return -code error "APSSRScopeMeasSetup: bad arguments"
    }

    global srScopeDataDir
    set backupFile /tmp/SRScopeMeas${system}Setup.$ID
    set setupFile $srScopeDataDir/SRScopeMeas${system}Setup.snap
    # request file is the same as the setup file, so we should be able
    # to recover existing config completely
    set requestFile $setupFile

    switch $mode {
        setup {
            if $abortIfBeamPresent {
                if [catch {exec cavget -list=S-DCCT:CurrentM} result] {
                    APSMpReturn error "APSSRScopeMeasSetup: problem reading beam current"
                }
		if {$result == "?"} {
		    APSMpReturn error "APSSRScopeMeasSetup: cannot read S-DCCT:CurrentM PV"
		}
                if [expr abs($result)>0.1] {
                    APSMpReturn error "APSSRScopeMeasSetup: can't do measurement--beam present."
                }
            }
            if {$doPrompt && $changesSystem} {
                if {![APSMultipleChoice [APSUniqueName .] -modal 1 \
                        -question "This procedure will change the $system parameters.  Do you want to proceed?" \
                        -labelList {No Yes} -returnList {0 1}]} {
                    APSMpReturn error "APSSRScopeMeasSetup: aborted by user"
                }
            }
            if [file exists $requestFile] {
                # Make a backup file
                if [catch {exec burtrb -f $requestFile -o $backupFile} result] {
                    return -code error "APSSRScopeMeasSetup: $result"
                    exit 1
                }
                
                # Send back snapshot file to establish standard conditions.
                if [catch {exec sddscasr -restore $setupFile} result] {
                    return -code error "APSSRScopeMeasSetup: $result"
                    exit 1
                }
            }
        }
        cleanup {
            if {[file exists $backupFile] && [catch {exec sddscasr -restore  $backupFile} result]} {
                return -code error "APSSRScopeMeasSetup: $result"
            }
        }
        default {
            return -code error "APSSRScopeMeasSetup: bad mode: $mode"
        }
    }
}

set srDeltaCharge ""

proc APSSRScopeMeasTopUpKickersSetup {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 srScopeDataDir 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"
            }
            if [catch {APScavget -list=Mt:TopUpLastInjectTime.VAL} lastInjection0] {
                return -code error "$lastInjection0"
            }
	    if {$lastInjection0 == "?"} {
		return -code error "Cannot read Mt:TopUpLastInjectTime.VAL PV"
	    }
            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:TopUpLastInjectTime.VAL} lastInjection1] {
                        return -code error "$lastInjection1"
                    }
		    if {$lastInjection1 == "?"} {
			return -code error "Cannot read Mt:TopUpLastInjectTime.VAL"
		    }
                    if [string compare $lastInjection0 $lastInjection1]!=0 {
                        # top-up has occurred
                        eval $statusCallback "{[clock format [clock seconds] -format %T]: Top-up has occurred.}"
                        after 1000
                        return
                    }
                    if $apsScopeWaveformAbort {
                        return -code ok "Abort"
                    }
                    APSWaitWithUpdate -waitSeconds 3 -abortVariable apsScopeWaveformAbort \
                      -updateInterval 1
                }
            }
        } 
        default {
        }
    }
}
