#!/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

#Output files: ../DataLogging/SRvac.mon
#              ../DataLogging/SRvac.loc
#              ../glitchLogs/SRvac.glitchlog

set data(ParameterNames) "InstallLocation"
set data(ColumnNames) "ControlName ReadbackName Provider ExpectNumeric ExpectFieldType ExpectElements"
set data(ParameterInfo.InstallLocation) "type SDDS_STRING"
set data(ColumnInfo.ControlName) "type SDDS_STRING"
set data(ColumnInfo.ReadbackName) "type SDDS_STRING"
set data(ColumnInfo.Provider) "type SDDS_STRING"
set data(ColumnInfo.ExpectNumeric) "type SDDS_CHARACTER"
set data(ColumnInfo.ExpectFieldType) "type SDDS_STRING"
set data(ColumnInfo.ExpectElements) "type SDDS_LONG"

set data(Parameter.InstallLocation) "/home/helios/oagData/monitoring/SRvac/SRvac.mon"

set names ""
for {set sector 1} {$sector <= 40} {incr sector} {
    set ssector [format %02d $sector]
    for {set i 1} {$i <= 15} {incr i} {
        for {set j 1} {$j <= 15} {incr j} {
            lappend names S${ssector}-TPS:RP${i}:CCG${j}:PressureM
            lappend names S${ssector}-TPS:RP${i}:CVG${j}:PressureM
            lappend names S${ssector}-TPS:TP${i}:CCG${j}:PressureM
            lappend names S${ssector}-TPS:TP${i}:CVG${j}:PressureM
            lappend names S${ssector}-TPS:VGC${i}:VS${j}:PressureM
            lappend names S${ssector}:VGC${i}:VS${j}:PressureM
            lappend names S${ssector}-BLS:VGC${i}:VS${j}:PressureM
            #lappend names S${ssector}:IPC${i}:IP${j}:PressureM
            #lappend names S${ssector}C:IPC${i}:IP${j}:PressureM
            lappend names S${ssector}BMFE:IPC${i}:IP${j}:PressureM
            lappend names S${ssector}IDFE:IPC${i}:IP${j}:PressureM
        }
    }
    for {set i 1} {$i <= 15} {incr i} {
        lappend names S${ssector}:CCG${i}:PressureM
        lappend names S${ssector}:CVG${i}:PressureM
        lappend names S${ssector}C:CCG${i}:PressureM
        lappend names S${ssector}C:CVG${i}:PressureM
        lappend names S${ssector}ID:CCG${i}:PressureM
        lappend names S${ssector}ID:CVG${i}:PressureM
        lappend names S${ssector}-BLS:IV:CCG${i}:PressureM
        lappend names S${ssector}-BLS:IV:CVG${i}:PressureM
        lappend names S${ssector}-BLS:CAV:CCG${i}:PressureM
        lappend names S${ssector}-BLS:CAV:CVG${i}:PressureM
        lappend names S${ssector}:IP${i}:PressureM
        lappend names S${ssector}C:IP${i}:PressureM
        if {$i != 2} {
            lappend names S${ssector}ID:IP${i}:PressureM
        }
        lappend names S${ssector}BMFE:VS${i}:PressureM
        lappend names S${ssector}IDFE:VS${i}:PressureM
        if {$ssector == 36} {
            lappend names S${ssector}C:IP${i}:PressureM.CRNT
            lappend names S${ssector}C:IP${i}:PressureM.VOLT
            lappend names S${ssector}TR:IP${i}:PressureM.CRNT
            lappend names S${ssector}TR:IP${i}:PressureM.VOLT
        }

        foreach suffix "ClosedM" {
            lappend names S${ssector}A:GV${i}:$suffix
            lappend names S${ssector}B:GV${i}:$suffix
            lappend names S${ssector}C:GV${i}:$suffix
            lappend names S${ssector}A:SV${i}:$suffix
            lappend names S${ssector}B:SV${i}:$suffix
            lappend names S${ssector}C:SV${i}:$suffix
        }
        foreach suffix "OpenC CloseC PositionM OpenedM ClosedM PermitM NocatasM PlcOpenM FaultM OK2OpenM MustCloseM MpiLockM OnC OffC RunningM OverloadM PlcOnM OK2RunM MustStopM Sp1M NomSpeedM KsForcedM KsForcedOpenM KsForcedCloseM InputsPsM OutputsPsM ScreenPsM " {
            lappend names S${ssector}-BLS:GV${i}:$suffix
            lappend names S${ssector}-BLS:IV:MV${i}:$suffix
            lappend names S${ssector}-BLS:IV:BV${i}:$suffix
            lappend names S${ssector}-BLS:IV:TV${i}:$suffix
            lappend names S${ssector}-BLS:IV:RP${i}:$suffix
            lappend names S${ssector}-BLS:IV:TP${i}:$suffix
            lappend names S${ssector}-BLS:IV:CCG${i}:$suffix
            lappend names S${ssector}-BLS:IV:CVG${i}:$suffix
            lappend names S${ssector}-BLS:IVC${i}:MV_$suffix
            lappend names S${ssector}-BLS:IVC${i}:BV_$suffix
            lappend names S${ssector}-BLS:IVC${i}:TV_$suffix
            lappend names S${ssector}-BLS:IVC${i}:RP_$suffix
            lappend names S${ssector}-BLS:IVC${i}:TP_$suffix
            for {set j 1} {$j <= 15} {incr j} {
                lappend names S${ssector}-BLS:IVC${i}:CCG${j}_$suffix
                lappend names S${ssector}-BLS:IVC${i}:CVG${j}_$suffix
            }
            lappend names S${ssector}-BLS:CAV:MV${i}:$suffix
            lappend names S${ssector}-BLS:CAV:BV${i}:$suffix
            lappend names S${ssector}-BLS:CAV:TV${i}:$suffix
            lappend names S${ssector}-BLS:CAV:RP${i}:$suffix
            lappend names S${ssector}-BLS:CAV:TP${i}:$suffix
            lappend names S${ssector}-BLS:CAV:CCG${i}:$suffix
            lappend names S${ssector}-BLS:CAV:CVG${i}:$suffix
            lappend names S${ssector}-BLS:CVC${i}:MV_$suffix
            lappend names S${ssector}-BLS:CVC${i}:BV_$suffix
            lappend names S${ssector}-BLS:CVC${i}:TV_$suffix
            lappend names S${ssector}-BLS:CVC${i}:RP_$suffix
            lappend names S${ssector}-BLS:CVC${i}:TP_$suffix
            for {set j 1} {$j <= 15} {incr j} {
                lappend names S${ssector}-BLS:CVC${i}:CCG${j}_$suffix
                lappend names S${ssector}-BLS:CVC${i}:CVG${j}_$suffix
            }
        }
        for {set j 1} {$j <= 15} {incr j} {
            lappend names S${ssector}-BLS:CVC${i}:Fin{j}M
            lappend names S${ssector}-BLS:CVC${i}:Fout{j}M
        }
    }     
}
foreach n $names {
    append data(Column.ControlName) "$n "
    append data(Column.ReadbackName) "$n "
    append data(Column.Provider) "ca "
    append data(Column.ExpectNumeric) "y "
    append data(Column.ExpectFieldType) "scalar "
    append data(Column.ExpectElements) "1 "
}

