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

Last change on this file since 454 was 446, checked in by mervart, 19 years ago

* empty log message *

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