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

proc APSMpPARShutdownInfo {} {
    return "This procedure turns off the PAR in one sequence.  It turns off DC power supplies, rf systems, and pulsed magnets."
}

proc APSMpPARShutdown {args} {
    global status

    #    set mode "abrupt"
    set mode "ramped"
    set rampdownTime 60
    set DCPS 1
    set PulsedPS 1
    set rfSystems 1
    set BM 0
    APSStrictParseArguments {mode rampdownTime DCPS PulsedPS rfSystems BM}
    
    APSMpStep init -promptDialog APSMpPARShutdownInitDialog -vars {mode rampdownTime DCPS PulsedPS rfSystems BM}
    
    if $DCPS {
        # start DCPS rampdown
        set rdID ""
        if [string compare $mode "ramped"]==0 {
            APSMpStep "PAR: Ramp Down DCPS (optional)" -proc "APSMpPARRampdown"
            set rdID [APSMpParallel -procedure "APSMpPARRampdown -rampdownTime $rampdownTime" \
                        -mode Automatic]
        } 
    }
    
    if $rfSystems {
        APSMpStep "PAR: Turn Off Rf1 (optional)" -proc "APSMpPARTurnOffRf1"
        if [catch {APSMpPARTurnOffRf1} result] {
            APSMpReturn error "Error turning off PAR rf1: $result"
        }
        APSMpStep "PAR: Turn Off Rf12 (optional)" -proc "APSMpPARTurnOffRf12"
        if [catch {APSMpPARTurnOffRf12} result] {
            APSMpReturn error "Error turning off PAR rf12: $result"
        }
    }
    
    if $PulsedPS {
        APSMpStep "PAR: Turn Off Kickers (optional)" -proc "APSMpPARTurnOffKickers"
        if [catch {APSMpPARTurnOffKickers} result] {
            APSMpReturn error "Error turning off PAR kickers: $result"
        }
        APSMpStep "PAR: Turn Off Septum (optional)" -proc "APSMpPARTurnOffSeptum"
        if [catch {APSMpPARTurnOffSeptum} result] {
            APSMpReturn error "Error turning off PAR septum: $result"
        }
    }
    
    if $DCPS {
        if [string length $rdID] {
            # block on rampdown
            APSMpStep "PAR: Wait For Rampdown (optional)"
            if {[catch {APSMpJoin $rdID} rdResult]} {
                APSMpReturn error "APSMpPARShutdown: $rdResult"
            }
        }
        # set all supplies to zero
        APSMpStep "PAR: Set DCPS To Zero (optional)" -proc "APSMpPARZeroDCPSSetpoints"
        if [catch {APSMpPARZeroDCPSSetpoints -BM $BM} result] {
            APSMpReturn error "Error zeroing PAR DCPS setpoints: $result"
        }
        
        # turn all supplies off
        APSMpStep "PAR: Turn Off DCPS (optional)" -proc "APSMpPARTurnOffDCPS"
        if [catch {APSMpPARTurnOffDCPS -BM $BM} result] {
            APSMpReturn error "Error zeroing PAR DCPS setpoints: $result"
        }
    } else {
        if $BM {
            if [catch {exec cavput -list=P:BM:CurrentAO=0} result] {
                APSMpReturn error "Error setting PAR BM to zero: $result"
            }
            if [catch {exec cavput -list=P:BM:OffBO=1} result] {
                APSMpReturn error "Error turning off PAR BM: $result"
            }
            if [catch {exec cavput -list=P:BM:StandardizeSUB.PROC=0} result] {
                APSMpReturn error "Error stopping conditioning of PAR BM: $result"
            }
        }
    }

    APSMpReturn ok
}

proc APSMpPARZeroDCPSSetpoints {args} {
    set BM 1
    APSStrictParseArguments {BM}
    APSMpStep init -promptDialog APSMpPromptInitDialog
    if [catch \
          {exec cavput \
             -list=P2H1,P2V2,P2H3,P3H2,P3H1,P4H1,P4H2,P1H3,P1V2,P1H1,P1V1,P1V3,P2V1,P2V3,P3V1,P3V2,P4V1,P4V2,P:Q3BP,P1H2 \
             -list=:CurrentAO=0} result] {
        APSMpReturn error "Error zeroing PAR correctors: $result"
    }
    if [catch {exec cavput -list=P1,P2,P3,P4 -list=BMT1,BMT2 \
                 -list=:CurrentAO=0} result] {
        APSMpReturn error "Error zeroing PAR bend trims: $result"
    }
    if [catch {exec cavput -list=P: -list=Q1,Q2,Q4,SD,SF,S1 -list=:CurrentAO=0} result] {
        APSMpReturn error "Error zero PAR main supplies: $result"
    }
    if {$BM} {
        if {[catch {exec cavput -list=P: -list=BM -list=:CurrentAO=0} result]} {
            APSMpReturn error "Error zero PAR main supplies: $result"
        }
    }
    APSMpReturn ok
}


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

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

