
set PLETSnapshotDir /home/helios/oagData/SCR/snapshots/LPL
set controlFilesDir /home/helios/oagData/controlFiles

proc APSMpParBypassShutdownInfo {} {
    return "This procedure turns off the PAR Bypass in one sequence.  It turns off DC power supplies."
}

proc APSMpParBypassShutdown {args} {
    global status

#    set mode "abrupt"
    set Q 1
    set H 1
    set V 1
    set DCPS 1
    APSStrictParseArguments {Q H V DCPS}

    if {$DCPS == 0} {
        set Q 0
        set H 0
        set V 0
    }

    APSMpStep init -promptDialog APSMpParBypassShutdownInitDialog -vars {Q H V}
    if {$Q || $H || $V} {
        
        # Set all DCPS to zero
        APSMpStep "PAR Bypass: Set DCPS To Zero" -proc "APSMpParBypassZeroDCPSSetpoints"
        if [catch {APSMpParBypassZeroDCPSSetpoints -Q $Q -H $H -V $V} result] {
            APSMpReturn error "Error setting PAR Bypass DCPS setpoints to zero: $result"
        }
        
        # turn off all DCPS
        APSMpStep "PAR Bypass: Turn Off DCPS" -proc "APSMpParBypassTurnOffDCPS"
        if [catch {APSMpParBypassTurnOffDCPS -Q $Q -H $H -V $V} result] {
            APSMpReturn error "Error turning off PAR Bypass DCPS: $result"
        }
    }
    APSMpStep "ParBypass Shutdown Finished"
    APSMpReturn ok
}

proc APSMpParBypassShutdownInitDialog {frame} {
    # Note: variables from root procedure are referenced as global array elements.
    #   This neccessary to prevent namespace collisions.
    global APSMpParBypassShutdown
    
    APSCheckButtonFrame .cb1 -parent $frame -label "Select Power Supplies to turn off" -buttonList "Q H V" -variableList "APSMpParBypassShutdown(Q) APSMpParBypassShutdown(H) APSMpParBypassShutdown(V)" -orientation horizontal
}

proc APSMpParBypassZeroDCPSSetpointsInfo {} {
    return "This procedure sets all PAR Bypass DC power supplies to zero."
}

proc APSMpParBypassZeroDCPSSetpoints {args} {
    global status
    set Q 1
    set H 1
    set V 1
    APSStrictParseArguments {Q H V}
    APSMpStep init -promptDialog APSMpPromptInitDialog
    if {$H} {
	if [catch {exec cavput -list=PB: -list=H -list=1,2 \
		       -list=:CurrentAO=0} result] {
	    APSMpReturn error "Error setting PAR Bypass H/V to zero: $result"
	}
    }
    if {$V} {
	if [catch {exec cavput -list=PB: -list=V -list=1,2 \
		       -list=:CurrentAO=0} result] {
	    APSMpReturn error "Error setting PAR Bypass H/V to zero: $result"
	}
    }
    if {$Q} {
	if [catch {exec cavput -list=PB: -list=Q -range=begin=1,end=4 \
		       -list=:CurrentAO=0} result] {
	    APSMpReturn error "Error setting PAR Bypass quad to zero: $result"
	}
    }
    APSMpReturn ok
}

proc APSMpParBypassTurnOffDCPSInfo {} {
    return "This procedure turns off all PAR Bypass DC power supplies.  It does not verify that supplies actually turn off, as this may take several minutes."
}

proc APSMpParBypassTurnOffDCPS {args} {
    set Q 1
    set H 1
    set V 1
    APSStrictParseArguments {Q H V}
    APSMpStep init -promptDialog APSMpPromptInitDialog
    if {$Q} {
        if [catch {exec cavput -list=PB:Q -list=1,2,3,4 -list=:OffBO=1} result] {
            APSMpReturn error "Error turning off of PAR Bypass quads: $result"
        }
    }
    if {$H} {
        if [catch {exec cavput -list=PB:H -list=1,2 -list=:OffBO=1} result] {
            APSMpReturn error "Error turning off of PAR Bypass H Corredtors: $result"
        }
    }
    if {$V} {
        if [catch {exec cavput -list=PB:V -list=1,2 -list=:OffBO=1} result] {
            APSMpReturn error "Error turning off of PAR Bypass H Corredtors: $result"
        }
    }
    APSMpReturn ok
}


