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

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

* empty log message *

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