remove any leading ::ffff: on ipv4 addresses
[spider.git] / perl / convkeps.pl
index 3b08e64db62c2a8997a2f8873812cb74983fedd1..5c8cc077a740d86e66d5e0ad4cec45c3376174d3 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl -w
+#!/usr/bin/env perl
 #
 # Convert an Amsat 2 line keps bull into Sun.pm format
 #
 #    email | convkeps.pl        (in amsat email format)  
 #    convkeps.pl -p keps.in     (a file with just plain keps)
 # 
+# if you add the -c flag then the %keps hash will be cleared down
+# before adding the new ones.
+#
 # Copyright (c) 2000 Dirk Koopman G1TLH
 #
-# $Id$
+#
 #
 
 require 5.004;
@@ -49,13 +52,23 @@ use strict;
 use vars qw($root %keps);
 
 use Data::Dumper;
-use Keps;
+require Keps;
 
 my $fn = "$root/local/Keps.pm";
 my $state = 0;
 my $name;
 my $ref;
 my $line;
+my $count = 0;
+
+my %lookup = (
+       'AO-5' => 'AO-05',
+       'AO-6' => 'AO-06',
+       'AO-7' => 'AO-07',
+       'AO-8' => 'AO-08',
+       
+);
+
 my $f = \*STDIN;
 
 while (@ARGV) {
@@ -64,30 +77,38 @@ while (@ARGV) {
                $state = 1;
        } elsif ($arg eq '-e') {
                $state = 0;
+       } elsif ($arg eq '-c') {
+               %keps = ();
        } elsif ($arg =~ /^-/) {
-               die "Usage: convkeps.pl [-e|-p] [<filename>]\n\t-p - plain file just containing keps\n\t-e - amsat email format input file (default)\n";
+               die "Usage: convkeps.pl [-c] [-e|-p] [<filename>]\n\t-p - plain file just containing keps\n\t-e - amsat email format input file (default)\n\t-c - clear Keps data before adding this lot\n";
        } else {
                open (IN, $arg) or die "cannot open $arg (!$)";
                $f = \*IN;
        }
 }
+
 while (<$f>) {
        ++$line;
+#    print;
        chomp;
+       last if m{^-};
+
        s/^\s+//;
-    s/\s+$//;
+    s/[\s\r]+$//;
        next unless $_;
        last if m{^/EX}i;
-       last if m{^-};
        
-       if ($state == 0 && /^TO ALL/) {
+       if ($state == 0 && /^Decode/i) {
                $state = 1;
        } elsif ($state == 1) {
-               last if m{^/EX/i};
+               last if m{^-};
+               next if m{^To\s+all}i;
                
-               if (/^\w+/) {
-                       s/\s/-/g;
-                       $name = $_;
+               if (/^([- \w]+)(?:\s+\[[-+\w]\])?$/) {
+                       my $n = uc $1;
+                       $n =~ s/\s/-/g;
+                       $name = $lookup{$n};
+                       $name ||= $n;
                        $ref = $keps{$name} = {}; 
                        $state = 2;
                }
@@ -101,12 +122,12 @@ while (<$f>) {
                        $ref->{mm2} = genenum($mm2);
                        $ref->{bstar} = genenum($bstar);
                        $ref->{elset} = $elset - 0;
-#                      print "$id $number $epoch $decay $mm2 $bstar $elset\n"; 
-#                      print "mm2: $ref->{mm2} bstar: $ref->{bstar}\n";
+                       #print "$id $number $epoch $decay $mm2 $bstar $elset\n"; 
+                       #print "mm2: $ref->{mm2} bstar: $ref->{bstar}\n";
                        
                        $state = 3;
                } else {
-                       print "out of order on line $line\n";
+                       #print "out of order on line $line\n";
                        undef $ref;
                        delete $keps{$name};
                        $state = 1;
@@ -121,8 +142,9 @@ while (<$f>) {
                        $ref->{argperigee} = $peri - 0;
                        $ref->{raan} = $raan - 0;
                        $ref->{orbit} = $orbit - 0;
+                       $count++;
                } else {
-                       print "out of order on line $line\n";
+                       #print "out of order on line $line\n";
                        delete $keps{$name};
                }
                undef $ref;
@@ -130,22 +152,28 @@ while (<$f>) {
        }
 }
 
-my $dd = new Data::Dumper([\%keps], [qw(*keps)]);
-$dd->Indent(1);
-$dd->Quotekeys(0);
-open(OUT, ">$fn") or die "$fn $!";
-print OUT "#\n# this file is automatically produced by convkeps.pl\n#\n";
-print OUT "# Last update: ", scalar gmtime, "\n#\n";
-print OUT "\npackage Sun;\n\n";
-print OUT $dd->Dumpxs;
-print OUT "\n";
-close(OUT);
+if ($count) {
+       my $dd = new Data::Dumper([\%keps], [qw(*keps)]);
+       $dd->Indent(1);
+       $dd->Quotekeys(0);
+       open(OUT, ">$fn") or die "$fn $!";
+       print OUT "#\n# this file is automatically produced by convkeps.pl\n#\n";
+       print OUT "# Last update: ", scalar gmtime, "\n#\n";
+       print OUT "\npackage Sun;\n\n";
+       print OUT $dd->Dumpxs;
+       print OUT "1;\n";
+       close(OUT);
+}
+
+print "$count keps converted\n";
+exit($count ? 0 : -1);
 
 
 # convert (+/-)00000-0 to (+/-).00000e-0
 sub genenum
 {
        my ($sign, $frac, $esign, $exp) = unpack "aa5aa", shift;
+       $esign = '+' if $esign eq ' ';
        my $n = $sign . "." . $frac . 'e' . $esign . $exp;
        return $n - 0;
 }