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

Last change on this file since 10569 was 10568, checked in by stuerze, 2 months ago

wrong observation limit changed

File size: 32.2 KB
RevLine 
[280]1// Part of BNC, a utility for retrieving decoding and
[464]2// converting GNSS data streams from NTRIP broadcasters.
[280]3//
[464]4// Copyright (C) 2007
[280]5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
[464]7// Czech Technical University Prague, Department of Geodesy
[280]8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
[83]24
25/* -------------------------------------------------------------------------
[93]26 * BKG NTRIP Client
[83]27 * -------------------------------------------------------------------------
28 *
29 * Class: bncutils
30 *
31 * Purpose: Auxiliary Functions
32 *
33 * Author: L. Mervart
34 *
35 * Created: 30-Aug-2006
36 *
[7527]37 * Changes:
[83]38 *
39 * -----------------------------------------------------------------------*/
40
[124]41#include <iostream>
[218]42#include <ctime>
[221]43#include <math.h>
[124]44
[83]45#include <QRegExp>
46#include <QStringList>
[271]47#include <QDateTime>
[83]48
[5807]49#include <newmatap.h>
50
[83]51#include "bncutils.h"
[5070]52#include "bnccore.h"
[83]53
[124]54using namespace std;
55
[6812]56struct leapseconds { /* specify the day of leap second */
57 int day; /* this is the day, where 23:59:59 exists 2 times */
58 int month; /* not the next day! */
59 int year;
60 int taicount;
61};
62static const int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
63static const struct leapseconds leap[] = {
64/*{31, 12, 1971, 10},*/
65/*{30, 06, 1972, 11},*/
66/*{31, 12, 1972, 12},*/
67/*{31, 12, 1973, 13},*/
68/*{31, 12, 1974, 14},*/
69/*{31, 12, 1975, 15},*/
70/*{31, 12, 1976, 16},*/
71/*{31, 12, 1977, 17},*/
72/*{31, 12, 1978, 18},*/
73/*{31, 12, 1979, 19},*/
74{30, 06, 1981,20},
75{30, 06, 1982,21},
76{30, 06, 1983,22},
77{30, 06, 1985,23},
78{31, 12, 1987,24},
79{31, 12, 1989,25},
80{31, 12, 1990,26},
81{30, 06, 1992,27},
82{30, 06, 1993,28},
83{30, 06, 1994,29},
84{31, 12, 1995,30},
85{30, 06, 1997,31},
86{31, 12, 1998,32},
87{31, 12, 2005,33},
88{31, 12, 2008,34},
89{30, 06, 2012,35},
90{30, 06, 2015,36},
[9503]91{31, 12, 2016,37},
[6812]92{0,0,0,0} /* end marker */
93};
94
95#define GPSLEAPSTART 19 /* 19 leap seconds existed at 6.1.1980 */
96
[10533]97static int longyear(int year, int month) {
[6812]98 if(!(year % 4) && (!(year % 400) || (year % 100)))
99 {
100 if(!month || month == 2)
101 return 1;
102 }
103 return 0;
104}
105
[10533]106int gnumleap(int year, int month, int day) {
[6812]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) */
[10533]119void updatetime(int *week, int *secOfWeek, int mSecOfWeek, bool fixnumleap) {
[6812]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
[7527]145 // new version
[6812]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
[7527]158//
[1381]159////////////////////////////////////////////////////////////////////////////
[83]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);
[8203]169 str.replace(rxStr, qgetenv(envVar.toLatin1()));
[83]170 }
171 }
172}
[124]173
[5910]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
[7527]187//
[1381]188////////////////////////////////////////////////////////////////////////////
[124]189QDateTime dateAndTimeFromGPSweek(int GPSWeek, double GPSWeeks) {
190
191 static const QDate zeroEpoch(1980, 1, 6);
[7527]192
[124]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}
[210]202
[7527]203//
[1381]204////////////////////////////////////////////////////////////////////////////
[218]205void currentGPSWeeks(int& week, double& sec) {
[210]206
[1942]207 QDateTime currDateTimeGPS;
[1155]208
[5846]209 if ( BNC_CORE->dateAndTimeGPSSet() ) {
210 currDateTimeGPS = BNC_CORE->dateAndTimeGPS();
[1155]211 }
212 else {
[1942]213 currDateTimeGPS = QDateTime::currentDateTime().toUTC();
214 QDate hlp = currDateTimeGPS.date();
[7527]215 currDateTimeGPS = currDateTimeGPS.addSecs(gnumleap(hlp.year(),
[1942]216 hlp.month(), hlp.day()));
[1155]217 }
218
[1942]219 QDate currDateGPS = currDateTimeGPS.date();
220 QTime currTimeGPS = currDateTimeGPS.time();
[210]221
[1942]222 week = int( (double(currDateGPS.toJulianDay()) - 2444244.5) / 7 );
[1036]223
[7527]224 sec = (currDateGPS.dayOfWeek() % 7) * 24.0 * 3600.0 +
225 currTimeGPS.hour() * 3600.0 +
226 currTimeGPS.minute() * 60.0 +
[1942]227 currTimeGPS.second() +
228 currTimeGPS.msec() / 1000.0;
[1036]229}
[1154]230
[7527]231//
[1381]232////////////////////////////////////////////////////////////////////////////
[1154]233QDateTime currentDateAndTimeGPS() {
[5846]234 if ( BNC_CORE->dateAndTimeGPSSet() ) {
235 return BNC_CORE->dateAndTimeGPS();
[2530]236 }
237 else {
238 int GPSWeek;
239 double GPSWeeks;
240 currentGPSWeeks(GPSWeek, GPSWeeks);
241 return dateAndTimeFromGPSweek(GPSWeek, GPSWeeks);
242 }
[1154]243}
244
[7527]245//
[1381]246////////////////////////////////////////////////////////////////////////////
[8011]247bool checkForWrongObsEpoch(bncTime obsEpoch) {
248 const double maxDt = 600.0;
[8417]249 bncTime obsTime = obsEpoch;
[8011]250 int week;
251 double sec;
252 currentGPSWeeks(week, sec);
[8417]253 bncTime currTime(week, sec);
[8011]254
[10568]255 if (fabs(currTime - obsTime) > maxDt) {
[8011]256 return true;
257 }
258 return false;
259}
[9245]260
[8011]261//
262////////////////////////////////////////////////////////////////////////////
[10330]263bool outDatedBcep(const t_eph *eph, bncTime tt) {
[9245]264 bncTime toc = eph->TOC();
[10330]265 double dt = tt -toc;
[9245]266
[9376]267 // update interval: 2h, data sets are valid for 4 hours
[9409]268 if (eph->type() == t_eph::GPS && (dt > 14400.0 || dt < -7200.0)) {
[9245]269 return true;
270 }
[9376]271 // update interval: 3h, data sets are valid for 4 hours
[9409]272 else if (eph->type() == t_eph::Galileo && (dt > 14400.0 || dt < 0.0)) {
[9245]273 return true;
274 }
[9362]275 // updated every 30 minutes + 5 min
[10330]276 else if (eph->type() == t_eph::GLONASS && (dt > 2100.0 || dt < -2100.0)) {
[9245]277 return true;
[9362]278 }
[9376]279 // orbit parameters are valid for 7200 seconds (minimum)
[9409]280 else if (eph->type() == t_eph::QZSS && (dt > 7200.0 || dt < -3600.0)) {
[9245]281 return true;
282 }
[9376]283 // maximum update interval: 300 sec
[9409]284 else if (eph->type() == t_eph::SBAS && (dt > 600.0 || dt < -600.0)) {
[9245]285 return true;
286 }
[9362]287 // updates 1h + 5 min
[9409]288 else if (eph->type() == t_eph::BDS && (dt > 3900.0 || dt < 0.0) ) {
[9245]289 return true;
290 }
[9376]291 // update interval: up to 24 hours
[9409]292 else if (eph->type() == t_eph::IRNSS && (fabs(dt > 86400.0))) {
[9245]293 return true;
294 }
[9409]295
[9245]296 return false;
297}
298
299//
300////////////////////////////////////////////////////////////////////////////
[1595]301QByteArray ggaString(const QByteArray& latitude,
302 const QByteArray& longitude,
[6786]303 const QByteArray& height,
304 const QString& ggaType) {
[1381]305
306 double lat = strtod(latitude,NULL);
307 double lon = strtod(longitude,NULL);
[1595]308 double hei = strtod(height,NULL);
[6786]309 QString sentences = "GPGGA,";
310 if (ggaType.contains("GNGGA")) {
311 sentences = "GNGGA,";
312 }
[1381]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());
[7527]321 int lat_deg = (int)lat;
[1381]322 double lat_min=(lat-lat_deg)*60.;
[7527]323 int lon_deg = (int)lon;
[1381]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;
[6786]331 gga += sentences;
[1381]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'));
[1595]336 gga += flagE + QString(",1,05,1.00");
[1599]337 gga += QString(",%1,").arg(hei, 2, 'f', 1);
[1595]338 gga += QString("M,10.000,M,,");
[7527]339
[8646]340 unsigned char XOR = 0;
341 for (int ii = 0; ii < gga.length(); ii++) {
342 XOR ^= (unsigned char) gga[ii].toLatin1();
[1381]343 }
[9787]344 gga = "$" + gga + QString("*%1").arg(XOR, 2, 16, QLatin1Char('0')) + "\r\n";
[1381]345
[8203]346 return gga.toLatin1();
[1381]347}
[2043]348
[7527]349//
[2043]350////////////////////////////////////////////////////////////////////////////
351void RSW_to_XYZ(const ColumnVector& rr, const ColumnVector& vv,
352 const ColumnVector& rsw, ColumnVector& xyz) {
353
[8903]354 ColumnVector along = vv / vv.NormFrobenius();
355 ColumnVector cross = crossproduct(rr, vv); cross /= cross.NormFrobenius();
[2043]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}
[2063]365
[2988]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
[8903]371 ColumnVector along = vv / vv.NormFrobenius();
372 ColumnVector cross = crossproduct(rr, vv); cross /= cross.NormFrobenius();
[2988]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
[2063]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) ;
[7527]388
[2063]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
[7527]398 Ell[0] = atan( (XYZ[2] + e2c * bell * sin3) / (ss - e2 * t_CST::aell * cos3) );
[2063]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}
[2065]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}
[2221]438
[2582]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
[7244]460// Rectangular Coordinates -> Geocentric Coordinates
461////////////////////////////////////////////////////////////////////////////
[7251]462t_irc xyz2geoc(const double* XYZ, double* Geoc) {
[7244]463
464 const double bell = t_CST::aell*(1.0-1.0/t_CST::fInv) ;
[7260]465 const double e2 = (t_CST::aell*t_CST::aell-bell*bell)/(t_CST::aell*t_CST::aell) ;
[7244]466 double Ell[3];
[7251]467 if (xyz2ell(XYZ, Ell) != success) {
468 return failure;
469 }
[7244]470 double rho = sqrt(XYZ[0]*XYZ[0]+XYZ[1]*XYZ[1]+XYZ[2]*XYZ[2]);
[7260]471 double Rn = t_CST::aell/sqrt(1-e2*pow(sin(Ell[0]),2));
[7244]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;
[7251]476
477 return success;
[7244]478}
479
[7527]480//
[5807]481////////////////////////////////////////////////////////////////////////////
482double Frac (double x) {
[7527]483 return x-floor(x);
[5807]484}
485
[7183]486//
[5807]487////////////////////////////////////////////////////////////////////////////
[7527]488double Modulo (double x, double y) {
489 return y*Frac(x/y);
[5807]490}
491
[5753]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
[7183]498//
499////////////////////////////////////////////////////////////////////////////
[8774]500double factorial(int n) {
[7183]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++) {
[7228]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));
[7183]518 }
519 double fac = pow(2.0,(double) -n) * pow((1 - t*t), (double)m/2);
520 return sum *= fac;
521}
522
523
[7527]524// Jacobian XYZ --> NEU
[5752]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;
[7527]538
539 jacobi(2,1) = - sinLam;
[5752]540 jacobi(2,2) = cosLam;
[7527]541 jacobi(2,3) = 0.0;
542
543 jacobi(3,1) = cosPhi * cosLam;
544 jacobi(3,2) = cosPhi * sinLam;
[5752]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;
[7527]575}
[5752]576
577// Covariance Matrix in NEU
578////////////////////////////////////////////////////////////////////////////
[7527]579void covariXYZ_NEU(const SymmetricMatrix& QQxyz, const double* Ell,
[5752]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////////////////////////////////////////////////////////////////////////////
[7527]591void covariNEU_XYZ(const SymmetricMatrix& QQneu, const double* Ell,
[5752]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
[2221]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
[8417]607 double* acc, // additional acceleration
[2556]608 ColumnVector (*der)(double x, const ColumnVector& y, double* acc)
[7527]609 // A pointer to a function that computes the
[2221]610 // derivative of a function at a point (x,y)
611 ) {
612
[2556]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;
[2221]617
618 ColumnVector yf = yi + k1/6.0 + k2/3.0 + k3/3.0 + k4/6.0;
[7527]619
[2221]620 return yf;
621}
[7527]622//
[3044]623////////////////////////////////////////////////////////////////////////////
[5886]624double djul(long jj, long mm, double tt) {
625 long ii, kk;
[3171]626 double djul ;
627 if( mm <= 2 ) {
628 jj = jj - 1;
629 mm = mm + 12;
[7527]630 }
[3171]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;
[7527]636}
[3171]637
[7527]638//
[3171]639////////////////////////////////////////////////////////////////////////////
[5886]640double gpjd(double second, int nweek) {
641 double deltat;
642 deltat = nweek*7.0 + second/86400.0 ;
643 return( 44244.0 + deltat) ;
[7527]644}
[5886]645
[7527]646//
[5886]647////////////////////////////////////////////////////////////////////////////
648void jdgp(double tjul, double & second, long & nweek) {
[3171]649 double deltat;
650 deltat = tjul - 44244.0 ;
[5886]651 nweek = (long) floor(deltat/7.0);
[3171]652 second = (deltat - (nweek)*7.0)*86400.0;
653}
654
[7527]655//
[3171]656////////////////////////////////////////////////////////////////////////////
[5886]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;
[7527]673}
[5886]674
[7527]675//
[5886]676////////////////////////////////////////////////////////////////////////////
[7527]677void GPSweekFromDateAndTime(const QDateTime& dateTime,
[3044]678 int& GPSWeek, double& GPSWeeks) {
679
680 static const QDateTime zeroEpoch(QDate(1980, 1, 6),QTime(),Qt::UTC);
[7527]681
[3044]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
[7527]688 - dateTime.time().msecsTo(QTime()) / 1e3;
[3044]689}
690
[7527]691//
[3044]692////////////////////////////////////////////////////////////////////////////
[3171]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
[5888]698 long GPSWeek_long;
699 jdgp(mjd, GPSWeeks, GPSWeek_long);
700 GPSWeek = GPSWeek_long;
[7527]701 GPSWeeks += hour * 3600.0 + min * 60.0 + sec;
[3171]702}
703
[7527]704//
[3171]705////////////////////////////////////////////////////////////////////////////
[3044]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() +
[7527]714 (dateTime.time().second() +
[3044]715 dateTime.time().msec() / 1000.0) / 60.0) / 60.0) / 24.0;
716}
[3408]717
[7527]718//
[3408]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
[7527]730//
[3664]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
[7527]738//
[3664]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}
[4338]751
752// Topocentrical Distance and Elevation
753////////////////////////////////////////////////////////////////////////////
[7527]754void topos(double xRec, double yRec, double zRec,
755 double xSat, double ySat, double zSat,
[4338]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
[7527]763 rho = sqrt( dx[0]*dx[0] + dx[1]*dx[1] + dx[2]*dx[2] );
[4338]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 );
[9797]776 if (neu[2] < 0.0) {
[4338]777 eleSat *= -1.0;
778 }
779
780 azSat = atan2(neu[1], neu[0]);
781}
[5230]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);
[7787]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;
[5230]790}
[5310]791
[7527]792//
[5310]793////////////////////////////////////////////////////////////////////////////
794QString fortranFormat(double value, int width, int prec) {
[6537]795 int expo = value == 0.0 ? 0 : int(log10(fabs(value)));
[7656]796 double mant = value == 0.0 ? 0 : value / pow(10.0, double(expo));
[5310]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}
[5807]808
809//
810//////////////////////////////////////////////////////////////////////////////
[7527]811void kalman(const Matrix& AA, const ColumnVector& ll, const DiagonalMatrix& PP,
[6168]812 SymmetricMatrix& QQ, ColumnVector& xx) {
[5807]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;
[7527]828
[5807]829 UpperTriangularMatrix UU;
830 QRZ(SRF, UU);
[7527]831
[5807]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);
[7527]835
[5807]836 UpperTriangularMatrix SHi = SH_rt.i();
[7527]837
838 Matrix KT = SHi * YY;
[5807]839 SymmetricMatrix Hi; Hi << SHi * SHi.t();
840
[6168]841 xx += KT.t() * (ll - AA * xx);
[5807]842 QQ << (SS.t() * SS);
843}
844
[8903]845//
846////////////////////////////////////////////////////////////////////////////
[6799]847double accuracyFromIndex(int index, t_eph::e_type type) {
[8801]848double accuracy = -1.0;
[6799]849
[8782]850 if (type == t_eph::GPS ||
851 type == t_eph::BDS ||
852 type == t_eph::SBAS||
853 type == t_eph::QZSS) {
[6799]854 if ((index >= 0) && (index <= 6)) {
855 if (index == 3) {
[8801]856 accuracy = ceil(10.0 * pow(2.0, (double(index) / 2.0) + 1.0)) / 10.0;
[6799]857 }
858 else {
[8801]859 accuracy = floor(10.0 * pow(2.0, (double(index) / 2.0) + 1.0)) / 10.0;
[6799]860 }
861 }
862 else if ((index > 6) && (index <= 15)) {
[8801]863 accuracy = (10.0 * pow(2.0, (double(index) - 2.0))) / 10.0;
[6799]864 }
865 else {
[8801]866 accuracy = 8192.0;
[6799]867 }
868 }
[8790]869 else if (type == t_eph::Galileo) {
[6799]870 if ((index >= 0) && (index <= 49)) {
[8801]871 accuracy = (double(index) / 100.0);
[6799]872 }
873 else if ((index > 49) && (index <= 74)) {
[8801]874 accuracy = (50.0 + (double(index) - 50.0) * 2.0) / 100.0;
[6799]875 }
876 else if ((index > 74) && (index <= 99)) {
[8801]877 accuracy = 1.0 + (double(index) - 75.0) * 0.04;
[6799]878 }
879 else if ((index > 99) && (index <= 125)) {
[8801]880 accuracy = 2.0 + (double(index) - 100.0) * 0.16;
[6799]881 }
882 else {
[8801]883 accuracy = -1.0;
[6799]884 }
885 }
[8790]886 else if (type == t_eph::IRNSS) {
887 if ((index >= 0) && (index <= 6)) {
888 if (index == 1) {
[8801]889 accuracy = 2.8;
[8790]890 }
891 else if (index == 3) {
[8801]892 accuracy = 5.7;
[8790]893 }
894 else if (index == 5) {
[8801]895 accuracy = 11.3;
[8790]896 }
897 else {
[8801]898 accuracy = pow(2, 1 + index / 2);
[8790]899 }
900 }
901 else if ((index > 6) && (index <= 15)) {
[8801]902 accuracy = pow(2, index - 2);
[8790]903 }
904 }
[8801]905 return accuracy;
[6799]906}
907
[8903]908//
909////////////////////////////////////////////////////////////////////////////
[6799]910int indexFromAccuracy(double accuracy, t_eph::e_type type) {
911
[10091]912 if (type == t_eph::GPS ||
913 type == t_eph::BDS ||
914 type == t_eph::SBAS ||
915 type == t_eph::QZSS) {
[6799]916
917 if (accuracy <= 2.40) {
918 return 0;
919 }
920 else if (accuracy <= 3.40) {
921 return 1;
922 }
923 else if (accuracy <= 4.85) {
924 return 2;
925 }
926 else if (accuracy <= 6.85) {
927 return 3;
928 }
929 else if (accuracy <= 9.65) {
930 return 4;
931 }
932 else if (accuracy <= 13.65) {
933 return 5;
934 }
935 else if (accuracy <= 24.00) {
936 return 6;
937 }
938 else if (accuracy <= 48.00) {
939 return 7;
940 }
941 else if (accuracy <= 96.00) {
942 return 8;
943 }
944 else if (accuracy <= 192.00) {
945 return 9;
946 }
947 else if (accuracy <= 384.00) {
948 return 10;
949 }
950 else if (accuracy <= 768.00) {
951 return 11;
952 }
953 else if (accuracy <= 1536.00) {
954 return 12;
955 }
956 else if (accuracy <= 3072.00) {
957 return 13;
958 }
959 else if (accuracy <= 6144.00) {
960 return 14;
961 }
962 else {
963 return 15;
964 }
965 }
966
967 if (type == t_eph::Galileo) {
[6800]968
969 if (accuracy <= 0.49) {
970 return int(ceil(accuracy * 100.0));
971 }
972 else if (accuracy <= 0.98) {
973 return int(50.0 + (((accuracy * 100.0) - 50) / 2.0));
974 }
975 else if (accuracy <= 2.0) {
976 return int(75.0 + ((accuracy - 1.0) / 0.04));
977 }
978 else if (accuracy <= 6.0) {
979 return int(100.0 + ((accuracy - 2.0) / 0.16));
980 }
981 else {
982 return 255;
983 }
[6799]984 }
985
986 return (type == t_eph::Galileo) ? 255 : 15;
987}
[7053]988
[8903]989// Returns fit interval in hours from flag
990////////////////////////////////////////////////////////////////////////////
991double fitIntervalFromFlag(int flag, double iodc, t_eph::e_type type) {
992 double fitInterval = 0.0;
993
994 switch (flag) {
995 case 0:
996 if (type == t_eph::GPS) {
997 fitInterval = 4.0;
998 }
999 else if (type == t_eph::QZSS) {
1000 fitInterval = 2.0;
1001 }
1002 break;
1003 case 1:
1004 if (type == t_eph::GPS) {
1005 if (iodc >= 240 && iodc <= 247) {
1006 fitInterval = 8.0;
1007 }
1008 else if ((iodc >= 248 && iodc <= 255) ||
1009 (iodc == 496) ) {
1010 fitInterval = 14.0;
1011 }
1012 else if ((iodc >= 497 && iodc <= 503) ||
1013 (iodc >= 2021 && iodc <= 1023) ) {
1014 fitInterval = 26.0;
1015 }
1016 else {
1017 fitInterval = 6.0;
1018 }
1019 }
1020 break;
1021 }
1022 return fitInterval;
1023}
1024
[7053]1025// Returns CRC24
1026////////////////////////////////////////////////////////////////////////////
1027unsigned long CRC24(long size, const unsigned char *buf) {
1028 unsigned long crc = 0;
1029 int ii;
1030 while (size--) {
1031 crc ^= (*buf++) << (16);
1032 for(ii = 0; ii < 8; ii++) {
1033 crc <<= 1;
1034 if (crc & 0x1000000) {
1035 crc ^= 0x01864cfb;
1036 }
1037 }
1038 }
1039 return crc;
1040}
1041
[9368]1042// Extracts k bits from position p and returns the extracted value as integer
1043////////////////////////////////////////////////////////////////////////////
1044int bitExtracted(int number, int k, int p) {
1045 return (((1 << k) - 1) & (number >> (p - 1)));
1046}
1047
[9088]1048// Convert RTCM3 lock-time indicator to minimum lock time in seconds
[8617]1049////////////////////////////////////////////////////////////////////////////
1050double lti2sec(int type, int lti) {
1051
1052 if ( (type>=1001 && type<=1004) ||
1053 (type>=1009 && type<=1012) ) { // RTCM3 msg 100[1...4] and 10[09...12]
1054 if (lti< 0) return -1;
1055 else if (lti< 24) return 1*lti; // [ 0 1 23]
1056 else if (lti< 48) return 2*lti-24; // [ 24 2 70]
1057 else if (lti< 72) return 4*lti-120; // [ 72 4 164]
1058 else if (lti< 96) return 8*lti-408; // [168 8 352]
1059 else if (lti< 120) return 16*lti-1176; // [360 16 728]
1060 else if (lti< 127) return 32*lti-3096; // [744 32 905]
1061 else if (lti==127) return 937;
[9088]1062 else return -1.0;
[8617]1063 }
1064 else if (type%10==2 || type%10==3 ||
1065 type%10==4 || type%10==5) { // RTCM3 MSM-2/-3/-4/-5
1066 switch(lti) {
1067 case( 0) : return 0;
1068 case( 1) : return 32e-3;
1069 case( 2) : return 64e-3;
1070 case( 3) : return 128e-3;
1071 case( 4) : return 256e-3;
1072 case( 5) : return 512e-3;
1073 case( 6) : return 1024e-3;
1074 case( 7) : return 2048e-3;
1075 case( 8) : return 4096e-3;
1076 case( 9) : return 8192e-3;
1077 case(10) : return 16384e-3;
1078 case(11) : return 32768e-3;
1079 case(12) : return 65536e-3;
1080 case(13) : return 131072e-3;
1081 case(14) : return 262144e-3;
1082 case(15) : return 524288e-3;
[9088]1083 default : return -1.0;
[8617]1084 };
1085 }
1086 else if (type%10==6 || type%10==7) { // RTCM3 MSM-6 and MSM-7
1087 if (lti< 0) return ( -1 );
1088 else if (lti< 64) return ( 1*lti )*1e-3;
1089 else if (lti< 96) return ( 2*lti-64 )*1e-3;
1090 else if (lti< 128) return ( 4*lti-256 )*1e-3;
1091 else if (lti< 160) return ( 8*lti-768 )*1e-3;
1092 else if (lti< 192) return ( 16*lti-2048 )*1e-3;
1093 else if (lti< 224) return ( 32*lti-5120 )*1e-3;
1094 else if (lti< 256) return ( 64*lti-12288 )*1e-3;
1095 else if (lti< 288) return ( 128*lti-28672 )*1e-3;
1096 else if (lti< 320) return ( 256*lti-65536 )*1e-3;
1097 else if (lti< 352) return ( 512*lti-147456 )*1e-3;
1098 else if (lti< 384) return ( 1024*lti-327680 )*1e-3;
1099 else if (lti< 416) return ( 2048*lti-720896 )*1e-3;
1100 else if (lti< 448) return ( 4096*lti-1572864 )*1e-3;
1101 else if (lti< 480) return ( 8192*lti-3407872 )*1e-3;
1102 else if (lti< 512) return ( 16384*lti-7340032 )*1e-3;
1103 else if (lti< 544) return ( 32768*lti-15728640 )*1e-3;
1104 else if (lti< 576) return ( 65536*lti-33554432 )*1e-3;
1105 else if (lti< 608) return ( 131072*lti-71303168 )*1e-3;
1106 else if (lti< 640) return ( 262144*lti-150994944 )*1e-3;
1107 else if (lti< 672) return ( 524288*lti-318767104 )*1e-3;
1108 else if (lti< 704) return (1048576*lti-671088640 )*1e-3;
1109 else if (lti==704) return (2097152*lti-1409286144)*1e-3;
[9088]1110 else return ( -1.0 );
[8617]1111 }
1112 else {
[9088]1113 return -1.0;
[8617]1114 };
1115};
Note: See TracBrowser for help on using the repository browser.