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

proc APSMpLTPShutdownInfo {} {
    return "This procedure turns off the LTP in one sequence.  It turns off DC power supplies."
}

proc APSMpLTPShutdown {args} {
    global status

#    set mode "abrupt"
    set mode "ramped"
    set rampdownTime 60
    set Q 1
    set H 1
    set V 1
    set B1 1
    set DCPS 1
    APSStrictParseArguments {mode rampdownTime Q H V B1 DCPS}

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

    APSMpStep init -promptDialog APSMpLTPShutdownInitDialog -vars {mode rampdownTime Q H V B1}
    if {$Q || $H || $V || $B1} {
        if [string compare $mode "ramped"]==0 {
            # start DCPS rampdown
            APSMpStep "LTP: Ramp Down DCPS (optional)" -proc "APSMpLTPRampdown"
            if [catch {APSMpLTPRampdown -rampdownTime $rampdownTime -Q $Q -H $H -V $V -B1 $B1} result] {
                APSMpReturn error "Error initiating rampdown of LTP: $result"
            }
        }
        
        # Set all DCPS to zero
        APSMpStep "LTP: Set DCPS To Zero" -proc "APSMpLTPZeroDCPSSetpoints"
        if [catch {APSMpLTPZeroDCPSSetpoints -Q $Q -H $H -V $V -B1 $B1} result] {
            APSMpReturn error "Error setting LTP DCPS setpoints to zero: $result"
        }
        
        # turn off all DCPS
        APSMpStep "LTP: Turn Off DCPS" -proc "APSMpLTPTurnOffDCPS"
        if [catch {APSMpLTPTurnOffDCPS -Q $Q -H $H -V $V -B1 $B1} result] {
            APSMpReturn error "Error turning off LTP DCPS: $result"
        }
    }
    APSMpStep "LTP Shutdown Finished"
    APSMpReturn ok
}

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

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

    APSCheckButtonFrame .cb1 -parent $frame -label "Select Power Supplies to turn off" -buttonList "Q H V B1" -variableList "APSMpLTPShutdown(Q) APSMpLTPShutdown(H) APSMpLTPShutdown(V) APSMpLTPShutdown(B1)" -orientation horizontal
}

proc APSMpLTPZeroDCPSSetpointsInfo {} {
    return "This procedure sets all LTP DC power supplies to zero."
}

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

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

proc APSMpLTPTurnOffDCPS {args} {
    set Q 1
    set H 1
    set V 1
    set B1 1
    APSStrictParseArguments {Q H V B1}
    APSMpStep init -promptDialog "APSMpPromptInitDialog"


    if [catch {APSLinkToDeviceConfigPVs -device par} result] {
	return -code error "APSMpLTPTurnOffDCPS: $result"
    }
    
    if {$Q} {
	if {[catch {APSDevSend \
		      -releaseLinks 1 \
		      -group PSOps \
		      -operation TurnOff \
		      -deviceList {"LTP:Q1" "LTP:Q2" "LTP:Q3" "LTP:Q4" "LTP:Q5" "LTP:Q6" "LTP:Q7" "LTP:Q8" "LTP:Q9" "LTP:Q10"}} result]} {
	    APSMpReturn error "APSMpLTPTurnOffDCPS: Error turning off LTP Q DCPS: $result"
	}
    }
    if {$H} {
	if {[catch {APSDevSend \
		      -releaseLinks 1 \
		      -group PSOps \
		      -operation TurnOff \
		      -deviceList {"LTP:H1" "LTP:H2" "LTP:H3" "LTP:H4"}} result]} {
	    APSMpReturn error "APSMpLTPTurnOffDCPS: Error turning off LTP H DCPS: $result"
	}
    }
    if {$V} {
	if {[catch {APSDevSend \
		      -releaseLinks 1 \
		      -group PSOps \
		      -operation TurnOff \
		      -deviceList {"LTP:V1" "LTP:V2" "LTP:V3" "LTP:V4"}} result]} {
	    APSMpReturn error "APSMpLTPTurnOffDCPS: Error turning off LTP V DCPS: $result"
	}
    }
    if {$B1} {
	if {[catch {APSDevSend \
		      -releaseLinks 1 \
		      -group PSOps \
		      -operation TurnOff \
		      -deviceList {"LTP:B1"}} result]} {
	    APSMpReturn error "APSMpLTPTurnOffDCPS: Error turning off LTP B1: $result"
	}
    }
    
    
    if {[catch {APSDevSend \
		  -releaseLinks 1 \
                  -group PSOps \
                  -operation StopConditioning \
                  -deviceList {"LTP:B1" "LTP:H1" "LTP:H2" "LTP:H3" "LTP:H4" "LTP:V1" "LTP:V2" "LTP:V3" "LTP:V4" "LTP:Q1" "LTP:Q2" "LTP:Q3" "LTP:Q4" "LTP:Q5" "LTP:Q6" "LTP:Q7" "LTP:Q8" "LTP:Q9" "LTP:Q10"}} result]} {
	APSMpReturn error "APSMpLTPTurnOffDCPS: Error stopping conditioning of LTP DCPS: $result"
    }

    APSMpReturn ok
}

