source: ntrip/trunk/BNC/src/combination/bnccomb.cpp@ 11083

Last change on this file since 11083 was 11083, checked in by stuerze, 8 days ago

further fixes and improvements

File size: 75.8 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncComb
6 *
7 * Purpose: Combinations of Orbit/Clock Corrections
8 *
9 * Author: L. Mervart
10 *
11 * Created: 22-Jan-2011
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <newmatio.h>
18#include <iomanip>
19#include <sstream>
20#include <map>
21#include <algorithm>
22
23#include "bnccomb.h"
24#include <combination/bncbiassnx.h>
25#include "bnccore.h"
26#include "upload/bncrtnetdecoder.h"
27#include "bncsettings.h"
28#include "bncutils.h"
29#include "bncantex.h"
30#include "t_prn.h"
31
32const double sig0_offAC = 1000.0;
33const double sigP_offAC = 1000.0;
34
35const double sig0_offACSat = 1000.0;
36const double sigP_offACSat = 0.01;
37
38const double sig0_clkSat = 100.0;
39const double sigP_clkSat = 100.0; //*) White noise may be set to something between a few centimeters and tens of meters.
40 // A large value results in a combination that is equivalent to a single-epoch combination algorithm.
41
42const double sigObs = 0.05;
43
44using namespace std;
45
46// Constructor
47////////////////////////////////////////////////////////////////////////////
48bncComb::cmbParam::cmbParam(parType type_, int index_, const QString& ac_, const QString& prn_) {
49
50 type = type_;
51 index = index_;
52 AC = ac_;
53 prn = prn_;
54 xx = 0.0;
55 eph = 0;
56
57 if (type == offACgnss) {
58 epoSpec = true;
59 sig0 = sig0_offAC; // 1000.0
60 sigP = sig0; // 1000.0
61 }
62 else if (type == offACSat) {
63 epoSpec = false;
64 sig0 = sig0_offACSat; // 100.0
65 sigP = sigP_offACSat; // 0.01
66 }
67 // combined clock
68 else if (type == clkSat) {
69 epoSpec = true;
70 sig0 = sig0_clkSat; // 100.0
71 sigP = sigP_clkSat; // 10.0
72
73 }
74
75}
76
77// Destructor
78////////////////////////////////////////////////////////////////////////////
79bncComb::cmbParam::~cmbParam() {
80}
81
82// Partial
83////////////////////////////////////////////////////////////////////////////
84double bncComb::cmbParam::partial(char sys, const QString& AC_, const QString& prn_) {
85
86 if (type == offACgnss) {
87 if (AC == AC_ && prn_[0].toLatin1() == sys) {
88 return 1.0;
89 }
90 }
91 else if (type == offACSat) {
92 if (AC == AC_ && prn == prn_) {
93 return 1.0;
94 }
95 }
96 else if (type == clkSat) {
97 if (prn == prn_) {
98 return 1.0;
99 }
100 }
101
102 return 0.0;
103}
104
105//
106////////////////////////////////////////////////////////////////////////////
107QString bncComb::cmbParam::toString(char sys) const {
108
109 QString outStr;
110
111 if (type == offACgnss) {
112 outStr = "AC Offset " + AC + " " + sys + " ";
113 }
114 else if (type == offACSat) {
115 outStr = "Sat Offset " + AC + " " + prn.mid(0,3);
116 }
117 else if (type == clkSat) {
118 outStr = "Clk Corr " + prn.mid(0,3);
119 }
120
121 return outStr;
122}
123
124// Singleton: definition class variable
125////////////////////////////////////////////////////////////////////////////
126bncComb* bncComb::instance = nullptr;
127
128
129// Constructor
130////////////////////////////////////////////////////////////////////////////
131bncComb::bncComb() : _ephUser(true) {
132
133 bncSettings settings;
134
135 _running = true;
136
137 QStringList cmbStreams = settings.value("cmbStreams").toStringList();
138
139 _cmbSampl = settings.value("cmbSampl").toString().split("sec").first().toInt();
140 if (_cmbSampl <= 0) {
141 _cmbSampl = 5;
142 }
143 _useGps = (Qt::CheckState(settings.value("cmbGps").toInt()) == Qt::Checked) ? true : false;
144 if (_useGps) {
145 _cmbSysPrn['G'] = t_prn::MAXPRN_GPS;
146 _masterMissingEpochs['G'] = 0;
147 }
148 _useGlo = (Qt::CheckState(settings.value("cmbGlo").toInt()) == Qt::Checked) ? true : false;
149 if (_useGlo) {
150 _cmbSysPrn['R'] = t_prn::MAXPRN_GLONASS;
151 _masterMissingEpochs['R'] = 0;
152 }
153 _useGal = (Qt::CheckState(settings.value("cmbGal").toInt()) == Qt::Checked) ? true : false;
154 if (_useGal) {
155 _cmbSysPrn['E'] = t_prn::MAXPRN_GALILEO;
156 _masterMissingEpochs['E'] = 0;
157 }
158 _useBds = (Qt::CheckState(settings.value("cmbBds").toInt()) == Qt::Checked) ? true : false;
159 if (_useBds) {
160 _cmbSysPrn['C'] = t_prn::MAXPRN_BDS;
161 _masterMissingEpochs['C'] = 0;
162 }
163 _useQzss = (Qt::CheckState(settings.value("cmbQzss").toInt()) == Qt::Checked) ? true : false;
164 if (_useQzss) {
165 _cmbSysPrn['J'] = t_prn::MAXPRN_QZSS;
166 _masterMissingEpochs['J'] = 0;
167 }
168 _useSbas = (Qt::CheckState(settings.value("cmbSbas").toInt()) == Qt::Checked) ? true : false;
169 if (_useSbas) {
170 _cmbSysPrn['S'] = t_prn::MAXPRN_SBAS;
171 _masterMissingEpochs['S'] = 0;
172 }
173 _useNavic = (Qt::CheckState(settings.value("cmbNavic").toInt()) == Qt::Checked) ? true : false;
174 if (_useNavic) {
175 _cmbSysPrn['I'] = t_prn::MAXPRN_NavIC;
176 _masterMissingEpochs['I'] = 0;
177 }
178
179 if (cmbStreams.size() >= 1 && !cmbStreams[0].isEmpty()) {
180 QListIterator<QString> it(cmbStreams);
181 while (it.hasNext()) {
182 QStringList hlp = it.next().split(" ");
183 cmbAC* newAC = new cmbAC();
184 newAC->mountPoint = hlp[0];
185 newAC->name = hlp[1];
186 newAC->weightFactor = hlp[2].toDouble();
187 newAC->excludeSats = hlp[3].split(QRegExp("[ ,]"), Qt::SkipEmptyParts);
188 newAC->isAPC = bool(newAC->mountPoint.mid(0,4) == "SSRA");
189 newAC->attitudeSource = (hlp.size() > 4) ? hlp[4] : "Computed";
190 QMapIterator<char, unsigned> itSys(_cmbSysPrn);
191 // init
192 while (itSys.hasNext()) {
193 itSys.next();
194 char sys = itSys.key();
195 if (!_masterOrbitAC.contains(sys) &&
196 !newAC->excludeSats.contains(QString(sys), Qt::CaseSensitive)) {
197 _masterOrbitAC[sys] = newAC->name;
198 _masterIsAPC[sys] = newAC->isAPC;
199 }
200 }
201 _ACs.append(newAC);
202 }
203 }
204
205 QString ssrFormat;
206 _ssrCorr = 0;
207 QListIterator<QString> it(settings.value("uploadMountpointsOut").toStringList());
208 while (it.hasNext()) {
209 QStringList hlp = it.next().split(",");
210 if (hlp.size() > 7) {
211 ssrFormat = hlp[7];
212 }
213 }
214 if (ssrFormat == "IGS-SSR") {
215 _ssrCorr = new SsrCorrIgs();
216 }
217 else if (ssrFormat == "RTCM-SSR") {
218 _ssrCorr = new SsrCorrRtcm();
219 }
220 else if (ssrFormat == "RTCM-NEW-SSR") {
221 _ssrCorr = new SsrCorrRtcmNew();
222 }
223 else { // default
224 _ssrCorr = new SsrCorrIgs();
225 }
226
227 _rtnetDecoder = new bncRtnetDecoder();
228
229 connect(this, SIGNAL(newMessage(QByteArray,bool)), BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
230 connect(BNC_CORE, SIGNAL(providerIDChanged(QString)), this, SLOT(slotProviderIDChanged(QString)));
231 connect(BNC_CORE, SIGNAL(newOrbCorrections(QList<t_orbCorr>)), this, SLOT(slotNewOrbCorrections(QList<t_orbCorr>)));
232 connect(BNC_CORE, SIGNAL(newClkCorrections(QList<t_clkCorr>)), this, SLOT(slotNewClkCorrections(QList<t_clkCorr>)));
233 connect(BNC_CORE, SIGNAL(newCodeBiases(QList<t_satCodeBias>)), this, SLOT(slotNewCodeBiases(QList<t_satCodeBias>)));
234 connect(BNC_CORE, SIGNAL(newPhaseBiases(QList<t_satPhaseBias>)), this, SLOT(slotNewPhaseBiases(QList<t_satPhaseBias>)));
235
236 // Combination Method
237 // ------------------
238 if (settings.value("cmbMethod").toString() == "Single-Epoch") {
239 _method = singleEpoch;
240 }
241 else {
242 _method = filter;
243 }
244
245 // Initialize Parameters (model: Clk_Corr = AC_Offset + Sat_Offset + Clk)
246 // ----------------------------------------------------------------------
247 if (_method == filter) {
248 // SYSTEM
249 QMapIterator<char, unsigned> itSys(_cmbSysPrn);
250 while (itSys.hasNext()) {
251 itSys.next();
252 int nextPar = 0;
253 char sys = itSys.key();
254 int maxPrn = itSys.value();
255 // AC
256 QListIterator<cmbAC*> itAc(_ACs);
257 while (itAc.hasNext()) {
258 cmbAC* AC = itAc.next();
259 _params[sys].push_back(new cmbParam(cmbParam::offACgnss, ++nextPar, AC->name, ""));
260 for (int iGnss = 1; iGnss <= maxPrn; iGnss++) {
261 int flag = t_corrSSR::getSsrNavTypeFlag(sys, iGnss);
262 QString prn = QString("%1%2_%3").arg(sys).arg(iGnss, 2, 10, QChar('0')).arg(flag);
263 _params[sys].push_back(new cmbParam(cmbParam::offACSat, ++nextPar, AC->name, prn));
264 }
265 }
266 for (int iGnss = 1; iGnss <= maxPrn; iGnss++) {
267 int flag = t_corrSSR::getSsrNavTypeFlag(sys, iGnss);
268 QString prn = QString("%1%2_%3").arg(sys).arg(iGnss, 2, 10, QChar('0')).arg(flag);
269 _params[sys].push_back(new cmbParam(cmbParam::clkSat, ++nextPar, "", prn));
270 }
271 // Initialize Variance-Covariance Matrix
272 // -------------------------------------
273 _QQ[sys].ReSize(_params[sys].size());
274 _QQ[sys] = 0.0;
275 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
276 cmbParam* pp = _params[sys][iPar-1];
277 _QQ[sys](iPar,iPar) = pp->sig0 * pp->sig0;
278 }
279 }
280 }
281
282 // ANTEX File
283 // ----------
284 _antex = 0;
285 QString antexFileName = settings.value("uploadAntexFile").toString();
286 if (!antexFileName.isEmpty()) {
287 _antex = new bncAntex();
288 if (_antex->readFile(antexFileName) != success) {
289 emit newMessage("bncComb: wrong ANTEX file", true);
290 delete _antex;
291 _antex = 0;
292 }
293 else {
294 emit newMessage("bncComb: " + _antex->info().toLatin1(), true);
295 }
296 }
297
298 // Reference point of the combination: with ACs of both kinds (APC streams
299 // 'SSRA...', CoM streams 'SSRC...') and an ANTEX file, the combination is
300 // done in the CoM frame and the orbits of the APC ACs are converted to
301 // CoM; otherwise in the frame of the master AC (ACs of the other kind are
302 // not used)
303 // ------------------------------------------------------------------------
304 bool hasAPC = false, hasCoM = false;
305 for (const cmbAC* AC : _ACs) {
306 if (AC->isAPC) hasAPC = true; else hasCoM = true;
307 }
308 _cmbCoM = (_antex && hasAPC && hasCoM);
309 if (_cmbCoM) {
310 for (QMap<char, bool>::iterator it = _masterIsAPC.begin(); it != _masterIsAPC.end(); ++it) {
311 it.value() = false;
312 }
313 emit newMessage("bncComb: APC and CoM streams configured - combination in the CoM frame, "
314 "orbits of the APC streams converted to CoM", true);
315 }
316 else if (hasAPC && hasCoM) {
317 emit newMessage("bncComb: APC and CoM streams configured, but no ANTEX file - only the streams "
318 "with the reference point of the master AC are combined", true);
319 }
320 if (!_antex && hasCoM) {
321 emit newMessage("bncComb: CoM streams configured, but no ANTEX file - the combined orbits (INTERNAL) "
322 "may refer to the CoM, while the PPP assumes the APC", true);
323 }
324
325
326 // Bias SINEX File
327 // ---------------
328 _bsx = 0;
329 QString bsxFileName = settings.value("cmbBsxFile").toString();
330 if (!bsxFileName.isEmpty()) {
331 _bsx = new bncBiasSnx();
332 if (_bsx->readFile(bsxFileName) != success) {
333 emit newMessage("bncComb: wrong Bias SINEX file", true);
334 delete _bsx;
335 _bsx = 0;
336 }
337 }
338 if (_bsx) {
339 _ms = 3.0 * 3600 * 1000.0;
340 QTimer::singleShot(_ms, this, SLOT(slotReadBiasSnxFile()));
341 }
342
343 // Maximal Residuum
344 // ----------------
345 _MAX_RES = settings.value("cmbMaxres").toDouble();
346 if (_MAX_RES <= 0.0) {
347 _MAX_RES = 999.0;
348 }
349
350 // Maximal Displacement
351 // --------------------
352 _MAX_DISPLACEMENT = settings.value("cmbMaxdisplacement").toDouble();
353 if (_MAX_DISPLACEMENT <= 0.0) {
354 _MAX_DISPLACEMENT = 2.0;
355 }
356
357 // Logfile
358 // -------
359 QString intr = "1 day";
360 QString logFileCmb = settings.value("cmbLogpath").toString();
361 int l = logFileCmb.length();
362 if (logFileCmb.isEmpty()) {
363 _logFile = 0;
364 }
365 else {
366 if (l && logFileCmb[l-1] != QDir::separator() ) {
367 logFileCmb += QDir::separator();
368 }
369
370 logFileCmb = logFileCmb + "COMB00BNC" + "${V3PROD}" + ".CMB";
371
372 _logFile = new bncoutf(logFileCmb, intr, _cmbSampl);
373 }
374
375 _newCorr = 0;
376}
377
378// Destructor
379////////////////////////////////////////////////////////////////////////////
380bncComb::~bncComb() {
381
382 _running = false;
383#ifndef WIN32
384 sleep(2);
385#else
386 Sleep(2000);
387#endif
388
389 QListIterator<cmbAC*> icAC(_ACs);
390 while (icAC.hasNext()) {
391 delete icAC.next();
392 }
393 _ACs.clear();
394
395 delete _rtnetDecoder;
396
397 if (_ssrCorr) {
398 delete _ssrCorr;
399 }
400
401 if (_newCorr) {
402 delete _newCorr;
403 }
404
405 delete _antex;
406 delete _bsx;
407 delete _logFile;
408
409 QMapIterator<char, unsigned> itSys(_cmbSysPrn);
410 while (itSys.hasNext()) {
411 itSys.next();
412 char sys = itSys.key();
413 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
414 delete _params[sys][iPar-1];
415 }
416 _buffer.remove(sys);
417 }
418 _params.clear();
419 _buffer.clear();
420 _cmbSysPrn.clear();
421
422 while (!_epoClkData.empty()) {
423 delete _epoClkData.front();
424 _epoClkData.pop_front();
425 }
426}
427
428void bncComb::slotReadBiasSnxFile() {
429 bncSettings settings;
430 QString bsxFileName = settings.value("cmbBsxFile").toString();
431
432 if (bsxFileName.isEmpty()) {
433 emit newMessage("bncComb: no Bias SINEX file specified", true);
434 return;
435 }
436 if (!_bsx) {
437 _bsx = new bncBiasSnx();
438 }
439 if (_bsx->readFile(bsxFileName) != success) {
440 emit newMessage("bncComb: wrong Bias SINEX file", true);
441 delete _bsx;
442 _bsx = 0;
443 }
444 else {
445 emit newMessage("bncComb: successfully read Bias SINEX file", true);
446 }
447
448 QTimer::singleShot(_ms, this, SLOT(slotReadBiasSnxFile()));
449}
450
451
452// Remember orbit corrections
453////////////////////////////////////////////////////////////////////////////
454void bncComb::slotNewOrbCorrections(QList<t_orbCorr> orbCorrections) {
455 QMutexLocker locker(&_mutex);
456 for (int ii = 0; ii < orbCorrections.size(); ii++) {
457 t_orbCorr& orbCorr = orbCorrections[ii];
458 QString staID(orbCorr._staID.c_str());
459 char sys = orbCorr._prn.system();
460
461 if (!_cmbSysPrn.contains(sys)){
462 continue;
463 }
464
465 // Find/Check the AC Name
466 // ----------------------
467 QString acName;
468 QStringList excludeSats;
469 QListIterator<cmbAC*> icAC(_ACs);
470 while (icAC.hasNext()) {
471 cmbAC* AC = icAC.next();
472 if (AC->mountPoint == staID) {
473 acName = AC->name;
474 excludeSats = AC->excludeSats;
475 break;
476 }
477 }
478 if (acName.isEmpty() || excludeSat(orbCorr._prn, excludeSats)) {
479 continue;
480 }
481
482 // Store the correction
483 // --------------------
484 QMap<t_prn, t_orbCorr>& storage = _orbCorrections[acName];
485 storage[orbCorr._prn] = orbCorr;
486 }
487}
488
489// Remember Satellite Code Biases
490////////////////////////////////////////////////////////////////////////////
491void bncComb::slotNewCodeBiases(QList<t_satCodeBias> satCodeBiases) {
492 QMutexLocker locker(&_mutex);
493
494 for (int ii = 0; ii < satCodeBiases.size(); ii++) {
495 t_satCodeBias& satCodeBias = satCodeBiases[ii];
496 QString staID(satCodeBias._staID.c_str());
497 char sys = satCodeBias._prn.system();
498
499 if (!_cmbSysPrn.contains(sys)){
500 continue;
501 }
502
503 // Find/Check the AC Name
504 // ----------------------
505 QString acName;
506 QStringList excludeSats;
507 QListIterator<cmbAC*> icAC(_ACs);
508 while (icAC.hasNext()) {
509 cmbAC* AC = icAC.next();
510 if (AC->mountPoint == staID) {
511 acName = AC->name;
512 excludeSats = AC->excludeSats;
513 break;
514 }
515 }
516 if (acName.isEmpty() || excludeSat(satCodeBias._prn, excludeSats)) {
517 continue;
518 }
519
520 // Store the correction
521 // --------------------
522 QMap<t_prn, t_satCodeBias>& storage = _satCodeBiases[acName];
523 storage[satCodeBias._prn] = satCodeBias;
524 }
525 return;
526}
527
528// Remember Satellite Phase Biases
529////////////////////////////////////////////////////////////////////////////
530void bncComb::slotNewPhaseBiases(QList<t_satPhaseBias> satPhaseBiases) {
531 QMutexLocker locker(&_mutex);
532
533 for (int ii = 0; ii < satPhaseBiases.size(); ii++) {
534 t_satPhaseBias& satPhaseBias = satPhaseBiases[ii];
535 QString staID(satPhaseBias._staID.c_str());
536 char sys = satPhaseBias._prn.system();
537
538 if (!_cmbSysPrn.contains(sys)){
539 continue;
540 }
541
542 // Find/Check the AC Name
543 // ----------------------
544 QString acName;
545 QStringList excludeSats;
546 QListIterator<cmbAC*> icAC(_ACs);
547 while (icAC.hasNext()) {
548 cmbAC* AC = icAC.next();
549 if (AC->mountPoint == staID) {
550 acName = AC->name;
551 excludeSats = AC->excludeSats;
552 break;
553 }
554 }
555 if (acName.isEmpty() || excludeSat(satPhaseBias._prn, excludeSats)) {
556 continue;
557 }
558
559 // Store the correction
560 // --------------------
561 QMap<t_prn, t_satPhaseBias>& storage = _satPhaseBiases[acName];
562 storage[satPhaseBias._prn] = satPhaseBias;
563 }
564 return;
565}
566
567
568// True if every AC has delivered clock corrections for an epoch after epoTime
569////////////////////////////////////////////////////////////////////////////
570bool bncComb::allACsBeyond(const bncTime& epoTime) const {
571 QListIterator<cmbAC*> itAC(_ACs);
572 while (itAC.hasNext()) {
573 const cmbAC* AC = itAC.next();
574 bncTime acLast = _acLastClkTime.value(AC->name);
575 if (!acLast.valid() || !(acLast > epoTime)) {
576 return false;
577 }
578 }
579 return true;
580}
581
582// Process clock corrections
583////////////////////////////////////////////////////////////////////////////
584void bncComb::slotNewClkCorrections(QList<t_clkCorr> clkCorrections) {
585 QMutexLocker locker(&_mutex);
586 const double outWait = 1.0 * _cmbSampl;
587 int currentWeek = 0;
588 double currentSec = 0.0;
589 currentGPSWeeks(currentWeek, currentSec);
590 bncTime currentTime(currentWeek, currentSec);
591
592 // Loop over all observations (possible different epochs)
593 // -----------------------------------------------------
594 for (int ii = 0; ii < clkCorrections.size(); ii++) {
595 t_clkCorr& newClk = clkCorrections[ii];
596 char sys = newClk._prn.system();
597 if (!_cmbSysPrn.contains(sys)){
598 continue;
599 }
600
601 // Remember the latest correction epoch of each AC (whether or not the
602 // correction is used), to know when an AC has finished an epoch
603 // --------------------------------------------------------------------
604 QListIterator<cmbAC*> itAC(_ACs);
605 while (itAC.hasNext()) {
606 cmbAC* AC = itAC.next();
607 if (AC->mountPoint == QString(newClk._staID.c_str())) {
608 bncTime& acLast = _acLastClkTime[AC->name];
609 if (!acLast.valid() || newClk._time > acLast) {
610 acLast = newClk._time;
611 }
612 break;
613 }
614 }
615
616 // Check Modulo Time
617 // -----------------
618 int sec = int(nint(newClk._time.gpssec()*10));
619 if (sec % (_cmbSampl*10) != 0.0) {
620 continue;
621 }
622
623 // Find/Check the AC Name
624 // ----------------------
625 QString acName;
626 QStringList excludeSats;
627 bool isAPC;
628 QString staID(newClk._staID.c_str());
629 QListIterator<cmbAC*> icAC(_ACs);
630 while (icAC.hasNext()) {
631 cmbAC* AC = icAC.next();
632 if (AC->mountPoint == staID) {
633 acName = AC->name;
634 excludeSats = AC->excludeSats;
635 isAPC = AC->isAPC;
636 break;
637 }
638 }
639 if (acName.isEmpty() || excludeSat(newClk._prn, excludeSats)) {
640 continue;
641 }
642 if (!_cmbCoM && isAPC != _masterIsAPC[sys]) {
643 bncTime& warned = _refPointWarned[acName + sys];
644 if (!warned.valid() || newClk._time - warned >= 3600.0) {
645 warned = newClk._time;
646 emit newMessage("bncComb: " + acName.toLatin1() + " " + QByteArray(1, sys) + " not combined: its orbits refer to the " +
647 (isAPC ? "APC" : "CoM") + ", those of the master AC " + _masterOrbitAC[sys].toLatin1() +
648 " to the " + (isAPC ? "CoM" : "APC") +
649 " (an ANTEX file allows to combine both in the CoM frame)", true);
650 }
651 continue;
652 }
653
654 // Check regarding current time
655 // ----------------------------
656 if (BNC_CORE->mode() != t_bncCore::batchPostProcessing) {
657 if (fabs(currentTime - newClk._time) > 60.0) { // future or very old data sets
658 #ifdef BNC_DEBUG_CMB
659 emit newMessage("bncComb: future or very old data sets: " + acName.toLatin1() + " " + newClk._prn.toString().c_str() +
660 QString(" %1 ").arg(newClk._time.timestr().c_str()).toLatin1() + "vs. current time" +
661 QString(" %1 ").arg(currentTime.timestr().c_str()).toLatin1(), true);
662 #endif
663 continue;
664 }
665 }
666
667 // Check Correction Age
668 // --------------------
669 if (_resTime.valid() && newClk._time <= _resTime) {
670#ifdef BNC_DEBUG_CMB
671 emit newMessage("bncComb: old correction: " + acName.toLatin1() + " " + newClk._prn.toString().c_str() +
672 QString(" %1 ").arg(newClk._time.timestr().c_str()).toLatin1() + "vs. last processing Epoch" +
673 QString(" %1 ").arg(_resTime.timestr().c_str()).toLatin1(), true);
674#endif
675 continue;
676 }
677
678 // Set the last time
679 // -----------------
680 if (_lastClkCorrTime.undef() || newClk._time > _lastClkCorrTime) {
681 _lastClkCorrTime = newClk._time;
682 }
683
684 // Find the corresponding data epoch or create a new one
685 // -----------------------------------------------------
686 epoClkData* epoch = 0;
687 deque<epoClkData*>::const_iterator it;
688 for (it = _epoClkData.begin(); it != _epoClkData.end(); it++) {
689 if (newClk._time == (*it)->_time) {
690 epoch = *it;
691 break;
692 }
693 }
694 if (epoch == 0) {
695 if (_epoClkData.empty() || newClk._time > _epoClkData.back()->_time) {
696 epoch = new epoClkData;
697 epoch->_time = newClk._time;
698 _epoClkData.push_back(epoch);
699 }
700 }
701
702 // Put data into the epoch
703 // -----------------------
704 if (epoch != 0) {
705 epoch->_clkCorr.push_back(newClk);
706 }
707 }
708
709 // Make sure the buffer does not grow beyond any limit
710 // ---------------------------------------------------
711 const unsigned MAX_EPODATA_SIZE = 60.0 / _cmbSampl;
712 if (_epoClkData.size() > MAX_EPODATA_SIZE) {
713 delete _epoClkData.front();
714 _epoClkData.pop_front();
715 }
716
717 // Process the oldest epochs
718 // ------------------------
719 while (_epoClkData.size()) {
720
721 if (!_lastClkCorrTime.valid()) {
722 return;
723 }
724
725 const vector<t_clkCorr>& clkCorrVec = _epoClkData.front()->_clkCorr;
726
727 bncTime epoTime = _epoClkData.front()->_time;
728
729 if (BNC_CORE->mode() != t_bncCore::batchPostProcessing) {
730 if ((currentTime - epoTime) > 60.0) {
731 delete _epoClkData.front();
732 _epoClkData.pop_front();
733 continue;
734 }
735 }
736 // Process the front epoch as soon as all ACs have delivered corrections
737 // for a later epoch (i.e. have finished this one), at the latest when
738 // the newest correction is maxWait newer (an AC may be late or missing).
739 // Waiting only outWait after the newest correction lost the corrections
740 // of ACs with a few seconds more latency ("old correction").
741 // -----------------------------------------------------------------------
742 const double maxWait = max(10.0, outWait);
743 if (allACsBeyond(epoTime) || epoTime < (_lastClkCorrTime - maxWait)) {
744 _resTime = epoTime;
745 processEpoch(_resTime, clkCorrVec);
746 delete _epoClkData.front();
747 _epoClkData.pop_front();
748 }
749 else {
750 return;
751 }
752 }
753}
754
755// Change the correction so that it refers to last received ephemeris
756////////////////////////////////////////////////////////////////////////////
757void bncComb::switchToLastEph(t_eph* lastEph, cmbCorr* corr) {
758
759 if (corr->_eph == lastEph) {
760 return;
761 }
762
763 ColumnVector oldXC(6);
764 ColumnVector oldVV(3);
765 if (corr->_eph->getCrd(corr->_time, oldXC, oldVV, false) != success) {
766 return;
767 }
768
769 ColumnVector newXC(6);
770 ColumnVector newVV(3);
771 if (lastEph->getCrd(corr->_time, newXC, newVV, false) != success) {
772 return;
773 }
774
775 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
776 ColumnVector dV = newVV - oldVV;
777 double dC = newXC(4) - oldXC(4);
778
779 ColumnVector dRAO(3);
780 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
781
782 ColumnVector dDotRAO(3);
783 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
784
785 QString msg = "bncComb: switch corr " + corr->_prn.mid(0,3)
786 + QString(" %1 -> %2 %3").arg(corr->_iod,3).arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
787
788 emit newMessage(msg.toLatin1(), false);
789
790 corr->_iod = lastEph->IOD();
791 corr->_eph = lastEph;
792
793 corr->_orbCorr._xr += dRAO;
794 corr->_orbCorr._dotXr += dDotRAO;
795 corr->_clkCorr._dClk -= dC;
796}
797
798// Process Epoch
799////////////////////////////////////////////////////////////////////////////
800void bncComb::processEpoch(bncTime epoTime, const vector<t_clkCorr>& clkCorrVec) {
801 QDateTime now = currentDateAndTimeGPS();
802 bncTime currentTime(now.toString(Qt::ISODate).toStdString());
803 for (unsigned ii = 0; ii < clkCorrVec.size(); ii++) {
804 const t_clkCorr& clkCorr = clkCorrVec[ii];
805 QString staID(clkCorr._staID.c_str());
806 QString prnStr(clkCorr._prn.toInternalString().c_str());
807 char sys = clkCorr._prn.system();
808
809 // Find/Check the AC Name
810 // ----------------------
811 QString acName;
812 double weigthFactor;
813 QListIterator<cmbAC*> icAC(_ACs);
814 while (icAC.hasNext()) {
815 cmbAC* AC = icAC.next();
816 if (AC->mountPoint == staID) {
817 acName = AC->name;
818 weigthFactor = AC->weightFactor;
819 break;
820 }
821 }
822
823 // Create new correction
824 // ---------------------
825 _newCorr = new cmbCorr();
826 _newCorr->_prn = prnStr;
827 _newCorr->_time = clkCorr._time;
828 _newCorr->_iod = clkCorr._iod;
829 _newCorr->_acName = acName;
830 _newCorr->_weightFactor = weigthFactor;
831 _newCorr->_clkCorr = clkCorr;
832
833 // Check orbit correction
834 // ----------------------
835 if (!_orbCorrections.contains(acName)) {
836 delete _newCorr; _newCorr = 0;
837 continue;
838 }
839 else {
840 QMap<t_prn, t_orbCorr>& storage = _orbCorrections[acName];
841 if (!storage.contains(clkCorr._prn) ||
842 storage[clkCorr._prn]._iod != _newCorr->_iod) {
843 delete _newCorr; _newCorr = 0;
844 continue;
845 }
846 else {
847 _newCorr->_orbCorr = storage[clkCorr._prn];
848 }
849 }
850
851 // Check the Ephemeris
852 //--------------------
853 t_eph* ephLast = _ephUser.ephLast(prnStr);
854 t_eph* ephPrev = _ephUser.ephPrev(prnStr);
855 if (ephLast == 0) {
856#ifdef BNC_DEBUG_CMB
857 emit newMessage("bncComb: eph not found for " + prnStr.mid(0,3).toLatin1(), true);
858#endif
859 delete _newCorr; _newCorr = 0;
860 continue;
861 }
862 else if (outDatedBcep(ephLast, currentTime) ||
863 ephLast->checkState() == t_eph::bad ||
864 ephLast->checkState() == t_eph::outdated ||
865 ephLast->checkState() == t_eph::unhealthy) {
866#ifdef BNC_DEBUG_CMB
867 emit newMessage("bncComb: ephLast not ok (checkState: " + ephLast->checkStateToString().toLatin1() + ") for " + prnStr.mid(0,3).toLatin1(), true);
868#endif
869 delete _newCorr; _newCorr = 0;
870 continue;
871 }
872 else {
873 if (ephLast->IOD() == _newCorr->_iod) {
874 _newCorr->_eph = ephLast;
875 }
876 else if (ephPrev &&
877 !outDatedBcep(ephPrev, currentTime) && // if not updated in storage
878 ephPrev->checkState() == t_eph::ok && // received
879 ephPrev->IOD() == _newCorr->_iod) {
880 _newCorr->_eph = ephPrev;
881 switchToLastEph(ephLast, _newCorr);
882 }
883 else {
884#ifdef BNC_DEBUG_CMB
885 emit newMessage("bncComb: eph not found for " + prnStr.mid(0,3).toLatin1() +
886 QString(" with IOD %1").arg(_newCorr->_iod).toLatin1(), true);
887#endif
888 delete _newCorr; _newCorr = 0;
889 continue;
890 }
891 }
892
893 // Check satellite code biases
894 // ----------------------------
895 QMap<t_frequency::type, double> codeBiasesRefSig;
896 if (_satCodeBiases.contains(acName)) {
897 QMap<t_prn, t_satCodeBias>& storage = _satCodeBiases[acName];
898 if (storage.contains(clkCorr._prn)) {
899 _newCorr->_satCodeBias = storage[clkCorr._prn];
900 for (unsigned ii = 1; ii < cmbRefSig::cIF; ii++) {
901 t_frequency::type frqType = cmbRefSig::toFreq(sys, static_cast<cmbRefSig::type>(ii));
902 char frqNum = t_frequency::toString(frqType)[1];
903 char attrib = cmbRefSig::toAttrib(sys, static_cast<cmbRefSig::type>(ii));
904 QString rnxType2ch = QString("%1%2").arg(frqNum).arg(attrib);
905 for (unsigned ii = 0; ii < _newCorr->_satCodeBias._bias.size(); ii++) {
906 const t_frqCodeBias& bias = _newCorr->_satCodeBias._bias[ii];
907 if (rnxType2ch.toStdString() == bias._rnxType2ch) {
908 codeBiasesRefSig[frqType] = bias._value;
909 }
910 }
911 }
912 // only with the biases of both reference signals (one of them alone
913 // does not refer the clocks to the reference signals)
914 if (codeBiasesRefSig.size() == 2) {
915 map<t_frequency::type, double> codeCoeff;
916 double channel = double(_newCorr->_eph->slotNum());
917 cmbRefSig::coeff(sys, cmbRefSig::cIF, channel, codeCoeff);
918 map<t_frequency::type, double>::const_iterator it;
919 for (it = codeCoeff.begin(); it != codeCoeff.end(); it++) {
920 t_frequency::type frqType = it->first;
921 double codeCoeff = it->second;
922 _newCorr->_satCodeBiasIF += codeCoeff * codeBiasesRefSig[frqType];
923 }
924 _acBiasIF[acName][prnStr.mid(0,3)] = _newCorr->_satCodeBiasIF;
925 }
926 else {
927 _acBiasIF[acName].remove(prnStr.mid(0,3));
928 }
929 _newCorr->_satCodeBias._bias.clear();
930 }
931 }
932 // Without the code biases of both reference signals (none at all, or
933 // only one of them, e.g. Galileo HAS for GPS), the AC clock cannot be
934 // referred to the reference signals: it is combined as it is, but
935 // excluded from the clock datum (see createAmat) - warn at most once
936 // per hour per AC and system
937 _newCorr->_refBiasesOK = (codeBiasesRefSig.size() == 2);
938 if (!_newCorr->_refBiasesOK) {
939 _acBiasIF[acName].remove(prnStr.mid(0,3));
940 QString key = acName + sys;
941 bncTime& warned = _refBiasWarned[key];
942 if (!warned.valid() || _newCorr->_time - warned >= 3600.0) {
943 warned = _newCorr->_time;
944 QString missing;
945 for (unsigned ii = 1; ii < cmbRefSig::cIF; ii++) {
946 t_frequency::type frqType = cmbRefSig::toFreq(sys, static_cast<cmbRefSig::type>(ii));
947 if (!codeBiasesRefSig.contains(frqType)) {
948 missing += QString(" C%1%2").arg(t_frequency::toString(frqType)[1])
949 .arg(cmbRefSig::toAttrib(sys, static_cast<cmbRefSig::type>(ii)));
950 }
951 }
952 emit newMessage("bncComb: " + acName.toLatin1() + " provides no code biases for the " +
953 QByteArray(1, sys) + " reference signal(s)" + missing.toLatin1() +
954 " (e.g. " + prnStr.mid(0,3).toLatin1() + ") - these clocks are combined"
955 " without referring them to the reference signals and, as long as other ACs"
956 " provide referred clocks, excluded from the clock datum", false);
957 }
958 }
959
960 // Check satellite phase biases
961 // ----------------------------
962 if (_satPhaseBiases.contains(acName)) {
963 QMap<t_prn, t_satPhaseBias>& storage = _satPhaseBiases[acName];
964 if (storage.contains(clkCorr._prn)) {
965 // Yaw angle
966 t_satPhaseBias& pb = storage[clkCorr._prn];
967 double dt = _newCorr->_time - pb._time;
968 if (pb._updateInt) {
969 dt -= (0.5 * ssrUpdateInt[pb._updateInt]);
970 }
971 _newCorr->_satYawAngle = pb._yaw + pb._yawRate * dt;
972 _newCorr->_satYawAngleValid = true;
973
974 // _lambdaIF
975 map<t_frequency::type, double> codeCoeff;
976 double channel = double(_newCorr->_eph->slotNum());
977 cmbRefSig::coeff(sys, cmbRefSig::cIF, channel, codeCoeff);
978 map<t_frequency::type, double>::const_iterator it;
979 for (it = codeCoeff.begin(); it != codeCoeff.end(); it++) {
980 t_frequency::type frqType = it->first;
981 double codeCoeff = it->second;
982 _newCorr->_lambdaIF += codeCoeff * t_CST::c / t_CST::freq(frqType, channel);
983 }
984 }
985 }
986
987 // Orbits of an APC AC in a combination in the CoM frame
988 // ------------------------------------------------------
989 if (_cmbCoM) {
990 const cmbAC* AC = 0;
991 for (const cmbAC* ac : _ACs) {
992 if (ac->name == acName) {
993 AC = ac;
994 break;
995 }
996 }
997 if (AC && AC->isAPC && convertApcToCoM(_newCorr, AC) != success) {
998 delete _newCorr; _newCorr = 0;
999 continue;
1000 }
1001 }
1002
1003 // Store correction into the buffer
1004 // --------------------------------
1005 QVector<cmbCorr*>& corrs = _buffer[sys].corrs;
1006 QVectorIterator<cmbCorr*> itCorr(corrs);
1007 bool available = false;
1008 while (itCorr.hasNext()) {
1009 cmbCorr* corr = itCorr.next();
1010 QString prnStr = corr->_prn;
1011 QString acName = corr->_acName;
1012 if (_newCorr->_acName == acName &&
1013 _newCorr->_prn == prnStr) {
1014 available = true;
1015 }
1016 }
1017
1018 if (!available) {
1019 corrs.push_back(_newCorr); _newCorr = 0;
1020 }
1021 else {
1022 delete _newCorr; _newCorr = 0;
1023 continue;
1024 }
1025 }
1026
1027 // Process Systems of this Epoch
1028 // ------------------------------
1029 QMapIterator<char, unsigned> itSys(_cmbSysPrn);
1030 while (itSys.hasNext()) {
1031 itSys.next();
1032 char sys = itSys.key();
1033 _log.clear();
1034 QTextStream out(&_log, QIODevice::WriteOnly);
1035 processSystem(epoTime, sys, out);
1036 out.flush(); // QTextStream buffers: pass the complete log text on
1037 _buffer.remove(sys);
1038 if (_logFile) {
1039 _logFile->write(epoTime.gpsw(),epoTime.gpssec(), QString(_log));
1040 }
1041 }
1042}
1043
1044void bncComb::processSystem(bncTime epoTime, char sys, QTextStream& out) {
1045
1046 out << "\n"
1047 << epoTime.datestr().c_str() << " "
1048 << epoTime.timestr().c_str() << " "
1049 << "Combination: " << sys << "\n"
1050 << "--------------------------------------" << "\n";
1051
1052 // Observation Statistics
1053 // ----------------------
1054 bool masterPresent = false;
1055 QListIterator<cmbAC*> icAC(_ACs);
1056 while (icAC.hasNext()) {
1057 cmbAC* AC = icAC.next();
1058 AC->numObs[sys] = 0;
1059 QVectorIterator<cmbCorr*> itCorr(corrs(sys));
1060 while (itCorr.hasNext()) {
1061 cmbCorr* corr = itCorr.next();
1062 if (corr->_acName == AC->name) {
1063 AC->numObs[sys] += 1;
1064 if (AC->name == _masterOrbitAC[sys]) {
1065 masterPresent = true;
1066 }
1067 }
1068 }
1069 out << epoTime.datestr().c_str() << " "
1070 << epoTime.timestr().c_str() << " "
1071 << "Sat Num " << sys << " "
1072 << AC->name.toLatin1().data() << ": " << AC->numObs[sys] << "\n";
1073 }
1074
1075 // If Master not present, switch to another one
1076 // --------------------------------------------
1077 const unsigned switchMasterAfterGap = 1;
1078 if (masterPresent) {
1079 _masterMissingEpochs[sys] = 0;
1080 }
1081 else {
1082 ++_masterMissingEpochs[sys];
1083 if (_masterMissingEpochs[sys] < switchMasterAfterGap) {
1084 out << "Missing Master, Epoch skipped" << "\n";
1085 _buffer.remove(sys);
1086 emit newMessage(_log, false);
1087 return;
1088 }
1089 else {
1090 _masterMissingEpochs[sys] = 0;
1091 QListIterator<cmbAC*> icAC(_ACs);
1092 while (icAC.hasNext()) {
1093 cmbAC* AC = icAC.next();
1094 if (AC->numObs[sys] > 0) {
1095 out << epoTime.datestr().c_str() << " "
1096 << epoTime.timestr().c_str() << " "
1097 << "Switching Master " << sys << " "
1098 << _masterOrbitAC[sys].toLatin1().data() << " --> "
1099 << AC->name.toLatin1().data() << "\n";
1100 _masterOrbitAC[sys] = AC->name;
1101 _masterIsAPC[sys] = _cmbCoM ? false : AC->isAPC;
1102 break;
1103 }
1104 }
1105 }
1106 }
1107
1108 QMap<QString, cmbCorr*> masterCorr;
1109
1110 // Perform the actual Combination using selected Method
1111 // ----------------------------------------------------
1112 t_irc irc;
1113 ColumnVector dx;
1114 if (_method == filter) {
1115 irc = processEpoch_filter(epoTime, sys, out, masterCorr, dx);
1116 }
1117 else {
1118 irc = processEpoch_singleEpoch(epoTime, sys, out, masterCorr, dx);
1119 }
1120
1121 // Update Parameter Values, Print Results
1122 // --------------------------------------
1123 if (irc == success) {
1124 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
1125 cmbParam* pp = _params[sys][iPar-1];
1126 pp->xx += dx(iPar);
1127 if (pp->type == cmbParam::clkSat) {
1128 if (masterCorr.find(pp->prn) != masterCorr.end()) {
1129 // set clock result
1130 masterCorr[pp->prn]->_dClkResult = pp->xx / t_CST::c;
1131 // Add Code Biases from SINEX File
1132 if (_bsx) {
1133 map<t_frequency::type, double> codeCoeff;
1134 double channel = double(masterCorr[pp->prn]->_eph->slotNum());
1135 cmbRefSig::coeff(sys, cmbRefSig::cIF, channel, codeCoeff);
1136 t_frequency::type fType1 = cmbRefSig::toFreq(sys, cmbRefSig::c1);
1137 t_frequency::type fType2 = cmbRefSig::toFreq(sys, cmbRefSig::c2);
1138 _bsx->determineSsrSatCodeBiases(pp->prn.mid(0,3), codeCoeff[fType1], codeCoeff[fType2], masterCorr[pp->prn]->_satCodeBias);
1139 }
1140 }
1141 }
1142 out << epoTime.datestr().c_str() << " "
1143 << epoTime.timestr().c_str() << " ";
1144 out.setRealNumberNotation(QTextStream::FixedNotation);
1145 out.setFieldWidth(8);
1146 out.setRealNumberPrecision(4);
1147 out << pp->toString(sys) << " "
1148 << pp->xx << " +- " << sqrt(_QQ[sys](pp->index,pp->index)) << "\n";
1149 out.setFieldWidth(0);
1150 out.flush();
1151 }
1152 if (_method == filter) {
1153 checkBiasConsistency(epoTime, sys, out);
1154 checkPersistentOutliers(epoTime, sys, out);
1155 }
1156 printResults(epoTime, out, masterCorr);
1157 dumpResults(epoTime, masterCorr);
1158 }
1159}
1160
1161// Process Epoch - Filter Method
1162////////////////////////////////////////////////////////////////////////////
1163t_irc bncComb::processEpoch_filter(bncTime epoTime, char sys, QTextStream& out,
1164 QMap<QString, cmbCorr*>& masterCorr,
1165 ColumnVector& dx) {
1166
1167 // Prediction Step
1168 // ---------------
1169 int nPar = _params[sys].size();
1170 ColumnVector x0(nPar);
1171 for (int iPar = 1; iPar <= nPar; iPar++) {
1172 cmbParam* pp = _params[sys][iPar-1];
1173 if (pp->epoSpec) {
1174 pp->xx = 0.0;
1175 _QQ[sys].Row(iPar) = 0.0;
1176 _QQ[sys].Column(iPar) = 0.0;
1177 _QQ[sys](iPar,iPar) = pp->sig0 * pp->sig0;
1178 }
1179 else {
1180 _QQ[sys](iPar,iPar) += pp->sigP * pp->sigP;
1181 }
1182 x0(iPar) = pp->xx;
1183 }
1184
1185 // Check Satellite Positions for Outliers
1186 // --------------------------------------
1187 if (checkOrbits(epoTime, sys, out, masterCorr) != success) {
1188 return failure;
1189 }
1190
1191 // Statistics of persistent outliers
1192 // ---------------------------------
1193 for (int ii = 0; ii < corrs(sys).size(); ii++) {
1194 const cmbCorr* corr = corrs(sys)[ii];
1195 _outlierStat[sys][corr->_acName + ' ' + corr->_prn.mid(0,3)].numEpo += 1;
1196 }
1197
1198 // Update and outlier detection loop
1199 // ---------------------------------
1200 SymmetricMatrix QQ_sav = _QQ[sys];
1201 while (_running) {
1202
1203 Matrix AA;
1204 ColumnVector ll;
1205 DiagonalMatrix PP;
1206
1207 if (createAmat(sys, AA, ll, PP, x0, masterCorr) != success) {
1208 return failure;
1209 }
1210
1211 dx.ReSize(nPar); dx = 0.0;
1212 kalman(AA, ll, PP, _QQ[sys], dx);
1213
1214 ColumnVector vv = ll - AA * dx;
1215
1216 int maxResIndex;
1217 double maxRes = vv.maximum_absolute_value1(maxResIndex);
1218 out.setRealNumberNotation(QTextStream::FixedNotation);
1219 out.setRealNumberPrecision(3);
1220 out << epoTime.datestr().c_str() << " " << epoTime.timestr().c_str()
1221 << " Maximum Residuum " << maxRes << ' '
1222 << corrs(sys)[maxResIndex-1]->_acName << ' ' << corrs(sys)[maxResIndex-1]->_prn.mid(0,3);
1223 if (maxRes > _MAX_RES) {
1224 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
1225 cmbParam* pp = _params[sys][iPar-1];
1226 if (pp->type == cmbParam::offACSat &&
1227 pp->AC == corrs(sys)[maxResIndex-1]->_acName &&
1228 pp->prn == corrs(sys)[maxResIndex-1]->_prn) {
1229 QQ_sav.Row(iPar) = 0.0;
1230 QQ_sav.Column(iPar) = 0.0;
1231 QQ_sav(iPar,iPar) = pp->sig0 * pp->sig0;
1232 }
1233 }
1234
1235 out << " Outlier" << "\n";
1236 _outlierStat[sys][corrs(sys)[maxResIndex-1]->_acName + ' ' +
1237 corrs(sys)[maxResIndex-1]->_prn.mid(0,3)].residuals.push_back(maxRes);
1238 _QQ[sys] = QQ_sav;
1239 delete corrs(sys)[maxResIndex-1];
1240 corrs(sys).remove(maxResIndex-1);
1241 }
1242 else {
1243 out << " OK" << "\n";
1244 out.setRealNumberNotation(QTextStream::FixedNotation);
1245 out.setRealNumberPrecision(4);
1246 for (int ii = 0; ii < corrs(sys).size(); ii++) {
1247 const cmbCorr* corr = corrs(sys)[ii];
1248 out << epoTime.datestr().c_str() << ' '
1249 << epoTime.timestr().c_str() << " "
1250 << corr->_acName << ' ' << corr->_prn.mid(0,3);
1251 out.setFieldWidth(10);
1252 out << " res = " << vv[ii] << "\n";
1253 out.setFieldWidth(0);
1254 }
1255 break;
1256 }
1257 }
1258
1259 return success;
1260}
1261
1262// Check the consistency of the AC code biases with the AC clocks
1263////////////////////////////////////////////////////////////////////////////
1264// An AC's clocks refer to its own clock signals; the code biases of the
1265// reference signals refer them to the reference signals (B_IF, ionosphere-
1266// free combination of these biases, added to the AC clocks). If the code
1267// biases do not belong to the clocks (e.g. taken from another product), the
1268// error B_IF is absorbed by the AC's satellite offsets. The satellite offsets
1269// of each AC are therefore regressed against its B_IF every 5 minutes (from
1270// 5 minutes after the start); a warning is issued (at most once per hour per
1271// AC and system) if B_IF varies noticeably between the satellites and the
1272// offsets clearly follow it.
1273////////////////////////////////////////////////////////////////////////////
1274void bncComb::checkBiasConsistency(bncTime epoTime, char sys, QTextStream& out) {
1275
1276 const double CHECK_INT = 300.0; // [s] check interval, first check after start
1277 const unsigned MIN_SAT = 5;
1278 const double MIN_SPREAD = 0.03; // [m] minimum std. deviation of B_IF
1279 const double MIN_SLOPE = 0.3; // 0.5 expected for 2 ACs, -> 1 for more ACs
1280 const double MIN_CORR = 0.7;
1281
1282 if (!_biasCheckStart[sys].valid()) {
1283 _biasCheckStart[sys] = epoTime;
1284 _biasCheckLast[sys] = epoTime;
1285 return;
1286 }
1287 if (epoTime - _biasCheckLast[sys] < CHECK_INT) {
1288 return;
1289 }
1290 _biasCheckLast[sys] = epoTime;
1291
1292 QListIterator<cmbAC*> itAc(_ACs);
1293 while (itAc.hasNext()) {
1294 const QString& acName = itAc.next()->name;
1295 if (!_acBiasIF.contains(acName)) {
1296 continue;
1297 }
1298 const QMap<QString, double>& biasIF = _acBiasIF[acName];
1299
1300 // satellites of this AC in the current epoch with B_IF available
1301 QVector<double> xx, yy;
1302 for (int ii = 0; ii < corrs(sys).size(); ii++) {
1303 const cmbCorr* corr = corrs(sys)[ii];
1304 if (corr->_acName != acName || !biasIF.contains(corr->_prn.mid(0,3))) {
1305 continue;
1306 }
1307 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
1308 const cmbParam* pp = _params[sys][iPar-1];
1309 if (pp->type == cmbParam::offACSat && pp->AC == acName && pp->prn == corr->_prn) {
1310 xx.push_back(biasIF[corr->_prn.mid(0,3)]);
1311 yy.push_back(pp->xx);
1312 break;
1313 }
1314 }
1315 }
1316 unsigned nn = xx.size();
1317 if (nn < MIN_SAT) {
1318 continue;
1319 }
1320 double mx = 0.0, my = 0.0;
1321 for (unsigned ii = 0; ii < nn; ii++) {
1322 mx += xx[ii] / nn;
1323 my += yy[ii] / nn;
1324 }
1325 double sxx = 0.0, syy = 0.0, sxy = 0.0;
1326 for (unsigned ii = 0; ii < nn; ii++) {
1327 sxx += (xx[ii] - mx) * (xx[ii] - mx);
1328 syy += (yy[ii] - my) * (yy[ii] - my);
1329 sxy += (xx[ii] - mx) * (yy[ii] - my);
1330 }
1331 QString key = acName + sys;
1332 double spread = sqrt(sxx / nn);
1333 double slope = (sxx > 0.0) ? sxy / sxx : 0.0;
1334 double corrCoeff = (sxx > 0.0 && syy > 0.0) ? sxy / sqrt(sxx * syy) : 0.0;
1335 if (spread < MIN_SPREAD || slope < MIN_SLOPE || corrCoeff < MIN_CORR) {
1336 if (_biasInconsistent.remove(key)) {
1337 QString msg = QString("%1 %2 code biases consistent with its clocks again, "
1338 "AC used for the clock datum again").arg(acName).arg(sys);
1339 out << epoTime.datestr().c_str() << " " << epoTime.timestr().c_str()
1340 << " " << msg << "\n";
1341 emit newMessage("bncComb: " + msg.toLatin1(), true);
1342 resetSatOffsets(sys);
1343 }
1344 continue;
1345 }
1346
1347 QString refSig;
1348 for (unsigned ii = 1; ii < cmbRefSig::cIF; ii++) {
1349 t_frequency::type frqType = cmbRefSig::toFreq(sys, static_cast<cmbRefSig::type>(ii));
1350 refSig += QString("%1C%2%3").arg(ii > 1 ? "/" : "").arg(t_frequency::toString(frqType)[1])
1351 .arg(cmbRefSig::toAttrib(sys, static_cast<cmbRefSig::type>(ii)));
1352 }
1353 QString msg = QString("%1 %2 code biases (%3) seem inconsistent with its clocks: "
1354 "satellite offsets follow the bias combination B_IF "
1355 "(%4 satellites, B_IF spread %5 m, slope %6, correlation %7)")
1356 .arg(acName).arg(sys).arg(refSig).arg(nn)
1357 .arg(spread, 0, 'f', 3).arg(slope, 0, 'f', 2).arg(corrCoeff, 0, 'f', 2);
1358 if (!_biasInconsistent.contains(key)) {
1359 _biasInconsistent.insert(key);
1360 msg += ", AC excluded from the clock datum";
1361 _biasCheckWarned.remove(key);
1362 resetSatOffsets(sys);
1363 }
1364 out << epoTime.datestr().c_str() << " " << epoTime.timestr().c_str()
1365 << " " << msg << "\n";
1366 bncTime& warned = _biasCheckWarned[key];
1367 if (!warned.valid() || epoTime - warned >= 3600.0) {
1368 warned = epoTime;
1369 emit newMessage("bncComb: " + msg.toLatin1(), true);
1370 }
1371 }
1372}
1373
1374// Convert the orbit correction of an APC AC to the satellite's CoM: the
1375// antenna offset (ANTEX, single reference frequency, attitude as configured
1376// for the AC) is added to the correction in radial, along-track and
1377// cross-track direction. The clock correction is not affected.
1378////////////////////////////////////////////////////////////////////////////
1379t_irc bncComb::convertApcToCoM(cmbCorr* corr, const cmbAC* AC) {
1380 corr->_eph->setOrbCorr(&corr->_orbCorr);
1381 corr->_eph->setClkCorr(&corr->_clkCorr);
1382 ColumnVector xc(6), vv(3);
1383 if (corr->_eph->getCrd(corr->_time, xc, vv, true) != success) {
1384 return failure;
1385 }
1386 bncAntex::e_attMode attMode = bncAntex::ATT_COMPUTED;
1387 double extYaw = 0.0;
1388 if (AC->attitudeSource == "SSR" && corr->_satYawAngleValid) {
1389 attMode = bncAntex::ATT_EXTERNAL;
1390 extYaw = corr->_satYawAngle;
1391 }
1392 else if (AC->attitudeSource == "Nominal") {
1393 attMode = bncAntex::ATT_NOMINAL;
1394 }
1395 double Mjd = corr->_time.mjd() + corr->_time.daysec() / 86400.0;
1396 ColumnVector dx(3); dx = 0.0;
1397 if (_antex->satCoMcorrection(corr->_prn, Mjd, xc.Rows(1,3), vv, dx, attMode, extYaw, AC->name) != success) {
1398 if (_antex->warnNoSatEntry(corr->_prn, corr->_time)) {
1399 emit newMessage("bncComb: no satellite antenna entry valid at " +
1400 QByteArray(corr->_time.datestr().c_str()) + " " + QByteArray(corr->_time.timestr().c_str()) +
1401 " for " + corr->_prn.mid(0,3).toLatin1() +
1402 " in ANTEX file - APC orbit of " + AC->name.toLatin1() + " not converted to CoM, not combined", true);
1403 }
1404 return failure;
1405 }
1406 // X = X_brdc - RSW(xr): X_CoM = X_APC - dx => xr_CoM = xr_APC + RSW(dx)
1407 ColumnVector dRAO(3);
1408 XYZ_to_RSW(xc.Rows(1,3), vv, dx, dRAO);
1409 corr->_orbCorr._xr += dRAO;
1410 return success;
1411}
1412
1413// Report AC satellites rejected as outliers in most epochs
1414////////////////////////////////////////////////////////////////////////////
1415// A persistent clock difference of one AC for a satellite cannot be absorbed
1416// by its satellite offset, as the offsets of all ACs of the satellite sum up
1417// to zero: the correction is rejected again in every epoch. Once per hour
1418// and system, AC satellites rejected in at least half of their epochs are
1419// reported.
1420////////////////////////////////////////////////////////////////////////////
1421void bncComb::checkPersistentOutliers(bncTime epoTime, char sys, QTextStream& out) {
1422
1423 const double INTERVAL = 3600.0;
1424 const unsigned MIN_EPO = 10;
1425
1426 if (!_outlierStatStart[sys].valid()) {
1427 _outlierStatStart[sys] = epoTime;
1428 return;
1429 }
1430 if (epoTime - _outlierStatStart[sys] < INTERVAL) {
1431 return;
1432 }
1433 QMapIterator<QString, t_outlierStat> it(_outlierStat[sys]);
1434 while (it.hasNext()) {
1435 it.next();
1436 const t_outlierStat& stat = it.value();
1437 unsigned numOut = stat.residuals.size();
1438 if (stat.numEpo < MIN_EPO || 2 * numOut < stat.numEpo) {
1439 continue;
1440 }
1441 QVector<double> res = stat.residuals;
1442 std::sort(res.begin(), res.end());
1443 double median = (numOut % 2) ? res[numOut/2] : 0.5 * (res[numOut/2-1] + res[numOut/2]);
1444 QString msg = QString("%1 rejected as outlier in %2% of %3 epochs in the last hour "
1445 "(median residual %4 m)")
1446 .arg(it.key()).arg(100.0 * numOut / stat.numEpo, 0, 'f', 0)
1447 .arg(stat.numEpo).arg(median, 0, 'f', 2);
1448 out << epoTime.datestr().c_str() << " " << epoTime.timestr().c_str() << " " << msg << "\n";
1449 emit newMessage("bncComb: " + msg.toLatin1(), true);
1450 }
1451 _outlierStat[sys].clear();
1452 _outlierStatStart[sys] = epoTime;
1453}
1454
1455// Re-initialize the satellite offsets of all ACs of a system (after a change
1456// of the clock datum)
1457////////////////////////////////////////////////////////////////////////////
1458void bncComb::resetSatOffsets(char sys) {
1459 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
1460 cmbParam* pp = _params[sys][iPar-1];
1461 if (pp->type == cmbParam::offACSat) {
1462 pp->xx = 0.0;
1463 _QQ[sys].Row(iPar) = 0.0;
1464 _QQ[sys].Column(iPar) = 0.0;
1465 _QQ[sys](iPar,iPar) = pp->sig0 * pp->sig0;
1466 }
1467 }
1468}
1469
1470// Print results
1471////////////////////////////////////////////////////////////////////////////
1472void bncComb::printResults(bncTime epoTime, QTextStream& out,
1473 const QMap<QString, cmbCorr*>& masterCorr) {
1474
1475 QMapIterator<QString, cmbCorr*> it(masterCorr);
1476 while (it.hasNext()) {
1477 it.next();
1478 cmbCorr* corr = it.value();
1479 const t_eph* eph = corr->_eph;
1480 if (eph) {
1481 ColumnVector xc(6);
1482 ColumnVector vv(3);
1483 if (eph->getCrd(epoTime, xc, vv, false) != success) {
1484 continue;
1485 }
1486 out << epoTime.datestr().c_str() << " "
1487 << epoTime.timestr().c_str() << " ";
1488 out.setFieldWidth(3);
1489 out << "Full Clock " << corr->_prn.mid(0,3) << " " << corr->_iod << " ";
1490 out.setFieldWidth(14);
1491 out << (xc(4) + corr->_dClkResult) * t_CST::c << "\n";
1492 out.setFieldWidth(0);
1493 out.flush();
1494 }
1495 else {
1496 out << "bncComb::printResults bug" << "\n";
1497 }
1498 }
1499}
1500
1501// Send results to RTNet Decoder and directly to PPP Client
1502////////////////////////////////////////////////////////////////////////////
1503void bncComb::dumpResults(bncTime epoTime, QMap<QString, cmbCorr*>& masterCorr) {
1504
1505 QList<t_orbCorr> orbCorrections;
1506 QList<t_clkCorr> clkCorrections;
1507 QList<t_satCodeBias> satCodeBiasList;
1508 QList<t_satPhaseBias> satPhaseBiasList;
1509
1510 unsigned year, month, day, hour, minute;
1511 double sec;
1512 epoTime.civil_date(year, month, day);
1513 epoTime.civil_time(hour, minute, sec);
1514
1515 QString outLines = QString().asprintf("* %4d %2d %2d %d %d %12.8f\n",
1516 year, month, day, hour, minute, sec);
1517
1518 QMutableMapIterator<QString, cmbCorr*> it(masterCorr);
1519 while (it.hasNext()) {
1520 it.next();
1521 cmbCorr* corr = it.value();
1522
1523 // Reject GLONASS corrections whose broadcast ephemeris has exceeded its
1524 // declared validity window. GLONASS nav ephemerides are issued every
1525 // 30 min and are valid for ±900 s around the reference time (TOC).
1526 // t_ephGlo::position() uses Runge-Kutta integration from TOC; beyond
1527 // that the integration error grows and propagates into both the
1528 // combined clock estimate and the SP3 position. A margin of 60 s is
1529 // allowed, as ACs switch to the next ephemeris a few seconds before its
1530 // window begins (otherwise satellites drop out at every switch).
1531 if (corr->_eph->prn().system() == 'R') {
1532 double age = epoTime - corr->_eph->TOC();
1533 if (fabs(age) > 960.0) {
1534 emit newMessage(QString().asprintf(
1535 "bncComb: skip %s, GLONASS eph age %.0fs",
1536 corr->_prn.mid(0,3).toLatin1().data(), age).toLatin1(), false);
1537 delete corr;
1538 it.remove();
1539 continue;
1540 }
1541 }
1542
1543 // ORBIT
1544 t_orbCorr orbCorr(corr->_orbCorr);
1545 orbCorr._staID = "INTERNAL";
1546 orbCorrections.push_back(orbCorr);
1547
1548 // CLOCK
1549 t_clkCorr clkCorr(corr->_clkCorr);
1550 clkCorr._staID = "INTERNAL";
1551 clkCorr._dClk = corr->_dClkResult;
1552 clkCorr._dotDClk = 0.0;
1553 clkCorr._dotDotDClk = 0.0;
1554 clkCorrections.push_back(clkCorr);
1555
1556 ColumnVector xc(6);
1557 ColumnVector vv(3);
1558 corr->_eph->setClkCorr(dynamic_cast<const t_clkCorr*>(&clkCorr));
1559 corr->_eph->setOrbCorr(dynamic_cast<const t_orbCorr*>(&orbCorr));
1560
1561 if (corr->_eph->getCrd(epoTime, xc, vv, true) != success) {
1562 delete corr;
1563 it.remove();
1564 continue;
1565 }
1566
1567 // Correction Phase Center --> CoM
1568 // -------------------------------
1569 ColumnVector dx(3); dx = 0.0;
1570 ColumnVector apc(3); apc = 0.0;
1571 ColumnVector com(3); com = 0.0;
1572 bool masterIsAPC = true;
1573 if (_antex) {
1574 double Mjd = epoTime.mjd() + epoTime.daysec()/86400.0;
1575 char sys = corr->_eph->prn().system();
1576 masterIsAPC = _masterIsAPC[sys];
1577
1578 // Determine attitude mode from the master orbit AC for this system
1579 bncAntex::e_attMode attMode = bncAntex::ATT_COMPUTED;
1580 double extYaw = 0.0;
1581 const QString& masterName = _masterOrbitAC[sys];
1582 for (const cmbAC* ac : _ACs) {
1583 if (ac->name == masterName) {
1584 if (ac->attitudeSource == "SSR" && corr->_satYawAngleValid) {
1585 attMode = bncAntex::ATT_EXTERNAL;
1586 extYaw = corr->_satYawAngle;
1587 }
1588 else if (ac->attitudeSource == "Nominal") {
1589 attMode = bncAntex::ATT_NOMINAL;
1590 }
1591 break;
1592 }
1593 }
1594
1595 if (_antex->satCoMcorrection(corr->_prn, Mjd, xc.Rows(1,3), vv, dx,
1596 attMode, extYaw) != success) {
1597 // without the satellite antenna offset, the orbit cannot be
1598 // converted between antenna phase center and center of mass
1599 if (_antex->warnNoSatEntry(corr->_prn, epoTime)) {
1600 emit newMessage("bncComb: no satellite antenna entry valid at " +
1601 QByteArray(epoTime.datestr().c_str()) + " " + QByteArray(epoTime.timestr().c_str()) +
1602 " for " + corr->_prn.mid(0,3).toLatin1() +
1603 " in ANTEX file - ANTEX file outdated? Satellite not combined", true);
1604 }
1605 orbCorrections.pop_back();
1606 clkCorrections.pop_back();
1607 delete corr;
1608 it.remove();
1609 continue;
1610 }
1611 QString glonassYawLog = _antex->takeGlonassYawLog();
1612 if (!glonassYawLog.isEmpty()) {
1613 emit newMessage(("bncComb: " + glonassYawLog.trimmed()).toLatin1(), false);
1614 }
1615 QString gpsYawLog = _antex->takeGpsYawLog();
1616 if (!gpsYawLog.isEmpty()) {
1617 emit newMessage(("bncComb: " + gpsYawLog.trimmed()).toLatin1(), false);
1618 }
1619 QString onYawLog = _antex->takeOnYawLog();
1620 if (!onYawLog.isEmpty()) {
1621 emit newMessage(("bncComb: " + onYawLog.trimmed()).toLatin1(), false);
1622 }
1623 }
1624 if (masterIsAPC) {
1625 apc(1) = xc(1);
1626 apc(2) = xc(2);
1627 apc(3) = xc(3);
1628 com(1) = xc(1)-dx(1);
1629 com(2) = xc(2)-dx(2);
1630 com(3) = xc(3)-dx(3);
1631 }
1632 else {
1633 com(1) = xc(1);
1634 com(2) = xc(2);
1635 com(3) = xc(3);
1636 apc(1) = xc(1)+dx(1);
1637 apc(2) = xc(2)+dx(2);
1638 apc(3) = xc(3)+dx(3);
1639 }
1640
1641 // The combined corrections passed on to the PPP (INTERNAL) always refer
1642 // to the APC: orbits combined in the CoM frame are converted
1643 if (!masterIsAPC && _antex) {
1644 ColumnVector dRAO(3);
1645 XYZ_to_RSW(xc.Rows(1,3), vv, dx, dRAO);
1646 orbCorrections.back()._xr -= dRAO;
1647 }
1648
1649 outLines += corr->_prn.mid(0,3);
1650 QString hlp = QString().asprintf(
1651 " APC 3 %15.4f %15.4f %15.4f"
1652 " Clk 1 %15.4f"
1653 " Vel 3 %15.4f %15.4f %15.4f"
1654 " CoM 3 %15.4f %15.4f %15.4f",
1655 apc(1), apc(2), apc(3),
1656 xc(4) * t_CST::c,
1657 vv(1), vv(2), vv(3),
1658 com(1), com(2), com(3));
1659 outLines += hlp;
1660 hlp.clear();
1661
1662 // CODE BIASES
1663 if (corr->_satCodeBias._bias.size()) {
1664 t_satCodeBias satCodeBias(corr->_satCodeBias);
1665 satCodeBias._staID = "INTERNAL";
1666 satCodeBias._ssrIOD = -1; // combined, not any single provider's solution
1667 satCodeBias._ssrProviderID = -1;
1668 satCodeBiasList.push_back(satCodeBias);
1669 hlp = QString().asprintf(" CodeBias %2lu", satCodeBias._bias.size());
1670 outLines += hlp;
1671 hlp.clear();
1672 for (unsigned ii = 0; ii < satCodeBias._bias.size(); ii++) {
1673 const t_frqCodeBias& frqCodeBias = satCodeBias._bias[ii];
1674 if (!frqCodeBias._rnxType2ch.empty()) {
1675 hlp = QString().asprintf(" %s%12.6f",
1676 frqCodeBias._rnxType2ch.c_str(),
1677 frqCodeBias._value);
1678 outLines += hlp;
1679 hlp.clear();
1680 }
1681 }
1682 }
1683 // PHASE BIASES
1684 /* can be added as soon as yaw agles from the ACs have useful values
1685 t_satPhaseBias satPhaseBias(corr->_satPhaseBias);
1686 satPhaseBias._staID = "INTERNAL";
1687 satPhaseBias._yaw = corr->_satYawAngle;
1688 satPhaseBiasList.push_back(satPhaseBias);
1689 hlp = QString().asprintf(" YawAngle %1.4f", satPhaseBias._yaw);
1690 outLines += hlp;
1691 hlp.clear();
1692 hlp = QString().asprintf(" PhaseBias %2lu", satPhaseBias._bias.size());
1693 outLines += hlp;
1694 hlp.clear();
1695 for (unsigned ii = 0; ii < satPhaseBias._bias.size(); ii++) {
1696 const t_frqPhaseBias& frqPhaseBias = satPhaseBias._bias[ii];
1697 if (!frqPhaseBias._rnxType2ch.empty()) {
1698 hlp = QString().asprintf(" %s%10.6f%3d%3d%3d",
1699 frqPhaseBias._rnxType2ch.c_str(),
1700 frqPhaseBias._value,
1701 frqPhaseBias._fixIndicator,
1702 frqPhaseBias._fixWideLaneIndicator,
1703 frqPhaseBias._jumpCounter);
1704 outLines += hlp;
1705 hlp.clear();
1706 }
1707 }*/
1708 outLines += "\n";
1709 delete corr;
1710 it.remove();
1711 }
1712 outLines += "EOE\n"; // End Of Epoch flag
1713 //cout << outLines.toStdString();
1714
1715 vector<string> errmsg;
1716 _rtnetDecoder->Decode(outLines.toLatin1().data(), outLines.length(), errmsg);
1717
1718 // Send new Corrections to PPP etc.
1719 // --------------------------------
1720 if (orbCorrections.size() > 0 && clkCorrections.size() > 0) {
1721 emit newOrbCorrections(orbCorrections);
1722 emit newClkCorrections(clkCorrections);
1723 }
1724 if (satCodeBiasList.size()) {
1725 emit newCodeBiases(satCodeBiasList);
1726 }
1727}
1728
1729// Create First Design Matrix and Vector of Measurements
1730////////////////////////////////////////////////////////////////////////////
1731t_irc bncComb::createAmat(char sys, Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP, const ColumnVector& x0, const QMap<QString, cmbCorr*>& masterCorr) {
1732
1733 unsigned nPar = _params[sys].size();
1734 unsigned nObs = corrs(sys).size();
1735
1736 if (nObs == 0) {
1737 return failure;
1738 }
1739
1740 int maxSat = _cmbSysPrn[sys];
1741
1742 // AC clocks not referred to the reference signals are excluded from the
1743 // zero-sum conditions of the satellite offsets (the clock datum): those of
1744 // ACs whose code biases are inconsistent with their clocks (see
1745 // checkBiasConsistency) and those without the code biases of both
1746 // reference signals. This applies as long as another AC provides referred
1747 // corrections; the sum of the excluded offsets of an AC is constrained to
1748 // zero instead.
1749 QSet<QString> excludedOff; // "AC PRN" (full PRN)
1750 QStringList excludedACs; // ACs with excluded offsets
1751 if (_method == filter) {
1752 bool referredAC = false;
1753 for (int ii = 0; ii < corrs(sys).size(); ii++) {
1754 const cmbCorr* corr = corrs(sys)[ii];
1755 if (_biasInconsistent.contains(corr->_acName + sys) || !corr->_refBiasesOK) {
1756 excludedOff.insert(corr->_acName + ' ' + corr->_prn);
1757 if (!excludedACs.contains(corr->_acName)) {
1758 excludedACs << corr->_acName;
1759 }
1760 }
1761 else {
1762 referredAC = true;
1763 }
1764 }
1765 if (!referredAC) {
1766 excludedOff.clear();
1767 excludedACs.clear();
1768 }
1769 }
1770
1771 const int nCon = (_method == filter) ? 1 + maxSat + excludedACs.size() : 0;
1772
1773 AA.ReSize(nObs+nCon, nPar); AA = 0.0;
1774 ll.ReSize(nObs+nCon); ll = 0.0;
1775 PP.ReSize(nObs+nCon); PP = 1.0 / (sigObs * sigObs);
1776
1777 int iObs = 0;
1778 QVectorIterator<cmbCorr*> itCorr(corrs(sys));
1779 while (itCorr.hasNext()) {
1780 cmbCorr* corr = itCorr.next();
1781 QString prn = corr->_prn;
1782
1783 ++iObs;
1784
1785 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
1786 cmbParam* pp = _params[sys][iPar-1];
1787 AA(iObs, iPar) = pp->partial(sys, corr->_acName, prn);
1788 }
1789 // Consistency corrections to keep the combined clock consistent to masterOrbit
1790 // ----------------------------------------------------------------------------
1791 double dC_orb = DotProduct((corr->_satPos - masterCorr[prn]->_satPos), corr->_satPos);
1792 dC_orb /= masterCorr[prn]->_satPos.NormFrobenius();
1793 double dC_att = (corr->_satYawAngle - masterCorr[prn]->_satYawAngle) / (2 * M_PI);
1794 dC_att *= corr->_lambdaIF;
1795 double dC_apriori_corr = dC_orb;// + dC_att; can be added if useful values are provided by the ACs
1796
1797 // Clock referred to the ionosphere-free combination of the reference
1798 // signals: dt - (alpha b1 + beta b2) with biases in Bias-SINEX sign
1799 // (subtracted from observations, Banville et al. 2020, Eq. 1). The code
1800 // biases taken from the SSR streams have the opposite sign (added to
1801 // observations), hence + _satCodeBiasIF.
1802 ll(iObs) = ((corr->_clkCorr._dClk * t_CST::c) - dC_apriori_corr + corr->_satCodeBiasIF) - DotProduct(AA.Row(iObs), x0);
1803
1804 PP(iObs, iObs) *= 1.0 / (corr->_weightFactor * corr->_weightFactor);
1805 }
1806
1807 // Regularization
1808 // --------------
1809 if (_method == filter) {
1810 const double Ph = 1.e6;
1811 PP(nObs+1) = Ph;
1812 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
1813 cmbParam* pp = _params[sys][iPar-1];
1814 if ( AA.Column(iPar).maximum_absolute_value() > 0.0 &&
1815 pp->type == cmbParam::clkSat ) {
1816 AA(nObs+1, iPar) = 1.0;
1817 }
1818 }
1819 //if (sys == 'R') {
1820 // return success;
1821 // }
1822 int iCond = 1;
1823 QSet<int> offExcluded; // offsets excluded from the zero-sum condition of their satellite
1824 QSet<QString> acsInDatum; // ACs with at least one offset in the zero-sum conditions
1825 // GNSS
1826 for (unsigned iGnss = 1; iGnss <= _cmbSysPrn[sys]; iGnss++) {
1827 int flag = t_corrSSR::getSsrNavTypeFlag(sys, iGnss);
1828 QString prn = QString("%1%2_%3").arg(sys).arg(iGnss, 2, 10, QChar('0')).arg(flag);
1829 ++iCond;
1830 PP(nObs+iCond) = Ph;
1831 QList<int> offAll, offConsistent;
1832 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
1833 cmbParam* pp = _params[sys][iPar-1];
1834 if ( pp &&
1835 AA.Column(iPar).maximum_absolute_value() > 0.0 &&
1836 pp->type == cmbParam::offACSat &&
1837 pp->prn == prn) {
1838 offAll << iPar;
1839 if (!excludedOff.contains(pp->AC + ' ' + pp->prn)) {
1840 offConsistent << iPar;
1841 }
1842 }
1843 }
1844 // satellite observed by inconsistent ACs only: all its offsets are used
1845 const QList<int>& offUsed = offConsistent.isEmpty() ? offAll : offConsistent;
1846 for (int iPar : offAll) {
1847 if (offUsed.contains(iPar)) {
1848 AA(nObs+iCond, iPar) = 1.0;
1849 acsInDatum.insert(_params[sys][iPar-1]->AC);
1850 }
1851 else {
1852 offExcluded << iPar;
1853 }
1854 }
1855 }
1856 // ACs with all their offsets excluded from the zero-sum conditions above:
1857 // the sum of their offsets is constrained to zero (otherwise not
1858 // separable from the AC's epoch offset)
1859 for (const QString& acName : excludedACs) {
1860 ++iCond;
1861 if (acsInDatum.contains(acName)) {
1862 continue;
1863 }
1864 PP(nObs+iCond) = Ph;
1865 for (int iPar : offExcluded) {
1866 if (_params[sys][iPar-1]->AC == acName) {
1867 AA(nObs+iCond, iPar) = 1.0;
1868 }
1869 }
1870 }
1871 }
1872
1873 return success;
1874}
1875
1876// Process Epoch - Single-Epoch Method
1877////////////////////////////////////////////////////////////////////////////
1878t_irc bncComb::processEpoch_singleEpoch(bncTime epoTime, char sys, QTextStream& out,
1879 QMap<QString, cmbCorr*>& masterCorr,
1880 ColumnVector& dx) {
1881
1882 // Check Satellite Positions for Outliers
1883 // --------------------------------------
1884 if (checkOrbits(epoTime, sys, out, masterCorr) != success) {
1885 return failure;
1886 }
1887
1888 // Outlier Detection Loop
1889 // ----------------------
1890 while (_running) {
1891 // Count Number of Observations per Satellite and per AC
1892 // -----------------------------------------------------
1893 QMap<QString, int> numObsPrn;
1894 QMap<QString, int> numObsAC;
1895 QVectorIterator<cmbCorr*> itCorr(corrs(sys));
1896 while (itCorr.hasNext()) {
1897 cmbCorr* corr = itCorr.next();
1898 QString prn = corr->_prn;
1899 QString AC = corr->_acName;
1900 if (numObsPrn.find(prn) == numObsPrn.end()) {
1901 numObsPrn[prn] = 1;
1902 }
1903 else {
1904 numObsPrn[prn] += 1;
1905 }
1906 if (numObsAC.find(AC) == numObsAC.end()) {
1907 numObsAC[AC] = 1;
1908 }
1909 else {
1910 numObsAC[AC] += 1;
1911 }
1912 }
1913
1914 // Clean-Up the Parameters
1915 // -----------------------
1916 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
1917 delete _params[sys][iPar-1];
1918 }
1919 _params[sys].clear();
1920
1921 // Set new Parameters
1922 // ------------------
1923 int nextPar = 0;
1924
1925 QMapIterator<QString, int> itAC(numObsAC);
1926 while (itAC.hasNext()) {
1927 itAC.next();
1928 const QString& AC = itAC.key();
1929 int numObs = itAC.value();
1930 if (AC != _masterOrbitAC[sys] && numObs > 0) {
1931 _params[sys].push_back(new cmbParam(cmbParam::offACgnss, ++nextPar, AC, ""));
1932 }
1933 }
1934
1935 QMapIterator<QString, int> itPrn(numObsPrn);
1936 while (itPrn.hasNext()) {
1937 itPrn.next();
1938 const QString& prn = itPrn.key();
1939 int numObs = itPrn.value();
1940 if (numObs > 0) {
1941 _params[sys].push_back(new cmbParam(cmbParam::clkSat, ++nextPar, "", prn));
1942 }
1943 }
1944
1945 int nPar = _params[sys].size();
1946 ColumnVector x0(nPar);
1947 x0 = 0.0;
1948
1949 // Create First-Design Matrix
1950 // --------------------------
1951 Matrix AA;
1952 ColumnVector ll;
1953 DiagonalMatrix PP;
1954 if (createAmat(sys, AA, ll, PP, x0, masterCorr) != success) {
1955 return failure;
1956 }
1957
1958 ColumnVector vv;
1959 try {
1960 Matrix ATP = AA.t() * PP;
1961 SymmetricMatrix NN; NN << ATP * AA;
1962 ColumnVector bb = ATP * ll;
1963 _QQ[sys] = NN.i();
1964 dx = _QQ[sys] * bb;
1965 vv = ll - AA * dx;
1966 }
1967 catch (Exception& exc) {
1968 out << exc.what() << "\n";
1969 return failure;
1970 }
1971
1972 int maxResIndex;
1973 double maxRes = vv.maximum_absolute_value1(maxResIndex);
1974 out.setRealNumberNotation(QTextStream::FixedNotation);
1975 out.setRealNumberPrecision(3);
1976 out << epoTime.datestr().c_str() << " " << epoTime.timestr().c_str()
1977 << " Maximum Residuum " << maxRes << ' '
1978 << corrs(sys)[maxResIndex-1]->_acName << ' ' << corrs(sys)[maxResIndex-1]->_prn.mid(0,3);
1979
1980 if (maxRes > _MAX_RES) {
1981 out << " Outlier" << "\n";
1982 delete corrs(sys)[maxResIndex-1];
1983 corrs(sys).remove(maxResIndex-1);
1984 }
1985 else {
1986 out << " OK" << "\n";
1987 out.setRealNumberNotation(QTextStream::FixedNotation);
1988 out.setRealNumberPrecision(3);
1989 for (int ii = 0; ii < vv.Nrows(); ii++) {
1990 const cmbCorr* corr = corrs(sys)[ii];
1991 out << epoTime.datestr().c_str() << ' '
1992 << epoTime.timestr().c_str() << " "
1993 << corr->_acName << ' ' << corr->_prn.mid(0,3);
1994 out.setFieldWidth(6);
1995 out << " res = " << vv[ii] << "\n";
1996 out.setFieldWidth(0);
1997 }
1998 return success;
1999 }
2000
2001 }
2002
2003 return failure;
2004}
2005
2006// Check Satellite Positions for Outliers
2007////////////////////////////////////////////////////////////////////////////
2008t_irc bncComb::checkOrbits(bncTime epoTime, char sys, QTextStream& out, QMap<QString, cmbCorr*>& masterCorr) {
2009
2010 // Switch to last ephemeris (if possible)
2011 // --------------------------------------
2012 QMutableVectorIterator<cmbCorr*> im(corrs(sys));
2013 while (im.hasNext()) {
2014 cmbCorr* corr = im.next();
2015 QString prn = corr->_prn;
2016
2017 t_eph* ephLast = _ephUser.ephLast(prn);
2018 t_eph* ephPrev = _ephUser.ephPrev(prn);
2019
2020 if (ephLast == 0) {
2021 out << "checkOrbit: missing eph (not found) " << corr->_prn.mid(0,3) << "\n";
2022 delete corr;
2023 im.remove();
2024 }
2025 else if (corr->_eph == 0) {
2026 out << "checkOrbit: missing eph (zero) " << corr->_prn.mid(0,3) << "\n";
2027 delete corr;
2028 im.remove();
2029 }
2030 // a newer ephemeris may have arrived (or an older one been re-classified)
2031 // since the correction was accepted - never switch to an unusable one
2032 else if (ephLast->checkState() == t_eph::bad ||
2033 ephLast->checkState() == t_eph::outdated ||
2034 ephLast->checkState() == t_eph::unhealthy) {
2035 out << "checkOrbit: last eph not ok (" << ephLast->checkStateToString() << ") " << corr->_prn.mid(0,3) << "\n";
2036 delete corr;
2037 im.remove();
2038 }
2039 else {
2040 if ( corr->_eph == ephLast ||
2041 corr->_eph == ephPrev ) {
2042 switchToLastEph(ephLast, corr);
2043
2044 ColumnVector xc(6);
2045 ColumnVector vv(3);
2046 if (corr->_eph->getCrd(corr->_time, xc, vv, false) != success) {
2047 delete corr;
2048 im.remove();
2049 }
2050 else {
2051 corr->_satPos = xc.Rows(1,3);
2052 }
2053 }
2054 else {
2055 out << "checkOrbit: missing eph (deleted) " << corr->_prn.mid(0,3) << "\n";
2056 delete corr;
2057 im.remove();
2058 }
2059 }
2060 }
2061
2062 QSet<QString> singleACSats; // satellites skipped: corrections from < 2 ACs
2063 QSet<QString> singleACNames;
2064
2065 while (_running) {
2066
2067 // Compute Mean Corrections for all Satellites
2068 // -------------------------------------------
2069 QMap<QString, int> numCorr;
2070 QMap<QString, ColumnVector> meanRao;
2071 QVectorIterator<cmbCorr*> it(corrs(sys));
2072 while (it.hasNext()) {
2073 cmbCorr* corr = it.next();
2074 QString prn = corr->_prn;
2075
2076 if (meanRao.find(prn) == meanRao.end()) {
2077 meanRao[prn].ReSize(4);
2078 meanRao[prn].Rows(1,3) = corr->_orbCorr._xr;
2079 meanRao[prn](4) = 1;
2080 }
2081 else {
2082 meanRao[prn].Rows(1,3) += corr->_orbCorr._xr;
2083 meanRao[prn](4) += 1;
2084 }
2085 if (numCorr.find(prn) == numCorr.end()) {
2086 numCorr[prn] = 1;
2087 }
2088 else {
2089 numCorr[prn] += 1;
2090 }
2091 }
2092
2093 // Compute Differences wrt. Mean, find Maximum
2094 // -------------------------------------------
2095 QMap<QString, cmbCorr*> maxDiff;
2096 it.toFront();
2097 while (it.hasNext()) {
2098 cmbCorr* corr = it.next();
2099 QString prn = corr->_prn;
2100 if (meanRao[prn](4) != 0) {
2101 meanRao[prn] /= meanRao[prn](4);
2102 meanRao[prn](4) = 0;
2103 }
2104 corr->_diffRao2Mean = corr->_orbCorr._xr - meanRao[prn].Rows(1,3);
2105
2106 if (maxDiff.find(prn) == maxDiff.end()) {
2107 maxDiff[prn] = corr;
2108 }
2109 else {
2110 double normMax = maxDiff[prn]->_diffRao2Mean.NormFrobenius();
2111 double norm = corr->_diffRao2Mean.NormFrobenius();
2112 if (norm > normMax) {
2113 maxDiff[prn] = corr;
2114 }
2115 }
2116 }
2117
2118 if (_ACs.size() == 1) {
2119 QVectorIterator<cmbCorr*> it(corrs(sys));
2120 while (it.hasNext()) {
2121 cmbCorr* corr = it.next();
2122 QString prn = corr->_prn;
2123 if (corr->_acName == _masterOrbitAC[sys] && masterCorr.find(prn) == masterCorr.end()) {
2124 masterCorr[prn] = new cmbCorr(*corr);
2125 }
2126 }
2127 break;
2128 }
2129
2130 // Remove Outliers
2131 // ---------------
2132 bool removed = false;
2133 QMutableVectorIterator<cmbCorr*> im(corrs(sys));
2134 while (im.hasNext()) {
2135 cmbCorr* corr = im.next();
2136 QString prn = corr->_prn;
2137 if (numCorr[prn] < 2) {
2138 singleACSats.insert(prn.mid(0,3));
2139 singleACNames.insert(corr->_acName);
2140 delete corr;
2141 im.remove();
2142 }
2143 else if (corr == maxDiff[prn]) {
2144 double norm = corr->_diffRao2Mean.NormFrobenius();
2145 if (norm > _MAX_DISPLACEMENT) {
2146 out << epoTime.datestr().c_str() << " "
2147 << epoTime.timestr().c_str() << " "
2148 << "Orbit Outlier: "
2149 << corr->_acName.toLatin1().data() << " "
2150 << prn.mid(0,3).toLatin1().data() << " "
2151 << corr->_iod << " "
2152 << norm << "\n";
2153 delete corr;
2154 im.remove();
2155 removed = true;
2156 }
2157 }
2158 }
2159 // Set master orbit
2160 // -----------------------------------------
2161 if (!removed) {
2162 QVectorIterator<cmbCorr*> it(corrs(sys));
2163 while (it.hasNext()) {
2164 cmbCorr* corr = it.next();
2165 QString prn = corr->_prn;
2166 if (corr->_acName == _masterOrbitAC[sys] && masterCorr.find(prn) == masterCorr.end()) {
2167 masterCorr[prn] = new cmbCorr(*corr);
2168 }
2169 }
2170 // Remove satellites that are not in masterOrbit
2171 // and compute differences wrt. masterOrbit
2172 // ----------------------------------------------
2173 QMutableVectorIterator<cmbCorr*> im(corrs(sys));
2174 while (im.hasNext()) {
2175 cmbCorr* corr = im.next();
2176 QString prn = corr->_prn;
2177 if (masterCorr.find(prn) == masterCorr.end()) {
2178 delete corr;
2179 im.remove();
2180 }
2181 }
2182 break;
2183 }
2184 }
2185
2186
2187 // Satellites with corrections from fewer than 2 ACs cannot be checked for
2188 // orbit outliers and are skipped (e.g. if an AC is missing): log it, in
2189 // the combination log every epoch, in the BNC log at most once per hour
2190 // -------------------------------------------------------------------------
2191 if (!singleACSats.isEmpty()) {
2192 QStringList sats = singleACSats.values(); sats.sort();
2193 QStringList acs = singleACNames.values(); acs.sort();
2194 QString msg = QString("%1 satellite(s) skipped, corrections from fewer than 2 ACs (only %2): %3")
2195 .arg(sats.size()).arg(acs.join(",")).arg(sats.join(" "));
2196 out << epoTime.datestr().c_str() << " " << epoTime.timestr().c_str() << " " << msg << "\n";
2197 bncTime& warned = _singleACWarned[sys];
2198 if (!warned.valid() || epoTime - warned >= 3600.0) {
2199 warned = epoTime;
2200 emit newMessage("bncComb: " + QByteArray(1, sys) + " " + msg.toLatin1(), false);
2201 }
2202 }
2203 return success;
2204}
2205
2206//
2207////////////////////////////////////////////////////////////////////////////
2208void bncComb::slotProviderIDChanged(QString mountPoint) {
2209 QMutexLocker locker(&_mutex);
2210
2211 QTextStream out(&_log, QIODevice::WriteOnly);
2212
2213 // Find the AC Name
2214 // ----------------
2215 QString acName;
2216 QListIterator<cmbAC*> icAC(_ACs);
2217 while (icAC.hasNext()) {
2218 cmbAC *AC = icAC.next();
2219 if (AC->mountPoint == mountPoint) {
2220 acName = AC->name;
2221 out << "SSR Provider or Service changed: AC " << AC->name.toLatin1().data() << " "
2222 << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
2223 << "\n";
2224 break;
2225 }
2226 }
2227 if (acName.isEmpty()) {
2228 return;
2229 }
2230 QMapIterator<char, unsigned> itSys(_cmbSysPrn);
2231 while (itSys.hasNext()) {
2232 itSys.next();
2233 char sys = itSys.key();
2234 // Remove all corrections of the corresponding AC
2235 // ----------------------------------------------
2236 QVector<cmbCorr*> &corrVec = _buffer[sys].corrs;
2237 QMutableVectorIterator<cmbCorr*> it(corrVec);
2238 while (it.hasNext()) {
2239 cmbCorr *corr = it.next();
2240 if (acName == corr->_acName) {
2241 delete corr;
2242 it.remove();
2243 }
2244 }
2245 // Reset Satellite Offsets
2246 // -----------------------
2247 if (_method == filter) {
2248 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
2249 cmbParam *pp = _params[sys][iPar - 1];
2250 if (pp->AC == acName && pp->type == cmbParam::offACSat) {
2251 pp->xx = 0.0;
2252 _QQ[sys].Row(iPar) = 0.0;
2253 _QQ[sys].Column(iPar) = 0.0;
2254 _QQ[sys](iPar, iPar) = pp->sig0 * pp->sig0;
2255 }
2256 }
2257 }
2258 }
2259}
2260
2261//
2262////////////////////////////////////////////////////////////////////////////
2263bool bncComb::excludeSat(const t_prn& prn, const QStringList excludeSats) const {
2264 QStringListIterator it(excludeSats);
2265 while (it.hasNext()) {
2266 string prnStr = it.next().toLatin1().data();
2267 if (prnStr == prn.toString() || // prn
2268 prnStr == prn.toString().substr(0,1)) { // sys
2269 return true;
2270 }
2271 }
2272 return false;
2273}
Note: See TracBrowser for help on using the repository browser.