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

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

* empty log message *

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