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

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

* empty log message *

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