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

Last change on this file since 5867 was 5867, checked in by mervart, 10 years ago
File size: 32.5 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->position(corr->tRao.gpsw(), corr->tRao.gpssec(),
415 oldXC.data(), oldVV.data());
416
417 ColumnVector newXC(4);
418 ColumnVector newVV(3);
419 lastEph->position(corr->tRao.gpsw(), corr->tRao.gpssec(),
420 newXC.data(), newVV.data());
421
422 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
423 ColumnVector dV = newVV - oldVV;
424 double dC = newXC(4) - oldXC(4);
425
426 ColumnVector dRAO(3);
427 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
428
429 ColumnVector dDotRAO(3);
430 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
431
432 QString msg = "switch corr " + corr->prn
433 + QString(" %1 -> %2 %3").arg(corr->iod,3)
434 .arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
435
436 emit newMessage(msg.toAscii(), false);
437
438 corr->iod = lastEph->IOD();
439 corr->eph = lastEph;
440 corr->rao += dRAO;
441 corr->dotRao += dDotRAO;
442 corr->dClk -= dC;
443}
444
445// Process Epoch
446////////////////////////////////////////////////////////////////////////////
447void bncComb::processEpoch() {
448
449 _log.clear();
450
451 QTextStream out(&_log, QIODevice::WriteOnly);
452
453 out << endl << "Combination:" << endl
454 << "------------------------------" << endl;
455
456 // Observation Statistics
457 // ----------------------
458 bool masterPresent = false;
459 QListIterator<cmbAC*> icAC(_ACs);
460 while (icAC.hasNext()) {
461 cmbAC* AC = icAC.next();
462 AC->numObs = 0;
463 QVectorIterator<cmbCorr*> itCorr(corrs());
464 while (itCorr.hasNext()) {
465 cmbCorr* corr = itCorr.next();
466 if (corr->acName == AC->name) {
467 AC->numObs += 1;
468 if (AC->name == _masterOrbitAC) {
469 masterPresent = true;
470 }
471 }
472 }
473 out << AC->name.toAscii().data() << ": " << AC->numObs << endl;
474 }
475
476 // If Master not present, switch to another one
477 // --------------------------------------------
478 const unsigned switchMasterAfterGap = 1;
479 if (masterPresent) {
480 _masterMissingEpochs = 0;
481 }
482 else {
483 ++_masterMissingEpochs;
484 if (_masterMissingEpochs < switchMasterAfterGap) {
485 out << "Missing Master, Epoch skipped" << endl;
486 _buffer.remove(_resTime);
487 emit newMessage(_log, false);
488 return;
489 }
490 else {
491 _masterMissingEpochs = 0;
492 QListIterator<cmbAC*> icAC(_ACs);
493 while (icAC.hasNext()) {
494 cmbAC* AC = icAC.next();
495 if (AC->numObs > 0) {
496 out << "Switching Master AC "
497 << _masterOrbitAC.toAscii().data() << " --> "
498 << AC->name.toAscii().data() << " "
499 << _resTime.datestr().c_str() << " "
500 << _resTime.timestr().c_str() << endl;
501 _masterOrbitAC = AC->name;
502 break;
503 }
504 }
505 }
506 }
507
508 QMap<QString, t_corr*> resCorr;
509
510 // Perform the actual Combination using selected Method
511 // ----------------------------------------------------
512 t_irc irc;
513 ColumnVector dx;
514 if (_method == filter) {
515 irc = processEpoch_filter(out, resCorr, dx);
516 }
517 else {
518 irc = processEpoch_singleEpoch(out, resCorr, dx);
519 }
520
521 // Update Parameter Values, Print Results
522 // --------------------------------------
523 if (irc == success) {
524 for (int iPar = 1; iPar <= _params.size(); iPar++) {
525 cmbParam* pp = _params[iPar-1];
526 pp->xx += dx(iPar);
527 if (pp->type == cmbParam::clkSat) {
528 if (resCorr.find(pp->prn) != resCorr.end()) {
529 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
530 }
531 }
532 out << _resTime.datestr().c_str() << " "
533 << _resTime.timestr().c_str() << " ";
534 out.setRealNumberNotation(QTextStream::FixedNotation);
535 out.setFieldWidth(8);
536 out.setRealNumberPrecision(4);
537 out << pp->toString() << " "
538 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
539 out.setFieldWidth(0);
540 }
541 printResults(out, resCorr);
542 dumpResults(resCorr);
543 }
544
545 // Delete Data, emit Message
546 // -------------------------
547 _buffer.remove(_resTime);
548 emit newMessage(_log, false);
549}
550
551// Process Epoch - Filter Method
552////////////////////////////////////////////////////////////////////////////
553t_irc bncComb::processEpoch_filter(QTextStream& out,
554 QMap<QString, t_corr*>& resCorr,
555 ColumnVector& dx) {
556
557 // Prediction Step
558 // ---------------
559 int nPar = _params.size();
560 ColumnVector x0(nPar);
561 for (int iPar = 1; iPar <= _params.size(); iPar++) {
562 cmbParam* pp = _params[iPar-1];
563 if (pp->epoSpec) {
564 pp->xx = 0.0;
565 _QQ.Row(iPar) = 0.0;
566 _QQ.Column(iPar) = 0.0;
567 _QQ(iPar,iPar) = pp->sig0 * pp->sig0;
568 }
569 else {
570 _QQ(iPar,iPar) += pp->sigP * pp->sigP;
571 }
572 x0(iPar) = pp->xx;
573 }
574
575 // Check Satellite Positions for Outliers
576 // --------------------------------------
577 if (checkOrbits(out) != success) {
578 return failure;
579 }
580
581 // Update and outlier detection loop
582 // ---------------------------------
583 SymmetricMatrix QQ_sav = _QQ;
584 while (true) {
585
586 Matrix AA;
587 ColumnVector ll;
588 DiagonalMatrix PP;
589
590 if (createAmat(AA, ll, PP, x0, resCorr) != success) {
591 return failure;
592 }
593
594 dx = 0.0;
595 kalman(AA, ll, PP, _QQ, dx);
596 ColumnVector vv = ll - AA * dx;
597
598 int maxResIndex;
599 double maxRes = vv.maximum_absolute_value1(maxResIndex);
600 out.setRealNumberNotation(QTextStream::FixedNotation);
601 out.setRealNumberPrecision(3);
602 out << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
603 << " Maximum Residuum " << maxRes << ' '
604 << corrs()[maxResIndex-1]->acName << ' ' << corrs()[maxResIndex-1]->prn;
605
606 if (maxRes > _MAXRES) {
607 for (int iPar = 1; iPar <= _params.size(); iPar++) {
608 cmbParam* pp = _params[iPar-1];
609 if (pp->type == cmbParam::offACSat &&
610 pp->AC == corrs()[maxResIndex-1]->acName &&
611 pp->prn == corrs()[maxResIndex-1]->prn) {
612 QQ_sav.Row(iPar) = 0.0;
613 QQ_sav.Column(iPar) = 0.0;
614 QQ_sav(iPar,iPar) = pp->sig0 * pp->sig0;
615 }
616 }
617
618 out << " Outlier" << endl;
619 _QQ = QQ_sav;
620 corrs().remove(maxResIndex-1);
621 }
622 else {
623 out << " OK" << endl;
624 break;
625 }
626 }
627
628 return success;
629}
630
631// Print results
632////////////////////////////////////////////////////////////////////////////
633void bncComb::printResults(QTextStream& out,
634 const QMap<QString, t_corr*>& resCorr) {
635
636 QMapIterator<QString, t_corr*> it(resCorr);
637 while (it.hasNext()) {
638 it.next();
639 t_corr* corr = it.value();
640 const t_eph* eph = corr->eph;
641 if (eph) {
642 double xx, yy, zz, cc;
643 eph->position(_resTime.gpsw(), _resTime.gpssec(), xx, yy, zz, cc);
644
645 out << _resTime.datestr().c_str() << " "
646 << _resTime.timestr().c_str() << " ";
647 out.setFieldWidth(3);
648 out << "Full Clock " << corr->prn << " " << corr->iod << " ";
649 out.setFieldWidth(14);
650 out << (cc + corr->dClk) * t_CST::c << endl;
651 out.setFieldWidth(0);
652 }
653 else {
654 out << "bncComb::printResuls bug" << endl;
655 }
656 }
657}
658
659// Send results to RTNet Decoder and directly to PPP Client
660////////////////////////////////////////////////////////////////////////////
661void bncComb::dumpResults(const QMap<QString, t_corr*>& resCorr) {
662
663 QString outLines;
664 QStringList corrLines;
665
666 unsigned year, month, day, hour, minute;
667 double sec;
668 _resTime.civil_date(year, month, day);
669 _resTime.civil_time(hour, minute, sec);
670
671 outLines.sprintf("* %4d %2d %2d %d %d %12.8f\n",
672 year, month, day, hour, minute, sec);
673
674 QMapIterator<QString, t_corr*> it(resCorr);
675 while (it.hasNext()) {
676 it.next();
677 t_corr* corr = it.value();
678
679 ColumnVector xc(4);
680 ColumnVector vv(3);
681 corr->eph->position(_resTime.gpsw(), _resTime.gpssec(),
682 xc.data(), vv.data());
683
684 // Correction Phase Center --> CoM
685 // -------------------------------
686 ColumnVector dx(3); dx = 0.0;
687 if (_antex) {
688 double Mjd = _resTime.mjd() + _resTime.daysec()/86400.0;
689 if (_antex->satCoMcorrection(corr->prn, Mjd, xc.Rows(1,3), dx) != success) {
690 dx = 0;
691 cout << "antenna not found " << corr->prn.toAscii().data() << endl;
692 }
693 }
694
695 outLines += corr->prn;
696 QString hlp;
697 hlp.sprintf(" APC 3 %15.4f %15.4f %15.4f"
698 " Clk 1 %15.4f"
699 " Vel 3 %15.4f %15.4f %15.4f"
700 " CoM 3 %15.4f %15.4f %15.4f\n",
701 xc(1), xc(2), xc(3),
702 xc(4)*t_CST::c,
703 vv(1), vv(2), vv(3),
704 xc(1)-dx(1), xc(2)-dx(2), xc(3)-dx(3));
705 outLines += hlp;
706
707 QString line;
708 int messageType = COTYPE_GPSCOMBINED;
709 int updateInt = 0;
710 line.sprintf("%d %d %d %.1f %s"
711 " %3d"
712 " %8.3f %8.3f %8.3f %8.3f"
713 " %10.5f %10.5f %10.5f %10.5f"
714 " %10.5f INTERNAL",
715 messageType, updateInt, _resTime.gpsw(), _resTime.gpssec(),
716 corr->prn.toAscii().data(),
717 corr->iod,
718 corr->dClk * t_CST::c,
719 corr->rao[0],
720 corr->rao[1],
721 corr->rao[2],
722 corr->dotDClk * t_CST::c,
723 corr->dotRao[0],
724 corr->dotRao[1],
725 corr->dotRao[2],
726 corr->dotDotDClk * t_CST::c);
727 corrLines << line;
728
729 delete corr;
730 }
731
732 outLines += "EOE\n"; // End Of Epoch flag
733
734 if (!_rtnetDecoder) {
735 _rtnetDecoder = new bncRtnetDecoder();
736 }
737
738 vector<string> errmsg;
739 _rtnetDecoder->Decode(outLines.toAscii().data(), outLines.length(), errmsg);
740
741 // Send new Corrections to PPP etc.
742 // --------------------------------
743 emit newCorrections(corrLines);
744}
745
746// Create First Design Matrix and Vector of Measurements
747////////////////////////////////////////////////////////////////////////////
748t_irc bncComb::createAmat(Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP,
749 const ColumnVector& x0,
750 QMap<QString, t_corr*>& resCorr) {
751
752 unsigned nPar = _params.size();
753 unsigned nObs = corrs().size();
754
755 if (nObs == 0) {
756 return failure;
757 }
758
759 int maxSat = t_prn::MAXPRN_GPS;
760// if (_useGlonass) {
761// maxSat = t_prn::MAXPRN_GPS + t_prn::MAXPRN_GLONASS;
762// }
763
764 const int nCon = (_method == filter) ? 1 + maxSat : 0;
765
766 AA.ReSize(nObs+nCon, nPar); AA = 0.0;
767 ll.ReSize(nObs+nCon); ll = 0.0;
768 PP.ReSize(nObs+nCon); PP = 1.0 / (sigObs * sigObs);
769
770 int iObs = 0;
771
772 QVectorIterator<cmbCorr*> itCorr(corrs());
773 while (itCorr.hasNext()) {
774 cmbCorr* corr = itCorr.next();
775 QString prn = corr->prn;
776
777 ++iObs;
778
779 if (corr->acName == _masterOrbitAC && resCorr.find(prn) == resCorr.end()) {
780 resCorr[prn] = new t_corr(*corr);
781 }
782
783 for (int iPar = 1; iPar <= _params.size(); iPar++) {
784 cmbParam* pp = _params[iPar-1];
785 AA(iObs, iPar) = pp->partial(corr->acName, prn);
786 }
787
788 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
789 }
790
791 // Regularization
792 // --------------
793 if (_method == filter) {
794 const double Ph = 1.e6;
795 PP(nObs+1) = Ph;
796 for (int iPar = 1; iPar <= _params.size(); iPar++) {
797 cmbParam* pp = _params[iPar-1];
798 if ( AA.Column(iPar).maximum_absolute_value() > 0.0 &&
799 pp->type == cmbParam::clkSat ) {
800 AA(nObs+1, iPar) = 1.0;
801 }
802 }
803 int iCond = 1;
804 for (unsigned iGps = 1; iGps <= t_prn::MAXPRN_GPS; iGps++) {
805 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
806 ++iCond;
807 PP(nObs+iCond) = Ph;
808 for (int iPar = 1; iPar <= _params.size(); iPar++) {
809 cmbParam* pp = _params[iPar-1];
810 if ( AA.Column(iPar).maximum_absolute_value() > 0.0 &&
811 pp->type == cmbParam::offACSat &&
812 pp->prn == prn) {
813 AA(nObs+iCond, iPar) = 1.0;
814 }
815 }
816 }
817// if (_useGlonass) {
818// for (int iGlo = 1; iGlo <= t_prn::MAXPRN_GLONASS; iGlo++) {
819// QString prn = QString("R%1").arg(iGlo, 2, 10, QChar('0'));
820// ++iCond;
821// PP(nObs+iCond) = Ph;
822// for (int iPar = 1; iPar <= _params.size(); iPar++) {
823// cmbParam* pp = _params[iPar-1];
824// if ( AA.Column(iPar).maximum_absolute_value() > 0.0 &&
825// pp->type == cmbParam::offACSat &&
826// pp->prn == prn) {
827// AA(nObs+iCond, iPar) = 1.0;
828// }
829// }
830// }
831// }
832 }
833
834 return success;
835}
836
837// Process Epoch - Single-Epoch Method
838////////////////////////////////////////////////////////////////////////////
839t_irc bncComb::processEpoch_singleEpoch(QTextStream& out,
840 QMap<QString, t_corr*>& resCorr,
841 ColumnVector& dx) {
842
843 // Check Satellite Positions for Outliers
844 // --------------------------------------
845 if (checkOrbits(out) != success) {
846 return failure;
847 }
848
849 // Outlier Detection Loop
850 // ----------------------
851 while (true) {
852
853 // Remove Satellites that are not in Master
854 // ----------------------------------------
855 QMutableVectorIterator<cmbCorr*> it(corrs());
856 while (it.hasNext()) {
857 cmbCorr* corr = it.next();
858 QString prn = corr->prn;
859 bool foundMaster = false;
860 QVectorIterator<cmbCorr*> itHlp(corrs());
861 while (itHlp.hasNext()) {
862 cmbCorr* corrHlp = itHlp.next();
863 QString prnHlp = corrHlp->prn;
864 QString ACHlp = corrHlp->acName;
865 if (ACHlp == _masterOrbitAC && prn == prnHlp) {
866 foundMaster = true;
867 break;
868 }
869 }
870 if (!foundMaster) {
871 delete corr;
872 it.remove();
873 }
874 }
875
876 // Count Number of Observations per Satellite and per AC
877 // -----------------------------------------------------
878 QMap<QString, int> numObsPrn;
879 QMap<QString, int> numObsAC;
880 QVectorIterator<cmbCorr*> itCorr(corrs());
881 while (itCorr.hasNext()) {
882 cmbCorr* corr = itCorr.next();
883 QString prn = corr->prn;
884 QString AC = corr->acName;
885 if (numObsPrn.find(prn) == numObsPrn.end()) {
886 numObsPrn[prn] = 1;
887 }
888 else {
889 numObsPrn[prn] += 1;
890 }
891 if (numObsAC.find(AC) == numObsAC.end()) {
892 numObsAC[AC] = 1;
893 }
894 else {
895 numObsAC[AC] += 1;
896 }
897 }
898
899 // Clean-Up the Paramters
900 // ----------------------
901 for (int iPar = 1; iPar <= _params.size(); iPar++) {
902 delete _params[iPar-1];
903 }
904 _params.clear();
905
906 // Set new Parameters
907 // ------------------
908 int nextPar = 0;
909
910 QMapIterator<QString, int> itAC(numObsAC);
911 while (itAC.hasNext()) {
912 itAC.next();
913 const QString& AC = itAC.key();
914 int numObs = itAC.value();
915 if (AC != _masterOrbitAC && numObs > 0) {
916 _params.push_back(new cmbParam(cmbParam::offACgps, ++nextPar, AC, ""));
917 if (_useGlonass) {
918 _params.push_back(new cmbParam(cmbParam::offACglo, ++nextPar, AC, ""));
919 }
920 }
921 }
922
923 QMapIterator<QString, int> itPrn(numObsPrn);
924 while (itPrn.hasNext()) {
925 itPrn.next();
926 const QString& prn = itPrn.key();
927 int numObs = itPrn.value();
928 if (numObs > 0) {
929 _params.push_back(new cmbParam(cmbParam::clkSat, ++nextPar, "", prn));
930 }
931 }
932
933 int nPar = _params.size();
934 ColumnVector x0(nPar);
935 x0 = 0.0;
936
937 // Create First-Design Matrix
938 // --------------------------
939 Matrix AA;
940 ColumnVector ll;
941 DiagonalMatrix PP;
942 if (createAmat(AA, ll, PP, x0, resCorr) != success) {
943 return failure;
944 }
945
946 ColumnVector vv;
947 try {
948 Matrix ATP = AA.t() * PP;
949 SymmetricMatrix NN; NN << ATP * AA;
950 ColumnVector bb = ATP * ll;
951 _QQ = NN.i();
952 dx = _QQ * bb;
953 vv = ll - AA * dx;
954 }
955 catch (Exception& exc) {
956 out << exc.what() << endl;
957 return failure;
958 }
959
960 int maxResIndex;
961 double maxRes = vv.maximum_absolute_value1(maxResIndex);
962 out.setRealNumberNotation(QTextStream::FixedNotation);
963 out.setRealNumberPrecision(3);
964 out << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
965 << " Maximum Residuum " << maxRes << ' '
966 << corrs()[maxResIndex-1]->acName << ' ' << corrs()[maxResIndex-1]->prn;
967
968 if (maxRes > _MAXRES) {
969 out << " Outlier" << endl;
970 delete corrs()[maxResIndex-1];
971 corrs().remove(maxResIndex-1);
972 }
973 else {
974 out << " OK" << endl;
975 out.setRealNumberNotation(QTextStream::FixedNotation);
976 out.setRealNumberPrecision(3);
977 for (int ii = 0; ii < vv.Nrows(); ii++) {
978 const cmbCorr* corr = corrs()[ii];
979 out << _resTime.datestr().c_str() << ' '
980 << _resTime.timestr().c_str() << " "
981 << corr->acName << ' ' << corr->prn;
982 out.setFieldWidth(6);
983 out << " dClk = " << corr->dClk * t_CST::c << " res = " << vv[ii] << endl;
984 out.setFieldWidth(0);
985 }
986 return success;
987 }
988
989 }
990
991 return failure;
992}
993
994// Check Satellite Positions for Outliers
995////////////////////////////////////////////////////////////////////////////
996t_irc bncComb::checkOrbits(QTextStream& out) {
997
998 const double MAX_DISPLACEMENT = 0.20;
999
1000 // Switch to last ephemeris (if possible)
1001 // --------------------------------------
1002 QMutableVectorIterator<cmbCorr*> im(corrs());
1003 while (im.hasNext()) {
1004 cmbCorr* corr = im.next();
1005 QString prn = corr->prn;
1006 if (_eph.find(prn) == _eph.end()) {
1007 out << "checkOrbit: missing eph (not found) " << corr->prn << endl;
1008 delete corr;
1009 im.remove();
1010 }
1011 else if (corr->eph == 0) {
1012 out << "checkOrbit: missing eph (zero) " << corr->prn << endl;
1013 delete corr;
1014 im.remove();
1015 }
1016 else {
1017 if ( corr->eph == _eph[prn]->last || corr->eph == _eph[prn]->prev ) {
1018 switchToLastEph(_eph[prn]->last, corr);
1019 }
1020 else {
1021 out << "checkOrbit: missing eph (deleted) " << corr->prn << endl;
1022 delete corr;
1023 im.remove();
1024 }
1025 }
1026 }
1027
1028 while (true) {
1029
1030 // Compute Mean Corrections for all Satellites
1031 // -------------------------------------------
1032 QMap<QString, int> numCorr;
1033 QMap<QString, ColumnVector> meanRao;
1034 QVectorIterator<cmbCorr*> it(corrs());
1035 while (it.hasNext()) {
1036 cmbCorr* corr = it.next();
1037 QString prn = corr->prn;
1038 if (meanRao.find(prn) == meanRao.end()) {
1039 meanRao[prn].ReSize(4);
1040 meanRao[prn].Rows(1,3) = corr->rao;
1041 meanRao[prn](4) = 1;
1042 }
1043 else {
1044 meanRao[prn].Rows(1,3) += corr->rao;
1045 meanRao[prn](4) += 1;
1046 }
1047 if (numCorr.find(prn) == numCorr.end()) {
1048 numCorr[prn] = 1;
1049 }
1050 else {
1051 numCorr[prn] += 1;
1052 }
1053 }
1054
1055 // Compute Differences wrt Mean, find Maximum
1056 // ------------------------------------------
1057 QMap<QString, cmbCorr*> maxDiff;
1058 it.toFront();
1059 while (it.hasNext()) {
1060 cmbCorr* corr = it.next();
1061 QString prn = corr->prn;
1062 if (meanRao[prn](4) != 0) {
1063 meanRao[prn] /= meanRao[prn](4);
1064 meanRao[prn](4) = 0;
1065 }
1066 corr->diffRao = corr->rao - meanRao[prn].Rows(1,3);
1067 if (maxDiff.find(prn) == maxDiff.end()) {
1068 maxDiff[prn] = corr;
1069 }
1070 else {
1071 double normMax = maxDiff[prn]->diffRao.norm_Frobenius();
1072 double norm = corr->diffRao.norm_Frobenius();
1073 if (norm > normMax) {
1074 maxDiff[prn] = corr;
1075 }
1076 }
1077 }
1078
1079 if (_ACs.size() == 1) {
1080 break;
1081 }
1082
1083 // Remove Outliers
1084 // ---------------
1085 bool removed = false;
1086 QMutableVectorIterator<cmbCorr*> im(corrs());
1087 while (im.hasNext()) {
1088 cmbCorr* corr = im.next();
1089 QString prn = corr->prn;
1090 if (numCorr[prn] < 2) {
1091 delete corr;
1092 im.remove();
1093 }
1094 else if (corr == maxDiff[prn]) {
1095 double norm = corr->diffRao.norm_Frobenius();
1096 if (norm > MAX_DISPLACEMENT) {
1097 out << _resTime.datestr().c_str() << " "
1098 << _resTime.timestr().c_str() << " "
1099 << "Orbit Outlier: "
1100 << corr->acName.toAscii().data() << " "
1101 << prn.toAscii().data() << " "
1102 << corr->iod << " "
1103 << norm << endl;
1104 delete corr;
1105 im.remove();
1106 removed = true;
1107 }
1108 }
1109 }
1110
1111 if (!removed) {
1112 break;
1113 }
1114 }
1115
1116 return success;
1117}
1118
1119//
1120////////////////////////////////////////////////////////////////////////////
1121t_irc bncComb::mergeOrbitCorr(const cmbCorr* orbitCorr, cmbCorr* clkCorr) {
1122
1123 clkCorr->iod = orbitCorr->iod; // is it always correct?
1124 clkCorr->eph = orbitCorr->eph;
1125 clkCorr->tRao = orbitCorr->tRao;
1126 clkCorr->rao = orbitCorr->rao;
1127 clkCorr->dotRao = orbitCorr->dotRao;
1128
1129 return success;
1130}
1131
1132//
1133////////////////////////////////////////////////////////////////////////////
1134void bncComb::slotProviderIDChanged(QString mountPoint) {
1135 QMutexLocker locker(&_mutex);
1136
1137 // Find the AC Name
1138 // ----------------
1139 QString acName;
1140 QListIterator<cmbAC*> icAC(_ACs);
1141 while (icAC.hasNext()) {
1142 cmbAC* AC = icAC.next();
1143 if (AC->mountPoint == mountPoint) {
1144 acName = AC->name;
1145 break;
1146 }
1147 }
1148 if (acName.isEmpty()) {
1149 return;
1150 }
1151
1152 // Remove all corrections of the corresponding AC
1153 // ----------------------------------------------
1154 QListIterator<bncTime> itTime(_buffer.keys());
1155 while (itTime.hasNext()) {
1156 bncTime epoTime = itTime.next();
1157 QVector<cmbCorr*>& corrVec = _buffer[epoTime].corrs;
1158 QMutableVectorIterator<cmbCorr*> it(corrVec);
1159 while (it.hasNext()) {
1160 cmbCorr* corr = it.next();
1161 if (acName == corr->acName) {
1162 delete corr;
1163 it.remove();
1164 }
1165 }
1166 }
1167
1168 // Reset Satellite Offsets
1169 // -----------------------
1170 if (_method == filter) {
1171 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1172 cmbParam* pp = _params[iPar-1];
1173 if (pp->AC == acName && pp->type == cmbParam::offACSat) {
1174 pp->xx = 0.0;
1175 _QQ.Row(iPar) = 0.0;
1176 _QQ.Column(iPar) = 0.0;
1177 _QQ(iPar,iPar) = pp->sig0 * pp->sig0;
1178 }
1179 }
1180 }
1181}
Note: See TracBrowser for help on using the repository browser.