source: ntrip/trunk/BNC/src/PPP_free/bncmodel.cpp@ 6062

Last change on this file since 6062 was 6062, checked in by mervart, 8 years ago
File size: 33.6 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 "bncpppclient.h"
49#include "bnccore.h"
50#include "bncpppclient.h"
51#include "bancroft.h"
52#include "bncutils.h"
53#include "bncantex.h"
54#include "pppOptions.h"
55#include "pppModel.h"
56
57using namespace BNC_PPP;
58using namespace std;
59
60const unsigned MINOBS = 5;
61const double MINELE = 10.0 * M_PI / 180.0;
62const double MAXRES_CODE = 15.0;
63const double MAXRES_PHASE_GPS = 0.04;
64const double MAXRES_PHASE_GLONASS = 0.08;
65const double GLONASS_WEIGHT_FACTOR = 5.0;
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 numEpo = 0;
77}
78
79// Destructor
80////////////////////////////////////////////////////////////////////////////
81bncParam::~bncParam() {
82}
83
84// Partial
85////////////////////////////////////////////////////////////////////////////
86double bncParam::partial(t_satData* satData, bool phase) {
87
88 Tracer tracer("bncParam::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////////////////////////////////////////////////////////////////////////////
154bncModel::bncModel(bncPPPclient* pppClient) {
155
156 _pppClient = pppClient;
157 _staID = pppClient->staID();
158 _opt = pppClient->opt();
159
160 _tides = new t_tides();
161
162 // Antenna Name, ANTEX File
163 // ------------------------
164 _antex = 0;
165 if (!_opt->_antexFileName.empty()) {
166 _antex = new bncAntex(_opt->_antexFileName.c_str());
167 }
168
169 // Bancroft Coordinates
170 // --------------------
171 _xcBanc.ReSize(4); _xcBanc = 0.0;
172 _ellBanc.ReSize(3); _ellBanc = 0.0;
173
174 // Save copy of data (used in outlier detection)
175 // ---------------------------------------------
176 _epoData_sav = new t_epoData();
177}
178
179// Destructor
180////////////////////////////////////////////////////////////////////////////
181bncModel::~bncModel() {
182 delete _tides;
183 for (int ii = 0; ii < _posAverage.size(); ++ii) {
184 delete _posAverage[ii];
185 }
186 delete _antex;
187 for (int iPar = 1; iPar <= _params.size(); iPar++) {
188 delete _params[iPar-1];
189 }
190 for (int iPar = 1; iPar <= _params_sav.size(); iPar++) {
191 delete _params_sav[iPar-1];
192 }
193 delete _epoData_sav;
194}
195
196// Reset Parameters and Variance-Covariance Matrix
197////////////////////////////////////////////////////////////////////////////
198void bncModel::reset() {
199
200 Tracer tracer("bncModel::reset");
201
202 double lastTrp = 0.0;
203 for (int ii = 0; ii < _params.size(); ii++) {
204 bncParam* pp = _params[ii];
205 if (pp->type == bncParam::TROPO) {
206 lastTrp = pp->xx;
207 }
208 delete pp;
209 }
210 _params.clear();
211
212 int nextPar = 0;
213 _params.push_back(new bncParam(bncParam::CRD_X, ++nextPar, ""));
214 _params.push_back(new bncParam(bncParam::CRD_Y, ++nextPar, ""));
215 _params.push_back(new bncParam(bncParam::CRD_Z, ++nextPar, ""));
216 _params.push_back(new bncParam(bncParam::RECCLK, ++nextPar, ""));
217 if (_opt->estTrp()) {
218 _params.push_back(new bncParam(bncParam::TROPO, ++nextPar, ""));
219 }
220 if (_opt->useSystem('R')) {
221 _params.push_back(new bncParam(bncParam::GLONASS_OFFSET, ++nextPar, ""));
222 }
223 if (_opt->useSystem('E')) {
224 _params.push_back(new bncParam(bncParam::GALILEO_OFFSET, ++nextPar, ""));
225 }
226
227 _QQ.ReSize(_params.size());
228 _QQ = 0.0;
229 for (int iPar = 1; iPar <= _params.size(); iPar++) {
230 bncParam* pp = _params[iPar-1];
231 pp->xx = 0.0;
232 if (pp->isCrd()) {
233 _QQ(iPar,iPar) = _opt->_aprSigCrd(1) * _opt->_aprSigCrd(1);
234 }
235 else if (pp->type == bncParam::RECCLK) {
236 _QQ(iPar,iPar) = _opt->_noiseClk * _opt->_noiseClk;
237 }
238 else if (pp->type == bncParam::TROPO) {
239 _QQ(iPar,iPar) = _opt->_aprSigTrp * _opt->_aprSigTrp;
240 pp->xx = lastTrp;
241 }
242 else if (pp->type == bncParam::GLONASS_OFFSET) {
243 _QQ(iPar,iPar) = 1000.0 * 1000.0;
244 }
245 else if (pp->type == bncParam::GALILEO_OFFSET) {
246 _QQ(iPar,iPar) = 1000.0 * 1000.0;
247 }
248 }
249}
250
251// Bancroft Solution
252////////////////////////////////////////////////////////////////////////////
253t_irc bncModel::cmpBancroft(t_epoData* epoData) {
254
255 Tracer tracer("bncModel::cmpBancroft");
256
257 if (epoData->sizeSys('G') < MINOBS) {
258 _log += "bncModel::cmpBancroft: not enough data\n";
259 return failure;
260 }
261
262 Matrix BB(epoData->sizeSys('G'), 4);
263
264 QMapIterator<QString, t_satData*> it(epoData->satData);
265 int iObsBanc = 0;
266 while (it.hasNext()) {
267 it.next();
268 t_satData* satData = it.value();
269 if (satData->system() == 'G') {
270 ++iObsBanc;
271 QString prn = it.key();
272 BB(iObsBanc, 1) = satData->xx(1);
273 BB(iObsBanc, 2) = satData->xx(2);
274 BB(iObsBanc, 3) = satData->xx(3);
275 BB(iObsBanc, 4) = satData->P3 + satData->clk;
276 }
277 }
278
279 bancroft(BB, _xcBanc);
280
281 // Ellipsoidal Coordinates
282 // ------------------------
283 xyz2ell(_xcBanc.data(), _ellBanc.data());
284
285 // Compute Satellite Elevations
286 // ----------------------------
287 QMutableMapIterator<QString, t_satData*> im(epoData->satData);
288 while (im.hasNext()) {
289 im.next();
290 t_satData* satData = im.value();
291 cmpEle(satData);
292 if (satData->eleSat < MINELE) {
293 delete satData;
294 im.remove();
295 }
296 }
297
298 return success;
299}
300
301// Computed Value
302////////////////////////////////////////////////////////////////////////////
303double bncModel::cmpValue(t_satData* satData, bool phase) {
304
305 Tracer tracer("bncModel::cmpValue");
306
307 ColumnVector xRec(3);
308 xRec(1) = x();
309 xRec(2) = y();
310 xRec(3) = z();
311
312 double rho0 = (satData->xx - xRec).norm_Frobenius();
313 double dPhi = t_CST::omega * rho0 / t_CST::c;
314
315 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
316 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
317 xRec(3) = z();
318
319 xRec += _tides->displacement(_time, xRec);
320
321 satData->rho = (satData->xx - xRec).norm_Frobenius();
322
323 double tropDelay = delay_saast(satData->eleSat) +
324 trp() / sin(satData->eleSat);
325
326 double wind = 0.0;
327 if (phase) {
328 wind = windUp(satData->prn, satData->xx, xRec) * satData->lambda3;
329 }
330
331 double offset = 0.0;
332 if (satData->prn[0] == 'R') {
333 offset = Glonass_offset();
334 }
335 else if (satData->prn[0] == 'E') {
336 offset = Galileo_offset();
337 }
338
339 double phaseCenter = 0.0;
340 if (_antex) {
341 bool found;
342 phaseCenter = _antex->pco(QString(_opt->_antNameRover.c_str()), satData->eleSat, found);
343 if (!found) {
344 LOG << "ANTEX: antenna >" << _opt->_antNameRover << "< not found\n";
345 }
346 }
347
348 double antennaOffset = 0.0;
349 double cosa = cos(satData->azSat);
350 double sina = sin(satData->azSat);
351 double cose = cos(satData->eleSat);
352 double sine = sin(satData->eleSat);
353 antennaOffset = -_opt->_neuEccRover(1) * cosa*cose
354 -_opt->_neuEccRover(2) * sina*cose
355 -_opt->_neuEccRover(3) * sine;
356
357 return satData->rho + phaseCenter + antennaOffset + clk()
358 + offset - satData->clk + tropDelay + wind;
359}
360
361// Tropospheric Model (Saastamoinen)
362////////////////////////////////////////////////////////////////////////////
363double bncModel::delay_saast(double Ele) {
364
365 Tracer tracer("bncModel::delay_saast");
366
367 double xyz[3];
368 xyz[0] = x();
369 xyz[1] = y();
370 xyz[2] = z();
371 double ell[3];
372 xyz2ell(xyz, ell);
373 double height = ell[2];
374
375 double pp = 1013.25 * pow(1.0 - 2.26e-5 * height, 5.225);
376 double TT = 18.0 - height * 0.0065 + 273.15;
377 double hh = 50.0 * exp(-6.396e-4 * height);
378 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
379
380 double h_km = height / 1000.0;
381
382 if (h_km < 0.0) h_km = 0.0;
383 if (h_km > 5.0) h_km = 5.0;
384 int ii = int(h_km + 1);
385 double href = ii - 1;
386
387 double bCor[6];
388 bCor[0] = 1.156;
389 bCor[1] = 1.006;
390 bCor[2] = 0.874;
391 bCor[3] = 0.757;
392 bCor[4] = 0.654;
393 bCor[5] = 0.563;
394
395 double BB = bCor[ii-1] + (bCor[ii]-bCor[ii-1]) * (h_km - href);
396
397 double zen = M_PI/2.0 - Ele;
398
399 return (0.002277/cos(zen)) * (pp + ((1255.0/TT)+0.05)*ee - BB*(tan(zen)*tan(zen)));
400}
401
402// Prediction Step of the Filter
403////////////////////////////////////////////////////////////////////////////
404void bncModel::predict(int iPhase, t_epoData* epoData) {
405
406 Tracer tracer("bncModel::predict");
407
408 if (iPhase == 0) {
409
410 const double maxSolGap = 0.0;
411
412 bool firstCrd = false;
413 if (!_lastTimeOK.valid() || (maxSolGap > 0.0 && _time - _lastTimeOK > maxSolGap)) {
414 firstCrd = true;
415 _startTime = epoData->tt;
416 reset();
417 }
418
419 // Use different white noise for Quick-Start mode
420 // ----------------------------------------------
421 double sigCrdP_used = _opt->_noiseCrd(1);
422 if ( _opt->_seedingTime > 0.0 && _opt->_seedingTime > (epoData->tt - _startTime) ) {
423 sigCrdP_used = 0.0;
424 }
425
426 // Predict Parameter values, add white noise
427 // -----------------------------------------
428 for (int iPar = 1; iPar <= _params.size(); iPar++) {
429 bncParam* pp = _params[iPar-1];
430
431 // Coordinates
432 // -----------
433 if (pp->type == bncParam::CRD_X) {
434 if (firstCrd) {
435 if (_opt->xyzAprRoverSet()) {
436 pp->xx = _opt->_xyzAprRover[0];
437 }
438 else {
439 pp->xx = _xcBanc(1);
440 }
441 }
442 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
443 }
444 else if (pp->type == bncParam::CRD_Y) {
445 if (firstCrd) {
446 if (_opt->xyzAprRoverSet()) {
447 pp->xx = _opt->_xyzAprRover[1];
448 }
449 else {
450 pp->xx = _xcBanc(2);
451 }
452 }
453 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
454 }
455 else if (pp->type == bncParam::CRD_Z) {
456 if (firstCrd) {
457 if (_opt->xyzAprRoverSet()) {
458 pp->xx = _opt->_xyzAprRover[2];
459 }
460 else {
461 pp->xx = _xcBanc(3);
462 }
463 }
464 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
465 }
466
467 // Receiver Clocks
468 // ---------------
469 else if (pp->type == bncParam::RECCLK) {
470 pp->xx = _xcBanc(4);
471 for (int jj = 1; jj <= _params.size(); jj++) {
472 _QQ(iPar, jj) = 0.0;
473 }
474 _QQ(iPar,iPar) = _opt->_noiseClk * _opt->_noiseClk;
475 }
476
477 // Tropospheric Delay
478 // ------------------
479 else if (pp->type == bncParam::TROPO) {
480 _QQ(iPar,iPar) += _opt->_noiseTrp * _opt->_noiseTrp;
481 }
482
483 // Glonass Offset
484 // --------------
485 else if (pp->type == bncParam::GLONASS_OFFSET) {
486 pp->xx = 0.0;
487 for (int jj = 1; jj <= _params.size(); jj++) {
488 _QQ(iPar, jj) = 0.0;
489 }
490 _QQ(iPar,iPar) = 1000.0 * 1000.0;
491 }
492
493 // Galileo Offset
494 // --------------
495 else if (pp->type == bncParam::GALILEO_OFFSET) {
496 _QQ(iPar,iPar) += 0.1 * 0.1;
497 }
498 }
499 }
500
501 // Add New Ambiguities if necessary
502 // --------------------------------
503 if (_opt->ambLCs('G').size() || _opt->ambLCs('R').size() || _opt->ambLCs('E').size()) {
504
505 // Make a copy of QQ and xx, set parameter indices
506 // -----------------------------------------------
507 SymmetricMatrix QQ_old = _QQ;
508
509 for (int iPar = 1; iPar <= _params.size(); iPar++) {
510 _params[iPar-1]->index_old = _params[iPar-1]->index;
511 _params[iPar-1]->index = 0;
512 }
513
514 // Remove Ambiguity Parameters without observations
515 // ------------------------------------------------
516 int iPar = 0;
517 QMutableVectorIterator<bncParam*> im(_params);
518 while (im.hasNext()) {
519 bncParam* par = im.next();
520 bool removed = false;
521 if (par->type == bncParam::AMB_L3) {
522 if (epoData->satData.find(par->prn) == epoData->satData.end()) {
523 removed = true;
524 delete par;
525 im.remove();
526 }
527 }
528 if (! removed) {
529 ++iPar;
530 par->index = iPar;
531 }
532 }
533
534 // Add new ambiguity parameters
535 // ----------------------------
536 QMapIterator<QString, t_satData*> it(epoData->satData);
537 while (it.hasNext()) {
538 it.next();
539 t_satData* satData = it.value();
540 addAmb(satData);
541 }
542
543 int nPar = _params.size();
544 _QQ.ReSize(nPar); _QQ = 0.0;
545 for (int i1 = 1; i1 <= nPar; i1++) {
546 bncParam* p1 = _params[i1-1];
547 if (p1->index_old != 0) {
548 _QQ(p1->index, p1->index) = QQ_old(p1->index_old, p1->index_old);
549 for (int i2 = 1; i2 <= nPar; i2++) {
550 bncParam* p2 = _params[i2-1];
551 if (p2->index_old != 0) {
552 _QQ(p1->index, p2->index) = QQ_old(p1->index_old, p2->index_old);
553 }
554 }
555 }
556 }
557
558 for (int ii = 1; ii <= nPar; ii++) {
559 bncParam* par = _params[ii-1];
560 if (par->index_old == 0) {
561 _QQ(par->index, par->index) = _opt->_aprSigAmb * _opt->_aprSigAmb;
562 }
563 par->index_old = par->index;
564 }
565 }
566}
567
568// Update Step of the Filter (currently just a single-epoch solution)
569////////////////////////////////////////////////////////////////////////////
570t_irc bncModel::update(t_epoData* epoData) {
571
572 Tracer tracer("bncModel::update");
573
574 _log.clear();
575
576 _time = epoData->tt; // current epoch time
577
578 if (_opt->useOrbClkCorr()) {
579 _log += "Precise Point Positioning of Epoch "
580 + QByteArray(_time.timestr(1).c_str()) +
581 "\n---------------------------------------------------------------\n";
582 }
583 else {
584 _log += "Single Point Positioning of Epoch "
585 + QByteArray(_time.timestr(1).c_str()) +
586 "\n--------------------------------------------------------------\n";
587 }
588
589 // Outlier Detection Loop
590 // ----------------------
591 if (update_p(epoData) != success) {
592 _pppClient->emitNewMessage(_log, false);
593 return failure;
594 }
595
596 // Remember the Epoch-specific Results for the computation of means
597 // ----------------------------------------------------------------
598 pppPos* newPos = new pppPos;
599 newPos->time = epoData->tt;
600
601 // Set Solution Vector
602 // -------------------
603 ostringstream strB;
604 strB.setf(ios::fixed);
605 QVectorIterator<bncParam*> itPar(_params);
606 while (itPar.hasNext()) {
607 bncParam* par = itPar.next();
608
609 if (par->type == bncParam::RECCLK) {
610 strB << "\n clk = " << setw(10) << setprecision(3) << par->xx
611 << " +- " << setw(6) << setprecision(3)
612 << sqrt(_QQ(par->index,par->index));
613 }
614 else if (par->type == bncParam::AMB_L3) {
615 ++par->numEpo;
616 strB << "\n amb " << par->prn.toAscii().data() << " = "
617 << setw(10) << setprecision(3) << par->xx
618 << " +- " << setw(6) << setprecision(3)
619 << sqrt(_QQ(par->index,par->index))
620 << " nEpo = " << par->numEpo;
621 }
622 else if (par->type == bncParam::TROPO) {
623 double aprTrp = delay_saast(M_PI/2.0);
624 strB << "\n trp = " << par->prn.toAscii().data()
625 << setw(7) << setprecision(3) << aprTrp << " "
626 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
627 << " +- " << setw(6) << setprecision(3)
628 << sqrt(_QQ(par->index,par->index));
629 newPos->xnt[6] = aprTrp + par->xx;
630 }
631 else if (par->type == bncParam::GLONASS_OFFSET) {
632 strB << "\n offGlo = " << setw(10) << setprecision(3) << par->xx
633 << " +- " << setw(6) << setprecision(3)
634 << sqrt(_QQ(par->index,par->index));
635 }
636 else if (par->type == bncParam::GALILEO_OFFSET) {
637 strB << "\n offGal = " << setw(10) << setprecision(3) << par->xx
638 << " +- " << setw(6) << setprecision(3)
639 << sqrt(_QQ(par->index,par->index));
640 }
641 }
642 strB << '\n';
643 _log += strB.str().c_str();
644 _pppClient->emitNewMessage(_log, false);
645
646 // Final Message (both log file and screen)
647 // ----------------------------------------
648 ostringstream strC;
649 strC.setf(ios::fixed);
650 strC << _staID.data() << " PPP "
651 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
652 << setw(14) << setprecision(3) << x() << " +- "
653 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
654 << setw(14) << setprecision(3) << y() << " +- "
655 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
656 << setw(14) << setprecision(3) << z() << " +- "
657 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
658
659 // NEU Output
660 // ----------
661 if (_opt->xyzAprRoverSet()) {
662 newPos->xnt[0] = x() - _opt->_xyzAprRover[0];
663 newPos->xnt[1] = y() - _opt->_xyzAprRover[1];
664 newPos->xnt[2] = z() - _opt->_xyzAprRover[2];
665
666 double ellRef[3];
667 xyz2ell(_opt->_xyzAprRover.data(), ellRef);
668 xyz2neu(ellRef, newPos->xnt, &newPos->xnt[3]);
669
670 strC << " NEU "
671 << setw(8) << setprecision(3) << newPos->xnt[3] << " "
672 << setw(8) << setprecision(3) << newPos->xnt[4] << " "
673 << setw(8) << setprecision(3) << newPos->xnt[5] << endl;
674
675 }
676
677 _pppClient->emitNewMessage(QByteArray(strC.str().c_str()), true);
678
679 delete newPos;
680
681 _lastTimeOK = _time; // remember time of last successful update
682 return success;
683}
684
685// Outlier Detection
686////////////////////////////////////////////////////////////////////////////
687QString bncModel::outlierDetection(int iPhase, const ColumnVector& vv,
688 QMap<QString, t_satData*>& satData) {
689
690 Tracer tracer("bncModel::outlierDetection");
691
692 QString prnGPS;
693 QString prnGlo;
694 double maxResGPS = 0.0;
695 double maxResGlo = 0.0;
696 findMaxRes(vv, satData, prnGPS, prnGlo, maxResGPS, maxResGlo);
697
698 if (iPhase == 1) {
699 if (maxResGlo > MAXRES_PHASE_GLONASS) {
700 _log += "Outlier Phase " + prnGlo + " "
701 + QByteArray::number(maxResGlo, 'f', 3) + "\n";
702 return prnGlo;
703 }
704 else if (maxResGPS > MAXRES_PHASE_GPS) {
705 _log += "Outlier Phase " + prnGPS + " "
706 + QByteArray::number(maxResGPS, 'f', 3) + "\n";
707 return prnGPS;
708 }
709 }
710 else if (iPhase == 0 && maxResGPS > MAXRES_CODE) {
711 _log += "Outlier Code " + prnGPS + " "
712 + QByteArray::number(maxResGPS, 'f', 3) + "\n";
713 return prnGPS;
714 }
715
716 return QString();
717}
718
719//
720//////////////////////////////////////////////////////////////////////////////
721void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
722 const DiagonalMatrix& PP,
723 SymmetricMatrix& QQ, ColumnVector& dx) {
724
725 Tracer tracer("bncModel::kalman");
726
727 int nPar = AA.Ncols();
728 int nObs = AA.Nrows();
729 UpperTriangularMatrix SS = Cholesky(QQ).t();
730
731 Matrix SA = SS*AA.t();
732 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
733 for (int ii = 1; ii <= nObs; ++ii) {
734 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
735 }
736
737 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
738 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
739
740 UpperTriangularMatrix UU;
741 QRZ(SRF, UU);
742
743 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
744 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
745 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
746
747 UpperTriangularMatrix SHi = SH_rt.i();
748
749 Matrix KT = SHi * YY;
750 SymmetricMatrix Hi; Hi << SHi * SHi.t();
751
752 dx = KT.t() * ll;
753 QQ << (SS.t() * SS);
754}
755
756// Phase Wind-Up Correction
757///////////////////////////////////////////////////////////////////////////
758double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
759 const ColumnVector& rRec) {
760
761 Tracer tracer("bncModel::windUp");
762
763 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
764
765 // First time - initialize to zero
766 // -------------------------------
767 if (!_windUpTime.contains(prn)) {
768 _windUpSum[prn] = 0.0;
769 }
770
771 // Compute the correction for new time
772 // -----------------------------------
773 if (!_windUpTime.contains(prn) || _windUpTime[prn] != Mjd) {
774 _windUpTime[prn] = Mjd;
775
776 // Unit Vector GPS Satellite --> Receiver
777 // --------------------------------------
778 ColumnVector rho = rRec - rSat;
779 rho /= rho.norm_Frobenius();
780
781 // GPS Satellite unit Vectors sz, sy, sx
782 // -------------------------------------
783 ColumnVector sz = -rSat / rSat.norm_Frobenius();
784
785 ColumnVector xSun = Sun(Mjd);
786 xSun /= xSun.norm_Frobenius();
787
788 ColumnVector sy = crossproduct(sz, xSun);
789 ColumnVector sx = crossproduct(sy, sz);
790
791 // Effective Dipole of the GPS Satellite Antenna
792 // ---------------------------------------------
793 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
794 - crossproduct(rho, sy);
795
796 // Receiver unit Vectors rx, ry
797 // ----------------------------
798 ColumnVector rx(3);
799 ColumnVector ry(3);
800
801 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
802 double neu[3];
803
804 neu[0] = 1.0;
805 neu[1] = 0.0;
806 neu[2] = 0.0;
807 neu2xyz(recEll, neu, rx.data());
808
809 neu[0] = 0.0;
810 neu[1] = -1.0;
811 neu[2] = 0.0;
812 neu2xyz(recEll, neu, ry.data());
813
814 // Effective Dipole of the Receiver Antenna
815 // ----------------------------------------
816 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
817 + crossproduct(rho, ry);
818
819 // Resulting Effect
820 // ----------------
821 double alpha = DotProduct(dipSat,dipRec) /
822 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
823
824 if (alpha > 1.0) alpha = 1.0;
825 if (alpha < -1.0) alpha = -1.0;
826
827 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
828
829 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
830 dphi = -dphi;
831 }
832
833 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
834 }
835
836 return _windUpSum[prn];
837}
838
839//
840///////////////////////////////////////////////////////////////////////////
841void bncModel::cmpEle(t_satData* satData) {
842 Tracer tracer("bncModel::cmpEle");
843 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
844 double rho = rr.norm_Frobenius();
845
846 double neu[3];
847 xyz2neu(_ellBanc.data(), rr.data(), neu);
848
849 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
850 if (neu[2] < 0) {
851 satData->eleSat *= -1.0;
852 }
853 satData->azSat = atan2(neu[1], neu[0]);
854}
855
856//
857///////////////////////////////////////////////////////////////////////////
858void bncModel::addAmb(t_satData* satData) {
859 Tracer tracer("bncModel::addAmb");
860 bool found = false;
861 for (int iPar = 1; iPar <= _params.size(); iPar++) {
862 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
863 _params[iPar-1]->prn == satData->prn) {
864 found = true;
865 break;
866 }
867 }
868 if (!found) {
869 bncParam* par = new bncParam(bncParam::AMB_L3,
870 _params.size()+1, satData->prn);
871 _params.push_back(par);
872 par->xx = satData->L3 - cmpValue(satData, true);
873 }
874}
875
876//
877///////////////////////////////////////////////////////////////////////////
878void bncModel::addObs(int iPhase, unsigned& iObs, t_satData* satData,
879 Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP) {
880
881 Tracer tracer("bncModel::addObs");
882
883 const double ELEWGHT = 20.0;
884 double ellWgtCoef = 1.0;
885 double eleD = satData->eleSat * 180.0 / M_PI;
886 if (eleD < ELEWGHT) {
887 ellWgtCoef = 1.5 - 0.5 / (ELEWGHT - 10.0) * (eleD - 10.0);
888 }
889
890 // Remember Observation Index
891 // --------------------------
892 ++iObs;
893 satData->obsIndex = iObs;
894
895 // Phase Observations
896 // ------------------
897 if (iPhase == 1) {
898 ll(iObs) = satData->L3 - cmpValue(satData, true);
899 double sigL3 = _opt->sigL3;
900 if (satData->system() == 'R') {
901 sigL3 *= GLONASS_WEIGHT_FACTOR;
902 }
903 PP(iObs,iObs) = 1.0 / (sigL3 * sigL3) / (ellWgtCoef * ellWgtCoef);
904 for (int iPar = 1; iPar <= _params.size(); iPar++) {
905 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
906 _params[iPar-1]->prn == satData->prn) {
907 ll(iObs) -= _params[iPar-1]->xx;
908 }
909 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
910 }
911 }
912
913 // Code Observations
914 // -----------------
915 else {
916 ll(iObs) = satData->P3 - cmpValue(satData, false);
917 PP(iObs,iObs) = 1.0 / (_opt->sigP3 * _opt->sigP3) / (ellWgtCoef * ellWgtCoef);
918 for (int iPar = 1; iPar <= _params.size(); iPar++) {
919 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
920 }
921 }
922}
923
924//
925///////////////////////////////////////////////////////////////////////////
926QByteArray bncModel::printRes(int iPhase, const ColumnVector& vv,
927 const QMap<QString, t_satData*>& satDataMap) {
928
929 Tracer tracer("bncModel::printRes");
930
931 ostringstream str;
932 str.setf(ios::fixed);
933
934 QMapIterator<QString, t_satData*> it(satDataMap);
935 while (it.hasNext()) {
936 it.next();
937 t_satData* satData = it.value();
938 if (satData->obsIndex != 0) {
939 str << _time.timestr(1)
940 << " RES " << satData->prn.toAscii().data()
941 << (iPhase ? " L3 " : " P3 ")
942 << setw(9) << setprecision(4) << vv(satData->obsIndex) << endl;
943 }
944 }
945
946 return QByteArray(str.str().c_str());
947}
948
949//
950///////////////////////////////////////////////////////////////////////////
951void bncModel::findMaxRes(const ColumnVector& vv,
952 const QMap<QString, t_satData*>& satData,
953 QString& prnGPS, QString& prnGlo,
954 double& maxResGPS, double& maxResGlo) {
955
956 Tracer tracer("bncModel::findMaxRes");
957
958 maxResGPS = 0.0;
959 maxResGlo = 0.0;
960
961 QMapIterator<QString, t_satData*> it(satData);
962 while (it.hasNext()) {
963 it.next();
964 t_satData* satData = it.value();
965 if (satData->obsIndex != 0) {
966 QString prn = satData->prn;
967 if (prn[0] == 'R') {
968 if (fabs(vv(satData->obsIndex)) > maxResGlo) {
969 maxResGlo = fabs(vv(satData->obsIndex));
970 prnGlo = prn;
971 }
972 }
973 else {
974 if (fabs(vv(satData->obsIndex)) > maxResGPS) {
975 maxResGPS = fabs(vv(satData->obsIndex));
976 prnGPS = prn;
977 }
978 }
979 }
980 }
981}
982
983// Update Step (private - loop over outliers)
984////////////////////////////////////////////////////////////////////////////
985t_irc bncModel::update_p(t_epoData* epoData) {
986
987 Tracer tracer("bncModel::update_p");
988
989 // Save Variance-Covariance Matrix, and Status Vector
990 // --------------------------------------------------
991 rememberState(epoData);
992
993 QString lastOutlierPrn;
994
995 // Try with all satellites, then with all minus one, etc.
996 // ------------------------------------------------------
997 while (selectSatellites(lastOutlierPrn, epoData->satData) == success) {
998
999 QByteArray strResCode;
1000 QByteArray strResPhase;
1001
1002 // Bancroft Solution
1003 // -----------------
1004 if (cmpBancroft(epoData) != success) {
1005 break;
1006 }
1007
1008 // First update using code observations, then phase observations
1009 // -------------------------------------------------------------
1010 for (int iPhase = 0; iPhase <= (_opt->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 (!_opt->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}
Note: See TracBrowser for help on using the repository browser.