#!/bin/sh
#
# proa2run -- convert ProAnalyst export text file to runfile format
#
# Usage:  proa2run [-u units] txtfile [runfile]
#
# where:  -u units	specifies the units in which the X & Y coordinates are
#			calibrated in ProAnalyst (default is mm)
#	  txtfile	specifies the name of the .txt file to be converted
#	  runfile	specifies the output run file name
#			(default is to use the same basename as txtfile)
#
# The specified txtfile will be analysed to find out the sampling rate
# (i.e. frame rate) of the samples, as well as the contents of the columns
# of data.  Angle data will be expanded to two columns, to get both inside and
# outside angles (360 - x), and both will be converted.  Column headings will
# be parsed and extended with alternate unit specifiers so the correct units
# appear in the output in the analysis program.
#
# The txtfile must have at least 3 columns, with the first column containing
# the Index field (ignored), and the second one containing the Time field,
# used to calculate the sampling rate.  The remaining columns can be X, Y and
# Angle fields in any order, and will be converted to waveforms in the runfile.
#
# Copyright (c) 2011-2018, Gilles Detillieux, Spinal Cord Research Centre,
# University of Manitoba.  All Rights Reserved.
#

units=mm
while :
do
	case "$1" in
	-\?|-help|--help)	sed -n '3,/^# Univ/s/^#/ /p' "$0"; exit ;;
	-u[A-Za-z0-9]*)	units=`expr "x$1" : 'x-u\(.*\)'`; shift ;;
	-u)	shift; units="$1"; shift ;;
	-*)	set --; break ;;
	*)	break ;;
	esac
done

if [ "$#" -lt 1 -o "$#" -gt 2 ]
then
	echo "Usage:  $0 [-u units] txtfile [runfile]
	or  $0 --help  for detailed usage information" >&2
	exit 1
fi

ifile="$1"
ofile="${2:-$1}"
case `basename "$ofile"` in
*.txt)		ofile=`expr "$ofile" : '\(.*\)\.txt$'` ;;
*.csv)		ofile=`expr "$ofile" : '\(.*\)\.csv$'` ;;
esac

if [ ! -r "$ifile" ]
then
	echo "$0: Can't read text file: $ifile" >&2
	exit 1
fi

icolumns=`grep '^# *Column' "$ifile" | wc -l`
angles=`grep '^# *Column.*[Aa]ngle' "$ifile" | wc -l`
columns=`expr $icolumns + $angles - 2`
samprate=`grep '^ *-*[0-9][-+e0-9.]*,' "$ifile" | head -2 | awk -F, 'NR == 2 {print int(1 / ($2 - last) + 0.5)}; {last = $2}'`
if [ -z "$samprate" ]
then
	samprate=120
	echo "Can't determine sampling rate from $ifile, assuming $samprate Hz" >&2
fi

echo "Sampling rate: $samprate Hz, Columns: $icolumns input, $columns output ($angles angles)" >&2

awk -F, '
  /^# *Column/ {
	v = $0;
	sub(/^# *Column *[0-9]* *: */, "", v);
	sub(/^2D - /, "", v);
	sub(/ \[FILTERED\]/, "", v);
	gsub(/,/, ";", v);
	if (v ~ /[Aa]ngle/) {
		v = v " (mV=Degrees)";
		column[++ncol] = v;
		sub(/[Aa]ngle/, "Out-angle", v);
	} else {
		v = v " (mV='"$units"')";
	}
	column[++ncol] = v;
  }
  /^ *-*[0-9][-+e0-9.]*,/ {
	if (nhdr < ncol) {
		for (nhdr = 3; nhdr <= ncol; ++nhdr)
			printf("%s%s", column[nhdr], nhdr < ncol ? ", " : "\n")
	}
	icol = 3;
	for (ocol = 3; ocol <= ncol; ++ocol) {
		if (column[ocol] ~ /Out-angle/)
			v = 360 - v;
		else {
			v = $(icol);
			++icol;
		}
		printf("%s%s", v, ocol < ncol ? ", " : "\n")
	}
  }
' "$ifile" |
    asc2run -s0 -c"$columns" -m.0005 -t, -f"$samprate" -umV - "$ofile"
