source: ntrip/trunk/BNC/src/bncantex.cpp@ 10951

Last change on this file since 10951 was 10951, checked in by stuerze, 7 weeks ago

minor changes to investigate a special GLO behavior

File size: 20.2 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: bncAntex
30 *
31 * Purpose: Antenna Phase Centers and Variations from ANTEX File
32 *
33 * Author: L. Mervart
34 *
35 * Created: 26-Jan-2011
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42#include <cmath>
43#include <newmatio.h>
44
45#include "bncantex.h"
46#include "pppModel.h"
47
48using namespace std;
49
50// Constructor
51////////////////////////////////////////////////////////////////////////////
52bncAntex::bncAntex() {
53}
54
55// Constructor
56////////////////////////////////////////////////////////////////////////////
57bncAntex::bncAntex(const char* fileName) {
58 readFile(QString(fileName));//print();
59}
60
61// Destructor
62////////////////////////////////////////////////////////////////////////////
63bncAntex::~bncAntex() {
64 QMapIterator<QString, t_antMap*> it(_maps);
65 while (it.hasNext()) {
66 it.next();
67 delete it.value();
68 }
69 _maps.clear();
70}
71
72// Print
73////////////////////////////////////////////////////////////////////////////
74void bncAntex::print() const {
75 QMapIterator<QString, t_antMap*> itAnt(_maps);
76 while (itAnt.hasNext()) {
77 itAnt.next();
78 t_antMap* map = itAnt.value();
79 cout << map->antName.toLatin1().data() << endl;
80 cout << " " << map->zen1 << " " << map->zen2 << " " << map->dZen << endl;
81 QMapIterator<t_frequency::type, t_frqMap*> itFrq(map->frqMap);
82 while (itFrq.hasNext()) {
83 itFrq.next();
84 const t_frqMap* frqMap = itFrq.value();
85 cout << t_frequency::toString(itFrq.key()) << ":\n"
86 << frqMap->neu[0] << " "
87 << frqMap->neu[1] << " "
88 << frqMap->neu[2] << endl;
89 cout << frqMap->pattern.t();
90 }
91 cout << endl;
92 }
93}
94
95// Print
96////////////////////////////////////////////////////////////////////////////
97QString bncAntex::pcoSinexString(const std::string& antName, t_frequency::type frqType) {
98
99 if (antName.find("NULLANTENNA") != string::npos) {
100 return QString(" ------ ------ ------");
101 }
102
103 QString antNameQ = antName.c_str();
104 if (_maps.find(antNameQ) == _maps.end()) {
105 return QString(" ------ ------ ------");
106 }
107
108 t_antMap* map = _maps[antNameQ];
109 if (map->frqMap.find(frqType) == map->frqMap.end()) {
110 return QString(" ------ ------ ------");
111 }
112
113 t_frqMap* frqMap = map->frqMap[frqType];
114
115 QString u = QString().asprintf("%+6.4f" ,frqMap->neu[2]); if (u.mid(1,1) == "0") {u.remove(1,1);}
116 QString n = QString().asprintf("%+6.4f" ,frqMap->neu[0]); if (n.mid(1,1) == "0") {n.remove(1,1);}
117 QString e = QString().asprintf("%+6.4f" ,frqMap->neu[1]); if (e.mid(1,1) == "0") {e.remove(1,1);}
118
119 return QString(" %1 %2 %3").arg(u).arg(n).arg(e);
120}
121
122// Print
123////////////////////////////////////////////////////////////////////////////
124QString bncAntex::snxCodeSinexString(const std::string& antName) {
125
126 if (antName.find("NULLANTENNA") != string::npos) {
127 return QString(" ----------");
128 }
129
130 QString antNameQ = antName.c_str();
131 if (_maps.find(antNameQ) == _maps.end()) {
132 return QString(" ----------");
133 }
134 else {
135 return QString(" %1").arg(_maps[antNameQ]->snxCode, 10, QLatin1Char(' '));
136 }
137}
138
139// Read ANTEX File
140////////////////////////////////////////////////////////////////////////////
141t_irc bncAntex::readFile(const QString& fileName) {
142
143 QFile inFile(fileName);
144 inFile.open(QIODevice::ReadOnly | QIODevice::Text);
145
146 QTextStream in(&inFile);
147
148 t_antMap* newAntMap = 0;
149 t_frqMap* newFrqMap = 0;
150
151 while ( !in.atEnd() ) {
152 QString line = in.readLine();
153
154 // Start of Antenna
155 // ----------------
156 if (line.indexOf("START OF ANTENNA") == 60) {
157 if (newAntMap) {
158 delete newAntMap;
159 return failure;
160 }
161 else {
162 delete newAntMap;
163 newAntMap = new t_antMap();
164 }
165 }
166
167 // End of Antenna
168 // --------------
169 else if (line.indexOf("END OF ANTENNA") == 60) {
170 if (newAntMap) {
171 if (_maps.contains(newAntMap->antName)) {
172 delete _maps[newAntMap->antName];
173 }
174 _maps[newAntMap->antName] = newAntMap;
175 newAntMap = 0;
176 }
177 else {
178 delete newAntMap;
179 return failure;
180 }
181 }
182
183 // Antenna Reading in Progress
184 // ---------------------------
185 else if (newAntMap) {
186 if (line.indexOf("TYPE / SERIAL NO") == 60) {
187 if (line.indexOf("BLOCK I") == 0 ||
188 line.indexOf("GLONASS") == 0 ||
189 line.indexOf("QZSS") == 0 ||
190 line.indexOf("BEIDOU") == 0 ||
191 line.indexOf("GALILEO") == 0 ||
192 line.indexOf("NavIC") == 0 ){
193 newAntMap->antName = line.mid(20,3);
194 }
195 else {
196 newAntMap->antName = line.mid(0,20);
197 }
198 }
199 else if (line.indexOf("ZEN1 / ZEN2 / DZEN") == 60) {
200 QTextStream inLine(&line, QIODevice::ReadOnly);
201 inLine >> newAntMap->zen1 >> newAntMap->zen2 >> newAntMap->dZen;
202 }
203 else if (line.indexOf("SINEX CODE") == 60) {
204 QTextStream inLine(&line, QIODevice::ReadOnly);
205 inLine >> newAntMap->snxCode;
206 }
207
208 // Start of Frequency
209 // ------------------
210 else if (line.indexOf("START OF FREQUENCY") == 60) {
211 if (newFrqMap) {
212 delete newFrqMap;
213 delete newAntMap;
214 return failure;
215 }
216 else {
217 newFrqMap = new t_frqMap();
218 }
219 }
220
221 // End of Frequency
222 // ----------------
223 else if (line.indexOf("END OF FREQUENCY") == 60) {
224 if (newFrqMap) {
225 t_frequency::type frqType = t_frequency::dummy;
226 // GPS
227 if (line.indexOf("G01") == 3) {
228 frqType = t_frequency::G1;
229 }
230 else if (line.indexOf("G02") == 3) {
231 frqType = t_frequency::G2;
232 }
233 else if (line.indexOf("G05") == 3) {
234 frqType = t_frequency::G5;
235 }
236 // GLONASS
237 else if (line.indexOf("R01") == 3) {
238 frqType = t_frequency::R1;
239 }
240 else if (line.indexOf("R02") == 3) {
241 frqType = t_frequency::R2;
242 }
243 // Galileo
244 else if (line.indexOf("E01") == 3) {
245 frqType = t_frequency::E1;
246 }
247 else if (line.indexOf("E05") == 3) {
248 frqType = t_frequency::E5;
249 }
250 else if (line.indexOf("E06") == 3) {
251 frqType = t_frequency::E6;
252 }
253 else if (line.indexOf("E07") == 3) {
254 frqType = t_frequency::E7;
255 }
256 else if (line.indexOf("E08") == 3) {
257 frqType = t_frequency::E8;
258 }
259 // QZSS
260 else if (line.indexOf("J01") == 3) {
261 frqType = t_frequency::J1;
262 }
263 else if (line.indexOf("J02") == 3) {
264 frqType = t_frequency::J2;
265 }
266 else if (line.indexOf("J05") == 3) {
267 frqType = t_frequency::J5;
268 }
269 else if (line.indexOf("J06") == 3) {
270 frqType = t_frequency::J6;
271 }
272 // BDS
273 else if (line.indexOf("C01") == 3) {
274 frqType = t_frequency::C1;
275 }
276 else if (line.indexOf("C02") == 3) {
277 frqType = t_frequency::C2;
278 }
279 else if (line.indexOf("C06") == 3) {
280 frqType = t_frequency::C6;
281 }
282 else if (line.indexOf("C07") == 3) {
283 frqType = t_frequency::C7;
284 }
285 if (frqType != t_frequency::dummy) {
286 if (newAntMap->frqMap.find(frqType) != newAntMap->frqMap.end()) {
287 delete newAntMap->frqMap[frqType];
288 }
289 newAntMap->frqMap[frqType] = newFrqMap;
290 }
291 else {
292 delete newFrqMap;
293 }
294 newFrqMap = 0;
295 }
296 else {
297 delete newAntMap;
298 return failure;
299 }
300 }
301
302 // Frequency Reading in Progress
303 // -----------------------------
304 else if (newFrqMap) {
305 if (line.indexOf("NORTH / EAST / UP") == 60) {
306 QTextStream inLine(&line, QIODevice::ReadOnly);
307 inLine >> newFrqMap->neu[0] >> newFrqMap->neu[1] >> newFrqMap->neu[2];
308 newFrqMap->neu[0] *= 1e-3;
309 newFrqMap->neu[1] *= 1e-3;
310 newFrqMap->neu[2] *= 1e-3;
311 }
312 else if (line.indexOf("NOAZI") == 3) {
313 QTextStream inLine(&line, QIODevice::ReadOnly);
314 int nPat = int((newAntMap->zen2-newAntMap->zen1)/newAntMap->dZen) + 1;
315 newFrqMap->pattern.ReSize(nPat);
316 QString dummy;
317 inLine >> dummy;
318 for (int ii = 0; ii < nPat; ii++) {
319 inLine >> newFrqMap->pattern[ii];
320 }
321 newFrqMap->pattern *= 1e-3;
322 }
323 }
324 }
325 }
326 inFile.close();
327 delete newFrqMap;
328 delete newAntMap;
329
330 return success;
331}
332
333// GLONASS Yaw Angle (Sun-pointing law overridden near noon/midnight when
334// the satellite cannot mechanically keep up, following the GLONASS-M
335// "yaw-fixed" behaviour described in Dilssner, Springer, Flohrer, Dow
336// (2011), "The GLONASS-M satellite yaw-attitude model", Advances in Space
337// Research 47(1), 160-171.
338//
339// Unlike GPS/Galileo/BeiDou, which are commonly approximated by the
340// nominal Sun-pointing yaw-steering law of Bar-Sever (1996) at all times,
341// GLONASS-M has been found to stop tracking that law and hold a constant
342// (frozen) yaw angle whenever the required yaw rate would exceed the
343// satellite's slew capability - which happens close to the orbit
344// noon/midnight points whenever the Sun's elevation above the orbital
345// plane (the "beta" angle) is small. The maximum yaw rate used below
346// (0.25 deg/s) and the general approach follow that paper; satellite
347// telemetry was not available to validate the exact rate against this
348// installation's GLONASS satellites, so it should be checked against
349// independently-known attitude/orbit residuals if high accuracy matters.
350////////////////////////////////////////////////////////////////////////////
351double bncAntex::glonassYawAngle(const QString& prn, double Mjd,
352 const ColumnVector& xSat,
353 const ColumnVector& vSat,
354 const ColumnVector& xSun) {
355
356 const double MAX_YAW_RATE = 0.25 * M_PI / 180.0; // [rad/s], approximate
357
358 // This bounds the gap BETWEEN CONSECUTIVE CALLS for a given satellite
359 // (not how long the freeze itself has lasted - a real low-beta passage
360 // can legitimately stay frozen far longer than this). If there was no
361 // call for longer than this, something else interrupted normal epoch-by-
362 // epoch processing (a stream outage, a maneuver-flagged-unhealthy
363 // period, ...), so the stored frozen value is more likely stale than a
364 // still-valid freeze, and we fall back to the current nominal value
365 // instead of trusting it indefinitely.
366 const double MAX_CALL_GAP = 1800.0 / 86400.0; // 30 minutes, in days
367
368 // Inertial-consistent velocity (xSat, vSat are Earth-fixed; remove the
369 // Earth-rotation contribution so that the orbit normal below is not
370 // contaminated by it)
371 // -------------------------------------------------------------------
372 ColumnVector Omega(3); Omega(1) = 0.0; Omega(2) = 0.0; Omega(3) = t_CST::omega;
373 ColumnVector vInert = vSat + crossproduct(Omega, xSat);
374
375 // Orbit normal and instantaneous orbital rate from r x v (exact, valid
376 // for any Keplerian orbit, not just circular ones)
377 // ---------------------------------------------------------------------
378 ColumnVector h = crossproduct(xSat, vInert);
379 double hNorm = sqrt(DotProduct(h, h));
380 ColumnVector orbNormal = h / hNorm;
381 double r = sqrt(DotProduct(xSat, xSat));
382 double nRate = hNorm / (r * r); // [rad/s]
383
384 // Beta angle (Sun elevation above the orbital plane)
385 // ----------------------------------------------------
386 double beta = asin(DotProduct(orbNormal, xSun));
387
388 // Orbit angle mu, measured from the orbit midnight point, increasing in
389 // the direction of satellite motion
390 // -----------------------------------------------------------------------
391 ColumnVector sunProj = xSun - DotProduct(xSun, orbNormal) * orbNormal;
392 sunProj /= sqrt(DotProduct(sunProj, sunProj));
393 ColumnVector eX = -1.0 * sunProj; // midnight direction, mu = 0
394 ColumnVector eY = crossproduct(orbNormal, eX);
395 ColumnVector rHat = xSat / r;
396 double mu = atan2(DotProduct(rHat, eY), DotProduct(rHat, eX));
397
398 // Nominal Sun-pointing yaw angle and its rate (Bar-Sever 1996)
399 // ----------------------------------------------------------------
400 double tanBeta = tan(beta);
401 double sinMu = sin(mu);
402 double psiNom = atan2(-tanBeta, sinMu);
403 double denom = tanBeta * tanBeta + sinMu * sinMu;
404 double psiRate = (denom > 1e-12)
405 ? nRate * fabs(tanBeta * cos(mu)) / denom
406 : 1e9;
407
408 t_glonassYaw& st = _glonassYaw[prn];
409 double callGap = st.valid ? (Mjd - st.lastCallMjd) : 0.0;
410 double psiEff;
411 if (psiRate > MAX_YAW_RATE) {
412 if (!st.valid || callGap > MAX_CALL_GAP) {
413 if (st.valid) { // i.e. it was a genuine gap, not cold-start
414 _glonassYawLog += QString().asprintf(
415 "%s glonassYaw STALE-RESET Mjd=%.6f beta=%6.3f mu=%7.2f rate=%6.3f"
416 " old=%7.2f new=%7.2f gap=%.1fmin\n",
417 prn.toLatin1().data(), Mjd, beta*180.0/M_PI, mu*180.0/M_PI,
418 psiRate*180.0/M_PI, st.yaw*180.0/M_PI, psiNom*180.0/M_PI,
419 callGap*1440.0);
420 }
421 st.yaw = psiNom; // no prior history, or the gap is too long to trust it
422 }
423 if (!st.fixed) {
424 _glonassYawLog += QString().asprintf(
425 "%s glonassYaw FIXED-ENTER Mjd=%.6f beta=%6.3f mu=%7.2f rate=%6.3f yaw=%7.2f\n",
426 prn.toLatin1().data(), Mjd, beta*180.0/M_PI, mu*180.0/M_PI,
427 psiRate*180.0/M_PI, st.yaw*180.0/M_PI);
428 st.fixed = true;
429 }
430 psiEff = st.yaw; // hold the frozen yaw angle
431 }
432 else {
433 if (st.fixed) {
434 _glonassYawLog += QString().asprintf(
435 "%s glonassYaw FIXED-EXIT Mjd=%.6f beta=%6.3f mu=%7.2f rate=%6.3f"
436 " frozen=%7.2f nominal=%7.2f\n",
437 prn.toLatin1().data(), Mjd, beta*180.0/M_PI, mu*180.0/M_PI,
438 psiRate*180.0/M_PI, st.yaw*180.0/M_PI, psiNom*180.0/M_PI);
439 st.fixed = false;
440 }
441 st.yaw = psiNom;
442 psiEff = psiNom;
443 }
444 st.lastCallMjd = Mjd; // update on every call, fixed or not, to detect gaps
445 st.valid = true;
446
447 return psiEff;
448}
449
450// Satellite Antenna Offset
451////////////////////////////////////////////////////////////////////////////
452t_irc bncAntex::satCoMcorrection(const QString& prn, double Mjd,
453 const ColumnVector& xSat,
454 const ColumnVector& vSat, ColumnVector& dx) {
455
456 t_frequency::type frqType = t_frequency::dummy;
457
458 if (prn[0] == 'G') {
459 frqType = t_frequency::G1;
460 }
461 else if (prn[0] == 'R') {
462 frqType = t_frequency::R1;
463 }
464 else if (prn[0] == 'E') {
465 frqType = t_frequency::E1;
466 }
467 else if (prn[0] == 'C') {
468 frqType = t_frequency::C2;
469 }
470 else if (prn[0] == 'S') {
471 frqType = t_frequency::S1;
472 }
473 else if (prn[0] == 'J') {
474 frqType = t_frequency::J1;
475 }
476 else if (prn[0] == 'I') {
477 frqType = t_frequency::I5;
478 }
479
480 QMap<QString, t_antMap*>::const_iterator it = _maps.find(prn.mid(0,3));
481 if (it != _maps.end()) {
482 t_antMap* map = it.value();
483 if (map->frqMap.find(frqType) != map->frqMap.end()) {
484
485 double* neu = map->frqMap[frqType]->neu;
486
487 // Unit Vectors sz, sy, sx
488 // -----------------------
489 ColumnVector sz = -xSat;
490 sz /= sqrt(DotProduct(sz,sz));
491
492 ColumnVector xSun = BNC_PPP::t_astro::Sun(Mjd);
493 xSun /= sqrt(DotProduct(xSun,xSun));
494
495 ColumnVector sy, sx;
496
497 // GLONASS: override the nominal Sun-pointing attitude near the
498 // orbit noon/midnight points when the beta angle is small (see
499 // glonassYawAngle() above). Elsewhere GLONASS-M follows the same
500 // nominal law as the other constellations, so the result is
501 // identical to the direct Sun-pointing computation used below.
502 // -----------------------------------------------------------------
503 if (prn[0] == 'R' && vSat.size() == 3) {
504 double psi = glonassYawAngle(prn, Mjd, xSat, vSat, xSun);
505
506 ColumnVector vInert = vSat;
507 ColumnVector Omega(3); Omega(1) = 0.0; Omega(2) = 0.0; Omega(3) = t_CST::omega;
508 vInert += crossproduct(Omega, xSat);
509
510 ColumnVector sy0 = crossproduct(sz, vInert);
511 sy0 /= sqrt(DotProduct(sy0,sy0));
512 ColumnVector sx0 = crossproduct(sy0, sz);
513
514 // Rodrigues rotation of (sx0, sy0) around sz by angle psi
515 double cosY = cos(psi);
516 double sinY = sin(psi);
517 sx = sx0 * cosY + crossproduct(sz, sx0) * sinY;
518 sy = sy0 * cosY + crossproduct(sz, sy0) * sinY;
519 }
520 else {
521 sy = crossproduct(sz, xSun);
522 sy /= sqrt(DotProduct(sy,sy));
523 sx = crossproduct(sy, sz);
524 }
525
526 dx[0] = sx[0] * neu[0] + sy[0] * neu[1] + sz[0] * neu[2];
527 dx[1] = sx[1] * neu[0] + sy[1] * neu[1] + sz[1] * neu[2];
528 dx[2] = sx[2] * neu[0] + sy[2] * neu[1] + sz[2] * neu[2];
529
530 return success;
531 }
532 }
533
534 return failure;
535}
536
537//
538////////////////////////////////////////////////////////////////////////////
539double bncAntex::satCorr(const QString& prn, t_frequency::type frqType,
540 double elTx, double azTx, bool& found) const {
541
542 if (_maps.find(prn.mid(0,3)) == _maps.end()) {
543 found = false;
544 return 0.0;
545 };
546
547 t_antMap* map = _maps[prn.mid(0,3)];
548
549 if (map->frqMap.find(frqType) == map->frqMap.end()) {
550 found = false;
551 return 0.0;
552 };
553
554 t_frqMap* frqMap = map->frqMap[frqType];
555
556 double var = 0.0;
557 if (frqMap->pattern.ncols() > 0) {
558 double zenDiff = 999.999;
559 double zenTx = 90.0 - elTx * 180.0 / M_PI;
560 unsigned iZen = 0;
561 for (double zen = map->zen1; zen <= map->zen2; zen += map->dZen) {
562 iZen += 1;
563 double newZenDiff = fabs(zen - zenTx);
564 if (newZenDiff < zenDiff) {
565 zenDiff = newZenDiff;
566 var = frqMap->pattern(iZen);
567 }
568 }
569 }
570
571 found = true;
572 return var - frqMap->neu[0] * cos(azTx)*cos(elTx)
573 - frqMap->neu[1] * sin(azTx)*cos(elTx)
574 - frqMap->neu[2] * sin(elTx);
575
576}
577
578//
579////////////////////////////////////////////////////////////////////////////
580double bncAntex::rcvCorr(const string& antName, t_frequency::type frqType,
581 double eleSat, double azSat, bool& found) const {
582
583 if (antName.find("NULLANTENNA") != string::npos) {
584 found = true;
585 return 0.0;
586 }
587
588 QString antNameQ = antName.c_str();
589
590 if (_maps.find(antNameQ) == _maps.end()) {
591 found = false;
592 return 0.0;
593 }
594
595 t_antMap* map = _maps[antNameQ];
596 if (map->frqMap.find(frqType) == map->frqMap.end()) {
597 found = false;
598 return 0.0;
599 }
600
601 t_frqMap* frqMap = map->frqMap[frqType];
602
603 double var = 0.0;
604 if (frqMap->pattern.ncols() > 0) {
605 double zenDiff = 999.999;
606 double zenSat = 90.0 - eleSat * 180.0 / M_PI;
607 unsigned iZen = 0;
608 for (double zen = map->zen1; zen <= map->zen2; zen += map->dZen) {
609 iZen += 1;
610 double newZenDiff = fabs(zen - zenSat);
611 if (newZenDiff < zenDiff) {
612 zenDiff = newZenDiff;
613 var = frqMap->pattern(iZen);
614 }
615 }
616 }
617
618 found = true;
619 return var - frqMap->neu[0] * cos(azSat)*cos(eleSat)
620 - frqMap->neu[1] * sin(azSat)*cos(eleSat)
621 - frqMap->neu[2] * sin(eleSat);
622
623}
Note: See TracBrowser for help on using the repository browser.