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

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