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

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