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

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

# $Log: not supported by cvs2svn $
# Revision 1.7  2003/08/26 18:04:42  emery
# Added capability of acquiring data from ADC #2.
#
# Revision 1.6  2003/07/24 22:29:14  emery
# Added option -same to the sddsplot command.
#
# Revision 1.5  2001/02/17 00:08:31  emery
# Corrected daily directory button definition, and make
# sure that the daily directory button is placed next to the
# directory entry slot.
#
# Revision 1.4  2000/06/13 21:28:43  emery
# Changed variable attenuation value to 33 dB
# which is the correct value for singlets bunch pattern.
#
# Revision 1.3  2000/06/02 21:50:33  emery
# Added "Done" message.
#
# Revision 1.2  2000/02/15 05:47:34  emery
# Added attenuator entry boxes.
# Removed complex processing which doesn't apply for the
# general bunch train.
#
# Revision 1.1  1998/11/17 21:37:48  emery
# First installation of this application.
#

set CVSRevisionAuthor  "\$Revision: 1.8 $ \$Author: emery $"
APSApplication . -name SRAcquireADC750  -version $CVSRevisionAuthor \
  -overview {}

proc MakeInputFrame {widget args} {
    global directory file inputFrame fixedAttenuator variableAttenuator
    set parent ""
    APSStrictParseArguments {parent}

    APSFrame $widget -parent $parent -label "Input parameters"
    set w $parent$widget.frame
    set inputFrame $w
    APSLabeledEntry .directory -parent $w \
      -label "Output directory:" -textVariable directory \
      -contextHelp "Enter a name for the output file directory." -width 50
    APSButton .daily -parent $w.directory -packOption "-anchor e" \
      -text "daily" -size small \
      -command {set directory [APSGoToDailyDirectory -subdirectory adc750]}
    APSLabeledEntry .file1 -parent $w -label "File name for ADC \#1: " \
      -textVariable file1 -width 45 \
      -contextHelp "Enter the file name for the processed output. The raw data is saved in another file with the extension .raw appended to this filename."
    APSLabeledEntry .file2 -parent $w -label "File name for ADC \#2: " \
      -textVariable file2 -width 45 \
      -contextHelp "Enter the file name for the processed output. The raw data is saved in another file with the extension .raw appended to this filename."
    APSLabeledEntry .attenuatorFixed1 -parent $w -label "Fixed attenuator for ADC \#1: (dB)" \
      -textVariable fixedAttenuator1 \
      -contextHelp "Enter the value of the fixed attenuator, if any, inserted in the signal path."
    APSLabeledEntry .attenuatorVar1 -parent $w -label "Variable attenuator for ADC \#1: (dB)" \
      -textVariable variableAttenuator1 \
      -contextHelp "Enter the value of the variable attenuator inserted in the signal path."

    APSLabeledEntry .attenuatorFixed2 -parent $w -label "Fixed attenuator for ADC \#2: (dB)" \
      -textVariable fixedAttenuator2 \
      -contextHelp "Enter the value of the fixed attenuator, if any, inserted in the signal path."
    APSLabeledEntry .attenuatorVar2 -parent $w -label "Variable attenuator for ADC \#2: (dB)" \
      -textVariable variableAttenuator2 \
      -contextHelp "Enter the value of the variable attenuator inserted in the signal path."
    
    APSFrameGrid .commandFrame -parent $w -yList {acquire process plot}

    set w1 $w.commandFrame.acquire 
    APSButton .acquire1  -parent $w1 \
      -text "Acquire Data on ADC \#1" \
      -command "AcquireACD750 -ADCunit 1" \
      -contextHelp "Acquires data and downloads to a file"
    APSButton .acquire2  -parent $w1 \
      -text "Acquire Data on ADC \#2" \
      -command "AcquireACD750 -ADCunit 2" \
      -contextHelp "Acquires data and downloads to a file"

    set w1 $w.commandFrame.process 
    APSButton .process1  -parent $w1 \
      -text "Process Data from ADC \#1" \
      -command "ProcessData -ADCunit 1" \
      -contextHelp "Processes the data from ADC \#1 and makes plots."
    APSButton .process2  -parent $w1 \
      -text "Process Data from ADC \#2" \
      -command "ProcessData -ADCunit 2" \
      -contextHelp "Processes the data from ADC \#2 and makes plots."

    set w1 $w.commandFrame.plot 
    APSButton .plot1 -parent $w1 \
      -text "Plot processed Data from ADC \#1" \
      -command "PlotBunchMotion -ADCunit 1" \
      -contextHelp "Plots stripline data as a function of bucket number."
    APSButton .plot2 -parent $w1 \
      -text "Plot processed Data from ADC \#2" \
      -command "PlotBunchMotion -ADCunit 2" \
      -contextHelp "Plots stripline data as a function of bucket number."
}

