-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_ephem.cxx
3207 lines (2244 loc) · 80.3 KB
/
read_ephem.cxx
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
/**
* @file read_ephem.cc
* @brief This file contains functions to read/calculate ephemerides and the attitude calculation.
* @author Giuseppe Romeo
* @date Created: Nov 15, 2005
*
* $Header: /nfs/slac/g/glast/ground/cvs/orbitSim/src/read_ephem.cxx,v 1.10 2009/06/23 17:51:24 vernaleo Exp $
*/
#include <cstdio>
#include "orbitSim/read_ephem.h"
#include "orbitSim/functions.h"
#include "orbitSim/atFunctions.h"
#include "orbitSim/GLAST_slew_estimate.h"
#include <vector>
#include <stdexcept>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include "st_stream/Stream.h"
#include "st_stream/StreamFormatter.h"
#include "st_stream/st_stream.h"
/// Stream to control output through verbosity level
st_stream::StreamFormatter osf("read_ephem", "", 2);
EphemData * allocateEphem(int num ) {
EphemData *eph = new EphemData;
//MJD(1), X(1), Y(1), Z(1), Lat(1), Long(1), Alt(1), VelRA(1), VelDEC(1)
eph->MJD.resize(num);
eph->X.resize(num);
eph->Y.resize(num);
eph->Z.resize(num);
eph->Lat.resize(num);
eph->Long.resize(num);
eph->Alt.resize(num);
eph->VelRA.resize(num);
eph->VelDEC.resize(num);
return eph;
}
EphemData * deallocateEphem(EphemData *eph) {
eph->MJD.clear();
eph->X.clear();
eph->Y.clear();
eph->Z.clear();
eph->Lat.clear();
eph->Long.clear();
eph->Alt.clear();
eph->VelRA.clear();
eph->VelDEC.clear();
delete eph;
return eph;
}
Attitude * deallocateAttitude (Attitude *att) {
att->mjd.clear();
att->SatRA.clear();
att->SatDEC.clear();
att->Xra.clear();
att->Xdec.clear();
att->Yra.clear();
att->Ydec.clear();
att->Zra.clear();
att->Zdec.clear();
att->X.clear();
att->Y.clear();
att->Z.clear();
att->Lat.clear();
att->Lon.clear();
att->Hei.clear();
att->in_saa.clear();
att->in_occ.clear();
att->rockAngle.clear();
delete att;
return att;
}
Attitude * allocateAttitude(int num) {
unsigned int size = num;
Attitude *att = new Attitude;
osf.setMethod("allocateAttitude");
osf.err().precision(15);
osf.info().precision(15);
osf.warn().precision(15);
osf.warn().precision(15);
osf.info(3) << "Allocating attitude vectors to " << size << " \n";
att->mjd.resize(size);
att->X.resize(size);
att->Y.resize(size);
att->Z.resize(size);
att->SatRA.resize(size);
att->SatDEC.resize(size);
att->Xra.resize(size);
att->Xdec.resize(size);
att->Yra.resize(size);
att->Ydec.resize(size);
att->Zra.resize(size);
att->Zdec.resize(size);
att->Lat.resize(size);
att->Lon.resize(size);
att->Hei.resize(size);
att->in_saa.resize(size);
att->in_occ.resize(size);
att->rockAngle.resize(size);
return att;
}
Attitude * reallocateAttitude(int num, Attitude *att) {
att->mjd.resize(num);
att->X.resize(num);
att->Y.resize(num);
att->Z.resize(num);
att->SatRA.resize(num);
att->SatDEC.resize(num);
att->Xra.resize(num);
att->Xdec.resize(num);
att->Yra.resize(num);
att->Ydec.resize(num);
att->Zra.resize(num);
att->Zdec.resize(num);
att->Lat.resize(num);
att->Lon.resize(num);
att->Hei.resize(num);
att->in_saa.resize(num);
att->in_occ.resize(num);
att->rockAngle.resize(num);
att->ent = num;
return att;
}
EphemData * yyyy_eph(FILE *ifp, double StartTime, double EndTime,
double Units, double Resolution) {
int YYYY, MM, DD, HH, Min, Sec;
int i, tyear, tmonth, tday, thour, tmin, tsec, it, iret;
double tx, ty, tz, tlon, tlat, tmjd, latt, height, Timespan;
// double do_cal2mjd(int,int,int,int,int,int);
EphemData * ephemeris = NULL;
char line[200];
AtPolarVect gSatP;
AtVect vSat, gSat;
int ipts, inum, okay;
int ckflg = 0;
double pmjd;
osf.setMethod("yyyy_eph");
// StartTime = StartTime - Resolution;
/*
"yyyy_eph" expects an ephemeris file input of this format:
%d %d %d %d %d %d %lf %lf %lf %lf %lf
YYYY MM DD HH Min SS Satellite_X_pos Satellite_Y_pos Satellite_Z_pos Lat Long
X, Y, Z are in decimegamters and get converted to km, and Lat and Long are
calculated (not used from the file) */
Timespan = EndTime - StartTime;
inum = (int)((Timespan+Resolution/2.0)/Resolution);
inum += 2; /* Delta plus 1 to get the end point */
ephemeris = allocateEphem(inum);
if (ephemeris == NULL) return(EPHNO);
do_mjd2cal(StartTime,&YYYY,&MM,&DD,&HH,&Min, &Sec);
i = 0;
it = 0;
/* types 1 and 2 have an initial line
with the filename and # pts */
iret=fscanf(ifp,"%s %d\n",line,&ipts);
if (ipts < inum) { /* either our resolution or our file size isn't enough */
osf.warn() << "Warning-- ephemeris does not have enough points to cover time of interest\nfound " << ipts << " points, we will still try to do the best to go on!\n";
}
iret=fscanf(ifp,"%d %d %d %d %d %d %lf %lf %lf %lf %lf",
&tyear, &tmonth, &tday, &thour, &tmin, &tsec, &tx,&ty,&tz, &tlon,&tlat);
tmjd = do_cal2mjd(tyear,tmonth,tday,thour,tmin,tsec);
if(tmjd > StartTime) {
std::ostringstream eBufT;
eBufT << "\n" << __FILE__ << ":" << __LINE__ << ", ERROR: Ephemeris file does not cover the entire time interval\nEphemeris file starts at " << tmjd << " while the interval of interest starts at " << StartTime << "\nPlease check. Exiting for now........\n\n" <<std::ends;
throw std::runtime_error(eBufT.str());
}
/* first, read through file until we get to start position */
okay = 1;
i = 0;
while (okay) {
iret=fscanf(ifp,"%d %d %d %d %d %d %lf %lf %lf %lf %lf",
&tyear, &tmonth, &tday, &thour, &tmin, &tsec, &tx,&ty,&tz, &tlon,&tlat);
if (iret == EOF ) {
osf.err() << "Error, ephemeris file does not reach start of schedule, exiting\n";
return(NULL);
}
tmjd = do_cal2mjd(tyear,tmonth,tday,thour,tmin,tsec);
if (tmjd >= StartTime) okay = 0;
++i;
}
/* next, read in data points, interpolating as needed */
if (inum+i > ipts) {
/* either our resolution or our file size isn't enough */
osf.warn() << "Warning-- ephemeris does not have enough points to cover time of interest\nfound " << ipts << " points, we will still try to do the best to go on\n";
}
while (it < inum) { /* keep reading until we get to end of timespan */
/* do appropriate conversion to make our values */
tmjd = do_cal2mjd(tyear,tmonth,tday,thour,tmin,tsec);
vSat[0] = tx * Units;
vSat[1] = ty * Units;
vSat[2] = tz * Units;
atGeodetic(tmjd,vSat,gSat);
atVectToPol(gSat,&gSatP);
atEllipsoido(&gSatP,&latt,&height);
gSatP.lat = latt;
/* save our values and move on */
ephemeris->MJD[it] = tmjd;
ephemeris->X[it] = vSat[0];
ephemeris->Y[it] = vSat[1];
ephemeris->Z[it] = vSat[2];
ephemeris->Lat[it] = gSatP.lat*RAD2DEG;
ephemeris->Long[it] = gSatP.lon*RAD2DEG;
ephemeris->Alt[it] = height;
/* now read through the file until we either run out of points, or
get the next element of time Resolution away from our current
reference point */
iret = fscanf(ifp,"%d %d %d %d %d %d %lf %lf %lf %lf %lf", &tyear, &tmonth,
&tday, &thour, &tmin, &tsec, &tx,&ty,&tz, &tlon,&tlat);
if (iret == EOF) {
osf.err() <<"Error, ephemeris file does not reach end of schedule, exiting\n";
return(NULL);
}
++i;
++it;
if(ckflg < 3){
ckflg++;
}
// Check the time resolution of the file
// only after the third row has been read in.
if(ckflg == 2) {
double dt = tmjd - pmjd;
if(fabs(dt-Resolution) > 1.0e-10){
std::ostringstream eBufT;
eBufT << "\n" << __FILE__ << ":" << __LINE__ << ", ERROR: The Ephemeris file does not have the requested time resolution.\nPlease, check it! Exiting now........\n\n"<<std::ends;
throw std::runtime_error(eBufT.str());
}
}
pmjd = tmjd;
}
/* ephemeris->Period = Period; */
ephemeris->SemiMajorAxis = 0;
ephemeris->Period = 0;
ephemeris->ent = inum;
fflush(stdout);
return(ephemeris);
}
/* CKS: The xyzll_eph routine was written to ingest the STK generated ephemeris for Swift.
It is similiar to the yyyy_eph routine. The differences include a comment line as
the first line of data from STK and the lattitude and longitude will be used
directly from the STK data. The altitude will be calculated, but is not currently
planned to be used.
The time format is also different, Swift will use a UTCJ format: doy/year HH:MM:SS.ss.
Swift ephem also includes the velocity RA and DEC of the satellite.
*/
EphemData * xyzll_eph(FILE *ifp, double StartTime, double EndTime,
double Units, double Resolution) {
int i, tyear, tdoy, thour, tmin, it, iret, isec;
double tsec, tx, ty, tz, tlon, tlat, mjd, latt, height, Timespan;
double tvra, tvdec;
int rez_check;
double mjd1, rez;
// double do_utcj2mjd(int,int,int,int,int);
EphemData * ephemeris = NULL;
char *r;
AtPolarVect gSatP;
AtVect vSat, gSat;
int inum, okay;
const int lnsz = 200;
char line[lnsz];
osf.setMethod("xyzll_eph");
/*
"xyzll_eph" expects an ephemeris file input of this format:
%d/%d %d:%d:%lf %lf %lf %lf %lf %lf %lf %lf
DOY/YYYY HH:Min:SS.s Satellite_X_pos Satellite_Y_pos Satellite_Z_pos Lat Long veocity_ra velocity_dec
X, Y, Z are in km, and Lat and Long (degrees) used, velocity Ra and Dec (degrees) and Altitude is
calculated */
rez_check = 1;
mjd1 = 0.0;
Timespan = EndTime - StartTime;
inum = (int)((Timespan+(Resolution/2.))/Resolution); // round up
inum+=2; // include the end point in the count
/* number of units of resolution we need */
ephemeris = allocateEphem(inum);
i = 0;
it = 0;
/* The first line in xyzll_eph formatted file is a comment line */
r=fgets(line, lnsz, ifp); /* read line and ignore */
if (r == NULL) {
osf.err() << "xyzll_eph: Error, ephemeris file is empty, exiting\n";
return EPHNO;
}
/* first, read through file until we get to start position */
okay = 1;
i = 0;
while (okay) {
iret = fscanf(ifp,"%d/%d %d:%d:%lf %lf %lf %lf %lf %lf %lf %lf", &tdoy, &tyear,
&thour, &tmin, &tsec, &tx,&ty,&tz, &tlat, &tlon, &tvra, &tvdec);
if (iret == EOF) {
osf.err() << "xyzll_eph: Error, ephemeris file does not reach start of schedule, exiting\n";
return EPHNO;
}
/* round seconds */
isec = (int) (tsec + 0.5);
mjd = do_utcj2mjd(tyear,tdoy,thour,tmin, isec);
if (mjd1 == 0) mjd1 = mjd;
/* Check if StartTime is prior to ephemeris data */
if ((i == 0) && (StartTime < (mjd - Resolution))) {
osf.err() << "xyzll_eph: Error, StartTime (" << StartTime << ") is prior to ephemeris data (" << (mjd-Resolution) << "). Exiting.\n";
return (EPHNO);
}
/* Check that the ephemeris resolution is the same as the TAKO resolution (within 3 seconds */
if ((rez_check) && (mjd != mjd1)) {
rez = mjd - mjd1;
if (fabs(Resolution - rez) < .00003472222222) {
/* it's good */
rez_check = 0;
} else {
osf.err() << "xyzll_eph: Error, the ephemeris resolution (" << rez << ") is NOT the same as the Tako Resolution (" << Resolution << "). Exiting.\n" << "The times compared were: MJD= " << mjd1 << " and MJD1= " << mjd << "\n";
return (EPHNO);
}
}
if (mjd >= StartTime) okay = 0;
++i;
}
/* next, read in data points */
while (it < inum) { /* keep reading until we get to end of timespan */
/* do appropriate conversion to make our values */
double tmjd = do_utcj2mjd(tyear,tdoy,thour,tmin, isec);
/* Check that the ephemeris resolution is the same as the TAKO resolution (within 3 seconds */
if ((rez_check) && (tmjd != mjd1)) {
rez = tmjd - mjd1;
if (fabs(Resolution - rez) < .00003472222222) {
/* it's good */
rez_check = 0;
} else {
osf.err() << "xyzll_eph: Error, the ephemeris resolution (" << rez << ") is NOT the same as the Tako Resolution (" << Resolution << "). Exiting.\n" << "The times compared were: MJD= " << mjd1 << " and MJD1= " << tmjd << "\n";
return (EPHNO);
}
}
vSat[0] = tx * Units;
vSat[1] = ty * Units;
vSat[2] = tz * Units;
atGeodetic(tmjd,vSat,gSat);
atVectToPol(gSat,&gSatP);
atEllipsoido(&gSatP,&latt,&height);
gSatP.lat = latt;
//if (tlon < 0) tlon = tlon + 360.0;
ephemeris->MJD[it] = tmjd;
ephemeris->X[it] = vSat[0];
ephemeris->Y[it] = vSat[1];
ephemeris->Z[it] = vSat[2];
ephemeris->Lat[it] = tlat;
ephemeris->Long[it] = tlon;
ephemeris->Alt[it] = height;
ephemeris->VelRA[it] = tvra;
ephemeris->VelDEC[it] = tvdec;
/* now read the next line of data */
iret = fscanf(ifp,"%d/%d %d:%d:%lf %lf %lf %lf %lf %lf %lf %lf", &tdoy, &tyear,
&thour, &tmin, &tsec, &tx,&ty,&tz,&tlat, &tlon, &tvra, &tvdec);
if (iret == EOF) {
osf.err() << "xyzll_eph: Error, ephemeris file does not reach end of schedule, exiting\n";
return EPHNO;
}
/* round seconds */
isec = (int) (tsec + 0.5);
++i;
++it;
}
/* ephemeris->Period = Period; */
ephemeris->SemiMajorAxis = 0;
ephemeris->Period = 0;
ephemeris->ent = inum;
return(ephemeris);
}
EphemData * tlederive(FILE *ifp, double StartTime,
double EndTime, double Units, double Resolution) {
EphemData * ephemeris = NULL;
double Timespan;
double latt, height;
AtVect vSat, gSat;
AtPolarVect gSatP;
vector pos, vel;
double temp;
char *SatN = "GLAST"; // TLE satellite name is 5 characters
char *SatN2 = "FGRST"; // This is the name used in NORAD file
int it, nit;
double mjd;
// const int Nlines = 4000;
const int Nlines = 3;
const int bufsiz = 100;
char ln[Nlines][bufsiz];// = {"\0"};
char tec[bufsiz];
int inum, istatus = 0;
atElemTle Tle;
AtTime tz;
double tsince, tdif;
int is = 0;
int il = 0;
int flgNam = 0;
int slen;
double resol; // resolution in minutes
// Minutes in one day;
osf.setMethod("tlederive");
Timespan = EndTime - StartTime;
inum = (int)((Timespan+Resolution/2.0)/Resolution); // round up
inum++; // include the end point in the count
osf.info(2) << "Ephemeris will contain inum=" << inum << " points\n";
slen = strlen(SatN);
while (fgets(tec,bufsiz,ifp)) {
if(strncmp(SatN, tec, slen) ==0){
strcpy(ln[il++],tec);
fgets(tec,bufsiz,ifp);
strcpy(ln[il++],tec);
fgets(tec,bufsiz,ifp);
strcpy(ln[il++],tec);
flgNam = 1;
break;
}
if(strncmp(SatN2, tec, slen) ==0){
strcpy(ln[il++],tec);
fgets(tec,bufsiz,ifp);
strcpy(ln[il++],tec);
fgets(tec,bufsiz,ifp);
strcpy(ln[il++],tec);
flgNam = 1;
// Make sure SatN contains the name we found.
SatN=SatN2;
break;
}
}
if(flgNam == 0){
osf.err() << "Error: Satellite " << SatN << " or " << SatN2 << " is NOT present in the TLE file\nPlease, check it and try again\n\n";
return(EPHNO);
}
ephemeris = allocateEphem(inum);
// resol is the time resolution in minutes
resol = Resolution*minInDay;
istatus = readTLE(Nlines, SatN, ln, &Tle, StartTime, EndTime, resol);
if (istatus == 1) {
tdif = (StartTime-Tle.mjd)*minInDay+Tle.isc;
int itdif = (int)((tdif-(double)((int)tdif))*60.0);
if(itdif > resol*60.0){
itdif = (int)(resol*60.0);
}
/*
tdif += resol;
if(tdif > resol)
tdif -= resol;
*/
tz.yr = Tle.tm.yr;
tz.mo = Tle.tm.mo;
tz.dy = Tle.tm.dy;
tz.hr = Tle.tm.hr;
tz.mn = Tle.tm.mn+(int)tdif;
// tz.sc = 0;
tz.sc = itdif;
temp = TWO_PI/_xmnpda/_xmnpda;
Tle.xndt2o = Tle.xndt2o*temp;
Tle.xndd6o = Tle.xndd6o*temp/_xmnpda;
Tle.xincl = Tle.xincl*DEG2RAD;
Tle.xnodeo = Tle.xnodeo*DEG2RAD;
Tle.omegao = Tle.omegao*DEG2RAD;
Tle.xmo = Tle.xmo*DEG2RAD;
Tle.xno = Tle.xno*temp*_xmnpda;
it = 0;
// correctTm(&tz);
// atMJulian(&tz,&mjd);
mjd = StartTime;
while (it < inum) {
tsince = tdif+(double)it * resol;
sgp4(tsince, &pos, &vel, &Tle);
/* do appropriate conversion to make our values */
// mjd = do_cal2mjd(tyear,tmonth,tday,thour,tmin);
/**/
if(pos.v[0] == 0.0 && pos.v[1] == 0.0 && pos.v[0] == 0.0)
osf.warn() << "at Sat#=" << is << "X=" << pos.v[0]* xkmper << ", Y=" << pos.v[1]* xkmper << ",Z=" << pos.v[2]* xkmper << ", the position vector is null \n";
//printf("%4d %02d %02d %02d %02d %05d %f %f %f %f\n",tz.yr, tz.mo, tz.dy, tz.hr, tz.mn, tz.sc, pos.v[0]* xkmper, pos.v[1]* xkmper, pos.v[2]* xkmper, Units); 1
/*
if(it < 0)
printf("%4d %02d %02d %02d %02d %05d %f %f %f %f\n",tz.yr, tz.mo, tz.dy, tz.hr, tz.mn, tz.sc, pos.v[0]* xkmper, pos.v[1]* xkmper, pos.v[2]* xkmper, Units);
*/
// break;
vSat[0] = pos.v[0] * Units * xkmper;
vSat[1] = pos.v[1] * Units * xkmper;
vSat[2] = pos.v[2] * Units * xkmper;
atGeodetic(mjd,vSat,gSat);
atVectToPol(gSat,&gSatP);
atEllipsoido(&gSatP,&latt,&height);
gSatP.lat = latt;
/* save our values and move on */
nit = it;
ephemeris->MJD[it] = mjd;
ephemeris->X[it] = vSat[0];
ephemeris->Y[it] = vSat[1];
ephemeris->Z[it] = vSat[2];
ephemeris->Lat[it] = gSatP.lat*RAD2DEG;
ephemeris->Long[it] = gSatP.lon*RAD2DEG;
ephemeris->Alt[it] = height;
++it;
mjd += resol/minInDay;
}
/* ephemeris->Period = Period; */
ephemeris->SemiMajorAxis = 0;
ephemeris->Period = 0;
ephemeris->ent = inum;
}else{
deallocateEphem(ephemeris);
return(EPHNO); /* AMake it clear that there is a problem */
}
return(ephemeris);
}
void MakeAtt(double start, double mjde, double mjds, double pra,
double pdec, double offset, double ra, double dec, int mode,
double res, EphemData *ephem, double *lpos, Attitude *OAtt, double TS ) {
// if the mode is 1, i.e. survey mode, then the final point for
// slewing must be calculated
double Timespan = mjde - start;
int inum = (int)((Timespan+res/2.0)/res);
inum++; // Delta plus 1 to get the end point
int oas = (int)(((mjde-TS)+res/2.0)/res);
int flgS = 0; // What is flgS for?
//Attitude *TOAtt = allocateAttitude(oas);
double tim = start;
osf.setMethod("makeAtt");
if(start < mjds) {
DoSlew(start, mjds, pra, pdec, ra, dec, res, ephem, OAtt, TS);
}
if(mode != 2){
Attitude *TOAtt = allocateAttitude(oas);
if (mode == 1 && start < mjds){
oas += 2;
TOAtt = reallocateAttitude(oas, TOAtt);
double RaDec[2];
TOAtt->ent = oas;
osf.info(3) <<"Calling MakeSurvey("<<start-res<<", "<<mjde+res<<", "<<res<<", "<<offset<<"\n";
MakeSurvey(tim, mjde+res, res, offset, ephem, TOAtt, RaDec, 1, TS);
int es = (int)(((mjds-TS)+res/2.0)/res);
ra = TOAtt->Zra[es];
dec = TOAtt->Zdec[es];
osf.info(3) <<"ra="<<ra<<", dec="<<dec<<"\n";
flgS = 1;
}
//if(start < mjds) {
// DoSlew(start, mjds, pra, pdec, ra, dec, res, ephem, OAtt, TS);
// }
// printf ("2) i=45 ==> mjd=%f, i=46 ==> mjd=%f\n", OAtt->mjd[45], OAtt->mjd[46]);
if(mode == 1) {
if(flgS == 0){
double RaDec[2];// = NULL;
MakeSurvey(mjds, mjde, res, offset, ephem, OAtt, RaDec, 1, TS);
int k = (int) (((mjde-TS)+res/2.0)/res);
lpos[0] = OAtt->Zra[k];
lpos[1] = OAtt->Zdec[k];
} else {
int k = (int) (((mjde-TS)+res/2.0)/res);
int j = (int) (((mjds-TS)+res/2.0)/res);
int ii;
for(ii=j; ii<k; ii++){
OAtt->mjd[ii] = TOAtt->mjd[ii];
OAtt->SatRA[ii] = TOAtt->SatRA[ii];
OAtt->SatDEC[ii] = TOAtt->SatDEC[ii];
OAtt->Xra[ii] = TOAtt->Xra[ii];
OAtt->Xdec[ii] = TOAtt->Xdec[ii];
OAtt->Yra[ii] = TOAtt->Yra[ii];
OAtt->Ydec[ii] = TOAtt->Ydec[ii];
OAtt->Zra[ii] = TOAtt->Zra[ii];
OAtt->Zdec[ii] = TOAtt->Zdec[ii];
OAtt->rockAngle[ii] = TOAtt->rockAngle[ii];
osf.info(6) <<"ii="<<ii<<", Zra="<<TOAtt->Zra[ii]<<", Zdec="<<TOAtt->Zdec[ii]<<"\n";
}
lpos[0] = TOAtt->Zra[k-1];
lpos[1] = TOAtt->Zdec[k-1];
}
}
TOAtt = deallocateAttitude(TOAtt);
}
if (mode = 2) {
MakePointed(mjds, mjde, res, ra, dec, ephem, OAtt, TS);
lpos[0] = ra;
lpos[1] = dec;
}
// TOAtt = deallocateAttitude(TOAtt);
osf.info(3) << "\nLeaving MakeAtt with lpos[0]="<<lpos[0]<<", lpos[1]="<<lpos[1]<<"\n\n\n";
// if(mjds > 54433.2) {
// exit(0);
// }
return;
}
/*
void MakeAtt(double start, double mjde, double mjds, double pra,
double pdec, double offset, double ra, double dec, int mode,
double res, EphemData *ephem, double *lpos, Attitude *OAtt, double TS ) {
// if the mode is 1, i.e. survey mode, then the final point for
// slewing must be calculated
double Timespan = mjde - start;
int inum = (int)((Timespan+res/2.0)/res);
inum++; // Delta plus 1 to get the end point
int oas = (int)(((mjde-TS)+res/2.0)/res);
int flgS = 0;
Attitude *TOAtt = allocateAttitude(oas);
if (mode == 1 && start < mjds){
oas += 2;
TOAtt = reallocateAttitude(oas, TOAtt);
double RaDec[2];
TOAtt->ent = oas;
MakeSurvey(TS, mjde+res, res, offset, ephem, TOAtt, RaDec, 1, TS);
int es = (int)(((mjds-TS)+res/2.0)/res);
ra = TOAtt->Zra[es];
dec = TOAtt->Zdec[es];
flgS = 1;
}
osf.setMethod("makeAtt");
if(start < mjds) {
DoSlew(start, mjds, pra, pdec, ra, dec, res, ephem, OAtt, TS);
}
// printf ("2) i=45 ==> mjd=%f, i=46 ==> mjd=%f\n", OAtt->mjd[45], OAtt->mjd[46]);
if(mode == 1) {
if(flgS == 0){
double RaDec[2];// = NULL;
MakeSurvey(mjds, mjde, res, offset, ephem, OAtt, RaDec, 1, TS);
int k = (int) (((mjde-TS)+res/2.0)/res);
lpos[0] = OAtt->Zra[k];
lpos[1] = OAtt->Zdec[k];
} else {
int k = (int) (((mjde-TS)+res/2.0)/res);
int j = (int) (((mjds-TS)+res/2.0)/res);
int ii;
for(ii=j; ii<k; ii++){
OAtt->mjd[ii] = TOAtt->mjd[ii];
OAtt->SatRA[ii] = TOAtt->SatRA[ii];
OAtt->SatDEC[ii] = TOAtt->SatDEC[ii];
OAtt->Xra[ii] = TOAtt->Xra[ii];
OAtt->Xdec[ii] = TOAtt->Xdec[ii];
OAtt->Yra[ii] = TOAtt->Yra[ii];
OAtt->Ydec[ii] = TOAtt->Ydec[ii];
OAtt->Zra[ii] = TOAtt->Zra[ii];
OAtt->Zdec[ii] = TOAtt->Zdec[ii];
osf.info(3) <<"ii="<<ii<<", Zra="<<TOAtt->Zra[ii]<<", Zdec="<<TOAtt->Zdec[ii]<<"\n";
}
lpos[0] = TOAtt->Zra[k-1];
lpos[1] = TOAtt->Zdec[k-1];
}
} else {
MakePointed(mjds, mjde, res, ra, dec, ephem, OAtt, TS);
lpos[0] = ra;
lpos[1] = dec;
}
TOAtt = deallocateAttitude(TOAtt);
osf.info(3) << "\nLeaving MakeAtt with lpos[0]="<<lpos[0]<<", lpos[1]="<<lpos[1]<<"\n\n\n";
return;
}
*/
void MakeAtt2(double start, double mjde, double pra, double pdec,
double offset, double ra, double dec, int mode, double res,
EphemData *ephem, double *lpos, Attitude *OAtt, double TS ){
/*
if the mode is 1, i.e. survey mode, then the final point for
slewing and slew time must be calculated
*/
double mjds = start;
double Timespan = mjde - start;
int inum = (int)((Timespan+res/2.0)/res);
inum++; /* Delta plus 1 to get the end point */
double slt = 0.0;
double pslt = 0.0;
if (mode == 1 ){
do {
pslt = slt;
double RaDec[2];
MakeSurvey(mjds, (mjds+res), res, offset, ephem, OAtt, RaDec, 0, TS);
ra = RaDec[0];
dec = RaDec[1];
// printf("pra=%f, ra=%f, pdec=%f, dec=%f\n", pra, ra, pdec, dec);
if(pra == ra && pdec == dec){
mjds = start;
slt = 0.0;
pslt = slt;
} else {
getslewtime(pra, pdec, ra, dec, res, &slt);
slt = slt/res;
slt = (double)((int)(slt+0.5))*res;
}
mjds = start+slt;
} while ((slt - pslt) > res/2.0);
} else if (mode == 2) {
if(pra == ra && pdec == dec){
mjds = start;
slt = 0.0;
pslt = slt;
} else {
getslewtime(pra, pdec, ra, dec, res, &slt);
slt = slt/res;
slt = (double)((int)(slt+0.5))*res;
}
mjds = start+slt;
}
if(start < mjds) {
DoSlew(start, mjds, pra, pdec, ra, dec, res, ephem, OAtt, TS);
}
if(mode == 1) {
double RaDec[2];// = NULL;
MakeSurvey(mjds, mjde, res, offset, ephem, OAtt, RaDec, 2, TS);
int k = (int) (((mjde-TS)+res/2.0)/res);
lpos[0] = OAtt->Zra[k];
lpos[1] = OAtt->Zdec[k];
} else {
MakePointed(mjds, mjde, res, ra, dec, ephem, OAtt, TS);
lpos[0] = ra;
lpos[1] = dec;
}