#!/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)]

# $Log: not supported by cvs2svn $
#originally wrote by CY, modified by Shang


# name of scope and PV prefix
# PV name = prefix + pvRoot
# scope     prefix
# tekdpo1   S:BM:los1 
# tekdpo2   S:BM:curr
# tekdpo5   p0fbk 
# tekdpo6   S:BM:los2

set args $argv
set filename ""
set scopename ""
set channel ""
set waitForTrigger 0
set usage "getScopeTrace -filename <filename> -scopename <scope name> -channel <channel ID> \[-waitForTrigger <0|1>\]"
APSStrictParseArguments {filename scopename channel waitForTrigger}

set scopeDir /net/oxygen/pcfiles/pc/scopeusr_share

if ![string length $filename] {
    puts stderr "the filename is not provided.\nUsage: $usage"
    exit 1
}

if [string length $scopename]==0 {
    puts stderr "The scope name is not provided.\nUsage: $usage"
    exit 1
}

proc SetupScope {args} {
    set scope ""
    set channel ""
    APSParseArguments {scope channel}
    
    if [catch {exec cavput -list=${scope}:chan${channel}EnableBO=1 } result] {
        return -code error"SetupScope(1): Unable to enable the channel: $result"
    }
   
    if [catch {exec cavget -list=${scope}:GPIBenableBO -num } scopeEnabled] {
        return -code error "SetupScope(3): Unable to read ${scope}:GPIBenableBO : $scopeEnabled"
    }
    if $scopeEnabled {
        if [catch {exec cavput -list=${scope}:GPIBenableBO=0} result] {
            return -code error "SetupScope(4): Unable to disable scope GPIB: $result"
        }
    }
    if [catch {exec cavput -list=${scope}:fileFormatMO=0 } result] {
        return -code error "SetupScope(5): Unable to set ${scope}:fileFormatMO : $result"
    }
    #set up for single sequence
    if [catch {exec cavput -list=${scope}:singleSeqBO=1 } result] {
        return -code error "SetupScope(6): unable to set up single sequece: $result" 
    }
    #Arm the trigger
    if [catch {exec cavput -list=${scope}:runBO=1 } result] {
        return -code error "SetupScope(7): unable to arm the trigger: $result"
    }    
}

proc TakeData {args} {
    set filename ""
    set scope ""
    set channel ""
    set timeout 500
    set waitForTrigger 0
    set scopename ""
    APSParseArguments {filename scope channel waitForTrigger scopename}
    
    global scopeDir
    
    if [catch {SetupScope -scope $scope -channel $channel} result] {
        return -code error "TakeData(1) -- unable setup scope $scope: $result"
    }
    if $waitForTrigger {
        set t0 [expr [clock seconds]+$timeout]
        while {[clock seconds]<$t0} {
            if [catch {exec cavget -list=${scope}:acquireStatBI} stat] {
                return -code error "TakeData(2 - error in reading ${scope}:acquireStatBI): $stat"
            }
            # test to make sure the scope is armed. 
            if { $stat == "RUNNING" } {
                break
            }
            after 500
        }
        if [catch {exec cavget -list=${scope}:acquireStatBI} stat] {
            return -code error "TakeData(3 - error in reading ${scope}:acquireStatBI): $stat"
        }
        # test to make sure the scope is armed. 
        if [string compare $stat "RUNNING"]!=0 {
            return -code error "TakeData(4): the scope was not running after triggered $timeout seconds."
        }
        set t0 [expr [clock seconds] + $timeout]
        # test to see whether the scope is triggered.
        while {[clock seconds]<$t0} {
            if [catch {exec cavget -list=${scope}:acquireStatBI } stat] {
                puts stderr "TakeData(5 - error in reading ${scope}:acquireStatBI): $stat"
                exit 1
            }
            if { $stat == "STOP" } {
                break
            }
            after 1000
        }
        
        if [catch {exec cavget -list=${scope}:acquireStatBI} stat] {
            return -code error "TakeData(6 - error in reading ${scope}:acquireStatBI): $stat"
        }
        # test to make sure the scope is armed. 
        if [string compare $stat "STOP"]!=0 {
            return -code error "TakeData(7): the scope did not stop after running for $timeout seconds."
        }
    }
    #take data
    set buffFileRoot ${channel}-[clock format [clock seconds] -format "%Y-%m%d-%H%M%S"]
   # set buffFileRoot ${scopename}-${channel}-read
    if [catch {exec cavput -list=${scope}:WrFileCh${channel}SO=\"H:\\${scopename}\\traces\\$buffFileRoot\" -pend=10} result] {
        return -code error "TakeData(8 - unable to set ${scope}:WrFileCh${channel}SO: $result"
    }
    set filename0 $scopeDir/$scopename/traces/$buffFileRoot.wfm
    set timeout [expr [clock seconds] + 10]
    while {[clock seconds]<$timeout} {
        if [file exist $filename0] {
            break
        }
        after 500
    }
    if ![file exist $filename0] {
        return -code error "$filename0 was not generated!"
    }
    if [catch {exec wfm2sdds ${scopeDir}/$scopename/traces/${buffFileRoot}.wfm $filename -float} result] {
        return -code error "TakeData(9 -- unable to move scope data to SDDS): $result"
    }
   # exec rm $filename0
}


switch $scopename {
    tekdpo1 {
        set scope S:BM:los1 
    }
    tekdpo2 {
        set scope S:BM:curr
    }
    tekdpo5 { 
        set scope p0fbk
    }
    S:BM:los2 {
        set scope S:BM:los2
    } 
    default {
        puts stderr "getScopeTrace(1): wrong scope name: $scopename"
        exit 1
    }
}

if { [catch { set channel [format %1.1d $channel] } error ] } {
    puts stderr "getScopeTrace(2): channel must be integer."
    exit 1
}
if [catch {TakeData -scope $scope -channel $channel -filename $filename -waitForTrigger $waitForTrigger -scopename $scopename} result] {
    puts stderr "getScopeTrace(3): $result"
    exit 1
}
exit 0




