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

Last change on this file since 3433 was 3433, checked in by mervart, 13 years ago
File size: 18.1 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 _ACs.append(newAC);
125 }
126 }
127
128 _rtnetDecoder = 0;
129
130 connect(this, SIGNAL(newMessage(QByteArray,bool)),
131 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
132
133
134 // Initialize Parameters (model: Clk_Corr = AC_Offset + Sat_Offset + Clk)
135 // ----------------------------------------------------------------------
136 int nextPar = 0;
137 QListIterator<cmbAC*> it(_ACs);
138 while (it.hasNext()) {
139 cmbAC* AC = it.next();
140 _params.push_back(new cmbParam(cmbParam::offAC, ++nextPar, AC->name, ""));
141 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
142 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
143 _params.push_back(new cmbParam(cmbParam::offACSat, ++nextPar,
144 AC->name, prn));
145 }
146 }
147 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
148 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
149 _params.push_back(new cmbParam(cmbParam::clkSat, ++nextPar, "", prn));
150 }
151
152 // Initialize Variance-Covariance Matrix
153 // -------------------------------------
154 _QQ.ReSize(_params.size());
155 _QQ = 0.0;
156 for (int iPar = 1; iPar <= _params.size(); iPar++) {
157 cmbParam* pp = _params[iPar-1];
158 _QQ(iPar,iPar) = pp->sig_0 * pp->sig_0;
159 }
160
161 // ANTEX File
162 // ----------
163 _antex = 0;
164 QString antexFileName = settings.value("pppAntex").toString();
165 if (!antexFileName.isEmpty()) {
166 _antex = new bncAntex();
167 if (_antex->readFile(antexFileName) != success) {
168 emit newMessage("wrong ANTEX file", true);
169 delete _antex;
170 _antex = 0;
171 }
172 }
173
174 // Not yet regularized
175 // -------------------
176 _firstReg = false;
177
178 // Maximal Residuum
179 // ----------------
180 _MAXRES = settings.value("cmbMaxres").toDouble();
181 if (_MAXRES <= 0.0) {
182 _MAXRES = 999.0;
183 }
184}
185
186// Destructor
187////////////////////////////////////////////////////////////////////////////
188bncComb::~bncComb() {
189 QListIterator<cmbAC*> icAC(_ACs);
190 while (icAC.hasNext()) {
191 delete icAC.next();
192 }
193 delete _rtnetDecoder;
194 delete _antex;
195 for (int iPar = 1; iPar <= _params.size(); iPar++) {
196 delete _params[iPar-1];
197 }
198 QVectorIterator<cmbCorr*> itCorr(_corrs);
199 while (itCorr.hasNext()) {
200 delete itCorr.next();
201 }
202}
203
204// Read and store one correction line
205////////////////////////////////////////////////////////////////////////////
206void bncComb::processCorrLine(const QString& staID, const QString& line) {
207 QMutexLocker locker(&_mutex);
208
209 // Find the AC Name
210 // ----------------
211 QString acName;
212 QListIterator<cmbAC*> icAC(_ACs);
213 while (icAC.hasNext()) {
214 cmbAC* AC = icAC.next();
215 if (AC->mountPoint == staID) {
216 acName = AC->name;
217 break;
218 }
219 }
220 if (acName.isEmpty()) {
221 return;
222 }
223
224 // Read the Correction
225 // -------------------
226 cmbCorr* newCorr = new cmbCorr();
227 newCorr->acName = acName;
228 if (!newCorr->readLine(line) == success) {
229 delete newCorr;
230 return;
231 }
232
233 // Check Modulo Time
234 // -----------------
235 const int moduloTime = 10;
236 if (int(newCorr->tt.gpssec()) % moduloTime != 0.0) {
237 delete newCorr;
238 return;
239 }
240
241 // Delete old corrections
242 // ----------------------
243 if (newCorr->tt < _resTime) {
244 delete newCorr;
245 return;
246 }
247
248 // Check the Ephemeris
249 //--------------------
250 if (_eph.find(newCorr->prn) == _eph.end()) {
251 delete newCorr;
252 return;
253 }
254 else {
255 t_eph* lastEph = _eph[newCorr->prn]->last;
256 t_eph* prevEph = _eph[newCorr->prn]->prev;
257 if (lastEph && lastEph->IOD() == newCorr->iod) {
258 newCorr->eph = lastEph;
259 }
260 else if (prevEph && prevEph->IOD() == newCorr->iod) {
261 newCorr->eph = prevEph;
262 switchToLastEph(lastEph, newCorr);
263 }
264 else {
265 delete newCorr;
266 return;
267 }
268 }
269
270 // Process previous Epoch
271 // ----------------------
272 if (_resTime.valid() && _resTime < newCorr->tt) {
273 processEpoch();
274 }
275
276 // Remember last correction time
277 // -----------------------------
278 _resTime = newCorr->tt;
279
280 // Merge or add the correction
281 // ---------------------------
282 cmbCorr* existingCorr = 0;
283 QVectorIterator<cmbCorr*> itCorr(_corrs);
284 while (itCorr.hasNext()) {
285 cmbCorr* hlp = itCorr.next();
286 if (hlp->prn == newCorr->prn && hlp->acName == newCorr->prn) {
287 existingCorr = hlp;
288 break;
289 }
290 }
291 if (existingCorr) {
292 delete newCorr;
293 existingCorr->readLine(line); // merge (multiple messages)
294 }
295 else {
296 _corrs.append(newCorr);
297 }
298}
299
300// Change the correction so that it refers to last received ephemeris
301////////////////////////////////////////////////////////////////////////////
302void bncComb::switchToLastEph(const t_eph* lastEph, t_corr* corr) {
303
304 if (corr->eph == lastEph) {
305 return;
306 }
307
308 ColumnVector oldXC(4);
309 ColumnVector oldVV(3);
310 corr->eph->position(corr->tt.gpsw(), corr->tt.gpssec(),
311 oldXC.data(), oldVV.data());
312
313 ColumnVector newXC(4);
314 ColumnVector newVV(3);
315 lastEph->position(corr->tt.gpsw(), corr->tt.gpssec(),
316 newXC.data(), newVV.data());
317
318 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
319 ColumnVector dV = newVV - oldVV;
320 double dC = newXC(4) - oldXC(4);
321
322 ColumnVector dRAO(3);
323 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
324
325 ColumnVector dDotRAO(3);
326 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
327
328 QString msg = "switch " + corr->prn
329 + QString(" %1 -> %2 %3").arg(corr->iod,3)
330 .arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
331
332 emit newMessage(msg.toAscii(), false);
333
334 corr->iod = lastEph->IOD();
335 corr->eph = lastEph;
336 corr->rao += dRAO;
337 corr->dotRao += dDotRAO;
338 corr->dClk -= dC;
339}
340
341// Process Epoch
342////////////////////////////////////////////////////////////////////////////
343void bncComb::processEpoch() {
344
345 int nPar = _params.size();
346 int nObs = _corrs.size();
347
348 if (nObs == 0) {
349 return;
350 }
351
352 _log.clear();
353
354 QTextStream out(&_log, QIODevice::WriteOnly);
355
356 out << endl << "Combination:" << endl
357 << "------------------------------" << endl;
358
359 // Observation Statistics
360 // ----------------------
361 QListIterator<cmbAC*> icAC(_ACs);
362 while (icAC.hasNext()) {
363 cmbAC* AC = icAC.next();
364 AC->numObs = 0;
365 QVectorIterator<cmbCorr*> itCorr(_corrs);
366 while (itCorr.hasNext()) {
367 cmbCorr* corr = itCorr.next();
368 if (corr->acName == AC->name) {
369 AC->numObs += 1;
370 }
371 }
372 out << AC->name.toAscii().data() << ": " << AC->numObs << endl;
373 }
374
375 // Prediction Step
376 // ---------------
377 ColumnVector x0(nPar);
378 for (int iPar = 1; iPar <= _params.size(); iPar++) {
379 cmbParam* pp = _params[iPar-1];
380 _QQ(iPar,iPar) += pp->sig_P * pp->sig_P;
381 x0(iPar) = pp->xx;
382 }
383
384 // Create First Design Matrix and Vector of Measurements
385 // -----------------------------------------------------
386 const double Pl = 1.0 / (0.05 * 0.05);
387
388 const int nCon = (_firstReg == false) ? 1 + MAXPRN_GPS : 1;
389 Matrix AA(nObs+nCon, nPar); AA = 0.0;
390 ColumnVector ll(nObs+nCon); ll = 0.0;
391 DiagonalMatrix PP(nObs+nCon); PP = Pl;
392
393 int iObs = 0;
394
395 QMap<QString, t_corr*> resCorr;
396
397 QVectorIterator<cmbCorr*> itCorr(_corrs);
398 while (itCorr.hasNext()) {
399 cmbCorr* corr = itCorr.next();
400 QString prn = corr->prn;
401 switchToLastEph(_eph[prn]->last, corr);
402 ++iObs;
403
404 if (resCorr.find(prn) == resCorr.end()) {
405 resCorr[prn] = new t_corr(*corr);
406 }
407
408 for (int iPar = 1; iPar <= _params.size(); iPar++) {
409 cmbParam* pp = _params[iPar-1];
410 AA(iObs, iPar) = pp->partial(corr->acName, prn);
411 }
412
413 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
414 }
415
416 // Regularization
417 // --------------
418 const double Ph = 1.e6;
419 int iCond = 1;
420 PP(nObs+iCond) = Ph;
421 for (int iPar = 1; iPar <= _params.size(); iPar++) {
422 cmbParam* pp = _params[iPar-1];
423 if (pp->type == cmbParam::clkSat &&
424 AA.Column(iPar).maximum_absolute_value() > 0.0) {
425 AA(nObs+iCond, iPar) = 1.0;
426 }
427 }
428
429 if (!_firstReg) {
430 _firstReg = true;
431 for (int iGps = 1; iGps <= MAXPRN_GPS; iGps++) {
432 ++iCond;
433 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
434 PP(nObs+1+iGps) = Ph;
435 for (int iPar = 1; iPar <= _params.size(); iPar++) {
436 cmbParam* pp = _params[iPar-1];
437 if (pp->type == cmbParam::offACSat && pp->prn == prn) {
438 AA(nObs+iCond, iPar) = 1.0;
439 }
440 }
441 }
442 }
443
444 ColumnVector dx;
445 SymmetricMatrix QQ_sav = _QQ;
446
447 // Update and outlier detection loop
448 // ---------------------------------
449 for (int ii = 1; ii < 10; ii++) {
450 bncModel::kalman(AA, ll, PP, _QQ, dx);
451 ColumnVector vv = ll - AA * dx;
452
453 int maxResIndex;
454 double maxRes = vv.maximum_absolute_value1(maxResIndex);
455 out.setRealNumberNotation(QTextStream::FixedNotation);
456 out.setRealNumberPrecision(3);
457 out << _resTime.datestr().c_str() << " " << _resTime.timestr().c_str()
458 << " Maximum Residuum " << maxRes << ' '
459 << _corrs[maxResIndex-1]->acName << ' ' << _corrs[maxResIndex-1]->prn;
460
461 if (maxRes > _MAXRES) {
462 for (int iPar = 1; iPar <= _params.size(); iPar++) {
463 cmbParam* pp = _params[iPar-1];
464 if (pp->type == cmbParam::offACSat &&
465 pp->AC == _corrs[maxResIndex-1]->acName &&
466 pp->prn == _corrs[maxResIndex-1]->prn) {
467 QQ_sav.Row(iPar) = 0.0;
468 QQ_sav.Column(iPar) = 0.0;
469 QQ_sav(iPar,iPar) = pp->sig_0 * pp->sig_0;
470 }
471 }
472
473 out << " Outlier" << endl;
474 _QQ = QQ_sav;
475 AA.Row(maxResIndex) = 0.0;
476 ll.Row(maxResIndex) = 0.0;
477 }
478 else {
479 out << " OK" << endl;
480 break;
481 }
482 }
483
484 // Update Parameter Values
485 // -----------------------
486 for (int iPar = 1; iPar <= _params.size(); iPar++) {
487 cmbParam* pp = _params[iPar-1];
488 pp->xx += dx(iPar);
489 if (pp->type == cmbParam::clkSat) {
490 if (resCorr.find(pp->prn) != resCorr.end()) {
491 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
492 }
493 }
494 out << _resTime.datestr().c_str() << " "
495 << _resTime.timestr().c_str() << " ";
496 out.setRealNumberNotation(QTextStream::FixedNotation);
497 out.setFieldWidth(8);
498 out.setRealNumberPrecision(4);
499 out << pp->toString() << " "
500 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
501 out.setFieldWidth(0);
502 }
503
504 // Print Results
505 // -------------
506 printResults(out, resCorr);
507 dumpResults(resCorr);
508
509 emit newMessage(_log, false);
510
511 // Delete Data
512 // -----------
513 QVectorIterator<cmbCorr*> it(_corrs);
514 while (it.hasNext()) {
515 delete it.next();
516 }
517 _corrs.clear();
518}
519
520// Print results
521////////////////////////////////////////////////////////////////////////////
522void bncComb::printResults(QTextStream& out,
523 const QMap<QString, t_corr*>& resCorr) {
524
525 QMapIterator<QString, t_corr*> it(resCorr);
526 while (it.hasNext()) {
527 it.next();
528 t_corr* corr = it.value();
529 const t_eph* eph = corr->eph;
530 if (eph) {
531 double xx, yy, zz, cc;
532 eph->position(_resTime.gpsw(), _resTime.gpssec(), xx, yy, zz, cc);
533
534 out << _resTime.datestr().c_str() << " "
535 << _resTime.timestr().c_str() << " ";
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 out.setFieldWidth(0);
541 }
542 else {
543 out << "bncComb::printResuls bug" << endl;
544 }
545 }
546}
547
548// Send results to RTNet Decoder and directly to PPP Client
549////////////////////////////////////////////////////////////////////////////
550void bncComb::dumpResults(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.