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

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