proc APSMpLTPRampdown {args} {
    global status
    global controlFilesDir
    
    set rampdownTime 60
    set block 1
    set Q 1
    set H 1
    set V 1
    set B1 1
    APSStrictParseArguments {rampdownTime block Q H V B1}

    APSMpStep "LTP: Configure Rampdown"
    set tmpFile /tmp/[APSTmpString]
    set rampdownMinutes [expr int($rampdownTime/60)]
    set rampdownSeconds [expr $rampdownTime-$rampdownMinutes*60]

    if {$H} {
	if [catch {exec cavput -list=LTP: -list=H -list=1,2,3,4 \
		       -list=:CurrentAO=0} result] {
	    APSMpReturn error "Error setting LTP H/V to zero: $result"
	}
    }
    if {$V} {
	if [catch {exec cavput -list=LTP: -list=V -list=1,2,3,4 \
		       -list=:CurrentAO=0} result] {
	    APSMpReturn error "Error setting LTP H/V to zero: $result"
	}
    }
    if {!($Q) && !($B1)} {
	APSMpReturn ok
    }
    if {($Q) && ($B1)} {
	set matchOption ""
    } elseif {$Q} {
	set matchOption "-match=column,ControlName=*Q*"
    } else {
	set matchOption "-match=column,ControlName=*B1*"
    }
    if [catch {eval exec sddsprocess $controlFilesDir/LTP/LTP_rampdown.std $tmpFile \
                 -redefine=column,NumCycles,-1,type=long \
                 -redefine=column,Minutes,$rampdownMinutes,type=long \
                 -redefine=column,Seconds,$rampdownSeconds,type=long $matchOption} result] {
        APSMpReturn error "Error during processing prior to LTP rampdown: $result"
    }
    APSMpStep "LTP: Initiate Rampdown"
    if [catch {exec standardize $tmpFile} result] {
        APSMpReturn error "Error initiating standardization for LTP rampdown: $result"
    }
    if [catch {exec rm $tmpFile} result] {
        APSMpReturn error "Error cleaning up after LTP rampdown: $result"
    }

    APSMpStep "LTP: Check Rampdown" -proc "APSMpPARLETCheckRampdown"
    if [catch {APSMpPARLETCheckRampdown -partList LTP -LTP_Q $Q -LTP_B1 $B1 -warnOnly 1} result] {
        APSMpReturn error "Error checking rampdown of LTP DCPS: $result"
    }
    
    if $block {
        APSMpStep "LTP: Wait For Rampdown (optional)" -proc "APSMpLTPRampdownWait"
        if [catch {APSMpLTPRampdownWait} result] {
            APSMpReturn error "Error waiting for rampdown of LTP DCPS: $result"
        }
    }
    APSMpReturn ok
}

proc APSMpLTPRampdownWait {} {
    while {[exec cavget -list=LTP:Q:BM:CondCALC] || \
             [exec cavget -list=LTP:HV:CondCALC]} {
        update
        after 1000
    }
    APSMpReturn ok
}

