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

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

* empty log message *

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