#!/bin/sh
# \
  exec oagwish "$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

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/bin/setupCameraForBunchLengthMeas } result] {
        SetStatus "Error in running initialization: $result"
        return
    }
    set runInitialize 0
    SetStatus "done."
}
proc RUN {} {
    global outputDir rootname configuration type config runInitialize
    SetStatus "start experiment with ExperimentDesigner (may take a while, please wait)..."
    switch $type {
        One {
            set config /home/helios/PAR/experimentConfig/bunchLengthImageCameraSweepTimeScanConfig
            set dec "one bunch length scan with sweep time fitting"
        }
        Full {
            set config /home/helios/PAR/experimentConfig/bunchLengthImageTimeScanConfig
            set dec "full bunch length scan over one par cycle from streak camera data"
        }
    }
    SetStatus "start $dec with ExperimentDesigner (may take a while, please wait)..."
    APSExecLog .log -width 80 -unixCommand "ExperimentDesigner -popupInterface 0 -outputRootname $rootname -outputDir $outputDir \
      -configuration $config -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 [regexp {One} $outputDir] {
        #one bunch length scan with sweep time fitting
        if [catch {exec /home/helios/PAR/bin/processAndFit $rootname} result] {
            SetStatus "Error in procesing one bunch length data: $result"
            cd $oldDir
            return
        }
    } elseif [regexp {Full} $outputDir] {
        #full bunch length scan onver one par cycly from streak camera data
        if [catch {exec /home/helios/PAR/bin/processBunchSigma $rootname} result] {
            SetStatus "Error in processing full bunch length data: $result"
            cd $oldDir
            return
        }
    }
    cd $oldDir
    SetStatus "done."
}

proc SetDailyOutputDir {args} {
    global type outputDir
    set outputDir  [APSGoToDailyDirectory -subdirectory par${type}BunchLengthMeas]
}

set status ""

set rootname parBunchLen
set configuration /home/helios/PAR/experimentConfig/bunchLengthCameraSweepTimeScanConfig
set type One
SetDailyOutputDir
set runInitialize 1
APSApplication . -name PARBunchLengthMeas -version $CVSRevisionAuthor \
  -overview {par bunch length measurement with sweep time fitting. This is an utility that calls commandline ExperimentDesigner for running experiment}
APSScrolledStatus .status -parent .userFrame -textVariable status -width 100 \
  -withButtons 1
APSRadioButtonFrame .type -parent .userFrame -buttonList {one-bunch-length-measure full-bunch-length-measure} \
  -valueList {One Full} -variable type -label "Select measurement type:" -orientation horizontal \
  -commandList {SetDailyOutputDir SetDailyOutputDir}
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 .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."




