#!/usr/bin/perl
#
# Take a set of lines with X Y Z vx vy vz data grabbed from JPL Horizons
#    site and reformat it to look nice.
#    
# We assume each line looks like this:
#
#     body    0    sun    mass   X  Y  Z  vx  vy  vz
#
#  where
#              body           is the word "body"
#               0             is a running integer index, starting at zero
#              sun            is the name of the body, like "sun"
#              mass           is the mass of the body in kg, like 1.99E30
#               X             is X-position of object, in km
#                                (we will convert to meters for output)
#               Y             likewise for Y-position
#               Z             likewise for Y-position
#               vx            is the X-velocity of body, in km/s
#                                (we will convert to m/s for output)
#               vy            likewise for Y-velocity
#               vz            likewise for Z-velocity
#
#
#        Usage:   perl reformat_planets.pl < infile > outfile
#
# MWR 1/2/2009

$debug = 1;

while (<STDIN>) {
  $line = $_;
  chomp($line);
  if ($line =~ /^#/) {
    printf "%s\n", $line;
    next;
  }
  $line = " " . $line;

  @words = split(/\s+/, $line);
  $nwords = $#words;
  if ($nwords != 10) {
    printf STDERR "bad line does not have 10 words, has %d ..%s..\n", 
                 $nwords, $line;
    exit(1);
  }
  $body = $words[1];
  $index = $words[2];
  $name = $words[3];
  $mass = $words[4];
  $xpos = $words[5]*1.0E3;
  $ypos = $words[6]*1.0E3;
  $zpos = $words[7]*1.0E3;
  $xvel = $words[8]*1.0E3;
  $yvel = $words[9]*1.0E3;
  $zvel = $words[10]*1.0E3;

  # here's where we print the result -- there may be lots and lots
  #   of significant digits here, so don't lose 'em
  printf "%4s %4d %10s %20.15e ", $body, $index, $name, $mass;
  printf "%22.15e %22.15e %22.15e ", $xpos, $ypos, $zpos;
  printf "%22.15e %22.15e %22.15e ", $xvel, $yvel, $zvel;
  printf "\n";

}



exit 0;
