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

Last change on this file since 6190 was 6190, checked in by mervart, 10 years ago
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#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 _samplingRate = settings.value("rnxSampl").toInt();
93}
94
95// Destructor
96////////////////////////////////////////////////////////////////////////////
97bncRinex::~bncRinex() {
98 bncSettings settings;
99 if ((_header._version >= 3.0) && ( Qt::CheckState(settings.value("rnxAppend").toInt()) != Qt::Checked) ) {
100 _out << "> 4 1" << endl;
101 _out << "END OF FILE" << endl;
102 }
103 _out.close();
104}
105
106// Download Skeleton Header File
107////////////////////////////////////////////////////////////////////////////
108t_irc bncRinex::downloadSkeleton() {
109
110 t_irc irc = failure;
111
112 QStringList table;
113 bncTableDlg::getFullTable(_ntripVersion, _mountPoint.host(),
114 _mountPoint.port(), table, true);
115 QString net;
116 QStringListIterator it(table);
117 while (it.hasNext()) {
118 QString line = it.next();
119 if (line.indexOf("STR") == 0) {
120 QStringList tags = line.split(";");
121 if (tags.size() > 7) {
122 if (tags.at(1) == _mountPoint.path().mid(1).toAscii()) {
123 net = tags.at(7);
124 break;
125 }
126 }
127 }
128 }
129 QString sklDir;
130 if (!net.isEmpty()) {
131 it.toFront();
132 while (it.hasNext()) {
133 QString line = it.next();
134 if (line.indexOf("NET") == 0) {
135 QStringList tags = line.split(";");
136 if (tags.size() > 6) {
137 if (tags.at(1) == net) {
138 sklDir = tags.at(6).trimmed();
139 break;
140 }
141 }
142 }
143 }
144 }
145 if (!sklDir.isEmpty() && sklDir != "none") {
146 QUrl url(sklDir + "/" + _mountPoint.path().mid(1,4).toLower() + ".skl");
147 if (url.port() == -1) {
148 url.setPort(80);
149 }
150
151 bncNetQuery* query;
152 if (_ntripVersion == "2s") {
153 query = new bncNetQueryV2(true);
154 }
155 else if (_ntripVersion == "2") {
156 query = new bncNetQueryV2(false);
157 }
158 else {
159 query = new bncNetQueryV1;
160 }
161
162 QByteArray outData;
163 query->waitForRequestResult(url, outData);
164 if (query->status() == bncNetQuery::finished) {
165 irc = success;
166 _header._obsTypes.clear();
167 QTextStream in(outData);
168 _header.read(&in);
169 }
170
171 delete query;
172 }
173
174 return irc;
175}
176
177// Read Skeleton Header File
178////////////////////////////////////////////////////////////////////////////
179bool bncRinex::readSkeleton() {
180
181 bool readDone = false;
182
183 // Read the local file
184 // -------------------
185 QFile skl(_sklName);
186 if ( skl.exists() && skl.open(QIODevice::ReadOnly) ) {
187 readDone = true;
188 _header._obsTypes.clear();
189 QTextStream in(&skl);
190 _header.read(&in);
191 }
192
193 // Read downloaded file
194 // --------------------
195 else if ( _ntripVersion != "N" && _ntripVersion != "UN" &&
196 _ntripVersion != "S" ) {
197 QDate currDate = currentDateAndTimeGPS().date();
198 if ( !_skeletonDate.isValid() || _skeletonDate != currDate ) {
199 if (downloadSkeleton() == success) {
200 readDone = true;
201 }
202 _skeletonDate = currDate;
203 }
204 }
205
206 return readDone;
207}
208
209// Next File Epoch (static)
210////////////////////////////////////////////////////////////////////////////
211QString bncRinex::nextEpochStr(const QDateTime& datTim,
212 const QString& intStr, QDateTime* nextEpoch) {
213
214 QString epoStr;
215
216 QTime nextTime;
217 QDate nextDate;
218
219 int indHlp = intStr.indexOf("min");
220
221 if ( indHlp != -1) {
222 int step = intStr.left(indHlp-1).toInt();
223 char ch = 'A' + datTim.time().hour();
224 epoStr = ch;
225 if (datTim.time().minute() >= 60-step) {
226 epoStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
227 if (datTim.time().hour() < 23) {
228 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
229 nextDate = datTim.date();
230 }
231 else {
232 nextTime.setHMS(0, 0, 0);
233 nextDate = datTim.date().addDays(1);
234 }
235 }
236 else {
237 for (int limit = step; limit <= 60-step; limit += step) {
238 if (datTim.time().minute() < limit) {
239 epoStr += QString("%1").arg(limit-step, 2, 10, QChar('0'));
240 nextTime.setHMS(datTim.time().hour(), limit, 0);
241 nextDate = datTim.date();
242 break;
243 }
244 }
245 }
246 }
247 else if (intStr == "1 hour") {
248 char ch = 'A' + datTim.time().hour();
249 epoStr = ch;
250 if (datTim.time().hour() < 23) {
251 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
252 nextDate = datTim.date();
253 }
254 else {
255 nextTime.setHMS(0, 0, 0);
256 nextDate = datTim.date().addDays(1);
257 }
258 }
259 else {
260 epoStr = "0";
261 nextTime.setHMS(0, 0, 0);
262 nextDate = datTim.date().addDays(1);
263 }
264
265 if (nextEpoch) {
266 *nextEpoch = QDateTime(nextDate, nextTime);
267 }
268
269 return epoStr;
270}
271
272// File Name according to RINEX Standards
273////////////////////////////////////////////////////////////////////////////
274void bncRinex::resolveFileName(const QDateTime& datTim) {
275
276 bncSettings settings;
277 QString path = settings.value("rnxPath").toString();
278 expandEnvVar(path);
279
280 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
281 path += QDir::separator();
282 }
283
284 QString hlpStr = nextEpochStr(datTim, settings.value("rnxIntr").toString(),
285 &_nextCloseEpoch);
286
287 QString ID4 = _statID.left(4);
288
289 // Check name conflict
290 // -------------------
291 QString distStr;
292 int num = 0;
293 QListIterator<QString> it(settings.value("mountPoints").toStringList());
294 while (it.hasNext()) {
295 QString mp = it.next();
296 if (mp.indexOf(ID4) != -1) {
297 ++num;
298 }
299 }
300 if (num > 1) {
301 distStr = "_" + _statID.mid(4);
302 }
303
304 QString sklExt = settings.value("rnxSkel").toString();
305 if (!sklExt.isEmpty()) {
306 _sklName = path + ID4 + distStr + "." + sklExt;
307 }
308
309 path += ID4 +
310 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
311 hlpStr + distStr + datTim.toString(".yyO");
312
313 _fName = path.toAscii();
314}
315
316// Write RINEX Header
317////////////////////////////////////////////////////////////////////////////
318void bncRinex::writeHeader(const QByteArray& format,
319 const QDateTime& datTimNom) {
320
321 bncSettings settings;
322
323 // Open the Output File
324 // --------------------
325 resolveFileName(datTimNom);
326
327 // Append to existing file and return
328 // ----------------------------------
329 if ( QFile::exists(_fName) &&
330 (_reconnectFlag || Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) ) {
331 _out.open(_fName.data(), ios::app);
332 _out.setf(ios::showpoint | ios::fixed);
333 _headerWritten = true;
334 _reconnectFlag = false;
335 }
336 else {
337 _out.open(_fName.data());
338 }
339
340 _out.setf(ios::showpoint | ios::fixed);
341
342 // Read Skeleton Header
343 // --------------------
344 bool skelRead = readSkeleton();
345
346 // Set RINEX Version
347 // -----------------
348 if ( Qt::CheckState(settings.value("rnxV3").toInt()) == Qt::Checked) {
349 _header._version = t_rnxObsHeader::defaultRnxObsVersion3;
350 }
351 else {
352 _header._version = t_rnxObsHeader::defaultRnxObsVersion2;
353 }
354
355 // A Few Additional Comments
356 // -------------------------
357 if (skelRead || _addComments.size() == 0) {
358 _addComments.clear();
359 _addComments << format.left(6) + " "
360 + _mountPoint.host() + _mountPoint.path();
361 if (_header._obsTypes.size() == 0) {
362 _addComments << "Default set of observation types used";
363 }
364 if (_nmea == "yes") {
365 _addComments << "NMEA LAT=" + _latitude + " " + "LONG=" + _longitude;
366 }
367 }
368
369 // Set Marker Name
370 // ---------------
371 if (_header._markerName.isEmpty()) {
372 _header._markerName = _statID;
373 }
374
375 // Set Default Observation Types
376 // -----------------------------
377 if (_header._obsTypes.size() == 0) {
378 if (_header._version < 3.0) {
379 _header._obsTypes['G'] << "C1" << "P1" << "L1" << "S1"
380 << "C2" << "P2" << "L2" << "S2";
381 _header._obsTypes['R'] = _header._obsTypes['G'];
382 _header._obsTypes['E'] = _header._obsTypes['G'];
383 _header._obsTypes['J'] = _header._obsTypes['G'];
384 _header._obsTypes['S'] = _header._obsTypes['G'];
385 _header._obsTypes['C'] = _header._obsTypes['G'];
386 }
387 else {
388 _header._obsTypes['G'] << "C1C" << "L1C" << "S1C"
389 << "C2W" << "L2W" << "S2W"
390 << "C5" << "L5" << "S5";
391
392 _header._obsTypes['J'] = _header._obsTypes['G'];
393
394 _header._obsTypes['R'] << "C1C" << "L1C" << "S1C"
395 << "C2P" << "L2P" << "S2P";
396
397 _header._obsTypes['E'] << "C1" << "L1" << "S1"
398 << "C5" << "L5" << "S5"
399 << "C7" << "L7" << "S7"
400 << "C8" << "L8" << "S8";
401
402 _header._obsTypes['S'] << "C1" << "L1" << "S1"
403 << "C5" << "L5" << "S5";
404
405 _header._obsTypes['C'] << "C1" << "L1" << "S1"
406 << "C6" << "L6" << "S6"
407 << "C7" << "L7" << "S7";
408 }
409 }
410
411 // Write the Header
412 // ----------------
413 QByteArray headerLines;
414 QTextStream outHlp(&headerLines);
415
416 QMap<QString, QString> txtMap;
417 txtMap["COMMENT"] = _addComments.join("\\n");
418
419 _header.write(&outHlp, &txtMap);
420
421 outHlp.flush();
422
423 if (!_headerWritten) {
424 _out << headerLines.data();
425 }
426
427 _headerWritten = true;
428}
429
430// Stores Observation into Internal Array
431////////////////////////////////////////////////////////////////////////////
432void bncRinex::deepCopy(t_satObs obs) {
433 _obs.push_back(obs);
434}
435
436// Write One Epoch into the RINEX File
437////////////////////////////////////////////////////////////////////////////
438void bncRinex::dumpEpoch(const QByteArray& format, long maxTime) {
439
440 // Select observations older than maxTime
441 // --------------------------------------
442 QList<t_satObs> dumpList;
443 QMutableListIterator<t_satObs> mIt(_obs);
444 while (mIt.hasNext()) {
445 t_satObs obs = mIt.next();
446 if (obs._time.gpsw() * 7*24*3600 + obs._time.gpssec() < maxTime - 0.05) {
447 dumpList.push_back(obs);
448 mIt.remove();
449 }
450 }
451
452 // Easy Return
453 // -----------
454 if (dumpList.isEmpty()) {
455 return;
456 }
457
458 // Time of Epoch
459 // -------------
460 const t_satObs& fObs = dumpList.first();
461 QDateTime datTim = dateAndTimeFromGPSweek(fObs._time.gpsw(), fObs._time.gpssec());
462 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs._time.gpsw(),
463 floor(fObs._time.gpssec()+0.5));
464
465 // Close the file
466 // --------------
467 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
468 closeFile();
469 _headerWritten = false;
470 }
471
472 // Write RINEX Header
473 // ------------------
474 if (!_headerWritten) {
475 _header._startTime.set(fObs._time.gpsw(), fObs._time.gpssec());
476 writeHeader(format, datTimNom);
477 }
478
479 // Check whether observation types available
480 // -----------------------------------------
481 QMutableListIterator<t_satObs> mItDump(dumpList);
482 while (mItDump.hasNext()) {
483 t_satObs& obs = mItDump.next();
484 if (!_header._obsTypes.contains(obs._prn.system()) && !_header._obsTypes.contains(obs._prn.system())) {
485 mItDump.remove();
486 }
487 }
488 if (dumpList.isEmpty()) {
489 return;
490 }
491
492 double sec = double(datTim.time().second()) + fmod(fObs._time.gpssec(),1.0);
493
494 // Epoch header line: RINEX Version 3
495 // ----------------------------------
496 if (_header._version >= 3.0) {
497 _out << datTim.toString("> yyyy MM dd hh mm ").toAscii().data()
498 << setw(10) << setprecision(7) << sec
499 << " " << 0 << setw(3) << dumpList.size() << endl;
500 }
501 // Epoch header line: RINEX Version 2
502 // ----------------------------------
503 else {
504 _out << datTim.toString(" yy MM dd hh mm ").toAscii().data()
505 << setw(10) << setprecision(7) << sec
506 << " " << 0 << setw(3) << dumpList.size();
507
508 QListIterator<t_satObs> it(dumpList); int iSat = 0;
509 while (it.hasNext()) {
510 iSat++;
511 const t_satObs& obs = it.next();
512 _out << obs._prn.toString();
513 if (iSat == 12 && it.hasNext()) {
514 _out << endl << " ";
515 iSat = 0;
516 }
517 }
518 _out << endl;
519 }
520
521 QListIterator<t_satObs> it(dumpList);
522 while (it.hasNext()) {
523 const t_satObs& obs = it.next();
524
525 // Cycle slips detection
526 // ---------------------
527 QString prn(obs._prn.toString().c_str());
528 char lli1 = ' ';
529 char lli2 = ' ';
530 char lli5 = ' ';
531 char* lli = 0;
532
533 for (unsigned iFrq = 0; iFrq < obs._obs.size(); iFrq++) {
534 const t_frqObs* frqObs = obs._obs[iFrq];
535 QMap<QString, int>* slip_cnt = 0;
536 if (frqObs->_rnxType2ch[0] == '1') {
537 slip_cnt = &_slip_cnt_L1;
538 lli = &lli1;
539 }
540 else if (frqObs->_rnxType2ch[0] == '2') {
541 slip_cnt = &_slip_cnt_L2;
542 lli = &lli2;
543 }
544 else if (frqObs->_rnxType2ch[0] == '5') {
545 slip_cnt = &_slip_cnt_L5;
546 lli = &lli5;
547 }
548 else {
549 continue;
550 }
551 if (frqObs->_slip) {
552 if ( slip_cnt->find(prn) != slip_cnt->end() &&
553 slip_cnt->find(prn).value() != frqObs->_slipCounter ) {
554 *lli = '1';
555 }
556 }
557 (*slip_cnt)[prn] = frqObs->_slipCounter;
558 }
559
560 // Write the data
561 // --------------
562 _out << rinexSatLine(obs, lli1, lli2, lli5) << endl;
563 }
564
565 _out.flush();
566}
567
568// Close the Old RINEX File
569////////////////////////////////////////////////////////////////////////////
570void bncRinex::closeFile() {
571 if (_header._version == 3) {
572 _out << "> 4 1" << endl;
573 _out << "END OF FILE" << endl;
574 }
575 _out.close();
576 if (!_rnxScriptName.isEmpty()) {
577 qApp->thread()->wait(100);
578#ifdef WIN32
579 QProcess::startDetached(_rnxScriptName, QStringList() << _fName) ;
580#else
581 QProcess::startDetached("nohup", QStringList() << _rnxScriptName << _fName) ;
582#endif
583
584 }
585}
586
587// One Line in RINEX v2 or v3
588////////////////////////////////////////////////////////////////////////////
589string bncRinex::rinexSatLine(const t_satObs& obs, char lli1, char lli2, char lli5) {
590 ostringstream str;
591 str.setf(ios::showpoint | ios::fixed);
592
593 if (_header._version >= 3.0) {
594 str << obs._prn.toString();
595 }
596
597 const QString obsKinds = "LCDS";
598
599 char sys = obs._prn.system();
600 const QVector<QString>& types = _header._obsTypes[sys];
601 for (int ii = 0; ii < types.size(); ii++) {
602 if (_header._version < 3.0 && ii > 0 && ii % 5 == 0) {
603 str << endl;
604 }
605 double obsValue = 0.0;
606 char lli = ' ';
607 QString rnxType = t_rnxObsFile::type2to3(types[ii]);
608 for (unsigned iFrq = 0; iFrq < obs._obs.size(); iFrq++) {
609 const t_frqObs* frqObs = obs._obs[iFrq];
610 for (int ik = 0; ik < obsKinds.length(); ik++) {
611 QChar ch = obsKinds[ik];
612 QString obsType = (ch + QString(frqObs->_rnxType2ch.c_str()));
613 obsType = t_rnxObsFile::type2to3(obsType).left(rnxType.length());
614 if (rnxType == obsType) {
615 if (ch == 'L' && frqObs->_phaseValid) {
616 obsValue = frqObs->_phase;
617 if (obsType[1] == '1') {
618 lli = lli1;
619 }
620 else if (obsType[1] == '2') {
621 lli = lli2;
622 }
623 else if (obsType[1] == '5') {
624 lli = lli5;
625 }
626 }
627 else if (ch == 'C' && frqObs->_codeValid) {
628 obsValue = frqObs->_code;
629 }
630 else if (ch == 'D' && frqObs->_dopplerValid) {
631 obsValue = frqObs->_doppler;
632 }
633 else if (ch == 'S' && frqObs->_snrValid) {
634 obsValue = frqObs->_snr;
635 }
636 }
637 }
638 }
639 str << setw(14) << setprecision(3) << obsValue << lli << ' ';
640 }
641
642 return str.str();
643}
644
645//
646////////////////////////////////////////////////////////////////////////////
647string bncRinex::obsToStr(double val, int width, int precision) {
648 if (val != 0.0) {
649 ostringstream str;
650 str.setf(ios::showpoint | ios::fixed);
651 str << setw(width) << setprecision(precision) << val;
652 return str.str();
653 }
654 else {
655 return "0.0";
656 }
657}
658
659// One Line in ASCII (Internal) Format
660////////////////////////////////////////////////////////////////////////////
661string bncRinex::asciiSatLine(const t_satObs& obs) {
662
663 ostringstream str;
664 str.setf(ios::showpoint | ios::fixed);
665
666 str << obs._prn.toString() << ' ';
667
668 for (unsigned ii = 0; ii < obs._obs.size(); ii++) {
669 const t_frqObs* frqObs = obs._obs[ii];
670 if (frqObs->_codeValid) {
671 str << " C" << frqObs->_rnxType2ch << ' '
672 << setw(14) << setprecision(3) << frqObs->_code;
673 }
674 if (frqObs->_phaseValid) {
675 str << " L" << frqObs->_rnxType2ch << ' '
676 << setw(14) << setprecision(3) << frqObs->_phase
677 << ' ' << setw(3) << frqObs->_slipCounter;
678 }
679 if (frqObs->_dopplerValid) {
680 str << " D" << frqObs->_rnxType2ch << ' '
681 << setw(14) << setprecision(3) << frqObs->_doppler;
682 }
683 if (frqObs->_snrValid) {
684 str << " S" << frqObs->_rnxType2ch << ' '
685 << setw(8) << setprecision(3) << frqObs->_doppler;
686 }
687 }
688
689 return str.str();
690}
Note: See TracBrowser for help on using the repository browser.