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

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

* empty log message *

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