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

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