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

Last change on this file since 464 was 464, checked in by weber, 17 years ago

* empty log message *

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