#!/bin/sh
#=======================================================================
#   This litle script read several output file (from ABINIT)
#   and writes out the results in terms of columns
#
#   It is useful to plot several ABINIT's datasets on the
#   same graph>
#
#   Parameters to customize (below):
#   NFILES: number of ABINIT output files
#   FILE1,FILE2,...: names of the ABINIT output files
#   NDTSET=number of datasets in each ABINIT output file
#          (in fact the min. of the ndtset values)
#   XX=the properties which has to be plotted as x
#   YY=the properties which has to be plotted as y(x)
#   OPTION= if 0, y(x) is printed out
#           if 1, y(x)-y(last x) is printed out
#                 (useful to print etotal-etotalmin...)
#=======================================================================

#=== To be customized ====
NFILES=3
FILE1=tpaw2_1.out.rc18
FILE2=tpaw2_1.out.rc20
FILE3=tpaw2_1.out.rc22
NDTSET=10
XX=ecut
YY=etotal
OPTION=1
AWK=awk

#=== Title ====
TITLE="#"${XX}
for FF in $FILE1 $FILE2 $FILE3 $FILE4 $FILE5 $FILE6 $FILE7 $FILE8 $FILE9 $FILE10 ;do
 TITLE=${TITLE}" "${FF}
done
echo $TITLE

#=== Loops over files and datasets ====
for ii in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ;do
 if [ "$ii" -le "$NDTSET" ];then
  LINE=`$AWK '{if ($1=="'${XX}${ii}'" && NR>250) {print $2};}' $FILE1`
  for FF in $FILE1 $FILE2 $FILE3 $FILE4 $FILE5 $FILE6 $FILE7 $FILE8 $FILE9 $FILE10 ;do
   if [ "$OPTION" == "0" ];then
    LINE=$LINE" "`$AWK '{if ($1=="'${YY}${ii}'" && NR>250) {print $2};}' $FF`
   else
    YMAX=`$AWK '{if ($1=="'${YY}${NDTSET}'" && NR>250) {print $2};}' $FF`
    LINE=$LINE" "`$AWK '{if ($1=="'${YY}${ii}'" && NR>250) {printf("%-20.10e",$2 - '${YMAX}')};}' $FF`
   fi
  done
  echo $LINE
 fi
done
