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

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