#!/bin/sh  
# \
exec oagtclsh "$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)]
APSStandardSetup

# $Log: not supported by cvs2svn $
# Revision 1.8  2008/06/25 19:30:33  shang
# added command for acquiring data (set SpMgAM:1:runBO to 1) for booter and par after switching the trigger. This should be the reason why the waveform was not being updated, so changed the waiting time from 10seconds to 5 seconds.
#
# Revision 1.7  2008/06/16 22:00:08  shang
# changed the wait time from .5 seconds to 10 seconds after the trigger is changed, to wait for the waveform being completely updated; and added setting the trigger level to 0.5 volts.
#
# Revision 1.6  2007/09/11 13:53:16  shang
# replaced tekTDS by SpMgAM due to the pv name's change
#
# Revision 1.5  2006/01/06 18:01:48  shang
# disabled lock for taking snapshot data
#
# Revision 1.4  2005/12/07 19:31:14  shang
# modified according to hardwar changes and added snapshot option to
# only take snapshot data without seting triggers or checking conditions.
#
# Revision 1.3  2005/08/09 19:42:40  shang
# modified to take 6 waveforms, 4 for SR and 2 for booster.
#
# Revision 1.2  2005/02/21 23:05:15  shang
# added post-processing after take is taken
#
# Revision 1.1  2005/02/21 19:48:05  shang
# first version, for taking the sr and booster septum scope waveform
#
set args $argv
set snapshot 0
set system ""
APSStrictParseArguments {snapshot system}

set boosterArchiveDir /home/helios/oagData/booster/scope/archiveData/septumAcousticSignal
set srArchiveDir /home/helios/oagData/sr/scope/archiveData/septumAcousticSignal

set lockFile TakeScopeWaveformData.lock

proc establishLock {lockFile} {
    if [file exists $lockFile] {
        if [catch {open $lockFile a} fid] {
            puts stderr "$fid"
            exit 1
        }
    } else {
        if [catch {open $lockFile w} fid] {
            puts stderr "$fid" 
            exit 1
        }
        if [catch {os lockf $fid F_LOCK 0} result] {
            puts stderr "$result"
            exit 1
        }
        puts $fid "lock file started [clock format [clock seconds]]"
        flush $fid
        # this is necessary to force NFS to create the file with the
        # name given
        close $fid
        if [catch {open $lockFile a+} fid] {
            puts stderr $fid
            exit 1
        }
        if [catch {os lockf $fid F_LOCK 0} result] {
            puts stderr $result
            exit 1
        }
    }
    return $fid
}

proc releaseLock {fid} {
    close $fid
}

set takedata 0
set pvList {Mt:TopUpAutoEnableC.VAL Mt:TopUpTime2Inject.VAL Mt:Ddg1chan5.GATE}
if !$snapshot {
  set fid [establishLock $lockFile]
  dp_atexit append "releaseLock $fid"
}
#take booster and par data anyway
set tmpRoot /tmp/[APSTmpString]
# APSAddToTmpFileList -ID 1 -fileList "$tmpRoot.1 $tmpRoot.2"
catch {exec timeconvert -now} timeList 
set filename [format %4ld-%03ld-%02ld%02ld-%02ld%02ld%02ld \
                [lindex $timeList 0] [lindex $timeList 1] \
                [lindex $timeList 2] [lindex $timeList 3] \
                [lindex $timeList 5] [lindex $timeList 6] \
                [lindex $timeList 7] ]
