forked from cbassa/sattools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rde2iod.c
152 lines (128 loc) · 3.28 KB
/
rde2iod.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#define LIM 128
int fgetline(FILE *file,char *s,int lim);
int find_satno(char *desig0)
{
FILE *file;
int satno=99999,status;
char desig[16];
char *env,filename[LIM];
env=getenv("ST_DATADIR");
sprintf(filename,"%s/data/desig.txt",env);
file=fopen(filename,"r");
if (file==NULL) {
fprintf(stderr,"Designation file not found!\n");
exit(0);
}
while (!feof(file)) {
status=fscanf(file,"%d %s",&satno,desig);
if (strcmp(desig,desig0)==0)
break;
}
fclose(file);
return satno;
}
int main(int argc,char *argv[])
{
FILE *file;
char line[LIM];
int intidy,intido,piece,site,year,month,day,hour,min,sec,fsec,satno;
char desig[16],pdesig[16];
int format,epoch,dummy,icsec;
float csec,cang;
int rah,ram,rafm,ded,dem,defm;
float tm,tx,am,ax;
char sign;
int lineno=0;
file=fopen(argv[1],"r");
while (fgetline(file,line,LIM)>0) {
if (strncmp(line,"2420",4)==0 && lineno==0) {
lineno++;
sscanf(line,"%04d %02d%02d",&site,&year,&month);
sscanf(line+12,"%1d%1d%1d %3d%1d",&icsec,&dummy,&format,&dummy,&epoch);
csec=0.1*icsec;
cang=dummy;
// Year switch
if (year>50)
year+=1900;
else
year+=2000;
// Time accuracy
tx=floor(log10(csec))+8;
tm=floor(csec/pow(10.0,tx-8));
// angle accuracy
ax=floor(log10(cang))+8;
am=floor(cang/pow(10.0,ax-8));
if (ax>9.0) {
ax=9.0;
am=9.0;
}
continue;
}
if (strlen(line)<5 && lineno==1) {
sscanf(line,"%d",&day);
if (day==999)
break;
continue;
}
// Skip wrong lines
if (!isdigit(line[0]))
continue;
// Skip short lines
if (strlen(line)<31)
continue;
// Scan line
sscanf(line,"%02d%03d%02d",&intidy,&intido,&piece);
sscanf(line+8,"%02d%02d%02d.%02d",&hour,&min,&sec,&fsec);
sscanf(line+18,"%02d%02d%d",&rah,&ram,&rafm);
sscanf(line+24,"%c%02d%02d%d",&sign,&ded,&dem,&defm);
fsec*=10.0;
// Format designation
if (piece<26) {
sprintf(desig,"%02d %03d%c",intidy,intido,piece+'A'-1);
sprintf(pdesig,"%02d%03d%c",intidy,intido,piece+'A'-1);
} else {
fprintf(stderr,"Failed to understand designation!\n");
fprintf(stderr,"%s\n",line);
continue;
}
// Test data format
if (format!=1) {
fprintf(stderr,"Angle format %d not implemented!\n",format);
fprintf(stderr,"%s\n",line);
continue;
}
// Fractional RA
if (rafm<10)
rafm*=100;
else if (rafm<100)
rafm*=10;
// Fractional DE
if (defm<10)
defm*=10;
else if (defm<100)
defm*=1;
// Get satellite number
satno=find_satno(pdesig);
// Format IOD line
printf("%05d %s %04d G %04d%02d%02d%02d%02d%02d%03d %1.0f%1.0f %d%d ",satno,desig,site,year,month,day,hour,min,sec,fsec,tm,tx,format,epoch);
printf("%02d%02d%03d%c%02d%02d%02d %1.0f%1.0f\n",rah,ram,rafm,sign,ded,dem,defm,am,ax);
}
fclose(file);
return 0;
}
// Read a line of maximum length int lim from file FILE into string s
int fgetline(FILE *file,char *s,int lim)
{
int c,i=0;
while (--lim > 0 && (c=fgetc(file)) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}