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

#
# $Log: not supported by cvs2svn $
# Revision 1.5  2003/09/03 16:18:22  soliday
# Added package require command for BLT
#
# Revision 1.4  2002/09/17 20:20:36  soliday
# Added the background option to the blt axis.
#
# Revision 1.3  2002/04/09 20:49:39  emery
# Changed default size.
#
# Revision 1.2  2002/04/09 20:44:51  emery
# Added bpm options ID and BM. Added method to reduce the graph
# size to fit the number of graphs requested.
#
# Revision 1.1  1999/08/30 07:23:45  emery
# First installation. Based on SRBPLDPositionDisplay
#
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)]
APSDebugPath

set CVSRevisionAuthor "\$Revision: 1.6 $ \$Author: soliday $"
wm geometry . +10+10
APSApplication . -name "Xray beam position ErrorCC" -version $CVSRevisionAuthor\
	-overview "SR Xray beam positions are display in a live x/y graph. A white dot \
	indicates that the position is within the minor alarm range.  A yellow dot indicates a minor alarm. \
	A red dot indicates a major alarm."

package require BLT
#
# Creates a new graph
#
proc displayGraph {args} {
    set minX -1
    set maxX 1
    set minY -1
    set maxY 1
    set Xpos 0
    set Ypos 0
    set Title "No Title"
    set dimension 150
    set Name [APSUniqueName a]
    APSStrictParseArguments {minX maxX minY maxY Title Name Xpos Ypos Frame dimension}
    global xVector$Name yVector$Name

    blt::graph .$Name -cursor crosshair -width $dimension -height $dimension -bg black \
      -title $Title -topmargin 15 -rightmargin 15 -bottommargin 53 \
      -leftmargin 53 -fg white -plotbackground #737373
    .$Name xaxis configure -color white -background black -max $maxX -min $minX
    .$Name yaxis configure -color white -background black -max $maxY -min $minY
    blt::vector xVector$Name yVector$Name
    xVector$Name set $Xpos
    yVector$Name set $Ypos
    .$Name element create dot -xdata xVector$Name -ydata yVector$Name\
      -symbol circle -color white
    .$Name legend configure -hide yes
    pack .$Name -in $Frame -side left
    return
}

#
# Changes the plot of an existing graph
#
proc modifyGraph {Name Xpos Ypos minX maxX minY maxY} {
    global xVector$Name yVector$Name
    xVector$Name set $Xpos
    yVector$Name set $Ypos
    set leftX [expr $Xpos - $minX]
    set rightX [expr $maxX - $Xpos]
    set bottomY [expr $Ypos - $minY]
    set topY [expr $maxY - $Ypos]
    set background black
    if {$leftX <= 0 || $rightX <= 0 || $bottomY <=0 || $topY <= 0} {
        set warning grey
        set background red
    } elseif {$leftX <= .1 || $rightX <= .1 || $bottomY <=.1 || $topY <= .1} {
        set warning red
    } elseif {$leftX <= .3 || $rightX <= .3 || $bottomY <=.3 || $topY <= .3} {
        set warning yellow
    } else {
        set warning white
    }
    
    .$Name element configure dot -color $warning
    .$Name configure -fg $warning -bg $background
}

#
# Check the command line arguments
#
set args $argv
set bpm all
APSStrictParseArguments {bpm}
switch $bpm {
    ID {
        set bpmList "[APSGetIDXRayBPMList]"
    }
    BM {
        set bpmList "[APSGetBMXRayBPMList]"
    }
    all {
        set bpmList "[APSGetIDXRayBPMList] [APSGetBMXRayBPMList]"
    }
    default {
        set bpmList $bpm
    }
}

#
# For each sector set up the variables
#
foreach bpm $bpmList {
    switch -regexp $bpm {
        ID {
            lappend pvList ${bpm}:msAve:x:ErrorCC ${bpm}:msAve:y:ErrorCC  
            lappend tclVarList ${bpm}x ${bpm}y
        }
        BM {
            lappend pvList ${bpm}:msAve:y:ErrorCC  
            lappend tclVarList ${bpm}y
            # there is no x readback so show a value of zero on the graph
            set ${bpm}x 0
        }
    }
}

#
# Link all the variables
#
if [pv linkw $tclVarList $pvList]!=0 {
    APSAlertBox [APSUniqueName .] -errorMessage "Problem with connection: $errorInfo"
    exit 
}

#
# For each sector call displayGraph to make a new graph
#
# size in pixels of available display
set displayWidth [expr 1920 - 0]
set displayHeight [expr 1280 - 30 ]
set numDisplay [llength $bpmList]
# default size in pixel of one graph
set defaultSize 500
set margin 0
set dimension $defaultSize

# try to fit all in default size
set columns [expr int($displayWidth / ($defaultSize + $margin)) ]
set rows [expr int($displayHeight / ($defaultSize  + $margin))  ]
set numPossibleDisplays [expr $columns * $rows]
if {$numPossibleDisplays > $numDisplay} {
    set GraphNumber $columns
} else {
    while {$numPossibleDisplays < $numDisplay} {
        set dimension [expr $dimension - 5]
        set columns [expr int($displayWidth / ($dimension + $margin) ) ]
        set rows [expr int($displayHeight / ($dimension + $margin) ) ]
        set numPossibleDisplays [expr $columns * $rows]
    }
    set GraphNumber $columns
}
set GraphNumber $columns
set GraphFrame firstRow
foreach bpm $bpmList {
    if {$GraphNumber == $columns} {
        set GraphFrame [APSUniqueName .]
        frame $GraphFrame -bg black
        pack $GraphFrame -in .userFrame -side top -expand true -fill x
        set GraphNumber 0
    }
    incr GraphNumber
    set minX -0.3
    set maxX 0.3
    set minY -0.3
    set maxY 0.3
    displayGraph -minX $minX -maxX $maxX -minY $minY \
      -dimension $dimension \
      -maxY $maxY -Title "$bpm" \
      -Name [string tolower $bpm] -Frame $GraphFrame  
}

#
# Update graph values every two seconds
#
while 1 {
    update
    eval global $tclVarList 
    if [pv getw $tclVarList]!=0 {
        APSAlertBox [APSUniqueName .] -errorMessage "Problem with connection: $errorInfo"
        exit
    }
    foreach bpm $bpmList {
        set minX -0.3
        set maxX 0.3
        set minY -0.3
        set maxY 0.3
        modifyGraph [string tolower $bpm] [set ${bpm}x] [set ${bpm}y] $minX $maxX $minY $maxY
    }
    after 2000
}
