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

Last change on this file since 4579 was 4579, checked in by mervart, 12 years ago
File size: 15.2 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
55using namespace std;
56
57const double SLIPTRESH = 5.0; // cycle-slip threshold (meters)
58
59// Constructor
60////////////////////////////////////////////////////////////////////////////
61t_reqcAnalyze::t_reqcAnalyze(QObject* parent) : QThread(parent) {
62
63 bncSettings settings;
64
65 _logFileName = settings.value("reqcOutLogFile").toString(); expandEnvVar(_logFileName);
66 _logFile = 0;
67 _log = 0;
68 _obsFileNames = settings.value("reqcObsFile").toString().split(",", QString::SkipEmptyParts);
69 _navFileNames = settings.value("reqcNavFile").toString().split(",", QString::SkipEmptyParts);
70
71 _currEpo = 0;
72
73 connect(this, SIGNAL(dspSkyPlot(const QString&,
74 const QByteArray&,
75 QVector<t_polarPoint*>*,
76 const QByteArray&,
77 QVector<t_polarPoint*>*,
78 const QByteArray&, double)),
79 this, SLOT(slotDspSkyPlot(const QString&,
80 const QByteArray&,
81 QVector<t_polarPoint*>*,
82 const QByteArray&,
83 QVector<t_polarPoint*>*,
84 const QByteArray&, double)));
85
86 connect(this, SIGNAL(dspAvailPlot(const QString&,
87 const QByteArray&,
88 QMap<QString, QVector<int> >*)),
89 this, SLOT(slotDspAvailPlot(const QString&,
90 const QByteArray&,
91 QMap<QString, QVector<int> >*)));
92
93}
94
95// Destructor
96////////////////////////////////////////////////////////////////////////////
97t_reqcAnalyze::~t_reqcAnalyze() {
98 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
99 delete _rnxObsFiles[ii];
100 }
101 for (int ii = 0; ii < _ephs.size(); ii++) {
102 delete _ephs[ii];
103 }
104 delete _log; _log = 0;
105 delete _logFile; _logFile = 0;
106 bncApp* app = (bncApp*) qApp;
107 if ( app->mode() != bncApp::interactive) {
108 app->exit(0);
109 }
110}
111
112//
113////////////////////////////////////////////////////////////////////////////
114void t_reqcAnalyze::slotDspSkyPlot(const QString& fileName,
115 const QByteArray& title1,
116 QVector<t_polarPoint*>* data1,
117 const QByteArray& title2,
118 QVector<t_polarPoint*>* data2,
119 const QByteArray& scaleTitle,
120 double maxValue) {
121
122 bncApp* app = dynamic_cast<bncApp*>(qApp);
123 if (app->GUIenabled()) {
124
125 if (maxValue == 0.0) {
126 if (data1) {
127 for (int ii = 0; ii < data1->size(); ii++) {
128 double val = data1->at(ii)->_value;
129 if (maxValue < val) {
130 maxValue = val;
131 }
132 }
133 }
134 if (data2) {
135 for (int ii = 0; ii < data2->size(); ii++) {
136 double val = data2->at(ii)->_value;
137 if (maxValue < val) {
138 maxValue = val;
139 }
140 }
141 }
142 }
143
144 QwtInterval scaleInterval(0.0, maxValue);
145
146 QVector<QWidget*> plots;
147 if (data1) {
148 t_polarPlot* plot1 = new t_polarPlot(QwtText(title1), scaleInterval,
149 app->mainWindow());
150 plot1->addCurve(data1);
151 plots << plot1;
152 }
153 if (data2) {
154 t_polarPlot* plot2 = new t_polarPlot(QwtText(title2), scaleInterval,
155 app->mainWindow());
156 plot2->addCurve(data2);
157 plots << plot2;
158 }
159
160 t_graphWin* graphWin = new t_graphWin(0, fileName, plots,
161 &scaleTitle, &scaleInterval);
162
163 graphWin->show();
164
165 bncSettings settings;
166 QString dirName = settings.value("reqcPlotDir").toString();
167 if (!dirName.isEmpty()) {
168 QByteArray ext = scaleTitle.isEmpty() ? "_SNR.png" : "_MP.png";
169 graphWin->savePNG(dirName, ext);
170 }
171 }
172}
173
174//
175////////////////////////////////////////////////////////////////////////////
176void t_reqcAnalyze::run() {
177
178 // Open Log File
179 // -------------
180 _logFile = new QFile(_logFileName);
181 if (_logFile->open(QIODevice::WriteOnly | QIODevice::Text)) {
182 _log = new QTextStream();
183 _log->setDevice(_logFile);
184 }
185
186 // Initialize RINEX Observation Files
187 // ----------------------------------
188 t_reqcEdit::initRnxObsFiles(_obsFileNames, _rnxObsFiles, _log);
189
190 // Read Ephemerides
191 // ----------------
192 t_reqcEdit::readEphemerides(_navFileNames, _ephs);
193
194 // Loop over all RINEX Files
195 // -------------------------
196 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
197 analyzeFile(_rnxObsFiles[ii]);
198 }
199
200 // Exit
201 // ----
202 emit finished();
203 deleteLater();
204}
205
206//
207////////////////////////////////////////////////////////////////////////////
208void t_reqcAnalyze::analyzeFile(t_rnxObsFile* obsFile) {
209
210 if (_log) {
211 *_log << "\nAnalyze File\n"
212 << "------------\n"
213 << obsFile->fileName().toAscii().data() << endl << endl;
214 }
215
216 _satStat.clear();
217
218 // A priori Coordinates
219 // --------------------
220 ColumnVector xyz = obsFile->xyz();
221
222 // Loop over all Epochs
223 // --------------------
224 try {
225 while ( (_currEpo = obsFile->nextEpoch()) != 0) {
226
227 // Loop over all satellites
228 // ------------------------
229 for (unsigned iObs = 0; iObs < _currEpo->rnxSat.size(); iObs++) {
230 const t_rnxObsFile::t_rnxSat& rnxSat = _currEpo->rnxSat[iObs];
231 t_obs obs;
232 t_postProcessing::setObsFromRnx(obsFile, _currEpo, rnxSat, obs);
233
234 if (obs.satSys == 'R') {
235 continue; // TODO: set channel number
236 }
237
238 QString prn = QString("%1%2").arg(obs.satSys)
239 .arg(obs.satNum, 2, 10, QChar('0'));
240
241 t_satStat& satStat = _satStat[prn];
242
243 satStat.addObs(obs);
244 }
245
246 } // while (_currEpo)
247 }
248 catch (QString str) {
249 if (_log) {
250 *_log << "Exception " << str << endl;
251 }
252 else {
253 qDebug() << str;
254 }
255 return;
256 }
257
258 // Analyze the Multipath
259 // ---------------------
260 QVector<t_polarPoint*>* dataMP1 = new QVector<t_polarPoint*>;
261 QVector<t_polarPoint*>* dataMP2 = new QVector<t_polarPoint*>;
262 QVector<t_polarPoint*>* dataSNR1 = new QVector<t_polarPoint*>;
263 QVector<t_polarPoint*>* dataSNR2 = new QVector<t_polarPoint*>;
264 QMap<QString, QVector<int> >* availL1 = new QMap<QString, QVector<int> >;
265
266
267 QMapIterator<QString, t_satStat> it(_satStat);
268 while (it.hasNext()) {
269 it.next();
270 QString prn = it.key();
271 const t_satStat& satStat = it.value();
272 QVector<int>& dataL1 = (*availL1)[prn];
273 preparePlotData(prn, satStat, xyz, obsFile->interval(),
274 dataMP1, dataMP2, dataSNR1, dataSNR2, dataL1);
275 }
276
277 emit dspSkyPlot(obsFile->fileName(), "MP1", dataMP1, "MP2", dataMP2,
278 "Meters", 2.0);
279
280 emit dspSkyPlot(obsFile->fileName(), "SNR1", dataSNR1, "SNR2", dataSNR2,
281 "", 9.0);
282
283 emit dspAvailPlot(obsFile->fileName(), "Availability L1", availL1);
284
285 if (_log) {
286 _log->flush();
287 }
288}
289
290//
291////////////////////////////////////////////////////////////////////////////
292void t_reqcAnalyze::t_satStat::addObs(const t_obs& obs) {
293
294 t_anaObs* newObs = new t_anaObs(obs.GPSWeek, obs.GPSWeeks);
295 bool okFlag = false;
296
297 // Compute the Multipath
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 (L1 != 0.0 && L2 != 0.0) {
308 double f1 = t_CST::f1(obs.satSys, obs.slotNum);
309 double f2 = t_CST::f2(obs.satSys, obs.slotNum);
310
311 L1 = L1 * t_CST::c / f1;
312 L2 = L2 * t_CST::c / f2;
313
314 double P1 = obs.measdata("C1", 3.0);
315 if (P1 != 0.0) {
316 newObs->_MP1 = P1 - L1 - 2.0*f2*f2/(f1*f1-f2*f2) * (L1 - L2);
317 okFlag = true;
318 }
319 double P2 = obs.measdata("C2", 3.0);
320 if (P2 != 0.0) {
321 newObs->_MP2 = P2 - L2 - 2.0*f1*f1/(f1*f1-f2*f2) * (L1 - L2);
322 okFlag = true;
323 }
324 }
325
326 // Signal-to-Noise
327 // ---------------
328 double S1 = obs.measdata("S1", 3.0);
329 if (S1 != 0.0) {
330 newObs->_SNR1 = floor(S1/6);
331 if (newObs->_SNR1 > 9.0) {
332 newObs->_SNR1 = 9.0;
333 }
334 if (newObs->_SNR1 < 1.0) {
335 newObs->_SNR1 = 1.0;
336 }
337 okFlag = true;
338 }
339 else {
340 if (obs.snrL1 > 0) {
341 newObs->_SNR1 = obs.snrL1;
342 okFlag = true;
343 }
344 }
345 double S2 = obs.measdata("S2", 3.0);
346 if (S2 != 0.0) {
347 newObs->_SNR2 = floor(S2/6);
348 if (newObs->_SNR2 > 9.0) {
349 newObs->_SNR2 = 9.0;
350 }
351 if (newObs->_SNR2 < 1.0) {
352 newObs->_SNR2 = 1.0;
353 }
354 okFlag = true;
355 }
356 else {
357 if (obs.snrL2 > 0) {
358 newObs->_SNR2 = obs.snrL2;
359 okFlag = true;
360 }
361 }
362
363 // Remember the Observation
364 // ------------------------
365 if (okFlag) {
366 anaObs << newObs;
367 }
368 else {
369 delete newObs;
370 }
371}
372
373//
374////////////////////////////////////////////////////////////////////////////
375void t_reqcAnalyze::preparePlotData(const QString& prn,
376 const t_satStat& satStat,
377 const ColumnVector& xyz,
378 double obsInterval,
379 QVector<t_polarPoint*>* dataMP1,
380 QVector<t_polarPoint*>* dataMP2,
381 QVector<t_polarPoint*>* dataSNR1,
382 QVector<t_polarPoint*>* dataSNR2,
383 QVector<int>& dataL1) {
384
385 const int chunkStep = int( 30.0 / obsInterval); // chunk step (30 sec)
386 const int numEpo = int(600.0 / obsInterval); // # epochs in one chunk (10 min)
387
388 for (int chunkStart = 0; chunkStart + numEpo < satStat.anaObs.size();
389 chunkStart += chunkStep) {
390
391 bncTime firstEpoch;
392
393 // Compute Mean
394 // ------------
395 bool slipFlag = false;
396 double mean1 = 0.0;
397 double mean2 = 0.0;
398 double SNR1 = 0.0;
399 double SNR2 = 0.0;
400
401 for (int ii = 0; ii < numEpo; ii++) {
402 int iEpo = chunkStart + ii;
403 const t_anaObs* anaObs = satStat.anaObs[iEpo];
404
405 if (ii == 0) {
406 firstEpoch.set(anaObs->_GPSWeek, anaObs->_GPSWeeks);
407 }
408
409 mean1 += anaObs->_MP1;
410 mean2 += anaObs->_MP2;
411
412 if ( anaObs->_SNR1 > 0 && (SNR1 == 0 || SNR1 > anaObs->_SNR1) ) {
413 SNR1 = anaObs->_SNR1;
414 }
415 if ( anaObs->_SNR2 > 0 && (SNR2 == 0 || SNR2 > anaObs->_SNR2) ) {
416 SNR2 = anaObs->_SNR2;
417 }
418
419 // Check Slip
420 // ----------
421 if (ii > 0) {
422 double diff1 = anaObs->_MP1 - satStat.anaObs[iEpo-1]->_MP1;
423 double diff2 = anaObs->_MP2 - satStat.anaObs[iEpo-1]->_MP2;
424 if (fabs(diff1) > SLIPTRESH || fabs(diff2) > SLIPTRESH) {
425 slipFlag = true;
426 break;
427 }
428 }
429 }
430
431 dataL1 << int(firstEpoch.gpssec());
432
433 if (slipFlag) {
434 continue;
435 }
436
437 mean1 /= numEpo;
438 mean2 /= numEpo;
439
440 // Compute Standard Deviation
441 // --------------------------
442 double stddev1 = 0.0;
443 double stddev2 = 0.0;
444 for (int ii = 0; ii < numEpo; ii++) {
445 int iEpo = chunkStart + ii;
446 const t_anaObs* anaObs = satStat.anaObs[iEpo];
447 double diff1 = anaObs->_MP1 - mean1;
448 double diff2 = anaObs->_MP2 - mean2;
449 stddev1 += diff1 * diff1;
450 stddev2 += diff2 * diff2;
451 }
452 double MP1 = sqrt(stddev1 / (numEpo-1));
453 double MP2 = sqrt(stddev2 / (numEpo-1));
454
455 const t_anaObs* anaObs0 = satStat.anaObs[chunkStart];
456
457 // Compute the Azimuth and Zenith Distance
458 // ---------------------------------------
459 double az = 0.0;
460 double zen = 0.0;
461 if (xyz.size()) {
462 t_eph* eph = 0;
463 for (int ie = 0; ie < _ephs.size(); ie++) {
464 if (_ephs[ie]->prn() == prn) {
465 eph = _ephs[ie];
466 break;
467 }
468 }
469
470 if (eph) {
471 double xSat, ySat, zSat, clkSat;
472 eph->position(anaObs0->_GPSWeek, anaObs0->_GPSWeeks,
473 xSat, ySat, zSat, clkSat);
474
475 double rho, eleSat, azSat;
476 topos(xyz(1), xyz(2), xyz(3), xSat, ySat, zSat, rho, eleSat, azSat);
477
478 az = azSat * 180.0/M_PI;
479 zen = 90.0 - eleSat * 180.0/M_PI;
480 }
481 }
482
483 // Add new Point
484 // -------------
485 (*dataMP1) << (new t_polarPoint(az, zen, MP1));
486 (*dataMP2) << (new t_polarPoint(az, zen, MP2));
487 (*dataSNR1) << (new t_polarPoint(az, zen, SNR1));
488 (*dataSNR2) << (new t_polarPoint(az, zen, SNR2));
489
490 if (_log) {
491 _log->setRealNumberNotation(QTextStream::FixedNotation);
492
493 _log->setRealNumberPrecision(2);
494 *_log << "MP1 " << prn << " " << az << " " << zen << " ";
495 _log->setRealNumberPrecision(3);
496 *_log << MP1 << endl;
497
498 _log->setRealNumberPrecision(2);
499 *_log << "MP2 " << prn << " " << az << " " << zen << " ";
500 _log->setRealNumberPrecision(3);
501 *_log << MP2 << endl;
502
503 _log->flush();
504 }
505 }
506}
507
508//
509////////////////////////////////////////////////////////////////////////////
510void t_reqcAnalyze::slotDspAvailPlot(const QString& fileName,
511 const QByteArray& title,
512 QMap<QString, QVector<int> >* prnAvail){
513
514 if (dynamic_cast<bncApp*>(qApp)->GUIenabled()) {
515
516 t_availPlot* plot = new t_availPlot(0, prnAvail);
517
518 QVector<QWidget*> plots;
519 plots << plot;
520 t_graphWin* graphWin = new t_graphWin(0, fileName, plots, 0, 0);
521 graphWin->show();
522
523 bncSettings settings;
524 QString dirName = settings.value("reqcPlotDir").toString();
525 if (!dirName.isEmpty()) {
526 QByteArray ext = "_AVAIL.png";
527 graphWin->savePNG(dirName, ext);
528 }
529 }
530}
Note: See TracBrowser for help on using the repository browser.