source: ntrip/trunk/BNC/bncapp.cpp@ 584

Last change on this file since 584 was 584, checked in by weber, 16 years ago

* empty log message *

File size: 16.4 KB
RevLine 
[280]1// Part of BNC, a utility for retrieving decoding and
[464]2// converting GNSS data streams from NTRIP broadcasters.
[280]3//
[464]4// Copyright (C) 2007
[280]5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
[464]7// Czech Technical University Prague, Department of Geodesy
[280]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.
[82]24
25/* -------------------------------------------------------------------------
[93]26 * BKG NTRIP Client
[82]27 * -------------------------------------------------------------------------
28 *
29 * Class: bncApp
30 *
31 * Purpose: This class implements the main application
32 *
33 * Author: L. Mervart
34 *
35 * Created: 29-Aug-2006
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
[149]42#include <QSettings>
[150]43#include <QMessageBox>
[519]44#include <cmath>
[82]45
46#include "bncapp.h"
[151]47#include "bncutils.h"
[82]48
49using namespace std;
50
[523]51struct converttimeinfo {
52 int second; /* seconds of GPS time [0..59] */
53 int minute; /* minutes of GPS time [0..59] */
54 int hour; /* hour of GPS time [0..24] */
55 int day; /* day of GPS time [1..28..30(31)*/
56 int month; /* month of GPS time [1..12]*/
57 int year; /* year of GPS time [1980..] */
58};
59
60extern "C" {
61 void converttime(struct converttimeinfo *c, int week, int tow);
62 void updatetime(int *week, int *tow, int tk, int fixnumleap);
63}
64
[82]65// Constructor
66////////////////////////////////////////////////////////////////////////////
67bncApp::bncApp(int argc, char* argv[], bool GUIenabled) :
68 QApplication(argc, argv, GUIenabled) {
[109]69
[565]70 _bncVersion = "BNC 1.5";
[533]71
[150]72 _logFileFlag = 0;
73 _logFile = 0;
74 _logStream = 0;
[152]75
[516]76 // Lists of Ephemeris
77 // ------------------
78 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
79 _gpsEph[ii-PRN_GPS_START] = 0;
80 }
81 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
82 _glonassEph[ii-PRN_GLONASS_START] = 0;
83 }
84
[533]85 // Eph file(s)
86 // -----------
[534]87 _rinexVers = 0;
[533]88 _ephFileGPS = 0;
89 _ephStreamGPS = 0;
90 _ephFileGlonass = 0;
91 _ephStreamGlonass = 0;
[559]92
93 _pgmName = _bncVersion.leftJustified(20, ' ', true);
94#ifdef WIN32
95 _userName = QString("${USERNAME}");
96#else
97 _userName = QString("${USER}");
98#endif
99 expandEnvVar(_userName);
100 _userName = _userName.leftJustified(20, ' ', true);
[82]101}
102
103// Destructor
104////////////////////////////////////////////////////////////////////////////
105bncApp::~bncApp() {
[109]106 delete _logStream;
107 delete _logFile;
[533]108 delete _ephStreamGPS;
109 delete _ephFileGPS;
[534]110 if (_rinexVers == 2) {
[533]111 delete _ephStreamGlonass;
112 delete _ephFileGlonass;
113 }
[516]114 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
115 delete _gpsEph[ii-PRN_GPS_START];
116 }
117 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
118 delete _glonassEph[ii-PRN_GLONASS_START];
119 }
[82]120}
121
122// Write a Program Message
123////////////////////////////////////////////////////////////////////////////
124void bncApp::slotMessage(const QByteArray msg) {
[150]125
[243]126 QMutexLocker locker(&_mutex);
127
[150]128 // First time resolve the log file name
129 // ------------------------------------
130 if (_logFileFlag == 0) {
131 _logFileFlag = 1;
132 QSettings settings;
133 QString logFileName = settings.value("logFile").toString();
134 if ( !logFileName.isEmpty() ) {
[151]135 expandEnvVar(logFileName);
[150]136 _logFile = new QFile(logFileName);
[275]137 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
138 _logFile->open(QIODevice::WriteOnly | QIODevice::Append);
139 }
140 else {
141 _logFile->open(QIODevice::WriteOnly);
142 }
[150]143 _logStream = new QTextStream();
144 _logStream->setDevice(_logFile);
145 }
146 }
147
[109]148 if (_logStream) {
[566]149 *_logStream << QDateTime::currentDateTime().toUTC().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
[109]150 *_logStream << msg.data() << endl;
151 _logStream->flush();
[82]152 }
153}
[511]154
[535]155// New GPS Ephemeris
[511]156////////////////////////////////////////////////////////////////////////////
157void bncApp::slotNewGPSEph(gpsephemeris* gpseph) {
[516]158
159 QMutexLocker locker(&_mutex);
160
[534]161 printEphHeader();
162
[533]163 if (!_ephStreamGPS) {
[517]164 delete gpseph;
165 return;
166 }
167
[532]168 gpsephemeris** ee = &_gpsEph[gpseph->satellite-1];
[538]169 if ( *ee == 0 ||
170 gpseph->GPSweek > (*ee)->GPSweek ||
[578]171 (gpseph->GPSweek == (*ee)->GPSweek && gpseph->TOW > (*ee)->TOW) ) {
[516]172 delete *ee;
173 *ee = gpseph;
174 printGPSEph(gpseph);
175 }
176 else {
177 delete gpseph;
178 }
[511]179}
180
[535]181// New Glonass Ephemeris
[511]182////////////////////////////////////////////////////////////////////////////
183void bncApp::slotNewGlonassEph(glonassephemeris* glonasseph) {
[516]184
185 QMutexLocker locker(&_mutex);
186
[534]187 printEphHeader();
188
[533]189 if (!_ephStreamGlonass) {
[517]190 delete glonasseph;
191 return;
192 }
193
[532]194 glonassephemeris** ee = &_glonassEph[glonasseph->almanac_number-1];
[531]195
[578]196 int wwOld, towOld, wwNew, towNew;
[577]197 if (*ee != 0) {
[578]198 wwOld = (*ee)->GPSWeek;
199 towOld = (*ee)->GPSTOW;
200 updatetime(&wwOld, &towOld, (*ee)->tb*1000, 1);
[577]201
[578]202 wwNew = glonasseph->GPSWeek;
203 towNew = glonasseph->GPSTOW;
204 updatetime(&wwNew, &towNew, glonasseph->tb*1000, 1);
[577]205 }
206
[578]207 if ( *ee == 0 ||
208 wwNew > wwOld ||
209 (wwNew == wwOld && towNew > towOld) ) {
[531]210 delete *ee;
211 *ee = glonasseph;
212 printGlonassEph(glonasseph);
213 }
214 else {
215 delete glonasseph;
216 }
[511]217}
218
[535]219// Print Header of the output File(s)
[516]220////////////////////////////////////////////////////////////////////////////
221void bncApp::printEphHeader() {
[528]222
[535]223 QSettings settings;
224
[534]225 // Initialization
226 // --------------
227 if (_rinexVers == 0) {
[528]228
[533]229 if ( Qt::CheckState(settings.value("ephV3").toInt()) == Qt::Checked) {
[534]230 _rinexVers = 3;
[533]231 }
232 else {
[534]233 _rinexVers = 2;
[533]234 }
[529]235
[533]236 _ephPath = settings.value("ephPath").toString();
237
238 if ( !_ephPath.isEmpty() ) {
239 if ( _ephPath[_ephPath.length()-1] != QDir::separator() ) {
240 _ephPath += QDir::separator();
241 }
242 expandEnvVar(_ephPath);
243 }
[517]244 }
[533]245
[534]246 // (Re-)Open output File(s)
247 // ------------------------
[533]248 if (!_ephPath.isEmpty()) {
249
[566]250 QDateTime datTim = QDateTime::currentDateTime().toUTC();
[533]251
[583]252 QString ephFileNameGPS = _ephPath + "BRDC" +
[563]253 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'));
[533]254
[563]255 QString hlpStr;
256 QString intStr = settings.value("ephIntr").toString();
257 if (intStr == "1 day") {
258 hlpStr = "0";
259 }
260 else if (intStr == "1 hour") {
261 char ch = 'A' + datTim.time().hour();
262 hlpStr = ch;
263 }
[584]264 else if (intStr == "15 min") {
[563]265 char ch = 'A' + datTim.time().hour();
266 hlpStr = ch;
267 if (datTim.time().minute() < 15) {
268 hlpStr += "00";
269 }
270 else if (datTim.time().minute() < 30) {
271 hlpStr += "15";
272 }
273 else if (datTim.time().minute() < 45) {
274 hlpStr += "30";
275 }
276 else {
277 hlpStr += "45";
278 }
279 }
[584]280 else {
281 char ch = 'A' + datTim.time().hour();
282 hlpStr = ch;
283 if (datTim.time().minute() < 5) {
284 hlpStr += "00";
285 }
286 else if (datTim.time().minute() < 10) {
287 hlpStr += "05";
288 }
289 else if (datTim.time().minute() < 15) {
290 hlpStr += "10";
291 }
292 else if (datTim.time().minute() < 20) {
293 hlpStr += "15";
294 }
295 else if (datTim.time().minute() < 25) {
296 hlpStr += "20";
297 }
298 else if (datTim.time().minute() < 30) {
299 hlpStr += "25";
300 }
301 else if (datTim.time().minute() < 35) {
302 hlpStr += "30";
303 }
304 else if (datTim.time().minute() < 40) {
305 hlpStr += "35";
306 }
307 else if (datTim.time().minute() < 45) {
308 hlpStr += "40";
309 }
310 else if (datTim.time().minute() < 50) {
311 hlpStr += "45";
312 }
313 else if (datTim.time().minute() < 55) {
314 hlpStr += "50";
315 }
316 else {
317 hlpStr += "55";
318 }
319 }
320
[575]321 if (_rinexVers == 3) {
322 ephFileNameGPS += hlpStr + datTim.toString(".yyP");
323 }
324 else {
325 ephFileNameGPS += hlpStr + datTim.toString(".yyN");
326 }
[563]327
[533]328 if (_ephFileNameGPS == ephFileNameGPS) {
329 return;
330 }
331 else {
332 _ephFileNameGPS = ephFileNameGPS;
333 }
334
[575]335 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
336 delete _gpsEph[ii-PRN_GPS_START];
337 _gpsEph[ii-PRN_GPS_START] = 0;
338 }
339 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
340 delete _glonassEph[ii-PRN_GLONASS_START];
341 _glonassEph[ii-PRN_GLONASS_START] = 0;
342 }
343
[533]344 delete _ephStreamGPS;
345 delete _ephFileGPS;
346
[535]347 QFlags<QIODevice::OpenModeFlag> appendFlagGPS;
[536]348 QFlags<QIODevice::OpenModeFlag> appendFlagGlonass;
349
[535]350 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
351 QFile::exists(ephFileNameGPS) ) {
352 appendFlagGPS = QIODevice::Append;
353 }
354
[533]355 _ephFileGPS = new QFile(ephFileNameGPS);
[535]356 _ephFileGPS->open(QIODevice::WriteOnly | appendFlagGPS);
[533]357 _ephStreamGPS = new QTextStream();
358 _ephStreamGPS->setDevice(_ephFileGPS);
359
[534]360 if (_rinexVers == 3) {
[533]361 _ephFileGlonass = _ephFileGPS;
362 _ephStreamGlonass = _ephStreamGPS;
363 }
[534]364 else if (_rinexVers == 2) {
[583]365 QString ephFileNameGlonass = _ephPath + "BRDC" +
[563]366 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
[575]367 hlpStr + datTim.toString(".yyG");
[533]368
369 delete _ephStreamGlonass;
370 delete _ephFileGlonass;
371
[535]372 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
373 QFile::exists(ephFileNameGlonass) ) {
374 appendFlagGlonass = QIODevice::Append;
375 }
376
[533]377 _ephFileGlonass = new QFile(ephFileNameGlonass);
[535]378 _ephFileGlonass->open(QIODevice::WriteOnly | appendFlagGlonass);
[533]379 _ephStreamGlonass = new QTextStream();
380 _ephStreamGlonass->setDevice(_ephFileGlonass);
381 }
382
[534]383 // Header - RINEX Version 3
384 // ------------------------
385 if (_rinexVers == 3) {
[537]386 if ( ! (appendFlagGPS & QIODevice::Append)) {
387 QString line;
388 line.sprintf(
389 "%9.2f%11sN: GNSS NAV DATA M: Mixed%12sRINEX VERSION / TYPE\n",
390 3.0, "", "");
391 *_ephStreamGPS << line;
392
[566]393 QString hlp = QDateTime::currentDateTime().toUTC().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
[559]394 *_ephStreamGPS << _pgmName.toAscii().data()
395 << _userName.toAscii().data()
396 << hlp.toAscii().data()
397 << "PGM / RUN BY / DATE" << endl;
398
399 line.sprintf("%60sEND OF HEADER\n", "");
[537]400 *_ephStreamGPS << line;
401
402 _ephStreamGPS->flush();
[535]403 }
[533]404 }
405
[536]406 // Headers - RINEX Version 2
407 // -------------------------
[534]408 else if (_rinexVers == 2) {
[536]409 if (! (appendFlagGPS & QIODevice::Append)) {
410 QString line;
411 line.sprintf(
[565]412 "%9.2f%11sN: GPS NAV DATA%25sRINEX VERSION / TYPE\n", 2.11, "", "");
[536]413 *_ephStreamGPS << line;
414
[566]415 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[559]416 *_ephStreamGPS << _pgmName.toAscii().data()
417 << _userName.toAscii().data()
418 << hlp.toAscii().data()
419 << "PGM / RUN BY / DATE" << endl;
420
421 line.sprintf("%60sEND OF HEADER\n", "");
[536]422 *_ephStreamGPS << line;
[537]423
424 _ephStreamGPS->flush();
[536]425 }
426 if (! (appendFlagGlonass & QIODevice::Append)) {
427 QString line;
428 line.sprintf(
[582]429 "%9.2f%11sG: GLONASS NAV DATA%21sRINEX VERSION / TYPE\n",2.11,"","");
[536]430 *_ephStreamGlonass << line;
431
[566]432 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[559]433 *_ephStreamGlonass << _pgmName.toAscii().data()
434 << _userName.toAscii().data()
435 << hlp.toAscii().data()
436 << "PGM / RUN BY / DATE" << endl;
437
438 line.sprintf("%60sEND OF HEADER\n", "");
[536]439 *_ephStreamGlonass << line;
[537]440
441 _ephStreamGlonass->flush();
[536]442 }
[533]443 }
444 }
[516]445}
446
[535]447// Print One GPS Ephemeris
[516]448////////////////////////////////////////////////////////////////////////////
449void bncApp::printGPSEph(gpsephemeris* ep) {
[519]450
[533]451 if (_ephStreamGPS) {
452
[519]453 QString line;
454
[524]455 struct converttimeinfo cti;
456 converttime(&cti, ep->GPSweek, ep->TOC);
457
[534]458 if (_rinexVers == 3) {
[526]459 line.sprintf("G%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e",
[524]460 ep->satellite, cti.year, cti.month, cti.day, cti.hour,
461 cti.minute, cti.second, ep->clock_bias, ep->clock_drift,
462 ep->clock_driftrate);
[519]463 }
[534]464 else if (_rinexVers == 2) {
[581]465 line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f%19.12e%19.12e%19.12e",
[524]466 ep->satellite, cti.year%100, cti.month, cti.day, cti.hour,
467 cti.minute, (double) cti.second, ep->clock_bias,
[519]468 ep->clock_drift, ep->clock_driftrate);
469 }
[533]470 *_ephStreamGPS << line << endl;
[520]471
[521]472 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", (double)ep->IODE,
[520]473 ep->Crs, ep->Delta_n, ep->M0);
[533]474 *_ephStreamGPS << line << endl;
[520]475
[521]476 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->Cuc,
[520]477 ep->e, ep->Cus, ep->sqrt_A);
[533]478 *_ephStreamGPS << line << endl;
[520]479
[521]480 line.sprintf(" %19.12e%19.12e%19.12e%19.12e",
[520]481 (double) ep->TOE, ep->Cic, ep->OMEGA0, ep->Cis);
[533]482 *_ephStreamGPS << line << endl;
[520]483
[521]484 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->i0,
[520]485 ep->Crc, ep->omega, ep->OMEGADOT);
[533]486 *_ephStreamGPS << line << endl;
[520]487
[519]488 double dd = 0;
489 unsigned long ii = ep->flags;
490 if(ii & GPSEPHF_L2CACODE)
491 dd += 2.0;
492 if(ii & GPSEPHF_L2PCODE)
493 dd += 1.0;
[521]494 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->IDOT, dd,
[520]495 (double) ep->GPSweek, ii & GPSEPHF_L2PCODEDATA ? 1.0 : 0.0);
[533]496 *_ephStreamGPS << line << endl;
[520]497
[519]498 if(ep->URAindex <= 6) /* URA index */
499 dd = ceil(10.0*pow(2.0, 1.0+((double)ep->URAindex)/2.0))/10.0;
500 else
501 dd = ceil(10.0*pow(2.0, ((double)ep->URAindex)/2.0))/10.0;
[521]502 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", dd,
[520]503 ((double) ep->SVhealth), ep->TGD, ((double) ep->IODC));
[533]504 *_ephStreamGPS << line << endl;
[519]505
[521]506 line.sprintf(" %19.12e%19.12e", ((double)ep->TOW), 0.0);
[533]507 *_ephStreamGPS << line << endl;
[519]508
[533]509 _ephStreamGPS->flush();
[517]510 }
[516]511}
512
[535]513// Print One Glonass Ephemeris
[516]514////////////////////////////////////////////////////////////////////////////
515void bncApp::printGlonassEph(glonassephemeris* ep) {
[523]516
[533]517 if (_ephStreamGlonass) {
[527]518 int ww = ep->GPSWeek;
519 int tow = ep->GPSTOW;
[525]520 struct converttimeinfo cti;
[523]521
[527]522 updatetime(&ww, &tow, ep->tb*1000, 1);
523 converttime(&cti, ww, tow);
[523]524
[527]525 int ii = ep->tk-3*60*60;
526 if (ii < 0) {
527 ii += 86400;
528 }
[525]529
530 QString line;
531
[534]532 if (_rinexVers == 3) {
[525]533 line.sprintf("R%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e",
534 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour,
[527]535 cti.minute, cti.second, -ep->tau, ep->gamma, (double) ii);
[525]536 }
[534]537 else if (_rinexVers == 2) {
[525]538 line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f%19.12e%19.12e%19.12e",
539 ep->almanac_number, cti.year%100, cti.month, cti.day,
540 cti.hour, cti.minute, (double) cti.second, -ep->tau,
[527]541 ep->gamma, (double) ii);
[525]542 }
[533]543 *_ephStreamGlonass << line << endl;
[525]544
545 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->x_pos,
546 ep->x_velocity, ep->x_acceleration,
547 (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
[533]548 *_ephStreamGlonass << line << endl;
[525]549
550 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->y_pos,
551 ep->y_velocity, ep->y_acceleration,
552 (double) ep->frequency_number);
[533]553 *_ephStreamGlonass << line << endl;
[525]554
555 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->z_pos,
556 ep->z_velocity, ep->z_acceleration, (double) ep->E);
[533]557 *_ephStreamGlonass << line << endl;
[525]558
[533]559 _ephStreamGlonass->flush();
[517]560 }
[516]561}
Note: See TracBrowser for help on using the repository browser.