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

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

proc APSMpPTBShutdown {args} {
    global status

#    set mode "abrupt"
    set mode "ramped"
    set rampdownTime 60
    set DCPS 1
    APSStrictParseArguments {mode rampdownTime DCPS}

    APSMpStep init -promptDialog APSMpPTBShutdownInitDialog -vars {mode rampdownTime}

    if {$DCPS} {
        if [string compare $mode "ramped"]==0 {
            # start DCPS rampdown
            APSMpStep "PTB: Ramp Down DCPS (optional)" -proc "APSMpPTBRampdown"
            if [catch {APSMpPTBRampdown -rampdownTime $rampdownTime} result] {
                APSMpReturn error "Error initiating rampdown of PTB: $result"
            }
        }
        
        # Set all DCPS to zero
        APSMpStep "PTB: Set DCPS To Zero" -proc "APSMpPTBZeroDCPSSetpoints"
        if [catch {APSMpPTBZeroDCPSSetpoints} result] {
            APSMpReturn error "Error setting PTB DCPS setpoints to zero: $result"
        }
        
        # turn off all DCPS
        APSMpStep "PTB: Turn Off DCPS" -proc "APSMpPTBTurnOffDCPS"
        if [catch {APSMpPTBTurnOffDCPS} result] {
            APSMpReturn error "Error turning off PTB DCPS: $result"
        }
    }
    APSMpStep "PTB Shutdown Finished"
    APSMpReturn ok
}


proc APSMpPTBShutdownInitDialog {frame} {
    # Note: variables from root procedure are referenced as global array elements.
    #   This neccessary to prevent namespace collisions.
    global APSMpPTBShutdown
    
    APSRadioButtonFrame .le1 -parent $frame -label "DC Magnet Mode" \
      -variable APSMpPTBShutdown(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 APSMpPTBShutdown(rampdownTime) \
      -contextHelp "Give the approximate maximum number of seconds for rampdown."
}

proc APSMpPTBZeroDCPSSetpointsInfo {} {
    return "This procedure sets all PTB DC power supplies to zero."
}

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

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

proc APSMpPTBTurnOffDCPS {} {
    APSMpStep init -promptDialog APSMpPromptInitDialog

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

proc APSMpPTBRampdown {args} {
    global status
    global controlFilesDir
    
    set rampdownTime 60
    set block 1
    APSStrictParseArguments {rampdownTime block}

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

    if [catch {exec cavput -list=PTB: -list=H,V -list=1,2,3,4 \
                 -list=:CurrentAO=0} result] {
        APSMpReturn error "Error setting PTB H/V to zero: $result"
    }
    if [catch {exec sddsprocess $controlFilesDir/PTB/PTB_rampdown.std $tmpFile \
                 -redefine=column,NumCycles,-1,type=long \
                 -redefine=column,Minutes,$rampdownMinutes,type=long \
                 -redefine=column,Seconds,$rampdownSeconds,type=long} result] {
        APSMpReturn error "Error during processing prior to PTB rampdown: $result"
    }

    APSMpStep "PTB: Initiate Rampdown"
    if [catch {exec standardize $tmpFile} result] {
        APSMpReturn error "Error initiating standardization for PTB rampdown: $result"
    }
    if [catch {exec rm $tmpFile} result] {
        APSMpReturn error "Error cleaning up after PTB rampdown: $result"
    }

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

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

