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

Last change on this file since 1336 was 1213, checked in by weber, 16 years ago

* empty log message *

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