#!/usr/apps/bin/perl
#
# Print to band.dat data for density of states
#  given CASTEP log file on stdin, with just ONE band structure iteration
#
#  /usr/apps/bin/perl for dodgy TCM set-up
#
$kpt=0;
$weight=0;
open(out,">band.dat") or die "Cannot open band.dat for output.\n";
#
while (<STDIN>){

  if ( m/^ K-POINT \(([0-9 ]*)/ ) {
    $tmp = $1;
    die "Jump from kpt ",$kpt," to kpt ",$tmp,"\n" unless $tmp==$kpt+1;
    $kpt=$tmp;
    if ( m/([0-9. ]*)$/ ) { $weight=$1; }
                          else { die "Cannot find weight\n";}
    print "Read",$_;
    print "Considering kpt number ",$kpt," with weight ",$weight,"\n";
    $band=0;
    next;
  }

  next unless $kpt>0;

  if ( m/^  BAND\(([0-9 ]*)\):  E=([0-9 -.]*)/ ) {
    $tmp=$1;
    die "Jump from band ",$band," to band ",$band,"\n" unless $tmp==$band+1;
    $band=$tmp;
    print out $2,$weight,"\n";
  }

}
