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

Last change on this file since 259 was 256, checked in by mervart, 20 years ago

* empty log message *

File size: 11.0 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
181 // Open the Output File
182 // --------------------
183 resolveFileName(datTim);
184 _out.open(_fName.data());
185 _out.setf(ios::showpoint | ios::fixed);
186
187 // Copy Skeleton Header
188 // --------------------
189 if (_headerLines.size() > 0) {
190 QStringListIterator it(_headerLines);
191 while (it.hasNext()) {
192 QString line = it.next();
193 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
194 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
195 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
196 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
197 }
198 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
199 _out << " 5 C1 P1 P2 L1 L2"
200 " # / TYPES OF OBSERV" << endl;
201 }
202 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
203 _out << datTim.toString(" yyyy MM dd"
204 " hh mm ss.zzz0000").toAscii().data();
205 _out << " TIME OF FIRST OBS" << endl;
206 QString hlp = QString("STREAM %1").arg(_mountPoint.host() + _mountPoint.path())
207 .leftJustified(60, ' ', true);
208 _out << hlp.toAscii().data() << "COMMENT" << endl;
209 }
210 else {
211 _out << line.toAscii().data() << endl;
212 }
213 }
214 }
215
216 // Write Dummy Header
217 // ------------------
218 else {
219 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
220 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
221
222 _out << " 2.10 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
223 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
224 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
225 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
226 _out.setf(ios::left);
227 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
228 _out << setw(60) << "BKG" << "OBSERVER / AGENCY" << endl;
229 _out << setw(20) << "unknown"
230 << setw(20) << "unknown"
231 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
232 _out << setw(20) << "unknown"
233 << setw(20) << "unknown"
234 << setw(20) << " " << "ANT # / TYPE" << endl;
235 _out.unsetf(ios::left);
236 _out << setw(14) << setprecision(4) << approxPos[0]
237 << setw(14) << setprecision(4) << approxPos[1]
238 << setw(14) << setprecision(4) << approxPos[2]
239 << " " << "APPROX POSITION XYZ" << endl;
240 _out << setw(14) << setprecision(4) << antennaNEU[0]
241 << setw(14) << setprecision(4) << antennaNEU[1]
242 << setw(14) << setprecision(4) << antennaNEU[2]
243 << " " << "ANTENNA: DELTA H/E/N" << endl;
244 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
245 _out << " 5 C1 P1 P2 L1 L2 # / TYPES OF OBSERV" << endl;
246 _out << datTim.toString(" yyyy MM dd"
247 " hh mm ss.zzz0000").toAscii().data();
248 _out << " " << "TIME OF FIRST OBS" << endl;
249 hlp = QString("STREAM %1").arg(_mountPoint.host() + _mountPoint.path())
250 .leftJustified(60, ' ', true);
251 _out << hlp.toAscii().data() << "COMMENT" << endl;
252 _out << " END OF HEADER" << endl;
253 }
254
255 _headerWritten = true;
256}
257
258// Stores Observation into Internal Array
259////////////////////////////////////////////////////////////////////////////
260void bncRinex::deepCopy(const Observation* obs) {
261 Observation* newObs = new Observation();
262 memcpy(newObs, obs, sizeof(*obs));
263 _obs.push_back(newObs);
264}
265
266// Write One Epoch into the RINEX File
267////////////////////////////////////////////////////////////////////////////
268void bncRinex::dumpEpoch(long maxTime) {
269
270 // Select observations older than maxTime
271 // --------------------------------------
272 QList<Observation*> dumpList;
273 QMutableListIterator<Observation*> mIt(_obs);
274 while (mIt.hasNext()) {
275 Observation* ob = mIt.next();
276 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime - 0.05) {
277 dumpList.push_back(ob);
278 mIt.remove();
279 }
280 }
281
282 // Easy Return
283 // -----------
284 if (dumpList.isEmpty()) {
285 return;
286 }
287
288 // Time of Epoch
289 // -------------
290 Observation* fObs = *dumpList.begin();
291 QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
292
293 // Close the file
294 // --------------
295 if (_nextCloseEpoch.isValid() && datTim >= _nextCloseEpoch) {
296 closeFile();
297 _headerWritten = false;
298 }
299
300 // Write RINEX Header
301 // ------------------
302 if (!_headerWritten) {
303 writeHeader(datTim);
304 }
305
306 _out << datTim.toString(" yy MM dd hh mm ss.zzz0000").toAscii().data()
307 << " " << 0 << setw(3) << dumpList.size();
308
309 QListIterator<Observation*> it(dumpList); int iSat = 0;
310 while (it.hasNext()) {
311 iSat++;
312 Observation* ob = it.next();
313 int prn = ob->SVPRN;
314 if (prn <= PRN_GPS_END) {
315 _out << "G" << setw(2) << prn;
316 }
317 else if (prn >= PRN_GLONASS_START && prn <= PRN_GLONASS_END) {
318 _out << "R" << setw(2) << prn - PRN_GLONASS_START + 1;
319 }
320 else {
321 _out << "R" << setw(2) << prn % 100;
322 }
323 if (iSat == 12 && it.hasNext()) {
324 _out << endl << " ";
325 iSat = 0;
326 }
327 }
328 _out << endl;
329
330 it.toFront();
331 while (it.hasNext()) {
332 Observation* ob = it.next();
333
334 char lli = ' ';
335 char snr = ' ';
336 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
337 _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
338 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
339 _out << setw(14) << setprecision(3) << ob->L1 << lli << snr;
340 _out << setw(14) << setprecision(3) << ob->L2 << lli << snr;
341 _out << endl;
342
343 delete ob;
344 }
345
346 _out.flush();
347}
348
349// Close the Old RINEX File
350////////////////////////////////////////////////////////////////////////////
351void bncRinex::closeFile() {
352 _out.close();
353 if (!_rnxScriptName.isEmpty()) {
354 _rnxScript.start(_rnxScriptName, QStringList() << _fName);
355 }
356}
Note: See TracBrowser for help on using the repository browser.