source: ntrip/branches/BNC_2.12/src/bncutils.cpp@ 10417

Last change on this file since 10417 was 9428, checked in by stuerze, 3 years ago

bug fixed

File size: 32.4 KB
RevLine 
[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 *
[7527]37 * Changes:
[83]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
[5807]49#include <newmatap.h>
50
[83]51#include "bncutils.h"
[5070]52#include "bnccore.h"
[83]53
[124]54using namespace std;
55
[6812]56struct leapseconds { /* specify the day of leap second */
57 int day; /* this is the day, where 23:59:59 exists 2 times */
58 int month; /* not the next day! */
59 int year;
60 int taicount;
61};
62static const int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
63static const struct leapseconds leap[] = {
64/*{31, 12, 1971, 10},*/
65/*{30, 06, 1972, 11},*/
66/*{31, 12, 1972, 12},*/
67/*{31, 12, 1973, 13},*/
68/*{31, 12, 1974, 14},*/
69/*{31, 12, 1975, 15},*/
70/*{31, 12, 1976, 16},*/
71/*{31, 12, 1977, 17},*/
72/*{31, 12, 1978, 18},*/
73/*{31, 12, 1979, 19},*/
74{30, 06, 1981,20},
75{30, 06, 1982,21},
76{30, 06, 1983,22},
77{30, 06, 1985,23},
78{31, 12, 1987,24},
79{31, 12, 1989,25},
80{31, 12, 1990,26},
81{30, 06, 1992,27},
82{30, 06, 1993,28},
83{30, 06, 1994,29},
84{31, 12, 1995,30},
85{30, 06, 1997,31},
86{31, 12, 1998,32},
87{31, 12, 2005,33},
88{31, 12, 2008,34},
89{30, 06, 2012,35},
90{30, 06, 2015,36},
[8064]91{01, 01, 2017,37},
[6812]92{0,0,0,0} /* end marker */
93};
94
95#define GPSLEAPSTART 19 /* 19 leap seconds existed at 6.1.1980 */
96
97static int longyear(int year, int month)
98{
99 if(!(year % 4) && (!(year % 400) || (year % 100)))
100 {
101 if(!month || month == 2)
102 return 1;
103 }
104 return 0;
105}
106
107int gnumleap(int year, int month, int day)
108{
109 int ls = 0;
110 const struct leapseconds *l;
111
112 for(l = leap; l->taicount && year >= l->year; ++l)
113 {
114 if(year > l->year || month > l->month || (month == l->month && day > l->day))
115 ls = l->taicount - GPSLEAPSTART;
116 }
117 return ls;
118}
119
120/* Convert Moscow time into UTC (fixnumleap == 1) or GPS (fixnumleap == 0) */
121void updatetime(int *week, int *secOfWeek, int mSecOfWeek, bool fixnumleap)
122{
123 int y,m,d,k,l, nul;
124 unsigned int j = *week*(7*24*60*60) + *secOfWeek + 5*24*60*60+3*60*60;
125 int glo_daynumber = 0, glo_timeofday;
126 for(y = 1980; j >= (unsigned int)(k = (l = (365+longyear(y,0)))*24*60*60)
127 + gnumleap(y+1,1,1); ++y)
128 {
129 j -= k; glo_daynumber += l;
130 }
131 for(m = 1; j >= (unsigned int)(k = (l = months[m]+longyear(y, m))*24*60*60)
132 + gnumleap(y, m+1, 1); ++m)
133 {
134 j -= k; glo_daynumber += l;
135 }
136 for(d = 1; j >= 24UL*60UL*60UL + gnumleap(y, m, d+1); ++d)
137 j -= 24*60*60;
138 glo_daynumber -= 16*365+4-d;
139 nul = gnumleap(y, m, d);
140 glo_timeofday = j-nul;
141
142 // original version
143 // if(mSecOfWeek < 5*60*1000 && glo_timeofday > 23*60*60)
144 // *secOfWeek += 24*60*60;
145 // else if(glo_timeofday < 5*60 && mSecOfWeek > 23*60*60*1000)
146 // *secOfWeek -= 24*60*60;
147
[7527]148 // new version
[6812]149 if(mSecOfWeek < 4*60*60*1000 && glo_timeofday > 20*60*60)
150 *secOfWeek += 24*60*60;
151 else if(glo_timeofday < 4*60*60 && mSecOfWeek > 20*60*60*1000)
152 *secOfWeek -= 24*60*60;
153
154 *secOfWeek += mSecOfWeek/1000-glo_timeofday;
155 if(fixnumleap)
156 *secOfWeek -= nul;
157 if(*secOfWeek < 0) {*secOfWeek += 24*60*60*7; --*week; }
158 if(*secOfWeek >= 24*60*60*7) {*secOfWeek -= 24*60*60*7; ++*week; }
159}
160
[7527]161//
[1381]162////////////////////////////////////////////////////////////////////////////
[83]163void expandEnvVar(QString& str) {
164
165 QRegExp rx("(\\$\\{.+\\})");
166
167 if (rx.indexIn(str) != -1) {
168 QStringListIterator it(rx.capturedTexts());
169 if (it.hasNext()) {
170 QString rxStr = it.next();
171 QString envVar = rxStr.mid(2,rxStr.length()-3);
172 str.replace(rxStr, qgetenv(envVar.toAscii()));
173 }
174 }
175}
[124]176
[5910]177// Strip White Space
178////////////////////////////////////////////////////////////////////////////
179void stripWhiteSpace(string& str) {
180 if (!str.empty()) {
181 string::size_type beg = str.find_first_not_of(" \t\f\n\r\v");
182 string::size_type end = str.find_last_not_of(" \t\f\n\r\v");
183 if (beg > str.max_size())
184 str.erase();
185 else
186 str = str.substr(beg, end-beg+1);
187 }
188}
189
[7527]190//
[1381]191////////////////////////////////////////////////////////////////////////////
[124]192QDateTime dateAndTimeFromGPSweek(int GPSWeek, double GPSWeeks) {
193
194 static const QDate zeroEpoch(1980, 1, 6);
[7527]195
[124]196 QDate date(zeroEpoch);
197 QTime time(0,0,0,0);
198
199 int weekDays = int(GPSWeeks) / 86400;
200 date = date.addDays( GPSWeek * 7 + weekDays );
201 time = time.addMSecs( int( (GPSWeeks - 86400 * weekDays) * 1e3 ) );
202
203 return QDateTime(date,time);
204}
[210]205
[7527]206//
[1381]207////////////////////////////////////////////////////////////////////////////
[218]208void currentGPSWeeks(int& week, double& sec) {
[210]209
[1942]210 QDateTime currDateTimeGPS;
[1155]211
[5846]212 if ( BNC_CORE->dateAndTimeGPSSet() ) {
213 currDateTimeGPS = BNC_CORE->dateAndTimeGPS();
[1155]214 }
215 else {
[1942]216 currDateTimeGPS = QDateTime::currentDateTime().toUTC();
217 QDate hlp = currDateTimeGPS.date();
[7527]218 currDateTimeGPS = currDateTimeGPS.addSecs(gnumleap(hlp.year(),
[1942]219 hlp.month(), hlp.day()));
[1155]220 }
221
[1942]222 QDate currDateGPS = currDateTimeGPS.date();
223 QTime currTimeGPS = currDateTimeGPS.time();
[210]224
[1942]225 week = int( (double(currDateGPS.toJulianDay()) - 2444244.5) / 7 );
[1036]226
[7527]227 sec = (currDateGPS.dayOfWeek() % 7) * 24.0 * 3600.0 +
228 currTimeGPS.hour() * 3600.0 +
229 currTimeGPS.minute() * 60.0 +
[1942]230 currTimeGPS.second() +
231 currTimeGPS.msec() / 1000.0;
[1036]232}
[1154]233
[7527]234//
[1381]235////////////////////////////////////////////////////////////////////////////
[1154]236QDateTime currentDateAndTimeGPS() {
[5846]237 if ( BNC_CORE->dateAndTimeGPSSet() ) {
238 return BNC_CORE->dateAndTimeGPS();
[2530]239 }
240 else {
241 int GPSWeek;
242 double GPSWeeks;
243 currentGPSWeeks(GPSWeek, GPSWeeks);
244 return dateAndTimeFromGPSweek(GPSWeek, GPSWeeks);
245 }
[1154]246}
247
[7527]248//
[1381]249////////////////////////////////////////////////////////////////////////////
[8012]250bool checkForWrongObsEpoch(bncTime obsEpoch) {
251 const double maxDt = 600.0;
[8418]252 bncTime obsTime = obsEpoch;
[8012]253 int week;
254 double sec;
255 currentGPSWeeks(week, sec);
[8418]256 bncTime currTime(week, sec);
[9249]257
[8669]258 if (((currTime - obsTime) < 0.0) ||
259 (fabs(currTime - obsTime) > maxDt)) {
[8012]260 return true;
261 }
262 return false;
263}
[9249]264
[8012]265//
266////////////////////////////////////////////////////////////////////////////
[9249]267bool outDatedBcep(const t_eph *eph) {
268 bncTime toc = eph->TOC();
269 QDateTime now = currentDateAndTimeGPS();
270 bncTime currentTime(now.toString(Qt::ISODate).toStdString());
271 double dt = currentTime - toc;
272
[9377]273 // update interval: 2h, data sets are valid for 4 hours
[9406]274 if (eph->type() == t_eph::GPS && (dt > 14400.0 || dt < -7200.0)) {
[9249]275 return true;
276 }
[9377]277 // update interval: 3h, data sets are valid for 4 hours
[9406]278 else if (eph->type() == t_eph::Galileo && (dt > 14400.0 || dt < 0.0)) {
[9249]279 return true;
280 }
[9363]281 // updated every 30 minutes + 5 min
[9406]282 else if (eph->type() == t_eph::GLONASS && (dt > 3900.0 || dt < -2100.0)) {
[9249]283 return true;
[9363]284 }
[9377]285 // orbit parameters are valid for 7200 seconds (minimum)
[9406]286 else if (eph->type() == t_eph::QZSS && (dt > 7200.0 || dt < -3600.0)) {
[9249]287 return true;
288 }
[9377]289 // maximum update interval: 300 sec
[9406]290 else if (eph->type() == t_eph::SBAS && (dt > 600.0 || dt < -600.0)) {
[9249]291 return true;
292 }
[9363]293 // updates 1h + 5 min
[9428]294 else if (eph->type() == t_eph::BDS && (dt > 3900.0 || dt < 0.0)) {
[9249]295 return true;
296 }
[9377]297 // update interval: up to 24 hours
[9406]298 else if (eph->type() == t_eph::IRNSS && (fabs(dt > 86400.0))) {
[9249]299 return true;
300 }
[9406]301
[9249]302 return false;
303}
304
305//
306////////////////////////////////////////////////////////////////////////////
[1595]307QByteArray ggaString(const QByteArray& latitude,
308 const QByteArray& longitude,
[6786]309 const QByteArray& height,
310 const QString& ggaType) {
[1381]311
312 double lat = strtod(latitude,NULL);
313 double lon = strtod(longitude,NULL);
[1595]314 double hei = strtod(height,NULL);
[6786]315 QString sentences = "GPGGA,";
316 if (ggaType.contains("GNGGA")) {
317 sentences = "GNGGA,";
318 }
[1381]319
320 const char* flagN="N";
321 const char* flagE="E";
322 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
323 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
324 if (lon < -180.) {lon=(lon+360.); flagE="E";}
325 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
326 QTime ttime(QDateTime::currentDateTime().toUTC().time());
[7527]327 int lat_deg = (int)lat;
[1381]328 double lat_min=(lat-lat_deg)*60.;
[7527]329 int lon_deg = (int)lon;
[1381]330 double lon_min=(lon-lon_deg)*60.;
331 int hh = 0 , mm = 0;
332 double ss = 0.0;
333 hh=ttime.hour();
334 mm=ttime.minute();
335 ss=(double)ttime.second()+0.001*ttime.msec();
336 QString gga;
[6786]337 gga += sentences;
[1381]338 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'));
339 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
340 gga += flagN;
341 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
[1595]342 gga += flagE + QString(",1,05,1.00");
[1599]343 gga += QString(",%1,").arg(hei, 2, 'f', 1);
[1595]344 gga += QString("M,10.000,M,,");
[7527]345
[8645]346 unsigned char XOR = 0;
347 for (int ii = 0; ii < gga.length(); ii++) {
348 XOR ^= (unsigned char) gga[ii].toAscii();
[1381]349 }
[8691]350 gga = "$" + gga + QString("*%1").arg(XOR, 2, 16, QLatin1Char('0')) + "\n";
[1381]351
[1387]352 return gga.toAscii();
[1381]353}
[2043]354
[7527]355//
[2043]356////////////////////////////////////////////////////////////////////////////
357void RSW_to_XYZ(const ColumnVector& rr, const ColumnVector& vv,
358 const ColumnVector& rsw, ColumnVector& xyz) {
359
360 ColumnVector along = vv / vv.norm_Frobenius();
361 ColumnVector cross = crossproduct(rr, vv); cross /= cross.norm_Frobenius();
362 ColumnVector radial = crossproduct(along, cross);
363
364 Matrix RR(3,3);
365 RR.Column(1) = radial;
366 RR.Column(2) = along;
367 RR.Column(3) = cross;
368
369 xyz = RR * rsw;
370}
[2063]371
[2988]372// Transformation xyz --> radial, along track, out-of-plane
373////////////////////////////////////////////////////////////////////////////
374void XYZ_to_RSW(const ColumnVector& rr, const ColumnVector& vv,
375 const ColumnVector& xyz, ColumnVector& rsw) {
376
377 ColumnVector along = vv / vv.norm_Frobenius();
378 ColumnVector cross = crossproduct(rr, vv); cross /= cross.norm_Frobenius();
379 ColumnVector radial = crossproduct(along, cross);
380
381 rsw.ReSize(3);
382 rsw(1) = DotProduct(xyz, radial);
383 rsw(2) = DotProduct(xyz, along);
384 rsw(3) = DotProduct(xyz, cross);
385}
386
[2063]387// Rectangular Coordinates -> Ellipsoidal Coordinates
388////////////////////////////////////////////////////////////////////////////
389t_irc xyz2ell(const double* XYZ, double* Ell) {
390
391 const double bell = t_CST::aell*(1.0-1.0/t_CST::fInv) ;
392 const double e2 = (t_CST::aell*t_CST::aell-bell*bell)/(t_CST::aell*t_CST::aell) ;
393 const double e2c = (t_CST::aell*t_CST::aell-bell*bell)/(bell*bell) ;
[7527]394
[2063]395 double nn, ss, zps, hOld, phiOld, theta, sin3, cos3;
396
397 ss = sqrt(XYZ[0]*XYZ[0]+XYZ[1]*XYZ[1]) ;
398 zps = XYZ[2]/ss ;
399 theta = atan( (XYZ[2]*t_CST::aell) / (ss*bell) );
400 sin3 = sin(theta) * sin(theta) * sin(theta);
401 cos3 = cos(theta) * cos(theta) * cos(theta);
402
403 // Closed formula
[7527]404 Ell[0] = atan( (XYZ[2] + e2c * bell * sin3) / (ss - e2 * t_CST::aell * cos3) );
[2063]405 Ell[1] = atan2(XYZ[1],XYZ[0]) ;
406 nn = t_CST::aell/sqrt(1.0-e2*sin(Ell[0])*sin(Ell[0])) ;
407 Ell[2] = ss / cos(Ell[0]) - nn;
408
409 const int MAXITER = 100;
410 for (int ii = 1; ii <= MAXITER; ii++) {
411 nn = t_CST::aell/sqrt(1.0-e2*sin(Ell[0])*sin(Ell[0])) ;
412 hOld = Ell[2] ;
413 phiOld = Ell[0] ;
414 Ell[2] = ss/cos(Ell[0])-nn ;
415 Ell[0] = atan(zps/(1.0-e2*nn/(nn+Ell[2]))) ;
416 if ( fabs(phiOld-Ell[0]) <= 1.0e-11 && fabs(hOld-Ell[2]) <= 1.0e-5 ) {
417 return success;
418 }
419 }
420
421 return failure;
422}
[2065]423
424// Rectangular Coordinates -> North, East, Up Components
425////////////////////////////////////////////////////////////////////////////
426void xyz2neu(const double* Ell, const double* xyz, double* neu) {
427
428 double sinPhi = sin(Ell[0]);
429 double cosPhi = cos(Ell[0]);
430 double sinLam = sin(Ell[1]);
431 double cosLam = cos(Ell[1]);
432
433 neu[0] = - sinPhi*cosLam * xyz[0]
434 - sinPhi*sinLam * xyz[1]
435 + cosPhi * xyz[2];
436
437 neu[1] = - sinLam * xyz[0]
438 + cosLam * xyz[1];
439
440 neu[2] = + cosPhi*cosLam * xyz[0]
441 + cosPhi*sinLam * xyz[1]
442 + sinPhi * xyz[2];
443}
[2221]444
[2582]445// North, East, Up Components -> Rectangular Coordinates
446////////////////////////////////////////////////////////////////////////////
447void neu2xyz(const double* Ell, const double* neu, double* xyz) {
448
449 double sinPhi = sin(Ell[0]);
450 double cosPhi = cos(Ell[0]);
451 double sinLam = sin(Ell[1]);
452 double cosLam = cos(Ell[1]);
453
454 xyz[0] = - sinPhi*cosLam * neu[0]
455 - sinLam * neu[1]
456 + cosPhi*cosLam * neu[2];
457
458 xyz[1] = - sinPhi*sinLam * neu[0]
459 + cosLam * neu[1]
460 + cosPhi*sinLam * neu[2];
461
462 xyz[2] = + cosPhi * neu[0]
463 + sinPhi * neu[2];
464}
465
[7244]466// Rectangular Coordinates -> Geocentric Coordinates
467////////////////////////////////////////////////////////////////////////////
[7251]468t_irc xyz2geoc(const double* XYZ, double* Geoc) {
[7244]469
470 const double bell = t_CST::aell*(1.0-1.0/t_CST::fInv) ;
[7260]471 const double e2 = (t_CST::aell*t_CST::aell-bell*bell)/(t_CST::aell*t_CST::aell) ;
[7244]472 double Ell[3];
[7251]473 if (xyz2ell(XYZ, Ell) != success) {
474 return failure;
475 }
[7244]476 double rho = sqrt(XYZ[0]*XYZ[0]+XYZ[1]*XYZ[1]+XYZ[2]*XYZ[2]);
[7260]477 double Rn = t_CST::aell/sqrt(1-e2*pow(sin(Ell[0]),2));
[7244]478
479 Geoc[0] = atan((1-e2 * Rn/(Rn + Ell[2])) * tan(Ell[0]));
480 Geoc[1] = Ell[1];
481 Geoc[2] = rho-t_CST::rgeoc;
[7251]482
483 return success;
[7244]484}
485
[7527]486//
[5807]487////////////////////////////////////////////////////////////////////////////
488double Frac (double x) {
[7527]489 return x-floor(x);
[5807]490}
491
[7183]492//
[5807]493////////////////////////////////////////////////////////////////////////////
[7527]494double Modulo (double x, double y) {
495 return y*Frac(x/y);
[5807]496}
497
[5753]498// Round to nearest integer
499////////////////////////////////////////////////////////////////////////////
500double nint(double val) {
501 return ((val < 0.0) ? -floor(fabs(val)+0.5) : floor(val+0.5));
502}
503
[7183]504//
505////////////////////////////////////////////////////////////////////////////
[8805]506double factorial(int n) {
[7183]507 if (n == 0) {
508 return 1;
509 }
510 else {
511 return (n * factorial(n - 1));
512 }
513}
514
515//
516////////////////////////////////////////////////////////////////////////////
517double associatedLegendreFunction(int n, int m, double t) {
518 double sum = 0.0;
519 int r = (int) floor((n - m) / 2);
520 for (int k = 0; k <= r; k++) {
[7228]521 sum += (pow(-1.0, (double)k) * factorial(2*n - 2*k)
522 / (factorial(k) * factorial(n-k) * factorial(n-m-2*k))
523 * pow(t, (double)n-m-2*k));
[7183]524 }
525 double fac = pow(2.0,(double) -n) * pow((1 - t*t), (double)m/2);
526 return sum *= fac;
527}
528
529
[7527]530// Jacobian XYZ --> NEU
[5752]531////////////////////////////////////////////////////////////////////////////
532void jacobiXYZ_NEU(const double* Ell, Matrix& jacobi) {
533
534 Tracer tracer("jacobiXYZ_NEU");
535
536 double sinPhi = sin(Ell[0]);
537 double cosPhi = cos(Ell[0]);
538 double sinLam = sin(Ell[1]);
539 double cosLam = cos(Ell[1]);
540
541 jacobi(1,1) = - sinPhi * cosLam;
542 jacobi(1,2) = - sinPhi * sinLam;
543 jacobi(1,3) = cosPhi;
[7527]544
545 jacobi(2,1) = - sinLam;
[5752]546 jacobi(2,2) = cosLam;
[7527]547 jacobi(2,3) = 0.0;
548
549 jacobi(3,1) = cosPhi * cosLam;
550 jacobi(3,2) = cosPhi * sinLam;
[5752]551 jacobi(3,3) = sinPhi;
552}
553
554// Jacobian Ell --> XYZ
555////////////////////////////////////////////////////////////////////////////
556void jacobiEll_XYZ(const double* Ell, Matrix& jacobi) {
557
558 Tracer tracer("jacobiEll_XYZ");
559
560 double sinPhi = sin(Ell[0]);
561 double cosPhi = cos(Ell[0]);
562 double sinLam = sin(Ell[1]);
563 double cosLam = cos(Ell[1]);
564 double hh = Ell[2];
565
566 double bell = t_CST::aell*(1.0-1.0/t_CST::fInv);
567 double e2 = (t_CST::aell*t_CST::aell-bell*bell)/(t_CST::aell*t_CST::aell) ;
568 double nn = t_CST::aell/sqrt(1.0-e2*sinPhi*sinPhi) ;
569
570 jacobi(1,1) = -(nn+hh) * sinPhi * cosLam;
571 jacobi(1,2) = -(nn+hh) * cosPhi * sinLam;
572 jacobi(1,3) = cosPhi * cosLam;
573
574 jacobi(2,1) = -(nn+hh) * sinPhi * sinLam;
575 jacobi(2,2) = (nn+hh) * cosPhi * cosLam;
576 jacobi(2,3) = cosPhi * sinLam;
577
578 jacobi(3,1) = (nn*(1.0-e2)+hh) * cosPhi;
579 jacobi(3,2) = 0.0;
580 jacobi(3,3) = sinPhi;
[7527]581}
[5752]582
583// Covariance Matrix in NEU
584////////////////////////////////////////////////////////////////////////////
[7527]585void covariXYZ_NEU(const SymmetricMatrix& QQxyz, const double* Ell,
[5752]586 SymmetricMatrix& Qneu) {
587
588 Tracer tracer("covariXYZ_NEU");
589
590 Matrix CC(3,3);
591 jacobiXYZ_NEU(Ell, CC);
592 Qneu << CC * QQxyz * CC.t();
593}
594
595// Covariance Matrix in XYZ
596////////////////////////////////////////////////////////////////////////////
[7527]597void covariNEU_XYZ(const SymmetricMatrix& QQneu, const double* Ell,
[5752]598 SymmetricMatrix& Qxyz) {
599
600 Tracer tracer("covariNEU_XYZ");
601
602 Matrix CC(3,3);
603 jacobiXYZ_NEU(Ell, CC);
604 Qxyz << CC.t() * QQneu * CC;
605}
606
[2221]607// Fourth order Runge-Kutta numerical integrator for ODEs
608////////////////////////////////////////////////////////////////////////////
609ColumnVector rungeKutta4(
610 double xi, // the initial x-value
611 const ColumnVector& yi, // vector of the initial y-values
612 double dx, // the step size for the integration
[8418]613 double* acc, // additional acceleration
[2556]614 ColumnVector (*der)(double x, const ColumnVector& y, double* acc)
[7527]615 // A pointer to a function that computes the
[2221]616 // derivative of a function at a point (x,y)
617 ) {
618
[2556]619 ColumnVector k1 = der(xi , yi , acc) * dx;
620 ColumnVector k2 = der(xi+dx/2.0, yi+k1/2.0, acc) * dx;
621 ColumnVector k3 = der(xi+dx/2.0, yi+k2/2.0, acc) * dx;
622 ColumnVector k4 = der(xi+dx , yi+k3 , acc) * dx;
[2221]623
624 ColumnVector yf = yi + k1/6.0 + k2/3.0 + k3/3.0 + k4/6.0;
[7527]625
[2221]626 return yf;
627}
[7527]628//
[3044]629////////////////////////////////////////////////////////////////////////////
[5886]630double djul(long jj, long mm, double tt) {
631 long ii, kk;
[3171]632 double djul ;
633 if( mm <= 2 ) {
634 jj = jj - 1;
635 mm = mm + 12;
[7527]636 }
[3171]637 ii = jj/100;
638 kk = 2 - ii + ii/4;
639 djul = (365.25*jj - fmod( 365.25*jj, 1.0 )) - 679006.0;
640 djul = djul + floor( 30.6001*(mm + 1) ) + tt + kk;
641 return djul;
[7527]642}
[3171]643
[7527]644//
[3171]645////////////////////////////////////////////////////////////////////////////
[5886]646double gpjd(double second, int nweek) {
647 double deltat;
648 deltat = nweek*7.0 + second/86400.0 ;
649 return( 44244.0 + deltat) ;
[7527]650}
[5886]651
[7527]652//
[5886]653////////////////////////////////////////////////////////////////////////////
654void jdgp(double tjul, double & second, long & nweek) {
[3171]655 double deltat;
656 deltat = tjul - 44244.0 ;
[5886]657 nweek = (long) floor(deltat/7.0);
[3171]658 second = (deltat - (nweek)*7.0)*86400.0;
659}
660
[7527]661//
[3171]662////////////////////////////////////////////////////////////////////////////
[5886]663void jmt(double djul, long& jj, long& mm, double& dd) {
664 long ih, ih1, ih2 ;
665 double t1, t2, t3, t4;
666 t1 = 1.0 + djul - fmod( djul, 1.0 ) + 2400000.0;
667 t4 = fmod( djul, 1.0 );
668 ih = long( (t1 - 1867216.25)/36524.25 );
669 t2 = t1 + 1 + ih - ih/4;
670 t3 = t2 - 1720995.0;
671 ih1 = long( (t3 - 122.1)/365.25 );
672 t1 = 365.25*ih1 - fmod( 365.25*ih1, 1.0 );
673 ih2 = long( (t3 - t1)/30.6001 );
674 dd = t3 - t1 - (int)( 30.6001*ih2 ) + t4;
675 mm = ih2 - 1;
676 if ( ih2 > 13 ) mm = ih2 - 13;
677 jj = ih1;
678 if ( mm <= 2 ) jj = jj + 1;
[7527]679}
[5886]680
[7527]681//
[5886]682////////////////////////////////////////////////////////////////////////////
[7527]683void GPSweekFromDateAndTime(const QDateTime& dateTime,
[3044]684 int& GPSWeek, double& GPSWeeks) {
685
686 static const QDateTime zeroEpoch(QDate(1980, 1, 6),QTime(),Qt::UTC);
[7527]687
[3044]688 GPSWeek = zeroEpoch.daysTo(dateTime) / 7;
689
690 int weekDay = dateTime.date().dayOfWeek() + 1; // Qt: Monday = 1
691 if (weekDay > 7) weekDay = 1;
692
693 GPSWeeks = (weekDay - 1) * 86400.0
[7527]694 - dateTime.time().msecsTo(QTime()) / 1e3;
[3044]695}
696
[7527]697//
[3044]698////////////////////////////////////////////////////////////////////////////
[3171]699void GPSweekFromYMDhms(int year, int month, int day, int hour, int min,
700 double sec, int& GPSWeek, double& GPSWeeks) {
701
702 double mjd = djul(year, month, day);
703
[5888]704 long GPSWeek_long;
705 jdgp(mjd, GPSWeeks, GPSWeek_long);
706 GPSWeek = GPSWeek_long;
[7527]707 GPSWeeks += hour * 3600.0 + min * 60.0 + sec;
[3171]708}
709
[7527]710//
[3171]711////////////////////////////////////////////////////////////////////////////
[3044]712void mjdFromDateAndTime(const QDateTime& dateTime, int& mjd, double& dayfrac) {
713
714 static const QDate zeroDate(1858, 11, 17);
715
716 mjd = zeroDate.daysTo(dateTime.date());
717
718 dayfrac = (dateTime.time().hour() +
719 (dateTime.time().minute() +
[7527]720 (dateTime.time().second() +
[3044]721 dateTime.time().msec() / 1000.0) / 60.0) / 60.0) / 24.0;
722}
[3408]723
[7527]724//
[3408]725////////////////////////////////////////////////////////////////////////////
726bool findInVector(const vector<QString>& vv, const QString& str) {
727 std::vector<QString>::const_iterator it;
728 for (it = vv.begin(); it != vv.end(); ++it) {
729 if ( (*it) == str) {
730 return true;
731 }
732 }
733 return false;
734}
735
[7527]736//
[3664]737////////////////////////////////////////////////////////////////////////////
738int readInt(const QString& str, int pos, int len, int& value) {
739 bool ok;
740 value = str.mid(pos, len).toInt(&ok);
741 return ok ? 0 : 1;
742}
743
[7527]744//
[3664]745////////////////////////////////////////////////////////////////////////////
746int readDbl(const QString& str, int pos, int len, double& value) {
747 QString hlp = str.mid(pos, len);
748 for (int ii = 0; ii < hlp.length(); ii++) {
749 if (hlp[ii]=='D' || hlp[ii]=='d' || hlp[ii] == 'E') {
750 hlp[ii]='e';
751 }
752 }
753 bool ok;
754 value = hlp.toDouble(&ok);
755 return ok ? 0 : 1;
756}
[4338]757
758// Topocentrical Distance and Elevation
759////////////////////////////////////////////////////////////////////////////
[7527]760void topos(double xRec, double yRec, double zRec,
761 double xSat, double ySat, double zSat,
[4338]762 double& rho, double& eleSat, double& azSat) {
763
764 double dx[3];
765 dx[0] = xSat-xRec;
766 dx[1] = ySat-yRec;
767 dx[2] = zSat-zRec;
768
[7527]769 rho = sqrt( dx[0]*dx[0] + dx[1]*dx[1] + dx[2]*dx[2] );
[4338]770
771 double xyzRec[3];
772 xyzRec[0] = xRec;
773 xyzRec[1] = yRec;
774 xyzRec[2] = zRec;
775
776 double Ell[3];
777 double neu[3];
778 xyz2ell(xyzRec, Ell);
779 xyz2neu(Ell, dx, neu);
780
781 eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
782 if (neu[2] < 0) {
783 eleSat *= -1.0;
784 }
785
786 azSat = atan2(neu[1], neu[0]);
787}
[5230]788
789// Degrees -> degrees, minutes, seconds
790////////////////////////////////////////////////////////////////////////////
791void deg2DMS(double decDeg, int& deg, int& min, double& sec) {
792 int sgn = (decDeg < 0.0 ? -1 : 1);
[7787]793 deg = static_cast<int>(decDeg);
794 min = sgn * static_cast<int>((decDeg - deg)*60);
795 sec = (sgn* (decDeg - deg) - min/60.0) * 3600.0;
[5230]796}
[5310]797
[7527]798//
[5310]799////////////////////////////////////////////////////////////////////////////
800QString fortranFormat(double value, int width, int prec) {
[6537]801 int expo = value == 0.0 ? 0 : int(log10(fabs(value)));
[7656]802 double mant = value == 0.0 ? 0 : value / pow(10.0, double(expo));
[5310]803 if (fabs(mant) >= 1.0) {
804 mant /= 10.0;
805 expo += 1;
806 }
807 if (expo >= 0) {
808 return QString("%1e+%2").arg(mant, width-4, 'f', prec).arg(expo, 2, 10, QChar('0'));
809 }
810 else {
811 return QString("%1e-%2").arg(mant, width-4, 'f', prec).arg(-expo, 2, 10, QChar('0'));
812 }
813}
[5807]814
815//
816//////////////////////////////////////////////////////////////////////////////
[7527]817void kalman(const Matrix& AA, const ColumnVector& ll, const DiagonalMatrix& PP,
[6168]818 SymmetricMatrix& QQ, ColumnVector& xx) {
[5807]819
820 Tracer tracer("kalman");
821
822 int nPar = AA.Ncols();
823 int nObs = AA.Nrows();
824 UpperTriangularMatrix SS = Cholesky(QQ).t();
825
826 Matrix SA = SS*AA.t();
827 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
828 for (int ii = 1; ii <= nObs; ++ii) {
829 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
830 }
831
832 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
833 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
[7527]834
[5807]835 UpperTriangularMatrix UU;
836 QRZ(SRF, UU);
[7527]837
[5807]838 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
839 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
840 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
[7527]841
[5807]842 UpperTriangularMatrix SHi = SH_rt.i();
[7527]843
844 Matrix KT = SHi * YY;
[5807]845 SymmetricMatrix Hi; Hi << SHi * SHi.t();
846
[6168]847 xx += KT.t() * (ll - AA * xx);
[5807]848 QQ << (SS.t() * SS);
849}
850
[8855]851//
852////////////////////////////////////////////////////////////////////////////
[6799]853double accuracyFromIndex(int index, t_eph::e_type type) {
[8805]854double accuracy = -1.0;
[6799]855
[8805]856 if (type == t_eph::GPS ||
857 type == t_eph::BDS ||
858 type == t_eph::SBAS||
859 type == t_eph::QZSS) {
[6799]860 if ((index >= 0) && (index <= 6)) {
861 if (index == 3) {
[8805]862 accuracy = ceil(10.0 * pow(2.0, (double(index) / 2.0) + 1.0)) / 10.0;
[6799]863 }
864 else {
[8805]865 accuracy = floor(10.0 * pow(2.0, (double(index) / 2.0) + 1.0)) / 10.0;
[6799]866 }
867 }
868 else if ((index > 6) && (index <= 15)) {
[8805]869 accuracy = (10.0 * pow(2.0, (double(index) - 2.0))) / 10.0;
[6799]870 }
871 else {
[8805]872 accuracy = 8192.0;
[6799]873 }
874 }
[8805]875 else if (type == t_eph::Galileo) {
[6799]876 if ((index >= 0) && (index <= 49)) {
[8805]877 accuracy = (double(index) / 100.0);
[6799]878 }
879 else if ((index > 49) && (index <= 74)) {
[8805]880 accuracy = (50.0 + (double(index) - 50.0) * 2.0) / 100.0;
[6799]881 }
882 else if ((index > 74) && (index <= 99)) {
[8805]883 accuracy = 1.0 + (double(index) - 75.0) * 0.04;
[6799]884 }
885 else if ((index > 99) && (index <= 125)) {
[8805]886 accuracy = 2.0 + (double(index) - 100.0) * 0.16;
[6799]887 }
888 else {
[8805]889 accuracy = -1.0;
[6799]890 }
891 }
[8805]892 else if (type == t_eph::IRNSS) {
893 if ((index >= 0) && (index <= 6)) {
894 if (index == 1) {
895 accuracy = 2.8;
896 }
897 else if (index == 3) {
898 accuracy = 5.7;
899 }
900 else if (index == 5) {
901 accuracy = 11.3;
902 }
903 else {
904 accuracy = pow(2, 1 + index / 2);
905 }
906 }
907 else if ((index > 6) && (index <= 15)) {
908 accuracy = pow(2, index - 2);
909 }
910 }
911 return accuracy;
[6799]912}
913
[8855]914//
915////////////////////////////////////////////////////////////////////////////
[6799]916int indexFromAccuracy(double accuracy, t_eph::e_type type) {
917
918 if (type == t_eph::GPS || type == t_eph::BDS || type == t_eph::SBAS
919 || type == t_eph::QZSS) {
920
921 if (accuracy <= 2.40) {
922 return 0;
923 }
924 else if (accuracy <= 3.40) {
925 return 1;
926 }
927 else if (accuracy <= 4.85) {
928 return 2;
929 }
930 else if (accuracy <= 6.85) {
931 return 3;
932 }
933 else if (accuracy <= 9.65) {
934 return 4;
935 }
936 else if (accuracy <= 13.65) {
937 return 5;
938 }
939 else if (accuracy <= 24.00) {
940 return 6;
941 }
942 else if (accuracy <= 48.00) {
943 return 7;
944 }
945 else if (accuracy <= 96.00) {
946 return 8;
947 }
948 else if (accuracy <= 192.00) {
949 return 9;
950 }
951 else if (accuracy <= 384.00) {
952 return 10;
953 }
954 else if (accuracy <= 768.00) {
955 return 11;
956 }
957 else if (accuracy <= 1536.00) {
958 return 12;
959 }
960 else if (accuracy <= 3072.00) {
961 return 13;
962 }
963 else if (accuracy <= 6144.00) {
964 return 14;
965 }
966 else {
967 return 15;
968 }
969 }
970
971 if (type == t_eph::Galileo) {
[6800]972
973 if (accuracy <= 0.49) {
974 return int(ceil(accuracy * 100.0));
975 }
976 else if (accuracy <= 0.98) {
977 return int(50.0 + (((accuracy * 100.0) - 50) / 2.0));
978 }
979 else if (accuracy <= 2.0) {
980 return int(75.0 + ((accuracy - 1.0) / 0.04));
981 }
982 else if (accuracy <= 6.0) {
983 return int(100.0 + ((accuracy - 2.0) / 0.16));
984 }
985 else {
986 return 255;
987 }
[6799]988 }
989
990 return (type == t_eph::Galileo) ? 255 : 15;
991}
[7053]992
[8855]993// Returns fit interval in hours from flag
994////////////////////////////////////////////////////////////////////////////
995double fitIntervalFromFlag(int flag, double iodc, t_eph::e_type type) {
996 double fitInterval = 0.0;
997
998 switch (flag) {
999 case 0:
1000 if (type == t_eph::GPS) {
1001 fitInterval = 4.0;
1002 }
1003 else if (type == t_eph::QZSS) {
1004 fitInterval = 2.0;
1005 }
1006 break;
1007 case 1:
1008 if (type == t_eph::GPS) {
1009 if (iodc >= 240 && iodc <= 247) {
1010 fitInterval = 8.0;
1011 }
1012 else if ((iodc >= 248 && iodc <= 255) ||
1013 (iodc == 496) ) {
1014 fitInterval = 14.0;
1015 }
1016 else if ((iodc >= 497 && iodc <= 503) ||
1017 (iodc >= 2021 && iodc <= 1023) ) {
1018 fitInterval = 26.0;
1019 }
1020 else {
1021 fitInterval = 6.0;
1022 }
1023 }
1024 break;
1025 }
1026 return fitInterval;
1027}
1028
[7053]1029// Returns CRC24
1030////////////////////////////////////////////////////////////////////////////
1031unsigned long CRC24(long size, const unsigned char *buf) {
1032 unsigned long crc = 0;
1033 int ii;
1034 while (size--) {
1035 crc ^= (*buf++) << (16);
1036 for(ii = 0; ii < 8; ii++) {
1037 crc <<= 1;
1038 if (crc & 0x1000000) {
1039 crc ^= 0x01864cfb;
1040 }
1041 }
1042 }
1043 return crc;
1044}
1045
[9371]1046// Extracts k bits from position p and returns the extracted value as integer
1047////////////////////////////////////////////////////////////////////////////
1048int bitExtracted(int number, int k, int p) {
1049 return (((1 << k) - 1) & (number >> (p - 1)));
1050}
1051
[9089]1052// Convert RTCM3 lock-time indicator to minimum lock time in seconds
[8616]1053////////////////////////////////////////////////////////////////////////////
1054double lti2sec(int type, int lti) {
1055
1056 if ( (type>=1001 && type<=1004) ||
1057 (type>=1009 && type<=1012) ) { // RTCM3 msg 100[1...4] and 10[09...12]
1058 if (lti< 0) return -1;
1059 else if (lti< 24) return 1*lti; // [ 0 1 23]
1060 else if (lti< 48) return 2*lti-24; // [ 24 2 70]
1061 else if (lti< 72) return 4*lti-120; // [ 72 4 164]
1062 else if (lti< 96) return 8*lti-408; // [168 8 352]
1063 else if (lti< 120) return 16*lti-1176; // [360 16 728]
1064 else if (lti< 127) return 32*lti-3096; // [744 32 905]
1065 else if (lti==127) return 937;
[9089]1066 else return -1.0;
[8616]1067 }
1068 else if (type%10==2 || type%10==3 ||
1069 type%10==4 || type%10==5) { // RTCM3 MSM-2/-3/-4/-5
1070 switch(lti) {
1071 case( 0) : return 0;
1072 case( 1) : return 32e-3;
1073 case( 2) : return 64e-3;
1074 case( 3) : return 128e-3;
1075 case( 4) : return 256e-3;
1076 case( 5) : return 512e-3;
1077 case( 6) : return 1024e-3;
1078 case( 7) : return 2048e-3;
1079 case( 8) : return 4096e-3;
1080 case( 9) : return 8192e-3;
1081 case(10) : return 16384e-3;
1082 case(11) : return 32768e-3;
1083 case(12) : return 65536e-3;
1084 case(13) : return 131072e-3;
1085 case(14) : return 262144e-3;
1086 case(15) : return 524288e-3;
[9089]1087 default : return -1.0;
[8616]1088 };
1089 }
1090 else if (type%10==6 || type%10==7) { // RTCM3 MSM-6 and MSM-7
1091 if (lti< 0) return ( -1 );
1092 else if (lti< 64) return ( 1*lti )*1e-3;
1093 else if (lti< 96) return ( 2*lti-64 )*1e-3;
1094 else if (lti< 128) return ( 4*lti-256 )*1e-3;
1095 else if (lti< 160) return ( 8*lti-768 )*1e-3;
1096 else if (lti< 192) return ( 16*lti-2048 )*1e-3;
1097 else if (lti< 224) return ( 32*lti-5120 )*1e-3;
1098 else if (lti< 256) return ( 64*lti-12288 )*1e-3;
1099 else if (lti< 288) return ( 128*lti-28672 )*1e-3;
1100 else if (lti< 320) return ( 256*lti-65536 )*1e-3;
1101 else if (lti< 352) return ( 512*lti-147456 )*1e-3;
1102 else if (lti< 384) return ( 1024*lti-327680 )*1e-3;
1103 else if (lti< 416) return ( 2048*lti-720896 )*1e-3;
1104 else if (lti< 448) return ( 4096*lti-1572864 )*1e-3;
1105 else if (lti< 480) return ( 8192*lti-3407872 )*1e-3;
1106 else if (lti< 512) return ( 16384*lti-7340032 )*1e-3;
1107 else if (lti< 544) return ( 32768*lti-15728640 )*1e-3;
1108 else if (lti< 576) return ( 65536*lti-33554432 )*1e-3;
1109 else if (lti< 608) return ( 131072*lti-71303168 )*1e-3;
1110 else if (lti< 640) return ( 262144*lti-150994944 )*1e-3;
1111 else if (lti< 672) return ( 524288*lti-318767104 )*1e-3;
1112 else if (lti< 704) return (1048576*lti-671088640 )*1e-3;
1113 else if (lti==704) return (2097152*lti-1409286144)*1e-3;
[9089]1114 else return ( -1.0 );
[8616]1115 }
1116 else {
[9089]1117 return -1.0;
[8616]1118 };
1119};
Note: See TracBrowser for help on using the repository browser.