forked from cbassa/sattools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
satutl.c
227 lines (177 loc) · 5.86 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
/* > 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);
/* ====================================================================
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;
}
}
}
/* ====================================================================
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.
==================================================================== */
int read_twoline(FILE *fp, long search_satno, orbit_t *orb)
{
static char search[ST_SIZE];
static char line1[ST_SIZE];
static char line2[ST_SIZE];
char *st1, *st2;
int found;
double bm, bx;
// Set defaults
strcpy(orb->desig,"");
st1 = line1;
st2 = line2;
do {
if(fgets(line1, ST_SIZE-1, fp) == NULL)
return -1;
st1 = st_start(line1);
} while(st1[0] != '1');
if(search_satno > 0) {
found = 0;
} else {
found = 1;
search_satno = atol(st1+2);
}
sprintf(search, "1 %05ld", search_satno);
do {
st1 = st_start(line1);
if(strncmp(st1, search, 7) == 0) {
found = 1;
break;
}
} while(fgets(line1, ST_SIZE-1, fp) != NULL);
sprintf(search, "2 %05ld", search_satno);
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;
// sscanf(st1+9,"%s",orb->desig);
strncpy(orb->desig,st1+9,8);
orb->desig[8]='\0';
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);
}
/* ====================================================================== */