# com_av_gap.sh to extract the band gap from the ZG nscf outputs
#
#!/bin/bash

rm tmp tmp2 tmp3
# find the gap at equilibrium
  grep highest equil-nscf_444.out > tmp
  a=$(awk '{print $8}' tmp) # cbm
  awk '/'$a'/ { getline; print }' equil-nscf_444.out > tmp # print line below
  sed -i '$d' tmp # remove last empty line
  awk '{print ($5+$6+$7)/3}' tmp >> tmp3
  grep highest equil-nscf_444.out > tmp
  a=$(awk '{print $7}' tmp) # vbm
  grep $a equil-nscf_444.out |head -1 > tmp
  awk '{print ($6+$7+$8)/3}' tmp >> tmp4
  paste tmp3 tmp4 > tmp5
  awk '{print ($1-$2)}' tmp5 > eq_direct_gap.dat
#
rm tmp*
rm direct_gap_T_av.dat
touch direct_gap_T_av.dat
declare -i i
array=(0 50 100 150 200 250 300 400 600 800 1000) 
for i in ${array[@]}
do
  grep highest ZG-nscf_444_"$i".00K.out > tmp
  a=$(awk '{print $8}' tmp) # cbm
  awk '/'$a'/ { getline; print }' ZG-nscf_444_"$i".00K.out > tmp # print line below
  sed -i '$d' tmp # remove last empty line
  awk '{print ($5+$6+$7)/3}' tmp >> tmp3
  grep highest ZG-nscf_444_"$i".00K.out > tmp
  a=$(awk '{print $7}' tmp) # vbm
  grep $a ZG-nscf_444_"$i".00K.out |head -1 > tmp 
  awk '{print ($6+$7+$8)/3}' tmp >> tmp4
  paste tmp3 tmp4 > tmp5
  awk '{print ($1-$2)}' tmp5 > tmp6
  echo $i >> tmp2
done
paste tmp2 tmp6 > direct_gap_T_av.dat
rm tmp*
