source: ntrip/trunk/BNC/combination/bnccomb.cpp@ 3452

Last change on this file since 3452 was 3452, checked in by mervart, 13 years ago
File size: 18.7 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 "bncapp.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
32using namespace std;
33
34const int MAXPRN_GPS = 32;
35
36// Constructor
37////////////////////////////////////////////////////////////////////////////
38cmbParam::cmbParam(parType type_, int index_,
39 const QString& ac_, const QString& prn_) {
40
41 type = type_;
42 index = index_;
43 AC = ac_;
44 prn = prn_;
45 xx = 0.0;
46
47 if (type == offAC) {
48 sig_0 = 1000.0;
49 sig_P = 1000.0;
50 }
51 else if (type == offACSat) {
52 sig_0 = 100.0;
53 sig_P = 0.0;
54 }
55 else if (type == clkSat) {
56 sig_0 = 100.0;
57 sig_P = 100.0;
58 }
59}
60
61// Destructor
62////////////////////////////////////////////////////////////////////////////
63cmbParam::~cmbParam() {
64}
65
66// Partial
67////////////////////////////////////////////////////////////////////////////
68double cmbParam::partial(const QString& AC_, const QString& prn_) {
69
70 if (type == offAC) {
71 if (AC == AC_) {
72 return 1.0;
73 }
74 }
75 else if (type == offACSat) {
76 if (AC == AC_ && prn == prn_) {
77 return 1.0;
78 }
79 }
80 else if (type == clkSat) {
81 if (prn == prn_) {
82 return 1.0;
83 }
84 }
85
86 return 0.0;
87}
88
89//
90////////////////////////////////////////////////////////////////////////////
91QString cmbParam::toString() const {
92
93 QString outStr;
94
95 if (type == offAC) {
96 outStr = "AC offset " + AC;
97 }
98 else if (type == offACSat) {
99 outStr = "Sat Offset " + AC + " " + prn;
100 }
101 else if (type == clkSat) {
102 outStr = "Clk Corr " + prn;
103 }
104
105 return outStr;
106}
107
108// Constructor
109////////////////////////////////////////////////////////////////////////////
110bncComb::bncComb() {
111
112 bncSettings settings;
113
114 QStringList combineStreams = settings.value("combineStreams").toStringList();
115
116 if (combineStreams.size() >= 1 && !combineStreams[0].isEmpty()) {
117 QListIterator<QString> it(combineStreams);
118 while (it.hasNext()) {
119 QStringList hlp = it.next().split(" ");
120 cmbAC* newAC = new cmbAC();
121 newAC->mountPoint = hlp[0];
122 newAC->name = hlp[1];
123 newAC->weight = hlp[2].toDouble();
124 _ACs.append(newAC);
125 }
126 }
127
128 _rtnetDecoder = 0;
129
130 connect(this, SIGNAL(newMessage(QByteArray,bool)),
131 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
132
133
134 // Initialize Parameters (model: Clk_Corr = AC_Offset + Sat_Offset + Clk)
135 // ----------------------------------------------------------------------
136 int nextPar = 0;
137 QListIterator<cmbAC*> it(_ACs);
138 while (it.hasNext()) {
139 cmbAC* AC = it.next();
140 _params.push_back(new cmbParam(cmbParam::offAC, ++nextPar, AC->name, ""));
141 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
142 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
143 _params.push_back(new cmbParam(cmbParam::offACSat, ++nextPar,
144 AC->name, prn));
145 }
146 }
147 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
148 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
149 _params.push_back(new cmbParam(cmbParam::clkSat, ++nextPar, "", prn));
150 }
151
152 // Initialize Variance-Covariance Matrix
153 // -------------------------------------
154 _QQ.ReSize(_params.size());
155 _QQ = 0.0;
156 for (int iPar = 1; iPar <= _params.size(); iPar++) {
157 cmbParam* pp = _params[iPar-1];
158 _QQ(iPar,iPar) = pp->sig_0 * pp->sig_0;
159 }
160
161 // ANTEX File
162 // ----------
163 _antex = 0;
164 QString antexFileName = settings.value("pppAntex").toString();
165 if (!antexFileName.isEmpty()) {
166 _antex = new bncAntex();
167 if (_antex->readFile(antexFileName) != success) {
168 emit newMessage("wrong ANTEX file", true);
169 delete _antex;
170 _antex = 0;
171 }
172 }
173
174 // Not yet regularized
175 // -------------------
176 _firstReg = false;
177
178 // Maximal Residuum
179 // ----------------
180 _MAXRES = settings.value("cmbMaxres").toDouble();
181 if (_MAXRES <= 0.0) {
182 _MAXRES = 999.0;
183 }
184}
185
186// Destructor
187////////////////////////////////////////////////////////////////////////////
188bncComb::~bncComb() {
189 QListIterator<cmbAC*> icAC(_ACs);
190 while (icAC.hasNext()) {
191 delete icAC.next();
192 }
193 delete _rtnetDecoder;
194 delete _antex;
195 for (int iPar = 1; iPar <= _params.size(); iPar++) {
196 delete _params[iPar-1];
197 }
198 QVectorIterator<cmbCorr*> itCorr(corrs());
199 while (itCorr.hasNext()) {
200 delete itCorr.next();
201 }
202}
203
204// Read and store one correction line
205////////////////////////////////////////////////////////////////////////////
206void bncComb::processCorrLine(const QString& staID, const QString& line) {
207 QMutexLocker locker(&_mutex);
208
209 // Find the AC Name
210 // ----------------
211 QString acName;
212 QListIterator<cmbAC*> icAC(_ACs);
213 while (icAC.hasNext()) {
214 cmbAC* AC = icAC.next();
215 if (AC->mountPoint == staID) {
216 acName = AC->name;
217 break;
218 }
219 }
220 if (acName.isEmpty()) {
221 return;
222 }
223
224 // Read the Correction
225 // -------------------
226 cmbCorr* newCorr = new cmbCorr();
227 newCorr->acName = acName;
228 if (!newCorr->readLine(line) == success) {
229 delete newCorr;
230 return;
231 }
232
233 // Check Modulo Time
234 // -----------------
235 const int moduloTime = 10;
236 if (int(newCorr->tt.gpssec()) % moduloTime != 0.0) {
237 delete newCorr;
238 return;
239 }
240
241 // Delete old corrections
242 // ----------------------
243 if (_resTime.valid() && newCorr->tt <= _resTime) {
244 delete newCorr;
245 return;
246 }
247
248 // Check the Ephemeris
249 //--------------------
250 if (_eph.find(newCorr->prn) == _eph.end()) {
251 delete newCorr;
252 return;
253 }
254 else {
255 t_eph* lastEph = _eph[newCorr->prn]->last;
256 t_eph* prevEph = _eph[newCorr->prn]->prev;
257 if (lastEph && lastEph->IOD() == newCorr->iod) {
258 newCorr->eph = lastEph;
259 }
260 else if (prevEph && prevEph->IOD() == newCorr->iod) {
261 newCorr->eph = prevEph;
262 switchToLastEph(lastEph, newCorr);
263 }
264 else {
265 delete newCorr;
266 return;
267 }
268 }
269
270 // Process previous Epoch(s)
271 // -------------------------
272 QListIterator<bncTime> itTime(_buffer.keys());
273 while (itTime.hasNext()) {
274 bncTime epoTime = itTime.next();
275 if (epoTime < newCorr->tt - moduloTime) {
276 _resTime = epoTime;
277 processEpoch();
278 }
279 }
280
281 // Merge or add the correction
282 // ---------------------------
283 QVector<cmbCorr*>& corrs = _buffer[newCorr->tt].corrs;
284 cmbCorr* existingCorr = 0;
285 QVectorIterator<cmbCorr*> itCorr(corrs);
286 while (itCorr.hasNext()) {
287 cmbCorr* hlp = itCorr.next();
288 if (hlp->prn == newCorr->prn && hlp->acName == newCorr->prn) {
289 existingCorr = hlp;
290 break;
291 }
292 }
293 if (existingCorr) {
294 delete newCorr;
295 existingCorr->readLine(line); // merge (multiple messages)
296 }
297 else {
298 corrs.append(newCorr);
299 }
300}
301
302// Change the correction so that it refers to last received ephemeris
303////////////////////////////////////////////////////////////////////////////
304void bncComb::switchToLastEph(const t_eph* lastEph, t_corr* corr) {
305
306 if (corr->eph == lastEph) {
307 return;
308 }
309
310 ColumnVector oldXC(4);
311 ColumnVector oldVV(3);
312 corr->eph->position(corr->tt.gpsw(), corr->tt.gpssec(),
313 oldXC.data(), oldVV.data());
314
315 ColumnVector newXC(4);
316 ColumnVector newVV(3);
317 lastEph->position(corr->tt.gpsw(), corr->tt.gpssec(),
318 newXC.data(), newVV.data());
319
320 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
321 ColumnVector dV = newVV - oldVV;
322 double dC = newXC(4) - oldXC(4);
323
324 ColumnVector dRAO(3);
325 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
326
327 ColumnVector dDotRAO(3);
328 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
329
330 QString msg = "switch " + corr->prn
331 + QString(" %1 -> %2 %3").arg(corr->iod,3)
332 .arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
333
334 emit newMessage(msg.toAscii(), false);
335
336 corr->iod = lastEph->IOD();
337 corr->eph = lastEph;
338 corr->rao += dRAO;
339 corr->dotRao += dDotRAO;
340 corr->dClk -= dC;
341}
342
343// Process Epoch
344////////////////////////////////////////////////////////////////////////////
345void bncComb::processEpoch() {
346
347 int nPar = _params.size();
348 int nObs = corrs().size();
349
350 if (nObs == 0) {
351 return;
352 }
353
354 _log.clear();
355
356 QTextStream out(&_log, QIODevice::WriteOnly);
357
358 out << endl << "Combination:" << endl
359 << "------------------------------" << endl;
360
361 // Observation Statistics
362 // ----------------------
363 QListIterator<cmbAC*> icAC(_ACs);
364 while (icAC.hasNext()) {
365 cmbAC* AC = icAC.next();
366 AC->numObs = 0;
367 QVectorIterator<cmbCorr*> itCorr(corrs());
368 while (itCorr.hasNext()) {
369 cmbCorr* corr = itCorr.next();
370 if (corr->acName == AC->name) {
371 AC->numObs += 1;
372 }
373 }
374 out << AC->name.toAscii().data() << ": " << AC->numObs << endl;
375 }
376
377 // Check Number of Observations per Satellite
378 // ------------------------------------------
379 QMap<QString, int> numObs;
380 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
381 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
382 numObs[prn] = 0;
383 QVectorIterator<cmbCorr*> itCorr(corrs());
384 while (itCorr.hasNext()) {
385 cmbCorr* corr = itCorr.next();
386 if (corr->prn == prn) {
387 ++numObs[prn];
388 }
389 }
390 }
391
392 // Prediction Step
393 // ---------------
394 ColumnVector x0(nPar);
395 for (int iPar = 1; iPar <= _params.size(); iPar++) {
396 cmbParam* pp = _params[iPar-1];
397 if (pp->sig_P != 0.0) {
398 pp->xx = 0.0;
399 for (int jj = 1; jj <= _params.size(); jj++) {
400 _QQ(iPar, jj) = 0.0;
401 }
402 _QQ(iPar,iPar) = pp->sig_P * pp->sig_P;
403 }
404 x0(iPar) = pp->xx;
405 }
406
407 // Create First Design Matrix and Vector of Measurements
408 // -----------------------------------------------------
409 const double Pl = 1.0 / (0.05 * 0.05);
410
411 const int nCon = (_firstReg == false) ? 1 + MAXPRN_GPS : 1;
412 Matrix AA(nObs+nCon, nPar); AA = 0.0;
413 ColumnVector ll(nObs+nCon); ll = 0.0;
414 DiagonalMatrix PP(nObs+nCon); PP = Pl;
415
416 int iObs = 0;
417
418 QMap<QString, t_corr*> resCorr;
419
420 QVectorIterator<cmbCorr*> itCorr(corrs());
421 while (itCorr.hasNext()) {
422 cmbCorr* corr = itCorr.next();
423 QString prn = corr->prn;
424 switchToLastEph(_eph[prn]->last, corr);
425 ++iObs;
426
427 if (numObs[prn] >= 3 && resCorr.find(prn) == resCorr.end()) {
428 resCorr[prn] = new t_corr(*corr);
429 }
430
431 for (int iPar = 1; iPar <= _params.size(); iPar++) {
432 cmbParam* pp = _params[iPar-1];
433 AA(iObs, iPar) = pp->partial(corr->acName, prn);
434 }
435
436 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
437 }
438
439 // Regularization
440 // --------------
441 const double Ph = 1.e6;
442 int iCond = 1;
443 PP(nObs+iCond) = Ph;
444 for (int iPar = 1; iPar <= _params.size(); iPar++) {
445 cmbParam* pp = _params[iPar-1];
446 if (pp->type == cmbParam::clkSat &&
447 AA.Column(iPar).maximum_absolute_value() > 0.0) {
448 AA(nObs+iCond, iPar) = 1.0;
449 }
450 }
451
452 if (!_firstReg) {
453 _firstReg = true;
454 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
455 ++iCond;
456 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
457 PP(nObs+1+iGps) = Ph;
458 for (int iPar = 1; iPar <= _params.size(); iPar++) {
459 cmbParam* pp = _params[iPar-1];
460 if (pp->type == cmbParam::offACSat && pp->prn == prn) {
461 AA(nObs+iCond, iPar) = 1.0;
462 }
463 }
464 }
465 }
466
467 ColumnVector dx;
468 SymmetricMatrix QQ_sav = _QQ;
469
470 // Update and outlier detection loop
471 // ---------------------------------
472 for (int ii = 1; ii < 10; ii++) {
473 bncModel::kalman(AA, ll, PP, _QQ, dx);
474 ColumnVector vv = ll - AA * dx;
475
476 int maxResIndex;
477 double maxRes = vv.maximum_absolute_value1(maxResIndex);
478 out.setRealNumberNotation(QTextStream::FixedNotation);
479 out.setRealNumberPrecision(3);
480 out << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
481 << " Maximum Residuum " << maxRes << ' '
482 << corrs()[maxResIndex-1]->acName << ' ' << corrs()[maxResIndex-1]->prn;
483
484 if (maxRes > _MAXRES) {
485 for (int iPar = 1; iPar <= _params.size(); iPar++) {
486 cmbParam* pp = _params[iPar-1];
487 if (pp->type == cmbParam::offACSat &&
488 pp->AC == corrs()[maxResIndex-1]->acName &&
489 pp->prn == corrs()[maxResIndex-1]->prn) {
490 QQ_sav.Row(iPar) = 0.0;
491 QQ_sav.Column(iPar) = 0.0;
492 QQ_sav(iPar,iPar) = pp->sig_0 * pp->sig_0;
493 }
494 }
495
496 out << " Outlier" << endl;
497 _QQ = QQ_sav;
498 AA.Row(maxResIndex) = 0.0;
499 ll.Row(maxResIndex) = 0.0;
500 }
501 else {
502 out << " OK" << endl;
503 break;
504 }
505 }
506
507 // Update Parameter Values
508 // -----------------------
509 for (int iPar = 1; iPar <= _params.size(); iPar++) {
510 cmbParam* pp = _params[iPar-1];
511 pp->xx += dx(iPar);
512 if (pp->type == cmbParam::clkSat) {
513 if (resCorr.find(pp->prn) != resCorr.end()) {
514 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
515 }
516 }
517 out << _resTime.datestr().c_str() << " "
518 << _resTime.timestr().c_str() << " ";
519 out.setRealNumberNotation(QTextStream::FixedNotation);
520 out.setFieldWidth(8);
521 out.setRealNumberPrecision(4);
522 out << pp->toString() << " "
523 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
524 out.setFieldWidth(0);
525 }
526
527 // Print Results
528 // -------------
529 printResults(out, resCorr);
530 dumpResults(resCorr);
531
532 emit newMessage(_log, false);
533
534 // Delete Data
535 // -----------
536 _buffer.remove(_resTime);
537}
538
539// Print results
540////////////////////////////////////////////////////////////////////////////
541void bncComb::printResults(QTextStream& out,
542 const QMap<QString, t_corr*>& resCorr) {
543
544 QMapIterator<QString, t_corr*> it(resCorr);
545 while (it.hasNext()) {
546 it.next();
547 t_corr* corr = it.value();
548 const t_eph* eph = corr->eph;
549 if (eph) {
550 double xx, yy, zz, cc;
551 eph->position(_resTime.gpsw(), _resTime.gpssec(), xx, yy, zz, cc);
552
553 out << _resTime.datestr().c_str() << " "
554 << _resTime.timestr().c_str() << " ";
555 out.setFieldWidth(3);
556 out << "Full Clock " << corr->prn << " " << corr->iod << " ";
557 out.setFieldWidth(14);
558 out << (cc + corr->dClk) * t_CST::c << endl;
559 out.setFieldWidth(0);
560 }
561 else {
562 out << "bncComb::printResuls bug" << endl;
563 }
564 }
565}
566
567// Send results to RTNet Decoder and directly to PPP Client
568////////////////////////////////////////////////////////////////////////////
569void bncComb::dumpResults(const QMap<QString, t_corr*>& resCorr) {
570
571 ostringstream out; out.setf(std::ios::fixed);
572 QStringList corrLines;
573
574 unsigned year, month, day, hour, minute;
575 double sec;
576 _resTime.civil_date(year, month, day);
577 _resTime.civil_time(hour, minute, sec);
578
579 out << "* "
580 << setw(4) << year << " "
581 << setw(2) << month << " "
582 << setw(2) << day << " "
583 << setw(2) << hour << " "
584 << setw(2) << minute << " "
585 << setw(12) << setprecision(8) << sec << " "
586 << endl;
587
588 QMapIterator<QString, t_corr*> it(resCorr);
589 while (it.hasNext()) {
590 it.next();
591 t_corr* corr = it.value();
592
593 double dT = 60.0;
594
595 for (int iTime = 1; iTime <= 2; iTime++) {
596
597 bncTime time12 = (iTime == 1) ? _resTime : _resTime + dT;
598
599 ColumnVector xc(4);
600 ColumnVector vv(3);
601 corr->eph->position(time12.gpsw(), time12.gpssec(),
602 xc.data(), vv.data());
603 bncPPPclient::applyCorr(time12, corr, xc, vv);
604
605 // Relativistic Correction
606 // -----------------------
607 double relCorr = - 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c;
608 xc(4) -= relCorr;
609
610 // Code Biases
611 // -----------
612 double dcbP1C1 = 0.0;
613 double dcbP1P2 = 0.0;
614
615 // Correction Phase Center --> CoM
616 // -------------------------------
617 ColumnVector dx(3); dx = 0.0;
618 if (_antex) {
619 double Mjd = time12.mjd() + time12.daysec()/86400.0;
620 if (_antex->satCoMcorrection(corr->prn, Mjd, xc.Rows(1,3), dx) == success) {
621 xc(1) -= dx(1);
622 xc(2) -= dx(2);
623 xc(3) -= dx(3);
624 }
625 else {
626 cout << "antenna not found" << endl;
627 }
628 }
629
630 if (iTime == 1) {
631 out << 'P' << corr->prn.toAscii().data()
632 << setw(14) << setprecision(6) << xc(1) / 1000.0
633 << setw(14) << setprecision(6) << xc(2) / 1000.0
634 << setw(14) << setprecision(6) << xc(3) / 1000.0
635 << setw(14) << setprecision(6) << xc(4) * 1e6
636 << setw(14) << setprecision(6) << relCorr * 1e6
637 << setw(8) << setprecision(3) << dx(1)
638 << setw(8) << setprecision(3) << dx(2)
639 << setw(8) << setprecision(3) << dx(3)
640 << setw(8) << setprecision(3) << dcbP1C1
641 << setw(8) << setprecision(3) << dcbP1P2
642 << setw(6) << setprecision(1) << dT;
643
644 QString line;
645 int messageType = COTYPE_GPSCOMBINED;
646 int updateInt = 0;
647 line.sprintf("%d %d %d %.1f %s"
648 " %3d"
649 " %8.3f %8.3f %8.3f %8.3f"
650 " %10.5f %10.5f %10.5f %10.5f"
651 " %10.5f %10.5f %10.5f %10.5f INTERNAL",
652 messageType, updateInt, time12.gpsw(), time12.gpssec(),
653 corr->prn.toAscii().data(),
654 corr->iod,
655 corr->dClk * t_CST::c,
656 corr->rao[0],
657 corr->rao[1],
658 corr->rao[2],
659 corr->dotDClk * t_CST::c,
660 corr->dotRao[0],
661 corr->dotRao[1],
662 corr->dotRao[2],
663 corr->dotDotDClk * t_CST::c,
664 corr->dotDotRao[0],
665 corr->dotDotRao[1],
666 corr->dotDotRao[2]);
667 corrLines << line;
668 }
669 else {
670 out << setw(14) << setprecision(6) << xc(1) / 1000.0
671 << setw(14) << setprecision(6) << xc(2) / 1000.0
672 << setw(14) << setprecision(6) << xc(3) / 1000.0 << endl;
673 }
674 }
675
676 delete corr;
677 }
678 out << "EOE" << endl; // End Of Epoch flag
679
680 if (!_rtnetDecoder) {
681 _rtnetDecoder = new bncRtnetDecoder();
682 }
683
684 vector<string> errmsg;
685 _rtnetDecoder->Decode((char*) out.str().data(), out.str().size(), errmsg);
686
687 // Optionally send new Corrections to PPP
688 // --------------------------------------
689 bncApp* app = (bncApp*) qApp;
690 if (app->_bncPPPclient) {
691 app->_bncPPPclient->slotNewCorrections(corrLines);
692 }
693}
Note: See TracBrowser for help on using the repository browser.