source: ntrip/trunk/BNS/bnsutils.cpp@ 1091

Last change on this file since 1091 was 930, checked in by weber, 17 years ago

* empty log message *

File size: 4.6 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Server
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncutils
6 *
7 * Purpose: Auxiliary Functions
8 *
9 * Author: L. Mervart
10 *
11 * Created: 08-Apr-2008
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <iostream>
18#include <ctime>
19#include <math.h>
20
21#include <QRegExp>
22#include <QStringList>
23#include <QDateTime>
24
25#include "bnsutils.h"
26#include "bnseph.h"
27
28using namespace std;
29
30//
31////////////////////////////////////////////////////////////////////////////
32void expandEnvVar(QString& str) {
33
34 QRegExp rx("(\\$\\{.+\\})");
35
36 if (rx.indexIn(str) != -1) {
37 QStringListIterator it(rx.capturedTexts());
38 if (it.hasNext()) {
39 QString rxStr = it.next();
40 QString envVar = rxStr.mid(2,rxStr.length()-3);
41 str.replace(rxStr, qgetenv(envVar.toAscii()));
42 }
43 }
44
45}
46
47//
48////////////////////////////////////////////////////////////////////////////
49QDateTime dateAndTimeFromGPSweek(int GPSWeek, double GPSWeeks) {
50
51 static const QDate zeroEpoch(1980, 1, 6);
52
53 QDate date(zeroEpoch);
54 QTime time(0,0,0,0);
55
56 int weekDays = int(GPSWeeks) / 86400;
57 date = date.addDays( GPSWeek * 7 + weekDays );
58 time = time.addMSecs( int( (GPSWeeks - 86400 * weekDays) * 1e3 ) );
59
60 return QDateTime(date,time,Qt::UTC);
61}
62
63//
64////////////////////////////////////////////////////////////////////////////
65void GPSweekFromDateAndTime(const QDateTime& dateTime,
66 int& GPSWeek, double& GPSWeeks) {
67
68 static const QDateTime zeroEpoch(QDate(1980, 1, 6),QTime(),Qt::UTC);
69
70 GPSWeek = zeroEpoch.daysTo(dateTime) / 7;
71
72 int weekDay = dateTime.date().dayOfWeek() + 1; // Qt: Monday = 1
73 if (weekDay > 7) weekDay = 1;
74
75 GPSWeeks = (weekDay - 1) * 86400.0
76 - dateTime.time().msecsTo(QTime()) / 1e3;
77}
78
79//
80////////////////////////////////////////////////////////////////////////////
81void currentGPSWeeks(int& week, double& sec) {
82
83 QDateTime currDateTime = QDateTime::currentDateTime().toUTC();
84 QDate currDate = currDateTime.date();
85 QTime currTime = currDateTime.time();
86
87 week = int( (double(currDate.toJulianDay()) - 2444244.5) / 7 );
88
89 sec = (currDate.dayOfWeek() % 7) * 24.0 * 3600.0 +
90 currTime.hour() * 3600.0 +
91 currTime.minute() * 60.0 +
92 currTime.second() +
93 currTime.msec() / 1000.0;
94}
95
96//
97////////////////////////////////////////////////////////////////////////////
98void mjdFromDateAndTime(const QDateTime& dateTime, int& mjd, double& dayfrac) {
99
100 const static QDate zeroDate(1858, 11, 17);
101
102 mjd = zeroDate.daysTo(dateTime.date());
103
104 dayfrac = (dateTime.time().hour() +
105 (dateTime.time().minute() +
106 (dateTime.time().second() +
107 dateTime.time().msec() / 1000.0) / 60.0) / 60.0) / 24.0;
108}
109
110// Transformation xyz --> radial, along track, out-of-plane
111////////////////////////////////////////////////////////////////////////////
112void XYZ_to_RSW(const ColumnVector& rr, const ColumnVector& vv,
113 const ColumnVector& xyz, ColumnVector& rsw) {
114
115 ColumnVector cross = crossproduct(rr, vv);
116
117 rsw.ReSize(3);
118 rsw(1) = DotProduct(xyz, rr) / rr.norm_Frobenius();
119 rsw(2) = DotProduct(xyz, vv) / vv.norm_Frobenius();
120 rsw(3) = DotProduct(xyz, cross) / cross.norm_Frobenius();
121}
122
123// Fourth order Runge-Kutta numerical integrator for ODEs
124////////////////////////////////////////////////////////////////////////////
125ColumnVector rungeKutta4(
126 double xi, // the initial x-value
127 const ColumnVector& yi, // vector of the initial y-values
128 double dx, // the step size for the integration
129 ColumnVector (*der)(double x, const ColumnVector& y)
130 // A pointer to a function that computes the
131 // derivative of a function at a point (x,y)
132 ) {
133
134 ColumnVector k1 = der(xi , yi ) * dx;
135 ColumnVector k2 = der(xi+dx/2.0, yi+k1/2.0) * dx;
136 ColumnVector k3 = der(xi+dx/2.0, yi+k2/2.0) * dx;
137 ColumnVector k4 = der(xi+dx , yi+k3 ) * dx;
138
139 ColumnVector yf = yi + k1/6.0 + k2/3.0 + k3/3.0 + k4/6.0;
140
141 return yf;
142}
143
144//
145////////////////////////////////////////////////////////////////////////////
146QByteArray waitForLine(QTcpSocket* socket) {
147
148 QByteArray line;
149
150 while (true) {
151 char ch;
152 if (socket->getChar(&ch)) {
153 line += ch;
154 if (ch == '\n') {
155 break;
156 }
157 }
158 else {
159 socket->waitForReadyRead(10);
160 }
161 }
162 return line;
163}
164
Note: See TracBrowser for help on using the repository browser.