forked from cbassa/sattools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunchtle.c
279 lines (217 loc) · 5.38 KB
/
launchtle.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <getopt.h>
#include "sgdp4h.h"
#include "satutl.h"
#define LIM 128
#define XKMPER 6378.135 /* Km per earth radii */
#define XMNPDA 1440.0 /* Minutes per day */
#define AE 1.0 /* Earth radius in "chosen units". */
#define XKE 0.743669161e-1
#define CK2 5.413080e-4 /* (0.5 * XJ2 * AE * AE) */
extern double SGDP4_jd0;
// Compute Julian Day from Date
double date2mjd(int year,int month,double day)
{
int a,b;
double jd;
if (month<3) {
year--;
month+=12;
}
a=floor(year/100.);
b=2.-a+floor(a/4.);
if (year<1582) b=0;
if (year==1582 && month<10) b=0;
if (year==1582 && month==10 && day<=4) b=0;
jd=floor(365.25*(year+4716))+floor(30.6001*(month+1))+day+b-1524.5;
return jd-2400000.5;
}
// nfd2mjd
double nfd2mjd(char *date)
{
int year,month,day,hour,min,sec;
double mjd,dday;
sscanf(date,"%04d-%02d-%02dT%02d:%02d:%02d",&year,&month,&day,&hour,&min,&sec);
dday=day+hour/24.0+min/1440.0+sec/86400.0;
mjd=date2mjd(year,month,dday);
return mjd;
}
// Return x modulo y [0,y)
double modulo(double x,double y)
{
x=fmod(x,y);
if (x<0.0) x+=y;
return x;
}
// DOY to MJD
double doy2mjd(int year,double doy)
{
int month,k=2;
double day;
if (year%4==0 && year%400!=0)
k=1;
month=floor(9.0*(k+doy)/275.0+0.98);
if (doy<32.0)
month=1;
day=doy-floor(275.0*month/9.0)+k*floor((month+9.0)/12.0)+30.0;
return date2mjd(year,month,day);
}
// Greenwich Mean Sidereal Time
double gmst(double mjd)
{
double t,gmst;
t=(mjd-51544.5)/36525.0;
gmst=modulo(280.46061837+360.98564736629*(mjd-51544.5)+t*t*(0.000387933-t/38710000),360.0);
return gmst;
}
// Compute Date from Julian Day
void mjd2date(double mjd,int *year,int *month,double *day)
{
double f,jd;
int z,alpha,a,b,c,d,e;
jd=mjd+2400000.5;
jd+=0.5;
z=floor(jd);
f=fmod(jd,1.);
if (z<2299161)
a=z;
else {
alpha=floor((z-1867216.25)/36524.25);
a=z+1+alpha-floor(alpha/4.);
}
b=a+1524;
c=floor((b-122.1)/365.25);
d=floor(365.25*c);
e=floor((b-d)/30.6001);
*day=b-d-floor(30.6001*e)+f;
if (e<14)
*month=e-1;
else
*month=e-13;
if (*month>2)
*year=c-4716;
else
*year=c-4715;
return;
}
// MJD to DOY
double mjd2doy(double mjd,int *yr)
{
int year,month,k=2;
double day,doy;
mjd2date(mjd,&year,&month,&day);
if (year%4==0 && year%400!=0)
k=1;
doy=floor(275.0*month/9.0)-k*floor((month+9.0)/12.0)+day-30;
*yr=year;
return doy;
}
// Format TLE
void format_tle(orbit_t orb,char *line1,char *line2)
{
int i,csum;
char sbstar[]=" 00000-0",bstar[13];
// Format Bstar term
if (fabs(orb.bstar)>1e-9) {
sprintf(bstar,"%11.4e",10*orb.bstar);
sbstar[0] = bstar[0]; sbstar[1] = bstar[1]; sbstar[2] = bstar[3]; sbstar[3] = bstar[4];
sbstar[4] = bstar[5]; sbstar[5] = bstar[6]; sbstar[6] = bstar[8]; sbstar[7] = bstar[10]; sbstar[8] = '\0';
}
// Print lines
sprintf(line1,"1 %05dU %-8s %2d%012.8f .00000000 00000-0 %8s 0 0",orb.satno,orb.desig,orb.ep_year-2000,orb.ep_day,sbstar);
sprintf(line2,"2 %05d %8.4f %8.4f %07.0f %8.4f %8.4f %11.8f 0",orb.satno,DEG(orb.eqinc),DEG(orb.ascn),1E7*orb.ecc,DEG(orb.argp),DEG(orb.mnan),orb.rev);
// Compute checksums
for (i=0,csum=0;i<strlen(line1);i++) {
if (isdigit(line1[i]))
csum+=line1[i]-'0';
else if (line1[i]=='-')
csum++;
}
sprintf(line1,"%s%d",line1,csum%10);
for (i=0,csum=0;i<strlen(line2);i++) {
if (isdigit(line2[i]))
csum+=line2[i]-'0';
else if (line2[i]=='-')
csum++;
}
sprintf(line2,"%s%d",line2,csum%10);
return;
}
void usage(void)
{
printf("launch tle c:i:t:T:I:d:\n\n");
printf("-c reference tle\n-i reference satno\n-t reference launch time\n-T launch time\n-I output satno\n-d output desig\n");
return;
}
int main(int argc,char *argv[])
{
int arg=0,satno=0,satno1=84001;
char tlefile[LIM];
char nfd0[32],nfd1[32];
char line1[70],line2[70];
FILE *file;
orbit_t orb;
double mjd0,mjd1,h0,h1,dmjd,dh;
char *env;
char desig[]="14900A";
env=getenv("ST_TLEDIR");
sprintf(tlefile,"%s/classfd.tle",env);
// Decode options
while ((arg=getopt(argc,argv,"c:i:t:T:I:d:"))!=-1) {
switch (arg) {
case 'c':
strcpy(tlefile,optarg);
break;
case 't':
strcpy(nfd0,optarg);
mjd0=nfd2mjd(nfd0);
break;
case 'T':
strcpy(nfd1,optarg);
mjd1=nfd2mjd(nfd1);
break;
case 'i':
satno=atoi(optarg);
break;
case 'I':
satno1=atoi(optarg);
break;
case 'd':
strcpy(desig,optarg);
break;
case 'h':
usage();
return 0;
break;
default:
usage();
return 0;
}
}
// Open file
file=fopen(tlefile,"rb");
if (file==NULL)
fatal_error("File open failed for reading \"%s\"",tlefile);
// Find elements
read_twoline(file,satno,&orb);
fclose(file);
// Difference between epoch and launch time
dmjd=doy2mjd(orb.ep_year,orb.ep_day)-mjd0;
// Difference in RAAN
orb.ascn=RAD(modulo(gmst(mjd1)-gmst(mjd0)+DEG(orb.ascn),360.0));
// New epoch
mjd0=mjd1+dmjd;
// Format epoch
orb.ep_day=mjd2doy(mjd0,&orb.ep_year);
// Update desig
strcpy(orb.desig,desig);
orb.satno=satno1;
// Print output
format_tle(orb,line1,line2);
printf("%s\n%s\n",line1,line2);
return 0;
}