Main Page   File List  

chestr.c

00001 /* Copyright: 1Earth Research, LLC
00002  * All Rights Reserved
00003  * December 11, 2004
00004  */
00005 
00006 #include <stdlib.h>
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <math.h>
00010 
00011 /*----------------------------------------------------------------------------*/
00012 /* function prototypes */
00013 long checksum (char *s, long nch, int ifl);
00014 
00015 /*******************************************************************************
00016 * PROGRAM NAME: chestr
00017 *   Converts a TLE satellite number to a new user-entererd number, 
00018 *       making sure that the checksum is also changed.
00019 * Inputs:
00020 *   Name of the TLE file
00021 * Outputs:
00022 *   Modified TLE file with different name
00023 * Return value:
00024 *   0 on successful completion
00025 * Includes required:
00026 *   (none)
00027 * Non-ANSI functions/macros/globals referenced:
00028 *   (none)
00029 * Comments/Reference:
00030 *   (none)
00031 * Modification History:
00032 *   041211  R.G. Gist       First release
00033 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00034 int main (int argc, char **argv)
00035 
00036 #define MAXCHR 512 /* max # of characters in a line */
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     /* console input */
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         /* printf("TLE filename: %s\n",file_name); */
00073 
00074         sprintf(new_file_name,"new_%s",file_name);
00075         /* printf("new TLE filename: %s\n",new_file_name); */
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     /* Read in lines until end of file */
00088     while (!feof(fp1)) {
00089 
00090           lnum++;
00091 
00092           /* If end of file, kick out of loop */
00093           if ( fgets(line,MAXCHR,fp1) == NULL ) break;
00094 
00095           l=strlen(line);
00096 
00097           /* Test for a TLE first line */
00098           if (line[0]=='1' && line[1]==' ') {
00099 
00100             /* Check length of line */
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             /* Report first line of TLE */
00111             printf("TLE found...\n");
00112 
00113             /* Read current object number from first line */
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             /* Prompt user for new number */
00121             printf("\tEnter new object number [Enter to skip]: ");
00122             fgets(s,MAXCHR,stdin);
00123             /* fscanf(stdin,"%s",&s); */ /* Doesn't allow skip */
00124             l=strlen(s);
00125             if (l==1) {
00126               printf("\tCurrent number kept.\n");
00127 
00128             } else {
00129 
00130               /* Clean up and format the input string */
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               /* Insert new number in place of the old one */
00143               for (i=4; i>=0; i--) {
00144                 line[i+2]=new_num[i];
00145               }
00146 
00147               /* Compute new checksum */
00148               chksm=checksum(line,68,1);
00149               printf("\tchecksum: %ld\n",chksm%10);
00150 
00151               /* Insert new checksum into line */
00152               sprintf(line+68,"%ld\r\n",chksm%10);
00153 
00154               /* Insert new checksum into line */
00155 
00156               modified_line_1_flag=1;
00157             }
00158 
00159             /* Write (possibly) modified line to new file */
00160             fprintf(fp2,"%s",line);
00161 
00162           } else if (line[0]=='2' && line[1]==' ') {
00163 
00164             /* Check length of line */
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             /* Report second line of TLE */
00175             /* printf("second line of TLE\n"); */
00176 
00177             if (modified_line_1_flag==1) {
00178               /* Insert new number in place of the old one */
00179               for (i=4; i>=0; i--) {
00180                 line[i+2]=new_num[i];
00181               }
00182 
00183               /* Compute new checksum */
00184               chksm=checksum(line,68,1);
00185               printf("\tchecksum: %ld\n",chksm%10);
00186 
00187               /* Insert new checksum into line */
00188               sprintf(line+68,"%ld\r\n",chksm%10);
00189 
00190               modified_line_1_flag=0;
00191             }
00192 
00193             /* Write (possibly) modified line to new file */
00194             fprintf(fp2,"%s",line);
00195 
00196             /* Not a TLE line.  Echo to new file */
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') { /* found a number */
00250             into_num=1;
00251             sum+=(*s-'0');
00252         }
00253     }
00254     return (sum);
00255 }
00256 /******************************************************************************/

Generated on Sun Dec 12 17:38:08 2004 for CHESTR by doxygen1.2.18
Sitemap