#!/bin/sh
# Script nutpl: (P. Valanju, Dec 2001)
# To render gnuplots from Nut_plot file
# Usage: nutpl filename [plotnumber]
#                         [plotnumber] is optional
###############################
# First define all sub-scripts
#--------------------------
# IsIn: Check if $1 is in $2
IsIn ()
{
if [ "$1" != "" ];then
 pat=$1
 shift
 echo $*|grep -q $pat
fi
}
#---------------------
# lowCase: Change to lower case
lowCase ()
{
 echo $*|tr '[A-Z]' '[a-z]'
}
#--------------------------------------------------------
# showit: Show what is after "pat" pattern
# used in FindVal
showit()
{
until [ "$1" = $pat -o $# = 1 ]
do
  shift
done
if [ $# -gt 1 ];then shift;echo $*;fi
}
#-----------------------------------------
# FindVal:
# Finds whatever follows after given pattern $1 in file $2
# Finds only the first appearance of pattern
# Usage: FindVal pattern file
FindVal()
{
if [ "$#" -lt 2 ];then exit 1;fi
if [ ! -f "$2" ];then exit 1;fi
pat="$1"
patline=`grep $1 $2`
if [ ! "$patline" ]; then exit 1;fi
showit $patline
}
#-----------------------------------------
# plfl: Write file to be gnuplotted by gnuplotit
plfl ()
{
  echo "'"$plfile"'"' using '$1'\' >> gtmp
  shift
  for arg in $*
  do
    arg=`lowCase $arg`
    case "$arg" in
      r  | y2   | x1y2 ) echo 'axes x1y2 \' >> gtmp;;
      u  | x2   | x2y1 ) echo 'axes x2y1 \' >> gtmp;;
      ru | x2y2 ) echo 'axes x2y2 \' >> gtmp;;
      * ) echo ",'"$plfile"'"' using '$arg'\' >> gtmp;;
    esac
  done
  echo ' ' >> gtmp
}
#-----------------------------------------------
# gnuplotit: finally makes a single gnuplot with all settings
gnuplotit ()
{
# Write all gnuplot commands to gtmp file
 if [ "$timest" = "T" ]; then echo 'set time' >> gtmp; fi
 if [ "$legend" = "F" ]; then echo 'set nokey' >> gtmp; fi
 if [ "$border" = "T" ]; then echo 'set border' >> gtmp; fi
 if [ "$set" != "" ]; then echo 'set "'$set'"' >> gtmp; fi
 if [ "$title" != "" ]; then echo 'set title "'$title'"' >> gtmp; fi
 if [ "$xlabel" != "" ]; then echo 'set xlabel "'$xlabel'"' >> gtmp; fi
 if [ "$ylabel" != "" ]; then echo 'set ylabel "'$ylabel'"' >> gtmp; fi
 if [ "$zlabel" != "" ]; then echo 'set zlabel "'$zlabel'"' >> gtmp; fi
 if [ "$xtics" != "" ]; then echo 'set noxtics' >> gtmp; fi
 if [ "$ytics" != "" ]; then echo 'set noytics' >> gtmp; fi
 if [ "$ztics" != "" ]; then echo 'set noztics' >> gtmp; fi
 if [ "$x2tics" != "" ]; then echo 'set x2tics' >> gtmp; fi
 if [ "$y2tics" != "" ]; then echo 'set y2tics' >> gtmp; fi
 if [ "$z2tics" != "" ]; then echo 'set z2tics' >> gtmp; fi
 if [ "$style" != ""  ]; then echo 'set data style '$style >> gtmp; fi
 if [ "$view" != "" ]; then echo 'set view "'$view'"' >> gtmp; fi
 if [ "$contour" != "" ];then
   if [ "$contour" = "base" ];then echo 'set contour base' >> gtmp;fi
   if [ "$contour" = "surface" ];then echo 'set contour surface' >> gtmp;fi
   if [ "$contour" = "both" ];then echo 'set contour both' >> gtmp;fi
   if [ "$contour" = "2dcontour" ];then
     echo 'set contour base' >> gtmp
     echo 'set nosurface' >> gtmp
     echo 'set view 0,0,1' >> gtmp
   fi
   if [ "$contour" = "3dcontour" ];then
     echo 'set contour base' >> gtmp
     echo 'set nosurface' >> gtmp
   fi
 fi
 if [ "$surface" = "hidden" ];then echo 'set hidden3d' >> gtmp;fi
 if [ "$device" != ""  ];then 
   echo 'set terminal '$device >> gtmp
   echo 'set output "'$output'"' >> gtmp
 fi
 if [ "$xgrid" = "T" ];then echo 'set grid ytics' >> gtmp; fi
 if [ "$ygrid" = "T" ];then echo 'set grid xtics' >> gtmp;fi
 if [ "$tics" != "" ]; then echo 'set tics '$tics >> gtmp; fi
# Plot or Splot depending on dimensions
 if [ "$plDim" = "2" ];then
  if [ "$graph" = "" ];then graph='1:2';fi 
  echo plot '\' >> gtmp
 else
  if [ "$graph" = "" ];then graph='1:2:3';fi 
  echo set parametric >> gtmp
  echo splot '\' >> gtmp
 fi
# Now actually plot the gtmp file
 plfl $graph
 echo 'pause -1' >> gtmp
 gnuplot gtmp  #plot datafile using gnuplot
}
#-----------------------------
# plotnew: Just do it
plotnew()
{
 echo '#' > gtmp; gnuplotit 
}
#----------------------------------------------
# Gnuplots 2-d or 3-d data from a file written by plot_2 or plot_3
PlotOne()
{
# Make sure given plot data file exists
if [ -f "$1" ];then
   plfile=$1
else
    echo -n 'Enter name of file to plot: '
   read plfile
fi
if [ ! -f "$plfile" ];then
    echo $plfile' does not exist, cannot help you, you moron!'
 exit 1
fi
#
# Get the default settings from plot data file, if there are any
plDim=`grep 3dPlot $plfile`
if [ "$plDim" = "" ]; then
 plDim=2
else
 plDim=3
fi
echo $plDim'-D Plot'
#
grep "#" "$plfile" | cat > tmp
echo 'Default settings are:'
cat tmp
timest=`FindVal pltime tmp`
legend=`FindVal legend tmp`
border=`FindVal border tmp`
view=`FindVal view tmp`
tics=`FindVal tics tmp`
graph=`FindVal graph tmp`
title=`FindVal title tmp`
xlabel=`FindVal xlabel tmp` 
ylabel=`FindVal ylabel tmp`
zlabel=`FindVal zlabel tmp` 
device=`FindVal device tmp` 
npts=`FindVal npts tmp` 
style=`FindVal style tmp` 
surface=`FindVal surface tmp`
contour=`FindVal contour tmp`
xgrid=`FindVal xgrid tmp` 
ygrid=`FindVal ygrid tmp` 
xerr=`FindVal xerr tmp`
yerr=`FindVal yerr tmp`
xtics=`FindVal xtics tmp` 
ytics=`FindVal ytics tmp` 
ztics=`FindVal ztics tmp` 
x2tics=`FindVal x2tics tmp` 
y2tics=`FindVal y2tics tmp` 
z2tics=`FindVal z2tics tmp` 
rm tmp
####
# Set all unset switches to their default values
if [ "$timest" = "" ]; then timest="T";fi
if [ "$legend" = "" ]; then legend="T";fi
if [ "$border" = "" ]; then border="F";fi
if [ "$xgrid" = "" ]; then xgrid="F";fi
if [ "$ygrid" = "" ]; then ygrid="F";fi
####
# Now plot the data with all given settings
plotnew
}
#----------------------------------------
# plotit: Plots the single extracted plot from file
plotit ()
{
# Extract correct plot number from file
 if [ "$plnum" != "" ]; then
  # See if given plot number is in list of plots
  grep Plot $pltfile > tmp
  ok=`grep "$plnum" tmp`
  if [ "$ok" = "" ];then
   echo Plot $plnum not in $pltfile
  else
   echo '/Plot *'$plnum'/' > tmp
   echo .,/---/p >> tmp
   echo q >> tmp
   ed -s $pltfile < tmp > LastPlot
   cut -c2- LastPlot > LP
   PlotOne LP
  fi
 fi
}
######################################
# Now the main script
# See if file exists, or get its name
if ! test -f $1; then
  echo Enter name of Plot Data File
  read pltfile
  if ! test -f $pltfile; then
     echo No $pltfile found, sorry
     exit
  fi
else
  pltfile=$1
fi
###
# Plot it if plnum and pldim are given
plnum=$2
plotit
###
# Render any more plots
# Go into inf loop until zero is given
while true
do
# Put out a list of all plots
  echo ------------------------
  echo List of plots found in ${pltfile}
  grep Plot $1
  echo ------------------------
  echo 'Enter plot number, 0 or q to exit'
  read plnum
  if [ "$plnum" = "0" -o "$plnum" = "q" ];then exit;fi
  if [ "$plnum" -lt 0 ];then exit;fi
  plotit
done
##################################
