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

Last change on this file since 6147 was 6147, checked in by mervart, 10 years ago
File size: 20.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 _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" << "D1C" << "S1C"
389 << "C1P" << "L1P" << "D1P" << "S1P"
390 << "C2C" << "L2C" << "D2C" << "S2C"
391 << "C2P" << "L2P" << "D2P" << "S2P"
392 << "C5" << "D5" << "L5" << "S5";
393
394 _header._obsTypes['R'] << "C1C" << "L1C" << "D1C" << "S1C"
395 << "C1P" << "L1P" << "D1P" << "S1P"
396 << "C2C" << "L2C" << "D2C" << "S2C"
397 << "C2P" << "L2P" << "D2P" << "S2P";
398
399 _header._obsTypes['E'] << "C1" << "L1" << "D1" << "S1"
400 << "C5" << "L5" << "D5" << "S5"
401 << "C6" << "L6" << "D6" << "S6"
402 << "C7" << "L7" << "D7" << "S7"
403 << "C8" << "L8" << "D8" << "S8";
404
405 _header._obsTypes['J'] << "C1" << "L1" << "D1" << "S1"
406 << "C2" << "L2" << "D2" << "S2"
407 << "C5" << "L5" << "D5" << "S5"
408 << "C6" << "D6" << "L6" << "S6";
409
410 _header._obsTypes['S'] << "C1" << "L1" << "D1" << "S1"
411 << "C5" << "L5" << "D5" << "S5";
412
413 _header._obsTypes['C'] << "C1" << "L1" << "D1" << "S1"
414 << "C6" << "L6" << "D6" << "S6"
415 << "C7" << "L7" << "D7" << "S7";
416 }
417 }
418
419 // Write the Header
420 // ----------------
421 QByteArray headerLines;
422 QTextStream outHlp(&headerLines);
423
424 QMap<QString, QString> txtMap;
425 txtMap["COMMENT"] = _addComments.join("\\n");
426
427 _header.write(&outHlp, &txtMap);
428
429 outHlp.flush();
430
431 if (!_headerWritten) {
432 _out << headerLines.data();
433 }
434
435 _headerWritten = true;
436}
437
438// Stores Observation into Internal Array
439////////////////////////////////////////////////////////////////////////////
440void bncRinex::deepCopy(t_satObs obs) {
441 _obs.push_back(obs);
442}
443
444// Write One Epoch into the RINEX File
445////////////////////////////////////////////////////////////////////////////
446void bncRinex::dumpEpoch(const QByteArray& format, long maxTime) {
447
448 // Select observations older than maxTime
449 // --------------------------------------
450 QList<t_satObs> dumpList;
451 QMutableListIterator<t_satObs> mIt(_obs);
452 while (mIt.hasNext()) {
453 t_satObs obs = mIt.next();
454 if (obs._time.gpsw() * 7*24*3600 + obs._time.gpssec() < maxTime - 0.05) {
455 dumpList.push_back(obs);
456 mIt.remove();
457 }
458 }
459
460 // Easy Return
461 // -----------
462 if (dumpList.isEmpty()) {
463 return;
464 }
465
466 // Time of Epoch
467 // -------------
468 const t_satObs& fObs = dumpList.first();
469 QDateTime datTim = dateAndTimeFromGPSweek(fObs._time.gpsw(), fObs._time.gpssec());
470 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs._time.gpsw(),
471 floor(fObs._time.gpssec()+0.5));
472
473 // Close the file
474 // --------------
475 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
476 closeFile();
477 _headerWritten = false;
478 }
479
480 // Write RINEX Header
481 // ------------------
482 if (!_headerWritten) {
483 _header._startTime.set(fObs._time.gpsw(), fObs._time.gpssec());
484 writeHeader(format, datTimNom);
485 }
486
487 // Check whether observation types available
488 // -----------------------------------------
489 QMutableListIterator<t_satObs> mItDump(dumpList);
490 while (mItDump.hasNext()) {
491 t_satObs& obs = mItDump.next();
492 if (!_header._obsTypes.contains(obs._prn.system()) && !_header._obsTypes.contains(obs._prn.system())) {
493 mItDump.remove();
494 }
495 }
496 if (dumpList.isEmpty()) {
497 return;
498 }
499
500 double sec = double(datTim.time().second()) + fmod(fObs._time.gpssec(),1.0);
501
502 // Epoch header line: RINEX Version 3
503 // ----------------------------------
504 if (_header._version >= 3.0) {
505 _out << datTim.toString("> yyyy MM dd hh mm ").toAscii().data()
506 << setw(10) << setprecision(7) << sec
507 << " " << 0 << setw(3) << dumpList.size() << endl;
508 }
509 // Epoch header line: RINEX Version 2
510 // ----------------------------------
511 else {
512 _out << datTim.toString(" yy MM dd hh mm ").toAscii().data()
513 << setw(10) << setprecision(7) << sec
514 << " " << 0 << setw(3) << dumpList.size();
515
516 QListIterator<t_satObs> it(dumpList); int iSat = 0;
517 while (it.hasNext()) {
518 iSat++;
519 const t_satObs& obs = it.next();
520 _out << obs._prn;
521 if (iSat == 12 && it.hasNext()) {
522 _out << endl << " ";
523 iSat = 0;
524 }
525 }
526 _out << endl;
527 }
528
529 QListIterator<t_satObs> it(dumpList);
530 while (it.hasNext()) {
531 const t_satObs& obs = it.next();
532
533 // Cycle slips detection
534 // ---------------------
535 QString prn(obs._prn.toString().c_str());
536 char lli1 = ' ';
537 char lli2 = ' ';
538 char lli5 = ' ';
539 char* lli = 0;
540
541 for (unsigned iFrq = 0; iFrq < obs._obs.size(); iFrq++) {
542 const t_frqObs* frqObs = obs._obs[iFrq];
543 QMap<QString, int>* slip_cnt = 0;
544 if (frqObs->_rnxType2ch[0] == '1') {
545 slip_cnt = &_slip_cnt_L1;
546 lli = &lli1;
547 }
548 else if (frqObs->_rnxType2ch[0] == '2') {
549 slip_cnt = &_slip_cnt_L2;
550 lli = &lli2;
551 }
552 else if (frqObs->_rnxType2ch[0] == '5') {
553 slip_cnt = &_slip_cnt_L5;
554 lli = &lli5;
555 }
556 else {
557 continue;
558 }
559 if (frqObs->_slip) {
560 if ( slip_cnt->find(prn) != slip_cnt->end() &&
561 slip_cnt->find(prn).value() != frqObs->_slipCounter ) {
562 *lli = '1';
563 }
564 }
565 (*slip_cnt)[prn] = frqObs->_slipCounter;
566 }
567
568 // Write the data
569 // --------------
570 _out << rinexSatLine(obs, lli1, lli2, lli5) << endl;
571 }
572
573 _out.flush();
574}
575
576// Close the Old RINEX File
577////////////////////////////////////////////////////////////////////////////
578void bncRinex::closeFile() {
579 if (_header._version == 3) {
580 _out << "> 4 1" << endl;
581 _out << "END OF FILE" << endl;
582 }
583 _out.close();
584 if (!_rnxScriptName.isEmpty()) {
585 qApp->thread()->wait(100);
586#ifdef WIN32
587 QProcess::startDetached(_rnxScriptName, QStringList() << _fName) ;
588#else
589 QProcess::startDetached("nohup", QStringList() << _rnxScriptName << _fName) ;
590#endif
591
592 }
593}
594
595// One Line in RINEX v2 or v3
596////////////////////////////////////////////////////////////////////////////
597string bncRinex::rinexSatLine(const t_satObs& obs, char lli1, char lli2, char lli5) {
598 ostringstream str;
599 str.setf(ios::showpoint | ios::fixed);
600
601 if (_header._version >= 3.0) {
602 str << obs._prn;
603 }
604
605 const QString obsKinds = "LCDS";
606
607 char sys = obs._prn.system();
608 const QVector<QString>& types = _header._obsTypes[sys];
609 for (int ii = 0; ii < types.size(); ii++) {
610 if (_header._version < 3.0 && ii > 0 && ii % 5 == 0) {
611 str << endl;
612 }
613 double obsValue = 0.0;
614 char lli = ' ';
615 QString rnxType = types[ii];
616 for (unsigned iFrq = 0; iFrq < obs._obs.size(); iFrq++) {
617 const t_frqObs* frqObs = obs._obs[iFrq];
618 for (int ik = 0; ik < obsKinds.length(); ik++) {
619 QChar ch = obsKinds[ik];
620 QString obsType = (ch + QString(frqObs->_rnxType2ch.c_str())).left(rnxType.length());
621 if (rnxType == obsType) {
622 if (ch == 'L' && frqObs->_phaseValid) {
623 obsValue = frqObs->_phase;
624 if (obsType[1] == '1') {
625 lli = lli1;
626 }
627 else if (obsType[1] == '2') {
628 lli = lli2;
629 }
630 else if (obsType[1] == '5') {
631 lli = lli5;
632 }
633 }
634 else if (ch == 'C' && frqObs->_codeValid) {
635 obsValue = frqObs->_code;
636 }
637 else if (ch == 'D' && frqObs->_dopplerValid) {
638 obsValue = frqObs->_doppler;
639 }
640 else if (ch == 'S' && frqObs->_snrValid) {
641 obsValue = frqObs->_snr;
642 }
643 }
644 }
645 }
646 if (obsValue != 0.0) {
647 str << setw(14) << setprecision(3) << obsValue << lli << ' ';
648 }
649 else {
650 str << " ";
651 }
652 }
653
654 cout << str.str() << endl;
655
656 return str.str();
657}
658
659//
660////////////////////////////////////////////////////////////////////////////
661string bncRinex::obsToStr(double val, int width, int precision) {
662 if (val != 0.0) {
663 ostringstream str;
664 str.setf(ios::showpoint | ios::fixed);
665 str << setw(width) << setprecision(precision) << val;
666 return str.str();
667 }
668 else {
669 return "0.0";
670 }
671}
672
673// One Line in ASCII (Internal) Format
674////////////////////////////////////////////////////////////////////////////
675string bncRinex::asciiSatLine(const t_satObs& obs) {
676
677 ostringstream str;
678//// str.setf(ios::showpoint | ios::fixed);
679////
680//// str << obs._prn << ' ';
681////
682//// float rnxVers = 3.0;
683////
684//// for (int ie = 0; ie < GNSSENTRY_NUMBER; ie++) {
685//// QString rnxStr = obs.rnxStr(ie);
686//// if (rnxStr.length() >= 2) {
687//// double data = obs.measdata(rnxStr, rnxVers);
688//// if (data != 0.0) {
689//// int width = (rnxStr[0] == 'S') ? 8 : 14;
690//// int prec = 3;
691//// str << ' ' << rnxStr.toAscii().data()
692//// << ' ' << obsToStr(data, width, prec);
693//// if (rnxStr[0] == 'L') {
694//// int slipCnt = 0;
695//// if (rnxStr[1] == '1') {
696//// slipCnt = obs.slip_cnt_L1;
697//// }
698//// else if (rnxStr[1] == '2') {
699//// slipCnt = obs.slip_cnt_L2;
700//// }
701//// else if (rnxStr[1] == '5') {
702//// slipCnt = obs.slip_cnt_L5;
703//// }
704//// str << ' ' << setw(3) << slipCnt;
705//// }
706//// }
707//// }
708//// }
709
710 return str.str();
711}
Note: See TracBrowser for help on using the repository browser.