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

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

* empty log message *

File size: 18.3 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()) {
152 irc = success;
[421]153 QString line = socket->readLine();
154 line.chop(1);
155 if (line.indexOf("RINEX VERSION") != -1) {
[545]156 if (_rinexVers == 3) {
157 _headerLines.append(" 3.00 OBSERVATION DATA"
158 " M (MIXED)"
159 " RINEX VERSION / TYPE");
160 }
161 else {
162 _headerLines.append(" 2.11 OBSERVATION DATA"
163 " M (MIXED)"
164 " RINEX VERSION / TYPE");
165 }
[421]166 _headerLines.append("PGM / RUN BY / DATE");
167 firstLineRead = true;
168 }
169 else if (firstLineRead) {
170 if (line.indexOf("END OF HEADER") != -1) {
171 _headerLines.append("# / TYPES OF OBSERV");
172 _headerLines.append(
173 QString(" 1 1").leftJustified(60, ' ', true) +
174 "WAVELENGTH FACT L1/2");
175 _headerLines.append("TIME OF FIRST OBS");
176 _headerLines.append( line );
177 break;
178 }
179 else {
180 _headerLines.append( line );
181 }
182 }
[420]183 }
184 else {
185 socket->waitForReadyRead(timeOut);
186 if (socket->bytesAvailable() > 0) {
187 continue;
188 }
189 else {
190 break;
191 }
192 }
193 }
194 delete socket;
195 }
196 }
197 return irc;
198}
199
[82]200// Read Skeleton Header File
201////////////////////////////////////////////////////////////////////////////
202void bncRinex::readSkeleton() {
203
[420]204 // Read the local file
205 // -------------------
[276]206 QFile skl(_sklName);
[82]207 if ( skl.exists() && skl.open(QIODevice::ReadOnly) ) {
[422]208 _headerLines.clear();
[82]209 QTextStream in(&skl);
210 while ( !in.atEnd() ) {
211 _headerLines.append( in.readLine() );
212 if (_headerLines.last().indexOf("END OF HEADER") != -1) {
213 break;
214 }
215 }
216 }
[298]217
[420]218 // Read downloaded file
219 // --------------------
[298]220 else {
[420]221 QDate currDate = QDate::currentDate();
222 if ( !_skeletonDate.isValid() || _skeletonDate != currDate ) {
223 if ( downloadSkeleton() == success) {
224 _skeletonDate = currDate;
[298]225 }
226 }
227 }
[82]228}
229
230// File Name according to RINEX Standards
231////////////////////////////////////////////////////////////////////////////
[125]232void bncRinex::resolveFileName(const QDateTime& datTim) {
[82]233
234 QSettings settings;
235 QString path = settings.value("rnxPath").toString();
236 expandEnvVar(path);
237
[391]238 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
[82]239 path += QDir::separator();
240 }
241
[127]242 QString intStr = settings.value("rnxIntr").toString();
243 QString hlpStr;
[129]244
245 QTime nextTime;
246 QDate nextDate;
247
[204]248 int indHlp = intStr.indexOf("min");
249
250 if ( indHlp != -1) {
251 int step = intStr.left(indHlp-1).toInt();
[127]252 char ch = 'A' + datTim.time().hour();
253 hlpStr = ch;
[204]254 if (datTim.time().minute() >= 60-step) {
255 hlpStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
[199]256 if (datTim.time().hour() < 23) {
257 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
258 nextDate = datTim.date();
259 }
260 else {
261 nextTime.setHMS(0, 0, 0);
262 nextDate = datTim.date().addDays(1);
263 }
264 }
265 else {
[204]266 for (int limit = step; limit <= 60-step; limit += step) {
267 if (datTim.time().minute() < limit) {
268 hlpStr += QString("%1").arg(limit-step, 2, 10, QChar('0'));
269 nextTime.setHMS(datTim.time().hour(), limit, 0);
270 nextDate = datTim.date();
271 break;
272 }
[199]273 }
274 }
275 }
[127]276 else if (intStr == "1 hour") {
277 char ch = 'A' + datTim.time().hour();
278 hlpStr = ch;
[129]279 if (datTim.time().hour() < 23) {
280 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
281 nextDate = datTim.date();
282 }
283 else {
284 nextTime.setHMS(0, 0, 0);
285 nextDate = datTim.date().addDays(1);
286 }
[127]287 }
288 else {
289 hlpStr = "0";
[129]290 nextTime.setHMS(0, 0, 0);
291 nextDate = datTim.date().addDays(1);
[127]292 }
[267]293 _nextCloseEpoch = QDateTime(nextDate, nextTime);
[82]294
[183]295 QString ID4 = _statID.left(4);
296
297 // Check name conflict
298 // -------------------
299 QString distStr;
300 int num = 0;
301 QListIterator<QString> it(settings.value("mountPoints").toStringList());
302 while (it.hasNext()) {
303 QString mp = it.next();
304 if (mp.indexOf(ID4) != -1) {
305 ++num;
306 }
307 }
308 if (num > 1) {
309 distStr = "_" + _statID.mid(4);
310 }
311
[276]312 QString sklExt = settings.value("rnxSkel").toString();
313 if (!sklExt.isEmpty()) {
314 _sklName = path + ID4 + distStr + "." + sklExt;
315 }
316
[183]317 path += ID4 +
[127]318 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
[276]319 hlpStr + distStr + datTim.toString(".yyO");
[82]320
321 _fName = path.toAscii();
322}
323
[77]324// Write RINEX Header
325////////////////////////////////////////////////////////////////////////////
[267]326void bncRinex::writeHeader(const QDateTime& datTim,
327 const QDateTime& datTimNom) {
[77]328
[356]329 QSettings settings;
330
[77]331 // Open the Output File
332 // --------------------
[267]333 resolveFileName(datTimNom);
[260]334
335 // Append to existing file and return
336 // ----------------------------------
337 if ( QFile::exists(_fName) ) {
[369]338 if (_reconnectFlag ||
339 Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
[260]340 _out.open(_fName.data(), ios::app);
341 _out.setf(ios::showpoint | ios::fixed);
342 _headerWritten = true;
[369]343 _reconnectFlag = false;
[260]344 return;
345 }
346 }
347
[82]348 _out.open(_fName.data());
[85]349 _out.setf(ios::showpoint | ios::fixed);
[77]350
[82]351 // Copy Skeleton Header
352 // --------------------
[276]353 readSkeleton();
[82]354 if (_headerLines.size() > 0) {
355 QStringListIterator it(_headerLines);
356 while (it.hasNext()) {
357 QString line = it.next();
[157]358 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
[159]359 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[157]360 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
361 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
362 }
363 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
[545]364 if (_rinexVers == 3) {
365 _out << "G 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
366 _out << "R 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
367 }
368 else {
369 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2"
370 " # / TYPES OF OBSERV" << endl;
371 }
[82]372 }
373 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
[125]374 _out << datTim.toString(" yyyy MM dd"
375 " hh mm ss.zzz0000").toAscii().data();
376 _out << " TIME OF FIRST OBS" << endl;
[337]377 QString hlp = (_format + QString(" %1").arg(_mountPoint.host() +
378 _mountPoint.path())).leftJustified(60, ' ', true);
[156]379 _out << hlp.toAscii().data() << "COMMENT" << endl;
[82]380 }
381 else {
382 _out << line.toAscii().data() << endl;
383 }
384 }
385 }
[78]386
[82]387 // Write Dummy Header
388 // ------------------
389 else {
390 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
391 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
392
[545]393 if (_rinexVers == 3) {
394 _out << " 3.00 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
395 }
396 else {
397 _out << " 2.11 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
398 }
[159]399 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[157]400 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
401 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
[86]402 _out.setf(ios::left);
[82]403 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
[354]404 _out << setw(60) << "unknown unknown" << "OBSERVER / AGENCY" << endl;
[82]405 _out << setw(20) << "unknown"
406 << setw(20) << "unknown"
407 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
408 _out << setw(20) << "unknown"
409 << setw(20) << "unknown"
410 << setw(20) << " " << "ANT # / TYPE" << endl;
[86]411 _out.unsetf(ios::left);
[82]412 _out << setw(14) << setprecision(4) << approxPos[0]
413 << setw(14) << setprecision(4) << approxPos[1]
414 << setw(14) << setprecision(4) << approxPos[2]
415 << " " << "APPROX POSITION XYZ" << endl;
416 _out << setw(14) << setprecision(4) << antennaNEU[0]
417 << setw(14) << setprecision(4) << antennaNEU[1]
418 << setw(14) << setprecision(4) << antennaNEU[2]
419 << " " << "ANTENNA: DELTA H/E/N" << endl;
[544]420 if (_rinexVers == 3) {
421 _out << "G 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
422 _out << "R 6 C1C L1C S1C C2P L2P S2P SYS / # / OBS TYPES" << endl;
423
424 }
425 else {
426 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
427 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2 # / TYPES OF OBSERV" << endl;
428 }
429 _out << datTim.toString(" yyyy MM dd"
[125]430 " hh mm ss.zzz0000").toAscii().data();
431 _out << " " << "TIME OF FIRST OBS" << endl;
[337]432 hlp = (_format + QString(" %1").arg(_mountPoint.host() +
433 _mountPoint.path())).leftJustified(60, ' ', true);
[156]434 _out << hlp.toAscii().data() << "COMMENT" << endl;
[366]435
[410]436 if (_nmea == "yes") {
437 hlp = ("NMEA LAT=" + _latitude + " " + "LONG=" + _longitude).leftJustified(60, ' ',true);
[366]438 _out << hlp.toAscii().data() << "COMMENT" << endl; }
439
[82]440 _out << " END OF HEADER" << endl;
441 }
[78]442
[77]443 _headerWritten = true;
444}
445
[73]446// Stores Observation into Internal Array
447////////////////////////////////////////////////////////////////////////////
448void bncRinex::deepCopy(const Observation* obs) {
[74]449 Observation* newObs = new Observation();
450 memcpy(newObs, obs, sizeof(*obs));
451 _obs.push_back(newObs);
[73]452}
453
454// Write One Epoch into the RINEX File
455////////////////////////////////////////////////////////////////////////////
[160]456void bncRinex::dumpEpoch(long maxTime) {
[73]457
[160]458 // Select observations older than maxTime
459 // --------------------------------------
460 QList<Observation*> dumpList;
461 QMutableListIterator<Observation*> mIt(_obs);
462 while (mIt.hasNext()) {
463 Observation* ob = mIt.next();
[238]464 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime - 0.05) {
[160]465 dumpList.push_back(ob);
466 mIt.remove();
467 }
468 }
469
[75]470 // Easy Return
471 // -----------
[160]472 if (dumpList.isEmpty()) {
[75]473 return;
474 }
475
476 // Time of Epoch
477 // -------------
[222]478 Observation* fObs = *dumpList.begin();
[267]479 QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
480 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs->GPSWeek,
481 floor(fObs->GPSWeeks+0.5));
[75]482
[130]483 // Close the file
484 // --------------
[267]485 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
[130]486 closeFile();
487 _headerWritten = false;
488 }
489
[78]490 // Write RINEX Header
491 // ------------------
492 if (!_headerWritten) {
[267]493 writeHeader(datTim, datTimNom);
[78]494 }
495
[353]496 double sec = double(datTim.time().second()) + fmod(fObs->GPSWeeks,1.0);
[75]497
[540]498 // RINEX Version 3
499 // ---------------
500 if (_rinexVers == 3) {
501 _out << datTim.toString("> yyyy MM dd hh mm ").toAscii().data()
502 << setw(10) << setprecision(7) << sec
503 << " " << 0 << setw(3) << dumpList.size() << endl;
[539]504
[540]505 QListIterator<Observation*> it(dumpList);
506 while (it.hasNext()) {
507 Observation* ob = it.next();
[543]508 _out << ob->satSys
509 << setw(2) << setfill('0') << ob->satNum << setfill(' ')
[541]510 << setw(14) << setprecision(3) << ob->C1 << " "
[542]511 << setw(14) << setprecision(3) << ob->L1 << " "
512 << setw(1) << ob->SNR1
[541]513 << setw(14) << setprecision(3) << ob->S1 << " "
514 << setw(14) << setprecision(3) << ob->P2 << " "
[542]515 << setw(14) << setprecision(3) << ob->L2 << " "
516 << setw(1) << ob->SNR2
[540]517 << setw(14) << setprecision(3) << ob->S2
518 << endl;
519 delete ob;
[75]520 }
[74]521 }
[75]522
[540]523 // RINEX Version 2
524 // ---------------
525 else {
526 _out << datTim.toString(" yy MM dd hh mm ").toAscii().data()
527 << setw(10) << setprecision(7) << sec
528 << " " << 0 << setw(3) << dumpList.size();
529
530 QListIterator<Observation*> it(dumpList); int iSat = 0;
531 while (it.hasNext()) {
532 iSat++;
533 Observation* ob = it.next();
534 _out << ob->satSys << setw(2) << ob->satNum;
535 if (iSat == 12 && it.hasNext()) {
536 _out << endl << " ";
537 iSat = 0;
538 }
539 }
[77]540 _out << endl;
[540]541
542 it.toFront();
543 while (it.hasNext()) {
544 Observation* ob = it.next();
545
546 char lli = ' ';
547 char snr = ' ';
548 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
549 _out << setw(14) << setprecision(3) << ob->C2 << lli << snr;
550 _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
551 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
552 _out << setw(14) << setprecision(3) << ob->L1 << lli
553 << setw(1) << ob->SNR1 << endl;
554 _out << setw(14) << setprecision(3) << ob->L2 << lli
555 << setw(1) << ob->SNR2;
556 _out << setw(14) << setprecision(3) << ob->S1 ;
557 _out << setw(16) << setprecision(3) << ob->S2 ;
558 _out << endl;
559
560 delete ob;
561 }
[75]562 }
563
[77]564 _out.flush();
[73]565}
566
[130]567// Close the Old RINEX File
568////////////////////////////////////////////////////////////////////////////
569void bncRinex::closeFile() {
[436]570 QMutexLocker locker(&_mutex);
[130]571 _out.close();
[132]572 if (!_rnxScriptName.isEmpty()) {
[436]573
[435]574#ifdef WIN32
575 QProcess::startDetached(_rnxScriptName, QStringList() << _fName) ;
576#else
[475]577 QProcess::startDetached("nohup", QStringList() << _rnxScriptName << _fName) ;
[435]578#endif
[436]579
[132]580 }
[130]581}
Note: See TracBrowser for help on using the repository browser.