#!/bin/bash

#-----------------------------------------------------------------------
#
#                                  PLFDSPEC
#
# Plots the spectrum generated by the filter program using Gnuplot.
#
# 12/99 MB
#
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# 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
v=1
w=2

#-----------------------------------------------------------------------
# Help Text
#-----------------------------------------------------------------------
purpose="\nPurpose: Plots line spectra created by the filter-diagonalisation program."
usage="Usage: plfdspec [-h] [-a emin] [-x emax] [-z ymin] [-y ymax] [-f file2]\n
\t\t [-l] [-G] [-n] [-c] [-v] [-w] [-p] [-P printer] file"
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
-f file2:\t reads in a second file and plots it with negative intensities\n
-l:\t\t use a logarithmic scale for the intensities (not with -f)\n
-G:\t\t show grid lines\n
-n:\t\t do not automatically append ".eig" if this extension is missing\n
-v:\t\t use column v as energy-values (default is v=1)\n
-w:\t\t use column w for intensities  (default is w=2)\n
-c:\t\t "file" is outputfile of fdcheck84, sets "-w 3 -n"\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
file:\t\t name of file containing the spectrum (default: filter.eig)\n
\t\t the extension ".eig" will be appended if missing (see option -n)\n'

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

while getopts ":a:x:y:z:f:v:w:hGclnpP:" option; do
  case $option in
    h ) echo -e $purpose
        echo -e $usage
        echo -e $options
        exit 0 ;;
    a ) xmin=$OPTARG ;;
    x ) xmax=$OPTARG ;;
    z ) ymin=$OPTARG ;;
    y ) ymax=$OPTARG ;;
    f ) file2=$OPTARG ;;
    G ) grid=1 ;;
    v ) v=$OPTARG ;;
    w ) w=$OPTARG ;;
    l ) logscale=1 ;;
    c ) no=1 ; w=3 ;;
    n ) no=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

file=${!OPTIND}
if [ -z "$file" ] ; then file="filter.eig" ; fi
if [ -z "$no" ]; then file="${file%%.eig}.eig" ; fi
if [ -z "$no" ] && [ -n "$file2" ]; then file2="${file2%%.eig}.eig" ; fi
if [ ! -r $file ] ; then
  echo -e "\nERROR: file $file not existent or readable!\n"
  exit 2
fi

#-----------------------------------------------------------------------
# Define GNU options
#-----------------------------------------------------------------------

dir=$(pwd)
if [ -z "${dir##$HOME/*}" ] ; then  dir="~/${dir##$HOME/}" ; fi
echo -e 'set style data impulses'                 >  $gnuop
if [ $logscale -eq 1 ] ; then
    if [ -n "$file2" ] ; then
	echo "Sorry, options -l and -f are incompatible!"
	exit 1
    fi
    ymin=${ymin:="1e-12"}
    echo -e 'set logscale y'                      >> $gnuop
else
    echo -e 'set xzeroaxis 1'                     >> $gnuop
fi
if [ -n "$grid" ] ; then
    echo -e 'set grid'                            >> $gnuop
fi
echo -e 'set xlabel "Energy"'                     >> $gnuop
echo -e 'set ylabel "Intensity"'                  >> $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'/'$file',   '$(date)'"' >> $gnuop
if [ -n "$file2" ] ; then
    echo -e 'plot "'$file'" us '$v':'$w' notitle ,'\
        '"'$file2'" us '$v':($'$w'*(-1)) notitle' >> $gnuop
else
    echo -e 'plot "'$file'" us '$v':'$w' notitle' >> $gnuop
fi

#-----------------------------------------------------------------------
# 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



