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

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

* empty log message *

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