-
Notifications
You must be signed in to change notification settings - Fork 52
/
navdump.cc
1435 lines (1229 loc) · 57.3 KB
/
navdump.cc
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 <string>
#include <stdio.h>
#include <iostream>
#include <arpa/inet.h>
#include "fmt/format.h"
#include "fmt/os.h"
#include "fmt/printf.h"
#include "CLI/CLI.hpp"
#include <fstream>
#include <map>
#include <bitset>
#include <vector>
#include <thread>
#include <signal.h>
#include <time.h>
#include "ubx.hh"
#include "bits.hh"
#include "minivec.hh"
#include "navmon.pb.h"
#include "ephemeris.hh"
#include "gps.hh"
#include "glonass.hh"
#include "beidou.hh"
#include "galileo.hh"
#include "navmon.hh"
#include "tle.hh"
#include "sp3.hh"
#include "ubx.hh"
#include <optional>
#include <unistd.h>
#include "sbas.hh"
#include "version.hh"
#include "gpscnav.hh"
#include "rinex.hh"
#include "rtcm.hh"
#include "fixhunter.hh"
static char program[]="navdump";
using namespace std;
extern const char* g_gitHash;
map<int, Point> g_srcpos;
vector<SP3Entry> g_sp3s;
bool sp3Order(const SP3Entry& a, const SP3Entry& b)
{
return tie(a.gnss, a.sv, a.t) < tie(b.gnss, b.sv, b.t);
}
void readSP3s(string_view fname)
{
SP3Reader sp3(fname);
SP3Entry e;
while(sp3.get(e)) {
g_sp3s.push_back(e);
}
sort(g_sp3s.begin(), g_sp3s.end(), sp3Order);
}
string beidouHealth(int in)
{
string ret;
if(in == 256) {
return "NO CLOCK";
}
if(in==511) {
return "NO SAT";
}
if(in & (1<<7))
ret += "B1I abnormal ";
if(in & (1<<6))
ret += "B2I abnormal ";
if(in & (1<<5))
ret += "B3I abnormal ";
if(in & (1<<1))
ret += "navigation abnormal ";
if(ret.empty())
return "OK";
return ret;
}
// GALILEO ONLY!!
template<typename T>
void doOrbitDump(int gnss, int sv, int wn, const T& oldEph, const T& newEph, int time_start, int time_end)
{
ofstream orbitcsv("orbit."+to_string(gnss)+"."+to_string(sv)+"."+to_string(oldEph.iodnav)+"-"+to_string(newEph.iodnav)+".csv");
orbitcsv << "timestamp x y z oldx oldy oldz\n";
orbitcsv << fixed;
for(int t = time_start; t < time_end; t += 30) {
Point p, oldp;
getCoordinates(t, newEph, &p);
getCoordinates(t, oldEph, &oldp);
time_t posix = utcFromGST(wn, t);
orbitcsv << posix <<" "
<<p.x<<" " <<p.y<<" "<<p.z<<" "
<<oldp.x<<" " <<oldp.y<<" "<<oldp.z<<"\n";
}
}
struct SVFilter
{
struct SatID
{
int gnss, sv, sigid;
bool operator<(const SatID& rhs) const
{
return std::tie(gnss, sv, sigid) < std::tie(rhs.gnss, rhs.sv, rhs.sigid);
}
};
void addFilter(string_view str)
{
SatID satid{0,0,-1};
satid.gnss = atoi(&str[0]);
auto pos= str.find(',');
if( pos != string::npos)
satid.sv = atoi(&str[pos+1]);
pos = str.find(',', pos+1);
if(pos != string::npos)
satid.sigid = atoi(&str[pos+1]);
// cout<<"Add to filter "<<satid.gnss<<", "<< satid.sv <<", "<<satid.sigid;
d_filter.insert(satid);
}
bool check(int gnss, int sv, int sigid=-1)
{
// cout<<"Check filter "<<gnss<<", "<< sv <<", "<<sigid<<endl;
if(d_filter.empty())
return true;
if(d_filter.count({gnss,0,-1})) { // gnss match
// cout<<"gnss match"<<endl;
return true;
}
if(d_filter.count({gnss,sv,-1})) { // gnss, sv match
// cout<<"gnss, sv match"<<endl;
return true;
}
if(d_filter.count({gnss,sv,sigid})) { // gnss, sv match, sigid
// cout<<"gnss, sv, sigid, match"<<endl;
return true;
}
// cout<<"Returning false"<<endl;
return false;
}
set<SatID> d_filter;
};
struct FixStat
{
double iTow{-1};
struct SatStat
{
double bestrange1{-1}; // corrected for clock
double bestrange5{-1}; // corrected for clock
double doppler1{-1}; // corrected for clock
double doppler5{-1}; // corrected for clock
double radvel{-1};
double ephrange{-1};
GalileoMessage ephemeris;
};
map<pair<int,int>, SatStat> sats;
};
double g_rcvoffset;
void emitFixState(int src, double iTow, FixStat& fs, int n)
{
cout<<"\nFix state for source "<<src<<", have "<<fs.sats.size()<<" satellites, n="<<n<<endl;
// for(double dt = -0.2; dt < 0.2; dt+=0.0001)
double dt=0;
double offset=0;
{
int count=0;
for(const auto& s : fs.sats) {
if(s.first.second==14 || s.first.second==18)
continue;
Point sat;
getCoordinates(iTow, s.second.ephemeris, &sat);
if(getElevationDeg(sat, g_srcpos[src]) < 20)
continue;
/*
Point sat;
auto [toffset, trend] = s.second.ephemeris.getAtomicOffset(iTow +dt);
(void)trend;
getCoordinates(iTow + toffset/1000000000.0 + dt, s.second.ephemeris, &sat);
double range = Vector(g_srcpos[nmm.sourceid()], sat).length();
getCoordinates(iTow + range/299792458.0 + toffset/1000000000.0 + dt, s.second.ephemeris, &sat);
range = Vector(g_srcpos[nmm.sourceid()], sat).length();
*/
double range = s.second.ephrange;
if(s.second.bestrange1 != -1) {
offset += s.second.bestrange1 - range;
count++;
}
if(s.second.bestrange5 != -1) {
offset += s.second.bestrange5 - range;
count++;
}
}
if(!count) {
fs.sats.clear();
return;
}
cout<< " dt "<<dt<<" err "<<offset << " " << count << " avg " << offset/count<< " "<<offset/count/3e5<<"ms"<<endl;
offset/=count;
}
for(const auto& s : fs.sats) {
Point sat;
double E=getCoordinates(iTow, s.second.ephemeris, &sat);
cout<<""<<s.first.first<<","<<s.first.second<<": "<<s.second.bestrange1-offset<<" "<<s.second.bestrange5-offset<<" " << s.second.ephrange<<", delta1 " << (s.second.bestrange1-offset-s.second.ephrange)<<", delta5 "<< (s.second.bestrange5-offset-s.second.ephrange)<<" dd "<< s.second.bestrange1 - s.second.bestrange5 <<" t0e " << s.second.ephemeris.getT0e()<< " elev " << getElevationDeg(sat, g_srcpos[src]) << " E " << E<< " clock " << s.second.ephemeris.getAtomicOffset(iTow).first/1000000<<"ms doppler1 "<<s.second.doppler1 << " doppler5 " <<s.second.doppler5<<" radvel " <<s.second.radvel<< " frac " << (s.second.bestrange1-offset-s.second.ephrange)/s.second.radvel;
cout<< " fixed "<<((s.second.bestrange1 - offset-s.second.ephrange) + (s.second.ephrange/299792458.0) * s.second.radvel)<< " BGD-ns "<<ldexp(s.second.ephemeris.BGDE1E5b,-32)*1000000000<< endl;
}
fs.sats.clear();
}
int main(int argc, char** argv)
try
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
CLI::App app(program);
TLERepo tles;
// tles.parseFile("active.txt");
tles.parseFile("galileo.txt");
tles.parseFile("glo-ops.txt");
tles.parseFile("gps-ops.txt");
tles.parseFile("beidou.txt");
// readSP3s("all.sp3");
if(!g_sp3s.empty()) {
// sort(g_sp3s.begin(), g_sp3s.end(), [](const auto& a, const auto&b) { return a.t < b.t; });
cout<<"Have "<<g_sp3s.size()<<" sp3 entries"<<endl; //, from "<<humanTime(g_sp3s.begin()->t) <<" to "<< humanTime(g_sp3s.rbegin()->t)<<endl;
}
vector<string> svpairs;
vector<int> stations;
bool doReceptionData{false};
bool doRFData{true};
bool doObserverPosition{false};
bool doObserverDetails{false};
bool doTimeOffsets{false};
bool doVERSION{false};
bool doJammingData{false};
string rinexfname;
string osnmafname;
app.add_option("--svs", svpairs, "Listen to specified svs. '0' = gps, '2' = Galileo, '2,1' is E01");
app.add_option("--stations", stations, "Listen to specified stations.");
app.add_option("--positions,-p", doObserverPosition, "Print out observer positions (or not)");
app.add_option("--rfdata,-r", doRFData, "Print out RF data (or not)");
app.add_option("--observerdetails,-o", doObserverDetails, "Print out observer detail data (or not)");
app.add_option("--timeoffsets,-t", doTimeOffsets, "Print out timeoffset data (or not)");
app.add_option("--recdata", doReceptionData, "Print out reception data (or not)");
app.add_option("--jamdata", doJammingData, "Print out jamming data (or not)");
app.add_option("--rinex", rinexfname, "If set, emit ephemerides to this filename");
app.add_option("--osnma", osnmafname, "If set, emit OSNMA CSV to this filename");
app.add_flag("--version", doVERSION, "show program version and copyright");
try {
app.parse(argc, argv);
} catch(const CLI::Error &e) {
return app.exit(e);
}
if(doVERSION) {
showVersion(program, g_gitHash);
exit(0);
}
SVFilter svfilter;
for(const auto& svp : svpairs) {
svfilter.addFilter(svp);
}
set<int> statset;
for(const auto& i : stations)
statset.insert(i);
ofstream almanac("almanac.txt");
ofstream iodstream("iodstream.csv");
iodstream << "timestamp gnssid sv iodnav t0e age" << endl;
ofstream ephcsv("eph.csv");
ephcsv<<"timestamp gnssid sv old_iod new_iod age insta_age x y z lat lon h"<<endl;
ofstream csv("delta.csv");
csv <<"timestamp gnssid sv tow tle_distance alma_distance utc_dist x y z vx vy vz rad inclination e iod"<<endl;
ofstream sp3csv;
sp3csv.open ("sp3.csv", std::ofstream::out | std::ofstream::app);
sp3csv<<"timestamp gnssid sv ephAge sp3X sp3Y sp3Z ephX ephY ephZ sp3Clock ephClock distance along clockDelta E speed"<<endl;
std::optional<RINEXNavWriter> rnw;
if(!rinexfname.empty())
rnw = RINEXNavWriter(rinexfname);
std::optional<ofstream> osnmacsv;
if(!osnmafname.empty()) {
osnmacsv = ofstream(osnmafname);
(*osnmacsv)<<"wn,tow,wtype,sv,osnma\n";
}
for(;;) {
char bert[4];
int res = readn2(0, bert, 4);
if( res != 4) {
cerr<<"EOF, res = "<<res<<endl;
break;
}
// I am so sorry
if(bert[0]!='b' || bert[1]!='e' || bert[2] !='r' || bert[3]!='t') {
cerr<<"Bad magic: "<<makeHexDump(string(bert, 4))<<endl;
int res;
for(int s=0;;++s) {
cerr<<"Skipping character hunting for good magic.. "<<s<<endl;
bert[0] = bert[1];
bert[1] = bert[2];
bert[2] = bert[3];
res = readn2(0, bert + 3, 1);
if(res != 1)
break;
if(bert[0]=='b' && bert[1]=='e' && bert[2] =='r' && bert[3]=='t')
break;
}
if(res != 1) {
cerr<<"EOF2, res = "<<res<<endl;
break;
}
}
uint16_t len;
if(readn2(0, &len, 2) != 2)
break;
len = htons(len);
char buffer[len];
if(readn2(0, buffer, len) != len)
break;
NavMonMessage nmm;
nmm.ParseFromString(string(buffer, len));
if(!statset.empty() && !statset.count(nmm.sourceid()))
continue;
// if(nmm.type() == NavMonMessage::ReceptionDataType)
// continue;
auto etstamp = [&nmm]() {
cout<<humanTime(nmm.localutcseconds(), nmm.localutcnanoseconds())<<" src "<< nmm.sourceid()<<" ";
uint32_t imptow = ((uint32_t)round(nmm.localutcseconds() + nmm.localutcnanoseconds()/1000000000.0) - 935280000 + 18) % (7*86400);
if(!(imptow % 2))
imptow--;
cout<< "imptow "<<imptow-2<<" ";
};
static map<int,GalileoMessage> galEphemeris;
if(nmm.type() == NavMonMessage::ReceptionDataType) {
if(doReceptionData) {
etstamp();
cout<<"receptiondata for "<<nmm.rd().gnssid()<<","<<nmm.rd().gnsssv()<<","<< (nmm.rd().has_sigid() ? nmm.rd().sigid() : 0) <<" db "<<nmm.rd().db()<<" ele "<<nmm.rd().el() <<" azi "<<nmm.rd().azi()<<" prRes "<<nmm.rd().prres() << " qi " << (nmm.rd().has_qi() ? nmm.rd().qi() : -1) << " used " << (nmm.rd().has_used() ? nmm.rd().used() : -1) << endl;
}
}
else if(nmm.type() == NavMonMessage::GalileoInavType) {
basic_string<uint8_t> inav((uint8_t*)nmm.gi().contents().c_str(), nmm.gi().contents().size());
static map<int, GalileoMessage> gms;
static map<pair<int, int>, GalileoMessage> gmwtypes;
static map<int, FixHunter> fixhunters;
static map<int, GalileoMessage> oldgm4s;
int sv = nmm.gi().gnsssv();
if(!svfilter.check(2, sv, nmm.gi().sigid()))
continue;
etstamp();
int64_t imptow = (((uint32_t)round(nmm.localutcseconds() + nmm.localutcnanoseconds()/1000000000.0) - 935280000 + 18) % (7*86400)) - 2;
if(nmm.gi().sigid() != 5) {
if(!(imptow % 2))
imptow--;
}
else if((imptow % 2))
imptow--;
if(imptow != nmm.gi().gnsstow())
cout<< " !!"<< (imptow - nmm.gi().gnsstow()) <<"!! ";
GalileoMessage& gm = gms[sv];
int wtype = gm.parse(inav);
if(wtype != 0 && wtype != 5 && wtype != 6)
gm.tow = nmm.gi().gnsstow();
bool isnew = gmwtypes[{nmm.gi().gnsssv(), wtype}].tow != gm.tow;
gmwtypes[{nmm.gi().gnsssv(), wtype}] = gm;
// fixhunters[nmm.gi().gnsssv()].reportInav(inav,nmm.gi().gnsstow() );
static map<int,GalileoMessage> oldEph;
cout << "gal inav wtype "<<wtype<<" for "<<nmm.gi().gnssid()<<","<<nmm.gi().gnsssv()<<","<<nmm.gi().sigid()<<" pbwn "<<nmm.gi().gnsswn()<<" pbtow "<< nmm.gi().gnsstow();
if(nmm.gi().has_ssp()) {
cout<<" ssp "<<(unsigned int) nmm.gi().ssp();
}
static uint32_t tow;
if(osnmacsv && isnew)
(*osnmacsv)<<nmm.gi().gnsswn()<<","<<gm.tow<<","<<wtype<<","<<nmm.gi().gnsssv()<<","<<makeHexDump(nmm.gi().reserved1())<<endl;
if(wtype >=1 && wtype <= 5) {
if(nmm.gi().has_reserved1()) {
cout<<" res1 "<<makeHexDump(nmm.gi().reserved1());
}
}
if(wtype == 4) {
// 2^-34 2^-46
cout <<" iodnav "<<gm.iodnav <<" af0 "<<gm.af0 <<" af1 "<<gm.af1 <<", af2 "<<(int)gm.af2<<" scaled: "<<1000000000.0*ldexp(1.0*gm.af0, 19-34)/(1<<19) <<" ns, "<<ldexp(1.0*gm.af1, 38-46);
cout << " t0g " << gm.t0g <<" a0g " << gm.a0g <<" a1g " << gm.a1g <<" WN0g " << gm.wn0g;
if(tow && oldgm4s.count(nmm.gi().gnsssv()) && oldgm4s[nmm.gi().gnsssv()].iodnav != gm.iodnav) {
auto& oldgm4 = oldgm4s[nmm.gi().gnsssv()];
auto oldOffset = oldgm4.getAtomicOffset(tow);
auto newOffset = gm.getAtomicOffset(tow);
cout<<" Timejump: "<<oldOffset.first - newOffset.first<<" ns after "<<(gm.getT0c() - oldgm4.getT0c() )<<" seconds";
}
oldgm4s[nmm.gi().gnsssv()] = gm;
int sv = nmm.gi().gnsssv();
if(gmwtypes[{sv,1}].iodnav == gmwtypes[{sv,2}].iodnav &&
gmwtypes[{sv,2}].iodnav == gmwtypes[{sv,3}].iodnav &&
gmwtypes[{sv,3}].iodnav == gmwtypes[{sv,4}].iodnav) {
cout <<" have complete ephemeris at " << gm.iodnav;
galEphemeris[sv] = gm;
SatID sid;
sid.gnss=2;
sid.sv = sv;
sid.sigid=1;
int start = utcFromGST(gm.wn, (int)gm.tow);
SP3Entry e{2, sv, start};
auto bestSP3 = lower_bound(g_sp3s.begin(), g_sp3s.end(), e, sp3Order);
if(bestSP3 != g_sp3s.end() && bestSP3->gnss == e.gnss && bestSP3->sv == sv) {
static set<pair<int,int>> haveSeen;
if(!haveSeen.count({sv, bestSP3->t})) {
haveSeen.insert({sv, bestSP3->t});
Point newPoint;
double E=getCoordinates(gm.tow + (bestSP3->t - start), gm, &newPoint);
Point sp3Point(bestSP3->x, bestSP3->y, bestSP3->z);
Vector dist(newPoint, sp3Point);
Vector nspeed;
getSpeed(gm.tow + (bestSP3->t - start), gm, &nspeed);
Vector speed = nspeed;
nspeed.norm();
double along = nspeed.inner(dist);
cout<<"\nsp3 "<<(bestSP3->t - start)<<" E"<<sv<<" "<<humanTime(bestSP3->t)<<" (" << newPoint.x/1000.0 <<", "<<newPoint.y/1000.0<<", "<<newPoint.z/1000.0<< ") (" <<
(bestSP3->x/1000.0) <<", " << (bestSP3->y/1000.0) <<", " << (bestSP3->z/1000.0) << ") "<<bestSP3->clockBias << " " << gm.getAtomicOffset(gm.tow + (bestSP3->t-start)).first<< " " << dist.length()<< " ";
cout << (bestSP3->clockBias - gm.getAtomicOffset(gm.tow + (bestSP3->t-start)).first);
cout << " " << gm.af0 <<" " << gm.af1;
cout << endl;
sp3csv <<std::fixed<< bestSP3->t << " 2 "<< sv <<" " << ephAge(gm.tow+(bestSP3->t - start), gm.getT0e()) <<" "<<bestSP3->x<<" " << bestSP3->y<<" " <<bestSP3->z <<" " << newPoint.x<<" " <<newPoint.y <<" " <<newPoint.z << " " <<bestSP3->clockBias <<" ";
sp3csv << gm.getAtomicOffset(gm.tow + (bestSP3->t-start)).first<<" " << dist.length() <<" " << along <<" ";
sp3csv << (bestSP3->clockBias - gm.getAtomicOffset(gm.tow + (bestSP3->t-start)).first) << " " << E << " " << speed.length()<<endl;
}
}
if(!oldEph[sv].sqrtA)
oldEph[sv] = gm;
else if(oldEph[sv].iodnav != gm.iodnav) {
if(rnw)
rnw->emitEphemeris(sid, gm);
// gm.toJSON();
cout<<" disco! "<< oldEph[sv].iodnav << " - > "<<gm.iodnav <<", "<< (gm.getT0e() - oldEph[sv].getT0e())/3600.0 <<" hours-jump insta-age "<<ephAge(gm.tow, gm.getT0e())/3600.0<<" hours";
Point oldPoint, newPoint;
getCoordinates(gm.tow, oldEph[sv], &oldPoint);
getCoordinates(gm.tow, gm, &newPoint);
Vector jump(oldPoint, newPoint);
cout<<" distance "<< jump.length() << " ("<<jump.x<<", "<<jump.y <<", "<<jump.z<<")";
auto oldAtomic = oldEph[sv].getAtomicOffset(gm.tow);
auto newAtomic = gm.getAtomicOffset(gm.tow);
cout<<" clock-jump "<<oldAtomic.first - newAtomic.first<<" ns ";
// doOrbitDump(2, sv, gm.wn, oldEph[sv], gm, gm.tow - 3*3600, gm.tow + 3*3600);
auto latlonh = ecefToWGS84(newPoint.x, newPoint.y, newPoint.z);
ephcsv<<nmm.localutcseconds()<<" "<< 2 <<" " << sv <<" " <<oldEph[sv].iodnav << " "<<gm.iodnav <<" "<< (gm.getT0e() - oldEph[sv].getT0e()) <<" "<<ephAge(gm.tow, gm.getT0e())/3600 << " " <<newPoint.x<<" " << newPoint.y <<" " <<newPoint.z<<" " << 180*get<0>(latlonh)/M_PI<<" " << 180*get<1>(latlonh)/M_PI <<" " <<get<2>(latlonh) << "\n";
oldEph[sv]=gm;
}
}
}
if(wtype == 1) {
cout << " iodnav " << gm.iodnav <<" t0e "<< gm.t0e*60 <<" " << ephAge(gm.t0e*60, gm.tow);
}
if(wtype == 2 || wtype == 3) {
cout << " iodnav " << gm.iodnav;
if(wtype == 3)
cout<<" sisa "<<(int)gm.sisa;
}
if(wtype == 1 || wtype == 2 || wtype == 3) {
iodstream << nmm.localutcseconds()<<" " << nmm.gi().gnssid() <<" "<< nmm.gi().gnsssv() << " " << gm.iodnav << " " << gm.t0e*60 <<" " << ephAge(gm.t0e*60, gm.tow);
if(wtype == 3)
iodstream<<endl;
else
iodstream<<"\n";
}
if(wtype == 0 || wtype == 5 || wtype == 6) {
if(wtype != 0 || gm.sparetime == 2) {
cout << " tow "<< gm.tow << " (%30=" << (gm.tow%30)<<") ";
tow = gm.tow;
}
}
if(wtype == 5) {
cout <<" e1bhs "<< (int) gm.e1bhs << " e5bhs "<< (int) gm.e5bhs << " e1bdvs "<< (int) gm.e1bdvs << " e5bdvs "<< (int) gm.e5bdvs<< " wn "<<gm.wn <<" ai0 "<< gm.ai0 <<" ai1 " << gm.ai1 <<" ai2 " <<gm.ai2 << " BGDE1E5a " << gm.BGDE1E5a <<" BGDE1E5b " <<gm.BGDE1E5b;
// wn = gm.wn;
}
if(wtype == 6) {
cout<<" a0 " << gm.a0 <<" a1 " << gm.a1 <<" t0t "<<gm.t0t << " dtLS "<<(int)gm.dtLS;
cout <<" wnLSF "<< (unsigned int)gm.wnLSF<<" dn " << (unsigned int)gm.dn<< " dtLSF "<<(int)gm.dtLSF;
}
// if(wtype < 7)
// gm = GalileoMessage{};
if(wtype >=7 && wtype<=10)
cout<<" ioda "<<gm.iodalmanac;
// af0 af1 scaling in almanac: 2^-19, 2^2^-38 plus "truncated"
if(wtype == 7) {
// this is possible because all the ephemeris stuff is in 7
cout<<" t0a "<<gm.t0almanac<<", alma sv1 "<<gm.alma1.svid<<", t0a age: "<< ephAge(gm.t0almanac *600, tow) << " ";
if(gm.alma1.svid) {
Point satpos;
getCoordinates(gm.tow, gm.alma1, &satpos);
cout<< "("<<satpos.x/1000<<", "<<satpos.y/1000<<", "<<satpos.z/1000<<")";
auto match = tles.getBestMatch(nmm.localutcseconds(), satpos.x, satpos.y, satpos.z);
cout<<" best-tle-match "<<match.name <<" distance "<<match.distance /1000<<" km ";
cout <<" tle-e "<<match.e <<" eph-e " <<gm.alma1.getE() <<" tle-ran "<<match.ran;
cout<<" norad " <<match.norad <<" int-desig " << match.internat;
cout<<" ele " << getElevationDeg(satpos, g_srcpos[nmm.sourceid()]) << " azi " << getAzimuthDeg(satpos, g_srcpos[nmm.sourceid()]);
}
}
else if(wtype == 8 && gm.tow - gmwtypes[{sv,7}].tow < 5 && gmwtypes[{sv,7}].alma1.svid && gm.iodalmanac == gmwtypes[{sv,7}].iodalmanac) {
// 8 finishes the rest
cout<<" alma1.sv "<<gmwtypes[{sv,7}].alma1.svid<<" af0 "<<gm.alma1.af0<<" af1 "<< gm.alma1.af1 <<" e5bhs "<< gm.alma1.e5bhs<<" e1bhs "<< gm.alma1.e1bhs <<" sv9 "<<gm.alma2.svid;
}
else if(wtype == 9 && gm.tow - gmwtypes[{sv,8}].tow < 30 && gm.iodalmanac == gmwtypes[{sv,8}].iodalmanac) {
if(gmwtypes[{sv,8}].alma2.svid)
cout<<" alma2.sv "<<gmwtypes[{sv,8}].alma2.svid<<" af0 "<<gm.alma2.af0<<" af1 "<< gm.alma2.af1 <<" e5bhs "<< gm.alma2.e5bhs<<" e1bhs "<< gm.alma2.e1bhs;
else
cout<<" empty almanac slot";
}
else if(wtype == 10 && gm.tow - gmwtypes[{sv,9}].tow < 5 && gmwtypes[{sv,9}].alma3.svid && gm.iodalmanac == gmwtypes[{sv,9}].iodalmanac) {
if(gm.alma3.e1bhs != 0) {
cout <<" gm.tow "<<gm.tow<<" gmwtypes.tow "<< gmwtypes[{sv,9}].tow <<" ";
}
cout<<" alma3.sv "<<gmwtypes[{sv,9}].alma3.svid <<" af0 "<<gm.alma3.af0<<" af1 "<< gm.alma3.af1 <<" e5bhs "<< gm.alma3.e5bhs<<" e1bhs "<< gm.alma3.e1bhs <<" a0g " << gm.a0g <<" a1g "<< gm.a1g <<" t0g "<<gm.t0g <<" wn0g "<<gm.wn0g <<" delta-gps "<< gm.getGPSOffset(gm.tow, gm.wn).first<<"ns";
int dw = (int)(gm.wn%64) - (int)(gm.wn0g);
if(dw > 31)
dw = 31- dw;
int delta = dw*7*86400 + gm.tow - gm.getT0g(); // NOT ephemeris age tricks
cout<<" wn%64 "<< (gm.wn%64) << " dw " << dw <<" delta " << delta;
}
else if(wtype==16) {
// was -35
cout<<" redced af0red "<< 1000000000.0*ldexp(gm.af0red, -26)<<" ns, "<<3600.0*(1000000000.0/(1<<20))*ldexp(gm.af1red, -15)<<" ns/hour ("<<gm.af1red<<")";
int32_t t0r = 1+nmm.gi().gnsstow() - ((nmm.gi().gnsstow()-2)%30) -2;
cout<<" t0r "<<t0r<<" ";
//(30*((nmm.gi().gnsstow()-2)/30)+1) % 604800; // page 56 of the OSS ICD 2.0
REDCEDAdaptor rca(gm, t0r);
#if 0
static REDCEDAdaptor* orig=0;
if(!orig)
orig = new REDCEDAdaptor(gm, t0r);
static auto ofs = fmt::output_file("error.csv");
static auto redcedcsv = fmt::output_file("redced.csv");
static bool csvInit;
if(!csvInit) {
ofs.print("{},{},{},{},{},{},{},{},{},{},{},{},{},{}\n",
"tow", "gnssid", "iod", "t0r", "red_x", "red_y", "red_z", "full_x", "full_y", "full_z", "redorig_x", "redorig_y", "redorig_z", "dist");
redcedcsv.print("gnssid,tow,t0r,deltaAred,exred,eyred,lambda0red,deltai0red,omega0red,af0red,af1red,nsec,fixedtow,x,y,z,radcor\n");
csvInit = true;
}
auto fixedtow = t0r + 300;
fixedtow -= (fixedtow % 200);
Point fixedredpoint;
getCoordinates(fixedtow, rca, &fixedredpoint);
redcedcsv.print("{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}\n",
nmm.gi().gnsssv(),
nmm.gi().gnsstow(),
t0r,
gm.deltaAred,
gm.exred,
gm.eyred,
gm.lambda0red,
gm.deltai0red,
gm.omega0red,
gm.af0red,
gm.af1red,
rca.getAtomicOffset(fixedtow).first,
fixedtow,
fixedredpoint.x, fixedredpoint.y, fixedredpoint.z,
sqrt(fixedredpoint.x*fixedredpoint.x + fixedredpoint.y*fixedredpoint.y + fixedredpoint.z*fixedredpoint.z) - rca.getAtomicOffset(fixedtow).first/3.0
);
redcedcsv.flush();
#endif
cout<<"eyred "<<gm.eyred<<" exred "<<gm.exred<<"\nlambda0red in rad "<< ldexp(M_PI*gm.lambda0red, -22)<<" atan2 " <<atan2(1.0*gm.eyred, 1.0*gm.exred)<<" deltaAred "<<gm.deltaAred;
Point pointRed;
getCoordinates(nmm.gi().gnsstow(), rca, &pointRed);
cout<<" reduced coordinates: "<<pointRed;
#if 0
for(int letow = nmm.gi().gnsstow(); letow < nmm.gi().gnsstow() + 120; ++letow) {
getCoordinates(letow, *orig, &pointOrigRed);
cout<<"Reduced coordinates original CED: "<<pointOrigRed<<endl;
getCoordinates(letow + gm.getAtomicOffset(letow).first/1000000000.0, gm, &point);
cout<<"Full coordinates: "<<point<<endl;
Vector dist(pointRed, point);
ofs.print("{},{},{},{},{},{},{},{},{},{},{},{},{},{}\n",
letow,
nmm.gi().gnsssv(),
gm.iodnav,
t0r,
pointRed.x,
pointRed.y,
pointRed.z,
point.x,
point.y,
point.z,
pointOrigRed.x,
pointOrigRed.y,
pointOrigRed.z,
dist.length());
auto ourpos=g_srcpos[nmm.sourceid()];
Vector range1(ourpos, pointRed), range2(ourpos, point);
cout<<"Distance: "<<dist<<", length "<<dist.length()<<", range difference: "<<range1.length() - range2.length()<< " " <<ourpos<<endl;
}
#endif
}
else if(wtype>=17 && wtype <=20) {
cout<<" reed-solomon 2iod "<< (int) gm.rs2bitiod;
}
cout<<endl;
}
else if(nmm.type() == NavMonMessage::GalileoCnavType) {
basic_string<uint8_t> cnav((uint8_t*)nmm.gc().contents().c_str(), nmm.gc().contents().size());
int sv = nmm.gc().gnsssv();
if(!svfilter.check(2, sv, nmm.gc().sigid()))
continue;
etstamp();
cout << "C/NAV for " << nmm.gc().gnssid()<<","<<nmm.gc().gnsssv()<<","<<nmm.gc().sigid() <<": header ";
cout<<fmt::sprintf("%02x%02x%02x (status %d, MT %d, MID %d, MS %d, PID %d) rest ",
getbitu(cnav.c_str(), 14, 8),
getbitu(cnav.c_str(), 22, 8),
getbitu(cnav.c_str(), 30, 8),
getbitu(cnav.c_str(), 14+0, 2), // status
getbitu(cnav.c_str(), 14+4, 2), // MT
getbitu(cnav.c_str(), 14+6, 5), // MID
getbitu(cnav.c_str(), 14+11, 5), // MIS
getbitu(cnav.c_str(), 14+16, 8) // PID
);
for(int n=0; n < 51; ++n)
cout << fmt::sprintf("%02x ", getbitu(cnav.c_str(), 38 +n *8, 8));
cout<<endl;
}
else if(nmm.type() == NavMonMessage::GalileoFnavType) {
basic_string<uint8_t> fnav((uint8_t*)nmm.gf().contents().c_str(), nmm.gf().contents().size());
int sv = nmm.gf().gnsssv();
if(!svfilter.check(2, sv, nmm.gf().sigid()))
continue;
etstamp();
GalileoMessage gm;
gm.parseFnav(fnav);
cout<<"gal F/NAV wtype "<< (int)gm.wtype << " for "<<nmm.gf().gnssid()<<","<<nmm.gf().gnsssv()<<","<<nmm.gf().sigid();
if(gm.wtype ==1 || gm.wtype == 2 || gm.wtype == 3 || gm.wtype == 4)
cout<<" iodnav " <<gm.iodnav <<" tow " << gm.tow;
if(gm.wtype == 1) {
cout <<" af0 "<<gm.af0 << " af1 "<<gm.af1 <<" af2 "<< (int)gm.af2;
}
if(gm.wtype == 2) {
cout <<" sqrtA "<<gm.sqrtA;
}
if(gm.wtype == 3) {
cout <<" t0e "<<gm.t0e;
}
if(gm.wtype == 4) {
cout <<" dtLS "<<(int)gm.dtLS <<" wnLSF "<< (unsigned int)gm.wnLSF<<" dn " << (unsigned int)gm.dn<< " dtLSF "<<(int)gm.dtLSF;
}
cout<<endl;
}
else if(nmm.type() == NavMonMessage::GPSInavType) {
int sv = nmm.gpsi().gnsssv();
if(!svfilter.check(0, sv))
continue;
etstamp();
auto cond = getCondensedGPSMessage(std::basic_string<uint8_t>((uint8_t*)nmm.gpsi().contents().c_str(), nmm.gpsi().contents().size()));
struct GPSState gs;
static map<int, GPSState> eph;
static map<int, GPSAlmanac> almas;
uint8_t page;
static int gpswn;
int frame=gs.parseGPSMessage(cond, &page);
cout<<"GPS "<<sv<<"@"<<nmm.gpsi().sigid()<<": "<<gs.tow<<" frame "<<frame<<" ";
static map<int, GPSState> oldgs1s;
static map<int, GPSState> oldgs2s;
if(frame == 1) {
gpswn = gs.wn;
cout << "gpshealth = "<<(int)gs.gpshealth<<", wn "<<gs.wn << " t0c "<<gs.t0c << " af0 " << gs.af0 << " af1 " << gs.af1 <<" af2 " << (int)gs.af2;
if(auto iter = oldgs1s.find(sv); iter != oldgs1s.end() && iter->second.t0c != gs.t0c) {
auto oldOffset = getGPSAtomicOffset(gs.tow, iter->second);
auto newOffset = getGPSAtomicOffset(gs.tow, gs);
cout<<" Timejump: "<<oldOffset.first - newOffset.first<<" after "<< ephAge(getT0c(gs), getT0c(iter->second) )<<" seconds, old t0c "<<iter->second.t0c;
}
oldgs1s[sv] = gs;
}
else if(frame == 2) {
eph[sv].parseGPSMessage(cond, &page);
// gs in frame 2 contains t0e, so legit
cout << "t0e = "<<gs.getT0e() << " " <<ephAge(gs.tow, gs.getT0e()) << " iod "<<gs.gpsiod;
oldgs2s[sv] = gs;
}
else if(frame == 3) {
eph[sv].parseGPSMessage(cond, &page);
cout <<"iod "<<gs.gpsiod;
if(eph[sv].gpsiod == oldgs2s[sv].gpsiod) {
Point sat;
getCoordinates(gs.tow, eph[sv], &sat);
TLERepo::Match second;
auto match = tles.getBestMatch(utcFromGPS(gpswn, gs.tow), sat.x, sat.y, sat.z, &second);
cout<<" best-tle-match "<<match.name <<" dist "<<match.distance /1000<<" km";
cout<<" norad " <<match.norad <<" int-desig " << match.internat;
cout<<" 2nd-match "<<second.name << " dist "<<second.distance/1000<<" km t0e "<<gs.gpsalma.getT0e() << " t " <<nmm.localutcseconds();
cout<<" ele " << getElevationDeg(sat, g_srcpos[nmm.sourceid()]) << " azi " << getAzimuthDeg(sat, g_srcpos[nmm.sourceid()]);
if(almas.count(sv)) {
Point almapoint;
getCoordinates(gs.tow, almas[sv], &almapoint);
cout<<" alma-dist " << Vector(sat, almapoint).length();
Vector speed;
getSpeed(gs.tow, eph[sv], &speed);
Point core;
csv << nmm.localutcseconds() << " 0 "<< sv <<" " << gs.tow << " " << match.distance <<" " << Vector(sat, almapoint).length() << " " << utcFromGPS(gpswn, gs.tow) - nmm.localutcseconds() << " " << sat.x <<" " << sat.y <<" " << sat.z <<" " <<speed.x <<" " <<speed.y<<" " <<speed.z<< " " << Vector(core, sat).length() << " " << eph[sv].getI0()<<" " << eph[sv].getE() << " " <<gs.gpsiod<<endl;
}
int start = utcFromGPS(gpswn, (int)gs.tow);
cout<<" sp3 start: "<<start<<" wn " << gpswn<<" tow " << gs.tow;
SP3Entry e{0, sv, start};
auto bestSP3 = lower_bound(g_sp3s.begin(), g_sp3s.end(), e, sp3Order);
if(bestSP3 != g_sp3s.end() && bestSP3->gnss == e.gnss && bestSP3->sv == sv) {
static set<pair<int,int>> haveSeen;
if(!haveSeen.count({sv, bestSP3->t})) {
haveSeen.insert({sv, bestSP3->t});
Point newPoint;
double E=getCoordinates(gs.tow + (bestSP3->t - start), eph[sv], &newPoint, false);
Point sp3Point(bestSP3->x, bestSP3->y, bestSP3->z);
Vector dist(newPoint, sp3Point);
Vector nspeed;
getSpeed(gs.tow + (bestSP3->t - start), eph[sv], &nspeed);
Vector speed = nspeed;
nspeed.norm();
double along = nspeed.inner(dist);
cout<<"\nsp3 "<<(bestSP3->t - start)<<" G"<<sv<<" "<<humanTime(bestSP3->t)<<" (" << newPoint.x/1000.0 <<", "<<newPoint.y/1000.0<<", "<<newPoint.z/1000.0<< ") (" <<
(bestSP3->x/1000.0) <<", " << (bestSP3->y/1000.0) <<", " << (bestSP3->z/1000.0) << ") "<<bestSP3->clockBias << " " << getGPSAtomicOffset(gs.tow + (bestSP3->t-start), oldgs1s[sv]).first<< " " << dist.length()<< " ";
cout << (bestSP3->clockBias - getGPSAtomicOffset(gs.tow + (bestSP3->t-start), oldgs1s[sv]).first);
cout << " " << gs.af0 <<" " << gs.af1;
cout << endl;
sp3csv <<std::fixed<< bestSP3->t << " 0 "<< sv <<" " << ephAge(gs.tow+(bestSP3->t - start), eph[sv].getT0e()) <<" "<<bestSP3->x<<" " << bestSP3->y<<" " <<bestSP3->z <<" " << newPoint.x<<" " <<newPoint.y <<" " <<newPoint.z << " " <<bestSP3->clockBias <<" ";
sp3csv << getGPSAtomicOffset(gs.tow + (bestSP3->t-start), oldgs1s[sv]).first<<" " << dist.length() <<" " << along <<" ";
sp3csv << (bestSP3->clockBias - getGPSAtomicOffset(gs.tow + (bestSP3->t-start), oldgs1s[sv]).first) << " " << E << " " << speed.length()<<endl;
}
}
}
}
else if(frame == 4) {
cout<<" page/svid " <<gs.gpsalma.sv;
if((gs.gpsalma.sv >= 25 && gs.gpsalma.sv <= 32) || gs.gpsalma.sv==57 ) { // see table 20-V of the GPS ICD
cout << " data-id "<<gs.gpsalma.dataid <<" alma-sv "<<gs.gpsalma.sv<<" t0a = "<<gs.gpsalma.getT0e() <<" health " <<gs.gpsalma.health;
Point sat;
getCoordinates(gs.tow, gs.gpsalma, &sat);
TLERepo::Match second;
auto match = tles.getBestMatch(nmm.localutcseconds(), sat.x, sat.y, sat.z, &second);
cout<<" best-tle-match "<<match.name <<" dist "<<match.distance /1000<<" km";
cout<<" norad " <<match.norad <<" int-desig " << match.internat;
cout<<" 2nd-match "<<second.name << " dist "<<second.distance/1000<<" km t0e "<<gs.gpsalma.getT0e() << " t " <<nmm.localutcseconds();
}
if(page == 18)
cout << " wnLSF "<< (int)gs.wnLSF <<" dn " << (int)gs.dn << " t0t " << (int)gs.t0t <<" wn0t "<<(int)gs.wn0t<<" dtLS " << (int)gs.dtLS <<" dtLSF "<< (int)gs.dtLSF;
}
else if(frame == 5) {
if(gs.gpsalma.sv <= 24) {
cout << " alma-sv "<<gs.gpsalma.sv<<" t0a = "<<gs.gpsalma.getT0e() <<" health " <<gs.gpsalma.health;
Point sat;
getCoordinates(gs.tow, gs.gpsalma, &sat);
TLERepo::Match second;
auto match = tles.getBestMatch(nmm.localutcseconds(), sat.x, sat.y, sat.z, &second);
cout<<" best-tle-match "<<match.name <<" dist "<<match.distance /1000<<" km";
cout<<" norad " <<match.norad <<" int-desig " << match.internat;
cout<<" 2nd-match "<<second.name << " dist "<<second.distance/1000<<" km t0e "<<gs.gpsalma.getT0e() << " t " <<nmm.localutcseconds();
almas[gs.gpsalma.sv] = gs.gpsalma;
}
}
cout<<"\n";
}
else if(nmm.type() == NavMonMessage::RTCMMessageType) {
etstamp();
RTCMMessage rm;
rm.parse(nmm.rm().contents());
cout<<" rtcm-msg "<<rm.type<<" ";
if(rm.type == 1057 || rm.type == 1240) {
cout<<"iod-ssr "<<rm.ssrIOD<<" ";
for(const auto& ed : rm.d_ephs) {
cout<<makeSatIDName(ed.id)<<": iode "<< ed.iod<<" ("<< ed.radial<<", "<<ed.along<<", "<<ed.cross<<") mm -> (";
cout<< ed.dradial<<", "<<ed.dalong<<", "<<ed.dcross<< ") mm/s\n";
}
}
else if(rm.type == 1058 || rm.type == 1241) {
cout<<"iod-ssr "<<rm.ssrIOD<<" ";
for(const auto& cd : rm.d_clocks) {
cout<<makeSatIDName(cd.id)<<": dclock0 "<< cd.dclock0 <<" dclock1 " << cd.dclock1 <<" dclock2 "<< cd.dclock2 << endl;
}
}
else if (rm.type == 1060 || rm.type == 1243) {
for(const auto& ed : rm.d_ephs) {
cout<<makeSatIDName(ed.id)<<": iode "<< ed.iod<<" ("<< ed.radial<<", "<<ed.along<<", "<<ed.cross<<") mm -> (";
cout<< ed.dradial<<", "<<ed.dalong<<", "<<ed.dcross<< ") mm/s\n";
}
for(const auto& cd : rm.d_clocks) {
cout<<makeSatIDName(cd.id)<<": dclock0 "<< cd.dclock0 <<" dclock1 " << cd.dclock1 <<" dclock2 "<< cd.dclock2 << endl;
}
}
else if(rm.type == 1045 || rm.type == 1046) {
static ofstream af0inavstr("af0inav.csv"), af0fnavstr("af0fnav.csv"), bgdstr("bgdstr.csv");
static bool first{true};
if(first) {
af0inavstr<<"timestamp sv wn t0c af0 af1\n";
af0fnavstr<<"timestamp sv wn t0c af0 af1\n";
first=false;
}
SatID sid;
sid.gnss = 2;
sid.sv = rm.d_sv;
sid.sigid = (rm.type == 1045) ? 5 : 1;
cout<< makeSatIDName(sid)<<" ";
if(rm.type == 1045) {
af0fnavstr << nmm.localutcseconds()<<" " << rm.d_sv <<" " << rm.d_gm.wn<<" "<< rm.d_gm.t0c << " " << rm.d_gm.af0 << " " << rm.d_gm.af1<<"\n";
cout<<"F/NAV";
}
else {
af0inavstr << nmm.localutcseconds() <<" " << rm.d_sv <<" " << rm.d_gm.wn<<" "<<rm.d_gm.t0c<<" "<< rm.d_gm.af0 << " " << rm.d_gm.af1<<"\n";
bgdstr << nmm.localutcseconds() <<" " << rm.d_sv<<" " <<rm.d_gm.BGDE1E5a <<" " << rm.d_gm.BGDE1E5b << "\n";
cout <<"I/NAV";
}
cout <<" iode " << rm.d_gm.iodnav << " sisa " << (unsigned int) rm.d_gm.sisa << " t0c " << rm.d_gm.t0c << " af0 "<<rm.d_gm.af0 <<" af1 " << rm.d_gm.af1 <<" af2 " << (int) rm.d_gm.af2 << " BGDE1E5a " << rm.d_gm.BGDE1E5a;
if(rm.type == 1046) // I/NAV
cout <<" BGDE1E5b "<< rm.d_gm.BGDE1E5b;
cout<<endl;
}
else if(rm.type == 1059 || rm.type==1242) {
cout<<"\n";
for(const auto& dcb : rm.d_dcbs) {
cout<<" "<<makeSatIDName(dcb.first)<<": "<<dcb.second<<" meters\n";
}
cout<<endl;
}
else {
cout<<" len " << nmm.rm().contents().size() << endl;
}
}
else if(nmm.type() == NavMonMessage::GPSCnavType) {
int sv = nmm.gpsc().gnsssv();
int sigid = nmm.gpsc().sigid();
if(!svfilter.check(0, sv, sigid))
continue;
etstamp();
static map<int, GPSCNavState> states;
auto& state = states[sv];
int type = parseGPSCNavMessage(
std::basic_string<uint8_t>((uint8_t*)nmm.gpsc().contents().c_str(),
nmm.gpsc().contents().size()),
state);
SatID sid{0, (uint32_t)sv, (uint32_t)sigid};
cout << "GPS CNAV " << makeSatIDName(sid) <<" tow "<<state.tow<<" type " << type;
if(type == 32) {
cout <<" delta-ut1 "<< state.getUT1OffsetMS(state.tow).first<<"ms";
}
cout<<endl;
}
else if(nmm.type() == NavMonMessage::BeidouInavTypeD1) {
int sv = nmm.bid1().gnsssv();
if(!svfilter.check(3, sv))
continue;
etstamp();
std::basic_string<uint8_t> cond;
try {
cond = getCondensedBeidouMessage(std::basic_string<uint8_t>((uint8_t*)nmm.bid1().contents().c_str(), nmm.bid1().contents().size()));
}
catch(std::exception& e) {
cout<<"Parsing error"<<endl;
continue;
}
uint8_t pageno;
static map<int, BeidouMessage> bms;
auto& bm = bms[sv];
int fraid = bm.parse(cond, &pageno);
cout<<"BeiDou "<<sv<<": "<<bm.sow<<", FraID "<<fraid;
if(fraid == 1) {
static map<int, BeidouMessage> msgs;
if(msgs[sv].wn>= 0 && msgs[sv].t0c != bm.t0c) {
auto oldOffset = msgs[sv].getAtomicOffset(bm.sow);
auto newOffset = bm.getAtomicOffset(bm.sow);
cout<<" Timejump: "<<oldOffset.first - newOffset.first<<" after "<<(bm.getT0c() - msgs[sv].getT0c() )<<" seconds";
}
msgs[sv]=bm;
cout<<" wn "<<bm.wn<<" t0c "<<(int)bm.t0c<<" aodc "<< (int)bm.aodc <<" aode "<< (int)bm.aode <<" sath1 "<< (int)bm.sath1 << " urai "<<(int)bm.urai << " af0 "<<bm.a0 <<" af1 " <<bm.a1 <<" af2 "<< (int)bm.a2;
auto offset = bm.getAtomicOffset();
cout<< ", "<<offset.first<<"ns " << (offset.second * 3600) <<" ns/hour "<< ephAge(bm.sow, bm.t0c*8);
}
else if(fraid == 3 && bm.sow) {
Point sat;
getCoordinates(bm.sow, bm, &sat);
TLERepo::Match second;
auto match = tles.getBestMatch(nmm.localutcseconds(), sat.x, sat.y, sat.z, &second);
cout<<" best-tle-match "<<match.name <<" dist "<<match.distance /1000<<" km";
cout<<" norad " <<match.norad <<" int-desig " << match.internat;
cout<<" 2nd-match "<<second.name << " dist "<<second.distance/1000<<" km";
}