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

Last change on this file since 9546 was 9546, checked in by stuerze, 2 years ago

minor changes

File size: 38.2 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
21#include "bnccomb.h"
22#include "bnccore.h"
23#include "upload/bncrtnetdecoder.h"
24#include "bncsettings.h"
25#include "bncutils.h"
26#include "bncsp3.h"
27#include "bncantex.h"
28#include "t_prn.h"
29
30const double sig0_offAC = 1000.0;
31const double sig0_offACSat = 100.0;
32const double sigP_offACSat = 0.01;
33const double sig0_clkSat = 100.0;
34
35const double sigObs = 0.05;
36
37using namespace std;
38
39// Constructor
40////////////////////////////////////////////////////////////////////////////
41bncComb::cmbParam::cmbParam(parType type_, int index_, const QString& ac_, const QString& prn_) {
42
43 type = type_;
44 index = index_;
45 AC = ac_;
46 prn = prn_;
47 xx = 0.0;
48 eph = 0;
49
50 if (type == offACgnss) {
51 epoSpec = true;
52 sig0 = sig0_offAC;
53 sigP = sig0;
54 }
55 else if (type == offACSat) {
56 epoSpec = false;
57 sig0 = sig0_offACSat;
58 sigP = sigP_offACSat;
59 }
60 else if (type == clkSat) {
61 epoSpec = true;
62 sig0 = sig0_clkSat;
63 sigP = sig0;
64 }
65}
66
67// Destructor
68////////////////////////////////////////////////////////////////////////////
69bncComb::cmbParam::~cmbParam() {
70}
71
72// Partial
73////////////////////////////////////////////////////////////////////////////
74double bncComb::cmbParam::partial(char sys, const QString& AC_, const QString& prn_) {
75
76 if (type == offACgnss) {
77 if (AC == AC_ && prn_[0].toLatin1() == sys) {
78 return 1.0;
79 }
80 }
81 else if (type == offACSat) {
82 if (AC == AC_ && prn == prn_) {
83 return 1.0;
84 }
85 }
86 else if (type == clkSat) {
87 if (prn == prn_) {
88 return 1.0;
89 }
90 }
91
92 return 0.0;
93}
94
95//
96////////////////////////////////////////////////////////////////////////////
97QString bncComb::cmbParam::toString(char sys) const {
98
99 QString outStr;
100
101 if (type == offACgnss) {
102 outStr = "AC Offset " + AC + " " + sys + " ";
103 }
104 else if (type == offACSat) {
105 outStr = "Sat Offset " + AC + " " + prn.mid(0,3);
106 }
107 else if (type == clkSat) {
108 outStr = "Clk Corr " + prn.mid(0,3);
109 }
110
111 return outStr;
112}
113
114// Singleton
115////////////////////////////////////////////////////////////////////////////
116bncComb* bncComb::instance() {
117 static bncComb _bncComb;
118 return &_bncComb;
119}
120
121// Constructor
122////////////////////////////////////////////////////////////////////////////
123bncComb::bncComb() : _ephUser(true) {
124
125 bncSettings settings;
126
127 QStringList cmbStreams = settings.value("cmbStreams").toStringList();
128
129 _cmbSampl = settings.value("cmbSampl").toInt();
130 if (_cmbSampl <= 0) {
131 _cmbSampl = 10;
132 }
133 _useGps = (Qt::CheckState(settings.value("cmbGps").toInt()) == Qt::Checked) ? true : false;
134 if (_useGps) {
135 _cmbSysPrn['G'] = t_prn::MAXPRN_GPS;
136 _masterMissingEpochs['G'] = 0;
137 }
138 _useGlo = (Qt::CheckState(settings.value("cmbGlo").toInt()) == Qt::Checked) ? true : false;
139 if (_useGlo) {
140 _cmbSysPrn['R'] = t_prn::MAXPRN_GLONASS;
141 _masterMissingEpochs['R'] = 0;
142 }
143 _useGal = (Qt::CheckState(settings.value("cmbGal").toInt()) == Qt::Checked) ? true : false;
144 if (_useGal) {
145 _cmbSysPrn['E'] = t_prn::MAXPRN_GALILEO;
146 _masterMissingEpochs['E'] = 0;
147 }
148 _useBds = (Qt::CheckState(settings.value("cmbBds").toInt()) == Qt::Checked) ? true : false;
149 if (_useBds) {
150 _cmbSysPrn['C'] = t_prn::MAXPRN_BDS;
151 _masterMissingEpochs['C'] = 0;
152 }
153 _useQzss = (Qt::CheckState(settings.value("cmbQzss").toInt()) == Qt::Checked) ? true : false;
154 if (_useQzss) {
155 _cmbSysPrn['J'] = t_prn::MAXPRN_QZSS;
156 _masterMissingEpochs['J'] = 0;
157 }
158 _useSbas = (Qt::CheckState(settings.value("cmbSbas").toInt()) == Qt::Checked) ? true : false;
159 if (_useSbas) {
160 _cmbSysPrn['S'] = t_prn::MAXPRN_SBAS;
161 _masterMissingEpochs['S'] = 0;
162 }
163 _useIrnss = (Qt::CheckState(settings.value("cmbIrnss").toInt()) == Qt::Checked) ? true : false;
164 if (_useIrnss) {
165 _cmbSysPrn['I'] = t_prn::MAXPRN_IRNSS;
166 _masterMissingEpochs['I'] = 0;
167 }
168
169 if (cmbStreams.size() >= 1 && !cmbStreams[0].isEmpty()) {
170 QListIterator<QString> it(cmbStreams);
171 while (it.hasNext()) {
172 QStringList hlp = it.next().split(" ");
173 cmbAC* newAC = new cmbAC();
174 newAC->mountPoint = hlp[0];
175 newAC->name = hlp[1];
176 newAC->weight = hlp[2].toDouble();
177 QMapIterator<char, unsigned> itSys(_cmbSysPrn);
178 // init
179 while (itSys.hasNext()) {
180 itSys.next();
181 char sys = itSys.key();
182 if (_masterOrbitAC.isEmpty()) {
183 _masterOrbitAC[sys] = newAC->name;
184 }
185 }
186 _ACs.append(newAC);
187 }
188 }
189
190 QString ssrFormat;
191 _ssrCorr = 0;
192 QListIterator<QString> it(settings.value("uploadMountpointsOut").toStringList());
193 while (it.hasNext()) {
194 QStringList hlp = it.next().split(",");
195 if (hlp.size() > 7) {
196 ssrFormat = hlp[7];
197 }
198 }
199 if (ssrFormat == "IGS-SSR") {
200 _ssrCorr = new SsrCorrIgs();
201 }
202 else if (ssrFormat == "RTCM-SSR") {
203 _ssrCorr = new SsrCorrRtcm();
204 }
205 else { // default
206 _ssrCorr = new SsrCorrIgs();
207 }
208
209 _rtnetDecoder = 0;
210
211 connect(this, SIGNAL(newMessage(QByteArray,bool)),
212 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
213
214 connect(BNC_CORE, SIGNAL(providerIDChanged(QString)),
215 this, SLOT(slotProviderIDChanged(QString)));
216
217 connect(BNC_CORE, SIGNAL(newOrbCorrections(QList<t_orbCorr>)),
218 this, SLOT(slotNewOrbCorrections(QList<t_orbCorr>)));
219
220 connect(BNC_CORE, SIGNAL(newClkCorrections(QList<t_clkCorr>)),
221 this, SLOT(slotNewClkCorrections(QList<t_clkCorr>)));
222
223 connect(BNC_CORE, SIGNAL(newCodeBiases(QList<t_satCodeBias>)),
224 this, SLOT(slotNewCodeBiases(QList<t_satCodeBias>)));
225
226 // Combination Method
227 // ------------------
228 if (settings.value("cmbMethod").toString() == "Single-Epoch") {
229 _method = singleEpoch;
230 }
231 else {
232 _method = filter;
233 }
234
235 // Initialize Parameters (model: Clk_Corr = AC_Offset + Sat_Offset + Clk)
236 // ----------------------------------------------------------------------
237 if (_method == filter) {
238 // SYSTEM
239 QMapIterator<char, unsigned> itSys(_cmbSysPrn);
240 while (itSys.hasNext()) {
241 itSys.next();
242 int nextPar = 0;
243 char sys = itSys.key();
244 unsigned maxPrn = itSys.value();
245 unsigned flag = 0;
246 if (sys == 'E') {
247 flag = 1;
248 }
249 // AC
250 QListIterator<cmbAC*> itAc(_ACs);
251 while (itAc.hasNext()) {
252 cmbAC* AC = itAc.next();
253 _params[sys].push_back(new cmbParam(cmbParam::offACgnss, ++nextPar, AC->name, ""));
254 for (unsigned iGnss = 1; iGnss <= maxPrn; iGnss++) {
255 QString prn = QString("%1%2_%3").arg(sys).arg(iGnss, 2, 10, QChar('0')).arg(flag);
256 _params[sys].push_back(new cmbParam(cmbParam::offACSat, ++nextPar, AC->name, prn));
257 }
258 }
259 for (unsigned iGnss = 1; iGnss <= maxPrn; iGnss++) {
260 QString prn = QString("%1%2_%3").arg(sys).arg(iGnss, 2, 10, QChar('0')).arg(flag);
261 _params[sys].push_back(new cmbParam(cmbParam::clkSat, ++nextPar, "", prn));
262 }
263 // Initialize Variance-Covariance Matrix
264 // -------------------------------------
265 _QQ[sys].ReSize(_params[sys].size());
266 _QQ[sys] = 0.0;
267 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
268 cmbParam* pp = _params[sys][iPar-1];
269 _QQ[sys](iPar,iPar) = pp->sig0 * pp->sig0;
270 }
271 }
272 }
273
274 // ANTEX File
275 // ----------
276 _antex = 0;
277 QString antexFileName = settings.value("uploadAntexFile").toString();
278 if (!antexFileName.isEmpty()) {
279 _antex = new bncAntex();
280 if (_antex->readFile(antexFileName) != success) {
281 emit newMessage("wrong ANTEX file", true);
282 delete _antex;
283 _antex = 0;
284 }
285 }
286
287 // Maximal Residuum
288 // ----------------
289 _MAXRES = settings.value("cmbMaxres").toDouble();
290 if (_MAXRES <= 0.0) {
291 _MAXRES = 999.0;
292 }
293}
294
295// Destructor
296////////////////////////////////////////////////////////////////////////////
297bncComb::~bncComb() {
298 QListIterator<cmbAC*> icAC(_ACs);
299 while (icAC.hasNext()) {
300 delete icAC.next();
301 }
302 delete _rtnetDecoder;
303 if (_ssrCorr) {
304 delete _ssrCorr;
305 }
306 delete _antex;
307 QMapIterator<char, unsigned> itSys(_cmbSysPrn);
308 while (itSys.hasNext()) {
309 itSys.next();
310 char sys = itSys.key();
311 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
312 delete _params[sys][iPar-1];
313 }
314 QListIterator<bncTime> itTime(_buffer[sys].keys());
315 while (itTime.hasNext()) {
316 bncTime epoTime = itTime.next();
317 _buffer[sys].remove(epoTime);
318 }
319 }
320
321}
322
323// Remember orbit corrections
324////////////////////////////////////////////////////////////////////////////
325void bncComb::slotNewOrbCorrections(QList<t_orbCorr> orbCorrections) {
326 QMutexLocker locker(&_mutex);
327
328 for (int ii = 0; ii < orbCorrections.size(); ii++) {
329 t_orbCorr& orbCorr = orbCorrections[ii];
330 QString staID(orbCorr._staID.c_str());
331 char sys = orbCorr._prn.system();
332
333 if (!_cmbSysPrn.contains(sys)){
334 continue;
335 }
336
337 // Find/Check the AC Name
338 // ----------------------
339 QString acName;
340 QListIterator<cmbAC*> icAC(_ACs);
341 while (icAC.hasNext()) {
342 cmbAC* AC = icAC.next();
343 if (AC->mountPoint == staID) {
344 acName = AC->name;
345 break;
346 }
347 }
348 if (acName.isEmpty()) {
349 continue;
350 }
351
352 // Store the correction
353 // --------------------
354 QMap<t_prn, t_orbCorr>& storage = _orbCorrections[acName];
355 storage[orbCorr._prn] = orbCorr;
356 }
357}
358
359// Remember satllite code biases
360////////////////////////////////////////////////////////////////////////////
361void bncComb::slotNewCodeBiases(QList<t_satCodeBias> satCodeBiases) {
362 QMutexLocker locker(&_mutex);
363
364 for (int ii = 0; ii < satCodeBiases.size(); ii++) {
365 t_satCodeBias& satCodeBias = satCodeBiases[ii];
366 QString staID(satCodeBias._staID.c_str());
367 char sys = satCodeBias._prn.system();
368
369 if (!_cmbSysPrn.contains(sys)){
370 continue;
371 }
372
373 // Find/Check the AC Name
374 // ----------------------
375 QString acName;
376 QListIterator<cmbAC*> icAC(_ACs);
377 while (icAC.hasNext()) {
378 cmbAC* AC = icAC.next();
379 if (AC->mountPoint == staID) {
380 acName = AC->name;
381 break;
382 }
383 }
384 if (acName.isEmpty()) {
385 continue;
386 }
387
388 // Store the correction
389 // --------------------
390 QMap<t_prn, t_satCodeBias>& storage = _satCodeBiases[acName];
391 storage[satCodeBias._prn] = satCodeBias;
392 }
393}
394
395// Process clock corrections
396////////////////////////////////////////////////////////////////////////////
397void bncComb::slotNewClkCorrections(QList<t_clkCorr> clkCorrections) {
398 QMutexLocker locker(&_mutex);
399
400 bncTime lastTime;
401
402 for (int ii = 0; ii < clkCorrections.size(); ii++) {
403 t_clkCorr& clkCorr = clkCorrections[ii];
404 QString staID(clkCorr._staID.c_str());
405 QString prn(clkCorr._prn.toInternalString().c_str());
406 char sys = clkCorr._prn.system();
407
408 if (!_cmbSysPrn.contains(sys)){
409 continue;
410 }
411
412 // Set the last time
413 // -----------------
414 if (lastTime.undef() || clkCorr._time > lastTime) {
415 lastTime = clkCorr._time;
416 }
417
418 // Find/Check the AC Name
419 // ----------------------
420 QString acName;
421 QListIterator<cmbAC*> icAC(_ACs);
422 while (icAC.hasNext()) {
423 cmbAC* AC = icAC.next();
424 if (AC->mountPoint == staID) {
425 acName = AC->name;
426 break;
427 }
428 }
429 if (acName.isEmpty()) {
430 continue;
431 }
432
433 // Check Modulo Time
434 // -----------------
435 int sec = int(nint(clkCorr._time.gpssec()*10));
436 if (sec % (_cmbSampl*10) != 0.0) {
437 continue;
438 }
439
440 // Check Correction Age
441 // --------------------
442 if (_resTime.valid() && clkCorr._time <= _resTime) {
443 emit newMessage("bncComb: old correction: " + acName.toLatin1() + " " + prn.mid(0,3).toLatin1(), true);
444 continue;
445 }
446
447 // Create new correction
448 // ---------------------
449 cmbCorr* newCorr = new cmbCorr();
450 newCorr->_prn = prn;
451 newCorr->_time = clkCorr._time;
452 newCorr->_iod = clkCorr._iod;
453 newCorr->_acName = acName;
454 newCorr->_clkCorr = clkCorr;
455
456 // Check orbit correction
457 // ----------------------
458 if (!_orbCorrections.contains(acName)) {
459 delete newCorr;
460 continue;
461 }
462 else {
463 QMap<t_prn, t_orbCorr>& storage = _orbCorrections[acName];
464 if (!storage.contains(clkCorr._prn) ||
465 storage[clkCorr._prn]._iod != newCorr->_iod) {
466 delete newCorr;
467 continue;
468 }
469 else {
470 newCorr->_orbCorr = storage[clkCorr._prn];
471 }
472 }
473
474 // Check satellite code biases
475 // ----------------------------
476 if (!_satCodeBiases.contains(acName)) {
477 delete newCorr;
478 continue;
479 }
480 else {
481 QMap<t_prn, t_satCodeBias>& storage = _satCodeBiases[acName];
482 if (!storage.contains(clkCorr._prn)) {
483 delete newCorr;
484 continue;
485 }
486 else {
487 newCorr->_satCodeBias = storage[clkCorr._prn];
488 }
489 }
490
491 // Check the Ephemeris
492 //--------------------
493 t_eph* ephLast = _ephUser.ephLast(prn);
494 t_eph* ephPrev = _ephUser.ephPrev(prn);
495 if (ephLast == 0) {
496 emit newMessage("bncComb: eph not found for " + prn.mid(0,3).toLatin1(), true);
497 delete newCorr;
498 continue;
499 }
500 else if (ephLast->checkState() == t_eph::bad ||
501 ephLast->checkState() == t_eph::outdated ||
502 ephLast->checkState() == t_eph::unhealthy) {
503 emit newMessage("bncComb: ephLast not ok for " + prn.mid(0,3).toLatin1(), true);
504 delete newCorr;
505 continue;
506 }
507 else {
508 if (ephLast->IOD() == newCorr->_iod) {
509 newCorr->_eph = ephLast;
510 }
511 else if (ephPrev && ephPrev->checkState() == t_eph::ok &&
512 ephPrev->IOD() == newCorr->_iod) {
513 newCorr->_eph = ephPrev;
514 switchToLastEph(ephLast, newCorr);
515 }
516 else {
517 emit newMessage("bncComb: eph not found for " + prn.mid(0,3).toLatin1() +
518 QString(" with IOD %1").arg(newCorr->_iod).toLatin1(), true);
519 delete newCorr;
520 continue;
521 }
522 }
523
524 // Store correction into the buffer
525 // --------------------------------
526 QVector<cmbCorr*>& corrs = _buffer[sys][newCorr->_time].corrs;
527 corrs.push_back(newCorr);
528 }
529
530 // Process previous Epoch(s)
531 // -------------------------
532 const double outWait = 1.0 * _cmbSampl;
533 QMapIterator<char, unsigned> itSys(_cmbSysPrn);
534 while (itSys.hasNext()) {
535 itSys.next();
536 char sys = itSys.key();
537 QListIterator<bncTime> itTime(_buffer[sys].keys());
538 while (itTime.hasNext()) {
539 bncTime epoTime = itTime.next();
540 if (epoTime < lastTime - outWait) {
541 _resTime = epoTime;
542 processEpoch(sys);
543 }
544 }
545 }
546}
547
548// Change the correction so that it refers to last received ephemeris
549////////////////////////////////////////////////////////////////////////////
550void bncComb::switchToLastEph(t_eph* lastEph, cmbCorr* corr) {
551
552 if (corr->_eph == lastEph) {
553 return;
554 }
555
556 ColumnVector oldXC(6);
557 ColumnVector oldVV(3);
558 if (corr->_eph->getCrd(corr->_time, oldXC, oldVV, false) != success) {
559 return;
560 }
561
562 ColumnVector newXC(6);
563 ColumnVector newVV(3);
564 if (lastEph->getCrd(corr->_time, newXC, newVV, false) != success) {
565 return;
566 }
567
568 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
569 ColumnVector dV = newVV - oldVV;
570 double dC = newXC(4) - oldXC(4);
571
572 ColumnVector dRAO(3);
573 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
574
575 ColumnVector dDotRAO(3);
576 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
577
578 QString msg = "switch corr " + corr->_prn.mid(0,3)
579 + QString(" %1 -> %2 %3").arg(corr->_iod,3).arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
580
581 emit newMessage(msg.toLatin1(), false);
582
583 corr->_iod = lastEph->IOD();
584 corr->_eph = lastEph;
585
586 corr->_orbCorr._xr += dRAO;
587 corr->_orbCorr._dotXr += dDotRAO;
588 corr->_clkCorr._dClk -= dC;
589}
590
591// Process Epoch
592////////////////////////////////////////////////////////////////////////////
593void bncComb::processEpoch(char sys) {
594
595 _log.clear();
596
597 QTextStream out(&_log, QIODevice::WriteOnly);
598
599 out << endl << "Combination: " << sys << endl
600 << "--------------------------------" << endl;
601
602 // Observation Statistics
603 // ----------------------
604 bool masterPresent = false;
605 QListIterator<cmbAC*> icAC(_ACs);
606 while (icAC.hasNext()) {
607 cmbAC* AC = icAC.next();
608 AC->numObs[sys] = 0;
609 QVectorIterator<cmbCorr*> itCorr(corrs(sys));
610 while (itCorr.hasNext()) {
611 cmbCorr* corr = itCorr.next();
612 if (corr->_acName == AC->name) {
613 AC->numObs[sys] += 1;
614 if (AC->name == _masterOrbitAC[sys]) {
615 masterPresent = true;
616 }
617 }
618 }
619 out << AC->name.toLatin1().data() << ": " << AC->numObs[sys] << endl;
620 }
621
622 // If Master not present, switch to another one
623 // --------------------------------------------
624 const unsigned switchMasterAfterGap = 1;
625 if (masterPresent) {
626 _masterMissingEpochs[sys] = 0;
627 }
628 else {
629 ++_masterMissingEpochs[sys];
630 if (_masterMissingEpochs[sys] < switchMasterAfterGap) {
631 out << "Missing Master, Epoch skipped" << endl;
632 _buffer[sys].remove(_resTime);
633 emit newMessage(_log, false);
634 return;
635 }
636 else {
637 _masterMissingEpochs[sys] = 0;
638 QListIterator<cmbAC*> icAC(_ACs);
639 while (icAC.hasNext()) {
640 cmbAC* AC = icAC.next();
641 if (AC->numObs[sys] > 0) {
642 out << "Switching Master AC "
643 << _masterOrbitAC[sys].toLatin1().data() << " --> "
644 << AC->name.toLatin1().data() << " "
645 << _resTime.datestr().c_str() << " "
646 << _resTime.timestr().c_str() << endl;
647 _masterOrbitAC[sys] = AC->name;
648 break;
649 }
650 }
651 }
652 }
653
654 QMap<QString, cmbCorr*> resCorr;
655
656 // Perform the actual Combination using selected Method
657 // ----------------------------------------------------
658 t_irc irc;
659 ColumnVector dx;
660 if (_method == filter) {
661 irc = processEpoch_filter(sys, out, resCorr, dx);
662 }
663 else {
664 irc = processEpoch_singleEpoch(sys, out, resCorr, dx);
665 }
666
667 // Update Parameter Values, Print Results
668 // --------------------------------------
669 if (irc == success) {
670 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
671 cmbParam* pp = _params[sys][iPar-1];
672 pp->xx += dx(iPar);
673 if (pp->type == cmbParam::clkSat) {
674 if (resCorr.find(pp->prn) != resCorr.end()) {
675 resCorr[pp->prn]->_dClkResult = pp->xx / t_CST::c;
676 }
677 }
678 out << _resTime.datestr().c_str() << " "
679 << _resTime.timestr().c_str() << " ";
680 out.setRealNumberNotation(QTextStream::FixedNotation);
681 out.setFieldWidth(8);
682 out.setRealNumberPrecision(4);
683 out << pp->toString(sys) << " "
684 << pp->xx << " +- " << sqrt(_QQ[sys](pp->index,pp->index)) << endl;
685 out.setFieldWidth(0);
686 }
687 printResults(out, resCorr);
688 dumpResults(resCorr);
689 }
690
691 // Delete Data, emit Message
692 // -------------------------
693 _buffer[sys].remove(_resTime);
694 emit newMessage(_log, false);
695}
696
697// Process Epoch - Filter Method
698////////////////////////////////////////////////////////////////////////////
699t_irc bncComb::processEpoch_filter(char sys, QTextStream& out,
700 QMap<QString, cmbCorr*>& resCorr,
701 ColumnVector& dx) {
702
703 // Prediction Step
704 // ---------------
705 int nPar = _params[sys].size();
706 ColumnVector x0(nPar);
707 for (int iPar = 1; iPar <= nPar; iPar++) {
708 cmbParam* pp = _params[sys][iPar-1];
709 if (pp->epoSpec) {
710 pp->xx = 0.0;
711 _QQ[sys].Row(iPar) = 0.0;
712 _QQ[sys].Column(iPar) = 0.0;
713 _QQ[sys](iPar,iPar) = pp->sig0 * pp->sig0;
714 }
715 else {
716 _QQ[sys](iPar,iPar) += pp->sigP * pp->sigP;
717 }
718 x0(iPar) = pp->xx;
719 }
720
721 // Check Satellite Positions for Outliers
722 // --------------------------------------
723 if (checkOrbits(sys, out) != success) {
724 return failure;
725 }
726
727 // Update and outlier detection loop
728 // ---------------------------------
729 SymmetricMatrix QQ_sav = _QQ[sys];
730 while (true) {
731
732 Matrix AA;
733 ColumnVector ll;
734 DiagonalMatrix PP;
735
736 if (createAmat(sys, AA, ll, PP, x0, resCorr) != success) {
737 return failure;
738 }
739
740 dx.ReSize(nPar); dx = 0.0;
741 kalman(AA, ll, PP, _QQ[sys], dx);
742
743 ColumnVector vv = ll - AA * dx;
744
745 int maxResIndex;
746 double maxRes = vv.maximum_absolute_value1(maxResIndex);
747 out.setRealNumberNotation(QTextStream::FixedNotation);
748 out.setRealNumberPrecision(3);
749 out << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
750 << " Maximum Residuum " << maxRes << ' '
751 << corrs(sys)[maxResIndex-1]->_acName << ' ' << corrs(sys)[maxResIndex-1]->_prn.mid(0,3);
752 if (maxRes > _MAXRES) {
753 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
754 cmbParam* pp = _params[sys][iPar-1];
755 if (pp->type == cmbParam::offACSat &&
756 pp->AC == corrs(sys)[maxResIndex-1]->_acName &&
757 pp->prn == corrs(sys)[maxResIndex-1]->_prn.mid(0,3)) {
758 QQ_sav.Row(iPar) = 0.0;
759 QQ_sav.Column(iPar) = 0.0;
760 QQ_sav(iPar,iPar) = pp->sig0 * pp->sig0;
761 }
762 }
763
764 out << " Outlier" << endl;
765 _QQ[sys] = QQ_sav;
766 corrs(sys).remove(maxResIndex-1);
767 }
768 else {
769 out << " OK" << endl;
770 out.setRealNumberNotation(QTextStream::FixedNotation);
771 out.setRealNumberPrecision(4);
772 for (int ii = 0; ii < corrs(sys).size(); ii++) {
773 const cmbCorr* corr = corrs(sys)[ii];
774 out << _resTime.datestr().c_str() << ' '
775 << _resTime.timestr().c_str() << " "
776 << corr->_acName << ' ' << corr->_prn.mid(0,3);
777 out.setFieldWidth(10);
778 out << " res = " << vv[ii] << endl;
779 out.setFieldWidth(0);
780 }
781 break;
782 }
783 }
784
785 return success;
786}
787
788// Print results
789////////////////////////////////////////////////////////////////////////////
790void bncComb::printResults(QTextStream& out,
791 const QMap<QString, cmbCorr*>& resCorr) {
792
793 QMapIterator<QString, cmbCorr*> it(resCorr);
794 while (it.hasNext()) {
795 it.next();
796 cmbCorr* corr = it.value();
797 const t_eph* eph = corr->_eph;
798 if (eph) {
799 ColumnVector xc(6);
800 ColumnVector vv(3);
801 if (eph->getCrd(_resTime, xc, vv, false) != success) {
802 continue;
803 }
804 out << _resTime.datestr().c_str() << " "
805 << _resTime.timestr().c_str() << " ";
806 out.setFieldWidth(3);
807 out << "Full Clock " << corr->_prn.mid(0,3) << " " << corr->_iod << " ";
808 out.setFieldWidth(14);
809 out << (xc(4) + corr->_dClkResult) * t_CST::c << endl;
810 out.setFieldWidth(0);
811 }
812 else {
813 out << "bncComb::printResuls bug" << endl;
814 }
815 }
816}
817
818// Send results to RTNet Decoder and directly to PPP Client
819////////////////////////////////////////////////////////////////////////////
820void bncComb::dumpResults(const QMap<QString, cmbCorr*>& resCorr) {
821
822 QList<t_orbCorr> orbCorrections;
823 QList<t_clkCorr> clkCorrections;
824
825 QString outLines;
826 unsigned year, month, day, hour, minute;
827 double sec;
828 _resTime.civil_date(year, month, day);
829 _resTime.civil_time(hour, minute, sec);
830
831 outLines.sprintf("* %4d %2d %2d %d %d %12.8f\n",
832 year, month, day, hour, minute, sec);
833
834 QMapIterator<QString, cmbCorr*> it(resCorr);
835 while (it.hasNext()) {
836 it.next();
837 cmbCorr* corr = it.value();
838
839 t_orbCorr orbCorr(corr->_orbCorr);
840 orbCorr._staID = "INTERNAL";
841 orbCorrections.push_back(orbCorr);
842
843 t_clkCorr clkCorr(corr->_clkCorr);
844 clkCorr._staID = "INTERNAL";
845 clkCorr._dClk = corr->_dClkResult;
846 clkCorr._dotDClk = 0.0;
847 clkCorr._dotDotDClk = 0.0;
848 clkCorrections.push_back(clkCorr);
849
850 ColumnVector xc(6);
851 ColumnVector vv(3);
852 corr->_eph->setClkCorr(dynamic_cast<const t_clkCorr*>(&clkCorr));
853 corr->_eph->setOrbCorr(dynamic_cast<const t_orbCorr*>(&orbCorr));
854 if (corr->_eph->getCrd(_resTime, xc, vv, true) != success) {
855 delete corr;
856 continue;
857 }
858
859 // Correction Phase Center --> CoM
860 // -------------------------------
861 ColumnVector dx(3); dx = 0.0;
862 if (_antex) {
863 double Mjd = _resTime.mjd() + _resTime.daysec()/86400.0;
864 if (_antex->satCoMcorrection(corr->_prn, Mjd, xc.Rows(1,3), dx) != success) {
865 dx = 0;
866 _log += "antenna not found " + corr->_prn.mid(0,3).toLatin1() + '\n';
867 }
868 }
869
870 outLines += corr->_prn.mid(0,3);
871 QString hlp;
872 hlp.sprintf(" APC 3 %15.4f %15.4f %15.4f"
873 " Clk 1 %15.4f"
874 " Vel 3 %15.4f %15.4f %15.4f"
875 " CoM 3 %15.4f %15.4f %15.4f\n",
876 xc(1), xc(2), xc(3),
877 xc(4) * t_CST::c,
878 vv(1), vv(2), vv(3),
879 xc(1)-dx(1), xc(2)-dx(2), xc(3)-dx(3));
880 outLines += hlp;
881
882 QString line;
883 int messageType = _ssrCorr->COTYPE_GPSCOMBINED;
884 int updateInt = 0;
885 line.sprintf("%d %d %d %.1f %s"
886 " %lu"
887 " %8.3f %8.3f %8.3f %8.3f"
888 " %10.5f %10.5f %10.5f %10.5f"
889 " %10.5f INTERNAL",
890 messageType, updateInt, _resTime.gpsw(), _resTime.gpssec(),
891 corr->_prn.mid(0,3).toLatin1().data(),
892 corr->_iod,
893 corr->_dClkResult * t_CST::c,
894 corr->_orbCorr._xr[0],
895 corr->_orbCorr._xr[1],
896 corr->_orbCorr._xr[2],
897 0.0,
898 corr->_orbCorr._dotXr[0],
899 corr->_orbCorr._dotXr[1],
900 corr->_orbCorr._dotXr[2],
901 0.0);
902
903 delete corr;
904 }
905
906 outLines += "EOE\n"; // End Of Epoch flag
907
908 if (!_rtnetDecoder) {
909 _rtnetDecoder = new bncRtnetDecoder();
910 }
911
912 vector<string> errmsg;
913 _rtnetDecoder->Decode(outLines.toLatin1().data(), outLines.length(), errmsg);
914
915 // Send new Corrections to PPP etc.
916 // --------------------------------
917 if (orbCorrections.size() > 0 && clkCorrections.size() > 0) {
918 emit newOrbCorrections(orbCorrections);
919 emit newClkCorrections(clkCorrections);
920 }
921}
922
923// Create First Design Matrix and Vector of Measurements
924////////////////////////////////////////////////////////////////////////////
925t_irc bncComb::createAmat(char sys, Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP,
926 const ColumnVector& x0, QMap<QString, cmbCorr*>& resCorr) {
927
928 unsigned nPar = _params[sys].size();
929 unsigned nObs = corrs(sys).size();
930
931 if (nObs == 0) {
932 return failure;
933 }
934
935 int maxSat = _cmbSysPrn[sys];
936
937 const int nCon = (_method == filter) ? 1 + maxSat : 0;
938
939 AA.ReSize(nObs+nCon, nPar); AA = 0.0;
940 ll.ReSize(nObs+nCon); ll = 0.0;
941 PP.ReSize(nObs+nCon); PP = 1.0 / (sigObs * sigObs);
942
943 int iObs = 0;
944 QVectorIterator<cmbCorr*> itCorr(corrs(sys));
945 while (itCorr.hasNext()) {
946 cmbCorr* corr = itCorr.next();
947 QString prn = corr->_prn;
948
949 ++iObs;
950
951 if (corr->_acName == _masterOrbitAC[sys] && resCorr.find(prn) == resCorr.end()) {
952 resCorr[prn] = new cmbCorr(*corr);
953 }
954
955 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
956 cmbParam* pp = _params[sys][iPar-1];
957 AA(iObs, iPar) = pp->partial(sys, corr->_acName, prn);
958 }
959
960 ll(iObs) = corr->_clkCorr._dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
961 }
962
963 // Regularization
964 // --------------
965 if (_method == filter) {
966 const double Ph = 1.e6;
967 PP(nObs+1) = Ph;
968 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
969 cmbParam* pp = _params[sys][iPar-1];
970 if ( AA.Column(iPar).maximum_absolute_value() > 0.0 &&
971 pp->type == cmbParam::clkSat ) {
972 AA(nObs+1, iPar) = 1.0;
973 }
974 }
975 unsigned flag = 0;
976 if (sys == 'E') {
977 flag = 1;
978 }
979 if (sys == 'R') {
980 return success;
981 }
982 int iCond = 1;
983 // GNSS
984 for (unsigned iGnss = 1; iGnss <= _cmbSysPrn[sys]; iGnss++) {
985 QString prn = QString("%1%2_%3").arg(sys).arg(iGnss, 2, 10, QChar('0')).arg(flag);
986 ++iCond;
987 PP(nObs+iCond) = Ph;
988 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
989 cmbParam* pp = _params[sys][iPar-1];
990 if ( pp &&
991 AA.Column(iPar).maximum_absolute_value() > 0.0 &&
992 pp->type == cmbParam::offACSat &&
993 pp->prn == prn) {
994 AA(nObs+iCond, iPar) = 1.0;
995 }
996 }
997 }
998 }
999
1000 return success;
1001}
1002
1003// Process Epoch - Single-Epoch Method
1004////////////////////////////////////////////////////////////////////////////
1005t_irc bncComb::processEpoch_singleEpoch(char sys, QTextStream& out,
1006 QMap<QString, cmbCorr*>& resCorr,
1007 ColumnVector& dx) {
1008
1009 // Check Satellite Positions for Outliers
1010 // --------------------------------------
1011 if (checkOrbits(sys, out) != success) {
1012 return failure;
1013 }
1014
1015 // Outlier Detection Loop
1016 // ----------------------
1017 while (true) {
1018
1019 // Remove Satellites that are not in Master
1020 // ----------------------------------------
1021 QMutableVectorIterator<cmbCorr*> it(corrs(sys));
1022 while (it.hasNext()) {
1023 cmbCorr* corr = it.next();
1024 QString prn = corr->_prn;
1025 bool foundMaster = false;
1026 QVectorIterator<cmbCorr*> itHlp(corrs(sys));
1027 while (itHlp.hasNext()) {
1028 cmbCorr* corrHlp = itHlp.next();
1029 QString prnHlp = corrHlp->_prn;
1030 QString ACHlp = corrHlp->_acName;
1031 if (ACHlp == _masterOrbitAC[sys] && prn == prnHlp) {
1032 foundMaster = true;
1033 break;
1034 }
1035 }
1036 if (!foundMaster) {
1037 delete corr;
1038 it.remove();
1039 }
1040 }
1041
1042 // Count Number of Observations per Satellite and per AC
1043 // -----------------------------------------------------
1044 QMap<QString, int> numObsPrn;
1045 QMap<QString, int> numObsAC;
1046 QVectorIterator<cmbCorr*> itCorr(corrs(sys));
1047 while (itCorr.hasNext()) {
1048 cmbCorr* corr = itCorr.next();
1049 QString prn = corr->_prn;
1050 QString AC = corr->_acName;
1051 if (numObsPrn.find(prn) == numObsPrn.end()) {
1052 numObsPrn[prn] = 1;
1053 }
1054 else {
1055 numObsPrn[prn] += 1;
1056 }
1057 if (numObsAC.find(AC) == numObsAC.end()) {
1058 numObsAC[AC] = 1;
1059 }
1060 else {
1061 numObsAC[AC] += 1;
1062 }
1063 }
1064
1065 // Clean-Up the Parameters
1066 // -----------------------
1067 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
1068 delete _params[sys][iPar-1];
1069 }
1070 _params[sys].clear();
1071
1072 // Set new Parameters
1073 // ------------------
1074 int nextPar = 0;
1075
1076 QMapIterator<QString, int> itAC(numObsAC);
1077 while (itAC.hasNext()) {
1078 itAC.next();
1079 const QString& AC = itAC.key();
1080 int numObs = itAC.value();
1081 if (AC != _masterOrbitAC[sys] && numObs > 0) {
1082 _params[sys].push_back(new cmbParam(cmbParam::offACgnss, ++nextPar, AC, ""));
1083 }
1084 }
1085
1086 QMapIterator<QString, int> itPrn(numObsPrn);
1087 while (itPrn.hasNext()) {
1088 itPrn.next();
1089 const QString& prn = itPrn.key();
1090 int numObs = itPrn.value();
1091 if (numObs > 0) {
1092 _params[sys].push_back(new cmbParam(cmbParam::clkSat, ++nextPar, "", prn));
1093 }
1094 }
1095
1096 int nPar = _params[sys].size();
1097 ColumnVector x0(nPar);
1098 x0 = 0.0;
1099
1100 // Create First-Design Matrix
1101 // --------------------------
1102 Matrix AA;
1103 ColumnVector ll;
1104 DiagonalMatrix PP;
1105 if (createAmat(sys, AA, ll, PP, x0, resCorr) != success) {
1106 return failure;
1107 }
1108
1109 ColumnVector vv;
1110 try {
1111 Matrix ATP = AA.t() * PP;
1112 SymmetricMatrix NN; NN << ATP * AA;
1113 ColumnVector bb = ATP * ll;
1114 _QQ[sys] = NN.i();
1115 dx = _QQ[sys] * bb;
1116 vv = ll - AA * dx;
1117 }
1118 catch (Exception& exc) {
1119 out << exc.what() << endl;
1120 return failure;
1121 }
1122
1123 int maxResIndex;
1124 double maxRes = vv.maximum_absolute_value1(maxResIndex);
1125 out.setRealNumberNotation(QTextStream::FixedNotation);
1126 out.setRealNumberPrecision(3);
1127 out << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
1128 << " Maximum Residuum " << maxRes << ' '
1129 << corrs(sys)[maxResIndex-1]->_acName << ' ' << corrs(sys)[maxResIndex-1]->_prn.mid(0,3);
1130
1131 if (maxRes > _MAXRES) {
1132 out << " Outlier" << endl;
1133 delete corrs(sys)[maxResIndex-1];
1134 corrs(sys).remove(maxResIndex-1);
1135 }
1136 else {
1137 out << " OK" << endl;
1138 out.setRealNumberNotation(QTextStream::FixedNotation);
1139 out.setRealNumberPrecision(3);
1140 for (int ii = 0; ii < vv.Nrows(); ii++) {
1141 const cmbCorr* corr = corrs(sys)[ii];
1142 out << _resTime.datestr().c_str() << ' '
1143 << _resTime.timestr().c_str() << " "
1144 << corr->_acName << ' ' << corr->_prn.mid(0,3);
1145 out.setFieldWidth(6);
1146 out << " res = " << vv[ii] << endl;
1147 out.setFieldWidth(0);
1148 }
1149 return success;
1150 }
1151
1152 }
1153
1154 return failure;
1155}
1156
1157// Check Satellite Positions for Outliers
1158////////////////////////////////////////////////////////////////////////////
1159t_irc bncComb::checkOrbits(char sys, QTextStream& out) {
1160
1161 const double MAX_DISPLACEMENT = 0.20;
1162
1163 // Switch to last ephemeris (if possible)
1164 // --------------------------------------
1165 QMutableVectorIterator<cmbCorr*> im(corrs(sys));
1166 while (im.hasNext()) {
1167 cmbCorr* corr = im.next();
1168 QString prn = corr->_prn;
1169
1170 t_eph* ephLast = _ephUser.ephLast(prn);
1171 t_eph* ephPrev = _ephUser.ephPrev(prn);
1172
1173 if (ephLast == 0) {
1174 out << "checkOrbit: missing eph (not found) " << corr->_prn.mid(0,3) << endl;
1175 delete corr;
1176 im.remove();
1177 }
1178 else if (corr->_eph == 0) {
1179 out << "checkOrbit: missing eph (zero) " << corr->_prn.mid(0,3) << endl;
1180 delete corr;
1181 im.remove();
1182 }
1183 else {
1184 if ( corr->_eph == ephLast || corr->_eph == ephPrev ) {
1185 switchToLastEph(ephLast, corr);
1186 }
1187 else {
1188 out << "checkOrbit: missing eph (deleted) " << corr->_prn.mid(0,3) << endl;
1189 delete corr;
1190 im.remove();
1191 }
1192 }
1193 }
1194
1195 while (true) {
1196
1197 // Compute Mean Corrections for all Satellites
1198 // -------------------------------------------
1199 QMap<QString, int> numCorr;
1200 QMap<QString, ColumnVector> meanRao;
1201 QVectorIterator<cmbCorr*> it(corrs(sys));
1202 while (it.hasNext()) {
1203 cmbCorr* corr = it.next();
1204 QString prn = corr->_prn;
1205 if (meanRao.find(prn) == meanRao.end()) {
1206 meanRao[prn].ReSize(4);
1207 meanRao[prn].Rows(1,3) = corr->_orbCorr._xr;
1208 meanRao[prn](4) = 1;
1209 }
1210 else {
1211 meanRao[prn].Rows(1,3) += corr->_orbCorr._xr;
1212 meanRao[prn](4) += 1;
1213 }
1214 if (numCorr.find(prn) == numCorr.end()) {
1215 numCorr[prn] = 1;
1216 }
1217 else {
1218 numCorr[prn] += 1;
1219 }
1220 }
1221
1222 // Compute Differences wrt. Mean, find Maximum
1223 // -------------------------------------------
1224 QMap<QString, cmbCorr*> maxDiff;
1225 it.toFront();
1226 while (it.hasNext()) {
1227 cmbCorr* corr = it.next();
1228 QString prn = corr->_prn;
1229 if (meanRao[prn](4) != 0) {
1230 meanRao[prn] /= meanRao[prn](4);
1231 meanRao[prn](4) = 0;
1232 }
1233 corr->_diffRao = corr->_orbCorr._xr - meanRao[prn].Rows(1,3);
1234 if (maxDiff.find(prn) == maxDiff.end()) {
1235 maxDiff[prn] = corr;
1236 }
1237 else {
1238 double normMax = maxDiff[prn]->_diffRao.NormFrobenius();
1239 double norm = corr->_diffRao.NormFrobenius();
1240 if (norm > normMax) {
1241 maxDiff[prn] = corr;
1242 }
1243 }
1244 }
1245
1246 if (_ACs.size() == 1) {
1247 break;
1248 }
1249
1250 // Remove Outliers
1251 // ---------------
1252 bool removed = false;
1253 QMutableVectorIterator<cmbCorr*> im(corrs(sys));
1254 while (im.hasNext()) {
1255 cmbCorr* corr = im.next();
1256 QString prn = corr->_prn;
1257 if (numCorr[prn] < 2) {
1258 delete corr;
1259 im.remove();
1260 }
1261 else if (corr == maxDiff[prn]) {
1262 double norm = corr->_diffRao.NormFrobenius();
1263 if (norm > MAX_DISPLACEMENT) {
1264 out << _resTime.datestr().c_str() << " "
1265 << _resTime.timestr().c_str() << " "
1266 << "Orbit Outlier: "
1267 << corr->_acName.toLatin1().data() << " "
1268 << prn.mid(0,3).toLatin1().data() << " "
1269 << corr->_iod << " "
1270 << norm << endl;
1271 delete corr;
1272 im.remove();
1273 removed = true;
1274 }
1275 }
1276 }
1277
1278 if (!removed) {
1279 break;
1280 }
1281 }
1282
1283 return success;
1284}
1285
1286//
1287////////////////////////////////////////////////////////////////////////////
1288void bncComb::slotProviderIDChanged(QString mountPoint) {
1289 QMutexLocker locker(&_mutex);
1290
1291 QTextStream out(&_log, QIODevice::WriteOnly);
1292
1293 // Find the AC Name
1294 // ----------------
1295 QString acName;
1296 QListIterator<cmbAC*> icAC(_ACs);
1297 while (icAC.hasNext()) {
1298 cmbAC* AC = icAC.next();
1299 if (AC->mountPoint == mountPoint) {
1300 acName = AC->name;
1301 out << "Provider ID changed: AC " << AC->name.toLatin1().data() << " "
1302 << _resTime.datestr().c_str() << " "
1303 << _resTime.timestr().c_str() << endl;
1304 break;
1305 }
1306 }
1307 if (acName.isEmpty()) {
1308 return;
1309 }
1310 QMapIterator<char, unsigned> itSys(_cmbSysPrn);
1311 while (itSys.hasNext()) {
1312 itSys.next();
1313 char sys = itSys.key();
1314 // Remove all corrections of the corresponding AC
1315 // ----------------------------------------------
1316 QListIterator<bncTime> itTime(_buffer[sys].keys());
1317 while (itTime.hasNext()) {
1318 bncTime epoTime = itTime.next();
1319 QVector<cmbCorr*>& corrVec = _buffer[sys][epoTime].corrs;
1320 QMutableVectorIterator<cmbCorr*> it(corrVec);
1321 while (it.hasNext()) {
1322 cmbCorr* corr = it.next();
1323 if (acName == corr->_acName) {
1324 delete corr;
1325 it.remove();
1326 }
1327 }
1328 }
1329
1330 // Reset Satellite Offsets
1331 // -----------------------
1332 if (_method == filter) {
1333 for (int iPar = 1; iPar <= _params[sys].size(); iPar++) {
1334 cmbParam* pp = _params[sys][iPar-1];
1335 if (pp->AC == acName && pp->type == cmbParam::offACSat) {
1336 pp->xx = 0.0;
1337 _QQ[sys].Row(iPar) = 0.0;
1338 _QQ[sys].Column(iPar) = 0.0;
1339 _QQ[sys](iPar,iPar) = pp->sig0 * pp->sig0;
1340 }
1341 }
1342 }
1343 }
1344}
Note: See TracBrowser for help on using the repository browser.