-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_tle.c
200 lines (172 loc) · 6.07 KB
/
parse_tle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "sgp4.h"
#define PI 3.141592653589793238462643383279502884197
#define TWOPI (2. * PI)
#define MINUTES_PER_DAY 1440.
#define MINUTES_PER_DAY_SQUARED (MINUTES_PER_DAY * MINUTES_PER_DAY)
#define MINUTES_PER_DAY_CUBED (MINUTES_PER_DAY * MINUTES_PER_DAY_SQUARED)
#define AE 1.0
/* distance units, earth radii */
/* TLEs have four angles on line 2, given in the form DDD.DDDD. This
can be parsed more quickly as an integer, then cast to double and
converted to radians, all in one step. */
static long get_angle( const char *buff)
{
long rval = 0;
while( *buff == ' ')
buff++;
while( *buff != ' ')
{
if( *buff != '.')
rval = rval * 10 + (long)( *buff - '0');
buff++;
}
return( rval);
}
/* Converts the quasi scientific notation of the "Motion Dot Dot/6" or
"BSTAR" field to double. The input will always be of the form
sdddddSe
....where s is blank or + or -; ddddd is a five-digit mantissa;
S is + or - or blank; and e is a single-digit exponent. A decimal
point is assumed before the five-digit mantissa. */
static double sci( const char *string)
{
double rval = 0.;
if( string[1] != ' ')
{
const long ival = atol( string);
if( ival)
{
rval = (double)ival * 1.e-5;
if( string[7] != '0')
{
long exponent = string[7] - '0';
if( string[6] == '-')
while( exponent--)
rval *= .1;
else
while( exponent--)
rval *= 10.;
}
}
}
return( rval);
}
/* Does a checksum modulo 10 on the given line. Digits = their
value, '-' = 1, all other chars = 0. Returns 0 if ok, a negative
value if it's definitely not a TLE line, positive if it's all OK
except the checksum. This last was added because people sometimes
want to use TLEs without worrying about the checksum. */
int tle_checksum( const char *buff)
{
int rval = 0;
int count = 69;
if( (*buff != '1' && *buff != '2') || buff[1] != ' ')
return( -1);
while( --count)
{
if( *buff > '0' && *buff <= '9')
rval += *buff - '0';
else if( *buff == '-')
rval++;
if( *buff < ' ' || *buff > 'z') /* invalid character */
return( -2);
buff++;
}
rval -= *buff++ - '0';
if( *buff > ' ') /* line unterminated */
rval = -3;
else
{
rval %= 10;
if( rval < 0)
rval += 10;
}
return( rval % 10);
}
static double get_eight_places( const char *ptr)
{
return( (double)atoi( ptr) + (double)atol(ptr + 4) * 1e-8);
}
/* Meteor 2-08 */
/* 1 13113U 88245.60005115 0.00000076 63463-4 0 5998 */
/* 2 13113 82.5386 288.0994 0015973 147.1294 213.0868 13.83869004325321 */
#define J2000 2451545.5
#define J1900 (J2000 - 36525. - 1.)
/* parse_elements returns:
0 if the elements are parsed without error;
1 if they're OK except the first line has a checksum error;
2 if they're OK except the second line has a checksum error;
3 if they're OK except both lines have checksum errors;
a negative value if the lines aren't at all parseable */
int parse_elements( const char *line1, const char *line2, tle_t *sat)
{
int rval, checksum_problem = 0;
if( *line1 != '1' || *line2 != '2')
rval = -4;
else
{
rval = tle_checksum( line1);
if( rval > 0)
{
checksum_problem = 1; /* there's a checksum problem, but it's */
rval = 0; /* not fatal; continue processing the TLE */
}
}
if( rval)
rval -= 100;
else
{
rval = tle_checksum( line2);
if( rval > 0)
{
checksum_problem |= 2; /* there's a checksum problem, but it's */
rval = 0; /* not fatal; continue processing the TLE */
}
}
if( !rval)
{
char tbuff[13];
long year = line1[19] - '0';
if( line1[18] >= '0')
year += (line1[18] - '0') * 10;
if( year < 57) /* cycle around Y2K */
year += 100;
sat->epoch = get_eight_places( line1 + 20) + J1900
+ (double)( year * 365 + (year - 1) / 4);
sat->norad_number = atoi( line1 + 2);
memcpy( tbuff, line1 + 64, 4);
tbuff[4] = '\0';
sat->bulletin_number = atoi( tbuff);
sat->classification = line1[7]; /* almost always 'U' */
memcpy( sat->intl_desig, line1 + 9, 8);
sat->intl_desig[8] = '\0';
memcpy( tbuff, line2 + 63, 5);
tbuff[5] = '\0';
sat->revolution_number = atol( tbuff);
sat->ephemeris_type = line1[62];
sat->xmo = (double)get_angle( line2 + 43) * (PI / 180e+4);
sat->xnodeo = (double)get_angle( line2 + 17) * (PI / 180e+4);
sat->omegao = (double)get_angle( line2 + 34) * (PI / 180e+4);
sat->xincl = (double)get_angle( line2 + 8) * (PI / 180e+4);
sat->eo = atol( line2 + 26) * 1.e-7;
/* Make sure mean motion is null-terminated, since rev. no.
may immediately follow. */
memcpy( tbuff, line2 + 51, 12);
tbuff[12] = '\0';
/* Input mean motion, derivative of mean motion and second */
/* deriv of mean motion, are all in revolutions and days. */
/* Convert them here to radians and minutes: */
sat->xno = get_eight_places( tbuff) * TWOPI / MINUTES_PER_DAY;
sat->xndt2o = (double)atol( line1 + 35)
* 1.e-8 * TWOPI / MINUTES_PER_DAY_SQUARED;
if( line1[33] == '-')
sat->xndt2o *= -1.;
sat->xndd6o = sci( line1 + 44) * TWOPI / MINUTES_PER_DAY_CUBED;
sat->bstar = sci( line1 + 53) * AE;
}
return( rval ? rval : checksum_problem);
}