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

#
# $Log: not supported by cvs2svn $
# Revision 1.12  2011/05/25 15:24:50  shang
# modified to take all needed columns from old devices.sdds when creating new file.
#
# Revision 1.11  2011/05/23 19:50:55  sajaev
# Added catch around final sddsxref with message about
# XrayBPMdistances.sdds file.
#
# Revision 1.10  2005/11/30 17:11:44  emery
# Retain the important column IgnorePS1 shutter. This column
# is used by the "generate test file" procedure in orbit correction.
#
# Revision 1.9  2005/10/04 01:57:18  emery
# Use IgnoreShutterFlag from Xray distances file
#
# Revision 1.8  2005/07/05 21:45:20  shang
# modifed to keep the Default value of previous configuration
#
# Revision 1.7  2005/07/05 19:48:07  emery
# Added comments.
#
# Revision 1.6  2005/02/14 23:46:12  shang
# added Default column to devices.sdds
#
# Revision 1.5  2004/05/19 22:10:35  emery
# Added the importation of Xray bpm distances from file
# XrayBPMdistances.sdds into sectors.sdds
#
# Revision 1.4  2003/02/18 15:47:54  shang
# replace sddssnapshot statement by commands that use the iocRecNames.sdds file
#
# Revision 1.3  2003/01/24 14:36:56  emery
# Put catch statement around sddssnapshot
#
# Revision 1.2  2002/06/17 23:43:38  borland
# Now adds taper information to the files.
#
# Revision 1.1  2002/06/17 17:42:07  borland
# 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

# The file devices.sdds is created from scratch, that is,
# from checking the existence of a tentative list of possible 
# PVs for all sectors and without a template file.
# Note the CPU in ID4 is excluded because it does not have a gap setting.

# File sectors.sdds used to be edited manually. Now
# it is derived from devices.sdds

set data(ColumnNames) "ControlName DeviceName Sector GapName"
set data(ColumnInfo.ControlName) "type SDDS_STRING"
set data(ColumnInfo.DeviceName) "type SDDS_STRING"
set data(ColumnInfo.GapName) "type SDDS_STRING"
set data(ColumnInfo.Sector) "type SDDS_LONG"
set recordFile /home/helios/oagData/pvdata/iocRecNamesOAG.sdds

for {set sector 1} {$sector<41} {incr sector} {
   # if {$sector==35} {
   #     continue
   # }
    foreach pos {ds us} {
	set pos0 [string toupper $pos]
        lappend CNList [format S%02ldID:${pos0}ID:GapSetC $sector] [format S%02ldID:${pos0}ID:TaperGapSetC $sector]
        lappend gapList [format ID%02ld$pos $sector] [format ID%02ld$pos $sector]
        lappend SectorList $sector $sector
	lappend DNList [format S%02ldID:${pos0}ID $sector] [format S%02ldID:${pos0}ID $sector]
    }
}
set data(Column.ControlName) [list $CNList]
set data(Column.DeviceName) [list $DNList]
set data(Column.GapName) [list $gapList]
set data(Column.Sector) [list $SectorList]
set tmpRoot /tmp/[APSTmpString]

if [catch {sdds save $tmpRoot.1 data} result] {
    return -code error "$result"
}

set tmpfile /tmp/[APSTmpString]
if [catch {exec sddsprocess $recordFile $tmpfile \
             "-match=col,rec_name=*:GapSetC,rec_name=*:TaperGapSetC,|"} result] {
    return -code error $result
}

if [catch {exec sddsselect $tmpRoot.1 $tmpfile $tmpRoot.full -match=ControlName=rec_name } result] {
    return -code error $result
}

eval file delete -force $tmpfile
eval file delete -force $tmpRoot.1

set newFile [APSNextGenerationedName -newFile 1 -name sectors.sdds-0001]
set newDFile [APSNextGenerationedName -newFile 1 -name devices.sdds-0001]

exec sddsprocess $tmpRoot.full $tmpRoot.dev1 -match=col,ControlName=*Taper* \
    -define=column,Taper,1,type=long

