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

Last change on this file since 3844 was 3844, checked in by mervart, 12 years ago
File size: 14.5 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, e_inpOut inpOut) {
223 _inpOut = inpOut;
224 _stream = 0;
225 _flgPowerFail = false;
226 if (_inpOut == input) {
227 openRead(fileName);
228 }
229 else {
230 }
231}
232
233// Open
234////////////////////////////////////////////////////////////////////////////
235void t_rnxObsFile::openRead(const QString& fileName) {
236
237 _fileName = fileName; expandEnvVar(_fileName);
238 _file = new QFile(_fileName);
239 _file->open(QIODevice::ReadOnly | QIODevice::Text);
240 _stream = new QTextStream();
241 _stream->setDevice(_file);
242
243 _header.read(_stream);
244
245 // Guess Observation Interval
246 // --------------------------
247 if (_header._interval == 0.0) {
248 bncTime ttPrev;
249 for (int iEpo = 0; iEpo < 10; iEpo++) {
250 const t_rnxEpo* rnxEpo = nextEpoch();
251 if (!rnxEpo) {
252 throw QString("t_rnxObsFile: not enough epochs");
253 }
254 if (iEpo > 0) {
255 double dt = rnxEpo->tt - ttPrev;
256 if (_header._interval == 0.0 || dt < _header._interval) {
257 _header._interval = dt;
258 }
259 }
260 ttPrev = rnxEpo->tt;
261 }
262 _stream->seek(0);
263 _header.read(_stream);
264 }
265
266 // Time of first observation
267 // -------------------------
268 if (!_header._startTime.valid()) {
269 const t_rnxEpo* rnxEpo = nextEpoch();
270 if (!rnxEpo) {
271 throw QString("t_rnxObsFile: not enough epochs");
272 }
273 _header._startTime = rnxEpo->tt;
274 _stream->seek(0);
275 _header.read(_stream);
276 }
277}
278
279// Destructor
280////////////////////////////////////////////////////////////////////////////
281t_rnxObsFile::~t_rnxObsFile() {
282 close();
283}
284
285// Close
286////////////////////////////////////////////////////////////////////////////
287void t_rnxObsFile::close() {
288 delete _stream; _stream = 0;
289 delete _file; _file = 0;
290}
291
292// Handle Special Epoch Flag
293////////////////////////////////////////////////////////////////////////////
294void t_rnxObsFile::handleEpochFlag(int flag, const QString& line) {
295
296 // Power Failure
297 // -------------
298 if (flag == 1) {
299 _flgPowerFail = true;
300 }
301
302 // Start moving antenna
303 // --------------------
304 else if (flag == 2) {
305 // no action
306 }
307
308 // Re-Read Header
309 // --------------
310 else if (flag == 3 || flag == 4) {
311 int numLines = 0;
312 if (version() < 3.0) {
313 readInt(line, 29, 3, numLines);
314 }
315 else {
316 readInt(line, 32, 3, numLines);
317 }
318 _header.read(_stream, numLines);
319 }
320
321 // Unhandled Flag
322 // --------------
323 else {
324 throw QString("t_rnxObsFile: unhandled flag\n" + line);
325 }
326}
327
328// Retrieve single Epoch
329////////////////////////////////////////////////////////////////////////////
330const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpoch() {
331
332 _currEpo.clear();
333
334 if (version() < 3.0) {
335 return nextEpochV2();
336 }
337 else {
338 return nextEpochV3();
339 }
340}
341
342// Retrieve single Epoch (RINEX Version 3)
343////////////////////////////////////////////////////////////////////////////
344const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV3() {
345
346 while ( _stream->status() == QTextStream::Ok && !_stream->atEnd() ) {
347
348 QString line = _stream->readLine();
349
350 if (line.isEmpty()) {
351 continue;
352 }
353
354 int flag = 0;
355 readInt(line, 31, 1, flag);
356 if (flag > 0) {
357 handleEpochFlag(flag, line);
358 continue;
359 }
360
361 QTextStream in(line.mid(1).toAscii(), QIODevice::ReadOnly);
362
363 // Epoch Time
364 // ----------
365 int year, month, day, hour, min;
366 double sec;
367 in >> year >> month >> day >> hour >> min >> sec;
368 _currEpo.tt.set(year, month, day, hour, min, sec);
369
370 // Number of Satellites
371 // --------------------
372 int numSat;
373 readInt(line, 32, 3, numSat);
374
375 _currEpo.rnxSat.resize(numSat);
376
377 // Observations
378 // ------------
379 for (int iSat = 0; iSat < numSat; iSat++) {
380 line = _stream->readLine();
381 _currEpo.rnxSat[iSat].satSys = line.toAscii()[0];
382 readInt(line, 1, 2, _currEpo.rnxSat[iSat].satNum);
383 char sys = line.toAscii()[0];
384 for (int iType = 0; iType < _header.nTypes(sys); iType++) {
385 int pos = 3 + 16*iType;
386 double obsValue = 0.0;
387 int lli = 0;
388 int snr = 0;
389 readDbl(line, pos, 14, obsValue);
390 readInt(line, pos + 14, 1, lli);
391 readInt(line, pos + 15, 1, snr);
392
393 if (_flgPowerFail) {
394 lli |= 1;
395 }
396
397 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
398 _currEpo.rnxSat[iSat].lli.push_back(lli);
399 _currEpo.rnxSat[iSat].snr.push_back(snr);
400 }
401 }
402
403 _flgPowerFail = false;
404
405 return &_currEpo;
406 }
407
408 return 0;
409}
410
411// Retrieve single Epoch (RINEX Version 2)
412////////////////////////////////////////////////////////////////////////////
413const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV2() {
414
415 while ( _stream->status() == QTextStream::Ok && !_stream->atEnd() ) {
416
417 QString line = _stream->readLine();
418
419 if (line.isEmpty()) {
420 continue;
421 }
422
423 int flag = 0;
424 readInt(line, 28, 1, flag);
425 if (flag > 0) {
426 handleEpochFlag(flag, line);
427 continue;
428 }
429
430 QTextStream in(line.toAscii(), QIODevice::ReadOnly);
431
432 // Epoch Time
433 // ----------
434 int year, month, day, hour, min;
435 double sec;
436 in >> year >> month >> day >> hour >> min >> sec;
437 if (year < 80) {
438 year += 2000;
439 }
440 else if (year < 100) {
441 year += 1900;
442 }
443 _currEpo.tt.set(year, month, day, hour, min, sec);
444
445 // Number of Satellites
446 // --------------------
447 int numSat;
448 readInt(line, 29, 3, numSat);
449
450 _currEpo.rnxSat.resize(numSat);
451
452 // Read Satellite Numbers
453 // ----------------------
454 int pos = 32;
455 for (int iSat = 0; iSat < numSat; iSat++) {
456 if (iSat > 0 && iSat % 12 == 0) {
457 line = _stream->readLine();
458 pos = 32;
459 }
460
461 _currEpo.rnxSat[iSat].satSys = line.toAscii()[pos];
462 readInt(line, pos + 1, 2, _currEpo.rnxSat[iSat].satNum);
463
464 pos += 3;
465 }
466
467 // Read Observation Records
468 // ------------------------
469 for (int iSat = 0; iSat < numSat; iSat++) {
470 line = _stream->readLine();
471 pos = 0;
472 for (int iType = 0; iType < _header.nTypes(_currEpo.rnxSat[iSat].satSys); iType++) {
473 if (iType > 0 && iType % 5 == 0) {
474 line = _stream->readLine();
475 pos = 0;
476 }
477 double obsValue = 0.0;
478 int lli = 0;
479 int snr = 0;
480 readDbl(line, pos, 14, obsValue);
481 readInt(line, pos + 14, 1, lli);
482 readInt(line, pos + 15, 1, snr);
483
484 if (_flgPowerFail) {
485 lli |= 1;
486 }
487
488 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
489 _currEpo.rnxSat[iSat].lli.push_back(lli);
490 _currEpo.rnxSat[iSat].snr.push_back(snr);
491
492 pos += 16;
493 }
494 }
495
496 _flgPowerFail = false;
497
498 return &_currEpo;
499 }
500
501 return 0;
502}
503
504// Set Header Information
505////////////////////////////////////////////////////////////////////////////
506void t_rnxObsFile::setHeader(const t_rnxObsHeader& header) {
507 _header._version = header._version;
508 _header._interval = header._interval;
509 _header._antennaName = header._antennaName;
510 _header._markerName = header._markerName;
511 _header._antNEU = header._antNEU;
512 _header._antXYZ = header._antXYZ;
513 _header._antBSG = header._antBSG;
514 _header._xyz = header._xyz;
515 for (unsigned iPrn = 1; iPrn <= MAXPRN_GPS; iPrn++) {
516 _header._wlFactorsL1[iPrn] = header._wlFactorsL1[iPrn];
517 _header._wlFactorsL2[iPrn] = header._wlFactorsL2[iPrn];
518 }
519 _header._startTime = header._startTime;
520 for (unsigned ii = 0; ii < header._obsTypesV2.size(); ii++) {
521 _header._obsTypesV2.push_back(header._obsTypesV2[ii]);
522 }
523 map<char, vector<QString> >::const_iterator it;
524 for (it = header._obsTypesV3.begin(); it != header._obsTypesV3.end(); it++) {
525 char sys = it->first;
526 const vector<QString>& typesV3 = it->second;
527 for (unsigned ii = 0; ii < typesV3.size(); ii++) {
528 _header._obsTypesV3[sys].push_back(typesV3[ii]);
529 }
530 }
531}
Note: See TracBrowser for help on using the repository browser.