forked from cbassa/sattools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskymap.c
More file actions
3341 lines (2835 loc) · 72.7 KB
/
skymap.c
File metadata and controls
3341 lines (2835 loc) · 72.7 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <getopt.h>
#include <ctype.h>
#include <cpgplot.h>
#include <wcslib/cel.h>
#include "sgdp4h.h"
#define LIM 384
#define NMAX 256
#define MMAX 1024
#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 STDMAG 6.0
long Isat=0;
long Isatsel=0;
extern double SGDP4_jd0;
struct map {
double alpha0,delta0,ra0,de0,azi0,alt0,q;
double fov,mjd,gmst,w,wl,wb;
float length;
float minmag,maxmag,minrad,maxrad;
char orientation[LIM],projection[4],observer[32],camera[16];
char nfd[LIM],starfile[LIM],tlefile[LIM],iodfile[LIM],xyzfile[LIM];
char datadir[LIM],tledir[LIM];
float saltmin;
double lat,lng;
double h,sra,sde,sazi,salt;
float alt,timezone;
float fw,fh,agelimit;
int level,grid,site_id,plotstars,plotapex;
int leoflag,iodflag,iodpoint,visflag,planar,pssatno,psnr,xyzflag,pflag,graves;
float psrmin,psrmax,rvis;
} m;
struct sat {
long Isat;
char state[10];
float mag,age;
double jd;
double dx,dy,dz;
double x,y,z,vx,vy,vz;
double rsun,rearth,h;
double psun,pearth,p,phase;
double r,v,ra,de;
double azi,alt,salt;
double rx,ry;
double rg,vg,azig,altg,rag,deg;
int illumg;
};
struct star {
double ra,de;
float pmra,pmde;
float mag;
};
struct observation {
int ssn,site;
char iod_line[LIM];
double mjd,ra,de,azi,alt;
double lng,lat;
float elv;
float dt,st,dr,sr,dx,dy,t;
int flag;
};
struct coeff_lr {
int nd,nm,nm1,nf;
double sa,ca;
} clr[60];
struct coeff_b {
int nd,nm,nm1,nf;
double sa;
} cb[60];
int fgetline(FILE *,char *,int);
double modulo(double,double);
void reverse(double,double,double *,double *);
void forward(double,double,double *,double *);
void init_plot(char *,float,float);
void skymap_plot_renew(void);
double gmst(double);
double dgmst(double);
void skymap_plothorizontal_grid();
void skymap_plotequatorial_grid();
void skymap_plotconstellations(char *);
void equatorial2horizontal(double,double,double,double *,double *);
void graves_equatorial2horizontal(double,double,double,double *,double *);
void graves_horizontal2equatorial(double,double,double,double *,double *);
void horizontal2equatorial(double,double,double,double *,double *);
void skymap_plotstars(char *);
void obspos_xyz(double,xyz_t *,xyz_t *);
void sunpos_xyz(double,xyz_t *,double *,double *);
void graves_xyz(double,xyz_t *,xyz_t *);
void skymap_plotsatellite(char *,int,double,double);
double date2mjd(int,int,double);
struct sat apparent_position(double);
long identify_satellite(char *,int,double,float,float);
int plot_skymap(void);
void rotate(int,float,float *,float *,float *);
int print_tle(char *,int);
void mjd2date(double mjd,char *date);
void dec2sex(double x,char *s,int f,int len);
void precess(double mjd0,double ra0,double de0,double mjd,double *ra,double *de);
double nfd2mjd(char *date);
void nfd_now(char *s);
double sex2dec(char *s);
double doy2mjd(int year,double doy);
struct observation decode_iod_observation(char *iod_line);
void plot_iod(char *filename);
void get_site(int site_id);
void lunpos_xyz(double mjd,xyz_t *pos,double *ra,double *de);
void ecliptical2equatorial(double l,double b,double *ra,double *de);
void skymap_plotsun(void);
void skymap_plotmoon(void);
void mjd2date_iod(double mjd,char *date);
void dec2sex_iod(double x,char *s,int type);
void usage()
{
printf("skymap t:c:i:R:D:hs:d:l:P:r:V:p:A:E:S:L:B:H:\n\n");
printf("t date/time (yyyy-mm-ddThh:mm:ss.sss) [default: now]\n");
printf("c TLE catalog file [default: classfd.tle]\n");
printf("i satellite ID (NORAD) [default: all]\n");
printf("R R.A. [hh:mm:ss.sss]\n");
printf("D Decl. [+dd:mm:ss.ss]\n");
printf("A Azimuth (deg)\n");
printf("E Elevation (deg)\n");
printf("S All night\n");
printf("Q hide stars\n");
printf("a show all objects from catalog (default: LEO)\n");
printf("h this help\n");
printf("s site (COSPAR)\n");
printf("d IOD observations\n");
printf("l trail length [default: 60s]\n");
printf("P planar search satellite ID\n");
printf("r planar search altitude\n");
printf("V altitude for visibility contours\n");
printf("p file with xyz positions\n");
printf("L manual site longitude (deg)\n");
printf("B manual site latitude (deg)\n");
printf("H manual site elevation (m)\n");
return;
}
void interactive_usage() {
printf("i Identify satellite\n");
printf("\n");
printf("f Select satellite\n");
printf("a Select on age\n");
printf("m Measure cursor RA/Dec, Alt/Azi\n");
printf("\n");
printf("g Toggle grid (on/off)\n");
printf("o Toggle orientation (horizontal/equatorial)\n");
printf("P Toggle planar search\n");
printf("p Toggle satellite name\n");
printf("L Toggle satellite selection (All, LEO, HEO/GEO, none)\n");
printf("v Toggle visibility contours\n");
printf("F Toggle camera configuration (data/cameras.txt)\n");
printf("Q Toggle plotting stars\n");
printf("x Toggle plotting apex (GEO, HEO, NOSS)\n");
printf("\n");
printf("c Center on cursor\n");
printf("z Center on zenith\n");
printf("n Center on North\n");
printf("s Center on South\n");
printf("e Center on East\n");
printf("w Center on West\n");
printf("\n");
printf("1-9 Zoom level\n");
printf("+ Zoom in one level\n");
printf("- Zoom out one level\n");
printf("\n");
printf("l Set integration length\n");
printf("> Increase step size\n");
printf("< Decrease step size\n");
printf("\n");
printf(". Increase time by 1 step\n");
printf(", Decrease time by 1 step\n");
printf("\n");
printf("I Create IOD measurement for current time and position\n");
printf("TAB Cycle IOD observations\n");
printf("\n");
printf("S Save observation position/time to schedule\n");
printf("E Save observation end-time to schedule\n");
printf("\n");
printf("R Read catalog\n");
printf("r Reset satellite selection/real time\n");
printf("\n");
printf("q quit\n");
}
void init_skymap(void)
{
int i;
char *env,filename[128];
FILE *file;
// Default Map parameters
m.azi0=180;
m.alt0=90.0;
m.w=120.0;
m.wl=180.0;
m.wb=180.0;
m.level=1;
m.minmag=-2.0;
m.maxmag=5.0;
m.maxrad=2.0;
m.minrad=0.02;
strcpy(m.orientation,"horizontal");
strcpy(m.starfile,"hip6mag.dat");
strcpy(m.projection,"STG");
m.lat=0.0;
m.lng=0.0;
m.alt=0.0;
m.timezone=+0.0;
m.grid=1;
m.length=60.0;
m.mjd=-1.0;
m.leoflag=1;
m.iodflag=0;
m.xyzflag=0;
m.visflag=0;
m.planar=0;
m.agelimit=-1.0;
m.saltmin=-6.0;
m.pflag=1;
m.graves=0;
m.plotstars=1;
m.plotapex=1;
// Default settings
strcpy(m.observer,"Unknown");
m.site_id=0;
// Get environment variables
env=getenv("ST_DATADIR");
if (env!=NULL) {
strcpy(m.datadir,env);
} else {
printf("ST_DATADIR environment variable not found.\n");
}
env=getenv("ST_COSPAR");
if (env!=NULL) {
get_site(atoi(env));
} else {
printf("ST_COSPAR environment variable not found.\n");
}
env=getenv("ST_TLEDIR");
if (env!=NULL) {
strcpy(m.tledir,env);
} else {
printf("ST_TLEDIR environment variable not found.\n");
}
sprintf(m.tlefile,"%s/classfd.tle",m.tledir);
// Read LR coefficients
sprintf(filename,"%s/data/moonLR.dat",m.datadir);
file=fopen(filename,"r");
for (i=0;i<60;i++)
fscanf(file,"%d %d %d %d %lf %lf",&clr[i].nd,&clr[i].nm,&clr[i].nm1,&clr[i].nf,&clr[i].sa,&clr[i].ca);
fclose(file);
// Read B coefficients
sprintf(filename,"%s/data/moonB.dat",m.datadir);
file=fopen(filename,"r");
for (i=0;i<60;i++)
fscanf(file,"%d %d %d %d %lf",&cb[i].nd,&cb[i].nm,&cb[i].nm1,&cb[i].nf,&cb[i].sa);
fclose(file);
return;
}
// Get observing site
void get_site(int site_id)
{
int i=0;
char line[LIM];
FILE *file;
int id;
double lat,lng;
float alt;
char abbrev[3],observer[64],filename[LIM];
sprintf(filename,"%s/data/sites.txt",m.datadir);
file=fopen(filename,"r");
if (file==NULL) {
printf("File with site information not found!\n");
return;
}
while (fgets(line,LIM,file)!=NULL) {
// Skip
if (strstr(line,"#")!=NULL)
continue;
// Strip newline
line[strlen(line)-1]='\0';
// Read data
sscanf(line,"%4d %2s %lf %lf %f",
&id,abbrev,&lat,&lng,&alt);
strcpy(observer,line+38);
// Change to km
alt/=1000.0;
if (id==site_id) {
m.lat=lat;
m.lng=lng;
m.alt=alt;
m.site_id=id;
strcpy(m.observer,observer);
}
}
fclose(file);
return;
}
void read_iod(char *filename,int iobs)
{
int i=0;
char line[LIM];
FILE *file;
struct observation obs;
file=fopen(filename,"r");
// Read data
while (fgets(line,LIM,file)!=NULL) {
if (strlen(line)<10)
continue;
if (strstr(line,"#")==NULL) {
obs=decode_iod_observation(line);
if (i==iobs) {
printf("%s\n",obs.iod_line);
break;
}
i++;
}
}
fclose(file);
// Set parameters
get_site(obs.site);
m.mjd=obs.mjd;
m.ra0=obs.ra;
m.de0=obs.de;
strcpy(m.orientation,"equatorial");
m.level=4;
return;
}
void plot_apex(float h,float beta)
{
int i;
xyz_t obspos,obsvel;
xyz_t satpos;
double rr,theta,dx,dy,dz,r,ra,de,azi,alt,rx,ry;
obspos_xyz(m.mjd,&obspos,&obsvel);
rr=h+XKMPER;
cpgsci(3);
for (theta=0.0;theta<=360.0;theta+=1.0) {
satpos.x=rr*cos(theta*D2R)*cos(beta*D2R);
satpos.y=rr*sin(theta*D2R)*cos(beta*D2R);
satpos.z=rr*sin(beta*D2R);
// Position differences
dx=satpos.x-obspos.x;
dy=satpos.y-obspos.y;
dz=satpos.z-obspos.z;
// Celestial position
r=sqrt(dx*dx+dy*dy+dz*dz);
ra=modulo(atan2(dy,dx)*R2D,360.0);
de=asin(dz/r)*R2D;
// Convert and project
if (strcmp(m.orientation,"horizontal")==0) {
equatorial2horizontal(m.mjd,ra,de,&azi,&alt);
forward(azi,alt,&rx,&ry);
} else if (strcmp(m.orientation,"equatorial")==0) {
forward(ra,de,&rx,&ry);
}
if (theta==0.0)
cpgmove((float) rx,(float) ry);
else
cpgdraw((float) rx,(float) ry);
}
cpgsci(1);
return;
}
// Plot XYZ point
void plot_xyz(double mjd0,char *filename)
{
struct sat s;
double jd,rsun,rearth,rsat;
double dx,dy,dz,dvx,dvy,dvz;
xyz_t satpos,obspos,obsvel,satvel,sunpos;
double sra,sde,mjd,mjd1;
FILE *file;
char line[LIM];
file=fopen(filename,"r");
while (fgetline(file,line,LIM)>0) {
sscanf(line,"%lf %lf %lf %lf",&mjd,&satpos.x,&satpos.y,&satpos.z);
if (mjd>mjd0)
break;
}
fclose(file);
// Get positions
obspos_xyz(mjd0,&obspos,&obsvel);
sunpos_xyz(mjd0,&sunpos,&sra,&sde);
// Age
s.age=0.0;
// Sat positions
s.x=satpos.x;
s.y=satpos.y;
s.z=satpos.z;
s.vx=satvel.x;
s.vy=satvel.y;
s.vz=satvel.z;
// Sun position from satellite
dx=-satpos.x+sunpos.x;
dy=-satpos.y+sunpos.y;
dz=-satpos.z+sunpos.z;
// Distances
rsun=sqrt(dx*dx+dy*dy+dz*dz);
rearth=sqrt(satpos.x*satpos.x+satpos.y*satpos.y+satpos.z*satpos.z);
s.h=rearth-XKMPER;
// Angles
s.psun=asin(696.0e3/rsun)*R2D;
s.pearth=asin(6378.135/rearth)*R2D;
s.p=acos((-dx*satpos.x-dy*satpos.y-dz*satpos.z)/(rsun*rearth))*R2D;
// Visibility state
if (s.p-s.pearth<-s.psun)
strcpy(s.state,"eclipsed");
else if (s.p-s.pearth>-s.psun && s.p-s.pearth<s.psun)
strcpy(s.state,"umbra");
else if (s.p-s.pearth>s.psun)
strcpy(s.state,"sunlit");
// Position differences
dx=satpos.x-obspos.x;
dy=satpos.y-obspos.y;
dz=satpos.z-obspos.z;
dvx=satvel.x-obsvel.x;
dvy=satvel.y-obsvel.y;
dvz=satvel.z-obsvel.z;
// Celestial position
s.r=sqrt(dx*dx+dy*dy+dz*dz);
s.v=(dvx*dx+dvy*dy+dvz*dz)/s.r;
s.ra=modulo(atan2(dy,dx)*R2D,360.0);
s.de=asin(dz/s.r)*R2D;
// Phase
s.phase=acos(((obspos.x-satpos.x)*(sunpos.x-satpos.x)+(obspos.y-satpos.y)*(sunpos.y-satpos.y)+(obspos.z-satpos.z)*(sunpos.z-satpos.z))/(rsun*s.r))*R2D;
// Magnitude
if (strcmp(s.state,"sunlit")==0)
s.mag=STDMAG-15.0+5*log10(s.r)-2.5*log10(sin(s.phase*D2R)+(M_PI-s.phase*D2R)*cos(s.phase*D2R));
else
s.mag=15;
// Convert and project
if (strcmp(m.orientation,"horizontal")==0) {
equatorial2horizontal(mjd,s.ra,s.de,&s.azi,&s.alt);
forward(s.azi,s.alt,&s.rx,&s.ry);
} else if (strcmp(m.orientation,"equatorial")==0) {
forward(s.ra,s.de,&s.rx,&s.ry);
}
cpgsci(3);
cpgpt1(s.rx,s.ry,17);
cpgsch(0.6);
cpgtext(s.rx,s.ry," xyz");
cpgsch(1.0);
cpgsci(1);
printf("%s %6.3f %6.3f %.1f km\n",m.nfd,s.ra,s.de,s.r);
return;
}
// Write out the current position and time in IOD format
void format_iod(double mjd,double ra,double de,int site)
{
char nfd[32];
double mjd0,ra0,de0;
char sra[16],sde[16];
// Get date/time
mjd2date_iod(mjd,nfd);
// Precess position to J2000
mjd0=51544.5;
precess(mjd,ra,de,mjd0,&ra0,&de0);
dec2sex_iod(ra0/15.0,sra,0);
dec2sex_iod(de0,sde,1);
// Print IOD line
printf("99999 99 999A %04d G %s 17 25 %s%s 37 S\n",site,nfd,sra,sde);
return;
}
void allnight(void)
{
int flag;
xyz_t sunpos;
double ra,de,azi,alt,alt0;
double mjd,mjdrise=-1.0,mjdset=-1.0;
char nfd[32];
// Find solar altitude at reference time
sunpos_xyz(m.mjd,&sunpos,&ra,&de);
equatorial2horizontal(m.mjd,ra,de,&azi,&alt);
// Sun below limit, find rise, then set
if (alt<m.saltmin) {
for (flag=0,mjd=m.mjd;mjd<m.mjd+0.5;mjd+=1.0/86400) {
sunpos_xyz(mjd,&sunpos,&ra,&de);
equatorial2horizontal(mjd,ra,de,&azi,&alt);
if (flag!=0) {
if (alt>m.saltmin && alt0<=m.saltmin)
mjdrise=mjd;
}
if (flag==0)
flag=1;
alt0=alt;
}
for (flag=0,mjd=m.mjd-0.5;mjd<m.mjd;mjd+=1.0/86400) {
sunpos_xyz(mjd,&sunpos,&ra,&de);
equatorial2horizontal(mjd,ra,de,&azi,&alt);
if (flag!=0) {
if (alt<m.saltmin && alt0>=m.saltmin)
mjdset=mjd;
}
if (flag==0)
flag=1;
alt0=alt;
}
// Sun above limit, find set, and rise
} else {
for (flag=0,mjd=m.mjd;mjd<m.mjd+1.0;mjd+=1.0/86400) {
sunpos_xyz(mjd,&sunpos,&ra,&de);
equatorial2horizontal(mjd,ra,de,&azi,&alt);
if (flag!=0) {
if (alt>m.saltmin && alt0<=m.saltmin)
mjdrise=mjd;
if (alt<m.saltmin && alt0>=m.saltmin)
mjdset=mjd;
}
if (flag==0)
flag=1;
alt0=alt;
}
}
m.mjd=mjdset;
mjd2date(m.mjd,m.nfd);
mjd2date(mjdrise,nfd);
printf("%s %s\n",m.nfd,nfd);
return;
}
int main(int argc,char *argv[])
{
int i,arg=0,isite=0;
double lat,lng;
float alt;
// Redirect stderr
freopen("/dev/null","w",stderr);
init_skymap();
// Decode options
while ((arg=getopt(argc,argv,"t:c:i:R:D:hs:d:l:P:r:V:p:A:E:S:QaL:B:H:"))!=-1) {
switch(arg) {
case 't':
strcpy(m.nfd,optarg);
m.mjd=nfd2mjd(m.nfd);
m.iodpoint=-1;
break;
case 'S':
m.saltmin=atof(optarg);
allnight();
break;
case 'L':
lng=(double) atof(optarg);
isite++;
break;
case 'B':
lat=(double) atof(optarg);
isite++;
break;
case 'H':
alt=atof(optarg);
isite++;
break;
case 'c':
strcpy(m.tlefile,optarg);
break;
case 'd':
strcpy(m.iodfile,optarg);
m.iodpoint=0;
m.leoflag=0;
read_iod(m.iodfile,m.iodpoint);
m.iodflag=1;
break;
case 'l':
m.length=atof(optarg);
break;
case 's':
get_site(atoi(optarg));
break;
case 'i':
Isatsel=atoi(optarg);
m.leoflag=0;
break;
case 'P':
m.planar=1;
m.pssatno=atoi(optarg);
m.psrmin=300;
m.psrmax=1000;
m.psnr=8;
break;
case 'p':
strcpy(m.xyzfile,optarg);
m.xyzflag=1;
break;
case 'r':
m.psrmin=atof(optarg);
m.psrmax=atof(optarg);
m.psnr=1;
break;
case 'V':
m.visflag=1;
m.rvis=atof(optarg);
break;
case 'R':
m.ra0=15.0*sex2dec(optarg);
strcpy(m.orientation,"equatorial");
m.level=5;
break;
case 'D':
m.de0=sex2dec(optarg);
strcpy(m.orientation,"equatorial");
m.level=5;
break;
case 'A':
m.azi0=modulo(atof(optarg)+180.0,360.0);
strcpy(m.orientation,"horizontal");
m.level=3;
break;
case 'E':
m.alt0=atof(optarg);
if (m.alt0>90.0)
m.alt0=90.0;
strcpy(m.orientation,"horizontal");
m.level=3;
break;
case 'Q':
m.plotstars=0;
break;
case 'a':
m.leoflag=0;
break;
case 'h':
usage();
return 0;
break;
default:
usage();
return 0;
}
}
// Set manual site
if (isite==3) {
m.lat=lat;
m.lng=lng;
m.alt=alt/1000.0;
m.site_id=0;
strcpy(m.observer,"Manual observer");
}
init_plot("/xs",0,0.75);
plot_skymap();
cpgend();
fclose(stderr);
return 0;
}
void plot_graves_visibility(void)
{
int i,j,k,nx=32,ny=8;
double azi,alt,ra,de;
double ax,ay,az,dr,dx,dy,dz,h,r,r1,r2,rx,ry;
xyz_t grvpos,grvvel,satpos,obspos,obsvel;
float azil[]={-90,-80,-70,-60,-50,-40,-30,-20,-10,0,10,20,30,40,50,60,70,80,90,90,90,90,90,90,80,70,60,50,40,30,20,10,0,-10,-20,-30,-40,-50,-60,-70,-80,-90,-90,-90,-90,-90,-90};
float altl[]={15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,20,25,30,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,35,30,25,20,15};
// Get observer and solar position
graves_xyz(m.mjd,&grvpos,&grvvel);
obspos_xyz(m.mjd,&obspos,&obsvel);
cpgsci(2);
for (h=300;h<=1200;h+=100) {
for (i=0;i<sizeof(azil)/sizeof(azil[0]);i++) {
graves_horizontal2equatorial(m.mjd,azil[i],altl[i],&ra,&de);
// Compute unit vector
ax=cos(ra*D2R)*cos(de*D2R);
ay=sin(ra*D2R)*cos(de*D2R);
az=sin(de*D2R);
// Find distance
for (k=0,r1=h;k<20;k++) {
dx=r1*ax;
dy=r1*ay;
dz=r1*az;
satpos.x=grvpos.x+dx;
satpos.y=grvpos.y+dy;
satpos.z=grvpos.z+dz;
r=sqrt(satpos.x*satpos.x+satpos.y*satpos.y+satpos.z*satpos.z);
dr=h+XKMPER-r;
if (dr<1.0)
break;
r1+=dr;
}
// Compute observer distance
dx=satpos.x-obspos.x;
dy=satpos.y-obspos.y;
dz=satpos.z-obspos.z;
r2=sqrt(dx*dx+dy*dy+dz*dz);
ra=modulo(atan2(dy,dx)*R2D,360.0);
de=asin(dz/r2)*R2D;
// Convert and project
if (strcmp(m.orientation,"horizontal")==0) {
equatorial2horizontal(m.mjd,ra,de,&azi,&alt);
forward(azi,alt,&rx,&ry);
} else if (strcmp(m.orientation,"equatorial")==0) {
forward(ra,de,&rx,&ry);
}
if (i==0)
cpgmove(rx,ry);
else
cpgdraw(rx,ry);
}
}
cpgsci(1);
return;
}
// Plot visibility contours
void plot_visibility(float h)
{
int i,j,k,nx=300,ny=200,nc;
float xmin,xmax,ymin,ymax;
double rx,ry,azi,alt,ra,de;
xyz_t obspos,obsvel,satpos,sunpos;
double dx,dy,dz,r,ax,ay,az,d,dr,sra,sde;
float rsun,rearth,psun,pearth,p,phase,mag;
char state[10];
float *cont,cmax;
float tr[6];
float c[]={0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0};
// Allocate
cont=(float *) malloc(sizeof(float)*nx*ny);
// Limits
xmin=-1.5*m.w;
xmax=1.5*m.w;
ymin=-m.w;
ymax=m.w;
// Transformation matrix
tr[2]=0.0;
tr[1]=(xmax-xmin)/(float) nx;
tr[0]=xmin-0.5*tr[1];
tr[4]=0.0;
tr[5]=(ymax-ymin)/(float) ny;
tr[3]=ymin-0.5*tr[5];
// Get observer and solar position
obspos_xyz(m.mjd,&obspos,&obsvel);
sunpos_xyz(m.mjd,&sunpos,&sra,&sde);
for (i=0;i<nx;i++) {
rx=xmin+(xmax-xmin)*(double) i/(double) (nx-1);
for (j=0;j<ny;j++) {
ry=ymin+(ymax-ymin)*(double) j/(double) (ny-1);
reverse(rx,ry,&azi,&alt);
// Skip low elevations
if (alt<=0.0) {
mag=15.0;
} else {
horizontal2equatorial(m.mjd,azi,alt,&ra,&de);
// Compute unit vector
ax=cos(ra*D2R)*cos(de*D2R);
ay=sin(ra*D2R)*cos(de*D2R);
az=sin(de*D2R);
// Find distance
for (k=0,d=h;k<20;k++) {
dx=d*ax;
dy=d*ay;
dz=d*az;
satpos.x=obspos.x+dx;
satpos.y=obspos.y+dy;
satpos.z=obspos.z+dz;
r=sqrt(satpos.x*satpos.x+satpos.y*satpos.y+satpos.z*satpos.z);
dr=h+XKMPER-r;
if (dr<1.0)
break;
d+=dr;
}
// Sun position from satellite
dx=-satpos.x+sunpos.x;
dy=-satpos.y+sunpos.y;
dz=-satpos.z+sunpos.z;
// Distances
rsun=sqrt(dx*dx+dy*dy+dz*dz);
rearth=sqrt(satpos.x*satpos.x+satpos.y*satpos.y+satpos.z*satpos.z);
// Angles
psun=asin(696.0e3/rsun)*R2D;
pearth=asin(6378.135/rearth)*R2D;
p=acos((-dx*satpos.x-dy*satpos.y-dz*satpos.z)/(rsun*rearth))*R2D;
p-=pearth;
// Visibility
if (p<-psun) {
strcpy(state,"eclipsed");
cpgsci(14);
} else if (p>-psun && p<psun) {
strcpy(state,"umbra");
cpgsci(15);
} else if (p>psun) {
strcpy(state,"sunlit");
cpgsci(7);
}
// Phase
phase=acos(((obspos.x-satpos.x)*(sunpos.x-satpos.x)+(obspos.y-satpos.y)*(sunpos.y-satpos.y)+(obspos.z-satpos.z)*(sunpos.z-satpos.z))/(rsun*d))*R2D;
// Magnitude
if (strcmp(state,"sunlit")==0)
mag=STDMAG-15.0+5*log10(d)-2.5*log10(sin(phase*D2R)+(M_PI-phase*D2R)*cos(phase*D2R));
else
mag=15;
}
k=i+nx*j;
cont[k]=mag;
}
}
// Find maximum contour value
for (i=0,j=0;i<nx*ny;i++) {
if (cont[i]<15.0) {
if (j==0 || cont[i]>cmax) cmax=cont[i];
j++;
}
}
nc=(int) cmax+1.0;
// Plot contours
cpgsci(7);
cpgcont(cont,nx,ny,1,nx,1,ny,c,nc,tr);
// Label contours
cpgsch(0.8);
for (i=0;i<nc;i++) {
sprintf(state,"%.0f",c[i]);
cpgconl(cont,nx,ny,1,nx,1,ny,c[i],tr,state,300,18);
}
cpgsch(1.0);
cpgsci(1);
return;
}
// Initialize plot
void init_plot(char *psfile,float width,float aspect)
{
int i;
// Initialize plot
cpgopen(psfile);
cpgslw(2);
cpgpap(width,aspect);
skymap_plot_renew();
return;
}
// Add to schedule
void schedule(char *nfd,double ra,double de,char *startstop)
{
FILE *file;
char sra[16],sde[16];
// Compute strings
dec2sex(ra/15.0,sra,0,5);
dec2sex(de,sde,0,4);
printf("%s %s %s %s\n",nfd,sra,sde,startstop);
// Open file
file=fopen("schedule.txt","a");
if (file==NULL) {
printf("Failed to create schedule.txt\n");
return;
}
fprintf(file,"%s %s %s %s %s\n",nfd,sra,sde, m.camera,startstop);
fclose(file);
return;
}
// Initialize plot
void skymap_plot_renew(void)
{
char filename[LIM];
// Size limit
if (m.w>120.0)
m.w=120.0;
if (m.level==1) {