int SpectralNorm(char *fname, float *sig, SAC_HD shd, int ithread) { int nk, flag_whiten; double dom; double dt = (double)shd.delta; int n = shd.npts; //double f1 = 1./100., f2 = 1./80., f3 = 1./4., f4 = 1./3.; double f2 = 1./perh, f1 = f2*0.8, f3 = 1./perl, f4 = f3*1.2; float *sigw = NULL; //if(strcmp(fwname,"0") != 0) { if(frechlen==-1.) { SAC_HD shdw; if( read_sac(fwname, &sigw, &shdw) == NULL ) { cerr<<"ERROR(SpectralNorm): Cannot open file "<<fwname<<endl; free(sig); exit(0); } if(shdw.npts!=shd.npts || fabs(shdw.delta-shd.delta)>1.e-3) { cerr<<"ERROR(SpectralNorm): Smoothing spectrum is incompatibale with input signals!"<<endl; exit(0); } } //float seis_out[n]; //int nf = (int)(log((double)n)/log(2.))+1; //if(nf<13) nf = 13; //nf = (int)pow(2,nf); float *outam=NULL, *outph=NULL; //[nf]; //memset (seis_outamp,0,nf*sizeof(float)); //memset (seis_outph,0,nf*sizeof(float)); //pthread_mutex_lock(&fftlock); //whiten_(&f1,&f2,&f3,&f4,&npow,&dt,&n,&frechlen,sig,sigw,seis_out,outam,outph,&ns,&dom,&flag_whiten); flag_whiten = Whiten( f1, f2, f3, f4, dt, n, frechlen, sig, sigw, &outam, &outph, &nk, &dom); //pthread_mutex_unlock(&fftlock); if(flag_whiten==0) reports[ithread].tail += sprintf(reports[ithread].tail, "*** Warning: Skipped due to probamatic spectrum. ***"); else { char nameamp[200], nameph[200]; sprintf(nameamp, "%s.am", fname); sprintf(nameph, "%s.ph", fname); shd.npts = nk; shd.delta = dom; shd.b = 0; shd.iftype = IXY; write_sac(nameamp,outam, &shd ); write_sac(nameph, outph, &shd ); } delete [] outam; outam = NULL; delete [] outph; outph = NULL; free(sig); free(sigw); return flag_whiten; }
/* * write_sac_xy * * Description: write binary SAC XY data * * IN: * const char *name : file name * SACHEAD hd : header * const float *xdata : float data array for X * const float *ydata : float data array for Y * * Return: * -1 : fail * 0 : succeed * */ int write_sac_xy(const char *name, SACHEAD hd, const float *xdata, const float *ydata) { float *ar; int npts; int error; size_t sz; npts = hd.npts; sz = (size_t)npts * SAC_DATA_SIZEOF; if ((ar = (float *)malloc(sz*2)) == NULL) { fprintf(stderr, "Error in allocating memory for file %s\n", name); return -1; } memcpy(ar, xdata, sz); memcpy(ar+npts, ydata, sz); /* needed for XY data */ hd.iftype = IXY; hd.leven = FALSE; error = write_sac(name, hd, ar); free(ar); return error; }
/*+++++++++++++++++++++++++++++++++++++++++*/ SHD->depmin = sig[0]; SHD->depmax = sig[0]; for ( i = 0; i < SHD->npts ; i++ ) { if ( SHD->depmin > sig[i] ) SHD->depmin = sig[i]; if ( SHD->depmax < sig[i] ) SHD->depmax = sig[i]; } fwrite(SHD,sizeof(SAC_HD),1,fsac); fwrite(sig,sizeof(float),(int)(SHD->npts),fsac); fclose (fsac); } #define SLEN 200000 /******************** define the max len of sig HERE!!**************/ int reverse( SAC_DB *sd, int ne, int ns ) { float sig0[SLEN],sig1[SLEN]; int i,leng; char exe[200],sacnm[300]; SAC_HD SAC_HEADER; if ( ne >= sd->nev ) return; if ( ns >= sd->nst ) return; if ( sd->rec[ne][ns].n <= 0 ) return; strcpy( sacnm, sd->rec[ne][ns].ft_fname); if(read_sac(sacnm,sig0,&SAC_HEADER,SLEN)== NULL) { fprintf(stderr,"file %s not found\n",sacnm); } leng = SAC_HEADER.npts; printf("cp %s %s_old\n\n",sacnm,sacnm); sprintf(exe,"/bin/cp %s %s_old",sacnm,sacnm); system(exe); for(i=0;i<leng;i++) { sig1[i]=sig0[i]*(-1); } write_sac(sacnm,&sig1[0],&SAC_HEADER); return(1); }
int main (int argc, char *argv[]) { char fout[80]; SACHEAD hd; float *data; float delta; int npts; float b; int i; delta = 0.01; // 采样周期 npts = 1000; // 数据点数 b = 10; // 文件开始时间 hd = sachdr(delta, npts, b); // 构建基本头段 hd.dist = 10; // 给其它头段变量赋值 hd.cmpaz = 0.0; hd.cmpinc = 0.0; strcpy(fout, "seis.syn"); // 生成合成数据 data = (float *)malloc(sizeof(float)*npts); for (i=0; i<npts; i++) { data[i] = i; } // 写入到文件中 write_sac(fout, hd, data); free(data); return 0; }
/* for fortran--write evenly-spaced data with comp orientation */ void wrtsac3_(const char *name, float dt, int ns, float b0, float dist, float cmpaz, float cmpinc, const float *ar) { SACHEAD hd; hd = sachdr(dt,ns,b0); hd.dist = dist; hd.cmpaz=cmpaz; hd.cmpinc=cmpinc; write_sac(name, hd, ar); }
int main (int argc, char *argv[]) { if(argc != 4) { printf("Usage: rm_instrument_response in_SAC_file TF_file out_SAC_name\n"); exit(-1); } static int n,nf; static double dt; static float sei[10480576]; // static int fMax=2000; int i,ii; static double freq[fMax], phase_res[fMax], amp_res[fMax],coh[fMax]; double temp,pi,degtorad; pi=4*atan(1.0); degtorad=pi/180; FILE *f_ph, *f_am; if((f_ph = fopen(argv[2], "r"))==NULL) { fprintf(stderr,"%s file not found\n",argv[2]); exit(1); } // if((f_am = fopen(argv[3], "r"))==NULL) { // fprintf(stderr,"%s file not found\n",argv[3]); //exit(1); //} for(i=0;i<fMax;i++) { if(fscanf(f_ph,"%lf %lf %lf %lf",&freq[i],&coh[i],&_res[i],&phase_res[i])==EOF) break; // phase_res[i]*=degtorad; //amp_res[i]*=0.000000001; // fprintf(stderr,"%lf %lf %lf\n",freq[i],phase_res[i],amp_res[i]); } fclose(f_ph); //fclose(f_am); nf=i; if(freq[0]>0.001||freq[i-1]<0.25) { fprintf(stderr,"frequency does not cover 0.001 and 0.25 Hz!!\n"); return 1; } SAC_HD shd; read_sac(argv[1],sei, &shd, 10480576 ); n=shd.npts; dt=shd.delta; //for(ii=0;ii<1000;ii++) //{ // sei[ii]*=cos(pi*(1000.0-ii)/1000.0/2.0); // sei[n-ii]*=cos(pi*(1000.0-ii)/1000.0/2.0); //} rmresponse_(&n,&dt,sei,freq,phase_res,amp_res,&nf); write_sac (argv[3], sei, &shd ); return 0; }
/*--------------------------------------------------------------------------*/ void one_rec_cut(SAC_DB *sd, int ne, int ns, float t1, float n) /*--------------------------------------------------------------------------*/ { float sig1[2000000]; double t1b, t1e, t2b, t2e, t2; int n1, n2; char ft_name[100]; SAC_HD shd1; t2 = t1 + (n-1)*sd->rec[ne][ns].dt; t1b = sd->rec[ne][ns].t0 - sd->ev[ne].t0; t1e = t1b + (sd->rec[ne][ns].n-1)*sd->rec[ne][ns].dt; fprintf(stderr,"%g %g %g\n",t1b,sd->rec[ne][ns].n,sd->rec[ne][ns].dt); fprintf(stderr,"t1 %lg t2 %lg t1b %lg t1e %lg\n", t1, t2, t1b, t1e); if ( (t1b>t1) || (t1e<t2) ) { fprintf(stderr,"t1b: %g t1: %g t1e:%g t2:%g\n",t1b,t1,t1e,t2); fprintf(stderr,"%s %g\n",sd->rec[ne][ns].fname, (sd->rec[ne][ns].n-1)*sd->rec[ne][ns].dt); fprintf(stderr,"incompatible time limits for station %s and event %s\n", sd->st[ns].name, sd->ev[ne].name ); //abort(); return; } if ( !read_sac ("s1.sac", sig1, &shd1, 2000000 ) ) { fprintf(stderr,"file %s not found\n", sd->rec[ne][ns].fname ); return; } n1 = (int)((t1-t1b)/sd->rec[ne][ns].dt); shd1.npts = n; shd1.nzyear = 2000; shd1.nzjday = 1; shd1.nzhour = 0; shd1.nzmin = 0; shd1.nzsec = 0; shd1.nzmsec = 0; shd1.b = 0.; strcpy(ft_name, sd->rec[ne][ns].ft_fname); write_sac (ft_name, &(sig1[n1]), &shd1 ); system("/bin/rm s1.sac"); }
int main(int argc, char *argv[]) { if(argc != 7) { std::cerr<<"Usage: "<<argv[0]<<" [sac file] [f1 (-1=gaussian)] [f2 (-1=lowpass fcenter=gaussian)] [f3 (frec halflength=gaussian)] [f4 (-1=gaussian)] [out_sacname]"<<std::endl; exit(-1); } SAC_HD shd; float *sig=NULL; if (read_sac(argv[1], &sig, &shd)==NULL) return 0; double f1 = atof(argv[2]), f2 = atof(argv[3]), f3 = atof(argv[4]), f4 = atof(argv[5]); Filter(f1, f2, f3, f4, (double)shd.delta, shd.npts, sig, sig); //char outname[300]; //sprintf(outname, "%s_ft", argv[1]); write_sac(argv[6], sig, &shd); return 0; }
int wrtsac2(const char *name, int n, const float *x, const float *y ) { SACHEAD hd = sac_null; float *ar; unsigned sz; int exit_code; hd.npts = n; hd.iftype = IXY; hd.leven = FALSE; sz = n*sizeof(float); if ( (ar = (float *) malloc(2*sz)) == NULL ) { fprintf(stderr, "error in allocating memory%s\n",name); return -1; } if (memcpy(ar, x, sz) == NULL) { fprintf(stderr, "error in copying data %s\n",name); free(ar); return -1; } if (memcpy(ar+sz, y, sz) == NULL) { fprintf(stderr, "error in copying data %s\n",name); free(ar); return -1; } exit_code = write_sac(name, hd, ar); free(ar); return exit_code; }
int wrtsac0(const char *name, float dt, int ns, float b0, float dist, const float *ar ) { SACHEAD hd = sac_null; hd.npts = ns; hd.delta = dt; hd.dist = dist; hd.b = b0; hd.o = 0.; hd.e = b0+(hd.npts-1)*hd.delta; hd.iztype = IO; hd.iftype = ITIME; hd.leven = TRUE; return write_sac(name, hd, ar); }
/*--------------------------------------------------------------------------*/ void one_rec_cut(SAC_DB *sd, int ne, int ns, float t1, float n) /*--------------------------------------------------------------------------*/ { float sig1[7500000],sig2[7500000],sig3[7500000],sig4[7500000]; double t1b, t1e, t2b, t2e, t2; int n1, n2,i,j,kk,npts_real,flag=0; char ft_name[500],ft_name_bit[500],ft_name_cut[500],str[300],tempstnm[10]; SAC_HD shd1,shd2,shd3; FILE *script; if ( ne >= sd->nev ) return; if ( ns >= sd->nst ) return; if ( sd->rec[ne][ns].n <= 0 ) return; fprintf(stderr,"t1 n %g %g\n",t1,n); printf("ne ns [begin from 0] %d %d\n",ne,ns); t2 = t1 + (n-1)*sd->rec[ne][ns].dt; //printf("n: %g rec.dt: %g\n",n,sd->rec[ne][ns].dt); //printf("rec.t0: %g\n",sd->rec[ne][ns].t0); //printf("ev.t0: %g\n",sd->ev[ne].t0); //printf("rec.n: %g\n",sd->rec[ne][ns].n); //printf("rec.dt: %g\n",sd->rec[ne][ns].dt); t1b = sd->rec[ne][ns].t0 - sd->ev[ne].t0; t1e = t1b + (sd->rec[ne][ns].n-1)*sd->rec[ne][ns].dt; /* sprintf(str,"/bin/cp %s s1.sac", sd->rec[ne][ns].fname ); fprintf(stderr,"/bin/cp %s s1.sac\n", sd->rec[ne][ns].fname ); system(str); */ //strcpy(ft_name, sd->rec[ne][ns].ft_fname); /////++++++ If ONLY do the cut not need the trans, use this sentence. Otherwise, delete it // sprintf(str,"/bin/cp %s s1.sac", sd->rec[ne][ns].fname ); // system(str); ///////++++++ strcpy(ft_name, sd->rec[ne][ns].ft_fname); fprintf(stderr,"t1 %lg t2 %lg t1b %lg t1e %lg\n", t1, t2, t1b, t1e); // sprintf(ft_name_bit, "%s_bit",ft_name); fprintf(stderr,"t1 %lg t2 %lg t1b %lg t1e %lg\n", t1, t2, t1b, t1e); if ( !read_sac ("s1.sac", sig1, &shd1, 7500000 ) ) { fprintf(stderr,"file %s not found\n", sd->rec[ne][ns].fname ); return; } if ( !read_sac ("s1.sac", sig1, &shd2, 7500000 ) ) { fprintf(stderr,"file %s not found\n", sd->rec[ne][ns].fname ); return; } if ( !read_sac ("s1.sac", sig1, &shd3, 7500000 ) ) { fprintf(stderr,"file %s not found\n", sd->rec[ne][ns].fname ); return; } // sig2[]contain the seimic waveform, if no signal(i.e. there is a range in sig2[] that is out of that of sig1[]), we put 1 there. sig3[], the bit file, if sig2[]has signal, sig3[]=1, else sig3[]=0 //sig4 contain only the real signals, they are attached together without any manmade sig, but the time of the sig4 is not true, the length is shorter is there was manmade sig //ini sig3 == 0->1 changed! ; sig2 == 1; for(i=0;i<n;i++) sig3[i]=1.0; if ( (t1b>t1) || (t1e<t2) ) { printf("\n t1b>t1 OR t1e<t2: not all contained\n"); // printf("here1!!!\n"); for(i=0;i<n;i++) sig2[i]=1; if( (t1b>t1) ) { n1 = (int)((t1b-t1)/sd->rec[ne][ns].dt); kk=0; for (i=0; i<shd1.npts; i++) { if (n1+i<n) sig2[n1+i] = sig1[i]; } for(i=0;i< n;i++) { // sig2[n1+i] =sig1[i]; if(fabs(sig2[i+1]-sig2[i])<0.00000001) { if(flag==0)fprintf(stderr,"==============here manmade sig!==============\n"); flag=flag+1; sig3[i]=0.0; sig3[i+1]=0.0; // continue; // in this case, sig2 and sig3 will keep the initialized values, but sig4 won't keep any } // sig2[n1+i] =sig1[i]; // sig3[n1+i] = 1; if(fabs(sig3[i]-1.0)<0.1)// its still within the day we need { sig4[kk]=sig2[i]; kk++;} } // for i npts_real=kk; // abort(); } else { // printf("here2!!!\n"); n1 = (int)((t1-t1b)/sd->rec[ne][ns].dt); kk=0; for (i=n1;i<shd1.npts;i++) sig2[i-n1] = sig1[i]; for(i=0;i< n;i++) { // fprintf(stderr,"here i =%d\n",i); if(fabs(sig2[i+1]-sig2[i])<0.00000001) { if(flag==0)fprintf(stderr,"==========here manmade sig!==============\n"); flag= flag+1; sig3[i]=0.0; sig3[i+1]=0.0; // continue; // see before } // sig2[i-n1] =sig1[i]; // sig3[i-n1] = 1; if(fabs(sig3[i]-1.0)<0.000001) { sig4[kk]=sig2[i]; kk++;} } //for i npts_real=kk; } shd2.npts = n;//here cut the singnal into the length we need !!! shd2.nzyear = sd->ev[ne].yy; shd2.nzjday = sd->ev[ne].jday; shd2.nzhour = 0; shd2.nzmin = 0; shd2.nzsec = 0; shd2.nzmsec = 0; shd2.b = t1; /* //get rid of the space after shd.kstnm int mm; for ( mm = 0; mm < 6; mm++ ) { if ( shd2.kstnm[mm] == ' ' ) break; else tempstnm[mm] = shd2.kstnm[mm]; } tempstnm[mm] = '\0'; //mkdir for SAC file DATAROOT/NTW/STN/... if((script=fopen("make_Dir.csh","w"))==NULL) {fprintf(stderr,"cannot creat file make_Dir\n"); exit(1);} fprintf(script,"if (! -e %s/%s/%s) then\n mkdir %s/%s/%s\n endif\n",DATAROOT,shd2.knetwk,tempstnm,DATAROOT,shd2.knetwk,tempstnm); fclose(script); /// system("csh make_Dir.csh"); fprintf(stderr,"mkdir %s/%s/%s\n",DATAROOT,shd2.knetwk,tempstnm); sprintf(ft_name,"%s/%s/%s/%s.%s.00.%s.%d.%d",DATAROOT,shd2.knetwk,tempstnm,tempstnm,shd2.knetwk,shd2.kcmpnm,shd2.nzyear,shd2.nzjday); //fprintf(stderr,"here is the name:%s\n",ft_name); fprintf(stderr,"write SAC:%s\n\n",ft_name); // sprintf(ft_name_bit, "%s_bit",ft_name); sprintf(ft_name_cut,"%s_cut",ft_name); printf("n=%g n4=%d\n",n,npts_real); /// write_sac (ft_name_cut, &(sig2[0]), &shd2 ); // write_sac (ft_name_bit, &(sig3[0]), &shd2 ); write_sac(ft_name,&(sig2[0]),&shd2); /// shd2.npts = npts_real; /// write_sac(ft_name,&(sig4[0]),&shd2); */ //++++ write_sac (ft_name, &(sig2[0]), &shd2 ); // write_sac (ft_name_bit, &(sig3[0]), &shd2 ); system("/bin/rm s1.sac"); } else { // the merged signal have the TOTAL signal for the day we need. printf("\n else: all contained \n"); n1 = (int)((t1-t1b)/sd->rec[ne][ns].dt); kk=0; for(i=0;i<n;i++) { sig3[i] = 1; if(fabs(sig1[i+1+n1]-sig1[i+n1])<0.00000001) { if(flag==0) {fprintf(stderr,"==========here manmade sig!==============\n");} flag=flag+1; sig3[i]=0.0; sig3[i+1]=0.0; // continue; } // sig3[i] = 1; if(fabs(sig3[i]-1.0)<0.1) { sig4[kk]=sig1[i+n1]; kk++; } } npts_real=kk; printf("n=%g n4=%d\n",n,npts_real); // abort(); // for(i = 0;i<n;i++) sig3[i] = 1; //the original definition for sig3. can't figure the manmade sig in sig1 // printf("finish initialize sig3\n"); shd1.npts = n; shd1.nzyear = sd->ev[ne].yy; shd1.nzjday = sd->ev[ne].jday; shd1.nzhour = 0; shd1.nzmin = 0; shd1.nzsec = 0; shd1.nzmsec = 0; shd1.b = t1; /* int mm; for ( mm = 0; mm < 4; mm++ ) { if ( shd1.kstnm[mm] == ' ' ) break; else tempstnm[mm] = shd1.kstnm[mm]; } tempstnm[mm] = '\0'; if((script=fopen("make_Dir.csh","w"))==NULL) {fprintf(stderr,"cannot creat file make_Dir\n"); exit(1);} fprintf(script,"if (! -e %s/%s/%s) then\n mkdir %s/%s/%s\n endif\n",DATAROOT,shd1.knetwk,tempstnm,DATAROOT,shd1.knetwk,tempstnm); fclose(script); system("csh make_Dir.csh"); system ("/bin/rm make_Dir.csh"); // fprintf(stderr,"mkdir %s/%s/%s\n",DATAROOT,shd1.knetwk,tempstnm); sprintf(ft_name,"%s/%s/%s/%s.%s.00.%s.%d.%d",DATAROOT,shd1.knetwk,tempstnm,tempstnm,shd1.knetwk,shd1.kcmpnm,shd1.nzyear,shd1.nzjday); fprintf(stderr,"write SAC:%s\n\n",ft_name); // sprintf(ft_name_bit, "%s_bit",ft_name); sprintf(ft_name_cut,"%s_cut",ft_name); write_sac (ft_name_cut, &(sig1[n1]), &shd1 ); // write_sac (ft_name_bit, &(sig3[0]), &shd1 ); /// write_sac(ft_name,&(sig1[n1]),&shd1); shd1.npts = npts_real; write_sac(ft_name,&(sig4[0]),&shd1); */ //++++ write_sac (ft_name, &(sig1[n1]), &shd1 ); // write_sac (ft_name_bit, &(sig3[0]), &shd1 ); system("/bin/rm s1.sac"); } }
int main(int argc,char *argv[]) { int n,m,i=0,j,k,id; SacTrace *st; int p=0,cn,shift; float cd,delta,delay,*ut,add,tmp,b,e; char *cst; char *path,*output,wf[128]; /*variables related with intel ipp */ if (argc !=5 ){ printf("usage: slant path cstation slowness output.\n"); exit(0); } else{ path=argv[1]; cst=argv[2]; p=atof(argv[3]); output=argv[4]; } /*decide method of read data*/ ftype(path,&id); switch(id){ case 0: st=readsacdir(path,&n,&m); break; case 1: st=readflist(path,&n,&m); break; default : printf("Fail to read data.") ; exit(EXIT_FAILURE);break; } /*obtain the staking centeral station */ while(strcmp(cst,st[i].hd.kstnm)) i++; cn=i; ut=(float *)malloc(m*sizeof(float)); memset(ut,0,m*sizeof(float)); delta=st[cn].hd.delta; cd=st[cn].hd.gcarc; b=st[cn].hd.b; e=st[cn].hd.e; /*perform the shift and stack */ for (i=0;i<n;i++) { delay=(cd-st[i].hd.gcarc)*p; tmp=(delay/delta); shift=(int)tmp; for (j=0;j<m;j++) { if (j>=shift && (j-shift)<m) { add=st[i].data[j-shift]; } else add=0; ut[j]+=add; } } for (k=0;k<m;k++) ut[k]=ut[k]/n; write_sac(output,st[cn].hd,ut); /*output maxtrix dimension */ printf("%d %d\n",n,m); free(st); free(ut); return 0; }
int main (int argc, char *argv[]) { static int n, ns,npow; static double f1, f2, f3, f4, dt,dom; static float seis_in[NSIG*2],seis_out[NSIG*2]; static float seis_outamp[Nf],seis_outph[Nf]; double t1,t2,t3,t4; char name[160], name1[160],name_flt[160]; char nameamp[160],nameph[160]; FILE *in, *ff; int i, j, nn, ntest, half_l=40, window_b, window_e; float sig_window[NSIG], window_sum[NSIG]; if( argc != 2) { printf("Usage: whiten_phamp parameter_file\n"); exit(-1); } // open and read parameter file param.dat if((in = fopen(argv[1],"r")) == NULL) { printf("Can not find file %s.\n",argv[1]); exit(1); } while((nn = fscanf(in,"%lf %lf %lf %lf %lf %d %s %s",&t1,&t2,&t3,&t4, &dt,&npow,name,name_flt)) != EOF) { // start main loop if(nn == 0 || nn != 8) break; printf("Corners periods. Low: %f - %f, High: %f - %f\n",t1, t2, t3, t4); printf("Step: %f, Cosine power: %d\n",dt, npow); // remove quotes from name j = 0; for(i = 0; i < strlen(name); i++) { if(name[i] == '\'' || name[i] == '\"') continue; name[j] = name[i]; j++; } name[j] = '\0'; // read in seis sig if ( !read_sac(name, sig, &shd1, NSIG ) ) { fprintf(stderr,"file %s did not found\n", name ); continue; } n = shd1.npts; dt = shd1.delta; ntest=0; for( i =0; i< n; ) { if(fabs(sig[i])<1e-20) ntest+=1; i=i+100/dt; } if(ntest > 600) { printf("%s: Signal time length not long enough.\n",name); continue; } if ( !read_sac(name_flt, sig_window, &shd1, NSIG ) ) { fprintf(stderr,"file %s did not found\n", name_flt ); continue; } // do running average using sac before whitening for(i=0;i<n;i++){ window_sum[i]=0; window_b=i-half_l; if(window_b<0)window_b=0; window_e=i+half_l; if(window_e>n)window_e=n; for(j=window_b;j<window_e;j++)window_sum[i]+=fabs(sig_window[j]); window_sum[i]/=(window_e-window_b); } for(i=0;i<n;i++) sig[i]=sig[i]/window_sum[i]; /* ff = fopen("sac_one_cor","w"); // fprintf(ff,"sac << END\n"); // fprintf(ff,"/home/nshapiro/PROGS/SAC/bin/sac2000 << END\n"); fprintf(ff,"sac << END\n"); fprintf(ff,"r %s\n", name); ********************* remove earth_quake HERE!!***************** // fprintf(ff,"bp c 0.02 0.0667 n 4 p 2\n", name); fprintf(ff,"abs\n"); fprintf(ff,"smooth mean h 40\n"); fprintf(ff,"w a.avg \n"); fprintf(ff,"r /utera/tianye/data_check_sampling/one.SAC\n"); fprintf(ff,"divf a.avg\n"); fprintf(ff,"w %s_Sampling\n",name); fprintf(ff,"r %s\n",name); fprintf(ff,"divf a.avg\n"); // fprintf(ff,"mulf %s_bit\n",name); fprintf(ff,"w smooth.sac\n"); fprintf(ff,"quit\n"); fprintf(ff,"END\n"); fclose(ff); system("csh sac_one_cor"); // end of running average if ( !read_sac("smooth.sac", sig, &shd1, 3500000 ) ) { fprintf(stderr,"file %s did not found\n", name1 ); return 0; } n = shd1.npts; dt = shd1.delta; */ for( i =0; i< n; i++) { seis_in[i] = sig[i]; // printf(" seis_in1 %d %f\n", i,sig[i]); } printf(" Dt1= %f, Nsamples1= %d\n",dt, n); f1 = 1.0/t1; f2 = 1.0/t2; f3 = 1.0/t3; f4 = 1.0/t4; printf("Check !!! %f %f %f %f %d %f %d\n",f1,f2,f3,f4,npow,dt,n); filter4_(&f1,&f2,&f3,&f4,&npow,&dt,&n,seis_in,seis_out,seis_outamp,seis_outph,&ns,&dom); shd1.npts = n; shd1.delta = dt; write_sac(name,seis_out, &shd1); strcpy(nameamp,name); strcpy(nameph,name); strcat(nameamp,".am"); strcat(nameph, ".ph"); shd1.npts = ns/2 + 1; shd1.delta = dom; shd1.b = 0; shd1.iftype = IXY; write_sac(nameamp,seis_outamp, &shd1 ); write_sac(nameph, seis_outph, &shd1 ); } return 0; }
/* for fortran--write evenly-spaced data */ void wrtsac0_(const char *name, float *dt, int *ns, float *b0, float *dist, const float *ar) { SACHEAD hd; hd = sachdr(*dt,*ns,*b0); hd.dist = *dist; write_sac(name, hd, ar); }
int main (int argc, char *argv[]) { static int n, ns,npow; static double f1, f2, f3, f4, dt,dom; static float seis_in[NSIG*2],seis_out[NSIG*2]; static float seis_outamp[Nf],seis_outph[Nf],rec_am[Nf]; int rec_b[1000],rec_e[1000]; double t1,t2,t3,t4; char name[160], name1[160],name_flt[160]; char nameamp[160],nameph[160],namesamp[160]; FILE *in, *ff, *fsm; int i, ii, j, nn, ntest, rec_i, s1k, flag; float sig_window[NSIG],sig1[NSIG],window_sum[NSIG]; double win_max[NSIG/1000+1],win_min,window_avg,window_std; int window_b,window_e; int half_l=(int)(40/1.); if( argc != 2) { printf("Usage: whiten_phamp parameter_file\n"); exit(-1); } // open and read parameter file param.dat if((in = fopen(argv[1],"r")) == NULL) { printf("Can not find file %s.\n",argv[1]); exit(1); } while((nn = fscanf(in,"%lf %lf %lf %lf %lf %d %s %s",&t1,&t2,&t3,&t4, &dt,&npow,name,name_flt)) != EOF) { // start main loop if(nn == 0 || nn != 8) break; printf("Whiten: Corners periods. Low: %f - %f, High: %f - %f\n",t1, t2, t3, t4); // printf("Step: %f, Cosine power: %d\n",dt, npow); // remove quotes from name j = 0; for(i = 0; i < strlen(name); i++) { if(name[i] == '\'' || name[i] == '\"') continue; name[j] = name[i]; j++; } name[j] = '\0'; if ( !read_sac(name, sig, &shd1, NSIG ) ) { fprintf(stderr,"file %s did not found\n", name ); continue; } n = shd1.npts; dt = shd1.delta; ntest=0; for( i =0; i< n; ) { if(fabs(sig[i])<1e-20) ntest+=1; i=i+100/dt; } // printf("ntest: %d\n",ntest); if(ntest > 600) { printf("%s: Signal time length not long enough.\n",name); continue; } if ( !read_sac(name_flt, sig_window, &shd1, NSIG ) ) { fprintf(stderr,"file %s did not found\n", name_flt ); continue; } // do running average /* for(i=0;i<n;i++){ window_sum[i]=0; window_b=i-half_l; if(window_b<0)window_b=0; window_e=i+half_l; if(window_e>n)window_e=n; for(j=window_b;j<window_e;j++)window_sum[i]+=fabs(sig_window[j]); window_sum[i]/=(window_e-window_b); } for(i=0;i<n;i++) sig[i]=sig[i]/window_sum[i]; */ // calculate noise level for( i =0; i< n; i++) sig_window[i] = fabs(sig_window[i]); s1k=(int)(1000./dt); for( i =0; i<= n-s1k; i+=s1k){ win_max[(int)(i/s1k)]=0; for( ii=i; ii<i+s1k; ii++ ) if(win_max[(int)(i/s1k)]<sig_window[ii]) win_max[(int)(i/s1k)]=sig_window[ii]; } flag=0; if(i<n)flag=1; for( ii=i;ii<n;ii++ ) if(win_max[(int)(i/s1k)]<sig_window[ii]) win_max[(int)(i/s1k)]=sig_window[ii]; window_avg=0;ii=0; win_min = 1e20; for( i =0; i< (int)(n/s1k); i++) if(win_max[i]>1e-20 && win_min>win_max[i])win_min=win_max[i]; for( i =0; i< (int)(n/s1k); i++){ if(win_max[i]>win_min*2.0 || win_max[i]<1e-20) continue; //for(j=i*s1k;j<(i+1)*s1k;j++)sig_temp[j]=1; window_avg+=win_max[i]; ii+=1; } //sprintf(namesamp,"%s_temp",name); //write_sac(namesamp,sig_temp, &shd1); if( ii < 20 ) { printf("%s: Time length not enough after throw away earthquakes.\n",name); continue; } window_avg=window_avg/ii; window_std=0; for( i =0; i< (int)(n/s1k); i++){ if(win_max[i]>win_min*2.0 || win_max[i]<1e-20)continue; window_std+=(window_avg-win_max[i])*(window_avg-win_max[i]); } window_std=sqrt(window_std/(ii-1)); printf("avg: %g std: %g\n",window_avg,window_std); ii=0; for( i =0; i < (int)(n/s1k); i++){ if(win_max[i]>window_avg+2.0*window_std || win_max[i]<1e-20){ for ( j=0; j<1000; j++ ) sig[i*s1k+j]=0; sig_window[i]=0; } else sig_window[i]=1; ii+=1; } //printf("The last win_max: %g",win_max[i]); if(win_max[i]>window_avg+2.0*window_std) { for ( j=i*s1k; j<n; j++ ) sig[j]=0; sig_window[i]=0; } else sig_window[i]=1; if( ii < 20 ) { printf("%s: Time length not enough after throw away earthquakes.\n",name); continue; } rec_i=0; rec_b[0]=0; for( i=1; i<(int)(n/s1k)+flag;){ if(sig_window[i]-sig_window[i-1]==1) rec_b[rec_i]=i*s1k; else if(sig_window[i]-sig_window[i-1]==-1) { rec_e[rec_i]=i*s1k; if ((rec_e[rec_i]-rec_b[rec_i])<2500/dt) for(ii=rec_b[rec_i];ii<rec_e[rec_i];ii++) sig[ii]=0; else rec_i++; } i++; } if(sig_window[i-1]==1) { rec_e[rec_i]=n; if ((rec_e[rec_i]-rec_b[rec_i])<1500/dt) for(ii=rec_b[rec_i];ii<rec_e[rec_i];ii++) sig[ii]=0; else rec_i++; } for(i=0;i<rec_i;i++) { if(rec_b[i]!=0){ rec_b[i]+=300; for(ii=rec_b[i]-300;ii<rec_b[i];ii++) sig[ii]=0; } if(rec_e[i]!=n){ rec_e[i]-=300; for(ii=rec_e[i]+1;ii<=rec_e[i]+300;ii++) sig[ii]=0; } } // sprintf(namesamp,"%s_water",name); // write_sac(namesamp,sig, &shd1); sprintf(namesamp,"%s_rec",name); if((ff = fopen(namesamp,"w")) == NULL) { printf("Can not open file %s to write\n",namesamp); exit(1); } for(i=0;i<rec_i;i++) fprintf(ff, "%d %d\n", rec_b[i], rec_e[i]); fclose(ff); // write_sac(namesamp,sig_window, &shd1); // output am and ph files for( i =0; i< n; i++) { seis_in[i] = sig[i]; // printf(" seis_in1 %d %f\n", i,sig[i]); } printf(" Dt1= %f, Nsamples1= %d\n",dt, n); f1 = 1.0/t1; f2 = 1.0/t2; f3 = 1.0/t3; f4 = 1.0/t4; //printf("Check !!! %f %f %f %f %d %f %d\n",f1,f2,f3,f4,npow,dt,n); filter4_(&f1,&f2,&f3,&f4,&npow,&dt,&n,seis_in,rec_am,seis_outamp,seis_outph,&ns,&dom); shd1.npts = n; shd1.delta = dt; // write_sac(name,seis_out, &shd1); strcpy(nameamp,name); strcpy(nameph,name); strcat(nameamp,".am"); strcat(nameph, ".ph"); shd1.npts = ns/2+1; shd1.delta = dom; shd1.b = 0; shd1.iftype = IXY; write_sac( nameamp, seis_outamp, &shd1 ); write_sac( nameph, seis_outph, &shd1 ); strcpy(nameamp,name); strcat(nameamp,".recam"); write_sac( nameamp, rec_am, &shd1 ); /* // output rec.am and rec.ph files for( i =0; i< n; i++) { seis_in[i] = sig_window[i]; } fft_phamp_(&dt,&n,seis_in,seis_outamp,seis_outph,&ns,&dom); shd1.npts = n; shd1.delta = dt; write_sac(name,seis_out, &shd1); strcpy(nameamp,name); strcpy(nameph,name); strcat(nameamp,"_rec.am"); strcat(nameph, "_rec.ph"); shd1.npts = ns/2+1; shd1.delta = dom; shd1.b = 0; shd1.iftype = IXY; write_sac(nameamp,seis_outamp, &shd1 ); write_sac(nameph, seis_outph, &shd1 ); */ } return 0; }