#!/usr/bin/env perl

-e "DONE" or die;

chomp(my $gap = `getparam bcsgap`);

open(O, "lastblock annotated.dat |") or die;
while (<O>) {
    /(\S*)\s\((\S*)\)/ or next;
    $E = $1;
    $S = $2;
#    print "$E $S\n";
    if ($S == 1 and !defined($first1)) {
        $first1 = $E;
        next;
    }
    if ($S == 2 and !defined($first2)) {
        $first2 = $E;
        next;
    }
    if ($S == 1 and !defined($second1)) {
        $second1 = $E;
        next;
    }
    if ($S == 2 and !defined($second2)) {
        $second2 = $E;
        next;
    }
}
close O;

open(O, ">subgap.dat");
print O "$first1 $second1\n";
print O "$first2 $second2\n";
close O;

$optical1 = $second1-$first1;
$optical2 = $second2-$first2;
$optical1 /= $gap;
$optical2 /= $gap;
print "$optical1 $optical2\n";
open(O, ">optical.dat");
print O "$optical1 $optical2\n";
close O;

$EYSR = $first1-$first2;
$EYSR /= $gap;
print "$EYSR\n";
open(O, ">EYSR.dat");
print O "$EYSR\n";
close O;


open(L, "<log") or die;
while (<L>) {
    if (/Iteration\s(\S*)\/(\S*)/) {
        $nr = $1;
        $max = $2;
        if ($nr == $max) {
            last;
        }
    }
}
while (<L>) {
    if (/Diagonal matrix elements of operator n_d/) {
        last;
    }
}
$_ = <L>;
/1:\s(\S*)/ or die;
$n1 = $1;
$_ = <L>;
/2:\s(\S*)/ or die;
$n2 = $1;

print "$n1 $n2\n";
open(O,">n.dat");
print O "$n1 $n2\n";
close O;

unlink "log";
unlink "annotated.dat";
unlink "data";
unlink "mmalog";
unlink "log2";
