#!/bin/sh  
# \
exec oagtclsh "$0" "$@"

# $Log: not supported by cvs2svn $
# Revision 1.7  2010/07/07 14:51:32  shang
# now includes the "Feedback MSI" pvs for FPGA bpms.
#
# Revision 1.6  2010/07/06 18:25:24  shang
# modified to remove the X/Y AVE pvs of FPGA bpms sine they do not exist.
#
# Revision 1.5  2010/01/08 20:49:09  soliday
# Removed S35A,B AVE and S36A,B AVE pvs since they no longer exist.
#
# Revision 1.4  2009/08/04 20:44:35  shang
# removed S38A,B AVE pvs since they no longer exist.
#
# Revision 1.3  2009/01/23 16:14:46  soliday
# Added filtering of PV names using the iocRecNames.sdds file.
#
# Revision 1.2  2004/05/26 17:56:44  emery
# Have separate loops to build list of general rf bpms and
# list of RTFB-only bpms.
#
# Revision 1.1  2004/05/26 17:38:02  emery
# First installation.
#
# Produces a monitor file for application SRBpmStats

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSStandardSetup

# Based on makeSRBPMAveInputFile
set rootname bpmStats
set usage {usage: makeSRbpmStatsMonitorFile [-output <filename> | -install 1]}
set output ""
set install 0
set args $argv
if {[APSStrictParseArguments {output install}] || \
      (![string length $output] && !$install) || \
      ([string length $output] && $install)} {
    puts stderr "Error1 for $argv0 in [pwd]: $usage"
    exit 1
}
if !$install {
    if [file exists $output] {
        puts stderr "Error2 for $argv0 in [pwd]: $output exists"
        exit 1
    }
} else {
    cd /home/helios/oagData/sr/BPMStats/inputFiles
    set lastFile [lindex [lsort [glob ${rootname}.mon-????]] end]
    set output [APSNextGenerationedName -name $lastFile -newFile 1]
}


set fid [sdds open $output.tmp w]
sdds defineColumn $fid ControlName -type SDDS_STRING
sdds writeLayout $fid
sdds startPage $fid [expr 40*15*9]

set ControlName ""
set CNxSuffixList {:ms.XAVE :ms:x  :sum.RVAL}
set CNySuffixList {:ms.YAVE :ms:y }
set CNxRTFBPrefixList {SRFB:     SRFB:}
set CNxRTFBSuffixList {:ms:x:Raw :ms:x.VAL}
set CNyRTFBPrefixList {SRFB:     SRFB:}
set CNyRTFBSuffixList {:ms:y:Raw :ms:y.VAL}

# x plane
set dataDir /home/helios/oagData/sr/BPMStatus
if [catch {exec sddsprocess $dataDir/config.sdds -pipe=out \
             -filter=col,NonexistentH,0,0,OkForLoggingH,1,1,& \
             -match=col,DeviceName=*ID*,!,DeviceName=*BM*,!,& \
             | sdds2stream -col=DeviceName -pipe \
         } xBPMs] {
    puts stderr "Error3 for $argv0 in [pwd]: $xBPMs"
    exit 1
}
if [catch {exec sddsprocess $dataDir/config.sdds -pipe=out \
             -filter=col,NonexistentH,0,0,OkForLoggingH,1,1,& \
             "-match=col,ElectronicsType=Monopulse,ElectronicsType=Narrowband,|,ElectronicsType=FPGA,|" \
             -filter=col,OkForRTFeedbackH,1,1 \
             | sdds2stream -col=DeviceName -pipe \
         } xBPMsRTFB] {
    puts stderr "Error4 for $argv0 in [pwd]: $xBPMsRTFB"
    exit 1
}

if [catch {exec sddsprocess $dataDir/config.sdds -pipe=out \
             -filter=col,NonexistentV,0,0,OkForLoggingV,1,1,& \
             -match=col,DeviceName=*ID*,!,DeviceName=*BM*,!,& \
             | sdds2stream -col=DeviceName -pipe \
         } yBPMs] {
    puts stderr "Error5 for $argv0 in [pwd]: $yBPMs"
    exit 1
}
if [catch {exec sddsprocess $dataDir/config.sdds -pipe=out \
             -filter=col,NonexistentV,0,0,OkForLoggingV,1,1,& \
             "-match=col,ElectronicsType=Monopulse,ElectronicsType=Narrowband,|,ElectronicsType=FPGA,|" \
             -filter=col,OkForRTFeedbackV,1,1 \
             | sdds2stream -col=DeviceName -pipe \
         } yBPMsRTFB] {
    puts stderr "Error6 for $argv0 in [pwd]: $yBPMsRTFB"
    exit 1
}

foreach bpm $xBPMs {
    set items [llength $CNxSuffixList]
    for {set index 0} {$index<$items} {incr index} {
        lappend ControlName ${bpm}[lindex $CNxSuffixList $index]
    }
}
foreach bpm $xBPMsRTFB {
    set items [llength $CNxRTFBSuffixList]
    for {set index 0} {$index<$items} {incr index} {
        lappend ControlName [lindex $CNxRTFBPrefixList $index]${bpm}[lindex $CNxRTFBSuffixList $index]
    }
}
foreach bpm $yBPMs {
    set items [llength $CNySuffixList]
    for {set index 0} {$index<$items} {incr index} {
        lappend ControlName ${bpm}[lindex $CNySuffixList $index]
    }
}
foreach bpm $yBPMsRTFB {
    set items [llength $CNyRTFBSuffixList]
    for {set index 0} {$index<$items} {incr index} {
        lappend ControlName [lindex $CNyRTFBPrefixList $index]${bpm}[lindex $CNyRTFBSuffixList $index]
    }
}

eval sdds setColumn $fid ControlName $ControlName
sdds writePage $fid
sdds close $fid

# remove duplicate control names when combining x and y names
exec sddssort $output.tmp -pipe=out  \
  -col=ControlName -unique -num \
  | sddsprocess -pipe=in $output.tmp2 \
  -edit=column,SimpleControlName,ControlName,S?/./K

file delete $output.tmp
#remove FPGA bpm AVE pvs
set option ""
for {set sector 1} {$sector<41} {incr sector} {
    append option " -match=col,ControlName=S${sector}*:P*:*AVE,!"
}

if [catch {eval exec sddsselect $output.tmp2 /home/helios/oagData/pvdata/iocRecNamesOAG.sdds \
             -pipe=out -match=SimpleControlName=rec_name -nowarning -reuse \
             | sddsprocess -pipe $option \
             | sddsconvert -pipe=in $output -delete=column,SimpleControlName } result] {
    puts stderr "Error7 for $argv0 in [pwd]: $result"
}

file delete $output.tmp2



if $install {
    if [catch {file delete $rootname.mon
        exec ln -s $output $rootname.mon
        exec sddsselect $lastFile $output removedPVs -invert -match=ControlName
        exec sddsselect $output $lastFile addedPVs -invert -match=ControlName \
             } result] {
        puts stderr "Error8 for $argv0 in [pwd]: $result"
        exit 1
    }
}
exit
