#!/usr/bin/perl
#
# Read in a file which has a number of versions of a trajectory
#   printed.  Each has a set of lines in chronological order.
#   Read them all, figure out where the final version starts,
#   and print out ONLY the final trajectory's data.
#
# MWR 8/6/2008

$debug = 1;

# read in all the lines
$num_lines = 0;
while (<STDIN>) {
  $line = $_;
  if ($line =~ /^#/) {
    next;
  }
  $line = " " . $line;
  chomp($line);
  $line_array[$num_lines] = $line;

  @words = split(/\s+/, $line);
  $time_array[$num_lines] = $words[3];

  $num_lines++;
}

# now, go back and find the last line which has a 
#   time value of 0.0 -- that's where we will start
$last_zero_index = 0;
for ($i = 0; $i < $num_lines; $i++) {
  if ($time_array[$i] == 0.0) {
    if ($i > $last_zero_index) {
      $last_zero_index = $i;
    }
  }
}

# finally, print out all lines after this last zero value
for ($i = $last_zero_index; $i < $num_lines; $i++) {
  printf "%s\n", $line_array[$i];
}


exit 0;