for {set sector 36} {$sector <= 40} {incr sector} {
    set ssector [format %02d $sector]
    for {set j 1} {$j <= 4} {incr j} {
        append data(Column.ControlName) "SRF:S${sector}:C${j}:VGC.PRES "
        append data(Column.ReadbackName) "S${sector}:RFC${j}:PressureM "
        append data(Column.Provider) "ca "
        append data(Column.ExpectNumeric) "y "
        append data(Column.ExpectFieldType) "scalar "
        append data(Column.ExpectElements) "1 "
    }
}

append data(Column.ControlName) "S-DCCT:CurrentM "
append data(Column.ReadbackName) "S-DCCT:CurrentM "
append data(Column.Provider) "ca "
append data(Column.ExpectNumeric) "y "
append data(Column.ExpectFieldType) "scalar "
append data(Column.ExpectElements) "1 "

#append data(Column.ControlName) "S-DCCT:LifetimeM "
#append data(Column.ReadbackName) "S-DCCT:LifetimeM "
#append data(Column.Provider) "ca "
#append data(Column.ExpectNumeric) "y "
#append data(Column.ExpectFieldType) "scalar "
#append data(Column.ExpectElements) "1 "

append data(Column.ControlName) "S-INJ:MPS:LifetimeM "
append data(Column.ReadbackName) "S-INJ:MPS:LifetimeM "
append data(Column.Provider) "ca "
append data(Column.ExpectNumeric) "y "
append data(Column.ExpectFieldType) "scalar "
append data(Column.ExpectElements) "1 "

