source: ntrip/trunk/BNC/src/PPP_free/pppFilter.cpp@ 6111

Last change on this file since 6111 was 6111, checked in by mervart, 10 years ago
File size: 36.2 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: t_pppParam, t_pppFilter
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 <sstream>
44#include <newmatio.h>
45#include <newmatap.h>
46
47#include "pppFilter.h"
48#include "pppClient.h"
49#include "bncutils.h"
50#include "bncantex.h"
51#include "pppOptions.h"
52#include "pppModel.h"
53
54using namespace BNC_PPP;
55using namespace std;
56
57const unsigned MINOBS = 5;
58const double MINELE = 10.0 * M_PI / 180.0;
59const double MAXRES_CODE = 15.0;
60const double MAXRES_PHASE_GPS = 0.04;
61const double MAXRES_PHASE_GLONASS = 0.08;
62const double GLONASS_WEIGHT_FACTOR = 5.0;
63
64#define LOG (_pppClient->log())
65#define OPT (_pppClient->opt())
66
67// Constructor
68////////////////////////////////////////////////////////////////////////////
69t_pppParam::t_pppParam(t_pppParam::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 numEpo = 0;
77}
78
79// Destructor
80////////////////////////////////////////////////////////////////////////////
81t_pppParam::~t_pppParam() {
82}
83
84// Partial
85////////////////////////////////////////////////////////////////////////////
86double t_pppParam::partial(t_satData* satData, bool phase) {
87
88 Tracer tracer("t_pppParam::partial");
89
90 // Coordinates
91 // -----------
92 if (type == CRD_X) {
93 return (xx - satData->xx(1)) / satData->rho;
94 }
95 else if (type == CRD_Y) {
96 return (xx - satData->xx(2)) / satData->rho;
97 }
98 else if (type == CRD_Z) {
99 return (xx - satData->xx(3)) / satData->rho;
100 }
101
102 // Receiver Clocks
103 // ---------------
104 else if (type == RECCLK) {
105 return 1.0;
106 }
107
108 // Troposphere
109 // -----------
110 else if (type == TROPO) {
111 return 1.0 / sin(satData->eleSat);
112 }
113
114 // Glonass Offset
115 // --------------
116 else if (type == GLONASS_OFFSET) {
117 if (satData->prn[0] == 'R') {
118 return 1.0;
119 }
120 else {
121 return 0.0;
122 }
123 }
124
125 // Galileo Offset
126 // --------------
127 else if (type == GALILEO_OFFSET) {
128 if (satData->prn[0] == 'E') {
129 return 1.0;
130 }
131 else {
132 return 0.0;
133 }
134 }
135
136 // Ambiguities
137 // -----------
138 else if (type == AMB_L3) {
139 if (phase && satData->prn == prn) {
140 return 1.0;
141 }
142 else {
143 return 0.0;
144 }
145 }
146
147 // Default return
148 // --------------
149 return 0.0;
150}
151
152// Constructor
153////////////////////////////////////////////////////////////////////////////
154t_pppFilter::t_pppFilter(t_pppClient* pppClient) {
155
156 _pppClient = pppClient;
157 _tides = new t_tides();
158
159 // Antenna Name, ANTEX File
160 // ------------------------
161 _antex = 0;
162 if (!OPT->_antexFileName.empty()) {
163 _antex = new bncAntex(OPT->_antexFileName.c_str());
164 }
165
166 // Bancroft Coordinates
167 // --------------------
168 _xcBanc.ReSize(4); _xcBanc = 0.0;
169 _ellBanc.ReSize(3); _ellBanc = 0.0;
170
171 // Save copy of data (used in outlier detection)
172 // ---------------------------------------------
173 _epoData_sav = new t_epoData();
174}
175
176// Destructor
177////////////////////////////////////////////////////////////////////////////
178t_pppFilter::~t_pppFilter() {
179 delete _tides;
180 delete _antex;
181 for (int iPar = 1; iPar <= _params.size(); iPar++) {
182 delete _params[iPar-1];
183 }
184 for (int iPar = 1; iPar <= _params_sav.size(); iPar++) {
185 delete _params_sav[iPar-1];
186 }
187 delete _epoData_sav;
188}
189
190// Reset Parameters and Variance-Covariance Matrix
191////////////////////////////////////////////////////////////////////////////
192void t_pppFilter::reset() {
193
194 Tracer tracer("t_pppFilter::reset");
195
196 double lastTrp = 0.0;
197 for (int ii = 0; ii < _params.size(); ii++) {
198 t_pppParam* pp = _params[ii];
199 if (pp->type == t_pppParam::TROPO) {
200 lastTrp = pp->xx;
201 }
202 delete pp;
203 }
204 _params.clear();
205
206 int nextPar = 0;
207 _params.push_back(new t_pppParam(t_pppParam::CRD_X, ++nextPar, ""));
208 _params.push_back(new t_pppParam(t_pppParam::CRD_Y, ++nextPar, ""));
209 _params.push_back(new t_pppParam(t_pppParam::CRD_Z, ++nextPar, ""));
210 _params.push_back(new t_pppParam(t_pppParam::RECCLK, ++nextPar, ""));
211 if (OPT->estTrp()) {
212 _params.push_back(new t_pppParam(t_pppParam::TROPO, ++nextPar, ""));
213 }
214 if (OPT->useSystem('R')) {
215 _params.push_back(new t_pppParam(t_pppParam::GLONASS_OFFSET, ++nextPar, ""));
216 }
217 if (OPT->useSystem('E')) {
218 _params.push_back(new t_pppParam(t_pppParam::GALILEO_OFFSET, ++nextPar, ""));
219 }
220
221 _QQ.ReSize(_params.size());
222 _QQ = 0.0;
223 for (int iPar = 1; iPar <= _params.size(); iPar++) {
224 t_pppParam* pp = _params[iPar-1];
225 pp->xx = 0.0;
226 if (pp->isCrd()) {
227 _QQ(iPar,iPar) = OPT->_aprSigCrd(1) * OPT->_aprSigCrd(1);
228 }
229 else if (pp->type == t_pppParam::RECCLK) {
230 _QQ(iPar,iPar) = OPT->_noiseClk * OPT->_noiseClk;
231 }
232 else if (pp->type == t_pppParam::TROPO) {
233 _QQ(iPar,iPar) = OPT->_aprSigTrp * OPT->_aprSigTrp;
234 pp->xx = lastTrp;
235 }
236 else if (pp->type == t_pppParam::GLONASS_OFFSET) {
237 _QQ(iPar,iPar) = 1000.0 * 1000.0;
238 }
239 else if (pp->type == t_pppParam::GALILEO_OFFSET) {
240 _QQ(iPar,iPar) = 1000.0 * 1000.0;
241 }
242 }
243}
244
245// Bancroft Solution
246////////////////////////////////////////////////////////////////////////////
247t_irc t_pppFilter::cmpBancroft(t_epoData* epoData) {
248
249 Tracer tracer("t_pppFilter::cmpBancroft");
250
251 if (epoData->sizeSys('G') < MINOBS) {
252 LOG << "t_pppFilter::cmpBancroft: not enough data\n";
253 return failure;
254 }
255
256 Matrix BB(epoData->sizeSys('G'), 4);
257
258 QMapIterator<QString, t_satData*> it(epoData->satData);
259 int iObsBanc = 0;
260 while (it.hasNext()) {
261 it.next();
262 t_satData* satData = it.value();
263 if (satData->system() == 'G') {
264 ++iObsBanc;
265 QString prn = it.key();
266 BB(iObsBanc, 1) = satData->xx(1);
267 BB(iObsBanc, 2) = satData->xx(2);
268 BB(iObsBanc, 3) = satData->xx(3);
269 BB(iObsBanc, 4) = satData->P3 + satData->clk;
270 }
271 }
272
273 bancroft(BB, _xcBanc);
274
275 // Ellipsoidal Coordinates
276 // ------------------------
277 xyz2ell(_xcBanc.data(), _ellBanc.data());
278
279 // Compute Satellite Elevations
280 // ----------------------------
281 QMutableMapIterator<QString, t_satData*> im(epoData->satData);
282 while (im.hasNext()) {
283 im.next();
284 t_satData* satData = im.value();
285 cmpEle(satData);
286 if (satData->eleSat < MINELE) {
287 delete satData;
288 im.remove();
289 }
290 }
291
292 return success;
293}
294
295// Computed Value
296////////////////////////////////////////////////////////////////////////////
297double t_pppFilter::cmpValue(t_satData* satData, bool phase) {
298
299 Tracer tracer("t_pppFilter::cmpValue");
300
301 ColumnVector xRec(3);
302 xRec(1) = x();
303 xRec(2) = y();
304 xRec(3) = z();
305
306 double rho0 = (satData->xx - xRec).norm_Frobenius();
307 double dPhi = t_CST::omega * rho0 / t_CST::c;
308
309 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
310 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
311 xRec(3) = z();
312
313 xRec += _tides->displacement(_time, xRec);
314
315 satData->rho = (satData->xx - xRec).norm_Frobenius();
316
317 double tropDelay = delay_saast(satData->eleSat) +
318 trp() / sin(satData->eleSat);
319
320 double wind = 0.0;
321 if (phase) {
322 wind = windUp(satData->prn, satData->xx, xRec) * satData->lambda3;
323 }
324
325 double offset = 0.0;
326 if (satData->prn[0] == 'R') {
327 offset = Glonass_offset();
328 }
329 else if (satData->prn[0] == 'E') {
330 offset = Galileo_offset();
331 }
332
333 double phaseCenter = 0.0;
334 if (_antex) {
335 bool found;
336 phaseCenter = _antex->pco(QString(OPT->_antNameRover.c_str()), satData->eleSat, found);
337 if (!found) {
338 LOG << "ANTEX: antenna >" << OPT->_antNameRover << "< not found\n";
339 }
340 }
341
342 double antennaOffset = 0.0;
343 double cosa = cos(satData->azSat);
344 double sina = sin(satData->azSat);
345 double cose = cos(satData->eleSat);
346 double sine = sin(satData->eleSat);
347 antennaOffset = -OPT->_neuEccRover(1) * cosa*cose
348 -OPT->_neuEccRover(2) * sina*cose
349 -OPT->_neuEccRover(3) * sine;
350
351 return satData->rho + phaseCenter + antennaOffset + clk()
352 + offset - satData->clk + tropDelay + wind;
353}
354
355// Tropospheric Model (Saastamoinen)
356////////////////////////////////////////////////////////////////////////////
357double t_pppFilter::delay_saast(double Ele) {
358
359 Tracer tracer("t_pppFilter::delay_saast");
360
361 double xyz[3];
362 xyz[0] = x();
363 xyz[1] = y();
364 xyz[2] = z();
365 double ell[3];
366 xyz2ell(xyz, ell);
367 double height = ell[2];
368
369 double pp = 1013.25 * pow(1.0 - 2.26e-5 * height, 5.225);
370 double TT = 18.0 - height * 0.0065 + 273.15;
371 double hh = 50.0 * exp(-6.396e-4 * height);
372 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
373
374 double h_km = height / 1000.0;
375
376 if (h_km < 0.0) h_km = 0.0;
377 if (h_km > 5.0) h_km = 5.0;
378 int ii = int(h_km + 1);
379 double href = ii - 1;
380
381 double bCor[6];
382 bCor[0] = 1.156;
383 bCor[1] = 1.006;
384 bCor[2] = 0.874;
385 bCor[3] = 0.757;
386 bCor[4] = 0.654;
387 bCor[5] = 0.563;
388
389 double BB = bCor[ii-1] + (bCor[ii]-bCor[ii-1]) * (h_km - href);
390
391 double zen = M_PI/2.0 - Ele;
392
393 return (0.002277/cos(zen)) * (pp + ((1255.0/TT)+0.05)*ee - BB*(tan(zen)*tan(zen)));
394}
395
396// Prediction Step of the Filter
397////////////////////////////////////////////////////////////////////////////
398void t_pppFilter::predict(int iPhase, t_epoData* epoData) {
399
400 Tracer tracer("t_pppFilter::predict");
401
402 if (iPhase == 0) {
403
404 const double maxSolGap = 0.0;
405
406 bool firstCrd = false;
407 if (!_lastTimeOK.valid() || (maxSolGap > 0.0 && _time - _lastTimeOK > maxSolGap)) {
408 firstCrd = true;
409 _startTime = epoData->tt;
410 reset();
411 }
412
413 // Use different white noise for Quick-Start mode
414 // ----------------------------------------------
415 double sigCrdP_used = OPT->_noiseCrd(1);
416 if ( OPT->_seedingTime > 0.0 && OPT->_seedingTime > (epoData->tt - _startTime) ) {
417 sigCrdP_used = 0.0;
418 }
419
420 // Predict Parameter values, add white noise
421 // -----------------------------------------
422 for (int iPar = 1; iPar <= _params.size(); iPar++) {
423 t_pppParam* pp = _params[iPar-1];
424
425 // Coordinates
426 // -----------
427 if (pp->type == t_pppParam::CRD_X) {
428 if (firstCrd) {
429 if (OPT->xyzAprRoverSet()) {
430 pp->xx = OPT->_xyzAprRover[0];
431 }
432 else {
433 pp->xx = _xcBanc(1);
434 }
435 }
436 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
437 }
438 else if (pp->type == t_pppParam::CRD_Y) {
439 if (firstCrd) {
440 if (OPT->xyzAprRoverSet()) {
441 pp->xx = OPT->_xyzAprRover[1];
442 }
443 else {
444 pp->xx = _xcBanc(2);
445 }
446 }
447 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
448 }
449 else if (pp->type == t_pppParam::CRD_Z) {
450 if (firstCrd) {
451 if (OPT->xyzAprRoverSet()) {
452 pp->xx = OPT->_xyzAprRover[2];
453 }
454 else {
455 pp->xx = _xcBanc(3);
456 }
457 }
458 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
459 }
460
461 // Receiver Clocks
462 // ---------------
463 else if (pp->type == t_pppParam::RECCLK) {
464 pp->xx = _xcBanc(4);
465 for (int jj = 1; jj <= _params.size(); jj++) {
466 _QQ(iPar, jj) = 0.0;
467 }
468 _QQ(iPar,iPar) = OPT->_noiseClk * OPT->_noiseClk;
469 }
470
471 // Tropospheric Delay
472 // ------------------
473 else if (pp->type == t_pppParam::TROPO) {
474 _QQ(iPar,iPar) += OPT->_noiseTrp * OPT->_noiseTrp;
475 }
476
477 // Glonass Offset
478 // --------------
479 else if (pp->type == t_pppParam::GLONASS_OFFSET) {
480 pp->xx = 0.0;
481 for (int jj = 1; jj <= _params.size(); jj++) {
482 _QQ(iPar, jj) = 0.0;
483 }
484 _QQ(iPar,iPar) = 1000.0 * 1000.0;
485 }
486
487 // Galileo Offset
488 // --------------
489 else if (pp->type == t_pppParam::GALILEO_OFFSET) {
490 _QQ(iPar,iPar) += 0.1 * 0.1;
491 }
492 }
493 }
494
495 // Add New Ambiguities if necessary
496 // --------------------------------
497 if (OPT->ambLCs('G').size() || OPT->ambLCs('R').size() || OPT->ambLCs('E').size()) {
498
499 // Make a copy of QQ and xx, set parameter indices
500 // -----------------------------------------------
501 SymmetricMatrix QQ_old = _QQ;
502
503 for (int iPar = 1; iPar <= _params.size(); iPar++) {
504 _params[iPar-1]->index_old = _params[iPar-1]->index;
505 _params[iPar-1]->index = 0;
506 }
507
508 // Remove Ambiguity Parameters without observations
509 // ------------------------------------------------
510 int iPar = 0;
511 QMutableVectorIterator<t_pppParam*> im(_params);
512 while (im.hasNext()) {
513 t_pppParam* par = im.next();
514 bool removed = false;
515 if (par->type == t_pppParam::AMB_L3) {
516 if (epoData->satData.find(par->prn) == epoData->satData.end()) {
517 removed = true;
518 delete par;
519 im.remove();
520 }
521 }
522 if (! removed) {
523 ++iPar;
524 par->index = iPar;
525 }
526 }
527
528 // Add new ambiguity parameters
529 // ----------------------------
530 QMapIterator<QString, t_satData*> it(epoData->satData);
531 while (it.hasNext()) {
532 it.next();
533 t_satData* satData = it.value();
534 addAmb(satData);
535 }
536
537 int nPar = _params.size();
538 _QQ.ReSize(nPar); _QQ = 0.0;
539 for (int i1 = 1; i1 <= nPar; i1++) {
540 t_pppParam* p1 = _params[i1-1];
541 if (p1->index_old != 0) {
542 _QQ(p1->index, p1->index) = QQ_old(p1->index_old, p1->index_old);
543 for (int i2 = 1; i2 <= nPar; i2++) {
544 t_pppParam* p2 = _params[i2-1];
545 if (p2->index_old != 0) {
546 _QQ(p1->index, p2->index) = QQ_old(p1->index_old, p2->index_old);
547 }
548 }
549 }
550 }
551
552 for (int ii = 1; ii <= nPar; ii++) {
553 t_pppParam* par = _params[ii-1];
554 if (par->index_old == 0) {
555 _QQ(par->index, par->index) = OPT->_aprSigAmb * OPT->_aprSigAmb;
556 }
557 par->index_old = par->index;
558 }
559 }
560}
561
562// Update Step of the Filter (currently just a single-epoch solution)
563////////////////////////////////////////////////////////////////////////////
564t_irc t_pppFilter::update(t_epoData* epoData) {
565
566 Tracer tracer("t_pppFilter::update");
567
568 _time = epoData->tt; // current epoch time
569
570 if (OPT->useOrbClkCorr()) {
571 LOG << "Precise Point Positioning of Epoch " << _time.timestr(1)
572 << "\n---------------------------------------------------------------\n";
573 }
574 else {
575 LOG << "Single Point Positioning of Epoch " << _time.timestr(1)
576 << "\n--------------------------------------------------------------\n";
577 }
578
579 // Outlier Detection Loop
580 // ----------------------
581 if (update_p(epoData) != success) {
582 return failure;
583 }
584
585 // Set Solution Vector
586 // -------------------
587 LOG.setf(ios::fixed);
588 QVectorIterator<t_pppParam*> itPar(_params);
589 while (itPar.hasNext()) {
590 t_pppParam* par = itPar.next();
591 if (par->type == t_pppParam::RECCLK) {
592 LOG << "\n clk = " << setw(10) << setprecision(3) << par->xx
593 << " +- " << setw(6) << setprecision(3)
594 << sqrt(_QQ(par->index,par->index));
595 }
596 else if (par->type == t_pppParam::AMB_L3) {
597 ++par->numEpo;
598 LOG << "\n amb " << par->prn.toAscii().data() << " = "
599 << setw(10) << setprecision(3) << par->xx
600 << " +- " << setw(6) << setprecision(3)
601 << sqrt(_QQ(par->index,par->index))
602 << " nEpo = " << par->numEpo;
603 }
604 else if (par->type == t_pppParam::TROPO) {
605 double aprTrp = delay_saast(M_PI/2.0);
606 LOG << "\n trp = " << par->prn.toAscii().data()
607 << setw(7) << setprecision(3) << aprTrp << " "
608 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
609 << " +- " << setw(6) << setprecision(3)
610 << sqrt(_QQ(par->index,par->index));
611 }
612 else if (par->type == t_pppParam::GLONASS_OFFSET) {
613 LOG << "\n offGlo = " << setw(10) << setprecision(3) << par->xx
614 << " +- " << setw(6) << setprecision(3)
615 << sqrt(_QQ(par->index,par->index));
616 }
617 else if (par->type == t_pppParam::GALILEO_OFFSET) {
618 LOG << "\n offGal = " << setw(10) << setprecision(3) << par->xx
619 << " +- " << setw(6) << setprecision(3)
620 << sqrt(_QQ(par->index,par->index));
621 }
622 }
623
624 LOG << endl << endl;
625
626 // Final Message (both log file and screen)
627 // ----------------------------------------
628 LOG << OPT->_roverName << " PPP "
629 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
630 << setw(14) << setprecision(3) << x() << " +- "
631 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
632 << setw(14) << setprecision(3) << y() << " +- "
633 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
634 << setw(14) << setprecision(3) << z() << " +- "
635 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
636
637 // NEU Output
638 // ----------
639 if (OPT->xyzAprRoverSet()) {
640 double xyz[3];
641 xyz[0] = x() - OPT->_xyzAprRover[0];
642 xyz[1] = y() - OPT->_xyzAprRover[1];
643 xyz[2] = z() - OPT->_xyzAprRover[2];
644
645 double ellRef[3];
646 xyz2ell(OPT->_xyzAprRover.data(), ellRef);
647 double neu[3];
648 xyz2neu(ellRef, xyz, neu);
649
650 LOG << " NEU "
651 << setw(8) << setprecision(3) << neu[0] << " "
652 << setw(8) << setprecision(3) << neu[1] << " "
653 << setw(8) << setprecision(3) << neu[2] << endl << endl;
654 }
655 else {
656 LOG << endl << endl;
657 }
658
659 _lastTimeOK = _time; // remember time of last successful update
660 return success;
661}
662
663// Outlier Detection
664////////////////////////////////////////////////////////////////////////////
665QString t_pppFilter::outlierDetection(int iPhase, const ColumnVector& vv,
666 QMap<QString, t_satData*>& satData) {
667
668 Tracer tracer("t_pppFilter::outlierDetection");
669
670 QString prnGPS;
671 QString prnGlo;
672 double maxResGPS = 0.0;
673 double maxResGlo = 0.0;
674 findMaxRes(vv, satData, prnGPS, prnGlo, maxResGPS, maxResGlo);
675
676 if (iPhase == 1) {
677 if (maxResGlo > MAXRES_PHASE_GLONASS) {
678 LOG << "Outlier Phase " << prnGlo.toAscii().data() << ' ' << maxResGlo << endl;
679 return prnGlo;
680 }
681 else if (maxResGPS > MAXRES_PHASE_GPS) {
682 LOG << "Outlier Phase " << prnGPS.toAscii().data() << ' ' << maxResGPS << endl;
683 return prnGPS;
684 }
685 }
686 else if (iPhase == 0 && maxResGPS > MAXRES_CODE) {
687 LOG << "Outlier Code " << prnGPS.toAscii().data() << ' ' << maxResGPS << endl;
688 return prnGPS;
689 }
690
691 return QString();
692}
693
694//
695//////////////////////////////////////////////////////////////////////////////
696void t_pppFilter::kalman(const Matrix& AA, const ColumnVector& ll,
697 const DiagonalMatrix& PP,
698 SymmetricMatrix& QQ, ColumnVector& dx) {
699
700 Tracer tracer("t_pppFilter::kalman");
701
702 int nPar = AA.Ncols();
703 int nObs = AA.Nrows();
704 UpperTriangularMatrix SS = Cholesky(QQ).t();
705
706 Matrix SA = SS*AA.t();
707 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
708 for (int ii = 1; ii <= nObs; ++ii) {
709 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
710 }
711
712 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
713 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
714
715 UpperTriangularMatrix UU;
716 QRZ(SRF, UU);
717
718 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
719 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
720 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
721
722 UpperTriangularMatrix SHi = SH_rt.i();
723
724 Matrix KT = SHi * YY;
725 SymmetricMatrix Hi; Hi << SHi * SHi.t();
726
727 dx = KT.t() * ll;
728 QQ << (SS.t() * SS);
729}
730
731// Phase Wind-Up Correction
732///////////////////////////////////////////////////////////////////////////
733double t_pppFilter::windUp(const QString& prn, const ColumnVector& rSat,
734 const ColumnVector& rRec) {
735
736 Tracer tracer("t_pppFilter::windUp");
737
738 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
739
740 // First time - initialize to zero
741 // -------------------------------
742 if (!_windUpTime.contains(prn)) {
743 _windUpSum[prn] = 0.0;
744 }
745
746 // Compute the correction for new time
747 // -----------------------------------
748 if (!_windUpTime.contains(prn) || _windUpTime[prn] != Mjd) {
749 _windUpTime[prn] = Mjd;
750
751 // Unit Vector GPS Satellite --> Receiver
752 // --------------------------------------
753 ColumnVector rho = rRec - rSat;
754 rho /= rho.norm_Frobenius();
755
756 // GPS Satellite unit Vectors sz, sy, sx
757 // -------------------------------------
758 ColumnVector sz = -rSat / rSat.norm_Frobenius();
759
760 ColumnVector xSun = t_astro::Sun(Mjd);
761 xSun /= xSun.norm_Frobenius();
762
763 ColumnVector sy = crossproduct(sz, xSun);
764 ColumnVector sx = crossproduct(sy, sz);
765
766 // Effective Dipole of the GPS Satellite Antenna
767 // ---------------------------------------------
768 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
769 - crossproduct(rho, sy);
770
771 // Receiver unit Vectors rx, ry
772 // ----------------------------
773 ColumnVector rx(3);
774 ColumnVector ry(3);
775
776 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
777 double neu[3];
778
779 neu[0] = 1.0;
780 neu[1] = 0.0;
781 neu[2] = 0.0;
782 neu2xyz(recEll, neu, rx.data());
783
784 neu[0] = 0.0;
785 neu[1] = -1.0;
786 neu[2] = 0.0;
787 neu2xyz(recEll, neu, ry.data());
788
789 // Effective Dipole of the Receiver Antenna
790 // ----------------------------------------
791 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
792 + crossproduct(rho, ry);
793
794 // Resulting Effect
795 // ----------------
796 double alpha = DotProduct(dipSat,dipRec) /
797 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
798
799 if (alpha > 1.0) alpha = 1.0;
800 if (alpha < -1.0) alpha = -1.0;
801
802 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
803
804 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
805 dphi = -dphi;
806 }
807
808 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
809 }
810
811 return _windUpSum[prn];
812}
813
814//
815///////////////////////////////////////////////////////////////////////////
816void t_pppFilter::cmpEle(t_satData* satData) {
817 Tracer tracer("t_pppFilter::cmpEle");
818 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
819 double rho = rr.norm_Frobenius();
820
821 double neu[3];
822 xyz2neu(_ellBanc.data(), rr.data(), neu);
823
824 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
825 if (neu[2] < 0) {
826 satData->eleSat *= -1.0;
827 }
828 satData->azSat = atan2(neu[1], neu[0]);
829}
830
831//
832///////////////////////////////////////////////////////////////////////////
833void t_pppFilter::addAmb(t_satData* satData) {
834 Tracer tracer("t_pppFilter::addAmb");
835 bool found = false;
836 for (int iPar = 1; iPar <= _params.size(); iPar++) {
837 if (_params[iPar-1]->type == t_pppParam::AMB_L3 &&
838 _params[iPar-1]->prn == satData->prn) {
839 found = true;
840 break;
841 }
842 }
843 if (!found) {
844 t_pppParam* par = new t_pppParam(t_pppParam::AMB_L3,
845 _params.size()+1, satData->prn);
846 _params.push_back(par);
847 par->xx = satData->L3 - cmpValue(satData, true);
848 }
849}
850
851//
852///////////////////////////////////////////////////////////////////////////
853void t_pppFilter::addObs(int iPhase, unsigned& iObs, t_satData* satData,
854 Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP) {
855
856 Tracer tracer("t_pppFilter::addObs");
857
858 const double ELEWGHT = 20.0;
859 double ellWgtCoef = 1.0;
860 double eleD = satData->eleSat * 180.0 / M_PI;
861 if (eleD < ELEWGHT) {
862 ellWgtCoef = 1.5 - 0.5 / (ELEWGHT - 10.0) * (eleD - 10.0);
863 }
864
865 // Remember Observation Index
866 // --------------------------
867 ++iObs;
868 satData->obsIndex = iObs;
869
870 // Phase Observations
871 // ------------------
872 if (iPhase == 1) {
873 ll(iObs) = satData->L3 - cmpValue(satData, true);
874 double sigL3 = 2.98 * OPT->_sigmaL1;
875 if (satData->system() == 'R') {
876 sigL3 *= GLONASS_WEIGHT_FACTOR;
877 }
878 PP(iObs,iObs) = 1.0 / (sigL3 * sigL3) / (ellWgtCoef * ellWgtCoef);
879 for (int iPar = 1; iPar <= _params.size(); iPar++) {
880 if (_params[iPar-1]->type == t_pppParam::AMB_L3 &&
881 _params[iPar-1]->prn == satData->prn) {
882 ll(iObs) -= _params[iPar-1]->xx;
883 }
884 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
885 }
886 }
887
888 // Code Observations
889 // -----------------
890 else {
891 double sigP3 = 2.98 * OPT->_sigmaC1;
892 ll(iObs) = satData->P3 - cmpValue(satData, false);
893 PP(iObs,iObs) = 1.0 / (sigP3 * sigP3) / (ellWgtCoef * ellWgtCoef);
894 for (int iPar = 1; iPar <= _params.size(); iPar++) {
895 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
896 }
897 }
898}
899
900//
901///////////////////////////////////////////////////////////////////////////
902QByteArray t_pppFilter::printRes(int iPhase, const ColumnVector& vv,
903 const QMap<QString, t_satData*>& satDataMap) {
904
905 Tracer tracer("t_pppFilter::printRes");
906
907 ostringstream str;
908 str.setf(ios::fixed);
909
910 QMapIterator<QString, t_satData*> it(satDataMap);
911 while (it.hasNext()) {
912 it.next();
913 t_satData* satData = it.value();
914 if (satData->obsIndex != 0) {
915 str << _time.timestr(1)
916 << " RES " << satData->prn.toAscii().data()
917 << (iPhase ? " L3 " : " P3 ")
918 << setw(9) << setprecision(4) << vv(satData->obsIndex) << endl;
919 }
920 }
921
922 return QByteArray(str.str().c_str());
923}
924
925//
926///////////////////////////////////////////////////////////////////////////
927void t_pppFilter::findMaxRes(const ColumnVector& vv,
928 const QMap<QString, t_satData*>& satData,
929 QString& prnGPS, QString& prnGlo,
930 double& maxResGPS, double& maxResGlo) {
931
932 Tracer tracer("t_pppFilter::findMaxRes");
933
934 maxResGPS = 0.0;
935 maxResGlo = 0.0;
936
937 QMapIterator<QString, t_satData*> it(satData);
938 while (it.hasNext()) {
939 it.next();
940 t_satData* satData = it.value();
941 if (satData->obsIndex != 0) {
942 QString prn = satData->prn;
943 if (prn[0] == 'R') {
944 if (fabs(vv(satData->obsIndex)) > maxResGlo) {
945 maxResGlo = fabs(vv(satData->obsIndex));
946 prnGlo = prn;
947 }
948 }
949 else {
950 if (fabs(vv(satData->obsIndex)) > maxResGPS) {
951 maxResGPS = fabs(vv(satData->obsIndex));
952 prnGPS = prn;
953 }
954 }
955 }
956 }
957}
958
959// Update Step (private - loop over outliers)
960////////////////////////////////////////////////////////////////////////////
961t_irc t_pppFilter::update_p(t_epoData* epoData) {
962
963 Tracer tracer("t_pppFilter::update_p");
964
965 // Save Variance-Covariance Matrix, and Status Vector
966 // --------------------------------------------------
967 rememberState(epoData);
968
969 QString lastOutlierPrn;
970
971 // Try with all satellites, then with all minus one, etc.
972 // ------------------------------------------------------
973 while (selectSatellites(lastOutlierPrn, epoData->satData) == success) {
974
975 QByteArray strResCode;
976 QByteArray strResPhase;
977
978 // Bancroft Solution
979 // -----------------
980 if (cmpBancroft(epoData) != success) {
981 break;
982 }
983
984 // First update using code observations, then phase observations
985 // -------------------------------------------------------------
986 bool usePhase = OPT->ambLCs('G').size() || OPT->ambLCs('R').size() ||
987 OPT->ambLCs('E').size();
988
989 for (int iPhase = 0; iPhase <= (usePhase ? 1 : 0); iPhase++) {
990
991 // Status Prediction
992 // -----------------
993 predict(iPhase, epoData);
994
995 // Create First-Design Matrix
996 // --------------------------
997 unsigned nPar = _params.size();
998 unsigned nObs = 0;
999 if (iPhase == 0) {
1000 nObs = epoData->sizeAll() - epoData->sizeSys('R'); // Glonass code not used
1001 }
1002 else {
1003 nObs = epoData->sizeAll();
1004 }
1005
1006 // Prepare first-design Matrix, vector observed-computed
1007 // -----------------------------------------------------
1008 Matrix AA(nObs, nPar); // first design matrix
1009 ColumnVector ll(nObs); // tems observed-computed
1010 DiagonalMatrix PP(nObs); PP = 0.0;
1011
1012 unsigned iObs = 0;
1013 QMapIterator<QString, t_satData*> it(epoData->satData);
1014 while (it.hasNext()) {
1015 it.next();
1016 t_satData* satData = it.value();
1017 if (iPhase == 1 || satData->system() != 'R') {
1018 QString prn = satData->prn;
1019 addObs(iPhase, iObs, satData, AA, ll, PP);
1020 }
1021 }
1022
1023 // Compute Filter Update
1024 // ---------------------
1025 ColumnVector dx;
1026 kalman(AA, ll, PP, _QQ, dx);
1027 ColumnVector vv = ll - AA * dx;
1028
1029 // Print Residuals
1030 // ---------------
1031 if (iPhase == 0) {
1032 strResCode = printRes(iPhase, vv, epoData->satData);
1033 }
1034 else {
1035 strResPhase = printRes(iPhase, vv, epoData->satData);
1036 }
1037
1038 // Check the residuals
1039 // -------------------
1040 lastOutlierPrn = outlierDetection(iPhase, vv, epoData->satData);
1041
1042 // No Outlier Detected
1043 // -------------------
1044 if (lastOutlierPrn.isEmpty()) {
1045
1046 QVectorIterator<t_pppParam*> itPar(_params);
1047 while (itPar.hasNext()) {
1048 t_pppParam* par = itPar.next();
1049 par->xx += dx(par->index);
1050 }
1051
1052 if (!usePhase || iPhase == 1) {
1053 if (_outlierGPS.size() > 0 || _outlierGlo.size() > 0) {
1054 LOG << "Neglected PRNs: ";
1055 if (!_outlierGPS.isEmpty()) {
1056 LOG << _outlierGPS.last().toAscii().data() << ' ';
1057 }
1058 QStringListIterator itGlo(_outlierGlo);
1059 while (itGlo.hasNext()) {
1060 QString prn = itGlo.next();
1061 LOG << prn.toAscii().data() << ' ';
1062 }
1063 }
1064 LOG << strResCode.data() << strResPhase.data();
1065
1066 return success;
1067 }
1068 }
1069
1070 // Outlier Found
1071 // -------------
1072 else {
1073 restoreState(epoData);
1074 break;
1075 }
1076
1077 } // for iPhase
1078
1079 } // while selectSatellites
1080
1081 restoreState(epoData);
1082 return failure;
1083}
1084
1085// Remeber Original State Vector and Variance-Covariance Matrix
1086////////////////////////////////////////////////////////////////////////////
1087void t_pppFilter::rememberState(t_epoData* epoData) {
1088
1089 _QQ_sav = _QQ;
1090
1091 QVectorIterator<t_pppParam*> itSav(_params_sav);
1092 while (itSav.hasNext()) {
1093 t_pppParam* par = itSav.next();
1094 delete par;
1095 }
1096 _params_sav.clear();
1097
1098 QVectorIterator<t_pppParam*> it(_params);
1099 while (it.hasNext()) {
1100 t_pppParam* par = it.next();
1101 _params_sav.push_back(new t_pppParam(*par));
1102 }
1103
1104 _epoData_sav->deepCopy(epoData);
1105}
1106
1107// Restore Original State Vector and Variance-Covariance Matrix
1108////////////////////////////////////////////////////////////////////////////
1109void t_pppFilter::restoreState(t_epoData* epoData) {
1110
1111 _QQ = _QQ_sav;
1112
1113 QVectorIterator<t_pppParam*> it(_params);
1114 while (it.hasNext()) {
1115 t_pppParam* par = it.next();
1116 delete par;
1117 }
1118 _params.clear();
1119
1120 QVectorIterator<t_pppParam*> itSav(_params_sav);
1121 while (itSav.hasNext()) {
1122 t_pppParam* par = itSav.next();
1123 _params.push_back(new t_pppParam(*par));
1124 }
1125
1126 epoData->deepCopy(_epoData_sav);
1127}
1128
1129//
1130////////////////////////////////////////////////////////////////////////////
1131t_irc t_pppFilter::selectSatellites(const QString& lastOutlierPrn,
1132 QMap<QString, t_satData*>& satData) {
1133
1134 // First Call
1135 // ----------
1136 if (lastOutlierPrn.isEmpty()) {
1137 _outlierGPS.clear();
1138 _outlierGlo.clear();
1139 return success;
1140 }
1141
1142 // Second and next trials
1143 // ----------------------
1144 else {
1145
1146 if (lastOutlierPrn[0] == 'R') {
1147 _outlierGlo << lastOutlierPrn;
1148 }
1149
1150 // Remove all Glonass Outliers
1151 // ---------------------------
1152 QStringListIterator it(_outlierGlo);
1153 while (it.hasNext()) {
1154 QString prn = it.next();
1155 if (satData.contains(prn)) {
1156 delete satData.take(prn);
1157 }
1158 }
1159
1160 if (lastOutlierPrn[0] == 'R') {
1161 _outlierGPS.clear();
1162 return success;
1163 }
1164
1165 // GPS Outlier appeared for the first time - try to delete it
1166 // ----------------------------------------------------------
1167 if (_outlierGPS.indexOf(lastOutlierPrn) == -1) {
1168 _outlierGPS << lastOutlierPrn;
1169 if (satData.contains(lastOutlierPrn)) {
1170 delete satData.take(lastOutlierPrn);
1171 }
1172 return success;
1173 }
1174
1175 }
1176
1177 return failure;
1178}
1179
1180//
1181////////////////////////////////////////////////////////////////////////////
1182double lorentz(const ColumnVector& aa, const ColumnVector& bb) {
1183 return aa(1)*bb(1) + aa(2)*bb(2) + aa(3)*bb(3) - aa(4)*bb(4);
1184}
1185
1186//
1187////////////////////////////////////////////////////////////////////////////
1188void t_pppFilter::bancroft(const Matrix& BBpass, ColumnVector& pos) {
1189
1190 if (pos.Nrows() != 4) {
1191 pos.ReSize(4);
1192 }
1193 pos = 0.0;
1194
1195 for (int iter = 1; iter <= 2; iter++) {
1196 Matrix BB = BBpass;
1197 int mm = BB.Nrows();
1198 for (int ii = 1; ii <= mm; ii++) {
1199 double xx = BB(ii,1);
1200 double yy = BB(ii,2);
1201 double traveltime = 0.072;
1202 if (iter > 1) {
1203 double zz = BB(ii,3);
1204 double rho = sqrt( (xx-pos(1)) * (xx-pos(1)) +
1205 (yy-pos(2)) * (yy-pos(2)) +
1206 (zz-pos(3)) * (zz-pos(3)) );
1207 traveltime = rho / t_CST::c;
1208 }
1209 double angle = traveltime * t_CST::omega;
1210 double cosa = cos(angle);
1211 double sina = sin(angle);
1212 BB(ii,1) = cosa * xx + sina * yy;
1213 BB(ii,2) = -sina * xx + cosa * yy;
1214 }
1215
1216 Matrix BBB;
1217 if (mm > 4) {
1218 SymmetricMatrix hlp; hlp << BB.t() * BB;
1219 BBB = hlp.i() * BB.t();
1220 }
1221 else {
1222 BBB = BB.i();
1223 }
1224 ColumnVector ee(mm); ee = 1.0;
1225 ColumnVector alpha(mm); alpha = 0.0;
1226 for (int ii = 1; ii <= mm; ii++) {
1227 alpha(ii) = lorentz(BB.Row(ii).t(),BB.Row(ii).t())/2.0;
1228 }
1229 ColumnVector BBBe = BBB * ee;
1230 ColumnVector BBBalpha = BBB * alpha;
1231 double aa = lorentz(BBBe, BBBe);
1232 double bb = lorentz(BBBe, BBBalpha)-1;
1233 double cc = lorentz(BBBalpha, BBBalpha);
1234 double root = sqrt(bb*bb-aa*cc);
1235
1236 Matrix hlpPos(4,2);
1237 hlpPos.Column(1) = (-bb-root)/aa * BBBe + BBBalpha;
1238 hlpPos.Column(2) = (-bb+root)/aa * BBBe + BBBalpha;
1239
1240 ColumnVector omc(2);
1241 for (int pp = 1; pp <= 2; pp++) {
1242 hlpPos(4,pp) = -hlpPos(4,pp);
1243 omc(pp) = BB(1,4) -
1244 sqrt( (BB(1,1)-hlpPos(1,pp)) * (BB(1,1)-hlpPos(1,pp)) +
1245 (BB(1,2)-hlpPos(2,pp)) * (BB(1,2)-hlpPos(2,pp)) +
1246 (BB(1,3)-hlpPos(3,pp)) * (BB(1,3)-hlpPos(3,pp)) ) -
1247 hlpPos(4,pp);
1248 }
1249 if ( fabs(omc(1)) > fabs(omc(2)) ) {
1250 pos = hlpPos.Column(2);
1251 }
1252 else {
1253 pos = hlpPos.Column(1);
1254 }
1255 }
1256}
1257
1258//
1259////////////////////////////////////////////////////////////////////////////
1260void t_pppFilter::cmpDOP(t_epoData* epoData) {
1261
1262 Tracer tracer("t_pppFilter::cmpDOP");
1263
1264 _numSat = 0;
1265 _pDop = 0.0;
1266
1267 if (_params.size() < 4) {
1268 return;
1269 }
1270
1271 const unsigned numPar = 4;
1272 Matrix AA(epoData->sizeAll(), numPar);
1273 QMapIterator<QString, t_satData*> it(epoData->satData);
1274 while (it.hasNext()) {
1275 it.next();
1276 t_satData* satData = it.value();
1277 _numSat += 1;
1278 for (unsigned iPar = 0; iPar < numPar; iPar++) {
1279 AA[_numSat-1][iPar] = _params[iPar]->partial(satData, false);
1280 }
1281 }
1282 if (_numSat < 4) {
1283 return;
1284 }
1285 AA = AA.Rows(1, _numSat);
1286 SymmetricMatrix NN; NN << AA.t() * AA;
1287 SymmetricMatrix QQ = NN.i();
1288
1289 _pDop = sqrt(QQ(1,1) + QQ(2,2) + QQ(3,3));
1290}
Note: See TracBrowser for help on using the repository browser.