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

proc APSMpPARLETShutdownInfo {} {
    return "This procedure turns off the PAR/LET in one sequence. It turns off DC power supplies, rf systems, and pulsed magnets. Once you start the procedure, you will be given the opportunity to choose how the DC magnets should be brought down (either abruptly or by ramping)."
}

proc APSMpPARLETShutdown {args} {
    global status

#    set mode "abrupt"
    set mode "ramped"
    set rampdownTime 60
    set DCPS 1
    set PulsedPS 1
    set rfSystems 1
    set LTPB1 1

    APSStrictParseArguments {mode rampdownTime DCPS PulsedPS rfSystems LTPB1}

    APSMpStep init -promptDialog APSMpPARLETShutdownInitDialog -vars {mode rampdownTime DCPS PulsedPS rfSystems LTPB1}

    APSMpStep "PAR: Shutdown" -proc "APSMpPARShutdown"
    set PARid [APSMpParallel -mode Automatic -procedure \
                 "APSMpPARShutdown -mode $mode -rampdownTime $rampdownTime -DCPS $DCPS -PulsedPS $PulsedPS -rfSystems $rfSystems"]
    APSMpStep "LTP: Shutdown" -proc "APSMpLTPShutdown"
    set LTPid [APSMpParallel -mode Automatic -procedure \
                 "APSMpLTPShutdown -mode $mode -rampdownTime $rampdownTime -B1 $LTPB1 -DCPS $DCPS"]
    APSMpStep "PTB: Shutdown" -proc "APSMpPTBShutdown"
    set PTBid [APSMpParallel -mode Automatic -procedure \
                 "APSMpPTBShutdown -mode $mode -rampdownTime $rampdownTime -DCPS $DCPS"]

    APSMpStep "PAR/LET: Wait For Shutdown"
    if [catch {APSMpJoin $PARid} result] {
        APSMpReturn error "Error turning off PAR: $result"
    }
    if [catch {APSMpJoin $LTPid} result] {
        APSMpReturn error "Error turning off LTP: $result"
    }
    if [catch {APSMpJoin $PTBid} result] {
        APSMpReturn error "Error turning off PTB: $result"
    }
    APSMpReturn ok
}	

proc APSMpPARLETShutdownInitDialog {frame} {
    # Note: variables from root procedure are referenced as global array elements.
    #   This neccessary to prevent namespace collisions.
    global APSMpPARLETShutdown
    
    APSRadioButtonFrame .le1 -parent $frame -label "DC Magnet Mode" \
      -variable APSMpPARLETShutdown(mode) -buttonList {Ramped Abrupt} \
      -valueList {ramped abrupt} -contextHelp \
      "Choose whether to ramp DC supplies to zero or set them to zero abruptly."

    APSLabeledEntry .le2 -parent $frame -label "Seconds for rampdown" \
      -textVariable APSMpPARLETShutdown(rampdownTime) \
      -contextHelp "Give the approximate maximum number of seconds for rampdown."

    APSCheckButtonFrame .cb1 -parent $frame -label "Systems: " \
      -buttonList {rf DCPS "Pulsed PS" "LTP:B1"} \
      -allNone 1 \
      -variableList {APSMpPARLETShutdown(rfSystems) APSMpPARLETShutdown(DCPS) APSMpPARLETShutdown(PulsedPS) APSMpPARLETShutdown(LTPB1)} \
      -orientation vertical -allNone 1 -contextHelp \
      "Use to select or deselect systems to be included in startup."
}


proc APSMpPARLETZeroDCPSSetpoints {} {
    global status
    APSMpStep init -promptDialog "APSMpPromptInitDialog"
    if [catch APSMpLTPZeroDCPSSetpoints result] {
        APSMpReturn error "Error setting LTP DCPS setpoints to zero: $result"
    }
    if [catch APSMpPARZeroDCPSSetpoints result] {
        APSMpReturn error "Error setting PAR DCPS setpoints to zero: $result"
    }
    if [catch APSMpPTBZeroDCPSSetpoints result] {
        APSMpReturn error "Error setting PTB DCPS setpoints to zero: $result"
    }
    APSMpReturn ok
}

