forked from kd2bd/predict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.c
6677 lines (5262 loc) · 154 KB
/
predict.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************\
* PREDICT: A satellite tracking/orbital prediction program *
* Project started 26-May-1991 by John A. Magliacane, KD2BD *
* Last update: 04-May-2018 *
*****************************************************************************
* *
* 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 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. *
* *
*****************************************************************************
* See the "CREDITS" file for the names of those who have *
* generously contributed their time, talent, and effort to this project. *
\***************************************************************************/
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include "predict.h"
/* Constants used by SGP4/SDP4 code */
#define km2mi 0.621371 /* km to miles */
#define deg2rad 1.745329251994330E-2 /* Degrees to radians */
#define pi 3.14159265358979323846 /* Pi */
#define pio2 1.57079632679489656 /* Pi/2 */
#define x3pio2 4.71238898038468967 /* 3*Pi/2 */
#define twopi 6.28318530717958623 /* 2*Pi */
#define e6a 1.0E-6
#define tothrd 6.6666666666666666E-1 /* 2/3 */
#define xj2 1.0826158E-3 /* J2 Harmonic (WGS '72) */
#define xj3 -2.53881E-6 /* J3 Harmonic (WGS '72) */
#define xj4 -1.65597E-6 /* J4 Harmonic (WGS '72) */
#define xke 7.43669161E-2
#define xkmper 6.378137E3 /* WGS 84 Earth radius km */
#define xmnpda 1.44E3 /* Minutes per day */
#define ae 1.0
#define ck2 5.413079E-4
#define ck4 6.209887E-7
#define f 3.35281066474748E-3 /* Flattening factor */
#define ge 3.986008E5 /* Earth gravitational constant (WGS '72) */
#define s 1.012229
#define qoms2t 1.880279E-09
#define secday 8.6400E4 /* Seconds per day */
#define omega_E 1.00273790934 /* Earth rotations/siderial day */
#define omega_ER 6.3003879 /* Earth rotations, rads/siderial day */
#define zns 1.19459E-5
#define c1ss 2.9864797E-6
#define zes 1.675E-2
#define znl 1.5835218E-4
#define c1l 4.7968065E-7
#define zel 5.490E-2
#define zcosis 9.1744867E-1
#define zsinis 3.9785416E-1
#define zsings -9.8088458E-1
#define zcosgs 1.945905E-1
#define zcoshs 1
#define zsinhs 0
#define q22 1.7891679E-6
#define q31 2.1460748E-6
#define q33 2.2123015E-7
#define g22 5.7686396
#define g32 9.5240898E-1
#define g44 1.8014998
#define g52 1.0508330
#define g54 4.4108898
#define root22 1.7891679E-6
#define root32 3.7393792E-7
#define root44 7.3636953E-9
#define root52 1.1428639E-7
#define root54 2.1765803E-9
#define thdt 4.3752691E-3
#define rho 1.5696615E-1
#define mfactor 7.292115E-5
#define sr 6.96000E5 /* Solar radius - km (IAU 76) */
#define AU 1.49597870691E8 /* Astronomical unit - km (IAU 76) */
/* Entry points of Deep() */
#define dpinit 1 /* Deep-space initialization code */
#define dpsec 2 /* Deep-space secular code */
#define dpper 3 /* Deep-space periodic code */
/* Flow control flag definitions */
#define ALL_FLAGS -1
#define SGP_INITIALIZED_FLAG 0x000001 /* not used */
#define SGP4_INITIALIZED_FLAG 0x000002
#define SDP4_INITIALIZED_FLAG 0x000004
#define SGP8_INITIALIZED_FLAG 0x000008 /* not used */
#define SDP8_INITIALIZED_FLAG 0x000010 /* not used */
#define SIMPLE_FLAG 0x000020
#define DEEP_SPACE_EPHEM_FLAG 0x000040
#define LUNAR_TERMS_DONE_FLAG 0x000080
#define NEW_EPHEMERIS_FLAG 0x000100 /* not used */
#define DO_LOOP_FLAG 0x000200
#define RESONANCE_FLAG 0x000400
#define SYNCHRONOUS_FLAG 0x000800
#define EPOCH_RESTART_FLAG 0x001000
#define VISIBLE_FLAG 0x002000
#define SAT_ECLIPSED_FLAG 0x004000
struct { char line1[70];
char line2[70];
char name[25];
long catnum;
long setnum;
char designator[10];
int year;
double refepoch;
double incl;
double raan;
double eccn;
double argper;
double meanan;
double meanmo;
double drag;
double nddot6;
double bstar;
long orbitnum;
} sat[24];
struct { char callsign[17];
double stnlat;
double stnlong;
int stnalt;
} qth;
struct { char name[25];
long catnum;
char squintflag;
double alat;
double alon;
unsigned char transponders;
char transponder_name[10][80];
double uplink_start[10];
double uplink_end[10];
double downlink_start[10];
double downlink_end[10];
unsigned char dayofweek[10];
int phase_start[10];
int phase_end[10];
} sat_db[24];
/* Global variables for sharing data among functions... */
double tsince, jul_epoch, jul_utc, eclipse_depth=0,
sat_azi, sat_ele, sat_range, sat_range_rate,
sat_lat, sat_lon, sat_alt, sat_vel, phase,
sun_azi, sun_ele, daynum, fm, fk, age, aostime,
lostime, ax, ay, az, rx, ry, rz, squint, alat, alon,
sun_ra, sun_dec, sun_lat, sun_lon, sun_range, sun_range_rate,
moon_az, moon_el, moon_dx, moon_ra, moon_dec, moon_gha, moon_dv;
char qthfile[50], tlefile[50], dbfile[50], temp[80], output[25],
serial_port[15], resave=0, reload_tle=0, netport[7],
once_per_second=0, ephem[5], sat_sun_status, findsun,
calc_squint, database=0, xterm, io_lat='N', io_lon='W';
int indx, antfd, iaz, iel, ma256, isplat, isplong, socket_flag=0,
Flags=0;
long rv, irk;
unsigned char val[256];
/* The following variables are used by the socket server. They
are updated in the MultiTrack() and SingleTrack() functions. */
char visibility_array[24], tracking_mode[30];
float az_array[24], el_array[24], long_array[24], lat_array[24],
footprint_array[24], range_array[24], altitude_array[24],
velocity_array[24], eclipse_depth_array[24], phase_array[24],
squint_array[24];
double doppler[24], nextevent[24];
long aos_array[24], orbitnum_array[24];
unsigned short portbase=0;
/** Type definitions **/
/* Two-line-element satellite orbital data
structure used directly by the SGP4/SDP4 code. */
typedef struct {
double epoch, xndt2o, xndd6o, bstar, xincl,
xnodeo, eo, omegao, xmo, xno;
int catnr, elset, revnum;
char sat_name[25], idesg[9];
} tle_t;
/* Geodetic position structure used by SGP4/SDP4 code. */
typedef struct {
double lat, lon, alt, theta;
} geodetic_t;
/* General three-dimensional vector structure used by SGP4/SDP4 code. */
typedef struct {
double x, y, z, w;
} vector_t;
/* Common arguments between deep-space functions used by SGP4/SDP4 code. */
typedef struct {
/* Used by dpinit part of Deep() */
double eosq, sinio, cosio, betao, aodp, theta2,
sing, cosg, betao2, xmdot, omgdot, xnodot, xnodp;
/* Used by dpsec and dpper parts of Deep() */
double xll, omgadf, xnode, em, xinc, xn, t;
/* Used by thetg and Deep() */
double ds50;
} deep_arg_t;
/* Global structure used by SGP4/SDP4 code. */
geodetic_t obs_geodetic;
/* Two-line Orbital Elements for the satellite used by SGP4/SDP4 code. */
tle_t tle;
/* Functions for testing and setting/clearing flags used in SGP4/SDP4 code */
int isFlagSet(int flag)
{
return (Flags&flag);
}
int isFlagClear(int flag)
{
return (~Flags&flag);
}
void SetFlag(int flag)
{
Flags|=flag;
}
void ClearFlag(int flag)
{
Flags&=~flag;
}
/* Remaining SGP4/SDP4 code follows... */
int Sign(double arg)
{
/* Returns sign of a double */
if (arg>0)
return 1;
else if (arg<0)
return -1;
else
return 0;
}
double Sqr(double arg)
{
/* Returns square of a double */
return (arg*arg);
}
double Cube(double arg)
{
/* Returns cube of a double */
return (arg*arg*arg);
}
double Radians(double arg)
{
/* Returns angle in radians from argument in degrees */
return (arg*deg2rad);
}
double Degrees(double arg)
{
/* Returns angle in degrees from argument in radians */
return (arg/deg2rad);
}
double ArcSin(double arg)
{
/* Returns the arcsine of the argument */
if (fabs(arg)>=1.0)
return(Sign(arg)*pio2);
else
return(atan(arg/sqrt(1.0-arg*arg)));
}
double ArcCos(double arg)
{
/* Returns arccosine of argument */
return(pio2-ArcSin(arg));
}
void Magnitude(vector_t *v)
{
/* Calculates scalar magnitude of a vector_t argument */
v->w=sqrt(Sqr(v->x)+Sqr(v->y)+Sqr(v->z));
}
void Vec_Add(vector_t *v1, vector_t *v2, vector_t *v3)
{
/* Adds vectors v1 and v2 together to produce v3 */
v3->x=v1->x+v2->x;
v3->y=v1->y+v2->y;
v3->z=v1->z+v2->z;
Magnitude(v3);
}
void Vec_Sub(vector_t *v1, vector_t *v2, vector_t *v3)
{
/* Subtracts vector v2 from v1 to produce v3 */
v3->x=v1->x-v2->x;
v3->y=v1->y-v2->y;
v3->z=v1->z-v2->z;
Magnitude(v3);
}
void Scalar_Multiply(double k, vector_t *v1, vector_t *v2)
{
/* Multiplies the vector v1 by the scalar k to produce the vector v2 */
v2->x=k*v1->x;
v2->y=k*v1->y;
v2->z=k*v1->z;
v2->w=fabs(k)*v1->w;
}
void Scale_Vector(double k, vector_t *v)
{
/* Multiplies the vector v1 by the scalar k */
v->x*=k;
v->y*=k;
v->z*=k;
Magnitude(v);
}
double Dot(vector_t *v1, vector_t *v2)
{
/* Returns the dot product of two vectors */
return (v1->x*v2->x+v1->y*v2->y+v1->z*v2->z);
}
double Angle(vector_t *v1, vector_t *v2)
{
/* Calculates the angle between vectors v1 and v2 */
Magnitude(v1);
Magnitude(v2);
return(ArcCos(Dot(v1,v2)/(v1->w*v2->w)));
}
void Cross(vector_t *v1, vector_t *v2 ,vector_t *v3)
{
/* Produces cross product of v1 and v2, and returns in v3 */
v3->x=v1->y*v2->z-v1->z*v2->y;
v3->y=v1->z*v2->x-v1->x*v2->z;
v3->z=v1->x*v2->y-v1->y*v2->x;
Magnitude(v3);
}
void Normalize(vector_t *v)
{
/* Normalizes a vector */
v->x/=v->w;
v->y/=v->w;
v->z/=v->w;
}
double AcTan(double sinx, double cosx)
{
/* Four-quadrant arctan function */
if (cosx==0.0)
{
if (sinx>0.0)
return (pio2);
else
return (x3pio2);
}
else
{
if (cosx>0.0)
{
if (sinx>0.0)
return (atan(sinx/cosx));
else
return (twopi+atan(sinx/cosx));
}
else
return (pi+atan(sinx/cosx));
}
}
double FMod2p(double x)
{
/* Returns mod 2PI of argument */
int i;
double ret_val;
ret_val=x;
i=ret_val/twopi;
ret_val-=i*twopi;
if (ret_val<0.0)
ret_val+=twopi;
return ret_val;
}
double Modulus(double arg1, double arg2)
{
/* Returns arg1 mod arg2 */
int i;
double ret_val;
ret_val=arg1;
i=ret_val/arg2;
ret_val-=i*arg2;
if (ret_val<0.0)
ret_val+=arg2;
return ret_val;
}
double Frac(double arg)
{
/* Returns fractional part of double argument */
return(arg-floor(arg));
}
int Round(double arg)
{
/* Returns argument rounded up to nearest integer */
return((int)floor(arg+0.5));
}
double Int(double arg)
{
/* Returns the floor integer of a double arguement, as double */
return(floor(arg));
}
void Convert_Sat_State(vector_t *pos, vector_t *vel)
{
/* Converts the satellite's position and velocity */
/* vectors from normalized values to km and km/sec */
Scale_Vector(xkmper, pos);
Scale_Vector(xkmper*xmnpda/secday, vel);
}
double Julian_Date_of_Year(double year)
{
/* The function Julian_Date_of_Year calculates the Julian Date */
/* of Day 0.0 of {year}. This function is used to calculate the */
/* Julian Date of any date by using Julian_Date_of_Year, DOY, */
/* and Fraction_of_Day. */
/* Astronomical Formulae for Calculators, Jean Meeus, */
/* pages 23-25. Calculate Julian Date of 0.0 Jan year */
long A, B, i;
double jdoy;
year=year-1;
i=year/100;
A=i;
i=A/4;
B=2-A+i;
i=365.25*year;
i+=30.6001*14;
jdoy=i+1720994.5+B;
return jdoy;
}
double Julian_Date_of_Epoch(double epoch)
{
/* The function Julian_Date_of_Epoch returns the Julian Date of */
/* an epoch specified in the format used in the NORAD two-line */
/* element sets. It has been modified to support dates beyond */
/* the year 1999 assuming that two-digit years in the range 00-56 */
/* correspond to 2000-2056. Until the two-line element set format */
/* is changed, it is only valid for dates through 2056 December 31. */
double year, day;
/* Modification to support Y2K */
/* Valid 1957 through 2056 */
day=modf(epoch*1E-3, &year)*1E3;
if (year<57)
year=year+2000;
else
year=year+1900;
return (Julian_Date_of_Year(year)+day);
}
int DOY (int yr, int mo, int dy)
{
/* The function DOY calculates the day of the year for the specified */
/* date. The calculation uses the rules for the Gregorian calendar */
/* and is valid from the inception of that calendar system. */
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i, day;
day=0;
for (i=0; i<mo-1; i++ )
day+=days[i];
day=day+dy;
/* Leap year correction */
if ((yr%4==0) && ((yr%100!=0) || (yr%400==0)) && (mo>2))
day++;
return day;
}
double Fraction_of_Day(int hr, int mi, double se)
{
/* Fraction_of_Day calculates the fraction of */
/* a day passed at the specified input time. */
double dhr, dmi;
dhr=(double)hr;
dmi=(double)mi;
return ((dhr+(dmi+se/60.0)/60.0)/24.0);
}
double Julian_Date(struct tm *cdate)
{
/* The function Julian_Date converts a standard calendar */
/* date and time to a Julian Date. The procedure Date_Time */
/* performs the inverse of this function. */
double julian_date;
julian_date=Julian_Date_of_Year(cdate->tm_year)+DOY(cdate->tm_year,cdate->tm_mon,cdate->tm_mday)+Fraction_of_Day(cdate->tm_hour,cdate->tm_min,cdate->tm_sec)+5.787037e-06; /* Round up to nearest 1 sec */
return julian_date;
}
void Date_Time(double julian_date, struct tm *cdate)
{
/* The function Date_Time() converts a Julian Date to
standard calendar date and time. The function
Julian_Date() performs the inverse of this function. */
time_t jtime;
jtime=(julian_date-2440587.5)*86400.0;
*cdate=*gmtime(&jtime);
}
double Delta_ET(double year)
{
/* The function Delta_ET has been added to allow calculations on */
/* the position of the sun. It provides the difference between UT */
/* (approximately the same as UTC) and ET (now referred to as TDT).*/
/* This function is based on a least squares fit of data from 1950 */
/* to 1991 and will need to be updated periodically. */
/* Values determined using data from 1950-1991 in the 1990
Astronomical Almanac. See DELTA_ET.WQ1 for details. */
double delta_et;
delta_et=26.465+0.747622*(year-1950)+1.886913*sin(twopi*(year-1975)/33);
return delta_et;
}
double ThetaG(double epoch, deep_arg_t *deep_arg)
{
/* The function ThetaG calculates the Greenwich Mean Sidereal Time */
/* for an epoch specified in the format used in the NORAD two-line */
/* element sets. It has now been adapted for dates beyond the year */
/* 1999, as described above. The function ThetaG_JD provides the */
/* same calculation except that it is based on an input in the */
/* form of a Julian Date. */
/* Reference: The 1992 Astronomical Almanac, page B6. */
double year, day, UT, jd, TU, GMST, ThetaG;
/* Modification to support Y2K */
/* Valid 1957 through 2056 */
day=modf(epoch*1E-3,&year)*1E3;
if (year<57)
year+=2000;
else
year+=1900;
UT=modf(day,&day);
jd=Julian_Date_of_Year(year)+day;
TU=(jd-2451545.0)/36525;
GMST=24110.54841+TU*(8640184.812866+TU*(0.093104-TU*6.2E-6));
GMST=Modulus(GMST+secday*omega_E*UT,secday);
ThetaG=twopi*GMST/secday;
deep_arg->ds50=jd-2433281.5+UT;
ThetaG=FMod2p(6.3003880987*deep_arg->ds50+1.72944494);
return ThetaG;
}
double ThetaG_JD(double jd)
{
/* Reference: The 1992 Astronomical Almanac, page B6. */
double UT, TU, GMST;
UT=Frac(jd+0.5);
jd=jd-UT;
TU=(jd-2451545.0)/36525;
GMST=24110.54841+TU*(8640184.812866+TU*(0.093104-TU*6.2E-6));
GMST=Modulus(GMST+secday*omega_E*UT,secday);
return (twopi*GMST/secday);
}
void Calculate_Solar_Position(double time, vector_t *solar_vector)
{
/* Calculates solar position vector */
double mjd, year, T, M, L, e, C, O, Lsa, nu, R, eps;
mjd=time-2415020.0;
year=1900+mjd/365.25;
T=(mjd+Delta_ET(year)/secday)/36525.0;
M=Radians(Modulus(358.47583+Modulus(35999.04975*T,360.0)-(0.000150+0.0000033*T)*Sqr(T),360.0));
L=Radians(Modulus(279.69668+Modulus(36000.76892*T,360.0)+0.0003025*Sqr(T),360.0));
e=0.01675104-(0.0000418+0.000000126*T)*T;
C=Radians((1.919460-(0.004789+0.000014*T)*T)*sin(M)+(0.020094-0.000100*T)*sin(2*M)+0.000293*sin(3*M));
O=Radians(Modulus(259.18-1934.142*T,360.0));
Lsa=Modulus(L+C-Radians(0.00569-0.00479*sin(O)),twopi);
nu=Modulus(M+C,twopi);
R=1.0000002*(1.0-Sqr(e))/(1.0+e*cos(nu));
eps=Radians(23.452294-(0.0130125+(0.00000164-0.000000503*T)*T)*T+0.00256*cos(O));
R=AU*R;
solar_vector->x=R*cos(Lsa);
solar_vector->y=R*sin(Lsa)*cos(eps);
solar_vector->z=R*sin(Lsa)*sin(eps);
solar_vector->w=R;
}
int Sat_Eclipsed(vector_t *pos, vector_t *sol, double *depth)
{
/* Calculates satellite's eclipse status and depth */
double sd_sun, sd_earth, delta;
vector_t Rho, earth;
/* Determine partial eclipse */
sd_earth=ArcSin(xkmper/pos->w);
Vec_Sub(sol,pos,&Rho);
sd_sun=ArcSin(sr/Rho.w);
Scalar_Multiply(-1,pos,&earth);
delta=Angle(sol,&earth);
*depth=sd_earth-sd_sun-delta;
if (sd_earth<sd_sun)
return 0;
else
if (*depth>=0)
return 1;
else
return 0;
}
void select_ephemeris(tle_t *tle)
{
/* 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 */
double ao, xnodp, dd1, dd2, delo, temp, a1, del1, r1;
/* Preprocess tle set */
tle->xnodeo*=deg2rad;
tle->omegao*=deg2rad;
tle->xmo*=deg2rad;
tle->xincl*=deg2rad;
temp=twopi/xmnpda/xmnpda;
tle->xno=tle->xno*temp*xmnpda;
tle->xndt2o*=temp;
tle->xndd6o=tle->xndd6o*temp/xmnpda;
tle->bstar/=ae;
/* Period > 225 minutes is deep space */
dd1=(xke/tle->xno);
dd2=tothrd;
a1=pow(dd1,dd2);
r1=cos(tle->xincl);
dd1=(1.0-tle->eo*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*.5+del1*(del1*1.654320987654321+1.0)));
delo=temp/(ao*ao);
xnodp=tle->xno/(delo+1.0);
/* Select a deep-space/near-earth ephemeris */
if (twopi/xnodp/xmnpda>=0.15625)
SetFlag(DEEP_SPACE_EPHEM_FLAG);
else
ClearFlag(DEEP_SPACE_EPHEM_FLAG);
}
void SGP4(double tsince, tle_t * tle, vector_t * pos, vector_t * vel)
{
/* This function is used to calculate the position and velocity */
/* of near-earth (period < 225 minutes) satellites. tsince is */
/* time since epoch in minutes, tle is a pointer to a tle_t */
/* structure with Keplerian orbital elements and pos and vel */
/* are vector_t structures returning ECI satellite position and */
/* velocity. Use Convert_Sat_State() to convert to km and km/s. */
static double aodp, aycof, c1, c4, c5, cosio, d2, d3, d4, delmo,
omgcof, eta, omgdot, sinio, xnodp, sinmo, t2cof, t3cof, t4cof,
t5cof, x1mth2, x3thm1, x7thm1, xmcof, xmdot, xnodcf, xnodot, xlcof;
double cosuk, sinuk, rfdotk, vx, vy, vz, ux, uy, uz, xmy, xmx, cosnok,
sinnok, cosik, sinik, rdotk, xinck, xnodek, uk, rk, cos2u, sin2u,
u, sinu, cosu, betal, rfdot, rdot, r, pl, elsq, esine, ecose, epw,
cosepw, x1m5th, xhdot1, tfour, sinepw, capu, ayn, xlt, aynl, xll,
axn, xn, beta, xl, e, a, tcube, delm, delomg, templ, tempe, tempa,
xnode, tsq, xmp, omega, xnoddf, omgadf, xmdf, a1, a3ovk2, ao,
betao, betao2, c1sq, c2, c3, coef, coef1, del1, delo, eeta, eosq,
etasq, perigee, pinvsq, psisq, qoms24, s4, temp, temp1, temp2,
temp3, temp4, temp5, temp6, theta2, theta4, tsi;
int i;
/* Initialization */
if (isFlagClear(SGP4_INITIALIZED_FLAG))
{
SetFlag(SGP4_INITIALIZED_FLAG);
/* Recover original mean motion (xnodp) and */
/* semimajor axis (aodp) from input elements. */
a1=pow(xke/tle->xno,tothrd);
cosio=cos(tle->xincl);
theta2=cosio*cosio;
x3thm1=3*theta2-1.0;
eosq=tle->eo*tle->eo;
betao2=1.0-eosq;
betao=sqrt(betao2);
del1=1.5*ck2*x3thm1/(a1*a1*betao*betao2);
ao=a1*(1.0-del1*(0.5*tothrd+del1*(1.0+134.0/81.0*del1)));
delo=1.5*ck2*x3thm1/(ao*ao*betao*betao2);
xnodp=tle->xno/(1.0+delo);
aodp=ao/(1.0-delo);
/* For perigee less than 220 kilometers, the "simple" */
/* flag is set and the equations are truncated to linear */
/* variation in sqrt a and quadratic variation in mean */
/* anomaly. Also, the c3 term, the delta omega term, and */
/* the delta m term are dropped. */
if ((aodp*(1-tle->eo)/ae)<(220/xkmper+ae))
SetFlag(SIMPLE_FLAG);
else
ClearFlag(SIMPLE_FLAG);
/* For perigees below 156 km, the */
/* values of s and qoms2t are altered. */
s4=s;
qoms24=qoms2t;
perigee=(aodp*(1-tle->eo)-ae)*xkmper;
if (perigee<156.0)
{
if (perigee<=98.0)
s4=20;
else
s4=perigee-78.0;
qoms24=pow((120-s4)*ae/xkmper,4);
s4=s4/xkmper+ae;
}
pinvsq=1/(aodp*aodp*betao2*betao2);
tsi=1/(aodp-s4);
eta=aodp*tle->eo*tsi;
etasq=eta*eta;
eeta=tle->eo*eta;
psisq=fabs(1-etasq);
coef=qoms24*pow(tsi,4);
coef1=coef/pow(psisq,3.5);
c2=coef1*xnodp*(aodp*(1+1.5*etasq+eeta*(4+etasq))+0.75*ck2*tsi/psisq*x3thm1*(8+3*etasq*(8+etasq)));
c1=tle->bstar*c2;
sinio=sin(tle->xincl);
a3ovk2=-xj3/ck2*pow(ae,3);
c3=coef*tsi*a3ovk2*xnodp*ae*sinio/tle->eo;
x1mth2=1-theta2;
c4=2*xnodp*coef1*aodp*betao2*(eta*(2+0.5*etasq)+tle->eo*(0.5+2*etasq)-2*ck2*tsi/(aodp*psisq)*(-3*x3thm1*(1-2*eeta+etasq*(1.5-0.5*eeta))+0.75*x1mth2*(2*etasq-eeta*(1+etasq))*cos(2*tle->omegao)));
c5=2*coef1*aodp*betao2*(1+2.75*(etasq+eeta)+eeta*etasq);
theta4=theta2*theta2;
temp1=3*ck2*pinvsq*xnodp;
temp2=temp1*ck2*pinvsq;
temp3=1.25*ck4*pinvsq*pinvsq*xnodp;
xmdot=xnodp+0.5*temp1*betao*x3thm1+0.0625*temp2*betao*(13-78*theta2+137*theta4);
x1m5th=1-5*theta2;
omgdot=-0.5*temp1*x1m5th+0.0625*temp2*(7-114*theta2+395*theta4)+temp3*(3-36*theta2+49*theta4);
xhdot1=-temp1*cosio;
xnodot=xhdot1+(0.5*temp2*(4-19*theta2)+2*temp3*(3-7*theta2))*cosio;
omgcof=tle->bstar*c3*cos(tle->omegao);
xmcof=-tothrd*coef*tle->bstar*ae/eeta;
xnodcf=3.5*betao2*xhdot1*c1;
t2cof=1.5*c1;
xlcof=0.125*a3ovk2*sinio*(3+5*cosio)/(1+cosio);
aycof=0.25*a3ovk2*sinio;
delmo=pow(1+eta*cos(tle->xmo),3);
sinmo=sin(tle->xmo);
x7thm1=7*theta2-1;
if (isFlagClear(SIMPLE_FLAG))
{
c1sq=c1*c1;
d2=4*aodp*tsi*c1sq;
temp=d2*tsi*c1/3;
d3=(17*aodp+s4)*temp;
d4=0.5*temp*aodp*tsi*(221*aodp+31*s4)*c1;
t3cof=d2+2*c1sq;
t4cof=0.25*(3*d3+c1*(12*d2+10*c1sq));
t5cof=0.2*(3*d4+12*c1*d3+6*d2*d2+15*c1sq*(2*d2+c1sq));
}
}
/* Update for secular gravity and atmospheric drag. */
xmdf=tle->xmo+xmdot*tsince;
omgadf=tle->omegao+omgdot*tsince;
xnoddf=tle->xnodeo+xnodot*tsince;
omega=omgadf;
xmp=xmdf;
tsq=tsince*tsince;
xnode=xnoddf+xnodcf*tsq;
tempa=1-c1*tsince;
tempe=tle->bstar*c4*tsince;
templ=t2cof*tsq;
if (isFlagClear(SIMPLE_FLAG))
{
delomg=omgcof*tsince;
delm=xmcof*(pow(1+eta*cos(xmdf),3)-delmo);
temp=delomg+delm;
xmp=xmdf+temp;
omega=omgadf-temp;
tcube=tsq*tsince;
tfour=tsince*tcube;
tempa=tempa-d2*tsq-d3*tcube-d4*tfour;
tempe=tempe+tle->bstar*c5*(sin(xmp)-sinmo);
templ=templ+t3cof*tcube+tfour*(t4cof+tsince*t5cof);
}
a=aodp*pow(tempa,2);
e=tle->eo-tempe;
xl=xmp+omega+xnode+xnodp*templ;
beta=sqrt(1-e*e);
xn=xke/pow(a,1.5);
/* Long period periodics */
axn=e*cos(omega);
temp=1/(a*beta*beta);
xll=temp*xlcof*axn;
aynl=temp*aycof;
xlt=xl+xll;
ayn=e*sin(omega)+aynl;
/* Solve Kepler's Equation */
capu=FMod2p(xlt-xnode);
temp2=capu;
i=0;
do
{
sinepw=sin(temp2);
cosepw=cos(temp2);
temp3=axn*sinepw;
temp4=ayn*cosepw;
temp5=axn*cosepw;
temp6=ayn*sinepw;
epw=(capu-temp4+temp3-temp2)/(1-temp5-temp6)+temp2;
if (fabs(epw-temp2)<= e6a)
break;
temp2=epw;
} while (i++<10);
/* Short period preliminary quantities */
ecose=temp5+temp6;
esine=temp3-temp4;
elsq=axn*axn+ayn*ayn;
temp=1-elsq;
pl=a*temp;
r=a*(1-ecose);
temp1=1/r;
rdot=xke*sqrt(a)*esine*temp1;
rfdot=xke*sqrt(pl)*temp1;
temp2=a*temp1;
betal=sqrt(temp);
temp3=1/(1+betal);
cosu=temp2*(cosepw-axn+ayn*esine*temp3);
sinu=temp2*(sinepw-ayn-axn*esine*temp3);
u=AcTan(sinu,cosu);
sin2u=2*sinu*cosu;
cos2u=2*cosu*cosu-1;
temp=1/pl;
temp1=ck2*temp;
temp2=temp1*temp;
/* Update for short periodics */
rk=r*(1-1.5*temp2*betal*x3thm1)+0.5*temp1*x1mth2*cos2u;
uk=u-0.25*temp2*x7thm1*sin2u;
xnodek=xnode+1.5*temp2*cosio*sin2u;
xinck=tle->xincl+1.5*temp2*cosio*sinio*cos2u;
rdotk=rdot-xn*temp1*x1mth2*sin2u;
rfdotk=rfdot+xn*temp1*(x1mth2*cos2u+1.5*x3thm1);
/* Orientation vectors */
sinuk=sin(uk);
cosuk=cos(uk);
sinik=sin(xinck);
cosik=cos(xinck);
sinnok=sin(xnodek);
cosnok=cos(xnodek);
xmx=-sinnok*cosik;
xmy=cosnok*cosik;
ux=xmx*sinuk+cosnok*cosuk;
uy=xmy*sinuk+sinnok*cosuk;
uz=sinik*sinuk;
vx=xmx*cosuk-cosnok*sinuk;
vy=xmy*cosuk-sinnok*sinuk;
vz=sinik*cosuk;
/* Position and velocity */
pos->x=rk*ux;
pos->y=rk*uy;
pos->z=rk*uz;
vel->x=rdotk*ux+rfdotk*vx;
vel->y=rdotk*uy+rfdotk*vy;
vel->z=rdotk*uz+rfdotk*vz;
/* Phase in radians */
phase=xlt-xnode-omgadf+twopi;