
#
# $Log: not supported by cvs2svn $
# Revision 1.6  2009/01/28 23:08:25  emery
# Improved description of APSMpTransferBpmRFErrorsToRFSetpoint.
#
# Revision 1.5  2005/10/12 04:39:38  emery
# Changed back default for plane in APSMpTransferBpmErrorsToSetpoint to both.
#
# Revision 1.4  2005/10/11 22:02:06  emery
# Added APSMpTransferBpmRFErrorsToRFSetpoint.
#
# Revision 1.3  2005/10/06 18:41:35  emery
# Removed BM xray bpms from default in transfer procedure.
#
# Revision 1.2  2004/02/17 21:38:18  shang
# return errors if only BM bpm is selected for x plane
#
# Revision 1.1  2004/02/17 21:20:25  shang
# first version, transfer bpm errors to setpoint
#
# First version. H. Shang
#

proc APSMpTransferBpmErrorsToSetpointInfo {} {
    return "Transfers msAve bpm errors to setpoints.\n\nFor startup it is recommended to transfer Nb rf bpms, Mp rf bpms, and BM x-ray bpms (not ID x-ray bpms).\n\nFor operations, say after a fill from 0 mA, it is recommended to transfer only the Nb rf bpms and Mp rf bpms (no x-ray bpms).\n\nThe default settings of the initial dialog box is the recommended settings for operations."
}

proc APSMpTransferBpmErrorsToSetpointDialog {frame} {
    global APSMpTransferBpmErrorsToSetpoint status
    
    set status "Select plane and bpm type."
    pack [frame $frame.f1]
    pack [label $frame.f1.label -text "Transfer Plane and Bpm Type Selection"]
    APSRadioButtonFrame .plane -parent $frame.f1 -label "plane:   " -buttonList {x y both} \
      -valueList {x y both} -variable APSMpTransferBpmErrorsToSetpoint(plane) \
      -contextHelp "select plane for transferring bpm errors to setpoint." \
      -orientation horizontal
    APSCheckButtonFrame .type -parent $frame.f1 -buttonList {"rf Mp" "rf Nb" ID BM} \
      -allNone 1 -orientation horizontal \
      -label "BPM type:" -variableList {APSMpTransferBpmErrorsToSetpoint(rfMp) \
                                          APSMpTransferBpmErrorsToSetpoint(rfNb) \
                                          APSMpTransferBpmErrorsToSetpoint(ID) \
                                          APSMpTransferBpmErrorsToSetpoint(BM) }
    return
}

proc APSMpTransferBpmErrorsToSetpoint {args} {
    set plane both
    foreach type {rfMp rfNb ID} {
        set $type 1
    }
    set ID 0 
    set BM 0
    APSStrictParseArguments {plane rfMp rfNb ID BM}
    
    APSMpStep init \
      -promptDialog APSMpTransferBpmErrorsToSetpointDialog \
      -vars "plane rfMp rfNb ID BM" -proc "APSMpTransferBpmErrorsToSetpoint"
    
    set xplane 0
    set yplane 0
    if {$plane=="both"} {
        set planes {x y}
        set Planes {H V}
    } else {
        set planes $plane
        switch $plane {
            x {set Planes H}
            y {set Planes V} 
        }
    }
    if {!$rfMp && !$rfNb && !$ID && !$BM} {
        APSMpReturn error "No bpms were selected."
    }
    if {!$rfMp && !$rfNb && !$ID && $BM && $plane=="x"} {
        APSMpReturn error "No valid bpms were selected for x plane"
    }
    set firstTerm 1
    if $rfMp {
        set matchOption "-match=col,ElectronicsType=Monopulse,ElectronicsType=FPGA,|"
        set firstTerm 0
    }
    if $rfNb {
        if $firstTerm {
            set matchOption "-match=col,ElectronicsType=Narrowband"
        } else {
            append matchOption ",ElectronicsType=Narrowband,|"
        }
        set firstTerm 0
    }
    if $ID {
        if $firstTerm {
            set matchOption "-match=col,DeviceName=*ID*"
        } else {
            append matchOption ",DeviceName=*ID*,|"
        }
        set firstTerm 0
    }
    if $BM {
        if $firstTerm {
            set matchOption "-match=col,DeviceName=*BM*"
        } else {
            append matchOption ",DeviceName=*BM*,|"
        }
    }
    set configFile  /home/helios/oagData/sr/BPMStatus/config.sdds
    set fileList ""
    set tmpFile /tmp/[APSTmpString]
    APSAddToTmpFileList -ID transfersetpoint -fileList "$tmpFile.device"
    if [catch {exec sddsprocess $configFile $tmpFile.device "$matchOption" } result] {
        APSMpReturn error $result
    }
    foreach coord $planes Coord $Planes {
        # I think we should use the msAve instead of just ms
        if [catch {exec sddsprocess $tmpFile.device -pipe=out \
                     -filter=col,Nonexistent$Coord,0,0 \
                     | sddsprocess -pipe -edit=col,ControlName,DeviceName,ei/:msAve:$coord:AdjustedCC/ \
                     | sddsconvert -pipe=in -retain=col,ControlName $tmpFile.$coord} result] {
            APSMpReturn error $result
        }
        lappend fileList $tmpFile.$coord
    }
    APSAddToTmpFileList -ID transfersetpoint -fileList $fileList
    APSAddToTmpFileList -ID transfersetpoint -fileList $tmpFile.setpoint
    if [catch {eval exec sddscombine $fileList -merge -pipe=out \
                 | sddscasr -pipe -pend=50 -save \
                 | sddsprocess -pipe=in -match=col,CAError=n \
                 -reedit=col,ControlName,%/msAve/ms/%/AdjustedCC/SetpointAO/ \
                 $tmpFile.setpoint } result] {
        APSMpReturn error $result
    }
    
    #transfer bpm errors to setpoints
    if [catch {exec sddscasr $tmpFile.setpoint -restore -pend=50 } result] {
        APSMpReturn error "sddscaser (restore): $result"
    }
    APSMpReturn ok
}

