forked from cbassa/sattools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
posvel.c
209 lines (160 loc) · 3.59 KB
/
posvel.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "cpgplot.h"
#include "sgdp4h.h"
#include "satutl.h"
#include <getopt.h>
#define LIM 128
#define XKE 0.07436680 // Guassian Gravitational Constant
#define XKMPER 6378.135
#define AE 1.0
#define XMNPDA 1440.0
#define R2D 180.0/M_PI
#define D2R M_PI/180.0
extern double SGDP4_jd0;
// 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;
}
// 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;
}
// Present nfd
void nfd_now(char *s)
{
time_t rawtime;
struct tm *ptm;
// Get UTC time
time(&rawtime);
ptm=gmtime(&rawtime);
sprintf(s,"%04d-%02d-%02dT%02d:%02d:%02d",ptm->tm_year+1900,ptm->tm_mon+1,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
return;
}
void usage(void)
{
printf("propagate c:i:t:m:\n\nPropagates orbital elements to a new epoch using the SGP4/SDP4 model.\nDefault operation propagates classfd.tle to now,\n\n-c input catalog\n-i Satellite number\n-t New epoch (YYYY-MM-DDTHH:MM:SS)\n-m New epoch (MJD)\n");
return;
}
int main(int argc,char *argv[])
{
int imode,satno=0,arg,i;
FILE *file;
orbit_t orb;
xyz_t r,v;
char tlefile[LIM],nfd[32];
char *env;
double mjd;
int length=3600,dl=60;
// Get environment variable
env=getenv("ST_TLEDIR");
sprintf(tlefile,"%s/classfd.tle",env);
// Set date
nfd_now(nfd);
mjd=nfd2mjd(nfd);
// Decode options
while ((arg=getopt(argc,argv,"c:i:t:l:d:"))!=-1) {
switch (arg) {
case 't':
strcpy(nfd,optarg);
mjd=nfd2mjd(nfd);
break;
case 'c':
strcpy(tlefile,optarg);
break;
case 'i':
satno=atoi(optarg);
break;
case 'l':
length=atoi(optarg);
break;
case 'd':
dl=atoi(optarg);
break;
case 'h':
usage();
return 0;
break;
default:
usage();
return 0;
}
}
// Reloop stderr
freopen("/tmp/stderr.txt","w",stderr);
// Open file
file=fopen(tlefile,"r");
while (read_twoline(file,satno,&orb)==0) {
// Propagate
imode=init_sgdp4(&orb);
for (i=0;i<length;i+=dl) {
satpos_xyz(mjd+2400000.5+(double) i/86400.0,&r,&v);
printf("%f %f %f %f %f %f\n",r.x,r.y,r.z,v.x,v.y,v.z);
}
}
fclose(file);
return 0;
}