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

# $Log: not supported by cvs2svn $
# Revision 1.3  2004/05/19 20:28:38  emery
# Corrected most of the error messages that had the variable usage instead of result.
#
# Revision 1.2  2004/05/13 16:58:02  emery
# Put catch statements around all sdds commands.
#
# Revision 1.1  2004/05/12 23:12:08  emery
# First installation
#

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

# Creates a new version of bpm configuation file for 
# directory /home/helios/oagData/sr/BPMStatus and
# for use in 
# ManageSRDeviceStatus -system bpms
#

# lattice file for the P0, P3 and P4 positions
set latticeFile /home/helios/oagData/sr/lattices/default/aps.twi

# file for Xray BPM distances. Assumes that makeIDSectorsFile has been run
# to use the data from /home/helios/oagData/sr/IDs/XrayBPMdistances.sdds
set IDbpmFile /home/helios/oagData/sr/IDs/sectors.sdds
set BMbpmFile /home/helios/oagData/sr/BMs/sectors.sdds

# create a ResponseEquation[HV] for ID Xray bpm for each ID sectors
# and for BM Xray bpms for BM sectors.

# assume that each row in the IDbpmFile and BMbpmFile files
# represent a pair of "working" Xray bpms, at least bpms
# for which an equation is desired.

set tmpfile /tmp/[APSTmpString]
# get the rf bpm data from latticeFile
if [catch {exec sddsprocess $latticeFile -pipe=out \
             -match=col,ElementType=MONI \
             | sddsconvert -pipe=in   $tmpfile.moni \
             -retain=col,ElementName,s -dele=para,* \
         } result] {
    puts stderr "Error1 for $argv0 in [pwd]: $result"
    exit 1
}

# get the AP0, BP0 and CP0 data
if [catch {exec sddsprocess $tmpfile.moni $tmpfile.AP0 \
             -match=col,ElementName=S*A:P0 \
             -scan=col,Sectorp1,ElementName,S%ld,type=long \
             "-def=col,Sector,Sectorp1 1 -,type=long" \
             "-def=col,SourcePosition,27.6 Sector *" \
             "-def=col,AP0Distance,s SourcePosition -" \
         } result] {
    puts stderr "Error2 for $argv0 in [pwd]: $result"
    exit 1
}

if [catch {exec sddsprocess $tmpfile.moni $tmpfile.BP0 \
             -match=col,ElementName=S*B:P0 \
             -scan=col,Sector,ElementName,S%ld,type=long \
             "-def=col,SourcePosition,27.6 Sector *" \
             "-def=col,BP0Distance,s SourcePosition -" \
         } result] {
    puts stderr "Error3 for $argv0 in [pwd]: $usage"
    exit 1
}

#2012,5,11 decide to take CP0 out from CU
if {0} {
# File $tmpfile.CP0 contain all C:P0 bpms even those
# not in CU sectors. Later filtering may be necessary.
if [catch {exec sddsprocess $tmpfile.moni $tmpfile.CP0 \
             -match=col,ElementName=S*C:P0 \
             -scan=col,Sector,ElementName,S%ld,type=long \
             "-def=col,SourcePosition,27.6 Sector *" \
             "-def=col,CP0Distance,s SourcePosition -" \
         } result] {
    puts stderr "Error4 for $argv0 in [pwd]: $usage"
    exit 1
}
}

# Get the BP4 and BP3 data
# The BM source position is assumed to be 17.19412 m
# from the middle of the previous straight section for
# all sectors.
if [catch {exec sddsprocess $tmpfile.moni $tmpfile.BP4 \
             -match=col,ElementName=S*B:P4 \
             -scan=col,Sector,ElementName,S%ld,type=long \
             "-def=col,SourcePosition,27.6 Sector 1 - * 17.19412 + " \
             "-def=col,BP4Distance,s SourcePosition -" \
         } result] {
    puts stderr "Error5 for $argv0 in [pwd]: $usage"
    exit 1
}


if [catch {exec sddsprocess $tmpfile.moni $tmpfile.BP3 \
             -match=col,ElementName=S*B:P3 \
             -scan=col,Sector,ElementName,S%ld,type=long \
             "-def=col,SourcePosition,27.6 Sector 1 - * 17.19412 + " \
             "-def=col,BP3Distance,s SourcePosition -" \
         } result] {
    puts stderr "Error6 for $argv0 in [pwd]: $usage"
    exit 1
}

if {0} {
# For canted undulator configurations. These configuration use C:P0's
set  CUdataDir /home/helios/oagData/sr/XAXSTF
if [catch {exec sddsprocess $CUdataDir/sectors.sdds $tmpfile.CUsectors  -noWarning \
             -filter=col,CUflag,1,1 } result ] {
    puts stder "Error7 for $argv0 in [pwd]: $result"
    exit
}
}

