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

Last change on this file since 566 was 566, checked in by mervart, 17 years ago

* empty log message *

File size: 14.8 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 ||
171 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
[538]196 if ( *ee == 0 ||
197 glonasseph->GPSWeek > (*ee)->GPSWeek ||
198 glonasseph->GPSTOW > (*ee)->GPSTOW ) {
[531]199 delete *ee;
200 *ee = glonasseph;
201 printGlonassEph(glonasseph);
202 }
203 else {
204 delete glonasseph;
205 }
[511]206}
207
[535]208// Print Header of the output File(s)
[516]209////////////////////////////////////////////////////////////////////////////
210void bncApp::printEphHeader() {
[528]211
[535]212 QSettings settings;
213
[534]214 // Initialization
215 // --------------
216 if (_rinexVers == 0) {
[528]217
[533]218 if ( Qt::CheckState(settings.value("ephV3").toInt()) == Qt::Checked) {
[534]219 _rinexVers = 3;
[533]220 }
221 else {
[534]222 _rinexVers = 2;
[533]223 }
[529]224
[533]225 _ephPath = settings.value("ephPath").toString();
226
227 if ( !_ephPath.isEmpty() ) {
228 if ( _ephPath[_ephPath.length()-1] != QDir::separator() ) {
229 _ephPath += QDir::separator();
230 }
231 expandEnvVar(_ephPath);
232 }
[517]233 }
[533]234
[534]235 // (Re-)Open output File(s)
236 // ------------------------
[533]237 if (!_ephPath.isEmpty()) {
238
[566]239 QDateTime datTim = QDateTime::currentDateTime().toUTC();
[533]240
[535]241 QString hlp = (_rinexVers == 3) ? "MIX_" : "GPS_";
[563]242 QString ephFileNameGPS = _ephPath + hlp +
243 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'));
[533]244
[563]245 QString hlpStr;
246 QString intStr = settings.value("ephIntr").toString();
247 if (intStr == "1 day") {
248 hlpStr = "0";
249 }
250 else if (intStr == "1 hour") {
251 char ch = 'A' + datTim.time().hour();
252 hlpStr = ch;
253 }
254 else {
255 char ch = 'A' + datTim.time().hour();
256 hlpStr = ch;
257 if (datTim.time().minute() < 15) {
258 hlpStr += "00";
259 }
260 else if (datTim.time().minute() < 30) {
261 hlpStr += "15";
262 }
263 else if (datTim.time().minute() < 45) {
264 hlpStr += "30";
265 }
266 else {
267 hlpStr += "45";
268 }
269 }
270 ephFileNameGPS += hlpStr + datTim.toString(".yyN");
271
[533]272 if (_ephFileNameGPS == ephFileNameGPS) {
273 return;
274 }
275 else {
276 _ephFileNameGPS = ephFileNameGPS;
277 }
278
279 delete _ephStreamGPS;
280 delete _ephFileGPS;
281
[535]282 QFlags<QIODevice::OpenModeFlag> appendFlagGPS;
[536]283 QFlags<QIODevice::OpenModeFlag> appendFlagGlonass;
284
[535]285 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
286 QFile::exists(ephFileNameGPS) ) {
287 appendFlagGPS = QIODevice::Append;
288 }
289
[533]290 _ephFileGPS = new QFile(ephFileNameGPS);
[535]291 _ephFileGPS->open(QIODevice::WriteOnly | appendFlagGPS);
[533]292 _ephStreamGPS = new QTextStream();
293 _ephStreamGPS->setDevice(_ephFileGPS);
294
[534]295 if (_rinexVers == 3) {
[533]296 _ephFileGlonass = _ephFileGPS;
297 _ephStreamGlonass = _ephStreamGPS;
298 }
[534]299 else if (_rinexVers == 2) {
[533]300 QString ephFileNameGlonass = _ephPath + "GLO_" +
[563]301 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
302 hlpStr + datTim.toString(".yyN");
[533]303
304 delete _ephStreamGlonass;
305 delete _ephFileGlonass;
306
[535]307 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
308 QFile::exists(ephFileNameGlonass) ) {
309 appendFlagGlonass = QIODevice::Append;
310 }
311
[533]312 _ephFileGlonass = new QFile(ephFileNameGlonass);
[535]313 _ephFileGlonass->open(QIODevice::WriteOnly | appendFlagGlonass);
[533]314 _ephStreamGlonass = new QTextStream();
315 _ephStreamGlonass->setDevice(_ephFileGlonass);
316 }
317
[534]318 // Header - RINEX Version 3
319 // ------------------------
320 if (_rinexVers == 3) {
[537]321 if ( ! (appendFlagGPS & QIODevice::Append)) {
322 QString line;
323 line.sprintf(
324 "%9.2f%11sN: GNSS NAV DATA M: Mixed%12sRINEX VERSION / TYPE\n",
325 3.0, "", "");
326 *_ephStreamGPS << line;
327
[566]328 QString hlp = QDateTime::currentDateTime().toUTC().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
[559]329 *_ephStreamGPS << _pgmName.toAscii().data()
330 << _userName.toAscii().data()
331 << hlp.toAscii().data()
332 << "PGM / RUN BY / DATE" << endl;
333
334 line.sprintf("%60sEND OF HEADER\n", "");
[537]335 *_ephStreamGPS << line;
336
337 _ephStreamGPS->flush();
[535]338 }
[533]339 }
340
[536]341 // Headers - RINEX Version 2
342 // -------------------------
[534]343 else if (_rinexVers == 2) {
[536]344 if (! (appendFlagGPS & QIODevice::Append)) {
345 QString line;
346 line.sprintf(
[565]347 "%9.2f%11sN: GPS NAV DATA%25sRINEX VERSION / TYPE\n", 2.11, "", "");
[536]348 *_ephStreamGPS << line;
349
[566]350 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[559]351 *_ephStreamGPS << _pgmName.toAscii().data()
352 << _userName.toAscii().data()
353 << hlp.toAscii().data()
354 << "PGM / RUN BY / DATE" << endl;
355
356 line.sprintf("%60sEND OF HEADER\n", "");
[536]357 *_ephStreamGPS << line;
[537]358
359 _ephStreamGPS->flush();
[536]360 }
361 if (! (appendFlagGlonass & QIODevice::Append)) {
362 QString line;
363 line.sprintf(
364 "%9.2f%11sG: GLONASS NAV DATA%21sRINEX VERSION / TYPE\n",2.1,"","");
365 *_ephStreamGlonass << line;
366
[566]367 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[559]368 *_ephStreamGlonass << _pgmName.toAscii().data()
369 << _userName.toAscii().data()
370 << hlp.toAscii().data()
371 << "PGM / RUN BY / DATE" << endl;
372
373 line.sprintf("%60sEND OF HEADER\n", "");
[536]374 *_ephStreamGlonass << line;
[537]375
376 _ephStreamGlonass->flush();
[536]377 }
[533]378 }
379 }
[516]380}
381
[535]382// Print One GPS Ephemeris
[516]383////////////////////////////////////////////////////////////////////////////
384void bncApp::printGPSEph(gpsephemeris* ep) {
[519]385
[533]386 if (_ephStreamGPS) {
387
[519]388 QString line;
389
[524]390 struct converttimeinfo cti;
391 converttime(&cti, ep->GPSweek, ep->TOC);
392
[534]393 if (_rinexVers == 3) {
[526]394 line.sprintf("G%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e",
[524]395 ep->satellite, cti.year, cti.month, cti.day, cti.hour,
396 cti.minute, cti.second, ep->clock_bias, ep->clock_drift,
397 ep->clock_driftrate);
[519]398 }
[534]399 else if (_rinexVers == 2) {
[526]400 line.sprintf("%02d %02d %02d %02d %02d %02d%05.1f%19.12e%19.12e%19.12e",
[524]401 ep->satellite, cti.year%100, cti.month, cti.day, cti.hour,
402 cti.minute, (double) cti.second, ep->clock_bias,
[519]403 ep->clock_drift, ep->clock_driftrate);
404 }
[533]405 *_ephStreamGPS << line << endl;
[520]406
[521]407 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", (double)ep->IODE,
[520]408 ep->Crs, ep->Delta_n, ep->M0);
[533]409 *_ephStreamGPS << line << endl;
[520]410
[521]411 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->Cuc,
[520]412 ep->e, ep->Cus, ep->sqrt_A);
[533]413 *_ephStreamGPS << line << endl;
[520]414
[521]415 line.sprintf(" %19.12e%19.12e%19.12e%19.12e",
[520]416 (double) ep->TOE, ep->Cic, ep->OMEGA0, ep->Cis);
[533]417 *_ephStreamGPS << line << endl;
[520]418
[521]419 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->i0,
[520]420 ep->Crc, ep->omega, ep->OMEGADOT);
[533]421 *_ephStreamGPS << line << endl;
[520]422
[519]423 double dd = 0;
424 unsigned long ii = ep->flags;
425 if(ii & GPSEPHF_L2CACODE)
426 dd += 2.0;
427 if(ii & GPSEPHF_L2PCODE)
428 dd += 1.0;
[521]429 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->IDOT, dd,
[520]430 (double) ep->GPSweek, ii & GPSEPHF_L2PCODEDATA ? 1.0 : 0.0);
[533]431 *_ephStreamGPS << line << endl;
[520]432
[519]433 if(ep->URAindex <= 6) /* URA index */
434 dd = ceil(10.0*pow(2.0, 1.0+((double)ep->URAindex)/2.0))/10.0;
435 else
436 dd = ceil(10.0*pow(2.0, ((double)ep->URAindex)/2.0))/10.0;
[521]437 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", dd,
[520]438 ((double) ep->SVhealth), ep->TGD, ((double) ep->IODC));
[533]439 *_ephStreamGPS << line << endl;
[519]440
[521]441 line.sprintf(" %19.12e%19.12e", ((double)ep->TOW), 0.0);
[533]442 *_ephStreamGPS << line << endl;
[519]443
[533]444 _ephStreamGPS->flush();
[517]445 }
[516]446}
447
[535]448// Print One Glonass Ephemeris
[516]449////////////////////////////////////////////////////////////////////////////
450void bncApp::printGlonassEph(glonassephemeris* ep) {
[523]451
[533]452 if (_ephStreamGlonass) {
[527]453 int ww = ep->GPSWeek;
454 int tow = ep->GPSTOW;
[525]455 struct converttimeinfo cti;
[523]456
[527]457 updatetime(&ww, &tow, ep->tb*1000, 1);
458 converttime(&cti, ww, tow);
[523]459
[527]460 int ii = ep->tk-3*60*60;
461 if (ii < 0) {
462 ii += 86400;
463 }
[525]464
465 QString line;
466
[534]467 if (_rinexVers == 3) {
[525]468 line.sprintf("R%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e",
469 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour,
[527]470 cti.minute, cti.second, -ep->tau, ep->gamma, (double) ii);
[525]471 }
[534]472 else if (_rinexVers == 2) {
[525]473 line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f%19.12e%19.12e%19.12e",
474 ep->almanac_number, cti.year%100, cti.month, cti.day,
475 cti.hour, cti.minute, (double) cti.second, -ep->tau,
[527]476 ep->gamma, (double) ii);
[525]477 }
[533]478 *_ephStreamGlonass << line << endl;
[525]479
480 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->x_pos,
481 ep->x_velocity, ep->x_acceleration,
482 (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
[533]483 *_ephStreamGlonass << line << endl;
[525]484
485 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->y_pos,
486 ep->y_velocity, ep->y_acceleration,
487 (double) ep->frequency_number);
[533]488 *_ephStreamGlonass << line << endl;
[525]489
490 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->z_pos,
491 ep->z_velocity, ep->z_acceleration, (double) ep->E);
[533]492 *_ephStreamGlonass << line << endl;
[525]493
[533]494 _ephStreamGlonass->flush();
[517]495 }
[516]496}
Note: See TracBrowser for help on using the repository browser.