source: ntrip/trunk/BNC/src/rinex/reqcanalyze.cpp@ 4680

Last change on this file since 4680 was 4680, checked in by mervart, 12 years ago
File size: 18.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: t_reqcAnalyze
30 *
31 * Purpose: Analyze RINEX Files
32 *
33 * Author: L. Mervart
34 *
35 * Created: 11-Apr-2012
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42#include <iomanip>
43#include <qwt_plot_renderer.h>
44
45#include "reqcanalyze.h"
46#include "bncapp.h"
47#include "bncsettings.h"
48#include "reqcedit.h"
49#include "bncutils.h"
50#include "bncpostprocess.h"
51#include "graphwin.h"
52#include "polarplot.h"
53#include "availplot.h"
54#include "eleplot.h"
55#include "dopplot.h"
56
57using namespace std;
58
59const double SLIPTRESH = 5.0; // cycle-slip threshold (meters)
60
61// Constructor
62////////////////////////////////////////////////////////////////////////////
63t_reqcAnalyze::t_reqcAnalyze(QObject* parent) : QThread(parent) {
64
65 bncSettings settings;
66
67 _logFileName = settings.value("reqcOutLogFile").toString(); expandEnvVar(_logFileName);
68 _logFile = 0;
69 _log = 0;
70 _obsFileNames = settings.value("reqcObsFile").toString().split(",", QString::SkipEmptyParts);
71 _navFileNames = settings.value("reqcNavFile").toString().split(",", QString::SkipEmptyParts);
72
73 _currEpo = 0;
74
75 connect(this, SIGNAL(dspSkyPlot(const QString&,
76 const QByteArray&,
77 QVector<t_polarPoint*>*,
78 const QByteArray&,
79 QVector<t_polarPoint*>*,
80 const QByteArray&, double)),
81 this, SLOT(slotDspSkyPlot(const QString&,
82 const QByteArray&,
83 QVector<t_polarPoint*>*,
84 const QByteArray&,
85 QVector<t_polarPoint*>*,
86 const QByteArray&, double)));
87
88 connect(this, SIGNAL(dspAvailPlot(const QString&, const QByteArray&)),
89 this, SLOT(slotDspAvailPlot(const QString&, const QByteArray&)));
90}
91
92// Destructor
93////////////////////////////////////////////////////////////////////////////
94t_reqcAnalyze::~t_reqcAnalyze() {
95 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
96 delete _rnxObsFiles[ii];
97 }
98 for (int ii = 0; ii < _ephs.size(); ii++) {
99 delete _ephs[ii];
100 }
101 delete _log; _log = 0;
102 delete _logFile; _logFile = 0;
103 bncApp* app = (bncApp*) qApp;
104 if ( app->mode() != bncApp::interactive) {
105 app->exit(0);
106 }
107}
108
109//
110////////////////////////////////////////////////////////////////////////////
111void t_reqcAnalyze::slotDspSkyPlot(const QString& fileName,
112 const QByteArray& title1,
113 QVector<t_polarPoint*>* data1,
114 const QByteArray& title2,
115 QVector<t_polarPoint*>* data2,
116 const QByteArray& scaleTitle,
117 double maxValue) {
118
119 bncApp* app = dynamic_cast<bncApp*>(qApp);
120 if (app->GUIenabled()) {
121
122 if (maxValue == 0.0) {
123 if (data1) {
124 for (int ii = 0; ii < data1->size(); ii++) {
125 double val = data1->at(ii)->_value;
126 if (maxValue < val) {
127 maxValue = val;
128 }
129 }
130 }
131 if (data2) {
132 for (int ii = 0; ii < data2->size(); ii++) {
133 double val = data2->at(ii)->_value;
134 if (maxValue < val) {
135 maxValue = val;
136 }
137 }
138 }
139 }
140
141 QwtInterval scaleInterval(0.0, maxValue);
142
143 QVector<QWidget*> plots;
144 if (data1) {
145 t_polarPlot* plot1 = new t_polarPlot(QwtText(title1), scaleInterval,
146 app->mainWindow());
147 plot1->addCurve(data1);
148 plots << plot1;
149 }
150 if (data2) {
151 t_polarPlot* plot2 = new t_polarPlot(QwtText(title2), scaleInterval,
152 app->mainWindow());
153 plot2->addCurve(data2);
154 plots << plot2;
155 }
156
157 t_graphWin* graphWin = new t_graphWin(0, fileName, plots,
158 &scaleTitle, &scaleInterval);
159
160 graphWin->show();
161
162 bncSettings settings;
163 QString dirName = settings.value("reqcPlotDir").toString();
164 if (!dirName.isEmpty()) {
165 QByteArray ext = scaleTitle.isEmpty() ? "_S.png" : "_M.png";
166 graphWin->savePNG(dirName, ext);
167 }
168 }
169}
170
171//
172////////////////////////////////////////////////////////////////////////////
173void t_reqcAnalyze::run() {
174
175 // Open Log File
176 // -------------
177 _logFile = new QFile(_logFileName);
178 if (_logFile->open(QIODevice::WriteOnly | QIODevice::Text)) {
179 _log = new QTextStream();
180 _log->setDevice(_logFile);
181 }
182
183 // Initialize RINEX Observation Files
184 // ----------------------------------
185 t_reqcEdit::initRnxObsFiles(_obsFileNames, _rnxObsFiles, _log);
186
187 // Read Ephemerides
188 // ----------------
189 t_reqcEdit::readEphemerides(_navFileNames, _ephs);
190
191 // Loop over all RINEX Files
192 // -------------------------
193 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
194 analyzeFile(_rnxObsFiles[ii]);
195 }
196
197 // Exit
198 // ----
199 emit finished();
200 deleteLater();
201}
202
203//
204////////////////////////////////////////////////////////////////////////////
205void t_reqcAnalyze::analyzeFile(t_rnxObsFile* obsFile) {
206
207 if (_log) {
208 *_log << "\nAnalyze File\n"
209 << "------------\n"
210 << obsFile->fileName().toAscii().data() << endl << endl;
211 }
212
213 _allObsMap.clear();
214 _availDataMap.clear();
215
216 // A priori Coordinates
217 // --------------------
218 ColumnVector xyzSta = obsFile->xyz();
219
220 // Loop over all Epochs
221 // --------------------
222 try {
223 unsigned iEpo = 0;
224 while ( (_currEpo = obsFile->nextEpoch()) != 0) {
225
226 // Loop over all satellites
227 // ------------------------
228 for (unsigned iObs = 0; iObs < _currEpo->rnxSat.size(); iObs++) {
229 const t_rnxObsFile::t_rnxSat& rnxSat = _currEpo->rnxSat[iObs];
230 t_obs obs;
231 t_postProcessing::setObsFromRnx(obsFile, _currEpo, rnxSat, obs);
232
233 if (obs.satSys == 'R') {
234 // TODO: set channel number
235 }
236
237 QString prn = QString("%1%2").arg(obs.satSys)
238 .arg(obs.satNum, 2, 10, QChar('0'));
239
240 _allObsMap[prn].addObs(obs);
241
242 }
243
244 prepareObsStat(iEpo, obsFile->interval(), xyzSta);
245 iEpo++;
246
247 } // while (_currEpo)
248 }
249 catch (QString str) {
250 if (_log) {
251 *_log << "Exception " << str << endl;
252 }
253 else {
254 qDebug() << str;
255 }
256 return;
257 }
258
259 // Analyze the Multipath
260 // ---------------------
261 QVector<t_polarPoint*>* dataMP1 = new QVector<t_polarPoint*>;
262 QVector<t_polarPoint*>* dataMP2 = new QVector<t_polarPoint*>;
263 QVector<t_polarPoint*>* dataSNR1 = new QVector<t_polarPoint*>;
264 QVector<t_polarPoint*>* dataSNR2 = new QVector<t_polarPoint*>;
265
266 QMutableMapIterator<QString, t_allObs> it(_allObsMap);
267 while (it.hasNext()) {
268 it.next();
269 QString prn = it.key();
270 preparePlotData(prn, xyzSta, obsFile->interval(),
271 dataMP1, dataMP2, dataSNR1, dataSNR2);
272 }
273
274 emit dspSkyPlot(obsFile->fileName(), "MP1", dataMP1, "MP2", dataMP2,
275 "Meters", 2.0);
276
277 emit dspSkyPlot(obsFile->fileName(), "SNR1", dataSNR1, "SNR2", dataSNR2,
278 "", 9.0);
279
280 QFileInfo fileInfo(obsFile->fileName());
281 QByteArray title = fileInfo.fileName().toAscii();
282
283 emit dspAvailPlot(obsFile->fileName(), title);
284
285 if (_log) {
286 _log->flush();
287 }
288}
289
290//
291////////////////////////////////////////////////////////////////////////////
292void t_reqcAnalyze::t_allObs::addObs(const t_obs& obs) {
293
294 t_oneObs* newObs = new t_oneObs(obs.GPSWeek, obs.GPSWeeks);
295 bool okFlag = false;
296
297 // Availability and Slip Flags
298 // ---------------------------
299 double L1 = obs.measdata("L1", 3.0);
300 if (L1 != 0) {
301 newObs->_hasL1 = true;
302 }
303 double L2 = obs.measdata("L2", 3.0);
304 if (L2 != 0) {
305 newObs->_hasL2 = true;
306 }
307 if (obs.slipL1) {
308 newObs->_slipL1 = true;
309 }
310 if (obs.slipL2) {
311 newObs->_slipL2 = true;
312 }
313
314 // Compute the Multipath
315 // ----------------------
316 if (L1 != 0.0 && L2 != 0.0) {
317 double f1 = t_CST::f1(obs.satSys, obs.slotNum);
318 double f2 = t_CST::f2(obs.satSys, obs.slotNum);
319
320 L1 = L1 * t_CST::c / f1;
321 L2 = L2 * t_CST::c / f2;
322
323 double P1 = obs.measdata("C1", 3.0);
324 if (P1 != 0.0) {
325 newObs->_MP1 = P1 - L1 - 2.0*f2*f2/(f1*f1-f2*f2) * (L1 - L2);
326 okFlag = true;
327 }
328 double P2 = obs.measdata("C2", 3.0);
329 if (P2 != 0.0) {
330 newObs->_MP2 = P2 - L2 - 2.0*f1*f1/(f1*f1-f2*f2) * (L1 - L2);
331 okFlag = true;
332 }
333 }
334
335 // Signal-to-Noise
336 // ---------------
337 double S1 = obs.measdata("S1", 3.0);
338 if (S1 != 0.0) {
339 newObs->_SNR1 = floor(S1/6);
340 if (newObs->_SNR1 > 9.0) {
341 newObs->_SNR1 = 9.0;
342 }
343 if (newObs->_SNR1 < 1.0) {
344 newObs->_SNR1 = 1.0;
345 }
346 okFlag = true;
347 }
348 else {
349 if (obs.snrL1 > 0) {
350 newObs->_SNR1 = obs.snrL1;
351 okFlag = true;
352 }
353 }
354 double S2 = obs.measdata("S2", 3.0);
355 if (S2 != 0.0) {
356 newObs->_SNR2 = floor(S2/6);
357 if (newObs->_SNR2 > 9.0) {
358 newObs->_SNR2 = 9.0;
359 }
360 if (newObs->_SNR2 < 1.0) {
361 newObs->_SNR2 = 1.0;
362 }
363 okFlag = true;
364 }
365 else {
366 if (obs.snrL2 > 0) {
367 newObs->_SNR2 = obs.snrL2;
368 okFlag = true;
369 }
370 }
371
372 // Remember the Observation
373 // ------------------------
374 if (okFlag) {
375 _oneObsVec << newObs;
376 }
377 else {
378 delete newObs;
379 }
380}
381
382//
383////////////////////////////////////////////////////////////////////////////
384void t_reqcAnalyze::prepareObsStat(unsigned iEpo, double obsInterval,
385 const ColumnVector& xyzSta) {
386 const int sampl = int(30.0 / obsInterval);
387 if (iEpo % sampl == 0) {
388 double mjdX24 = _currEpo->tt.mjddec() * 24.0;
389 if (iEpo != 0) {
390 _obsStat._mjdX24 << mjdX24;
391 _obsStat._numSat << _obsStat._numSat.last();
392 _obsStat._PDOP << _obsStat._PDOP.last();
393 }
394 _obsStat._mjdX24 << mjdX24;
395 _obsStat._numSat << _currEpo->rnxSat.size();
396 _obsStat._PDOP << cmpDOP(xyzSta);
397 }
398}
399
400//
401////////////////////////////////////////////////////////////////////////////
402void t_reqcAnalyze::preparePlotData(const QString& prn,
403 const ColumnVector& xyzSta,
404 double obsInterval,
405 QVector<t_polarPoint*>* dataMP1,
406 QVector<t_polarPoint*>* dataMP2,
407 QVector<t_polarPoint*>* dataSNR1,
408 QVector<t_polarPoint*>* dataSNR2) {
409
410 const int chunkStep = int( 30.0 / obsInterval); // chunk step (30 sec)
411 const int numEpo = int(600.0 / obsInterval); // # epochs in one chunk (10 min)
412
413 t_allObs& allObs = _allObsMap[prn];
414
415 // Loop over all Chunks of Data
416 // ----------------------------
417 for (int chunkStart = 0; chunkStart + numEpo < allObs._oneObsVec.size();
418 chunkStart += chunkStep) {
419
420 // Chunk-Specific Variables
421 // ------------------------
422 bncTime currTime;
423 bncTime prevTime;
424 bncTime chunkStartTime;
425 double mjdX24 = 0.0;
426 bool availL1 = false;
427 bool availL2 = false;
428 bool gapL1 = false;
429 bool gapL2 = false;
430 bool slipL1 = false;
431 bool slipL2 = false;
432 bool slipMP = false;
433 double meanMP1 = 0.0;
434 double meanMP2 = 0.0;
435 double minSNR1 = 0.0;
436 double minSNR2 = 0.0;
437 double aziDeg = 0.0;
438 double zenDeg = 0.0;
439 bool zenFlag = false;
440
441 // Loop over all Epochs within one Chunk of Data
442 // ---------------------------------------------
443 for (int ii = 0; ii < numEpo; ii++) {
444 int iEpo = chunkStart + ii;
445 const t_oneObs* oneObs = allObs._oneObsVec[iEpo];
446
447 currTime.set(oneObs->_GPSWeek, oneObs->_GPSWeeks);
448
449 // Compute the Azimuth and Zenith Distance
450 // ---------------------------------------
451 if (ii == 0) {
452 chunkStartTime = currTime;
453 mjdX24 = chunkStartTime.mjddec() * 24.0;
454
455 if (xyzSta.size()) {
456 t_eph* eph = 0;
457 for (int ie = 0; ie < _ephs.size(); ie++) {
458 if (_ephs[ie]->prn() == prn) {
459 eph = _ephs[ie];
460 break;
461 }
462 }
463
464 if (eph) {
465 double xSat, ySat, zSat, clkSat;
466 eph->position(oneObs->_GPSWeek, oneObs->_GPSWeeks,
467 xSat, ySat, zSat, clkSat);
468
469 double rho, eleSat, azSat;
470 topos(xyzSta(1), xyzSta(2), xyzSta(3),
471 xSat, ySat, zSat, rho, eleSat, azSat);
472
473 aziDeg = azSat * 180.0/M_PI;
474 zenDeg = 90.0 - eleSat * 180.0/M_PI;
475 zenFlag = true;
476 }
477 }
478 }
479
480 // Check Interval
481 // --------------
482 if (prevTime.valid()) {
483 double dt = currTime - prevTime;
484 if (dt != obsInterval) {
485 gapL1 = true;
486 gapL2 = true;
487 }
488 }
489 prevTime = currTime;
490
491 // Check L1 and L2 availability
492 // ----------------------------
493 if (oneObs->_hasL1) {
494 availL1 = true;
495 }
496 else {
497 gapL1 = true;
498 }
499 if (oneObs->_hasL2) {
500 availL2 = true;
501 }
502 else {
503 gapL2 = true;
504 }
505
506 // Check Minimal Signal-to-Noise Ratio
507 // -----------------------------------
508 if ( oneObs->_SNR1 > 0 && (minSNR1 == 0 || minSNR1 > oneObs->_SNR1) ) {
509 minSNR1 = oneObs->_SNR1;
510 }
511 if ( oneObs->_SNR2 > 0 && (minSNR2 == 0 || minSNR2 > oneObs->_SNR2) ) {
512 minSNR2 = oneObs->_SNR2;
513 }
514
515 // Check Slip Flags
516 // ----------------
517 if (oneObs->_slipL1) {
518 slipL1 = true;
519 }
520 if (oneObs->_slipL2) {
521 slipL2 = true;
522 }
523
524 // Check Slip Threshold
525 // --------------------
526 if (ii > 0) {
527 double diff1 = oneObs->_MP1 - allObs._oneObsVec[iEpo-1]->_MP1;
528 double diff2 = oneObs->_MP2 - allObs._oneObsVec[iEpo-1]->_MP2;
529 if (fabs(diff1) > SLIPTRESH || fabs(diff2) > SLIPTRESH) {
530 slipMP = true;
531 }
532 }
533
534 meanMP1 += oneObs->_MP1;
535 meanMP2 += oneObs->_MP2;
536 }
537
538 // Availability Plot Data
539 // ----------------------
540 if (availL1) {
541 if (slipL1) {
542 _availDataMap[prn]._L1slip << mjdX24;
543 }
544 else if (gapL1) {
545 _availDataMap[prn]._L1gap << mjdX24;
546 }
547 else {
548 _availDataMap[prn]._L1ok << mjdX24;
549 }
550 }
551 if (availL2) {
552 if (slipL2) {
553 _availDataMap[prn]._L2slip << mjdX24;
554 }
555 else if (gapL2) {
556 _availDataMap[prn]._L2gap << mjdX24;
557 }
558 else {
559 _availDataMap[prn]._L2ok << mjdX24;
560 }
561 }
562 if (zenFlag) {
563 _availDataMap[prn]._eleTim << mjdX24;
564 _availDataMap[prn]._eleDeg << 90.0 - zenDeg;
565 }
566
567 // Signal-to-Noise Ration Plot Data
568 // --------------------------------
569 (*dataSNR1) << (new t_polarPoint(aziDeg, zenDeg, minSNR1));
570 (*dataSNR2) << (new t_polarPoint(aziDeg, zenDeg, minSNR2));
571
572 // Compute the Multipath
573 // ---------------------
574 if (!slipMP) {
575 meanMP1 /= numEpo;
576 meanMP2 /= numEpo;
577 double MP1 = 0.0;
578 double MP2 = 0.0;
579 for (int ii = 0; ii < numEpo; ii++) {
580 int iEpo = chunkStart + ii;
581 const t_oneObs* oneObs = allObs._oneObsVec[iEpo];
582 double diff1 = oneObs->_MP1 - meanMP1;
583 double diff2 = oneObs->_MP2 - meanMP2;
584 MP1 += diff1 * diff1;
585 MP2 += diff2 * diff2;
586 }
587 MP1 = sqrt(MP1 / (numEpo-1));
588 MP2 = sqrt(MP2 / (numEpo-1));
589 (*dataMP1) << (new t_polarPoint(aziDeg, zenDeg, MP1));
590 (*dataMP2) << (new t_polarPoint(aziDeg, zenDeg, MP2));
591 }
592 }
593}
594
595//
596////////////////////////////////////////////////////////////////////////////
597void t_reqcAnalyze::slotDspAvailPlot(const QString& fileName,
598 const QByteArray& title) {
599
600 if (dynamic_cast<bncApp*>(qApp)->GUIenabled()) {
601
602 t_availPlot* plotA = new t_availPlot(0, &_availDataMap);
603 plotA->setTitle(title);
604
605 t_elePlot* plotZ = new t_elePlot(0, &_availDataMap);
606
607 t_dopPlot* plotD = new t_dopPlot(0, &_obsStat);
608
609 QVector<QWidget*> plots;
610 plots << plotA << plotZ << plotD;
611 t_graphWin* graphWin = new t_graphWin(0, fileName, plots, 0, 0);
612
613 int ww = QFontMetrics(graphWin->font()).width('w');
614 graphWin->setMinimumSize(120*ww, 40*ww);
615
616 graphWin->show();
617
618 bncSettings settings;
619 QString dirName = settings.value("reqcPlotDir").toString();
620 if (!dirName.isEmpty()) {
621 QByteArray ext = "_A.png";
622 graphWin->savePNG(dirName, ext);
623 }
624 }
625}
626
627// Compute Dilution of Precision
628////////////////////////////////////////////////////////////////////////////
629double t_reqcAnalyze::cmpDOP(const ColumnVector& xyzSta) const {
630
631 if (xyzSta.size() != 3) {
632 return 0.0;
633 }
634
635 unsigned nSat = _currEpo->rnxSat.size();
636
637 if (nSat < 4) {
638 return 0.0;
639 }
640
641 Matrix AA(nSat, 4);
642
643 unsigned nSatUsed = 0;
644 for (unsigned iSat = 0; iSat < nSat; iSat++) {
645
646 const t_rnxObsFile::t_rnxSat& rnxSat = _currEpo->rnxSat[iSat];
647
648 QString prn = QString("%1%2").arg(rnxSat.satSys)
649 .arg(rnxSat.satNum, 2, 10, QChar('0'));
650
651 t_eph* eph = 0;
652 for (int ie = 0; ie < _ephs.size(); ie++) {
653 if (_ephs[ie]->prn() == prn) {
654 eph = _ephs[ie];
655 break;
656 }
657 }
658 if (eph) {
659 ++nSatUsed;
660 ColumnVector xSat(3);
661 double clkSat;
662 eph->position(_currEpo->tt.gpsw(), _currEpo->tt.gpssec(),
663 xSat(1), xSat(2), xSat(3), clkSat);
664 ColumnVector dx = xSat - xyzSta;
665 }
666
667 }
668
669 return 0.0;
670}
Note: See TracBrowser for help on using the repository browser.