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

Last change on this file since 4689 was 4689, checked in by mervart, 12 years ago
File size: 19.8 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 if (iEpo == 0) {
227 _obsStat._startTime = _currEpo->tt;
228 _obsStat._antennaName = obsFile->antennaName();
229 _obsStat._markerName = obsFile->markerName();
230 _obsStat._receiverType = obsFile->receiverType();
231 _obsStat._interval = obsFile->interval();
232 }
233 _obsStat._endTime = _currEpo->tt;
234
235 // Loop over all satellites
236 // ------------------------
237 for (unsigned iObs = 0; iObs < _currEpo->rnxSat.size(); iObs++) {
238 const t_rnxObsFile::t_rnxSat& rnxSat = _currEpo->rnxSat[iObs];
239 t_obs obs;
240 t_postProcessing::setObsFromRnx(obsFile, _currEpo, rnxSat, obs);
241
242 if (obs.satSys == 'R') {
243 // TODO: set channel number
244 }
245
246 QString prn = QString("%1%2").arg(obs.satSys)
247 .arg(obs.satNum, 2, 10, QChar('0'));
248
249 _allObsMap[prn].addObs(obs);
250
251 }
252
253 prepareObsStat(iEpo, obsFile->interval(), xyzSta);
254 iEpo++;
255
256 } // while (_currEpo)
257 }
258 catch (QString str) {
259 if (_log) {
260 *_log << "Exception " << str << endl;
261 }
262 else {
263 qDebug() << str;
264 }
265 return;
266 }
267
268 // Analyze the Multipath
269 // ---------------------
270 QVector<t_polarPoint*>* dataMP1 = new QVector<t_polarPoint*>;
271 QVector<t_polarPoint*>* dataMP2 = new QVector<t_polarPoint*>;
272 QVector<t_polarPoint*>* dataSNR1 = new QVector<t_polarPoint*>;
273 QVector<t_polarPoint*>* dataSNR2 = new QVector<t_polarPoint*>;
274
275 QMutableMapIterator<QString, t_allObs> it(_allObsMap);
276 while (it.hasNext()) {
277 it.next();
278 QString prn = it.key();
279 preparePlotData(prn, xyzSta, obsFile->interval(),
280 dataMP1, dataMP2, dataSNR1, dataSNR2);
281 }
282
283 emit dspSkyPlot(obsFile->fileName(), "MP1", dataMP1, "MP2", dataMP2,
284 "Meters", 2.0);
285
286 emit dspSkyPlot(obsFile->fileName(), "SNR1", dataSNR1, "SNR2", dataSNR2,
287 "", 9.0);
288
289 QFileInfo fileInfo(obsFile->fileName());
290 QByteArray title = fileInfo.fileName().toAscii();
291
292 emit dspAvailPlot(obsFile->fileName(), title);
293
294 printReport();
295}
296
297//
298////////////////////////////////////////////////////////////////////////////
299void t_reqcAnalyze::t_allObs::addObs(const t_obs& obs) {
300
301 t_oneObs* newObs = new t_oneObs(obs.GPSWeek, obs.GPSWeeks);
302 bool okFlag = false;
303
304 // Availability and Slip Flags
305 // ---------------------------
306 double L1 = obs.measdata("L1", 3.0);
307 if (L1 != 0) {
308 newObs->_hasL1 = true;
309 }
310 double L2 = obs.measdata("L2", 3.0);
311 if (L2 != 0) {
312 newObs->_hasL2 = true;
313 }
314 if (obs.slipL1) {
315 newObs->_slipL1 = true;
316 }
317 if (obs.slipL2) {
318 newObs->_slipL2 = true;
319 }
320
321 // Compute the Multipath
322 // ----------------------
323 if (L1 != 0.0 && L2 != 0.0) {
324 double f1 = t_CST::f1(obs.satSys, obs.slotNum);
325 double f2 = t_CST::f2(obs.satSys, obs.slotNum);
326
327 L1 = L1 * t_CST::c / f1;
328 L2 = L2 * t_CST::c / f2;
329
330 double P1 = obs.measdata("C1", 3.0);
331 if (P1 != 0.0) {
332 newObs->_MP1 = P1 - L1 - 2.0*f2*f2/(f1*f1-f2*f2) * (L1 - L2);
333 okFlag = true;
334 }
335 double P2 = obs.measdata("C2", 3.0);
336 if (P2 != 0.0) {
337 newObs->_MP2 = P2 - L2 - 2.0*f1*f1/(f1*f1-f2*f2) * (L1 - L2);
338 okFlag = true;
339 }
340 }
341
342 // Signal-to-Noise
343 // ---------------
344 double S1 = obs.measdata("S1", 3.0);
345 if (S1 != 0.0) {
346 newObs->_SNR1 = floor(S1/6);
347 if (newObs->_SNR1 > 9.0) {
348 newObs->_SNR1 = 9.0;
349 }
350 if (newObs->_SNR1 < 1.0) {
351 newObs->_SNR1 = 1.0;
352 }
353 okFlag = true;
354 }
355 else {
356 if (obs.snrL1 > 0) {
357 newObs->_SNR1 = obs.snrL1;
358 okFlag = true;
359 }
360 }
361 double S2 = obs.measdata("S2", 3.0);
362 if (S2 != 0.0) {
363 newObs->_SNR2 = floor(S2/6);
364 if (newObs->_SNR2 > 9.0) {
365 newObs->_SNR2 = 9.0;
366 }
367 if (newObs->_SNR2 < 1.0) {
368 newObs->_SNR2 = 1.0;
369 }
370 okFlag = true;
371 }
372 else {
373 if (obs.snrL2 > 0) {
374 newObs->_SNR2 = obs.snrL2;
375 okFlag = true;
376 }
377 }
378
379 // Remember the Observation
380 // ------------------------
381 if (okFlag) {
382 _oneObsVec << newObs;
383 }
384 else {
385 delete newObs;
386 }
387}
388
389//
390////////////////////////////////////////////////////////////////////////////
391void t_reqcAnalyze::prepareObsStat(unsigned iEpo, double obsInterval,
392 const ColumnVector& xyzSta) {
393 const int sampl = int(30.0 / obsInterval);
394 if (iEpo % sampl == 0) {
395 double mjdX24 = _currEpo->tt.mjddec() * 24.0;
396 if (iEpo != 0) {
397 _obsStat._mjdX24 << mjdX24;
398 _obsStat._numSat << _obsStat._numSat.last();
399 _obsStat._PDOP << _obsStat._PDOP.last();
400 }
401 _obsStat._mjdX24 << mjdX24;
402 _obsStat._numSat << _currEpo->rnxSat.size();
403 _obsStat._PDOP << cmpDOP(xyzSta);
404 }
405}
406
407//
408////////////////////////////////////////////////////////////////////////////
409void t_reqcAnalyze::preparePlotData(const QString& prn,
410 const ColumnVector& xyzSta,
411 double obsInterval,
412 QVector<t_polarPoint*>* dataMP1,
413 QVector<t_polarPoint*>* dataMP2,
414 QVector<t_polarPoint*>* dataSNR1,
415 QVector<t_polarPoint*>* dataSNR2) {
416
417 const int chunkStep = int( 30.0 / obsInterval); // chunk step (30 sec)
418 const int numEpo = int(600.0 / obsInterval); // # epochs in one chunk (10 min)
419
420 t_allObs& allObs = _allObsMap[prn];
421
422 // Loop over all Chunks of Data
423 // ----------------------------
424 for (int chunkStart = 0; chunkStart + numEpo < allObs._oneObsVec.size();
425 chunkStart += chunkStep) {
426
427 // Chunk-Specific Variables
428 // ------------------------
429 bncTime currTime;
430 bncTime prevTime;
431 bncTime chunkStartTime;
432 double mjdX24 = 0.0;
433 bool availL1 = false;
434 bool availL2 = false;
435 bool gapL1 = false;
436 bool gapL2 = false;
437 bool slipL1 = false;
438 bool slipL2 = false;
439 bool slipMP = false;
440 double meanMP1 = 0.0;
441 double meanMP2 = 0.0;
442 double minSNR1 = 0.0;
443 double minSNR2 = 0.0;
444 double aziDeg = 0.0;
445 double zenDeg = 0.0;
446 bool zenFlag = false;
447
448 // Loop over all Epochs within one Chunk of Data
449 // ---------------------------------------------
450 for (int ii = 0; ii < numEpo; ii++) {
451 int iEpo = chunkStart + ii;
452 const t_oneObs* oneObs = allObs._oneObsVec[iEpo];
453
454 currTime.set(oneObs->_GPSWeek, oneObs->_GPSWeeks);
455
456 // Compute the Azimuth and Zenith Distance
457 // ---------------------------------------
458 if (ii == 0) {
459 chunkStartTime = currTime;
460 mjdX24 = chunkStartTime.mjddec() * 24.0;
461
462 if (xyzSta.size()) {
463 t_eph* eph = 0;
464 for (int ie = 0; ie < _ephs.size(); ie++) {
465 if (_ephs[ie]->prn() == prn) {
466 eph = _ephs[ie];
467 break;
468 }
469 }
470
471 if (eph) {
472 double xSat, ySat, zSat, clkSat;
473 eph->position(oneObs->_GPSWeek, oneObs->_GPSWeeks,
474 xSat, ySat, zSat, clkSat);
475
476 double rho, eleSat, azSat;
477 topos(xyzSta(1), xyzSta(2), xyzSta(3),
478 xSat, ySat, zSat, rho, eleSat, azSat);
479
480 aziDeg = azSat * 180.0/M_PI;
481 zenDeg = 90.0 - eleSat * 180.0/M_PI;
482 zenFlag = true;
483 }
484 }
485 }
486
487 // Check Interval
488 // --------------
489 if (prevTime.valid()) {
490 double dt = currTime - prevTime;
491 if (dt != obsInterval) {
492 gapL1 = true;
493 gapL2 = true;
494 }
495 }
496 prevTime = currTime;
497
498 // Check L1 and L2 availability
499 // ----------------------------
500 if (oneObs->_hasL1) {
501 availL1 = true;
502 }
503 else {
504 gapL1 = true;
505 }
506 if (oneObs->_hasL2) {
507 availL2 = true;
508 }
509 else {
510 gapL2 = true;
511 }
512
513 // Check Minimal Signal-to-Noise Ratio
514 // -----------------------------------
515 if ( oneObs->_SNR1 > 0 && (minSNR1 == 0 || minSNR1 > oneObs->_SNR1) ) {
516 minSNR1 = oneObs->_SNR1;
517 }
518 if ( oneObs->_SNR2 > 0 && (minSNR2 == 0 || minSNR2 > oneObs->_SNR2) ) {
519 minSNR2 = oneObs->_SNR2;
520 }
521
522 // Check Slip Flags
523 // ----------------
524 if (oneObs->_slipL1) {
525 slipL1 = true;
526 }
527 if (oneObs->_slipL2) {
528 slipL2 = true;
529 }
530
531 // Check Slip Threshold
532 // --------------------
533 if (ii > 0) {
534 double diff1 = oneObs->_MP1 - allObs._oneObsVec[iEpo-1]->_MP1;
535 double diff2 = oneObs->_MP2 - allObs._oneObsVec[iEpo-1]->_MP2;
536 if (fabs(diff1) > SLIPTRESH || fabs(diff2) > SLIPTRESH) {
537 slipMP = true;
538 }
539 }
540
541 meanMP1 += oneObs->_MP1;
542 meanMP2 += oneObs->_MP2;
543 }
544
545 // Availability Plot Data
546 // ----------------------
547 if (availL1) {
548 if (slipL1) {
549 _availDataMap[prn]._L1slip << mjdX24;
550 }
551 else if (gapL1) {
552 _availDataMap[prn]._L1gap << mjdX24;
553 }
554 else {
555 _availDataMap[prn]._L1ok << mjdX24;
556 }
557 }
558 if (availL2) {
559 if (slipL2) {
560 _availDataMap[prn]._L2slip << mjdX24;
561 }
562 else if (gapL2) {
563 _availDataMap[prn]._L2gap << mjdX24;
564 }
565 else {
566 _availDataMap[prn]._L2ok << mjdX24;
567 }
568 }
569 if (zenFlag) {
570 _availDataMap[prn]._eleTim << mjdX24;
571 _availDataMap[prn]._eleDeg << 90.0 - zenDeg;
572 }
573
574 // Signal-to-Noise Ration Plot Data
575 // --------------------------------
576 (*dataSNR1) << (new t_polarPoint(aziDeg, zenDeg, minSNR1));
577 (*dataSNR2) << (new t_polarPoint(aziDeg, zenDeg, minSNR2));
578
579 // Compute the Multipath
580 // ---------------------
581 if (!slipMP) {
582 meanMP1 /= numEpo;
583 meanMP2 /= numEpo;
584 double MP1 = 0.0;
585 double MP2 = 0.0;
586 for (int ii = 0; ii < numEpo; ii++) {
587 int iEpo = chunkStart + ii;
588 const t_oneObs* oneObs = allObs._oneObsVec[iEpo];
589 double diff1 = oneObs->_MP1 - meanMP1;
590 double diff2 = oneObs->_MP2 - meanMP2;
591 MP1 += diff1 * diff1;
592 MP2 += diff2 * diff2;
593 }
594 MP1 = sqrt(MP1 / (numEpo-1));
595 MP2 = sqrt(MP2 / (numEpo-1));
596 (*dataMP1) << (new t_polarPoint(aziDeg, zenDeg, MP1));
597 (*dataMP2) << (new t_polarPoint(aziDeg, zenDeg, MP2));
598 }
599 }
600}
601
602//
603////////////////////////////////////////////////////////////////////////////
604void t_reqcAnalyze::slotDspAvailPlot(const QString& fileName,
605 const QByteArray& title) {
606
607 if (dynamic_cast<bncApp*>(qApp)->GUIenabled()) {
608
609 t_availPlot* plotA = new t_availPlot(0, &_availDataMap);
610 plotA->setTitle(title);
611
612 t_elePlot* plotZ = new t_elePlot(0, &_availDataMap);
613
614 t_dopPlot* plotD = new t_dopPlot(0, &_obsStat);
615
616 QVector<QWidget*> plots;
617 plots << plotA << plotZ << plotD;
618 t_graphWin* graphWin = new t_graphWin(0, fileName, plots, 0, 0);
619
620 int ww = QFontMetrics(graphWin->font()).width('w');
621 graphWin->setMinimumSize(120*ww, 40*ww);
622
623 graphWin->show();
624
625 bncSettings settings;
626 QString dirName = settings.value("reqcPlotDir").toString();
627 if (!dirName.isEmpty()) {
628 QByteArray ext = "_A.png";
629 graphWin->savePNG(dirName, ext);
630 }
631 }
632}
633
634// Compute Dilution of Precision
635////////////////////////////////////////////////////////////////////////////
636double t_reqcAnalyze::cmpDOP(const ColumnVector& xyzSta) const {
637
638 if (xyzSta.size() != 3) {
639 return 0.0;
640 }
641
642 unsigned nSat = _currEpo->rnxSat.size();
643
644 if (nSat < 4) {
645 return 0.0;
646 }
647
648 Matrix AA(nSat, 4);
649
650 unsigned nSatUsed = 0;
651 for (unsigned iSat = 0; iSat < nSat; iSat++) {
652
653 const t_rnxObsFile::t_rnxSat& rnxSat = _currEpo->rnxSat[iSat];
654
655 QString prn = QString("%1%2").arg(rnxSat.satSys)
656 .arg(rnxSat.satNum, 2, 10, QChar('0'));
657
658 t_eph* eph = 0;
659 for (int ie = 0; ie < _ephs.size(); ie++) {
660 if (_ephs[ie]->prn() == prn) {
661 eph = _ephs[ie];
662 break;
663 }
664 }
665 if (eph) {
666 ++nSatUsed;
667 ColumnVector xSat(3);
668 double clkSat;
669 eph->position(_currEpo->tt.gpsw(), _currEpo->tt.gpssec(),
670 xSat(1), xSat(2), xSat(3), clkSat);
671 ColumnVector dx = xSat - xyzSta;
672 double rho = dx.norm_Frobenius();
673 AA(nSatUsed,1) = dx(1) / rho;
674 AA(nSatUsed,2) = dx(2) / rho;
675 AA(nSatUsed,3) = dx(3) / rho;
676 AA(nSatUsed,4) = 1.0;
677 }
678 }
679
680 if (nSatUsed < 4) {
681 return 0.0;
682 }
683
684 AA = AA.Rows(1, nSatUsed);
685
686 SymmetricMatrix QQ;
687 QQ << AA.t() * AA;
688 QQ = QQ.i();
689
690 return sqrt(QQ.trace());
691}
692
693// Finish the report
694////////////////////////////////////////////////////////////////////////////
695void t_reqcAnalyze::printReport() {
696
697 if (!_log) {
698 return;
699 }
700
701 *_log << "Marker name: " << _obsStat._markerName << endl
702 << "Receiver: " << _obsStat._receiverType << endl
703 << "Antenna: " << _obsStat._antennaName << endl
704 << "Start time: " << _obsStat._startTime.datestr().c_str() << ' '
705 << _obsStat._startTime.timestr().c_str() << endl;
706
707 _log->flush();
708}
Note: See TracBrowser for help on using the repository browser.