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

Last change on this file since 9264 was 9264, checked in by stuerze, 20 months ago

minor changes

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