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

Last change on this file since 3262 was 3262, checked in by mervart, 13 years ago
File size: 18.6 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(cmbParam::parType type_, int index_,
39 const QString& ac_, const QString& prn_,
40 double sig_0_, double sig_P_) {
41
42 type = type_;
43 index = index_;
44 AC = ac_;
45 prn = prn_;
46 sig_0 = sig_0_;
47 sig_P = sig_P_;
48 xx = 0.0;
49}
50
51// Destructor
52////////////////////////////////////////////////////////////////////////////
53cmbParam::~cmbParam() {
54}
55
56// Partial
57////////////////////////////////////////////////////////////////////////////
58double cmbParam::partial(const QString& AC_, t_corr* corr) {
59
60 if (type == AC_offset) {
61 if (AC == AC_) {
62 return 1.0;
63 }
64 }
65 else if (type == Sat_offset) {
66 if (AC == AC_ && prn == corr->prn) {
67 return 1.0;
68 }
69 }
70 else if (type == clk) {
71 if (prn == corr->prn) {
72 return 1.0;
73 }
74 }
75
76 return 0.0;
77}
78
79//
80////////////////////////////////////////////////////////////////////////////
81QString cmbParam::toString() const {
82
83 QString outStr;
84
85 if (type == AC_offset) {
86 outStr = "AC offset " + AC;
87 }
88 else if (type == Sat_offset) {
89 outStr = "Sat Offset " + AC + " " + prn;
90 }
91 else if (type == clk) {
92 outStr = "Clk Corr " + prn;
93 }
94
95 return outStr;
96}
97
98// Constructor
99////////////////////////////////////////////////////////////////////////////
100bncComb::bncComb() {
101
102 bncSettings settings;
103
104 QStringList combineStreams = settings.value("combineStreams").toStringList();
105
106 if (combineStreams.size() >= 1 && !combineStreams[0].isEmpty()) {
107 QListIterator<QString> it(combineStreams);
108 while (it.hasNext()) {
109 QStringList hlp = it.next().split(" ");
110 cmbAC* newAC = new cmbAC();
111 newAC->mountPoint = hlp[0];
112 newAC->name = hlp[1];
113 newAC->weight = hlp[2].toDouble();
114 if (_masterAC.isEmpty()) {
115 _masterAC = newAC->name;
116 }
117 _ACs[newAC->mountPoint] = newAC;
118 }
119 }
120
121 _rtnetDecoder = 0;
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 ostringstream out; out.setf(std::ios::fixed);
549 QStringList corrLines;
550
551 unsigned year, month, day, hour, minute;
552 double sec;
553 resTime.civil_date(year, month, day);
554 resTime.civil_time(hour, minute, sec);
555
556 out << "* "
557 << setw(4) << year << " "
558 << setw(2) << month << " "
559 << setw(2) << day << " "
560 << setw(2) << hour << " "
561 << setw(2) << minute << " "
562 << setw(12) << setprecision(8) << sec << " "
563 << endl;
564
565 QMapIterator<QString, t_corr*> it(resCorr);
566 while (it.hasNext()) {
567 it.next();
568 t_corr* corr = it.value();
569
570 double dT = 60.0;
571
572 for (int iTime = 1; iTime <= 2; iTime++) {
573
574 bncTime time12 = (iTime == 1) ? resTime : resTime + dT;
575
576 ColumnVector xc(4);
577 ColumnVector vv(3);
578 corr->eph->position(time12.gpsw(), time12.gpssec(),
579 xc.data(), vv.data());
580 bncPPPclient::applyCorr(time12, corr, xc, vv);
581
582 // Relativistic Correction
583 // -----------------------
584 double relCorr = - 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c;
585 xc(4) -= relCorr;
586
587 // Code Biases
588 // -----------
589 double dcbP1C1 = 0.0;
590 double dcbP1P2 = 0.0;
591
592 // Correction Phase Center --> CoM
593 // -------------------------------
594 ColumnVector dx(3); dx = 0.0;
595 if (_antex) {
596 double Mjd = time12.mjd() + time12.daysec()/86400.0;
597 if (_antex->satCoMcorrection(corr->prn, Mjd, xc.Rows(1,3), dx) == success) {
598 xc(1) -= dx(1);
599 xc(2) -= dx(2);
600 xc(3) -= dx(3);
601 }
602 else {
603 cout << "antenna not found" << endl;
604 }
605 }
606
607 if (iTime == 1) {
608 out << 'P' << corr->prn.toAscii().data()
609 << setw(14) << setprecision(6) << xc(1) / 1000.0
610 << setw(14) << setprecision(6) << xc(2) / 1000.0
611 << setw(14) << setprecision(6) << xc(3) / 1000.0
612 << setw(14) << setprecision(6) << xc(4) * 1e6
613 << setw(14) << setprecision(6) << relCorr * 1e6
614 << setw(8) << setprecision(3) << dx(1)
615 << setw(8) << setprecision(3) << dx(2)
616 << setw(8) << setprecision(3) << dx(3)
617 << setw(8) << setprecision(3) << dcbP1C1
618 << setw(8) << setprecision(3) << dcbP1P2
619 << setw(6) << setprecision(1) << dT;
620
621 QString line;
622 int messageType = COTYPE_GPSCOMBINED;
623 int updateInt = 0;
624 line.sprintf("%d %d %d %.1f %s"
625 " %3d"
626 " %8.3f %8.3f %8.3f %8.3f"
627 " %10.5f %10.5f %10.5f %10.5f"
628 " %10.5f %10.5f %10.5f %10.5f INTERNAL",
629 messageType, updateInt, time12.gpsw(), time12.gpssec(),
630 corr->prn.toAscii().data(),
631 corr->iod,
632 corr->dClk * t_CST::c,
633 corr->rao[0],
634 corr->rao[1],
635 corr->rao[2],
636 corr->dotDClk * t_CST::c,
637 corr->dotRao[0],
638 corr->dotRao[1],
639 corr->dotRao[2],
640 corr->dotDotDClk * t_CST::c,
641 corr->dotDotRao[0],
642 corr->dotDotRao[1],
643 corr->dotDotRao[2]);
644 corrLines << line;
645 }
646 else {
647 out << setw(14) << setprecision(6) << xc(1) / 1000.0
648 << setw(14) << setprecision(6) << xc(2) / 1000.0
649 << setw(14) << setprecision(6) << xc(3) / 1000.0 << endl;
650 }
651 }
652
653 delete corr;
654 }
655 out << "EOE" << endl; // End Of Epoch flag
656
657 if (!_rtnetDecoder) {
658 _rtnetDecoder = new bncRtnetDecoder();
659 }
660
661 vector<string> errmsg;
662 _rtnetDecoder->Decode((char*) out.str().data(), out.str().size(), errmsg);
663
664 // Optionally send new Corrections to PPP
665 // --------------------------------------
666 bncApp* app = (bncApp*) qApp;
667 if (app->_bncPPPclient) {
668 app->_bncPPPclient->slotNewCorrections(corrLines);
669 }
670}
Note: See TracBrowser for help on using the repository browser.