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

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

* empty log message *

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