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

Last change on this file since 3205 was 3205, checked in by mervart, 13 years ago
File size: 16.3 KB
RevLine 
[2898]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
[2933]17#include <newmatio.h>
[2921]18#include <iomanip>
[2898]19
20#include "bnccomb.h"
21#include "bncapp.h"
[3201]22#include "upload/bncrtnetdecoder.h"
[2910]23#include "bncsettings.h"
[2933]24#include "bncmodel.h"
[2988]25#include "bncutils.h"
[3027]26#include "bncpppclient.h"
[3181]27#include "bncsp3.h"
[3052]28#include "bncantex.h"
[3055]29#include "bnctides.h"
[2898]30
31using namespace std;
32
[3134]33const int MAXPRN_GPS = 32;
34
[2898]35// Constructor
36////////////////////////////////////////////////////////////////////////////
[3032]37cmbParam::cmbParam(cmbParam::parType type_, int index_,
38 const QString& ac_, const QString& prn_,
39 double sig_0_, double sig_P_) {
40
41 type = type_;
42 index = index_;
43 AC = ac_;
44 prn = prn_;
45 sig_0 = sig_0_;
46 sig_P = sig_P_;
[2933]47 xx = 0.0;
48}
49
50// Destructor
51////////////////////////////////////////////////////////////////////////////
52cmbParam::~cmbParam() {
53}
54
55// Partial
56////////////////////////////////////////////////////////////////////////////
[3032]57double cmbParam::partial(const QString& AC_, t_corr* corr) {
[2933]58
[2934]59 if (type == AC_offset) {
[3032]60 if (AC == AC_) {
[2934]61 return 1.0;
62 }
63 }
64 else if (type == Sat_offset) {
[3032]65 if (AC == AC_ && prn == corr->prn) {
[2933]66 return 1.0;
67 }
68 }
69 else if (type == clk) {
70 if (prn == corr->prn) {
71 return 1.0;
72 }
73 }
74
75 return 0.0;
76}
77
[2990]78//
79////////////////////////////////////////////////////////////////////////////
80QString cmbParam::toString() const {
[2933]81
[2990]82 QString outStr;
83
84 if (type == AC_offset) {
[2992]85 outStr = "AC offset " + AC;
[2990]86 }
87 else if (type == Sat_offset) {
[2992]88 outStr = "Sat Offset " + AC + " " + prn;
[2990]89 }
90 else if (type == clk) {
[2992]91 outStr = "Clk Corr " + prn;
[2990]92 }
[2933]93
[2990]94 return outStr;
95}
96
[2933]97// Constructor
98////////////////////////////////////////////////////////////////////////////
[2898]99bncComb::bncComb() {
[2910]100
101 bncSettings settings;
102
103 QStringList combineStreams = settings.value("combineStreams").toStringList();
[2918]104
[3143]105 if (combineStreams.size() >= 1 && !combineStreams[0].isEmpty()) {
[2910]106 QListIterator<QString> it(combineStreams);
107 while (it.hasNext()) {
108 QStringList hlp = it.next().split(" ");
[2918]109 cmbAC* newAC = new cmbAC();
110 newAC->mountPoint = hlp[0];
111 newAC->name = hlp[1];
112 newAC->weight = hlp[2].toDouble();
[3064]113 if (_masterAC.isEmpty()) {
114 _masterAC = newAC->name;
115 }
[2918]116 _ACs[newAC->mountPoint] = newAC;
[2910]117 }
118 }
[2927]119
[3201]120 _rtnetDecoder = new bncRtnetDecoder();
[3205]121 _rtnetDecoder->start();
[3201]122
[2990]123 connect(this, SIGNAL(newMessage(QByteArray,bool)),
124 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
[2933]125
[3032]126
127 // A Priori Sigmas (in Meters)
128 // ---------------------------
129 double sigAC_0 = 100.0;
130 double sigAC_P = 100.0;
131 double sigSat_0 = 100.0;
132 double sigSat_P = 0.0;
133 double sigClk_0 = 100.0;
134 double sigClk_P = 100.0;
135
136 // Initialize Parameters (model: Clk_Corr = AC_Offset + Sat_Offset + Clk)
137 // ----------------------------------------------------------------------
[2933]138 int nextPar = 0;
[2991]139 QMapIterator<QString, cmbAC*> it(_ACs);
140 while (it.hasNext()) {
141 it.next();
142 cmbAC* AC = it.value();
[3134]143 _params.push_back(new cmbParam(cmbParam::AC_offset, ++nextPar,
144 AC->name, "", sigAC_0, sigAC_P));
145 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
146 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
147 _params.push_back(new cmbParam(cmbParam::Sat_offset, ++nextPar,
148 AC->name, prn, sigSat_0, sigSat_P));
[2991]149 }
150 }
[3134]151 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
[2933]152 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
[3032]153 _params.push_back(new cmbParam(cmbParam::clk, ++nextPar, "", prn,
154 sigClk_0, sigClk_P));
[2933]155 }
156
[3032]157 // Initialize Variance-Covariance Matrix
158 // -------------------------------------
159 _QQ.ReSize(_params.size());
[2933]160 _QQ = 0.0;
161 for (int iPar = 1; iPar <= _params.size(); iPar++) {
162 cmbParam* pp = _params[iPar-1];
[3032]163 _QQ(iPar,iPar) = pp->sig_0 * pp->sig_0;
[2933]164 }
[3035]165
[3052]166 // ANTEX File
167 // ----------
168 _antex = 0;
169 QString antexFileName = settings.value("pppAntex").toString();
170 if (!antexFileName.isEmpty()) {
171 _antex = new bncAntex();
172 if (_antex->readFile(antexFileName) != success) {
173 emit newMessage("wrong ANTEX file", true);
174 delete _antex;
175 _antex = 0;
176 }
177 }
[3138]178
179 // Not yet regularized
180 // -------------------
181 _firstReg = false;
[2898]182}
183
184// Destructor
185////////////////////////////////////////////////////////////////////////////
186bncComb::~bncComb() {
[2918]187 QMapIterator<QString, cmbAC*> it(_ACs);
188 while (it.hasNext()) {
189 it.next();
190 delete it.value();
191 }
[3201]192 delete _rtnetDecoder;
[3052]193 delete _antex;
[2898]194}
195
[2928]196// Read and store one correction line
[2898]197////////////////////////////////////////////////////////////////////////////
198void bncComb::processCorrLine(const QString& staID, const QString& line) {
[2906]199 QMutexLocker locker(&_mutex);
[2913]200
[2918]201 // Find the relevant instance of cmbAC class
202 // -----------------------------------------
203 if (_ACs.find(staID) == _ACs.end()) {
204 return;
205 }
206 cmbAC* AC = _ACs[staID];
207
208 // Read the Correction
209 // -------------------
[2913]210 t_corr* newCorr = new t_corr();
211 if (!newCorr->readLine(line) == success) {
212 delete newCorr;
213 return;
214 }
215
[2924]216 // Reject delayed corrections
217 // --------------------------
218 if (_processedBeforeTime.valid() && newCorr->tt < _processedBeforeTime) {
219 delete newCorr;
220 return;
221 }
222
[3029]223 // Check the Ephemeris
224 //--------------------
[2986]225 if (_eph.find(newCorr->prn) == _eph.end()) {
226 delete newCorr;
227 return;
228 }
229 else {
230 t_eph* lastEph = _eph[newCorr->prn]->last;
231 t_eph* prevEph = _eph[newCorr->prn]->prev;
[3029]232 if (lastEph && lastEph->IOD() == newCorr->iod) {
233 newCorr->eph = lastEph;
[2986]234 }
[3029]235 else if (prevEph && prevEph->IOD() == newCorr->iod) {
236 newCorr->eph = prevEph;
237 }
238 else {
[2986]239 delete newCorr;
240 return;
241 }
242 }
243
[2924]244 // Process all older Epochs (if there are any)
245 // -------------------------------------------
246 const double waitTime = 5.0; // wait 5 sec
247 _processedBeforeTime = newCorr->tt - waitTime;
248
[2927]249 QList<cmbEpoch*> epochsToProcess;
250
251 QMapIterator<QString, cmbAC*> itAC(_ACs);
252 while (itAC.hasNext()) {
253 itAC.next();
254 cmbAC* AC = itAC.value();
255
256 QMutableListIterator<cmbEpoch*> itEpo(AC->epochs);
257 while (itEpo.hasNext()) {
258 cmbEpoch* epoch = itEpo.next();
259 if (epoch->time < _processedBeforeTime) {
260 epochsToProcess.append(epoch);
261 itEpo.remove();
262 }
263 }
264 }
265
266 if (epochsToProcess.size()) {
267 processEpochs(epochsToProcess);
268 }
269
[2920]270 // Check Modulo Time
271 // -----------------
272 const int moduloTime = 10;
273 if (int(newCorr->tt.gpssec()) % moduloTime != 0.0) {
274 delete newCorr;
275 return;
276 }
277
[2918]278 // Find/Create the instance of cmbEpoch class
279 // ------------------------------------------
280 cmbEpoch* newEpoch = 0;
281 QListIterator<cmbEpoch*> it(AC->epochs);
282 while (it.hasNext()) {
283 cmbEpoch* hlpEpoch = it.next();
284 if (hlpEpoch->time == newCorr->tt) {
285 newEpoch = hlpEpoch;
286 break;
287 }
288 }
289 if (newEpoch == 0) {
[2927]290 newEpoch = new cmbEpoch(AC->name);
[2918]291 newEpoch->time = newCorr->tt;
292 AC->epochs.append(newEpoch);
293 }
[2924]294
295 // Merge or add the correction
296 // ---------------------------
[2918]297 if (newEpoch->corr.find(newCorr->prn) != newEpoch->corr.end()) {
[2919]298 newEpoch->corr[newCorr->prn]->readLine(line); // merge (multiple messages)
[2918]299 }
[2919]300 else {
301 newEpoch->corr[newCorr->prn] = newCorr;
302 }
[2898]303}
304
[2986]305// Change the correction so that it refers to last received ephemeris
306////////////////////////////////////////////////////////////////////////////
[3029]307void bncComb::switchToLastEph(const t_eph* lastEph, t_corr* corr) {
[3028]308
[2987]309 ColumnVector oldXC(4);
310 ColumnVector oldVV(3);
[3029]311 corr->eph->position(corr->tt.gpsw(), corr->tt.gpssec(),
312 oldXC.data(), oldVV.data());
[2988]313
[2987]314 ColumnVector newXC(4);
315 ColumnVector newVV(3);
[3029]316 lastEph->position(corr->tt.gpsw(), corr->tt.gpssec(),
[2987]317 newXC.data(), newVV.data());
[2988]318
319 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
[2989]320 ColumnVector dV = newVV - oldVV;
321 double dC = newXC(4) - oldXC(4);
322
[2988]323 ColumnVector dRAO(3);
324 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
[2989]325
326 ColumnVector dDotRAO(3);
327 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
328
[3029]329 QString msg = "switch " + corr->prn
330 + QString(" %1 -> %2 %3").arg(corr->iod,3)
[3013]331 .arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
332
[3029]333 emit newMessage(msg.toAscii(), false);
[3028]334
[3029]335 corr->iod = lastEph->IOD();
336 corr->eph = lastEph;
[3031]337 corr->rao += dRAO;
338 corr->dotRao += dDotRAO;
[3029]339 corr->dClk -= dC;
[2986]340}
341
[2928]342// Process Epochs
343////////////////////////////////////////////////////////////////////////////
[2927]344void bncComb::processEpochs(const QList<cmbEpoch*>& epochs) {
[2918]345
[2990]346 _log.clear();
347
348 QTextStream out(&_log, QIODevice::WriteOnly);
349
[2991]350 out << "Combination:" << endl
351 << "------------------------------" << endl;
[2990]352
[2933]353 // Predict Parameters Values, Add White Noise
354 // ------------------------------------------
355 for (int iPar = 1; iPar <= _params.size(); iPar++) {
356 cmbParam* pp = _params[iPar-1];
[3032]357 if (pp->sig_P != 0.0) {
[2933]358 pp->xx = 0.0;
359 for (int jj = 1; jj <= _params.size(); jj++) {
360 _QQ(iPar, jj) = 0.0;
361 }
[3032]362 _QQ(iPar,iPar) = pp->sig_P * pp->sig_P;
[2933]363 }
364 }
365
[2928]366 bncTime resTime = epochs.first()->time;
367 QMap<QString, t_corr*> resCorr;
368
[2933]369 int nPar = _params.size();
370 int nObs = 0;
371
372 ColumnVector x0(nPar);
373 for (int iPar = 1; iPar <= _params.size(); iPar++) {
374 cmbParam* pp = _params[iPar-1];
375 x0(iPar) = pp->xx;
376 }
377
378 // Count Observations
379 // ------------------
[2927]380 QListIterator<cmbEpoch*> itEpo(epochs);
381 while (itEpo.hasNext()) {
382 cmbEpoch* epo = itEpo.next();
[3029]383 QMutableMapIterator<QString, t_corr*> itCorr(epo->corr);
[2927]384 while (itCorr.hasNext()) {
385 itCorr.next();
[3029]386 t_corr* corr = itCorr.value();
387
[3032]388 // Switch to last ephemeris
389 // ------------------------
[3029]390 t_eph* lastEph = _eph[corr->prn]->last;
391 if (lastEph == corr->eph) {
392 ++nObs;
393 }
394 else {
395 if (corr->eph == _eph[corr->prn]->prev) {
396 switchToLastEph(lastEph, corr);
397 ++nObs;
398 }
399 else {
400 itCorr.remove();
401 }
402 }
[2933]403 }
404 }
[2928]405
[2933]406 if (nObs > 0) {
[3135]407 const double Pl = 1.0 / (0.05 * 0.05);
408
[3140]409 const int nCon = (_firstReg == false) ? 1 + MAXPRN_GPS : 1;
[3134]410 Matrix AA(nObs+nCon, nPar); AA = 0.0;
411 ColumnVector ll(nObs+nCon); ll = 0.0;
[3135]412 DiagonalMatrix PP(nObs+nCon); PP = Pl;
[2933]413
414 int iObs = 0;
415 QListIterator<cmbEpoch*> itEpo(epochs);
416 while (itEpo.hasNext()) {
417 cmbEpoch* epo = itEpo.next();
418 QMapIterator<QString, t_corr*> itCorr(epo->corr);
419
420 while (itCorr.hasNext()) {
421 itCorr.next();
422 ++iObs;
423 t_corr* corr = itCorr.value();
424
[3032]425 if (epo->acName == _masterAC) {
[2933]426 resCorr[corr->prn] = new t_corr(*corr);
427 }
428
429 for (int iPar = 1; iPar <= _params.size(); iPar++) {
430 cmbParam* pp = _params[iPar-1];
431 AA(iObs, iPar) = pp->partial(epo->acName, corr);
432 }
433
434 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
[3034]435 }
[2990]436
[3034]437 delete epo;
[2933]438 }
[2928]439
[3134]440 // Regularization
441 // --------------
442 const double Ph = 1.e6;
443 int iCond = 1;
444 PP(nObs+iCond) = Ph;
445 for (int iPar = 1; iPar <= _params.size(); iPar++) {
446 cmbParam* pp = _params[iPar-1];
[3136]447 if (pp->type == cmbParam::clk &&
448 AA.Column(iPar).maximum_absolute_value() > 0.0) {
449 AA(nObs+iCond, iPar) = 1.0;
[3134]450 }
451 }
452
[3138]453 if (!_firstReg) {
454 _firstReg = true;
455 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
456 ++iCond;
457 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
458 PP(nObs+1+iGps) = Ph;
459 for (int iPar = 1; iPar <= _params.size(); iPar++) {
460 cmbParam* pp = _params[iPar-1];
461 if (pp->type == cmbParam::Sat_offset && pp->prn == prn) {
462 AA(nObs+iCond, iPar) = 1.0;
463 }
[3134]464 }
465 }
466 }
467
[3081]468 const double MAXRES = 999.10; // TODO: make it an option
[3080]469
[2933]470 ColumnVector dx;
[3080]471 SymmetricMatrix QQ_sav = _QQ;
[2933]472
[3080]473 for (int ii = 1; ii < 10; ii++) {
474 bncModel::kalman(AA, ll, PP, _QQ, dx);
475 ColumnVector vv = ll - AA * dx;
476
477 int maxResIndex;
478 double maxRes = vv.maximum_absolute_value1(maxResIndex);
479 out.setRealNumberNotation(QTextStream::FixedNotation);
480 out.setRealNumberPrecision(3);
481 out << "Maximum Residuum " << maxRes << " (index " << maxResIndex << ")\n";
482
483 if (maxRes > MAXRES) {
484 out << "Outlier Detected" << endl;
485 _QQ = QQ_sav;
486 AA.Row(maxResIndex) = 0.0;
487 ll.Row(maxResIndex) = 0.0;
488 }
489 else {
490 break;
491 }
492 }
493
[2933]494 for (int iPar = 1; iPar <= _params.size(); iPar++) {
495 cmbParam* pp = _params[iPar-1];
496 pp->xx += dx(iPar);
[2935]497 if (pp->type == cmbParam::clk) {
[2936]498 if (resCorr.find(pp->prn) != resCorr.end()) {
499 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
500 }
[2935]501 }
[3011]502 out << currentDateAndTimeGPS().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
[2990]503 out.setRealNumberNotation(QTextStream::FixedNotation);
504 out.setFieldWidth(8);
505 out.setRealNumberPrecision(4);
506 out << pp->toString() << " "
[2991]507 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
[2918]508 }
509 }
[2919]510
[3015]511 printResults(out, resTime, resCorr);
[2928]512 dumpResults(resTime, resCorr);
513
[2990]514 emit newMessage(_log, false);
[2918]515}
[3011]516
[3201]517// Print results
[3011]518////////////////////////////////////////////////////////////////////////////
[3015]519void bncComb::printResults(QTextStream& out, const bncTime& resTime,
[3011]520 const QMap<QString, t_corr*>& resCorr) {
521
522 QMapIterator<QString, t_corr*> it(resCorr);
523 while (it.hasNext()) {
524 it.next();
525 t_corr* corr = it.value();
[3029]526 const t_eph* eph = corr->eph;
[3015]527 if (eph) {
528 double xx, yy, zz, cc;
529 eph->position(resTime.gpsw(), resTime.gpssec(), xx, yy, zz, cc);
530
531 out << currentDateAndTimeGPS().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
532 out.setFieldWidth(3);
533 out << "Full Clock " << corr->prn << " " << corr->iod << " ";
534 out.setFieldWidth(14);
535 out << (cc + corr->dClk) * t_CST::c << endl;
536 }
537 else {
538 out << "bncComb::printResuls bug" << endl;
539 }
[3011]540 }
541}
[3202]542
543// Send results to RTNet Decoder and directly to PPP Client
544////////////////////////////////////////////////////////////////////////////
545void bncComb::dumpResults(const bncTime& resTime,
546 const QMap<QString, t_corr*>& resCorr) {
547
548 QMapIterator<QString, t_corr*> it(resCorr);
549 while (it.hasNext()) {
550 it.next();
551 t_corr* corr = it.value();
552
553// // SP3 Output
554// // ----------
555// if (_sp3) {
556// ColumnVector xc(4);
557// ColumnVector vv(3);
558// corr->eph->position(resTime.gpsw(), resTime.gpssec(),
559// xc.data(), vv.data());
560// bncPPPclient::applyCorr(resTime, corr, xc, vv);
561//
562// // Relativistic Correction
563// // -----------------------
564// xc(4) += 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c;
565//
566// // Correction Phase Center --> CoM
567// // -------------------------------
568// if (_antex) {
569// ColumnVector dx(3); dx = 0.0;
570// double Mjd = resTime.mjd() + resTime.daysec()/86400.0;
571// if (_antex->satCoMcorrection(corr->prn, Mjd, xc.Rows(1,3), dx) == success) {
572// xc(1) -= dx(1);
573// xc(2) -= dx(2);
574// xc(3) -= dx(3);
575// }
576// else {
577// cout << "antenna not found" << endl;
578// }
579// }
580// _sp3->write(resTime.gpsw(), resTime.gpssec(), corr->prn, xc);
581// }
582//
583// delete corr;
584// }
585
586 // Optionally send new Corrections to PPP
587 // --------------------------------------
588 bncApp* app = (bncApp*) qApp;
589 if (app->_bncPPPclient) {
590// QStringList corrLines;
591// co.messageType = COTYPE_GPSCOMBINED;
592// QStringListIterator il(RTCM3coDecoder::corrsToASCIIlines(resTime.gpsw(),
593// resTime.gpssec(), co, 0));
594// while (il.hasNext()) {
595// QString line = il.next();
596// line += " COMB";
597// corrLines << line;
598// }
599//
600// app->_bncPPPclient->slotNewCorrections(corrLines);
601 }
602 }
603}
Note: See TracBrowser for help on using the repository browser.