#! /bin/sh 
# CVS: $Id: plot_efficiency,v 1.15 2006/01/24 01:13:25 bvk Exp $
# Tag: $name$
# Info: $CVSROOT/Copyright.txt

if [ $# -le 1  ]; then
  echo " "
  echo "plot_efficiency <log-file-name> [plotName] [format] [start-cycle] [end-cycle] [title]"
  echo " "
  echo " <log-file-name>, mandatory, the log file to be used"
  echo " [parse/noparse], noparse  option will save time by using the already parsed file"
  echo " [plotName], name of the plot, if the name 'screen' is given then it goes to the screen.  Plots are written to image files in png format."
  echo " [format], optional, lines is display used by default, nolines is an option"
  echo " [start-cycle], optional, start point for the graph "
  echo " [end-cycle], optional, end point for the graph "
  echo " [title] title of the plot"
  echo "" 
  echo " Example usage: "
  echo " ./plot_efficiency log plot1"
  echo " ./plot_efficiency log plot2 nolines"
  echo " ./plot_efficiency log screen lines 200 700 new_title"
  echo " ./plot_efficiency log myImage nolines 0 1000 Graph_0_1000"
  echo 
  echo " The first time plot_steps is used, the log file will be parsed and plotted."
  echo " Thereafter, the original log file does not have to be parsed thus you may use the "
  echo " log-plot.data file with the noparse option to disable the parsing of the original file."
  echo " When no parse is selected, it only needs to parse the much smaller -plot.data file"
  echo " thus it is quicker."
  echo 
  exit 1
fi

orig_log_file=$1
log_file=$1

if [  ! -e $log_file  ]; then
  echo " "
  echo 'Cant find: '$1
  echo " "
  exit
fi

displayPlot=0;
if [ "$2" == "screen" ]; then
  displayPlot=1;
fi

# default image name
imageName="efficiency.png"
if [ "$2" != "" ]; then
  imageName=$2;
fi

format="lines"
if [ "$3" != "" ]; then
  format=$3
fi

#  end is arbitrarily large; it's assumed 
#  the max ticks won't need to exceed 10000
start=0
end=10000
useRange=0

if [ "$4" != "" ] && [ "$5" != "" ]; then
  useRange=1
  start=$4
  end=$5 
fi

title="Efficiency plot"
if [ "$6" != "" ]; then
    title=$6;
fi
    
# Sometimes the user forgets to specify -plot.data when the noparse
# option is selected.  Add -plot.data for the user if he forgets it
# when using the noparse tag.

temp_data=$log_file-plot.data
gplot_cmd_file=$log_file-plot.cmds

show_graph=""

# There can be multiple planners in the log, we only look  
# at the one running in thread by the following name
thread="Controller"
    
#  Dont re-parse the log file if the second argument is noparse
#  Parsing through the log file could take a long time


#  Two is added to depth to compensate for step+1 (due to divide by zero possiblity ];
#  and to compensate for (step starts at 1 and depth starts at 0 ]; 

  awk 'BEGIN { LastTick=0; Step=1; Depth=0; First=0; } \
	/current depth/ {  if (First==0) {First++;LastTick=$6;} \
	else { Tick=$6; if (LastTick != Tick) \
	{ Efficiency=1-((Step-Depth)/Step); printf("%f %f %f %f\n", LastTick, Efficiency, Step, Depth); LastTick=Tick; } \
	else { LastTick=Tick; Depth=$14; Depth=Depth+1; Step=$10 }}}' $log_file > $temp_data

# this is actually a parse of an already parsed file.
# but its not parsing the original log file.

lc=`cat $temp_data | wc -l`

if [ $lc -le 2 ]; then
	echo "Not enough points to plot, perhaps you need to choose a wider range"
	exit
fi
 
width=1
lstyle="points"

rm -f $gplot_cmd_file

touch $gplot_cmd_file
echo 'set grid' > $gplot_cmd_file 
echo 'set ylabel "(Efficiency=1-((Step-Depth)/Step))"' >> $gplot_cmd_file

if [ $useRange = 1 ]; then
  echo 'set xrange ['$start':'$end']' >> $gplot_cmd_file   
fi

echo 'set xlabel "Tick Number"' >> $gplot_cmd_file
echo 'set ylabel "(Efficiency=1-((Step-Depth)/Step))"' >> $gplot_cmd_file
echo 'set xlabel "Time [seconds]"' >> $gplot_cmd_file
echo 'set title "'$title'\n"' >> $gplot_cmd_file
echo 'set pointsize 3' >> $gplot_cmd_file

if [ "$format" = "lines" ]; then
#  format=david
awk 'BEGIN { offset=0; LastTick=0; Step=0; } // { offset++;  if ( offset > 5 ) offset=0; if ($2 < 1) { Step++; printf("set label \" %d\" at first %d, graph %f \n", $1, $1, $2-offset*.04); printf("set arrow from first %s, graph 0 to first %s, graph 1 nohead\n", $1, $1);}}' $temp_data>> $gplot_cmd_file
fi

if [ $format = "nolines" ]; then
#  format=chris
awk 'BEGIN { downset=0; downseta=0; LastTick=0; Step=0; } \
        // { Step++;  if ( $2 < 1 )  \
        	{  if ( downseta > 3 ) { downseta=-1; downseta++; \
                	printf("label \"%d\" at first %s, first %s\n", \
                	$3, $1+.2, $2*(1.02-.01*down)); } \
          	   else { if ( down> 3 ) { downset=-1;downset++; \
                	printf("label \"%d\" at first %s, graph %s\n", \
                	$3, $1+.2, .99-.03*down);}}}}' $temp_data>> $gplot_cmd_file
fi

rm -f $imageName
if [ $displayPlot != 1 ]; then
  echo 'set terminal png' >> $gplot_cmd_file
  echo 'set output "'$imageName'"'>> $gplot_cmd_file
fi

echo 'plot '"'"$temp_data"'"' using 1:2 title "Pre-steps for multiple ticks" w '$lstyle' lw '$width >> $gplot_cmd_file

if [ $displayPlot = 1 ]; then
  echo 'set terminal png' >> $gplot_cmd_file
  echo 'set output "'$imageName'"'>> $gplot_cmd_file
  echo 'replot' >> $gplot_cmd_file
fi

#echo "Data went to "$temp_data
#echo "Command file used "$gplot_cmd_file

gnuplot -geometry -0-0 -persist $gplot_cmd_file &
rm -f $temp_data
rm -f $gplot_cmd_file
# EOF
