[280] | 1 | // Part of BNC, a utility for retrieving decoding and
|
---|
[464] | 2 | // converting GNSS data streams from NTRIP broadcasters.
|
---|
[280] | 3 | //
|
---|
[464] | 4 | // Copyright (C) 2007
|
---|
[280] | 5 | // German Federal Agency for Cartography and Geodesy (BKG)
|
---|
| 6 | // http://www.bkg.bund.de
|
---|
[464] | 7 | // Czech Technical University Prague, Department of Geodesy
|
---|
[280] | 8 | // http://www.fsv.cvut.cz
|
---|
| 9 | //
|
---|
| 10 | // Email: euref-ip@bkg.bund.de
|
---|
| 11 | //
|
---|
| 12 | // This program is free software; you can redistribute it and/or
|
---|
| 13 | // modify it under the terms of the GNU General Public License
|
---|
| 14 | // as published by the Free Software Foundation, version 2.
|
---|
| 15 | //
|
---|
| 16 | // This program is distributed in the hope that it will be useful,
|
---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | // GNU General Public License for more details.
|
---|
| 20 | //
|
---|
| 21 | // You should have received a copy of the GNU General Public License
|
---|
| 22 | // along with this program; if not, write to the Free Software
|
---|
| 23 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
[83] | 24 |
|
---|
| 25 | /* -------------------------------------------------------------------------
|
---|
[93] | 26 | * BKG NTRIP Client
|
---|
[83] | 27 | * -------------------------------------------------------------------------
|
---|
| 28 | *
|
---|
| 29 | * Class: bncutils
|
---|
| 30 | *
|
---|
| 31 | * Purpose: Auxiliary Functions
|
---|
| 32 | *
|
---|
| 33 | * Author: L. Mervart
|
---|
| 34 | *
|
---|
| 35 | * Created: 30-Aug-2006
|
---|
| 36 | *
|
---|
| 37 | * Changes:
|
---|
| 38 | *
|
---|
| 39 | * -----------------------------------------------------------------------*/
|
---|
| 40 |
|
---|
[124] | 41 | #include <iostream>
|
---|
[218] | 42 | #include <ctime>
|
---|
[221] | 43 | #include <math.h>
|
---|
[124] | 44 |
|
---|
[83] | 45 | #include <QRegExp>
|
---|
| 46 | #include <QStringList>
|
---|
[271] | 47 | #include <QDateTime>
|
---|
[83] | 48 |
|
---|
| 49 | #include "bncutils.h"
|
---|
[1155] | 50 | #include "bncapp.h"
|
---|
[83] | 51 |
|
---|
[124] | 52 | using namespace std;
|
---|
| 53 |
|
---|
[1381] | 54 | //
|
---|
| 55 | ////////////////////////////////////////////////////////////////////////////
|
---|
[83] | 56 | void expandEnvVar(QString& str) {
|
---|
| 57 |
|
---|
| 58 | QRegExp rx("(\\$\\{.+\\})");
|
---|
| 59 |
|
---|
| 60 | if (rx.indexIn(str) != -1) {
|
---|
| 61 | QStringListIterator it(rx.capturedTexts());
|
---|
| 62 | if (it.hasNext()) {
|
---|
| 63 | QString rxStr = it.next();
|
---|
| 64 | QString envVar = rxStr.mid(2,rxStr.length()-3);
|
---|
| 65 | str.replace(rxStr, qgetenv(envVar.toAscii()));
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | }
|
---|
[124] | 70 |
|
---|
[1381] | 71 | //
|
---|
| 72 | ////////////////////////////////////////////////////////////////////////////
|
---|
[124] | 73 | QDateTime dateAndTimeFromGPSweek(int GPSWeek, double GPSWeeks) {
|
---|
| 74 |
|
---|
| 75 | static const QDate zeroEpoch(1980, 1, 6);
|
---|
| 76 |
|
---|
| 77 | QDate date(zeroEpoch);
|
---|
| 78 | QTime time(0,0,0,0);
|
---|
| 79 |
|
---|
| 80 | int weekDays = int(GPSWeeks) / 86400;
|
---|
| 81 | date = date.addDays( GPSWeek * 7 + weekDays );
|
---|
| 82 | time = time.addMSecs( int( (GPSWeeks - 86400 * weekDays) * 1e3 ) );
|
---|
| 83 |
|
---|
| 84 | return QDateTime(date,time);
|
---|
| 85 | }
|
---|
[210] | 86 |
|
---|
[1381] | 87 | //
|
---|
| 88 | ////////////////////////////////////////////////////////////////////////////
|
---|
[218] | 89 | void currentGPSWeeks(int& week, double& sec) {
|
---|
[210] | 90 |
|
---|
[1942] | 91 | QDateTime currDateTimeGPS;
|
---|
[1155] | 92 |
|
---|
| 93 | if ( ((bncApp*) qApp)->_currentDateAndTimeGPS ) {
|
---|
[1942] | 94 | currDateTimeGPS = *(((bncApp*) qApp)->_currentDateAndTimeGPS);
|
---|
[1155] | 95 | }
|
---|
| 96 | else {
|
---|
[1942] | 97 | currDateTimeGPS = QDateTime::currentDateTime().toUTC();
|
---|
| 98 | QDate hlp = currDateTimeGPS.date();
|
---|
| 99 | currDateTimeGPS = currDateTimeGPS.addSecs(gnumleap(hlp.year(),
|
---|
| 100 | hlp.month(), hlp.day()));
|
---|
[1155] | 101 | }
|
---|
| 102 |
|
---|
[1942] | 103 | QDate currDateGPS = currDateTimeGPS.date();
|
---|
| 104 | QTime currTimeGPS = currDateTimeGPS.time();
|
---|
[210] | 105 |
|
---|
[1942] | 106 | week = int( (double(currDateGPS.toJulianDay()) - 2444244.5) / 7 );
|
---|
[1036] | 107 |
|
---|
[1942] | 108 | sec = (currDateGPS.dayOfWeek() % 7) * 24.0 * 3600.0 +
|
---|
| 109 | currTimeGPS.hour() * 3600.0 +
|
---|
| 110 | currTimeGPS.minute() * 60.0 +
|
---|
| 111 | currTimeGPS.second() +
|
---|
| 112 | currTimeGPS.msec() / 1000.0;
|
---|
[1036] | 113 | }
|
---|
[1154] | 114 |
|
---|
[1381] | 115 | //
|
---|
| 116 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1154] | 117 | QDateTime currentDateAndTimeGPS() {
|
---|
[2530] | 118 | if ( ((bncApp*) qApp)->_currentDateAndTimeGPS ) {
|
---|
| 119 | return *(((bncApp*) qApp)->_currentDateAndTimeGPS);
|
---|
| 120 | }
|
---|
| 121 | else {
|
---|
| 122 | int GPSWeek;
|
---|
| 123 | double GPSWeeks;
|
---|
| 124 | currentGPSWeeks(GPSWeek, GPSWeeks);
|
---|
| 125 | return dateAndTimeFromGPSweek(GPSWeek, GPSWeeks);
|
---|
| 126 | }
|
---|
[1154] | 127 | }
|
---|
| 128 |
|
---|
[1381] | 129 | //
|
---|
| 130 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1595] | 131 | QByteArray ggaString(const QByteArray& latitude,
|
---|
| 132 | const QByteArray& longitude,
|
---|
| 133 | const QByteArray& height) {
|
---|
[1381] | 134 |
|
---|
| 135 | double lat = strtod(latitude,NULL);
|
---|
| 136 | double lon = strtod(longitude,NULL);
|
---|
[1595] | 137 | double hei = strtod(height,NULL);
|
---|
[1381] | 138 |
|
---|
| 139 | const char* flagN="N";
|
---|
| 140 | const char* flagE="E";
|
---|
| 141 | if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
|
---|
| 142 | if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
|
---|
| 143 | if (lon < -180.) {lon=(lon+360.); flagE="E";}
|
---|
| 144 | if (lat < 0.) {lat=lat*(-1.); flagN="S";}
|
---|
| 145 | QTime ttime(QDateTime::currentDateTime().toUTC().time());
|
---|
| 146 | int lat_deg = (int)lat;
|
---|
| 147 | double lat_min=(lat-lat_deg)*60.;
|
---|
| 148 | int lon_deg = (int)lon;
|
---|
| 149 | double lon_min=(lon-lon_deg)*60.;
|
---|
| 150 | int hh = 0 , mm = 0;
|
---|
| 151 | double ss = 0.0;
|
---|
| 152 | hh=ttime.hour();
|
---|
| 153 | mm=ttime.minute();
|
---|
| 154 | ss=(double)ttime.second()+0.001*ttime.msec();
|
---|
| 155 | QString gga;
|
---|
[1506] | 156 | gga += "GPGGA,";
|
---|
[1381] | 157 | gga += QString("%1%2%3,").arg((int)hh, 2, 10, QLatin1Char('0')).arg((int)mm, 2, 10, QLatin1Char('0')).arg((int)ss, 2, 10, QLatin1Char('0'));
|
---|
| 158 | gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
| 159 | gga += flagN;
|
---|
| 160 | gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
[1595] | 161 | gga += flagE + QString(",1,05,1.00");
|
---|
[1599] | 162 | gga += QString(",%1,").arg(hei, 2, 'f', 1);
|
---|
[1595] | 163 | gga += QString("M,10.000,M,,");
|
---|
[1381] | 164 | int xori;
|
---|
| 165 | char XOR = 0;
|
---|
| 166 | char *Buff =gga.toAscii().data();
|
---|
| 167 | int iLen = strlen(Buff);
|
---|
| 168 | for (xori = 0; xori < iLen; xori++) {
|
---|
| 169 | XOR ^= (char)Buff[xori];
|
---|
| 170 | }
|
---|
[1506] | 171 | gga = "$" + gga + QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
|
---|
[1381] | 172 |
|
---|
[1387] | 173 | return gga.toAscii();
|
---|
[1381] | 174 | }
|
---|
[2043] | 175 |
|
---|
| 176 | //
|
---|
| 177 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 178 | void RSW_to_XYZ(const ColumnVector& rr, const ColumnVector& vv,
|
---|
| 179 | const ColumnVector& rsw, ColumnVector& xyz) {
|
---|
| 180 |
|
---|
| 181 | ColumnVector along = vv / vv.norm_Frobenius();
|
---|
| 182 | ColumnVector cross = crossproduct(rr, vv); cross /= cross.norm_Frobenius();
|
---|
| 183 | ColumnVector radial = crossproduct(along, cross);
|
---|
| 184 |
|
---|
| 185 | Matrix RR(3,3);
|
---|
| 186 | RR.Column(1) = radial;
|
---|
| 187 | RR.Column(2) = along;
|
---|
| 188 | RR.Column(3) = cross;
|
---|
| 189 |
|
---|
| 190 | xyz = RR * rsw;
|
---|
| 191 | }
|
---|
[2063] | 192 |
|
---|
[2988] | 193 | // Transformation xyz --> radial, along track, out-of-plane
|
---|
| 194 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 195 | void XYZ_to_RSW(const ColumnVector& rr, const ColumnVector& vv,
|
---|
| 196 | const ColumnVector& xyz, ColumnVector& rsw) {
|
---|
| 197 |
|
---|
| 198 | ColumnVector along = vv / vv.norm_Frobenius();
|
---|
| 199 | ColumnVector cross = crossproduct(rr, vv); cross /= cross.norm_Frobenius();
|
---|
| 200 | ColumnVector radial = crossproduct(along, cross);
|
---|
| 201 |
|
---|
| 202 | rsw.ReSize(3);
|
---|
| 203 | rsw(1) = DotProduct(xyz, radial);
|
---|
| 204 | rsw(2) = DotProduct(xyz, along);
|
---|
| 205 | rsw(3) = DotProduct(xyz, cross);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[2063] | 208 | // Rectangular Coordinates -> Ellipsoidal Coordinates
|
---|
| 209 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 210 | t_irc xyz2ell(const double* XYZ, double* Ell) {
|
---|
| 211 |
|
---|
| 212 | const double bell = t_CST::aell*(1.0-1.0/t_CST::fInv) ;
|
---|
| 213 | const double e2 = (t_CST::aell*t_CST::aell-bell*bell)/(t_CST::aell*t_CST::aell) ;
|
---|
| 214 | const double e2c = (t_CST::aell*t_CST::aell-bell*bell)/(bell*bell) ;
|
---|
| 215 |
|
---|
| 216 | double nn, ss, zps, hOld, phiOld, theta, sin3, cos3;
|
---|
| 217 |
|
---|
| 218 | ss = sqrt(XYZ[0]*XYZ[0]+XYZ[1]*XYZ[1]) ;
|
---|
| 219 | zps = XYZ[2]/ss ;
|
---|
| 220 | theta = atan( (XYZ[2]*t_CST::aell) / (ss*bell) );
|
---|
| 221 | sin3 = sin(theta) * sin(theta) * sin(theta);
|
---|
| 222 | cos3 = cos(theta) * cos(theta) * cos(theta);
|
---|
| 223 |
|
---|
| 224 | // Closed formula
|
---|
| 225 | Ell[0] = atan( (XYZ[2] + e2c * bell * sin3) / (ss - e2 * t_CST::aell * cos3) );
|
---|
| 226 | Ell[1] = atan2(XYZ[1],XYZ[0]) ;
|
---|
| 227 | nn = t_CST::aell/sqrt(1.0-e2*sin(Ell[0])*sin(Ell[0])) ;
|
---|
| 228 | Ell[2] = ss / cos(Ell[0]) - nn;
|
---|
| 229 |
|
---|
| 230 | const int MAXITER = 100;
|
---|
| 231 | for (int ii = 1; ii <= MAXITER; ii++) {
|
---|
| 232 | nn = t_CST::aell/sqrt(1.0-e2*sin(Ell[0])*sin(Ell[0])) ;
|
---|
| 233 | hOld = Ell[2] ;
|
---|
| 234 | phiOld = Ell[0] ;
|
---|
| 235 | Ell[2] = ss/cos(Ell[0])-nn ;
|
---|
| 236 | Ell[0] = atan(zps/(1.0-e2*nn/(nn+Ell[2]))) ;
|
---|
| 237 | if ( fabs(phiOld-Ell[0]) <= 1.0e-11 && fabs(hOld-Ell[2]) <= 1.0e-5 ) {
|
---|
| 238 | return success;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | return failure;
|
---|
| 243 | }
|
---|
[2065] | 244 |
|
---|
| 245 | // Rectangular Coordinates -> North, East, Up Components
|
---|
| 246 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 247 | void xyz2neu(const double* Ell, const double* xyz, double* neu) {
|
---|
| 248 |
|
---|
| 249 | double sinPhi = sin(Ell[0]);
|
---|
| 250 | double cosPhi = cos(Ell[0]);
|
---|
| 251 | double sinLam = sin(Ell[1]);
|
---|
| 252 | double cosLam = cos(Ell[1]);
|
---|
| 253 |
|
---|
| 254 | neu[0] = - sinPhi*cosLam * xyz[0]
|
---|
| 255 | - sinPhi*sinLam * xyz[1]
|
---|
| 256 | + cosPhi * xyz[2];
|
---|
| 257 |
|
---|
| 258 | neu[1] = - sinLam * xyz[0]
|
---|
| 259 | + cosLam * xyz[1];
|
---|
| 260 |
|
---|
| 261 | neu[2] = + cosPhi*cosLam * xyz[0]
|
---|
| 262 | + cosPhi*sinLam * xyz[1]
|
---|
| 263 | + sinPhi * xyz[2];
|
---|
| 264 | }
|
---|
[2221] | 265 |
|
---|
[2582] | 266 | // North, East, Up Components -> Rectangular Coordinates
|
---|
| 267 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 268 | void neu2xyz(const double* Ell, const double* neu, double* xyz) {
|
---|
| 269 |
|
---|
| 270 | double sinPhi = sin(Ell[0]);
|
---|
| 271 | double cosPhi = cos(Ell[0]);
|
---|
| 272 | double sinLam = sin(Ell[1]);
|
---|
| 273 | double cosLam = cos(Ell[1]);
|
---|
| 274 |
|
---|
| 275 | xyz[0] = - sinPhi*cosLam * neu[0]
|
---|
| 276 | - sinLam * neu[1]
|
---|
| 277 | + cosPhi*cosLam * neu[2];
|
---|
| 278 |
|
---|
| 279 | xyz[1] = - sinPhi*sinLam * neu[0]
|
---|
| 280 | + cosLam * neu[1]
|
---|
| 281 | + cosPhi*sinLam * neu[2];
|
---|
| 282 |
|
---|
| 283 | xyz[2] = + cosPhi * neu[0]
|
---|
| 284 | + sinPhi * neu[2];
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[2221] | 287 | // Fourth order Runge-Kutta numerical integrator for ODEs
|
---|
| 288 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 289 | ColumnVector rungeKutta4(
|
---|
| 290 | double xi, // the initial x-value
|
---|
| 291 | const ColumnVector& yi, // vector of the initial y-values
|
---|
| 292 | double dx, // the step size for the integration
|
---|
[2556] | 293 | double* acc, // aditional acceleration
|
---|
| 294 | ColumnVector (*der)(double x, const ColumnVector& y, double* acc)
|
---|
[2221] | 295 | // A pointer to a function that computes the
|
---|
| 296 | // derivative of a function at a point (x,y)
|
---|
| 297 | ) {
|
---|
| 298 |
|
---|
[2556] | 299 | ColumnVector k1 = der(xi , yi , acc) * dx;
|
---|
| 300 | ColumnVector k2 = der(xi+dx/2.0, yi+k1/2.0, acc) * dx;
|
---|
| 301 | ColumnVector k3 = der(xi+dx/2.0, yi+k2/2.0, acc) * dx;
|
---|
| 302 | ColumnVector k4 = der(xi+dx , yi+k3 , acc) * dx;
|
---|
[2221] | 303 |
|
---|
| 304 | ColumnVector yf = yi + k1/6.0 + k2/3.0 + k3/3.0 + k4/6.0;
|
---|
| 305 |
|
---|
| 306 | return yf;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
[3044] | 309 | //
|
---|
| 310 | ////////////////////////////////////////////////////////////////////////////
|
---|
[3171] | 311 | double djul(int jj, int mm, double tt) {
|
---|
| 312 | int ii, kk;
|
---|
| 313 | double djul ;
|
---|
| 314 | if( mm <= 2 ) {
|
---|
| 315 | jj = jj - 1;
|
---|
| 316 | mm = mm + 12;
|
---|
| 317 | }
|
---|
| 318 | ii = jj/100;
|
---|
| 319 | kk = 2 - ii + ii/4;
|
---|
| 320 | djul = (365.25*jj - fmod( 365.25*jj, 1.0 )) - 679006.0;
|
---|
| 321 | djul = djul + floor( 30.6001*(mm + 1) ) + tt + kk;
|
---|
| 322 | return djul;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | //
|
---|
| 326 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 327 | void jdgp(double tjul, double & second, int & nweek) {
|
---|
| 328 | double deltat;
|
---|
| 329 | deltat = tjul - 44244.0 ;
|
---|
| 330 | // current gps week
|
---|
| 331 | nweek = (int) floor(deltat/7.0);
|
---|
| 332 | // seconds past midnight of last weekend
|
---|
| 333 | second = (deltat - (nweek)*7.0)*86400.0;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | //
|
---|
| 337 | ////////////////////////////////////////////////////////////////////////////
|
---|
[3044] | 338 | void GPSweekFromDateAndTime(const QDateTime& dateTime,
|
---|
| 339 | int& GPSWeek, double& GPSWeeks) {
|
---|
| 340 |
|
---|
| 341 | static const QDateTime zeroEpoch(QDate(1980, 1, 6),QTime(),Qt::UTC);
|
---|
| 342 |
|
---|
| 343 | GPSWeek = zeroEpoch.daysTo(dateTime) / 7;
|
---|
| 344 |
|
---|
| 345 | int weekDay = dateTime.date().dayOfWeek() + 1; // Qt: Monday = 1
|
---|
| 346 | if (weekDay > 7) weekDay = 1;
|
---|
| 347 |
|
---|
| 348 | GPSWeeks = (weekDay - 1) * 86400.0
|
---|
| 349 | - dateTime.time().msecsTo(QTime()) / 1e3;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | //
|
---|
| 353 | ////////////////////////////////////////////////////////////////////////////
|
---|
[3171] | 354 | void GPSweekFromYMDhms(int year, int month, int day, int hour, int min,
|
---|
| 355 | double sec, int& GPSWeek, double& GPSWeeks) {
|
---|
| 356 |
|
---|
| 357 | double mjd = djul(year, month, day);
|
---|
| 358 |
|
---|
| 359 | jdgp(mjd, GPSWeeks, GPSWeek);
|
---|
| 360 | GPSWeeks += hour * 3600.0 + min * 60.0 + sec;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | //
|
---|
| 364 | ////////////////////////////////////////////////////////////////////////////
|
---|
[3044] | 365 | void mjdFromDateAndTime(const QDateTime& dateTime, int& mjd, double& dayfrac) {
|
---|
| 366 |
|
---|
| 367 | static const QDate zeroDate(1858, 11, 17);
|
---|
| 368 |
|
---|
| 369 | mjd = zeroDate.daysTo(dateTime.date());
|
---|
| 370 |
|
---|
| 371 | dayfrac = (dateTime.time().hour() +
|
---|
| 372 | (dateTime.time().minute() +
|
---|
| 373 | (dateTime.time().second() +
|
---|
| 374 | dateTime.time().msec() / 1000.0) / 60.0) / 60.0) / 24.0;
|
---|
| 375 | }
|
---|
[3408] | 376 |
|
---|
| 377 | //
|
---|
| 378 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 379 | bool findInVector(const vector<QString>& vv, const QString& str) {
|
---|
| 380 | std::vector<QString>::const_iterator it;
|
---|
| 381 | for (it = vv.begin(); it != vv.end(); ++it) {
|
---|
| 382 | if ( (*it) == str) {
|
---|
| 383 | return true;
|
---|
| 384 | }
|
---|
| 385 | }
|
---|
| 386 | return false;
|
---|
| 387 | }
|
---|
| 388 |
|
---|