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

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