#!/bin/sh  
# \
exec oagwish "$0" "$@"
#
# $Log: not supported by cvs2svn $
# Revision 1.2  2011/02/25 22:54:28  shang
# fixed a bug in finding files.
#
# Revision 1.1  2011/02/25 22:02:30  shang
# provide review for booster energy saver.
#
#
#

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 CVSRevisionAuthor "\$Revision: 1.3 $ \$Author: shang $"

proc SetStatus {text} {
    global status
    set status "[clock format [clock seconds] -format %H:%M:%S] $text"
    update
}

proc FindFiles {args} {
    global startYear startMonth startDay startHour stopYear stopMonth stopDay stopHour startTime stopTime rootname
    if {$startYear!=$stopYear} {
	return -code error "unable to view data more than one year!"
    }
    set mainDir /home/helios/ASDOPS/daily
    set startTime [expr $startHour*3600 + [clock scan $startMonth/$startDay/$startYear]]
    set endTime [expr $stopHour*3600 + [clock scan $stopMonth/$stopDay/$stopYear]]
    
    set jstart [scan [clock format $startTime -format %j] %ld]
    set jend [scan [clock format $endTime -format %j] %ld]
    set fileList ""
    for {set jday $jstart} {$jday<=$jend} {incr jday} {
	set time [clock scan $startYear-$jday -format %Y-%j]
	set dir [clock format $time -format %y%m]
	set day [clock format $time -format %d]
	set subdir $mainDir/$dir/$day
	SetStatus "$jday $dir"
	foreach shift {1 2 3} {
	    set dir $subdir/$shift/rampTest
	    if [file exist $dir] {
		set files [glob -nocomplain $dir/${rootname}-*]
		if [llength $files] {
		    set fileList [concat $fileList $files]
		}
	    }
	}
    }
    if ![llength $fileList] {
	return -code error "No files found."
    }
    SetStatus "[llength $fileList] files found."
    
    set tmpFile /tmp/[APSTmpString].ramptest
    if [catch {eval exec sddscombine $fileList -pipe=out -merge \
		   | sddssort -pipe -col=Time \
		   | tee $tmpFile.1 \
		   | sddsprocess -pipe=in $tmpFile -filter=col,Time,$startTime,$endTime } result] {
	return -code error "No data found between start/stop time: $result"
    }
    return $tmpFile
}

proc PlotData {args} {
    global outputDir rootname

    if [catch {FindFiles} file] {
	SetStatus "$file"
	return
    }
    eval exec sddsplot -topline=RMS -col=Time,B:*:DeltaIrmsAO -sep -grap=sym,sub=1 -tick=xtime $file &
    eval exec sddsplot -topline=AFGDelay  -col=Time,It:*VafgTrigAO.VAL -sep -grap=sym,sub=1 -tick=xtime $file &
    eval exec sddsplot -topline=AFGGain  -col=Time,B:*:VoltageRT.GAIN -sep -grap=sym,sub=1 -tick=xtime $file &
    set cols [exec sddsquery -col $file]
    if [lsearch -exact $cols "B:BM:IRampRMSAI"]>=0 {
        eval exec sddsplot -topline=IRamp-RMS -col=Time,B:*IRampRMSAI -sep -grap=sym,sub=1 -tick=xtime $file &
        eval exec sddsplot -topline=IRamp-Gain -col=Time,B:*IRampGainErrAI -sep -grap=sym,sub=1 -tick=xtime $file &
        eval exec sddsplot -topline=IRamp-Delay -col=Time,B:*IRampDelayErrAI -sep -grap=sym,sub=1 -tick=xtime $file &
    }
}


APSApplication . -name "Booster Energy Save Review" -version $CVSRevisionAuthor \
  -overview "booster energy saver review."

set status "Ready."
set IRamp 1
set rootname boosterIRamp

APSScrolledStatus .status -parent .userFrame -textVariable status \
    -width 70 -height 8

APSDateTimeAdjEntry .startTime -parent .userFrame \
    -dayVariable startDay -yearVariable startYear \
    -monthVariable startMonth -hourVariable startHour \
    -label "Start date/time: " -twoDigitYear 0 -defaultHour 0
set startHour 0
APSDateTimeAdjEntry .stopTime -parent .userFrame \
    -dayVariable stopDay -yearVariable stopYear \
    -monthVariable stopMonth -hourVariable stopHour \
    -label "Stop date/time: " -twoDigitYear 0 -defaultHour 24
set stopHour 24
APSRadioButtonFrame .type -parent .userFrame -label "Ramp Type:" -variable IRamp -buttonList {IRamp VRamp} \
  -valueList {1 0} -commandList {"set rootname boosterIRamp" "set rootname boosterVRamp"} -orientation horizontal

APSButton .plot -parent .userFrame -text "PLOT" -command "PlotData"
