#!/bin/sh  
# \
exec oagtclsh "$0" "$@"
    
# $Log: not supported by cvs2svn $

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

# 
# This script measure the normalized ID intensity i.e., ID intensity divided by the SR current.

set usage "usage: measureBMIntensity -video <video> -findBeam <0|1>"
set video ""
set xCenter 255
set yCenter 241
set findBeam 0
set args $argv
if {[APSStrictParseArguments {video findBeam}] || ![string length $video] } {
    puts stderr "Error: the video system is not provided!"
    puts stderr $usage
    exit 1
}
# original value was :raw:peakPosM
if $findBeam {
    if [catch {exec cavget -list=${video} -list=:x,:y -list=:fit:centroidM -pend=30} posList] {
        puts stderr "Error: $posList"
        exit 1
    }
    set xPeak [lindex $posList 0]
    set yPeak [lindex $posList 1]
    set diff [expr abs($xPeak-$xCenter) + abs($yPeak-$yCenter)]
    puts [expr 1.0 * $diff]
} else {
    if [catch {exec cavget -list=${video} -list=:roiXSizeC -pend=30} ROIsize] {
        puts stderr "Error: $ROIsize"
        exit 1
    }
    
    if [catch {exec sddswget -pv=${video}:xDataWF -pipe=out \
                 | sddsprocess -pipe \
                 -clip=$ROIsize,0,invert -process=Waveform,sum,totalInt \
                 | sdds2stream -pipe -par=totalInt } totalInt] {
        puts stderr "Error: $totalInt"
        exit 1
    }
    puts [expr 1.0 * $totalInt]
}
exit 0