append data(Column.ControlName) "S-INJ:MPS:LifetimeMinutesM "
append data(Column.ReadbackName) "S-INJ:MPS:LifetimeMinutesM "
append data(Column.Provider) "ca "
append data(Column.ExpectNumeric) "y "
append data(Column.ExpectFieldType) "scalar "
append data(Column.ExpectElements) "1 "

append data(Column.ControlName) "S-DCCT:AmpHoursAM "
append data(Column.ReadbackName) "S-DCCT:AmpHoursAM "
append data(Column.Provider) "ca "
append data(Column.ExpectNumeric) "y "
append data(Column.ExpectFieldType) "scalar "
append data(Column.ExpectElements) "1 "

append data(Column.ControlName) "S-DCCT:AmpHoursM "
append data(Column.ReadbackName) "S-DCCT:AmpHoursM "
append data(Column.Provider) "ca "
append data(Column.ExpectNumeric) "y "
append data(Column.ExpectFieldType) "scalar "
append data(Column.ExpectElements) "1 "

append data(Column.ControlName) "S-INJ:BCM:NumberOfBunchesFilledM "
append data(Column.ReadbackName) "SRBucketsFilled "
append data(Column.Provider) "ca "
append data(Column.ExpectNumeric) "y "
append data(Column.ExpectFieldType) "scalar "
append data(Column.ExpectElements) "1 "

append data(Column.ControlName) "S34-SBM:liveM S34-SBM:limitM S34-SBM:tripM S36-SBM:liveM S36-SBM:limitM S36-SBM:tripM "
append data(Column.ReadbackName) "S34-SBM:liveM S34-SBM:limitM S34-SBM:tripM S36-SBM:liveM S36-SBM:limitM S36-SBM:tripM "
append data(Column.Provider) "ca ca ca ca ca ca "
append data(Column.ExpectNumeric) "y y y y y y "
append data(Column.ExpectFieldType) "scalar scalar scalar scalar scalar scalar "
append data(Column.ExpectElements) "1 1 1 1 1 1 "

set data(Column.ControlName) [list $data(Column.ControlName)]
set data(Column.ReadbackName) [list $data(Column.ReadbackName)]
set data(Column.Provider) [list $data(Column.Provider)]
set data(Column.ExpectNumeric) [list $data(Column.ExpectNumeric)]
set data(Column.ExpectFieldType) [list $data(Column.ExpectFieldType)]
set data(Column.ExpectElements) [list $data(Column.ExpectElements)]

sdds save ../DataLogging/SRvac.mon.new data

exec sddssort ../DataLogging/SRvac.mon.new -col=ControlName -num -nowarn

puts "Comparing against master PV list"

exec sddsprocess ../DataLogging/SRvac.mon.new -pipe=out \
  -edit=column,SimpleControlName,ControlName,S?/./K | \
  sddsselect -pipe /home/helios/oagData/pvdata/iocRecNamesOAG.sdds \
    -match=SimpleControlName=rec_name -nowarning -reuse | \
    sddsconvert -pipe=in ./../DataLogging/SRvac.mon -delete=column,SimpleControlName

file delete ../DataLogging/SRvac.mon.new ../DataLogging/SRvac.mon.new~

