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

Last change on this file since 3206 was 3206, 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
122 connect(this, SIGNAL(newMessage(QByteArray,bool)),
123 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
124
125
126 // A Priori Sigmas (in Meters)
127 // ---------------------------
128 double sigAC_0 = 100.0;
129 double sigAC_P = 100.0;
130 double sigSat_0 = 100.0;
131 double sigSat_P = 0.0;
132 double sigClk_0 = 100.0;
133 double sigClk_P = 100.0;
134
135 // Initialize Parameters (model: Clk_Corr = AC_Offset + Sat_Offset + Clk)
136 // ----------------------------------------------------------------------
137 int nextPar = 0;
138 QMapIterator<QString, cmbAC*> it(_ACs);
139 while (it.hasNext()) {
140 it.next();
141 cmbAC* AC = it.value();
142 _params.push_back(new cmbParam(cmbParam::AC_offset, ++nextPar,
143 AC->name, "", sigAC_0, sigAC_P));
144 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
145 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
146 _params.push_back(new cmbParam(cmbParam::Sat_offset, ++nextPar,
147 AC->name, prn, sigSat_0, sigSat_P));
148 }
149 }
150 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
151 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
152 _params.push_back(new cmbParam(cmbParam::clk, ++nextPar, "", prn,
153 sigClk_0, sigClk_P));
154 }
155
156 // Initialize Variance-Covariance Matrix
157 // -------------------------------------
158 _QQ.ReSize(_params.size());
159 _QQ = 0.0;
160 for (int iPar = 1; iPar <= _params.size(); iPar++) {
161 cmbParam* pp = _params[iPar-1];
162 _QQ(iPar,iPar) = pp->sig_0 * pp->sig_0;
163 }
164
165 // ANTEX File
166 // ----------
167 _antex = 0;
168 QString antexFileName = settings.value("pppAntex").toString();
169 if (!antexFileName.isEmpty()) {
170 _antex = new bncAntex();
171 if (_antex->readFile(antexFileName) != success) {
172 emit newMessage("wrong ANTEX file", true);
173 delete _antex;
174 _antex = 0;
175 }
176 }
177
178 // Not yet regularized
179 // -------------------
180 _firstReg = false;
181}
182
183// Destructor
184////////////////////////////////////////////////////////////////////////////
185bncComb::~bncComb() {
186 QMapIterator<QString, cmbAC*> it(_ACs);
187 while (it.hasNext()) {
188 it.next();
189 delete it.value();
190 }
191 delete _rtnetDecoder;
192 delete _antex;
193}
194
195// Read and store one correction line
196////////////////////////////////////////////////////////////////////////////
197void bncComb::processCorrLine(const QString& staID, const QString& line) {
198 QMutexLocker locker(&_mutex);
199
200 // Find the relevant instance of cmbAC class
201 // -----------------------------------------
202 if (_ACs.find(staID) == _ACs.end()) {
203 return;
204 }
205 cmbAC* AC = _ACs[staID];
206
207 // Read the Correction
208 // -------------------
209 t_corr* newCorr = new t_corr();
210 if (!newCorr->readLine(line) == success) {
211 delete newCorr;
212 return;
213 }
214
215 // Reject delayed corrections
216 // --------------------------
217 if (_processedBeforeTime.valid() && newCorr->tt < _processedBeforeTime) {
218 delete newCorr;
219 return;
220 }
221
222 // Check the Ephemeris
223 //--------------------
224 if (_eph.find(newCorr->prn) == _eph.end()) {
225 delete newCorr;
226 return;
227 }
228 else {
229 t_eph* lastEph = _eph[newCorr->prn]->last;
230 t_eph* prevEph = _eph[newCorr->prn]->prev;
231 if (lastEph && lastEph->IOD() == newCorr->iod) {
232 newCorr->eph = lastEph;
233 }
234 else if (prevEph && prevEph->IOD() == newCorr->iod) {
235 newCorr->eph = prevEph;
236 }
237 else {
238 delete newCorr;
239 return;
240 }
241 }
242
243 // Process all older Epochs (if there are any)
244 // -------------------------------------------
245 const double waitTime = 5.0; // wait 5 sec
246 _processedBeforeTime = newCorr->tt - waitTime;
247
248 QList<cmbEpoch*> epochsToProcess;
249
250 QMapIterator<QString, cmbAC*> itAC(_ACs);
251 while (itAC.hasNext()) {
252 itAC.next();
253 cmbAC* AC = itAC.value();
254
255 QMutableListIterator<cmbEpoch*> itEpo(AC->epochs);
256 while (itEpo.hasNext()) {
257 cmbEpoch* epoch = itEpo.next();
258 if (epoch->time < _processedBeforeTime) {
259 epochsToProcess.append(epoch);
260 itEpo.remove();
261 }
262 }
263 }
264
265 if (epochsToProcess.size()) {
266 processEpochs(epochsToProcess);
267 }
268
269 // Check Modulo Time
270 // -----------------
271 const int moduloTime = 10;
272 if (int(newCorr->tt.gpssec()) % moduloTime != 0.0) {
273 delete newCorr;
274 return;
275 }
276
277 // Find/Create the instance of cmbEpoch class
278 // ------------------------------------------
279 cmbEpoch* newEpoch = 0;
280 QListIterator<cmbEpoch*> it(AC->epochs);
281 while (it.hasNext()) {
282 cmbEpoch* hlpEpoch = it.next();
283 if (hlpEpoch->time == newCorr->tt) {
284 newEpoch = hlpEpoch;
285 break;
286 }
287 }
288 if (newEpoch == 0) {
289 newEpoch = new cmbEpoch(AC->name);
290 newEpoch->time = newCorr->tt;
291 AC->epochs.append(newEpoch);
292 }
293
294 // Merge or add the correction
295 // ---------------------------
296 if (newEpoch->corr.find(newCorr->prn) != newEpoch->corr.end()) {
297 newEpoch->corr[newCorr->prn]->readLine(line); // merge (multiple messages)
298 }
299 else {
300 newEpoch->corr[newCorr->prn] = newCorr;
301 }
302}
303
304// Change the correction so that it refers to last received ephemeris
305////////////////////////////////////////////////////////////////////////////
306void bncComb::switchToLastEph(const t_eph* lastEph, t_corr* corr) {
307
308 ColumnVector oldXC(4);
309 ColumnVector oldVV(3);
310 corr->eph->position(corr->tt.gpsw(), corr->tt.gpssec(),
311 oldXC.data(), oldVV.data());
312
313 ColumnVector newXC(4);
314 ColumnVector newVV(3);
315 lastEph->position(corr->tt.gpsw(), corr->tt.gpssec(),
316 newXC.data(), newVV.data());
317
318 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
319 ColumnVector dV = newVV - oldVV;
320 double dC = newXC(4) - oldXC(4);
321
322 ColumnVector dRAO(3);
323 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
324
325 ColumnVector dDotRAO(3);
326 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
327
328 QString msg = "switch " + corr->prn
329 + QString(" %1 -> %2 %3").arg(corr->iod,3)
330 .arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
331
332 emit newMessage(msg.toAscii(), false);
333
334 corr->iod = lastEph->IOD();
335 corr->eph = lastEph;
336 corr->rao += dRAO;
337 corr->dotRao += dDotRAO;
338 corr->dClk -= dC;
339}
340
341// Process Epochs
342////////////////////////////////////////////////////////////////////////////
343void bncComb::processEpochs(const QList<cmbEpoch*>& epochs) {
344
345 _log.clear();
346
347 QTextStream out(&_log, QIODevice::WriteOnly);
348
349 out << "Combination:" << endl
350 << "------------------------------" << endl;
351
352 // Predict Parameters Values, Add White Noise
353 // ------------------------------------------
354 for (int iPar = 1; iPar <= _params.size(); iPar++) {
355 cmbParam* pp = _params[iPar-1];
356 if (pp->sig_P != 0.0) {
357 pp->xx = 0.0;
358 for (int jj = 1; jj <= _params.size(); jj++) {
359 _QQ(iPar, jj) = 0.0;
360 }
361 _QQ(iPar,iPar) = pp->sig_P * pp->sig_P;
362 }
363 }
364
365 bncTime resTime = epochs.first()->time;
366 QMap<QString, t_corr*> resCorr;
367
368 int nPar = _params.size();
369 int nObs = 0;
370
371 ColumnVector x0(nPar);
372 for (int iPar = 1; iPar <= _params.size(); iPar++) {
373 cmbParam* pp = _params[iPar-1];
374 x0(iPar) = pp->xx;
375 }
376
377 // Count Observations
378 // ------------------
379 QListIterator<cmbEpoch*> itEpo(epochs);
380 while (itEpo.hasNext()) {
381 cmbEpoch* epo = itEpo.next();
382 QMutableMapIterator<QString, t_corr*> itCorr(epo->corr);
383 while (itCorr.hasNext()) {
384 itCorr.next();
385 t_corr* corr = itCorr.value();
386
387 // Switch to last ephemeris
388 // ------------------------
389 t_eph* lastEph = _eph[corr->prn]->last;
390 if (lastEph == corr->eph) {
391 ++nObs;
392 }
393 else {
394 if (corr->eph == _eph[corr->prn]->prev) {
395 switchToLastEph(lastEph, corr);
396 ++nObs;
397 }
398 else {
399 itCorr.remove();
400 }
401 }
402 }
403 }
404
405 if (nObs > 0) {
406 const double Pl = 1.0 / (0.05 * 0.05);
407
408 const int nCon = (_firstReg == false) ? 1 + MAXPRN_GPS : 1;
409 Matrix AA(nObs+nCon, nPar); AA = 0.0;
410 ColumnVector ll(nObs+nCon); ll = 0.0;
411 DiagonalMatrix PP(nObs+nCon); PP = Pl;
412
413 int iObs = 0;
414 QListIterator<cmbEpoch*> itEpo(epochs);
415 while (itEpo.hasNext()) {
416 cmbEpoch* epo = itEpo.next();
417 QMapIterator<QString, t_corr*> itCorr(epo->corr);
418
419 while (itCorr.hasNext()) {
420 itCorr.next();
421 ++iObs;
422 t_corr* corr = itCorr.value();
423
424 if (epo->acName == _masterAC) {
425 resCorr[corr->prn] = new t_corr(*corr);
426 }
427
428 for (int iPar = 1; iPar <= _params.size(); iPar++) {
429 cmbParam* pp = _params[iPar-1];
430 AA(iObs, iPar) = pp->partial(epo->acName, corr);
431 }
432
433 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
434 }
435
436 delete epo;
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::clk &&
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::Sat_offset && pp->prn == prn) {
461 AA(nObs+iCond, iPar) = 1.0;
462 }
463 }
464 }
465 }
466
467 const double MAXRES = 999.10; // TODO: make it an option
468
469 ColumnVector dx;
470 SymmetricMatrix QQ_sav = _QQ;
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 << "Maximum Residuum " << maxRes << " (index " << maxResIndex << ")\n";
481
482 if (maxRes > MAXRES) {
483 out << "Outlier Detected" << endl;
484 _QQ = QQ_sav;
485 AA.Row(maxResIndex) = 0.0;
486 ll.Row(maxResIndex) = 0.0;
487 }
488 else {
489 break;
490 }
491 }
492
493 for (int iPar = 1; iPar <= _params.size(); iPar++) {
494 cmbParam* pp = _params[iPar-1];
495 pp->xx += dx(iPar);
496 if (pp->type == cmbParam::clk) {
497 if (resCorr.find(pp->prn) != resCorr.end()) {
498 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
499 }
500 }
501 out << currentDateAndTimeGPS().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
502 out.setRealNumberNotation(QTextStream::FixedNotation);
503 out.setFieldWidth(8);
504 out.setRealNumberPrecision(4);
505 out << pp->toString() << " "
506 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
507 }
508 }
509
510 printResults(out, resTime, resCorr);
511 dumpResults(resTime, resCorr);
512
513 emit newMessage(_log, false);
514}
515
516// Print results
517////////////////////////////////////////////////////////////////////////////
518void bncComb::printResults(QTextStream& out, const bncTime& resTime,
519 const QMap<QString, t_corr*>& resCorr) {
520
521 QMapIterator<QString, t_corr*> it(resCorr);
522 while (it.hasNext()) {
523 it.next();
524 t_corr* corr = it.value();
525 const t_eph* eph = corr->eph;
526 if (eph) {
527 double xx, yy, zz, cc;
528 eph->position(resTime.gpsw(), resTime.gpssec(), xx, yy, zz, cc);
529
530 out << currentDateAndTimeGPS().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
531 out.setFieldWidth(3);
532 out << "Full Clock " << corr->prn << " " << corr->iod << " ";
533 out.setFieldWidth(14);
534 out << (cc + corr->dClk) * t_CST::c << endl;
535 }
536 else {
537 out << "bncComb::printResuls bug" << endl;
538 }
539 }
540}
541
542// Send results to RTNet Decoder and directly to PPP Client
543////////////////////////////////////////////////////////////////////////////
544void bncComb::dumpResults(const bncTime& resTime,
545 const QMap<QString, t_corr*>& resCorr) {
546
547 QMapIterator<QString, t_corr*> it(resCorr);
548 while (it.hasNext()) {
549 it.next();
550 t_corr* corr = it.value();
551
552// // SP3 Output
553// // ----------
554// if (_sp3) {
555// ColumnVector xc(4);
556// ColumnVector vv(3);
557// corr->eph->position(resTime.gpsw(), resTime.gpssec(),
558// xc.data(), vv.data());
559// bncPPPclient::applyCorr(resTime, corr, xc, vv);
560//
561// // Relativistic Correction
562// // -----------------------
563// xc(4) += 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c;
564//
565// // Correction Phase Center --> CoM
566// // -------------------------------
567// if (_antex) {
568// ColumnVector dx(3); dx = 0.0;
569// double Mjd = resTime.mjd() + resTime.daysec()/86400.0;
570// if (_antex->satCoMcorrection(corr->prn, Mjd, xc.Rows(1,3), dx) == success) {
571// xc(1) -= dx(1);
572// xc(2) -= dx(2);
573// xc(3) -= dx(3);
574// }
575// else {
576// cout << "antenna not found" << endl;
577// }
578// }
579// _sp3->write(resTime.gpsw(), resTime.gpssec(), corr->prn, xc);
580// }
581//
582// delete corr;
583// }
584
585 // Optionally send new Corrections to PPP
586 // --------------------------------------
587 bncApp* app = (bncApp*) qApp;
588 if (app->_bncPPPclient) {
589// QStringList corrLines;
590// co.messageType = COTYPE_GPSCOMBINED;
591// QStringListIterator il(RTCM3coDecoder::corrsToASCIIlines(resTime.gpsw(),
592// resTime.gpssec(), co, 0));
593// while (il.hasNext()) {
594// QString line = il.next();
595// line += " COMB";
596// corrLines << line;
597// }
598//
599// app->_bncPPPclient->slotNewCorrections(corrLines);
600 }
601 }
602}
Note: See TracBrowser for help on using the repository browser.