# In the case of correctors a change is proposed. There are 4 possible 
#values for the corrector inUse PV (e.g. S20A:H2:InUseMBBO)
#"Not used" RTFB DC "RTFB & DC" or just 0, 1, 2, or 3.

#So far we have ignored whether a corrector was in RTFB system or not.
#Now we want to include this. We want the InUseMBBO to be updated
#when we press START in SROrbitControllaw and when we
#press Initialize in SRFBControl. Obviously this should then
#be a library procedure, say APSSetCorrectorInUSe
#examples:
#  APSSetCorrectorInUse -resetUnspeficied 1 -plane h
#  This sets all DCon and RTFBon to zero.
#  APSSetCorrectorInUse -resetUnspeficied 1 -configDC h.default -plane h
#This would set DCon value for correctors in h.default according to value of 
#column Flag. The correctors not mentioned in file get DCon=0. Also all
#correctors should have RTFBon set to zero.
# APSSetCorrectorInUse -resetUnspeficied 1 -plane h \
  #    -configDC h.default -configRTFB h.default
#The sets the DCon according to h.default in orbitControllaw base directory,
#set the RTFBon according to h.default in rtfbfeedback base directory.

#the plane argument is not needed when config file is given

#this procedure is obselete, it is replaced by APSSetCorrectorInUse

proc APSSetCorrectorInUse {args} {
    set plane ""
    set configDC ""
    set configRTFB ""
    set resetUnspecified 0
    set DCBaseDir /home/helios/oagData/sr/orbitControllaw/lattices/default
    set RTFBBaseDir /home/helios/oagData/sr/rtfeedback/lattices/default
    APSStrictParseArguments {plane configDC configRTFB resetUnspecified}
    
    if {![string length $configDC] && ![string length $configRTFB] && \
          $resetUnspecified} {
        #set all correctors InUse to zero
        switch $plane {
            H -
            h -
            x -
            X {
                set Plane H
            }
            V -
            v -
            y -
            Y {
                set Plane V
            }
            default {
                return -code error "Invalid plane - $plane given!"
            }
        }
        if [catch {exec cavput -list=S -range=begin=1,end=40,interval=1 \
                     -list=A:,B: \
                     -list=$Plane -range=begin=1,end=4,interval=1 \
                     -list=:InUseMBBO=0 \
                     -pend=30} result] {
            return -code error "APSSetCorrectorInUse: $result"
        }
        return
    }
    set type ""
    set tmpfile /tmp/[APSTmpString]
    if [string length $configDC] {
        if ![regexp {[hv]\.} $configDC plane1] {
            return -code error "APSSetCorrectorInUse: the DC config file is invalid"
        }
        set type DC
        set plane [string range $plane1 0 0]
    }
    if [string length $configRTFB] {
        if ![regexp {[hv]\.} $configRTFB plane2] {
            return -code error "APSSetCorrectorInUse: the config file is invalid"
        }
        set plane2 [string range $plane2 0 0]
        if { [string length $plane] &&
             [string compare $plane $plane2]!=0} {
            return -code error "APSSetCorrectorInUse: The plane of DC and RTFB config is not consistent"
        }
        set plane $plane2
        if [string length $type] {
            set type BOTH
        } else {
            set type RTFB
        }
    }
    set Plane [string toupper $plane]
    APSAddToTmpFileList -ID APSSetCorrectorInUse -fileList "$tmpfile.all $tmpfile.set1 $tmpfile.set $tmpfile.dccor $tmpfile.rtfbcor"
    #get all the correctors in $plane
    if [catch {exec sddsprocess /home/helios/oagData/sr/${Plane}CorrectorStatus/config.sdds \
                 -filter=col,Nonexistent,0,0 -pipe=out \
                 -match=col,DeviceName=*:${Plane}* \
                 -edit=col,ControlName,DeviceName,ei/:InUseMBBO/ \
                 | sddsconvert -pipe \
                 -retain=col,DeviceName,ControlName -dele=para,* \
                 | sddscasr -pipe -save -numerical \
                 | sddsprocess -pipe=in $tmpfile.all -match=col,CAError=n \
                 -scan=col,Value,ValueString,%ld,type=long \
                 "-define=col,RTFB,Value 2 mod,type=short" \
                 "-define=col,DC,Value 2 /,type=short" } result] {
        return -code error $result
    }
    
    if {$type=="DC" || $type=="BOTH"} {
        if [catch {exec sddsprocess $DCBaseDir/$configDC/config -match=par,NameType=CorrectorNames \
                     $tmpfile.dccor
            exec sddsxref $tmpfile.all $tmpfile.dccor -match=DeviceName=Name \
                     -rename=col,Flag=NewDC -fill -nowarnings $tmpfile.dc} result] {
            return -code error $result
        }
    }
    if {$type=="RTFB" || $type=="BOTH"} {
        if [catch {exec sddsprocess $RTFBBaseDir/$configRTFB/config -match=par,NameType=CorrectorNames \
                     $tmpfile.rtfbcor
            exec sddsxref $tmpfile.all $tmpfile.rtfbcor -match=DeviceName=Name \
                     -rename=col,Flag=NewRTFB -fill -nowarnings  $tmpfile.rtfb} result] {
            return -code error $result
        }
    }
    switch $type {
        DC {
            if [catch {exec sddsxref $tmpfile.all $tmpfile.dc -match=DeviceName -take=NewDC -pipe=out \
                         | sddsprocess -pipe=in $tmpfile.set1 \
                         "-redefine=col,Value,RTFB 2 NewDC * +,type=long" } result] {
                return -code error $result
            }
        }
        RTFB {
            if [catch {exec sddsxref $tmpfile.all $tmpfile.rtfb -match=DeviceName -take=NewRTFB -pipe=out \
                         | sddsprocess -pipe=in $tmpfile.set1 \
                         "-redefine=col,Value,NewRTFB 2 DC * +,type=long" } result] {
                return -code error $result
            }
        }
        BOTH {
            if [catch {exec sddsxref $tmpfile.dc $tmpfile.rtfb -match=DeviceName -take=NewRTFB \
                         -pipe=out \
                         | sddsprocess -pipe=in $tmpfile.set1 \
                         "-redefine=col,Value,NewRTFB 2 NewDC * +,type=long" } result] {
                return -code error $result
            }
        }
    }
    if [catch {exec sddsxref $tmpfile.all $tmpfile.set1 -match=DeviceName -rename=col,Value=NewValue \
                 -pipe=out -nowarnings \
                 | sddsprocess -pipe "-define=col,Diff,Value NewValue -,type=long" -nowarnings \
                 | sddsprocess -pipe=in $tmpfile.set -filter=col,Diff,0,0,! -nowarnings \
                 "-reprint=col,ValueString,%ld,NewValue" } result] {
        return -code error $result
    }
    set rows [exec sdds2stream $tmpfile.set -rows=bare]
    if $rows {
        if [catch {exec sddscasr $tmpfile.set -restore} result] {
            return -code error $result
        }
    }
}
