#!/bin/sh
#
# addwfs2run -- add waveform data from other runs to a run file
#
# Usage:  addwfs2run runfile otherrun.wnn ...
#
# where:  runfile	specifies the name of the run file to be modified
#	  otherrun.wnn	specifies one or more waveform files from other runs
#
# The specified waveform files will be copied as new waveforms that are
# added to the runfile.  You should ensure that the effective sampling
# rate of the waveforms evenly divides the sampling rate of the runfile,
# so that the data lines up correctly with existing waveforms.  addwfs2run
# will pick the closest integer sampling rate divisor and use this for
# each added waveform.  You should also use runfiles whose length match
# reasonably well.  If the waveforms you add have a different run length
# than the specified runfile, the shorter waveforms will be padded with
# zeros to the appropriate length.
#
# Copyright (c) 2010, Gilles Detillieux, Spinal Cord Research Centre,
# University of Manitoba.  All Rights Reserved.
#

while :
do
	case "$1" in
	-\?|-help|--help)	sed -n '3,/^# Univ/s/^#/ /p' "$0"; exit ;;
	-*)	set --; break ;;
	*)	break ;;
	esac
done

if [ "$#" -lt 2 ]
then
	echo "Usage:  $0 runfile otherrun.wnn ...
	or  $0 --help  for detailed usage information" >&2
	exit 1
fi

rfile="$1"
shift

case "$rfile" in
*.frm)	rfile=`expr "$rfile" : '\(.*\)\.frm'` ;;
esac

lastwf=`dumprun "$rfile" | sed -n '1,/^WF /d; s/^ //; / uV /s/  .*//p' | tail -1`
case "$lastwf" in
"")	echo "$0: Can't find last waveform in $rfile" >&2
	exit 1
	;;
esac

freq=`dumprun "$rfile" | awk '/^Sampling rate:/ { print $3 }'`
case "$freq" in
"")	echo "$0: Can't find sampling rate of $rfile" >&2
	exit 1
	;;
esac

newwfs=
for wff
do
	runname=`expr "$wff" : '\(.*\)\.w..'`
	if [ ! -r "$runname.frm" ]
	then
		echo "$0: Can't open '$runname.frm'" >&2; continue
	fi
	wfn=`expr "$wff" : '.*\.w0*\([0-9][0-9]*\)'`
	wffreq=`dumprun "$runname" | awk '/^Sampling rate:/{f=$3}; /^WF/{w=1}; w==1 && /^ *'"$wfn"' / {print f/$3}'`
	case "$wffreq" in
	[0-9]*)	;;
	*)	echo "$0: Can't determine effective sampling rate of $wff" >&2
		continue
		;;
	esac
	ptsdiv=`dumprun "$runname" | sed -n '1,/^WF /d; s/^ //; /^'"$wfn"' .* uV /p'`
	wfchan=`echo "$ptsdiv" | awk '{print $4}'`
	wfgain=`echo "$ptsdiv" | awk '{print $5}'`
	wfzero=`echo "$ptsdiv" | awk '{print $6}'`
	wfheight=`echo "$ptsdiv" | awk '{print $7}'`
	wflevel=`echo "$ptsdiv" | awk '{print $8}'`
	wfname=`echo "$ptsdiv" | sed 's/^.* uV *//'`
	lastwf=`expr $lastwf + 1`
	if [ "$lastwf" -gt 99 ]
	then
		echo "$0: Reached last available waveform number before adding $wff" >&2
		exit 1
	fi
	# Make sure we have .rhd file...
	if [ ! -e "$rfile.rhd" ]
	then
		echo "csqyn" |
		  NEEDRHDFILE=y TERM=dumb DISPLAY=none analysis "$rfile" 2>/dev/null
	fi
	if [ ! -w "$rfile.rhd" ]
	then
		echo "$0: Can't append to '$rfile.rhd'" >&2
		exit 1
	fi
	fwf=$lastwf
	if [ "$fwf" -le 9 ]
	then
		fwf="0$fwf"
	fi
	wfdiv=`echo "$freq $wffreq" | awk '{print int($1/$2+0.5)}'`
	echo "Creating $rfile.w$fwf: $wfname..." >&2
	echo "$freq $wfdiv $wffreq" | awk '{
		nf = $1/$2
		if (nf != $3) {
			printf("   Changing effective sampling rate from %g to %g (%g/%d)\n", $3, nf, $1, $2)
		}
	}' >&2
	# Copy waveform and add info to .rhd file...
	cp -p "$wff" "$rfile.w$fwf"
	cat >> "$rfile.rhd" <<!
REGDIV_$lastwf='$wfdiv'
REGCHAN_$lastwf='$wfchan'
REGCALZERO_$lastwf='$wfzero'
REGCALHEIGHT_$lastwf='$wfheight'
REGCALLEVEL_$lastwf='$wflevel'
REGCALGAIN_$lastwf='$wfgain'
REGCALNAME_$lastwf='$wfname'
!
	newwfs="$newwfs $lastwf"
done

case "$newwfs" in
"")	;;
*)	echo "Fixing run header for $rfile to incorporate new waveforms: $newwfs" >&2
	salvagerun "$rfile"
	;;
esac
