source: ntrip/trunk/BNC/bncrinex.cpp@ 626

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

* empty log message *

File size: 19.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: bncRinex
30 *
31 * Purpose: writes RINEX files
32 *
33 * Author: L. Mervart
34 *
35 * Created: 27-Aug-2006
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <stdlib.h>
42#include <iostream>
43#include <iomanip>
44#include <math.h>
45
46#include <QtCore>
47#include <QUrl>
48#include <QString>
49
50#include "bncrinex.h"
51#include "bncapp.h"
52#include "bncutils.h"
53#include "bncconst.h"
54#include "bnctabledlg.h"
55#include "bncgetthread.h"
56#include "RTCM3/rtcm3torinex.h"
57
58using namespace std;
59
60// Constructor
61////////////////////////////////////////////////////////////////////////////
62bncRinex::bncRinex(const QByteArray& statID, const QUrl& mountPoint,
63 const QByteArray& format, const QByteArray& latitude,
64 const QByteArray& longitude, const QByteArray& nmea) {
65 _statID = statID;
66 _mountPoint = mountPoint;
67 _format = format.left(6);
68 _latitude = latitude;
69 _longitude = longitude;
70 _nmea = nmea;
71 _headerWritten = false;
72 _reconnectFlag = false;
73
74 QSettings settings;
75 _rnxScriptName = settings.value("rnxScript").toString();
76 expandEnvVar(_rnxScriptName);
77
78 _pgmName = ((bncApp*)qApp)->bncVersion().leftJustified(20, ' ', true);
79#ifdef WIN32
80 _userName = QString("${USERNAME}");
81#else
82 _userName = QString("${USER}");
83#endif
84 expandEnvVar(_userName);
85 _userName = _userName.leftJustified(20, ' ', true);
86
87 if ( Qt::CheckState(settings.value("rnxV3").toInt()) == Qt::Checked) {
88 _rinexVers = 3;
89 }
90 else {
91 _rinexVers = 2;
92 }
93}
94
95// Destructor
96////////////////////////////////////////////////////////////////////////////
97bncRinex::~bncRinex() {
98 QListIterator<p_obs> it(_obs);
99 while (it.hasNext()) {
100 delete it.next();
101 }
102 _out.close();
103}
104
105// Download Skeleton Header File
106////////////////////////////////////////////////////////////////////////////
107t_irc bncRinex::downloadSkeleton() {
108
109 t_irc irc = failure;
110
111 QStringList table;
112 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(),
113 table, false);
114 QString net;
115 QStringListIterator it(table);
116 while (it.hasNext()) {
117 QString line = it.next();
118 if (line.indexOf("STR") == 0) {
119 QStringList tags = line.split(";");
120 if (tags.at(1) == _mountPoint.path().mid(1).toAscii()) {
121 net = tags.at(7);
122 break;
123 }
124 }
125 }
126 QString sklDir;
127 it.toFront();
128 while (it.hasNext()) {
129 QString line = it.next();
130 if (line.indexOf("NET") == 0) {
131 QStringList tags = line.split(";");
132 if (tags.at(1) == net) {
133 sklDir = tags.at(6).trimmed();
134 break;
135 }
136 }
137 }
138 if (!sklDir.isEmpty() && sklDir != "none") {
139 QUrl url(sklDir + "/" + _mountPoint.path().mid(1,4).toLower() + ".skl");
140 if (url.port() == -1) {
141 url.setPort(80);
142 }
143
144 const int timeOut = 10*1000;
145 QString msg;
146 QByteArray _latitude;
147 QByteArray _longitude;
148 QByteArray _nmea;
149 QTcpSocket* socket = bncGetThread::request(url, _latitude, _longitude, _nmea, timeOut, msg);
150
151 if (socket) {
152 _headerLines.clear();
153 bool firstLineRead = false;
154 while (true) {
155 if (socket->canReadLine()) {
156 QString line = socket->readLine();
157 line.chop(1);
158 if (line.indexOf("MARKER NAME") != -1) {
159 irc = success;
160 }
161 if (line.indexOf("RINEX VERSION") != -1) {
162 if (_rinexVers == 3) {
163 _headerLines.append(" 3.00 OBSERVATION DATA"
164 " M (MIXED)"
165 " RINEX VERSION / TYPE");
166 }
167 else {
168 _headerLines.append(" 2.11 OBSERVATION DATA"
169 " M (MIXED)"
170 " RINEX VERSION / TYPE");
171 }
172 _headerLines.append("PGM / RUN BY / DATE");
173 firstLineRead = true;
174 }
175 else if (firstLineRead) {
176 if (line.indexOf("END OF HEADER") != -1) {
177 _headerLines.append("# / TYPES OF OBSERV");
178 if (_rinexVers == 2) {
179 _headerLines.append(
180 QString(" 1 1").leftJustified(60, ' ', true) +
181 "WAVELENGTH FACT L1/2");
182 }
183 _headerLines.append("TIME OF FIRST OBS");
184 _headerLines.append( line );
185 break;
186 }
187 else {
188 _headerLines.append( line );
189 }
190 }
191 }
192 else {
193 socket->waitForReadyRead(timeOut);
194 if (socket->bytesAvailable() > 0) {
195 continue;
196 }
197 else {
198 break;
199 }
200 }
201 }
202 delete socket;
203 }
204 }
205 return irc;
206}
207
208// Read Skeleton Header File
209////////////////////////////////////////////////////////////////////////////
210void bncRinex::readSkeleton() {
211
212 // Read the local file
213 // -------------------
214 QFile skl(_sklName);
215 if ( skl.exists() && skl.open(QIODevice::ReadOnly) ) {
216 _headerLines.clear();
217 QTextStream in(&skl);
218 while ( !in.atEnd() ) {
219 _headerLines.append( in.readLine() );
220 if (_headerLines.last().indexOf("END OF HEADER") != -1) {
221 break;
222 }
223 }
224 }
225
226 // Read downloaded file
227 // --------------------
228 else {
229 QDate currDate = QDateTime::currentDateTime().toUTC().date();
230 if ( !_skeletonDate.isValid() || _skeletonDate != currDate ) {
231 if ( downloadSkeleton() == success) {
232 _skeletonDate = currDate;
233 }
234 }
235 }
236}
237
238// File Name according to RINEX Standards
239////////////////////////////////////////////////////////////////////////////
240void bncRinex::resolveFileName(const QDateTime& datTim) {
241
242 QSettings settings;
243 QString path = settings.value("rnxPath").toString();
244 expandEnvVar(path);
245
246 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
247 path += QDir::separator();
248 }
249
250 QString intStr = settings.value("rnxIntr").toString();
251 QString hlpStr;
252
253 QTime nextTime;
254 QDate nextDate;
255
256 int indHlp = intStr.indexOf("min");
257
258 if ( indHlp != -1) {
259 int step = intStr.left(indHlp-1).toInt();
260 char ch = 'A' + datTim.time().hour();
261 hlpStr = ch;
262 if (datTim.time().minute() >= 60-step) {
263 hlpStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
264 if (datTim.time().hour() < 23) {
265 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
266 nextDate = datTim.date();
267 }
268 else {
269 nextTime.setHMS(0, 0, 0);
270 nextDate = datTim.date().addDays(1);
271 }
272 }
273 else {
274 for (int limit = step; limit <= 60-step; limit += step) {
275 if (datTim.time().minute() < limit) {
276 hlpStr += QString("%1").arg(limit-step, 2, 10, QChar('0'));
277 nextTime.setHMS(datTim.time().hour(), limit, 0);
278 nextDate = datTim.date();
279 break;
280 }
281 }
282 }
283 }
284 else if (intStr == "1 hour") {
285 char ch = 'A' + datTim.time().hour();
286 hlpStr = ch;
287 if (datTim.time().hour() < 23) {
288 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
289 nextDate = datTim.date();
290 }
291 else {
292 nextTime.setHMS(0, 0, 0);
293 nextDate = datTim.date().addDays(1);
294 }
295 }
296 else {
297 hlpStr = "0";
298 nextTime.setHMS(0, 0, 0);
299 nextDate = datTim.date().addDays(1);
300 }
301 _nextCloseEpoch = QDateTime(nextDate, nextTime);
302
303 QString ID4 = _statID.left(4);
304
305 // Check name conflict
306 // -------------------
307 QString distStr;
308 int num = 0;
309 QListIterator<QString> it(settings.value("mountPoints").toStringList());
310 while (it.hasNext()) {
311 QString mp = it.next();
312 if (mp.indexOf(ID4) != -1) {
313 ++num;
314 }
315 }
316 if (num > 1) {
317 distStr = "_" + _statID.mid(4);
318 }
319
320 QString sklExt = settings.value("rnxSkel").toString();
321 if (!sklExt.isEmpty()) {
322 _sklName = path + ID4 + distStr + "." + sklExt;
323 }
324
325 path += ID4 +
326 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
327 hlpStr + distStr + datTim.toString(".yyO");
328
329 _fName = path.toAscii();
330}
331
332// Write RINEX Header
333////////////////////////////////////////////////////////////////////////////
334void bncRinex::writeHeader(const QDateTime& datTim,
335 const QDateTime& datTimNom) {
336
337 QSettings settings;
338
339 // Open the Output File
340 // --------------------
341 resolveFileName(datTimNom);
342
343 // Append to existing file and return
344 // ----------------------------------
345 if ( QFile::exists(_fName) ) {
346 if (_reconnectFlag ||
347 Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
348 _out.open(_fName.data(), ios::app);
349 _out.setf(ios::showpoint | ios::fixed);
350 _headerWritten = true;
351 _reconnectFlag = false;
352 return;
353 }
354 }
355
356 _out.open(_fName.data());
357 _out.setf(ios::showpoint | ios::fixed);
358
359 // Copy Skeleton Header
360 // --------------------
361 readSkeleton();
362 if (_headerLines.size() > 0) {
363 QStringListIterator it(_headerLines);
364 while (it.hasNext()) {
365 QString line = it.next();
366 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
367 if (_rinexVers == 3) {
368 QString hlp = QDateTime::currentDateTime().toUTC().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
369 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
370 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
371 }
372 else {
373 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
374 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
375 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
376 }
377 }
378 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
379 if (_rinexVers == 3) {
380 _out << "G 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
381 _out << "R 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
382 _out << "S 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
383 }
384 else {
385 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2"
386 " # / TYPES OF OBSERV" << endl;
387 }
388 }
389 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
390 _out << datTim.toString(" yyyy MM dd"
391 " hh mm ss.zzz0000").toAscii().data();
392 _out << " GPS TIME OF FIRST OBS" << endl;
393 QString hlp = (_format + QString(" %1").arg(_mountPoint.host() +
394 _mountPoint.path())).leftJustified(60, ' ', true);
395 _out << hlp.toAscii().data() << "COMMENT" << endl;
396 }
397 else {
398 _out << line.toAscii().data() << endl;
399 }
400 }
401 }
402
403 // Write Dummy Header
404 // ------------------
405 else {
406 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
407 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
408
409 if (_rinexVers == 3) {
410 _out << " 3.00 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
411 QString hlp = QDateTime::currentDateTime().toUTC().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
412 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
413 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
414 }
415 else {
416 _out << " 2.11 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
417 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
418 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
419 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
420 }
421 _out.setf(ios::left);
422 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
423 _out << setw(60) << "unknown unknown" << "OBSERVER / AGENCY" << endl;
424 _out << setw(20) << "unknown"
425 << setw(20) << "unknown"
426 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
427 _out << setw(20) << "unknown"
428 << setw(20) << "unknown"
429 << setw(20) << " " << "ANT # / TYPE" << endl;
430 _out.unsetf(ios::left);
431 _out << setw(14) << setprecision(4) << approxPos[0]
432 << setw(14) << setprecision(4) << approxPos[1]
433 << setw(14) << setprecision(4) << approxPos[2]
434 << " " << "APPROX POSITION XYZ" << endl;
435 _out << setw(14) << setprecision(4) << antennaNEU[0]
436 << setw(14) << setprecision(4) << antennaNEU[1]
437 << setw(14) << setprecision(4) << antennaNEU[2]
438 << " " << "ANTENNA: DELTA H/E/N" << endl;
439 if (_rinexVers == 3) {
440 _out << "G 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
441 _out << "R 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
442 _out << "S 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
443
444 }
445 else {
446 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
447 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2 # / TYPES OF OBSERV" << endl;
448 }
449 _out << datTim.toString(" yyyy MM dd"
450 " hh mm ss.zzz0000").toAscii().data();
451 _out << " GPS TIME OF FIRST OBS" << endl;
452 QString hlp = (_format + QString(" %1").arg(_mountPoint.host() +
453 _mountPoint.path())).leftJustified(60, ' ', true);
454 _out << hlp.toAscii().data() << "COMMENT" << endl;
455
456 if (_nmea == "yes") {
457 hlp = ("NMEA LAT=" + _latitude + " " + "LONG=" + _longitude).leftJustified(60, ' ',true);
458 _out << hlp.toAscii().data() << "COMMENT" << endl; }
459
460 _out << " END OF HEADER" << endl;
461 }
462
463 _headerWritten = true;
464}
465
466// Stores Observation into Internal Array
467////////////////////////////////////////////////////////////////////////////
468void bncRinex::deepCopy(const p_obs obs) {
469 p_obs newObs = new t_obs();
470 memcpy(&newObs->_o, &obs->_o, sizeof(t_obsInternal));
471 _obs.push_back(newObs);
472}
473
474// Write One Epoch into the RINEX File
475////////////////////////////////////////////////////////////////////////////
476void bncRinex::dumpEpoch(long maxTime) {
477
478 // Select observations older than maxTime
479 // --------------------------------------
480 QList<p_obs> dumpList;
481 QMutableListIterator<p_obs> mIt(_obs);
482 while (mIt.hasNext()) {
483 p_obs obs = mIt.next();
484 if (obs->_o.GPSWeek * 7*24*3600 + obs->_o.GPSWeeks < maxTime - 0.05) {
485 dumpList.push_back(obs);
486 mIt.remove();
487 }
488 }
489
490 // Easy Return
491 // -----------
492 if (dumpList.isEmpty()) {
493 return;
494 }
495
496 // Time of Epoch
497 // -------------
498 p_obs fObs = *dumpList.begin();
499 QDateTime datTim = dateAndTimeFromGPSweek(fObs->_o.GPSWeek, fObs->_o.GPSWeeks);
500 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs->_o.GPSWeek,
501 floor(fObs->_o.GPSWeeks+0.5));
502
503 // Close the file
504 // --------------
505 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
506 closeFile();
507 _headerWritten = false;
508 }
509
510 // Write RINEX Header
511 // ------------------
512 if (!_headerWritten) {
513 writeHeader(datTim, datTimNom);
514 }
515
516 double sec = double(datTim.time().second()) + fmod(fObs->_o.GPSWeeks,1.0);
517
518 // RINEX Version 3
519 // ---------------
520 if (_rinexVers == 3) {
521 _out << datTim.toString("> yyyy MM dd hh mm ").toAscii().data()
522 << setw(10) << setprecision(7) << sec
523 << " " << 0 << setw(3) << dumpList.size() << endl;
524
525 QListIterator<p_obs> it(dumpList);
526 while (it.hasNext()) {
527 p_obs obs = it.next();
528 _out << obs->_o.satSys
529 << setw(2) << setfill('0') << obs->_o.satNum << setfill(' ')
530 << setw(14) << setprecision(3) << obs->_o.C1 << " "
531 << setw(14) << setprecision(3) << obs->_o.L1 << " "
532 << setw(1) << obs->_o.SNR1
533 << setw(14) << setprecision(3) << obs->_o.S1 << " "
534 << setw(14) << setprecision(3) << obs->_o.P2 << " "
535 << setw(14) << setprecision(3) << obs->_o.L2 << " "
536 << setw(1) << obs->_o.SNR2
537 << setw(14) << setprecision(3) << obs->_o.S2
538 << endl;
539 delete obs;
540 }
541 }
542
543 // RINEX Version 2
544 // ---------------
545 else {
546 _out << datTim.toString(" yy MM dd hh mm ").toAscii().data()
547 << setw(10) << setprecision(7) << sec
548 << " " << 0 << setw(3) << dumpList.size();
549
550 QListIterator<p_obs> it(dumpList); int iSat = 0;
551 while (it.hasNext()) {
552 iSat++;
553 p_obs obs = it.next();
554 _out << obs->_o.satSys << setw(2) << obs->_o.satNum;
555 if (iSat == 12 && it.hasNext()) {
556 _out << endl << " ";
557 iSat = 0;
558 }
559 }
560 _out << endl;
561
562 it.toFront();
563 while (it.hasNext()) {
564 p_obs obs = it.next();
565
566 char lli = ' ';
567 char snr = ' ';
568 _out << setw(14) << setprecision(3) << obs->_o.C1 << lli << snr;
569 _out << setw(14) << setprecision(3) << obs->_o.C2 << lli << snr;
570 _out << setw(14) << setprecision(3) << obs->_o.P1 << lli << snr;
571 _out << setw(14) << setprecision(3) << obs->_o.P2 << lli << snr;
572 _out << setw(14) << setprecision(3) << obs->_o.L1 << lli
573 << setw(1) << obs->_o.SNR1 << endl;
574 _out << setw(14) << setprecision(3) << obs->_o.L2 << lli
575 << setw(1) << obs->_o.SNR2;
576 _out << setw(14) << setprecision(3) << obs->_o.S1 ;
577 _out << setw(16) << setprecision(3) << obs->_o.S2 ;
578 _out << endl;
579
580 delete obs;
581 }
582 }
583
584 _out.flush();
585}
586
587// Close the Old RINEX File
588////////////////////////////////////////////////////////////////////////////
589void bncRinex::closeFile() {
590 QMutexLocker locker(&_mutex);
591 _out.close();
592 if (!_rnxScriptName.isEmpty()) {
593
594#ifdef WIN32
595 QProcess::startDetached(_rnxScriptName, QStringList() << _fName) ;
596#else
597 QProcess::startDetached("nohup", QStringList() << _rnxScriptName << _fName) ;
598#endif
599
600 }
601}
Note: See TracBrowser for help on using the repository browser.