#!/bin/bash

# This script takes one parameter -- a text file containing table with columns:
#
# time		dx		dt		q_err	p_err
# 
# in rows there are sumarized data from runs of Richards solver for different setting of 
# discretization parameters. This script produce four auxiliary data files for plotting four graphs:
# p_err on dx
# q_err on dx
# p_err on dt
# q_err on dx
#
# These four auxiliary files are used as direct input data files for gnuplot.
# 
set -x

in_file="$1"



set +x
# Makes one auxiliary plot file with data for gnuplot.
# Three parameters are:
# $1 -- output file name
# $2 -- whether to transpose output table (default table is cols -- dt values, rows -- dx values
# $3 -- which variable should be in the table (q_err or p_err)
function plot_file {
  file_name=$1
  swap=$2
  out_var="$3"
  
  if [ "$swap" == "swap" ]
  then
    x_list="$dt_values"
    t_list="$dx_values"
  else
    x_list="$dx_values"
    t_list="$dt_values"
  fi

  echo >$file_name
  # dt on x axes
  for x in $x_list
  do
    line="$x "
    for t in $t_list
    do
      if [ "$swap" == "swap" ]
      then
        dx=$t; dt=$x
      else
        dx=$x;dt=$t
      fi
      set -x
      values=(`cat $in_file | grep "^ *[^ ]* *$dx *$dt"`)
      q_err=${values[4]}
      p_err=${values[3]} 
      set +x
      if [ "$q_err" == "" ]; then q_err="0"; fi
      if [ "$p_err" == "" ]; then p_err="0"; fi

      line+=" ${!out_var}"
    done
    echo $line >>$file_name
  done
}

# takes an auxiliary filename $1 and produces plot command to plot 
# every column of the file as different line in the graph labeled by
# tags in list $2. Third parameter is description of the x-axes.
function make_plot_cmd {
  file_name=$1
  y_axes_list=`echo $2` # convert linebreaks into spaces

  #out="set title offset 0,-0.6 \"$file_name\";
  out="plot "
  first_tok=${y_axes_list%% *}
  last_tok=${y_axes_list##* }
  i=2
  for dy in $y_axes_list
  do
    out+="'$file_name' using 1:$i " #title '$dy', "
    if [ $dy == $first_tok -o $dy == $last_tok ]
    then
      # first line wit title
      out+="title '$3=$dy', "
    else
      out+="notitle, "
    fi
    i=`expr $i + 1`
  done
  #last line with title
  #out=${out%notitle, }
  #out+="title '$3=$dy', "
  echo "${out}  0.01*x**2 with lines title '0.01*${3}^2' "
}

function plot_one_time {
time=$1

in_file_new="${in_file}_t_$time"
cat $in_file | grep "^ *$time " >$in_file_new
in_file=$in_file_new

# get possible dt and dx values
time_values="`cat $in_file | grep -v "time" | sed "s/^ *\([^ ]*\) .*/\1/"|sort -un `"
dt_values="`cat $in_file | grep -v "time" | sed "s/^[^ ]* *//"| sed "s/[^ ]* *//"|sed "s/  *.*//"|sort -un `"
dx_values="`cat $in_file | grep -v "time" | sed "s/^[^ ]* *//"| sed "s/  *.*//"|sort -un `"

plot_file "p_err_on_dt" "swap" "p_err"
plot_file "q_err_on_dt" "swap" "q_err"
plot_file "p_err_on_dx" "no_swap" "p_err"
plot_file "q_err_on_dx" "no_swap" "q_err"


p_dt_plot=`make_plot_cmd p_err_on_dt "$dx_values" 'dx'`
q_dt_plot=`make_plot_cmd q_err_on_dt "$dx_values" 'dx'`
p_dx_plot=`make_plot_cmd p_err_on_dx "$dt_values" 'dt'`
q_dx_plot=`make_plot_cmd q_err_on_dx "$dt_values" 'dt'`
}

# select and plot only given time from summary
plot_one_time "0.5"


# gnuplot script, plot four graphs with p_dt_plot, .. commands
cat >g_cmd <<END
#set terminal pdf monochrome font "Arial,5"
#set output 'conv_plot.pdf'
set pointsize 0.5

set logscale x
set logscale y
set style data linespoints
set key right bottom
#unset key

set size 2,2
set origin 0,0
set multiplot 

#set bmargin at screen 0.01
#set lmargin at screen 0.01
#set rmargin at screen 1
#set tmargin at screen 1
#show margin


set title offset 16,-0.6 "(a)"
set xlabel "dt" offset 16,1.5
set xr [0.001:0.5]
set yr [1e-7:0.01]
# plot the first graph so that it takes a quarter of the screen
set size 0.53,0.53
set origin -0.01,0.5
$p_dt_plot

set title offset 16,-0.6 "(c)"
set xlabel "dt" offset 16,1.5
set xr [0.001:0.5]
set yr [1e-7:0.01]
# plot the second graph so that it takes a quarter of the screen
set size 0.53,0.53
set origin -0.01,0
$q_dt_plot

set title offset 16,-0.6 "(b)"
set xlabel "dx" offset 16,1.5
set xr [0.001:0.5]
set yr [1e-7:0.01]
# plot the third graph so that it takes a quarter of the screen
set size 0.53,0.53
set origin 0.47,0.5
$p_dx_plot

set title offset 16,-0.6 "(d)"
set xlabel "dx" offset 16,1.5
set xr [0.001:0.5]
set yr [1e-7:0.01]
# plot the fourth graph so that it takes a quarter of the screen
set size 0.53,0.53
set origin 0.47,0
$q_dx_plot

# On some terminals, nothing gets plotted until this command is issued
unset multiplot
  
END

gnuplot <g_cmd