source: ntrip/trunk/BNC/bncrinex.cpp@ 596

Last change on this file since 596 was 566, checked in by mervart, 17 years ago

* empty log message *

File size: 19.2 KB
RevLine 
[280]1// Part of BNC, a utility for retrieving decoding and
[464]2// converting GNSS data streams from NTRIP broadcasters.
[280]3//
[464]4// Copyright (C) 2007
[280]5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
[464]7// Czech Technical University Prague, Department of Geodesy
[280]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.
[73]24
25/* -------------------------------------------------------------------------
[93]26 * BKG NTRIP Client
[73]27 * -------------------------------------------------------------------------
28 *
29 * Class: bncRinex
30 *
31 * Purpose: writes RINEX files
32 *
33 * Author: L. Mervart
34 *
35 * Created: 27-Aug-2006
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
[292]41#include <stdlib.h>
[300]42#include <iostream>
[75]43#include <iomanip>
[221]44#include <math.h>
[75]45
[300]46#include <QtCore>
47#include <QUrl>
48#include <QString>
49
[73]50#include "bncrinex.h"
[157]51#include "bncapp.h"
[82]52#include "bncutils.h"
[126]53#include "bncconst.h"
[298]54#include "bnctabledlg.h"
[301]55#include "bncgetthread.h"
[223]56#include "RTCM3/rtcm3torinex.h"
[75]57
58using namespace std;
59
[73]60// Constructor
61////////////////////////////////////////////////////////////////////////////
[408]62bncRinex::bncRinex(const QByteArray& statID, const QUrl& mountPoint,
[366]63 const QByteArray& format, const QByteArray& latitude,
64 const QByteArray& longitude, const QByteArray& nmea) {
[408]65 _statID = statID;
[336]66 _mountPoint = mountPoint;
[337]67 _format = format.left(6);
[366]68 _latitude = latitude;
69 _longitude = longitude;
70 _nmea = nmea;
[77]71 _headerWritten = false;
[369]72 _reconnectFlag = false;
[132]73
74 QSettings settings;
75 _rnxScriptName = settings.value("rnxScript").toString();
76 expandEnvVar(_rnxScriptName);
[153]77
[157]78 _pgmName = ((bncApp*)qApp)->bncVersion().leftJustified(20, ' ', true);
[319]79#ifdef WIN32
80 _userName = QString("${USERNAME}");
81#else
[158]82 _userName = QString("${USER}");
[319]83#endif
[157]84 expandEnvVar(_userName);
[158]85 _userName = _userName.leftJustified(20, ' ', true);
[539]86
87 if ( Qt::CheckState(settings.value("rnxV3").toInt()) == Qt::Checked) {
88 _rinexVers = 3;
89 }
90 else {
91 _rinexVers = 2;
92 }
[73]93}
94
95// Destructor
96////////////////////////////////////////////////////////////////////////////
97bncRinex::~bncRinex() {
[77]98 _out.close();
[73]99}
100
[420]101// Download Skeleton Header File
102////////////////////////////////////////////////////////////////////////////
103t_irc bncRinex::downloadSkeleton() {
104
105 t_irc irc = failure;
106
107 QStringList table;
108 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(),
109 table, false);
110 QString net;
111 QStringListIterator it(table);
112 while (it.hasNext()) {
113 QString line = it.next();
114 if (line.indexOf("STR") == 0) {
115 QStringList tags = line.split(";");
116 if (tags.at(1) == _mountPoint.path().mid(1).toAscii()) {
117 net = tags.at(7);
118 break;
119 }
120 }
121 }
122 QString sklDir;
123 it.toFront();
124 while (it.hasNext()) {
125 QString line = it.next();
126 if (line.indexOf("NET") == 0) {
127 QStringList tags = line.split(";");
128 if (tags.at(1) == net) {
129 sklDir = tags.at(6).trimmed();
130 break;
131 }
132 }
133 }
134 if (!sklDir.isEmpty() && sklDir != "none") {
135 QUrl url(sklDir + "/" + _mountPoint.path().mid(1,4).toLower() + ".skl");
136 if (url.port() == -1) {
137 url.setPort(80);
138 }
139
140 const int timeOut = 10*1000;
141 QString msg;
142 QByteArray _latitude;
143 QByteArray _longitude;
144 QByteArray _nmea;
145 QTcpSocket* socket = bncGetThread::request(url, _latitude, _longitude, _nmea, timeOut, msg);
146
147 if (socket) {
[421]148 _headerLines.clear();
149 bool firstLineRead = false;
[420]150 while (true) {
151 if (socket->canReadLine()) {
[421]152 QString line = socket->readLine();
153 line.chop(1);
[556]154 if (line.indexOf("MARKER NAME") != -1) {
155 irc = success;
156 }
[421]157 if (line.indexOf("RINEX VERSION") != -1) {
[545]158 if (_rinexVers == 3) {
159 _headerLines.append(" 3.00 OBSERVATION DATA"
160 " M (MIXED)"
161 " RINEX VERSION / TYPE");
162 }
163 else {
164 _headerLines.append(" 2.11 OBSERVATION DATA"
165 " M (MIXED)"
166 " RINEX VERSION / TYPE");
167 }
[421]168 _headerLines.append("PGM / RUN BY / DATE");
169 firstLineRead = true;
170 }
171 else if (firstLineRead) {
172 if (line.indexOf("END OF HEADER") != -1) {
173 _headerLines.append("# / TYPES OF OBSERV");
[546]174 if (_rinexVers == 2) {
175 _headerLines.append(
176 QString(" 1 1").leftJustified(60, ' ', true) +
177 "WAVELENGTH FACT L1/2");
178 }
[421]179 _headerLines.append("TIME OF FIRST OBS");
180 _headerLines.append( line );
181 break;
182 }
183 else {
184 _headerLines.append( line );
185 }
186 }
[420]187 }
188 else {
189 socket->waitForReadyRead(timeOut);
190 if (socket->bytesAvailable() > 0) {
191 continue;
192 }
193 else {
194 break;
195 }
196 }
197 }
198 delete socket;
199 }
200 }
201 return irc;
202}
203
[82]204// Read Skeleton Header File
205////////////////////////////////////////////////////////////////////////////
206void bncRinex::readSkeleton() {
207
[420]208 // Read the local file
209 // -------------------
[276]210 QFile skl(_sklName);
[82]211 if ( skl.exists() && skl.open(QIODevice::ReadOnly) ) {
[422]212 _headerLines.clear();
[82]213 QTextStream in(&skl);
214 while ( !in.atEnd() ) {
215 _headerLines.append( in.readLine() );
216 if (_headerLines.last().indexOf("END OF HEADER") != -1) {
217 break;
218 }
219 }
220 }
[298]221
[420]222 // Read downloaded file
223 // --------------------
[298]224 else {
[566]225 QDate currDate = QDateTime::currentDateTime().toUTC().date();
[420]226 if ( !_skeletonDate.isValid() || _skeletonDate != currDate ) {
227 if ( downloadSkeleton() == success) {
228 _skeletonDate = currDate;
[298]229 }
230 }
231 }
[82]232}
233
234// File Name according to RINEX Standards
235////////////////////////////////////////////////////////////////////////////
[125]236void bncRinex::resolveFileName(const QDateTime& datTim) {
[82]237
238 QSettings settings;
239 QString path = settings.value("rnxPath").toString();
240 expandEnvVar(path);
241
[391]242 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
[82]243 path += QDir::separator();
244 }
245
[127]246 QString intStr = settings.value("rnxIntr").toString();
247 QString hlpStr;
[129]248
249 QTime nextTime;
250 QDate nextDate;
251
[204]252 int indHlp = intStr.indexOf("min");
253
254 if ( indHlp != -1) {
255 int step = intStr.left(indHlp-1).toInt();
[127]256 char ch = 'A' + datTim.time().hour();
257 hlpStr = ch;
[204]258 if (datTim.time().minute() >= 60-step) {
259 hlpStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
[199]260 if (datTim.time().hour() < 23) {
261 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
262 nextDate = datTim.date();
263 }
264 else {
265 nextTime.setHMS(0, 0, 0);
266 nextDate = datTim.date().addDays(1);
267 }
268 }
269 else {
[204]270 for (int limit = step; limit <= 60-step; limit += step) {
271 if (datTim.time().minute() < limit) {
272 hlpStr += QString("%1").arg(limit-step, 2, 10, QChar('0'));
273 nextTime.setHMS(datTim.time().hour(), limit, 0);
274 nextDate = datTim.date();
275 break;
276 }
[199]277 }
278 }
279 }
[127]280 else if (intStr == "1 hour") {
281 char ch = 'A' + datTim.time().hour();
282 hlpStr = ch;
[129]283 if (datTim.time().hour() < 23) {
284 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
285 nextDate = datTim.date();
286 }
287 else {
288 nextTime.setHMS(0, 0, 0);
289 nextDate = datTim.date().addDays(1);
290 }
[127]291 }
292 else {
293 hlpStr = "0";
[129]294 nextTime.setHMS(0, 0, 0);
295 nextDate = datTim.date().addDays(1);
[127]296 }
[267]297 _nextCloseEpoch = QDateTime(nextDate, nextTime);
[82]298
[183]299 QString ID4 = _statID.left(4);
300
301 // Check name conflict
302 // -------------------
303 QString distStr;
304 int num = 0;
305 QListIterator<QString> it(settings.value("mountPoints").toStringList());
306 while (it.hasNext()) {
307 QString mp = it.next();
308 if (mp.indexOf(ID4) != -1) {
309 ++num;
310 }
311 }
312 if (num > 1) {
313 distStr = "_" + _statID.mid(4);
314 }
315
[276]316 QString sklExt = settings.value("rnxSkel").toString();
317 if (!sklExt.isEmpty()) {
318 _sklName = path + ID4 + distStr + "." + sklExt;
319 }
320
[183]321 path += ID4 +
[127]322 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
[276]323 hlpStr + distStr + datTim.toString(".yyO");
[82]324
325 _fName = path.toAscii();
326}
327
[77]328// Write RINEX Header
329////////////////////////////////////////////////////////////////////////////
[267]330void bncRinex::writeHeader(const QDateTime& datTim,
331 const QDateTime& datTimNom) {
[77]332
[356]333 QSettings settings;
334
[77]335 // Open the Output File
336 // --------------------
[267]337 resolveFileName(datTimNom);
[260]338
339 // Append to existing file and return
340 // ----------------------------------
341 if ( QFile::exists(_fName) ) {
[369]342 if (_reconnectFlag ||
343 Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
[260]344 _out.open(_fName.data(), ios::app);
345 _out.setf(ios::showpoint | ios::fixed);
346 _headerWritten = true;
[369]347 _reconnectFlag = false;
[260]348 return;
349 }
350 }
351
[82]352 _out.open(_fName.data());
[85]353 _out.setf(ios::showpoint | ios::fixed);
[77]354
[82]355 // Copy Skeleton Header
356 // --------------------
[276]357 readSkeleton();
[82]358 if (_headerLines.size() > 0) {
359 QStringListIterator it(_headerLines);
360 while (it.hasNext()) {
361 QString line = it.next();
[157]362 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
[552]363 if (_rinexVers == 3) {
[566]364 QString hlp = QDateTime::currentDateTime().toUTC().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
[552]365 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
366 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
367 }
368 else {
[566]369 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[552]370 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
371 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
372 }
[157]373 }
374 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
[545]375 if (_rinexVers == 3) {
376 _out << "G 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
377 _out << "R 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
[554]378 _out << "S 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
[545]379 }
380 else {
381 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2"
382 " # / TYPES OF OBSERV" << endl;
383 }
[82]384 }
385 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
[125]386 _out << datTim.toString(" yyyy MM dd"
387 " hh mm ss.zzz0000").toAscii().data();
[553]388 _out << " GPS TIME OF FIRST OBS" << endl;
[337]389 QString hlp = (_format + QString(" %1").arg(_mountPoint.host() +
390 _mountPoint.path())).leftJustified(60, ' ', true);
[156]391 _out << hlp.toAscii().data() << "COMMENT" << endl;
[82]392 }
393 else {
394 _out << line.toAscii().data() << endl;
395 }
396 }
397 }
[78]398
[82]399 // Write Dummy Header
400 // ------------------
401 else {
402 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
403 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
404
[545]405 if (_rinexVers == 3) {
406 _out << " 3.00 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
[566]407 QString hlp = QDateTime::currentDateTime().toUTC().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
[552]408 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
409 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
[545]410 }
411 else {
412 _out << " 2.11 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
[566]413 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[552]414 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
415 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
[545]416 }
[86]417 _out.setf(ios::left);
[82]418 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
[354]419 _out << setw(60) << "unknown unknown" << "OBSERVER / AGENCY" << endl;
[82]420 _out << setw(20) << "unknown"
421 << setw(20) << "unknown"
422 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
423 _out << setw(20) << "unknown"
424 << setw(20) << "unknown"
425 << setw(20) << " " << "ANT # / TYPE" << endl;
[86]426 _out.unsetf(ios::left);
[82]427 _out << setw(14) << setprecision(4) << approxPos[0]
428 << setw(14) << setprecision(4) << approxPos[1]
429 << setw(14) << setprecision(4) << approxPos[2]
430 << " " << "APPROX POSITION XYZ" << endl;
431 _out << setw(14) << setprecision(4) << antennaNEU[0]
432 << setw(14) << setprecision(4) << antennaNEU[1]
433 << setw(14) << setprecision(4) << antennaNEU[2]
434 << " " << "ANTENNA: DELTA H/E/N" << endl;
[544]435 if (_rinexVers == 3) {
436 _out << "G 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
437 _out << "R 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
[554]438 _out << "S 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
[544]439
440 }
441 else {
442 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
443 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2 # / TYPES OF OBSERV" << endl;
444 }
445 _out << datTim.toString(" yyyy MM dd"
[125]446 " hh mm ss.zzz0000").toAscii().data();
[553]447 _out << " GPS TIME OF FIRST OBS" << endl;
[552]448 QString hlp = (_format + QString(" %1").arg(_mountPoint.host() +
[337]449 _mountPoint.path())).leftJustified(60, ' ', true);
[156]450 _out << hlp.toAscii().data() << "COMMENT" << endl;
[366]451
[410]452 if (_nmea == "yes") {
453 hlp = ("NMEA LAT=" + _latitude + " " + "LONG=" + _longitude).leftJustified(60, ' ',true);
[366]454 _out << hlp.toAscii().data() << "COMMENT" << endl; }
455
[82]456 _out << " END OF HEADER" << endl;
457 }
[78]458
[77]459 _headerWritten = true;
460}
461
[73]462// Stores Observation into Internal Array
463////////////////////////////////////////////////////////////////////////////
464void bncRinex::deepCopy(const Observation* obs) {
[74]465 Observation* newObs = new Observation();
466 memcpy(newObs, obs, sizeof(*obs));
467 _obs.push_back(newObs);
[73]468}
469
470// Write One Epoch into the RINEX File
471////////////////////////////////////////////////////////////////////////////
[160]472void bncRinex::dumpEpoch(long maxTime) {
[73]473
[160]474 // Select observations older than maxTime
475 // --------------------------------------
476 QList<Observation*> dumpList;
477 QMutableListIterator<Observation*> mIt(_obs);
478 while (mIt.hasNext()) {
479 Observation* ob = mIt.next();
[238]480 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime - 0.05) {
[160]481 dumpList.push_back(ob);
482 mIt.remove();
483 }
484 }
485
[75]486 // Easy Return
487 // -----------
[160]488 if (dumpList.isEmpty()) {
[75]489 return;
490 }
491
492 // Time of Epoch
493 // -------------
[222]494 Observation* fObs = *dumpList.begin();
[267]495 QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
496 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs->GPSWeek,
497 floor(fObs->GPSWeeks+0.5));
[75]498
[130]499 // Close the file
500 // --------------
[267]501 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
[130]502 closeFile();
503 _headerWritten = false;
504 }
505
[78]506 // Write RINEX Header
507 // ------------------
508 if (!_headerWritten) {
[267]509 writeHeader(datTim, datTimNom);
[78]510 }
511
[353]512 double sec = double(datTim.time().second()) + fmod(fObs->GPSWeeks,1.0);
[75]513
[540]514 // RINEX Version 3
515 // ---------------
516 if (_rinexVers == 3) {
517 _out << datTim.toString("> yyyy MM dd hh mm ").toAscii().data()
518 << setw(10) << setprecision(7) << sec
519 << " " << 0 << setw(3) << dumpList.size() << endl;
[539]520
[540]521 QListIterator<Observation*> it(dumpList);
522 while (it.hasNext()) {
523 Observation* ob = it.next();
[543]524 _out << ob->satSys
525 << setw(2) << setfill('0') << ob->satNum << setfill(' ')
[541]526 << setw(14) << setprecision(3) << ob->C1 << " "
[542]527 << setw(14) << setprecision(3) << ob->L1 << " "
528 << setw(1) << ob->SNR1
[541]529 << setw(14) << setprecision(3) << ob->S1 << " "
530 << setw(14) << setprecision(3) << ob->P2 << " "
[542]531 << setw(14) << setprecision(3) << ob->L2 << " "
532 << setw(1) << ob->SNR2
[540]533 << setw(14) << setprecision(3) << ob->S2
534 << endl;
535 delete ob;
[75]536 }
[74]537 }
[75]538
[540]539 // RINEX Version 2
540 // ---------------
541 else {
542 _out << datTim.toString(" yy MM dd hh mm ").toAscii().data()
543 << setw(10) << setprecision(7) << sec
544 << " " << 0 << setw(3) << dumpList.size();
545
546 QListIterator<Observation*> it(dumpList); int iSat = 0;
547 while (it.hasNext()) {
548 iSat++;
549 Observation* ob = it.next();
550 _out << ob->satSys << setw(2) << ob->satNum;
551 if (iSat == 12 && it.hasNext()) {
552 _out << endl << " ";
553 iSat = 0;
554 }
555 }
[77]556 _out << endl;
[540]557
558 it.toFront();
559 while (it.hasNext()) {
560 Observation* ob = it.next();
561
562 char lli = ' ';
563 char snr = ' ';
564 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
565 _out << setw(14) << setprecision(3) << ob->C2 << lli << snr;
566 _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
567 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
568 _out << setw(14) << setprecision(3) << ob->L1 << lli
569 << setw(1) << ob->SNR1 << endl;
570 _out << setw(14) << setprecision(3) << ob->L2 << lli
571 << setw(1) << ob->SNR2;
572 _out << setw(14) << setprecision(3) << ob->S1 ;
573 _out << setw(16) << setprecision(3) << ob->S2 ;
574 _out << endl;
575
576 delete ob;
577 }
[75]578 }
579
[77]580 _out.flush();
[73]581}
582
[130]583// Close the Old RINEX File
584////////////////////////////////////////////////////////////////////////////
585void bncRinex::closeFile() {
[436]586 QMutexLocker locker(&_mutex);
[130]587 _out.close();
[132]588 if (!_rnxScriptName.isEmpty()) {
[436]589
[435]590#ifdef WIN32
591 QProcess::startDetached(_rnxScriptName, QStringList() << _fName) ;
592#else
[475]593 QProcess::startDetached("nohup", QStringList() << _rnxScriptName << _fName) ;
[435]594#endif
[436]595
[132]596 }
[130]597}
Note: See TracBrowser for help on using the repository browser.