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

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