added plain file mode to convkeps and also made it an update
[spider.git] / perl / convkeps.pl
1 #!/usr/bin/perl -w
2 #
3 # Convert an Amsat 2 line keps bull into Sun.pm format
4 #
5 # This program will accept on stdin a standard AMSAT 2 line keps
6 # bull such as you would find in an email or from the packet network
7 #
8 # It will write a file called /spider/local/Keps.pm, this means that
9 # the latest version will be read in every time you restart the 
10 # cluster.pl. You can also call Sun::load from a cron line if
11 # you like to re-read it automatically. If you update it manually
12 # load/keps will load the latest version into the cluster
13 #
14 # This program is designed to be called from /etc/aliases or
15 # a .forward file so you can get yourself on the keps mailing
16 # list from AMSAT and have the keps updated automatically once
17 # a week.
18 #
19 # I will distribute the latest keps with every patch but you can
20 # get your own data from: 
21 #
22 # http://www.amsat.org/amsat/ftp/keps/current/nasa.all
23 #
24 # Please note that this will UPDATE your keps file
25
26 # Usage: 
27 #    email | convkeps.pl        (in amsat email format)  
28 #    convkeps.pl -p keps.in     (a file with just plain keps)
29
30 # Copyright (c) 2000 Dirk Koopman G1TLH
31 #
32 # $Id$
33 #
34
35 require 5.004;
36 package Sun;
37
38 # search local then perl directories
39 BEGIN {
40         # root of directory tree for this system
41         $root = "/spider"; 
42         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
43         
44         unshift @INC, "$root/perl";     # this IS the right way round!
45         unshift @INC, "$root/local";
46 }
47
48 use strict;
49 use vars qw($root %keps);
50
51 use Data::Dumper;
52 use Keps;
53
54 my $fn = "$root/local/Keps.pm";
55 my $state = 0;
56 my $name;
57 my $ref;
58 my $line;
59 my $f = \*STDIN;
60
61 while (@ARGV) {
62         my $arg = shift @ARGV;
63         if ($arg eq '-p') {
64                 $state = 1;
65         } elsif ($arg eq '-e') {
66                 $state = 0;
67         } elsif ($arg =~ /^-/) {
68                 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";
69         } else {
70                 open (IN, $arg) or die "cannot open $arg (!$)";
71                 $f = \*IN;
72         }
73 }
74 while (<$f>) {
75         ++$line;
76         chomp;
77         s/^\s+//;
78     s/\s+$//;
79         next unless $_;
80         last if m{^/EX}i;
81         last if m{^-};
82         
83         if ($state == 0 && /^TO ALL/) {
84                 $state = 1;
85         } elsif ($state == 1) {
86                 last if m{^/EX/i};
87                 
88                 if (/^\w+/) {
89                         s/\s/-/g;
90                         $name = $_;
91                         $ref = $keps{$name} = {}; 
92                         $state = 2;
93                 }
94         } elsif ($state == 2) {
95                 if (/^1 /) {
96                         my ($id, $number, $epoch, $decay, $mm2, $bstar, $elset) = unpack "xxa5xxa5xxxa15xa10xa8xa8xxxa4x", $_;
97                         $ref->{id} = $id - 0;
98                         $ref->{number} = $number - 0;
99                         $ref->{epoch} = $epoch - 0;
100                         $ref->{mm1} = $decay - 0;
101                         $ref->{mm2} = genenum($mm2);
102                         $ref->{bstar} = genenum($bstar);
103                         $ref->{elset} = $elset - 0;
104 #                       print "$id $number $epoch $decay $mm2 $bstar $elset\n"; 
105 #                       print "mm2: $ref->{mm2} bstar: $ref->{bstar}\n";
106                         
107                         $state = 3;
108                 } else {
109                         print "out of order on line $line\n";
110                         undef $ref;
111                         delete $keps{$name};
112                         $state = 1;
113                 }
114         } elsif ($state == 3) {
115                 if (/^2 /) {
116                         my ($id, $incl, $raan, $ecc, $peri, $man, $mmo, $orbit) = unpack "xxa5xa8xa8xa7xa8xa8xa11a5x", $_;
117                         $ref->{meananomaly} = $man - 0;
118                         $ref->{meanmotion} = $mmo - 0;
119                         $ref->{inclination} = $incl - 0;
120                         $ref->{eccentricity} = ".$ecc" - 0;
121                         $ref->{argperigee} = $peri - 0;
122                         $ref->{raan} = $raan - 0;
123                         $ref->{orbit} = $orbit - 0;
124                 } else {
125                         print "out of order on line $line\n";
126                         delete $keps{$name};
127                 }
128                 undef $ref;
129                 $state = 1;
130         }
131 }
132
133 my $dd = new Data::Dumper([\%keps], [qw(*keps)]);
134 $dd->Indent(1);
135 $dd->Quotekeys(0);
136 open(OUT, ">$fn") or die "$fn $!";
137 print OUT "#\n# this file is automatically produced by convkeps.pl\n#\n";
138 print OUT "# Last update: ", scalar gmtime, "\n#\n";
139 print OUT "\npackage Sun;\n\n";
140 print OUT $dd->Dumpxs;
141 print OUT "\n";
142 close(OUT);
143
144
145 # convert (+/-)00000-0 to (+/-).00000e-0
146 sub genenum
147 {
148         my ($sign, $frac, $esign, $exp) = unpack "aa5aa", shift;
149         my $n = $sign . "." . $frac . 'e' . $esign . $exp;
150         return $n - 0;
151 }
152