#!/bin/bash
###############################################################################
# $Id: gds-csv-to-afg,v 1.2 2010/09/26 21:53:58 tw Exp $
#
# Script to invoke conversion to GW Instek AFG function generator data.
#
# Copyright (C) 2010 Thomas Weidenfeller
# 
# This file is part of gds2000tools/gds-funcgen.
# 
# gds2000tools/gds-funcgen is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# version 3 as published by the Free Software Foundation.
# 
# gds2000tools/gds-funcgen is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with gds2000tools/gds-funcgen.
# If not, see <http://www.gnu.org/licenses/>.
###############################################################################

shopt -s extglob

MyName=$(basename $0)
MyDir=$(dirname "$0")

libdir="$MyDir/../lib"

awkScript="$libdir/$MyName.awk"
awkLib="$libdir/gds-csv-common.awk"
rcFile="~/.${MyName}rc"

#
# AFG-3051:	50 MHz, 200 MS/s, 16 bit, 1M points
# AFG-3081:	80 MHz, 200 MS/s, 16 bit, 1M points
#
devNames=("afg3051" "afg3081")
formatName=("csv" "scpi")

#
# Defaults
#
defDevType=0			# default device type, AFG-3051
defDevName=${devNames[$defDevType]}
defDC=0				# don't adjust DC
defFormat=0			# format csv
defMinimize=1			# generate minimum nbr of samples
defSteps=8			# datapoints per DATA:DAC line
defVerbose=0			# don't be verbose

[[ -r "$rcFile" ]] && . "$rcFile"

#
# Current value
#
devType=$defDevType		# device type
dc=$defDC			# DC adjustment
minimize=$defMinimize		# minimize 
verbose=$defVerbose		# verbose
format=$defFormat		# format
steps=$defSteps			# nbr of datapoints
out="/dev/stdout"
fflag=0

usage() {
cat >&2 <<!
Convert GDS oscilloscope waveform data to GW Instek AFG format
usage: $MyName \
[-d] \
[-f csv|scpi] \
[-o <out.(csv|scpi|...)>] \
[-s <points-per-line>] \
[-t afg3051|afg3081] \
[-v] \

[--] [<gds.csv>]
       $MyName -h

-d	Adjust DC component of the signal to maximize available amplitude
	range. Sample values are centered around 0 V.
-f <format>
	Output format. "csv" for GW Instek AFG CSV file format.  "scpi"
	for GW Instek AFG SCPI commands.  If not given, but -o is given,
	the format is taken from -o file extension.
	Default ${formatName[$defFormat]}.
-h	This help information.
-o <out.(csv|scpi|...)>
	Output file with GW Instek AFG function generator arbitrary
	waveform data. If -f is not given, the output format is taken
	from the file extension. If -f is given, -f overrides -o.
-s <points-per-line>
	Number of data-points to put on a DATA:DAC output line in the
	SCPI format. Default $defStep.
-t <type>
	Function generator type. Either afg3051 or afg3081.
	Default ${devNames[$defDevType]}.
-v	Verbose.
--	End of options.
<gds.csv>
	Input CSV file with GW Instek GDS oscilloscope waveform data in
	GW Instek oscilloscope data format.
!
}

###############################################################################
# Main
#

while getopts "df:ho:s:t:v-" o
do
	case $o in
	d)	dc=1 ;; 
	f)
		case "$OPTARG" in
		csv|CSV)	format=0 ;;
		scpi|SCPI|data|DATA|data:dac|DATA:DAC)
				format=1 ;;
		*)		usage; exit 1 ;;
		esac
		fflag=1
		;;
	h)	usage; exit 0 ;;
#	m)	minimize=0
#		;;
	o)	out="$OPTARG"
		if [ $fflag -eq 0 ] ; then
			case "$out" in
			*.csv|*.CSV)	format=0 ;;
			*.scpi|*.SCPI|*.data|*.DATA|*data:dac|*DATA:DAC)
					format=1 ;;
			esac
		fi
		;;
	s)
		steps="$OPTARG"
		if [[ ( "$steps" != +([0-9]) ) || ( "$steps" -lt 1 ) || ( "$steps" -gt 1048576) ]]
		then
			usage
			exit 1
		fi
		;;
	t)
		case "$OPTARG" in
		afg3051|AFG3051|afg-3051|AFG-3051)	devType=0 ;;
		afg3081|AFG3081|afg-3081|AFG-3081)	devType=1 ;;
		*)					usage; exit 1 ;;
		esac
		;;
	v)	verbose=1 ;;
	-)	break ;;
	*)	usage; exit 1 ;;
	esac
done
shift $(($OPTIND - 1))
[[ $# -gt 1 ]] && { usage; exit 1; }
in="${1:-/dev/stdin}"
[[  "$in" == "-" ]] && in="/dev/stdin"
[[ "$out" == "-" ]] && out="/dev/stdout"

[[ -r "$in" ]] || { echo "$MyName: No such input file '$in'."; exit 1; }

[[ $res == 0 ]] && res=$devRes

exec gawk -f "$awkLib" -f "$awkScript" 	\
	"-vMY_NAME=$MyName"	\
	"-vMINIMIZE=$minimize"	\
	"-vVERBOSE=$verbose"	\
	"-vDEV_TYPE=$devType"	\
	"-vDC=$dc"		\
	"-vFORMAT=$format"	\
	"-vSTEPS=$steps"	\
	"$in" > "$out"
