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

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

* empty log message *

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