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

Last change on this file since 3453 was 3453, 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 }
399 }
400 }
401
402 // Check Number of Observations per Satellite
403 // ------------------------------------------
404 QMap<QString, int> numObs;
405 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
406 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
407 numObs[prn] = 0;
408 QVectorIterator<cmbCorr*> itCorr(corrs());
409 while (itCorr.hasNext()) {
410 cmbCorr* corr = itCorr.next();
411 if (corr->prn == prn) {
412 ++numObs[prn];
413 }
414 }
415 }
416
417 // Prediction Step
418 // ---------------
419 ColumnVector x0(nPar);
420 for (int iPar = 1; iPar <= _params.size(); iPar++) {
421 cmbParam* pp = _params[iPar-1];
422 if (pp->sig_P != 0.0) {
423 pp->xx = 0.0;
424 for (int jj = 1; jj <= _params.size(); jj++) {
425 _QQ(iPar, jj) = 0.0;
426 }
427 _QQ(iPar,iPar) = pp->sig_P * pp->sig_P;
428 }
429 x0(iPar) = pp->xx;
430 }
431
432 // Create First Design Matrix and Vector of Measurements
433 // -----------------------------------------------------
434 const double Pl = 1.0 / (0.05 * 0.05);
435
436 const int nCon = (_firstReg == false) ? 1 + MAXPRN_GPS : 1;
437 Matrix AA(nObs+nCon, nPar); AA = 0.0;
438 ColumnVector ll(nObs+nCon); ll = 0.0;
439 DiagonalMatrix PP(nObs+nCon); PP = Pl;
440
441 int iObs = 0;
442
443 QMap<QString, t_corr*> resCorr;
444
445 QVectorIterator<cmbCorr*> itCorr(corrs());
446 while (itCorr.hasNext()) {
447 cmbCorr* corr = itCorr.next();
448 QString prn = corr->prn;
449 switchToLastEph(_eph[prn]->last, corr);
450 ++iObs;
451
452 if (corr->acName == _masterOrbitAC && resCorr.find(prn) == resCorr.end()) {
453 resCorr[prn] = new t_corr(*corr);
454 }
455
456 for (int iPar = 1; iPar <= _params.size(); iPar++) {
457 cmbParam* pp = _params[iPar-1];
458 AA(iObs, iPar) = pp->partial(corr->acName, prn);
459 }
460
461 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
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::clkSat &&
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::offACSat && pp->prn == prn) {
486 AA(nObs+iCond, iPar) = 1.0;
487 }
488 }
489 }
490 }
491
492 ColumnVector dx;
493 SymmetricMatrix QQ_sav = _QQ;
494
495 // Update and outlier detection loop
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 << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
506 << " Maximum Residuum " << maxRes << ' '
507 << corrs()[maxResIndex-1]->acName << ' ' << corrs()[maxResIndex-1]->prn;
508
509 if (maxRes > _MAXRES) {
510 for (int iPar = 1; iPar <= _params.size(); iPar++) {
511 cmbParam* pp = _params[iPar-1];
512 if (pp->type == cmbParam::offACSat &&
513 pp->AC == corrs()[maxResIndex-1]->acName &&
514 pp->prn == corrs()[maxResIndex-1]->prn) {
515 QQ_sav.Row(iPar) = 0.0;
516 QQ_sav.Column(iPar) = 0.0;
517 QQ_sav(iPar,iPar) = pp->sig_0 * pp->sig_0;
518 }
519 }
520
521 out << " Outlier" << endl;
522 _QQ = QQ_sav;
523 AA.Row(maxResIndex) = 0.0;
524 ll.Row(maxResIndex) = 0.0;
525 }
526 else {
527 out << " OK" << endl;
528 break;
529 }
530 }
531
532 // Update Parameter Values
533 // -----------------------
534 for (int iPar = 1; iPar <= _params.size(); iPar++) {
535 cmbParam* pp = _params[iPar-1];
536 pp->xx += dx(iPar);
537 if (pp->type == cmbParam::clkSat) {
538 if (resCorr.find(pp->prn) != resCorr.end()) {
539 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
540 }
541 }
542 out << _resTime.datestr().c_str() << " "
543 << _resTime.timestr().c_str() << " ";
544 out.setRealNumberNotation(QTextStream::FixedNotation);
545 out.setFieldWidth(8);
546 out.setRealNumberPrecision(4);
547 out << pp->toString() << " "
548 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
549 out.setFieldWidth(0);
550 }
551
552 // Print Results
553 // -------------
554 printResults(out, resCorr);
555 dumpResults(resCorr);
556
557 emit newMessage(_log, false);
558
559 // Delete Data
560 // -----------
561 _buffer.remove(_resTime);
562}
563
564// Print results
565////////////////////////////////////////////////////////////////////////////
566void bncComb::printResults(QTextStream& out,
567 const QMap<QString, t_corr*>& resCorr) {
568
569 QMapIterator<QString, t_corr*> it(resCorr);
570 while (it.hasNext()) {
571 it.next();
572 t_corr* corr = it.value();
573 const t_eph* eph = corr->eph;
574 if (eph) {
575 double xx, yy, zz, cc;
576 eph->position(_resTime.gpsw(), _resTime.gpssec(), xx, yy, zz, cc);
577
578 out << _resTime.datestr().c_str() << " "
579 << _resTime.timestr().c_str() << " ";
580 out.setFieldWidth(3);
581 out << "Full Clock " << corr->prn << " " << corr->iod << " ";
582 out.setFieldWidth(14);
583 out << (cc + corr->dClk) * t_CST::c << endl;
584 out.setFieldWidth(0);
585 }
586 else {
587 out << "bncComb::printResuls bug" << endl;
588 }
589 }
590}
591
592// Send results to RTNet Decoder and directly to PPP Client
593////////////////////////////////////////////////////////////////////////////
594void bncComb::dumpResults(const QMap<QString, t_corr*>& resCorr) {
595
596 ostringstream out; out.setf(std::ios::fixed);
597 QStringList corrLines;
598
599 unsigned year, month, day, hour, minute;
600 double sec;
601 _resTime.civil_date(year, month, day);
602 _resTime.civil_time(hour, minute, sec);
603
604 out << "* "
605 << setw(4) << year << " "
606 << setw(2) << month << " "
607 << setw(2) << day << " "
608 << setw(2) << hour << " "
609 << setw(2) << minute << " "
610 << setw(12) << setprecision(8) << sec << " "
611 << endl;
612
613 QMapIterator<QString, t_corr*> it(resCorr);
614 while (it.hasNext()) {
615 it.next();
616 t_corr* corr = it.value();
617
618 double dT = 60.0;
619
620 for (int iTime = 1; iTime <= 2; iTime++) {
621
622 bncTime time12 = (iTime == 1) ? _resTime : _resTime + dT;
623
624 ColumnVector xc(4);
625 ColumnVector vv(3);
626 corr->eph->position(time12.gpsw(), time12.gpssec(),
627 xc.data(), vv.data());
628 bncPPPclient::applyCorr(time12, corr, xc, vv);
629
630 // Relativistic Correction
631 // -----------------------
632 double relCorr = - 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c;
633 xc(4) -= relCorr;
634
635 // Code Biases
636 // -----------
637 double dcbP1C1 = 0.0;
638 double dcbP1P2 = 0.0;
639
640 // Correction Phase Center --> CoM
641 // -------------------------------
642 ColumnVector dx(3); dx = 0.0;
643 if (_antex) {
644 double Mjd = time12.mjd() + time12.daysec()/86400.0;
645 if (_antex->satCoMcorrection(corr->prn, Mjd, xc.Rows(1,3), dx) == success) {
646 xc(1) -= dx(1);
647 xc(2) -= dx(2);
648 xc(3) -= dx(3);
649 }
650 else {
651 cout << "antenna not found" << endl;
652 }
653 }
654
655 if (iTime == 1) {
656 out << 'P' << corr->prn.toAscii().data()
657 << setw(14) << setprecision(6) << xc(1) / 1000.0
658 << setw(14) << setprecision(6) << xc(2) / 1000.0
659 << setw(14) << setprecision(6) << xc(3) / 1000.0
660 << setw(14) << setprecision(6) << xc(4) * 1e6
661 << setw(14) << setprecision(6) << relCorr * 1e6
662 << setw(8) << setprecision(3) << dx(1)
663 << setw(8) << setprecision(3) << dx(2)
664 << setw(8) << setprecision(3) << dx(3)
665 << setw(8) << setprecision(3) << dcbP1C1
666 << setw(8) << setprecision(3) << dcbP1P2
667 << setw(6) << setprecision(1) << dT;
668
669 QString line;
670 int messageType = COTYPE_GPSCOMBINED;
671 int updateInt = 0;
672 line.sprintf("%d %d %d %.1f %s"
673 " %3d"
674 " %8.3f %8.3f %8.3f %8.3f"
675 " %10.5f %10.5f %10.5f %10.5f"
676 " %10.5f %10.5f %10.5f %10.5f INTERNAL",
677 messageType, updateInt, time12.gpsw(), time12.gpssec(),
678 corr->prn.toAscii().data(),
679 corr->iod,
680 corr->dClk * t_CST::c,
681 corr->rao[0],
682 corr->rao[1],
683 corr->rao[2],
684 corr->dotDClk * t_CST::c,
685 corr->dotRao[0],
686 corr->dotRao[1],
687 corr->dotRao[2],
688 corr->dotDotDClk * t_CST::c,
689 corr->dotDotRao[0],
690 corr->dotDotRao[1],
691 corr->dotDotRao[2]);
692 corrLines << line;
693 }
694 else {
695 out << setw(14) << setprecision(6) << xc(1) / 1000.0
696 << setw(14) << setprecision(6) << xc(2) / 1000.0
697 << setw(14) << setprecision(6) << xc(3) / 1000.0 << endl;
698 }
699 }
700
701 delete corr;
702 }
703 out << "EOE" << endl; // End Of Epoch flag
704
705 if (!_rtnetDecoder) {
706 _rtnetDecoder = new bncRtnetDecoder();
707 }
708
709 vector<string> errmsg;
710 _rtnetDecoder->Decode((char*) out.str().data(), out.str().size(), errmsg);
711
712 // Optionally send new Corrections to PPP
713 // --------------------------------------
714 bncApp* app = (bncApp*) qApp;
715 if (app->_bncPPPclient) {
716 app->_bncPPPclient->slotNewCorrections(corrLines);
717 }
718}
Note: See TracBrowser for help on using the repository browser.