proc APSMpTransferBpmRFErrorsToRFSetpointInfo {} {
    return "Transfers msAve bpm RF errors (i.e. S1A:P1:msAve:x:RfErrorM, not *mswAve* ) to rf setpoints (i.e. S1A:P1:msAve:x:RfSetpointP).\n\nBefore running this, the rf frequency controllaw must be turned off and then the rf frequency manually changed to the desired circumference value or the desired average of horizontal bpm errors over the ring.\n\nWhen the rf frequency controllaw is started up again, it will maintain the average value of bpms around the ring. Only the rf bpms (Nb and Mp types) in the horizontal plane are used in rf frequency controllaw, and these bpms are the ones with RF error and setpoint PVs.\n\nThere is no dialog box in this procedure."
}

proc APSMpTransferBpmRFErrorsToRFSetpointDialog {frame} {
    global  APSMpTransferBpmRFErrorsToRFSetpoint
    pack [frame $frame.f1]
    set  APSMpTransferBpmRFErrorsToRFSetpoint(rfFreqDelta) 100
    checkbutton $frame.f1.cb \
      -text "Update the RF ControlLaw Test Limits" \
      -variable APSMpTransferBpmRFErrorsToRFSetpoint(updateRFControlLawTestLimits) \
      -command "
          if {\$APSMpTransferBpmRFErrorsToRFSetpoint(updateRFControlLawTestLimits)} {
              $frame.f1.rf.entry configure -state normal
          } else {
              $frame.f1.rf.entry configure -state disable
          }
      "
    pack $frame.f1.cb -anchor nw
    APSLabeledEntry .rf -parent $frame.f1 -label "RF frequency delta test limit (Hz) :" -textVariable APSMpTransferBpmRFErrorsToRFSetpoint(rfFreqDelta) \
      -contextHelp "The rf frequency delta test limit (+-) for rf controllaw, the rf frequency test limit will be current RF frequency +/- this delta limit."
    if {!$APSMpTransferBpmRFErrorsToRFSetpoint(updateRFControlLawTestLimits)} {
        $frame.f1.rf.entry configure -state disable
    }
    return
}

proc APSMpTransferBpmRFErrorsToRFSetpoint {args} {
    set rfFreqDelta 100
    set updateRFControlLawTestLimits 0
    APSStrictParseArguments {rfFreqDelta updateRFControlLawTestLimits}

    APSMpStep init \
      -promptDialog APSMpTransferBpmRFErrorsToRFSetpointDialog \
      -vars "rfFreqDelta updateRFControlLawTestLimits" -proc "APSMpTransferBpmRFErrorsToRFSetpoint"

    set plane x
    foreach type {rfMp rfNb} {
        set $type 1
    }
    
    set xplane 1
    set yplane 0
    set planes $plane
    set Planes H

    set matchOption "-match=col,ElectronicsType=Monopulse,ElectronicsType=FPGA,|"
    append matchOption ",ElectronicsType=Narrowband,|"
    set configFile  /home/helios/oagData/sr/BPMStatus/config.sdds
    set tmpFile /tmp/[APSTmpString]
    APSAddToTmpFileList -ID transfersetpoint -fileList "$tmpFile.device"
    if [catch {exec sddsprocess $configFile $tmpFile.device "$matchOption" } result] {
        APSMpReturn error $result
    }
    
    APSAddToTmpFileList -ID transfersetpoint -fileList $tmpFile.x
    # There are no RfError PVs for P0's
    if [catch {exec sddsprocess $tmpFile.device -pipe=out \
                 -filter=col,NonexistentH,0,0 \
                 -match=col,DeviceName=*P0*,! \
                 | sddsprocess -pipe -edit=col,ControlName,DeviceName,ei/:msAve:x:AdjustedCC/ \
                 | sddsconvert -pipe=in -retain=col,ControlName $tmpFile.x} result] {
        APSMpReturn error $result
    }
#    APSAddToTmpFileList -ID transfersetpoint -fileList $tmpFile.setpoint
    if [catch {eval exec sddscombine $tmpFile.x -merge -pipe=out \
                 | sddscasr -pipe -pend=50 -save \
                 | sddsprocess -pipe=in -match=col,CAError=n \
                 -reedit=col,ControlName,%/msAve/ms/%/AdjustedCC/RfSetpointP/ \
                 $tmpFile.setpoint } result] {
        APSMpReturn error $result
    }
    
    #transfer bpm errors to setpoints
    if [catch {exec sddscasr $tmpFile.setpoint -restore -pend=5 } result] {
        APSMpReturn error "sddscaser (restore): $result"
    }

    if {$updateRFControlLawTestLimits} {
        if {[catch {APSMpUpdateRFControllawTestLimits -rfFreqDelta $rfFreqDelta} results]} {
            APSMpReturn error "APSMpTransferBpmRFErrorsToRFSetpoint: $results"
        }
    }

    APSMpReturn ok
}
