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

# input files: S-MPS.mon.template
#
# output files: S-MPS.mon
#

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

set root S-MPS

exec sddsprocess /home/helios/oagData/pvdata/iocRecNamesOAG.sdds -pipe=out \
  "-match=column,rec_name=S-MPS:*M,rec_name=*imestampM,!,&,rec_name=*FirstFaultM,!,&,rec_name=*BeamCurrentStatusM,!,&,rec_name=*WaveformM,!,&,rec_name=S-MPS:FirstFaultTimeM,!,&" -delete=column,ioc_name | \
  sddsconvert -pipe \
  -rename=column,rec_name=ControlName | \
  sddssort -pipe=in ../DataLogging/S-MPS.mon.others.new \
  -col=ControlName -num -uniq

sdds load ../DataLogging/S-MPS.mon.others.new data
unset data(Description.Text)
unset data(Description.Contents)
sdds save ../DataLogging/S-MPS.mon.others.new data

exec sddscainfo ../DataLogging/S-MPS.mon.others.new ../DataLogging/S-MPS.mon.others.new.info -pend=10
file delete ../DataLogging/S-MPS.mon.others.new

sdds load ../DataLogging/S-MPS.mon.others.new.info data
set data(ColumnNames) "ControlName ReadbackName ReadbackUnits"
set data(Column.ReadbackName) $data(Column.ControlName)
set data(Column.ReadbackUnits) $data(Column.Units)
set data(Layout.DataMode.Mode) "ascii"
sdds save ../DataLogging/S-MPS.mon.others data
file delete ../DataLogging/S-MPS.mon.others.new.info

for {set sector 0} {$sector<41} {incr sector} {
    # sectoru is the unformatted sector value.
    exec sddsprocess ./../DataLogging/${root}.mon.template /tmp/${root}.mon.$sector \
      -reedit=column,ControlName,%/<sector>/[format %02ld $sector]/ \
      -reedit=column,ReadbackName,%/<sector>/[format %02ld $sector]/
    lappend fileList /tmp/${root}.mon.$sector
}

    
if [catch {eval exec sddscombine $fileList ../DataLogging/S-MPS.mon.others -pipe=out -merge | \
             sddssort -pipe -col=ControlName -numericHigh -unique | \
             sddsprocess -pipe=in /tmp/${root}.mon \
             -edit=column,SimpleControlName,ControlName,S?/./K \
             -delete=col,sector} result] {
    puts stderr "error: $result"
    exit
}

eval file delete $fileList

puts "Comparing against master PV list"
exec sddsselect /tmp/${root}.mon /home/helios/oagData/pvdata/iocRecNamesOAG.sdds -pipe=out -match=SimpleControlName=rec_name -nowarning -reuse | sddsprocess -pipe -print=column,Provider,ca -print=column,ExpectNumeric,y,type=character -print=column,ExpectFieldType,scalar -define=column,ExpectElements,1,type=long | sddsconvert -pipe=in ./../DataLogging/${root}.mon -delete=column,SimpleControlName

file delete -force /tmp/${root}.mon

sdds load ./../DataLogging/${root}.mon data
set data(ParameterNames) "InstallLocation"
set data(Parameter.InstallLocation) [list /home/helios/oagData/logging/${root}/${root}.mon]
set data(Layout.DataMode.Mode) "ascii"
sdds save ./../DataLogging/${root}.mon data