proc AcquireACD750 {args} {
    global directory file1 file2 status 
    global fixedAttenuator1 variableAttenuator1 fixedAttenuator2 variableAttenuator2

    set ADCunit 1
    APSParseArguments {ADCunit}
    set file [set file${ADCunit}]

    if [file exists $directory/$file] {
        return -code error "AcquireACD750: File $directory/$file exists!"
    }
    if [catch {APSSRReadBunchCMTurnByTurn -ADCunit $ADCunit -output $directory/$file.raw \
             } result] {
        return -code error "AcquireACD750: $result"
    }
    if [catch {exec cavget -list=S-DCCT:CurrentM \
             } result] {
        return -code error $result
    }
    set current $result
 
    if [catch {exec sddsprocess $directory/$file.raw \
                 $directory/${file}  \
                 -def=para,S35DCCT,$current \
                 -def=para,ADCUnit,$ADCunit,type=long \
                 -def=para,fixedAttenuator,[set fixedAttenuator$ADCunit] \
                 -def=para,variableAttenuator,[set variableAttenuator$ADCunit] \
                 "-print=para,S35DCCTLabel,%6.2f mA,S35DCCT" \
             } result] {
        return -code error $result
    }
    set status "Data acquired and copied to file."
}

proc ProcessData {args} {
    global file1 file2  directory status

    set ADCunit 1
    APSParseArguments {ADCunit}
    set file [set file${ADCunit}]
    set root [file root $file]

    cd $directory
    set status "Processing data..."
    DisableButtons
    update 

    set tmpfile /tmp/[APSTmpString]
    if [catch {exec sddsprocess ${file} -noWarning \
                 "-def=col,Index,i_row,type=long" \
                 -def=col,Waveforml,Waveform,type=long \
                 "-def=col,Turn,Index 1296 /,type=long" \
                 "-def=col,Bucket,Index 1296 mod,type=long" \
             } result ] {
        return -code error "ProcessData: $result"
    }
    
    if [catch {exec sddsbreak $file $root.break \
                 -increaseof=Turn \
             } result ] {
        return -code error "ProcessData: $result"
    }
    EnableButtons
    set status "Done."
    return
}

proc PlotBunchMotion {args} {
    global plotOptions file1 file2

    set ADCunit 1
    APSParseArguments {ADCunit}
    set file [set file${ADCunit}]
    set root [file root $file]
    if [catch {exec sddsplot -topline=@S35DCCTLabel \
                 $root.break -col=Bucket,Waveforml \
                 -split=pages -sep -grap=dots,sub=2 -same & \
             } result ] {
            return -code error "PlotBunchMotion: $result"
    }
    return
    $ processed files regroup and fft are not calculated
    if [catch {exec sddsplot $root.regroup -topline=@BucketLabel \
                 -col=Turn,Waveforml -split=pages -sep -grap=sym,conn \
                 -mode=y=offset -same & \
             } result ] {
        return -code error "PlotBunchMotion: $result"
    }
    if [catch {exec sddsplot -topline=@BucketLabel \
                 -string=@S35DCCTLabel,p=0.95,q=0.95,j=r \
                 $root.fft -col=f,FFTWaveforml \
                 -split=pages -sep -same & \
             } result ] {
        return -code error "PlotBunchMotion: $result"
    }
    return
}

proc DisableButtons {args} {
    global inputFrame
    foreach frame {acquire process plot} {
        foreach adc {1 2} {
            APSDisableButton $inputFrame.commandFrame.$frame.$frame$adc.button
        }
    }
}

proc EnableButtons {args} {
    global inputFrame
    foreach frame {acquire process plot} {
        foreach adc {1 2} {
            APSEnableButton $inputFrame.commandFrame.$frame.$frame$adc.button
        }
    }
}


set directory .
set file1 turnDataSum-01.sdds
set file2 turnDataDelta-01.sdds
set status Ready.
set fixedAttenuator1 0
set fixedAttenuator2 0
# a value of 33 dB is the correct value for 6+21 singlets.
set variableAttenuator1 33
set variableAttenuator2 0
APSScrolledStatus .status -parent .userFrame -textVariable status -width 50
MakeInputFrame .input -parent .userFrame
