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

Last change on this file since 5337 was 5337, checked in by mervart, 8 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: " + staID.toAscii() + " " + line.toAscii(), true);
342 delete newCorr;
343 return;
344 }
345
346 // Check the Ephemeris
347 //--------------------
348 if (_eph.find(newCorr->prn) == _eph.end()) {
349 emit newMessage("bncComb: eph not found " + newCorr->prn.toAscii(), true);
350 delete newCorr;
351 return;
352 }
353 else {
354 t_eph* lastEph = _eph[newCorr->prn]->last;
355 t_eph* prevEph = _eph[newCorr->prn]->prev;
356 if (lastEph && lastEph->IOD() == newCorr->iod) {
357 newCorr->eph = lastEph;
358 }
359 else if (lastEph && prevEph && prevEph->IOD() == newCorr->iod) {
360 newCorr->eph = prevEph;
361 switchToLastEph(lastEph, newCorr);
362 }
363 else {
364 emit newMessage("bncComb: eph not found " + newCorr->prn.toAscii() +
365 QString(" %1").arg(newCorr->iod).toAscii(), true);
366 delete newCorr;
367 return;
368 }
369 }
370
371 // Process previous Epoch(s)
372 // -------------------------
373 const double waitTime = 1.0 * _cmbSampl;
374 QListIterator<bncTime> itTime(_buffer.keys());
375 while (itTime.hasNext()) {
376 bncTime epoTime = itTime.next();
377 if (epoTime < newCorr->tClk - waitTime) {
378 _resTime = epoTime;
379 processEpoch();
380 }
381 }
382
383 // Merge or add the correction
384 // ---------------------------
385 QVector<cmbCorr*>& corrs = _buffer[newCorr->tClk].corrs;
386 cmbCorr* existingCorr = 0;
387 QVectorIterator<cmbCorr*> itCorr(corrs);
388 while (itCorr.hasNext()) {
389 cmbCorr* hlp = itCorr.next();
390 if (hlp->prn == newCorr->prn && hlp->acName == newCorr->acName) {
391 existingCorr = hlp;
392 break;
393 }
394 }
395 if (existingCorr) {
396 delete newCorr; newCorr = 0;
397 existingCorr->readLine(line); // merge (multiple messages)
398
399 }
400 else {
401 corrs.append(newCorr);
402 }
403}
404
405// Change the correction so that it refers to last received ephemeris
406////////////////////////////////////////////////////////////////////////////
407void bncComb::switchToLastEph(const t_eph* lastEph, t_corr* corr) {
408
409 if (corr->eph == lastEph) {
410 return;
411 }
412
413 ColumnVector oldXC(4);
414 ColumnVector oldVV(3);
415 corr->eph->position(corr->tRao.gpsw(), corr->tRao.gpssec(),
416 oldXC.data(), oldVV.data());
417
418 ColumnVector newXC(4);
419 ColumnVector newVV(3);
420 lastEph->position(corr->tRao.gpsw(), corr->tRao.gpssec(),
421 newXC.data(), newVV.data());
422
423 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
424 ColumnVector dV = newVV - oldVV;
425 double dC = newXC(4) - oldXC(4);
426
427 ColumnVector dRAO(3);
428 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
429
430 ColumnVector dDotRAO(3);
431 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
432
433 QString msg = "switch corr " + corr->prn
434 + QString(" %1 -> %2 %3").arg(corr->iod,3)
435 .arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
436
437 emit newMessage(msg.toAscii(), false);
438
439 corr->iod = lastEph->IOD();
440 corr->eph = lastEph;
441 corr->rao += dRAO;
442 corr->dotRao += dDotRAO;
443 corr->dClk -= dC;
444}
445
446// Process Epoch
447////////////////////////////////////////////////////////////////////////////
448void bncComb::processEpoch() {
449
450 _log.clear();
451
452 QTextStream out(&_log, QIODevice::WriteOnly);
453
454 out << endl << "Combination:" << endl
455 << "------------------------------" << endl;
456
457 // Observation Statistics
458 // ----------------------
459 bool masterPresent = false;
460 QListIterator<cmbAC*> icAC(_ACs);
461 while (icAC.hasNext()) {
462 cmbAC* AC = icAC.next();
463 AC->numObs = 0;
464 QVectorIterator<cmbCorr*> itCorr(corrs());
465 while (itCorr.hasNext()) {
466 cmbCorr* corr = itCorr.next();
467 if (corr->acName == AC->name) {
468 AC->numObs += 1;
469 if (AC->name == _masterOrbitAC) {
470 masterPresent = true;
471 }
472 }
473 }
474 out << AC->name.toAscii().data() << ": " << AC->numObs << endl;
475 }
476
477 // If Master not present, switch to another one
478 // --------------------------------------------
479 const unsigned switchMasterAfterGap = 1;
480 if (masterPresent) {
481 _masterMissingEpochs = 0;
482 }
483 else {
484 ++_masterMissingEpochs;
485 if (_masterMissingEpochs < switchMasterAfterGap) {
486 out << "Missing Master, Epoch skipped" << endl;
487 _buffer.remove(_resTime);
488 emit newMessage(_log, false);
489 return;
490 }
491 else {
492 _masterMissingEpochs = 0;
493 QListIterator<cmbAC*> icAC(_ACs);
494 while (icAC.hasNext()) {
495 cmbAC* AC = icAC.next();
496 if (AC->numObs > 0) {
497 out << "Switching Master AC "
498 << _masterOrbitAC.toAscii().data() << " --> "
499 << AC->name.toAscii().data() << " "
500 << _resTime.datestr().c_str() << " "
501 << _resTime.timestr().c_str() << endl;
502 _masterOrbitAC = AC->name;
503 break;
504 }
505 }
506 }
507 }
508
509 QMap<QString, t_corr*> resCorr;
510
511 // Perform the actual Combination using selected Method
512 // ----------------------------------------------------
513 t_irc irc;
514 ColumnVector dx;
515 if (_method == filter) {
516 irc = processEpoch_filter(out, resCorr, dx);
517 }
518 else {
519 irc = processEpoch_singleEpoch(out, resCorr, dx);
520 }
521
522 // Update Parameter Values, Print Results
523 // --------------------------------------
524 if (irc == success) {
525 for (int iPar = 1; iPar <= _params.size(); iPar++) {
526 cmbParam* pp = _params[iPar-1];
527 pp->xx += dx(iPar);
528 if (pp->type == cmbParam::clkSat) {
529 if (resCorr.find(pp->prn) != resCorr.end()) {
530 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
531 }
532 }
533 out << _resTime.datestr().c_str() << " "
534 << _resTime.timestr().c_str() << " ";
535 out.setRealNumberNotation(QTextStream::FixedNotation);
536 out.setFieldWidth(8);
537 out.setRealNumberPrecision(4);
538 out << pp->toString() << " "
539 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
540 out.setFieldWidth(0);
541 }
542 printResults(out, resCorr);
543 dumpResults(resCorr);
544 }
545
546 // Delete Data, emit Message
547 // -------------------------
548 _buffer.remove(_resTime);
549 emit newMessage(_log, false);
550}
551
552// Process Epoch - Filter Method
553////////////////////////////////////////////////////////////////////////////
554t_irc bncComb::processEpoch_filter(QTextStream& out,
555 QMap<QString, t_corr*>& resCorr,
556 ColumnVector& dx) {
557
558 // Prediction Step
559 // ---------------
560 int nPar = _params.size();
561 ColumnVector x0(nPar);
562 for (int iPar = 1; iPar <= _params.size(); iPar++) {
563 cmbParam* pp = _params[iPar-1];
564 if (pp->epoSpec) {
565 pp->xx = 0.0;
566 _QQ.Row(iPar) = 0.0;
567 _QQ.Column(iPar) = 0.0;
568 _QQ(iPar,iPar) = pp->sig0 * pp->sig0;
569 }
570 else {
571 _QQ(iPar,iPar) += pp->sigP * pp->sigP;
572 }
573 x0(iPar) = pp->xx;
574 }
575
576 // Check Satellite Positions for Outliers
577 // --------------------------------------
578 if (checkOrbits(out) != success) {
579 return failure;
580 }
581
582 // Update and outlier detection loop
583 // ---------------------------------
584 SymmetricMatrix QQ_sav = _QQ;
585 while (true) {
586
587 Matrix AA;
588 ColumnVector ll;
589 DiagonalMatrix PP;
590
591 if (createAmat(AA, ll, PP, x0, resCorr) != success) {
592 return failure;
593 }
594
595 bncModel::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 bncPPPclient::applyCorr(_resTime, corr, xc, vv);
684
685 // Correction Phase Center --> CoM
686 // -------------------------------
687 ColumnVector dx(3); dx = 0.0;
688 if (_antex) {
689 double Mjd = _resTime.mjd() + _resTime.daysec()/86400.0;
690 if (_antex->satCoMcorrection(corr->prn, Mjd, xc.Rows(1,3), dx) != success) {
691 dx = 0;
692 cout << "antenna not found " << corr->prn.toAscii().data() << endl;
693 }
694 }
695
696 outLines += corr->prn;
697 QString hlp;
698 hlp.sprintf(" APC 3 %15.4f %15.4f %15.4f"
699 " Clk 1 %15.4f"
700 " Vel 3 %15.4f %15.4f %15.4f"
701 " CoM 3 %15.4f %15.4f %15.4f\n",
702 xc(1), xc(2), xc(3),
703 xc(4)*t_CST::c,
704 vv(1), vv(2), vv(3),
705 xc(1)-dx(1), xc(2)-dx(2), xc(3)-dx(3));
706 outLines += hlp;
707
708 qDebug() << corr->prn << xc(1) << xc(2) << xc(3) << xc(4)*t_CST::c
709 << vv(1) << vv(2) << vv(3) << dx(1) << dx(2) << dx(3);
710
711 QString line;
712 int messageType = COTYPE_GPSCOMBINED;
713 int updateInt = 0;
714 line.sprintf("%d %d %d %.1f %s"
715 " %3d"
716 " %8.3f %8.3f %8.3f %8.3f"
717 " %10.5f %10.5f %10.5f %10.5f"
718 " %10.5f %10.5f %10.5f %10.5f INTERNAL",
719 messageType, updateInt, _resTime.gpsw(), _resTime.gpssec(),
720 corr->prn.toAscii().data(),
721 corr->iod,
722 corr->dClk * t_CST::c,
723 corr->rao[0],
724 corr->rao[1],
725 corr->rao[2],
726 corr->dotDClk * t_CST::c,
727 corr->dotRao[0],
728 corr->dotRao[1],
729 corr->dotRao[2],
730 corr->dotDotDClk * t_CST::c,
731 corr->dotDotRao[0],
732 corr->dotDotRao[1],
733 corr->dotDotRao[2]);
734 corrLines << line;
735
736 delete corr;
737 }
738
739 outLines += "EOE\n"; // End Of Epoch flag
740
741 if (!_rtnetDecoder) {
742 _rtnetDecoder = new bncRtnetDecoder();
743 }
744
745 vector<string> errmsg;
746 _rtnetDecoder->Decode(outLines.toAscii().data(), outLines.length(), errmsg);
747
748 // Optionally send new Corrections to PPP
749 // --------------------------------------
750 if (BNC_CORE->_bncPPPclient) {
751 BNC_CORE->_bncPPPclient->slotNewCorrections(corrLines);
752 }
753}
754
755// Create First Design Matrix and Vector of Measurements
756////////////////////////////////////////////////////////////////////////////
757t_irc bncComb::createAmat(Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP,
758 const ColumnVector& x0,
759 QMap<QString, t_corr*>& resCorr) {
760
761 unsigned nPar = _params.size();
762 unsigned nObs = corrs().size();
763
764 if (nObs == 0) {
765 return failure;
766 }
767
768 int MAXPRN = MAXPRN_GPS;
769// if (_useGlonass) {
770// MAXPRN = MAXPRN_GPS + MAXPRN_GLONASS;
771// }
772
773 const int nCon = (_method == filter) ? 1 + MAXPRN : 0;
774
775 AA.ReSize(nObs+nCon, nPar); AA = 0.0;
776 ll.ReSize(nObs+nCon); ll = 0.0;
777 PP.ReSize(nObs+nCon); PP = 1.0 / (sigObs * sigObs);
778
779 int iObs = 0;
780
781 QVectorIterator<cmbCorr*> itCorr(corrs());
782 while (itCorr.hasNext()) {
783 cmbCorr* corr = itCorr.next();
784 QString prn = corr->prn;
785
786 ++iObs;
787
788 if (corr->acName == _masterOrbitAC && resCorr.find(prn) == resCorr.end()) {
789 resCorr[prn] = new t_corr(*corr);
790 }
791
792 for (int iPar = 1; iPar <= _params.size(); iPar++) {
793 cmbParam* pp = _params[iPar-1];
794 AA(iObs, iPar) = pp->partial(corr->acName, prn);
795 }
796
797 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
798 }
799
800 // Regularization
801 // --------------
802 if (_method == filter) {
803 const double Ph = 1.e6;
804 PP(nObs+1) = Ph;
805 for (int iPar = 1; iPar <= _params.size(); iPar++) {
806 cmbParam* pp = _params[iPar-1];
807 if ( AA.Column(iPar).maximum_absolute_value() > 0.0 &&
808 pp->type == cmbParam::clkSat ) {
809 AA(nObs+1, iPar) = 1.0;
810 }
811 }
812 int iCond = 1;
813 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
814 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
815 ++iCond;
816 PP(nObs+iCond) = Ph;
817 for (int iPar = 1; iPar <= _params.size(); iPar++) {
818 cmbParam* pp = _params[iPar-1];
819 if ( AA.Column(iPar).maximum_absolute_value() > 0.0 &&
820 pp->type == cmbParam::offACSat &&
821 pp->prn == prn) {
822 AA(nObs+iCond, iPar) = 1.0;
823 }
824 }
825 }
826// if (_useGlonass) {
827// for (int iGlo = 1; iGlo <= MAXPRN_GLONASS; iGlo++) {
828// QString prn = QString("R%1").arg(iGlo, 2, 10, QChar('0'));
829// ++iCond;
830// PP(nObs+iCond) = Ph;
831// for (int iPar = 1; iPar <= _params.size(); iPar++) {
832// cmbParam* pp = _params[iPar-1];
833// if ( AA.Column(iPar).maximum_absolute_value() > 0.0 &&
834// pp->type == cmbParam::offACSat &&
835// pp->prn == prn) {
836// AA(nObs+iCond, iPar) = 1.0;
837// }
838// }
839// }
840// }
841 }
842
843 return success;
844}
845
846// Process Epoch - Single-Epoch Method
847////////////////////////////////////////////////////////////////////////////
848t_irc bncComb::processEpoch_singleEpoch(QTextStream& out,
849 QMap<QString, t_corr*>& resCorr,
850 ColumnVector& dx) {
851
852 // Check Satellite Positions for Outliers
853 // --------------------------------------
854 if (checkOrbits(out) != success) {
855 return failure;
856 }
857
858 // Outlier Detection Loop
859 // ----------------------
860 while (true) {
861
862 // Remove Satellites that are not in Master
863 // ----------------------------------------
864 QMutableVectorIterator<cmbCorr*> it(corrs());
865 while (it.hasNext()) {
866 cmbCorr* corr = it.next();
867 QString prn = corr->prn;
868 bool foundMaster = false;
869 QVectorIterator<cmbCorr*> itHlp(corrs());
870 while (itHlp.hasNext()) {
871 cmbCorr* corrHlp = itHlp.next();
872 QString prnHlp = corrHlp->prn;
873 QString ACHlp = corrHlp->acName;
874 if (ACHlp == _masterOrbitAC && prn == prnHlp) {
875 foundMaster = true;
876 break;
877 }
878 }
879 if (!foundMaster) {
880 delete corr;
881 it.remove();
882 }
883 }
884
885 // Count Number of Observations per Satellite and per AC
886 // -----------------------------------------------------
887 QMap<QString, int> numObsPrn;
888 QMap<QString, int> numObsAC;
889 QVectorIterator<cmbCorr*> itCorr(corrs());
890 while (itCorr.hasNext()) {
891 cmbCorr* corr = itCorr.next();
892 QString prn = corr->prn;
893 QString AC = corr->acName;
894 if (numObsPrn.find(prn) == numObsPrn.end()) {
895 numObsPrn[prn] = 1;
896 }
897 else {
898 numObsPrn[prn] += 1;
899 }
900 if (numObsAC.find(AC) == numObsAC.end()) {
901 numObsAC[AC] = 1;
902 }
903 else {
904 numObsAC[AC] += 1;
905 }
906 }
907
908 // Clean-Up the Paramters
909 // ----------------------
910 for (int iPar = 1; iPar <= _params.size(); iPar++) {
911 delete _params[iPar-1];
912 }
913 _params.clear();
914
915 // Set new Parameters
916 // ------------------
917 int nextPar = 0;
918
919 QMapIterator<QString, int> itAC(numObsAC);
920 while (itAC.hasNext()) {
921 itAC.next();
922 const QString& AC = itAC.key();
923 int numObs = itAC.value();
924 if (AC != _masterOrbitAC && numObs > 0) {
925 _params.push_back(new cmbParam(cmbParam::offACgps, ++nextPar, AC, ""));
926 if (_useGlonass) {
927 _params.push_back(new cmbParam(cmbParam::offACglo, ++nextPar, AC, ""));
928 }
929 }
930 }
931
932 QMapIterator<QString, int> itPrn(numObsPrn);
933 while (itPrn.hasNext()) {
934 itPrn.next();
935 const QString& prn = itPrn.key();
936 int numObs = itPrn.value();
937 if (numObs > 0) {
938 _params.push_back(new cmbParam(cmbParam::clkSat, ++nextPar, "", prn));
939 }
940 }
941
942 int nPar = _params.size();
943 ColumnVector x0(nPar);
944 x0 = 0.0;
945
946 // Create First-Design Matrix
947 // --------------------------
948 Matrix AA;
949 ColumnVector ll;
950 DiagonalMatrix PP;
951 if (createAmat(AA, ll, PP, x0, resCorr) != success) {
952 return failure;
953 }
954
955 ColumnVector vv;
956 try {
957 Matrix ATP = AA.t() * PP;
958 SymmetricMatrix NN; NN << ATP * AA;
959 ColumnVector bb = ATP * ll;
960 _QQ = NN.i();
961 dx = _QQ * bb;
962 vv = ll - AA * dx;
963 }
964 catch (Exception& exc) {
965 out << exc.what() << endl;
966 return failure;
967 }
968
969 int maxResIndex;
970 double maxRes = vv.maximum_absolute_value1(maxResIndex);
971 out.setRealNumberNotation(QTextStream::FixedNotation);
972 out.setRealNumberPrecision(3);
973 out << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
974 << " Maximum Residuum " << maxRes << ' '
975 << corrs()[maxResIndex-1]->acName << ' ' << corrs()[maxResIndex-1]->prn;
976
977 if (maxRes > _MAXRES) {
978 out << " Outlier" << endl;
979 delete corrs()[maxResIndex-1];
980 corrs().remove(maxResIndex-1);
981 }
982 else {
983 out << " OK" << endl;
984 out.setRealNumberNotation(QTextStream::FixedNotation);
985 out.setRealNumberPrecision(3);
986 for (int ii = 0; ii < vv.Nrows(); ii++) {
987 const cmbCorr* corr = corrs()[ii];
988 out << _resTime.datestr().c_str() << ' '
989 << _resTime.timestr().c_str() << " "
990 << corr->acName << ' ' << corr->prn;
991 out.setFieldWidth(6);
992 out << " dClk = " << corr->dClk * t_CST::c << " res = " << vv[ii] << endl;
993 out.setFieldWidth(0);
994 }
995 return success;
996 }
997
998 }
999
1000 return failure;
1001}
1002
1003// Check Satellite Positions for Outliers
1004////////////////////////////////////////////////////////////////////////////
1005t_irc bncComb::checkOrbits(QTextStream& out) {
1006
1007 const double MAX_DISPLACEMENT = 0.20;
1008
1009 // Switch to last ephemeris (if possible)
1010 // --------------------------------------
1011 QMutableVectorIterator<cmbCorr*> im(corrs());
1012 while (im.hasNext()) {
1013 cmbCorr* corr = im.next();
1014 QString prn = corr->prn;
1015 if (_eph.find(prn) == _eph.end()) {
1016 out << "checkOrbit: missing eph (not found) " << corr->prn << endl;
1017 delete corr;
1018 im.remove();
1019 }
1020 else if (corr->eph == 0) {
1021 out << "checkOrbit: missing eph (zero) " << corr->prn << endl;
1022 delete corr;
1023 im.remove();
1024 }
1025 else {
1026 if ( corr->eph == _eph[prn]->last || corr->eph == _eph[prn]->prev ) {
1027 switchToLastEph(_eph[prn]->last, corr);
1028 }
1029 else {
1030 out << "checkOrbit: missing eph (deleted) " << corr->prn << endl;
1031 delete corr;
1032 im.remove();
1033 }
1034 }
1035 }
1036
1037 while (true) {
1038
1039 // Compute Mean Corrections for all Satellites
1040 // -------------------------------------------
1041 QMap<QString, int> numCorr;
1042 QMap<QString, ColumnVector> meanRao;
1043 QVectorIterator<cmbCorr*> it(corrs());
1044 while (it.hasNext()) {
1045 cmbCorr* corr = it.next();
1046 QString prn = corr->prn;
1047 if (meanRao.find(prn) == meanRao.end()) {
1048 meanRao[prn].ReSize(4);
1049 meanRao[prn].Rows(1,3) = corr->rao;
1050 meanRao[prn](4) = 1;
1051 }
1052 else {
1053 meanRao[prn].Rows(1,3) += corr->rao;
1054 meanRao[prn](4) += 1;
1055 }
1056 if (numCorr.find(prn) == numCorr.end()) {
1057 numCorr[prn] = 1;
1058 }
1059 else {
1060 numCorr[prn] += 1;
1061 }
1062 }
1063
1064 // Compute Differences wrt Mean, find Maximum
1065 // ------------------------------------------
1066 QMap<QString, cmbCorr*> maxDiff;
1067 it.toFront();
1068 while (it.hasNext()) {
1069 cmbCorr* corr = it.next();
1070 QString prn = corr->prn;
1071 if (meanRao[prn](4) != 0) {
1072 meanRao[prn] /= meanRao[prn](4);
1073 meanRao[prn](4) = 0;
1074 }
1075 corr->diffRao = corr->rao - meanRao[prn].Rows(1,3);
1076 if (maxDiff.find(prn) == maxDiff.end()) {
1077 maxDiff[prn] = corr;
1078 }
1079 else {
1080 double normMax = maxDiff[prn]->diffRao.norm_Frobenius();
1081 double norm = corr->diffRao.norm_Frobenius();
1082 if (norm > normMax) {
1083 maxDiff[prn] = corr;
1084 }
1085 }
1086 }
1087
1088 if (_ACs.size() == 1) {
1089 break;
1090 }
1091
1092 // Remove Outliers
1093 // ---------------
1094 bool removed = false;
1095 QMutableVectorIterator<cmbCorr*> im(corrs());
1096 while (im.hasNext()) {
1097 cmbCorr* corr = im.next();
1098 QString prn = corr->prn;
1099 if (numCorr[prn] < 2) {
1100 delete corr;
1101 im.remove();
1102 }
1103 else if (corr == maxDiff[prn]) {
1104 double norm = corr->diffRao.norm_Frobenius();
1105 if (norm > MAX_DISPLACEMENT) {
1106 out << _resTime.datestr().c_str() << " "
1107 << _resTime.timestr().c_str() << " "
1108 << "Orbit Outlier: "
1109 << corr->acName.toAscii().data() << " "
1110 << prn.toAscii().data() << " "
1111 << corr->iod << " "
1112 << norm << endl;
1113 delete corr;
1114 im.remove();
1115 removed = true;
1116 }
1117 }
1118 }
1119
1120 if (!removed) {
1121 break;
1122 }
1123 }
1124
1125 return success;
1126}
1127
1128//
1129////////////////////////////////////////////////////////////////////////////
1130t_irc bncComb::mergeOrbitCorr(const cmbCorr* orbitCorr, cmbCorr* clkCorr) {
1131
1132 clkCorr->iod = orbitCorr->iod; // is it always correct?
1133 clkCorr->eph = orbitCorr->eph;
1134 clkCorr->tRao = orbitCorr->tRao;
1135 clkCorr->rao = orbitCorr->rao;
1136 clkCorr->dotRao = orbitCorr->dotRao;
1137 clkCorr->dotDotRao = orbitCorr->dotDotRao;
1138
1139 return success;
1140}
Note: See TracBrowser for help on using the repository browser.