awk 'BEGIN{i=0} /HEAD OF MICRO/,\
                /\/real/ \
                 {if ($1=="<r>") {a[i]=$2 ; b[i]=$3 ; c[i]=$4 ; d[i]=$5 ; i=i+1}} \
     END{for (j=0;j<i/2;j++) print a[j],b[j],b[j+i/2]}' vasprun.xml > chi0.dat

awk 'BEGIN{i=0} /INVERSE MACRO/,\
                /\/real/ \
                 {if ($1=="<r>") {a[i]=$2 ; b[i]=$3 ; c[i]=$4 ; d[i]=$5 ; i=i+1}} \
     END{for (j=0;j<i/2;j++) print a[j],b[j],b[j+i/2]}' vasprun.xml > chi.dat


cat >plotfile<<!
# set term postscript enhanced eps colour lw 2 "Helvetica" 20
# set output "optics.eps"

set xrange [0:15]

plot "chi0.dat"   using (\$1):(\$3)  w lp lt -1 lw 1 pt 4 lc -1 title "IPA real", \
     "chi0.dat"   using (\$1):(-\$2) w lp lt  0 lw 2 pt 4 lc -1 title "IPA imag", \
     "chi.dat"    using (\$1):(\$3)  w lp lt -1 lw 1 pt 2 lc  1 title "RPA real", \
     "chi.dat"    using (\$1):(-\$2) w lp lt  0 lw 2 pt 2 lc  1 title "RPA imag"
!

gnuplot -persist plotfile
