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

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

* empty log message *

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