00001
00002
00003
00004
00005
00006 #include <stdlib.h>
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <math.h>
00010
00011
00012
00013 long checksum (char *s, long nch, int ifl);
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 int main (int argc, char **argv)
00035
00036 #define MAXCHR 512
00037 {
00038 FILE *fp1,*fp2;
00039 char *line,*file_name,*new_file_name;
00040 char curr_num[6],new_num[6],s[MAXCHR],input[5];
00041 int i,j,l,lnum=0;
00042 int modified_line_1_flag=0;
00043 long chksm;
00044 long il,jl;
00045
00046 line=(char *) malloc (MAXCHR*sizeof(char));
00047 file_name=(char *) malloc (MAXCHR*sizeof(char));
00048 new_file_name=(char *) malloc (MAXCHR*sizeof(char));
00049
00050 printf("\n\n --------------------- \n");
00051 printf(" TLE CHECKSUM MODIFIER \n");
00052 printf(" --------------------- \n");
00053 printf(" 1Earth Research, LLC \n");
00054 printf(" [email protected] \n\n");
00055
00056 if (argc==1) {
00057 printf("This program scans a file for two-line element sets (TLES)\n");
00058 printf("When a TLE is detected, the object number is reported to\n");
00059 printf("the user. The user can keep the number, or enter a new one.\n");
00060 printf("If a new number is entered, a new checksum will be computed\n");
00061 printf("for that object.\n");
00062 printf("\n");
00063 }
00064
00065
00066 if (argc!=2 || argv[1]=="?") {
00067 fprintf(stderr,"Usage: chestr filename");
00068 return(1);
00069 }
00070
00071 strncpy(file_name,argv[1],MAXCHR);
00072
00073
00074 sprintf(new_file_name,"new_%s",file_name);
00075
00076
00077 if (!(fp1=fopen(file_name,"r"))) {
00078 fprintf(stderr,"Can\'t open input file \'%s\'.",file_name);
00079 return(1);
00080 }
00081 if (!(fp2=fopen(new_file_name,"w"))) {
00082 fprintf(fp2,new_file_name,"Can\'t open output file \'%s\'.",
00083 new_file_name);
00084 return(1);
00085 }
00086
00087
00088 while (!feof(fp1)) {
00089
00090 lnum++;
00091
00092
00093 if ( fgets(line,MAXCHR,fp1) == NULL ) break;
00094
00095 l=strlen(line);
00096
00097
00098 if (line[0]=='1' && line[1]==' ') {
00099
00100
00101 if (l!=71) {
00102 printf("Apparent TLE line 1 (line %d of %s) ",lnum,file_name);
00103 printf("does not have the right number of characters.\n");
00104 printf("It should be of the format:\n");
00105 printf("1 88888U 04313.81696759 .00000000 ");
00106 printf("00000-0 00000-0 0 38\n");
00107 return(1);
00108 }
00109
00110
00111 printf("TLE found...\n");
00112
00113
00114 for (i=0; i<5; i++) {
00115 curr_num[i]=line[i+2];
00116 }
00117 curr_num[i]='\0';
00118 printf("\tCurrent object number: %s\n",curr_num);
00119
00120
00121 printf("\tEnter new object number [Enter to skip]: ");
00122 fgets(s,MAXCHR,stdin);
00123
00124 l=strlen(s);
00125 if (l==1) {
00126 printf("\tCurrent number kept.\n");
00127
00128 } else {
00129
00130
00131 strcpy(new_num,"00000");
00132 for (i=0,j=0; i<l; i++) {
00133 if (s[i]>47 && s[i]<58) input[j++]=s[i];
00134 }
00135 input[j]='\0';
00136 l=strlen(input);
00137 for (i=l; i>=0; i--) {
00138 new_num[i+5-l]=input[i];
00139 }
00140 printf("\tNew number: %s\n",new_num);
00141
00142
00143 for (i=4; i>=0; i--) {
00144 line[i+2]=new_num[i];
00145 }
00146
00147
00148 chksm=checksum(line,68,1);
00149 printf("\tchecksum: %ld\n",chksm%10);
00150
00151
00152 sprintf(line+68,"%ld\r\n",chksm%10);
00153
00154
00155
00156 modified_line_1_flag=1;
00157 }
00158
00159
00160 fprintf(fp2,"%s",line);
00161
00162 } else if (line[0]=='2' && line[1]==' ') {
00163
00164
00165 if (l!=71) {
00166 printf("Apparent TLE line 2 (line %d of %s) ",lnum,file_name);
00167 printf("does not have the right number of characters.\n");
00168 printf("It should be of the format:\n");
00169 printf("2 88888 28.8016 156.5475 0061387 291.2918 ");
00170 printf("216.6065 16.21858240 00\n");
00171 return(1);
00172 }
00173
00174
00175
00176
00177 if (modified_line_1_flag==1) {
00178
00179 for (i=4; i>=0; i--) {
00180 line[i+2]=new_num[i];
00181 }
00182
00183
00184 chksm=checksum(line,68,1);
00185 printf("\tchecksum: %ld\n",chksm%10);
00186
00187
00188 sprintf(line+68,"%ld\r\n",chksm%10);
00189
00190 modified_line_1_flag=0;
00191 }
00192
00193
00194 fprintf(fp2,"%s",line);
00195
00196
00197 } else {
00198 fprintf(fp2,"%s",line);
00199 }
00200 }
00201
00202 printf("\nModified TLEs written to: %s\n",new_file_name);
00203
00204 free(line);
00205
00206 fclose(fp1);
00207 fclose(fp2);
00208
00209
00210 return(0);
00211 }
00212
00235 long checksum (char *s, long nch, int ifl)
00236 {
00237 long i,into_num=0,sum=0;
00238
00239 for (i=0;i<nch;i++,s++) {
00240 if (*s=='\n') break;
00241 if (*s==' ') {
00242 if (ifl || !into_num) continue;
00243 break;
00244 } else if (*s=='-') {
00245 into_num=1;
00246 sum++;
00247 } else if (*s=='+' || *s=='&') {
00248 into_num=1;
00249 } else if (*s>='0' && *s<='9') {
00250 into_num=1;
00251 sum+=(*s-'0');
00252 }
00253 }
00254 return (sum);
00255 }
00256