#!/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  2005/06/13 18:12:54  shang
# replaced APScavget by cavget since APScavget got "segmentation error"
#
# Revision 1.7  2005/04/14 19:55:35  shang
# corrected the tune calculations
#
# Revision 1.6  2005/04/14 19:44:27  shang
# added calculation of tune and write it to output file
#
# Revision 1.5  2005/03/04 21:17:38  shang
# changed the file format to end with 4 digits
#
# Revision 1.4  2005/03/04 21:06:38  shang
# added format with 2 digits for the filename
#
# Revision 1.3  2005/03/04 19:25:42  shang
# fixed a typo
#
# Revision 1.2  2005/02/22 17:20:34  shang
# updated usage message
#
# Revision 1.1  2005/02/18 19:00:34  shang
# take the par vsa data and insert the values of par monitor pvs as parameter
# in the output file, first version
#
# take the par vsa data and insert the values of the monitor pvs as parameter
# in the output file.
# note you should setup the instrument in advance before you use this script to take data.

set args $argv
set usage "getParVsaTrace -rootname <string> -index <number> \[-outputDir <string>\] \[-monitorFile <filename>\]"
set index 0
set rootname ""
set outputDir ./
set monitorFile /home/helios/oagData/par/vsaTuneArchive/parVsaTrace.mon
APSStrictParseArguments {index rootname outputDir monitorFile}

if ![llength $argv] {
    puts stderr $usage
    exit
}
if ![string length $rootname] {
    puts stderr "getVSAtrace: rootname is not given!\n$usage"
    exit
}
if ![file exist $outputDir] {
    puts stderr "getVSAtrace: directory $outputDir does not exist!"
    exit
}
if ![file exist $monitorFile] {
    puts stderr "getVSAtrace: monitor file $monitorFile does not exist!"
    exit
}

if [catch {exec sdds2stream $monitorFile -col=ControlName} pvList] {
    puts stderr "getVSAtrace: $pvList"
    exit
}

if [catch {exec cavget -list=[join $pvList ,] -pend=30} valueList] {
    puts stderr "getVSAtrace: $valueList"
    exit
}

set filename $outputDir/${rootname}[format %04d ${index}]
APSAddToTmpFileList -ID 1 -fileList $filename.1
if [catch {exec hpVecTrace -unit=boo a $filename.1} result] {
    puts stderr "getVSAtrace: $result"
    exit
}
set options ""
foreach pv $pvList val $valueList {
    if [catch {expr $val*2} result] {
        append options " \"-reprint=par,$pv,[APSMakeSafeQualifierStringForEval $val]\""
    } else {
        append options " -redefine=par,$pv,$val"
    }
}
if [catch {eval exec sddsprocess $filename.1 $options -pipe=out \
             | sddsprocess -pipe \
             \"-redefine=col,tune1,Frequency A014-IETS:BTC:BInjSetFreqM 36 / 1.0e6 / /\" \
             | sddsprocess -pipe \"-redefine=col,tune, tune1 tune1 int -\" \
             | sddsconvert -pipe=in $filename -delete=col,tune1 } result] {
    puts stderr "getVSAtrace: $result"
}
exit
