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

Last change on this file since 306 was 304, checked in by mervart, 19 years ago

* empty log message *

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