#include "stdio.h" #include "astring.h" #include "string.h" #include "flatfile.h" #include "stdlib.h" void average_data(float *avedata, int totpts, 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; int inunit, outunit, ncol[1], csize[1], error, nvar, strl; long srow, erow, nrow[1], orow, irow, npts, sqrow, eqrow, mqrow, nonmiss; long totrow, i, ii, k; float *data, *avedata, missing, dt; 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]); } /* open input file */ inunit = ffopen(filein); /* read input file header information */ vn = calloc(sizeof(char),7*16+2); vu = calloc(sizeof(char),7*16+2); vs = calloc(sizeof(char),7*31+2); vt = calloc(sizeof(char),7*2+2); data = calloc(sizeof(float),ff_ncol[inunit]); error = rdhead(inunit,vn,vu,vs,vt,no, stime,etime,nrow,ncol,csize); error = fdget(inunit,1,uttime,data); dts = uttime[0]; error = fdget(inunit,0,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; while (sqrow == eqrow) { 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); 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. Please enter a new day.\n"); } } average_data(avedata,totrow,npts); /* create new flat file */ strcat(no,",THIS DATA HAS QUIET DAY REMOVED"); strcat(no,",start time of quiet day removed : "); strcat(no,quietstime); /* nvar = 7; strcat(vn,","); strcat(vn,strmid(vn,15,4)); strcat(vn,"-old"); strcat(vn,","); strcat(vn,strmid(vn,30,4)); strcat(vn,"-old"); strcat(vn,","); strcat(vn,strmid(vn,45,4)); strcat(vn,"-old"); strcat(vu,",nT,nT,nT"); strcpy(vs,"Time from copied file"); strcat(vs,",Variable with "); if (npts == 0) strcat(vs,"mean removed"); else strcat(vs,"curve removed"); strcat(vs,",Variable with "); if (npts == 0) strcat(vs,"mean removed"); else strcat(vs,"curve removed"); strcat(vs,",Variable with "); if (npts == 0) strcat(vs,"mean removed"); else strcat(vs,"curve removed"); strcat(vs,",Old value of variable"); strcat(vs,",Old value of variable"); strcat(vs,",Old value of variable"); strcat(vt,",R,R,R"); */ nvar = 4; error = mkhead(fileout,nvar,vn,vu,vs,vt,no); outunit = ffcreate(fileout); if (outunit != -1) { orow = 0; irow = 0; erow = n_rows(inunit); while (irow < erow) { error = fdget(inunit,irow,uttime,data); if (uttime[0] < 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; } } } } }