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

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

* empty log message *

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