fixed sh/qra so that it shows the correct lat/long
[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.
12 #
13 # This program is designed to be called from /etc/aliases or
14 # a .forward file so you can get yourself on the keps mailing
15 # list from AMSAT and have the keps updated automatically once
16 # a week.
17 #
18 # I will distribute the latest keps with every patch but you can
19 # get your own data from: 
20 #
21 # http://www.amsat.org/amsat/ftp/keps/current/nasa.all
22 #
23 # Copyright (c) 2000 Dirk Koopman G1TLH
24 #
25 # $Id$
26 #
27
28 require 5.004;
29
30 # search local then perl directories
31 BEGIN {
32         # root of directory tree for this system
33         $root = "/spider"; 
34         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
35         
36         unshift @INC, "$root/perl";     # this IS the right way round!
37         unshift @INC, "$root/local";
38 }
39
40 use strict;
41 use Data::Dumper;
42
43 use vars qw($root);
44
45 my $fn = "$root/local/Keps.pm";
46 my $state = 0;
47 my $name;
48 my %keps;
49 my $ref;
50 my $line;
51
52 while (<STDIN>) {
53         ++$line;
54         chomp;
55         s/^\s+//;
56     s/\s+$//;
57         next unless $_;
58         last if m{^/EX}i;
59         last if m{^-};
60         
61         if ($state == 0 && /^TO ALL/) {
62                 $state = 1;
63         } elsif ($state == 1) {
64                 last if m{^/EX/i};
65                 
66                 if (/^\w+/) {
67                         s/\s/-/g;
68                         $name = $_;
69                         $ref = $keps{$name} = {}; 
70                         $state = 2;
71                 }
72         } elsif ($state == 2) {
73                 if (/^1 /) {
74                         my ($id, $number, $epoch, $decay, $mm2, $bstar, $elset) = unpack "xxa5xxa5xxxa15xa10xa8xa8xxxa4x", $_;
75                         $ref->{id} = $id - 0;
76                         $ref->{number} = $number - 0;
77                         $ref->{epoch} = $epoch - 0;
78                         $ref->{mm1} = $decay - 0;
79                         $ref->{mm2} = genenum($mm2);
80                         $ref->{bstar} = genenum($bstar);
81                         $ref->{elset} = $elset - 0;
82 #                       print "$id $number $epoch $decay $mm2 $bstar $elset\n"; 
83 #                       print "mm2: $ref->{mm2} bstar: $ref->{bstar}\n";
84                         
85                         $state = 3;
86                 } else {
87                         print "out of order on line $line\n";
88                         undef $ref;
89                         delete $keps{$name};
90                         $state = 1;
91                 }
92         } elsif ($state == 3) {
93                 if (/^2 /) {
94                         my ($id, $incl, $raan, $ecc, $peri, $man, $mmo, $orbit) = unpack "xxa5xa8xa8xa7xa8xa8xa11a5x", $_;
95                         $ref->{meananomaly} = $man - 0;
96                         $ref->{meanmotion} = $mmo - 0;
97                         $ref->{inclination} = $incl - 0;
98                         $ref->{eccentricity} = ".$ecc" - 0;
99                         $ref->{argperigee} = $peri - 0;
100                         $ref->{raan} = $raan - 0;
101                         $ref->{orbit} = $orbit - 0;
102                 } else {
103                         print "out of order on line $line\n";
104                         delete $keps{$name};
105                 }
106                 undef $ref;
107                 $state = 1;
108         }
109 }
110
111 my $dd = new Data::Dumper([\%keps], [qw(*keps)]);
112 $dd->Indent(1);
113 $dd->Quotekeys(0);
114 open(OUT, ">$fn") or die "$fn $!";
115 print OUT "#\n# this file is automatically produced by convkeps.pl\n#\n";
116 print OUT "\npackage Sun;\n\n";
117 print OUT $dd->Dumpxs;
118 print OUT "\n";
119 close(OUT);
120
121
122 # convert (+/-)00000-0 to (+/-).00000e-0
123 sub genenum
124 {
125         my ($sign, $frac, $esign, $exp) = unpack "aa5aa", shift;
126         my $n = $sign . "." . $frac . 'e' . $esign . $exp;
127         return $n - 0;
128 }
129