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

Last change on this file since 3035 was 2558, checked in by mervart, 14 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#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 double* acc, // aditional acceleration
131 ColumnVector (*der)(double x, const ColumnVector& y, double* acc)
132 // A pointer to a function that computes the
133 // derivative of a function at a point (x,y)
134 ) {
135
136 ColumnVector k1 = der(xi , yi , acc) * dx;
137 ColumnVector k2 = der(xi+dx/2.0, yi+k1/2.0, acc) * dx;
138 ColumnVector k3 = der(xi+dx/2.0, yi+k2/2.0, acc) * dx;
139 ColumnVector k4 = der(xi+dx , yi+k3 , acc) * dx;
140
141 ColumnVector yf = yi + k1/6.0 + k2/3.0 + k3/3.0 + k4/6.0;
142
143 return yf;
144}
145
146//
147////////////////////////////////////////////////////////////////////////////
148QByteArray waitForLine(QTcpSocket* socket) {
149
150 QByteArray line;
151
152 while (true) {
153 char ch;
154 if (socket->getChar(&ch)) {
155 line += ch;
156 if (ch == '\n') {
157 break;
158 }
159 }
160 else {
161 if (socket->state() != QAbstractSocket::ConnectedState) {
162 return "";
163 }
164 socket->waitForReadyRead(10);
165 }
166 }
167 return line;
168}
169
170//
171////////////////////////////////////////////////////////////////////////////
172double djul(int jj, int mm, double tt) {
173 int ii, kk;
174 double djul ;
175
176 if( mm <= 2 ) {
177 jj = jj - 1;
178 mm = mm + 12;
179 }
180
181 ii = jj/100;
182 kk = 2 - ii + ii/4;
183 djul = (365.25*jj - fmod( 365.25*jj, 1.0 )) - 679006.0;
184 djul = djul + floor( 30.6001*(mm + 1) ) + tt + kk;
185 return djul;
186}
187
188void jdgp(double tjul, double & second, int & nweek) {
189 double deltat;
190
191 deltat = tjul - 44244.0 ;
192
193 // current gps week
194
195 nweek = (int) floor(deltat/7.0);
196
197 // seconds past midnight of last weekend
198
199 second = (deltat - (nweek)*7.0)*86400.0;
200
201}
202
203void GPSweekFromYMDhms(int year, int month, int day, int hour, int min,
204 double sec, int& GPSWeek, double& GPSWeeks) {
205
206 double mjd = djul(year, month, day);
207
208 jdgp(mjd, GPSWeeks, GPSWeek);
209 GPSWeeks += hour * 3600.0 + min * 60.0 + sec;
210}
211
Note: See TracBrowser for help on using the repository browser.