exec sddssort $tmpRoot.full -pipe=out -col=DeviceName -unique \
    | sddsxref $tmpRoot.dev1 -pipe -match=DeviceName -take=Taper -fillIn -nowarning \
    | sddsconvert -pipe -retain=col,DeviceName,Sector,Taper,GapName \
    | sddsxref -pipe IDdevices.sdds -match=GapName \
    -nowarnings -fillIn -take=Default*,GapThreshold,Group* \
    | sddsprocess -pipe=in $newDFile "-redefine=col,GapThreshold,GapThreshold 0 == ? 31.0 : GapThreshold $ "

file delete -force devices.sdds $tmpRoot.dev1 
exec ln -s $newDFile devices.sdds

exec sddssort $tmpRoot.full -pipe=out -col=Sector -unique \
    | sddsprocess -pipe=in $tmpRoot.sectors \
    -define=col,Downstream,1,type=long
exec sddsprocess $tmpRoot.full $tmpRoot.us  -match=col,ControlName=*US* \
    -match=col,ControlName=*GapSetC \
    -print=col,UpstreamLabel,US \
    -define=col,Upstream,1,type=long 
exec sddsprocess $tmpRoot.full $tmpRoot.ds   -match=col,ControlName=*DS* \
    -match=col,ControlName=*GapSetC \
    -print=col,DownstreamLabel,DS
exec sddsprocess $tmpRoot.full $tmpRoot.taper -match=col,ControlName=*US*,! -nowarning \
    -match=col,ControlName=*Taper* \
    -define=col,DSTaper,1,type=long
exec sddsprocess $tmpRoot.full $tmpRoot.us.taper -match=col,ControlName=*US*Taper*  -nowarning \
    -define=col,USTaper,1,type=long

if [catch {exec sddsxref $tmpRoot.sectors $tmpRoot.ds -fillIn -pipe=out -take=Down* -nowarning -equate=Sector \
	       | sddsxref $tmpRoot.us -pipe -fillIn -take=Up* -nowarning -equate=Sector  \
	       | sddsxref $tmpRoot.taper -pipe -fillIn -take=DSTaper -nowarning -equate=Sector \
	       | sddsxref $tmpRoot.us.taper -pipe -fillIn -take=USTaper -nowarning -equate=Sector \
	       | sddsxref -pipe XrayBPMdistances.sdds -equate=Sector \
	       -take=*Distance,IgnorePS1Shutter \
	       | sddsconvert -pipe=in $newFile \
	       -retain=col,Sector,Downstream,DownstreamLabel,Upstream,UpstreamLabel,*Taper,*Distance,IgnorePS1Shutter} result] {
    puts stderr "Error compiling final file, check that /home/helios/oagData/sr/IDs/XrayBPMdistances.sdds file has all rows: $result"
    exit 1
}



file delete -force sectors.sdds
exec ln -s $newFile sectors.sdds

#generate SetGapToBPLDTripLimits (BPLD trip limits file)
set newFile [APSNextGenerationedName -newFile 1 -name SetGapToBPLDTripLimits-0001]
if [catch {exec sddsprocess $tmpRoot.full -match=col,ControlName=*:GapSetC -pipe=out \
	       | sddsprocess -pipe -edit=col,ReadbackName,ControlName,%/GapSetC/GapM/ \
	       -print=col,ValueString,56 \
	       | sddsprocess -pipe "-define=col,LMPSSector,Sector 2 mod 0 > ? Sector :  Sector 1 + $,type=long" \
	       "-define=col,ID,Sector 2 mod 0 > ? 2 : 1 $,type=long" \
	       | sddsprocess -pipe=in $newFile -print=col,LMPSGap,ID%ld,ID } result] {
    puts stderr "Error generating  SetGapToBPLDTripLimits: $result"
    exit 1
}

file delete -force  SetGapToBPLDTripLimits
exec ln -s $newFile  SetGapToBPLDTripLimits

eval file delete -force sectorsFound.sdds $tmpRoot.ds $tmpRoot.us $tmpRoot.full  \
    $tmpRoot.taper $tmpRoot.us.taper $tmpRoot.sectors
