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

Last change on this file since 5520 was 5518, checked in by weber, 11 years ago

Made C1 in BeiDou the default observation type insteamd of C2

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