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

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

* empty log message *

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