-
Notifications
You must be signed in to change notification settings - Fork 37
/
rftrace.c
838 lines (701 loc) · 19.5 KB
/
rftrace.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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "sgdp4h.h"
#include "satutl.h"
#include "rftime.h"
#include "rftrace.h"
#include <sys/time.h>
#include <time.h>
#include "rftles.h"
#define LIM 80
#define D2R M_PI/180.0
#define R2D 180.0/M_PI
#define XKMPER 6378.135 // Earth radius in km
#define XKMPAU 149597879.691 // AU in km
#define FLAT (1.0/298.257)
#define C 299792.458 // Speed of light in km/s
struct point {
xyz_t obspos,obsvel;
xyz_t grpos,grvel;
};
struct site {
int id;
double lng,lat;
float alt;
char observer[64];
};
// Return x modulo y [0,y)
double modulo(double x,double y)
{
x=fmod(x,y);
if (x<0.0) x+=y;
return x;
}
// Read a line of maximum length int lim from file FILE into string s
int fgetline(FILE *file,char *s,int lim)
{
int c,i=0;
while (--lim > 0 && (c=fgetc(file)) != EOF && c != '\n')
s[i++] = c;
// if (c == '\n')
// s[i++] = c;
s[i] = '\0';
return i;
}
// 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;
}
// Greenwich Mean Sidereal Time
double dgmst(double mjd)
{
double t,dgmst;
t=(mjd-51544.5)/36525.0;
dgmst=360.98564736629+t*(0.000387933-t/38710000);
return dgmst;
}
// Observer position
void obspos_xyz(double mjd,double lng,double lat,float alt,xyz_t *pos,xyz_t *vel)
{
double ff,gc,gs,theta,s,dtheta;
s=sin(lat*D2R);
ff=sqrt(1.0-FLAT*(2.0-FLAT)*s*s);
gc=1.0/ff+alt/XKMPER;
gs=(1.0-FLAT)*(1.0-FLAT)/ff+alt/XKMPER;
theta=gmst(mjd)+lng;
dtheta=dgmst(mjd)*D2R/86400;
pos->x=gc*cos(lat*D2R)*cos(theta*D2R)*XKMPER;
pos->y=gc*cos(lat*D2R)*sin(theta*D2R)*XKMPER;
pos->z=gs*sin(lat*D2R)*XKMPER;
vel->x=-gc*cos(lat*D2R)*sin(theta*D2R)*XKMPER*dtheta;
vel->y=gc*cos(lat*D2R)*cos(theta*D2R)*XKMPER*dtheta;
vel->z=0.0;
return;
}
// Convert equatorial into horizontal coordinates
void equatorial2horizontal(double mjd,double ra,double de,double lng,double lat,double *azi,double *alt)
{
double h;
h=gmst(mjd)+lng-ra;
*azi=modulo(atan2(sin(h*D2R),cos(h*D2R)*sin(lat*D2R)-tan(de*D2R)*cos(lat*D2R))*R2D,360.0);
*alt=asin(sin(lat*D2R)*sin(de*D2R)+cos(lat*D2R)*cos(de*D2R)*cos(h*D2R))*R2D;
return;
}
// Get observing site
struct site get_site(int site_id)
{
int i=0,status;
char line[LIM];
FILE *file;
int id;
double lat,lng;
float alt;
char abbrev[3],observer[64];
struct site s;
char *env,filename[LIM];
env=getenv("ST_DATADIR");
if(env==NULL||strlen(env)==0)
env=".";
sprintf(filename,"%s/data/sites.txt",env);
file=fopen(filename,"r");
if (file==NULL) {
printf("File with site information not found!\n");
return s;
}
while (fgets(line,LIM,file)!=NULL) {
// Skip
if (strstr(line,"#")!=NULL)
continue;
// Strip newline
line[strlen(line)-1]='\0';
// Read data
status=sscanf(line,"%4d %2s %lf %lf %f",
&id,abbrev,&lat,&lng,&alt);
strcpy(observer,line+38);
// Change to km
alt/=1000.0;
// Copy site
if (id==site_id) {
s.lat=lat;
s.lng=lng;
s.alt=alt;
s.id=id;
strcpy(s.observer,observer);
}
}
fclose(file);
return s;
}
// Identify trace
void identify_trace_graves(char *tlefile,struct trace t,int satno,char *freqlist)
{
int i,imode,flag=0,status,imid;
struct point *p;
struct site s,sg;
double *v,*vg;
tle_t *tle;
xyz_t satpos,satvel;
FILE *file;
double dx,dy,dz,dvx,dvy,dvz,r,za;
double sum1,sum2,beta,freq0,rms,mjd0;
char nfd[32],nfdmin[32],text[16];
char * satnamemin = NULL;
int satnomin;
double rmsmin,freqmin,altmin,azimin;
double ra,de,azi,alt;
// Reloop stderr
if (freopen("/tmp/stderr.txt","w",stderr)==NULL)
fprintf(stderr,"Failed to redirect stderr\n");
// Get sites
s=get_site(t.site);
sg=get_site(9999);
// Allocate
p=(struct point *) malloc(sizeof(struct point)*t.n);
v=(double *) malloc(sizeof(double)*t.n);
vg=(double *) malloc(sizeof(double)*t.n);
// Get observer position
for (i=0;i<t.n;i++) {
obspos_xyz(t.mjd[i],s.lng,s.lat,s.alt,&p[i].obspos,&p[i].obsvel);
obspos_xyz(t.mjd[i],sg.lng,sg.lat,sg.alt,&p[i].grpos,&p[i].grvel);
}
printf("Fitting trace:\n");
// Mid point
imid=t.n/2;
// Load TLEs
tle_array_t *tle_array = load_tles(tlefile);
if (tle_array->number_of_elements == 0) {
fprintf(stderr,"TLE file %s not found or empty\n", tlefile);
return;
}
for (long elem = 0; elem < tle_array->number_of_elements; elem++) {
// Get TLE
tle = get_tle_by_index(tle_array, elem);
// Initialize
imode=init_sgdp4(&(tle->orbit));
if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n", tle->orbit.satno);
continue;
}
// Loop over points
for (i=0,sum1=0.0,sum2=0.0;i<t.n;i++) {
// Get satellite position
satpos_xyz(t.mjd[i]+2400000.5,&satpos,&satvel);
dx=satpos.x-p[i].obspos.x;
dy=satpos.y-p[i].obspos.y;
dz=satpos.z-p[i].obspos.z;
dvx=satvel.x-p[i].obsvel.x;
dvy=satvel.y-p[i].obsvel.y;
dvz=satvel.z-p[i].obsvel.z;
r=sqrt(dx*dx+dy*dy+dz*dz);
v[i]=(dvx*dx+dvy*dy+dvz*dz)/r;
za=acos((p[i].obspos.x*dx+p[i].obspos.y*dy+p[i].obspos.z*dz)/(r*XKMPER))*R2D;
if (i==imid) {
ra=modulo(atan2(dy,dx)*R2D,360.0);
de=asin(dz/r)*R2D;
equatorial2horizontal(t.mjd[i],ra,de,s.lng,s.lat,&azi,&alt);
}
dx=satpos.x-p[i].grpos.x;
dy=satpos.y-p[i].grpos.y;
dz=satpos.z-p[i].grpos.z;
dvx=satvel.x-p[i].grvel.x;
dvy=satvel.y-p[i].grvel.y;
dvz=satvel.z-p[i].grvel.z;
r=sqrt(dx*dx+dy*dy+dz*dz);
vg[i]=(dvx*dx+dvy*dy+dvz*dz)/r;
// t[j].freq[i]=(1.0-v/C)*(1.0-vg/C)*freq0;
// if (!((azi<90.0 || azi>270.0) && alt>15.0 && alt<40.0))
// t[j].za[i]=100.0;
}
freq0=143050000.0;
// Compute residuals
for (i=0,rms=0.0;i<t.n;i++)
rms+=pow(t.freq[i]-(1.0-v[i]/C)*(1.0-vg[i]/C)*freq0,2);
rms=sqrt(rms/(double) t.n);
// Find TCA
for (i=1,mjd0=0.0;i<t.n;i++)
if (v[i]*v[i-1]<0.0)
mjd0=t.mjd[i];
if (mjd0>0.0)
mjd2nfd(mjd0,nfd);
else
strcpy(nfd,"0000-00-00T00:00:00");
if (rms<1000) {
if (rms<50.0) {
if (tle->name) {
printf("%05d %s %8.1f Hz (%.1f,%.1f) | %s\n", tle->orbit.satno, nfd, rms, modulo(azi+180.0,360.0), alt, tle->name);
} else {
printf("%05d %s %8.1f Hz (%.1f,%.1f)\n", tle->orbit.satno, nfd, rms, modulo(azi+180.0,360.0), alt);
}
}
// printf("%05d: %s %8.3f MHz %8.3f kHz\n",tle->orbit.satno,nfd,1e-6*freq0,1e-3*rms);
if (flag==0 || rms<rmsmin) {
satnomin=tle->orbit.satno;
satnamemin = tle->name;
strcpy(nfdmin,nfd);
freqmin=freq0;
rmsmin=rms;
altmin=alt;
azimin=azi;
flag=1;
}
}
}
fclose(stderr);
if (flag==1) {
printf("\nBest fitting object:\n");
if (satnamemin) {
printf("%05d - %s: %s %8.1f Hz (%.1f,%.1f)\n", satnomin, satnamemin, nfdmin, rmsmin, modulo(azimin+180.0,360.0), altmin);
} else {
printf("%05d: %s %8.1f Hz (%.1f,%.1f)\n", satnomin, nfdmin, rmsmin, modulo(azimin+180.0,360.0), altmin);
}
printf("Store frequency? [y/n]\n");
status=scanf("%s",text);
if (text[0]=='y') {
file=fopen(freqlist,"a");
fprintf(file,"%05d %lf\n",satnomin,1e-6*freqmin);
fclose(file);
file=fopen("log.txt","a");
fprintf(file,"%05d %lf %.3f %.19s\n",satnomin,1e-6*freqmin,1e-3*rmsmin,nfdmin);
fclose(file);
printf("Frequency stored\n\n");
}
} else {
printf("\nTrace not identified..\n");
}
// Free
free_tles(tle_array);
free(p);
free(v);
free(vg);
return;
}
// Identify trace
void identify_trace(char *tlefile,struct trace t,int satno,char *freqlist)
{
int i,imode,flag=0,status;
struct point *p;
struct site s;
double *v;
tle_t *tle;
xyz_t satpos,satvel;
FILE *file;
double dx,dy,dz,dvx,dvy,dvz,r,za;
double sum1,sum2,beta,freq0,rms,mjd0;
char nfd[32],nfdmin[32],text[16];
char * satnamemin = NULL;
int satnomin;
double rmsmin,freqmin;
struct timeval tv;
char tbuf[30];
// Reloop stderr
if (freopen("/tmp/stderr.txt","w",stderr)==NULL)
fprintf(stderr,"Failed to redirect stderr\n");
// Get site
s=get_site(t.site);
// Allocate
p=(struct point *) malloc(sizeof(struct point)*t.n);
v=(double *) malloc(sizeof(double)*t.n);
// Get observer position
for (i=0;i<t.n;i++)
obspos_xyz(t.mjd[i],s.lng,s.lat,s.alt,&p[i].obspos,&p[i].obsvel);
printf("Fitting trace:\n");
// Load TLEs
tle_array_t *tle_array = load_tles(tlefile);
if (tle_array->number_of_elements == 0) {
fprintf(stderr,"TLE file %s not found or empty\n", tlefile);
return;
}
for (long elem = 0; elem < tle_array->number_of_elements; elem++) {
// Get TLE
tle = get_tle_by_index(tle_array, elem);
// Initialize
imode=init_sgdp4(&(tle->orbit));
if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n", tle->orbit.satno);
continue;
}
// Loop over points
for (i=0,sum1=0.0,sum2=0.0;i<t.n;i++) {
// Get satellite position
satpos_xyz(t.mjd[i]+2400000.5,&satpos,&satvel);
dx=satpos.x-p[i].obspos.x;
dy=satpos.y-p[i].obspos.y;
dz=satpos.z-p[i].obspos.z;
dvx=satvel.x-p[i].obsvel.x;
dvy=satvel.y-p[i].obsvel.y;
dvz=satvel.z-p[i].obsvel.z;
r=sqrt(dx*dx+dy*dy+dz*dz);
v[i]=(dvx*dx+dvy*dy+dvz*dz)/r;
za=acos((p[i].obspos.x*dx+p[i].obspos.y*dy+p[i].obspos.z*dz)/(r*XKMPER))*R2D;
beta=(1.0-v[i]/C);
sum1+=beta*t.freq[i];
sum2+=beta*beta;
}
freq0=sum1/sum2;
// Compute residuals
for (i=0,rms=0.0;i<t.n;i++)
rms+=pow(t.freq[i]-(1.0-v[i]/C)*freq0,2);
rms=sqrt(rms/(double) t.n);
// Find TCA
for (i=1,mjd0=0.0;i<t.n;i++)
if (v[i]*v[i-1]<0.0)
mjd0=t.mjd[i];
if (mjd0>0.0)
mjd2nfd(mjd0,nfd);
else
strcpy(nfd,"0000-00-00T00:00:00");
if (rms<1000) {
if (tle->name) {
printf("%05d %s %8.3f MHz %8.3f kHz | %s\n", tle->orbit.satno, nfd, 1e-6*freq0, 1e-3*rms, tle->name);
} else {
printf("%05d %s %8.3f MHz %8.3f kHz\n", tle->orbit.satno, nfd, 1e-6*freq0, 1e-3*rms);
}
if (flag==0 || rms<rmsmin) {
satnomin=tle->orbit.satno;
satnamemin = tle->name;
strcpy(nfdmin,nfd);
freqmin=freq0;
rmsmin=rms;
flag=1;
}
}
}
fclose(stderr);
if (flag==1) {
printf("\nBest fitting object:\n");
if (satnamemin) {
printf("%05d %s %8.3f MHz %8.3f kHz | %s\n", satnomin, nfdmin, 1e-6*freqmin, 1e-3*rmsmin, satnamemin);
} else {
printf("%05d %s %8.3f MHz %8.3f kHz\n", satnomin, nfdmin, 1e-6*freqmin, 1e-3*rmsmin);
}
printf("Store frequency? [y/n]\n");
status=scanf("%s",text);
if (text[0]=='y') {
gettimeofday(&tv,0);
strftime(tbuf,30,"%Y-%m-%dT%T",gmtime(&tv.tv_sec));
file=fopen(freqlist,"a");
fprintf(file,"%05d %lf %.19s %04d\n",satnomin,1e-6*freqmin,tbuf,s.id);
fclose(file);
file=fopen("log.txt","a");
fprintf(file,"%05d %lf %.3f %.19s\n",satnomin,1e-6*freqmin,1e-3*rmsmin,nfdmin);
fclose(file);
printf("Frequency stored\n\n");
}
} else {
printf("\nTrace not identified..\n");
}
// Free
free_tles(tle_array);
free(p);
free(v);
return;
}
// Is it a classified satellite
int is_classified(int satno)
{
int flag=0,no;
char *env,tlefile[128],line[LIM];
FILE *file;
// Get classfd.tle path
env=getenv("ST_TLEDIR");
if(env==NULL||strlen(env)==0)
env=".";
sprintf(tlefile,"%s/classfd.tle",env);
// Does it exist
file=fopen(tlefile,"r");
if (file==NULL) {
printf("%s not found\n",tlefile);
flag=0;
} else {
// Loop over TLEs
while (fgetline(file,line,LIM)>0) {
// Use 1st TLE line
if (line[0]=='1') {
sscanf(line+2,"%d",&no);
if (no==satno) flag=1;
}
}
fclose(file);
}
return flag;
}
// Compute trace
struct trace *compute_trace(char *tlefile,double *mjd,int n,int site_id,float freq,float bw,int *nsat,int graves,char *freqlist)
{
int i,j,imode,flag,satno,tflag,m,status,hastle;
struct point *p;
struct site s,sg;
FILE *file,*infile;
tle_t *tle;
xyz_t satpos,satvel;
double dx,dy,dz,dvx,dvy,dvz,r,v,za,vg;
double freq0,dfreq;
char * line = NULL;
size_t line_size = 0;
struct trace *t;
float fmin,fmax;
double ra,de,azi,alt;
// Maximum doppler offset (assumes max 20km/s velocity)
dfreq=20.0/299792.458*freq;
// Frequency limits
fmin=freq-0.5*bw-dfreq;
fmax=freq+0.5*bw+dfreq;
// Reloop stderr
if (freopen("/tmp/stderr.txt","w",stderr)==NULL)
fprintf(stderr,"Failed to redirect stderr\n");
// Find number of satellites in frequency range
infile=fopen(freqlist,"r");
if (infile==NULL) {
printf("%s not found\n",freqlist);
*nsat=0;
return NULL;
} else {
for (i=0;;) {
if (getline(&line, &line_size, infile) < 0)
break;
if ((line[0] == '\n') || (line[0] == '#'))
continue;
status=sscanf(line,"%d %lf",&satno,&freq0);
if (status != 2) {
printf("Frequency line not parsable, skipping:\n%s\n", line);
continue;
}
if (graves==1 && fabs(freq0-143.050)<1e-3)
i++;
else if (freq0>=fmin && freq0<=fmax && graves==0)
i++;
}
fclose(infile);
*nsat=i;
}
// Free allocated buffer by getline() as we might return NULL
// before next call to to getline()
free(line);
line = NULL;
// Break out
if (i==0)
{
*nsat=0;
return NULL;
}
// Valid MJDs
for (i=0;i<n;i++)
if (mjd[i]==0.0)
break;
m=i;
// Allocate traces
t=(struct trace *) malloc(sizeof(struct trace)* *nsat);
// Get site
s=get_site(site_id);
// Allocate
p=(struct point *) malloc(sizeof(struct point)*m);
// Get observer position
for (i=0;i<m;i++)
obspos_xyz(mjd[i],s.lng,s.lat,s.alt,&p[i].obspos,&p[i].obsvel);
// Compute Graves positions
if (graves==1) {
sg=get_site(9999);
for (i=0;i<m;i++)
obspos_xyz(mjd[i],sg.lng,sg.lat,sg.alt,&p[i].grpos,&p[i].grvel);
}
// Load TLEs
tle_array_t *tle_array = load_tles(tlefile);
if (tle_array->number_of_elements == 0) {
fprintf(stderr,"TLE file %s not found or empty\n", tlefile);
return NULL;
}
infile=fopen(freqlist,"r");
for (j=0;;) {
if (getline(&line, &line_size, infile) < 0)
break;
if ((line[0] == '\n') || (line[0] == '#'))
continue;
status=sscanf(line,"%d %lf",&satno,&freq0);
if (status != 2) {
// Error about not being able to parse line already reported
// while counting lines, don't repeat ourselves
continue;
}
flag=0;
if (graves==1 && fabs(freq0-143.050)<1e-3)
flag=1;
else if (freq0>=fmin && freq0<=fmax && graves==0)
flag=1;
if (flag==0)
continue;
// Allocate
t[j].satname[0] = '\0';
t[j].satno=satno;
t[j].site=site_id;
t[j].n=m;
t[j].freq0=freq0;
t[j].mjd=(double *) malloc(sizeof(double)*m);
t[j].freq=(double *) malloc(sizeof(double)*m);
t[j].za=(float *) malloc(sizeof(float)*m);
t[j].classfd=is_classified(t[j].satno);
t[j].graves=graves;
// Get TLE
tle = get_tle_by_catalog_id(tle_array, satno);
if (tle) {
// Initialize
imode=init_sgdp4(&(tle->orbit));
if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n", tle->orbit.satno);
continue;
}
// Copy sat name into trace
if (tle->name != NULL) {
strncpy(t[j].satname, tle->name, 25);
}
// Loop over points
for (i=0,flag=0,tflag=0;i<m;i++) {
// Get satellite position
satpos_xyz(mjd[i]+2400000.5,&satpos,&satvel);
dx=satpos.x-p[i].obspos.x;
dy=satpos.y-p[i].obspos.y;
dz=satpos.z-p[i].obspos.z;
dvx=satvel.x-p[i].obsvel.x;
dvy=satvel.y-p[i].obsvel.y;
dvz=satvel.z-p[i].obsvel.z;
r=sqrt(dx*dx+dy*dy+dz*dz);
v=(dvx*dx+dvy*dy+dvz*dz)/r;
za=acos((p[i].obspos.x*dx+p[i].obspos.y*dy+p[i].obspos.z*dz)/(r*XKMPER))*R2D;
// Store
t[j].mjd[i]=mjd[i];
t[j].freq[i]=(1.0-v/C)*freq0;
t[j].za[i]=za;
// Compute Graves velocity/frequency
if (graves==1) {
dx=satpos.x-p[i].grpos.x;
dy=satpos.y-p[i].grpos.y;
dz=satpos.z-p[i].grpos.z;
dvx=satvel.x-p[i].grvel.x;
dvy=satvel.y-p[i].grvel.y;
dvz=satvel.z-p[i].grvel.z;
r=sqrt(dx*dx+dy*dy+dz*dz);
vg=(dvx*dx+dvy*dy+dvz*dz)/r;
ra=modulo(atan2(dy,dx)*R2D,360.0);
de=asin(dz/r)*R2D;
equatorial2horizontal(mjd[i],ra,de,sg.lng,sg.lat,&azi,&alt);
t[j].freq[i]=(1.0-v/C)*(1.0-vg/C)*freq0;
if (!((azi<90.0 || azi>270.0) && alt>15.0 && alt<40.0))
t[j].za[i]=100.0;
}
}
// Increment
j++;
}
}
fclose(infile);
fclose(stderr);
// Free
free(line);
line = NULL;
free_tles(tle_array);
free(p);
// Update counter
*nsat=j;
return t;
}
// Compute trace
void compute_doppler(char *tlefile,double *mjd,int n,int site_id,int satno,int graves, int skiphigh, char *outfname)
{
int i,j,imode,flag,tflag,m,status;
struct point *p;
struct site s,sg;
FILE *outfile;
tle_t *tle;
xyz_t satpos,satvel;
double dx,dy,dz,dvx,dvy,dvz,r,v,rg,vg;
double freq0;
char line[LIM],text[8];
struct trace *t;
float fmin,fmax;
double ra,de,azi,alt;
double rag,deg,azig,altg;
// Reloop stderr
if (freopen("/tmp/stderr.txt","w",stderr)==NULL)
fprintf(stderr,"Failed to redirect stderr\n");
// Get site
s=get_site(site_id);
// Allocate
p=(struct point *) malloc(sizeof(struct point)*n);
// Get observer position
for (i=0;i<n;i++)
obspos_xyz(mjd[i],s.lng,s.lat,s.alt,&p[i].obspos,&p[i].obsvel);
// Compute Graves positions
if (graves==1) {
sg=get_site(9999);
for (i=0;i<n;i++)
obspos_xyz(mjd[i],sg.lng,sg.lat,sg.alt,&p[i].grpos,&p[i].grvel);
}
// Open output file
outfile=fopen(outfname, "w");
// Print header
if (graves==1)
fprintf(outfile, "# satno mjd r v azi alt rg vg azig altg\n");
else
fprintf(outfile, "# satno mjd r v azi alt\n");
// Load TLEs
tle_array_t *tle_array = load_tles(tlefile);
if (tle_array->number_of_elements == 0) {
fprintf(stderr,"TLE file %s not found or empty\n", tlefile);
return;
}
// Get TLE
tle = get_tle_by_catalog_id(tle_array, satno);
// Skip high satellites
if (tle && !(skiphigh == 1 && tle->orbit.rev < 10.0)) {
// Initialize
imode=init_sgdp4(&(tle->orbit));
if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n", tle->orbit.satno);
}
// Loop over points
for (i=0,flag=0,tflag=0;i<n;i++) {
// Get satellite position
satpos_xyz(mjd[i]+2400000.5,&satpos,&satvel);
dx=satpos.x-p[i].obspos.x;
dy=satpos.y-p[i].obspos.y;
dz=satpos.z-p[i].obspos.z;
dvx=satvel.x-p[i].obsvel.x;
dvy=satvel.y-p[i].obsvel.y;
dvz=satvel.z-p[i].obsvel.z;
r=sqrt(dx*dx+dy*dy+dz*dz);
v=(dvx*dx+dvy*dy+dvz*dz)/r;
ra=modulo(atan2(dy,dx)*R2D,360.0);
de=asin(dz/r)*R2D;
equatorial2horizontal(mjd[i],ra,de,s.lng,s.lat,&azi,&alt);
// Compute Graves velocity/frequency
if (graves==1) {
dx=satpos.x-p[i].grpos.x;
dy=satpos.y-p[i].grpos.y;
dz=satpos.z-p[i].grpos.z;
dvx=satvel.x-p[i].grvel.x;
dvy=satvel.y-p[i].grvel.y;
dvz=satvel.z-p[i].grvel.z;
rg=sqrt(dx*dx+dy*dy+dz*dz);
vg=(dvx*dx+dvy*dy+dvz*dz)/rg;
rag=modulo(atan2(dy,dx)*R2D,360.0);
deg=asin(dz/rg)*R2D;
equatorial2horizontal(mjd[i],rag,deg,sg.lng,sg.lat,&azig,&altg);
fprintf(outfile,"%05d %14.8lf %f %f %f %f %f %f %f %f\n", tle->orbit.satno,mjd[i],r,v,azi,alt,rg,vg,azig,altg);
} else {
fprintf(outfile,"%05d %14.8lf %f %f %f %f\n", tle->orbit.satno,mjd[i],r,v,azi,alt);
}
}
}
fclose(outfile);
fclose(stderr);
// Free
free_tles(tle_array);
free(p);
return;
}