source: ntrip/trunk/BNC/bncmodel.cpp@ 2881

Last change on this file since 2881 was 2881, checked in by mervart, 13 years ago
File size: 36.9 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: bncParam, bncModel
30 *
31 * Purpose: Model for PPP
32 *
33 * Author: L. Mervart
34 *
35 * Created: 01-Dec-2009
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iomanip>
42#include <cmath>
43#include <newmatio.h>
44#include <sstream>
45
46#include "bncmodel.h"
47#include "bncapp.h"
48#include "bncpppclient.h"
49#include "bancroft.h"
50#include "bncutils.h"
51#include "bncsettings.h"
52#include "bnctides.h"
53#include "bncantex.h"
54
55using namespace std;
56
57const unsigned MINOBS = 4;
58const double MINELE_GPS = 10.0 * M_PI / 180.0;
59const double MINELE_GLO = 10.0 * M_PI / 180.0;
60const double MINELE_GAL = 10.0 * M_PI / 180.0;
61const double MAXRES_CODE_GPS = 10.0;
62const double MAXRES_PHASE_GPS = 0.10;
63const double MAXRES_PHASE_GLO = 0.05;
64const double MAXRES_CODE_GAL = 10.0;
65const double MAXRES_PHASE_GAL = 0.10;
66
67// Constructor
68////////////////////////////////////////////////////////////////////////////
69bncParam::bncParam(bncParam::parType typeIn, int indexIn,
70 const QString& prnIn) {
71 type = typeIn;
72 index = indexIn;
73 prn = prnIn;
74 index_old = 0;
75 xx = 0.0;
76
77}
78
79// Destructor
80////////////////////////////////////////////////////////////////////////////
81bncParam::~bncParam() {
82}
83
84// Partial
85////////////////////////////////////////////////////////////////////////////
86double bncParam::partial(t_satData* satData, bool phase) {
87
88 // Coordinates
89 // -----------
90 if (type == CRD_X) {
91 return (xx - satData->xx(1)) / satData->rho;
92 }
93 else if (type == CRD_Y) {
94 return (xx - satData->xx(2)) / satData->rho;
95 }
96 else if (type == CRD_Z) {
97 return (xx - satData->xx(3)) / satData->rho;
98 }
99
100 // Receiver Clocks
101 // ---------------
102 else if (type == RECCLK) {
103 return 1.0;
104 }
105
106 // Troposphere
107 // -----------
108 else if (type == TROPO) {
109 return 1.0 / sin(satData->eleSat);
110 }
111
112 // Galileo Offset
113 // --------------
114 else if (type == GALILEO_OFFSET) {
115 if (satData->prn[0] == 'E') {
116 return 1.0;
117 }
118 else {
119 return 0.0;
120 }
121 }
122
123 // Ambiguities
124 // -----------
125 else if (type == AMB_L3) {
126 if (phase && satData->prn == prn) {
127 return 1.0;
128 }
129 else {
130 return 0.0;
131 }
132 }
133
134 // Default return
135 // --------------
136 return 0.0;
137}
138
139// Constructor
140////////////////////////////////////////////////////////////////////////////
141bncModel::bncModel(QByteArray staID) {
142
143 _staID = staID;
144
145 bncSettings settings;
146
147 // Observation Sigmas
148 // ------------------
149 _sigP3 = 5.0;
150 if (!settings.value("pppSigmaCode").toString().isEmpty()) {
151 _sigP3 = settings.value("pppSigmaCode").toDouble();
152 }
153 _sigL3 = 0.02;
154 if (!settings.value("pppSigmaPhase").toString().isEmpty()) {
155 _sigL3 = settings.value("pppSigmaPhase").toDouble();
156 }
157
158 // Parameter Sigmas
159 // ----------------
160 _sigCrd0 = 100.0;
161 if (!settings.value("pppSigCrd0").toString().isEmpty()) {
162 _sigCrd0 = settings.value("pppSigCrd0").toDouble();
163 }
164 _sigCrdP = 100.0;
165 if (!settings.value("pppSigCrdP").toString().isEmpty()) {
166 _sigCrdP = settings.value("pppSigCrdP").toDouble();
167 }
168 _sigTrp0 = 0.1;
169 if (!settings.value("pppSigTrp0").toString().isEmpty()) {
170 _sigTrp0 = settings.value("pppSigTrp0").toDouble();
171 }
172 _sigTrpP = 1e-6;
173 if (!settings.value("pppSigTrpP").toString().isEmpty()) {
174 _sigTrpP = settings.value("pppSigTrpP").toDouble();
175 }
176 _sigClk0 = 1000.0;
177 _sigAmb0 = 1000.0;
178 _sigGalileoOffset0 = 1000.0;
179 _sigGalileoOffsetP = 0.0;
180
181 // Quick-Start Mode
182 // ----------------
183 _quickStart = 0;
184 if (settings.value("pppRefCrdX").toString() != "" &&
185 settings.value("pppRefCrdY").toString() != "" &&
186 settings.value("pppRefCrdZ").toString() != "" &&
187 !settings.value("pppQuickStart").toString().isEmpty()) {
188 _quickStart = settings.value("pppQuickStart").toDouble();
189 }
190
191 connect(this, SIGNAL(newMessage(QByteArray,bool)),
192 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
193
194 _usePhase = false;
195 if ( Qt::CheckState(settings.value("pppUsePhase").toInt()) == Qt::Checked) {
196 _usePhase = true;
197 }
198
199 _estTropo = false;
200 if ( Qt::CheckState(settings.value("pppEstTropo").toInt()) == Qt::Checked) {
201 _estTropo = true;
202 }
203
204 _xcBanc.ReSize(4); _xcBanc = 0.0;
205 _ellBanc.ReSize(3); _ellBanc = 0.0;
206
207 int nextPar = 0;
208 _params.push_back(new bncParam(bncParam::CRD_X, ++nextPar, ""));
209 _params.push_back(new bncParam(bncParam::CRD_Y, ++nextPar, ""));
210 _params.push_back(new bncParam(bncParam::CRD_Z, ++nextPar, ""));
211 _params.push_back(new bncParam(bncParam::RECCLK, ++nextPar, ""));
212 if (_estTropo) {
213 _params.push_back(new bncParam(bncParam::TROPO, ++nextPar, ""));
214 }
215 if ( Qt::CheckState(settings.value("pppGalileo").toInt()) == Qt::Checked) {
216 _params.push_back(new bncParam(bncParam::GALILEO_OFFSET, ++nextPar, ""));
217 }
218
219 unsigned nPar = _params.size();
220
221 _QQ.ReSize(nPar);
222
223 _QQ = 0.0;
224
225 for (int iPar = 1; iPar <= _params.size(); iPar++) {
226 bncParam* pp = _params[iPar-1];
227 if (pp->isCrd()) {
228 _QQ(iPar,iPar) = _sigCrd0 * _sigCrd0;
229 }
230 else if (pp->type == bncParam::RECCLK) {
231 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
232 }
233 else if (pp->type == bncParam::TROPO) {
234 _QQ(iPar,iPar) = _sigTrp0 * _sigTrp0;
235 }
236 else if (pp->type == bncParam::GALILEO_OFFSET) {
237 _QQ(iPar,iPar) = _sigGalileoOffset0 * _sigGalileoOffset0;
238 }
239 }
240
241 // NMEA Output
242 // -----------
243 QString nmeaFileName = settings.value("nmeaFile").toString();
244 if (nmeaFileName.isEmpty()) {
245 _nmeaFile = 0;
246 _nmeaStream = 0;
247 }
248 else {
249 expandEnvVar(nmeaFileName);
250 _nmeaFile = new QFile(nmeaFileName);
251 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
252 _nmeaFile->open(QIODevice::WriteOnly | QIODevice::Append);
253 }
254 else {
255 _nmeaFile->open(QIODevice::WriteOnly);
256 }
257 _nmeaStream = new QTextStream();
258 _nmeaStream->setDevice(_nmeaFile);
259 }
260
261 QString antexFileName = settings.value("pppAntex").toString();
262 if (!antexFileName.isEmpty()) {
263 _antex = new bncAntex();
264 _antex->readFile(antexFileName);
265 }
266}
267
268// Destructor
269////////////////////////////////////////////////////////////////////////////
270bncModel::~bncModel() {
271 delete _nmeaStream;
272 delete _nmeaFile;
273 for (int ii = 0; ii < _posAverage.size(); ++ii) {
274 delete _posAverage[ii];
275 }
276 delete _antex;
277}
278
279// Bancroft Solution
280////////////////////////////////////////////////////////////////////////////
281t_irc bncModel::cmpBancroft(t_epoData* epoData) {
282
283 if (epoData->sizeGPS() < MINOBS) {
284 _log += "bncModel::cmpBancroft: not enough data\n";
285 return failure;
286 }
287
288 Matrix BB(epoData->sizeGPS(), 4);
289
290 QMapIterator<QString, t_satData*> it(epoData->satDataGPS);
291 int iObsBanc = 0;
292 while (it.hasNext()) {
293 ++iObsBanc;
294 it.next();
295 QString prn = it.key();
296 t_satData* satData = it.value();
297 BB(iObsBanc, 1) = satData->xx(1);
298 BB(iObsBanc, 2) = satData->xx(2);
299 BB(iObsBanc, 3) = satData->xx(3);
300 BB(iObsBanc, 4) = satData->P3 + satData->clk;
301 }
302
303 bancroft(BB, _xcBanc);
304
305 // Ellipsoidal Coordinates
306 // ------------------------
307 xyz2ell(_xcBanc.data(), _ellBanc.data());
308
309 // Compute Satellite Elevations
310 // ----------------------------
311 QMutableMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
312 while (iGPS.hasNext()) {
313 iGPS.next();
314 t_satData* satData = iGPS.value();
315 cmpEle(satData);
316 if (satData->eleSat < MINELE_GPS) {
317 delete satData;
318 iGPS.remove();
319 }
320 }
321
322 QMutableMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
323 while (iGlo.hasNext()) {
324 iGlo.next();
325 t_satData* satData = iGlo.value();
326 cmpEle(satData);
327 if (satData->eleSat < MINELE_GLO) {
328 delete satData;
329 iGlo.remove();
330 }
331 }
332
333 QMutableMapIterator<QString, t_satData*> iGal(epoData->satDataGal);
334 while (iGal.hasNext()) {
335 iGal.next();
336 t_satData* satData = iGal.value();
337 cmpEle(satData);
338 if (satData->eleSat < MINELE_GAL) {
339 delete satData;
340 iGal.remove();
341 }
342 }
343
344 return success;
345}
346
347// Computed Value
348////////////////////////////////////////////////////////////////////////////
349double bncModel::cmpValue(t_satData* satData, bool phase) {
350
351 ColumnVector xRec(3);
352 xRec(1) = x();
353 xRec(2) = y();
354 xRec(3) = z();
355
356 double rho0 = (satData->xx - xRec).norm_Frobenius();
357 double dPhi = t_CST::omega * rho0 / t_CST::c;
358
359 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
360 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
361 xRec(3) = z();
362
363 tides(_time, xRec);
364
365 satData->rho = (satData->xx - xRec).norm_Frobenius();
366
367 double tropDelay = delay_saast(satData->eleSat) +
368 trp() / sin(satData->eleSat);
369
370 double wind = 0.0;
371 if (phase) {
372 wind = windUp(satData->prn, satData->xx, xRec) * satData->lambda3;
373 }
374
375 double offset = 0.0;
376 if (satData->prn[0] == 'E') {
377 offset = Galileo_offset();
378 }
379
380 return satData->rho + clk() + offset - satData->clk + tropDelay + wind;
381}
382
383// Tropospheric Model (Saastamoinen)
384////////////////////////////////////////////////////////////////////////////
385double bncModel::delay_saast(double Ele) {
386
387 double xyz[3];
388 xyz[0] = x();
389 xyz[1] = y();
390 xyz[2] = z();
391 double ell[3];
392 xyz2ell(xyz, ell);
393 double height = ell[2];
394
395 double pp = 1013.25 * pow(1.0 - 2.26e-5 * height, 5.225);
396 double TT = 18.0 - height * 0.0065 + 273.15;
397 double hh = 50.0 * exp(-6.396e-4 * height);
398 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
399
400 double h_km = height / 1000.0;
401
402 if (h_km < 0.0) h_km = 0.0;
403 if (h_km > 5.0) h_km = 5.0;
404 int ii = int(h_km + 1);
405 double href = ii - 1;
406
407 double bCor[6];
408 bCor[0] = 1.156;
409 bCor[1] = 1.006;
410 bCor[2] = 0.874;
411 bCor[3] = 0.757;
412 bCor[4] = 0.654;
413 bCor[5] = 0.563;
414
415 double BB = bCor[ii-1] + (bCor[ii]-bCor[ii-1]) * (h_km - href);
416
417 double zen = M_PI/2.0 - Ele;
418
419 return (0.002277/cos(zen)) * (pp + ((1255.0/TT)+0.05)*ee - BB*(tan(zen)*tan(zen)));
420}
421
422// Prediction Step of the Filter
423////////////////////////////////////////////////////////////////////////////
424void bncModel::predict(t_epoData* epoData) {
425
426 bncSettings settings;
427
428 bool firstCrd = false;
429 if (x() == 0.0 && y() == 0.0 && z() == 0.0) {
430 firstCrd = true;
431 _startTime = epoData->tt;
432 }
433
434 // Use different white noise for Quick-Start mode
435 // ----------------------------------------------
436 double sigCrdP_used = _sigCrdP;
437 if ( _quickStart > 0.0 && _quickStart > (epoData->tt - _startTime) ) {
438 sigCrdP_used = 0.0;
439 }
440
441 // Predict Parameter values, add white noise
442 // -----------------------------------------
443 for (int iPar = 1; iPar <= _params.size(); iPar++) {
444 bncParam* pp = _params[iPar-1];
445
446 // Coordinates
447 // -----------
448 if (pp->type == bncParam::CRD_X) {
449 if (firstCrd) {
450 if (settings.value("pppRefCrdX").toString() != "" &&
451 settings.value("pppRefCrdY").toString() != "" &&
452 settings.value("pppRefCrdZ").toString() != "") {
453 pp->xx = settings.value("pppRefCrdX").toDouble();
454 }
455 else {
456 pp->xx = _xcBanc(1);
457 }
458 }
459 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
460 }
461 else if (pp->type == bncParam::CRD_Y) {
462 if (firstCrd) {
463 if (settings.value("pppRefCrdX").toString() != "" &&
464 settings.value("pppRefCrdY").toString() != "" &&
465 settings.value("pppRefCrdZ").toString() != "") {
466 pp->xx = settings.value("pppRefCrdY").toDouble();
467 }
468 else {
469 pp->xx = _xcBanc(2);
470 }
471 }
472 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
473 }
474 else if (pp->type == bncParam::CRD_Z) {
475 if (firstCrd) {
476 if (settings.value("pppRefCrdX").toString() != "" &&
477 settings.value("pppRefCrdY").toString() != "" &&
478 settings.value("pppRefCrdZ").toString() != "") {
479 pp->xx = settings.value("pppRefCrdZ").toDouble();
480 }
481 else {
482 pp->xx = _xcBanc(3);
483 }
484 }
485 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
486 }
487
488 // Receiver Clocks
489 // ---------------
490 else if (pp->type == bncParam::RECCLK) {
491 pp->xx = _xcBanc(4);
492 for (int jj = 1; jj <= _params.size(); jj++) {
493 _QQ(iPar, jj) = 0.0;
494 }
495 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
496 }
497
498 // Tropospheric Delay
499 // ------------------
500 else if (pp->type == bncParam::TROPO) {
501 _QQ(iPar,iPar) += _sigTrpP * _sigTrpP;
502 }
503
504 // Galileo Offset
505 // --------------
506 else if (pp->type == bncParam::GALILEO_OFFSET) {
507 _QQ(iPar,iPar) += _sigGalileoOffsetP * _sigGalileoOffsetP;
508 }
509 }
510
511 // Add New Ambiguities if necessary
512 // --------------------------------
513 if (_usePhase) {
514
515 // Make a copy of QQ and xx, set parameter indices
516 // -----------------------------------------------
517 SymmetricMatrix QQ_old = _QQ;
518
519 for (int iPar = 1; iPar <= _params.size(); iPar++) {
520 _params[iPar-1]->index_old = _params[iPar-1]->index;
521 _params[iPar-1]->index = 0;
522 }
523
524 // Remove Ambiguity Parameters without observations
525 // ------------------------------------------------
526 int iPar = 0;
527 QMutableVectorIterator<bncParam*> it(_params);
528 while (it.hasNext()) {
529 bncParam* par = it.next();
530 bool removed = false;
531 if (par->type == bncParam::AMB_L3) {
532 if (epoData->satDataGPS.find(par->prn) == epoData->satDataGPS.end() &&
533 epoData->satDataGlo.find(par->prn) == epoData->satDataGlo.end() &&
534 epoData->satDataGal.find(par->prn) == epoData->satDataGal.end() ) {
535 removed = true;
536 delete par;
537 it.remove();
538 }
539 }
540 if (! removed) {
541 ++iPar;
542 par->index = iPar;
543 }
544 }
545
546 // Add new ambiguity parameters
547 // ----------------------------
548 QMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
549 while (iGPS.hasNext()) {
550 iGPS.next();
551 t_satData* satData = iGPS.value();
552 addAmb(satData);
553 }
554
555 QMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
556 while (iGlo.hasNext()) {
557 iGlo.next();
558 t_satData* satData = iGlo.value();
559 addAmb(satData);
560 }
561
562 QMapIterator<QString, t_satData*> iGal(epoData->satDataGal);
563 while (iGal.hasNext()) {
564 iGal.next();
565 t_satData* satData = iGal.value();
566 addAmb(satData);
567 }
568
569 int nPar = _params.size();
570 _QQ.ReSize(nPar); _QQ = 0.0;
571 for (int i1 = 1; i1 <= nPar; i1++) {
572 bncParam* p1 = _params[i1-1];
573 if (p1->index_old != 0) {
574 _QQ(p1->index, p1->index) = QQ_old(p1->index_old, p1->index_old);
575 for (int i2 = 1; i2 <= nPar; i2++) {
576 bncParam* p2 = _params[i2-1];
577 if (p2->index_old != 0) {
578 _QQ(p1->index, p2->index) = QQ_old(p1->index_old, p2->index_old);
579 }
580 }
581 }
582 }
583
584 for (int ii = 1; ii <= nPar; ii++) {
585 bncParam* par = _params[ii-1];
586 if (par->index_old == 0) {
587 _QQ(par->index, par->index) = _sigAmb0 * _sigAmb0;
588 }
589 par->index_old = par->index;
590 }
591 }
592}
593
594// Update Step of the Filter (currently just a single-epoch solution)
595////////////////////////////////////////////////////////////////////////////
596t_irc bncModel::update(t_epoData* epoData) {
597
598 bncSettings settings;
599
600 _log.clear();
601
602 _time = epoData->tt;
603
604 if (settings.value("pppSPP").toString() == "PPP") {
605 _log += "Precise Point Positioning of Epoch "
606 + QByteArray(_time.timestr(1).c_str()) +
607 "\n---------------------------------------------------------------\n";
608 }
609 else {
610 _log += "Single Point Positioning of Epoch "
611 + QByteArray(_time.timestr(1).c_str()) +
612 "\n--------------------------------------------------------------\n";
613 }
614
615 SymmetricMatrix QQsav;
616 ColumnVector dx;
617 ColumnVector vv;
618
619 // Loop over all outliers
620 // ----------------------
621 do {
622
623 // Bancroft Solution
624 // -----------------
625 if (cmpBancroft(epoData) != success) {
626 emit newMessage(_log, false);
627 return failure;
628 }
629
630 // Status Prediction
631 // -----------------
632 predict(epoData);
633
634 // Create First-Design Matrix
635 // --------------------------
636 unsigned nPar = _params.size();
637 unsigned nObs = 0;
638 if (_usePhase) {
639 nObs = 2 * (epoData->sizeGPS() + epoData->sizeGal()) + epoData->sizeGlo();
640 }
641 else {
642 nObs = epoData->sizeGPS() + epoData->sizeGal(); // Glonass code not used
643 }
644
645 if (nObs < nPar) {
646 _log += "bncModel::update: nObs < nPar\n";
647 emit newMessage(_log, false);
648 return failure;
649 }
650
651 Matrix AA(nObs, nPar); // first design matrix
652 ColumnVector ll(nObs); // tems observed-computed
653 DiagonalMatrix PP(nObs); PP = 0.0;
654
655 unsigned iObs = 0;
656
657 // GPS code and (optionally) phase observations
658 // --------------------------------------------
659 QMapIterator<QString, t_satData*> itGPS(epoData->satDataGPS);
660 while (itGPS.hasNext()) {
661 itGPS.next();
662 t_satData* satData = itGPS.value();
663 addObs(iObs, satData, AA, ll, PP);
664 }
665
666 // Glonass phase observations
667 // --------------------------
668 QMapIterator<QString, t_satData*> itGlo(epoData->satDataGlo);
669 while (itGlo.hasNext()) {
670 itGlo.next();
671 t_satData* satData = itGlo.value();
672 addObs(iObs, satData, AA, ll, PP);
673 }
674
675 // Galileo code and (optionally) phase observations
676 // ------------------------------------------------
677 QMapIterator<QString, t_satData*> itGal(epoData->satDataGal);
678 while (itGal.hasNext()) {
679 itGal.next();
680 t_satData* satData = itGal.value();
681 addObs(iObs, satData, AA, ll, PP);
682 }
683
684 // Compute Filter Update
685 // ---------------------
686 QQsav = _QQ;
687
688 kalman(AA, ll, PP, _QQ, dx);
689
690 vv = ll - AA * dx;
691
692 // Print Residuals
693 // ---------------
694 if (true) {
695 ostringstream str;
696 str.setf(ios::fixed);
697
698 QMapIterator<QString, t_satData*> itGPS(epoData->satDataGPS);
699 while (itGPS.hasNext()) {
700 itGPS.next();
701 t_satData* satData = itGPS.value();
702 printRes(vv, str, satData);
703 }
704 QMapIterator<QString, t_satData*> itGlo(epoData->satDataGlo);
705 while (itGlo.hasNext()) {
706 itGlo.next();
707 t_satData* satData = itGlo.value();
708 printRes(vv, str, satData);
709 }
710 QMapIterator<QString, t_satData*> itGal(epoData->satDataGal);
711 while (itGal.hasNext()) {
712 itGal.next();
713 t_satData* satData = itGal.value();
714 printRes(vv, str, satData);
715 }
716 _log += str.str().c_str();
717 }
718
719 } while (outlierDetection(QQsav, vv, epoData->satDataGPS,
720 epoData->satDataGlo, epoData->satDataGal) != 0);
721
722 // Remember the Epoch-specific Results for the computation of means
723 // ----------------------------------------------------------------
724 pppPos* newPos = new pppPos;
725 newPos->time = epoData->tt;
726
727 // Set Solution Vector
728 // -------------------
729 ostringstream strB;
730 strB.setf(ios::fixed);
731 QVectorIterator<bncParam*> itPar(_params);
732 while (itPar.hasNext()) {
733 bncParam* par = itPar.next();
734 par->xx += dx(par->index);
735
736 if (par->type == bncParam::RECCLK) {
737 strB << "\n clk = " << setw(10) << setprecision(3) << par->xx
738 << " +- " << setw(6) << setprecision(3)
739 << sqrt(_QQ(par->index,par->index));
740 }
741 else if (par->type == bncParam::AMB_L3) {
742 strB << "\n amb " << par->prn.toAscii().data() << " = "
743 << setw(10) << setprecision(3) << par->xx
744 << " +- " << setw(6) << setprecision(3)
745 << sqrt(_QQ(par->index,par->index));
746 }
747 else if (par->type == bncParam::TROPO) {
748 double aprTrp = delay_saast(M_PI/2.0);
749 strB << "\n trp = " << par->prn.toAscii().data()
750 << setw(7) << setprecision(3) << aprTrp << " "
751 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
752 << " +- " << setw(6) << setprecision(3)
753 << sqrt(_QQ(par->index,par->index));
754 newPos->xnt[6] = aprTrp + par->xx;
755 }
756 else if (par->type == bncParam::GALILEO_OFFSET) {
757 strB << "\n offset = " << setw(10) << setprecision(3) << par->xx
758 << " +- " << setw(6) << setprecision(3)
759 << sqrt(_QQ(par->index,par->index));
760 }
761 }
762 strB << '\n';
763 _log += strB.str().c_str();
764 emit newMessage(_log, false);
765
766 // Final Message (both log file and screen)
767 // ----------------------------------------
768 ostringstream strC;
769 strC.setf(ios::fixed);
770 strC << _staID.data() << " PPP "
771 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
772 << setw(14) << setprecision(3) << x() << " +- "
773 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
774 << setw(14) << setprecision(3) << y() << " +- "
775 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
776 << setw(14) << setprecision(3) << z() << " +- "
777 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
778
779 // NEU Output
780 // ----------
781 double xyzRef[3];
782
783 if (settings.value("pppRefCrdX").toString() != "" &&
784 settings.value("pppRefCrdY").toString() != "" &&
785 settings.value("pppRefCrdZ").toString() != "") {
786
787 xyzRef[0] = settings.value("pppRefCrdX").toDouble();
788 xyzRef[1] = settings.value("pppRefCrdY").toDouble();
789 xyzRef[2] = settings.value("pppRefCrdZ").toDouble();
790
791 newPos->xnt[0] = x() - xyzRef[0];
792 newPos->xnt[1] = y() - xyzRef[1];
793 newPos->xnt[2] = z() - xyzRef[2];
794
795 double ellRef[3];
796 xyz2ell(xyzRef, ellRef);
797 xyz2neu(ellRef, newPos->xnt, &newPos->xnt[3]);
798
799 strC << " NEU "
800 << setw(8) << setprecision(3) << newPos->xnt[3] << " "
801 << setw(8) << setprecision(3) << newPos->xnt[4] << " "
802 << setw(8) << setprecision(3) << newPos->xnt[5] << endl;
803
804 }
805
806 emit newMessage(QByteArray(strC.str().c_str()), true);
807
808 if (settings.value("pppAverage").toString() == "") {
809 delete newPos;
810 }
811 else {
812
813 _posAverage.push_back(newPos);
814
815 // Time Span for Average Computation
816 // ---------------------------------
817 double tRangeAverage = settings.value("pppAverage").toDouble() * 60.;
818 if (tRangeAverage < 0) {
819 tRangeAverage = 0;
820 }
821 if (tRangeAverage > 86400) {
822 tRangeAverage = 86400;
823 }
824
825 // Compute the Mean
826 // ----------------
827 ColumnVector mean(7); mean = 0.0;
828
829 QMutableVectorIterator<pppPos*> it(_posAverage);
830 while (it.hasNext()) {
831 pppPos* pp = it.next();
832 if ( (epoData->tt - pp->time) >= tRangeAverage ) {
833 delete pp;
834 it.remove();
835 }
836 else {
837 for (int ii = 0; ii < 7; ++ii) {
838 mean[ii] += pp->xnt[ii];
839 }
840 }
841 }
842
843 int nn = _posAverage.size();
844
845 if (nn > 0) {
846
847 mean /= nn;
848
849 // Compute the Deviation
850 // ---------------------
851 ColumnVector std(7); std = 0.0;
852 QVectorIterator<pppPos*> it2(_posAverage);
853 while (it2.hasNext()) {
854 pppPos* pp = it2.next();
855 for (int ii = 0; ii < 7; ++ii) {
856 std[ii] += (pp->xnt[ii] - mean[ii]) * (pp->xnt[ii] - mean[ii]);
857 }
858 }
859 for (int ii = 0; ii < 7; ++ii) {
860 std[ii] = sqrt(std[ii] / nn);
861 }
862
863 ostringstream strD; strD.setf(ios::fixed);
864 strD << _staID.data() << " AVE-XYZ "
865 << epoData->tt.timestr(1) << " "
866// << setw(13) << setprecision(3) << mean[0] << " +- "
867 << setw(13) << setprecision(3) << mean[0] + xyzRef[0] << " +- "
868 << setw(6) << setprecision(3) << std[0] << " "
869// << setw(14) << setprecision(3) << mean[1] << " +- "
870 << setw(14) << setprecision(3) << mean[1] + xyzRef[1] << " +- "
871 << setw(6) << setprecision(3) << std[1] << " "
872// << setw(14) << setprecision(3) << mean[2] << " +- "
873 << setw(14) << setprecision(3) << mean[2] + xyzRef[2] << " +- "
874 << setw(6) << setprecision(3) << std[2];
875 emit newMessage(QByteArray(strD.str().c_str()), true);
876
877 ostringstream strE; strE.setf(ios::fixed);
878 strE << _staID.data() << " AVE-NEU "
879 << epoData->tt.timestr(1) << " "
880 << setw(13) << setprecision(3) << mean[3] << " +- "
881 << setw(6) << setprecision(3) << std[3] << " "
882 << setw(14) << setprecision(3) << mean[4] << " +- "
883 << setw(6) << setprecision(3) << std[4] << " "
884 << setw(14) << setprecision(3) << mean[5] << " +- "
885 << setw(6) << setprecision(3) << std[5];
886
887 emit newMessage(QByteArray(strE.str().c_str()), true);
888
889 if ( Qt::CheckState(settings.value("pppEstTropo").toInt()) == Qt::Checked) {
890 ostringstream strF; strF.setf(ios::fixed);
891 strF << _staID.data() << " AVE-TRP "
892 << epoData->tt.timestr(1) << " "
893 << setw(13) << setprecision(3) << mean[6] << " +- "
894 << setw(6) << setprecision(3) << std[6] << endl;
895 emit newMessage(QByteArray(strF.str().c_str()), true);
896 }
897 }
898 }
899
900 // NMEA Output
901 // -----------
902 double xyz[3];
903 xyz[0] = x();
904 xyz[1] = y();
905 xyz[2] = z();
906 double ell[3];
907 xyz2ell(xyz, ell);
908 double phiDeg = ell[0] * 180 / M_PI;
909 double lamDeg = ell[1] * 180 / M_PI;
910
911 char phiCh = 'N';
912 if (phiDeg < 0) {
913 phiDeg = -phiDeg;
914 phiCh = 'S';
915 }
916 char lamCh = 'E';
917 if (lamDeg < 0) {
918 lamDeg = -lamDeg;
919 lamCh = 'W';
920 }
921
922 string datestr = epoData->tt.datestr(0); // yyyymmdd
923 ostringstream strRMC;
924 strRMC.setf(ios::fixed);
925 strRMC << "GPRMC,"
926 << epoData->tt.timestr(0,0) << ",A,"
927 << setw(2) << setfill('0') << int(phiDeg)
928 << setw(6) << setprecision(3) << setfill('0')
929 << fmod(60*phiDeg,60) << ',' << phiCh << ','
930 << setw(3) << setfill('0') << int(lamDeg)
931 << setw(6) << setprecision(3) << setfill('0')
932 << fmod(60*lamDeg,60) << ',' << lamCh << ",,,"
933 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
934 << datestr[2] << datestr[3] << ",,";
935
936 writeNMEAstr(QString(strRMC.str().c_str()));
937
938 double dop = 2.0; // TODO
939
940 ostringstream strGGA;
941 strGGA.setf(ios::fixed);
942 strGGA << "GPGGA,"
943 << epoData->tt.timestr(0,0) << ','
944 << setw(2) << setfill('0') << int(phiDeg)
945 << setw(10) << setprecision(7) << setfill('0')
946 << fmod(60*phiDeg,60) << ',' << phiCh << ','
947 << setw(3) << setfill('0') << int(lamDeg)
948 << setw(10) << setprecision(7) << setfill('0')
949 << fmod(60*lamDeg,60) << ',' << lamCh
950 << ",1," << setw(2) << setfill('0') << epoData->sizeAll() << ','
951 << setw(3) << setprecision(1) << dop << ','
952 << setprecision(3) << ell[2] << ",M,0.0,M,,";
953
954 writeNMEAstr(QString(strGGA.str().c_str()));
955
956 return success;
957}
958
959// Outlier Detection
960////////////////////////////////////////////////////////////////////////////
961int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
962 const ColumnVector& vv,
963 QMap<QString, t_satData*>& satDataGPS,
964 QMap<QString, t_satData*>& satDataGlo,
965 QMap<QString, t_satData*>& satDataGal) {
966
967 QString prnCode;
968 QString prnPhase;
969 double maxResCode = 0.0;
970 double maxResPhase = 0.0;
971
972 QString prnRemoved;
973 double maxRes;
974
975 int irc = 0;
976
977 // Check Glonass
978 // -------------
979 if (irc == 0) {
980 findMaxRes(vv,satDataGlo, prnCode, maxResCode, prnPhase, maxResPhase);
981 if (maxResPhase > MAXRES_PHASE_GLO) {
982 satDataGlo.remove(prnPhase);
983 prnRemoved = prnPhase;
984 maxRes = maxResPhase;
985 irc = 1;
986 }
987 }
988
989 // Check Galileo
990 // -------------
991 if (irc == 0) {
992 findMaxRes(vv,satDataGal, prnCode, maxResCode, prnPhase, maxResPhase);
993 if (maxResPhase > MAXRES_PHASE_GAL) {
994 satDataGal.remove(prnPhase);
995 prnRemoved = prnPhase;
996 maxRes = maxResPhase;
997 irc = 1;
998 }
999 else if (maxResCode > MAXRES_CODE_GAL) {
1000 satDataGal.remove(prnCode);
1001 prnRemoved = prnCode;
1002 maxRes = maxResCode;
1003 irc = 1;
1004 }
1005 }
1006
1007 // Check GPS
1008 // ---------
1009 if (irc == 0) {
1010 findMaxRes(vv,satDataGPS, prnCode, maxResCode, prnPhase, maxResPhase);
1011 if (maxResPhase > MAXRES_PHASE_GPS) {
1012 satDataGPS.remove(prnPhase);
1013 prnRemoved = prnPhase;
1014 maxRes = maxResPhase;
1015 irc = 1;
1016 }
1017 else if (maxResCode > MAXRES_CODE_GPS) {
1018 satDataGPS.remove(prnCode);
1019 prnRemoved = prnCode;
1020 maxRes = maxResCode;
1021 irc = 1;
1022 }
1023 }
1024
1025 if (irc != 0) {
1026 _log += "Outlier " + prnRemoved.toAscii() + " "
1027 + QByteArray::number(maxRes, 'f', 3) + "\n";
1028 _QQ = QQsav;
1029 }
1030
1031 return irc;
1032}
1033
1034//
1035////////////////////////////////////////////////////////////////////////////
1036void bncModel::writeNMEAstr(const QString& nmStr) {
1037
1038 unsigned char XOR = 0;
1039 for (int ii = 0; ii < nmStr.length(); ii++) {
1040 XOR ^= (unsigned char) nmStr[ii].toAscii();
1041 }
1042
1043 QString outStr = '$' + nmStr
1044 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
1045
1046 if (_nmeaStream) {
1047 *_nmeaStream << outStr;
1048 _nmeaStream->flush();
1049 }
1050
1051 emit newNMEAstr(outStr.toAscii());
1052}
1053
1054////
1055//////////////////////////////////////////////////////////////////////////////
1056void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
1057 const DiagonalMatrix& PP,
1058 SymmetricMatrix& QQ, ColumnVector& dx) {
1059
1060 int nObs = AA.Nrows();
1061 int nPar = AA.Ncols();
1062
1063 UpperTriangularMatrix SS = Cholesky(QQ).t();
1064
1065 Matrix SA = SS*AA.t();
1066 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
1067 for (int ii = 1; ii <= nObs; ++ii) {
1068 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
1069 }
1070
1071 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
1072 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
1073
1074 UpperTriangularMatrix UU;
1075 QRZ(SRF, UU);
1076
1077 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
1078 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
1079 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
1080
1081 UpperTriangularMatrix SHi = SH_rt.i();
1082
1083 Matrix KT = SHi * YY;
1084 SymmetricMatrix Hi; Hi << SHi * SHi.t();
1085
1086 dx = KT.t() * ll;
1087 QQ << (SS.t() * SS);
1088}
1089
1090// Phase Wind-Up Correction
1091///////////////////////////////////////////////////////////////////////////
1092double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
1093 const ColumnVector& rRec) {
1094
1095 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
1096
1097 // First time - initialize to zero
1098 // -------------------------------
1099 if (!_windUpTime.contains(prn)) {
1100 _windUpTime[prn] = Mjd;
1101 _windUpSum[prn] = 0.0;
1102 }
1103
1104 // Compute the correction for new time
1105 // -----------------------------------
1106 else if (_windUpTime[prn] != Mjd) {
1107 _windUpTime[prn] = Mjd;
1108
1109 // Unit Vector GPS Satellite --> Receiver
1110 // --------------------------------------
1111 ColumnVector rho = rRec - rSat;
1112 rho /= rho.norm_Frobenius();
1113
1114 // GPS Satellite unit Vectors sz, sy, sx
1115 // -------------------------------------
1116 ColumnVector sz = -rSat / rSat.norm_Frobenius();
1117
1118 ColumnVector xSun = Sun(Mjd);
1119 xSun /= xSun.norm_Frobenius();
1120
1121 ColumnVector sy = crossproduct(sz, xSun);
1122 ColumnVector sx = crossproduct(sy, sz);
1123
1124 // Effective Dipole of the GPS Satellite Antenna
1125 // ---------------------------------------------
1126 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
1127 - crossproduct(rho, sy);
1128
1129 // Receiver unit Vectors rx, ry
1130 // ----------------------------
1131 ColumnVector rx(3);
1132 ColumnVector ry(3);
1133
1134 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
1135 double neu[3];
1136
1137 neu[0] = 1.0;
1138 neu[1] = 0.0;
1139 neu[2] = 0.0;
1140 neu2xyz(recEll, neu, rx.data());
1141
1142 neu[0] = 0.0;
1143 neu[1] = -1.0;
1144 neu[2] = 0.0;
1145 neu2xyz(recEll, neu, ry.data());
1146
1147 // Effective Dipole of the Receiver Antenna
1148 // ----------------------------------------
1149 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
1150 + crossproduct(rho, ry);
1151
1152 // Resulting Effect
1153 // ----------------
1154 double alpha = DotProduct(dipSat,dipRec) /
1155 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
1156
1157 if (alpha > 1.0) alpha = 1.0;
1158 if (alpha < -1.0) alpha = -1.0;
1159
1160 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
1161
1162 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
1163 dphi = -dphi;
1164 }
1165
1166 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
1167 }
1168
1169 return _windUpSum[prn];
1170}
1171
1172//
1173///////////////////////////////////////////////////////////////////////////
1174void bncModel::cmpEle(t_satData* satData) {
1175 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
1176 double rho = rr.norm_Frobenius();
1177
1178 double neu[3];
1179 xyz2neu(_ellBanc.data(), rr.data(), neu);
1180
1181 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
1182 if (neu[2] < 0) {
1183 satData->eleSat *= -1.0;
1184 }
1185 satData->azSat = atan2(neu[1], neu[0]);
1186}
1187
1188//
1189///////////////////////////////////////////////////////////////////////////
1190void bncModel::addAmb(t_satData* satData) {
1191 bool found = false;
1192 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1193 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1194 _params[iPar-1]->prn == satData->prn) {
1195 found = true;
1196 break;
1197 }
1198 }
1199 if (!found) {
1200 bncParam* par = new bncParam(bncParam::AMB_L3,
1201 _params.size()+1, satData->prn);
1202 _params.push_back(par);
1203 par->xx = satData->L3 - cmpValue(satData, true);
1204 }
1205}
1206
1207//
1208///////////////////////////////////////////////////////////////////////////
1209void bncModel::addObs(unsigned& iObs, t_satData* satData,
1210 Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP) {
1211
1212 // Code Observations
1213 // -----------------
1214 if (satData->system() != 'R') {
1215 ++iObs;
1216 ll(iObs) = satData->P3 - cmpValue(satData, false);
1217 PP(iObs,iObs) = 1.0 / (_sigP3 * _sigP3);
1218 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1219 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
1220 }
1221 satData->indexCode = iObs;
1222 }
1223
1224 // Phase Observations
1225 // ------------------
1226 if (_usePhase) {
1227 ++iObs;
1228 ll(iObs) = satData->L3 - cmpValue(satData, true);
1229 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
1230 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1231 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1232 _params[iPar-1]->prn == satData->prn) {
1233 ll(iObs) -= _params[iPar-1]->xx;
1234 }
1235 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
1236 }
1237 satData->indexPhase = iObs;
1238 }
1239}
1240
1241//
1242///////////////////////////////////////////////////////////////////////////
1243void bncModel::printRes(const ColumnVector& vv,
1244 ostringstream& str, t_satData* satData) {
1245 if (satData->indexPhase) {
1246 str << _time.timestr(1)
1247 << " RES " << satData->prn.toAscii().data() << " L3 "
1248 << setw(9) << setprecision(4) << vv(satData->indexPhase);
1249 if (satData->indexCode) {
1250 str << " P3 " << setw(9) << setprecision(4) << vv(satData->indexCode);
1251 }
1252 str << endl;
1253 }
1254}
1255
1256//
1257///////////////////////////////////////////////////////////////////////////
1258void bncModel::findMaxRes(const ColumnVector& vv,
1259 const QMap<QString, t_satData*>& satData,
1260 QString& prnCode, double& maxResCode,
1261 QString& prnPhase, double& maxResPhase) {
1262 maxResCode = 0.0;
1263 maxResPhase = 0.0;
1264
1265 QMapIterator<QString, t_satData*> it(satData);
1266 while (it.hasNext()) {
1267 it.next();
1268 t_satData* satData = it.value();
1269 if (satData->indexCode) {
1270 if (fabs(vv(satData->indexCode)) > maxResCode) {
1271 maxResCode = fabs(vv(satData->indexCode));
1272 prnCode = satData->prn;
1273 }
1274 }
1275 if (satData->indexPhase) {
1276 if (fabs(vv(satData->indexPhase)) > maxResPhase) {
1277 maxResPhase = fabs(vv(satData->indexPhase));
1278 prnPhase = satData->prn;
1279 }
1280 }
1281 }
1282}
1283
Note: See TracBrowser for help on using the repository browser.