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
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
20#include "bnccomb.h"
21#include "bncapp.h"
22#include "upload/bncrtnetdecoder.h"
23#include "bncsettings.h"
24#include "bncmodel.h"
25#include "bncutils.h"
26#include "bncpppclient.h"
27#include "bncsp3.h"
28#include "bncantex.h"
29#include "bnctides.h"
30
31using namespace std;
32
33const int MAXPRN_GPS = 32;
34
35// Constructor
36////////////////////////////////////////////////////////////////////////////
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_;
47 xx = 0.0;
48}
49
50// Destructor
51////////////////////////////////////////////////////////////////////////////
52cmbParam::~cmbParam() {
53}
54
55// Partial
56////////////////////////////////////////////////////////////////////////////
57double cmbParam::partial(const QString& AC_, t_corr* corr) {
58
59 if (type == AC_offset) {
60 if (AC == AC_) {
61 return 1.0;
62 }
63 }
64 else if (type == Sat_offset) {
65 if (AC == AC_ && prn == corr->prn) {
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
78//
79////////////////////////////////////////////////////////////////////////////
80QString cmbParam::toString() const {
81
82 QString outStr;
83
84 if (type == AC_offset) {
85 outStr = "AC offset " + AC;
86 }
87 else if (type == Sat_offset) {
88 outStr = "Sat Offset " + AC + " " + prn;
89 }
90 else if (type == clk) {
91 outStr = "Clk Corr " + prn;
92 }
93
94 return outStr;
95}
96
97// Constructor
98////////////////////////////////////////////////////////////////////////////
99bncComb::bncComb() {
100
101 bncSettings settings;
102
103 QStringList combineStreams = settings.value("combineStreams").toStringList();
104
105 if (combineStreams.size() >= 1 && !combineStreams[0].isEmpty()) {
106 QListIterator<QString> it(combineStreams);
107 while (it.hasNext()) {
108 QStringList hlp = it.next().split(" ");
109 cmbAC* newAC = new cmbAC();
110 newAC->mountPoint = hlp[0];
111 newAC->name = hlp[1];
112 newAC->weight = hlp[2].toDouble();
113 if (_masterAC.isEmpty()) {
114 _masterAC = newAC->name;
115 }
116 _ACs[newAC->mountPoint] = newAC;
117 }
118 }
119
120 _rtnetDecoder = new bncRtnetDecoder();
121 _rtnetDecoder->start();
122
123 connect(this, SIGNAL(newMessage(QByteArray,bool)),
124 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
125
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 // ----------------------------------------------------------------------
138 int nextPar = 0;
139 QMapIterator<QString, cmbAC*> it(_ACs);
140 while (it.hasNext()) {
141 it.next();
142 cmbAC* AC = it.value();
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));
149 }
150 }
151 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
152 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
153 _params.push_back(new cmbParam(cmbParam::clk, ++nextPar, "", prn,
154 sigClk_0, sigClk_P));
155 }
156
157 // Initialize Variance-Covariance Matrix
158 // -------------------------------------
159 _QQ.ReSize(_params.size());
160 _QQ = 0.0;
161 for (int iPar = 1; iPar <= _params.size(); iPar++) {
162 cmbParam* pp = _params[iPar-1];
163 _QQ(iPar,iPar) = pp->sig_0 * pp->sig_0;
164 }
165
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 }
178
179 // Not yet regularized
180 // -------------------
181 _firstReg = false;
182}
183
184// Destructor
185////////////////////////////////////////////////////////////////////////////
186bncComb::~bncComb() {
187 QMapIterator<QString, cmbAC*> it(_ACs);
188 while (it.hasNext()) {
189 it.next();
190 delete it.value();
191 }
192 delete _rtnetDecoder;
193 delete _antex;
194}
195
196// Read and store one correction line
197////////////////////////////////////////////////////////////////////////////
198void bncComb::processCorrLine(const QString& staID, const QString& line) {
199 QMutexLocker locker(&_mutex);
200
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 // -------------------
210 t_corr* newCorr = new t_corr();
211 if (!newCorr->readLine(line) == success) {
212 delete newCorr;
213 return;
214 }
215
216 // Reject delayed corrections
217 // --------------------------
218 if (_processedBeforeTime.valid() && newCorr->tt < _processedBeforeTime) {
219 delete newCorr;
220 return;
221 }
222
223 // Check the Ephemeris
224 //--------------------
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;
232 if (lastEph && lastEph->IOD() == newCorr->iod) {
233 newCorr->eph = lastEph;
234 }
235 else if (prevEph && prevEph->IOD() == newCorr->iod) {
236 newCorr->eph = prevEph;
237 }
238 else {
239 delete newCorr;
240 return;
241 }
242 }
243
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
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
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
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) {
290 newEpoch = new cmbEpoch(AC->name);
291 newEpoch->time = newCorr->tt;
292 AC->epochs.append(newEpoch);
293 }
294
295 // Merge or add the correction
296 // ---------------------------
297 if (newEpoch->corr.find(newCorr->prn) != newEpoch->corr.end()) {
298 newEpoch->corr[newCorr->prn]->readLine(line); // merge (multiple messages)
299 }
300 else {
301 newEpoch->corr[newCorr->prn] = newCorr;
302 }
303}
304
305// Change the correction so that it refers to last received ephemeris
306////////////////////////////////////////////////////////////////////////////
307void bncComb::switchToLastEph(const t_eph* lastEph, t_corr* corr) {
308
309 ColumnVector oldXC(4);
310 ColumnVector oldVV(3);
311 corr->eph->position(corr->tt.gpsw(), corr->tt.gpssec(),
312 oldXC.data(), oldVV.data());
313
314 ColumnVector newXC(4);
315 ColumnVector newVV(3);
316 lastEph->position(corr->tt.gpsw(), corr->tt.gpssec(),
317 newXC.data(), newVV.data());
318
319 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
320 ColumnVector dV = newVV - oldVV;
321 double dC = newXC(4) - oldXC(4);
322
323 ColumnVector dRAO(3);
324 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
325
326 ColumnVector dDotRAO(3);
327 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
328
329 QString msg = "switch " + corr->prn
330 + QString(" %1 -> %2 %3").arg(corr->iod,3)
331 .arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
332
333 emit newMessage(msg.toAscii(), false);
334
335 corr->iod = lastEph->IOD();
336 corr->eph = lastEph;
337 corr->rao += dRAO;
338 corr->dotRao += dDotRAO;
339 corr->dClk -= dC;
340}
341
342// Process Epochs
343////////////////////////////////////////////////////////////////////////////
344void bncComb::processEpochs(const QList<cmbEpoch*>& epochs) {
345
346 _log.clear();
347
348 QTextStream out(&_log, QIODevice::WriteOnly);
349
350 out << "Combination:" << endl
351 << "------------------------------" << endl;
352
353 // Predict Parameters Values, Add White Noise
354 // ------------------------------------------
355 for (int iPar = 1; iPar <= _params.size(); iPar++) {
356 cmbParam* pp = _params[iPar-1];
357 if (pp->sig_P != 0.0) {
358 pp->xx = 0.0;
359 for (int jj = 1; jj <= _params.size(); jj++) {
360 _QQ(iPar, jj) = 0.0;
361 }
362 _QQ(iPar,iPar) = pp->sig_P * pp->sig_P;
363 }
364 }
365
366 bncTime resTime = epochs.first()->time;
367 QMap<QString, t_corr*> resCorr;
368
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 // ------------------
380 QListIterator<cmbEpoch*> itEpo(epochs);
381 while (itEpo.hasNext()) {
382 cmbEpoch* epo = itEpo.next();
383 QMutableMapIterator<QString, t_corr*> itCorr(epo->corr);
384 while (itCorr.hasNext()) {
385 itCorr.next();
386 t_corr* corr = itCorr.value();
387
388 // Switch to last ephemeris
389 // ------------------------
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 }
403 }
404 }
405
406 if (nObs > 0) {
407 const double Pl = 1.0 / (0.05 * 0.05);
408
409 const int nCon = (_firstReg == false) ? 1 + MAXPRN_GPS : 1;
410 Matrix AA(nObs+nCon, nPar); AA = 0.0;
411 ColumnVector ll(nObs+nCon); ll = 0.0;
412 DiagonalMatrix PP(nObs+nCon); PP = Pl;
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
425 if (epo->acName == _masterAC) {
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);
435 }
436
437 delete epo;
438 }
439
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];
447 if (pp->type == cmbParam::clk &&
448 AA.Column(iPar).maximum_absolute_value() > 0.0) {
449 AA(nObs+iCond, iPar) = 1.0;
450 }
451 }
452
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 }
464 }
465 }
466 }
467
468 const double MAXRES = 999.10; // TODO: make it an option
469
470 ColumnVector dx;
471 SymmetricMatrix QQ_sav = _QQ;
472
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
494 for (int iPar = 1; iPar <= _params.size(); iPar++) {
495 cmbParam* pp = _params[iPar-1];
496 pp->xx += dx(iPar);
497 if (pp->type == cmbParam::clk) {
498 if (resCorr.find(pp->prn) != resCorr.end()) {
499 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
500 }
501 }
502 out << currentDateAndTimeGPS().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
503 out.setRealNumberNotation(QTextStream::FixedNotation);
504 out.setFieldWidth(8);
505 out.setRealNumberPrecision(4);
506 out << pp->toString() << " "
507 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
508 }
509 }
510
511 printResults(out, resTime, resCorr);
512 dumpResults(resTime, resCorr);
513
514 emit newMessage(_log, false);
515}
516
517// Print results
518////////////////////////////////////////////////////////////////////////////
519void bncComb::printResults(QTextStream& out, const bncTime& resTime,
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();
526 const t_eph* eph = corr->eph;
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 }
540 }
541}
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.