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

Last change on this file since 7244 was 7244, checked in by stuerze, 9 years ago

method for transformation of rectangular coordinates into geocentric coordinates is added

File size: 26.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{0,0,0,0} /* end marker */
92};
93
94#define GPSLEAPSTART 19 /* 19 leap seconds existed at 6.1.1980 */
95
96static int longyear(int year, int month)
97{
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{
108 int ls = 0;
109 const struct leapseconds *l;
110
111 for(l = leap; l->taicount && year >= l->year; ++l)
112 {
113 if(year > l->year || month > l->month || (month == l->month && day > l->day))
114 ls = l->taicount - GPSLEAPSTART;
115 }
116 return ls;
117}
118
119/* Convert Moscow time into UTC (fixnumleap == 1) or GPS (fixnumleap == 0) */
120void updatetime(int *week, int *secOfWeek, int mSecOfWeek, bool fixnumleap)
121{
122 int y,m,d,k,l, nul;
123 unsigned int j = *week*(7*24*60*60) + *secOfWeek + 5*24*60*60+3*60*60;
124 int glo_daynumber = 0, glo_timeofday;
125 for(y = 1980; j >= (unsigned int)(k = (l = (365+longyear(y,0)))*24*60*60)
126 + gnumleap(y+1,1,1); ++y)
127 {
128 j -= k; glo_daynumber += l;
129 }
130 for(m = 1; j >= (unsigned int)(k = (l = months[m]+longyear(y, m))*24*60*60)
131 + gnumleap(y, m+1, 1); ++m)
132 {
133 j -= k; glo_daynumber += l;
134 }
135 for(d = 1; j >= 24UL*60UL*60UL + gnumleap(y, m, d+1); ++d)
136 j -= 24*60*60;
137 glo_daynumber -= 16*365+4-d;
138 nul = gnumleap(y, m, d);
139 glo_timeofday = j-nul;
140
141 // original version
142 // if(mSecOfWeek < 5*60*1000 && glo_timeofday > 23*60*60)
143 // *secOfWeek += 24*60*60;
144 // else if(glo_timeofday < 5*60 && mSecOfWeek > 23*60*60*1000)
145 // *secOfWeek -= 24*60*60;
146
147 // new version
148 if(mSecOfWeek < 4*60*60*1000 && glo_timeofday > 20*60*60)
149 *secOfWeek += 24*60*60;
150 else if(glo_timeofday < 4*60*60 && mSecOfWeek > 20*60*60*1000)
151 *secOfWeek -= 24*60*60;
152
153 *secOfWeek += mSecOfWeek/1000-glo_timeofday;
154 if(fixnumleap)
155 *secOfWeek -= nul;
156 if(*secOfWeek < 0) {*secOfWeek += 24*60*60*7; --*week; }
157 if(*secOfWeek >= 24*60*60*7) {*secOfWeek -= 24*60*60*7; ++*week; }
158}
159
160//
161////////////////////////////////////////////////////////////////////////////
162void expandEnvVar(QString& str) {
163
164 QRegExp rx("(\\$\\{.+\\})");
165
166 if (rx.indexIn(str) != -1) {
167 QStringListIterator it(rx.capturedTexts());
168 if (it.hasNext()) {
169 QString rxStr = it.next();
170 QString envVar = rxStr.mid(2,rxStr.length()-3);
171 str.replace(rxStr, qgetenv(envVar.toAscii()));
172 }
173 }
174
175}
176
177// Strip White Space
178////////////////////////////////////////////////////////////////////////////
179void stripWhiteSpace(string& str) {
180 if (!str.empty()) {
181 string::size_type beg = str.find_first_not_of(" \t\f\n\r\v");
182 string::size_type end = str.find_last_not_of(" \t\f\n\r\v");
183 if (beg > str.max_size())
184 str.erase();
185 else
186 str = str.substr(beg, end-beg+1);
187 }
188}
189
190//
191////////////////////////////////////////////////////////////////////////////
192QDateTime dateAndTimeFromGPSweek(int GPSWeek, double GPSWeeks) {
193
194 static const QDate zeroEpoch(1980, 1, 6);
195
196 QDate date(zeroEpoch);
197 QTime time(0,0,0,0);
198
199 int weekDays = int(GPSWeeks) / 86400;
200 date = date.addDays( GPSWeek * 7 + weekDays );
201 time = time.addMSecs( int( (GPSWeeks - 86400 * weekDays) * 1e3 ) );
202
203 return QDateTime(date,time);
204}
205
206//
207////////////////////////////////////////////////////////////////////////////
208void currentGPSWeeks(int& week, double& sec) {
209
210 QDateTime currDateTimeGPS;
211
212 if ( BNC_CORE->dateAndTimeGPSSet() ) {
213 currDateTimeGPS = BNC_CORE->dateAndTimeGPS();
214 }
215 else {
216 currDateTimeGPS = QDateTime::currentDateTime().toUTC();
217 QDate hlp = currDateTimeGPS.date();
218 currDateTimeGPS = currDateTimeGPS.addSecs(gnumleap(hlp.year(),
219 hlp.month(), hlp.day()));
220 }
221
222 QDate currDateGPS = currDateTimeGPS.date();
223 QTime currTimeGPS = currDateTimeGPS.time();
224
225 week = int( (double(currDateGPS.toJulianDay()) - 2444244.5) / 7 );
226
227 sec = (currDateGPS.dayOfWeek() % 7) * 24.0 * 3600.0 +
228 currTimeGPS.hour() * 3600.0 +
229 currTimeGPS.minute() * 60.0 +
230 currTimeGPS.second() +
231 currTimeGPS.msec() / 1000.0;
232}
233
234//
235////////////////////////////////////////////////////////////////////////////
236QDateTime currentDateAndTimeGPS() {
237 if ( BNC_CORE->dateAndTimeGPSSet() ) {
238 return BNC_CORE->dateAndTimeGPS();
239 }
240 else {
241 int GPSWeek;
242 double GPSWeeks;
243 currentGPSWeeks(GPSWeek, GPSWeeks);
244 return dateAndTimeFromGPSweek(GPSWeek, GPSWeeks);
245 }
246}
247
248//
249////////////////////////////////////////////////////////////////////////////
250QByteArray ggaString(const QByteArray& latitude,
251 const QByteArray& longitude,
252 const QByteArray& height,
253 const QString& ggaType) {
254
255 double lat = strtod(latitude,NULL);
256 double lon = strtod(longitude,NULL);
257 double hei = strtod(height,NULL);
258 QString sentences = "GPGGA,";
259 if (ggaType.contains("GNGGA")) {
260 sentences = "GNGGA,";
261 }
262
263 const char* flagN="N";
264 const char* flagE="E";
265 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
266 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
267 if (lon < -180.) {lon=(lon+360.); flagE="E";}
268 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
269 QTime ttime(QDateTime::currentDateTime().toUTC().time());
270 int lat_deg = (int)lat;
271 double lat_min=(lat-lat_deg)*60.;
272 int lon_deg = (int)lon;
273 double lon_min=(lon-lon_deg)*60.;
274 int hh = 0 , mm = 0;
275 double ss = 0.0;
276 hh=ttime.hour();
277 mm=ttime.minute();
278 ss=(double)ttime.second()+0.001*ttime.msec();
279 QString gga;
280 gga += sentences;
281 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'));
282 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
283 gga += flagN;
284 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
285 gga += flagE + QString(",1,05,1.00");
286 gga += QString(",%1,").arg(hei, 2, 'f', 1);
287 gga += QString("M,10.000,M,,");
288 int xori;
289 char XOR = 0;
290 char *Buff =gga.toAscii().data();
291 int iLen = strlen(Buff);
292 for (xori = 0; xori < iLen; xori++) {
293 XOR ^= (char)Buff[xori];
294 }
295 gga = "$" + gga + QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
296
297 return gga.toAscii();
298}
299
300//
301////////////////////////////////////////////////////////////////////////////
302void RSW_to_XYZ(const ColumnVector& rr, const ColumnVector& vv,
303 const ColumnVector& rsw, ColumnVector& xyz) {
304
305 ColumnVector along = vv / vv.norm_Frobenius();
306 ColumnVector cross = crossproduct(rr, vv); cross /= cross.norm_Frobenius();
307 ColumnVector radial = crossproduct(along, cross);
308
309 Matrix RR(3,3);
310 RR.Column(1) = radial;
311 RR.Column(2) = along;
312 RR.Column(3) = cross;
313
314 xyz = RR * rsw;
315}
316
317// Transformation xyz --> radial, along track, out-of-plane
318////////////////////////////////////////////////////////////////////////////
319void XYZ_to_RSW(const ColumnVector& rr, const ColumnVector& vv,
320 const ColumnVector& xyz, ColumnVector& rsw) {
321
322 ColumnVector along = vv / vv.norm_Frobenius();
323 ColumnVector cross = crossproduct(rr, vv); cross /= cross.norm_Frobenius();
324 ColumnVector radial = crossproduct(along, cross);
325
326 rsw.ReSize(3);
327 rsw(1) = DotProduct(xyz, radial);
328 rsw(2) = DotProduct(xyz, along);
329 rsw(3) = DotProduct(xyz, cross);
330}
331
332// Rectangular Coordinates -> Ellipsoidal Coordinates
333////////////////////////////////////////////////////////////////////////////
334t_irc xyz2ell(const double* XYZ, double* Ell) {
335
336 const double bell = t_CST::aell*(1.0-1.0/t_CST::fInv) ;
337 const double e2 = (t_CST::aell*t_CST::aell-bell*bell)/(t_CST::aell*t_CST::aell) ;
338 const double e2c = (t_CST::aell*t_CST::aell-bell*bell)/(bell*bell) ;
339
340 double nn, ss, zps, hOld, phiOld, theta, sin3, cos3;
341
342 ss = sqrt(XYZ[0]*XYZ[0]+XYZ[1]*XYZ[1]) ;
343 zps = XYZ[2]/ss ;
344 theta = atan( (XYZ[2]*t_CST::aell) / (ss*bell) );
345 sin3 = sin(theta) * sin(theta) * sin(theta);
346 cos3 = cos(theta) * cos(theta) * cos(theta);
347
348 // Closed formula
349 Ell[0] = atan( (XYZ[2] + e2c * bell * sin3) / (ss - e2 * t_CST::aell * cos3) );
350 Ell[1] = atan2(XYZ[1],XYZ[0]) ;
351 nn = t_CST::aell/sqrt(1.0-e2*sin(Ell[0])*sin(Ell[0])) ;
352 Ell[2] = ss / cos(Ell[0]) - nn;
353
354 const int MAXITER = 100;
355 for (int ii = 1; ii <= MAXITER; ii++) {
356 nn = t_CST::aell/sqrt(1.0-e2*sin(Ell[0])*sin(Ell[0])) ;
357 hOld = Ell[2] ;
358 phiOld = Ell[0] ;
359 Ell[2] = ss/cos(Ell[0])-nn ;
360 Ell[0] = atan(zps/(1.0-e2*nn/(nn+Ell[2]))) ;
361 if ( fabs(phiOld-Ell[0]) <= 1.0e-11 && fabs(hOld-Ell[2]) <= 1.0e-5 ) {
362 return success;
363 }
364 }
365
366 return failure;
367}
368
369// Rectangular Coordinates -> North, East, Up Components
370////////////////////////////////////////////////////////////////////////////
371void xyz2neu(const double* Ell, const double* xyz, double* neu) {
372
373 double sinPhi = sin(Ell[0]);
374 double cosPhi = cos(Ell[0]);
375 double sinLam = sin(Ell[1]);
376 double cosLam = cos(Ell[1]);
377
378 neu[0] = - sinPhi*cosLam * xyz[0]
379 - sinPhi*sinLam * xyz[1]
380 + cosPhi * xyz[2];
381
382 neu[1] = - sinLam * xyz[0]
383 + cosLam * xyz[1];
384
385 neu[2] = + cosPhi*cosLam * xyz[0]
386 + cosPhi*sinLam * xyz[1]
387 + sinPhi * xyz[2];
388}
389
390// North, East, Up Components -> Rectangular Coordinates
391////////////////////////////////////////////////////////////////////////////
392void neu2xyz(const double* Ell, const double* neu, double* xyz) {
393
394 double sinPhi = sin(Ell[0]);
395 double cosPhi = cos(Ell[0]);
396 double sinLam = sin(Ell[1]);
397 double cosLam = cos(Ell[1]);
398
399 xyz[0] = - sinPhi*cosLam * neu[0]
400 - sinLam * neu[1]
401 + cosPhi*cosLam * neu[2];
402
403 xyz[1] = - sinPhi*sinLam * neu[0]
404 + cosLam * neu[1]
405 + cosPhi*sinLam * neu[2];
406
407 xyz[2] = + cosPhi * neu[0]
408 + sinPhi * neu[2];
409}
410
411// Rectangular Coordinates -> Geocentric Coordinates
412////////////////////////////////////////////////////////////////////////////
413void xyz2geoc(const double* XYZ, double* Geoc) {
414
415 const double bell = t_CST::aell*(1.0-1.0/t_CST::fInv) ;
416 const double e2 = (t_CST::aell*t_CST::aell-bell*bell)/(t_CST::aell*t_CST::aell) ;
417
418 double Ell[3];
419 xyz2ell(XYZ, Ell);
420
421 double rho = sqrt(XYZ[0]*XYZ[0]+XYZ[1]*XYZ[1]+XYZ[2]*XYZ[2]);
422 double Rn = t_CST::aell/sqrt(1-(e2)*pow(sin(Ell[0]),2));
423
424 Geoc[0] = atan((1-e2 * Rn/(Rn + Ell[2])) * tan(Ell[0]));
425 Geoc[1] = Ell[1];
426 Geoc[2] = rho-t_CST::rgeoc;
427}
428
429//
430////////////////////////////////////////////////////////////////////////////
431double Frac (double x) {
432 return x-floor(x);
433}
434
435//
436////////////////////////////////////////////////////////////////////////////
437double Modulo (double x, double y) {
438 return y*Frac(x/y);
439}
440
441// Round to nearest integer
442////////////////////////////////////////////////////////////////////////////
443double nint(double val) {
444 return ((val < 0.0) ? -floor(fabs(val)+0.5) : floor(val+0.5));
445}
446
447//
448////////////////////////////////////////////////////////////////////////////
449int factorial(int n) {
450 if (n == 0) {
451 return 1;
452 }
453 else {
454 return (n * factorial(n - 1));
455 }
456}
457
458//
459////////////////////////////////////////////////////////////////////////////
460double associatedLegendreFunction(int n, int m, double t) {
461 double sum = 0.0;
462 int r = (int) floor((n - m) / 2);
463 for (int k = 0; k <= r; k++) {
464 sum += (pow(-1.0, (double)k) * factorial(2*n - 2*k)
465 / (factorial(k) * factorial(n-k) * factorial(n-m-2*k))
466 * pow(t, (double)n-m-2*k));
467 }
468 double fac = pow(2.0,(double) -n) * pow((1 - t*t), (double)m/2);
469 return sum *= fac;
470}
471
472
473// Jacobian XYZ --> NEU
474////////////////////////////////////////////////////////////////////////////
475void jacobiXYZ_NEU(const double* Ell, Matrix& jacobi) {
476
477 Tracer tracer("jacobiXYZ_NEU");
478
479 double sinPhi = sin(Ell[0]);
480 double cosPhi = cos(Ell[0]);
481 double sinLam = sin(Ell[1]);
482 double cosLam = cos(Ell[1]);
483
484 jacobi(1,1) = - sinPhi * cosLam;
485 jacobi(1,2) = - sinPhi * sinLam;
486 jacobi(1,3) = cosPhi;
487
488 jacobi(2,1) = - sinLam;
489 jacobi(2,2) = cosLam;
490 jacobi(2,3) = 0.0;
491
492 jacobi(3,1) = cosPhi * cosLam;
493 jacobi(3,2) = cosPhi * sinLam;
494 jacobi(3,3) = sinPhi;
495}
496
497// Jacobian Ell --> XYZ
498////////////////////////////////////////////////////////////////////////////
499void jacobiEll_XYZ(const double* Ell, Matrix& jacobi) {
500
501 Tracer tracer("jacobiEll_XYZ");
502
503 double sinPhi = sin(Ell[0]);
504 double cosPhi = cos(Ell[0]);
505 double sinLam = sin(Ell[1]);
506 double cosLam = cos(Ell[1]);
507 double hh = Ell[2];
508
509 double bell = t_CST::aell*(1.0-1.0/t_CST::fInv);
510 double e2 = (t_CST::aell*t_CST::aell-bell*bell)/(t_CST::aell*t_CST::aell) ;
511 double nn = t_CST::aell/sqrt(1.0-e2*sinPhi*sinPhi) ;
512
513 jacobi(1,1) = -(nn+hh) * sinPhi * cosLam;
514 jacobi(1,2) = -(nn+hh) * cosPhi * sinLam;
515 jacobi(1,3) = cosPhi * cosLam;
516
517 jacobi(2,1) = -(nn+hh) * sinPhi * sinLam;
518 jacobi(2,2) = (nn+hh) * cosPhi * cosLam;
519 jacobi(2,3) = cosPhi * sinLam;
520
521 jacobi(3,1) = (nn*(1.0-e2)+hh) * cosPhi;
522 jacobi(3,2) = 0.0;
523 jacobi(3,3) = sinPhi;
524}
525
526// Covariance Matrix in NEU
527////////////////////////////////////////////////////////////////////////////
528void covariXYZ_NEU(const SymmetricMatrix& QQxyz, const double* Ell,
529 SymmetricMatrix& Qneu) {
530
531 Tracer tracer("covariXYZ_NEU");
532
533 Matrix CC(3,3);
534 jacobiXYZ_NEU(Ell, CC);
535 Qneu << CC * QQxyz * CC.t();
536}
537
538// Covariance Matrix in XYZ
539////////////////////////////////////////////////////////////////////////////
540void covariNEU_XYZ(const SymmetricMatrix& QQneu, const double* Ell,
541 SymmetricMatrix& Qxyz) {
542
543 Tracer tracer("covariNEU_XYZ");
544
545 Matrix CC(3,3);
546 jacobiXYZ_NEU(Ell, CC);
547 Qxyz << CC.t() * QQneu * CC;
548}
549
550// Fourth order Runge-Kutta numerical integrator for ODEs
551////////////////////////////////////////////////////////////////////////////
552ColumnVector rungeKutta4(
553 double xi, // the initial x-value
554 const ColumnVector& yi, // vector of the initial y-values
555 double dx, // the step size for the integration
556 double* acc, // aditional acceleration
557 ColumnVector (*der)(double x, const ColumnVector& y, double* acc)
558 // A pointer to a function that computes the
559 // derivative of a function at a point (x,y)
560 ) {
561
562 ColumnVector k1 = der(xi , yi , acc) * dx;
563 ColumnVector k2 = der(xi+dx/2.0, yi+k1/2.0, acc) * dx;
564 ColumnVector k3 = der(xi+dx/2.0, yi+k2/2.0, acc) * dx;
565 ColumnVector k4 = der(xi+dx , yi+k3 , acc) * dx;
566
567 ColumnVector yf = yi + k1/6.0 + k2/3.0 + k3/3.0 + k4/6.0;
568
569 return yf;
570}
571//
572////////////////////////////////////////////////////////////////////////////
573double djul(long jj, long mm, double tt) {
574 long ii, kk;
575 double djul ;
576 if( mm <= 2 ) {
577 jj = jj - 1;
578 mm = mm + 12;
579 }
580 ii = jj/100;
581 kk = 2 - ii + ii/4;
582 djul = (365.25*jj - fmod( 365.25*jj, 1.0 )) - 679006.0;
583 djul = djul + floor( 30.6001*(mm + 1) ) + tt + kk;
584 return djul;
585}
586
587//
588////////////////////////////////////////////////////////////////////////////
589double gpjd(double second, int nweek) {
590 double deltat;
591 deltat = nweek*7.0 + second/86400.0 ;
592 return( 44244.0 + deltat) ;
593}
594
595//
596////////////////////////////////////////////////////////////////////////////
597void jdgp(double tjul, double & second, long & nweek) {
598 double deltat;
599 deltat = tjul - 44244.0 ;
600 nweek = (long) floor(deltat/7.0);
601 second = (deltat - (nweek)*7.0)*86400.0;
602}
603
604//
605////////////////////////////////////////////////////////////////////////////
606void jmt(double djul, long& jj, long& mm, double& dd) {
607 long ih, ih1, ih2 ;
608 double t1, t2, t3, t4;
609 t1 = 1.0 + djul - fmod( djul, 1.0 ) + 2400000.0;
610 t4 = fmod( djul, 1.0 );
611 ih = long( (t1 - 1867216.25)/36524.25 );
612 t2 = t1 + 1 + ih - ih/4;
613 t3 = t2 - 1720995.0;
614 ih1 = long( (t3 - 122.1)/365.25 );
615 t1 = 365.25*ih1 - fmod( 365.25*ih1, 1.0 );
616 ih2 = long( (t3 - t1)/30.6001 );
617 dd = t3 - t1 - (int)( 30.6001*ih2 ) + t4;
618 mm = ih2 - 1;
619 if ( ih2 > 13 ) mm = ih2 - 13;
620 jj = ih1;
621 if ( mm <= 2 ) jj = jj + 1;
622}
623
624//
625////////////////////////////////////////////////////////////////////////////
626void GPSweekFromDateAndTime(const QDateTime& dateTime,
627 int& GPSWeek, double& GPSWeeks) {
628
629 static const QDateTime zeroEpoch(QDate(1980, 1, 6),QTime(),Qt::UTC);
630
631 GPSWeek = zeroEpoch.daysTo(dateTime) / 7;
632
633 int weekDay = dateTime.date().dayOfWeek() + 1; // Qt: Monday = 1
634 if (weekDay > 7) weekDay = 1;
635
636 GPSWeeks = (weekDay - 1) * 86400.0
637 - dateTime.time().msecsTo(QTime()) / 1e3;
638}
639
640//
641////////////////////////////////////////////////////////////////////////////
642void GPSweekFromYMDhms(int year, int month, int day, int hour, int min,
643 double sec, int& GPSWeek, double& GPSWeeks) {
644
645 double mjd = djul(year, month, day);
646
647 long GPSWeek_long;
648 jdgp(mjd, GPSWeeks, GPSWeek_long);
649 GPSWeek = GPSWeek_long;
650 GPSWeeks += hour * 3600.0 + min * 60.0 + sec;
651}
652
653//
654////////////////////////////////////////////////////////////////////////////
655void mjdFromDateAndTime(const QDateTime& dateTime, int& mjd, double& dayfrac) {
656
657 static const QDate zeroDate(1858, 11, 17);
658
659 mjd = zeroDate.daysTo(dateTime.date());
660
661 dayfrac = (dateTime.time().hour() +
662 (dateTime.time().minute() +
663 (dateTime.time().second() +
664 dateTime.time().msec() / 1000.0) / 60.0) / 60.0) / 24.0;
665}
666
667//
668////////////////////////////////////////////////////////////////////////////
669bool findInVector(const vector<QString>& vv, const QString& str) {
670 std::vector<QString>::const_iterator it;
671 for (it = vv.begin(); it != vv.end(); ++it) {
672 if ( (*it) == str) {
673 return true;
674 }
675 }
676 return false;
677}
678
679//
680////////////////////////////////////////////////////////////////////////////
681int readInt(const QString& str, int pos, int len, int& value) {
682 bool ok;
683 value = str.mid(pos, len).toInt(&ok);
684 return ok ? 0 : 1;
685}
686
687//
688////////////////////////////////////////////////////////////////////////////
689int readDbl(const QString& str, int pos, int len, double& value) {
690 QString hlp = str.mid(pos, len);
691 for (int ii = 0; ii < hlp.length(); ii++) {
692 if (hlp[ii]=='D' || hlp[ii]=='d' || hlp[ii] == 'E') {
693 hlp[ii]='e';
694 }
695 }
696 bool ok;
697 value = hlp.toDouble(&ok);
698 return ok ? 0 : 1;
699}
700
701// Topocentrical Distance and Elevation
702////////////////////////////////////////////////////////////////////////////
703void topos(double xRec, double yRec, double zRec,
704 double xSat, double ySat, double zSat,
705 double& rho, double& eleSat, double& azSat) {
706
707 double dx[3];
708 dx[0] = xSat-xRec;
709 dx[1] = ySat-yRec;
710 dx[2] = zSat-zRec;
711
712 rho = sqrt( dx[0]*dx[0] + dx[1]*dx[1] + dx[2]*dx[2] );
713
714 double xyzRec[3];
715 xyzRec[0] = xRec;
716 xyzRec[1] = yRec;
717 xyzRec[2] = zRec;
718
719 double Ell[3];
720 double neu[3];
721 xyz2ell(xyzRec, Ell);
722 xyz2neu(Ell, dx, neu);
723
724 eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
725 if (neu[2] < 0) {
726 eleSat *= -1.0;
727 }
728
729 azSat = atan2(neu[1], neu[0]);
730}
731
732// Degrees -> degrees, minutes, seconds
733////////////////////////////////////////////////////////////////////////////
734void deg2DMS(double decDeg, int& deg, int& min, double& sec) {
735 int sgn = (decDeg < 0.0 ? -1 : 1);
736 deg = sgn * static_cast<int>(decDeg);
737 min = static_cast<int>((decDeg - deg)*60);
738 sec = (decDeg - deg - min/60.0) * 3600.0;
739}
740
741//
742////////////////////////////////////////////////////////////////////////////
743QString fortranFormat(double value, int width, int prec) {
744 int expo = value == 0.0 ? 0 : int(log10(fabs(value)));
745 double mant = value == 0.0 ? 0 : value / pow(10, expo);
746 if (fabs(mant) >= 1.0) {
747 mant /= 10.0;
748 expo += 1;
749 }
750 if (expo >= 0) {
751 return QString("%1e+%2").arg(mant, width-4, 'f', prec).arg(expo, 2, 10, QChar('0'));
752 }
753 else {
754 return QString("%1e-%2").arg(mant, width-4, 'f', prec).arg(-expo, 2, 10, QChar('0'));
755 }
756}
757
758//
759//////////////////////////////////////////////////////////////////////////////
760void kalman(const Matrix& AA, const ColumnVector& ll, const DiagonalMatrix& PP,
761 SymmetricMatrix& QQ, ColumnVector& xx) {
762
763 Tracer tracer("kalman");
764
765 int nPar = AA.Ncols();
766 int nObs = AA.Nrows();
767 UpperTriangularMatrix SS = Cholesky(QQ).t();
768
769 Matrix SA = SS*AA.t();
770 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
771 for (int ii = 1; ii <= nObs; ++ii) {
772 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
773 }
774
775 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
776 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
777
778 UpperTriangularMatrix UU;
779 QRZ(SRF, UU);
780
781 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
782 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
783 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
784
785 UpperTriangularMatrix SHi = SH_rt.i();
786
787 Matrix KT = SHi * YY;
788 SymmetricMatrix Hi; Hi << SHi * SHi.t();
789
790 xx += KT.t() * (ll - AA * xx);
791 QQ << (SS.t() * SS);
792}
793
794double accuracyFromIndex(int index, t_eph::e_type type) {
795
796 if (type == t_eph::GPS || type == t_eph::BDS || type == t_eph::SBAS
797 || type == t_eph::QZSS) {
798
799 if ((index >= 0) && (index <= 6)) {
800 if (index == 3) {
801 return ceil(10.0 * pow(2.0, (double(index) / 2.0) + 1.0)) / 10.0;
802 }
803 else {
804 return floor(10.0 * pow(2.0, (double(index) / 2.0) + 1.0)) / 10.0;
805 }
806 }
807 else if ((index > 6) && (index <= 15)) {
808 return (10.0 * pow(2.0, (double(index) - 2.0))) / 10.0;
809 }
810 else {
811 return 8192.0;
812 }
813 }
814
815 if (type == t_eph::Galileo) {
816
817 if ((index >= 0) && (index <= 49)) {
818 return (double(index) / 100.0);
819 }
820 else if ((index > 49) && (index <= 74)) {
821 return (50.0 + (double(index) - 50.0) * 2.0) / 100.0;
822 }
823 else if ((index > 74) && (index <= 99)) {
824 return 1.0 + (double(index) - 75.0) * 0.04;
825 }
826 else if ((index > 99) && (index <= 125)) {
827 return 2.0 + (double(index) - 100.0) * 0.16;
828 }
829 else {
830 return -1.0;
831 }
832 }
833
834 return double(index);
835}
836
837int indexFromAccuracy(double accuracy, t_eph::e_type type) {
838
839 if (type == t_eph::GPS || type == t_eph::BDS || type == t_eph::SBAS
840 || type == t_eph::QZSS) {
841
842 if (accuracy <= 2.40) {
843 return 0;
844 }
845 else if (accuracy <= 3.40) {
846 return 1;
847 }
848 else if (accuracy <= 4.85) {
849 return 2;
850 }
851 else if (accuracy <= 6.85) {
852 return 3;
853 }
854 else if (accuracy <= 9.65) {
855 return 4;
856 }
857 else if (accuracy <= 13.65) {
858 return 5;
859 }
860 else if (accuracy <= 24.00) {
861 return 6;
862 }
863 else if (accuracy <= 48.00) {
864 return 7;
865 }
866 else if (accuracy <= 96.00) {
867 return 8;
868 }
869 else if (accuracy <= 192.00) {
870 return 9;
871 }
872 else if (accuracy <= 384.00) {
873 return 10;
874 }
875 else if (accuracy <= 768.00) {
876 return 11;
877 }
878 else if (accuracy <= 1536.00) {
879 return 12;
880 }
881 else if (accuracy <= 3072.00) {
882 return 13;
883 }
884 else if (accuracy <= 6144.00) {
885 return 14;
886 }
887 else {
888 return 15;
889 }
890 }
891
892 if (type == t_eph::Galileo) {
893
894 if (accuracy <= 0.49) {
895 return int(ceil(accuracy * 100.0));
896 }
897 else if (accuracy <= 0.98) {
898 return int(50.0 + (((accuracy * 100.0) - 50) / 2.0));
899 }
900 else if (accuracy <= 2.0) {
901 return int(75.0 + ((accuracy - 1.0) / 0.04));
902 }
903 else if (accuracy <= 6.0) {
904 return int(100.0 + ((accuracy - 2.0) / 0.16));
905 }
906 else {
907 return 255;
908 }
909 }
910
911 return (type == t_eph::Galileo) ? 255 : 15;
912}
913
914// Returns CRC24
915////////////////////////////////////////////////////////////////////////////
916unsigned long CRC24(long size, const unsigned char *buf) {
917 unsigned long crc = 0;
918 int ii;
919 while (size--) {
920 crc ^= (*buf++) << (16);
921 for(ii = 0; ii < 8; ii++) {
922 crc <<= 1;
923 if (crc & 0x1000000) {
924 crc ^= 0x01864cfb;
925 }
926 }
927 }
928 return crc;
929}
930
Note: See TracBrowser for help on using the repository browser.