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

Last change on this file since 267 was 267, checked in by mervart, 18 years ago

* empty log message *

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