[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() {
|
---|
| 118 | int GPSWeek;
|
---|
| 119 | double GPSWeeks;
|
---|
| 120 | currentGPSWeeks(GPSWeek, GPSWeeks);
|
---|
| 121 | return dateAndTimeFromGPSweek(GPSWeek, GPSWeeks);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[1381] | 124 | //
|
---|
| 125 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1595] | 126 | QByteArray ggaString(const QByteArray& latitude,
|
---|
| 127 | const QByteArray& longitude,
|
---|
| 128 | const QByteArray& height) {
|
---|
[1381] | 129 |
|
---|
| 130 | double lat = strtod(latitude,NULL);
|
---|
| 131 | double lon = strtod(longitude,NULL);
|
---|
[1595] | 132 | double hei = strtod(height,NULL);
|
---|
[1381] | 133 |
|
---|
| 134 | const char* flagN="N";
|
---|
| 135 | const char* flagE="E";
|
---|
| 136 | if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
|
---|
| 137 | if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
|
---|
| 138 | if (lon < -180.) {lon=(lon+360.); flagE="E";}
|
---|
| 139 | if (lat < 0.) {lat=lat*(-1.); flagN="S";}
|
---|
| 140 | QTime ttime(QDateTime::currentDateTime().toUTC().time());
|
---|
| 141 | int lat_deg = (int)lat;
|
---|
| 142 | double lat_min=(lat-lat_deg)*60.;
|
---|
| 143 | int lon_deg = (int)lon;
|
---|
| 144 | double lon_min=(lon-lon_deg)*60.;
|
---|
| 145 | int hh = 0 , mm = 0;
|
---|
| 146 | double ss = 0.0;
|
---|
| 147 | hh=ttime.hour();
|
---|
| 148 | mm=ttime.minute();
|
---|
| 149 | ss=(double)ttime.second()+0.001*ttime.msec();
|
---|
| 150 | QString gga;
|
---|
[1506] | 151 | gga += "GPGGA,";
|
---|
[1381] | 152 | 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'));
|
---|
| 153 | gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
| 154 | gga += flagN;
|
---|
| 155 | gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
[1595] | 156 | gga += flagE + QString(",1,05,1.00");
|
---|
[1599] | 157 | gga += QString(",%1,").arg(hei, 2, 'f', 1);
|
---|
[1595] | 158 | gga += QString("M,10.000,M,,");
|
---|
[1381] | 159 | int xori;
|
---|
| 160 | char XOR = 0;
|
---|
| 161 | char *Buff =gga.toAscii().data();
|
---|
| 162 | int iLen = strlen(Buff);
|
---|
| 163 | for (xori = 0; xori < iLen; xori++) {
|
---|
| 164 | XOR ^= (char)Buff[xori];
|
---|
| 165 | }
|
---|
[1506] | 166 | gga = "$" + gga + QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
|
---|
[1381] | 167 |
|
---|
[1387] | 168 | return gga.toAscii();
|
---|
[1381] | 169 | }
|
---|
[2043] | 170 |
|
---|
| 171 | //
|
---|
| 172 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 173 | void RSW_to_XYZ(const ColumnVector& rr, const ColumnVector& vv,
|
---|
| 174 | const ColumnVector& rsw, ColumnVector& xyz) {
|
---|
| 175 |
|
---|
| 176 | ColumnVector along = vv / vv.norm_Frobenius();
|
---|
| 177 | ColumnVector cross = crossproduct(rr, vv); cross /= cross.norm_Frobenius();
|
---|
| 178 | ColumnVector radial = crossproduct(along, cross);
|
---|
| 179 |
|
---|
| 180 | Matrix RR(3,3);
|
---|
| 181 | RR.Column(1) = radial;
|
---|
| 182 | RR.Column(2) = along;
|
---|
| 183 | RR.Column(3) = cross;
|
---|
| 184 |
|
---|
| 185 | xyz = RR * rsw;
|
---|
| 186 | }
|
---|
[2063] | 187 |
|
---|
| 188 | // Rectangular Coordinates -> Ellipsoidal Coordinates
|
---|
| 189 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 190 | t_irc xyz2ell(const double* XYZ, double* Ell) {
|
---|
| 191 |
|
---|
| 192 | const double bell = t_CST::aell*(1.0-1.0/t_CST::fInv) ;
|
---|
| 193 | const double e2 = (t_CST::aell*t_CST::aell-bell*bell)/(t_CST::aell*t_CST::aell) ;
|
---|
| 194 | const double e2c = (t_CST::aell*t_CST::aell-bell*bell)/(bell*bell) ;
|
---|
| 195 |
|
---|
| 196 | double nn, ss, zps, hOld, phiOld, theta, sin3, cos3;
|
---|
| 197 |
|
---|
| 198 | ss = sqrt(XYZ[0]*XYZ[0]+XYZ[1]*XYZ[1]) ;
|
---|
| 199 | zps = XYZ[2]/ss ;
|
---|
| 200 | theta = atan( (XYZ[2]*t_CST::aell) / (ss*bell) );
|
---|
| 201 | sin3 = sin(theta) * sin(theta) * sin(theta);
|
---|
| 202 | cos3 = cos(theta) * cos(theta) * cos(theta);
|
---|
| 203 |
|
---|
| 204 | // Closed formula
|
---|
| 205 | Ell[0] = atan( (XYZ[2] + e2c * bell * sin3) / (ss - e2 * t_CST::aell * cos3) );
|
---|
| 206 | Ell[1] = atan2(XYZ[1],XYZ[0]) ;
|
---|
| 207 | nn = t_CST::aell/sqrt(1.0-e2*sin(Ell[0])*sin(Ell[0])) ;
|
---|
| 208 | Ell[2] = ss / cos(Ell[0]) - nn;
|
---|
| 209 |
|
---|
| 210 | const int MAXITER = 100;
|
---|
| 211 | for (int ii = 1; ii <= MAXITER; ii++) {
|
---|
| 212 | nn = t_CST::aell/sqrt(1.0-e2*sin(Ell[0])*sin(Ell[0])) ;
|
---|
| 213 | hOld = Ell[2] ;
|
---|
| 214 | phiOld = Ell[0] ;
|
---|
| 215 | Ell[2] = ss/cos(Ell[0])-nn ;
|
---|
| 216 | Ell[0] = atan(zps/(1.0-e2*nn/(nn+Ell[2]))) ;
|
---|
| 217 | if ( fabs(phiOld-Ell[0]) <= 1.0e-11 && fabs(hOld-Ell[2]) <= 1.0e-5 ) {
|
---|
| 218 | return success;
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | return failure;
|
---|
| 223 | }
|
---|
[2065] | 224 |
|
---|
| 225 | // Rectangular Coordinates -> North, East, Up Components
|
---|
| 226 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 227 | void xyz2neu(const double* Ell, const double* xyz, double* neu) {
|
---|
| 228 |
|
---|
| 229 | double sinPhi = sin(Ell[0]);
|
---|
| 230 | double cosPhi = cos(Ell[0]);
|
---|
| 231 | double sinLam = sin(Ell[1]);
|
---|
| 232 | double cosLam = cos(Ell[1]);
|
---|
| 233 |
|
---|
| 234 | neu[0] = - sinPhi*cosLam * xyz[0]
|
---|
| 235 | - sinPhi*sinLam * xyz[1]
|
---|
| 236 | + cosPhi * xyz[2];
|
---|
| 237 |
|
---|
| 238 | neu[1] = - sinLam * xyz[0]
|
---|
| 239 | + cosLam * xyz[1];
|
---|
| 240 |
|
---|
| 241 | neu[2] = + cosPhi*cosLam * xyz[0]
|
---|
| 242 | + cosPhi*sinLam * xyz[1]
|
---|
| 243 | + sinPhi * xyz[2];
|
---|
| 244 | }
|
---|
[2221] | 245 |
|
---|
| 246 | // Fourth order Runge-Kutta numerical integrator for ODEs
|
---|
| 247 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 248 | ColumnVector rungeKutta4(
|
---|
| 249 | double xi, // the initial x-value
|
---|
| 250 | const ColumnVector& yi, // vector of the initial y-values
|
---|
| 251 | double dx, // the step size for the integration
|
---|
| 252 | ColumnVector (*der)(double x, const ColumnVector& y)
|
---|
| 253 | // A pointer to a function that computes the
|
---|
| 254 | // derivative of a function at a point (x,y)
|
---|
| 255 | ) {
|
---|
| 256 |
|
---|
| 257 | ColumnVector k1 = der(xi , yi ) * dx;
|
---|
| 258 | ColumnVector k2 = der(xi+dx/2.0, yi+k1/2.0) * dx;
|
---|
| 259 | ColumnVector k3 = der(xi+dx/2.0, yi+k2/2.0) * dx;
|
---|
| 260 | ColumnVector k4 = der(xi+dx , yi+k3 ) * dx;
|
---|
| 261 |
|
---|
| 262 | ColumnVector yf = yi + k1/6.0 + k2/3.0 + k3/3.0 + k4/6.0;
|
---|
| 263 |
|
---|
| 264 | return yf;
|
---|
| 265 | }
|
---|
| 266 |
|
---|