#!/bin/sh
#
# wfampls - export and plot waveform amplitudes for normalized cycles in runs
#
# Usage:  wfampls [-c wfnum] [-w wfnum,...] runfile ...
#
# where:  -c wfnum	specifies the waveform number for the marked up cycles
#			(default is 0)
#	  -w wfnum,...	specifies the list of waveform numbers to export
#			(default is all waveforms - taken from first runfile)
#	  runfile	specifies one or more run file names to be measured
#			(no default, runfile must be specified)
#
# Output will be of the form:
# time, voltage, voltage, ...
# where time is a number between 0 and 1, for the position in a normalized
# cycle, and voltage values (in mV by default) are given for all selected
# waveforms.  There will be multiple groups of these, separated by blank lines,
# for each cycle in a given run.  In the case where multiple waveforms are
# included and they have different sampling rate divisors, only the samples that
# all line up at the same time value appear in the output, so the output will
# be at the base sampling rate divided by the least common multiple of all the
# divisors of the included waveforms.
#
# A comma-separated text file of that form is generated for each runfile
# specified.  Also, a plot of the averaged waveform amplitude is produced for
# each exported waveform in each runfile.
#
# Copyright (c) 2011, Gilles Detillieux, Spinal Cord Research Centre,
# University of Manitoba.  All Rights Reserved.
#

cwfnum=0
wfnums=
T=/tmp/wfa$$
while :
do
	case "$1" in
	-\?|-help|--help)	sed -n '3,/^# Univ/s/^#/ /p' "$0"; exit ;;
	-debug|--debug)	shift; T=wfa.out. ;;
	-c)	shift; cwfnum="$1"; shift ;;
	-w)	shift; wfnums="$1"; shift ;;
	-*)	set --; break ;;
	*)	break ;;
	esac
done
ascript=${T}a
diags=${T}b
anafile=${T}c
csvfile1=${T}d
csvfile2=${T}e

case "$#" in
0)	echo "Usage:  wfampls [-c wfnum] [-w wfnum,...] runfile ...
	or wfampls --help	for detailed usage information" >&2; exit 1 ;;
esac

case "$cwfnum" in
[0-9]*)	;;
*)	echo "$0: Invalid number for cycle waveform (with marked bursts): -c $cwfnum" >&2
	exit 1
	;;
esac

case "${T}" in
/tmp/*)	trap "rm -f ${T}[a-e]" 0		# cleanup on exit
	trap "exit 1" 1 2 3 15			# exit, & cleanup, if killed
	;;
esac

IFS="$IFS,"
for run
do
    results=
    presults=
    case "$wfnums" in
    ""|all)	wfnums=`ls $run.w?? 2>/dev/null | sed 's/^.*\.w//; s/^0//'` ;;
    esac
    for wf in $wfnums
    do
	pwf="$wf"
	case "$pwf" in [0-9])	pwf="0$pwf" ;; esac
	pfname="$run-wfa-w$pwf.plt"
	rm -f "$pfname"
	cat <<! > $ascript
agwva scw$cwfnum
qgny r0
c1
qlwn$wf
rlmin
umax
qqqdsay qtay sy qqq gpd2
m3
f$pfname
y sdnWFAMPL: x, y\\n
qq agwvr b
qyn
!
	echo "Analyzing $run W.F. $wf" >&2
	if TERM=dumb DISPLAY= analysis "$run" < $ascript > $anafile 2> $diags
	then
		[ -s "$pfname" ] && presults="$presults $pfname"
		sed -n 's/^.*WFAMPL: *//p;s/^ *$//p' $anafile | uniq |
			sed '1 {/^$/d}; $ {/^$/d}' > $csvfile2
		if [ -s "$csvfile2" ]
		then
		    if [ -e "$csvfile1" -a -s "$csvfile1" ]
		    then
			join -j 1 -t , $csvfile1 $csvfile2 > $anafile
			cat $anafile > $csvfile1
		    else
			mv $csvfile2 $csvfile1
		    fi
		fi
	else
		sed -n -e 's/FATAL ERROR: */analysis: /p' $diags >&2
		echo "$0: analysis failed on $run!" >&2
		echo
	fi
    done

    if [ -s "$csvfile1" ]
    then
	    cat $csvfile1 > "$run-wfa.txt"
	    echo "Raw exported data from W.F.s $wfnums in: $run-wfa.txt" >&2
    fi

    [ -n "$presults" ] && echo "Averaged data plots in:$presults" >&2
done
