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

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

* empty log message *

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