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

Last change on this file since 3039 was 3039, checked in by mervart, 13 years ago
File size: 5.7 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 <ctime>
18#include <math.h>
19
20#include <QRegExp>
21#include <QStringList>
22#include <QDateTime>
23
24#include "bnsutils.h"
25
26using namespace std;
27
28//
29////////////////////////////////////////////////////////////////////////////
30void expandEnvVar(QString& str) {
31
32 QRegExp rx("(\\$\\{.+\\})");
33
34 if (rx.indexIn(str) != -1) {
35 QStringListIterator it(rx.capturedTexts());
36 if (it.hasNext()) {
37 QString rxStr = it.next();
38 QString envVar = rxStr.mid(2,rxStr.length()-3);
39 str.replace(rxStr, qgetenv(envVar.toAscii()));
40 }
41 }
42
43}
44
45//
46////////////////////////////////////////////////////////////////////////////
47QDateTime dateAndTimeFromGPSweek(int GPSWeek, double GPSWeeks) {
48
49 static const QDate zeroEpoch(1980, 1, 6);
50
51 QDate date(zeroEpoch);
52 QTime time(0,0,0,0);
53
54 int weekDays = int(GPSWeeks) / 86400;
55 date = date.addDays( GPSWeek * 7 + weekDays );
56 time = time.addMSecs( int( (GPSWeeks - 86400 * weekDays) * 1e3 ) );
57
58 return QDateTime(date,time,Qt::UTC);
59}
60
61//
62////////////////////////////////////////////////////////////////////////////
63void GPSweekFromDateAndTime(const QDateTime& dateTime,
64 int& GPSWeek, double& GPSWeeks) {
65
66 static const QDateTime zeroEpoch(QDate(1980, 1, 6),QTime(),Qt::UTC);
67
68 GPSWeek = zeroEpoch.daysTo(dateTime) / 7;
69
70 int weekDay = dateTime.date().dayOfWeek() + 1; // Qt: Monday = 1
71 if (weekDay > 7) weekDay = 1;
72
73 GPSWeeks = (weekDay - 1) * 86400.0
74 - dateTime.time().msecsTo(QTime()) / 1e3;
75}
76
77//
78////////////////////////////////////////////////////////////////////////////
79void currentGPSWeeks(int& week, double& sec) {
80
81 QDateTime currDateTime = QDateTime::currentDateTime().toUTC();
82 QDate currDate = currDateTime.date();
83 QTime currTime = currDateTime.time();
84
85 week = int( (double(currDate.toJulianDay()) - 2444244.5) / 7 );
86
87 sec = (currDate.dayOfWeek() % 7) * 24.0 * 3600.0 +
88 currTime.hour() * 3600.0 +
89 currTime.minute() * 60.0 +
90 currTime.second() +
91 currTime.msec() / 1000.0;
92}
93
94//
95////////////////////////////////////////////////////////////////////////////
96void mjdFromDateAndTime(const QDateTime& dateTime, int& mjd, double& dayfrac) {
97
98 static const QDate zeroDate(1858, 11, 17);
99
100 mjd = zeroDate.daysTo(dateTime.date());
101
102 dayfrac = (dateTime.time().hour() +
103 (dateTime.time().minute() +
104 (dateTime.time().second() +
105 dateTime.time().msec() / 1000.0) / 60.0) / 60.0) / 24.0;
106}
107
108// Transformation xyz --> radial, along track, out-of-plane
109////////////////////////////////////////////////////////////////////////////
110void XYZ_to_RSW(const ColumnVector& rr, const ColumnVector& vv,
111 const ColumnVector& xyz, ColumnVector& rsw) {
112
113 ColumnVector along = vv / vv.norm_Frobenius();
114 ColumnVector cross = crossproduct(rr, vv); cross /= cross.norm_Frobenius();
115 ColumnVector radial = crossproduct(along, cross);
116
117 rsw.ReSize(3);
118 rsw(1) = DotProduct(xyz, radial);
119 rsw(2) = DotProduct(xyz, along);
120 rsw(3) = DotProduct(xyz, cross);
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 double* acc, // aditional acceleration
130 ColumnVector (*der)(double x, const ColumnVector& y, double* acc)
131 // A pointer to a function that computes the
132 // derivative of a function at a point (x,y)
133 ) {
134
135 ColumnVector k1 = der(xi , yi , acc) * dx;
136 ColumnVector k2 = der(xi+dx/2.0, yi+k1/2.0, acc) * dx;
137 ColumnVector k3 = der(xi+dx/2.0, yi+k2/2.0, acc) * dx;
138 ColumnVector k4 = der(xi+dx , yi+k3 , acc) * dx;
139
140 ColumnVector yf = yi + k1/6.0 + k2/3.0 + k3/3.0 + k4/6.0;
141
142 return yf;
143}
144
145//
146////////////////////////////////////////////////////////////////////////////
147QByteArray waitForLine(QTcpSocket* socket) {
148
149 QByteArray line;
150
151 while (true) {
152 char ch;
153 if (socket->getChar(&ch)) {
154 line += ch;
155 if (ch == '\n') {
156 break;
157 }
158 }
159 else {
160 if (socket->state() != QAbstractSocket::ConnectedState) {
161 return "";
162 }
163 socket->waitForReadyRead(10);
164 }
165 }
166 return line;
167}
168
169//
170////////////////////////////////////////////////////////////////////////////
171double djul(int jj, int mm, double tt) {
172 int ii, kk;
173 double djul ;
174
175 if( mm <= 2 ) {
176 jj = jj - 1;
177 mm = mm + 12;
178 }
179
180 ii = jj/100;
181 kk = 2 - ii + ii/4;
182 djul = (365.25*jj - fmod( 365.25*jj, 1.0 )) - 679006.0;
183 djul = djul + floor( 30.6001*(mm + 1) ) + tt + kk;
184 return djul;
185}
186
187void jdgp(double tjul, double & second, int & nweek) {
188 double deltat;
189
190 deltat = tjul - 44244.0 ;
191
192 // current gps week
193
194 nweek = (int) floor(deltat/7.0);
195
196 // seconds past midnight of last weekend
197
198 second = (deltat - (nweek)*7.0)*86400.0;
199
200}
201
202void GPSweekFromYMDhms(int year, int month, int day, int hour, int min,
203 double sec, int& GPSWeek, double& GPSWeeks) {
204
205 double mjd = djul(year, month, day);
206
207 jdgp(mjd, GPSWeeks, GPSWeek);
208 GPSWeeks += hour * 3600.0 + min * 60.0 + sec;
209}
210
Note: See TracBrowser for help on using the repository browser.