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

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