source: ntrip/trunk/BNC/src/rinex/rnxnavfile.cpp@ 10660

Last change on this file since 10660 was 10659, checked in by stuerze, 8 months ago

minor changes

File size: 12.8 KB
RevLine 
[3645]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: t_rnxNavFile
30 *
31 * Purpose: Reads RINEX Navigation File
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Jan-2012
36 *
[7638]37 * Changes:
[3645]38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
[3666]42#include <newmatio.h>
[3645]43#include "rnxnavfile.h"
[5070]44#include "bnccore.h"
[3657]45#include "bncutils.h"
[5738]46#include "ephemeris.h"
[3645]47
48using namespace std;
49
50// Constructor
51////////////////////////////////////////////////////////////////////////////
[3657]52t_rnxNavFile::t_rnxNavHeader::t_rnxNavHeader() {
53 _version = 0.0;
[3659]54 _glonass = false;
[3645]55}
56
57// Destructor
58////////////////////////////////////////////////////////////////////////////
[3657]59t_rnxNavFile::t_rnxNavHeader::~t_rnxNavHeader() {
60}
61
62// Read Header
63////////////////////////////////////////////////////////////////////////////
64t_irc t_rnxNavFile::t_rnxNavHeader::read(QTextStream* stream) {
65 while (stream->status() == QTextStream::Ok && !stream->atEnd()) {
66 QString line = stream->readLine();
67 if (line.isEmpty()) {
68 continue;
69 }
70 QString value = line.left(60).trimmed();
71 QString key = line.mid(60).trimmed();
72 if (key == "END OF HEADER") {
73 break;
74 }
75 else if (key == "RINEX VERSION / TYPE") {
[8204]76 QTextStream in(value.toLatin1(), QIODevice::ReadOnly);
[3657]77 in >> _version;
[3659]78 if (value.indexOf("GLONASS") != -1) {
79 _glonass = true;
80 }
[3657]81 }
[7999]82 else if (key == "COMMENT") {
83 _comments.append(value.trimmed());
84 }
[9945]85 else if (key == "PGM / RUN BY / DATE") {
86 _runByDate.append(value.trimmed());
87 }
[3657]88 }
89
90 return success;
91}
92
93// Constructor
94////////////////////////////////////////////////////////////////////////////
[3999]95t_rnxNavFile::t_rnxNavFile(const QString& fileName, e_inpOut inpOut) {
96 _inpOut = inpOut;
97 _stream = 0;
98 _file = 0;
99 if (_inpOut == input) {
100 openRead(fileName);
101 }
102 else {
103 openWrite(fileName);
104 }
[3657]105}
106
[3999]107// Open for input
108////////////////////////////////////////////////////////////////////////////
109void t_rnxNavFile::openRead(const QString& fileName) {
110
111 _fileName = fileName; expandEnvVar(_fileName);
112 _file = new QFile(_fileName);
113 _file->open(QIODevice::ReadOnly | QIODevice::Text);
114 _stream = new QTextStream();
115 _stream->setDevice(_file);
116
117 _header.read(_stream);
118 this->read(_stream);
119}
120
121// Open for output
122////////////////////////////////////////////////////////////////////////////
123void t_rnxNavFile::openWrite(const QString& fileName) {
124
125 _fileName = fileName; expandEnvVar(_fileName);
126 _file = new QFile(_fileName);
127 _file->open(QIODevice::WriteOnly | QIODevice::Text);
128 _stream = new QTextStream();
129 _stream->setDevice(_file);
130}
131
[3657]132// Destructor
133////////////////////////////////////////////////////////////////////////////
[3645]134t_rnxNavFile::~t_rnxNavFile() {
[3999]135 close();
[3758]136 for (unsigned ii = 0; ii < _ephs.size(); ii++) {
137 delete _ephs[ii];
[7638]138 }
[3645]139}
140
[3999]141// Close
142////////////////////////////////////////////////////////////////////////////
143void t_rnxNavFile::close() {
144 delete _stream; _stream = 0;
145 delete _file; _file = 0;
146}
147
[3746]148// Read File Content
[3659]149////////////////////////////////////////////////////////////////////////////
[3746]150void t_rnxNavFile::read(QTextStream* stream) {
[10603]151 QString navType;
152 QString prn;
[3659]153
[3746]154 while (stream->status() == QTextStream::Ok && !stream->atEnd()) {
[10603]155 char sys;
[3746]156 QString line = stream->readLine();
[3659]157 if (line.isEmpty()) {
158 continue;
159 }
[10603]160
[10548]161 QStringList hlp = line.split(QRegExp("\\s+"), Qt::SkipEmptyParts);
[10587]162 QString firstStr = hlp.at(0);
[10574]163
[10587]164 // RINEX version 3
165 if (version() >= 3.0 &&
166 version() < 4.0 && firstStr != ">") {
[9765]167 prn = firstStr;
[10574]168 sys = prn[0].toLatin1();
[10587]169 navType = "";
[3659]170 }
[10587]171 // RINEX version 4
[9765]172 else if (version() >= 4.0 && firstStr == ">") {
[10587]173 QString key = hlp.at(1);
[10574]174 prn = hlp.at(2);
175 sys = prn[0].toLatin1();
[10587]176 navType = hlp.at(3);
[10574]177
178 // ALL Non-EPH messages are currently ignored
[9765]179 int lines2skip = 0;
[10574]180 // STO
181 if (key == "STO") {
[9765]182 lines2skip = 2;
183 }
[10574]184 // EOP
185 else if (key == "EOP") {
[9765]186 lines2skip = 3;
187 }
[10574]188 // ION
189 else if (key == "ION") {
190 if (sys == 'R') {
191 lines2skip = 1; //if (navType == "LXOC") {}
192 }
193 else if (sys == 'E') {
194 lines2skip = 2;
195 }
196 else if (sys == 'G' || sys == 'C' ||
197 sys == 'J' || sys == 'I') {
198 lines2skip = 3;
[10587]199 if ((sys == 'I' && navType == "L1NV") || // I: KLOB, NEQN
200 (sys == 'J' && navType == "CNVX")) { // J: WIDE, JAPN
201 QString navSubType = hlp.at(4);
[10574]202 if (navSubType == "KLOB") {
203 lines2skip += 1;
204 }
205 else if (navSubType == "NEQN") {
206 lines2skip += 4;
207 }
208 }
209 }
210 }
[9765]211 if (lines2skip) {
[10603]212 for (int ii = 0; ii < lines2skip; ii++) {
[9765]213 stream->readLine();
214 }
215 }
216 continue;
217 }
[10587]218 // RINEX version 2
[3659]219 else {
220 if (glonass()) {
[10659]221 sys = 'R';
[3659]222 }
223 else {
[10659]224 sys = 'G';
[3659]225 }
[10659]226 navType = "";
227 prn = QString("%1").arg(hlp.at(0).toInt(), 2, 10, QChar('0'));
[3659]228 }
[7638]229
[3746]230 t_eph* eph = 0;
[10574]231 QStringList lines;
232 lines << line;
233 if (sys == 'G') {
[10587]234 int nLines = 8;
235 if (navType == "CNAV") {
236 nLines += 1;
237 }
238 if (navType == "CNV2") {
239 nLines += 2;
240 }
241 for (int ii = 1; ii < nLines; ii++) {
[3746]242 lines << stream->readLine();
[3659]243 }
[10603]244 eph = new t_ephGPS(version(), lines, navType);
[3659]245 }
[10574]246 else if (sys == 'R') {
[10587]247 int nLines = 4;
[9366]248 if (version() >= 3.05) {
[10587]249 nLines += 1;
[9366]250 }
[10587]251 for (int ii = 1; ii < nLines; ii++) {
[3746]252 lines << stream->readLine();
[3659]253 }
[10603]254 eph = new t_ephGlo(version(), lines, navType);
[3659]255 }
[10574]256 else if (sys == 'E') {
[3659]257 for (int ii = 1; ii < 8; ii++) {
[3746]258 lines << stream->readLine();
[3659]259 }
[10603]260 eph = new t_ephGal(version(), lines, navType);
[3659]261 }
[10574]262 else if (sys == 'J') {
[10587]263 int nLines = 8;
264 if (navType == "CNAV") {
265 nLines += 1;
266 }
267 if (navType == "CNV2") {
268 nLines += 2;
269 }
270 for (int ii = 1; ii < nLines; ii++) {
[6377]271 lines << stream->readLine();
272 }
[10603]273 eph = new t_ephGPS(version(), lines, navType);
[6377]274 }
[10574]275 else if (sys== 'S') {
[6391]276 for (int ii = 1; ii < 4; ii++) {
277 lines << stream->readLine();
278 }
[10603]279 eph = new t_ephSBAS(version(), lines, navType);
[6391]280 }
[10574]281 else if (sys == 'C') {
[10587]282 int nLines = 8;
283 if (navType == "CNV1" ||
284 navType == "CNV2") {
285 nLines += 2;
286 }
287 if (navType == "CNV3") {
288 nLines += 1;
289 }
290 for (int ii = 1; ii < nLines; ii++) {
[6399]291 lines << stream->readLine();
292 }
[10603]293 eph = new t_ephBDS(version(), lines, navType);
[6399]294 }
[10574]295 else if (sys == 'I') {
[10587]296 int nLines = 8;
297 if (navType == "L1NV") {
298 nLines += 1;
299 }
300 for (int ii = 1; ii < nLines; ii++) {
[8168]301 lines << stream->readLine();
302 }
[10603]303 eph = new t_ephGPS(version(), lines, navType);
[8168]304 }
[10525]305
[10318]306 if (eph) {
307 _ephs.push_back(eph);
308 }
[10587]309
[3659]310 }
[3746]311}
[3659]312
[3746]313// Read Next Ephemeris
314////////////////////////////////////////////////////////////////////////////
[7638]315t_eph* t_rnxNavFile::getNextEph(const bncTime& tt,
[7169]316 const QMap<QString, unsigned int>* corrIODs) {
[3758]317
[3764]318 // Get Ephemeris according to IOD
319 // ------------------------------
[3758]320 if (corrIODs) {
[7169]321 QMapIterator<QString, unsigned int> itIOD(*corrIODs);
[3758]322 while (itIOD.hasNext()) {
323 itIOD.next();
[10599]324 QString corrPrn = itIOD.key();
325 unsigned int corrIod = itIOD.value();
[3758]326 vector<t_eph*>::iterator it = _ephs.begin();
327 while (it != _ephs.end()) {
328 t_eph* eph = *it;
[4150]329 double dt = eph->TOC() - tt;
[10599]330 QString ephPrn = QString(eph->prn().toInternalString().c_str());
331 unsigned int ephIod = eph->IOD();
332 if (dt < 8*3600.0 &&
333 ephPrn == corrPrn &&
334 ephIod == corrIod) {
[3758]335 it = _ephs.erase(it);
336 return eph;
337 }
338 ++it;
339 }
[3748]340 }
[3746]341 }
[3758]342
[3764]343 // Get Ephemeris according to time
344 // -------------------------------
345 else {
346 vector<t_eph*>::iterator it = _ephs.begin();
347 while (it != _ephs.end()) {
348 t_eph* eph = *it;
[4018]349 double dt = eph->TOC() - tt;
[10599]350 char sys = eph->prn().system();
351 int num = eph->prn().number();
352 int ssrNavType = t_corrSSR::getSsrNavTypeFlag(sys, num);
353 if (dt < 2*3600.0 && eph->type() == ssrNavType) {
[3764]354 it = _ephs.erase(it);
355 return eph;
356 }
357 ++it;
358 }
359 }
360
[3683]361 return 0;
[3659]362}
[4004]363
[7638]364//
[4004]365////////////////////////////////////////////////////////////////////////////
[9765]366void t_rnxNavFile::writeHeader(const QMap<QString, QString>* txtMap, int numMergedFiles, int leapSecs) {
[4010]367
[5068]368 QString runBy = BNC_CORE->userName();
[4219]369 QStringList comments;
[9945]370 QStringList runByDate;
[4219]371
372 if (txtMap) {
373 QMapIterator<QString, QString> it(*txtMap);
374 while (it.hasNext()) {
375 it.next();
376 if (it.key() == "RUN BY") {
377 runBy = it.value();
378 }
[9945]379 else if (it.key() == "RUN BY DATE") {
[10548]380 runByDate = it.value().split("\\n", Qt::SkipEmptyParts);
[9945]381 }
[4219]382 else if (it.key() == "COMMENT") {
[10548]383 comments = it.value().split("\\n", Qt::SkipEmptyParts);
[4219]384 }
385 }
386 }
387
[4010]388 if (version() < 3.0) {
[7638]389 const QString fmt = glonass() ? "%1 GLONASS navigation data"
[4230]390 : "%1 Navigation data";
391 *_stream << QString(fmt)
[4010]392 .arg(_header._version, 9, 'f', 2)
393 .leftJustified(60)
394 << "RINEX VERSION / TYPE\n";
395 }
396 else {
[8354]397 QString fmt;
[10587]398 t_eph::e_system sys = satSystem();
[8354]399 switch(sys) {
400 case t_eph::GPS:
401 fmt.append("%1 N: GNSS NAV DATA G: GPS");
402 break;
403 case t_eph::GLONASS:
404 fmt.append("%1 N: GNSS NAV DATA R: GLONASS");
405 break;
406 case t_eph::Galileo:
407 fmt.append("%1 N: GNSS NAV DATA E: Galileo");
408 break;
409 case t_eph::QZSS:
410 fmt.append("%1 N: GNSS NAV DATA J: QZSS");
411 break;
412 case t_eph::BDS:
413 fmt.append("%1 N: GNSS NAV DATA C: BDS");
414 break;
[10619]415 case t_eph::NavIC:
416 fmt.append("%1 N: GNSS NAV DATA I: NavIC");
[8354]417 break;
418 case t_eph::SBAS:
419 fmt.append("%1 N: GNSS NAV DATA S: SBAS");
420 break;
421 case t_eph::unknown:
422 fmt.append("%1 N: GNSS NAV DATA M: MIXED");
423 break;
424 }
425 *_stream << fmt
[4010]426 .arg(_header._version, 9, 'f', 2)
427 .leftJustified(60)
428 << "RINEX VERSION / TYPE\n";
429 }
430
[4232]431 const QString fmtDate = (version() < 3.0) ? "dd-MMM-yy hh:mm"
[4231]432 : "yyyyMMdd hhmmss UTC";
[4010]433 *_stream << QString("%1%2%3")
[5068]434 .arg(BNC_CORE->pgmName(), -20)
[4219]435 .arg(runBy.trimmed().left(20), -20)
[4231]436 .arg(QDateTime::currentDateTime().toUTC().toString(fmtDate), -20)
[4010]437 .leftJustified(60)
438 << "PGM / RUN BY / DATE\n";
439
[9765]440 if (version() >= 4.0) {
[9945]441 QStringListIterator itRunByDt(runByDate);
442 while (itRunByDt.hasNext()) {
443 *_stream << itRunByDt.next().trimmed().left(60).leftJustified(60)
444 << "PGM / RUN BY / DATE\n";
445 }
[9765]446 *_stream << QString("%1").arg(leapSecs, 6, 10, QLatin1Char(' ')).left(60).leftJustified(60)
447 << "LEAP SECONDS\n";
448
449 *_stream << QString("%1").arg(numMergedFiles, 19, 10, QLatin1Char(' ')).leftJustified(60)
450 << "MERGED FILE\n";
451 }
452
[4222]453 QStringListIterator itCmnt(comments);
454 while (itCmnt.hasNext()) {
455 *_stream << itCmnt.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
456 }
457
[4010]458 *_stream << QString()
459 .leftJustified(60)
460 << "END OF HEADER\n";
[4004]461}
462
[7638]463//
[4004]464////////////////////////////////////////////////////////////////////////////
465void t_rnxNavFile::writeEph(const t_eph* eph) {
[4013]466 *_stream << eph->toString(version());
[4004]467}
Note: See TracBrowser for help on using the repository browser.