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

Last change on this file since 6061 was 6061, checked in by mervart, 10 years ago
File size: 36.1 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->usePhase) {
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->sigAmb0 * _opt->sigAmb0;
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->pppMode) {
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->refCrdSet()) {
662 newPos->xnt[0] = x() - _opt->refCrd[0];
663 newPos->xnt[1] = y() - _opt->refCrd[1];
664 newPos->xnt[2] = z() - _opt->refCrd[2];
665
666 double ellRef[3];
667 xyz2ell(_opt->refCrd, 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 if (_opt->pppAverage == 0.0) {
680 delete newPos;
681 }
682 else {
683
684 _posAverage.push_back(newPos);
685
686 // Compute the Mean
687 // ----------------
688 ColumnVector mean(7); mean = 0.0;
689
690 QMutableVectorIterator<pppPos*> it(_posAverage);
691 while (it.hasNext()) {
692 pppPos* pp = it.next();
693 if ( (epoData->tt - pp->time) >= _opt->pppAverage ) {
694 delete pp;
695 it.remove();
696 }
697 else {
698 for (int ii = 0; ii < 7; ++ii) {
699 mean[ii] += pp->xnt[ii];
700 }
701 }
702 }
703
704 int nn = _posAverage.size();
705
706 if (nn > 0) {
707
708 mean /= nn;
709
710 // Compute the Deviation
711 // ---------------------
712 ColumnVector std(7); std = 0.0;
713 QVectorIterator<pppPos*> it2(_posAverage);
714 while (it2.hasNext()) {
715 pppPos* pp = it2.next();
716 for (int ii = 0; ii < 7; ++ii) {
717 std[ii] += (pp->xnt[ii] - mean[ii]) * (pp->xnt[ii] - mean[ii]);
718 }
719 }
720 for (int ii = 0; ii < 7; ++ii) {
721 std[ii] = sqrt(std[ii] / nn);
722 }
723
724 if (_opt->refCrdSet()) {
725 ostringstream strD; strD.setf(ios::fixed);
726 strD << _staID.data() << " AVE-XYZ "
727 << epoData->tt.timestr(1) << " "
728 << setw(13) << setprecision(3) << mean[0] + _opt->refCrd[0] << " +- "
729 << setw(6) << setprecision(3) << std[0] << " "
730 << setw(14) << setprecision(3) << mean[1] + _opt->refCrd[1] << " +- "
731 << setw(6) << setprecision(3) << std[1] << " "
732 << setw(14) << setprecision(3) << mean[2] + _opt->refCrd[2] << " +- "
733 << setw(6) << setprecision(3) << std[2];
734 _pppClient->emitNewMessage(QByteArray(strD.str().c_str()), true);
735
736 ostringstream strE; strE.setf(ios::fixed);
737 strE << _staID.data() << " AVE-NEU "
738 << epoData->tt.timestr(1) << " "
739 << setw(13) << setprecision(3) << mean[3] << " +- "
740 << setw(6) << setprecision(3) << std[3] << " "
741 << setw(14) << setprecision(3) << mean[4] << " +- "
742 << setw(6) << setprecision(3) << std[4] << " "
743 << setw(14) << setprecision(3) << mean[5] << " +- "
744 << setw(6) << setprecision(3) << std[5];
745 _pppClient->emitNewMessage(QByteArray(strE.str().c_str()), true);
746
747 if (_opt->estTropo) {
748 ostringstream strF; strF.setf(ios::fixed);
749 strF << _staID.data() << " AVE-TRP "
750 << epoData->tt.timestr(1) << " "
751 << setw(13) << setprecision(3) << mean[6] << " +- "
752 << setw(6) << setprecision(3) << std[6] << endl;
753 _pppClient->emitNewMessage(QByteArray(strF.str().c_str()), true);
754 }
755 }
756 }
757 }
758
759 _lastTimeOK = _time; // remember time of last successful update
760 return success;
761}
762
763// Outlier Detection
764////////////////////////////////////////////////////////////////////////////
765QString bncModel::outlierDetection(int iPhase, const ColumnVector& vv,
766 QMap<QString, t_satData*>& satData) {
767
768 Tracer tracer("bncModel::outlierDetection");
769
770 QString prnGPS;
771 QString prnGlo;
772 double maxResGPS = 0.0;
773 double maxResGlo = 0.0;
774 findMaxRes(vv, satData, prnGPS, prnGlo, maxResGPS, maxResGlo);
775
776 if (iPhase == 1) {
777 if (maxResGlo > MAXRES_PHASE_GLONASS) {
778 _log += "Outlier Phase " + prnGlo + " "
779 + QByteArray::number(maxResGlo, 'f', 3) + "\n";
780 return prnGlo;
781 }
782 else if (maxResGPS > MAXRES_PHASE_GPS) {
783 _log += "Outlier Phase " + prnGPS + " "
784 + QByteArray::number(maxResGPS, 'f', 3) + "\n";
785 return prnGPS;
786 }
787 }
788 else if (iPhase == 0 && maxResGPS > MAXRES_CODE) {
789 _log += "Outlier Code " + prnGPS + " "
790 + QByteArray::number(maxResGPS, 'f', 3) + "\n";
791 return prnGPS;
792 }
793
794 return QString();
795}
796
797//
798//////////////////////////////////////////////////////////////////////////////
799void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
800 const DiagonalMatrix& PP,
801 SymmetricMatrix& QQ, ColumnVector& dx) {
802
803 Tracer tracer("bncModel::kalman");
804
805 int nPar = AA.Ncols();
806 int nObs = AA.Nrows();
807 UpperTriangularMatrix SS = Cholesky(QQ).t();
808
809 Matrix SA = SS*AA.t();
810 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
811 for (int ii = 1; ii <= nObs; ++ii) {
812 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
813 }
814
815 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
816 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
817
818 UpperTriangularMatrix UU;
819 QRZ(SRF, UU);
820
821 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
822 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
823 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
824
825 UpperTriangularMatrix SHi = SH_rt.i();
826
827 Matrix KT = SHi * YY;
828 SymmetricMatrix Hi; Hi << SHi * SHi.t();
829
830 dx = KT.t() * ll;
831 QQ << (SS.t() * SS);
832}
833
834// Phase Wind-Up Correction
835///////////////////////////////////////////////////////////////////////////
836double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
837 const ColumnVector& rRec) {
838
839 Tracer tracer("bncModel::windUp");
840
841 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
842
843 // First time - initialize to zero
844 // -------------------------------
845 if (!_windUpTime.contains(prn)) {
846 _windUpSum[prn] = 0.0;
847 }
848
849 // Compute the correction for new time
850 // -----------------------------------
851 if (!_windUpTime.contains(prn) || _windUpTime[prn] != Mjd) {
852 _windUpTime[prn] = Mjd;
853
854 // Unit Vector GPS Satellite --> Receiver
855 // --------------------------------------
856 ColumnVector rho = rRec - rSat;
857 rho /= rho.norm_Frobenius();
858
859 // GPS Satellite unit Vectors sz, sy, sx
860 // -------------------------------------
861 ColumnVector sz = -rSat / rSat.norm_Frobenius();
862
863 ColumnVector xSun = Sun(Mjd);
864 xSun /= xSun.norm_Frobenius();
865
866 ColumnVector sy = crossproduct(sz, xSun);
867 ColumnVector sx = crossproduct(sy, sz);
868
869 // Effective Dipole of the GPS Satellite Antenna
870 // ---------------------------------------------
871 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
872 - crossproduct(rho, sy);
873
874 // Receiver unit Vectors rx, ry
875 // ----------------------------
876 ColumnVector rx(3);
877 ColumnVector ry(3);
878
879 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
880 double neu[3];
881
882 neu[0] = 1.0;
883 neu[1] = 0.0;
884 neu[2] = 0.0;
885 neu2xyz(recEll, neu, rx.data());
886
887 neu[0] = 0.0;
888 neu[1] = -1.0;
889 neu[2] = 0.0;
890 neu2xyz(recEll, neu, ry.data());
891
892 // Effective Dipole of the Receiver Antenna
893 // ----------------------------------------
894 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
895 + crossproduct(rho, ry);
896
897 // Resulting Effect
898 // ----------------
899 double alpha = DotProduct(dipSat,dipRec) /
900 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
901
902 if (alpha > 1.0) alpha = 1.0;
903 if (alpha < -1.0) alpha = -1.0;
904
905 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
906
907 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
908 dphi = -dphi;
909 }
910
911 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
912 }
913
914 return _windUpSum[prn];
915}
916
917//
918///////////////////////////////////////////////////////////////////////////
919void bncModel::cmpEle(t_satData* satData) {
920 Tracer tracer("bncModel::cmpEle");
921 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
922 double rho = rr.norm_Frobenius();
923
924 double neu[3];
925 xyz2neu(_ellBanc.data(), rr.data(), neu);
926
927 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
928 if (neu[2] < 0) {
929 satData->eleSat *= -1.0;
930 }
931 satData->azSat = atan2(neu[1], neu[0]);
932}
933
934//
935///////////////////////////////////////////////////////////////////////////
936void bncModel::addAmb(t_satData* satData) {
937 Tracer tracer("bncModel::addAmb");
938 bool found = false;
939 for (int iPar = 1; iPar <= _params.size(); iPar++) {
940 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
941 _params[iPar-1]->prn == satData->prn) {
942 found = true;
943 break;
944 }
945 }
946 if (!found) {
947 bncParam* par = new bncParam(bncParam::AMB_L3,
948 _params.size()+1, satData->prn);
949 _params.push_back(par);
950 par->xx = satData->L3 - cmpValue(satData, true);
951 }
952}
953
954//
955///////////////////////////////////////////////////////////////////////////
956void bncModel::addObs(int iPhase, unsigned& iObs, t_satData* satData,
957 Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP) {
958
959 Tracer tracer("bncModel::addObs");
960
961 const double ELEWGHT = 20.0;
962 double ellWgtCoef = 1.0;
963 double eleD = satData->eleSat * 180.0 / M_PI;
964 if (eleD < ELEWGHT) {
965 ellWgtCoef = 1.5 - 0.5 / (ELEWGHT - 10.0) * (eleD - 10.0);
966 }
967
968 // Remember Observation Index
969 // --------------------------
970 ++iObs;
971 satData->obsIndex = iObs;
972
973 // Phase Observations
974 // ------------------
975 if (iPhase == 1) {
976 ll(iObs) = satData->L3 - cmpValue(satData, true);
977 double sigL3 = _opt->sigL3;
978 if (satData->system() == 'R') {
979 sigL3 *= GLONASS_WEIGHT_FACTOR;
980 }
981 PP(iObs,iObs) = 1.0 / (sigL3 * sigL3) / (ellWgtCoef * ellWgtCoef);
982 for (int iPar = 1; iPar <= _params.size(); iPar++) {
983 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
984 _params[iPar-1]->prn == satData->prn) {
985 ll(iObs) -= _params[iPar-1]->xx;
986 }
987 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
988 }
989 }
990
991 // Code Observations
992 // -----------------
993 else {
994 ll(iObs) = satData->P3 - cmpValue(satData, false);
995 PP(iObs,iObs) = 1.0 / (_opt->sigP3 * _opt->sigP3) / (ellWgtCoef * ellWgtCoef);
996 for (int iPar = 1; iPar <= _params.size(); iPar++) {
997 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
998 }
999 }
1000}
1001
1002//
1003///////////////////////////////////////////////////////////////////////////
1004QByteArray bncModel::printRes(int iPhase, const ColumnVector& vv,
1005 const QMap<QString, t_satData*>& satDataMap) {
1006
1007 Tracer tracer("bncModel::printRes");
1008
1009 ostringstream str;
1010 str.setf(ios::fixed);
1011
1012 QMapIterator<QString, t_satData*> it(satDataMap);
1013 while (it.hasNext()) {
1014 it.next();
1015 t_satData* satData = it.value();
1016 if (satData->obsIndex != 0) {
1017 str << _time.timestr(1)
1018 << " RES " << satData->prn.toAscii().data()
1019 << (iPhase ? " L3 " : " P3 ")
1020 << setw(9) << setprecision(4) << vv(satData->obsIndex) << endl;
1021 }
1022 }
1023
1024 return QByteArray(str.str().c_str());
1025}
1026
1027//
1028///////////////////////////////////////////////////////////////////////////
1029void bncModel::findMaxRes(const ColumnVector& vv,
1030 const QMap<QString, t_satData*>& satData,
1031 QString& prnGPS, QString& prnGlo,
1032 double& maxResGPS, double& maxResGlo) {
1033
1034 Tracer tracer("bncModel::findMaxRes");
1035
1036 maxResGPS = 0.0;
1037 maxResGlo = 0.0;
1038
1039 QMapIterator<QString, t_satData*> it(satData);
1040 while (it.hasNext()) {
1041 it.next();
1042 t_satData* satData = it.value();
1043 if (satData->obsIndex != 0) {
1044 QString prn = satData->prn;
1045 if (prn[0] == 'R') {
1046 if (fabs(vv(satData->obsIndex)) > maxResGlo) {
1047 maxResGlo = fabs(vv(satData->obsIndex));
1048 prnGlo = prn;
1049 }
1050 }
1051 else {
1052 if (fabs(vv(satData->obsIndex)) > maxResGPS) {
1053 maxResGPS = fabs(vv(satData->obsIndex));
1054 prnGPS = prn;
1055 }
1056 }
1057 }
1058 }
1059}
1060
1061// Update Step (private - loop over outliers)
1062////////////////////////////////////////////////////////////////////////////
1063t_irc bncModel::update_p(t_epoData* epoData) {
1064
1065 Tracer tracer("bncModel::update_p");
1066
1067 // Save Variance-Covariance Matrix, and Status Vector
1068 // --------------------------------------------------
1069 rememberState(epoData);
1070
1071 QString lastOutlierPrn;
1072
1073 // Try with all satellites, then with all minus one, etc.
1074 // ------------------------------------------------------
1075 while (selectSatellites(lastOutlierPrn, epoData->satData) == success) {
1076
1077 QByteArray strResCode;
1078 QByteArray strResPhase;
1079
1080 // Bancroft Solution
1081 // -----------------
1082 if (cmpBancroft(epoData) != success) {
1083 break;
1084 }
1085
1086 // First update using code observations, then phase observations
1087 // -------------------------------------------------------------
1088 for (int iPhase = 0; iPhase <= (_opt->usePhase ? 1 : 0); iPhase++) {
1089
1090 // Status Prediction
1091 // -----------------
1092 predict(iPhase, epoData);
1093
1094 // Create First-Design Matrix
1095 // --------------------------
1096 unsigned nPar = _params.size();
1097 unsigned nObs = 0;
1098 if (iPhase == 0) {
1099 nObs = epoData->sizeAll() - epoData->sizeSys('R'); // Glonass code not used
1100 }
1101 else {
1102 nObs = epoData->sizeAll();
1103 }
1104
1105 // Prepare first-design Matrix, vector observed-computed
1106 // -----------------------------------------------------
1107 Matrix AA(nObs, nPar); // first design matrix
1108 ColumnVector ll(nObs); // tems observed-computed
1109 DiagonalMatrix PP(nObs); PP = 0.0;
1110
1111 unsigned iObs = 0;
1112 QMapIterator<QString, t_satData*> it(epoData->satData);
1113 while (it.hasNext()) {
1114 it.next();
1115 t_satData* satData = it.value();
1116 if (iPhase == 1 || satData->system() != 'R') {
1117 QString prn = satData->prn;
1118 addObs(iPhase, iObs, satData, AA, ll, PP);
1119 }
1120 }
1121
1122 // Compute Filter Update
1123 // ---------------------
1124 ColumnVector dx;
1125 kalman(AA, ll, PP, _QQ, dx);
1126 ColumnVector vv = ll - AA * dx;
1127
1128 // Print Residuals
1129 // ---------------
1130 if (iPhase == 0) {
1131 strResCode = printRes(iPhase, vv, epoData->satData);
1132 }
1133 else {
1134 strResPhase = printRes(iPhase, vv, epoData->satData);
1135 }
1136
1137 // Check the residuals
1138 // -------------------
1139 lastOutlierPrn = outlierDetection(iPhase, vv, epoData->satData);
1140
1141 // No Outlier Detected
1142 // -------------------
1143 if (lastOutlierPrn.isEmpty()) {
1144
1145 QVectorIterator<bncParam*> itPar(_params);
1146 while (itPar.hasNext()) {
1147 bncParam* par = itPar.next();
1148 par->xx += dx(par->index);
1149 }
1150
1151 if (!_opt->usePhase || iPhase == 1) {
1152 if (_outlierGPS.size() > 0 || _outlierGlo.size() > 0) {
1153 _log += "Neglected PRNs: ";
1154 if (!_outlierGPS.isEmpty()) {
1155 _log += _outlierGPS.last() + ' ';
1156 }
1157 QStringListIterator itGlo(_outlierGlo);
1158 while (itGlo.hasNext()) {
1159 QString prn = itGlo.next();
1160 _log += prn + ' ';
1161 }
1162 }
1163 _log += '\n';
1164
1165 _log += strResCode + strResPhase;
1166
1167 return success;
1168 }
1169 }
1170
1171 // Outlier Found
1172 // -------------
1173 else {
1174 restoreState(epoData);
1175 break;
1176 }
1177
1178 } // for iPhase
1179
1180 } // while selectSatellites
1181
1182 restoreState(epoData);
1183 return failure;
1184}
1185
1186// Remeber Original State Vector and Variance-Covariance Matrix
1187////////////////////////////////////////////////////////////////////////////
1188void bncModel::rememberState(t_epoData* epoData) {
1189
1190 _QQ_sav = _QQ;
1191
1192 QVectorIterator<bncParam*> itSav(_params_sav);
1193 while (itSav.hasNext()) {
1194 bncParam* par = itSav.next();
1195 delete par;
1196 }
1197 _params_sav.clear();
1198
1199 QVectorIterator<bncParam*> it(_params);
1200 while (it.hasNext()) {
1201 bncParam* par = it.next();
1202 _params_sav.push_back(new bncParam(*par));
1203 }
1204
1205 _epoData_sav->deepCopy(epoData);
1206}
1207
1208// Restore Original State Vector and Variance-Covariance Matrix
1209////////////////////////////////////////////////////////////////////////////
1210void bncModel::restoreState(t_epoData* epoData) {
1211
1212 _QQ = _QQ_sav;
1213
1214 QVectorIterator<bncParam*> it(_params);
1215 while (it.hasNext()) {
1216 bncParam* par = it.next();
1217 delete par;
1218 }
1219 _params.clear();
1220
1221 QVectorIterator<bncParam*> itSav(_params_sav);
1222 while (itSav.hasNext()) {
1223 bncParam* par = itSav.next();
1224 _params.push_back(new bncParam(*par));
1225 }
1226
1227 epoData->deepCopy(_epoData_sav);
1228}
1229
1230//
1231////////////////////////////////////////////////////////////////////////////
1232t_irc bncModel::selectSatellites(const QString& lastOutlierPrn,
1233 QMap<QString, t_satData*>& satData) {
1234
1235 // First Call
1236 // ----------
1237 if (lastOutlierPrn.isEmpty()) {
1238 _outlierGPS.clear();
1239 _outlierGlo.clear();
1240 return success;
1241 }
1242
1243 // Second and next trials
1244 // ----------------------
1245 else {
1246
1247 if (lastOutlierPrn[0] == 'R') {
1248 _outlierGlo << lastOutlierPrn;
1249 }
1250
1251 // Remove all Glonass Outliers
1252 // ---------------------------
1253 QStringListIterator it(_outlierGlo);
1254 while (it.hasNext()) {
1255 QString prn = it.next();
1256 if (satData.contains(prn)) {
1257 delete satData.take(prn);
1258 }
1259 }
1260
1261 if (lastOutlierPrn[0] == 'R') {
1262 _outlierGPS.clear();
1263 return success;
1264 }
1265
1266 // GPS Outlier appeared for the first time - try to delete it
1267 // ----------------------------------------------------------
1268 if (_outlierGPS.indexOf(lastOutlierPrn) == -1) {
1269 _outlierGPS << lastOutlierPrn;
1270 if (satData.contains(lastOutlierPrn)) {
1271 delete satData.take(lastOutlierPrn);
1272 }
1273 return success;
1274 }
1275
1276 }
1277
1278 return failure;
1279}
Note: See TracBrowser for help on using the repository browser.