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

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