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

Last change on this file since 3329 was 3329, checked in by mervart, 13 years ago
File size: 19.7 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 // Maximal Residuum
184 // ----------------
185 _MAXRES = settings.value("cmbMaxres").toDouble();
186 if (_MAXRES <= 0.0) {
187 _MAXRES = 999.0;
188 }
189}
190
191// Destructor
192////////////////////////////////////////////////////////////////////////////
193bncComb::~bncComb() {
194 QMapIterator<QString, cmbAC*> it(_ACs);
195 while (it.hasNext()) {
196 it.next();
197 delete it.value();
198 }
199 delete _rtnetDecoder;
200 delete _antex;
201 for (int iPar = 1; iPar <= _params.size(); iPar++) {
202 delete _params[iPar-1];
203 }
204}
205
206// Read and store one correction line
207////////////////////////////////////////////////////////////////////////////
208void bncComb::processCorrLine(const QString& staID, const QString& line) {
209 QMutexLocker locker(&_mutex);
210
211 // Find the relevant instance of cmbAC class
212 // -----------------------------------------
213 if (_ACs.find(staID) == _ACs.end()) {
214 return;
215 }
216 cmbAC* AC = _ACs[staID];
217
218 // Read the Correction
219 // -------------------
220 t_corr* newCorr = new t_corr();
221 if (!newCorr->readLine(line) == success) {
222 delete newCorr;
223 return;
224 }
225
226 // Check Modulo Time
227 // -----------------
228 const int moduloTime = 10;
229 if (int(newCorr->tt.gpssec()) % moduloTime != 0.0) {
230 delete newCorr;
231 return;
232 }
233
234 // Remember last correction time
235 // -----------------------------
236 if (!_lastCorrTime.valid() || _lastCorrTime < newCorr->tt) {
237 _lastCorrTime = newCorr->tt;
238 }
239
240 bncTime processTime = _lastCorrTime - 1.5 * moduloTime;
241
242 // Delete old corrections
243 // ----------------------
244 if (newCorr->tt < processTime) {
245 delete newCorr;
246 return;
247 }
248
249 // Check the Ephemeris
250 //--------------------
251 if (_eph.find(newCorr->prn) == _eph.end()) {
252 delete newCorr;
253 return;
254 }
255 else {
256 t_eph* lastEph = _eph[newCorr->prn]->last;
257 t_eph* prevEph = _eph[newCorr->prn]->prev;
258 if (lastEph && lastEph->IOD() == newCorr->iod) {
259 newCorr->eph = lastEph;
260 }
261 else if (prevEph && prevEph->IOD() == newCorr->iod) {
262 newCorr->eph = prevEph;
263 }
264 else {
265 delete newCorr;
266 return;
267 }
268 }
269
270 // Process all older Epochs (if there are any)
271 // -------------------------------------------
272 QList<cmbEpoch*> epochsToProcess;
273 QMapIterator<QString, cmbAC*> itAC(_ACs);
274 while (itAC.hasNext()) {
275 itAC.next();
276 cmbAC* AC = itAC.value();
277 QMutableListIterator<cmbEpoch*> itEpo(AC->epochs);
278 while (itEpo.hasNext()) {
279 cmbEpoch* epoch = itEpo.next();
280 if (epoch->time <= processTime) {
281 epochsToProcess.append(epoch);
282 itEpo.remove();
283 }
284 }
285 }
286
287 if (epochsToProcess.size()) {
288 processEpochs(epochsToProcess);
289 }
290
291 // Find/Create the instance of cmbEpoch class
292 // ------------------------------------------
293 cmbEpoch* newEpoch = 0;
294 QListIterator<cmbEpoch*> it(AC->epochs);
295 while (it.hasNext()) {
296 cmbEpoch* hlpEpoch = it.next();
297 if (hlpEpoch->time == newCorr->tt) {
298 newEpoch = hlpEpoch;
299 break;
300 }
301 }
302 if (newEpoch == 0) {
303 newEpoch = new cmbEpoch(AC->name);
304 newEpoch->time = newCorr->tt;
305 AC->epochs.append(newEpoch);
306 }
307
308 // Merge or add the correction
309 // ---------------------------
310 if (newEpoch->corr.find(newCorr->prn) != newEpoch->corr.end()) {
311 newEpoch->corr[newCorr->prn]->readLine(line); // merge (multiple messages)
312 delete newCorr;
313 }
314 else {
315 newEpoch->corr[newCorr->prn] = newCorr;
316 }
317}
318
319// Change the correction so that it refers to last received ephemeris
320////////////////////////////////////////////////////////////////////////////
321void bncComb::switchToLastEph(const t_eph* lastEph, t_corr* corr) {
322
323 ColumnVector oldXC(4);
324 ColumnVector oldVV(3);
325 corr->eph->position(corr->tt.gpsw(), corr->tt.gpssec(),
326 oldXC.data(), oldVV.data());
327
328 ColumnVector newXC(4);
329 ColumnVector newVV(3);
330 lastEph->position(corr->tt.gpsw(), corr->tt.gpssec(),
331 newXC.data(), newVV.data());
332
333 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
334 ColumnVector dV = newVV - oldVV;
335 double dC = newXC(4) - oldXC(4);
336
337 ColumnVector dRAO(3);
338 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
339
340 ColumnVector dDotRAO(3);
341 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
342
343 QString msg = "switch " + corr->prn
344 + QString(" %1 -> %2 %3").arg(corr->iod,3)
345 .arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
346
347 emit newMessage(msg.toAscii(), false);
348
349 corr->iod = lastEph->IOD();
350 corr->eph = lastEph;
351 corr->rao += dRAO;
352 corr->dotRao += dDotRAO;
353 corr->dClk -= dC;
354}
355
356// Process Epochs
357////////////////////////////////////////////////////////////////////////////
358void bncComb::processEpochs(const QList<cmbEpoch*>& epochs) {
359
360 _log.clear();
361
362 QTextStream out(&_log, QIODevice::WriteOnly);
363
364 out << "Combination:" << endl
365 << "------------------------------" << endl;
366
367 // Check whether master AC present
368 // -------------------------------
369 if (epochs.first()->acName != _masterAC) {
370 QListIterator<cmbEpoch*> itEpo(epochs);
371 while (itEpo.hasNext()) {
372 cmbEpoch* epo = itEpo.next();
373 bncTime epoTime = epo->time;
374 out << epo->acName.toAscii().data() << " "
375 << epoTime.datestr().c_str() << " "
376 << epoTime.timestr().c_str() << endl;
377 delete epo;
378 }
379 out << "Missing Master AC" << endl;
380 emit newMessage(_log, false);
381 return;
382 }
383
384 // Predict Parameters Values, Add White Noise
385 // ------------------------------------------
386 for (int iPar = 1; iPar <= _params.size(); iPar++) {
387 cmbParam* pp = _params[iPar-1];
388 if (pp->sig_P != 0.0) {
389 pp->xx = 0.0;
390 for (int jj = 1; jj <= _params.size(); jj++) {
391 _QQ(iPar, jj) = 0.0;
392 }
393 _QQ(iPar,iPar) = pp->sig_P * pp->sig_P;
394 }
395 }
396
397 bncTime resTime = epochs.first()->time;
398 QMap<QString, t_corr*> resCorr;
399
400 int nPar = _params.size();
401 int nObs = 0;
402
403 ColumnVector x0(nPar);
404 for (int iPar = 1; iPar <= _params.size(); iPar++) {
405 cmbParam* pp = _params[iPar-1];
406 x0(iPar) = pp->xx;
407 }
408
409 // Count Observations
410 // ------------------
411 QListIterator<cmbEpoch*> itEpo(epochs);
412 while (itEpo.hasNext()) {
413 cmbEpoch* epo = itEpo.next();
414 bncTime epoTime = epo->time;
415 out << epo->acName.toAscii().data() << " "
416 << epoTime.datestr().c_str() << " "
417 << epoTime.timestr().c_str() << endl;
418
419 QMutableMapIterator<QString, t_corr*> itCorr(epo->corr);
420 while (itCorr.hasNext()) {
421 itCorr.next();
422 t_corr* corr = itCorr.value();
423
424 // Switch to last ephemeris
425 // ------------------------
426 t_eph* lastEph = _eph[corr->prn]->last;
427 if (lastEph == corr->eph) {
428 ++nObs;
429 }
430 else {
431 if (corr->eph == _eph[corr->prn]->prev) {
432 switchToLastEph(lastEph, corr);
433 ++nObs;
434 }
435 else {
436 itCorr.remove();
437 }
438 }
439 }
440 }
441
442 if (nObs > 0) {
443 const double Pl = 1.0 / (0.05 * 0.05);
444
445 const int nCon = (_firstReg == false) ? 1 + MAXPRN_GPS : 1;
446 Matrix AA(nObs+nCon, nPar); AA = 0.0;
447 ColumnVector ll(nObs+nCon); ll = 0.0;
448 DiagonalMatrix PP(nObs+nCon); PP = Pl;
449
450 int iObs = 0;
451 QListIterator<cmbEpoch*> itEpo(epochs);
452 while (itEpo.hasNext()) {
453 cmbEpoch* epo = itEpo.next();
454 QMapIterator<QString, t_corr*> itCorr(epo->corr);
455
456 while (itCorr.hasNext()) {
457 itCorr.next();
458 ++iObs;
459 t_corr* corr = itCorr.value();
460
461 if (epo->acName == _masterAC) {
462 resCorr[corr->prn] = new t_corr(*corr);
463 }
464
465 for (int iPar = 1; iPar <= _params.size(); iPar++) {
466 cmbParam* pp = _params[iPar-1];
467 AA(iObs, iPar) = pp->partial(epo->acName, corr);
468 }
469
470 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
471 }
472 }
473
474 // Regularization
475 // --------------
476 const double Ph = 1.e6;
477 int iCond = 1;
478 PP(nObs+iCond) = Ph;
479 for (int iPar = 1; iPar <= _params.size(); iPar++) {
480 cmbParam* pp = _params[iPar-1];
481 if (pp->type == cmbParam::clk &&
482 AA.Column(iPar).maximum_absolute_value() > 0.0) {
483 AA(nObs+iCond, iPar) = 1.0;
484 }
485 }
486
487 if (!_firstReg) {
488 _firstReg = true;
489 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
490 ++iCond;
491 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
492 PP(nObs+1+iGps) = Ph;
493 for (int iPar = 1; iPar <= _params.size(); iPar++) {
494 cmbParam* pp = _params[iPar-1];
495 if (pp->type == cmbParam::Sat_offset && pp->prn == prn) {
496 AA(nObs+iCond, iPar) = 1.0;
497 }
498 }
499 }
500 }
501
502 ColumnVector dx;
503 SymmetricMatrix QQ_sav = _QQ;
504
505 for (int ii = 1; ii < 10; ii++) {
506 bncModel::kalman(AA, ll, PP, _QQ, dx);
507 ColumnVector vv = ll - AA * dx;
508
509 int maxResIndex;
510 double maxRes = vv.maximum_absolute_value1(maxResIndex);
511 out.setRealNumberNotation(QTextStream::FixedNotation);
512 out.setRealNumberPrecision(3);
513 out << "Maximum Residuum " << maxRes << " (index " << maxResIndex << ")\n";
514
515 if (maxRes > _MAXRES) {
516 out << "Outlier Detected" << endl;
517 _QQ = QQ_sav;
518 AA.Row(maxResIndex) = 0.0;
519 ll.Row(maxResIndex) = 0.0;
520 }
521 else {
522 break;
523 }
524 }
525
526 for (int iPar = 1; iPar <= _params.size(); iPar++) {
527 cmbParam* pp = _params[iPar-1];
528 pp->xx += dx(iPar);
529 if (pp->type == cmbParam::clk) {
530 if (resCorr.find(pp->prn) != resCorr.end()) {
531 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
532 }
533 }
534 out << currentDateAndTimeGPS().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
535 out.setRealNumberNotation(QTextStream::FixedNotation);
536 out.setFieldWidth(8);
537 out.setRealNumberPrecision(4);
538 out << pp->toString() << " "
539 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
540 }
541 }
542
543 printResults(out, resTime, resCorr);
544 dumpResults(resTime, resCorr);
545
546 emit newMessage(_log, false);
547
548 QListIterator<cmbEpoch*> itEpo2(epochs);
549 while (itEpo2.hasNext()) {
550 cmbEpoch* epo = itEpo2.next();
551 delete epo;
552 }
553}
554
555// Print results
556////////////////////////////////////////////////////////////////////////////
557void bncComb::printResults(QTextStream& out, const bncTime& resTime,
558 const QMap<QString, t_corr*>& resCorr) {
559
560 QMapIterator<QString, t_corr*> it(resCorr);
561 while (it.hasNext()) {
562 it.next();
563 t_corr* corr = it.value();
564 const t_eph* eph = corr->eph;
565 if (eph) {
566 double xx, yy, zz, cc;
567 eph->position(resTime.gpsw(), resTime.gpssec(), xx, yy, zz, cc);
568
569 out << currentDateAndTimeGPS().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
570 out.setFieldWidth(3);
571 out << "Full Clock " << corr->prn << " " << corr->iod << " ";
572 out.setFieldWidth(14);
573 out << (cc + corr->dClk) * t_CST::c << endl;
574 }
575 else {
576 out << "bncComb::printResuls bug" << endl;
577 }
578 }
579}
580
581// Send results to RTNet Decoder and directly to PPP Client
582////////////////////////////////////////////////////////////////////////////
583void bncComb::dumpResults(const bncTime& resTime,
584 const QMap<QString, t_corr*>& resCorr) {
585
586 ostringstream out; out.setf(std::ios::fixed);
587 QStringList corrLines;
588
589 unsigned year, month, day, hour, minute;
590 double sec;
591 resTime.civil_date(year, month, day);
592 resTime.civil_time(hour, minute, sec);
593
594 out << "* "
595 << setw(4) << year << " "
596 << setw(2) << month << " "
597 << setw(2) << day << " "
598 << setw(2) << hour << " "
599 << setw(2) << minute << " "
600 << setw(12) << setprecision(8) << sec << " "
601 << endl;
602
603 QMapIterator<QString, t_corr*> it(resCorr);
604 while (it.hasNext()) {
605 it.next();
606 t_corr* corr = it.value();
607
608 double dT = 60.0;
609
610 for (int iTime = 1; iTime <= 2; iTime++) {
611
612 bncTime time12 = (iTime == 1) ? resTime : resTime + dT;
613
614 ColumnVector xc(4);
615 ColumnVector vv(3);
616 corr->eph->position(time12.gpsw(), time12.gpssec(),
617 xc.data(), vv.data());
618 bncPPPclient::applyCorr(time12, corr, xc, vv);
619
620 // Relativistic Correction
621 // -----------------------
622 double relCorr = - 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c;
623 xc(4) -= relCorr;
624
625 // Code Biases
626 // -----------
627 double dcbP1C1 = 0.0;
628 double dcbP1P2 = 0.0;
629
630 // Correction Phase Center --> CoM
631 // -------------------------------
632 ColumnVector dx(3); dx = 0.0;
633 if (_antex) {
634 double Mjd = time12.mjd() + time12.daysec()/86400.0;
635 if (_antex->satCoMcorrection(corr->prn, Mjd, xc.Rows(1,3), dx) == success) {
636 xc(1) -= dx(1);
637 xc(2) -= dx(2);
638 xc(3) -= dx(3);
639 }
640 else {
641 cout << "antenna not found" << endl;
642 }
643 }
644
645 if (iTime == 1) {
646 out << 'P' << corr->prn.toAscii().data()
647 << 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
650 << setw(14) << setprecision(6) << xc(4) * 1e6
651 << setw(14) << setprecision(6) << relCorr * 1e6
652 << setw(8) << setprecision(3) << dx(1)
653 << setw(8) << setprecision(3) << dx(2)
654 << setw(8) << setprecision(3) << dx(3)
655 << setw(8) << setprecision(3) << dcbP1C1
656 << setw(8) << setprecision(3) << dcbP1P2
657 << setw(6) << setprecision(1) << dT;
658
659 QString line;
660 int messageType = COTYPE_GPSCOMBINED;
661 int updateInt = 0;
662 line.sprintf("%d %d %d %.1f %s"
663 " %3d"
664 " %8.3f %8.3f %8.3f %8.3f"
665 " %10.5f %10.5f %10.5f %10.5f"
666 " %10.5f %10.5f %10.5f %10.5f INTERNAL",
667 messageType, updateInt, time12.gpsw(), time12.gpssec(),
668 corr->prn.toAscii().data(),
669 corr->iod,
670 corr->dClk * t_CST::c,
671 corr->rao[0],
672 corr->rao[1],
673 corr->rao[2],
674 corr->dotDClk * t_CST::c,
675 corr->dotRao[0],
676 corr->dotRao[1],
677 corr->dotRao[2],
678 corr->dotDotDClk * t_CST::c,
679 corr->dotDotRao[0],
680 corr->dotDotRao[1],
681 corr->dotDotRao[2]);
682 corrLines << line;
683 }
684 else {
685 out << setw(14) << setprecision(6) << xc(1) / 1000.0
686 << setw(14) << setprecision(6) << xc(2) / 1000.0
687 << setw(14) << setprecision(6) << xc(3) / 1000.0 << endl;
688 }
689 }
690
691 delete corr;
692 }
693 out << "EOE" << endl; // End Of Epoch flag
694
695 if (!_rtnetDecoder) {
696 _rtnetDecoder = new bncRtnetDecoder();
697 }
698
699 vector<string> errmsg;
700 _rtnetDecoder->Decode((char*) out.str().data(), out.str().size(), errmsg);
701
702 // Optionally send new Corrections to PPP
703 // --------------------------------------
704 bncApp* app = (bncApp*) qApp;
705 if (app->_bncPPPclient) {
706 app->_bncPPPclient->slotNewCorrections(corrLines);
707 }
708}
Note: See TracBrowser for help on using the repository browser.