#include "stdio.h" #include "string.h" #include "astring.h" #include "flatfile.h" #include "stdlib.h" #include "math.h" void average_data(float *avedata, int totpts, int npts); void average(float *avedata, int totpts, float *aves, float *std, int *npts); void main(int argc, char *argv[]) { FILE *fpout; char filein[100], fileout[100], que[10]; char *vn, *vu, *vs, *vt, no[1000]; char stime[24],etime[24], outstime[24], outetime[24], quietstime[24]; double suttime, euttime, uttime[1], dts, squiettime, equiettime; double midnight, outut; int inunit, outunit, ncol[1], csize[1], error, nvar, tnpts[3]; long srow, erow, nrow[1], orow, irow, npts, sqrow, eqrow, mqrow, nonmiss; long totrow, i, ii, k; float *data, *avedata, missing, dt, *curve, aves[3],stds[3]; int itime[6], strl; missing = -1.0e32; /* get input file name */ if (argc < 2) { printf("Enter flat file to remove quiet day from : "); gets(filein); } else { strcpy(filein,argv[1]); } /* get output file name */ if (argc < 3) { printf("Enter output file name : "); gets(fileout); } else { strcpy(fileout,argv[2]); } if (strloc(fileout,".remca") == -1) strcat(fileout,".remca"); /* open input file */ inunit = ffopen(filein); /* read input file header information */ vn = calloc(sizeof(char),ff_ncol[inunit]*16+2); vu = calloc(sizeof(char),ff_ncol[inunit]*16+2); vs = calloc(sizeof(char),ff_ncol[inunit]*31+2); vt = calloc(sizeof(char),ff_ncol[inunit]*2+2); data = calloc(sizeof(float),7); error = rdhead(inunit,vn,vu,vs,vt,no, stime,etime,nrow,ncol,csize); /* allow users to chop file down in size by getting new start and stop */ /* times for the output data file, then determine the start and end rows */ /* in the input file which these times coorespond to. */ printf("Start time of input file : %s\n",stime); printf("Enter new start time of output file : "); gets(outstime); printf("\n"); printf("End time of input file : %s\n",etime); printf("Enter end start time of output file : "); gets(outetime); strl = strlen(outstime); if (strl < 9) strcpy(outstime,strmid(stime,0,9)); strl = strlen(outetime); if (strl < 9) strcpy(outetime,etime); suttime = c_s_to_r(strmid(outstime,0,9)); euttime = c_s_to_r(outetime); srow = fdsearch(inunit, suttime); erow = fdsearch(inunit, euttime); error = fdget(inunit,2,uttime,data); dts = uttime[0]; error = fdget(inunit,1,uttime,data); dts = dts - uttime[0]; /* determine whether a curve or mean is going to be subtracted */ printf("Remove (m)ean or (c)urve : "); gets(que); strcpy(que,mklower(que)); /* if a curve is removed, then a 3 hour running average is going to be */ /* done on the data to smooth out the curve. The number of points to do */ /* the average on is determined here also. */ if ((int) que[0] == (int) 'm') { dt = 0.0; npts = 0; } else { dt = 3.0*3600.0; error = fdget(inunit,1,uttime,data); npts = (long) (dt/dts); } /* determine the start time and end time of the quiet day. */ /* read in the data for the quiet day. If a curve is going to be removed */ /* from the data, then read in a little extra data for the edge effects. */ /* if no good data is read in, prompt for a new quiet day. */ sqrow = 0; eqrow = 0; printf("Enter start time of the quiet day : "); gets(quietstime); strl = strlen(quietstime); if (strl < 9) strcpy(quietstime,stime); squiettime = c_s_to_r(quietstime); midnight = c_s_to_r(strmid(quietstime,0,9)); equiettime = squiettime + 24.0*3600.0 - 1.0; sqrow = fdsearch(inunit, squiettime); mqrow = fdsearch(inunit, midnight); eqrow = fdsearch(inunit, equiettime); nonmiss=0; if (sqrow < eqrow) { totrow = eqrow-sqrow+1; avedata = calloc(sizeof(float), 3*totrow); for (i=sqrow; i<=eqrow; i++) { error = fdget(inunit,i,uttime,data); if (i==sqrow) { c_r_to_i(itime,uttime[0]); strcpy(outstime,c_int_str(itime[1],2)); strcat(outstime,"/"); strcat(outstime,c_int_str(itime[2],2)); strcat(outstime," "); strcat(outstime,c_int_str(itime[3],2)); strcat(outstime,c_int_str(itime[4],2)); strcat(outstime," UT"); } else { if (i==eqrow) { c_r_to_i(itime,uttime[0]); strcpy(outetime,c_int_str(itime[1],2)); strcat(outetime,"/"); strcat(outetime,c_int_str(itime[2],2)); strcat(outetime," "); strcat(outetime,c_int_str(itime[3],2)); strcat(outetime,c_int_str(itime[4],2)); strcat(outetime," UT"); } } ii = i - mqrow; if (ii >= totrow) ii = ii - totrow; for (k=0; k<3; k++) { avedata[totrow*k+ii] = data[k]; if (data[k] != missing) nonmiss++; } } } if (!nonmiss) { printf("There is no data on this day. Skipping station.\n"); } else { average(avedata,totrow,aves,stds,tnpts); average_data(avedata,totrow,npts); if (npts == 0) npts = tnpts[0]; fpout = fopen(fileout,"w"); fprintf(fpout,"%s",strmid(vn,15,3)); fprintf(fpout,"quiet means (%s - %s):\n",outstime,outetime); fprintf(fpout,"comp # mean std comments\n"); for (i=0;i<3;i++) { fprintf(fpout," %s %4d%6ld%6ld\n", strmid(vn,15*(i+1)+3,1),tnpts[i],(long) aves[i], (long) stds[i]); } orow = 0; irow = srow; uttime[0] = 0.0; while (outut < euttime-dts) { if (irow <= erow) { error = fdget(inunit,irow,uttime,data); } else { uttime[0] = suttime+(double) dts*(double) orow; for (i=0; i suttime+(double) dts*(double) orow) { for (i=0; i 0) { for (i=0; i= totpts) kk = kk - totpts; if (avedata[kk] != missing) { ave = ave + avedata[kk]; np++; } } if (np > 0) { avedata[j*totpts+i] = ave / (float) np; } } } } } void average(float *avedata, int totpts, float *ave, float *std, int *npts) { long i,j,k; float missing; missing = -1.0e32; for (j=0; j<3; j++) { ave[j] = 0.0; std[j] = 0.0; npts[j] = 0; for (i=0; i 0) { ave[j] = ave[j] / (float) npts[j]; for (i=0; i