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

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