source: ntrip/trunk/BNC/rinex/rnxobsfile.cpp@ 3837

Last change on this file since 3837 was 3837, checked in by mervart, 12 years ago
File size: 13.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: t_rnxObsFile
30 *
31 * Purpose: Reads RINEX Observation File
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Jan-2012
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42#include <iomanip>
43#include <sstream>
44#include "rnxobsfile.h"
45#include "bncutils.h"
46
47using namespace std;
48
49const QString t_rnxObsFile::t_rnxObsHeader::_emptyStr;
50
51// Constructor
52////////////////////////////////////////////////////////////////////////////
53t_rnxObsFile::t_rnxObsHeader::t_rnxObsHeader() {
54 _antNEU.ReSize(3); _antNEU = 0.0;
55 _antXYZ.ReSize(3); _antXYZ = 0.0;
56 _antBSG.ReSize(3); _antBSG = 0.0;
57 _xyz.ReSize(3); _xyz = 0.0;
58 _version = 0.0;
59 _interval = 0.0;
60 for (unsigned iPrn = 1; iPrn <= MAXPRN_GPS; iPrn++) {
61 _wlFactorsL1[iPrn] = 1;
62 _wlFactorsL2[iPrn] = 1;
63 }
64}
65
66// Destructor
67////////////////////////////////////////////////////////////////////////////
68t_rnxObsFile::t_rnxObsHeader::~t_rnxObsHeader() {
69}
70
71// Read Header
72////////////////////////////////////////////////////////////////////////////
73t_irc t_rnxObsFile::t_rnxObsHeader::read(QTextStream* stream, int maxLines) {
74 int numLines = 0;
75 while ( stream->status() == QTextStream::Ok && !stream->atEnd() ) {
76 QString line = stream->readLine(); ++ numLines;
77 if (line.isEmpty()) {
78 continue;
79 }
80 if (line.indexOf("END OF FILE") != -1) {
81 break;
82 }
83 QString value = line.mid(0,60).trimmed();
84 QString key = line.mid(60).trimmed();
85 if (key == "END OF HEADER") {
86 break;
87 }
88 else if (key == "RINEX VERSION / TYPE") {
89 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
90 in >> _version;
91 }
92 else if (key == "MARKER NAME") {
93 _markerName = value;
94 }
95 else if (key == "ANT # / TYPE") {
96 _antennaName = line.mid(20,20).trimmed();
97 }
98 else if (key == "INTERVAL") {
99 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
100 in >> _interval;
101 }
102 else if (key == "WAVELENGTH FACT L1/2") {
103 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
104 int wlFactL1 = 0;
105 int wlFactL2 = 0;
106 int numSat = 0;
107 in >> wlFactL1 >> wlFactL2 >> numSat;
108 if (numSat == 0) {
109 for (unsigned iPrn = 1; iPrn <= MAXPRN_GPS; iPrn++) {
110 _wlFactorsL1[iPrn] = wlFactL1;
111 _wlFactorsL2[iPrn] = wlFactL2;
112 }
113 }
114 else {
115 for (int ii = 0; ii < numSat; ii++) {
116 QString prn; in >> prn;
117 if (prn[0] == 'G') {
118 int iPrn;
119 readInt(prn, 1, 2, iPrn);
120 _wlFactorsL1[iPrn] = wlFactL1;
121 _wlFactorsL2[iPrn] = wlFactL2;
122 }
123 }
124 }
125 }
126 else if (key == "APPROX POSITION XYZ") {
127 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
128 in >> _xyz[0] >> _xyz[1] >> _xyz[2];
129 }
130 else if (key == "ANTENNA: DELTA H/E/N") {
131 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
132 in >> _antNEU[2] >> _antNEU[1] >> _antNEU[0];
133 }
134 else if (key == "ANTENNA: DELTA X/Y/Z") {
135 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
136 in >> _antXYZ[0] >> _antXYZ[1] >> _antXYZ[2];
137 }
138 else if (key == "ANTENNA: B.SIGHT XYZ") {
139 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
140 in >> _antBSG[0] >> _antBSG[1] >> _antBSG[2];
141 }
142 else if (key == "# / TYPES OF OBSERV") {
143 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
144 int nTypes;
145 in >> nTypes;
146 _obsTypesV2.clear();
147 for (int ii = 0; ii < nTypes; ii++) {
148 QString hlp;
149 in >> hlp;
150 _obsTypesV2.push_back(hlp);
151 }
152 }
153 else if (key == "SYS / # / OBS TYPES") {
154 QTextStream* in = new QTextStream(value.toAscii(), QIODevice::ReadOnly);
155 char sys;
156 int nTypes;
157 *in >> sys >> nTypes;
158 _obsTypesV3[sys].clear();
159 for (int ii = 0; ii < nTypes; ii++) {
160 if (ii > 0 && ii % 13 == 0) {
161 line = stream->readLine(); ++numLines;
162 delete in;
163 in = new QTextStream(line.toAscii(), QIODevice::ReadOnly);
164 }
165 QString hlp;
166 *in >> hlp;
167 _obsTypesV3[sys].push_back(hlp);
168 }
169 delete in;
170 }
171 else if (key == "TIME OF FIRST OBS") {
172 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
173 int year, month, day, hour, min;
174 double sec;
175 in >> year >> month >> day >> hour >> min >> sec;
176 _startTime.set(year, month, day, hour, min, sec);
177 }
178 if (maxLines > 0 && numLines == maxLines) {
179 break;
180 }
181 }
182
183 return success;
184}
185
186// Number of Observation Types (satellite-system specific)
187////////////////////////////////////////////////////////////////////////////
188int t_rnxObsFile::t_rnxObsHeader::nTypes(char sys) const {
189 if (_version < 3.0) {
190 return _obsTypesV2.size();
191 }
192 else {
193 map<char, vector<QString> >::const_iterator it = _obsTypesV3.find(sys);
194 if (it != _obsTypesV3.end()) {
195 return it->second.size();
196 }
197 else {
198 return 0;
199 }
200 }
201}
202
203// Observation Type (satellite-system specific)
204////////////////////////////////////////////////////////////////////////////
205const QString& t_rnxObsFile::t_rnxObsHeader::obsType(char sys, int index) const {
206 if (_version < 3.0) {
207 return _obsTypesV2.at(index);
208 }
209 else {
210 map<char, vector<QString> >::const_iterator it = _obsTypesV3.find(sys);
211 if (it != _obsTypesV3.end()) {
212 return it->second.at(index);
213 }
214 else {
215 return _emptyStr;
216 }
217 }
218}
219
220// Constructor
221////////////////////////////////////////////////////////////////////////////
222t_rnxObsFile::t_rnxObsFile(const QString& fileName) {
223 _stream = 0;
224 _flgPowerFail = false;
225 open(fileName);
226}
227
228// Open
229////////////////////////////////////////////////////////////////////////////
230void t_rnxObsFile::open(const QString& fileName) {
231
232 _fileName = fileName; expandEnvVar(_fileName);
233 _file = new QFile(_fileName);
234 _file->open(QIODevice::ReadOnly | QIODevice::Text);
235 _stream = new QTextStream();
236 _stream->setDevice(_file);
237
238 _header.read(_stream);
239
240 // Guess Observation Interval
241 // --------------------------
242 if (_header._interval == 0.0) {
243 bncTime ttPrev;
244 for (int iEpo = 0; iEpo < 10; iEpo++) {
245 const t_rnxEpo* rnxEpo = nextEpoch();
246 if (!rnxEpo) {
247 throw QString("t_rnxObsFile: not enough epochs");
248 }
249 if (iEpo > 0) {
250 double dt = rnxEpo->tt - ttPrev;
251 if (_header._interval == 0.0 || dt < _header._interval) {
252 _header._interval = dt;
253 }
254 }
255 ttPrev = rnxEpo->tt;
256 }
257 _stream->seek(0);
258 _header.read(_stream);
259 }
260
261 // Time of first observation
262 // -------------------------
263 if (!_header._startTime.valid()) {
264 const t_rnxEpo* rnxEpo = nextEpoch();
265 if (!rnxEpo) {
266 throw QString("t_rnxObsFile: not enough epochs");
267 }
268 _header._startTime = rnxEpo->tt;
269 _stream->seek(0);
270 _header.read(_stream);
271 }
272}
273
274// Destructor
275////////////////////////////////////////////////////////////////////////////
276t_rnxObsFile::~t_rnxObsFile() {
277 close();
278}
279
280// Close
281////////////////////////////////////////////////////////////////////////////
282void t_rnxObsFile::close() {
283 delete _stream; _stream = 0;
284 delete _file; _file = 0;
285}
286
287// Handle Special Epoch Flag
288////////////////////////////////////////////////////////////////////////////
289void t_rnxObsFile::handleEpochFlag(int flag, const QString& line) {
290
291 // Power Failure
292 // -------------
293 if (flag == 1) {
294 _flgPowerFail = true;
295 }
296
297 // Start moving antenna
298 // --------------------
299 else if (flag == 2) {
300 // no action
301 }
302
303 // Re-Read Header
304 // --------------
305 else if (flag == 3 || flag == 4) {
306 int numLines = 0;
307 if (version() < 3.0) {
308 readInt(line, 29, 3, numLines);
309 }
310 else {
311 readInt(line, 32, 3, numLines);
312 }
313 _header.read(_stream, numLines);
314 }
315
316 // Unhandled Flag
317 // --------------
318 else {
319 throw QString("t_rnxObsFile: unhandled flag\n" + line);
320 }
321}
322
323// Retrieve single Epoch
324////////////////////////////////////////////////////////////////////////////
325const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpoch() {
326
327 _currEpo.clear();
328
329 if (version() < 3.0) {
330 return nextEpochV2();
331 }
332 else {
333 return nextEpochV3();
334 }
335}
336
337// Retrieve single Epoch (RINEX Version 3)
338////////////////////////////////////////////////////////////////////////////
339const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV3() {
340
341 while ( _stream->status() == QTextStream::Ok && !_stream->atEnd() ) {
342
343 QString line = _stream->readLine();
344
345 if (line.isEmpty()) {
346 continue;
347 }
348
349 int flag = 0;
350 readInt(line, 31, 1, flag);
351 if (flag > 0) {
352 handleEpochFlag(flag, line);
353 continue;
354 }
355
356 QTextStream in(line.mid(1).toAscii(), QIODevice::ReadOnly);
357
358 // Epoch Time
359 // ----------
360 int year, month, day, hour, min;
361 double sec;
362 in >> year >> month >> day >> hour >> min >> sec;
363 _currEpo.tt.set(year, month, day, hour, min, sec);
364
365 // Number of Satellites
366 // --------------------
367 int numSat;
368 readInt(line, 32, 3, numSat);
369
370 _currEpo.rnxSat.resize(numSat);
371
372 // Observations
373 // ------------
374 for (int iSat = 0; iSat < numSat; iSat++) {
375 line = _stream->readLine();
376 _currEpo.rnxSat[iSat].satSys = line.toAscii()[0];
377 readInt(line, 1, 2, _currEpo.rnxSat[iSat].satNum);
378 char sys = line.toAscii()[0];
379 for (int iType = 0; iType < _header.nTypes(sys); iType++) {
380 int pos = 3 + 16*iType;
381 double obsValue = 0.0;
382 int lli = 0;
383 int snr = 0;
384 readDbl(line, pos, 14, obsValue);
385 readInt(line, pos + 14, 1, lli);
386 readInt(line, pos + 15, 1, snr);
387
388 if (_flgPowerFail) {
389 lli |= 1;
390 }
391
392 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
393 _currEpo.rnxSat[iSat].lli.push_back(lli);
394 _currEpo.rnxSat[iSat].snr.push_back(snr);
395 }
396 }
397
398 _flgPowerFail = false;
399
400 return &_currEpo;
401 }
402
403 return 0;
404}
405
406// Retrieve single Epoch (RINEX Version 2)
407////////////////////////////////////////////////////////////////////////////
408const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV2() {
409
410 while ( _stream->status() == QTextStream::Ok && !_stream->atEnd() ) {
411
412 QString line = _stream->readLine();
413
414 if (line.isEmpty()) {
415 continue;
416 }
417
418 int flag = 0;
419 readInt(line, 28, 1, flag);
420 if (flag > 0) {
421 handleEpochFlag(flag, line);
422 continue;
423 }
424
425 QTextStream in(line.toAscii(), QIODevice::ReadOnly);
426
427 // Epoch Time
428 // ----------
429 int year, month, day, hour, min;
430 double sec;
431 in >> year >> month >> day >> hour >> min >> sec;
432 if (year < 80) {
433 year += 2000;
434 }
435 else if (year < 100) {
436 year += 1900;
437 }
438 _currEpo.tt.set(year, month, day, hour, min, sec);
439
440 // Number of Satellites
441 // --------------------
442 int numSat;
443 readInt(line, 29, 3, numSat);
444
445 _currEpo.rnxSat.resize(numSat);
446
447 // Read Satellite Numbers
448 // ----------------------
449 int pos = 32;
450 for (int iSat = 0; iSat < numSat; iSat++) {
451 if (iSat > 0 && iSat % 12 == 0) {
452 line = _stream->readLine();
453 pos = 32;
454 }
455
456 _currEpo.rnxSat[iSat].satSys = line.toAscii()[pos];
457 readInt(line, pos + 1, 2, _currEpo.rnxSat[iSat].satNum);
458
459 pos += 3;
460 }
461
462 // Read Observation Records
463 // ------------------------
464 for (int iSat = 0; iSat < numSat; iSat++) {
465 line = _stream->readLine();
466 pos = 0;
467 for (int iType = 0; iType < _header.nTypes(_currEpo.rnxSat[iSat].satSys); iType++) {
468 if (iType > 0 && iType % 5 == 0) {
469 line = _stream->readLine();
470 pos = 0;
471 }
472 double obsValue = 0.0;
473 int lli = 0;
474 int snr = 0;
475 readDbl(line, pos, 14, obsValue);
476 readInt(line, pos + 14, 1, lli);
477 readInt(line, pos + 15, 1, snr);
478
479 if (_flgPowerFail) {
480 lli |= 1;
481 }
482
483 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
484 _currEpo.rnxSat[iSat].lli.push_back(lli);
485 _currEpo.rnxSat[iSat].snr.push_back(snr);
486
487 pos += 16;
488 }
489 }
490
491 _flgPowerFail = false;
492
493 return &_currEpo;
494 }
495
496 return 0;
497}
498
Note: See TracBrowser for help on using the repository browser.