proc APSMpPARLETTurnOffDCPS {} {
    global status
    APSMpStep init -promptDialog "APSMpPromptInitDialog"
    if [catch APSMpLTPTurnOffDCPS result] {
        APSMpReturn error "Error turning LTP DCPS off: $result"
    }
    if [catch APSMpPARTurnOffDCPS result] {
        APSMpReturn error "Error turning PAR DCPS off: $result"
    }
    if [catch APSMpPTBTurnOffDCPS result] {
        APSMpReturn error "Error turning PTB DCPS off: $result"
    }
    APSMpReturn ok
}

proc APSMpPARLETRampdownInfo {} {
    return "This procedure ramps the PAR/LET magnets to zero in approximately 60 seconds."
}

proc APSMpPARLETStartRampdown {args} {
    global status

    set rampdownTime 60
    APSStrictParseArguments {rampdownTime}

    APSMpStep "LTP: Initiate Rampdown" -proc "APSMpLTPRampdown"
    if [catch {APSMpLTPRampdown -rampdownTime $rampdownTime -block 0} result] {
        APSMpReturn error "Error starting rampdown of LTP: $result"
    }

    APSMpStep "PAR: Initiate Rampdown" -proc "APSMpPARRampdown"
    if [catch {APSMpPARRampdown -rampdownTime $rampdownTime -block 0} result] {
        APSMpReturn error "Error starting rampdown of PAR: $result"
    }
    
    APSMpStep "PTB: Initiate Rampdown" -proc "APSMpPTBRampdown"
    if [catch {APSMpPTBRampdown -rampdownTime $rampdownTime -block 0} result] {
        APSMpReturn error "Error starting rampdown of PTB: $result"
    }

    APSMpStep "PAR/LET: Check Rampdown" -proc "APSMpPARLETCheckRampdown"
    if [catch {APSMpPARLETCheckRampdown -partList {PAR PTB LTP} } result] {
        APSMpReturn error "Error checking rampdown of power supplies: $result"
    }
}

proc APSMpPARLETCheckRampdown {args} {
    lappend partList PAR LTP PTB
    set warnOnly 0
    set LTP_Q 1
    set LTP_B1 1
    APSStrictParseArguments {partList warnOnly LTP_Q LTP_B1}
    set condList ""
    if [lsearch -exact $partList LTP]!=-1 {
	if {$LTP_Q} {
	    set condList \
		[exec cavget -list=LTP:Q -range=begin=1,end=10 \
		     -list=:StandardizeSUB.PROC -label]
	}
	if {$LTP_B1} {
	    set condList \
		[concat $condList \
		     [exec cavget -list=LTP:B -range=begin=1,end=1 \
			  -list=:StandardizeSUB.PROC -label]]
	} 
    }

    if [lsearch -exact $partList PAR]!=-1 {
        set condList \
          [concat $condList \
             [exec cavget -list=P:Q -list=1,2,4 -list=:StandardizeSUB.PROC -label]]
        set condList \
          [concat $condList \
             [exec cavget -list=P:S -list=1,F,D -list=:StandardizeSUB.PROC -label]]
        set condList \
          [concat $condList \
             [exec cavget -list=P:BM -list=:StandardizeSUB.PROC -label]]
    }

    if [lsearch -exact $partList PTB]!=-1 {
        set condList \
          [concat $condList \
             [exec cavget -list=PTB:Q -range=begin=1,end=11 \
                -list=:StandardizeSUB.PROC -label]]
        set condList \
          [concat $condList \
             [exec cavget -list=PTB:B -range=begin=1,end=2 \
                -list=:StandardizeSUB.PROC -label]]
    }

    if [llength $condList] {
        if $warnOnly {
            set mode ok
        } else {
            set mode error
        }
        set problemCond [APSSearchAssociateListForValue -list $condList -value 0]
        if [llength $problemCond] {
            regsub -all StandardizeSUB.PROC $problemCond CurrentAO statusList
            if [catch {exec cavget -list=[join $statusList ,] -label} statusResult] {
                APSMpReturn $mode "Problem checking current of supplies during rampdown check: $statusResult"
            }
            set problemList [APSSearchAssociateListForValue -list $statusResult -notvalue 0]
            if {[llength $problemList]} {
                APSMpReturn $mode \
                  "$mode: The PS associated with these PVs are not ramping:\n[join $problemList \n]"
            }
        }
    }
    APSMpReturn ok
}