proc APSMpPARTurnOffDCPS {args} {
    set BM 1
    APSStrictParseArguments {BM}
    APSMpStep init -promptDialog "APSMpPromptInitDialog"
    if [catch {APSLinkToDeviceConfigPVs -device par} result] {
        return -code error "APSMpPARTurnOffDCPS: $result"
    }
    if {[catch {APSDevSend \
                  -releaseLinks 1 \
                  -group PSOps \
                  -operation TurnOff \
                  -deviceList {"P1BMT1" "P1BMT2" "P2BMT1" "P2BMT2" "P3BMT1" "P3BMT2" "P4BMT1" "P4BMT2" "P1H1" "P1H2" "P1H3" "P2H1" "P2H3" "P3H1" "P3H2" "P4H1" "P4H2" "P1V1" "P1V2" "P1V3" "P2V1" "P2V2" "P2V3" "P3V1" "P3V2" "P4V1" "P4V2" "P:Q1" "P:Q2" "P:Q3BP" "P:Q4" "P:S1" "P:SD" "P:SF"}} result]} {
        APSMpReturn error "APSMpPARTurnOffDCPS: Error turning off PAR DCPS: $result"
    }

    if {[catch {APSDevSend \
                  -releaseLinks 1 \
                  -group PSOps \
                  -operation StopConditioning \
                  -deviceList {"P1BMT1" "P1BMT2" "P2BMT1" "P2BMT2" "P3BMT1" "P3BMT2" "P4BMT1" "P4BMT2" "P1H1" "P1H2" "P1H3" "P2H1" "P2H3" "P3H1" "P3H2" "P4H1" "P4H2" "P1V1" "P1V2" "P1V3" "P2V1" "P2V2" "P2V3" "P3V1" "P3V2" "P4V1" "P4V2" "P:Q1" "P:Q2" "P:Q3BP" "P:Q4" "P:S1" "P:SD" "P:SF"}} result]} {
        APSMpReturn error "APSMpPARTurnOffDCPS: Error stopping conditioning of PAR DCPS: $result"
    }
    if $BM {
        if {[catch {APSDevSend \
                      -releaseLinks 1 \
                      -group PSOps \
                      -operation TurnOff \
                      -deviceList {"P:BM"}} result]} {
            APSMpReturn error "APSMpPARTurnOffDCPS: Error turning off PAR DCPS: $result"
        }
        
        if {[catch {APSDevSend \
                      -releaseLinks 1 \
                      -group PSOps \
                      -operation StopConditioning \
                      -deviceList {"P:BM"}} result]} {
            APSMpReturn error "APSMpPARTurnOffDCPS: Error stopping conditioning of PAR DCPS: $result"
        }
    }
    APSMpReturn ok
}

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

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

    if [catch \
          {exec cavput \
             -list=P2H1,P2V2,P2H3,P3H2,P3H1,P4H1,P4H2,P1H3,P1V2,P1H1,P1V1,P1V3,P2V1,P2V3,P3V1,P3V2,P4V1,P4V2,P:Q3BP,P1H2 \
             -list=:CurrentAO=0} result] {
        APSMpReturn error "Error zeroing PAR correctors: $result"
    }
    if [catch {exec cavput -list=P1,P2,P3,P4 -list=BMT1,BMT2 \
                 -list=:CurrentAO=0} result] {
        APSMpReturn error "Error zeroing PAR bend trims: $result"
    }
    if [catch {exec sddsprocess $controlFilesDir/PAR/PAR_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 PAR rampdown: $result"
    }

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

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

proc APSMpPARRampdownWait {} {
    while {[exec cavget -list=P:Q:BM:CondCALC] || \
             [exec cavget -list=P:S:CondCALC]} {
        update
        after 2000
    }
    APSMpReturn ok
}

proc APSMpPARTurnOffRf12Info {} {
    return "This procedure turns off the PAR rf12 system. Please refer to the document OpsDoc_PAR_PEM001_rev.2 under required reading for a detailed description of what this software does and how to use it."
}


proc APSMpPARTurnOffRf12 {} {
    APSMpStep init -promptDialog APSMpPromptInitDialog
    if [catch {exec cavput -list=PAR:RF12:abBoA1C5S2=Off} result] {
        APSMpReturn error "Error turning off rf input for rf12: $result"
    }
    if [catch {exec cavput -list=PAR:Har:RemoteStandBy=1} result] {
        APSMpReturn error "Error turning off rf amplifier for rf12: $result"
    }
    if [catch {exec cavput -list=PRF:H:FBM1:pidSpCh0AO=0.1,PRF:H:FBM2:pidSpCh1AO=0} result] {
        APSMpReturn error "Error writing feedback setpoints for rf12: $result"
    }
    
    APSMpReturn ok
}

proc APSMpPARTurnOffRf1Info {} {
    return "This procedure turns off the PAR rf1 system. Please refer to the document OpsDoc_PAR_PEM001_rev.2 under required reading for a detailed description of what this software does and how to use it."
}

proc APSMpPARTurnOffRf1 {} {
    APSMpStep init -promptDialog APSMpPromptInitDialog
    if [catch {exec cavput -list=PAR:RF1:abBoA0C7S0=Off} result] {
        APSMpReturn error "Error turning off rf input for rf1: $result"
    }
    if [catch {exec cavput -list=PRF:F:FBM1:pidSpCh0AO=0.1,PRF:F:FBM2:pidSpCh1AO=-10} result] {
        APSMpReturn error "Error writing feedback setpoints for rf1: $result"
    }
    APSMpReturn ok
}


proc APSMpPARTurnOffKickers {} {
    APSMpStep init -promptDialog "APSMpPromptInitDialog"
    if [catch {exec cavput -list=P1IK,P2IK,P4EK -list=:OnBO=OFF} result] { 
        APSMpReturn error "Error turning off PAR kickers: $result"
    }
    APSMpReturn ok
}

proc APSMpPARTurnOffSeptum {} {
    APSMpStep init -promptDialog "APSMpPromptInitDialog"
    if [catch {exec caput P:ISP:OffBO 1} result] {
        APSMpReturn error "Error turning off PAR septum: $result"
    }
    APSMpReturn ok
}

