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


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



for {set i 1} {$i <= 40} {incr i} {
    set ii [format %02d $i]
    set j [expr $i + 1]
    if {$j == 41} {
        set j 1
    }
    set jj [format %02d $j]
    exec sddsprocess ../DataLogging/srBPMs.mon ../DataLogging/srBPMFast_${i}.mon "-match=column,ControlName=S${ii}B*:AdjustedM,ControlName=S${jj}A*:AdjustedM,|,ControlName=S${jj}BMFE-XBPM:P1:y,|,ControlName=S${jj}BMFE-XBPM:P2:y,|,ControlName=S${jj}IDFE-XBPM:P???:x,|,ControlName=S${jj}IDFE-XBPM:P???:y,|,ControlName=S${jj}IDFE-XBPM:P???:?PositionM,|" -reprint=parameter,InstallLocation,/home/helios/oagData/logging/srBPMFast/${i}/srBPMFast_${i}.mon -delete=column,ControlType,ioc_name,Beamline,ControlMode -delete=parameter,NumberCombined
}

exit


# input files:
# /home/helios/oagData/sr/localSteering/lightSources.config
# /home/helios/oagData/logging/srBPMs/srBPMs.mon
# SRBPMFastExtra.sdds
# 
# output files:
# srBPMFast_1.mon - srBPMFast_40.mon

if ![file exists /home/helios/oagData/sr/localSteering/lightSources.config] {
    return -code error "$argv0: not found: /home/helios/oagData/sr/localSteering/lightSources.config"
}

puts "Ensure makeDataReviewSRBPMFiles has been run to create an up-to-date srBPMs.mon file"
if ![file exists /home/helios/oagData/logging/srBPMs/srBPMs.mon] {
    return -code error "$argv0: not found: /home/helios/oagData/logging/srBPMs/srBPMs.mon"
}

set tmpRoot /tmp/[APSTmpString]
if [catch {exec sddsprocess /home/helios/oagData/sr/localSteering/lightSources.config $tmpRoot.0 \
             "-print=column,ControlName,%s:ms:x:AdjustedCC,Name" \
             -print=column,ReadbackName,%s,ControlName \
             -print=column,ReadbackUnits,mm
    exec sddsprocess /home/helios/oagData/sr/localSteering/lightSources.config $tmpRoot.1 \
             "-print=column,ControlName,%s:ms:y:AdjustedCC,Name" \
             -print=column,ReadbackName,%s,ControlName \
             -print=column,ReadbackUnits,mm
    exec sddsprocess /home/helios/oagData/logging/srBPMs/srBPMs.mon $tmpRoot.2 \
             -match=column,ControlName=*ID*ErrorCC,ControlName=*BM*ErrorCC,| \
             -reedit=column,ControlName,%/Error/Adjusted/ \
             -reedit=column,ReadbackName,%/Error/Adjusted/
    exec sddscombine $tmpRoot.0 $tmpRoot.1 $tmpRoot.2 ../DataLogging/SRBPMFastExtra.sdds -merge \
             -retain=column,ControlName,ReadbackName,ReadbackUnits -pipe=out \
             | sddssort -pipe -column=ControlName -numericHigh \
             | sddsprocess -pipe=in /tmp/srBPMFast.mon  \
             -edit=column,SimpleControlName,ControlName,S?/./K } result] {
    return -code error "$argv0: $result"
}

if {[catch {exec sddsprocess /tmp/srBPMFast.mon -print=column,Provider,ca -print=column,ExpectNumeric,y,type=character -print=column,ExpectFieldType,scalar -define=column,ExpectElements,1,type=long -nowarn} results]} {
    puts "ERROR: $results"
    exit
}


puts "Comparing against master PV list"
exec sddsselect /tmp/srBPMFast.mon /home/helios/oagData/pvdata/iocRecNamesOAG.sdds -pipe=out -match=SimpleControlName=rec_name -nowarning -reuse | sddsconvert -pipe=in ./../DataLogging/srBPMFast.mon -delete=column,SimpleControlName

file delete -force /tmp/srBPMFast.mon $tmpRoot.0 $tmpRoot.1 $tmpRoot.2

for {set i 1} {$i <= 40} {incr i} {
    if {$i == 40} {
        set b 1
    } else {
        set b [expr {$i + 1}]
    }
    if {$i == 25} {
        exec sddsprocess ./../DataLogging/srBPMFast.mon ./../DataLogging/srBPMFast_${i}.mon \
          -match=column,ControlName=S${b}A:*,ControlName=S${i}B:*,|,ControlName=S${i}BM:*,|,ControlName=S${i}ID:*,|,ControlName=SR:S${i}:CH??:TempValC,|,ControlName=SR:S${i}:CH??:TempVal,|,ControlName=S${i}:C1:1771N:ai:S?,|     
    } else {
        exec sddsprocess ./../DataLogging/srBPMFast.mon ./../DataLogging/srBPMFast_${i}.mon \
          -match=column,ControlName=S${b}A:*,ControlName=S${i}B:*,|,ControlName=S${i}BM:*,|,ControlName=S${i}ID:*,|
    }
    sdds load ./../DataLogging/srBPMFast_${i}.mon data
    set data(ParameterNames) "InstallLocation"
    set data(Parameter.InstallLocation) [list /home/helios/oagData/logging/srBPMFast/${i}/srBPMFast_${i}.mon]
    set data(Layout.DataMode.Mode) "ascii"
    sdds save ./../DataLogging/srBPMFast_${i}.mon data
}

puts "use cppatch to install srBPMFast_1.mon through srBPMFast_40.mon"

