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

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

* empty log message *

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