-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathsatutl.c
363 lines (291 loc) · 9.72 KB
/
satutl.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
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
/* > satutl.c
*
*/
#include "sgdp4h.h"
#include <ctype.h>
static char *st_start(char *buf);
static long i_read(char *str, int start, int stop);
static double d_read(char *str, int start, int stop);
// Fixed mapping
// Only capital letters and numbers are used in Alpha-5. The letters “I” and “O” are omitted to avoid confusion with the numbers “1” and “0”.
const char mapping[] = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
// Function to convert alpha5 format to number
int alpha5_to_number(const char *s)
{
// Split components
char first_char = s[0];
int digits = 0;
sscanf(s + 1, "%4d", &digits); // Get the last four digits
// Decode first character
int first_digit = 0;
if (first_char == ' ') {
first_digit = 0;
} else if (isdigit(first_char)) {
first_digit = first_char - '0';
} else {
for (int i = 0; mapping[i] != '\0'; i++) {
if (mapping[i] == first_char) {
first_digit = i;
break;
}
}
}
// Create and return number
return first_digit * 10000 + digits;
}
// Function to convert number to alpha5 format
void number_to_alpha5(int number, char *result)
{
// Split components
int first_digit = number / 10000;
int digits = number % 10000;
// Get char
char first_char = mapping[first_digit];
// Create string
sprintf(result, "%c%04d", first_char, digits);
return;
}
// Function to strip leading spaces
void strip_leading_spaces(const char *s, char *result)
{
while (*s == ' ') {
s++; // Skip leading spaces
}
strcpy(result, s); // Copy the remainder of the string
return;
}
// Function to remove trailing spaces from a string
void strip_trailing_spaces(char *s)
{
int i;
// Loop from the end of the string to find the first non-space character
for (i=strlen(s)-1;i>=0;i--)
if (s[i]!=' ')
break;
s[i+1]='\0';
}
// Function to zero pad a string
void zero_pad(const char *s, char *result)
{
char stripped[6]; // Temporarily store the string without leading spaces
strip_leading_spaces(s, stripped);
if (isdigit(stripped[0])) {
int value = atoi(stripped);
sprintf(result, "%05d", value);
} else {
strcpy(result, stripped); // No zero-padding for non-digit strings
}
}
/* ====================================================================
Read a string from key board, remove CR/LF etc.
==================================================================== */
void read_kb(char *buf)
{
int ii;
fgets(buf, ST_SIZE-1, stdin);
/* Remove the CR/LF etc. */
for(ii = 0; ii < ST_SIZE; ii++)
{
if(buf[ii] == '\r' || buf[ii] == '\n')
{
buf[ii] = '\0';
break;
}
}
}
// If current_line not lenght of 70 or doesn't start with "1 " or "2 ",copy it
// to satname, stripping a potential leading "0 ", leading whitespaces and
// trailing whitespaces and newline
void conditional_copy_satname(char *satname, char *current_line) {
if ((strlen(current_line) != 70) ||
((current_line[0] != '1' || current_line[1] != ' ') &&
(current_line[0] != '2' || current_line[1] != ' '))) {
// Name line found
// st_start will strip the leading whitespaces
if (current_line[0] == '0' && current_line[1] == ' ') {
strncpy(satname, st_start(current_line + 2), ST_SIZE);
} else {
strncpy(satname, st_start(current_line), ST_SIZE);
}
}
// Strip the trailing whitespaces and newline
for (int i = strlen(satname); i >= 0; i--) {
if (!isprint(satname[i])) {
satname[i] = '\0';
} else {
break;
}
}
return;
}
/* ====================================================================
Read orbit parameters for "satno" in file "filename", return -1 if
failed to find the corresponding data. Call with satno = 0 to get the
next elements of whatever sort.
When calling with satno != 0 and the corresponding sat doesn't have a
name in the tle file, depending on the current file position, it can
return the name of the previous satellite. This won't happen when
called with satno == 0 or through the rftles wrapper which is
currently used everywhere, there is no direct call to read_twoline.
==================================================================== */
int read_twoline(FILE *fp, long search_satno, orbit_t *orb, char *satname)
{
char tmp_satname[ST_SIZE] = "";
static char search[ST_SIZE];
static char line1[ST_SIZE];
static char line2[ST_SIZE];
char search_satstr[6];
char *st1, *st2;
int found = 0;
double bm, bx;
st1 = line1;
st2 = line2;
// alpha5 designation to search
number_to_alpha5(search_satno, search_satstr);
do {
if(fgets(line1, ST_SIZE-1, fp) == NULL) return -1;
st1 = st_start(line1);
conditional_copy_satname(tmp_satname, line1);
} while((st1[0] != '1') || (st1[1] != ' '));
if (search_satno != 0) {
sprintf(search, "1 %5s", search_satstr);
} else {
// If no search_satno given, set it to the currently read one
// so next do/while loop will find it
//search_satno = atol(st1+2);
strncpy(search_satstr, st1+2, 5);
search_satstr[5]='\0';
search_satno=alpha5_to_number(search_satstr);
strncpy(search, line1, 7);
search[7] = '\0';
}
do {
st1 = st_start(line1);
if(strncmp(st1, search, 7) == 0)
{
found = 1;
break;
}
conditional_copy_satname(tmp_satname, line1);
} while(fgets(line1, ST_SIZE-1, fp) != NULL);
search[0] = '2';
if(found)
{
fgets(line2, ST_SIZE-1, fp);
st2 = st_start(line2);
}
if(!found || strncmp(st2, search, 7) != 0)
{
return -1;
}
orb->ep_year = (int)i_read(st1, 19, 20);
if(orb->ep_year < 57) orb->ep_year += 2000;
else orb->ep_year += 1900;
orb->ep_day = d_read(st1, 21, 32);
orb->ndot2 = d_read(st1, 34, 43);
bm = d_read(st1, 45, 50) * 1.0e-5;
bx = d_read(st1, 51, 52);
orb->nddot6 = bm * pow(10.0, bx);
bm = d_read(st1, 54, 59) * 1.0e-5;
bx = d_read(st1, 60, 61);
orb->bstar = bm * pow(10.0, bx);
orb->eqinc = RAD(d_read(st2, 9, 16));
orb->ascn = RAD(d_read(st2, 18, 25));
orb->ecc = d_read(st2, 27, 33) * 1.0e-7;
orb->argp = RAD(d_read(st2, 35, 42));
orb->mnan = RAD(d_read(st2, 44, 51));
orb->rev = d_read(st2, 53, 63);
orb->norb = i_read(st2, 64, 68);
orb->satno = search_satno;
strncpy(orb->desig,st1+9,8);
orb->desig[8]='\0';
strip_trailing_spaces(orb->desig);
if (satname != NULL) {
strncpy(satname, tmp_satname, ST_SIZE);
}
return 0;
}
/* ==================================================================
Locate the first non-white space character, return location.
================================================================== */
static char *st_start(char *buf)
{
if(buf == NULL) return buf;
while(*buf != '\0' && isspace(*buf)) buf++;
return buf;
}
/* ==================================================================
Mimick the FORTRAN formatted read (assumes array starts at 1), copy
characters to buffer then convert.
================================================================== */
static long i_read(char *str, int start, int stop)
{
long itmp=0;
char *buf, *tmp;
int ii;
start--; /* 'C' arrays start at 0 */
stop--;
tmp = buf = (char *)vector(stop-start+2, sizeof(char));
for(ii = start; ii <= stop; ii++)
{
*tmp++ = str[ii]; /* Copy the characters. */
}
*tmp = '\0'; /* NUL terminate */
itmp = atol(buf); /* Convert to long integer. */
free(buf);
return itmp;
}
/* ==================================================================
Mimick the FORTRAN formatted read (assumes array starts at 1), copy
characters to buffer then convert.
================================================================== */
static double d_read(char *str, int start, int stop)
{
double dtmp=0;
char *buf, *tmp;
int ii;
start--;
stop--;
tmp = buf = (char *)vector(stop-start+2, sizeof(char));
for(ii = start; ii <= stop; ii++)
{
*tmp++ = str[ii]; /* Copy the characters. */
}
*tmp = '\0'; /* NUL terminate */
dtmp = atof(buf); /* Convert to long integer. */
free(buf);
return dtmp;
}
/* ==================================================================
Allocate and check an all-zero array of memory (storage vector).
================================================================== */
void *vector(size_t num, size_t size)
{
void *ptr;
ptr = calloc(num, size);
if(ptr == NULL)
{
fatal_error("vector: Allocation failed %u * %u", num, size);
}
return ptr;
}
/* ==================================================================
Print out orbital parameters.
================================================================== */
void print_orb(orbit_t *orb)
{
printf("# Satellite ID = %ld\n", (long)orb->satno);
printf("# Satellite designation = %s\n",orb->desig);
printf("# Epoch year = %d day = %.8f\n", orb->ep_year, orb->ep_day);
printf("# Eccentricity = %.7f\n", orb->ecc);
printf("# Equatorial inclination = %.4f deg\n", DEG(orb->eqinc));
printf("# Argument of perigee = %.4f deg\n", DEG(orb->argp));
printf("# Mean anomaly = %.4f deg\n", DEG(orb->mnan));
printf("# Right Ascension of Ascending Node = %.4f deg\n", DEG(orb->ascn));
printf("# Mean Motion (number of rev/day) = %.8f\n", orb->rev);
printf("# First derivative of mean motion = %e\n",orb->ndot2);
printf("# Second derivative of mean motion = %e\n",orb->nddot6);
printf("# BSTAR drag = %.4e\n", orb->bstar);
printf("# Orbit number = %ld\n", orb->norb);
}
/* ====================================================================== */