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

# $Log: not supported by cvs2svn $
# Revision 1.4  2010/01/08 22:22:48  shang
# modified to removed the lab FPGA bpms from the configuration file.
#
# Revision 1.3  2009/05/26 15:19:57  shang
# added displaying of number of deleted/added PVs.
#
# Revision 1.2  2009/03/18 19:55:03  shang
# modified to be able to generate the request file for the first time.
#
# Revision 1.1  2009/03/18 19:41:21  shang
# first version
#
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

cd /home/helios/oagData/booster/responseMeasFiles

set tmpRoot /tmp/[APSTmpString]
if [catch {exec sddsprocess /home/helios/oagData/SCR/requestFiles/BoosterBPMWaveform.config \
             $tmpRoot.config -match=par,Location=Accel -match=par,BPMType=Booster } result] {
    puts stderr $result
    exit 1
}
set iocList [exec sdds2stream -par=IOCName $tmpRoot.config]

for {set region 0} {$region<10} {incr region} {
    set nameList ""
    foreach ioc $iocList {
        if [catch {exec sddsprocess $tmpRoot.config -match=par,IOCName=$ioc -pipe=out \
            | sdds2stream -pipe -col=BPM } bpmList ] {
            puts stderr "Error1: $bpmList"
            exit 1
        }
        foreach bpm $bpmList {
            lappend nameList ${bpm}:region${region}Avg
        }
    }
    if [catch {exec sddsmakedataset $tmpRoot.$region -col=SymbolicName,type=string -data=[join $nameList ,] } result] {
        puts stderr "Error2: $bpmList"
        exit 1
    }
    if [catch {exec sddsprocess $tmpRoot.$region -edit=col,ControlName,SymbolicName,ei%:XPositionAI seg${region}MeasH.sdds
        exec sddsprocess $tmpRoot.$region -edit=col,ControlName,SymbolicName,ei%:YPositionAI seg${region}MeasV.sdds } result] {
        puts stderr "Error3: $bpmList"
        exit 1
    }
}
set files [glob seg*MeasH.sdds]
set output [APSNextGenerationedName -name measH.sdds-0000 -newFile -separator "-"]
if [catch {eval exec sddscombine $files -merge $output } result] {
    puts stderr "Error4: $result"
    exit 1
}
exec rm measH.sddsmeas
exec ln -s $output measH.sdds

set files [glob seg*MeasV.sdds]
set output [APSNextGenerationedName -name measV.sdds-0000 -newFile -separator "-"]
if [catch {eval exec sddscombine $files -merge $output } result] {
    puts stderr "Error4: $result"
    exit 1
}
exec rm measV.sddsmeas
exec ln -s $output measV.sdds

exit
