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

Last change on this file since 9258 was 9258, checked in by stuerze, 20 months ago

combination adapted to work system by system

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