#!/usr/bin/perl
#
# Parse the MPCORB.DAT file obtained from
#    https://www.minorplanetcenter.net/iau/MPCORB.html
#
# Flag with "#" objects with perihelion or semi-major axis values
#    which are 0.000.  We'll want to avoid them.
#
# MWR 11/13/2018

$debug = 0;

open(INFILE, "MPCORB.DAT") || die("can't open MPCORB.DAT");
while (<INFILE>) {
  $line = $_;
  if ($line =~ /^#/) {
    next;
  }

  @words = split(/\s+/, $line);

  $desig = substr($line, 1, 7);
  $H = substr($line, 9, 5);
  $G = substr($line, 15, 5);
  $epoch = substr($line, 21, 5);
  $M = substr($line, 27, 9);
  $peri = substr($line, 38, 9);
  $node = substr($line, 49, 9);
  $incl = substr($line, 60, 9);
  $e = substr($line, 71, 9);
  $n = substr($line, 81, 11);
  $a = substr($line, 92, 11);

  $ref = substr($line, 108, 8);
  $nobs = substr($line, 118, 4);
  $nopp = substr($line, 124, 3);

  # set a flag if the object has bogus values
  $bogus_flag = 0;
  if (($peri == 0.000) || ($a == 0.000)) {
    $bogus_flag = 1;
  }
 
  if ($debug > 0) {
    printf " desig ..%s.. \n", $desig;
    printf " peri  ..%s.. \n", $peri;
    printf " incl  ..%s.. \n", $incl;
    printf " a     ..%s.. \n", $a;
    printf " ref   ..%s.. \n", $ref;
    printf " nobs  ..%s.. \n", $nobs;
    printf " nopp  ..%s.. \n", $nopp;
    printf " \n";
  }

  if ($bogus_flag == 1) {
#    printf "# bogus input line is ..%s", $line;
    printf "#";
  }
  printf " %6d  H %6.3f peri %8.3f incl %6.1f e %6.4f a %12.3f nopp %5d \n",   
       $desig, $H, $peri, $incl, $e, $a, $nopp;
}
close(INFILE);





exit 0;
