sgp_in.c 9.86 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
  Gpredict: Real-time satellite tracking and orbit prediction program

  This unit is:

  Copyright (C)  1992-1999  Dr TS Kelso.
  Copyright (C)  2001 by N. Kyriazis
  Copyright (C)  2006-2008 Alexandru Csete, OZ9AEC.


  Comments, questions and bugreports should be submitted via
  http://sourceforge.net/projects/gpredict/
  More details can be found at the project home page:

  http://gpredict.oz9aec.net/
 
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, visit http://www.fsf.org/
*/
/* original header:               */
/* Unit SGP_In                    */
/*           Author:  Dr TS Kelso */
/* Original Version:  1992 Jun 25 */
/* Current Revision:  1999 Nov 27 */
/*          Version:  2.10 */
/*        Copyright:  1992-1999, All Rights Reserved */

/* Ported to C by N. Kyriazis  April 6  2001 */

#include "sgp4sdp4.h"

/* Calculates the checksum mod 10 of a line from a TLE set and */
/* returns 1 if it compares with checksum in column 68, else 0.*/
/* tle_set is a character string holding the two lines read    */
/* from a text file containing NASA format Keplerian elements. */
/* NOTE!!! The stuff about two lines is not quite true.
   The function assumes that tle_set[0] is the begining
   of the line and that there are 68 elements - see the consumer
*/
int
Checksum_Good (char *tle_set)
{
	int i, check_digit, value, checksum = 0;

	for (i = 0; i < 68; i++) {
		if ( (tle_set[i] >= '0') && (tle_set[i] <= '9') )
			value = tle_set[i] - '0';
		else if ( tle_set[i] == '-' )
			value = 1;
		else
			value = 0;
		
		checksum += value;
	} /* End for(i = 0; i < 68; i++) */

	checksum %= 10;
	check_digit = tle_set[68] - '0';

	return (checksum == check_digit);
} /* Function Checksums_Good */

/*------------------------------------------------------------------*/

/* Carries out various checks on a TLE set to verify its validity */
/* tle_set is a character string holding the two lines read    */
/* from a text file containing NASA format Keplerian elements. */
int
Good_Elements( char *tle_set )
{
	/* Verify checksum of both lines of a TLE set */
	if ( !Checksum_Good(&tle_set[0]) || !Checksum_Good(&tle_set[69]) )
		return (0);
	/* Check the line number of each line */
	if ( (tle_set[0] != '1') || (tle_set[69] != '2') )
		return (0);
	/* Verify that Satellite Number is same in both lines */
	if ( strncmp( &tle_set[2], &tle_set[71], 5 ) != 0 )
		return (0);
	/* Check that various elements are in the right place */
	if ( 
		(tle_set[ 23] != '.') ||
		(tle_set[ 34] != '.') ||
		(tle_set[ 80] != '.') ||
		(tle_set[ 89] != '.') ||
		(tle_set[106] != '.') ||
		(tle_set[115] != '.') ||
		(tle_set[123] != '.') ||
		(strncmp(&tle_set[61], " 0 ", 3) != 0)
		)
		return (0);

	return (1);
}  /* Function Good_Elements */

/*------------------------------------------------------------------*/

