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

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

* empty log message *

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