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

Last change on this file since 10583 was 10577, checked in by stuerze, 5 weeks ago

updates regarding RINEX version 4.02 navigation information

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