if $snapshot {
    set filename Snapshot_$system$filename
    switch $system  {
        par {
            set timePV SpMgAM:1:scaledTimeAxisWF 
            set signalPV SpMgAM:1:chan1ScaledWaveWF
            set dir $boosterArchiveDir
        }
        boo_inj {
            set timePV SpMgAM:1:scaledTimeAxisWF
            set signalPV SpMgAM:1:chan2ScaledWaveWF
            set dir $boosterArchiveDir
        } 
        boo_ext_thin {
            set timePV SpMgAM:2:scaledTimeAxisWF
            set signalPV SpMgAM:2:chan1ScaledWaveWF
            set dir $srArchiveDir
        }
        boo_ext_thick {
            set timePV SpMgAM:2:scaledTimeAxisWF
            set signalPV SpMgAM:2:chan2ScaledWaveWF
            set dir $srArchiveDir
        }
        sr_thick {
            set timePV SpMgAM:2:scaledTimeAxisWF
            set signalPV SpMgAM:2:chan3ScaledWaveWF
            set dir $srArchiveDir
        }
        sr_thin {
            set timePV SpMgAM:2:scaledTimeAxisWF
            set signalPV SpMgAM:2:chan4ScaledWaveWF
            set dir $srArchiveDir
        }
        default {
            put stderr "Invalid system - $sytem given."
            exit
        }
    }
    if [catch {exec sddswmonitor $tmpRoot.$system \
                 -PVnames=$timePV,$signalPV \
                 -interval=1 -steps=1 } result] {
        puts stderr $result
        exit
    }
    if [catch {exec sddsprocess $tmpRoot.$system $dir/$system/$filename \
                 "-define=col,V,$signalPV 0.5 *,units=V" \
                 "-define=col,t,$timePV 1e-6 *,units=s" 
        exec gzip $dir/$system/$filename } result] {
        puts stderr $result
        exit
    }
} else {
    #channel 1 (par), par trigger is on channel 3 (numerical value is 2)
    #channel 2 (booster signal), booster trigger is on channel 4 (numerical value is 3)
    #booster
    #increase the trigger level to 0.5 volts (0.1 volts -- noise)
    if [catch {exec cavput -list=SpMgAM:1:singleSeqBO=1} result] {
        puts stderr $result
        exit
    }
    if [catch {exec cavput -list=SpMgAM:1:trigLevAO=0.5} result] {
        puts stderr $result
        exit
    }
    if [catch {exec cavput -list=SpMgAM:1:trigSourMO=3 -pend=30} result] {
        puts stderr $result
        exit
    }
    if [catch {exec cavput -list=SpMgAM:1:runBO=1 -pend=30} result] {
        puts stderr $result
        exit
    }
    after 10000
    if [catch {exec sddswmonitor $tmpRoot.booster \
                -PVnames=SpMgAM:1:scaledTimeAxisWF,SpMgAM:1:chan2ScaledWaveWF \
                -interval=1 -steps=1 } result] {
            puts stderr $result
        exit
    }
    #par
    if [catch {exec cavput -list=SpMgAM:1:trigSourMO=2 -pend=30} result] {
        puts stderr $result
        exit
    }
    if [catch {exec cavput -list=SpMgAM:1:runBO=1 -pend=30} result] {
        puts stderr $result
        exit
    }
    
    after 10000
    if [catch {exec sddswmonitor $tmpRoot.par \
                 -PVnames=SpMgAM:1:scaledTimeAxisWF,SpMgAM:1:chan1ScaledWaveWF \
                 -interval=1 -steps=1 } result] {
        puts stderr $result
        exit
    }
    set fileList [glob $tmpRoot.*]
    APSAddToTmpFileList -ID 1 -fileList $fileList
    if [catch {exec sddsprocess $tmpRoot.par $boosterArchiveDir/par/par$filename \
                 "-define=col,V,SpMgAM:1:chan1ScaledWaveWF 0.5 *,units=V" \
                 "-define=col,t,SpMgAM:1:scaledTimeAxisWF 1e-6 *,units=s" 
        exec sddsprocess $tmpRoot.booster $boosterArchiveDir/boo_inj/boo_inj$filename \
                 "-define=col,V,SpMgAM:1:chan2ScaledWaveWF 0.5 *,units=V" \
             "-define=col,t,SpMgAM:1:scaledTimeAxisWF 1e-6 *,units=s" 
    } result] {
        puts stderr $result
        exit
    }
    if [catch {exec gzip $boosterArchiveDir/par/par$filename
        exec gzip $boosterArchiveDir/boo_inj/boo_inj$filename } result] {
        puts stderr $result
    }
    
    #take sr data only at topup
    if [catch {APScavget -list=Mt:TopUpAutoEnableC.VAL -pend=30 -numeric} topup] {
        puts stderr "$topup"
        exit
    }
    if {$topup} {
        #wait utill the Mt:TopUpTime2Inject.VAL count down to 20
        while {1} {
            if [catch {APScavget -list=Mt:TopUpTime2Inject.VAL -pend=30} count] {
                puts stderr $count
                exit
            }
            update
            if {$count==20} {
                break
            }
            after 500
        }
        if [catch {exec cavput -list=SpMgAM:2:trigSourMO=EXT,SpMgAM:2:trigLevAO=0.25,SpMgAM:2:trigSlopeMO=RISE -pend=30} result] {
            puts stderr $result
            exit
        }
        if [catch {exec cavput -list=SpMgAM:2:singleSeqBO=SEQUENCE,SpMgAM:2:runBO=1 -pend=30} result] {
            puts stderr $result
            exit
        }
        after 20000
        #wait for the data to be acuqired (stop 0; running 1)
        while {1} {
            if [catch {APScavget -list=SpMgAM:2:acquireStatBI -numerical -pend=30} acquireData] {
                puts stderr $acquireData
                exit
            }
            update
            if {!$acquireData} {
                break
            }
            after 500
        }
        after 20000
        # APSAddToTmpFileList -ID 1 -fileList "$tmpRoot.1 $tmpRoot.2"
        catch {exec timeconvert -now} timeList 
        set filename [format %4ld-%03ld-%02ld%02ld-%02ld%02ld%02ld \
                        [lindex $timeList 0] [lindex $timeList 1] \
                        [lindex $timeList 2] [lindex $timeList 3] \
                        [lindex $timeList 5] [lindex $timeList 6] \
                        [lindex $timeList 7] ]
        
        if [catch {exec sddswmonitor $tmpRoot.boo_ext \
                     -PVnames=SpMgAM:2:scaledTimeAxisWF,SpMgAM:2:chan1ScaledWaveWF \
                     -interval=1 -steps=1 
            exec sddswmonitor $tmpRoot.boo_ext_thick \
                     -PVnames=SpMgAM:2:scaledTimeAxisWF,SpMgAM:2:chan2ScaledWaveWF \
                     -interval=1 -steps=1
            exec sddswmonitor $tmpRoot.sr_thick \
                     -PVnames=SpMgAM:2:scaledTimeAxisWF,SpMgAM:2:chan3ScaledWaveWF \
                     -interval=1 -steps=1
            exec sddswmonitor $tmpRoot.sr_thin \
                     -PVnames=SpMgAM:2:scaledTimeAxisWF,SpMgAM:2:chan4ScaledWaveWF \
                     -interval=1 -steps=1 } result] {
            puts stderr $result
            exit
        }
        set fileList [glob $tmpRoot.*]
        APSAddToTmpFileList -ID 1 -fileList $fileList
        if [catch {exec sddsprocess $tmpRoot.boo_ext $srArchiveDir/boo_ext_thin/boo_ext_thin$filename \
                     "-define=col,V,SpMgAM:2:chan1ScaledWaveWF 0.5 *,units=V" \
                     "-define=col,t,SpMgAM:2:scaledTimeAxisWF 1e-6 *,units=s" 
            exec sddsprocess $tmpRoot.boo_ext_thick $srArchiveDir/boo_ext_thick/boo_ext_thick$filename \
                     "-define=col,V,SpMgAM:2:chan2ScaledWaveWF 0.5 *,units=V" \
                     "-define=col,t,SpMgAM:2:scaledTimeAxisWF 1e-6 *,units=s"
            exec sddsprocess $tmpRoot.sr_thick $srArchiveDir/sr_thick/sr_thick$filename \
                     "-define=col,V,SpMgAM:2:chan3ScaledWaveWF 0.5 *,units=V" \
                     "-define=col,t,SpMgAM:2:scaledTimeAxisWF 1e-6 *,units=s"
            exec sddsprocess $tmpRoot.sr_thin $srArchiveDir/sr_thin/sr_thin$filename \
                     "-define=col,V,SpMgAM:2:chan4ScaledWaveWF 0.5 *,units=V" \
                     "-define=col,t,SpMgAM:2:scaledTimeAxisWF 1e-6 *,units=s" } result] {
            puts stderr $result
            exit
        }
        if [catch {exec gzip $srArchiveDir/boo_ext_thin/boo_ext_thin$filename 
            exec gzip $srArchiveDir/boo_ext_thick/boo_ext_thick$filename 
            exec gzip $srArchiveDir/sr_thick/sr_thick$filename 
            exec gzip $srArchiveDir/sr_thin/sr_thin$filename } result] {
            puts stderr $result
        }
    }
}
exit
