#!/bin/bash
# Syntax: raw2rinex input output [teqcoptions]
# Description: GNSS raw data files convertion to daily RINEX 2.11
# Arguments:
#	      input	Directory or filename of raw data (Leica, Trimble, Rinex)
#              compressed of not
#	     output Either output directory to write rinex (must exist), or any
#              output rinex filename
#	teqcoptions	Any options to add to teqc (example: -O.dec 30 -O.rt "receiver"
#              -O.at "antenna")
#
# Dependencies:
#            teqc for all data preprocessing, binary available at unavco
#                 https://www.unavco.org/software/data-processing/teqc/teqc.html
#          gunzip with .gz files
#           unzip with .zip files
#         CRX2RNX with Hatanaka files
#    	   runpkr00 with some Trimble .T02 files
# cnvrnx3-rnx2-v3 with Rinex v.3 files, source available at
#                 http://acc.igs.org/misc/cnvrnx3-rnx2-v3.f
#
#
# Author: F. Beauducel / DOMERAPI <beauducel@ipgp.fr>
# Created: 2014-01-12
# Updated: 2020-08-10

if [ $# -lt 2 ]; then
	echo "raw2rinex input output [teqc options]"
	exit 0;
fi

tmpdir=$(mktemp -d /tmp/raw2rinex.XXXXXXXXXX)
cd $tmpdir

rawdir=$1
if [ -d "$rawdir" ]; then
	files=$(find $rawdir -type f)
else
	files=$(ls $rawdir 2>/dev/null)
fi

#echo "** files = $files"

outdir=$2

if [ "$#" -ge 3 ]
then
	shift
	shift
	teqcopt=$@
fi

# --- copy and uncompress files in a temporary directory
for f in $files
do
	# unzip (-o option is mandatory to avoid prompting in some circumstances !)
	if [[ $f == *".zip" ]]; then
		unzip -oq $f -d $tmpdir
	else
		cp -L $f $tmpdir/
	fi

	# gunzip (must copy first before unzip)
	if [[ $f == *".gz" ]]; then
		gunzip -f $tmpdir/*.gz
	fi
done

# --- try rinex files
wc="*.??o"
rf=$(find $tmpdir -name "$wc" | sort )
if [ ! -z "$rf" ]; then
	echo "   Found some Rinex files in $rawdir: proceeding..."
	# must convert Rinex 3 files
	for f in $rf; do
		if [[ $(head -n1 $f | awk '{print $1}') == '3.01' ]]; then
			cnvrnx3-rnx2-v3 $f +0 g
			if [ -s $f.gps.rnx2 ]; then
				# must remove these specific header lines for teqc...
				grep -v "PHASE SHIFTS" $f.gps.rnx2 > $f
			fi
		fi
	done
	wildcard=$wc
	rawfiles=$rf
	navfiles=$(find $tmpdir -name "*.??n" | sort )
	teqcfmt=""
fi

# --- try Hatanaka files
rf=$(find $tmpdir -name "*.??d.Z" -o -name "*.??d" | sort )
if [ ! -z "$rf" ]; then
	echo "   Found some Hatanaka files in $rawdir: proceeding..."
	# must convert ??d to ??o
	for f in $rf; do
		if [[ $f == *".Z" ]]; then
			gunzip -f $f
			f=${f%.*}
		fi
		CRX2RNX $f -f
	done
	wildcard="*.??o"
	rawfiles=$(find $tmpdir -name "$wildcard" | sort )
	navfiles=""
	teqcfmt=""
fi
# try Leica mdb...
wc="*.m??"
rf=$(find $tmpdir -name "$wc" | sort )
if [ ! -z "$rf" ]; then
	echo "   Found some Leica files in $rawdir: proceeding..."
	wildcard=$wc
	rawfiles=$rf
	navfiles="$rawfiles"
	teqcfmt="-lei mdb"
fi

# try Trimble T02...
wc="*.T02"
rf=$(find $tmpdir -name "$wc" | sort )
if [ ! -z "$rf" ]; then
	echo "   Found some Trimble files in $rawdir: proceeding..."
	# must convert T02 to dat
	for f in $rf; do
		runpkr00 -d $f
	done
	wildcard="*.dat"
	rawfiles=$(find $tmpdir -name "$wildcard" | sort )
	navfiles="$rawfiles"
	teqcfmt="-tr d"
fi

if [ -z "$rawfiles" ]; then
	echo "No valid GNSS file found in $1... abort."
	exit
fi


tmprnx=$tmpdir/rinex
tmpnav=$tmpdir/nav

# --- converts rawfiles to single daily Rinex
if [ ! -z $VERBOSE ]; then
	echo "teqc $teqcfmt $teqcopt $tmpdir/$wildcard > $tmprnx"
fi
teqc $teqcfmt $teqcopt $tmpdir/$wildcard > $tmprnx

# case A: output directory specified => archiving rinex with proper names (obs and nav)
if [ -d $outdir ]; then
	# gets the station code from the filename
	for f in $rawfiles; do
		#echo $f
		mdbfile=$(basename $f)
		code=${mdbfile:0:4}
		rinex=$outdir/${mdbfile:0:8}
	done
	code=$(echo $code | tr '[:lower:]' '[:upper:]')
	starttime=$(teqc +meta $tmprnx | grep "start date" | sed -e 's/.*: //' | sed -e 's/[ \t]*//')
	yy=${starttime:2:2}
	echo "   cp -f $tmprnx ${rinex}.${yy}o"
	cp -f $tmprnx ${rinex}.${yy}o
	if [ ! -z "$navfiles" ]; then
		teqc ${teqcfmt}n $teqcopt $navfiles > $tmpnav
		if [ ! -z $VERBOSE ]; then
			echo "   cp -f $tmpnav ${rinex}.${yy}n"
		fi
		cp -f $tmpnav ${rinex}.${yy}n
	fi
# case B: output file specified => write the rinex using given filename
else
	if [ ! -z $VERBOSE ]; then
		echo "   cp -f $tmprnx $outdir"
	fi
	cp -f $tmprnx $outdir
fi

rm -rf $tmpdir
