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

Last change on this file since 11158 was 11158, checked in by stuerze, 3 weeks ago

deprecated functions exchanged libs updated

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