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

Last change on this file since 3437 was 3437, checked in by mervart, 13 years ago
File size: 18.4 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 // Remember Last Correction Time
271 // -----------------------------
272 if (!_lastCorrTime.valid() || _lastCorrTime < newCorr->tt) {
273 _lastCorrTime = newCorr->tt;
274 }
275
276 // Process previous Epoch(s)
277 // -------------------------
278 const double waitTime = 20.0; // wait 20 seconds
279 QListIterator<bncTime> itTime(_buffer.keys());
280 while (itTime.hasNext()) {
281 bncTime epoTime = itTime.next();
282 if (epoTime < _lastCorrTime - waitTime) {
283 _resTime = epoTime;
284 processEpoch();
285 }
286 }
287
288 // Merge or add the correction
289 // ---------------------------
290 QVector<cmbCorr*>& corrs = _buffer[newCorr->tt].corrs;
291 cmbCorr* existingCorr = 0;
292 QVectorIterator<cmbCorr*> itCorr(corrs);
293 while (itCorr.hasNext()) {
294 cmbCorr* hlp = itCorr.next();
295 if (hlp->prn == newCorr->prn && hlp->acName == newCorr->prn) {
296 existingCorr = hlp;
297 break;
298 }
299 }
300 if (existingCorr) {
301 delete newCorr;
302 existingCorr->readLine(line); // merge (multiple messages)
303 }
304 else {
305 corrs.append(newCorr);
306 }
307}
308
309// Change the correction so that it refers to last received ephemeris
310////////////////////////////////////////////////////////////////////////////
311void bncComb::switchToLastEph(const t_eph* lastEph, t_corr* corr) {
312
313 if (corr->eph == lastEph) {
314 return;
315 }
316
317 ColumnVector oldXC(4);
318 ColumnVector oldVV(3);
319 corr->eph->position(corr->tt.gpsw(), corr->tt.gpssec(),
320 oldXC.data(), oldVV.data());
321
322 ColumnVector newXC(4);
323 ColumnVector newVV(3);
324 lastEph->position(corr->tt.gpsw(), corr->tt.gpssec(),
325 newXC.data(), newVV.data());
326
327 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
328 ColumnVector dV = newVV - oldVV;
329 double dC = newXC(4) - oldXC(4);
330
331 ColumnVector dRAO(3);
332 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
333
334 ColumnVector dDotRAO(3);
335 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
336
337 QString msg = "switch " + corr->prn
338 + QString(" %1 -> %2 %3").arg(corr->iod,3)
339 .arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
340
341 emit newMessage(msg.toAscii(), false);
342
343 corr->iod = lastEph->IOD();
344 corr->eph = lastEph;
345 corr->rao += dRAO;
346 corr->dotRao += dDotRAO;
347 corr->dClk -= dC;
348}
349
350// Process Epoch
351////////////////////////////////////////////////////////////////////////////
352void bncComb::processEpoch() {
353
354 int nPar = _params.size();
355 int nObs = corrs().size();
356
357 if (nObs == 0) {
358 return;
359 }
360
361 _log.clear();
362
363 QTextStream out(&_log, QIODevice::WriteOnly);
364
365 out << endl << "Combination:" << endl
366 << "------------------------------" << endl;
367
368 // Observation Statistics
369 // ----------------------
370 QListIterator<cmbAC*> icAC(_ACs);
371 while (icAC.hasNext()) {
372 cmbAC* AC = icAC.next();
373 AC->numObs = 0;
374 QVectorIterator<cmbCorr*> itCorr(corrs());
375 while (itCorr.hasNext()) {
376 cmbCorr* corr = itCorr.next();
377 if (corr->acName == AC->name) {
378 AC->numObs += 1;
379 }
380 }
381 out << AC->name.toAscii().data() << ": " << AC->numObs << endl;
382 }
383
384 // Prediction Step
385 // ---------------
386 ColumnVector x0(nPar);
387 for (int iPar = 1; iPar <= _params.size(); iPar++) {
388 cmbParam* pp = _params[iPar-1];
389 _QQ(iPar,iPar) += pp->sig_P * pp->sig_P;
390 x0(iPar) = pp->xx;
391 }
392
393 // Create First Design Matrix and Vector of Measurements
394 // -----------------------------------------------------
395 const double Pl = 1.0 / (0.05 * 0.05);
396
397 const int nCon = (_firstReg == false) ? 1 + MAXPRN_GPS : 1;
398 Matrix AA(nObs+nCon, nPar); AA = 0.0;
399 ColumnVector ll(nObs+nCon); ll = 0.0;
400 DiagonalMatrix PP(nObs+nCon); PP = Pl;
401
402 int iObs = 0;
403
404 QMap<QString, t_corr*> resCorr;
405
406 QVectorIterator<cmbCorr*> itCorr(corrs());
407 while (itCorr.hasNext()) {
408 cmbCorr* corr = itCorr.next();
409 QString prn = corr->prn;
410 switchToLastEph(_eph[prn]->last, corr);
411 ++iObs;
412
413 if (resCorr.find(prn) == resCorr.end()) {
414 resCorr[prn] = new t_corr(*corr);
415 }
416
417 for (int iPar = 1; iPar <= _params.size(); iPar++) {
418 cmbParam* pp = _params[iPar-1];
419 AA(iObs, iPar) = pp->partial(corr->acName, prn);
420 }
421
422 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
423 }
424
425 // Regularization
426 // --------------
427 const double Ph = 1.e6;
428 int iCond = 1;
429 PP(nObs+iCond) = Ph;
430 for (int iPar = 1; iPar <= _params.size(); iPar++) {
431 cmbParam* pp = _params[iPar-1];
432 if (pp->type == cmbParam::clkSat &&
433 AA.Column(iPar).maximum_absolute_value() > 0.0) {
434 AA(nObs+iCond, iPar) = 1.0;
435 }
436 }
437
438 if (!_firstReg) {
439 _firstReg = true;
440 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
441 ++iCond;
442 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
443 PP(nObs+1+iGps) = Ph;
444 for (int iPar = 1; iPar <= _params.size(); iPar++) {
445 cmbParam* pp = _params[iPar-1];
446 if (pp->type == cmbParam::offACSat && pp->prn == prn) {
447 AA(nObs+iCond, iPar) = 1.0;
448 }
449 }
450 }
451 }
452
453 ColumnVector dx;
454 SymmetricMatrix QQ_sav = _QQ;
455
456 // Update and outlier detection loop
457 // ---------------------------------
458 for (int ii = 1; ii < 10; ii++) {
459 bncModel::kalman(AA, ll, PP, _QQ, dx);
460 ColumnVector vv = ll - AA * dx;
461
462 int maxResIndex;
463 double maxRes = vv.maximum_absolute_value1(maxResIndex);
464 out.setRealNumberNotation(QTextStream::FixedNotation);
465 out.setRealNumberPrecision(3);
466 out << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
467 << " Maximum Residuum " << maxRes << ' '
468 << corrs()[maxResIndex-1]->acName << ' ' << corrs()[maxResIndex-1]->prn;
469
470 if (maxRes > _MAXRES) {
471 for (int iPar = 1; iPar <= _params.size(); iPar++) {
472 cmbParam* pp = _params[iPar-1];
473 if (pp->type == cmbParam::offACSat &&
474 pp->AC == corrs()[maxResIndex-1]->acName &&
475 pp->prn == corrs()[maxResIndex-1]->prn) {
476 QQ_sav.Row(iPar) = 0.0;
477 QQ_sav.Column(iPar) = 0.0;
478 QQ_sav(iPar,iPar) = pp->sig_0 * pp->sig_0;
479 }
480 }
481
482 out << " Outlier" << endl;
483 _QQ = QQ_sav;
484 AA.Row(maxResIndex) = 0.0;
485 ll.Row(maxResIndex) = 0.0;
486 }
487 else {
488 out << " OK" << endl;
489 break;
490 }
491 }
492
493 // Update Parameter Values
494 // -----------------------
495 for (int iPar = 1; iPar <= _params.size(); iPar++) {
496 cmbParam* pp = _params[iPar-1];
497 pp->xx += dx(iPar);
498 if (pp->type == cmbParam::clkSat) {
499 if (resCorr.find(pp->prn) != resCorr.end()) {
500 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
501 }
502 }
503 out << _resTime.datestr().c_str() << " "
504 << _resTime.timestr().c_str() << " ";
505 out.setRealNumberNotation(QTextStream::FixedNotation);
506 out.setFieldWidth(8);
507 out.setRealNumberPrecision(4);
508 out << pp->toString() << " "
509 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
510 out.setFieldWidth(0);
511 }
512
513 // Print Results
514 // -------------
515 printResults(out, resCorr);
516 dumpResults(resCorr);
517
518 emit newMessage(_log, false);
519
520 // Delete Data
521 // -----------
522 _buffer.remove(_resTime);
523}
524
525// Print results
526////////////////////////////////////////////////////////////////////////////
527void bncComb::printResults(QTextStream& out,
528 const QMap<QString, t_corr*>& resCorr) {
529
530 QMapIterator<QString, t_corr*> it(resCorr);
531 while (it.hasNext()) {
532 it.next();
533 t_corr* corr = it.value();
534 const t_eph* eph = corr->eph;
535 if (eph) {
536 double xx, yy, zz, cc;
537 eph->position(_resTime.gpsw(), _resTime.gpssec(), xx, yy, zz, cc);
538
539 out << _resTime.datestr().c_str() << " "
540 << _resTime.timestr().c_str() << " ";
541 out.setFieldWidth(3);
542 out << "Full Clock " << corr->prn << " " << corr->iod << " ";
543 out.setFieldWidth(14);
544 out << (cc + corr->dClk) * t_CST::c << endl;
545 out.setFieldWidth(0);
546 }
547 else {
548 out << "bncComb::printResuls bug" << endl;
549 }
550 }
551}
552
553// Send results to RTNet Decoder and directly to PPP Client
554////////////////////////////////////////////////////////////////////////////
555void bncComb::dumpResults(const QMap<QString, t_corr*>& resCorr) {
556
557 ostringstream out; out.setf(std::ios::fixed);
558 QStringList corrLines;
559
560 unsigned year, month, day, hour, minute;
561 double sec;
562 _resTime.civil_date(year, month, day);
563 _resTime.civil_time(hour, minute, sec);
564
565 out << "* "
566 << setw(4) << year << " "
567 << setw(2) << month << " "
568 << setw(2) << day << " "
569 << setw(2) << hour << " "
570 << setw(2) << minute << " "
571 << setw(12) << setprecision(8) << sec << " "
572 << endl;
573
574 QMapIterator<QString, t_corr*> it(resCorr);
575 while (it.hasNext()) {
576 it.next();
577 t_corr* corr = it.value();
578
579 double dT = 60.0;
580
581 for (int iTime = 1; iTime <= 2; iTime++) {
582
583 bncTime time12 = (iTime == 1) ? _resTime : _resTime + dT;
584
585 ColumnVector xc(4);
586 ColumnVector vv(3);
587 corr->eph->position(time12.gpsw(), time12.gpssec(),
588 xc.data(), vv.data());
589 bncPPPclient::applyCorr(time12, corr, xc, vv);
590
591 // Relativistic Correction
592 // -----------------------
593 double relCorr = - 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c;
594 xc(4) -= relCorr;
595
596 // Code Biases
597 // -----------
598 double dcbP1C1 = 0.0;
599 double dcbP1P2 = 0.0;
600
601 // Correction Phase Center --> CoM
602 // -------------------------------
603 ColumnVector dx(3); dx = 0.0;
604 if (_antex) {
605 double Mjd = time12.mjd() + time12.daysec()/86400.0;
606 if (_antex->satCoMcorrection(corr->prn, Mjd, xc.Rows(1,3), dx) == success) {
607 xc(1) -= dx(1);
608 xc(2) -= dx(2);
609 xc(3) -= dx(3);
610 }
611 else {
612 cout << "antenna not found" << endl;
613 }
614 }
615
616 if (iTime == 1) {
617 out << 'P' << corr->prn.toAscii().data()
618 << setw(14) << setprecision(6) << xc(1) / 1000.0
619 << setw(14) << setprecision(6) << xc(2) / 1000.0
620 << setw(14) << setprecision(6) << xc(3) / 1000.0
621 << setw(14) << setprecision(6) << xc(4) * 1e6
622 << setw(14) << setprecision(6) << relCorr * 1e6
623 << setw(8) << setprecision(3) << dx(1)
624 << setw(8) << setprecision(3) << dx(2)
625 << setw(8) << setprecision(3) << dx(3)
626 << setw(8) << setprecision(3) << dcbP1C1
627 << setw(8) << setprecision(3) << dcbP1P2
628 << setw(6) << setprecision(1) << dT;
629
630 QString line;
631 int messageType = COTYPE_GPSCOMBINED;
632 int updateInt = 0;
633 line.sprintf("%d %d %d %.1f %s"
634 " %3d"
635 " %8.3f %8.3f %8.3f %8.3f"
636 " %10.5f %10.5f %10.5f %10.5f"
637 " %10.5f %10.5f %10.5f %10.5f INTERNAL",
638 messageType, updateInt, time12.gpsw(), time12.gpssec(),
639 corr->prn.toAscii().data(),
640 corr->iod,
641 corr->dClk * t_CST::c,
642 corr->rao[0],
643 corr->rao[1],
644 corr->rao[2],
645 corr->dotDClk * t_CST::c,
646 corr->dotRao[0],
647 corr->dotRao[1],
648 corr->dotRao[2],
649 corr->dotDotDClk * t_CST::c,
650 corr->dotDotRao[0],
651 corr->dotDotRao[1],
652 corr->dotDotRao[2]);
653 corrLines << line;
654 }
655 else {
656 out << setw(14) << setprecision(6) << xc(1) / 1000.0
657 << setw(14) << setprecision(6) << xc(2) / 1000.0
658 << setw(14) << setprecision(6) << xc(3) / 1000.0 << endl;
659 }
660 }
661
662 delete corr;
663 }
664 out << "EOE" << endl; // End Of Epoch flag
665
666 if (!_rtnetDecoder) {
667 _rtnetDecoder = new bncRtnetDecoder();
668 }
669
670 vector<string> errmsg;
671 _rtnetDecoder->Decode((char*) out.str().data(), out.str().size(), errmsg);
672
673 // Optionally send new Corrections to PPP
674 // --------------------------------------
675 bncApp* app = (bncApp*) qApp;
676 if (app->_bncPPPclient) {
677 app->_bncPPPclient->slotNewCorrections(corrLines);
678 }
679}
Note: See TracBrowser for help on using the repository browser.