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

Last change on this file since 577 was 577, checked in by mervart, 16 years ago

* empty log message *

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