forked from cbassa/sattools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uk2iod.c
164 lines (142 loc) · 3.49 KB
/
uk2iod.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
#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;
float csec,cang,x;
int rah,ram,rafm,ded,dem,defm;
float tm,tx,am,ax;
char sign;
file=fopen(argv[1],"r");
while (fgetline(file,line,LIM)>0) {
// Skip wrong lines
if (!isdigit(line[0]))
continue;
// Skip short lines
if (strlen(line)<55)
continue;
// Scan line
sscanf(line,"%02d%03d%02d%04d%02d%02d%02d%02d%02d%02d%03d",&intidy,&intido,
&piece,
&site,
&year,
&month,
&day,
&hour,
&min,
&sec,
&fsec);
sscanf(line+27,"%f",&csec);
sscanf(line+33,"%1d",&format);
if (format==2) {
sscanf(line+34,"%02d%02d%d",&rah,&ram,&rafm);
sscanf(line+42,"%c%02d%02d%d",&sign,&ded,&dem,&defm);
} else if (format==3) {
sscanf(line+34,"%02d%02d%d",&rah,&ram,&rafm);
sscanf(line+42,"%c%02d%02d",&sign,&ded,&dem);
}
sscanf(line+50,"%f",&cang);
sscanf(line+54,"%d",&epoch);
// Year switch
if (year>50)
year+=1900;
else
year+=2000;
// 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==3) {
x=dem*0.6;
dem=(int) floor(x);
defm=(int) (100.0*(x-dem));
} else if (format!=2) {
fprintf(stderr,"Angle format not implemented!\n");
fprintf(stderr,"%s\n",line);
continue;
}
// Fractional seconds
if (fsec<10)
fsec*=100;
else if (fsec<100)
fsec*=10;
// Time accuracy
if (csec<10)
csec*=0.1;
else if (csec<100)
csec*=0.01;
tx=floor(log10(csec))+8;
tm=floor(csec/pow(10.0,tx-8));
// angle accuracy
if (cang<10)
cang*=1;
else if (cang<100)
cang*=0.1;
ax=floor(log10(cang))+8;
am=floor(cang/pow(10.0,ax-8));
// 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;
}