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

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

* empty log message *

File size: 19.7 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// Next File Epoch (static)
239////////////////////////////////////////////////////////////////////////////
240QString bncRinex::nextEpochStr(const QDateTime& datTim,
241 const QString& intStr, QDateTime* nextEpoch) {
242
243 QString epoStr;
244
245 QTime nextTime;
246 QDate nextDate;
247
248 int indHlp = intStr.indexOf("min");
249
250 if ( indHlp != -1) {
251 int step = intStr.left(indHlp-1).toInt();
252 char ch = 'A' + datTim.time().hour();
253 epoStr = ch;
254 if (datTim.time().minute() >= 60-step) {
255 epoStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
256 if (datTim.time().hour() < 23) {
257 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
258 nextDate = datTim.date();
259 }
260 else {
261 nextTime.setHMS(0, 0, 0);
262 nextDate = datTim.date().addDays(1);
263 }
264 }
265 else {
266 for (int limit = step; limit <= 60-step; limit += step) {
267 if (datTim.time().minute() < limit) {
268 epoStr += QString("%1").arg(limit-step, 2, 10, QChar('0'));
269 nextTime.setHMS(datTim.time().hour(), limit, 0);
270 nextDate = datTim.date();
271 break;
272 }
273 }
274 }
275 }
276 else if (intStr == "1 hour") {
277 char ch = 'A' + datTim.time().hour();
278 epoStr = ch;
279 if (datTim.time().hour() < 23) {
280 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
281 nextDate = datTim.date();
282 }
283 else {
284 nextTime.setHMS(0, 0, 0);
285 nextDate = datTim.date().addDays(1);
286 }
287 }
288 else {
289 epoStr = "0";
290 nextTime.setHMS(0, 0, 0);
291 nextDate = datTim.date().addDays(1);
292 }
293
294 if (nextEpoch) {
295 *nextEpoch = QDateTime(nextDate, nextTime);
296 }
297
298 return epoStr;
299}
300
301// File Name according to RINEX Standards
302////////////////////////////////////////////////////////////////////////////
303void bncRinex::resolveFileName(const QDateTime& datTim) {
304
305 QSettings settings;
306 QString path = settings.value("rnxPath").toString();
307 expandEnvVar(path);
308
309 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
310 path += QDir::separator();
311 }
312
313 QString hlpStr = nextEpochStr(datTim, settings.value("rnxIntr").toString(),
314 &_nextCloseEpoch);
315
316 QString ID4 = _statID.left(4);
317
318 // Check name conflict
319 // -------------------
320 QString distStr;
321 int num = 0;
322 QListIterator<QString> it(settings.value("mountPoints").toStringList());
323 while (it.hasNext()) {
324 QString mp = it.next();
325 if (mp.indexOf(ID4) != -1) {
326 ++num;
327 }
328 }
329 if (num > 1) {
330 distStr = "_" + _statID.mid(4);
331 }
332
333 QString sklExt = settings.value("rnxSkel").toString();
334 if (!sklExt.isEmpty()) {
335 _sklName = path + ID4 + distStr + "." + sklExt;
336 }
337
338 path += ID4 +
339 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
340 hlpStr + distStr + datTim.toString(".yyO");
341
342 _fName = path.toAscii();
343}
344
345// Write RINEX Header
346////////////////////////////////////////////////////////////////////////////
347void bncRinex::writeHeader(const QDateTime& datTim,
348 const QDateTime& datTimNom) {
349
350 QSettings settings;
351
352 // Open the Output File
353 // --------------------
354 resolveFileName(datTimNom);
355
356 // Append to existing file and return
357 // ----------------------------------
358 if ( QFile::exists(_fName) ) {
359 if (_reconnectFlag ||
360 Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
361 _out.open(_fName.data(), ios::app);
362 _out.setf(ios::showpoint | ios::fixed);
363 _headerWritten = true;
364 _reconnectFlag = false;
365 return;
366 }
367 }
368
369 _out.open(_fName.data());
370 _out.setf(ios::showpoint | ios::fixed);
371
372 // Copy Skeleton Header
373 // --------------------
374 readSkeleton();
375 if (_headerLines.size() > 0) {
376 QStringListIterator it(_headerLines);
377 while (it.hasNext()) {
378 QString line = it.next();
379 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
380 if (_rinexVers == 3) {
381 QString hlp = QDateTime::currentDateTime().toUTC().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
382 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
383 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
384 }
385 else {
386 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
387 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
388 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
389 }
390 }
391 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
392 if (_rinexVers == 3) {
393 _out << "G 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
394 _out << "R 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
395 _out << "S 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
396 }
397 else {
398 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2"
399 " # / TYPES OF OBSERV" << endl;
400 }
401 }
402 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
403 _out << datTim.toString(" yyyy MM dd"
404 " hh mm ss.zzz0000").toAscii().data();
405 _out << " GPS TIME OF FIRST OBS" << endl;
406 QString hlp = (_format + QString(" %1").arg(_mountPoint.host() +
407 _mountPoint.path())).leftJustified(60, ' ', true);
408 _out << hlp.toAscii().data() << "COMMENT" << endl;
409 }
410 else {
411 _out << line.toAscii().data() << endl;
412 }
413 }
414 }
415
416 // Write Dummy Header
417 // ------------------
418 else {
419 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
420 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
421
422 if (_rinexVers == 3) {
423 _out << " 3.00 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
424 QString hlp = QDateTime::currentDateTime().toUTC().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
425 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
426 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
427 }
428 else {
429 _out << " 2.11 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
430 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
431 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
432 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
433 }
434 _out.setf(ios::left);
435 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
436 _out << setw(60) << "unknown unknown" << "OBSERVER / AGENCY" << endl;
437 _out << setw(20) << "unknown"
438 << setw(20) << "unknown"
439 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
440 _out << setw(20) << "unknown"
441 << setw(20) << "unknown"
442 << setw(20) << " " << "ANT # / TYPE" << endl;
443 _out.unsetf(ios::left);
444 _out << setw(14) << setprecision(4) << approxPos[0]
445 << setw(14) << setprecision(4) << approxPos[1]
446 << setw(14) << setprecision(4) << approxPos[2]
447 << " " << "APPROX POSITION XYZ" << endl;
448 _out << setw(14) << setprecision(4) << antennaNEU[0]
449 << setw(14) << setprecision(4) << antennaNEU[1]
450 << setw(14) << setprecision(4) << antennaNEU[2]
451 << " " << "ANTENNA: DELTA H/E/N" << endl;
452 if (_rinexVers == 3) {
453 _out << "G 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
454 _out << "R 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
455 _out << "S 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
456
457 }
458 else {
459 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
460 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2 # / TYPES OF OBSERV" << endl;
461 }
462 _out << datTim.toString(" yyyy MM dd"
463 " hh mm ss.zzz0000").toAscii().data();
464 _out << " GPS TIME OF FIRST OBS" << endl;
465 QString hlp = (_format + QString(" %1").arg(_mountPoint.host() +
466 _mountPoint.path())).leftJustified(60, ' ', true);
467 _out << hlp.toAscii().data() << "COMMENT" << endl;
468
469 if (_nmea == "yes") {
470 hlp = ("NMEA LAT=" + _latitude + " " + "LONG=" + _longitude).leftJustified(60, ' ',true);
471 _out << hlp.toAscii().data() << "COMMENT" << endl; }
472
473 _out << " END OF HEADER" << endl;
474 }
475
476 _headerWritten = true;
477}
478
479// Stores Observation into Internal Array
480////////////////////////////////////////////////////////////////////////////
481void bncRinex::deepCopy(const p_obs obs) {
482 p_obs newObs = new t_obs();
483 memcpy(&newObs->_o, &obs->_o, sizeof(t_obsInternal));
484 _obs.push_back(newObs);
485}
486
487// Write One Epoch into the RINEX File
488////////////////////////////////////////////////////////////////////////////
489void bncRinex::dumpEpoch(long maxTime) {
490
491 // Select observations older than maxTime
492 // --------------------------------------
493 QList<p_obs> dumpList;
494 QMutableListIterator<p_obs> mIt(_obs);
495 while (mIt.hasNext()) {
496 p_obs obs = mIt.next();
497 if (obs->_o.GPSWeek * 7*24*3600 + obs->_o.GPSWeeks < maxTime - 0.05) {
498 dumpList.push_back(obs);
499 mIt.remove();
500 }
501 }
502
503 // Easy Return
504 // -----------
505 if (dumpList.isEmpty()) {
506 return;
507 }
508
509 // Time of Epoch
510 // -------------
511 p_obs fObs = *dumpList.begin();
512 QDateTime datTim = dateAndTimeFromGPSweek(fObs->_o.GPSWeek, fObs->_o.GPSWeeks);
513 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs->_o.GPSWeek,
514 floor(fObs->_o.GPSWeeks+0.5));
515
516 // Close the file
517 // --------------
518 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
519 closeFile();
520 _headerWritten = false;
521 }
522
523 // Write RINEX Header
524 // ------------------
525 if (!_headerWritten) {
526 writeHeader(datTim, datTimNom);
527 }
528
529 double sec = double(datTim.time().second()) + fmod(fObs->_o.GPSWeeks,1.0);
530
531 // RINEX Version 3
532 // ---------------
533 if (_rinexVers == 3) {
534 _out << datTim.toString("> yyyy MM dd hh mm ").toAscii().data()
535 << setw(10) << setprecision(7) << sec
536 << " " << 0 << setw(3) << dumpList.size() << endl;
537
538 QListIterator<p_obs> it(dumpList);
539 while (it.hasNext()) {
540 p_obs obs = it.next();
541 _out << obs->_o.satSys
542 << setw(2) << setfill('0') << obs->_o.satNum << setfill(' ')
543 << setw(14) << setprecision(3) << obs->_o.C1 << " "
544 << setw(14) << setprecision(3) << obs->_o.L1 << " "
545 << setw(1) << obs->_o.SNR1
546 << setw(14) << setprecision(3) << obs->_o.S1 << " "
547 << setw(14) << setprecision(3) << obs->_o.P2 << " "
548 << setw(14) << setprecision(3) << obs->_o.L2 << " "
549 << setw(1) << obs->_o.SNR2
550 << setw(14) << setprecision(3) << obs->_o.S2
551 << endl;
552 delete obs;
553 }
554 }
555
556 // RINEX Version 2
557 // ---------------
558 else {
559 _out << datTim.toString(" yy MM dd hh mm ").toAscii().data()
560 << setw(10) << setprecision(7) << sec
561 << " " << 0 << setw(3) << dumpList.size();
562
563 QListIterator<p_obs> it(dumpList); int iSat = 0;
564 while (it.hasNext()) {
565 iSat++;
566 p_obs obs = it.next();
567 _out << obs->_o.satSys << setw(2) << obs->_o.satNum;
568 if (iSat == 12 && it.hasNext()) {
569 _out << endl << " ";
570 iSat = 0;
571 }
572 }
573 _out << endl;
574
575 it.toFront();
576 while (it.hasNext()) {
577 p_obs obs = it.next();
578
579 char lli = ' ';
580 char snr = ' ';
581 _out << setw(14) << setprecision(3) << obs->_o.C1 << lli << snr;
582 _out << setw(14) << setprecision(3) << obs->_o.C2 << lli << snr;
583 _out << setw(14) << setprecision(3) << obs->_o.P1 << lli << snr;
584 _out << setw(14) << setprecision(3) << obs->_o.P2 << lli << snr;
585 _out << setw(14) << setprecision(3) << obs->_o.L1 << lli
586 << setw(1) << obs->_o.SNR1 << endl;
587 _out << setw(14) << setprecision(3) << obs->_o.L2 << lli
588 << setw(1) << obs->_o.SNR2;
589 _out << setw(14) << setprecision(3) << obs->_o.S1 ;
590 _out << setw(16) << setprecision(3) << obs->_o.S2 ;
591 _out << endl;
592
593 delete obs;
594 }
595 }
596
597 _out.flush();
598}
599
600// Close the Old RINEX File
601////////////////////////////////////////////////////////////////////////////
602void bncRinex::closeFile() {
603 QMutexLocker locker(&_mutex);
604 _out.close();
605 if (!_rnxScriptName.isEmpty()) {
606
607#ifdef WIN32
608 QProcess::startDetached(_rnxScriptName, QStringList() << _fName) ;
609#else
610 QProcess::startDetached("nohup", QStringList() << _rnxScriptName << _fName) ;
611#endif
612
613 }
614}
Note: See TracBrowser for help on using the repository browser.