
# Procedures for linac diagnostic timing scans and configuration.

proc ActOnSelectedDiagnostics {action args} {

    global timeinc avgfactor cmlist fclist bpmlist cmvlist fcvlist bpmvlist statusCallback
    global L1GM1 L1CM1 L3CM1 L5CM1 L3FC L5FCN L5FCS
    global L1PM1 L2PM1 L2PM2 L2PM3 L3PM1 L4PM1 L4PM2 L4PM3 L5PM2 L5PM3 L5PM4 L5FCNBPM

    # Loop over BPMs 

    if {![info exists timeinc] && $action=="Scan"} {
        $statusCallback "Timing scan time increment is not set."
        return 0
    }
    if {![info exists avgfactor] && $action=="Avg"} {
        $statusCallback "Average factor is not set."
        return 0
    }

    APSStrictParseArguments {statusCallback}

    set listindex 0

    foreach elem $bpmlist {
        set bpm [subst $[lindex $bpmvlist $listindex]]
        if {$bpm==1} {
            if {$action=="Scan"} {
                if {$statusCallback!=""} {
                    $statusCallback "Scanning BPM $elem."
                }
                exec caput -t -w 2 ${elem}:BPM.DINC $timeinc
                exec caput -t -w 2 ${elem}:BPM.SPD 1
                exec caput -t -w 2 ${elem}:BPM.AQWF 1
            } elseif {$action=="Avg"} {
                if {$statusCallback!=""} {
                    $statusCallback "Setting the average factor for BPM $elem."
                }
                exec caput -t -w 2 ${elem}:BPM.CAVG $avgfactor  
                after 250
            } else {APSAlertBox .alert[APSTmpString] -errorMessage "Configuration option is unknown."
                return 0}
        }
        incr listindex
    }

    # Loop over Current Monitors

    set listindex 0

    foreach elem $cmlist {
        set currentmonitor [subst $[lindex $cmvlist $listindex]]
        if {$currentmonitor==1} {
            if {$action=="Scan"} {
                if {$statusCallback!=""} {
                    $statusCallback "Scanning current monitor $elem."
                }
                exec caput -t -w 2 ${elem}:measCurrentCM.DINC $timeinc
                exec caput -t -w 2 ${elem}:measCurrentCM.SPD 1
                exec caput -t -w 2 ${elem}:measCurrentCM.AQWF 1
            } elseif {$action=="Avg"} {
                if {$statusCallback!=""} {
                    $statusCallback "Setting the average factor for current monitor $elem."
                }
                exec caput -t -w 2 ${elem}:measCurrentCM.CAVG $avgfactor
                after 250
            } else {APSAlertBox .alert[APSTmpString] -errorMessage "Configuration option is unknown."
                return 0}
        }
        incr listindex
    }

    # Loop over Faraday Cups

    set listindex 0

    foreach elem $fclist {
        set faradaycup [subst $[lindex $fcvlist $listindex]]
        if {$faradaycup==1} {
            if {$action=="Scan"} {
                if {$statusCallback!=""} {
                    $statusCallback "Scanning faraday cup $elem."
                }
                exec caput -t -w 2 ${elem}:measCurrentCM.DINC $timeinc
                exec caput -t -w 2 ${elem}:measCurrentCM.SPD 1
                exec caput -t -w 2 ${elem}:measCurrentCM.AQWF 1
            } elseif {$action=="Avg"} {
                if {$statusCallback!=""} {
                    $statusCallback "Setting the average factor for faraday cup $elem."
                }
                exec caput -t -w 2 ${elem}:measCurrentCM.CAVG $avgfactor
                after 250
            } else {APSAlertBox .alert[APSTmpString] -errorMessage "Configuration option is unknown."
                return 0}
        }
        incr listindex
    }
    if {$statusCallback!=""} {
        $statusCallback "Linac diagnostics configuration complete."
    }
    return 1
}

proc PerformActionOnSelectedDiagnostics {action args} {

    global statusCallback
    
    set statusCallback ""

    APSStrictParseArguments {statusCallback}

    if {$action=="Scan"} {
        if {$statusCallback!=""} {
            $statusCallback "Linac timing scan option selected."
            after 2000
        }
        ActOnSelectedDiagnostics $action -statusCallback 
    } elseif {$action=="Avg"} {
        if {$statusCallback!=""} {
            $statusCallback "Linac average factor option selected."
            after 2000
        }
        ActOnSelectedDiagnostics $action -statusCallback 
    } else {
        if {$statusCallback!=""} {
            $statusCallback "Configuration option is unknown."
        }
        APSAlertBox .alert[APSTmpString] -errorMessage "Configuration option is unknown."
        return 0
    }
    return 1
}


