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

Last change on this file since 3718 was 3718, checked in by mervart, 12 years ago
File size: 12.6 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 "rinex/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 if (maxLines > 0 && numLines == maxLines) {
172 break;
173 }
174 }
175
176 return success;
177}
178
179// Number of Observation Types (satellite-system specific)
180////////////////////////////////////////////////////////////////////////////
181int t_rnxObsFile::t_rnxObsHeader::nTypes(char sys) const {
182 if (_version < 3.0) {
183 return _obsTypesV2.size();
184 }
185 else {
186 map<char, vector<QString> >::const_iterator it = _obsTypesV3.find(sys);
187 if (it != _obsTypesV3.end()) {
188 return it->second.size();
189 }
190 else {
191 return 0;
192 }
193 }
194}
195
196// Observation Type (satellite-system specific)
197////////////////////////////////////////////////////////////////////////////
198const QString& t_rnxObsFile::t_rnxObsHeader::obsType(char sys, int index) const {
199 if (_version < 3.0) {
200 return _obsTypesV2.at(index);
201 }
202 else {
203 map<char, vector<QString> >::const_iterator it = _obsTypesV3.find(sys);
204 if (it != _obsTypesV3.end()) {
205 return it->second.at(index);
206 }
207 else {
208 return _emptyStr;
209 }
210 }
211}
212
213// Constructor
214////////////////////////////////////////////////////////////////////////////
215t_rnxObsFile::t_rnxObsFile(const QString& fileName) {
216 _stream = 0;
217 _flgPowerFail = false;
218 open(fileName);
219}
220
221// Open
222////////////////////////////////////////////////////////////////////////////
223void t_rnxObsFile::open(const QString& fileName) {
224
225 _fileName = fileName; expandEnvVar(_fileName);
226 _file = new QFile(_fileName);
227 _file->open(QIODevice::ReadOnly | QIODevice::Text);
228 _stream = new QTextStream();
229 _stream->setDevice(_file);
230
231 _header.read(_stream);
232
233 // Guess Observation Interval
234 // --------------------------
235 if (_header._interval == 0.0) {
236 bncTime ttPrev;
237 for (int iEpo = 0; iEpo < 10; iEpo++) {
238 const t_rnxEpo* rnxEpo = nextEpoch();
239 if (!rnxEpo) {
240 throw QString("t_rnxObsFile: not enough epochs");
241 }
242 if (iEpo > 0) {
243 double dt = rnxEpo->tt - ttPrev;
244 if (_header._interval == 0.0 || dt < _header._interval) {
245 _header._interval = dt;
246 }
247 }
248 ttPrev = rnxEpo->tt;
249 }
250 _stream->clear();
251 _stream->seekg(0, ios::beg);
252 _header.read(_stream);
253 }
254}
255
256// Destructor
257////////////////////////////////////////////////////////////////////////////
258t_rnxObsFile::~t_rnxObsFile() {
259 close();
260}
261
262// Close
263////////////////////////////////////////////////////////////////////////////
264void t_rnxObsFile::close() {
265 delete _stream; _stream = 0;
266 delete _file; _file = 0;
267}
268
269// Handle Special Epoch Flag
270////////////////////////////////////////////////////////////////////////////
271void t_rnxObsFile::handleEpochFlag(int flag, const QString& line) {
272
273 // Power Failure
274 // -------------
275 if (flag == 1) {
276 _flgPowerFail = true;
277 }
278
279 // Start moving antenna
280 // --------------------
281 else if (flag == 2) {
282 // no action
283 }
284
285 // Re-Read Header
286 // --------------
287 else if (flag == 3 || flag == 4) {
288 int numLines = 0;
289 if (version() < 3.0) {
290 readInt(line, 29, 3, numLines);
291 }
292 else {
293 readInt(line, 32, 3, numLines);
294 }
295 _header.read(_stream, numLines);
296 }
297
298 // Unhandled Flag
299 // --------------
300 else {
301 throw QString("t_rnxObsFile: unhandled flag\n" + line);
302 }
303}
304
305// Retrieve single Epoch
306////////////////////////////////////////////////////////////////////////////
307const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpoch() {
308
309 _currEpo.clear();
310
311 if (version() < 3.0) {
312 return nextEpochV2();
313 }
314 else {
315 return nextEpochV3();
316 }
317}
318
319// Retrieve single Epoch (RINEX Version 3)
320////////////////////////////////////////////////////////////////////////////
321const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV3() {
322
323 while ( _stream->->status() == QTextStream::Ok && !_stream->atEnd() ) {
324
325 QString line = _stream->readLine();
326
327 if (line.isEmpty()) {
328 continue;
329 }
330
331 int flag = 0;
332 readInt(line, 31, 1, flag);
333 if (flag > 0) {
334 handleEpochFlag(flag, line);
335 continue;
336 }
337
338 QTextStream in(line.mid(1).toAscii(), QIODevice::ReadOnly);
339
340 // Epoch Time
341 // ----------
342 int year, month, day, hour, min;
343 double sec;
344 in >> year >> month >> day >> hour >> min >> sec;
345 _currEpo.tt.set(hour, min, sec, day, month, year);
346
347 // Number of Satellites
348 // --------------------
349 int numSat;
350 readInt(line, 32, 3, numSat);
351
352 _currEpo.rnxSat.resize(numSat);
353
354 // Observations
355 // ------------
356 for (int iSat = 0; iSat < numSat; iSat++) {
357 line = _stream->readLine();
358 _currEpo.rnxSat[iSat].satSys = line[0];
359 readInt(line, 1, 2, _currEpo.rnxSat[iSat].satNum);
360 char sys = line[0];
361 for (int iType = 0; iType < _header.nTypes(sys); iType++) {
362 int pos = 3 + 16*iType;
363 double obsValue = 0.0;
364 int lli = 0;
365 int snr = 0;
366 readDbl(line, pos, 14, obsValue);
367 readInt(line, pos + 14, 1, lli);
368 readInt(line, pos + 15, 1, snr);
369
370 if (_flgPowerFail) {
371 lli |= 1;
372 }
373
374 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
375 _currEpo.rnxSat[iSat].lli.push_back(lli);
376 _currEpo.rnxSat[iSat].snr.push_back(snr);
377 }
378 }
379
380 _flgPowerFail = false;
381
382 return &_currEpo;
383 }
384
385 return 0;
386}
387
388// Retrieve single Epoch (RINEX Version 2)
389////////////////////////////////////////////////////////////////////////////
390const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV2() {
391
392 while ( _stream->->status() == QTextStream::Ok && !_stream->atEnd() ) {
393
394 QString line = _stream->readLine();
395
396 if (line.isEmpty()) {
397 continue;
398 }
399
400 int flag = 0;
401 readInt(line, 28, 1, flag);
402 if (flag > 0) {
403 handleEpochFlag(flag, line);
404 continue;
405 }
406
407 QTextStream in(line.toAscii(), QIODevice::ReadOnly);
408
409 // Epoch Time
410 // ----------
411 int year, month, day, hour, min;
412 double sec;
413 in >> year >> month >> day >> hour >> min >> sec;
414 if (year < 80) {
415 year += 2000;
416 }
417 else if (year < 100) {
418 year += 1900;
419 }
420 _currEpo.tt.set(hour, min, sec, day, month, year);
421
422 // Number of Satellites
423 // --------------------
424 int numSat;
425 readInt(line, 29, 3, numSat);
426
427 _currEpo.rnxSat.resize(numSat);
428
429 // Read Satellite Numbers
430 // ----------------------
431 int pos = 32;
432 for (int iSat = 0; iSat < numSat; iSat++) {
433 if (iSat > 0 && iSat % 12 == 0) {
434 line = _stream->readLine();
435 pos = 32;
436 }
437
438 _currEpo.rnxSat[iSat].satSys = line[pos];
439 readInt(line, pos + 1, 2, _currEpo.rnxSat[iSat].satNum);
440
441 pos += 3;
442 }
443
444 // Read Observation Records
445 // ------------------------
446 for (int iSat = 0; iSat < numSat; iSat++) {
447 line = _stream->readLine();
448 pos = 0;
449 for (int iType = 0; iType < _header.nTypes(_currEpo.rnxSat[iSat].satSys); iType++) {
450 if (iType > 0 && iType % 5 == 0) {
451 line = _stream->readLine();
452 pos = 0;
453 }
454 double obsValue = 0.0;
455 int lli = 0;
456 int snr = 0;
457 readDbl(line, pos, 14, obsValue);
458 readInt(line, pos + 14, 1, lli);
459 readInt(line, pos + 15, 1, snr);
460
461 if (_flgPowerFail) {
462 lli |= 1;
463 }
464
465 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
466 _currEpo.rnxSat[iSat].lli.push_back(lli);
467 _currEpo.rnxSat[iSat].snr.push_back(snr);
468
469 pos += 16;
470 }
471 }
472
473 _flgPowerFail = false;
474
475 return &_currEpo;
476 }
477
478 return 0;
479}
480
Note: See TracBrowser for help on using the repository browser.