source: ntrip/branches/BNC_2.12/src/combination/bnccomb.cpp@ 9080

Last change on this file since 9080 was 9080, checked in by stuerze, 4 years ago

bug fixed

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