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

Last change on this file since 2382 was 1687, checked in by mervart, 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 along = vv / vv.norm_Frobenius();
115 ColumnVector cross = crossproduct(rr, vv); cross /= cross.norm_Frobenius();
116 ColumnVector radial = crossproduct(along, cross);
117
118 rsw.ReSize(3);
119 rsw(1) = DotProduct(xyz, radial);
120 rsw(2) = DotProduct(xyz, along);
121 rsw(3) = DotProduct(xyz, cross);
122}
123
124// Fourth order Runge-Kutta numerical integrator for ODEs
125////////////////////////////////////////////////////////////////////////////
126ColumnVector rungeKutta4(
127 double xi, // the initial x-value
128 const ColumnVector& yi, // vector of the initial y-values
129 double dx, // the step size for the integration
130 ColumnVector (*der)(double x, const ColumnVector& y)
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 ) * dx;
136 ColumnVector k2 = der(xi+dx/2.0, yi+k1/2.0) * dx;
137 ColumnVector k3 = der(xi+dx/2.0, yi+k2/2.0) * dx;
138 ColumnVector k4 = der(xi+dx , yi+k3 ) * 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.