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

Last change on this file since 3454 was 3454, 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(parType type_, int index_,
39 const QString& ac_, const QString& prn_) {
40
41 type = type_;
42 index = index_;
43 AC = ac_;
44 prn = prn_;
45 xx = 0.0;
46
47 if (type == offAC) {
48 sig_0 = 1000.0;
49 sig_P = 1000.0;
50 }
51 else if (type == offACSat) {
52 sig_0 = 100.0;
53 sig_P = 0.0;
54 }
55 else if (type == clkSat) {
56 sig_0 = 100.0;
57 sig_P = 100.0;
58 }
59}
60
61// Destructor
62////////////////////////////////////////////////////////////////////////////
63cmbParam::~cmbParam() {
64}
65
66// Partial
67////////////////////////////////////////////////////////////////////////////
68double cmbParam::partial(const QString& AC_, const QString& prn_) {
69
70 if (type == offAC) {
71 if (AC == AC_) {
72 return 1.0;
73 }
74 }
75 else if (type == offACSat) {
76 if (AC == AC_ && prn == prn_) {
77 return 1.0;
78 }
79 }
80 else if (type == clkSat) {
81 if (prn == prn_) {
82 return 1.0;
83 }
84 }
85
86 return 0.0;
87}
88
89//
90////////////////////////////////////////////////////////////////////////////
91QString cmbParam::toString() const {
92
93 QString outStr;
94
95 if (type == offAC) {
96 outStr = "AC offset " + AC;
97 }
98 else if (type == offACSat) {
99 outStr = "Sat Offset " + AC + " " + prn;
100 }
101 else if (type == clkSat) {
102 outStr = "Clk Corr " + prn;
103 }
104
105 return outStr;
106}
107
108// Constructor
109////////////////////////////////////////////////////////////////////////////
110bncComb::bncComb() {
111
112 bncSettings settings;
113
114 QStringList combineStreams = settings.value("combineStreams").toStringList();
115
116 if (combineStreams.size() >= 1 && !combineStreams[0].isEmpty()) {
117 QListIterator<QString> it(combineStreams);
118 while (it.hasNext()) {
119 QStringList hlp = it.next().split(" ");
120 cmbAC* newAC = new cmbAC();
121 newAC->mountPoint = hlp[0];
122 newAC->name = hlp[1];
123 newAC->weight = hlp[2].toDouble();
124 if (_masterOrbitAC.isEmpty()) {
125 _masterOrbitAC = newAC->name;
126 }
127 _ACs.append(newAC);
128 }
129 }
130
131 _rtnetDecoder = 0;
132
133 connect(this, SIGNAL(newMessage(QByteArray,bool)),
134 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
135
136
137 // Initialize Parameters (model: Clk_Corr = AC_Offset + Sat_Offset + Clk)
138 // ----------------------------------------------------------------------
139 int nextPar = 0;
140 QListIterator<cmbAC*> it(_ACs);
141 while (it.hasNext()) {
142 cmbAC* AC = it.next();
143 _params.push_back(new cmbParam(cmbParam::offAC, ++nextPar, AC->name, ""));
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::offACSat, ++nextPar,
147 AC->name, prn));
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::clkSat, ++nextPar, "", prn));
153 }
154
155 // Initialize Variance-Covariance Matrix
156 // -------------------------------------
157 _QQ.ReSize(_params.size());
158 _QQ = 0.0;
159 for (int iPar = 1; iPar <= _params.size(); iPar++) {
160 cmbParam* pp = _params[iPar-1];
161 _QQ(iPar,iPar) = pp->sig_0 * pp->sig_0;
162 }
163
164 // ANTEX File
165 // ----------
166 _antex = 0;
167 QString antexFileName = settings.value("pppAntex").toString();
168 if (!antexFileName.isEmpty()) {
169 _antex = new bncAntex();
170 if (_antex->readFile(antexFileName) != success) {
171 emit newMessage("wrong ANTEX file", true);
172 delete _antex;
173 _antex = 0;
174 }
175 }
176
177 // Not yet regularized
178 // -------------------
179 _firstReg = false;
180
181 // Maximal Residuum
182 // ----------------
183 _MAXRES = settings.value("cmbMaxres").toDouble();
184 if (_MAXRES <= 0.0) {
185 _MAXRES = 999.0;
186 }
187}
188
189// Destructor
190////////////////////////////////////////////////////////////////////////////
191bncComb::~bncComb() {
192 QListIterator<cmbAC*> icAC(_ACs);
193 while (icAC.hasNext()) {
194 delete icAC.next();
195 }
196 delete _rtnetDecoder;
197 delete _antex;
198 for (int iPar = 1; iPar <= _params.size(); iPar++) {
199 delete _params[iPar-1];
200 }
201 QVectorIterator<cmbCorr*> itCorr(corrs());
202 while (itCorr.hasNext()) {
203 delete itCorr.next();
204 }
205}
206
207// Read and store one correction line
208////////////////////////////////////////////////////////////////////////////
209void bncComb::processCorrLine(const QString& staID, const QString& line) {
210 QMutexLocker locker(&_mutex);
211
212 // Find the AC Name
213 // ----------------
214 QString acName;
215 QListIterator<cmbAC*> icAC(_ACs);
216 while (icAC.hasNext()) {
217 cmbAC* AC = icAC.next();
218 if (AC->mountPoint == staID) {
219 acName = AC->name;
220 break;
221 }
222 }
223 if (acName.isEmpty()) {
224 return;
225 }
226
227 // Read the Correction
228 // -------------------
229 cmbCorr* newCorr = new cmbCorr();
230 newCorr->acName = acName;
231 if (!newCorr->readLine(line) == success) {
232 delete newCorr;
233 return;
234 }
235
236 // Check Modulo Time
237 // -----------------
238 const int moduloTime = 10;
239 if (int(newCorr->tt.gpssec()) % moduloTime != 0.0) {
240 delete newCorr;
241 return;
242 }
243
244 // Delete old corrections
245 // ----------------------
246 if (_resTime.valid() && newCorr->tt <= _resTime) {
247 delete newCorr;
248 return;
249 }
250
251 // Check the Ephemeris
252 //--------------------
253 if (_eph.find(newCorr->prn) == _eph.end()) {
254 delete newCorr;
255 return;
256 }
257 else {
258 t_eph* lastEph = _eph[newCorr->prn]->last;
259 t_eph* prevEph = _eph[newCorr->prn]->prev;
260 if (lastEph && lastEph->IOD() == newCorr->iod) {
261 newCorr->eph = lastEph;
262 }
263 else if (prevEph && prevEph->IOD() == newCorr->iod) {
264 newCorr->eph = prevEph;
265 switchToLastEph(lastEph, newCorr);
266 }
267 else {
268 delete newCorr;
269 return;
270 }
271 }
272
273 // Process previous Epoch(s)
274 // -------------------------
275 QListIterator<bncTime> itTime(_buffer.keys());
276 while (itTime.hasNext()) {
277 bncTime epoTime = itTime.next();
278 if (epoTime < newCorr->tt - moduloTime) {
279 _resTime = epoTime;
280 processEpoch();
281 }
282 }
283
284 // Merge or add the correction
285 // ---------------------------
286 QVector<cmbCorr*>& corrs = _buffer[newCorr->tt].corrs;
287 cmbCorr* existingCorr = 0;
288 QVectorIterator<cmbCorr*> itCorr(corrs);
289 while (itCorr.hasNext()) {
290 cmbCorr* hlp = itCorr.next();
291 if (hlp->prn == newCorr->prn && hlp->acName == newCorr->prn) {
292 existingCorr = hlp;
293 break;
294 }
295 }
296 if (existingCorr) {
297 delete newCorr;
298 existingCorr->readLine(line); // merge (multiple messages)
299 }
300 else {
301 corrs.append(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 if (corr->eph == lastEph) {
310 return;
311 }
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 Epoch
347////////////////////////////////////////////////////////////////////////////
348void bncComb::processEpoch() {
349
350 int nPar = _params.size();
351 int nObs = corrs().size();
352
353 if (nObs == 0) {
354 return;
355 }
356
357 _log.clear();
358
359 QTextStream out(&_log, QIODevice::WriteOnly);
360
361 out << endl << "Combination:" << endl
362 << "------------------------------" << endl;
363
364 // Observation Statistics
365 // ----------------------
366 bool masterPresent = false;
367
368 QListIterator<cmbAC*> icAC(_ACs);
369 while (icAC.hasNext()) {
370 cmbAC* AC = icAC.next();
371 AC->numObs = 0;
372 QVectorIterator<cmbCorr*> itCorr(corrs());
373 while (itCorr.hasNext()) {
374 cmbCorr* corr = itCorr.next();
375 if (corr->acName == AC->name) {
376 AC->numObs += 1;
377 if (AC->name == _masterOrbitAC) {
378 masterPresent = true;
379 }
380 }
381 }
382 out << AC->name.toAscii().data() << ": " << AC->numObs << endl;
383 }
384
385 // If Master not present, switch to another one
386 // --------------------------------------------
387 if (!masterPresent) {
388 QListIterator<cmbAC*> icAC(_ACs);
389 while (icAC.hasNext()) {
390 cmbAC* AC = icAC.next();
391 if (AC->numObs > 0) {
392 out << "Switching Master AC "
393 << _masterOrbitAC.toAscii().data() << " --> "
394 << AC->name.toAscii().data() << " "
395 << _resTime.datestr().c_str() << " "
396 << _resTime.timestr().c_str() << endl;
397 _masterOrbitAC = AC->name;
398 break;
399 }
400 }
401 }
402
403 // Check Number of Observations per Satellite
404 // ------------------------------------------
405 QMap<QString, int> numObs;
406 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
407 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
408 numObs[prn] = 0;
409 QVectorIterator<cmbCorr*> itCorr(corrs());
410 while (itCorr.hasNext()) {
411 cmbCorr* corr = itCorr.next();
412 if (corr->prn == prn) {
413 ++numObs[prn];
414 }
415 }
416 }
417
418 // Prediction Step
419 // ---------------
420 ColumnVector x0(nPar);
421 for (int iPar = 1; iPar <= _params.size(); iPar++) {
422 cmbParam* pp = _params[iPar-1];
423 if (pp->sig_P != 0.0) {
424 pp->xx = 0.0;
425 for (int jj = 1; jj <= _params.size(); jj++) {
426 _QQ(iPar, jj) = 0.0;
427 }
428 _QQ(iPar,iPar) = pp->sig_P * pp->sig_P;
429 }
430 x0(iPar) = pp->xx;
431 }
432
433 // Create First Design Matrix and Vector of Measurements
434 // -----------------------------------------------------
435 const double Pl = 1.0 / (0.05 * 0.05);
436
437 const int nCon = (_firstReg == false) ? 1 + MAXPRN_GPS : 1;
438 Matrix AA(nObs+nCon, nPar); AA = 0.0;
439 ColumnVector ll(nObs+nCon); ll = 0.0;
440 DiagonalMatrix PP(nObs+nCon); PP = Pl;
441
442 int iObs = 0;
443
444 QMap<QString, t_corr*> resCorr;
445
446 QVectorIterator<cmbCorr*> itCorr(corrs());
447 while (itCorr.hasNext()) {
448 cmbCorr* corr = itCorr.next();
449 QString prn = corr->prn;
450 switchToLastEph(_eph[prn]->last, corr);
451 ++iObs;
452
453 if (corr->acName == _masterOrbitAC && resCorr.find(prn) == resCorr.end()) {
454 resCorr[prn] = new t_corr(*corr);
455 }
456
457 for (int iPar = 1; iPar <= _params.size(); iPar++) {
458 cmbParam* pp = _params[iPar-1];
459 AA(iObs, iPar) = pp->partial(corr->acName, prn);
460 }
461
462 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
463 }
464
465 // Regularization
466 // --------------
467 const double Ph = 1.e6;
468 int iCond = 1;
469 PP(nObs+iCond) = Ph;
470 for (int iPar = 1; iPar <= _params.size(); iPar++) {
471 cmbParam* pp = _params[iPar-1];
472 if (pp->type == cmbParam::clkSat &&
473 AA.Column(iPar).maximum_absolute_value() > 0.0) {
474 AA(nObs+iCond, iPar) = 1.0;
475 }
476 }
477
478 if (!_firstReg) {
479 _firstReg = true;
480 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
481 ++iCond;
482 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
483 PP(nObs+1+iGps) = Ph;
484 for (int iPar = 1; iPar <= _params.size(); iPar++) {
485 cmbParam* pp = _params[iPar-1];
486 if (pp->type == cmbParam::offACSat && pp->prn == prn) {
487 AA(nObs+iCond, iPar) = 1.0;
488 }
489 }
490 }
491 }
492
493 ColumnVector dx;
494 SymmetricMatrix QQ_sav = _QQ;
495
496 // Update and outlier detection loop
497 // ---------------------------------
498 for (int ii = 1; ii < 10; ii++) {
499 bncModel::kalman(AA, ll, PP, _QQ, dx);
500 ColumnVector vv = ll - AA * dx;
501
502 int maxResIndex;
503 double maxRes = vv.maximum_absolute_value1(maxResIndex);
504 out.setRealNumberNotation(QTextStream::FixedNotation);
505 out.setRealNumberPrecision(3);
506 out << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
507 << " Maximum Residuum " << maxRes << ' '
508 << corrs()[maxResIndex-1]->acName << ' ' << corrs()[maxResIndex-1]->prn;
509
510 if (maxRes > _MAXRES) {
511 for (int iPar = 1; iPar <= _params.size(); iPar++) {
512 cmbParam* pp = _params[iPar-1];
513 if (pp->type == cmbParam::offACSat &&
514 pp->AC == corrs()[maxResIndex-1]->acName &&
515 pp->prn == corrs()[maxResIndex-1]->prn) {
516 QQ_sav.Row(iPar) = 0.0;
517 QQ_sav.Column(iPar) = 0.0;
518 QQ_sav(iPar,iPar) = pp->sig_0 * pp->sig_0;
519 }
520 }
521
522 out << " Outlier" << endl;
523 _QQ = QQ_sav;
524 AA.Row(maxResIndex) = 0.0;
525 ll.Row(maxResIndex) = 0.0;
526 }
527 else {
528 out << " OK" << endl;
529 break;
530 }
531 }
532
533 // Update Parameter Values
534 // -----------------------
535 for (int iPar = 1; iPar <= _params.size(); iPar++) {
536 cmbParam* pp = _params[iPar-1];
537 pp->xx += dx(iPar);
538 if (pp->type == cmbParam::clkSat) {
539 if (resCorr.find(pp->prn) != resCorr.end()) {
540 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
541 }
542 }
543 out << _resTime.datestr().c_str() << " "
544 << _resTime.timestr().c_str() << " ";
545 out.setRealNumberNotation(QTextStream::FixedNotation);
546 out.setFieldWidth(8);
547 out.setRealNumberPrecision(4);
548 out << pp->toString() << " "
549 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
550 out.setFieldWidth(0);
551 }
552
553 // Print Results
554 // -------------
555 printResults(out, resCorr);
556 dumpResults(resCorr);
557
558 emit newMessage(_log, false);
559
560 // Delete Data
561 // -----------
562 _buffer.remove(_resTime);
563}
564
565// Print results
566////////////////////////////////////////////////////////////////////////////
567void bncComb::printResults(QTextStream& out,
568 const QMap<QString, t_corr*>& resCorr) {
569
570 QMapIterator<QString, t_corr*> it(resCorr);
571 while (it.hasNext()) {
572 it.next();
573 t_corr* corr = it.value();
574 const t_eph* eph = corr->eph;
575 if (eph) {
576 double xx, yy, zz, cc;
577 eph->position(_resTime.gpsw(), _resTime.gpssec(), xx, yy, zz, cc);
578
579 out << _resTime.datestr().c_str() << " "
580 << _resTime.timestr().c_str() << " ";
581 out.setFieldWidth(3);
582 out << "Full Clock " << corr->prn << " " << corr->iod << " ";
583 out.setFieldWidth(14);
584 out << (cc + corr->dClk) * t_CST::c << endl;
585 out.setFieldWidth(0);
586 }
587 else {
588 out << "bncComb::printResuls bug" << endl;
589 }
590 }
591}
592
593// Send results to RTNet Decoder and directly to PPP Client
594////////////////////////////////////////////////////////////////////////////
595void bncComb::dumpResults(const QMap<QString, t_corr*>& resCorr) {
596
597 ostringstream out; out.setf(std::ios::fixed);
598 QStringList corrLines;
599
600 unsigned year, month, day, hour, minute;
601 double sec;
602 _resTime.civil_date(year, month, day);
603 _resTime.civil_time(hour, minute, sec);
604
605 out << "* "
606 << setw(4) << year << " "
607 << setw(2) << month << " "
608 << setw(2) << day << " "
609 << setw(2) << hour << " "
610 << setw(2) << minute << " "
611 << setw(12) << setprecision(8) << sec << " "
612 << endl;
613
614 QMapIterator<QString, t_corr*> it(resCorr);
615 while (it.hasNext()) {
616 it.next();
617 t_corr* corr = it.value();
618
619 double dT = 60.0;
620
621 for (int iTime = 1; iTime <= 2; iTime++) {
622
623 bncTime time12 = (iTime == 1) ? _resTime : _resTime + dT;
624
625 ColumnVector xc(4);
626 ColumnVector vv(3);
627 corr->eph->position(time12.gpsw(), time12.gpssec(),
628 xc.data(), vv.data());
629 bncPPPclient::applyCorr(time12, corr, xc, vv);
630
631 // Relativistic Correction
632 // -----------------------
633 double relCorr = - 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c;
634 xc(4) -= relCorr;
635
636 // Code Biases
637 // -----------
638 double dcbP1C1 = 0.0;
639 double dcbP1P2 = 0.0;
640
641 // Correction Phase Center --> CoM
642 // -------------------------------
643 ColumnVector dx(3); dx = 0.0;
644 if (_antex) {
645 double Mjd = time12.mjd() + time12.daysec()/86400.0;
646 if (_antex->satCoMcorrection(corr->prn, Mjd, xc.Rows(1,3), dx) == success) {
647 xc(1) -= dx(1);
648 xc(2) -= dx(2);
649 xc(3) -= dx(3);
650 }
651 else {
652 cout << "antenna not found" << endl;
653 }
654 }
655
656 if (iTime == 1) {
657 out << 'P' << corr->prn.toAscii().data()
658 << setw(14) << setprecision(6) << xc(1) / 1000.0
659 << setw(14) << setprecision(6) << xc(2) / 1000.0
660 << setw(14) << setprecision(6) << xc(3) / 1000.0
661 << setw(14) << setprecision(6) << xc(4) * 1e6
662 << setw(14) << setprecision(6) << relCorr * 1e6
663 << setw(8) << setprecision(3) << dx(1)
664 << setw(8) << setprecision(3) << dx(2)
665 << setw(8) << setprecision(3) << dx(3)
666 << setw(8) << setprecision(3) << dcbP1C1
667 << setw(8) << setprecision(3) << dcbP1P2
668 << setw(6) << setprecision(1) << dT;
669
670 QString line;
671 int messageType = COTYPE_GPSCOMBINED;
672 int updateInt = 0;
673 line.sprintf("%d %d %d %.1f %s"
674 " %3d"
675 " %8.3f %8.3f %8.3f %8.3f"
676 " %10.5f %10.5f %10.5f %10.5f"
677 " %10.5f %10.5f %10.5f %10.5f INTERNAL",
678 messageType, updateInt, time12.gpsw(), time12.gpssec(),
679 corr->prn.toAscii().data(),
680 corr->iod,
681 corr->dClk * t_CST::c,
682 corr->rao[0],
683 corr->rao[1],
684 corr->rao[2],
685 corr->dotDClk * t_CST::c,
686 corr->dotRao[0],
687 corr->dotRao[1],
688 corr->dotRao[2],
689 corr->dotDotDClk * t_CST::c,
690 corr->dotDotRao[0],
691 corr->dotDotRao[1],
692 corr->dotDotRao[2]);
693 corrLines << line;
694 }
695 else {
696 out << setw(14) << setprecision(6) << xc(1) / 1000.0
697 << setw(14) << setprecision(6) << xc(2) / 1000.0
698 << setw(14) << setprecision(6) << xc(3) / 1000.0 << endl;
699 }
700 }
701
702 delete corr;
703 }
704 out << "EOE" << endl; // End Of Epoch flag
705
706 if (!_rtnetDecoder) {
707 _rtnetDecoder = new bncRtnetDecoder();
708 }
709
710 vector<string> errmsg;
711 _rtnetDecoder->Decode((char*) out.str().data(), out.str().size(), errmsg);
712
713 // Optionally send new Corrections to PPP
714 // --------------------------------------
715 bncApp* app = (bncApp*) qApp;
716 if (app->_bncPPPclient) {
717 app->_bncPPPclient->slotNewCorrections(corrLines);
718 }
719}
Note: See TracBrowser for help on using the repository browser.