| 1 |  | 
|---|
| 2 | /* ------------------------------------------------------------------------- | 
|---|
| 3 | * BKG NTRIP Server | 
|---|
| 4 | * ------------------------------------------------------------------------- | 
|---|
| 5 | * | 
|---|
| 6 | * Class:      bnsoutf | 
|---|
| 7 | * | 
|---|
| 8 | * Purpose:    Basis Class for File-Writers | 
|---|
| 9 | * | 
|---|
| 10 | * Author:     L. Mervart | 
|---|
| 11 | * | 
|---|
| 12 | * Created:    25-Apr-2008 | 
|---|
| 13 | * | 
|---|
| 14 | * Changes: | 
|---|
| 15 | * | 
|---|
| 16 | * -----------------------------------------------------------------------*/ | 
|---|
| 17 |  | 
|---|
| 18 | #include <math.h> | 
|---|
| 19 | #include <iomanip> | 
|---|
| 20 |  | 
|---|
| 21 | #include "bnsoutf.h" | 
|---|
| 22 | #include "bnssettings.h" | 
|---|
| 23 |  | 
|---|
| 24 | using namespace std; | 
|---|
| 25 |  | 
|---|
| 26 | // Constructor | 
|---|
| 27 | //////////////////////////////////////////////////////////////////////////// | 
|---|
| 28 | bnsoutf::bnsoutf(const QString& prep, const QString& ext, const QString& path, | 
|---|
| 29 | const QString& intr, int sampl) { | 
|---|
| 30 |  | 
|---|
| 31 | _headerWritten = false; | 
|---|
| 32 | _prep          = prep; | 
|---|
| 33 | _ext           = ext; | 
|---|
| 34 | _sampl         = sampl; | 
|---|
| 35 | _intr          = intr; | 
|---|
| 36 | _path          = path; | 
|---|
| 37 | expandEnvVar(_path); | 
|---|
| 38 | if ( _path.length() > 0 && _path[_path.length()-1] != QDir::separator() ) { | 
|---|
| 39 | _path += QDir::separator(); | 
|---|
| 40 | } | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | // Destructor | 
|---|
| 44 | //////////////////////////////////////////////////////////////////////////// | 
|---|
| 45 | bnsoutf::~bnsoutf() { | 
|---|
| 46 | closeFile(); | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | // Close the Old RINEX File | 
|---|
| 50 | //////////////////////////////////////////////////////////////////////////// | 
|---|
| 51 | void bnsoutf::closeFile() { | 
|---|
| 52 | _out.close(); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | // Next File Epoch (static) | 
|---|
| 56 | //////////////////////////////////////////////////////////////////////////// | 
|---|
| 57 | QString bnsoutf::nextEpochStr(const QDateTime& datTim, | 
|---|
| 58 | const QString& intStr, QDateTime* nextEpoch) { | 
|---|
| 59 |  | 
|---|
| 60 | QString epoStr; | 
|---|
| 61 |  | 
|---|
| 62 | QTime nextTime; | 
|---|
| 63 | QDate nextDate; | 
|---|
| 64 |  | 
|---|
| 65 | int indHlp = intStr.indexOf("min"); | 
|---|
| 66 |  | 
|---|
| 67 | if ( indHlp != -1) { | 
|---|
| 68 | int step = intStr.left(indHlp-1).toInt(); | 
|---|
| 69 | char ch = 'A' + datTim.time().hour(); | 
|---|
| 70 | epoStr = QString("_") + ch; | 
|---|
| 71 | if (datTim.time().minute() >= 60-step) { | 
|---|
| 72 | epoStr += QString("%1").arg(60-step, 2, 10, QChar('0')); | 
|---|
| 73 | if (datTim.time().hour() < 23) { | 
|---|
| 74 | nextTime.setHMS(datTim.time().hour() + 1 , 0, 0); | 
|---|
| 75 | nextDate = datTim.date(); | 
|---|
| 76 | } | 
|---|
| 77 | else { | 
|---|
| 78 | nextTime.setHMS(0, 0, 0); | 
|---|
| 79 | nextDate = datTim.date().addDays(1); | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 | else { | 
|---|
| 83 | for (int limit = step; limit <= 60-step; limit += step) { | 
|---|
| 84 | if (datTim.time().minute() < limit) { | 
|---|
| 85 | epoStr += QString("%1").arg(limit-step, 2, 10, QChar('0')); | 
|---|
| 86 | nextTime.setHMS(datTim.time().hour(), limit, 0); | 
|---|
| 87 | nextDate = datTim.date(); | 
|---|
| 88 | break; | 
|---|
| 89 | } | 
|---|
| 90 | } | 
|---|
| 91 | } | 
|---|
| 92 | } | 
|---|
| 93 | else if (intStr == "1 hour") { | 
|---|
| 94 | char ch = 'A' + datTim.time().hour(); | 
|---|
| 95 | epoStr = QString("_") + ch; | 
|---|
| 96 | if (datTim.time().hour() < 23) { | 
|---|
| 97 | nextTime.setHMS(datTim.time().hour() + 1 , 0, 0); | 
|---|
| 98 | nextDate = datTim.date(); | 
|---|
| 99 | } | 
|---|
| 100 | else { | 
|---|
| 101 | nextTime.setHMS(0, 0, 0); | 
|---|
| 102 | nextDate = datTim.date().addDays(1); | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 | else { | 
|---|
| 106 | epoStr = ""; | 
|---|
| 107 | nextTime.setHMS(0, 0, 0); | 
|---|
| 108 | nextDate = datTim.date().addDays(1); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | if (nextEpoch) { | 
|---|
| 112 | *nextEpoch = QDateTime(nextDate, nextTime, Qt::UTC); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | return epoStr; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | // File Name according to RINEX Standards | 
|---|
| 119 | //////////////////////////////////////////////////////////////////////////// | 
|---|
| 120 | void bnsoutf::resolveFileName(int GPSweek, const QDateTime& datTim) { | 
|---|
| 121 |  | 
|---|
| 122 | QString epoStr = nextEpochStr(datTim, _intr, &_nextCloseEpoch); | 
|---|
| 123 |  | 
|---|
| 124 | int dayOfWeek = datTim.date().dayOfWeek(); | 
|---|
| 125 | if (dayOfWeek == 7) { | 
|---|
| 126 | dayOfWeek = 0; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | _fName = (_path + _prep | 
|---|
| 130 | + QString("%1").arg(GPSweek) | 
|---|
| 131 | + QString("%1").arg(dayOfWeek) | 
|---|
| 132 | + epoStr | 
|---|
| 133 | + _ext).toAscii(); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | // Write One Epoch | 
|---|
| 137 | //////////////////////////////////////////////////////////////////////////// | 
|---|
| 138 | t_irc bnsoutf::write(int GPSweek, double GPSweeks, const QString&, | 
|---|
| 139 | const ColumnVector&) { | 
|---|
| 140 |  | 
|---|
| 141 | if (_sampl != 0 && fmod(GPSweeks, _sampl) != 0.0) { | 
|---|
| 142 | return failure; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | QDateTime datTim = dateAndTimeFromGPSweek(GPSweek, GPSweeks); | 
|---|
| 146 |  | 
|---|
| 147 | // Close the file | 
|---|
| 148 | // -------------- | 
|---|
| 149 | if (_nextCloseEpoch.isValid() && datTim >= _nextCloseEpoch) { | 
|---|
| 150 | closeFile(); | 
|---|
| 151 | _headerWritten = false; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | // Write Header | 
|---|
| 155 | // ------------ | 
|---|
| 156 | if (!_headerWritten) { | 
|---|
| 157 | resolveFileName(GPSweek, datTim); | 
|---|
| 158 | _out.setf(ios::showpoint | ios::fixed); | 
|---|
| 159 | bnsSettings settings; | 
|---|
| 160 | if (QFile::exists(_fName) && | 
|---|
| 161 | Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) { | 
|---|
| 162 | _out.open(_fName.data(), ios::out | ios::app); | 
|---|
| 163 | } | 
|---|
| 164 | else { | 
|---|
| 165 | _out.open(_fName.data()); | 
|---|
| 166 | writeHeader(datTim); | 
|---|
| 167 | } | 
|---|
| 168 | _headerWritten = true; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | return success; | 
|---|
| 172 | } | 
|---|