source: ntrip/trunk/BNC/src/rinex/rnxobsfile.cpp@ 6541

Last change on this file since 6541 was 6541, checked in by mervart, 9 years ago
File size: 35.5 KB
RevLine 
[3716]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>
[3722]44#include "rnxobsfile.h"
[3716]45#include "bncutils.h"
[5070]46#include "bnccore.h"
[6242]47#include "bncsettings.h"
[3716]48
49using namespace std;
50
51// Constructor
52////////////////////////////////////////////////////////////////////////////
[4480]53t_rnxObsHeader::t_rnxObsHeader() {
[6247]54 _usedSystems = "GREJCS";
[3716]55 _antNEU.ReSize(3); _antNEU = 0.0;
56 _antXYZ.ReSize(3); _antXYZ = 0.0;
57 _antBSG.ReSize(3); _antBSG = 0.0;
58 _xyz.ReSize(3); _xyz = 0.0;
59 _version = 0.0;
60 _interval = 0.0;
[5742]61 for (unsigned iPrn = 1; iPrn <= t_prn::MAXPRN_GPS; iPrn++) {
[3716]62 _wlFactorsL1[iPrn] = 1;
63 _wlFactorsL2[iPrn] = 1;
64 }
65}
66
67// Destructor
68////////////////////////////////////////////////////////////////////////////
[4480]69t_rnxObsHeader::~t_rnxObsHeader() {
[3716]70}
71
72// Read Header
73////////////////////////////////////////////////////////////////////////////
[4480]74t_irc t_rnxObsHeader::read(QTextStream* stream, int maxLines) {
[4534]75 _comments.clear();
[3716]76 int numLines = 0;
[3718]77 while ( stream->status() == QTextStream::Ok && !stream->atEnd() ) {
78 QString line = stream->readLine(); ++ numLines;
79 if (line.isEmpty()) {
[3716]80 continue;
81 }
[3718]82 if (line.indexOf("END OF FILE") != -1) {
[3716]83 break;
84 }
[3718]85 QString value = line.mid(0,60).trimmed();
86 QString key = line.mid(60).trimmed();
[3716]87 if (key == "END OF HEADER") {
88 break;
89 }
90 else if (key == "RINEX VERSION / TYPE") {
[3718]91 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
[3716]92 in >> _version;
93 }
94 else if (key == "MARKER NAME") {
95 _markerName = value;
96 }
[3930]97 else if (key == "MARKER NUMBER") {
98 _markerNumber = line.mid(0,20).trimmed();
99 }
[3716]100 else if (key == "ANT # / TYPE") {
[3930]101 _antennaNumber = line.mid( 0,20).trimmed();
102 _antennaName = line.mid(20,20).trimmed();
[3716]103 }
[3930]104 else if (key == "OBSERVER / AGENCY") {
105 _observer = line.mid( 0,20).trimmed();
106 _agency = line.mid(20,40).trimmed();
107 }
108 else if (key == "REC # / TYPE / VERS") {
109 _receiverNumber = line.mid( 0,20).trimmed();
110 _receiverType = line.mid(20,20).trimmed();
111 _receiverVersion = line.mid(40,20).trimmed();
112 }
[3716]113 else if (key == "INTERVAL") {
[3718]114 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
[3716]115 in >> _interval;
116 }
[4494]117 else if (key == "COMMENT") {
118 _comments << line.mid(0,60).trimmed();
119 }
[3716]120 else if (key == "WAVELENGTH FACT L1/2") {
[3718]121 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
[3716]122 int wlFactL1 = 0;
123 int wlFactL2 = 0;
124 int numSat = 0;
125 in >> wlFactL1 >> wlFactL2 >> numSat;
126 if (numSat == 0) {
[5742]127 for (unsigned iPrn = 1; iPrn <= t_prn::MAXPRN_GPS; iPrn++) {
[3716]128 _wlFactorsL1[iPrn] = wlFactL1;
129 _wlFactorsL2[iPrn] = wlFactL2;
130 }
131 }
132 else {
133 for (int ii = 0; ii < numSat; ii++) {
[3718]134 QString prn; in >> prn;
[3716]135 if (prn[0] == 'G') {
136 int iPrn;
137 readInt(prn, 1, 2, iPrn);
138 _wlFactorsL1[iPrn] = wlFactL1;
139 _wlFactorsL2[iPrn] = wlFactL2;
140 }
141 }
142 }
143 }
144 else if (key == "APPROX POSITION XYZ") {
[3718]145 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
[3716]146 in >> _xyz[0] >> _xyz[1] >> _xyz[2];
147 }
148 else if (key == "ANTENNA: DELTA H/E/N") {
[3718]149 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
[3716]150 in >> _antNEU[2] >> _antNEU[1] >> _antNEU[0];
151 }
152 else if (key == "ANTENNA: DELTA X/Y/Z") {
[3718]153 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
[3716]154 in >> _antXYZ[0] >> _antXYZ[1] >> _antXYZ[2];
155 }
156 else if (key == "ANTENNA: B.SIGHT XYZ") {
[3718]157 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
[3716]158 in >> _antBSG[0] >> _antBSG[1] >> _antBSG[2];
159 }
160 else if (key == "# / TYPES OF OBSERV") {
[6223]161 if (_version == 0.0) {
162 _version = t_rnxObsHeader::defaultRnxObsVersion2;
163 }
[3864]164 QTextStream* in = new QTextStream(value.toAscii(), QIODevice::ReadOnly);
[3716]165 int nTypes;
[3864]166 *in >> nTypes;
[6245]167 char sys0 = _usedSystems[0].toAscii();
[6125]168 _obsTypes[sys0].clear();
[3716]169 for (int ii = 0; ii < nTypes; ii++) {
[3864]170 if (ii > 0 && ii % 9 == 0) {
171 line = stream->readLine(); ++numLines;
172 delete in;
[4152]173 in = new QTextStream(line.left(60).toAscii(), QIODevice::ReadOnly);
[3864]174 }
[3718]175 QString hlp;
[3864]176 *in >> hlp;
[6125]177 _obsTypes[sys0].append(hlp);
[3716]178 }
[6245]179 for (int ii = 1; ii < _usedSystems.length(); ii++) {
180 char sysI = _usedSystems[ii].toAscii();
[6125]181 _obsTypes[sysI] = _obsTypes[sys0];
[6119]182 }
[3716]183 }
184 else if (key == "SYS / # / OBS TYPES") {
[6223]185 if (_version == 0.0) {
[6228]186 _version = t_rnxObsHeader::defaultRnxObsVersion3;
[6223]187 }
[3718]188 QTextStream* in = new QTextStream(value.toAscii(), QIODevice::ReadOnly);
[3716]189 char sys;
190 int nTypes;
191 *in >> sys >> nTypes;
[6119]192 _obsTypes[sys].clear();
[3716]193 for (int ii = 0; ii < nTypes; ii++) {
194 if (ii > 0 && ii % 13 == 0) {
[3718]195 line = stream->readLine(); ++numLines;
[3716]196 delete in;
[3718]197 in = new QTextStream(line.toAscii(), QIODevice::ReadOnly);
[3716]198 }
[3718]199 QString hlp;
[3716]200 *in >> hlp;
[6541]201 if (sys == 'C') {
202 hlp.replace('2', '1');
203 }
[6119]204 _obsTypes[sys].push_back(hlp);
[3716]205 }
206 delete in;
207 }
[3837]208 else if (key == "TIME OF FIRST OBS") {
209 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
210 int year, month, day, hour, min;
211 double sec;
212 in >> year >> month >> day >> hour >> min >> sec;
213 _startTime.set(year, month, day, hour, min, sec);
214 }
[3716]215 if (maxLines > 0 && numLines == maxLines) {
216 break;
217 }
218 }
219
[6245]220 // Systems used
221 // ------------
222 _usedSystems.clear();
223 QMapIterator<char, QStringList> it(_obsTypes);
224 while (it.hasNext()) {
225 it.next();
226 _usedSystems += QChar(it.key());
227 }
228
[3716]229 return success;
230}
231
[6222]232// Set Default Header
233////////////////////////////////////////////////////////////////////////////
234void t_rnxObsHeader::setDefault(const QString& markerName, int version) {
235
236 _markerName = markerName;
237
238 if (version <= 2) {
239 _version = t_rnxObsHeader::defaultRnxObsVersion2;
240 }
241 else {
242 _version = t_rnxObsHeader::defaultRnxObsVersion3;
243 }
244
245 _comments << "Default set of observation types used";
246
247 _obsTypes.clear();
248 if (_version < 3.0) {
249 _obsTypes['G'] << "C1" << "P1" << "L1" << "S1"
250 << "C2" << "P2" << "L2" << "S2";
251 _obsTypes['R'] = _obsTypes['G'];
252 _obsTypes['E'] = _obsTypes['G'];
253 _obsTypes['J'] = _obsTypes['G'];
254 _obsTypes['S'] = _obsTypes['G'];
255 _obsTypes['C'] = _obsTypes['G'];
256 }
257 else {
258 _obsTypes['G'] << "C1C" << "L1C" << "S1C"
259 << "C2W" << "L2W" << "S2W"
260 << "C5" << "L5" << "S5";
261
262 _obsTypes['J'] = _obsTypes['G'];
263
264 _obsTypes['R'] << "C1C" << "L1C" << "S1C"
265 << "C2P" << "L2P" << "S2P";
266
267 _obsTypes['E'] << "C1" << "L1" << "S1"
268 << "C5" << "L5" << "S5"
269 << "C7" << "L7" << "S7"
270 << "C8" << "L8" << "S8";
271
272 _obsTypes['S'] << "C1" << "L1" << "S1"
273 << "C5" << "L5" << "S5";
274
275 _obsTypes['C'] << "C1" << "L1" << "S1"
276 << "C6" << "L6" << "S6"
277 << "C7" << "L7" << "S7";
278 }
279}
280
281// Copy header
282////////////////////////////////////////////////////////////////////////////
283void t_rnxObsHeader::set(const t_rnxObsHeader& header, int version,
284 const QStringList* useObsTypes) {
285
286 if (version <= 2) {
287 _version = t_rnxObsHeader::defaultRnxObsVersion2;
288 }
289 else {
290 _version = t_rnxObsHeader::defaultRnxObsVersion3;
291 }
292 _interval = header._interval;
293 _antennaNumber = header._antennaNumber;
294 _antennaName = header._antennaName;
295 _markerName = header._markerName;
296 _markerNumber = header._markerNumber;
297 _antNEU = header._antNEU;
298 _antXYZ = header._antXYZ;
299 _antBSG = header._antBSG;
300 _xyz = header._xyz;
301 _observer = header._observer;
302 _agency = header._agency;
303 _receiverNumber = header._receiverNumber;
304 _receiverType = header._receiverType;
305 _receiverVersion = header._receiverVersion;
[6224]306 _startTime = header._startTime;
307 _comments = header._comments;
[6245]308 _usedSystems = header._usedSystems;
[6222]309 for (unsigned iPrn = 1; iPrn <= t_prn::MAXPRN_GPS; iPrn++) {
310 _wlFactorsL1[iPrn] = header._wlFactorsL1[iPrn];
311 _wlFactorsL2[iPrn] = header._wlFactorsL2[iPrn];
312 }
313
314 // Set observation types
315 // ---------------------
316 _obsTypes.clear();
317 if (!useObsTypes || useObsTypes->size() == 0) {
318 if (int(_version) == int(header._version)) {
319 _obsTypes = header._obsTypes;
320 }
321 else {
[6250]322 if (_version >= 3.0) {
323 for (int iSys = 0; iSys < header.numSys(); iSys++) {
324 char sys = header.system(iSys);
325 for (int iType = 0; iType < header.nTypes(sys); iType++) {
326 QString type = header.obsType(sys, iType, _version);
327 if (!_obsTypes[sys].contains(type)) {
328 _obsTypes[sys].push_back(type);
329 }
[6249]330 }
[6250]331 }
332 }
333 else {
334 for (int iSys = 0; iSys < header.numSys(); iSys++) {
335 char sys = header.system(iSys);
336 for (int iType = 0; iType < header.nTypes(sys); iType++) {
337 QString type = header.obsType(sys, iType, _version);
[6249]338 for (int jSys = 0; jSys < _usedSystems.length(); jSys++) {
339 char thisSys = _usedSystems[jSys].toAscii();
340 if (!_obsTypes[thisSys].contains(type)) {
341 _obsTypes[thisSys].push_back(type);
342 }
343 }
344 }
[6222]345 }
346 }
347 }
348 }
349 else {
350 for (int iType = 0; iType < useObsTypes->size(); iType++) {
351 if (useObsTypes->at(iType).indexOf(":") != -1) {
352 QStringList hlp = useObsTypes->at(iType).split(":", QString::SkipEmptyParts);
353 if (hlp.size() == 2 && hlp[0].length() == 1) {
[6248]354 if (_version >= 3.0) {
355 char sys = hlp[0][0].toAscii();
356 QString type = t_rnxObsFile::type2to3(sys, hlp[1]);
357 if (!_obsTypes[sys].contains(type)) {
358 _obsTypes[sys].push_back(type);
359 }
360 }
361 else {
362 for (int iSys = 0; iSys < _usedSystems.length(); iSys++) {
363 char sys = _usedSystems[iSys].toAscii();
364 QString type = t_rnxObsFile::type3to2(sys, hlp[1]);
365 if (!_obsTypes[sys].contains(type)) {
366 _obsTypes[sys].push_back(type);
367 }
368 }
369 }
[6222]370 }
371 }
372 else {
[6245]373 for (int iSys = 0; iSys < _usedSystems.length(); iSys++) {
[6248]374 char sys = _usedSystems[iSys].toAscii();
375 QString type = _version >= 3.0 ? t_rnxObsFile::type2to3(sys, useObsTypes->at(iType)) :
376 t_rnxObsFile::type3to2(sys, useObsTypes->at(iType));
377 if (!_obsTypes[sys].contains(type)) {
378 _obsTypes[sys].push_back(type);
379 }
[6222]380 }
381 }
382 }
[6245]383 _usedSystems.clear();
[6244]384 QMapIterator<char, QStringList> it(_obsTypes);
385 while (it.hasNext()) {
386 it.next();
[6245]387 _usedSystems += QChar(it.key());
[6244]388 }
[6222]389 }
390}
391
[4481]392// Write Header
393////////////////////////////////////////////////////////////////////////////
394void t_rnxObsHeader::write(QTextStream* stream,
395 const QMap<QString, QString>* txtMap) const {
396
[4493]397 QStringList newComments;
[5068]398 QString runBy = BNC_CORE->userName();
[4481]399
400 if (txtMap) {
401 QMapIterator<QString, QString> it(*txtMap);
402 while (it.hasNext()) {
403 it.next();
404 if (it.key() == "RUN BY") {
405 runBy = it.value();
406 }
407 else if (it.key() == "COMMENT") {
[4493]408 newComments = it.value().split("\\n", QString::SkipEmptyParts);
[4481]409 }
410 }
411 }
412
413 *stream << QString("%1 Observation data Mixed")
414 .arg(_version, 9, 'f', 2)
415 .leftJustified(60)
416 << "RINEX VERSION / TYPE\n";
417
418 const QString fmtDate = (_version < 3.0) ? "dd-MMM-yy hh:mm"
419 : "yyyyMMdd hhmmss UTC";
420 *stream << QString("%1%2%3")
[5068]421 .arg(BNC_CORE->pgmName(), -20)
[4481]422 .arg(runBy.trimmed().left(20), -20)
423 .arg(QDateTime::currentDateTime().toUTC().toString(fmtDate), -20)
424 .leftJustified(60)
425 << "PGM / RUN BY / DATE\n";
426
[4493]427 QStringListIterator itCmnt(_comments + newComments);
[4481]428 while (itCmnt.hasNext()) {
429 *stream << itCmnt.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
430 }
431
432 *stream << QString("%1")
433 .arg(_markerName, -60)
434 .leftJustified(60)
435 << "MARKER NAME\n";
436
437 if (!_markerNumber.isEmpty()) {
438 *stream << QString("%1")
439 .arg(_markerNumber, -20)
440 .leftJustified(60)
441 << "MARKER NUMBER\n";
442 }
443
444 *stream << QString("%1%2")
[4529]445 .arg(_observer, -20)
446 .arg(_agency, -40)
[4481]447 .leftJustified(60)
448 << "OBSERVER / AGENCY\n";
449
450 *stream << QString("%1%2%3")
[4529]451 .arg(_receiverNumber, -20)
452 .arg(_receiverType, -20)
453 .arg(_receiverVersion, -20)
[4481]454 .leftJustified(60)
455 << "REC # / TYPE / VERS\n";
456
457 *stream << QString("%1%2")
[4529]458 .arg(_antennaNumber, -20)
459 .arg(_antennaName, -20)
[4481]460 .leftJustified(60)
461 << "ANT # / TYPE\n";
462
463 *stream << QString("%1%2%3")
464 .arg(_xyz(1), 14, 'f', 4)
465 .arg(_xyz(2), 14, 'f', 4)
466 .arg(_xyz(3), 14, 'f', 4)
467 .leftJustified(60)
468 << "APPROX POSITION XYZ\n";
469
470 *stream << QString("%1%2%3")
471 .arg(_antNEU(3), 14, 'f', 4)
472 .arg(_antNEU(2), 14, 'f', 4)
473 .arg(_antNEU(1), 14, 'f', 4)
474 .leftJustified(60)
475 << "ANTENNA: DELTA H/E/N\n";
476
477 if (_version < 3.0) {
478 int defaultWlFact1 = _wlFactorsL1[1];
479 int defaultWlFact2 = _wlFactorsL2[1]; // TODO check all prns
480 *stream << QString("%1%2")
481 .arg(defaultWlFact1, 6)
482 .arg(defaultWlFact2, 6)
483 .leftJustified(60)
484 << "WAVELENGTH FACT L1/2\n";
485 }
486
487 *stream << obsTypesStrings().join("");
488
[4484]489 if (_interval > 0) {
490 *stream << QString("%1")
491 .arg(_interval, 10, 'f', 3)
492 .leftJustified(60)
493 << "INTERVAL\n";
494 }
[4481]495
496 unsigned year, month, day, hour, min;
497 double sec;
498 _startTime.civil_date(year, month, day);
499 _startTime.civil_time(hour, min, sec);
500 *stream << QString("%1%2%3%4%5%6%7")
501 .arg(year, 6)
502 .arg(month, 6)
503 .arg(day, 6)
504 .arg(hour, 6)
505 .arg(min, 6)
506 .arg(sec, 13, 'f', 7)
507 .arg("GPS", 8)
508 .leftJustified(60)
509 << "TIME OF FIRST OBS\n";
510
511 *stream << QString()
512 .leftJustified(60)
513 << "END OF HEADER\n";
514}
515
[6119]516// Number of Different Systems
517////////////////////////////////////////////////////////////////////////////
518int t_rnxObsHeader::numSys() const {
519 return _obsTypes.size();
520}
521
[6130]522//
523////////////////////////////////////////////////////////////////////////////
524char t_rnxObsHeader::system(int iSys) const {
525 int iSysLocal = -1;
[6230]526 QMapIterator<char, QStringList> it(_obsTypes);
[6130]527 while (it.hasNext()) {
528 ++iSysLocal;
529 it.next();
530 if (iSysLocal == iSys) {
531 return it.key();
532 }
533 }
534 return ' ';
535}
536
[3716]537// Number of Observation Types (satellite-system specific)
538////////////////////////////////////////////////////////////////////////////
[4480]539int t_rnxObsHeader::nTypes(char sys) const {
[6119]540 if (_obsTypes.contains(sys)) {
541 return _obsTypes[sys].size();
[3716]542 }
543 else {
[6119]544 return 0;
[3716]545 }
546}
547
548// Observation Type (satellite-system specific)
549////////////////////////////////////////////////////////////////////////////
[6126]550QString t_rnxObsHeader::obsType(char sys, int index, double version) const {
551
552 if (version == 0.0) {
553 version = _version;
554 }
[6119]555 if (_obsTypes.contains(sys)) {
[6126]556 QString origType = _obsTypes[sys].at(index);
557 if (int(version) == int(_version)) {
558 return origType;
559 }
560 else if (int(version) == 2) {
[6192]561 return t_rnxObsFile::type3to2(sys, origType);
[6126]562 }
563 else if (int(version) == 3) {
[6192]564 return t_rnxObsFile::type2to3(sys, origType);
[6126]565 }
[3716]566 }
[6126]567 return "";
[3716]568}
569
[4481]570// Write Observation Types
571////////////////////////////////////////////////////////////////////////////
572QStringList t_rnxObsHeader::obsTypesStrings() const {
573
574 QStringList strList;
575
576 if (_version < 3.0) {
[6245]577 char sys0 = _usedSystems[0].toAscii();
[4481]578 QString hlp;
[6125]579 QTextStream(&hlp) << QString("%1").arg(_obsTypes[sys0].size(), 6);
580 for (int ii = 0; ii < _obsTypes[sys0].size(); ii++) {
581 QTextStream(&hlp) << QString("%1").arg(_obsTypes[sys0][ii], 6);
582 if ((ii+1) % 9 == 0 || ii == _obsTypes[sys0].size()-1) {
[4481]583 strList.append(hlp.leftJustified(60) + "# / TYPES OF OBSERV\n");
584 hlp = QString().leftJustified(6);
585 }
586 }
587 }
588 else {
[6219]589 for (int iSys = 0; iSys < numSys(); iSys++) {
590 char sys = system(iSys);
[4481]591 QString hlp;
[6219]592 QTextStream(&hlp) << QString("%1 %2").arg(sys).arg(nTypes(sys), 3);
593 for (int iType = 0; iType < nTypes(sys); iType++) {
[6221]594 QString type = obsType(sys, iType);
[6219]595 QTextStream(&hlp) << QString(" %1").arg(type, -3);
596 if ((iType+1) % 13 == 0 || iType == nTypes(sys)-1) {
[4481]597 strList.append(hlp.leftJustified(60) + "SYS / # / OBS TYPES\n");
598 hlp = QString().leftJustified(6);
599 }
600 }
601 }
602 }
603
604 return strList;
605}
606
[3716]607// Constructor
608////////////////////////////////////////////////////////////////////////////
[3843]609t_rnxObsFile::t_rnxObsFile(const QString& fileName, e_inpOut inpOut) {
610 _inpOut = inpOut;
[3716]611 _stream = 0;
612 _flgPowerFail = false;
[3843]613 if (_inpOut == input) {
614 openRead(fileName);
615 }
616 else {
[3845]617 openWrite(fileName);
[3843]618 }
[3716]619}
620
[3845]621// Open for input
[3716]622////////////////////////////////////////////////////////////////////////////
[3843]623void t_rnxObsFile::openRead(const QString& fileName) {
[3718]624
625 _fileName = fileName; expandEnvVar(_fileName);
626 _file = new QFile(_fileName);
627 _file->open(QIODevice::ReadOnly | QIODevice::Text);
628 _stream = new QTextStream();
629 _stream->setDevice(_file);
630
[3716]631 _header.read(_stream);
632
633 // Guess Observation Interval
634 // --------------------------
635 if (_header._interval == 0.0) {
636 bncTime ttPrev;
637 for (int iEpo = 0; iEpo < 10; iEpo++) {
638 const t_rnxEpo* rnxEpo = nextEpoch();
639 if (!rnxEpo) {
[3718]640 throw QString("t_rnxObsFile: not enough epochs");
[3716]641 }
642 if (iEpo > 0) {
643 double dt = rnxEpo->tt - ttPrev;
644 if (_header._interval == 0.0 || dt < _header._interval) {
645 _header._interval = dt;
646 }
647 }
648 ttPrev = rnxEpo->tt;
649 }
[3719]650 _stream->seek(0);
[3716]651 _header.read(_stream);
652 }
[3837]653
654 // Time of first observation
655 // -------------------------
656 if (!_header._startTime.valid()) {
657 const t_rnxEpo* rnxEpo = nextEpoch();
658 if (!rnxEpo) {
659 throw QString("t_rnxObsFile: not enough epochs");
660 }
661 _header._startTime = rnxEpo->tt;
662 _stream->seek(0);
663 _header.read(_stream);
664 }
[3716]665}
666
[3845]667// Open for output
668////////////////////////////////////////////////////////////////////////////
669void t_rnxObsFile::openWrite(const QString& fileName) {
670
671 _fileName = fileName; expandEnvVar(_fileName);
672 _file = new QFile(_fileName);
673 _file->open(QIODevice::WriteOnly | QIODevice::Text);
674 _stream = new QTextStream();
675 _stream->setDevice(_file);
676}
677
[3716]678// Destructor
679////////////////////////////////////////////////////////////////////////////
680t_rnxObsFile::~t_rnxObsFile() {
681 close();
682}
683
684// Close
685////////////////////////////////////////////////////////////////////////////
686void t_rnxObsFile::close() {
687 delete _stream; _stream = 0;
[3718]688 delete _file; _file = 0;
[3716]689}
690
691// Handle Special Epoch Flag
692////////////////////////////////////////////////////////////////////////////
[4540]693void t_rnxObsFile::handleEpochFlag(int flag, const QString& line,
694 bool& headerReRead) {
[3716]695
[4540]696 headerReRead = false;
697
[3716]698 // Power Failure
699 // -------------
700 if (flag == 1) {
701 _flgPowerFail = true;
702 }
703
704 // Start moving antenna
705 // --------------------
706 else if (flag == 2) {
707 // no action
708 }
709
710 // Re-Read Header
711 // --------------
[5468]712 else if (flag == 3 || flag == 4 || flag == 5) {
[3716]713 int numLines = 0;
714 if (version() < 3.0) {
715 readInt(line, 29, 3, numLines);
716 }
717 else {
718 readInt(line, 32, 3, numLines);
719 }
[5468]720 if (flag == 3 || flag == 4) {
721 _header.read(_stream, numLines);
722 headerReRead = true;
723 }
724 else {
725 for (int ii = 0; ii < numLines; ii++) {
726 _stream->readLine();
727 }
728 }
[3716]729 }
730
731 // Unhandled Flag
732 // --------------
733 else {
[3718]734 throw QString("t_rnxObsFile: unhandled flag\n" + line);
[3716]735 }
736}
737
738// Retrieve single Epoch
739////////////////////////////////////////////////////////////////////////////
[3994]740t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpoch() {
[3716]741 _currEpo.clear();
742 if (version() < 3.0) {
743 return nextEpochV2();
744 }
745 else {
746 return nextEpochV3();
747 }
748}
749
750// Retrieve single Epoch (RINEX Version 3)
751////////////////////////////////////////////////////////////////////////////
[3994]752t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV3() {
[3716]753
[3719]754 while ( _stream->status() == QTextStream::Ok && !_stream->atEnd() ) {
[3716]755
[3718]756 QString line = _stream->readLine();
[3716]757
[3718]758 if (line.isEmpty()) {
[3716]759 continue;
760 }
761
762 int flag = 0;
763 readInt(line, 31, 1, flag);
764 if (flag > 0) {
[4540]765 bool headerReRead = false;
766 handleEpochFlag(flag, line, headerReRead);
767 if (headerReRead) {
768 continue;
769 }
[3716]770 }
771
[3718]772 QTextStream in(line.mid(1).toAscii(), QIODevice::ReadOnly);
[3716]773
774 // Epoch Time
775 // ----------
776 int year, month, day, hour, min;
777 double sec;
778 in >> year >> month >> day >> hour >> min >> sec;
[3720]779 _currEpo.tt.set(year, month, day, hour, min, sec);
[3716]780
781 // Number of Satellites
782 // --------------------
783 int numSat;
784 readInt(line, 32, 3, numSat);
785
786 _currEpo.rnxSat.resize(numSat);
787
788 // Observations
789 // ------------
790 for (int iSat = 0; iSat < numSat; iSat++) {
[3718]791 line = _stream->readLine();
[6119]792 t_prn prn; prn.set(line.left(3).toAscii().data());
793 _currEpo.rnxSat[iSat].prn = prn;
794 char sys = prn.system();
[3716]795 for (int iType = 0; iType < _header.nTypes(sys); iType++) {
796 int pos = 3 + 16*iType;
797 double obsValue = 0.0;
798 int lli = 0;
799 int snr = 0;
800 readDbl(line, pos, 14, obsValue);
801 readInt(line, pos + 14, 1, lli);
802 readInt(line, pos + 15, 1, snr);
803 if (_flgPowerFail) {
804 lli |= 1;
805 }
[6119]806 QString type = obsType(sys, iType);
807 _currEpo.rnxSat[iSat].obs[type].value = obsValue;
808 _currEpo.rnxSat[iSat].obs[type].lli = lli;
809 _currEpo.rnxSat[iSat].obs[type].snr = snr;
[3716]810 }
811 }
812
813 _flgPowerFail = false;
814
815 return &_currEpo;
816 }
817
818 return 0;
819}
820
821// Retrieve single Epoch (RINEX Version 2)
822////////////////////////////////////////////////////////////////////////////
[3994]823t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV2() {
[3716]824
[3719]825 while ( _stream->status() == QTextStream::Ok && !_stream->atEnd() ) {
[3716]826
[3718]827 QString line = _stream->readLine();
[3716]828
[3718]829 if (line.isEmpty()) {
[3716]830 continue;
831 }
832
833 int flag = 0;
834 readInt(line, 28, 1, flag);
835 if (flag > 0) {
[4540]836 bool headerReRead = false;
837 handleEpochFlag(flag, line, headerReRead);
838 if (headerReRead) {
839 continue;
840 }
[3716]841 }
842
[3718]843 QTextStream in(line.toAscii(), QIODevice::ReadOnly);
[3716]844
845 // Epoch Time
846 // ----------
847 int year, month, day, hour, min;
848 double sec;
849 in >> year >> month >> day >> hour >> min >> sec;
850 if (year < 80) {
851 year += 2000;
852 }
853 else if (year < 100) {
854 year += 1900;
855 }
[3720]856 _currEpo.tt.set(year, month, day, hour, min, sec);
[3716]857
858 // Number of Satellites
859 // --------------------
860 int numSat;
861 readInt(line, 29, 3, numSat);
862
863 _currEpo.rnxSat.resize(numSat);
864
865 // Read Satellite Numbers
866 // ----------------------
867 int pos = 32;
868 for (int iSat = 0; iSat < numSat; iSat++) {
869 if (iSat > 0 && iSat % 12 == 0) {
[3718]870 line = _stream->readLine();
[3716]871 pos = 32;
872 }
873
[6119]874 char sys = line.toAscii()[pos];
[6258]875 if (sys == ' ') {
876 sys = 'G';
877 }
[6119]878 int satNum; readInt(line, pos + 1, 2, satNum);
879 _currEpo.rnxSat[iSat].prn.set(sys, satNum);
880
[3716]881 pos += 3;
882 }
883
884 // Read Observation Records
885 // ------------------------
886 for (int iSat = 0; iSat < numSat; iSat++) {
[6119]887 char sys = _currEpo.rnxSat[iSat].prn.system();
[3718]888 line = _stream->readLine();
[3716]889 pos = 0;
[6119]890 for (int iType = 0; iType < _header.nTypes(sys); iType++) {
[3716]891 if (iType > 0 && iType % 5 == 0) {
[3718]892 line = _stream->readLine();
[3716]893 pos = 0;
894 }
895 double obsValue = 0.0;
896 int lli = 0;
897 int snr = 0;
898 readDbl(line, pos, 14, obsValue);
899 readInt(line, pos + 14, 1, lli);
900 readInt(line, pos + 15, 1, snr);
901
902 if (_flgPowerFail) {
903 lli |= 1;
904 }
905
[6119]906 QString type = obsType(sys, iType);
907 _currEpo.rnxSat[iSat].obs[type].value = obsValue;
908 _currEpo.rnxSat[iSat].obs[type].lli = lli;
909 _currEpo.rnxSat[iSat].obs[type].snr = snr;
[3716]910
911 pos += 16;
912 }
913 }
914
915 _flgPowerFail = false;
916
917 return &_currEpo;
918 }
919
920 return 0;
921}
922
[3845]923// Write Data Epoch
924////////////////////////////////////////////////////////////////////////////
925void t_rnxObsFile::writeEpoch(const t_rnxEpo* epo) {
[6246]926 if (epo == 0) {
927 return;
928 }
929 t_rnxEpo epoLocal;
930 epoLocal.tt = epo->tt;
931 for (unsigned ii = 0; ii < epo->rnxSat.size(); ii++) {
932 const t_rnxSat& rnxSat = epo->rnxSat[ii];
933 if (_header._obsTypes[rnxSat.prn.system()].size() > 0) {
934 epoLocal.rnxSat.push_back(rnxSat);
935 }
936 }
937
[3866]938 if (version() < 3.0) {
[6246]939 return writeEpochV2(_stream, _header, &epoLocal);
[3866]940 }
941 else {
[6246]942 return writeEpochV3(_stream, _header, &epoLocal);
[3866]943 }
944}
[3855]945
[3866]946// Write Data Epoch (RINEX Version 2)
947////////////////////////////////////////////////////////////////////////////
[6222]948void t_rnxObsFile::writeEpochV2(QTextStream* stream, const t_rnxObsHeader& header,
949 const t_rnxEpo* epo) {
[3866]950
[3855]951 unsigned year, month, day, hour, min;
952 double sec;
953 epo->tt.civil_date(year, month, day);
954 epo->tt.civil_time(hour, min, sec);
955
956 QString dateStr;
[3868]957 QTextStream(&dateStr) << QString(" %1 %2 %3 %4 %5%6")
[3856]958 .arg(int(fmod(year, 100)), 2, 10, QChar('0'))
[3868]959 .arg(month, 2, 10, QChar('0'))
960 .arg(day, 2, 10, QChar('0'))
961 .arg(hour, 2, 10, QChar('0'))
962 .arg(min, 2, 10, QChar('0'))
963 .arg(sec, 11, 'f', 7);
[3855]964
[3857]965 int flag = 0;
[6222]966 *stream << dateStr << QString("%1%2").arg(flag, 3).arg(epo->rnxSat.size(), 3);
[3858]967 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
[3869]968 const t_rnxSat& rnxSat = epo->rnxSat[iSat];
[3858]969 if (iSat > 0 && iSat % 12 == 0) {
[6222]970 *stream << endl << QString().leftJustified(32);
[3857]971 }
[6222]972 *stream << rnxSat.prn.toString().c_str();
[3857]973 }
[6222]974 *stream << endl;
[3858]975 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
[3967]976
[3858]977 const t_rnxSat& rnxSat = epo->rnxSat[iSat];
[6119]978 char sys = rnxSat.prn.system();
[3966]979
[6222]980 for (int iTypeV2 = 0; iTypeV2 < header.nTypes(sys); iTypeV2++) {
[6216]981 if (iTypeV2 > 0 && iTypeV2 % 5 == 0) {
[6222]982 *stream << endl;
[3967]983 }
[6222]984 QString typeV2 = header.obsType(sys, iTypeV2);
[6216]985 bool found = false;
[6231]986
[6241]987 QString preferredAttrib = signalPriorities(sys);
[6231]988 for (int iPref = 0; iPref < preferredAttrib.length(); iPref++) {
989 QMapIterator<QString, t_rnxObs> itObs(rnxSat.obs);
990 while (itObs.hasNext()) {
991 itObs.next();
992 const QString& type = itObs.key();
993 const t_rnxObs& rnxObs = itObs.value();
[6232]994 if ( preferredAttrib[iPref] == '?' ||
[6241]995 (type.length() == 2 && preferredAttrib[iPref] == '_' ) ||
[6232]996 (type.length() == 3 && preferredAttrib[iPref] == type[2]) ) {
997 if (typeV2 == type3to2(sys, type)) {
998 found = true;
999 if (rnxObs.value == 0.0) {
1000 *stream << QString().leftJustified(16);
[6231]1001 }
1002 else {
[6232]1003 *stream << QString("%1").arg(rnxObs.value, 14, 'f', 3);
1004 if (rnxObs.lli != 0.0) {
1005 *stream << QString("%1").arg(rnxObs.lli,1);
1006 }
1007 else {
1008 *stream << ' ';
1009 }
1010 if (rnxObs.snr != 0.0) {
1011 *stream << QString("%1").arg(rnxObs.snr,1);
1012 }
1013 else {
1014 *stream << ' ';
1015 }
[6231]1016 }
[6251]1017 goto end_loop_iPref;
[6216]1018 }
[6127]1019 }
[6231]1020 }
[6251]1021 } end_loop_iPref:
[6216]1022 if (!found) {
[6222]1023 *stream << QString().leftJustified(16);
[6216]1024 }
[3858]1025 }
[6222]1026 *stream << endl;
[3858]1027 }
[3845]1028}
[3866]1029
1030// Write Data Epoch (RINEX Version 3)
1031////////////////////////////////////////////////////////////////////////////
[6222]1032void t_rnxObsFile::writeEpochV3(QTextStream* stream, const t_rnxObsHeader& header,
1033 const t_rnxEpo* epo) {
[3866]1034
1035 unsigned year, month, day, hour, min;
1036 double sec;
1037 epo->tt.civil_date(year, month, day);
1038 epo->tt.civil_time(hour, min, sec);
1039
[3867]1040 QString dateStr;
[3868]1041 QTextStream(&dateStr) << QString("> %1 %2 %3 %4 %5%6")
[3867]1042 .arg(year, 4)
1043 .arg(month, 2, 10, QChar('0'))
1044 .arg(day, 2, 10, QChar('0'))
1045 .arg(hour, 2, 10, QChar('0'))
1046 .arg(min, 2, 10, QChar('0'))
1047 .arg(sec, 11, 'f', 7);
[3866]1048
[3867]1049 int flag = 0;
[6222]1050 *stream << dateStr << QString("%1%2\n").arg(flag, 3).arg(epo->rnxSat.size(), 3);
[3867]1051
[3869]1052 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
1053 const t_rnxSat& rnxSat = epo->rnxSat[iSat];
[6119]1054 char sys = rnxSat.prn.system();
[3964]1055
[6222]1056 *stream << rnxSat.prn.toString().c_str();
[6220]1057
[6222]1058 for (int iTypeV3 = 0; iTypeV3 < header.nTypes(sys); iTypeV3++) {
1059 QString typeV3 = header.obsType(sys, iTypeV3);
[6220]1060 bool found = false;
1061 QMapIterator<QString, t_rnxObs> itObs(rnxSat.obs);
1062 while (itObs.hasNext()) {
1063 itObs.next();
1064 const QString& type = itObs.key();
1065 const t_rnxObs& rnxObs = itObs.value();
[6397]1066 if (typeV3 == type2to3(sys, type) || typeV3 == type2to3(sys, type).left(2)) {
[6220]1067 found = true;
1068 if (rnxObs.value == 0.0) {
[6222]1069 *stream << QString().leftJustified(16);
[6127]1070 }
1071 else {
[6222]1072 *stream << QString("%1").arg(rnxObs.value, 14, 'f', 3);
[6220]1073 if (rnxObs.lli != 0.0) {
[6222]1074 *stream << QString("%1").arg(rnxObs.lli,1);
[6220]1075 }
1076 else {
[6222]1077 *stream << ' ';
[6220]1078 }
1079 if (rnxObs.snr != 0.0) {
[6222]1080 *stream << QString("%1").arg(rnxObs.snr,1);
[6220]1081 }
1082 else {
[6222]1083 *stream << ' ';
[6220]1084 }
[6127]1085 }
[3979]1086 }
[3869]1087 }
[6220]1088 if (!found) {
[6222]1089 *stream << QString().leftJustified(16);
[6220]1090 }
[3869]1091 }
[6222]1092 *stream << endl;
[3869]1093 }
[3866]1094}
[3956]1095
1096// Translate Observation Type v2 --> v3
1097////////////////////////////////////////////////////////////////////////////
[6193]1098QString t_rnxObsFile::type2to3(char sys, const QString& typeV2) {
[6147]1099 if (typeV2 == "P1") {
[6193]1100 return (sys == 'G') ? "C1W" : "C1P";
[3956]1101 }
[6147]1102 else if (typeV2 == "P2") {
[6193]1103 return (sys == 'G') ? "C2W" : "C2P";
[3956]1104 }
[6147]1105 return typeV2;
[3956]1106}
[3962]1107
1108// Translate Observation Type v3 --> v2
1109////////////////////////////////////////////////////////////////////////////
[6192]1110QString t_rnxObsFile::type3to2(char /* sys */, const QString& typeV3) {
[6193]1111 if (typeV3 == "C1P" || typeV3 == "C1W") {
[3962]1112 return "P1";
1113 }
[6193]1114 else if (typeV3 == "C2P" || typeV3 == "C2W") {
[3962]1115 return "P2";
1116 }
[6147]1117 return typeV3.left(2);
[3962]1118}
[4053]1119
[5883]1120// Set Observations from RINEX File
1121////////////////////////////////////////////////////////////////////////////
[6137]1122void t_rnxObsFile::setObsFromRnx(const t_rnxObsFile* rnxObsFile, const t_rnxObsFile::t_rnxEpo* epo,
1123 const t_rnxObsFile::t_rnxSat& rnxSat, t_satObs& obs) {
1124 obs._staID = rnxObsFile->markerName().toAscii().constData();
1125 obs._prn = rnxSat.prn;
1126 obs._time = epo->tt;
[5883]1127
[6137]1128 char sys = rnxSat.prn.system();
[5883]1129
[6259]1130 QChar addToL2;
[6137]1131 for (int iType = 0; iType < rnxObsFile->nTypes(sys); iType++) {
[6253]1132 QString type = rnxObsFile->obsType(sys, iType);
1133 QString typeV3 = rnxObsFile->obsType(sys, iType, 3.0); // may or may not differ from type
[6259]1134 if (rnxSat.obs.contains(type) && rnxSat.obs[type].value != 0.0) {
1135 if (type == "P2" && typeV3.length() > 2) {
1136 addToL2 = typeV3[2];
1137 break;
1138 }
1139 }
1140 }
1141
1142 for (int iType = 0; iType < rnxObsFile->nTypes(sys); iType++) {
1143 QString type = rnxObsFile->obsType(sys, iType);
1144 QString typeV3 = rnxObsFile->obsType(sys, iType, 3.0); // may or may not differ from type
1145 if (type == "L2") {
1146 typeV3 += addToL2;
1147 }
[6119]1148 if (rnxSat.obs.contains(type)) {
1149 const t_rnxObs& rnxObs = rnxSat.obs[type];
[6188]1150 if (rnxObs.value != 0.0) {
[6253]1151 string type2ch(typeV3.mid(1).toAscii().data());
[6188]1152
1153 t_frqObs* frqObs = 0;
1154 for (unsigned iFrq = 0; iFrq < obs._obs.size(); iFrq++) {
1155 if (obs._obs[iFrq]->_rnxType2ch == type2ch) {
1156 frqObs = obs._obs[iFrq];
1157 break;
1158 }
1159 }
1160 if (frqObs == 0) {
1161 frqObs = new t_frqObs;
1162 frqObs->_rnxType2ch = type2ch;
1163 obs._obs.push_back(frqObs);
1164 }
1165
[6253]1166 switch( typeV3.toAscii().data()[0] ) {
[6188]1167 case 'C':
1168 frqObs->_codeValid = true;
1169 frqObs->_code = rnxObs.value;
[6137]1170 break;
[6188]1171 case 'L':
1172 frqObs->_phaseValid = true;
1173 frqObs->_phase = rnxObs.value;
1174 frqObs->_slip = (rnxObs.lli & 1);
1175 break;
1176 case 'D':
1177 frqObs->_dopplerValid = true;
1178 frqObs->_doppler = rnxObs.value;
1179 break;
1180 case 'S':
1181 frqObs->_snrValid = true;
1182 frqObs->_snr = rnxObs.value;
1183 break;
[6137]1184 }
[6260]1185
1186 // Handle old-fashioned SNR values
1187 // -------------------------------
1188 if (rnxObs.snr != 0 && !frqObs->_snrValid) {
1189 frqObs->_snrValid = true;
[6263]1190 frqObs->_snr = rnxObs.snr * 6.0 + 2.5;
[6260]1191 }
[6119]1192 }
[5883]1193 }
[6119]1194 }
1195}
1196
[6241]1197// Tracking Mode Priorities
1198////////////////////////////////////////////////////////////////////////////
1199QString t_rnxObsFile::signalPriorities(char sys) {
[6242]1200
1201 bncSettings settings;
1202 QStringList priorList = settings.value("rnxV2Priority").toString().split(" ", QString::SkipEmptyParts);
[6243]1203 if (priorList.empty()) {
1204 priorList << "CWPX_?";
1205 }
[6242]1206
1207 QString result;
1208 for (int ii = 0; ii < priorList.size(); ii++) {
1209 if (priorList[ii].indexOf(":") != -1) {
1210 QStringList hlp = priorList[ii].split(":", QString::SkipEmptyParts);
1211 if (hlp.size() == 2 && hlp[0].length() == 1 && hlp[0][0] == sys) {
1212 result = hlp[1];
1213 break;
1214 }
1215 }
1216 else {
1217 result = priorList[ii];
1218 }
1219 }
1220
1221 return result;
[6241]1222}
Note: See TracBrowser for help on using the repository browser.