//CFAeri3.c, taking 115 second averages of IRT data centred on AERI time. #include #include #include #include #include #define dev (0) //development flag, prints working values FILE *fp1,*fp2,*fp3; void main(void); void openfiles(void); //procedure prototypes void readfile (void); double convertime(char *); void getinquiry(void); void closefiles(void); char date[80], infile1name[50], infile2name[50], outfilename[50], suffix[5]; int runmode; void main(void) { getinquiry(); openfiles(); while (!feof(fp1)) readfile (); closefiles(); } /*-----------------------------------------------------------*/ void getinquiry (void) { char y1,y2,d1,d2,m1,m2, day[5], month[5], year[5], shortdate[8], look[7]; int yy,mm,dd; printf("Please enter the date for your SST comparison enquiry, in the form YYMMDD\n"); scanf("%s", &date); sscanf(date, "%c%c%c%c%c%c", &y1,&y2, &m1, &m2, &d1, &d2); sprintf(year, "%c%c", y1,y2); sprintf(month, "%c%c", m1, m2); sprintf(day, "%c%c", d1, d2); sprintf(shortdate, "%c%c%c%c", m1,m2,d1,d2); mm = atoi(month); yy = atoi (year); dd = atoi (day); //check date integrity if (yy!=96) { printf("Sorry, only 1996 data is available\n"); exit(0); } if ((mm<0) || (mm>12)) { printf("Input error in month field\n"); exit(0); } if ((dd<0) || (dd>31)) { printf("Input error in day field\n"); exit(0); } printf("Please enter the AERI datafile suffix (s55,w35, etc)\n"); scanf("%s", &suffix); sprintf(infile1name,"c:\\osborne\\cdev\\ae%s.%s", date, suffix); if (strcmp(suffix, "s55") == 0) sprintf(look, "sky"); else if (strcmp(suffix, "w55") == 0) sprintf(look, "sea"); else printf("Sorry, only 55 degree angles are supported\n"); sprintf(infile2name, "c:\\osborne\\cdev\\%s%s.irt",look, shortdate); sprintf(outfilename, "c:\\osborne\\cdev\\AT%s.%s", date, suffix); if (dev) printf("The filename for IRT data is %s\n", infile2name); if (dev) printf("The filename for AERI data is %s\n", infile1name); } /*-----------------------------------------------------------*/ void openfiles() { char chaff[200]; fp1 = fopen(infile1name,"r"); if (fp1 == NULL) { printf("Sorry, could not open file %s\n", infile1name); exit(0); } fgets(chaff, 199, fp1); //remove header lines fgets(chaff, 199, fp1); fp2 = fopen (infile2name, "r"); if (fp2 == NULL) { printf("Sorry, could not open file %s\n", infile2name); exit(0); } fgets(chaff, 199, fp2); //remove headers fgets(chaff, 199, fp2); fgets(chaff, 199, fp2); fp3 = fopen (outfilename, "w"); if (fp3 == NULL) { printf("Sorry, could not open file %s\n", outfilename); exit(0); } fprintf(fp3, "AeriDate \t AeriTime\t UTCHour \t AvWavenumber \t"); fprintf(fp3, "AeriRadiance \t AeriOBT (K) \t AeriTime \t AeriOBT (C) \t avIRTTemp \t IRTTime1\t IRTTime2\t Diff(secs) \t Stdev (IRT) \t Stdev (IRT-AERI)\n"); } /*-----------------------------------------------------------*/ void readfile (void) { char aeribuffer[200], irtbuffer1[200], irtbuffer2[200]; char outputbuff[400]; char chaff[200]; char cchaff; char aeridate[15], aeritime[15], irtdate[15], irttime[15], irt2date[15], irt2time[15]; char aeday[5], aemonth[5], aeyear[5], irday[5], irmonth[5], iryear[5]; char aey1, aey2, aed1, aed2, aem1, aem2; char iry1, iry2, ird1, ird2, irm1, irm2; double ModyFraction, UTCHour, AvWavenumber, Rad, BTempK, BTempC; int aeyy, aemm, aedd, iryy, irmm, irdd,i,j,k, firsttime; double decaetime, decirtime, decirt2time; //decimal conversion for time double difftime1, difftime2; //the difference between AERI and IRT time double aeritemp, irttemp, irt2temp, irtstat, irt2stat; long int aedateint, aetimeint; //for each row double setirttemp[14], setirtstat[14], setirttime[14], sumirttemp, stdevirttemp, stdevdiff, avirttemp; double timeav; fgets(aeribuffer,199,fp1); //get the first row of AERI file sscanf(aeribuffer, "%ld %c %ld %lf %lf %lf %lf %lf %lf", &aedateint, &cchaff, &aetimeint, &ModyFraction, &UTCHour, &AvWavenumber, &Rad, &BTempK, &BTempC); //extract variables if(dev) printf("AERI Data is retrieved as follows:\n\n"); if(dev) printf("Date is %ld, Time is %.6ld, ModyFraction is %lf, UTCHour is %lf\n",aedateint, aetimeint,ModyFraction, UTCHour); if (dev) printf("AvWavenumber is %lf, Rad is %lf, BTempK is %lf, BTempC is %lf\n", AvWavenumber, Rad, BTempK, BTempC); sprintf(aeridate, "%ld", aedateint); sscanf(aeridate, "%c%c%c%c%c%c", &aey1,&aey2, &aem1, &aem2, &aed1, &aed2); sprintf(aeyear, "%c%c", aey1,aey2); sprintf(aemonth, "%c%c", aem1, aem2); sprintf(aeday, "%c%c", aed1, aed2); aemm = atoi(aemonth); aeyy = atoi (aeyear); aedd = atoi (aeday); decaetime = UTCHour - 0.028; //backup from aeri by 100secs firsttime = 1; while (!feof(fp1)) { fgets(irtbuffer1,199,fp2); if (feof(fp2)) { printf("End of IRT Data File reached. Exiting...\n"); exit(0); } sscanf(irtbuffer1, "%10s %10s %lf %lf", irtdate, irttime, &irttemp, &irtstat); if (firsttime) { sscanf(irtdate, "%c%c%c%c%c%c%c%c%c%c", &irm1, &irm2, &cchaff, &ird1,&ird2, &cchaff, &cchaff,&cchaff, &iry1, &iry2); sprintf(iryear, "%c%c", iry1,iry2); sprintf(irmonth, "%c%c", irm1, irm2); sprintf(irday, "%c%c", ird1, ird2); irmm = atoi(irmonth); iryy = atoi (iryear); irdd = atoi (irday); if ((irmm == aemm)&&(iryy == aeyy)&&(irdd == aedd)) { printf("Successful date match achieved. Stand by...\n"); } else { printf("Unsuccessful date match. Exiting...\n"); exit(0); } } decirtime = convertime(irttime); if (firsttime) { printf("Decimal time conversion complete: first AERI data is for %lf\n", decaetime); printf(" first IRT data is for %lf\n", decirtime); } difftime1 = decaetime - decirtime; if(((difftime1) < 0)&&(firsttime)) { printf("Sorry, this date will require manual manipulation\n"); exit(0); } if(dev) printf("Difftime1 is %lf\n", difftime1); if (dev) scanf("%c", cchaff); fgets(irtbuffer2,199,fp2); if (feof(fp2)) { printf("End of IRT Data File reached. Exiting...\n"); exit(0); } sscanf(irtbuffer2, "%10s %10s %lf %lf", irt2date, irt2time, &irt2temp, &irt2stat); decirt2time = convertime(irt2time); difftime2 = decaetime - decirt2time; if (dev) printf("Difftime2 is %lf\n",difftime2); if (difftime2 < 0) break; if(difftime2>difftime1) { printf("Sorry, time differences are increasing. Exiting...\n"); exit(0); } firsttime = 0; //skip all initial tasks next time } if (fabs(difftime1)>fabs(difftime2)) { //if here, buffer2 is closest. Ignore buffer 1 and take //buffer 2 as the first data: setirttime[1] = convertime(irt2time); setirttemp[1] = irt2temp; setirtstat[1] = irt2stat; sumirttemp = irt2temp; stdevdiff = (irt2temp - BTempC)*(irt2temp - BTempC); k=2; //at next for loop, start at element [2] of array j=1; //one data set obtained so far } else { //if here, buffer 1 is closest. Have already read in the line after //this, so take buffer2 as the second row of data: setirttime[1] = convertime(irttime); setirttemp[1] = irttemp; setirtstat[1] = irtstat; sumirttemp = irttemp; stdevdiff = (irttemp - BTempC)*(irt2temp - BTempC); setirttemp[2] = irt2temp; setirtstat[2] = irt2stat; sumirttemp = sumirttemp + irt2temp; stdevdiff = stdevdiff + (irttemp - BTempC)*(irt2temp - BTempC); k=3; //at next for loop, start at element [3] of array j=2; //two data sets obtained so far } for (i=k; i<=12; i++) { fgets(irtbuffer1, 199, fp2); if (feof(fp2)) { printf("End of IRT Data file reached. Exiting (2) ...\n"); break; } sscanf(irtbuffer1, "%10s %10s %lf %lf", irtdate, irttime, &irttemp, &irtstat); setirttime[i] = convertime(irttime); setirttemp[i] = irttemp; setirtstat[i] = irtstat; sumirttemp = sumirttemp + irttemp; stdevdiff = stdevdiff + (irttemp - BTempC)*(irttemp - BTempC); j++; } avirttemp = sumirttemp/(double)(j); if (dev) printf("j is %d, avirttemp is %lf, sumirttemp is %lf\n", j, avirttemp, sumirttemp); stdevdiff = sqrt(stdevdiff/(j-1)); stdevirttemp = 0.0; for (i=1;i<=(j);i++) {stdevirttemp = stdevirttemp + (setirttemp[i] - avirttemp)*(setirttemp[i] - avirttemp);} stdevirttemp = sqrt(stdevirttemp/(j-1)); if (dev) printf("stdevirttemp is %lf\n", stdevirttemp); timeav = setirttime[j] - setirttime[1]; fprintf(fp3, "%ld \t %.6ld \t %lf\t %lf \t %lf\t %lf\t %lf\t %lf\t %lf\t %lf \t %lf \t %lf\t %lf \t %lf\n", aedateint, aetimeint, UTCHour, AvWavenumber, Rad, BTempK, UTCHour, BTempC, avirttemp,setirttime[1], setirttime[j], timeav*3600.0, stdevirttemp, stdevdiff); } /*-----------------------------------------------------------*/ double convertime(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); } /*-----------------------------------------------------------*/ void closefiles() { fclose(fp1); fclose(fp2); fclose(fp3); printf("Files successfully closed\n"); } /*-----------------------------------------------------------*/