#!/bin/sh

# tmp files
TMP1="file_${RANDOM}.tmp"
TMP2="file_${RANDOM}.tmp"
#
# display script usage
#
function usage() {
	echo
	echo "Usage: "
	echo "    $0 <output file> <BIT input file> <start step> <end step>"
	echo
	echo "where:"
	echo "    output file    = file name for storing results of approximation, e.g. results01.txt"
	echo "    BIT input file = name of input file, used to run simulation with BIT1"
	echo "    start step     = step number from which to start approximation of results, e.g. 10" 
	echo "    end step       = step number at which to end approximation of results, e.g. 100"
	echo 
	echo "File names for step results are as follows: <input file>-999999-block1.dat, with 999999 is replaced with"
	echo "step number. Length of step number is 6, with pending 0 (step 123 is 000123)."
	echo "If any file between start and end files is missing, it is skipped and warning is displayed."
	echo
	exit 1
}


# input parameters: results output file name, BIT input file name, start step of results, end step of results 

if [ "$1" == "-?" ] || [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
	# no parameters given; display usage
	usage
fi

if [ -z $4 ]; then
	# no parameters given; display usage
	echo "Parameters missing! See usage below."
	usage
fi

# get parameters
OUTFILE=$1
INFILE=$2
STEP_START=$3
STEP_END=$4

# display parameters
echo "Outfile: $OUTFILE; infile: $INFILE"
echo "Joining block from $STEP_START to $STEP_END"
echo


#
# first join all files into one BIG one..
#
NUM_FILES=0

echo -n "Joining files..."


for i in `seq $STEP_START $STEP_END`; do
	NUM=`printf "%06d" $i`
	FILE=`printf "%s-%s-block1.dat" $INFILE $NUM`
	
#	echo $NUM, $FILE

	if [ -r $FILE ]; then
		# file exists; join the file....
		echo -n " $i"
		
		if [ $NUM_FILES == 0 ]; then
			# first file; just copy to temp file 2
			cp $FILE ${TMP2}	
		else 
			# join current file with temp file 1 and then rename file to temp file 2
			join ${TMP2} $FILE > ${TMP1}
			mv ${TMP1} ${TMP2}
		fi
		NUM_FILES=$(($NUM_FILES+1))
	fi
done

echo " Done! $NUM_FILES file(s) joined."

# delete first line in result file
cat ${TMP2} | sed "1,1d" > ${TMP1}

echo -n "Averaging..."
#
# then make approximation of values....
#
# first write header line in which values in columns are explained
echo "# cell_no n_sp1 T_sp1 n_sp2 T_sp2 pot V_x" > $OUTFILE  

awk -v MAX=$NUM_FILES '{ n=MAX ; a = 0; b = 0; c = 0; d = 0; e = 0; f = 0; for (i = 0; i < n; i++) {
		a += $(3+i*12); b += $(8+i*12); f += $(2+i*12);
		c += $(4+i*12); d += $(9+i*12); e += $(12+i*12);
 	};
 	print $1 " " a/n " " c/n " " b/n " " d/n " " e/n " " f/n;
  }' ${TMP1} >> $OUTFILE

echo "Done!"
echo
echo "Averaged values are stored in file: $OUTFILE" 
echo

# delete template files...
rm -f ${TMP1} ${TMP2}

# just for checking if averaging was done OK
echo 
echo "Copy following line into shell to check if averaging was done OK. Density is displayed from averaged file and first and last file."
echo

NUM=`printf "%06d" $STEP_START`
FILE1=`printf "%s-%s-block1.dat" $INFILE $NUM`
NUM=`printf "%06d" $STEP_END`
FILE2=`printf "%s-%s-block1.dat" $INFILE $NUM`
echo "echo \"plot '$OUTFILE' using 1:2 w l, '${FILE1}' using 1:3 w l, '${FILE2}' using 1:3 w l\" | gnuplot -persist" 
echo

