#!/bin/bash
###############################################################################
# $Id: gds-csv-to-rigol,v 1.12 2010/09/26 21:53:58 tw Exp $
#
# Script to invoke conversion to Rigol DG function generator data.
#
# Copyright (C) 2009, 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"

#
# dg1000:    ch1 14 bit, 4kpts, 5 MHz
# dg1000ch2: ch2 10 bit, 1kpts, 5 MHz
# dg2000:    14 bit, 512kpts, 12 MHz
# dg3000:    14 bit, 1Mpts, 25 MHz
# dg5000:    14 bit, 128Mpts, 100 MHz, 250 MHz, 350 MHz
#
# TODO: Do the silently introduced DG2000A and DG3000A series have other
#	parameters?
#
# TODO: Is the mystical DG5000 series really available? Can't even get
#	a manual or datasheet. And Rigol "support", is as "helpful" as ever ...
#
devNames=("dg1000" "dg1000ch2" "dg2000" "dg3000" "dg5000")
formatName=("txt" "csv" "rdf", "scpi")

#
# Defaults
#
defDevType=0			# default device type, DG1000
defDevName=${devNames[$defDevType]}
defRes=14			# default bit resolution
defDC=0				# don't adjust DC
defMinimize=1			# don't generate maximum nbr of samples
defVerbose=0			# don't be verbose
defFormat=0			# format txt

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

#
# Current value
#
devType=$defDevType		# device type
res=0				# bit resolution
dc=$defDC			# DC adjustment
minimize=$defMinimize		# minimize 
verbose=$defVerbose		# verbose
format=$defFormat		# format
out="/dev/stdout"
fflag=0

usage() {
cat >&2 <<!
Convert GDS oscilloscope waveform data to Rigol UltraWave DG format
usage: $MyName [-t (dg1000|dg1000ch2|dg2000|dg3000|dg5000)] [-r <res>] [-d] \
[-f (txt|csv|rdf|scpi)] [-l <locale>] [-m] [-o <out.(txt|csv|rdf|scpi|...)>] \
[-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. "txt" for Rigol DG text file format. "csv" for
	Rigol DG CSV file format. "rdf" for Rigol DG raw data format.
	"scpi" for Rigol DG SCPI commands.
	If not given, but -o is given, format is taken from -o file extension.
	Default ${formatName[$defFormat]}.
-h	This help information.
-l <locale>
	Locale to be used for the floating point number format in the output
	for the txt and csv format.
-m	Generate output data containing the maximum number of samples
	for the particular type of generator.
-o <out.(txt|csv|rdf|scpi|...)>
	Output file with Rigol UltraWave DG 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.
-r <res>
	Bit resolution. 8 ... 32. Default $defRes. The resolution is
	advisory only.
-t <type>
	Function generator type. Either dg1000, dg1000ch2, dg2000, dg3000, or
	dg5000. 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:hl:mr:o:t:v-" o
do
	case $o in
	d)	dc=1 ;; 
	f)
		case "$OPTARG" in
		txt|TXT)	format=0 ;;
		csv|CSV)	format=1 ;;
		rdf|rdf)	format=2 ;;
		scpi|SCPI|data|DATA|data:dac|DATA:DAC)
				format=3 ;;
		*)		usage; exit 1 ;;
		esac
		fflag=1
		;;
	h)	usage; exit 0 ;;
	l)	export LC_ALL="$OPTARG"
		;;
	m)	minimize=0
		;;
	o)	out="$OPTARG"
		if [ $fflag -eq 0 ] ; then
			case "$out" in
			*.txt|*.TXT)	format=0 ;;
			*.csv|*.CSV)	format=1 ;;
			*.rdf|*.rdf)	format=2 ;;
			*.scpi|*.SCPI|*.data|*.DATA|*data:dac|*DATA:DAC)
					format=3 ;;
			esac
		fi
		;;
	r)
		res="$OPTARG"
		if [[ ( "$res" != +([0-9]) ) || ( "$res" -lt 8 ) || ( "$res" -gt 32 ) ]]
		then
			usage
			exit 1
		fi
		;;
	t)
		case "$OPTARG" in
		dg1000|DG1000)	devType=0 ;;
		dg1000ch2|DG1000CH2)
				devType=1
				devRes=10
				;;
		dg2000|DG2000)	devType=2 ;;
		dg3000|DG3000)	devType=3 ;;
		dg5000|DG5000)	devType=4 ;;
		*)		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"		\
	"-vRES=$res"		\
	"-vFORMAT=$format"	\
	"$in" > "$out"
