source: ntrip/trunk/BNC/src/bncrinex.cpp@ 9107

Last change on this file since 9107 was 9107, checked in by stuerze, 4 years ago

minor changes

File size: 19.2 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#include <sstream>
46
47#include <QtCore>
48#include <QUrl>
49#include <QString>
50
51#include "bncrinex.h"
52#include "bnccore.h"
53#include "bncutils.h"
54#include "bncconst.h"
55#include "bnctabledlg.h"
56#include "bncgetthread.h"
57#include "bncnetqueryv1.h"
58#include "bncnetqueryv2.h"
59#include "bncsettings.h"
60#include "bncversion.h"
61
62using namespace std;
63
64// Constructor
65////////////////////////////////////////////////////////////////////////////
66bncRinex::bncRinex(const QByteArray& statID, const QUrl& mountPoint,
67 const QByteArray& latitude, const QByteArray& longitude,
68 const QByteArray& nmea, const QByteArray& ntripVersion) {
69
70 _statID = statID;
71 _mountPoint = mountPoint;
72 _latitude = latitude;
73 _longitude = longitude;
74 _nmea = nmea;
75 _ntripVersion = ntripVersion;
76 _headerWritten = false;
77 _reconnectFlag = false;
78
79 bncSettings settings;
80 _rnxScriptName = settings.value("rnxScript").toString();
81 expandEnvVar(_rnxScriptName);
82
83 _pgmName = QString(BNCPGMNAME).leftJustified(20, ' ', true);
84#ifdef WIN32
85 _userName = QString("${USERNAME}");
86#else
87 _userName = QString("${USER}");
88#endif
89 expandEnvVar(_userName);
90 _userName = _userName.leftJustified(20, ' ', true);
91
92 _samplingRateMult10 = int(settings.value("rnxSampl").toString().split("sec").first().toDouble() * 10.0);
93
94 if (settings.value("rnxSampl").toString() == "0.1 sec") {
95 _samplingRateStr = "_10Z";
96 }
97 else if (settings.value("rnxSampl").toString() == "1 sec") {
98 _samplingRateStr = "_01S";
99 }
100 else if (settings.value("rnxSampl").toString() == "5 sec") {
101 _samplingRateStr = "_05S";
102 }
103 else if (settings.value("rnxSampl").toString() == "10 sec") {
104 _samplingRateStr = "_10S";
105 }
106 else if (settings.value("rnxSampl").toString() == "15 sec") {
107 _samplingRateStr = "_15S";
108 }
109 else if (settings.value("rnxSampl").toString() == "30 sec") {
110 _samplingRateStr = "_30S";
111 }
112 else if (settings.value("rnxSampl").toString() == "60 sec") {
113 _samplingRateStr = "_01M";
114 }
115
116 _writeRinexFileOnlyWithSkl = settings.value("rnxOnlyWithSKL").toBool();
117
118 _rnxV3 = (Qt::CheckState(settings.value("rnxV3").toInt()) == Qt::Checked) ? true : false;
119}
120
121// Destructor
122////////////////////////////////////////////////////////////////////////////
123bncRinex::~bncRinex() {
124 bncSettings settings;
125 if ((_header.version() >= 3.0) && ( Qt::CheckState(settings.value("rnxAppend").toInt()) != Qt::Checked) ) {
126 _out << "> 4 1" << endl;
127 _out << "END OF FILE" << endl;
128 }
129 _out.close();
130}
131
132// Download Skeleton Header File
133////////////////////////////////////////////////////////////////////////////
134t_irc bncRinex::downloadSkeleton() {
135
136 t_irc irc = failure;
137
138 QStringList table;
139 bncTableDlg::getFullTable(_ntripVersion, _mountPoint.host(), _mountPoint.port(),
140 _mountPoint.userName(), _mountPoint.password(), table, true);
141 QString net;
142 QStringListIterator it(table);
143 while (it.hasNext()) {
144 QString line = it.next();
145 if (line.indexOf("STR") == 0) {
146 QStringList tags = line.split(";");
147 if (tags.size() > 7) {
148 if (tags.at(1) == _mountPoint.path().mid(1).toLatin1()) {
149 net = tags.at(7);
150 break;
151 }
152 }
153 }
154 }
155 QString sklDir;
156 if (!net.isEmpty()) {
157 it.toFront();
158 while (it.hasNext()) {
159 QString line = it.next();
160 if (line.indexOf("NET") == 0) {
161 QStringList tags = line.split(";");
162 if (tags.size() > 6) {
163 if (tags.at(1) == net) {
164 sklDir = tags.at(6).trimmed();
165 break;
166 }
167 }
168 }
169 }
170 }
171 if (!sklDir.isEmpty() && sklDir != "none") {
172 QString staID, staIDalternative;
173 QUrl url;
174 int stIdLength = _mountPoint.path().length()-2;
175 staID = _mountPoint.path().mid(1,stIdLength).toUpper() + ".skl";
176 staIDalternative = _mountPoint.path().mid(1,stIdLength).toLower() + ".skl";
177 url = sklDir + "/" + staID;
178 if (url.port() == -1) {
179 if (sklDir.contains("https", Qt::CaseInsensitive)) {
180 url.setPort(443);
181 }
182 else {
183 url.setPort(80);
184 }
185 }
186
187 bncNetQuery* query = new bncNetQueryV2(true);
188 QByteArray outData;
189 query->waitForRequestResult(url, outData);
190 if (query->status() == bncNetQuery::finished &&
191 outData.contains("END OF HEADER")) {
192 QTextStream in(outData);
193 irc = _sklHeader.read(&in);
194 }
195 else {
196 url = sklDir + "/" + staIDalternative;
197 query->waitForRequestResult(url, outData);
198 if (query->status() == bncNetQuery::finished &&
199 outData.contains("END OF HEADER")) {
200 QTextStream in(outData);
201 irc = _sklHeader.read(&in);
202 }
203 }
204 delete query;
205 }
206
207 return irc;
208}
209
210// Read Skeleton Header File
211////////////////////////////////////////////////////////////////////////////
212bool bncRinex::readSkeleton() {
213
214 bool readDone = false;
215
216 // Read the local file
217 // -------------------
218 QFile skl(_localSklName), sklAlt(_localSklNameAlternative);
219 if ( skl.exists() && skl.open(QIODevice::ReadOnly) ) {
220 QTextStream in(&skl);
221 if (_sklHeader.read(&in) == success) {
222 readDone = true;
223 }
224 } else if ( sklAlt.exists() && sklAlt.open(QIODevice::ReadOnly) ) {
225 QTextStream in(&sklAlt);
226 if (_sklHeader.read(&in) == success) {
227 readDone = true;
228 }
229 }
230
231 // Read downloaded file
232 // --------------------
233 else if ( _ntripVersion != "N" && _ntripVersion != "UN" &&
234 _ntripVersion != "S" ) {
235 QDate currDate = currentDateAndTimeGPS().date();
236 if ( !_skeletonDate.isValid() || _skeletonDate != currDate ) {
237 if (downloadSkeleton() == success) {
238 readDone = true;
239 _skeletonDate = currDate;
240 }
241 } else if (_skeletonDate.isValid()) {
242 readDone = true;
243 }
244 }
245 return readDone;
246}
247
248// Next File Epoch (static)
249////////////////////////////////////////////////////////////////////////////
250QString bncRinex::nextEpochStr(const QDateTime& datTim,
251 const QString& intStr, bool rnxV3,
252 QDateTime* nextEpoch) {
253
254 QString epoStr = "";
255
256 QTime nextTime;
257 QDate nextDate;
258
259 int indHlp = intStr.indexOf("min");
260
261 if ( indHlp != -1) {
262 int step = intStr.left(indHlp-1).toInt();
263 if (rnxV3) {
264 epoStr += QString("%1").arg(datTim.time().hour(), 2, 10, QChar('0')); // H
265 } else {
266 epoStr += 'A' + datTim.time().hour();
267 }
268
269 if (datTim.time().minute() >= 60-step) {
270 epoStr += QString("%1").arg(60-step, 2, 10, QChar('0')); // M
271 if (datTim.time().hour() < 23) {
272 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
273 nextDate = datTim.date();
274 }
275 else {
276 nextTime.setHMS(0, 0, 0);
277 nextDate = datTim.date().addDays(1);
278 }
279 }
280 else {
281 for (int limit = step; limit <= 60-step; limit += step) {
282 if (datTim.time().minute() < limit) {
283 epoStr += QString("%1").arg(limit-step, 2, 10, QChar('0')); // M
284 nextTime.setHMS(datTim.time().hour(), limit, 0);
285 nextDate = datTim.date();
286 break;
287 }
288 }
289 }
290 if (rnxV3) {
291 epoStr += QString("_%1M").arg(step, 2, 10, QChar('0')); // period
292 }
293 }
294 else if (intStr == "1 hour") {
295 int step = intStr.left(indHlp-1).toInt();
296 if (rnxV3) {
297 epoStr += QString("%1").arg(datTim.time().hour(), 2, 10, QChar('0')); // H
298 epoStr += QString("%1").arg(0, 2, 10, QChar('0')); // M
299 epoStr += QString("_%1H").arg(step+1, 2, 10, QChar('0')); // period
300 } else {
301 epoStr += 'A' + datTim.time().hour();
302 }
303 if (datTim.time().hour() < 23) {
304 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
305 nextDate = datTim.date();
306 }
307 else {
308 nextTime.setHMS(0, 0, 0);
309 nextDate = datTim.date().addDays(1);
310 }
311 }
312 else {
313 int step = intStr.left(indHlp-1).toInt();
314 if (rnxV3) {
315 epoStr += QString("%1").arg(0, 2, 10, QChar('0')); // H
316 epoStr += QString("%1").arg(0, 2, 10, QChar('0')); // M
317 epoStr += QString("_%1D").arg(step+1, 2, 10, QChar('0')); // period
318 } else {
319 epoStr = "0";
320 }
321 nextTime.setHMS(0, 0, 0);
322 nextDate = datTim.date().addDays(1);
323 }
324
325 if (nextEpoch) {
326 *nextEpoch = QDateTime(nextDate, nextTime);
327 }
328
329 return epoStr;
330}
331
332// File Name according to RINEX Standards
333////////////////////////////////////////////////////////////////////////////
334void bncRinex::resolveFileName(const QDateTime& datTim) {
335
336 bncSettings settings;
337 QString path = settings.value("rnxPath").toString();
338 expandEnvVar(path);
339
340 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
341 path += QDir::separator();
342 }
343
344 QString hlpStr = nextEpochStr(datTim, settings.value("rnxIntr").toString(),
345 _rnxV3, &_nextCloseEpoch);
346
347 int n = _statID.size();
348 int statIDlength;
349 if (n > 9) {
350 statIDlength = 9; // rnx3
351 }
352 else if (n > 4) {
353 statIDlength = 4; // rnx2
354 }
355 else {
356 statIDlength = n;
357 }
358 QString ID = _statID.left(statIDlength);
359 ID = ID.toUpper();
360
361 // Check name conflict
362 // -------------------
363 QString distStr;
364 int num = 0;
365 QListIterator<QString> it(settings.value("mountPoints").toStringList());
366 while (it.hasNext()) {
367 QString mp = it.next();
368 if (mp.indexOf(_statID.left(statIDlength)) != -1) {
369 ++num;
370 }
371 }
372 if (num > 1) {
373 distStr = "_" + _statID.right(1);
374 }
375
376 QString sklExt = settings.value("rnxSkel").toString();
377 if (!sklExt.isEmpty()) {
378 _localSklName = path + ID + distStr + "." + sklExt;
379 _localSklNameAlternative = path + ID.toLower() + distStr + "." + sklExt;
380 }
381
382 if (_rnxV3) {
383 QString country;
384 QString monNum = "0";
385 QString recNum = "0";
386 if (statIDlength == 9) {
387 monNum = QChar(_statID[4]);
388 recNum = QChar(_statID[5]);
389 }
390 QListIterator<QString> it(settings.value("mountPoints").toStringList());
391 while (it.hasNext()) {
392 QStringList hlp = it.next().split(" ");
393 if (hlp.size() < 7)
394 continue;
395 if (hlp.join(" ").indexOf(_statID, 0) != -1) {
396 country = hlp[2].left(3);
397 }
398 }
399
400 path += ID.left(4) +
401 QString("%1").arg(monNum, 1, 10) +
402 QString("%1").arg(recNum, 1, 10) +
403 country +
404 "_S_" + // stream
405 QString("%1").arg(datTim.date().year()) +
406 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
407 hlpStr + // HMS_period
408 QString("%1").arg(_samplingRateStr) + // sampling rate
409 "_MO" + // mixed OBS
410 distStr +
411 ".rnx";
412 }
413 else {
414 path += ID.left(4) +
415 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
416 hlpStr + distStr + datTim.toString(".yyO");
417 }
418
419 _fName = path.toLatin1();
420}
421
422// Write RINEX Header
423////////////////////////////////////////////////////////////////////////////
424void bncRinex::writeHeader(const QByteArray& format, const bncTime& firstObsTime) {
425
426 bncSettings settings;
427
428 // Set RINEX Version
429 // -----------------
430 int intHeaderVers = (Qt::CheckState(settings.value("rnxV3").toInt()) == Qt::Checked ? 3 : 2);
431
432 // Open the Output File
433 // --------------------
434 QDateTime datTimNom = dateAndTimeFromGPSweek(firstObsTime.gpsw(),
435 floor(firstObsTime.gpssec()+0.5));
436
437 resolveFileName(datTimNom);
438
439 // Read Skeleton Header
440 // --------------------
441 if (readSkeleton()) {
442 _header.set(_sklHeader, intHeaderVers);
443 }
444 else {
445 if (_writeRinexFileOnlyWithSkl) {
446 return;
447 }
448 _header.setDefault(_statID, intHeaderVers);
449 }
450
451 // Append to existing file and return
452 // ----------------------------------
453 if ( QFile::exists(_fName) &&
454 (_reconnectFlag || Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) ) {
455 _out.open(_fName.data(), ios::app);
456 _out.setf(ios::showpoint | ios::fixed);
457 _headerWritten = true;
458 _reconnectFlag = false;
459 }
460 else {
461 _out.open(_fName.data());
462 _addComments.clear();
463 }
464
465 _out.setf(ios::showpoint | ios::fixed);
466
467 // A Few Additional Comments
468 // -------------------------
469 _addComments << format.left(6) + " " + _mountPoint.host() + _mountPoint.path();
470 if (_nmea == "yes") {
471 _addComments << "NMEA LAT=" + _latitude + " " + "LONG=" + _longitude;
472 }
473
474 // Write the Header
475 // ----------------
476 QByteArray headerLines;
477 QTextStream outHlp(&headerLines);
478
479 QMap<QString, QString> txtMap;
480 txtMap["COMMENT"] = _addComments.join("\\n");
481
482 _header.setStartTime(firstObsTime);
483 _header.write(&outHlp, &txtMap);
484
485 outHlp.flush();
486
487 if (!_headerWritten) {
488 _out << headerLines.data();
489 }
490
491 _headerWritten = true;
492}
493
494// Stores Observation into Internal Array
495////////////////////////////////////////////////////////////////////////////
496void bncRinex::deepCopy(t_satObs obs) {
497 _obs.push_back(obs);
498}
499
500// Write One Epoch into the RINEX File
501////////////////////////////////////////////////////////////////////////////
502void bncRinex::dumpEpoch(const QByteArray& format, const bncTime& maxTime) {
503
504 // Select observations older than maxTime
505 // --------------------------------------
506 QList<t_satObs> obsList;
507 QMutableListIterator<t_satObs> mIt(_obs);
508 while (mIt.hasNext()) {
509 t_satObs obs = mIt.next();
510 if (obs._time < maxTime) {
511 obsList.push_back(obs);
512 mIt.remove();
513 }
514 }
515
516 // Easy Return
517 // -----------
518 if (obsList.isEmpty()) {
519 return;
520 }
521
522 // Time of Epoch
523 // -------------
524 const t_satObs& fObs = obsList.first();
525 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs._time.gpsw(), fObs._time.gpssec());
526
527 // Close the file
528 // --------------
529 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
530 closeFile();
531 _headerWritten = false;
532 }
533
534 // Write RINEX Header
535 // ------------------
536 if (!_headerWritten) {
537 writeHeader(format, fObs._time);
538 }
539 if (!_headerWritten) {
540 return;
541 }
542
543 // Prepare structure t_rnxEpo
544 // --------------------------
545 t_rnxObsFile::t_rnxEpo rnxEpo;
546 rnxEpo.tt = fObs._time;
547
548 QListIterator<t_satObs> it(obsList);
549 while (it.hasNext()) {
550 const t_satObs& satObs = it.next();
551 t_rnxObsFile::t_rnxSat rnxSat;
552 rnxSat.prn = satObs._prn;
553
554 // Initialize all observations mentioned in skeleton header
555 // --------------------------------------------------------
556 char sys = rnxSat.prn.system();
557 for (int iType = 0; iType < _sklHeader.nTypes(sys); iType++) {
558 QString type = _sklHeader.obsType(sys, iType);
559 t_rnxObsFile::t_rnxObs rnxObs; // create an empty observation
560 rnxSat.obs[type] = rnxObs;
561 }
562
563 for (unsigned ii = 0; ii < satObs._obs.size(); ii++) {
564 const t_frqObs* frqObs = satObs._obs[ii];
565 if (frqObs->_codeValid) {
566 QString type = 'C' + QString(frqObs->_rnxType2ch.c_str());
567 t_rnxObsFile::t_rnxObs rnxObs;
568 rnxObs.value = frqObs->_code;
569 rnxSat.obs[type] = rnxObs;
570 }
571 if (frqObs->_phaseValid) {
572 QString type = 'L' + QString(frqObs->_rnxType2ch.c_str());
573 t_rnxObsFile::t_rnxObs rnxObs;
574 rnxObs.value = frqObs->_phase;
575 if (frqObs->_slip) {
576 rnxObs.lli |= 1;
577 }
578 rnxSat.obs[type] = rnxObs;
579 }
580 if (frqObs->_dopplerValid) {
581 QString type = 'D' + QString(frqObs->_rnxType2ch.c_str());
582 t_rnxObsFile::t_rnxObs rnxObs;
583 rnxObs.value = frqObs->_doppler;
584 rnxSat.obs[type] = rnxObs;
585 }
586 if (frqObs->_snrValid) {
587 QString type = 'S' + QString(frqObs->_rnxType2ch.c_str());
588 t_rnxObsFile::t_rnxObs rnxObs;
589 rnxObs.value = frqObs->_snr;
590 rnxSat.obs[type] = rnxObs;
591 }
592 }
593
594
595 rnxEpo.rnxSat.push_back(rnxSat);
596 }
597
598 // Write the epoch
599 // ---------------
600 QByteArray outLines;
601 QTextStream outStream(&outLines);
602 t_rnxObsFile::writeEpoch(&outStream, _header, &rnxEpo);
603
604 _out << outLines.data();
605 _out.flush();
606}
607
608// Close the Old RINEX File
609////////////////////////////////////////////////////////////////////////////
610void bncRinex::closeFile() {
611
612 if (_header.version() == 3) {
613 _out << "> 4 1" << endl;
614 _out << "END OF FILE" << endl;
615 }
616 _out.close();
617 if (!_rnxScriptName.isEmpty()) {
618 qApp->thread()->wait(100);
619#ifdef WIN32
620 QProcess::startDetached(_rnxScriptName, QStringList() << _fName) ;
621#else
622 QProcess::startDetached("nohup", QStringList() << _rnxScriptName << _fName) ;
623#endif
624
625 }
626}
627
628// One Line in ASCII (Internal) Format
629////////////////////////////////////////////////////////////////////////////
630string bncRinex::asciiSatLine(const t_satObs& obs, bool outLockTime) {
631
632 ostringstream str;
633 str.setf(ios::showpoint | ios::fixed);
634
635 str << obs._prn.toString();
636
637 for (unsigned ii = 0; ii < obs._obs.size(); ii++) {
638 const t_frqObs* frqObs = obs._obs[ii];
639 if (frqObs->_codeValid) {
640 str << ' '
641 << left << setw(3) << "C" + frqObs->_rnxType2ch << ' '
642 << right << setw(14) << setprecision(3) << frqObs->_code;
643 }
644 if (frqObs->_phaseValid) {
645 str << ' '
646 << left << setw(3) << "L" + frqObs->_rnxType2ch << ' '
647 << right << setw(14) << setprecision(3) << frqObs->_phase << ' '
648 << right << setw(4) << frqObs->_slipCounter;
649 }
650 if (frqObs->_dopplerValid) {
651 str << ' '
652 << left << setw(3) << "D" + frqObs->_rnxType2ch << ' '
653 << right << setw(14) << setprecision(3) << frqObs->_doppler;
654 }
655 if (frqObs->_snrValid) {
656 str << ' '
657 << left << setw(3) << "S" + frqObs->_rnxType2ch << ' '
658 << right << setw(8) << setprecision(3) << frqObs->_snr;
659 }
660 if (frqObs->_lockTimeValid && outLockTime) {
661 str << ' '
662 << left << setw(3) << "T" + frqObs->_rnxType2ch << ' '
663 << right << setw(9) << setprecision(3) << frqObs->_lockTime;
664 }
665 }
666
667 return str.str();
668}
Note: See TracBrowser for help on using the repository browser.