-
Notifications
You must be signed in to change notification settings - Fork 4
/
rinex.c
1420 lines (1301 loc) · 50.5 KB
/
rinex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*------------------------------------------------------------------------------
* rinex.c : rinex functions
*-----------------------------------------------------------------------------*/
#include "gamp.h"
/* constants/macros ----------------------------------------------------------*/
#define SQR(x) ((x)*(x))
#define NUMSYS 6 /* number of systems */
#define MAXRNXLEN (16*MAXOBSTYPE+4) /* max rinex record length */
#define MAXPOSHEAD 1024 /* max head line position */
#define MINFREQ_GLO -7 /* min frequency number glonass */
#define MAXFREQ_GLO 13 /* max frequency number glonass */
#define NINCOBS 262144 /* inclimental number of obs data */
static const int navsys[]={ /* satellite systems */
SYS_GPS,SYS_GLO,SYS_GAL,SYS_QZS,SYS_SBS,SYS_CMP,0
};
static const char syscodes[]="GREJSC"; /* satellite system codes */
static const char obscodes[]="CLDS"; /* obs type codes */
static const char frqcodes[]="125678"; /* frequency codes */
static const double ura_eph[]={ /* ura values (ref [3] 20.3.3.3.1.1) */
2.4,3.4,4.85,6.85,9.65,13.65,24.0,48.0,96.0,192.0,384.0,768.0,1536.0,
3072.0,6144.0,0.0
};
/* type definition -----------------------------------------------------------*/
typedef struct { /* signal index type */
int n; /* number of index */
int frq[MAXOBSTYPE]; /* signal frequency (1:L1,2:L2,...) */
int pos[MAXOBSTYPE]; /* signal index in obs data (-1:no) */
unsigned char pri [MAXOBSTYPE]; /* signal priority (15-0) */
unsigned char type[MAXOBSTYPE]; /* type (0:C,1:L,2:D,3:S) */
unsigned char code[MAXOBSTYPE]; /* obs code (CODE_L??) */
double shift[MAXOBSTYPE]; /* phase shift (cycle) */
} sigind_t;
/* set string without tail space ---------------------------------------------*/
static void setstr(char *dst, const char *src, int n)
{
char *p=dst;
const char *q=src;
while (*q&&q<src+n) *p++=*q++;
*p--='\0';
while (p>=dst&&*p==' ') *p--='\0';
}
/* adjust time considering week handover -------------------------------------*/
static gtime_t adjweek(gtime_t t, gtime_t t0)
{
double tt=timediff(t,t0);
if (tt<-302400.0) return timeadd(t, 604800.0);
if (tt> 302400.0) return timeadd(t,-604800.0);
return t;
}
/* adjust time considering week handover -------------------------------------*/
static gtime_t adjday(gtime_t t, gtime_t t0)
{
double tt=timediff(t,t0);
if (tt<-43200.0) return timeadd(t, 86400.0);
if (tt> 43200.0) return timeadd(t,-86400.0);
return t;
}
/* ura value (m) to ura index ------------------------------------------------*/
static int uraindex(double value)
{
int i;
for (i=0;i<15;i++) if (ura_eph[i]>=value) break;
return i;
}
/* initialize station parameter ----------------------------------------------*/
static void init_sta(sta_t *sta)
{
int i;
*sta->name ='\0';
*sta->marker ='\0';
*sta->antdes ='\0';
*sta->antsno ='\0';
*sta->rectype='\0';
*sta->recver ='\0';
*sta->recsno ='\0';
sta->antsetup=sta->itrf=sta->deltype=0;
for (i=0;i<3;i++) sta->pos[i]=0.0;
for (i=0;i<3;i++) sta->del[i]=0.0;
sta->hgt=0.0;
}
/*------------------------------------------------------------------------------
* input rinex functions
*-----------------------------------------------------------------------------*/
/* convert rinex obs type ver.2 -> ver.3 -------------------------------------*/
static void convcode(double ver, int sys, const char *str, char *type)
{
strcpy(type," ");
if (!strcmp(str,"P1")) { /* ver.2.11 GPS L1PY,GLO L2P */
if (sys==SYS_GPS) sprintf(type,"%c1W",'C');
else if (sys==SYS_GLO) sprintf(type,"%c1P",'C');
}
else if (!strcmp(str,"P2")) { /* ver.2.11 GPS L2PY,GLO L2P */
if (sys==SYS_GPS) sprintf(type,"%c2W",'C');
else if (sys==SYS_GLO) sprintf(type,"%c2P",'C');
}
else if (!strcmp(str,"C1")) { /* ver.2.11 GPS L1C,GLO L1C/A */
if (ver>=2.12) ; /* reject C1 for 2.12 */
else if (sys==SYS_GPS) sprintf(type,"%c1C",'C');
else if (sys==SYS_GLO) sprintf(type,"%c1C",'C');
else if (sys==SYS_GAL) sprintf(type,"%c1X",'C'); /* ver.2.12 */
else if (sys==SYS_QZS) sprintf(type,"%c1C",'C');
else if (sys==SYS_SBS) sprintf(type,"%c1C",'C');
}
else if (!strcmp(str,"C2")) {
if (sys==SYS_GPS) {
if (ver>=2.12) sprintf(type,"%c2W",'C'); /* L2P(Y) */
else sprintf(type,"%c2X",'C'); /* L2C */
}
else if (sys==SYS_GLO) sprintf(type,"%c2C",'C');
else if (sys==SYS_QZS) sprintf(type,"%c2X",'C');
else if (sys==SYS_CMP) sprintf(type,"%c1X",'C'); /* ver.2.12 B1 */
}
else if (ver>=2.12&&str[1]=='A') { /* ver.2.12 L1C/A */
if (sys==SYS_GPS) sprintf(type,"%c1C",str[0]);
else if (sys==SYS_GLO) sprintf(type,"%c1C",str[0]);
else if (sys==SYS_QZS) sprintf(type,"%c1C",str[0]);
else if (sys==SYS_SBS) sprintf(type,"%c1C",str[0]);
}
else if (ver>=2.12&&str[1]=='B') { /* ver.2.12 GPS L1C */
if (sys==SYS_GPS) sprintf(type,"%c1X",str[0]);
else if (sys==SYS_QZS) sprintf(type,"%c1X",str[0]);
}
else if (ver>=2.12&&str[1]=='C') { /* ver.2.12 GPS L2C */
if (sys==SYS_GPS) sprintf(type,"%c2X",str[0]);
else if (sys==SYS_QZS) sprintf(type,"%c2X",str[0]);
}
else if (ver>=2.12&&str[1]=='D') { /* ver.2.12 GLO L2C/A */
if (sys==SYS_GLO) sprintf(type,"%c2C",str[0]);
}
else if (ver>=2.12&&str[1]=='1') { /* ver.2.12 GPS L1PY,GLO L1P */
if (sys==SYS_GPS) sprintf(type,"%c1W",str[0]);
else if (sys==SYS_GLO) sprintf(type,"%c1P",str[0]);
else if (sys==SYS_GAL) sprintf(type,"%c1X",str[0]); /* tentative */
else if (sys==SYS_CMP) sprintf(type,"%c1X",str[0]); /* extension */
}
else if (ver<2.12&&str[1]=='1') {
if (sys==SYS_GPS) sprintf(type,"%c1C",str[0]);
else if (sys==SYS_GLO) sprintf(type,"%c1C",str[0]);
else if (sys==SYS_GAL) sprintf(type,"%c1X",str[0]); /* tentative */
else if (sys==SYS_QZS) sprintf(type,"%c1C",str[0]);
else if (sys==SYS_SBS) sprintf(type,"%c1C",str[0]);
}
else if (str[1]=='2') {
if (sys==SYS_GPS) sprintf(type,"%c2W",str[0]);
else if (sys==SYS_GLO) sprintf(type,"%c2P",str[0]);
else if (sys==SYS_QZS) sprintf(type,"%c2X",str[0]);
else if (sys==SYS_CMP) sprintf(type,"%c1X",str[0]); /* ver.2.12 B1 */
}
else if (str[1]=='5') {
if (sys==SYS_GPS) sprintf(type,"%c5X",str[0]);
else if (sys==SYS_GAL) sprintf(type,"%c5X",str[0]);
else if (sys==SYS_QZS) sprintf(type,"%c5X",str[0]);
else if (sys==SYS_SBS) sprintf(type,"%c5X",str[0]);
}
else if (str[1]=='6') {
if (sys==SYS_GAL) sprintf(type,"%c6X",str[0]);
else if (sys==SYS_QZS) sprintf(type,"%c6X",str[0]);
else if (sys==SYS_CMP) sprintf(type,"%c6X",str[0]); /* ver.2.12 B3 */
}
else if (str[1]=='7') {
if (sys==SYS_GAL) sprintf(type,"%c7X",str[0]);
else if (sys==SYS_CMP) sprintf(type,"%c7X",str[0]); /* ver.2.12 B2 */
}
else if (str[1]=='8') {
if (sys==SYS_GAL) sprintf(type,"%c8X",str[0]);
}
}
/* decode obs header ---------------------------------------------------------*/
static void decode_obsh(FILE *fp, char *buff, double ver, int *tsys,
char tobs[][MAXOBSTYPE][4], nav_t *nav, sta_t *sta)
{
/* default codes for unknown code */
const char *defcodes[]={
"CWX ", /* GPS: L125___ */
"CC ", /* GLO: L12____ */
"X XXXX", /* GAL: L1_5678 */
"CXXX ", /* QZS: L1256__ */
"C X ", /* SBS: L1_5___ */
"X XX " /* BDS: L1__67_ */
};
double del[3];
int i,j,k,n,nt,prn,fcn;
const char *p;
char *label=buff+60,str[4];
if (strstr(label,"MARKER NAME" )) {
if (sta) setstr(sta->name,buff,60);
}
else if (strstr(label,"MARKER NUMBER" )) { /* opt */
if (sta) setstr(sta->marker,buff,20);
}
else if (strstr(label,"MARKER TYPE" )) ; /* ver.3 */
else if (strstr(label,"OBSERVER / AGENCY" )) ;
else if (strstr(label,"REC # / TYPE / VERS" )) {
if (sta) {
setstr(sta->recsno, buff, 20);
setstr(sta->rectype,buff+20,20);
setstr(sta->recver, buff+40,20);
}
}
else if (strstr(label,"ANT # / TYPE" )) {
if (sta) {
setstr(sta->antsno,buff ,20);
setstr(sta->antdes,buff+20,20);
}
}
else if (strstr(label,"APPROX POSITION XYZ" )) {
if (sta) {
for (i=0,j=0;i<3;i++,j+=14) sta->pos[i]=str2num(buff,j,14);
}
}
else if (strstr(label,"ANTENNA: DELTA H/E/N")) {
if (sta) {
for (i=0,j=0;i<3;i++,j+=14) del[i]=str2num(buff,j,14);
sta->del[2]=del[0]; /* h */
sta->del[0]=del[1]; /* e */
sta->del[1]=del[2]; /* n */
}
}
else if (strstr(label,"ANTENNA: DELTA X/Y/Z")) ; /* opt ver.3 */
else if (strstr(label,"ANTENNA: PHASECENTER")) ; /* opt ver.3 */
else if (strstr(label,"ANTENNA: B.SIGHT XYZ")) ; /* opt ver.3 */
else if (strstr(label,"ANTENNA: ZERODIR AZI")) ; /* opt ver.3 */
else if (strstr(label,"ANTENNA: ZERODIR XYZ")) ; /* opt ver.3 */
else if (strstr(label,"CENTER OF MASS: XYZ" )) ; /* opt ver.3 */
else if (strstr(label,"SYS / # / OBS TYPES" )) { /* ver.3 */
if (!(p=strchr(syscodes,buff[0]))) {
printf("invalid system code: sys=%c\n",buff[0]);
return;
}
i=(int)(p-syscodes);
n=(int)str2num(buff,3,3);
for (j=nt=0,k=7;j<n;j++,k+=4) {
if (k>58) {
if (!fgets(buff,MAXRNXLEN,fp)) break;
k=7;
}
if (nt<MAXOBSTYPE-1) setstr(tobs[i][nt++],buff+k,3);
}
*tobs[i][nt]='\0';
/* change beidou B1 code: 3.02 draft -> 3.02 */
if (i==5) {
for (j=0;j<nt;j++) if (tobs[i][j][1]=='2') tobs[i][j][1]='1';
}
/* if unknown code in ver.3, set default code */
for (j=0;j<nt;j++) {
if (tobs[i][j][2]) continue;
if (!(p=strchr(frqcodes,tobs[i][j][1]))) continue;
tobs[i][j][2]=defcodes[i][(int)(p-frqcodes)];
}
}
else if (strstr(label,"WAVELENGTH FACT L1/2")) ; /* opt ver.2 */
else if (strstr(label,"# / TYPES OF OBSERV" )) { /* ver.2 */
n=(int)str2num(buff,0,6);
for (i=nt=0,j=10;i<n;i++,j+=6) {
if (j>58) {
if (!fgets(buff,MAXRNXLEN,fp)) break;
j=10;
}
if (nt>=MAXOBSTYPE-1) continue;
if (ver<=2.99) {
setstr(str,buff+j,2);
convcode(ver,SYS_GPS,str,tobs[0][nt]);
convcode(ver,SYS_GLO,str,tobs[1][nt]);
convcode(ver,SYS_GAL,str,tobs[2][nt]);
convcode(ver,SYS_QZS,str,tobs[3][nt]);
convcode(ver,SYS_SBS,str,tobs[4][nt]);
convcode(ver,SYS_CMP,str,tobs[5][nt]);
}
nt++;
}
*tobs[0][nt]='\0';
}
else if (strstr(label,"SIGNAL STRENGTH UNIT")) ; /* opt ver.3 */
else if (strstr(label,"INTERVAL" )) ; /* opt */
else if (strstr(label,"TIME OF FIRST OBS" )) {
if (!strncmp(buff+48,"GPS",3)) *tsys=TSYS_GPS;
else if (!strncmp(buff+48,"GLO",3)) *tsys=TSYS_UTC;
else if (!strncmp(buff+48,"GAL",3)) *tsys=TSYS_GAL;
else if (!strncmp(buff+48,"QZS",3)) *tsys=TSYS_QZS; /* ver.3.02 */
else if (!strncmp(buff+48,"BDT",3)) *tsys=TSYS_CMP; /* ver.3.02 */
}
else if (strstr(label,"TIME OF LAST OBS" )) ; /* opt */
else if (strstr(label,"RCV CLOCK OFFS APPL" )) ; /* opt */
else if (strstr(label,"SYS / DCBS APPLIED" )) ; /* opt ver.3 */
else if (strstr(label,"SYS / PCVS APPLIED" )) ; /* opt ver.3 */
else if (strstr(label,"SYS / SCALE FACTOR" )) ; /* opt ver.3 */
else if (strstr(label,"SYS / PHASE SHIFTS" )) ; /* ver.3.01 */
else if (strstr(label,"GLONASS SLOT / FRQ #")) { /* ver.3.02 */
if (nav) {
for (i=0,p=buff+4;i<8;i++,p+=8) {
if (sscanf(p,"R%2d %2d",&prn,&fcn)<2) continue;
if (1<=prn&&prn<=MAXPRNGLO) nav->glo_fcn[prn-1]=fcn+8;
}
}
}
else if (strstr(label,"GLONASS COD/PHS/BIS" )) { /* ver.3.02 */
if (nav) {
for (i=0,p=buff;i<4;i++,p+=13) {
if (strncmp(p+1,"C1C",3)) nav->glo_cpbias[0]=str2num(p,5,8);
else if (strncmp(p+1,"C1P",3)) nav->glo_cpbias[1]=str2num(p,5,8);
else if (strncmp(p+1,"C2C",3)) nav->glo_cpbias[2]=str2num(p,5,8);
else if (strncmp(p+1,"C2P",3)) nav->glo_cpbias[3]=str2num(p,5,8);
}
}
}
else if (strstr(label,"LEAP SECONDS" )) { /* opt */
if (nav) nav->leaps=(int)str2num(buff,0,6);
}
else if (strstr(label,"# OF SALTELLITES" )) ; /* opt */
else if (strstr(label,"PRN / # OF OBS" )) ; /* opt */
}
/* decode nav header ---------------------------------------------------------*/
static void decode_navh(char *buff, nav_t *nav)
{
int i,j;
char *label=buff+60;
if (strstr(label,"ION ALPHA" )) { /* opt ver.2 */
if (nav) {
for (i=0,j=2;i<4;i++,j+=12) nav->ion_gps[i]=str2num(buff,j,12);
}
}
else if (strstr(label,"ION BETA" )) { /* opt ver.2 */
if (nav) {
for (i=0,j=2;i<4;i++,j+=12) nav->ion_gps[i+4]=str2num(buff,j,12);
}
}
else if (strstr(label,"DELTA-UTC: A0,A1,T,W")) { /* opt ver.2 */
if (nav) {
for (i=0,j=3;i<2;i++,j+=19) nav->utc_gps[i]=str2num(buff,j,19);
for (;i<4;i++,j+=9) nav->utc_gps[i]=str2num(buff,j,9);
}
}
else if (strstr(label,"IONOSPHERIC CORR" )) { /* opt ver.3 */
if (nav) {
if (!strncmp(buff,"GPSA",4)) {
for (i=0,j=5;i<4;i++,j+=12) nav->ion_gps[i]=str2num(buff,j,12);
}
else if (!strncmp(buff,"GPSB",4)) {
for (i=0,j=5;i<4;i++,j+=12) nav->ion_gps[i+4]=str2num(buff,j,12);
}
else if (!strncmp(buff,"GAL",3)) {
for (i=0,j=5;i<4;i++,j+=12) nav->ion_gal[i]=str2num(buff,j,12);
}
else if (!strncmp(buff,"QZSA",4)) { /* v.3.02 */
for (i=0,j=5;i<4;i++,j+=12) nav->ion_qzs[i]=str2num(buff,j,12);
}
else if (!strncmp(buff,"QZSB",4)) { /* v.3.02 */
for (i=0,j=5;i<4;i++,j+=12) nav->ion_qzs[i+4]=str2num(buff,j,12);
}
else if (!strncmp(buff,"BDSA",4)) { /* v.3.02 */
for (i=0,j=5;i<4;i++,j+=12) nav->ion_cmp[i]=str2num(buff,j,12);
}
else if (!strncmp(buff,"BDSB",4)) { /* v.3.02 */
for (i=0,j=5;i<4;i++,j+=12) nav->ion_cmp[i+4]=str2num(buff,j,12);
}
}
}
else if (strstr(label,"TIME SYSTEM CORR" )) { /* opt ver.3 */
if (nav) {
if (!strncmp(buff,"GPUT",4)) {
nav->utc_gps[0]=str2num(buff, 5,17);
nav->utc_gps[1]=str2num(buff,22,16);
nav->utc_gps[2]=str2num(buff,38, 7);
nav->utc_gps[3]=str2num(buff,45, 5);
}
else if (!strncmp(buff,"GLUT",4)) {
nav->utc_glo[0]=str2num(buff, 5,17);
nav->utc_glo[1]=str2num(buff,22,16);
}
else if (!strncmp(buff,"GAUT",4)) { /* v.3.02 */
nav->utc_gal[0]=str2num(buff, 5,17);
nav->utc_gal[1]=str2num(buff,22,16);
nav->utc_gal[2]=str2num(buff,38, 7);
nav->utc_gal[3]=str2num(buff,45, 5);
}
else if (!strncmp(buff,"QZUT",4)) { /* v.3.02 */
nav->utc_qzs[0]=str2num(buff, 5,17);
nav->utc_qzs[1]=str2num(buff,22,16);
nav->utc_qzs[2]=str2num(buff,38, 7);
nav->utc_qzs[3]=str2num(buff,45, 5);
}
else if (!strncmp(buff,"BDUT",4)) { /* v.3.02 */
nav->utc_cmp[0]=str2num(buff, 5,17);
nav->utc_cmp[1]=str2num(buff,22,16);
nav->utc_cmp[2]=str2num(buff,38, 7);
nav->utc_cmp[3]=str2num(buff,45, 5);
}
else if (!strncmp(buff,"SBUT",4)) { /* v.3.02 */
nav->utc_cmp[0]=str2num(buff, 5,17);
nav->utc_cmp[1]=str2num(buff,22,16);
nav->utc_cmp[2]=str2num(buff,38, 7);
nav->utc_cmp[3]=str2num(buff,45, 5);
}
}
}
else if (strstr(label,"LEAP SECONDS" )) { /* opt */
if (nav) nav->leaps=(int)str2num(buff,0,6);
}
}
/* decode gnav header --------------------------------------------------------*/
static void decode_gnavh(char *buff, nav_t *nav)
{
char *label=buff+60;
if (strstr(label,"CORR TO SYTEM TIME" )) ; /* opt */
else if (strstr(label,"LEAP SECONDS" )) { /* opt */
if (nav) nav->leaps=(int)str2num(buff,0,6);
}
}
/* read rinex header ---------------------------------------------------------*/
static int readrnxh(FILE *fp, double *ver, char *type, int *sys, int *tsys,
char tobs[][MAXOBSTYPE][4], nav_t *nav, sta_t *sta)
{
double bias;
char buff[MAXRNXLEN],*label=buff+60;
int i=0,block=0,sat;
*ver=2.10; *type=' '; *sys=SYS_GPS; *tsys=TSYS_GPS;
while (fgets(buff,MAXRNXLEN,fp)) {
if (strlen(buff)<=60) continue;
else if (strstr(label,"RINEX VERSION / TYPE")) {
*ver=str2num(buff,0,9);
*type=*(buff+20);
/* satellite system */
switch (*(buff+40)) {
case ' ':
case 'G': *sys=SYS_GPS; *tsys=TSYS_GPS; break;
case 'R': *sys=SYS_GLO; *tsys=TSYS_UTC; break;
case 'E': *sys=SYS_GAL; *tsys=TSYS_GAL; break; /* v.2.12 */
case 'J': *sys=SYS_QZS; *tsys=TSYS_QZS; break; /* v.3.02 */
case 'C': *sys=SYS_CMP; *tsys=TSYS_CMP; break; /* v.2.12 */
case 'M': *sys=SYS_NONE; *tsys=TSYS_GPS; break; /* mixed */
default :
printf("not supported satellite system: %c\n",*(buff+40));
break;
}
continue;
}
else if (strstr(label,"PGM / RUN BY / DATE")) continue;
else if (strstr(label,"COMMENT")) { /* opt */
/* read cnes wl satellite fractional bias */
if (strstr(buff,"WIDELANE SATELLITE FRACTIONAL BIASES")||
strstr(buff,"WIDELANE SATELLITE FRACTIONNAL BIASES")) {
block=1;
}
else if (block) {
/* cnes/cls grg clock */
if (!strncmp(buff,"WL",2)&&(sat=satid2no(buff+3))&&
sscanf(buff+40,"%lf",&bias)==1) {
nav->wlbias[sat-1]=bias;
}
/* cnes ppp-wizard clock */
else if ((sat=satid2no(buff+1))&&sscanf(buff+6,"%lf",&bias)==1) {
nav->wlbias[sat-1]=bias;
}
}
continue;
}
/* file type */
switch (*type) {
case 'O': decode_obsh(fp,buff,*ver,tsys,tobs,nav,sta); break;
case 'N': decode_navh (buff,nav); break;
case 'G': decode_gnavh(buff,nav); break;
case 'J': decode_navh (buff,nav); break; /* extension */
case 'L': decode_navh (buff,nav); break; /* extension */
}
if (strstr(label,"END OF HEADER")) return 1;
if (++i>=MAXPOSHEAD&&*type==' ') break; /* no rinex file */
}
return 0;
}
/* decode obs epoch ----------------------------------------------------------*/
static int decode_obsepoch(FILE *fp, char *buff, double ver, gtime_t *time,
int *flag, int *sats)
{
int i,j,n;
char satid[8]="";
if (ver<=2.99) { /* ver.2 */
if ((n=(int)str2num(buff,29,3))<=0) return 0;
/* epoch flag: 3:new site,4:header info,5:external event */
*flag=(int)str2num(buff,28,1);
if (3<=*flag&&*flag<=5) return n;
if (str2time(buff,0,26,time)) {
printf("rinex obs invalid epoch: epoch=%26.26s\n",buff);
return 0;
}
for (i=0,j=32;i<n;i++,j+=3) {
if (j>=68) {
if (!fgets(buff,MAXRNXLEN,fp)) break;
j=32;
}
if (i<MAXOBS) {
strncpy(satid,buff+j,3);
sats[i]=satid2no(satid);
}
}
}
else { /* ver.3 */
if ((n=(int)str2num(buff,32,3))<=0) return 0;
*flag=(int)str2num(buff,31,1);
if (3<=*flag&&*flag<=5) return n;
if (buff[0]!='>'||str2time(buff,1,28,time)) {
printf("rinex obs invalid epoch: epoch=%29.29s\n",buff);
return 0;
}
}
return n;
}
/* decode obs data -----------------------------------------------------------*/
static int decode_obsdata(FILE *fp, char *buff, double ver, int mask,
sigind_t *index, obsd_t *obs)
{
sigind_t *ind;
double val[MAXOBSTYPE]={0};
unsigned char lli[MAXOBSTYPE]={0};
char satid[8]="";
int i,j,n,m,stat=1,p[MAXOBSTYPE],k[16],l[16];
if (ver>2.99) { /* ver.3 */
strncpy(satid,buff,3);
//strncpy(obs->csat,buff,3);
obs->sat=(unsigned char)satid2no(satid);
}
if (!obs->sat) {
//printf("decode_obsdata: unsupported sat sat=%s\n",satid);
stat=0;
}
else if (!(satsys(obs->sat,NULL)&mask)) {
stat=0;
}
/* read obs data fields */
switch (satsys(obs->sat,NULL)) {
case SYS_GLO: ind=index+1; break;
case SYS_GAL: ind=index+2; break;
case SYS_QZS: ind=index+3; break;
case SYS_SBS: ind=index+4; break;
case SYS_CMP: ind=index+5; break;
default: ind=index ; break;
}
for (i=0,j=ver<=2.99?0:3;i<ind->n;i++,j+=16) {
if (ver<=2.99&&j>=80) { /* ver.2 */
if (!fgets(buff,MAXRNXLEN,fp)) break;
j=0;
}
if (stat) {
val[i]=str2num(buff,j,14)+ind->shift[i];
lli[i]=(unsigned char)str2num(buff,j+14,1)&3;
}
}
if (!stat) return 0;
for (i=0;i<NFREQ+NEXOBS;i++) {
obs->P[i]=obs->L[i]=0.0; obs->D[i]=0.0f;
obs->SNR[i]=obs->LLI[i]=obs->code[i]=0;
}
/* assign position in obs data */
for (i=n=m=0;i<ind->n;i++) {
p[i]=ver<=2.11?ind->frq[i]-1:ind->pos[i];
if (ind->type[i]==0&&p[i]==0) k[n++]=i; /* C1? index */
if (ind->type[i]==0&&p[i]==1) l[m++]=i; /* C2? index */
}
if (ver<=2.11) {
/* if multiple codes (C1/P1,C2/P2), select higher priority */
if (n>=2) {
if (val[k[0]]==0.0&&val[k[1]]==0.0) {
p[k[0]]=-1; p[k[1]]=-1;
}
else if (val[k[0]]!=0.0&&val[k[1]]==0.0) {
p[k[0]]=0; p[k[1]]=-1;
}
else if (val[k[0]]==0.0&&val[k[1]]!=0.0) {
p[k[0]]=-1; p[k[1]]=0;
}
else if (ind->pri[k[1]]>ind->pri[k[0]]) {
p[k[1]]=0; p[k[0]]=NEXOBS<1?-1:NFREQ;
}
else {
p[k[0]]=0; p[k[1]]=NEXOBS<1?-1:NFREQ;
}
}
if (m>=2) {
if (val[l[0]]==0.0&&val[l[1]]==0.0) {
p[l[0]]=-1; p[l[1]]=-1;
}
else if (val[l[0]]!=0.0&&val[l[1]]==0.0) {
p[l[0]]=1; p[l[1]]=-1;
}
else if (val[l[0]]==0.0&&val[l[1]]!=0.0) {
p[l[0]]=-1; p[l[1]]=1;
}
else if (ind->pri[l[1]]>ind->pri[l[0]]) {
p[l[1]]=1; p[l[0]]=NEXOBS<2?-1:NFREQ+1;
}
else {
p[l[0]]=1; p[l[1]]=NEXOBS<2?-1:NFREQ+1;
}
}
}
/* save obs data */
j=0;
for (i=0;i<ind->n;i++) {
if (p[i]<0||val[i]==0.0) continue;
switch (ind->type[i]) {
case 0: obs->P[p[i]]=val[i]; obs->code[p[i]]=ind->code[i];
obs->type[j++]=code2obs(obs->code[p[i]],&p[i]); break;
case 1: obs->L[p[i]]=val[i]; obs->LLI [p[i]]=lli[i]; break;
case 2: obs->D[p[i]]=(float)val[i]; break;
case 3: obs->SNR[p[i]]=(unsigned char)(val[i]*4.0+0.5); break;
}
}
return 1;
}
/* save slips ----------------------------------------------------------------*/
static void saveslips(unsigned char slips[][NFREQ], obsd_t *data)
{
int i;
for (i=0;i<NFREQ;i++) {
if (data->LLI[i]&1) slips[data->sat-1][i]|=1;
}
}
/* restore slips -------------------------------------------------------------*/
static void restslips(unsigned char slips[][NFREQ], obsd_t *data)
{
int i;
for (i=0;i<NFREQ;i++) {
if (slips[data->sat-1][i]&1) data->LLI[i]|=1;
slips[data->sat-1][i]=0;
}
}
/* add obs data --------------------------------------------------------------*/
static int addobsdata(obs_t *obs, const obsd_t *data)
{
obsd_t *obs_data;
double dt;
if (!(PPP_Glo.sFlag[data->sat-1].sys&PPP_Glo.prcOpt_Ex.navSys)) {
return 1;
}
if (PPP_Glo.prcOpt_Ex.tPrcUnit>0.0) {
dt=fmod(data->time.time+data->time.sec,PPP_Glo.prcOpt_Ex.tPrcUnit);
if (fabs(dt)>0.005)
return 1;
}
if (obs->nmax<=obs->n) {
if (obs->nmax<=0) obs->nmax=NINCOBS; else obs->nmax*=2;
if (!(obs_data=(obsd_t *)realloc(obs->data,sizeof(obsd_t)*obs->nmax))) {
//sprintf(PPP_Glo.chMsg,"*** ERROR: addobsdata: memalloc error n=%dx%d\n",sizeof(obsd_t),obs->nmax);
//outDebug(1,1,0);
free(obs->data); obs->data=NULL; obs->n=obs->nmax=0;
return -1;
}
obs->data=obs_data;
}
obs->data[obs->n++]=*data;
return 1;
}
/* set system mask -----------------------------------------------------------*/
static int set_sysmask(const char *opt)
{
const char *p;
int mask=SYS_NONE;
if (!(p=strstr(opt,"-SYS="))) return SYS_ALL;
for (p+=5;*p&&*p!=' ';p++) {
switch (*p) {
case 'G': mask|=SYS_GPS; break;
case 'R': mask|=SYS_GLO; break;
case 'E': mask|=SYS_GAL; break;
case 'J': mask|=SYS_QZS; break;
case 'C': mask|=SYS_CMP; break;
case 'S': mask|=SYS_SBS; break;
}
}
return mask;
}
/* set signal index ----------------------------------------------------------*/
static void set_index(double ver, int sys, const char *opt,
char tobs[MAXOBSTYPE][4], sigind_t *ind)
{
const char *p;
char str[8],*optstr="";
double shift;
int i,j,k,n,k0,k1,k2,k3;
for (i=n=0;*tobs[i];i++,n++) {
ind->code[i]=obs2code(tobs[i]+1,ind->frq+i);
ind->type[i]=(p=strchr(obscodes,tobs[i][0]))?(int)(p-obscodes):0;
ind->pri[i]=getcodepri(sys,ind->code[i],opt);
ind->pos[i]=-1;
/* frequency index for beidou */
if (sys==SYS_CMP) {
if (ind->frq[i]==5) ind->frq[i]=2; /* B2 */
else if (ind->frq[i]==4) ind->frq[i]=3; /* B3 */
}
else if (sys==SYS_GAL) { /* frequency index for galileo, added by fzhou @ GFZ, 2017-04-10 */
if (ind->frq[i]==3) ind->frq[i]=2; /* E5a */
else if (ind->frq[i]==5) ind->frq[i]=3; /* E5b */
}
}
/* parse phase shift options */
switch (sys) {
case SYS_GPS: optstr="-GL%2s=%lf"; break;
case SYS_GLO: optstr="-RL%2s=%lf"; break;
case SYS_GAL: optstr="-EL%2s=%lf"; break;
case SYS_QZS: optstr="-JL%2s=%lf"; break;
case SYS_SBS: optstr="-SL%2s=%lf"; break;
case SYS_CMP: optstr="-CL%2s=%lf"; break;
}
for (p=opt;p&&(p=strchr(p,'-'));p++) {
if (sscanf(p,optstr,str,&shift)<2) continue;
for (i=0;i<n;i++) {
if (strcmp(code2obs(ind->code[i],NULL),str)) continue;
ind->shift[i]=shift;
printf("phase shift: sys=%2d tobs=%s shift=%.3f\n",sys,
tobs[i],shift);
}
}
/* assign index for highest priority code */
for (i=0;i<NFREQ;i++) {
/*for (j=0,k=-1;j<n;j++) {
if (ind->frq[j]==i+1&&ind->pri[j]&&(k<0||ind->pri[j]>ind->pri[k])) {
k=j;
}
}
if (k<0) continue;
for (j=0;j<n;j++) {
if (ind->code[j]==ind->code[k]) ind->pos[j]=i;
}*/
//flynn 2017-04-02
k0=k1=k2=k3=-1;
for (j=0;j<n;j++) {
if (ind->type[j]==0) {
if (ind->frq[j]==i+1&&ind->pri[j]&&(k0<0||ind->pri[j]>ind->pri[k0])) {
k0=j;
}
}
else if (ind->type[j]==1) {
if (ind->frq[j]==i+1&&ind->pri[j]&&(k1<0||ind->pri[j]>ind->pri[k1])) {
k1=j;
}
}
else if (ind->type[j]==2) {
if (ind->frq[j]==i+1&&ind->pri[j]&&(k2<0||ind->pri[j]>ind->pri[k2])) {
k2=j;
}
}
else if (ind->type[j]==3) {
if (ind->frq[j]==i+1&&ind->pri[j]&&(k3<0||ind->pri[j]>ind->pri[k3])) {
k3=j;
}
}
}
if (k0>=0) ind->pos[k0]=i;
if (k1>=0) ind->pos[k1]=i;
if (k2>=0) ind->pos[k2]=i;
if (k3>=0) ind->pos[k3]=i;
}
/* assign index of extended obs data */
for (i=0;i<NEXOBS;i++) {
for (j=0;j<n;j++) {
if (ind->code[j]&&ind->pri[j]&&ind->pos[j]<0) break;
}
if (j>=n) break;
for (k=0;k<n;k++) {
if (ind->code[k]==ind->code[j]) ind->pos[k]=NFREQ+i;
}
}
for (i=0;i<n;i++) {
if (!ind->code[i]||!ind->pri[i]||ind->pos[i]>=0) continue;
//printf("reject obs type: sys=%2d, obs=%s\n",sys,tobs[i]);
}
ind->n=n;
}
/* read rinex obs data body --------------------------------------------------*/
static int readrnxobsb(FILE *fp, const char *opt, double ver,
char tobs[][MAXOBSTYPE][4], int *flag, obsd_t *data)
{
gtime_t time={0};
sigind_t index[6]={{0}};
char buff[MAXRNXLEN];
int i=0,n=0,nsat=0,sats[MAXOBS]={0},mask;
/* set system mask */
mask=set_sysmask(opt);
/* set signal index */
set_index(ver,SYS_GPS,opt,tobs[0],index );
set_index(ver,SYS_GLO,opt,tobs[1],index+1);
set_index(ver,SYS_GAL,opt,tobs[2],index+2);
set_index(ver,SYS_QZS,opt,tobs[3],index+3);
set_index(ver,SYS_SBS,opt,tobs[4],index+4);
set_index(ver,SYS_CMP,opt,tobs[5],index+5);
/* read record */
while (fgets(buff,MAXRNXLEN,fp)) {
/* decode obs epoch */
if (i==0) {
if ((nsat=decode_obsepoch(fp,buff,ver,&time,flag,sats))<=0) {
continue;
}
}
else if (*flag<=2||*flag==6) {
data[n].time=time;
data[n].sat=(unsigned char)sats[i-1];
/* decode obs data */
if (decode_obsdata(fp,buff,ver,mask,index,data+n)&&n<MAXOBS) n++;
}
if (++i>nsat) return n;
}
return -1;
}
/* read rinex obs ------------------------------------------------------------*/
static int readrnxobs(FILE *fp, gtime_t ts, gtime_t te, double tint,
const char *opt, int rcv, double ver, int tsys,
char tobs[][MAXOBSTYPE][4], obs_t *obs)
{
obsd_t *data;
unsigned char slips[MAXSAT][NFREQ]={{0}};
int i,n,flag=0,stat=0;
rcv=1;
if (!obs||rcv>MAXRCV) return 0;
if (!(data=(obsd_t *)malloc(sizeof(obsd_t)*MAXOBS))) return 0;
/* read rinex obs data body */
while ((n=readrnxobsb(fp,opt,ver,tobs,&flag,data))>=0&&stat>=0) {
for (i=0;i<n;i++) {
/* utc -> gpst */
if (tsys==TSYS_UTC) data[i].time=utc2gpst(data[i].time);
/* save cycle-slip */
saveslips(slips,data+i);
}
/* screen data by time */
if (n>0&&!screent(data[0].time,ts,te,tint)) continue;
for (i=0;i<n;i++) {
/* restore cycle-slip */
restslips(slips,data+i);
data[i].rcv=(unsigned char)rcv;
/* save obs data */
if ((stat=addobsdata(obs,data+i))<0) break;
}
}
free(data);
return stat;
}
/* decode ephemeris ----------------------------------------------------------*/
static int decode_eph(double ver, int sat, gtime_t toc, const double *data,
eph_t *eph)
{
eph_t eph0={0};
int sys;
sys=satsys(sat,NULL);
if (!(sys&(SYS_GPS|SYS_GAL|SYS_QZS|SYS_CMP))) {
//printf("ephemeris error: invalid satellite sat=%2d\n",sat);
return 0;
}
*eph=eph0;
eph->sat=sat;
eph->toc=toc;
eph->f0=data[0];
eph->f1=data[1];
eph->f2=data[2];
eph->A=SQR(data[10]); eph->e=data[ 8]; eph->i0 =data[15]; eph->OMG0=data[13];
eph->omg =data[17]; eph->M0 =data[ 6]; eph->deln=data[ 5]; eph->OMGd=data[18];
eph->idot=data[19]; eph->crc=data[16]; eph->crs =data[ 4]; eph->cuc =data[ 7];
eph->cus =data[ 9]; eph->cic=data[12]; eph->cis =data[14];
if (sys==SYS_GPS||sys==SYS_QZS) {
eph->iode=(int)data[ 3]; /* IODE */
eph->iodc=(int)data[26]; /* IODC */
eph->toes= data[11]; /* toe (s) in gps week */
eph->week=(int)data[21]; /* gps week */
eph->toe=adjweek(gpst2time(eph->week,data[11]),toc);
eph->ttr=adjweek(gpst2time(eph->week,data[27]),toc);
eph->code=(int)data[20]; /* GPS: codes on L2 ch */
eph->svh =(int)data[24]; /* sv health */
eph->sva=uraindex(data[23]); /* ura (m->index) */
eph->flag=(int)data[22]; /* GPS: L2 P data flag */
eph->tgd[0]= data[25]; /* TGD */
eph->fit = data[28]; /* fit interval */
}
else if (sys==SYS_GAL) { /* GAL ver.3 */
eph->iode=(int)data[ 3]; /* IODnav */
eph->toes= data[11]; /* toe (s) in galileo week */
eph->week=(int)data[21]; /* gal week = gps week */
eph->toe=adjweek(gpst2time(eph->week,data[11]),toc);
eph->ttr=adjweek(gpst2time(eph->week,data[27]),toc);
eph->code=(int)data[20]; /* data sources */
/* bit 0 set: I/NAV E1-B */
/* bit 1 set: F/NAV E5a-I */
/* bit 2 set: F/NAV E5b-I */
/* bit 8 set: af0-af2 toc are for E5a.E1 */
/* bit 9 set: af0-af2 toc are for E5b.E1 */
eph->svh =(int)data[24]; /* sv health */
/* bit 0: E1B DVS */
/* bit 1-2: E1B HS */
/* bit 3: E5a DVS */
/* bit 4-5: E5a HS */
/* bit 6: E5b DVS */
/* bit 7-8: E5b HS */
eph->sva =uraindex(data[23]); /* ura (m->index) */
eph->tgd[0]= data[25]; /* BGD E5a/E1 */
eph->tgd[1]= data[26]; /* BGD E5b/E1 */
}
else if (sys==SYS_CMP) { /* BeiDou v.3.02 */
eph->toc=bdt2gpst(eph->toc); /* bdt -> gpst */
eph->iode=(int)data[ 3]; /* AODE */
eph->iodc=(int)data[28]; /* AODC */
eph->toes= data[11]; /* toe (s) in bdt week */
eph->week=(int)data[21]; /* bdt week */
eph->toe=bdt2gpst(bdt2time(eph->week,data[11])); /* bdt -> gpst */
eph->ttr=bdt2gpst(bdt2time(eph->week,data[27])); /* bdt -> gpst */
eph->toe=adjweek(eph->toe,toc);
eph->ttr=adjweek(eph->ttr,toc);
eph->svh =(int)data[24]; /* satH1 */
eph->sva=uraindex(data[23]); /* ura (m->index) */
eph->tgd[0]= data[25]; /* TGD1 B1/B3 */
eph->tgd[1]= data[26]; /* TGD2 B2/B3 */
}
if (eph->iode<0||1023<eph->iode) {
printf("rinex nav invalid: sat=%2d iode=%d\n",sat,eph->iode);
}
if (eph->iodc<0||1023<eph->iodc) {
printf("rinex nav invalid: sat=%2d iodc=%d\n",sat,eph->iodc);
}
return 1;
}
/* decode glonass ephemeris --------------------------------------------------*/
static int decode_geph(double ver, int sat, gtime_t toc, double *data,
geph_t *geph)
{
geph_t geph0={0};
gtime_t tof;
double tow,tod;
int week,dow;
if (satsys(sat,NULL)!=SYS_GLO) {
printf("glonass ephemeris error: invalid satellite sat=%2d\n",sat);
return 0;
}
*geph=geph0;
geph->sat=sat;
/* toc rounded by 15 min in utc */