unset data
exec sddsprocess ../DataLogging/SRvac.mon -pipe=out \
  "-match=column,ControlName=S*:PressureM" \
  -print=column,ReadbackUnits,T \
  -delete=para,InstallLocation | \
  sddsbreak -pipe=in ../glitchLogs/SRvac.glitchlog -rowlimit=1

sdds load ../glitchLogs/SRvac.glitchlog data
set data(ParameterNames) "OutputRootname TriggerControlName MinorAlarm MajorAlarm NoAlarm InstallLocation"
set data(ParameterInfo.OutputRootname) "type SDDS_STRING"
set data(ParameterInfo.TriggerControlName) "type SDDS_STRING"
set data(ParameterInfo.MinorAlarm) "type SDDS_SHORT"
set data(ParameterInfo.MajorAlarm) "type SDDS_SHORT"
set data(ParameterInfo.NoAlarm) "type SDDS_SHORT"
set data(ParameterInfo.InstallLocation) "type SDDS_STRING"

set cnList ""
set data(Column.ReadbackName) ""
set data(Column.Provider) ""
set data(Column.ExpectNumeric) ""
set data(Column.ExpectFieldType) ""
set data(Column.ExpectElements) ""
set data(Column.ReadbackUnits) ""
foreach cnPage $data(Column.ControlName) {
    set cn [lindex $cnPage 0]
    set prefix [string range $cn 0 end-10]
    
    lappend data(Parameter.OutputRootname) ${prefix}/$prefix
    lappend data(Parameter.TriggerControlName) $cn
    lappend data(Parameter.MinorAlarm) 1
    lappend data(Parameter.MajorAlarm) 1
    lappend data(Parameter.NoAlarm) 0
    lappend data(Parameter.InstallLocation) /home/helios/oagData/glitchLogs/SRvac/SRvac.glitchlog  
    
    if {([string first "36C:IP" $prefix] != -1) || ([string first "37C:IP" $prefix] != -1) || ([string first "40C:IP" $prefix] != -1)} {
        lappend cnList "$cn ${cn}.CRNT ${cn}.VOLT"
        lappend data(Column.ReadbackName) "$cn ${cn}.CRNT ${cn}.VOLT"
        lappend data(Column.Provider) "ca ca ca"
        lappend data(Column.ExpectNumeric) "y y y"
        lappend data(Column.ExpectFieldType) "scalar scalar scalar"
        lappend data(Column.ExpectElements) "1 1 1"
        lappend data(Column.ReadbackUnits) "Torr Amps Volts"
    } else {
        lappend cnList "$cn"
        lappend data(Column.ReadbackName) "$cn"
        lappend data(Column.Provider) "ca"
        lappend data(Column.ExpectNumeric) "y"
        lappend data(Column.ExpectFieldType) "scalar"
        lappend data(Column.ExpectElements) "1"
        lappend data(Column.ReadbackUnits) "Torr"
    }
}
set data(Column.ControlName) $cnList
sdds save ../glitchLogs/SRvac.glitchlog data


#Most .HIHI limits are 1e-6
#I am setting a tolerance that will allow 100 steps
exec sddsprocess ../DataLogging/SRvac.mon ../DataLogging/SRvac.loc1 \
  "-match=column,ControlName=S*:PressureM" \
  -define=column,Tolerance,5e-8,type=double \
  -reprint=para,InstallLocation,/home/helios/oagData/logonchange/SRvac/SRvac.loc

exec sddsprocess ../DataLogging/SRvac.mon ../DataLogging/SRvac.loc2 \
  "-match=column,ControlName=S*:ClosedM" \
  -define=column,Tolerance,.5,type=double \
  -reprint=para,InstallLocation,/home/helios/oagData/logonchange/SRvac/SRvac.loc

exec sddscombine ../DataLogging/SRvac.loc1 ../DataLogging/SRvac.loc2 -pipe=out -merge | \
    sddsconvert -pipe=in ../DataLogging/SRvac.loc -ascii -delete=para,NumberCombined

file delete ../DataLogging/SRvac.loc1 ../DataLogging/SRvac.loc2