/* Converts the strings in a raw two-line element set  */
/* to their intended numerical values. No processing   */
/* of these values is done, e.g. from deg to rads etc. */
/* This is done in the select_ephemeris() function.    */
void
Convert_Satellite_Data (char *tle_set, tle_t *tle)
{ 
	char buff[15];

	/** Decode Card 1 **/
	/* Satellite's catalogue number */
	strncpy (buff, &tle_set[2], 5);
	buff[5] = '\0';
	tle->catnr = atoi (buff);

	/* International Designator for satellite */
	strncpy (tle->idesg, &tle_set[9], 8);
	tle->idesg[8] = '\0';

	/* Epoch time; this is the complete,
	   unconverted epoch.
	*/
	strncpy (buff, &tle_set[18], 14);
	buff[14] = '\0';
	//        tle->epoch = atof (buff);
	tle->epoch = strtod (buff, NULL);

	/* Now, convert the epoch time into year, day
	   and fraction of day, according to:

	   YYDDD.FFFFFFFF
	*/

	/* Epoch year; we assume > 2000 ... */
	strncpy (buff, &tle_set[18], 2);
	buff[2] = '\0';
	tle->epoch_year = 2000 + atoi (buff);

	/* Epoch day */
	strncpy (buff, &tle_set[20], 3);
	buff[3] = '\0';
	tle->epoch_day = atoi (buff);

	/* Epoch fraction of day */
	buff[0] = '0';
	strncpy (&buff[1], &tle_set[23], 9);
	buff[10] = '\0';
	tle->epoch_fod = strtod (buff, NULL);


	/* Satellite's First Time Derivative */
	strncpy (buff, &tle_set[33], 10);
	buff[10] = '\0';
	tle->xndt2o = strtod (buff, NULL);

	/* Satellite's Second Time Derivative */
	strncpy (buff, &tle_set[44], 1);
	buff[1] = '.';
	strncpy (&buff[2], &tle_set[45], 5);
	buff[7] = 'E';
	strncpy (&buff[8], &tle_set[50], 2);
	buff[10] = '\0';
	tle->xndd6o = strtod (buff, NULL);

	/* Satellite's bstar drag term
	   FIXME: How about buff[0] ????
	*/
	strncpy (buff, &tle_set[53], 1);
	buff[1] = '.';
	strncpy (&buff[2], &tle_set[54], 5);
	buff[7] = 'E';
	strncpy (&buff[8], &tle_set[59], 2);
	buff[10] = '\0';
	tle->bstar = strtod (buff, NULL);

	/* Element Number */
	strncpy (buff, &tle_set[64], 4);
	buff[4] = '\0';
	tle->elset = atoi (buff);

	for (; tle_set[69] != '2' && tle_set[69] != 0; tle_set++);
	
	/** Decode Card 2 **/
	/* Satellite's Orbital Inclination (degrees) */
	strncpy (buff, &tle_set[77], 8);
	buff[8] = '\0';
	tle->xincl = strtod (buff, NULL);


	/* Satellite's RAAN (degrees) */
	strncpy (buff, &tle_set[86], 8);
	buff[8] = '\0';
	tle->xnodeo = strtod (buff, NULL);

	/* Satellite's Orbital Eccentricity */
	buff[0] = '.';
	strncpy (&buff[1], &tle_set[95], 7);
	buff[8] = '\0';
	tle->eo = strtod (buff, NULL);

	/* Satellite's Argument of Perigee (degrees) */
	strncpy (buff, &tle_set[103], 8);
	buff[8] = '\0';
	tle->omegao = strtod (buff, NULL);

	/* Satellite's Mean Anomaly of Orbit (degrees) */
	strncpy (buff, &tle_set[112], 8);
	buff[8] = '\0';
	tle->xmo = strtod (buff, NULL);

	/* Satellite's Mean Motion (rev/day) */
	strncpy (buff, &tle_set[121], 10);
	buff[10] = '\0';
	tle->xno = strtod (buff, NULL);

	/* Satellite's Revolution number at epoch */
	strncpy (buff, &tle_set[132], 5);
	buff[5] = '\0';
	tle->revnum = strtod (buff, NULL);

} /* Procedure Convert_Satellite_Data */

/*------------------------------------------------------------------*/

