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

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

* empty log message *

File size: 13.2 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 int HandleRunBy(char *buffer, int buffersize, const char **u, int rinex3);
64}
65
66
67// Constructor
68////////////////////////////////////////////////////////////////////////////
69bncApp::bncApp(int argc, char* argv[], bool GUIenabled) :
70 QApplication(argc, argv, GUIenabled) {
71
72 _bncVersion = "BNC 1.4";
73
74 _logFileFlag = 0;
75 _logFile = 0;
76 _logStream = 0;
77
78 // Lists of Ephemeris
79 // ------------------
80 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
81 _gpsEph[ii-PRN_GPS_START] = 0;
82 }
83 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
84 _glonassEph[ii-PRN_GLONASS_START] = 0;
85 }
86
87 // Eph file(s)
88 // -----------
89 _rinexVers = 0;
90 _ephFileGPS = 0;
91 _ephStreamGPS = 0;
92 _ephFileGlonass = 0;
93 _ephStreamGlonass = 0;
94}
95
96// Destructor
97////////////////////////////////////////////////////////////////////////////
98bncApp::~bncApp() {
99 delete _logStream;
100 delete _logFile;
101 delete _ephStreamGPS;
102 delete _ephFileGPS;
103 if (_rinexVers == 2) {
104 delete _ephStreamGlonass;
105 delete _ephFileGlonass;
106 }
107 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
108 delete _gpsEph[ii-PRN_GPS_START];
109 }
110 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
111 delete _glonassEph[ii-PRN_GLONASS_START];
112 }
113}
114
115// Write a Program Message
116////////////////////////////////////////////////////////////////////////////
117void bncApp::slotMessage(const QByteArray msg) {
118
119 QMutexLocker locker(&_mutex);
120
121 // First time resolve the log file name
122 // ------------------------------------
123 if (_logFileFlag == 0) {
124 _logFileFlag = 1;
125 QSettings settings;
126 QString logFileName = settings.value("logFile").toString();
127 if ( !logFileName.isEmpty() ) {
128 expandEnvVar(logFileName);
129 _logFile = new QFile(logFileName);
130 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
131 _logFile->open(QIODevice::WriteOnly | QIODevice::Append);
132 }
133 else {
134 _logFile->open(QIODevice::WriteOnly);
135 }
136 _logStream = new QTextStream();
137 _logStream->setDevice(_logFile);
138 }
139 }
140
141 if (_logStream) {
142 *_logStream << QDate::currentDate().toString("yy-MM-dd ").toAscii().data();
143 *_logStream << QTime::currentTime().toString("hh:mm:ss ").toAscii().data();
144 *_logStream << msg.data() << endl;
145 _logStream->flush();
146 }
147}
148
149// New GPS Ephemeris
150////////////////////////////////////////////////////////////////////////////
151void bncApp::slotNewGPSEph(gpsephemeris* gpseph) {
152
153 QMutexLocker locker(&_mutex);
154
155 printEphHeader();
156
157 if (!_ephStreamGPS) {
158 delete gpseph;
159 return;
160 }
161
162 gpsephemeris** ee = &_gpsEph[gpseph->satellite-1];
163 if ( *ee == 0 || (*ee)->IODE != gpseph->IODE ) {
164 delete *ee;
165 *ee = gpseph;
166 printGPSEph(gpseph);
167 }
168 else {
169 delete gpseph;
170 }
171}
172
173// New Glonass Ephemeris
174////////////////////////////////////////////////////////////////////////////
175void bncApp::slotNewGlonassEph(glonassephemeris* glonasseph) {
176
177 QMutexLocker locker(&_mutex);
178
179 printEphHeader();
180
181 if (!_ephStreamGlonass) {
182 delete glonasseph;
183 return;
184 }
185
186 glonassephemeris** ee = &_glonassEph[glonasseph->almanac_number-1];
187
188 if ( *ee == 0 || (*ee)->GPSWeek != glonasseph->GPSWeek ||
189 (*ee)->GPSTOW != glonasseph->GPSTOW ) {
190 delete *ee;
191 *ee = glonasseph;
192 printGlonassEph(glonasseph);
193 }
194 else {
195 delete glonasseph;
196 }
197}
198
199// Print Header of the output File(s)
200////////////////////////////////////////////////////////////////////////////
201void bncApp::printEphHeader() {
202
203 QSettings settings;
204
205 // Initialization
206 // --------------
207 if (_rinexVers == 0) {
208
209 if ( Qt::CheckState(settings.value("ephV3").toInt()) == Qt::Checked) {
210 _rinexVers = 3;
211 }
212 else {
213 _rinexVers = 2;
214 }
215
216 _ephPath = settings.value("ephPath").toString();
217
218 if ( !_ephPath.isEmpty() ) {
219 if ( _ephPath[_ephPath.length()-1] != QDir::separator() ) {
220 _ephPath += QDir::separator();
221 }
222 expandEnvVar(_ephPath);
223 }
224 }
225
226 // (Re-)Open output File(s)
227 // ------------------------
228 if (!_ephPath.isEmpty()) {
229
230 QDate date = QDate::currentDate();
231
232 QString hlp = (_rinexVers == 3) ? "MIX_" : "GPS_";
233 QString ephFileNameGPS = _ephPath + hlp +
234 QString("%1").arg(date.dayOfYear(), 3, 10, QChar('0')) +
235 date.toString("0.yyN");
236
237 if (_ephFileNameGPS == ephFileNameGPS) {
238 return;
239 }
240 else {
241 _ephFileNameGPS = ephFileNameGPS;
242 }
243
244 delete _ephStreamGPS;
245 delete _ephFileGPS;
246
247 QFlags<QIODevice::OpenModeFlag> appendFlagGPS;
248 QFlags<QIODevice::OpenModeFlag> appendFlagGlonass;
249
250 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
251 QFile::exists(ephFileNameGPS) ) {
252 appendFlagGPS = QIODevice::Append;
253 }
254
255 _ephFileGPS = new QFile(ephFileNameGPS);
256 _ephFileGPS->open(QIODevice::WriteOnly | appendFlagGPS);
257 _ephStreamGPS = new QTextStream();
258 _ephStreamGPS->setDevice(_ephFileGPS);
259
260 if (_rinexVers == 3) {
261 _ephFileGlonass = _ephFileGPS;
262 _ephStreamGlonass = _ephStreamGPS;
263 }
264 else if (_rinexVers == 2) {
265 QString ephFileNameGlonass = _ephPath + "GLO_" +
266 QString("%1").arg(date.dayOfYear(), 3, 10, QChar('0')) +
267 date.toString("0.yyN");
268
269 delete _ephStreamGlonass;
270 delete _ephFileGlonass;
271
272 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
273 QFile::exists(ephFileNameGlonass) ) {
274 appendFlagGlonass = QIODevice::Append;
275 }
276
277 _ephFileGlonass = new QFile(ephFileNameGlonass);
278 _ephFileGlonass->open(QIODevice::WriteOnly | appendFlagGlonass);
279 _ephStreamGlonass = new QTextStream();
280 _ephStreamGlonass->setDevice(_ephFileGlonass);
281 }
282
283 // Header - RINEX Version 3
284 // ------------------------
285 if (_rinexVers == 3) {
286 if ( ! (appendFlagGPS & QIODevice::Append)) {
287 QString line;
288 line.sprintf(
289 "%9.2f%11sN: GNSS NAV DATA M: Mixed%12sRINEX VERSION / TYPE\n",
290 3.0, "", "");
291 *_ephStreamGPS << line;
292
293 char buffer[100];
294 HandleRunBy(buffer, sizeof(buffer), 0, 1);
295 line.sprintf("%s\n%60sEND OF HEADER\n", buffer, "");
296 *_ephStreamGPS << line;
297
298 _ephStreamGPS->flush();
299 }
300 }
301
302 // Headers - RINEX Version 2
303 // -------------------------
304 else if (_rinexVers == 2) {
305 if (! (appendFlagGPS & QIODevice::Append)) {
306 QString line;
307 line.sprintf(
308 "%9.2f%11sN: GPS NAV DATA%25sRINEX VERSION / TYPE\n", 2.1, "", "");
309 *_ephStreamGPS << line;
310
311 char buffer[100];
312 HandleRunBy(buffer, sizeof(buffer), 0, 0);
313 line.sprintf("%s\n%60sEND OF HEADER\n", buffer, "");
314 *_ephStreamGPS << line;
315
316 _ephStreamGPS->flush();
317 }
318 if (! (appendFlagGlonass & QIODevice::Append)) {
319 QString line;
320 line.sprintf(
321 "%9.2f%11sG: GLONASS NAV DATA%21sRINEX VERSION / TYPE\n",2.1,"","");
322 *_ephStreamGlonass << line;
323
324 char buffer[100];
325 HandleRunBy(buffer, sizeof(buffer), 0, 0);
326 line.sprintf("%s\n%60sEND OF HEADER\n", buffer, "");
327 *_ephStreamGlonass << line;
328
329 _ephStreamGlonass->flush();
330 }
331 }
332 }
333}
334
335// Print One GPS Ephemeris
336////////////////////////////////////////////////////////////////////////////
337void bncApp::printGPSEph(gpsephemeris* ep) {
338
339 if (_ephStreamGPS) {
340
341 QString line;
342
343 struct converttimeinfo cti;
344 converttime(&cti, ep->GPSweek, ep->TOC);
345
346 if (_rinexVers == 3) {
347 line.sprintf("G%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e",
348 ep->satellite, cti.year, cti.month, cti.day, cti.hour,
349 cti.minute, cti.second, ep->clock_bias, ep->clock_drift,
350 ep->clock_driftrate);
351 }
352 else if (_rinexVers == 2) {
353 line.sprintf("%02d %02d %02d %02d %02d %02d%05.1f%19.12e%19.12e%19.12e",
354 ep->satellite, cti.year%100, cti.month, cti.day, cti.hour,
355 cti.minute, (double) cti.second, ep->clock_bias,
356 ep->clock_drift, ep->clock_driftrate);
357 }
358 *_ephStreamGPS << line << endl;
359
360 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", (double)ep->IODE,
361 ep->Crs, ep->Delta_n, ep->M0);
362 *_ephStreamGPS << line << endl;
363
364 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->Cuc,
365 ep->e, ep->Cus, ep->sqrt_A);
366 *_ephStreamGPS << line << endl;
367
368 line.sprintf(" %19.12e%19.12e%19.12e%19.12e",
369 (double) ep->TOE, ep->Cic, ep->OMEGA0, ep->Cis);
370 *_ephStreamGPS << line << endl;
371
372 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->i0,
373 ep->Crc, ep->omega, ep->OMEGADOT);
374 *_ephStreamGPS << line << endl;
375
376 double dd = 0;
377 unsigned long ii = ep->flags;
378 if(ii & GPSEPHF_L2CACODE)
379 dd += 2.0;
380 if(ii & GPSEPHF_L2PCODE)
381 dd += 1.0;
382 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->IDOT, dd,
383 (double) ep->GPSweek, ii & GPSEPHF_L2PCODEDATA ? 1.0 : 0.0);
384 *_ephStreamGPS << line << endl;
385
386 if(ep->URAindex <= 6) /* URA index */
387 dd = ceil(10.0*pow(2.0, 1.0+((double)ep->URAindex)/2.0))/10.0;
388 else
389 dd = ceil(10.0*pow(2.0, ((double)ep->URAindex)/2.0))/10.0;
390 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", dd,
391 ((double) ep->SVhealth), ep->TGD, ((double) ep->IODC));
392 *_ephStreamGPS << line << endl;
393
394 line.sprintf(" %19.12e%19.12e", ((double)ep->TOW), 0.0);
395 *_ephStreamGPS << line << endl;
396
397 _ephStreamGPS->flush();
398 }
399}
400
401// Print One Glonass Ephemeris
402////////////////////////////////////////////////////////////////////////////
403void bncApp::printGlonassEph(glonassephemeris* ep) {
404
405 if (_ephStreamGlonass) {
406 int ww = ep->GPSWeek;
407 int tow = ep->GPSTOW;
408 struct converttimeinfo cti;
409
410 updatetime(&ww, &tow, ep->tb*1000, 1);
411 converttime(&cti, ww, tow);
412
413 int ii = ep->tk-3*60*60;
414 if (ii < 0) {
415 ii += 86400;
416 }
417
418 QString line;
419
420 if (_rinexVers == 3) {
421 line.sprintf("R%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e",
422 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour,
423 cti.minute, cti.second, -ep->tau, ep->gamma, (double) ii);
424 }
425 else if (_rinexVers == 2) {
426 line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f%19.12e%19.12e%19.12e",
427 ep->almanac_number, cti.year%100, cti.month, cti.day,
428 cti.hour, cti.minute, (double) cti.second, -ep->tau,
429 ep->gamma, (double) ii);
430 }
431 *_ephStreamGlonass << line << endl;
432
433 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->x_pos,
434 ep->x_velocity, ep->x_acceleration,
435 (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
436 *_ephStreamGlonass << line << endl;
437
438 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->y_pos,
439 ep->y_velocity, ep->y_acceleration,
440 (double) ep->frequency_number);
441 *_ephStreamGlonass << line << endl;
442
443 line.sprintf(" %19.12e%19.12e%19.12e%19.12e", ep->z_pos,
444 ep->z_velocity, ep->z_acceleration, (double) ep->E);
445 *_ephStreamGlonass << line << endl;
446
447 _ephStreamGlonass->flush();
448 }
449}
Note: See TracBrowser for help on using the repository browser.