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

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

parameter added to be able to chose the corrections to be combined

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