#include "stdio.h" #include "misc.h" #include "string.h" #include "astring.h" #include "stdlib.h" #include "flatfile.h" #include "amag.h" void main(int argc, char *argv[]) { FILE *fpin, *fpout; int flag, flatf, helpf, ifilef, error; int nfiles, pos, found, done, inunit, strl, ngood, itime[6]; long oldr, newr, j,i; float lat, maxlat, missing, data[10]; char filein[100], line[80], datafile[80], sday[24], qday[24], ymd[10]; char stat[4], *filelist, rmold_c[100], rmquiet_c[100], temp[100]; double qd, qdp, uttime[1]; missing = -1.0e32; maxlat = 55.0; helpf = 0; ifilef = 0; error = 0; flatf = 0; if (argc >= 2) { for (i=2; i<=argc; i++) { if (strpos(argv[i-1],"-")==0) { flag = 0; if (strpos(argv[i-1],"l")>0) { flatf = 1; flag=1; } if ((strpos(argv[i-1],"h")>0) || (!flag)) { helpf = 1; printf("usage : \n"); printf("aveflat [-h]\n"); printf(" -l save old files\n"); printf(" -h display help\n"); } } else { if (!ifilef) { ifilef = 1; strcpy(filein,argv[i-1]); } } } } if (!helpf) { printf("Enter start time : "); gets(sday); c_s_to_i(itime, sday); strcpy(ymd,c_int_str(itime[0],2)); strcat(ymd,c_int_str(itime[1],2)); strcat(ymd,c_int_str(itime[2],2)); printf("Enter quiet day (return for automatic selection) : "); gets(qday); strl = strlen(qday); if (strl < 9) { qd = quiet_day(10, "/export/data1/ganglu/mags/tempfiles/master_psi.ridleya.1995.23", "*.hed"); } else { qd = c_s_to_r(qday); } printf("Quiet day selected : %s\n",c_r_to_s(qd)); filelist = spawn("ls *.hed"); done = 0; while (!done) { pos = strpos(filelist,"\n"); if (pos > 0) { strcpy(datafile,strmid(filelist,0,strpos(filelist,"\n")-4)); filelist = filelist + strpos(filelist,"\n")+1; strl = strlen(filelist); if (strl < 4) done = 1; } else { done = 1; } strcpy(stat, strmid(datafile,0,3)); printf("file ---%s---\n",datafile); inunit = ffopen(datafile); oldr = fdsearch(inunit, qd); found = 0; i = 1; while (found == 0) { qdp = qd + (double) (i*3600.0); newr = fdsearch(inunit, qdp); if (newr <= oldr) { found = 2; } else { ngood = 0; for (j=oldr; j<=newr; j++) { error = fdget(inunit,j,uttime,data); if ((data[0] != missing) && (data[1] != missing) && (data[2] != missing)) { ngood = ngood++;} } if (ngood > 0) { oldr = newr; i=i+2; } else { found = 2; } } if ((found == 0) && (i >= 24)) found++; } error = ffclose(inunit); if (found == 2) { printf("data on selected quiet day is missing, finding another\n"); strcpy(temp, datafile); strcat(temp,".hed"); qdp = quiet_day(1, "/export/data1/ganglu/mags/tempfiles/master_psi.ridleya.1995.23", temp); } else { qdp = qd; } if (qdp > 0) { printf("Quiet day for file %s : %s\n",datafile,c_r_to_s(qdp)); fpin = fopen( "/export/data1/ganglu/mags/tempfiles/master_psi.ridleya.1995.23", "r"); found = 0; while (!found) { fgets(line,79,fpin); if (feof(fpin)) { found = 1; } else { pos = strpos(line, mkupper(stat)); if (pos == 4) { found = 1; lat = abs(atof(strmid(line,36,6))); } } } fclose(fpin); fpout = fopen("TEMP_FILE.RMCURVE","w"); fprintf(fpout,"%s\n",sday); fprintf(fpout,"%s 23:59:59\n",sday); if (maxlat > lat) { fprintf(fpout,"c\n"); } else { fprintf(fpout,"m\n"); } fprintf(fpout,"%s\n",c_r_to_s(qdp)); fclose(fpout); strcpy(rmquiet_c,"mkremca "); strcat(rmquiet_c,datafile); strcat(rmquiet_c," "); strcat(rmquiet_c,strmid(datafile,0,3)); strcat(rmquiet_c,ymd); strcat(rmquiet_c," < TEMP_FILE.RMCURVE"); printf("%s\n", rmquiet_c); system(rmquiet_c); system("/bin/rm TEMP_FILE.RMCURVE"); } else { printf("no quiet day found for file : %s\n",datafile); } } } }