#!/bin/sh
# The next line restarts using tclsh since sh ignores the backslash \
exec oagtclsh "$0" "$@"

#
# Tcl script to plot adt files
#

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 $

# **********************************************************************
# **************************** main ************************************
# **********************************************************************

# Initialize
set tmp /tmp/plotadtsnap[pid]

# Usage
set usage {
Plots data in snapshots made from ADT.
    Usage: plotadtfiles <files>
}
# Print usage
if {!$argc} {
    puts $usage
    exit 1
}
# Parse arguments
set files {}
for {set i 0} {$i < $argc} {incr i} {
    switch -exact -- [lindex $argv $i] {
	default {
	    set files [linsert $files end [lindex $argv $i]]
	}
    }	
}
# Check
set nfiles [llength $files] 
if {$nfiles == 0} {
    puts "No files were specified"
    exit 1
}
# Process files
set procfiles {}
for {set i 0} {$i < $nfiles} {incr i} {
    set file [lindex $files $i]
    set procfile $tmp.file$i
    # Check if Heading is included (is not with old ADT)
    set params [APSGetSDDSNames -fileName $file -class parameter]
    if {[lsearch -exact $params Heading] != -1} {
	exec sddsprocess $file $procfile "-rpnexp=0 sto index,repeat" \
	  "-define=col,Index,index 1 + sto index" \
	  -scan=col,Value,ValueString,%lg
    } else {
	exec sddsprocess $file $procfile "-rpnexp=0 sto index,repeat" \
	  "-define=col,Index,index 1 + sto index" \
	  -scan=col,Value,ValueString,%lg \
	  -print=parameter,Heading,Value
    }
    set procfiles [linsert $procfiles end $procfile]
}
# Plot
eval exec sddsplot -graph=sym,connect -toptitle -split=page \
  -groupby=page,fileindex -sep=page -graph=line,vary -leg=param=TimeStamp \
  -xlabel=Index -ylabel=@Heading -column=Index,Value \
  [join $procfiles " "] -end

# Clean up
foreach file [glob -nocomplain ${tmp}*] {
    exec rm $file
}

exit 0

# **************************** Emacs Editing Sequences *****************
# Local Variables:
# mode: tcl
# End:
