forked from kd2bd/predict
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.c
More file actions
executable file
·5136 lines (3998 loc) · 119 KB
/
predict.c
File metadata and controls
executable file
·5136 lines (3998 loc) · 119 KB
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/stat.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 "more_math.h"
#include "deep.h"
#include "SGP4.h"
#include "SDP4.h"
#include "predict.h"
#include "constants.h"
//const char *version="2.2.5";
//char *predictpath="/usr/local/lib/predict-2.5.5/";
//int soundcard=0;
struct sat_db_st;
struct sat_st {
char line1[70];
char line2[70];
char name[25];
// Line 1
long catnum; // Satellite catalog number
char designator[10]; // International Designator:
// * 2 digits: last two digits of launch year,
// * 3 digits: launch number of the year
// * 3 chars: piece of the launch
int year; // Epoch year (last two digits of year)
double refepoch; // Epoch (day of the year and fractional portion of the day)
double drag; // First derivative of mean motion; the ballistic coefficient
double nddot6; // Second derivative of mean motion
double bstar; // B*, the drag term, or radiation pressure coefficient
long setnum; // Element set number
// Line 2
double incl; // nclination (degrees)
double raan; // Right ascension of the ascending node (degrees)
double eccn; // Eccentricity
double argper; // Argument of perigee (degrees)
double meanan; // Mean anomaly (degrees)
double meanmo; // Mean motion (revolutions per day)
long orbitnum; // Revolution number at epoch (revolutions)
const struct sat_db_st *db;
} sat_table[24];
struct qth_st{
char callsign[17];
double stnlat;
double stnlong;
int stnalt;
} qth;
struct transponder_st {
char name[80];
double uplink_start;
double uplink_end;
double downlink_start;
double downlink_end;
unsigned char dayofweek;
int phase_start;
int phase_end;
};
struct sat_db_st {
char name[25];
long catnum;
char squintflag;
double alat;
double alon;
unsigned char transponders;
struct transponder_st transponder[10];
} sat_db[24];
/* Global variables for sharing data among functions... */
double 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, sat_footprint, 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, sat_sun_status, findsun,
database=0, xterm, io_lat='N', io_lon='W';
int calc_squint;
int antfd, ma256, socket_flag=0;
int Flags=0;
long rv;
unsigned char val[256];
/* The following variables are used by the socket server. They
are updated in the MultiTrack() and SingleTrack() functions. */
char tracking_mode[30];
struct server_data_st {
char visibility;
float az;
float el;
float longitude;
float lattitude;
float footprint;
float range;
float altitude;
float velocity;
float eclipse_depth;
float phase;
float squint;
double doppler;
double nextevent;
long aos;
long orbitnum;
};
struct server_data_st server_data[24];
unsigned short portbase=0;
/** Type definitions **/
/* Geodetic position structure used by SGP4/SDP4 code. */
typedef struct {
double lat, lon, alt, theta;
} geodetic_t;
/* Common arguments between deep-space functions used by SGP4/SDP4 code. */
/* Global structure used by SGP4/SDP4 code. */
geodetic_t obs_geodetic;
/* 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... */
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(const 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;
}
int select_ephemeris(const 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 */
/* Period > 225 minutes is deep space */
double dd1=(xke/tle->xno);
const double dd2=tothrd;
const double a1=pow(dd1,dd2);
const double r1=cos(tle->xincl);
dd1=(1.0-tle->eo*tle->eo);
const double temp=ck2*1.5f*(r1*r1*3.0-1.0)/pow(dd1,1.5);
const double del1=temp/(a1*a1);
const double ao=a1*(1.0-del1*(tothrd*.5+del1*(del1*1.654320987654321+1.0)));
const double delo=temp/(ao*ao);
const double xnodp=tle->xno/(delo+1.0);
/* Select a deep-space/near-earth ephemeris */
return twopi/xnodp/xmnpda>=0.15625;
}
void Calculate_User_PosVel(double time, geodetic_t *geodetic, vector_t *obs_pos, vector_t *obs_vel)
{
/* Calculate_User_PosVel() passes the user's geodetic position
and the time of interest and returns the ECI position and
velocity of the observer. The velocity calculation assumes
the geodetic position is stationary relative to the earth's
surface. */
/* Reference: The 1992 Astronomical Almanac, page K11. */
double c, sq, achcp;
geodetic->theta=FMod2p(ThetaG_JD(time)+geodetic->lon); /* LMST */
c=1/sqrt(1+f*(f-2)*Sqr(sin(geodetic->lat)));
sq=Sqr(1-f)*c;
achcp=(xkmper*c+geodetic->alt)*cos(geodetic->lat);
obs_pos->x=achcp*cos(geodetic->theta); /* kilometers */
obs_pos->y=achcp*sin(geodetic->theta);
obs_pos->z=(xkmper*sq+geodetic->alt)*sin(geodetic->lat);
obs_vel->x=-mfactor*obs_pos->y; /* kilometers/second */
obs_vel->y=mfactor*obs_pos->x;
obs_vel->z=0;
Magnitude(obs_pos);
Magnitude(obs_vel);
}
void Calculate_LatLonAlt(double time, vector_t *pos, geodetic_t *geodetic)
{
/* Procedure Calculate_LatLonAlt will calculate the geodetic */
/* position of an object given its ECI position pos and time. */
/* It is intended to be used to determine the ground track of */
/* a satellite. The calculations assume the earth to be an */
/* oblate spheroid as defined in WGS '72. */
/* Reference: The 1992 Astronomical Almanac, page K12. */
double r, e2, phi, c;
geodetic->theta=AcTan(pos->y,pos->x); /* radians */
geodetic->lon=FMod2p(geodetic->theta-ThetaG_JD(time)); /* radians */
r=sqrt(Sqr(pos->x)+Sqr(pos->y));
e2=f*(2-f);
geodetic->lat=AcTan(pos->z,r); /* radians */
do
{
phi=geodetic->lat;
c=1/sqrt(1-e2*Sqr(sin(phi)));
geodetic->lat=AcTan(pos->z+xkmper*c*e2*sin(phi),r);
} while (fabs(geodetic->lat-phi)>=1E-10);
geodetic->alt=r/cos(geodetic->lat)-xkmper*c; /* kilometers */
if (geodetic->lat>pio2)
geodetic->lat-=twopi;
}
void Calculate_Obs(double time, const vector_t *pos, const vector_t *vel, geodetic_t *geodetic, vector_t *obs_set)
{
/* The procedures Calculate_Obs and Calculate_RADec calculate */
/* the *topocentric* coordinates of the object with ECI position, */
/* {pos}, and velocity, {vel}, from location {geodetic} at {time}. */
/* The {obs_set} returned for Calculate_Obs consists of azimuth, */
/* elevation, range, and range rate (in that order) with units of */
/* radians, radians, kilometers, and kilometers/second, respectively. */
/* The WGS '72 geoid is used and the effect of atmospheric refraction */
/* (under standard temperature and pressure) is incorporated into the */
/* elevation calculation; the effect of atmospheric refraction on */
/* range and range rate has not yet been quantified. */
/* The {obs_set} for Calculate_RADec consists of right ascension and */
/* declination (in that order) in radians. Again, calculations are */
/* based on *topocentric* position using the WGS '72 geoid and */
/* incorporating atmospheric refraction. */
double sin_lat, cos_lat, sin_theta, cos_theta, el, azim, top_s, top_e, top_z;
vector_t obs_pos, obs_vel, range, rgvel;
Calculate_User_PosVel(time, geodetic, &obs_pos, &obs_vel);
range.x=pos->x-obs_pos.x;
range.y=pos->y-obs_pos.y;
range.z=pos->z-obs_pos.z;
/* Save these values globally for calculating squint angles later... */
rx=range.x;
ry=range.y;
rz=range.z;
rgvel.x=vel->x-obs_vel.x;
rgvel.y=vel->y-obs_vel.y;
rgvel.z=vel->z-obs_vel.z;
Magnitude(&range);
sin_lat=sin(geodetic->lat);
cos_lat=cos(geodetic->lat);
sin_theta=sin(geodetic->theta);
cos_theta=cos(geodetic->theta);
top_s=sin_lat*cos_theta*range.x+sin_lat*sin_theta*range.y-cos_lat*range.z;
top_e=-sin_theta*range.x+cos_theta*range.y;
top_z=cos_lat*cos_theta*range.x+cos_lat*sin_theta*range.y+sin_lat*range.z;
azim=atan(-top_e/top_s); /* Azimuth */
if (top_s>0.0)
azim=azim+pi;
if (azim<0.0)
azim=azim+twopi;
el=ArcSin(top_z/range.w);
obs_set->x=azim; /* Azimuth (radians) */
obs_set->y=el; /* Elevation (radians) */
obs_set->z=range.w; /* Range (kilometers) */
/* Range Rate (kilometers/second) */
obs_set->w=Dot(&range,&rgvel)/range.w;
/* Corrections for atmospheric refraction */
/* Reference: Astronomical Algorithms by Jean Meeus, pp. 101-104 */
/* Correction is meaningless when apparent elevation is below horizon */
/*** The following adjustment for
atmospheric refraction is bypassed ***/
/* obs_set->y=obs_set->y+Radians((1.02/tan(Radians(Degrees(el)+10.3/(Degrees(el)+5.11))))/60); */
obs_set->y=el;
/**** End bypass ****/
if (obs_set->y>=0.0)
SetFlag(VISIBLE_FLAG);
else
{
obs_set->y=el; /* Reset to true elevation */
ClearFlag(VISIBLE_FLAG);
}
}
void Calculate_RADec(double time, const vector_t *pos, const vector_t *vel, geodetic_t *geodetic, vector_t *obs_set)
{
/* Reference: Methods of Orbit Determination by */
/* Pedro Ramon Escobal, pp. 401-402 */
double phi, theta, sin_theta, cos_theta, sin_phi, cos_phi, az, el,
Lxh, Lyh, Lzh, Sx, Ex, Zx, Sy, Ey, Zy, Sz, Ez, Zz, Lx, Ly,
Lz, cos_delta, sin_alpha, cos_alpha;
Calculate_Obs(time,pos,vel,geodetic,obs_set);
az=obs_set->x;
el=obs_set->y;
phi=geodetic->lat;
theta=FMod2p(ThetaG_JD(time)+geodetic->lon);
sin_theta=sin(theta);
cos_theta=cos(theta);
sin_phi=sin(phi);
cos_phi=cos(phi);
Lxh=-cos(az)*cos(el);
Lyh=sin(az)*cos(el);
Lzh=sin(el);
Sx=sin_phi*cos_theta;
Ex=-sin_theta;
Zx=cos_theta*cos_phi;
Sy=sin_phi*sin_theta;
Ey=cos_theta;
Zy=sin_theta*cos_phi;
Sz=-cos_phi;
Ez=0.0;
Zz=sin_phi;
Lx=Sx*Lxh+Ex*Lyh+Zx*Lzh;
Ly=Sy*Lxh+Ey*Lyh+Zy*Lzh;
Lz=Sz*Lxh+Ez*Lyh+Zz*Lzh;
obs_set->y=ArcSin(Lz); /* Declination (radians) */
cos_delta=sqrt(1.0-Sqr(Lz));
sin_alpha=Ly/cos_delta;
cos_alpha=Lx/cos_delta;
obs_set->x=AcTan(sin_alpha,cos_alpha); /* Right Ascension (radians) */
obs_set->x=FMod2p(obs_set->x);
}
/* .... SGP4/SDP4 functions end .... */
void bailout(const char * string)
{
/* This function quits ncurses, resets and "beeps"
the terminal, and displays an error message (string)
when we need to bail out of the program in a hurry. */
beep();
curs_set(1);
bkgdset(COLOR_PAIR(1));
clear();
refresh();
endwin();
fprintf(stderr,"*** predict: %s!\n",string);
}
void TrackDataOut(int antfd, double elevation, double azimuth)
{
/* This function sends Azimuth and Elevation data
to an antenna tracker connected to the serial port */
size_t n;
int port;
char message[30]="\n";
port=antfd;
sprintf(message, "AZ%3.1f EL%3.1f \x0D\x0A", azimuth,elevation);
n=write(port,message,strlen(message));
if (n<0)
{
bailout("Error Writing To Antenna Port");
exit(-1);
}
}
int passivesock(const char *service, const char *protocol, int qlen)
{
/* This function opens the socket port */
struct servent *pse;
struct protoent *ppe;
struct sockaddr_in sin;
int sd, type;
memset((char *)&sin, 0, sizeof(struct sockaddr_in));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=INADDR_ANY;
if ((pse=getservbyname(service,protocol)))
sin.sin_port=htons(ntohs((unsigned short)pse->s_port)+portbase);
else if ((sin.sin_port=htons((unsigned short)atoi(service)))==0)
{
bailout("Can't get service");
exit(-1);
}
if ((ppe=getprotobyname(protocol))==0)
{
bailout("Can't get protocol");
exit(-1);
}
if (strcmp(protocol,"udp")==0)
type=SOCK_DGRAM;
else
type=SOCK_STREAM;
sd=socket(PF_INET,type, ppe->p_proto);
if (sd<0)
{
bailout("Can't open socket");
exit(-1);
}
if (bind(sd,(struct sockaddr *)&sin,sizeof(sin))<0)
{
bailout("Can't bind");
exit(-1);
}
if ((type==SOCK_STREAM) && listen(s,qlen)<0)
{
bailout("Listen fail");
exit(-1);
}
return sd;
}
void socket_server(const char * predict_name)
{
/* This is the socket server code */
int i, j, sock;
size_t n;
socklen_t alen;
struct sockaddr_in fsin;
char buf[80], buff[1000], satname[50], tempname[30], ok;
time_t t;
FILE *fd=NULL;
/* Open a socket port at "predict" or netport if defined */
if (netport[0]==0)
strncpy(netport,"predict",7);
sock=passivesock(netport,"udp",10);
alen=sizeof(fsin);
/* This is the main loop for monitoring the socket
port and sending back replies to clients */
while (1)
{
/* Get datagram from socket port */
if ((n=recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr *)&fsin,&alen)) < 0)
exit (-1);
buf[n]=0;
ok=0;
/* Parse the command in the datagram */
if ((strncmp("GET_SAT",buf,7)==0) && (strncmp("GET_SAT_POS",buf,11)!=0))
{
/* Parse "buf" for satellite name */
for (i=0; buf[i]!=32 && buf[i]!=0 && i<39; i++);
for (j=++i; buf[j]!='\n' && buf[j]!=0 && (j-i)<25; j++)
satname[j-i]=buf[j];
satname[j-i]=0;
/* Do a simple search for the matching satellite name */
for (i=0; i<24; i++)
{
if ((strncmp(satname,sat_table[i].name,25)==0) || (atol(satname)==sat_table[i].catnum))
{
struct server_data_st * const server = &server_data[i];
long nxtevt=(long)rint(86400.0*(server->nextevent+3651.0));
/* Build text buffer with satellite data */
sprintf(buff,"%s\n%-7.2f\n%+-6.2f\n%-7.2f\n%+-6.2f\n%ld\n%-7.2f\n%-7.2f\n%-7.2f\n%-7.2f\n%ld\n%c\n%-7.2f\n%-7.2f\n%-7.2f\n",
sat_table[i].name,
server->longitude, server->lattitude,
server->az, server->el,
nxtevt, server->footprint,
server->range, server->altitude,
server->velocity, server->orbitnum,
server->visibility, server->phase,
server->eclipse_depth, server->squint);
/* Send buffer back to the client that sent the request */
sendto(sock,buff,strlen(buff),0,(struct sockaddr*)&fsin,sizeof(fsin));
ok=1;
break;
}
}
}
if (strncmp("GET_TLE",buf,7)==0)
{
/* Parse "buf" for satellite name */
for (i=0; buf[i]!=32 && buf[i]!=0 && i<39; i++);
for (j=++i; buf[j]!='\n' && buf[j]!=0 && (j-i)<25; j++)
satname[j-i]=buf[j];
satname[j-i]=0;
/* Do a simple search for the matching satellite name */
for (i=0; i<24; i++)
{
if ((strncmp(satname,sat_table[i].name,25)==0) || (atol(satname)==sat_table[i].catnum))
{
/* Build text buffer with satellite data */
sprintf(buff,"%s\n%s\n%s\n",sat_table[i].name,sat_table[i].line1, sat_table[i].line2);
/* Send buffer back to the client that sent the request */
sendto(sock,buff,strlen(buff),0,(struct sockaddr*)&fsin,sizeof(fsin));
ok=1;
break;
}
}
}
if (strncmp("GET_DOPPLER",buf,11)==0)
{
/* Parse "buf" for satellite name */
for (i=0; buf[i]!=32 && buf[i]!=0 && i<39; i++);
for (j=++i; buf[j]!='\n' && buf[j]!=0 && (j-i)<25; j++)
satname[j-i]=buf[j];
satname[j-i]=0;
/* Do a simple search for the matching satellite name */
for (i=0; i<24; i++)
{
if ((strncmp(satname,sat_table[i].name,25)==0) || (atol(satname)==sat_table[i].catnum))
{
/* Get Normalized (100 MHz)
Doppler shift for sat[i] */
sprintf(buff,"%f\n",server_data[i].doppler);
/* Send buffer back to client who sent request */
sendto(sock,buff,strlen(buff),0,(struct sockaddr*)&fsin,sizeof(fsin));
ok=1;
break;
}
}
}
if (strncmp("GET_LIST",buf,8)==0)
{
buff[0]=0;
for (i=0; i<24; i++)
{
if (sat_table[i].name[0]!=0)
strcat(buff,sat_table[i].name);
strcat(buff,"\n");
}
sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&fsin,sizeof(fsin));
ok=1;
}
if (strncmp("RELOAD_TLE",buf,10)==0)
{
buff[0]=0;
sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&fsin,sizeof(fsin));
reload_tle=1;
ok=1;
}
if (strncmp("GET_SUN",buf,7)==0)
{
buff[0]=0;
sprintf(buff,"%-7.2f\n%+-6.2f\n%-7.2f\n%-7.2f\n%-7.2f\n",sun_azi, sun_ele, sun_lat, sun_lon, sun_ra);
sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&fsin,sizeof(fsin));
ok=1;
}
if (strncmp("GET_MOON",buf,8)==0)
{
buff[0]=0;
sprintf(buff,"%-7.2f\n%+-6.2f\n%-7.2f\n%-7.2f\n%-7.2f\n",moon_az, moon_el, moon_dec, moon_gha, moon_ra);
sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&fsin,sizeof(fsin));
ok=1;
}
if (strncmp("GET_MODE",buf,8)==0)
{
sendto(sock,tracking_mode,strlen(tracking_mode),0,(struct sockaddr *)&fsin,sizeof(fsin));
ok=1;
}
if (strncmp("GET_VERSION",buf,11)==0)
{
buff[0]=0;
sprintf(buff,"%s\n",version);
sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&fsin,sizeof(fsin));
ok=1;
}
if (strncmp("GET_QTH",buf,7)==0)
{
buff[0]=0;
sprintf(buff,"%s\n%g\n%g\n%d\n",qth.callsign, qth.stnlat, qth.stnlong, qth.stnalt);
sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&fsin,sizeof(fsin));
ok=1;
}
if (strncmp("GET_TIME$",buf,9)==0)
{
buff[0]=0;
t=time(NULL);
sprintf(buff,"%s",asctime(gmtime(&t)));
if (buff[8]==32)
buff[8]='0';
sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&fsin,sizeof(fsin));
buf[0]=0;
ok=1;
}
if (strncmp("GET_TIME",buf,8)==0)
{
buff[0]=0;
t=time(NULL);
sprintf(buff,"%lu\n",(unsigned long)t);
sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&fsin,sizeof(fsin));
ok=1;
}
if (strncmp("GET_SAT_POS",buf,11)==0)
{
/* Parse "buf" for satellite name and arguments */
for (i=0; buf[i]!=32 && buf[i]!=0 && i<39; i++);
for (j=++i; buf[j]!='\n' && buf[j]!=0 && (j-i)<49; j++)
satname[j-i]=buf[j];
satname[j-i]=0;
/* Send request to predict with output
directed to a temporary file under /tmp */
strcpy(tempname,"/tmp/XXXXXX\0");
i=mkstemp(tempname);
sprintf(buff,"%s -f %s -t %s -q %s -o %s\n",predict_name,satname,tlefile,qthfile,tempname);
system(buff);
/* Append an EOF marker (CNTRL-Z) to the end of file */
fd=fopen(tempname,"a");
fprintf(fd,"%c\n",26); /* Control-Z */
fclose(fd);
buff[0]=0;
/* Send the file to the client */
fd=fopen(tempname,"rb");
fgets(buff,80,fd);
do
{
sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&fsin,sizeof(fsin));
fgets(buff,80,fd);
/* usleep(2); if needed (for flow-control) */
} while (feof(fd)==0);
fclose(fd);
unlink(tempname);
close(i);
ok=1;
}
if (strncmp("PREDICT",buf,7)==0)
{
/* Parse "buf" for satellite name and arguments */
for (i=0; buf[i]!=32 && buf[i]!=0 && i<39; i++);
for (j=++i; buf[j]!='\n' && buf[j]!=0 && (j-i)<49; j++)
satname[j-i]=buf[j];
satname[j-i]=0;
/* Send request to predict with output
directed to a temporary file under /tmp */
strcpy(tempname,"/tmp/XXXXXX\0");
i=mkstemp(tempname);
sprintf(buff,"%s -p %s -t %s -q %s -o %s\n",predict_name, satname,tlefile,qthfile,tempname);
system(buff);
/* Append an EOF marker (CNTRL-Z) to the end of file */
fd=fopen(tempname,"a");
fprintf(fd,"%c\n",26); /* Control-Z */
fclose(fd);
buff[0]=0;
/* Send the file to the client */
fd=fopen(tempname,"rb");
fgets(buff,80,fd);
do
{
sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&fsin,sizeof(fsin));
fgets(buff,80,fd);
/* usleep(2); if needed (for flow-control) */
} while (feof(fd)==0);
fclose(fd);
unlink(tempname);
close(i);
ok=1;
}
if (ok==0)
sendto(sock,"Huh?\n",5,0,(struct sockaddr *)&fsin,sizeof(fsin));
}
}
void Banner(void)
{
curs_set(0);
bkgdset(COLOR_PAIR(3));
clear();
refresh();
attrset(COLOR_PAIR(6)|A_REVERSE|A_BOLD);
mvprintw(3,18," ");
mvprintw(4,18," --== PREDICT v%s ==-- ",version);
mvprintw(5,18," Released by John A. Magliacane, KD2BD ");
mvprintw(6,18," May 2018 ");
mvprintw(7,18," ");
}
void AnyKey(void)
{
mvprintw(23,24,"<< Press Any Key To Continue >>");
refresh();
getch();
}
double FixAngle(double x)
{
/* This function reduces angles greater than
two pi by subtracting two pi from the angle */
while (x>twopi)
x-=twopi;
return x;
}
double PrimeAngle(double x)
{
/* This function is used in the FindMoon() function. */
x=x-360.0*floor(x/360.0);
return x;
}
char *SubString(const char *string, unsigned start, unsigned end)
{
/* This function returns a substring based on the starting
and ending positions provided. It is used heavily in
the AutoUpdate function when parsing 2-line element data. */
static char temp[80];
unsigned x, y;
if (end>=start)
{
for (x=start, y=0; x<=end && string[x]!=0; x++)
if (string[x]!=' ')
{
temp[y]=string[x];
y++;
}
temp[y]=0;
return temp;
}
else
return NULL;
}
void CopyString(const char *source, char *destination, unsigned start, unsigned end)
{
/* This function copies elements of the string "source"
bounded by "start" and "end" into the string "destination". */
unsigned j, k=0;
for (j=start; j<=end; j++)
if (source[k]!=0)
{
destination[j]=source[k];
k++;
}
}
char *Abbreviate(const char * string,int n)
{
/* This function returns an abbreviated substring of the original,
including a '~' character if a non-blank character is chopped
out of the generated substring. n is the length of the desired
substring. It is used for abbreviating satellite names. */
static char temp[80];
strncpy(temp,string,79);
if (temp[n]!=0 && temp[n]!=32)
{
temp[n-2]='~';
temp[n-1]=temp[strlen(temp)-1];
}
temp[n]=0;
return temp;
}
char KepCheck(const char *line1,const char *line2)
{
/* This function scans line 1 and line 2 of a NASA 2-Line element
set and returns a 1 if the element set appears to be valid or
a 0 if it does not. If the data survives this torture test,
it's a pretty safe bet we're looking at a valid 2-line
element set and not just some random text that might pass
as orbital data based on a simple checksum calculation alone. */
int x;
unsigned sum1, sum2;
/* Compute checksum for each line */
for (x=0, sum1=0, sum2=0; x<=67; sum1+=val[(int)line1[x]], sum2+=val[(int)line2[x]], x++);
/* Perform a "torture test" on the data */
x=(val[(int)line1[68]]^(sum1%10)) | (val[(int)line2[68]]^(sum2%10)) |
(line1[0]^'1') | (line1[1]^' ') | (line1[7]^'U') |
(line1[8]^' ') | (line1[17]^' ') | (line1[23]^'.') |
(line1[32]^' ') | (line1[34]^'.') | (line1[43]^' ') |
(line1[52]^' ') | (line1[61]^' ') | (line1[62]^'0') |
(line1[63]^' ') | (line2[0]^'2') | (line2[1]^' ') |
(line2[7]^' ') | (line2[11]^'.') | (line2[16]^' ') |
(line2[20]^'.') | (line2[25]^' ') | (line2[33]^' ') |
(line2[37]^'.') | (line2[42]^' ') | (line2[46]^'.') |
(line2[51]^' ') | (line2[54]^'.') | (line1[2]^line2[2]) |
(line1[3]^line2[3]) | (line1[4]^line2[4]) |