source: ntrip/trunk/BNC/src/bncutils.cpp@ 11024

Last change on this file since 11024 was 11022, checked in by stuerze, 6 days ago

delete 'www' from 'bkg.bund.de'

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