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

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

* empty log message *

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