# Combine the data for ID Xray bpms
# We have to do something different for canted undulators
if [catch {exec sddsxref $IDbpmFile $tmpfile.AP0 -pipe=out \
             -equate=Sector -take=Sectorp1,AP0Distance -noWarning  \
             | sddsxref -pipe $tmpfile.BP0  -noWarning \
             -equate=Sector -take=BP0Distance \
             | sddsconvert -pipe \
             -retain=col,Sector,Sectorp1,*Distance \
             | sddsprocess -pipe=in $tmpfile.ID.distances \
             "-def=col,Denominator,AP0Distance BP0Distance -" \
             "-def=col,P1AP0Coeff,P1Distance BP0Distance - Denominator /" \
             "-def=col,P1BP0Coeff,P1Distance chs AP0Distance + Denominator /" \
             "-def=col,P2AP0Coeff,P2Distance BP0Distance - Denominator /" \
             "-def=col,P2BP0Coeff,P2Distance chs AP0Distance + Denominator /" \
         } result] {
    puts stderr "Error8 for $argv0 in [pwd]: $result"
    exit 1
}

#2011,5,11 treat CU as other IDs
if {0} {
# CU undulators: P1 is for upstream, P2 is for downstream
if [catch {exec sddsxref $IDbpmFile $tmpfile.AP0 -pipe=out \
             -equate=Sector -take=Sectorp1,AP0Distance -noWarning \
             | sddsxref -pipe $tmpfile.BP0  -noWarning \
             -equate=Sector -take=BP0Distance \
             | sddsxref -pipe $tmpfile.CP0  -noWarning \
             -equate=Sector -take=CP0Distance \
             | sddsselect -pipe $tmpfile.CUsectors \
             -equate=Sector \
             | sddsconvert -pipe \
             -retain=col,Sector,Sectorp1,*Distance \
             | sddsprocess -pipe=in $tmpfile.ID.CU.distances \
             "-def=col,DenominatorUS,CP0Distance BP0Distance -" \
             "-def=col,DenominatorDS,AP0Distance CP0Distance -" \
             "-def=col,P1CP0Coeff,P1Distance BP0Distance - DenominatorUS /" \
             "-def=col,P1BP0Coeff,P1Distance chs CP0Distance + DenominatorUS /" \
             "-def=col,P2AP0Coeff,P2Distance CP0Distance - DenominatorDS /" \
             "-def=col,P2CP0Coeff,P2Distance chs AP0Distance + DenominatorDS /" \
         } result] {
    puts stderr "Error9 for $argv0 in [pwd]: $result"
    exit 1
}
}

# Combine the data for BM Xray bpms
if [catch {exec sddsxref $BMbpmFile $tmpfile.BP3 -pipe=out \
             -equate=Sector -take=BP3Distance \
             | sddsxref -pipe $tmpfile.BP4  \
             -equate=Sector -take=BP4Distance \
             | sddsconvert -pipe \
             -retain=col,Sector,*Distance \
             | sddsprocess -pipe=in $tmpfile.BM.distances \
             "-def=col,Denominator,BP3Distance BP4Distance -" \
             "-def=col,P1BP3Coeff,P1Distance BP4Distance - Denominator /" \
             "-def=col,P1BP4Coeff,P1Distance chs BP3Distance + Denominator /" \
             "-def=col,P2BP3Coeff,P2Distance BP4Distance - Denominator /" \
             "-def=col,P2BP4Coeff,P2Distance chs BP3Distance + Denominator /" \
         } result] {
    puts stderr "Error10 for $argv0 in [pwd]: $result"
    exit 1
}


# Prepare equation string for ID bpms
if [catch {exec sddsprocess $tmpfile.ID.distances -pipe=out \
             "-print=col,ResponseEquationH,%.3lf S%ldA:P0 * %.3lf S%ldB:P0 * +,P1AP0Coeff,Sectorp1,P1BP0Coeff,Sector" \
             "-print=col,ResponseEquationV,%.3lf S%ldA:P0 * %.3lf S%ldB:P0 * +,P1AP0Coeff,Sectorp1,P1BP0Coeff,Sector"  \
             -print=col,DeviceName,S%ldID:P1,Sector \
             | sddsconvert -pipe=in $tmpfile.IDP1.equation \
             -retain=col,DeviceName,ResponseEquation? \
         } result] {
    puts stderr "Error11 for $argv0 in [pwd]: $result"
    exit 1
}

if {0} {
if [catch {exec sddsprocess $tmpfile.ID.CU.distances -pipe=out \
             "-print=col,ResponseEquationH,%.3lf S%ldC:P0 * %.3lf S%ldB:P0 * +,P1CP0Coeff,Sector,P1BP0Coeff,Sector" \
             "-print=col,ResponseEquationV,%.3lf S%ldC:P0 * %.3lf S%ldB:P0 * +,P1CP0Coeff,Sector,P1BP0Coeff,Sector"  \
             -print=col,DeviceName,S%ldID:P1,Sector \
             | sddsconvert -pipe=in $tmpfile.IDP1.CU.equation \
             -retain=col,DeviceName,ResponseEquation? \
         } result] {
    puts stderr "Error12 for $argv0 in [pwd]: $result"
    exit 1
}
}


