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
RevLine 
[73]1
2/* -------------------------------------------------------------------------
[93]3 * BKG NTRIP Client
[73]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
[82]18#include <QSettings>
19#include <QDir>
[156]20#include <QUrl>
[82]21#include <QDate>
22#include <QFile>
23#include <QTextStream>
[75]24#include <iomanip>
[221]25#include <math.h>
[75]26
[73]27#include "bncrinex.h"
[157]28#include "bncapp.h"
[82]29#include "bncutils.h"
[126]30#include "bncconst.h"
[223]31#include "RTCM3/rtcm3torinex.h"
[75]32
33using namespace std;
34
[73]35// Constructor
36////////////////////////////////////////////////////////////////////////////
[256]37bncRinex::bncRinex(const char* StatID, const QUrl& mountPoint) {
[77]38 _statID = StatID;
[256]39 _mountPoint = mountPoint;
[77]40 _headerWritten = false;
[82]41 readSkeleton();
[132]42
43 QSettings settings;
44 _rnxScriptName = settings.value("rnxScript").toString();
45 expandEnvVar(_rnxScriptName);
[153]46
[157]47 _pgmName = ((bncApp*)qApp)->bncVersion().leftJustified(20, ' ', true);
[158]48 _userName = QString("${USER}");
[157]49 expandEnvVar(_userName);
[158]50 _userName = _userName.leftJustified(20, ' ', true);
[73]51}
52
53// Destructor
54////////////////////////////////////////////////////////////////////////////
55bncRinex::~bncRinex() {
[77]56 _out.close();
[73]57}
58
[82]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////////////////////////////////////////////////////////////////////////////
[125]89void bncRinex::resolveFileName(const QDateTime& datTim) {
[82]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
[127]99 QString intStr = settings.value("rnxIntr").toString();
100 QString hlpStr;
[129]101
102 QTime nextTime;
103 QDate nextDate;
104
[204]105 int indHlp = intStr.indexOf("min");
106
107 if ( indHlp != -1) {
108 int step = intStr.left(indHlp-1).toInt();
[127]109 char ch = 'A' + datTim.time().hour();
110 hlpStr = ch;
[204]111 if (datTim.time().minute() >= 60-step) {
112 hlpStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
[199]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 {
[204]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 }
[199]130 }
131 }
132 }
[127]133 else if (intStr == "1 hour") {
134 char ch = 'A' + datTim.time().hour();
135 hlpStr = ch;
[129]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 }
[127]144 }
145 else {
146 hlpStr = "0";
[129]147 nextTime.setHMS(0, 0, 0);
148 nextDate = datTim.date().addDays(1);
[127]149 }
[129]150 _nextCloseEpoch = QDateTime(nextDate, nextTime);
[82]151
[183]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 +
[127]170 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
[183]171 hlpStr + distStr +
[127]172 datTim.toString(".yyO");
[82]173
174 _fName = path.toAscii();
175}
176
[77]177// Write RINEX Header
178////////////////////////////////////////////////////////////////////////////
[125]179void bncRinex::writeHeader(const QDateTime& datTim) {
[77]180
181 // Open the Output File
182 // --------------------
[125]183 resolveFileName(datTim);
[82]184 _out.open(_fName.data());
[85]185 _out.setf(ios::showpoint | ios::fixed);
[77]186
[82]187 // Copy Skeleton Header
188 // --------------------
189 if (_headerLines.size() > 0) {
190 QStringListIterator it(_headerLines);
191 while (it.hasNext()) {
192 QString line = it.next();
[157]193 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
[159]194 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[157]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) {
[225]199 _out << " 5 C1 P1 P2 L1 L2"
200 " # / TYPES OF OBSERV" << endl;
[82]201 }
202 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
[125]203 _out << datTim.toString(" yyyy MM dd"
204 " hh mm ss.zzz0000").toAscii().data();
205 _out << " TIME OF FIRST OBS" << endl;
[256]206 QString hlp = QString("STREAM %1").arg(_mountPoint.host() + _mountPoint.path())
[156]207 .leftJustified(60, ' ', true);
208 _out << hlp.toAscii().data() << "COMMENT" << endl;
[82]209 }
210 else {
211 _out << line.toAscii().data() << endl;
212 }
213 }
214 }
[78]215
[82]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
[255]222 _out << " 2.10 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
[159]223 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[157]224 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
225 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
[86]226 _out.setf(ios::left);
[82]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;
[86]235 _out.unsetf(ios::left);
[82]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;
[225]245 _out << " 5 C1 P1 P2 L1 L2 # / TYPES OF OBSERV" << endl;
[125]246 _out << datTim.toString(" yyyy MM dd"
247 " hh mm ss.zzz0000").toAscii().data();
248 _out << " " << "TIME OF FIRST OBS" << endl;
[256]249 hlp = QString("STREAM %1").arg(_mountPoint.host() + _mountPoint.path())
[157]250 .leftJustified(60, ' ', true);
[156]251 _out << hlp.toAscii().data() << "COMMENT" << endl;
[82]252 _out << " END OF HEADER" << endl;
253 }
[78]254
[77]255 _headerWritten = true;
256}
257
[73]258// Stores Observation into Internal Array
259////////////////////////////////////////////////////////////////////////////
260void bncRinex::deepCopy(const Observation* obs) {
[74]261 Observation* newObs = new Observation();
262 memcpy(newObs, obs, sizeof(*obs));
263 _obs.push_back(newObs);
[73]264}
265
266// Write One Epoch into the RINEX File
267////////////////////////////////////////////////////////////////////////////
[160]268void bncRinex::dumpEpoch(long maxTime) {
[73]269
[160]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();
[238]276 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime - 0.05) {
[160]277 dumpList.push_back(ob);
278 mIt.remove();
279 }
280 }
281
[75]282 // Easy Return
283 // -----------
[160]284 if (dumpList.isEmpty()) {
[75]285 return;
286 }
287
288 // Time of Epoch
289 // -------------
[222]290 Observation* fObs = *dumpList.begin();
291 QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
[75]292
[130]293 // Close the file
294 // --------------
295 if (_nextCloseEpoch.isValid() && datTim >= _nextCloseEpoch) {
296 closeFile();
297 _headerWritten = false;
298 }
299
[78]300 // Write RINEX Header
301 // ------------------
302 if (!_headerWritten) {
[125]303 writeHeader(datTim);
[78]304 }
305
[131]306 _out << datTim.toString(" yy MM dd hh mm ss.zzz0000").toAscii().data()
[160]307 << " " << 0 << setw(3) << dumpList.size();
[75]308
[160]309 QListIterator<Observation*> it(dumpList); int iSat = 0;
[74]310 while (it.hasNext()) {
[75]311 iSat++;
312 Observation* ob = it.next();
[223]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 {
[224]321 _out << "R" << setw(2) << prn % 100;
[223]322 }
[75]323 if (iSat == 12 && it.hasNext()) {
[77]324 _out << endl << " ";
[75]325 iSat = 0;
326 }
[74]327 }
[77]328 _out << endl;
[75]329
330 it.toFront();
331 while (it.hasNext()) {
332 Observation* ob = it.next();
[77]333
334 char lli = ' ';
335 char snr = ' ';
336 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
[225]337 _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
[77]338 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
[222]339 _out << setw(14) << setprecision(3) << ob->L1 << lli << snr;
340 _out << setw(14) << setprecision(3) << ob->L2 << lli << snr;
[77]341 _out << endl;
342
[75]343 delete ob;
344 }
345
[77]346 _out.flush();
[73]347}
348
[130]349// Close the Old RINEX File
350////////////////////////////////////////////////////////////////////////////
351void bncRinex::closeFile() {
352 _out.close();
[132]353 if (!_rnxScriptName.isEmpty()) {
354 _rnxScript.start(_rnxScriptName, QStringList() << _fName);
355 }
[130]356}
Note: See TracBrowser for help on using the repository browser.