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

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

* empty log message *

File size: 14.1 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 QDate date = QDate::currentDate();
241
242 QString hlp = (_rinexVers == 3) ? "MIX_" : "GPS_";
243 QString ephFileNameGPS = _ephPath + hlp +
244 QString("%1").arg(date.dayOfYear(), 3, 10, QChar('0')) +
245 date.toString("0.yyN");
246
247 if (_ephFileNameGPS == ephFileNameGPS) {
248 return;
249 }
250 else {
251 _ephFileNameGPS = ephFileNameGPS;
252 }
253
254 delete _ephStreamGPS;
255 delete _ephFileGPS;
256
257 QFlags<QIODevice::OpenModeFlag> appendFlagGPS;
258 QFlags<QIODevice::OpenModeFlag> appendFlagGlonass;
259
260 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
261 QFile::exists(ephFileNameGPS) ) {
262 appendFlagGPS = QIODevice::Append;
263 }
264
265 _ephFileGPS = new QFile(ephFileNameGPS);
266 _ephFileGPS->open(QIODevice::WriteOnly | appendFlagGPS);
267 _ephStreamGPS = new QTextStream();
268 _ephStreamGPS->setDevice(_ephFileGPS);
269
270 if (_rinexVers == 3) {
271 _ephFileGlonass = _ephFileGPS;
272 _ephStreamGlonass = _ephStreamGPS;
273 }
274 else if (_rinexVers == 2) {
275 QString ephFileNameGlonass = _ephPath + "GLO_" +
276 QString("%1").arg(date.dayOfYear(), 3, 10, QChar('0')) +
277 date.toString("0.yyN");
278
279 delete _ephStreamGlonass;
280 delete _ephFileGlonass;
281
282 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
283 QFile::exists(ephFileNameGlonass) ) {
284 appendFlagGlonass = QIODevice::Append;
285 }
286
287 _ephFileGlonass = new QFile(ephFileNameGlonass);
288 _ephFileGlonass->open(QIODevice::WriteOnly | appendFlagGlonass);
289 _ephStreamGlonass = new QTextStream();
290 _ephStreamGlonass->setDevice(_ephFileGlonass);
291 }
292
293 // Header - RINEX Version 3
294 // ------------------------
295 if (_rinexVers == 3) {
296 if ( ! (appendFlagGPS & QIODevice::Append)) {
297 QString line;
298 line.sprintf(
299 "%9.2f%11sN: GNSS NAV DATA M: Mixed%12sRINEX VERSION / TYPE\n",
300 3.0, "", "");
301 *_ephStreamGPS << line;
302
303 QString hlp = QDateTime::currentDateTime().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
304 *_ephStreamGPS << _pgmName.toAscii().data()
305 << _userName.toAscii().data()
306 << hlp.toAscii().data()
307 << "PGM / RUN BY / DATE" << endl;
308
309 line.sprintf("%60sEND OF HEADER\n", "");
310 *_ephStreamGPS << line;
311
312 _ephStreamGPS->flush();
313 }
314 }
315
316 // Headers - RINEX Version 2
317 // -------------------------
318 else if (_rinexVers == 2) {
319 if (! (appendFlagGPS & QIODevice::Append)) {
320 QString line;
321 line.sprintf(
322 "%9.2f%11sN: GPS NAV DATA%25sRINEX VERSION / TYPE\n", 2.1, "", "");
323 *_ephStreamGPS << line;
324
325 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
326 *_ephStreamGPS << _pgmName.toAscii().data()
327 << _userName.toAscii().data()
328 << hlp.toAscii().data()
329 << "PGM / RUN BY / DATE" << endl;
330
331 line.sprintf("%60sEND OF HEADER\n", "");
332 *_ephStreamGPS << line;
333
334 _ephStreamGPS->flush();
335 }
336 if (! (appendFlagGlonass & QIODevice::Append)) {
337 QString line;
338 line.sprintf(
339 "%9.2f%11sG: GLONASS NAV DATA%21sRINEX VERSION / TYPE\n",2.1,"","");
340 *_ephStreamGlonass << line;
341
342 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
343 *_ephStreamGlonass << _pgmName.toAscii().data()
344 << _userName.toAscii().data()
345 << hlp.toAscii().data()
346 << "PGM / RUN BY / DATE" << endl;
347
348 line.sprintf("%60sEND OF HEADER\n", "");
349 *_ephStreamGlonass << line;
350
351 _ephStreamGlonass->flush();
352 }
353 }
354 }
355}
356
357// Print One GPS Ephemeris
358////////////////////////////////////////////////////////////////////////////
359void bncApp::printGPSEph(gpsephemeris* ep) {
360
361 if (_ephStreamGPS) {
362
363 QString line;
364
365 struct converttimeinfo cti;
366 converttime(&cti, ep->GPSweek, ep->TOC);
367
368 if (_rinexVers == 3) {
369 line.sprintf("G%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e",
370 ep->satellite, cti.year, cti.month, cti.day, cti.hour,
371 cti.minute, cti.second, ep->clock_bias, ep->clock_drift,
372 ep->clock_driftrate);
373 }
374 else if (_rinexVers == 2) {
375 line.sprintf("%02d %02d %02d %02d %02d %02d%05.1f%19.12e%19.12e%19.12e",
376 ep->satellite, cti.year%100, cti.month, cti.day, cti.hour,
377 cti.minute, (double) cti.second, ep->clock_bias,
378 ep->clock_drift, ep->clock_driftrate);
379 }
380 *_ephStreamGPS << line << endl;
381
382 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", (double)ep->IODE,
383 ep->Crs, ep->Delta_n, ep->M0);
384 *_ephStreamGPS << line << endl;
385
386 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->Cuc,
387 ep->e, ep->Cus, ep->sqrt_A);
388 *_ephStreamGPS << line << endl;
389
390 line.sprintf(" %19.12e%19.12e%19.12e%19.12e",
391 (double) ep->TOE, ep->Cic, ep->OMEGA0, ep->Cis);
392 *_ephStreamGPS << line << endl;
393
394 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->i0,
395 ep->Crc, ep->omega, ep->OMEGADOT);
396 *_ephStreamGPS << line << endl;
397
398 double dd = 0;
399 unsigned long ii = ep->flags;
400 if(ii & GPSEPHF_L2CACODE)
401 dd += 2.0;
402 if(ii & GPSEPHF_L2PCODE)
403 dd += 1.0;
404 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->IDOT, dd,
405 (double) ep->GPSweek, ii & GPSEPHF_L2PCODEDATA ? 1.0 : 0.0);
406 *_ephStreamGPS << line << endl;
407
408 if(ep->URAindex <= 6) /* URA index */
409 dd = ceil(10.0*pow(2.0, 1.0+((double)ep->URAindex)/2.0))/10.0;
410 else
411 dd = ceil(10.0*pow(2.0, ((double)ep->URAindex)/2.0))/10.0;
412 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", dd,
413 ((double) ep->SVhealth), ep->TGD, ((double) ep->IODC));
414 *_ephStreamGPS << line << endl;
415
416 line.sprintf(" %19.12e%19.12e", ((double)ep->TOW), 0.0);
417 *_ephStreamGPS << line << endl;
418
419 _ephStreamGPS->flush();
420 }
421}
422
423// Print One Glonass Ephemeris
424////////////////////////////////////////////////////////////////////////////
425void bncApp::printGlonassEph(glonassephemeris* ep) {
426
427 if (_ephStreamGlonass) {
428 int ww = ep->GPSWeek;
429 int tow = ep->GPSTOW;
430 struct converttimeinfo cti;
431
432 updatetime(&ww, &tow, ep->tb*1000, 1);
433 converttime(&cti, ww, tow);
434
435 int ii = ep->tk-3*60*60;
436 if (ii < 0) {
437 ii += 86400;
438 }
439
440 QString line;
441
442 if (_rinexVers == 3) {
443 line.sprintf("R%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e",
444 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour,
445 cti.minute, cti.second, -ep->tau, ep->gamma, (double) ii);
446 }
447 else if (_rinexVers == 2) {
448 line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f%19.12e%19.12e%19.12e",
449 ep->almanac_number, cti.year%100, cti.month, cti.day,
450 cti.hour, cti.minute, (double) cti.second, -ep->tau,
451 ep->gamma, (double) ii);
452 }
453 *_ephStreamGlonass << line << endl;
454
455 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->x_pos,
456 ep->x_velocity, ep->x_acceleration,
457 (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
458 *_ephStreamGlonass << line << endl;
459
460 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->y_pos,
461 ep->y_velocity, ep->y_acceleration,
462 (double) ep->frequency_number);
463 *_ephStreamGlonass << line << endl;
464
465 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->z_pos,
466 ep->z_velocity, ep->z_acceleration, (double) ep->E);
467 *_ephStreamGlonass << line << endl;
468
469 _ephStreamGlonass->flush();
470 }
471}
Note: See TracBrowser for help on using the repository browser.