int
Get_Next_Tle_Set (char *line[3], tle_t *tle)
{
	int idx,  /* Index for loops and arrays    */
		chr;  /* Used for inputting characters */

	char tle_set[139]; /* Two lines of a TLE set */

	
	/* set status to unknown by sefault */
	tle->status = OP_STAT_UNKNOWN;

	/* Read the satellite's name */
	for (idx = 0 ; idx < 25; idx++) {

		chr = line[0][idx];

		if ( (chr != CR) && (chr != LF) && (chr != '\0')) {

			/* avoid escaping & */
			if (chr == '&') {
				tle->sat_name[idx] = '/';
				continue;
			}

			/* check for operational status encoded in name */
			if ((idx < 23) && (chr == '[') && (line[0][idx+2] == ']') &&
				((line[0][idx+1] == '+') ||
				 (line[0][idx+1] == '-') ||
				 (line[0][idx+1] == 'P') ||
				 (line[0][idx+1] == 'B') ||
				 (line[0][idx+1] == 'S') ||
				 (line[0][idx+1] == 'X'))) {

				/* get operational status */
				switch (line[0][idx+1]) {

				case '+':
					tle->status = OP_STAT_OPERATIONAL;
					break;

				case '-':
					tle->status = OP_STAT_NONOP;
					break;

				case 'P':
					tle->status = OP_STAT_PARTIAL;
					break;

				case 'B':
					tle->status = OP_STAT_STDBY;
					break;

				case 'S':
					tle->status = OP_STAT_SPARE;
					break;

				case 'X':
					tle->status = OP_STAT_EXTENDED;
					break;

				default:
					tle->status = OP_STAT_UNKNOWN;
					break;
				}


				/* terminate sat_name string */
				tle->sat_name[idx] = '\0';

				/* force loop exit */
				idx = 25;

			}
			else {
				/* no operational status in name, go on */
				tle->sat_name[idx] = chr;
			}
		}
		else {
			/* strip off trailing spaces */
			while ((chr = line[0][--idx]) == ' ');
			tle->sat_name[++idx] = '\0';
			break;
		}
	}

	/* Read in first line of TLE set */
	strncpy (tle_set, line[1], 70);
  
	/* Read in second line of TLE set and terminate string */
	strncpy (&tle_set[69], line[2], 70);
	tle_set[138] = '\0';
  
	/* Check TLE set and abort if not valid */
	if(  !Good_Elements(tle_set) )
		return (-2);

	/* Convert the TLE set to orbital elements */
	Convert_Satellite_Data ( tle_set, tle );

	return (1);
}

/*------------------------------------------------------------------*/

/* Selects the apropriate ephemeris type to be used */
/* for predictions according to the data in the TLE */
/* It also processes values in the tle set so that  */
/* they are apropriate for the sgp4/sdp4 routines   */
void
select_ephemeris (sat_t *sat)
{


	double ao,xnodp,dd1,dd2,delo,temp,a1,del1,r1;

	/* Preprocess tle set */
	sat->tle.xnodeo *= de2ra;
	sat->tle.omegao *= de2ra;
	sat->tle.xmo *= de2ra;
	sat->tle.xincl *= de2ra;
	temp = twopi/xmnpda/xmnpda;

	/* store mean motion before conversion */
	sat->meanmo = sat->tle.xno; 
	sat->tle.xno = sat->tle.xno * temp * xmnpda;
	sat->tle.xndt2o *= temp;
	sat->tle.xndd6o = sat->tle.xndd6o * temp / xmnpda;
	sat->tle.bstar /= ae;

	/* Period > 225 minutes is deep space */
	dd1 = (xke / sat->tle.xno);
	dd2 = tothrd;
	a1 = pow (dd1, dd2);
	r1 = cos (sat->tle.xincl);
	dd1 = (1.0 - sat->tle.eo * sat->tle.eo);
	temp = ck2 * 1.5f * (r1*r1 * 3.0 - 1.0) / pow (dd1, 1.5);
	del1 = temp / (a1*a1);
	ao = a1 * (1.0 - del1 * (tothrd * 0.5 + del1 *
							 (del1 * 1.654320987654321 + 1.0)));
	delo = temp / (ao*ao);
	xnodp = sat->tle.xno / (delo + 1.0);

	/* Select a deep-space/near-earth ephemeris */
	if (twopi/xnodp/xmnpda >= .15625)
		sat->flags |= DEEP_SPACE_EPHEM_FLAG;
	else
		sat->flags &= ~DEEP_SPACE_EPHEM_FLAG;

	return;
} /* End of select_ephemeris() */

/*------------------------------------------------------------------*/