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

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

* empty log message *

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