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

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

* empty log message *

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