/*+++++++++++++++++++++++++++++++++++++++++*/ 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 TemperalNorm( char *fname, float **sig, SAC_HD *shd, int ithread) { *sig = NULL; if( read_sac(fname, sig, shd) == NULL ) { reports[ithread].tail += sprintf(reports[ithread].tail, "*** Warning: Cannot open file %s ***", fname); return 0; } char recname[300]; sprintf(recname,"%s_rec",fname); if( tnorm_flag==1 ) OneBit(*sig, shd); else if( tnorm_flag==2 ) RunAvg(*sig, shd); else if( tnorm_flag==3 || tnorm_flag==4 ) { if(!EqkCut(*sig, shd, recname, ithread)) return 0; } else if( tnorm_flag!=0 ) { cerr<<"ERROR(TemperalNorm): Undefined normalization method!"<<endl; exit(0); } /* //check norm results std::string outname( (shd->kstnm) ); outname.replace(outname.begin()+4, outname.end(), "_check_eqkcut.SAC"); write_sac(outname.c_str(), *sig, shd); std::cerr<<outname<<std::endl; exit(0); */ return 1; }
int main(int argc, char *argv[]) { FILE *ff; char *month_name[100]; int day_n,month,i,j; int month_day[12]; float sig1[SLEN],sig2[SLEN]; SAC_HD tysac1,tysac2; if(argc != 3) { printf("Usage: Calc_cor_time rec_file_1 rec_file_2\n"); exit(-1); } // if((ff = fopen(argv[1], "r"))==NULL) // sprintf(fname1,"%s",argv[1]); if ( read_sac (argv[1], sig1, &tysac1, SLEN) == NULL ) { fprintf(stderr,"file %s not found\n", argv[1]); return 0; } if ( read_sac (argv[2], sig2, &tysac2, SLEN) == NULL ) { fprintf(stderr,"file %s not found\n", argv[2]); return 0; } i=0; month_day[0]=31;month_name[0]="JAN"; month_day[1]=29;month_name[1]="FEB"; month_day[2]=31;month_name[2]="MAR"; month_day[3]=30;month_name[3]="APR"; month_day[4]=31;month_name[4]="MAY"; month_day[5]=30;month_name[5]="JUN"; month_day[6]=31;month_name[6]="JUL"; month_day[7]=31;month_name[7]="AUG"; month_day[8]=30;month_name[8]="SEP"; month_day[9]=31;month_name[9]="OCT"; month_day[10]=30;month_name[10]="NOV"; month_day[11]=31;month_name[11]="DEC"; for(month=0;month<12;month++){ day_n=0; for(j=1;j<=month_day[month];j++){ if(sig1[i+j]*sig2[i+j]>0)day_n++; // printf("%d\n",day_n); } i+=month_day[month]; printf("month: %s num_of_days: %d\n",month_name[month],day_n); } // day_n=tysac.npts; }
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; }
/*for fortran--read evenly-spaced data */ void rdsac0_(const char *name, float *dt, int *ns, float *b0, float *ar) { int i; SACHEAD hd; float *temp; temp = read_sac(name,&hd); *dt = hd.delta; *ns = hd.npts; *b0 = hd.b; for(i=0; i<*ns; i++) ar[i]=temp[i]; free(temp); }
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; }
/*--------------------------------------------------------------------------*/ 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; }
/* * read_sac_xy * * Description: read SAC XY binary file * * IN: * const char *name : file name * OUT: * SACHEAD *hd : SAC head to be filled * float *xdata : pointer for X * float *ydata : pointer for Y * * Return: 0 for success, -1 for fail * */ int read_sac_xy(const char *name, SACHEAD *hd, float *xdata, float *ydata) { float *data; size_t npts; if ((data = read_sac(name, hd)) == NULL) return -1; npts = (size_t)hd->npts; if ((xdata = (float *)malloc(npts*SAC_DATA_SIZEOF)) == NULL) { fprintf(stderr, "Error in allocating memory for %s\n", name); return -1; } if ((ydata = (float *)malloc(npts*SAC_DATA_SIZEOF)) == NULL) { fprintf(stderr, "Error in allocating memory for %s\n", name); return -1; } memcpy(xdata, data , npts*SAC_DATA_SIZEOF); memcpy(ydata, data+npts, npts*SAC_DATA_SIZEOF); free(data); return 0; }
/*--------------------------------------------------------------------------*/ 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[]) { 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; }
/*------------------------------------------------------------------------*/ void mk_one_rec (SAC_DB *sdb, int ne, int ns, char *nseed, char *ch, char *DATAROOT) /*------------------------------------------------------------------------*/ { FILE *ff; static char resp_name[150]; char seisname,ch2[4]; char filename[300]; double t1; if ( sdb->rec[ne][ns].n > 0 ) return; sprintf(filename, "%s/%s.%s.SAC", sdb->ev[ne].name, sdb->st[ns].name, ch); if (ch[2]=='Z') sprintf(ch2,"BHZ"); if (ch[2]=='N') sprintf(ch2,"BHN"); if (ch[2]=='E') sprintf(ch2,"BHE"); if ( access(filename,F_OK) != 0) return; printf("filename %s iev %d ista %d\n",filename, ne,ns); if ( !read_sac (filename, sig1, &sd, NPTSMAX) ) { fprintf(stderr,"file %s not found\n", filename ); } printf(" sd.nzyear %d,sd.nzjday %d,sd.nzhour %d ,sd.nzmin %d,sd.nzsec %d,sd.nzmsec %d\n", sd.nzyear, sd.nzjday, sd.nzhour, sd.nzmin, sd.nzsec, sd.nzmsec); t1 = abs_time(sd.nzyear,sd.nzjday,sd.nzhour,sd.nzmin,sd.nzsec,sd.nzmsec); sdb->rec[ne][ns].t0 = t1; sdb->rec[ne][ns].dt = sd.delta; sdb->rec[ne][ns].n = sd.npts; /*---------- response file -----------*/ // sprintf(str,"ls %s/RESP*%s* > list_resp\0", sdb->ev[ne].name, sdb->st[ns].name); //here we change the polezero file to RESP file // sprintf(str,"ls ~/work/noise/northeast/PZs/SAC_%s_%s_PZs > list_resp\0", sdb->st[ns].name,ch2); // sprintf(str,"ls /home/zheng/work/noise/northeast/response_CH/RESP.%s.00.%s > list_resp\0", sdb->st[ns].name,ch2); // sprintf(str,"ls /mtera/weisen/for_yong/response_CH/RESP.%s.00.%s > list_resp\0", sdb->st[ns].name,ch2); sprintf(str,"ls /Users/jiayixie/Tianshan/RESP/RESP.%s.00.%s > list_resp",sdb->st[ns].name,ch2); // sprintf(str,"ls ~/work/noise/northeast/PZs/SAC_%s_%s_PZs > list_resp\0", sdb->st[ns].name,ch); printf("%s\n",str); system(str); ff = fopen("list_resp","r"); if ( fscanf(ff,"%s", resp_name ) == EOF ) { sdb->rec[ne][ns].n = 0; return; sprintf(resp_name,"no_resp"); } fclose(ff); sprintf(sdb->rec[ne][ns].resp_fname,"%s", resp_name); // we set the ft_name in cut_today_throw1.c, so we don;t need to declam it here //sprintf(sdb->rec[ne][ns].ft_name,"%s/%s/%s/%s.%s..%s.%d.%d",DATAROOT,shd1.knetwk,shd1.kstnm,shd1.kstnm,shd1.knetwk,shd1.kcmpnm,shd1.nzyear,shd1.nzjday); sprintf(sdb->rec[ne][ns].fname,"%s/%s.%s.SAC", sdb->ev[ne].name, sdb->st[ns].name, ch); // sprintf(sdb->rec[ne][ns].ft_fname,"%s/ft_%s.%s.SAC\0", sdb->ev[ne].name, sdb->st[ns].name, ch); sprintf(sdb->rec[ne][ns].chan,"%s", ch ); printf("make_one_rec sdb->rec[ne][ns].t0 %f\n",sdb->rec[ne][ns].t0); }
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; }
/*--------------------------------------------------------------*/ int get_snr(char *fname,int day_num) /*--------------------------------------------------------------*/ { FILE *f2; double dist,minV,maxV,minT,maxT,window,signalmax,noisemax,noiserms; double maxP=35.0; int nf=6; double per[nf],f[nf],f1[22],snr[nf],norm_amp_day[nf],norm_amp_noise[nf],amp_max[nf]; double minP=5.0, num; double b,e,fb,fe,step; // double per1, per2, per3, per4; int k,i,ii,bi,ei; // char fname1[300], fltname[200], tstr[300]; char *fname1, *fltname, *tstr; fname1=(char *) malloc(300*sizeof(char)); fltname = (char *) malloc(300*sizeof(char)); tstr = (char *) malloc(300*sizeof(char)); printf("fname is %s\n", fname); /*---------------- reading sac file -------------------*/ //system("pwd\n"); if ( read_sac (fname, sig0, &tysac, SLEN) == NULL ) { fprintf(stderr,"file %s not found\n", fname); free(fname1); free(fltname); free(tstr); return 0; } fb=1.0/maxP; fe=1.0/minP; step=(log(fb)-log(fe))/(nf-1); for(k=0;k<nf;k++) { f[k]=exp(log(fe)+k*step); per[k]=1/f[k]; } b=tysac.b; e=tysac.e; // e=-b; bi=(int)b; ei=(int)e; //printf("b: %f e: %f\n",b,e); //printf("bi: %d ei: %d\n",bi,ei); for(i=0;i<=ei;i++) { sig1[i]=sig0[i-bi]; //if(sig0[i]*sig0[i]>3)printf("i: %d i: %f -i: %f s: %f\n",i+bi,sig0[i],sig0[-bi*2-i],sig1[i+bi]); } dist = tysac.dist; //printf("%f\n",dist); double dt; int nsamples,n,npow; dt = tysac.delta; nsamples = tysac.npts; npow=1; n=nsamples; minV = 2.0; maxV = 3.5; minT = dist/maxV-maxP/2; maxT = dist/minV+maxP; if(minT<0) minT=0; if(maxT>e) maxT=e; window=maxT-minT; double ff1,ff2,ff3,ff4; //for(k=0;k<=21;k++) // { // f1[k] = exp(log(0.4)-0.2*k); // } //for(k=1;k<21;k++) // { // if(k>7 && k<17){continue;} // ff1 = f1[k+1]/1.25; // ff2 = f1[k+1]; // ff3 = f1[k-1]; // ff4 = f1[k-1]*1.25; // // filter4_(&ff1,&ff2,&ff3,&ff4,&npow,&dt,&n,sig1,seis_out); ff1=1./6./1.25; ff2=1./6.; ff3=1./4.; ff4=1./4.*1.25; filter4_(&ff1,&ff2,&ff3,&ff4,&npow,&dt,&n,sig1,seis_out); noiserms=0.; ii=0; int noise_b=1500; if(maxT+500>1500)noise_b=(int)maxT+500; for(i=noise_b;i<2500;i++) { // noise[k]=seis_out[i]*seis_out[i]+noise[k]; noiserms=seis_out[i]*seis_out[i]+noiserms; ii++; } noiserms=sqrt(noiserms/(ii-1)); //fprintf(stderr,"%lf\n",noiserms); // } // sprintf(fname1,"%s_amp_l_norm.txt",fname); // f2=fopen(fname1,"w"); // for(i=1;i<21;i++) { // if(i>7 && i<17){continue;} // fprintf(f2,"%lf normd_amp amp_max %lf\n",1/f1[i],noise[i]); // } // fclose(f2); // free(fname1); // return 1; // char buff[300]; for(k=1;k<nf-1;k++) { if((f[k+1] != 0) && (f[k-1] != 0)) { ff1 = f[k+1]/1.25; ff2 = f[k+1]; ff3 = f[k-1]; ff4 = f[k-1]*1.25; //printf("%f %f %f %f\n",1/ff1,1/ff2,1/ff3,1/ff4); } else { free(fname1); free(fltname); free(tstr); return 1; } filter4_(&ff1,&ff2,&ff3,&ff4,&npow,&dt,&n,sig1,seis_out); // if(ff2<0.01&&ff3>0.01) //{ // sprintf(buff,"test.sac"); // write_sac(buff, seis_out, &SAC_HEADER); //} //fprintf(stderr,"%lf %lf %lf %lf\n", ff1, ff2, ff3, ff4); /////////////////////////////////////////////////////////// // bp filtered signal has been obtained, now go through and // find the snr /////////////////////////////////////////////////////////// signalmax=0; noisemax=0; //for(i=int(minT);i<maxT;i++) for(i=(int)minT;i<maxT;i++) { if(seis_out[i] < 0) num = seis_out[i]*(-1); else num = seis_out[i]; if(num>signalmax) signalmax=num; } //if(signalmax>3)printf("signalmax: %f\n",signalmax); //fprintf(stderr,"%lf %lf %lf\n", minT, maxT, signalmax); //printf("signalmax: %f noiserms: %f\n",signalmax,noiserms); snr[k]=signalmax/noiserms; norm_amp_day[k]=signalmax/day_num; norm_amp_noise[k]=snr[k]/noiserms; // norm_amp[k]=snr[k]/pow(noiserms,2./3.); amp_max[k]=signalmax; //printf("period:%lf noiserms_square:%lf normd_amp:%lf amp:%lf\n",per[k],noiserms*noiserms,norm_amp[k],amp_max[k]); // snr[k]=signalmax/noisemax; } //printf("%lf\n",noiserms*noiserms); sprintf(fname1,"%s_amp_l_norm_pos.txt",fname); f2=fopen(fname1,"w"); if(snr[(int)(nf/2)]<3 || snr[nf-1-(int)(nf/2)]<3){ fprintf(stderr,"SNR of %s: %f. less than 3. skipped!\n",fname,snr[(int)(nf/2)] ); sprintf(tstr,"rm -f %s\n",fname1); system(tstr); free(fname1); free(fltname); free(tstr); fclose(f2); return 0; } // if(day_num<30){ if(noiserms<0.015){ fprintf(stderr,"stacking time of %s: %f is too short. skipped!\n",fname,noiserms ); sprintf(tstr,"rm -f %s\n",fname1); system(tstr); free(fname1); free(fltname); free(tstr); fclose(f2); return 0; } for(i=1;i<nf-1;i++) { fprintf(f2,"%lf %lf %lf %lf %lf %d\n",per[i],norm_amp_noise[i],norm_amp_day[i],amp_max[i],noiserms,day_num); } // printf("maxP=%f minP=%f maxV=%f minV=%f!!!\n",maxP,minP,maxV,minV); fclose(f2); free(fname1); free(fltname); free(tstr); return 1; }