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

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