if [catch {exec sddsprocess $tmpfile.ID.distances -pipe=out \
             "-print=col,ResponseEquationH,%.3lf S%ldA:P0 * %.3lf S%ldB:P0 * +,P2AP0Coeff,Sectorp1,P2BP0Coeff,Sector" \
             "-print=col,ResponseEquationV,%.3lf S%ldA:P0 * %.3lf S%ldB:P0 * +,P2AP0Coeff,Sectorp1,P2BP0Coeff,Sector"  \
             -print=col,DeviceName,S%ldID:P2,Sector \
             | sddsconvert -pipe=in $tmpfile.IDP2.equation \
             -retain=col,DeviceName,ResponseEquation? \
         } result] {
    puts stderr "Error13 for $argv0 in [pwd]: $result"
    exit 1
}


if {0} {
#do not use CP0, but use AP0 and BP0 as in above
if [catch {exec sddsprocess $tmpfile.ID.CU.distances -pipe=out \
             "-print=col,ResponseEquationH,%.3lf S%ldA:P0 * %.3lf S%ldC:P0 * +,P2AP0Coeff,Sectorp1,P2CP0Coeff,Sector" \
             "-print=col,ResponseEquationV,%.3lf S%ldA:P0 * %.3lf S%ldC:P0 * +,P2AP0Coeff,Sectorp1,P2CP0Coeff,Sector"  \
             -print=col,DeviceName,S%ldID:P2,Sector \
             | sddsconvert -pipe=in $tmpfile.IDP2.CU.equation \
             -retain=col,DeviceName,ResponseEquation? \
         } result] {
    puts stderr "Error14 for $argv0 in [pwd]: $result"
    exit 1
}
}


# do the BM equation strings
# Prepare equation string for ID bpms
if [catch {exec sddsprocess $tmpfile.BM.distances -pipe=out \
             "-print=col,ResponseEquationH,%.3lf S%ldB:P4 * %.3lf S%ldB:P3 * +,P1BP4Coeff,Sector,P1BP3Coeff,Sector" \
             "-print=col,ResponseEquationV,%.3lf S%ldB:P4 * %.3lf S%ldB:P3 * +,P1BP4Coeff,Sector,P1BP3Coeff,Sector"  \
             -print=col,DeviceName,S%ldBM:P1,Sector \
             | sddsconvert -pipe=in $tmpfile.BMP1.equation \
             -retain=col,DeviceName,ResponseEquation? \
         } result] {
    puts stderr "Error15 for $argv0 in [pwd]: $result"
    exit 1
}



if [catch {exec sddsprocess $tmpfile.BM.distances -pipe=out \
             "-print=col,ResponseEquationH,%.3lf S%ldB:P4 * %.3lf S%ldB:P3 * +,P2BP4Coeff,Sector,P2BP3Coeff,Sector" \
             "-print=col,ResponseEquationV,%.3lf S%ldB:P4 * %.3lf S%ldB:P3 * +,P2BP4Coeff,Sector,P2BP3Coeff,Sector" \
             -print=col,DeviceName,S%ldBM:P2,Sector \
             | sddsconvert -pipe=in $tmpfile.BMP2.equation \
             -retain=col,DeviceName,ResponseEquation? \
         } result] {
    puts stderr "Error16 for $argv0 in [pwd]: $result"
    exit 1
}


if [catch {exec sddscombine $tmpfile.BMP1.equation $tmpfile.BMP2.equation $tmpfile.IDP1.equation $tmpfile.IDP2.equation $tmpfile.all.equation -merge  \
         } result] {
    puts stderr "Error17 for $argv0 in [pwd]: $result"
    exit 1
}

set dataDir /home/helios/oagData/sr/BPMStatus
set output config-[clock format [clock seconds] -format %Y-%j-%m%d-%H%M%S]
set lastFile config.sdds

# incorporate data
if [catch {exec sddsselect $dataDir/$lastFile $tmpfile.all.equation \
             $tmpfile.noEquations -invert -match=DeviceName \
         } result] {
    puts stderr "Error18 for $argv0 in [pwd]: $result"
    exit 1
}

if [catch {exec sddsselect $dataDir/$lastFile $tmpfile.all.equation -pipe=out \
             -match=DeviceName \
             | sddsconvert -pipe \
             -dele=col,ResponseEquation? \
             | sddsxref -pipe=in $tmpfile.all.equation $tmpfile.newEquations \
             -match=DeviceName -take=ResponseEquation? \
         } result] {
    puts stderr "Error19 for $argv0 in [pwd]: $result"
    exit 1
}


if [catch {exec sddscombine $tmpfile.noEquations $tmpfile.newEquations -pipe=out \
             -merge \
             | sddsconvert -pipe=in $dataDir/$output \
             -dele=para,NumberCombined \
         } result] {
    puts stderr "Error20 for $argv0 in [pwd]: $result"
    exit 1
}


# At this point one has to use the READ... button in
# "ManageSRDeviceStatus -system bpms" to load the data.

exit
