#!/bin/bash

#-----------------------------------------------------------------------
#
#                                  PLPWEIGHT
#
# Reads the prodwei file and plots the separable weights used by potfit.
# 01/01 HDM
#
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Exit status:
#   0: o.k.
#   1: illegal options
#   2: file not existent or readable
#-----------------------------------------------------------------------
# set locale, so that date etc will be in uniform format.
export LC_ALL=C

#-----------------------------------------------------------------------
# Definitions
#-----------------------------------------------------------------------
if [ -d /tmpa ] && [ -w /tmpa ] ; then
   tmp="/tmpa"
elif [ -d /work ] && [ -w /work ] ; then
   tmp="/work"
elif [ -d /tmp ] && [ -w /tmp ] ; then
   tmp="/tmp"
elif [ -d "$HOME" ] && [ -w "$HOME" ] ; then
   tmp="$HOME"
else
   tmp="."
fi

gnuop=$tmp/gnu_options.${LOGNAME}_$$
gnupr=$tmp/gnu_print.${LOGNAME}_$$
logscale=0
file=prodwei


#-----------------------------------------------------------------------
# Help Text
#-----------------------------------------------------------------------
purpose="\nPurpose: Plots separable weights of the potfit program."
usage="Usage: plpweight [-a -x -z -y -l -G] [-p] [-P printer] [-h] dof"
options='Options:\n
-h:\t\t print this help text\n
-a emin:\t set the lower range of energy to emin\n
-x emax:\t set the upper range of energy to emax\n
-y ymax:\t set the upper range of intensity to ymax\n
-z ymin:\t set the lower range of intensity to ymin\n
-l:\t\t use a logarithmic scale for the intensities\n
-G:\t\t show grid lines\n
-p:\t\t prompt for printing the plot at default printer lpr\n
-P printer:\t specify printer (e.g. -P "| lpr -Pps2" or -P file.ps)\n
dof:\t\t number of the degree of freedom for which the weight is plotted.\n'


#-----------------------------------------------------------------------
# Read and check arguments
#-----------------------------------------------------------------------

while getopts ":a:x:y:z:hGlpP:" option; do
  case $option in
    h ) echo -e $purpose
        echo -e $usage
        echo -e $options
	echo ' '
        exit 0 ;;
    a ) xmin=$OPTARG ;;
    x ) xmax=$OPTARG ;;
    z ) ymin=$OPTARG ;;
    y ) ymax=$OPTARG ;;
    G ) grid=1 ;;
    l ) logscale=1 ;;
    p ) lpr="| lpr" ;;
    P ) lpr=$OPTARG
        if [ -z "${lpr##-*}" ] ; then
     	  echo -e "\nERROR: no printer specified with -P option!"
	  $0 -h
	  exit 1
	fi ;;
    \?) echo -e "\nERROR: option $OPTARG unknown!"
	$0 -h
	exit 1 ;;
  esac
done

dof=${!OPTIND}
if [ -z "$dof" ] ; then
    echo "Number of dof? :"
    read  dof
fi

modelab=`fgrep '# dof =  '$dof prodwei | cut -c13-25`
#-----------------------------------------------------------------------
# Define GNU options
#-----------------------------------------------------------------------

dir=$(pwd)
ind=$(($dof-1))
echo " dof $dof , ind $ind"

if [ -z "${dir##$HOME/*}" ] ; then  dir="~/${dir##$HOME/}" ; fi
echo -e 'set style data lines'                    >  $gnuop
if [ $logscale -eq 1 ] ; then
    ymin=${ymin:="1e-4"}
    echo -e 'set logscale y'                      >> $gnuop
fi
if [ -n "$grid" ] ; then
    echo -e 'set grid'                            >> $gnuop
fi
echo -e 'set xlabel "'$dof' : '$modelab'"'        >> $gnuop
echo -e 'set ylabel "Weight"'                     >> $gnuop
echo -e "set xrange [$xmin:$xmax]"                >> $gnuop
echo -e "set yrange [$ymin:$ymax]"                >> $gnuop
#echo -e "set tics out"                            >> $gnuop
#echo -e "set nokey"                               >> $gnuop
echo -e 'set title "'$dir'/prodwei,  '$(date)'"'  >> $gnuop
echo -e 'plot "'$file'" index '$ind' notitle'     >> $gnuop


#-----------------------------------------------------------------------
# Call Gnuplot
#-----------------------------------------------------------------------

gnuplot -geometry +0+0 -persist $gnuop
if [ ! -z "$lpr" ] ; then
  echo -e 'Print the plot to "'$lpr'" (y/n, return=no)? \c'
  read print
  if [ ${print:-n} = 'y' ]  ; then
     echo "set terminal postscript" >> $gnupr
     echo 'set output "'$lpr'"'     >> $gnupr
     gnuplot $gnupr $gnuop
  fi
fi

#-----------------------------------------------------------------------
# Remove garbage and exit
#-----------------------------------------------------------------------

/bin/rm -f $gnupr $gnuop
exit 0

