int EqkCut(float *sig, SAC_HD *shd, char *recname, int ithread) { int i, n = shd->npts, ninc = n/1000, npole=0; for(i=0;i<n;i+=ninc) if(fabs(sig[i])<1.e-20) npole++; if(npole>600) { reports[ithread].tail += sprintf(reports[ithread].tail, "*** Warning: Signal time length not long enough. ***"); return 0; } float* sigw = new float[n]; double f2 = 1./Eperh, f1 = f2*0.8, f3 = 1./Eperl, f4 = f3*1.2; double dt = (double)(shd->delta); if( Eperl == -1 ) memcpy(sigw, sig, shd->npts*sizeof(float)); else Filter( f1, f2, f3, f4, dt, n, sig, sigw); /* compute noise level */ int ii, is; int s1k=(int)floor(1000./dt+0.5); // npts of a 1000 sec window int nos1k = (int)(n/s1k); // for (each of) the ith 1000 sec window, search for maximum amplitude and store into win_max[i] double win_max[nos1k]; memset (win_max,0,nos1k*sizeof(double)); for(i=0;i<n;i++) sigw[i] = fabs(sigw[i]); for( ii=0,is=0; is<nos1k; is++ ){ for( ; ii<(is+1)*s1k; ii++ ) if(win_max[is]<sigw[ii]) win_max[is]=sigw[ii]; } for( ;ii<n;ii++ ) if(win_max[is]<sigw[ii]) win_max[is]=sigw[ii]; // sort win_max std::vector<double> win_max_sorted( win_max, win_max+nos1k ); std::sort( win_max_sorted.begin(), win_max_sorted.end() ); // and define max noise level as 2 times the smallest 10 average max double noisemax = 0.; std::vector<double>::iterator iter, itermin; for(itermin=win_max_sorted.begin(); itermin<win_max_sorted.end(); itermin++) if( *itermin > 1.e-20 ) break; if( itermin < win_max_sorted.end() ) { for(iter=itermin; iter<win_max_sorted.end() && iter<itermin+30; iter++) noisemax += *iter; noisemax *= 2./(iter-itermin); } /* compute noise average and noise std between windows */ double window_avg = 0.; for(iter=itermin; iter<win_max_sorted.end() && *iter<noisemax; iter++) window_avg += *iter; ii = iter-itermin; window_avg /= ii; double window_std=0., dtmp; for(iter=itermin; iter<itermin+ii; iter++) { dtmp = window_avg-*iter; window_std += dtmp * dtmp; } window_std=sqrt(window_std/(ii-1)); /* mark windows with a max amp > window_avg+2.0*window_std to be 'zero' */ dtmp = window_avg+2.0*window_std; short keep[nos1k]; for( i =0; i < nos1k; i++) keep[i] = win_max[i] > dtmp ? 0 : 1; /* and zero out invalidated windows */ for( i=0; i < nos1k; i++) if( keep[i] == 0 ) for( ii=i*s1k; ii<(i+1)*s1k; ii++) sig[ii] = 0.; /* locate contigious valid windows and apply a cosine taper */ int rec_b[1000], rec_e[1000], rec_i=0; int winlen_min = (int)ceil(2500./dt); /* locate all rec_begin and rec_end pairs zero out the windows that are shorter than winlen_min */ rec_b[0]=0; for( i=1; i<nos1k; i++){ if(keep[i]-keep[i-1] == 1) rec_b[rec_i]=i*s1k; // a new window begins else if(keep[i]-keep[i-1] == -1) { // the current window ends rec_e[rec_i]=i*s1k; /* invalidate the current window if it is shorter than winlen_min */ if ((rec_e[rec_i]-rec_b[rec_i]) < winlen_min) for(ii=rec_b[rec_i]; ii<rec_e[rec_i]; ii++) sig[ii] = 0.; else rec_i++; } } /* mark the last rec_end and check its window length */ if(keep[nos1k-1]==1) { if( (n-rec_b[rec_i]) < winlen_min*0.6 ) for(ii=rec_b[rec_i]; ii<n; ii++) sig[ii]=0; else { rec_e[rec_i] = n; rec_i++; } } /* check if there's enough data left */ for(ii=0,i=0; i<rec_i; i++) ii += rec_e[i] - rec_b[i]; if( ii < 0.2*n ) { reports[ithread].tail += sprintf(reports[ithread].tail, "*** Warning: Time length not enough after removing earthquakes. Skipped. ***"); return 0; } /* taper 300 sec of data on each side of each window just to be safe */ int taperhl = (int)ceil(150./dt); for(i=0;i<rec_i;i++) { if(rec_b[i]!=0){ TaperCos( sig, rec_b[i] + 2*taperhl, rec_b[i]-1 ); rec_b[i] += taperhl; } if(rec_e[i]!=n){ TaperCos( sig, rec_e[i] - 2*taperhl, rec_e[i]+1 ); rec_e[i] -= taperhl; } } /* produce a new rec file named ft_name_rec2 */ UpdateRec(recname, rec_b, rec_e, rec_i, ithread); /* norm by running average if required */ if( tnorm_flag == 4 ) RunAvgNorm( sig, shd, sigw ); delete [] sigw; sigw = NULL; return 1; }
int main() { printf("******************************************\n"); printf("* Record Manager Application *\n"); printf("* Xinyi He *\n"); printf("******************************************\n"); char c; do { printf("\n"); printf("a. Open database\n"); printf("b. Get first record\n"); printf("c. Get next record \n"); printf("d. Get previous record\n"); printf("e. Get Nth record\n"); printf("f. Insert record\n"); printf("g. Bulk insert records in file\n"); printf("h. Delete record\n"); printf("i. Update record\n"); printf("j. Find record with first attribute value\n"); printf("k. Show catalog file\n"); printf("l. Get first page\n"); printf("m. Get next page\n"); printf("n. Show buf stats\n"); printf("o. Commit changes\n"); printf("p. Exit\n"); printf("Please press letter to choose an action.\n"); c = getchar(); fflush(stdin); char filename[100]; switch (c) { //a. Open database case 'a': { printf("Please input the database name (don't include extension): "); gets(filename); OpenStore(filename); break; } //b. Get first record case 'b': { Output(FirstRec()); break; } //c. Get next record case 'c': { Output(NextRec()); break; } //d. Get previous record case 'd': { Output(PriorRec()); break; } //e. Get Nth record case 'e': { printf("\nInput N: "); int n; scanf("%d", &n); Output(NRec(n)); fflush(stdin); break; } //f. Insert record case 'f': { printf("\nInput a comma-delimited Record:\n"); char statement[100]; scanf("%s", &statement); fflush(stdin); InsertRec(statement); break; } //g. Bulk insert records in file case 'g': { printf("\nInput the filename(include extension): "); char file[100]; scanf("%s", &file); InsertRecFromFile(file); fflush(stdin); break; } //h. Delete record case 'h': { DeleteRec(); break; } //i. Update record case 'i': { printf("\nInput a comma-delimited Record:\n"); char statement[100]; scanf("%s", &statement); fflush(stdin); UpdateRec(statement); break; } //j. Find record with first attribute value case 'j': { printf("\nInput value:\n"); char statement[100]; scanf("%s", &statement); fflush(stdin); SearchRec(statement); break; } //k. Show catalog file case 'k': { DisplyCatFile(filename); break; } //l. Get first page case 'l': { GetPage(0); break; } //m. Get next page case 'm': { GetPage(1); break; } //n. Show buf stats case 'n': { ShowBuf(); break; } //o. Commit changes case 'o': { Commit(filename); break; } //p. Exit case 'p': { CloseStore(filename); } }; }while(c != 'p'); return 0; }