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