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

Last change on this file since 8204 was 8204, checked in by wiese, 7 years ago

CHANGE: #105 toAscii deprecated

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