#!/bin/csh -f

set headname=`echo $1 | awk 'BEGIN{FS="."}{print $1}'`
set fname=$headname.radial
set wfiles="file_cols"
set wfiles1="mem_en"
set wfiles2="num_at"
set count=1
set mem_count=1
set num_cols=0
set outfname=$headname.gnu.rad
set efname=$headname.xyz
set scriptdir=${HOME}/bin/.scripts
set psname=$headname.ps

# help script
if ($1 == -h || $1 == --help) then
    echo "RADIAL2GNU"
    echo " "
    echo "usage:"
    echo "        $ radial2gnu {file} ({arrows}/{time}) ({arrows}/{time})" 
    echo " "
    echo "{file}   = {file}.radial. Folder must also contain {file}.xyz"
    echo "           both of format outputted from EPOC"
    echo "{time}   = optional. If present it will animate with this time delay"
    echo "           -1: use return to proceed; any +ve "\#"=delay in seconds"
    echo "           between frames"
    echo "{arrows} = optional: if it isn't present no arrows, otherwise:"
    echo "           arrows: use arrows."
    echo "           the arrows are located at 1.122           (blue)" 
    echo "                                     sqrt(2)*1.122   (blue)"
    echo "                                     sqrt(8/3)*1.122 (green)"       
    echo "                                     sqrt(3)*1.122   (pink)" 
    echo "           corresponding to the peaks in the radial distribution"
    echo "           function of a Lennard-Jones solid with sigma=1.0"
    echo " "
    echo "After showing the files as animations, then the whole are plotted as"
    echo ".ps files for ease of future viewing. "
    echo " "
    echo " written by Luke Abaham 2005/09/12"
    exit 1
endif
    
# get filename
if ($#argv == 1) then
set t_int=2.0
set do_arrows="yes"
set do_animate="no"
else if ($#argv >= 2 && $#argv <= 3) then
    if ($#argv == 2) then
	if ($2 == "arrows") then 
	    set do_arrows="yes"
	    set do_animate="no"
	else
	    set do_arrows="no"
	    set t_int=$2
	    set do_animate="yes"
	endif
    endif
    if ($#argv == 3) then
	if ($2 == "arrows") then 
	    set do_arrows="yes"
	    set t_int=$3
	    set do_animate="yes"
	else if ($3 == "arrows") then 
	    set do_arrows="yes"
	    set t_int=$2
	    set do_animate="yes"
	else
	    echo "error - see -h or --help for useage"
	    exit -1
	endif
    endif
else if ($#argv < 1 || $#argv > 4) then
echo "radial2gnu only require 2 arguments, file sting and time interval"
echo "if there is a third argument: 'arrows'; then arrows will"
echo "be added to the gnuplot graph at the FCC and HCP peak" 
echo "positions, a fourth, 'animate' and the graphs will animate. If there is"
echo "no third or fourth argument then arrows will be used, and it will"
echo "animate"
echo ""
echo "please use -h or --help options"
exit -1 
endif

 awk -f $scriptdir/$wfiles.awk $fname > /tmp/$wfiles.csh

 source /tmp/$wfiles.csh
 rm -f /tmp/$wfiles.csh

 awk -f $scriptdir/$wfiles1.awk $efname > /tmp/$wfiles1.csh
 source /tmp/$wfiles1.csh
 rm -f /tmp/$wfiles1.csh

 awk -f $scriptdir/$wfiles2.awk $efname > /tmp/$wfiles2.csh
 source /tmp/$wfiles2.csh
 rm -f /tmp/$wfiles2.csh

 rm -f $outfname
 touch $outfname

 # write simple gnuplot script
 echo \#\!"/usr/bin/gnuplot -persist" >> $outfname
 echo " " >> $outfname
 echo "set xlabel "\""r_ij"\" >> $outfname
 echo "set ylabel "\""g(r)"\" >> $outfname
 if ($do_arrows == "yes") then
    echo "set arrow from 1.122,-2 to 1.122,0 lt 3" >> $outfname
    echo "set arrow from sqrt(2.0)*1.122,-2 to sqrt(2.0)*1.122,0 lt 3" >> $outfname 
    echo "set arrow from sqrt(8.0/3.0)*1.122,-2 to sqrt(8.0/3.0)*1.122,0 lt 2" >> $outfname 
    echo "set arrow from sqrt(3.0)*1.122,-2 to sqrt(3.0)*1.122,0 lt 4" >> $outfname
 endif
 if ($do_animate == "yes") then
    while ($count < $num_cols)
	@ count = $count + 1
	echo "plot "\"$fname\"" u 1:$count t "\""Member "$mem_count", Energy="$mem_en[${mem_count}]", "\#" Atoms="$num_at[${mem_count}]\"" w l"  >> $outfname
	echo "pause "$t_int >> $outfname
	@ mem_count = $mem_count + 1 
    end
 endif

 # get .ps file
 echo "set term postscript color solid" >> $outfname
 echo "set output '"$psname"'" >> $outfname
 set count=1
 set mem_count=1
 while ($count < $num_cols)
 @ count = $count + 1
 echo "plot "\"$fname\"" u 1:$count t "\""Member "$mem_count", Energy="$mem_en[${mem_count}]", "\#" Atoms="$num_at[${mem_count}]\"" w l"  >> $outfname
 @ mem_count = $mem_count + 1 
 end

 # run gnuplot - first show graphs in animation, then makes eps files
 gnuplot $outfname