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

# $Log: not supported by cvs2svn $
# Revision 1.1  2009/07/30 18:49:26  shang


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.1 $ \$Author: shang $"

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

proc Initialize {} {
    global type runInitialize
    SetStatus "Initialize ...."
    if [catch {exec /home/helios/PAR/experimentConfig/RF12DemodWF-WT1OffsetScanConfig/setupRF12Demod } result] {
        SetStatus "Error in running initialization: $result"
        return
    }
    set runInitialize 0
    SetStatus "done."
}
set configuration /home/helios/PAR/experimentConfig/RF12DemodWF-WT1OffsetScanConfig/
proc RUN {} {
    global outputDir rootname configuration runInitialize
    SetStatus "start experiment with ExperimentDesigner (may take a while, please wait)..."
    APSExecLog .log -width 80 -unixCommand "ExperimentDesigner -popupInterface 0 -outputRootname $rootname -outputDir $outputDir \
      -configuration $configuration -initialize $runInitialize" \
        -height 30 -lineLimit 2048
    SetStatus "Measurement done."
    set runInitialize 0
}

proc PostProcess {args} {
    global outputDir rootname
    SetStatus "Processing data ..."
    set oldDir [pwd]
    cd $outputDir
    if [catch {exec /home/helios/PAR/experimentConfig/RF12DemodWF-WT1OffsetScanConfig/combineAndPlot \
                 $outputDir $rootname & } result] {
        return -code error $result
    }
    cd $oldDir
    SetStatus "done."
}

proc ReadWaveform {args} {
    global arm outputDir rootname waitTime
    SetStatus "Taking single waveform data ..."
    if $arm {
        if [catch {exec cavput -list=PAR:awg:phiMag:gtr:arm=1,PAR:awg:phiMag:gtr:autoRestart=1 } result] {
            return -code error $result
        }
    }
    for {set t 0} {$t<$waitTime} {incr t} {
        after 1000
        update
    }
    set filename $outputDir/${rootname}-[clock format [clock seconds] -format %Y-%m%d:%H%M%S]
    if [catch {exec sddswmonitor -pv=PAR:awg:phiMag:gtr:waveform0,PAR:awg:phiMag:gtr:waveform1 \
                 -step=1 $filename} result] {
        return -code error $result
    }
    
    exec sddsplot -grap=line,vary -legend \
    -col=Index,PAR:awg:phiMag:gtr:waveform1 $filename \
    	-factor=yMultiplier=0.005493164062500,xMultiplier=3.5802e-3 \
	-offset=xChange=67 -yScalesgroup=request \
	-yLabel=Phase\(Deg\) -xLabel=Time\(ms\)  \
	-col=Index,PAR:awg:phiMag:gtr:waveform0 $filename \
	-grap=li,vary -legend \
	-factor=xMultiplier=3.5802e-3 \
	-offset=xChange=67 -yScalesgroup=request \
	-yLabel=Amplitude\(adcCounts\) &
    SetStatus "done."

}
proc SetDailyOutputDir {args} {
    global outputDir
    set outputDir  [APSGoToDailyDirectory -subdirectory RF12PhaseAmp]
}

set status ""

set rootname RF12PhaseAmp
set waitTime 10
set arm 0
SetDailyOutputDir
set runInitialize 1
APSApplication . -name RF12PhaseAmplitudeMeas -version $CVSRevisionAuthor \
  -overview {RF12 phase amplitude measurement. This is an utility that calls commandline ExperimentDesigner for running experiment}
APSScrolledStatus .status -parent .userFrame -textVariable status -width 100 \
  -withButtons 1
APSRadioButtonFrame .arm -parent .userFrame -buttonList {Yes No} -variable arm -orientation horizontal \
  -valueList {1 0} -label "Arm before collecting single waveform?"
APSLabeledEntry .wait -parent .userFrame -label "Wait time after arm (seconds):" -textVariable waitTime
APSRadioButtonFrame .init -parent .userFrame -buttonList {Yes No} -label "Run Initialization before experiment?" \
    -valueList {1 0} -variable runInitialize -orientation horizontal \
    -contextHelp "check to wheter run initialization for the experiment."
APSLabeledEntry .dir -parent .userFrame -width 80 -textVariable outputDir -label "Directory:" \
  -contextHelp "directory for saving the output files."
APSButton .daily -parent .userFrame.dir -packOption "-side right" \
  -text "daily" -command "SetDailyOutputDir"
APSLabeledEntry .root -parent .userFrame -width 80 -textVariable rootname -label "Rootname:"

APSButton .col -parent .userFrame -text "Collect Single Waveform" -command "ReadWaveform"
APSButton .initial -parent .userFrame -text "Run Initialization" -command "Initialize" \
  -contextHelp "click to run the initialization for experiment."
APSButton .run -parent .userFrame -text "Run Experiment" -command "RUN" \
  -contextHelp "click to run the bunch length measurement experiment."
APSButton .process -parent .userFrame -text "Post Process" -command "PostProcess" \
  -contextHelp "Post process the experiment results."




