source: ntrip/trunk/BNC/src/PPP/pppClient.cpp@ 10254

Last change on this file since 10254 was 10254, checked in by stuerze, 8 months ago

minor changes

  • Property svn:keywords set to Author Date Id Rev URL;svn:eol-style=native
  • Property svn:mime-type set to text/plain
File size: 17.2 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: t_pppClient
6 *
7 * Purpose: PPP Client processing starts here
8 *
9 * Author: L. Mervart
10 *
11 * Created: 29-Jul-2014
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <QThreadStorage>
18
19#include <iostream>
20#include <iomanip>
21#include <cmath>
22#include <stdlib.h>
23#include <string.h>
24#include <stdexcept>
25
26#include "pppClient.h"
27#include "pppEphPool.h"
28#include "pppObsPool.h"
29#include "bncconst.h"
30#include "bncutils.h"
31#include "pppStation.h"
32#include "bncantex.h"
33#include "pppFilter.h"
34
35using namespace BNC_PPP;
36using namespace std;
37
38// Global variable holding thread-specific pointers
39//////////////////////////////////////////////////////////////////////////////
40QThreadStorage<t_pppClient*> CLIENTS;
41
42// Static function returning thread-specific pointer
43//////////////////////////////////////////////////////////////////////////////
44t_pppClient* t_pppClient::instance() {
45 return CLIENTS.localData();
46}
47
48// Constructor
49//////////////////////////////////////////////////////////////////////////////
50t_pppClient::t_pppClient(const t_pppOptions* opt) {
51 _output = 0;
52 _opt = new t_pppOptions(*opt);
53 _log = new ostringstream();
54 _ephPool = new t_pppEphPool();
55 _obsPool = new t_pppObsPool();
56 _staRover = new t_pppStation();
57 _filter = new t_pppFilter();
58 _tides = new t_tides();
59 _antex = 0;
60 if (!_opt->_antexFileName.empty()) {
61 _antex = new bncAntex(_opt->_antexFileName.c_str());
62 }
63 if (!_opt->_blqFileName.empty()) {
64 if (_tides->readBlqFile(_opt->_blqFileName.c_str()) == success) {
65 //_tides->printAllBlqSets();
66 }
67 }
68
69 CLIENTS.setLocalData(this); // CLIENTS takes ownership over "this"
70}
71
72// Destructor
73//////////////////////////////////////////////////////////////////////////////
74t_pppClient::~t_pppClient() {
75 delete _log;
76 delete _opt;
77 delete _ephPool;
78 delete _obsPool;
79 delete _staRover;
80 if (_antex) {
81 delete _antex;
82 }
83 delete _filter;
84 delete _tides;
85 clearObs();
86}
87
88//
89//////////////////////////////////////////////////////////////////////////////
90void t_pppClient::putEphemeris(const t_eph* eph) {
91 const t_ephGPS* ephGPS = dynamic_cast<const t_ephGPS*>(eph);
92 const t_ephGlo* ephGlo = dynamic_cast<const t_ephGlo*>(eph);
93 const t_ephGal* ephGal = dynamic_cast<const t_ephGal*>(eph);
94 const t_ephBDS* ephBDS = dynamic_cast<const t_ephBDS*>(eph);
95 if (ephGPS) {
96 _ephPool->putEphemeris(new t_ephGPS(*ephGPS));
97 }
98 else if (ephGlo) {
99 _ephPool->putEphemeris(new t_ephGlo(*ephGlo));
100 }
101 else if (ephGal) {
102 _ephPool->putEphemeris(new t_ephGal(*ephGal));
103 }
104 else if (ephBDS) {
105 _ephPool->putEphemeris(new t_ephBDS(*ephBDS));
106 }
107}
108
109//
110//////////////////////////////////////////////////////////////////////////////
111void t_pppClient::putTec(const t_vTec* vTec) {
112 _obsPool->putTec(new t_vTec(*vTec));
113}
114
115//
116//////////////////////////////////////////////////////////////////////////////
117void t_pppClient::putOrbCorrections(const vector<t_orbCorr*>& corr) {
118 for (unsigned ii = 0; ii < corr.size(); ii++) {
119 _ephPool->putOrbCorrection(new t_orbCorr(*corr[ii]));
120 }
121}
122
123//
124//////////////////////////////////////////////////////////////////////////////
125void t_pppClient::putClkCorrections(const vector<t_clkCorr*>& corr) {
126 for (unsigned ii = 0; ii < corr.size(); ii++) {
127 _ephPool->putClkCorrection(new t_clkCorr(*corr[ii]));
128 }
129}
130
131//
132//////////////////////////////////////////////////////////////////////////////
133void t_pppClient::putCodeBiases(const vector<t_satCodeBias*>& biases) {
134 for (unsigned ii = 0; ii < biases.size(); ii++) {
135 _obsPool->putCodeBias(new t_satCodeBias(*biases[ii]));
136 }
137}
138
139//
140//////////////////////////////////////////////////////////////////////////////
141void t_pppClient::putPhaseBiases(const vector<t_satPhaseBias*>& biases) {
142 for (unsigned ii = 0; ii < biases.size(); ii++) {
143 _obsPool->putPhaseBias(new t_satPhaseBias(*biases[ii]));
144 }
145}
146
147//
148//////////////////////////////////////////////////////////////////////////////
149t_irc t_pppClient::prepareObs(const vector<t_satObs*>& satObs,
150 vector<t_pppSatObs*>& obsVector, bncTime& epoTime) {
151
152 // Default
153 // -------
154 epoTime.reset();
155 clearObs();
156
157 // Create vector of valid observations
158 // -----------------------------------
159 for (unsigned ii = 0; ii < satObs.size(); ii++) {
160 char sys = satObs[ii]->_prn.system();
161 if (_opt->useSystem(sys)) {
162 t_pppSatObs* pppSatObs = new t_pppSatObs(*satObs[ii]);
163 if (pppSatObs->isValid()) {
164 obsVector.push_back(pppSatObs);
165 }
166 else {
167 delete pppSatObs;
168 }
169 }
170 }
171
172 // Check whether data are synchronized, compute epoTime
173 // ----------------------------------------------------
174 const double MAXSYNC = 0.05; // synchronization limit
175 double meanDt = 0.0;
176 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
177 const t_pppSatObs* satObs = obsVector.at(ii);
178 if (epoTime.undef()) {
179 epoTime = satObs->time();
180 }
181 else {
182 double dt = satObs->time() - epoTime;
183 if (fabs(dt) > MAXSYNC) {
184 LOG << "t_pppClient::prepareObs asynchronous observations" << endl;
185 return failure;
186 }
187 meanDt += dt;
188 }
189 }
190
191 if (obsVector.size() > 0) {
192 epoTime += meanDt / obsVector.size();
193 }
194
195 return success;
196}
197
198//
199//////////////////////////////////////////////////////////////////////////////
200bool t_pppClient::preparePseudoObs(std::vector<t_pppSatObs*>& obsVector) {
201
202 bool pseudoObsIono = false;
203
204 if (_opt->_pseudoObsIono) {
205 vector<t_pppSatObs*>::iterator it = obsVector.begin();
206 while (it != obsVector.end()) {
207 t_pppSatObs* satObs = *it;
208 pseudoObsIono = satObs->setPseudoObsIono(t_frequency::G1);
209 it++;
210 }
211 }
212 /*
213 vector<t_pppSatObs*>::iterator it = obsVector.begin();
214 while (it != obsVector.end()) {
215 t_pppSatObs* satObs = *it;
216 satObs->printObsMinusComputed();
217 it++;
218 }
219*/
220 return pseudoObsIono;
221}
222
223//
224//////////////////////////////////////////////////////////////////////////////
225void t_pppClient::useObsWithCodeBiasesOnly(std::vector<t_pppSatObs*>& obsVector) {
226
227 vector<t_pppSatObs*>::iterator it = obsVector.begin();
228 while (it != obsVector.end()) {
229 t_pppSatObs* pppSatObs = *it;
230 bool codeBiasesAvailable = false;
231 if (pppSatObs->getCodeBias(pppSatObs->fType1()) &&
232 pppSatObs->getCodeBias(pppSatObs->fType2())) {
233 codeBiasesAvailable = true;
234 }
235 if (codeBiasesAvailable) {
236 ++it;
237 }
238 else {
239 it = obsVector.erase(it);
240 delete pppSatObs;
241 }
242 }
243 return;
244}
245
246
247// Compute the Bancroft position, check for blunders
248//////////////////////////////////////////////////////////////////////////////
249t_irc t_pppClient::cmpBancroft(const bncTime& epoTime,
250 vector<t_pppSatObs*>& obsVector,
251 ColumnVector& xyzc, bool print) {
252
253 t_lc::type tLC = t_lc::dummy;
254
255 while (true) {
256 Matrix BB(obsVector.size(), 4);
257 int iObs = -1;
258 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
259 const t_pppSatObs* satObs = obsVector.at(ii);
260 if (tLC == t_lc::dummy) {
261 if (satObs->isValid(t_lc::cIF)) {
262 tLC = t_lc::cIF;
263 }
264 else if (satObs->isValid(t_lc::c1)) {
265 tLC = t_lc::c1;
266 }
267 else if (satObs->isValid(t_lc::c2)) {
268 tLC = t_lc::c2;
269 }
270 }
271 if ( satObs->isValid(tLC) &&
272 (!satObs->modelSet() || satObs->eleSat() >= _opt->_minEle) ) {
273 ++iObs;
274 BB[iObs][0] = satObs->xc()[0];
275 BB[iObs][1] = satObs->xc()[1];
276 BB[iObs][2] = satObs->xc()[2];
277 BB[iObs][3] = satObs->obsValue(tLC) - satObs->cmpValueForBanc(tLC);
278 }
279 }
280 if (iObs + 1 < _opt->_minObs) {
281 LOG << "t_pppClient::cmpBancroft not enough observations: " << iObs + 1 << endl;
282 return failure;
283 }
284 BB = BB.Rows(1,iObs+1);
285 if (bancroft(BB, xyzc) != success) {
286 return failure;
287 }
288
289 xyzc[3] /= t_CST::c;
290
291 // Check Blunders
292 // --------------
293 const double BLUNDER = 500.0;
294 double maxRes = 0.0;
295 unsigned maxResIndex = 0;
296 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
297 const t_pppSatObs* satObs = obsVector.at(ii);
298 if (satObs->isValid() &&
299 (!satObs->modelSet() || satObs->eleSat() >= _opt->_minEle) ) {
300 ColumnVector rr = satObs->xc().Rows(1,3) - xyzc.Rows(1,3);
301 double res = rr.NormFrobenius() - satObs->obsValue(tLC)
302 - (satObs->xc()[3] - xyzc[3]) * t_CST::c;
303 if (fabs(res) > maxRes) {
304 maxRes = fabs(res);
305 maxResIndex = ii;
306 }
307 }
308 }
309 if (maxRes < BLUNDER) {
310 if (print) {
311 LOG.setf(ios::fixed);
312 LOG << "\nPPP of Epoch ";
313 if (!_epoTimeRover.undef()) LOG << string(_epoTimeRover);
314 LOG << "\n---------------------------------------------------------------\n";
315 LOG << string(epoTime) << " BANCROFT:" << ' '
316 << setw(14) << setprecision(3) << xyzc[0] << ' '
317 << setw(14) << setprecision(3) << xyzc[1] << ' '
318 << setw(14) << setprecision(3) << xyzc[2] << ' '
319 << setw(14) << setprecision(3) << xyzc[3] * t_CST::c << endl << endl;
320 }
321 break;
322 }
323 else {
324 t_pppSatObs* satObs = obsVector.at(maxResIndex);
325 LOG << "t_pppClient::cmpBancroft Outlier " << satObs->prn().toString()
326 << " " << maxRes << endl;
327 delete satObs;
328 obsVector.erase(obsVector.begin() + maxResIndex);
329 }
330 }
331
332 return success;
333}
334
335//
336//////////////////////////////////////////////////////////////////////////////
337void t_pppClient::initOutput(t_output* output) {
338 _output = output;
339 _output->_numSat = 0;
340 _output->_hDop = 0.0;
341 _output->_error = false;
342}
343
344//
345//////////////////////////////////////////////////////////////////////////////
346void t_pppClient::clearObs() {
347 for (unsigned ii = 0; ii < _obsRover.size(); ii++) {
348 delete _obsRover.at(ii);
349 }
350 _obsRover.clear();
351}
352
353//
354//////////////////////////////////////////////////////////////////////////////
355void t_pppClient::finish(t_irc irc, int ind) {
356#ifdef BNC_DEBUG_PPP
357 LOG << "t_pppClient::finish(" << ind << "): " << irc << endl;
358#endif
359
360 clearObs();
361
362 _output->_epoTime = _epoTimeRover;
363
364 if (irc == success) {
365 _output->_xyzRover[0] = _staRover->xyzApr()[0] + _filter->x()[0];
366 _output->_xyzRover[1] = _staRover->xyzApr()[1] + _filter->x()[1];
367 _output->_xyzRover[2] = _staRover->xyzApr()[2] + _filter->x()[2];
368
369 xyz2neu(_staRover->ellApr().data(), _filter->x().data(), _output->_neu);
370
371 copy(&_filter->Q().data()[0], &_filter->Q().data()[6], _output->_covMatrix);
372
373 _output->_trp0 = t_tropo::delay_saast(_staRover->xyzApr(), M_PI/2.0);
374 _output->_trp = _filter->trp();
375 _output->_trpStdev = _filter->trpStdev();
376
377 _output->_numSat = _filter->numSat();
378 _output->_hDop = _filter->HDOP();
379 _output->_error = false;
380 }
381 else {
382 _output->_error = true;
383 }
384
385 _output->_log = _log->str();
386 delete _log; _log = new ostringstream();
387
388 return;
389}
390
391//
392//////////////////////////////////////////////////////////////////////////////
393t_irc t_pppClient::cmpModel(t_pppStation* station, const ColumnVector& xyzc,
394 vector<t_pppSatObs*>& obsVector) {
395 bncTime time;
396 time = _epoTimeRover;
397 station->setName(_opt->_roverName);
398 station->setAntName(_opt->_antNameRover);
399 station->setEpochTime(time);
400
401 if (_opt->xyzAprRoverSet()) {
402 station->setXyzApr(_opt->_xyzAprRover);
403 }
404 else {
405 station->setXyzApr(xyzc.Rows(1,3));
406 }
407 station->setNeuEcc(_opt->_neuEccRover);
408
409 // Receiver Clock
410 // --------------
411 station->setDClk(xyzc[3]);
412
413 // Tides
414 // -----
415 station->setTideDsplEarth(_tides->earth(time, station->xyzApr()));
416 station->setTideDsplOcean(_tides->ocean(time, station->xyzApr(), station->name()));
417
418 // Observation model
419 // -----------------
420 vector<t_pppSatObs*>::iterator it = obsVector.begin();
421 while (it != obsVector.end()) {
422 t_pppSatObs* satObs = *it;
423 t_irc modelSetup;
424 if (satObs->isValid()) {
425 modelSetup = satObs->cmpModel(station);
426 }
427 if (satObs->isValid() &&
428 satObs->eleSat() >= _opt->_minEle &&
429 modelSetup == success) {
430 ++it;
431 }
432 else {
433 delete satObs;
434 it = obsVector.erase(it);
435 }
436 }
437
438 return success;
439}
440
441//
442//////////////////////////////////////////////////////////////////////////////
443void t_pppClient::processEpoch(const vector<t_satObs*>& satObs, t_output* output) {
444
445 try {
446 initOutput(output);
447
448 // Prepare Observations of the Rover
449 // ---------------------------------
450 if (prepareObs(satObs, _obsRover, _epoTimeRover) != success) {
451 return finish(failure, 1);
452 }
453
454 for (int iter = 1; iter <= 2; iter++) {
455 ColumnVector xyzc(4); xyzc = 0.0;
456 bool print = (iter == 2);
457 if (cmpBancroft(_epoTimeRover, _obsRover, xyzc, print) != success) {
458 return finish(failure, 2);
459 }
460 if (cmpModel(_staRover, xyzc, _obsRover) != success) {
461 return finish(failure, 3);
462 }
463 }
464 // use observations only if satellite code biases are available
465 /* ------------------------------------------------------------
466 if (!_opt->_corrMount.empty() {
467 useObsWithCodeBiasesOnly(_obsRover);
468 }*/
469
470 if (int(_obsRover.size()) < _opt->_minObs) {
471 LOG << "t_pppClient::processEpoch not enough observations" << endl;
472 return finish(failure, 4);
473 }
474
475 // Prepare Pseudo Observations of the Rover
476 // ----------------------------------------
477 _pseudoObsIono = preparePseudoObs(_obsRover);
478
479 // Store last epoch of data
480 // ------------------------
481 _obsPool->putEpoch(_epoTimeRover, _obsRover, _pseudoObsIono);
482
483 // Process Epoch in Filter
484 // -----------------------
485 if (_filter->processEpoch(_obsPool) != success) {
486 LOG << "t_pppFilter::processEpoch() != success" << endl;
487 return finish(failure, 5);
488 }
489 }
490 catch (Exception& exc) {
491 LOG << exc.what() << endl;
492 return finish(failure, 6);
493 }
494 catch (t_except msg) {
495 LOG << msg.what() << endl;
496 return finish(failure, 7);
497 }
498 catch (const char* msg) {
499 LOG << msg << endl;
500 return finish(failure, 8);
501 }
502 catch (...) {
503 LOG << "unknown exception" << endl;
504 return finish(failure, 9);
505 }
506 return finish(success, 0);
507}
508
509//
510////////////////////////////////////////////////////////////////////////////
511double lorentz(const ColumnVector& aa, const ColumnVector& bb) {
512 return aa[0]*bb[0] + aa[1]*bb[1] + aa[2]*bb[2] - aa[3]*bb[3];
513}
514
515//
516////////////////////////////////////////////////////////////////////////////
517t_irc t_pppClient::bancroft(const Matrix& BBpass, ColumnVector& pos) {
518
519 if (pos.Nrows() != 4) {
520 pos.ReSize(4);
521 }
522 pos = 0.0;
523
524 for (int iter = 1; iter <= 2; iter++) {
525 Matrix BB = BBpass;
526 int mm = BB.Nrows();
527 for (int ii = 1; ii <= mm; ii++) {
528 double xx = BB(ii,1);
529 double yy = BB(ii,2);
530 double traveltime = 0.072;
531 if (iter > 1) {
532 double zz = BB(ii,3);
533 double rho = sqrt( (xx-pos(1)) * (xx-pos(1)) +
534 (yy-pos(2)) * (yy-pos(2)) +
535 (zz-pos(3)) * (zz-pos(3)) );
536 traveltime = rho / t_CST::c;
537 }
538 double angle = traveltime * t_CST::omega;
539 double cosa = cos(angle);
540 double sina = sin(angle);
541 BB(ii,1) = cosa * xx + sina * yy;
542 BB(ii,2) = -sina * xx + cosa * yy;
543 }
544
545 Matrix BBB;
546 if (mm > 4) {
547 SymmetricMatrix hlp; hlp << BB.t() * BB;
548 BBB = hlp.i() * BB.t();
549 }
550 else {
551 BBB = BB.i();
552 }
553 ColumnVector ee(mm); ee = 1.0;
554 ColumnVector alpha(mm); alpha = 0.0;
555 for (int ii = 1; ii <= mm; ii++) {
556 alpha(ii) = lorentz(BB.Row(ii).t(),BB.Row(ii).t())/2.0;
557 }
558 ColumnVector BBBe = BBB * ee;
559 ColumnVector BBBalpha = BBB * alpha;
560 double aa = lorentz(BBBe, BBBe);
561 double bb = lorentz(BBBe, BBBalpha)-1;
562 double cc = lorentz(BBBalpha, BBBalpha);
563 double root = sqrt(bb*bb-aa*cc);
564
565 Matrix hlpPos(4,2);
566 hlpPos.Column(1) = (-bb-root)/aa * BBBe + BBBalpha;
567 hlpPos.Column(2) = (-bb+root)/aa * BBBe + BBBalpha;
568
569 ColumnVector omc(2);
570 for (int pp = 1; pp <= 2; pp++) {
571 hlpPos(4,pp) = -hlpPos(4,pp);
572 omc(pp) = BB(1,4) -
573 sqrt( (BB(1,1)-hlpPos(1,pp)) * (BB(1,1)-hlpPos(1,pp)) +
574 (BB(1,2)-hlpPos(2,pp)) * (BB(1,2)-hlpPos(2,pp)) +
575 (BB(1,3)-hlpPos(3,pp)) * (BB(1,3)-hlpPos(3,pp)) ) -
576 hlpPos(4,pp);
577 }
578 if ( fabs(omc(1)) > fabs(omc(2)) ) {
579 pos = hlpPos.Column(2);
580 }
581 else {
582 pos = hlpPos.Column(1);
583 }
584 }
585 if (pos.size() != 4 ||
586 std::isnan(pos(1)) ||
587 std::isnan(pos(2)) ||
588 std::isnan(pos(3)) ||
589 std::isnan(pos(4))) {
590 return failure;
591 }
592
593 return success;
594}
595
596//
597//////////////////////////////////////////////////////////////////////////////
598void t_pppClient::reset() {cout << "t_pppClient::reset()" << endl;
599
600 // to delete old orbit and clock corrections
601 delete _ephPool;
602 _ephPool = new t_pppEphPool();
603
604 // to delete old code biases
605 delete _obsPool;
606 _obsPool = new t_pppObsPool();
607
608 // to delete all parameters
609 delete _filter;
610 _filter = new t_pppFilter();
611
612}
Note: See TracBrowser for help on using the repository browser.