//Filter.c v1.1, 27 March 1996, to remove integrity-compromised data from the //AERI datafile. Ensure the datafile containing the info on which data is //to be excluded is of the form YYMMDD hh:mm:ss hh:mm:ss where times are //start excluding and stop excluding times respectively. Modified from v1 //to write whole strings, making it more flexible for other uses. #include #include #include #include #include #define dev (0) //development flag, prints working values #define batch (1) //for batch use set to 1 FILE *fp1,*fp2,*fp3, *fp4; //void main(void); remove comments for non-batch operation void main(int _argc, char **_argv); //remove comments for batch operation void openfiles(void); //procedure prototypes void filterfile (void); void closefiles(void); void getinquiry(void); double convertcoltime(char *); double convertime(char *); void findmonth(int); char date[80], infile1name[50], infile2name[50], outfilename[50]; int numheaderlines; char monthtext[6]; //void main(void) remove comments for non-batch operation void main (int _argc, char** _argv) //remove comments for batch operation { getinquiry(); while (!feof(fp1)) filterfile(); closefiles(); } /*-----------------------------------------------------------*/ void getinquiry (void) { char chaff[200]; char cchaff; char header[200]; int i; struct dosdate_t d; _dos_getdate(&d); if(batch) { if(_argc!=5) { printf("Sorry, wrong number of arguments to filter.c\n"); printf("Expected: AERI filename, Integrity-compromised data file, Output filename, Lines of Header\n"); exit(0); } strcpy (infile1name, _argv[1]); strcpy (infile2name, _argv[2]); strcpy (outfilename, _argv[3]); numheaderlines = atoi(_argv[4]); } if(dev) //if developing, use these templates to avoiding entering full paths { sprintf(infile1name,"c:\\osborne\\cdev2\\ae960321.w55"); sprintf(infile2name,"c:\\osborne\\cdev2\\baddata.txt"); sprintf(outfilename,"c:\\osborne\\cdev2\\output.txt"); } printf("\nAERI Data Filtering Program...\n\n"); printf("Version 1.1, March 27 1996\n"); printf("\n"); printf("For removing integrity-compromised data from AERI datafile\n"); if(!(batch)) { printf("Please enter the filename for the source AERI data\n"); if(!(dev)) scanf("%s", infile1name); } if(!(batch)) { printf("How many header lines does this file have?\n"); scanf("%d", numheaderlines); } fp1 = fopen(infile1name,"r"); if (fp1 == NULL) { printf("Sorry, could not open file %s\n", infile1name); printf("Press ENTER to continue...\n"); fflush(stdin); scanf("%c", &cchaff); exit(0); } if(!(batch)) { printf("\nPlease enter the name of the data integrity file\n"); if(!(dev)) scanf("%s", infile2name); } fp2 = fopen (infile2name, "r"); if (fp2 == NULL) { printf("Sorry, could not open file %s\n", infile2name); printf("Press ENTER to continue...\n"); fflush(stdin); scanf("%c", &cchaff); exit(0); } fgets(chaff, 199, fp2); //remove header if(!(batch)) { printf("\nPlease enter a name for the filtered output file\n"); if(!(dev)) scanf("%s", outfilename); } fp3 = fopen (outfilename, "w"); if (fp3 == NULL) { printf("Sorry, could not open file %s\n", outfilename); printf("Press return to continue\n"); fflush(stdin); scanf("%c", &cchaff); exit(0); } findmonth(d.month); fprintf(fp3, "%s. Processed %2d %s %d by Filter.c, v1.1\n", infile1name, d.day, monthtext, d.year); for (i=1; i<=numheaderlines; i++) { fgets(header, 199, fp1); //remove header lines fprintf(fp3, "%s", header); } fp4 = fopen ("filtlog.txt", "w"); if (fp4 == NULL) { printf("Sorry, could not open file filterlog\n"); printf("Press ENTER to continue...\n"); fflush(stdin); scanf("%c", &cchaff); exit(0); } fprintf(fp4, "Lines removed from file %s by Filter.c v1.1\n", infile1name); fprintf(fp4, "(SSEC Integrity-compromised data removal software)\n"); } /*-----------------------------------------------------------*/ void filterfile (void) { char aeribuffer[200], removebuffer[200]; char chaff[200]; char aeridate[15], aeritime[15]; double UTCHour; double decaetime; //decimal conversion for time double aetimefloat; //float version of aedateint for printing leading zeros long int aedateint, aetimeint; //for each row double starttime, stoptime; long int bldate; //date on baddata file char blstoptime[14], blstarttime[14]; //start & stoptimes for exclusion int noprint; //flag for fprinting or not certain lines char cchaff; int j = 0; //counter for number of lines omitted char timestring[14]; while(!(feof(fp1))) //for every line in the AERI datafile... { fgets(aeribuffer, 199, fp1); //read in a line of AERI data if(feof(fp1)) //reached EOF { printf("\nFilter Complete (%d lines removed).\n",j); if(batch==0) { fflush(stdin); printf("Press ENTER to continue\n"); scanf("%c", &cchaff); } exit(0); } sscanf(aeribuffer, "%ld %ld", &aedateint,&aetimeint); noprint =0; sprintf(timestring, "%06.lf", ((double)(aetimeint))); UTCHour = convertime(timestring); while(!(feof(fp2))) //for every line in the baddata file... { fgets(removebuffer, 199, fp2); if(feof(fp2)) break; sscanf(removebuffer, "%ld %10s %10s", &bldate, blstarttime, blstoptime); starttime = convertcoltime(blstarttime); stoptime = convertcoltime(blstoptime); if ((UTCHour > starttime)&&(UTCHour < stoptime)&&(bldate==aedateint)) { if(j==0) printf("\nData excluded from O/P file:\n"); //printf("%06.lf\n",((double)(aetimeint))); printf("%s", aeribuffer); if(((j==20)||(j==40))&&((batch==0))) { printf("Press ENTER for more...\n"); fflush(stdin); scanf("%c", &cchaff); } noprint = 1; aetimefloat = (double)(aetimeint); fprintf(fp4, "%s", aeribuffer); //fprintf(fp4, "%6.ld %06.lf\n", aedateint, aetimefloat); j++; break; } } if(noprint==0) //if here, no exclusion necessary { aetimefloat = (double)(aetimeint); fprintf(fp3, "%s", aeribuffer); } rewind(fp2); fgets(chaff, 199, fp2); //remove header again } } /*-----------------------------------------------------------*/ double convertcoltime(char *intime) { char h1, h2, min1, min2, s1, s2, chaff; //single characters for reading in time string. char hour[5], min[5], sec[5]; //hours, minutes and seconds in string form float hh, minmin, ss; //hours, minutes and seconds as ints double newtime; sscanf(intime, "%c%c%c%c%c%c%c%c", &h1,&h2, &chaff, &min1, &min2, &chaff, &s1, &s2); //chaff colons. sprintf(hour, "%c%c", h1,h2); sprintf(min, "%c%c", min1, min2); sprintf(sec, "%c%c", s1, s2); //if(dev) printf("Time to convert is %s\n", intime); hh = atof(hour); minmin = atof (min); ss = atof (sec); newtime = (hh + (minmin/60.0) + (ss/3600.0)); //if(dev) printf("Newtime after conversion is %lf\n", newtime); return(newtime); } /*------------------------------------------------*/ double convertime(char *in2time) { char h1, h2, min1, min2, s1, s2, chaff; //single characters for reading in time string. char hour[5], min[5], sec[5]; //hours, minutes and seconds in string form float hh, minmin, ss; //hours, minutes and seconds as ints double new2time; sscanf(in2time, "%c%c%c%c%c%c", &h1,&h2,&min1, &min2, &s1, &s2); //chaff colons. sprintf(hour, "%c%c", h1,h2); sprintf(min, "%c%c", min1, min2); sprintf(sec, "%c%c", s1, s2); //if(dev) printf("Time to convert is %s\n", intime); hh = atof(hour); minmin = atof (min); ss = atof (sec); new2time = (hh + (minmin/60.0) + (ss/3600.0)); //if(dev) printf("Newtime after conversion is %lf\n", new2time); return(new2time); } /*-----------------------------------------------------------*/ void closefiles() //program normally exits before here { fclose(fp1); fclose(fp2); fclose(fp3); printf("Files successfully closed\n"); } /*-----------------------------------------------------------*/ void findmonth(int monthnumber) { switch(monthnumber) { case 1: sprintf(monthtext, "JAN"); break; case 2: sprintf(monthtext, "FEB"); break; case 3: sprintf(monthtext, "MAR"); break; case 4: sprintf(monthtext, "APR"); break; case 5: sprintf(monthtext, "MAY"); break; case 6: sprintf(monthtext, "JUN"); break; case 7: sprintf(monthtext, "JUL"); break; case 8: sprintf(monthtext, "AUG"); break; case 9: sprintf(monthtext, "SEP"); break; case 10: sprintf(monthtext, "OCT"); break; case 11: sprintf(monthtext, "NOV"); break; case 12: sprintf(monthtext, "DEC"); break; default: printf("ERROR in month field\n"); } }