int main(int argc ,char **argv) { char buf[1024]; char *ptr; int i; int j; int k; int total; int t1; int t2; int t3; int t4; int t5; float r; float v[128]; float x; float dx; float min_x; float max_x; float rmin = 0.0; float rmax = 0.0; float y; float dy; float min_y; float max_y; int ix; int iy; int ir = -1; int nx = 100; int ny = 100; float** bin; int* xcnt; float sigma; /* intermediates for calculating standard deviation */ float* a; float* b; float sum; float m; int logx = 0; int logy = 0; /*--------------------------------------------------------------------*\ Get the commandline arguents in SU style \*--------------------------------------------------------------------*/ initargs( argc, argv ); requestdoc( 1 ); if( !getparint( "ix" ,&ix ) ){ err( "Missing X column index, ix=" ); } if( !getparint( "iy" ,&iy ) ){ err( "Missing Y column index, ix=" ); } getparint( "ir" ,&ir ); if( ir > 0 && !getparfloat( "rmax" ,&rmax ) && !getparfloat( "rmin" ,&rmin ) ){ err( "Failed to specify rejection threshold" ); } if( !getparfloat( "min_x" ,&min_x ) ){ err( "min_x= not specified" ); } if( !getparfloat( "max_x" ,&max_x ) ){ err( "max_x= not specified" ); } if( !getparfloat( "min_y" ,&min_y ) ){ err( "min_y= not specified" ); } if( !getparfloat( "max_y" ,&max_y ) ){ err( "max_y= not specified" ); } getparint( "nx" ,&nx ); getparint( "ny" ,&ny ); getparint( "logx" ,&logx ); getparint( "logy" ,&logy ); if( logx ){ if( min_x < 0 || max_x < 0 ){ err( "Cannot use logscale for X" ); }else{ min_x = log(min_x); max_x = log(max_x); } } if( logy ){ if( min_y < 0 || max_y < 0 ){ err( "Cannot use logscale for Y" ); }else{ min_y = log(min_y); max_y = log(max_y); } } dx = (max_x - min_x) / nx; dy = (max_y - min_y) / ny; /*--------------------------------------------------------------------*\ Allocate and clear the space for the work arrays. \*--------------------------------------------------------------------*/ bin = (float**) ealloc2( ny ,nx ,sizeof(float) ); xcnt = (int*) ealloc1( nx ,sizeof(int) ); a = (float*) ealloc1( nx ,sizeof(float) ); b = (float*) ealloc1( nx ,sizeof(float) ); for( i=0; i<nx; i++ ){ memset( bin[i] ,0 ,ny*sizeof(float) ); } memset( xcnt ,0 ,nx*sizeof(int) ); memset( a ,0 ,nx*sizeof(float) ); memset( b ,0 ,nx*sizeof(float) ); total = 0; /*--------------------------------------------------------------------*\ Read the input data as space delimited ASCII text \*--------------------------------------------------------------------*/ while (!feof(stdin) && fgets( buf ,sizeof(buf) ,stdin ) ){ /* tokenize and read input */ ptr = buf; k = 0; while( (ptr=strtok( ptr ," \t\n" )) ){ sscanf( ptr ,"%f" ,&v[k] ); k++; ptr = 0; } x = v[ix-1]; y = v[iy-1]; if( logx && x > 0 ){ x = log(x); } if( logy && y > 0 ){ y = log(y); } i = (x - min_x ) /dx; j = (y - min_y ) /dy; if( ir > 0 ){ /* 3rd value threshold options */ r = v[ir-1]; if( rmin != 0.0 && r > rmin && i > 0 && i < nx && j > 0 && j < ny ){ /* lower threshold */ bin[i][j]++; xcnt[i]++; a[i] += y*y; b[i] += y; total++; }else if( rmax != 0.0 && r < rmax && i > 0 && i < nx && j > 0 && j < ny ){ /* upper threshold */ bin[i][j]++; xcnt[i]++; a[i] += y*y; b[i] += y; total++; } }else{ /* no threshold value */ if( i > 0 && i < nx && j > 0 && j < ny ){ bin[i][j]++; xcnt[i]++; a[i] += y*y; b[i] += y; total++; } } } for( i=0; i<nx; i++ ){ if( xcnt[i] > 0.0001*total ){ sum = 0.0; if( logx ){ printf( "%f " ,exp(min_x + i*dx) ); }else{ printf( "%f " ,min_x + i*dx ); } printf( "%d " ,xcnt[i] ); t1 = 0; t2 = 0; t3 = 0; t4 = 0; t5 = 0; for( j=0; j<ny; j++ ){ sum += bin[i][j]; m = (100.0*sum ) / xcnt[i]; if( !t1 && m >= 2.28 ){ t1 = 1; if( logy ){ printf( "%f " ,exp(min_y + j*dy) ); }else{ printf( "%f " ,min_y + j*dy ); } }else if( !t2 && m >= 15.87 ){ t2 = 1; if( logy ){ printf( "%f " ,exp(min_y + j*dy) ); }else{ printf( "%f " ,min_y + j*dy ); } }else if( !t3 && m >= 50.00 ){ t3 = 1; if( logy ){ printf( "%f " ,exp(min_y + j*dy) ); }else{ printf( "%f " ,min_y + j*dy ); } }else if( !t4 && m >= 84.13 ){ t4 = 1; if( logy ){ printf( "%f " ,exp(min_y + j*dy) ); }else{ printf( "%f " ,min_y + j*dy ); } }else if( !t5 && m >= 97.72 ){ t5 = 1; if( logy ){ printf( "%f " ,exp(min_y + j*dy) ); }else{ printf( "%f " ,min_y + j*dy ); } } } sigma = sqrt( (xcnt[i]*a[i] - b[i]*b[i])/(xcnt[i]*xcnt[i]-1) ); if( logy && sigma > 0 ){ printf( "%f " ,log(sigma) ); }else{ printf( "%f " ,sigma ); } printf( "\n" ); } } return EXIT_SUCCESS; }
int main( int argc, char *argv[] ) { int nx; int fbt; int nt; float *stacked=NULL; int *nnz=NULL; int itr=0; initargs(argc, argv); requestdoc(1); if (!getparint("nx", &nx)) nx = 51; if( !ISODD(nx) ) { nx++; warn(" nx has been changed to %d to be odd.\n",nx); } if (!getparint("fbt", &fbt)) fbt = 60; checkpars(); /* Get info from first trace */ if (!gettr(&tr)) err("can't get first trace"); nt = tr.ns; stacked = ealloc1float(fbt); nnz = ealloc1int(fbt); memset((void *) nnz, (int) '\0', fbt*ISIZE); memset((void *) stacked, (int) '\0', fbt*FSIZE); /* read nx traces and stack them */ /* The first trace is already read */ { int i,it; float **tr_b; char **hdr_b; int NXP2=nx/2; short shft,scaler; /* ramp on read the first nx traces and create stack */ tr_b = ealloc2float(nt,nx); hdr_b = (char**)ealloc2(HDRBYTES,nx,sizeof(char)); memcpy((void *) hdr_b[0], (const void *) &tr, HDRBYTES); memcpy((void *) tr_b[0], (const void *) &tr.data, nt*FSIZE); for(i=1;i<nx;i++) { gettr(&tr); memcpy((void *) hdr_b[i], (const void *) &tr, HDRBYTES); memcpy((void *) tr_b[i], (const void *) &tr.data, nt*FSIZE); } for(i=0;i<nx;i++) for(it=0;it<fbt;it++) stacked[it] += tr_b[i][it]; for(it=0;it<fbt;it++) stacked[it] /=(float)nx; /* filter and write out the first nx/2 +1 traces */ for(i=0;i<NXP2+1;i++) { memcpy((void *) &tr, (const void *) hdr_b[i], HDRBYTES); memcpy((void *) tr.data, (const void *) tr_b[i], nt*FSIZE); remove_fb(tr.data,stacked,fbt,&scaler,&shft); tr.trwf = scaler; tr.grnors = shft; puttr(&tr); ++itr; } /* do the rest of the traces */ gettr(&tr); do { /* Update the stacked trace - remove old */ for(it=0;it<fbt;it++) stacked[it] -= tr_b[0][it]/(float)nx; /* Bump up the storage arrays */ /* This is not very efficient , but good enough */ {int ib; for(ib=1;ib<nx;ib++) { memcpy((void *) hdr_b[ib-1], (const void *) hdr_b[ib], HDRBYTES); memcpy((void *) tr_b[ib-1], (const void *) tr_b[ib], nt*FSIZE); } } /* Store the new trace */ memcpy((void *) hdr_b[nx-1], (const void *) &tr, HDRBYTES); memcpy((void *) tr_b[nx-1], (const void *) &tr.data, nt*FSIZE); /* Update the stacked array - add new */ for(it=0;it<fbt;it++) stacked[it] += tr_b[nx-1][it]/(float)nx; /* Filter and write out the middle one NXP2+1 */ memcpy((void *) &tr, (const void *) hdr_b[NXP2], HDRBYTES); memcpy((void *) tr.data, (const void *) tr_b[NXP2], nt*FSIZE); remove_fb(tr.data,stacked,fbt,&scaler,&shft); tr.trwf = scaler; tr.grnors = shft; puttr(&tr); ++itr; } while(gettr(&tr)); /* Ramp out - write ot the rest of the traces */ /* filter and write out the last nx/2 traces */ for(i=NXP2+1;i<nx;i++) { memcpy((void *) &tr, (const void *) hdr_b[i], HDRBYTES); memcpy((void *) tr.data, (const void *) tr_b[i], nt*FSIZE); remove_fb(tr.data,stacked,fbt,&scaler,&shft); tr.trwf = scaler; tr.grnors = shft; puttr(&tr); itr++; } } free1float(stacked); free1int(nnz); return EXIT_SUCCESS; }
int main( int argc, char *argv[] ) { int ntr=0; /* number of traces */ int ntrv=0; /* number of traces */ int ns=0; int nsv=0; float dt; float dtv; cwp_String fs; cwp_String fv; FILE *fps; FILE *fpv; FILE *headerfp; float *data; /* data matrix of the migration volume */ float *vel; /* velocity matrix */ float *velfi; /* velocity function interpolated to ns values*/ float *velf; /* velocity function */ float *vdt; float *ddt; float *ap; /* array of apperture values in m */ float apr; /* array of apperture values in m */ int *apt=NULL; /* array of apperture time limits in mig. gath*/ float r; /* maximum radius with a given apperture */ float ir2; /* r/d2 */ float ir3; /* r/d3 */ float d2; /* spatial sampling int. in dir 2. */ float d3; /* spatial sampling int. in dir 3. */ float **mgd=NULL; /* migration gather data */ float *migt; /* migrated data trace */ int **mgdnz=NULL; /* migration gather data non zero samples*/ float dm; /* migration gather spatial sample int. */ int im; /* number of traces in migration gather */ int *mtnz; /* migrated trace data non zero smaples */ char **dummyi; /* index array that the trace contains zeros only */ float fac; /* velocity scale factor */ int sphr; /* spherical divergence flag */ int imt; /* mute time sample of trace */ float tmp; int imoff; int **igtr=NULL; int nigtr; int n2; int n3; int verbose; /* phase shift filter stuff */ float power; /* power of i omega applied to data */ float amp; /* amplitude associated with the power */ float arg; /* argument of power */ float phasefac; /* phase factor */ float phase; /* phase shift = phasefac*PI */ complex exparg; /* cexp(I arg) */ register float *rt; /* real trace */ register complex *ct; /* complex transformed trace */ complex *filt; /* complex power */ float omega; /* circular frequency */ float domega; /* circular frequency spacing (from dt) */ float sign; /* sign in front of i*omega default -1 */ int nfft; /* number of points in nfft */ int nf; /* number of frequencies (incl Nyq) */ float onfft; /* 1 / nfft */ size_t nzeros; /* number of padded zeroes in bytes */ initargs(argc, argv); requestdoc(1); MUSTGETPARSTRING("fs",&fs); MUSTGETPARSTRING("fv",&fv); MUSTGETPARINT("n2",&n2); MUSTGETPARINT("n3",&n3); MUSTGETPARFLOAT("d2",&d2); MUSTGETPARFLOAT("d3",&d3); if (!getparfloat("dm", &dm)) dm=(d2+d3)/2.0; /* open datafile */ fps = efopen(fs,"r"); fpv = efopen(fv,"r"); /* Open tmpfile for headers */ headerfp = etmpfile(); /* get information from the first data trace */ ntr = fgettra(fps,&tr,0); if(n2*n3!=ntr) err(" Number of traces in file %d not equal to n2*n3 %d \n", ntr,n2*n3); ns=tr.ns; if (!getparfloat("dt", &dt)) dt = ((float) tr.dt)/1000000.0; if (!dt) { dt = .002; warn("dt not set, assumed to be .002"); } /* get information from the first velocity trace */ ntrv = fgettra(fpv,&trv,0); if(ntrv!=ntr) err(" Number of traces in velocity file %d differ from %d \n", ntrv,ntr); nsv=trv.ns; if (!getparfloat("dtv", &dtv)) dtv = ((float) trv.dt)/1000000.0; if (!dtv) { dtv = .002; warn("dtv not set, assumed to be .002 for velocity"); } if (!getparfloat("fac", &fac)) fac=2.0; if (!getparint("verbose", &verbose)) verbose=0; if (!getparint("sphr", &sphr)) sphr=0; if (!getparfloat("apr", &apr)) apr=75; apr*=3.141592653/180; /* allocate arrays */ data = bmalloc(sizeof(float),ns,ntr); vel = bmalloc(sizeof(float),nsv,ntr); velf = ealloc1float(nsv); velfi = ealloc1float(ns); migt = ealloc1float(ns); vdt = ealloc1float(nsv); ddt = ealloc1float(ns); ap = ealloc1float(ns); mtnz = ealloc1int(ns); dummyi = (char **) ealloc2(n2,n3,sizeof(char)); /* Times to do interpolation of velocity from sparse sampling */ /* to fine sampling of the data */ { register int it; for(it=0;it<nsv;it++) vdt[it]=it*dtv; for(it=0;it<ns;it++) ddt[it]=it*dt; } /* Read traces into data */ /* Store headers in tmpfile */ ntr=0; erewind(fps); erewind(fpv); { register int i2,i3; for(i3=0;i3<n3;i3++) for(i2=0;i2<n2;i2++) { fgettr(fps,&tr); fgettr(fpv,&trv); if(tr.trid > 2) dummyi[i3][i2]=1; else dummyi[i3][i2]=0; efwrite(&tr, 1, HDRBYTES, headerfp); bmwrite(data,1,0,i3*n2+i2,ns,tr.data); bmwrite(vel,1,0,i3*n2+i2,nsv,trv.data); } erewind(headerfp); /* set up the phase filter */ power = 1.0;sign = 1.0;phasefac = 0.5; phase = phasefac * PI; /* Set up for fft */ nfft = npfaro(ns, LOOKFAC * ns); if (nfft >= SU_NFLTS || nfft >= PFA_MAX) err("Padded nt=%d -- too big", nfft); nf = nfft/2 + 1; onfft = 1.0 / nfft; nzeros = (nfft - ns) * FSIZE; domega = TWOPI * onfft / dt; /* Allocate fft arrays */ rt = ealloc1float(nfft); ct = ealloc1complex(nf); filt = ealloc1complex(nf); /* Set up args for complex power evaluation */ arg = sign * PIBY2 * power + phase; exparg = cexp(crmul(I, arg)); { register int i; for (i = 0 ; i < nf; ++i) { omega = i * domega; /* kludge to handle omega=0 case for power < 0 */ if (power < 0 && i == 0) omega = FLT_MAX; /* calculate filter */ amp = pow(omega, power) * onfft; filt[i] = crmul(exparg, amp); } } /* set up constants for migration */ if(verbose) fprintf(stderr," Setting up constants....\n"); r=0; for(i3=0;i3<n3;i3++) for(i2=0;i2<n2;i2++) { if(dummyi[i3][i2] < 1) { /* get the velocity function */ bmread(vel,1,0,i3*n2+i2,nsv,velf); /* linear interpolation from nsv to ns values */ intlin(nsv,vdt,velf,velf[0],velf[nsv-1],ns,ddt,velfi); /* Apply scale factor to velocity */ { register int it; for(it=0;it<ns;it++) velfi[it] *=fac; } /* compute maximum radius from apperture and velocity */ { register int it; for(it=0;it<ns;it++) ap[it] = ddt[it]*velfi[it]*tan(apr)/2.0; } tmp = ap[isamax(ns,ap,1)]; if(tmp>r) r=tmp; } } r=MIN(r,sqrt(SQR((n2-1)*d2)+SQR((n3-1)*d3))); ir2 = (int)(2*r/d2)+1; ir3 = (int)(2*r/d3)+1; im = (int)(r/dm)+1; /* allocate migration gather */ mgd = ealloc2float(ns,im); mgdnz = ealloc2int(ns,im); apt = ealloc1int(im); /* set up the stencil for selecting traces */ igtr = ealloc2int(ir2*ir3,2); stncl(r, d2, d3,igtr,&nigtr); if(verbose) { fprintf(stderr," Maximum radius %f\n",r); fprintf(stderr," Maximum offset %f\n", sqrt(SQR((n2-1)*d2)+SQR((n3-1)*d3))); } /* main processing loop */ for(i3=0;i3<n3;i3++) for(i2=0;i2<n2;i2++) { memset( (void *) tr.data, (int) '\0',ns*FSIZE); if(dummyi[i3][i2] < 1) { memset( (void *) mgd[0], (int) '\0',ns*im*FSIZE); memset( (void *) mgdnz[0], (int) '\0',ns*im*ISIZE); /* get the velocity function */ bmread(vel,1,0,i3*n2+i2,nsv,velf); /* linear interpolation from nsv to ns values */ intlin(nsv,vdt,velf,velf[0],velf[nsv-1],ns,ddt,velfi); /* Apply scale factor to velocity */ { register int it; for(it=0;it<ns;it++) velfi[it] *=fac; } /* create the migration gather */ { register int itr,ist2,ist3; for(itr=0;itr<nigtr;itr++) { ist2=i2+igtr[0][itr]; ist3=i3+igtr[1][itr]; if(ist2 >= 0 && ist2 <n2) if(ist3 >= 0 && ist3 <n3) { if(dummyi[ist3][ist2] <1) { imoff = (int) ( sqrt(SQR(igtr[0][itr]*d2) +SQR(igtr[1][itr]*d3))/dm+0.5); bmread(data,1,0,ist3*n2+ist2,ns,tr.data); imoff=MIN(imoff,im-1); { register int it; /* get the mute time for this offset, apperture and velocity */ xindex(ns,ap,imoff*dm,&imt); for(it=imt;it<ns;it++) if(tr.data[it]!=0) { mgd[imoff][it]+=tr.data[it]; mgdnz[imoff][it]+=1; } } } } } } /* normalize the gather */ { register int ix,it; for(ix=0;ix<im;ix++) for(it=0;it<ns;it++) if(mgdnz[ix][it] > 1) mgd[ix][it] /=(float) mgdnz[ix][it]; } memset( (void *) tr.data, (int) '\0',ns*FSIZE); memset( (void *) mtnz, (int) '\0',ns*ISIZE); /* do a knmo */ { register int ix,it; for(ix=0;ix<im;ix++) { /* get the mute time for this offset, apperture and velocity */ xindex(ns,ap,ix*dm,&imt); knmo(mgd[ix],migt,ns,velfi,0,ix*dm,dt,imt,sphr); /* stack the gather */ for(it=0;it<ns;it++) { if(migt[it]!=0.0) { tr.data[it] += migt[it]; mtnz[it]++; } /* tr.data[it] += mgd[ix][it]; */ } } } { register int it; for(it=0;it<ns;it++) if(mtnz[it]>1) tr.data[it] /=(float)mtnz[it]; } /*Do the phase filtering before the trace is released*/ /* Load trace into rt (zero-padded) */ memcpy( (void *) rt, (const void *) tr.data, ns*FSIZE); memset((void *) (rt + ns), (int) '\0', nzeros); pfarc(1, nfft, rt, ct); { register int i; for (i = 0; i < nf; ++i) ct[i] = cmul(ct[i], filt[i]); } pfacr(-1, nfft, ct, rt); memcpy( (void *) tr.data, (const void *) rt, ns*FSIZE); } /* end of dummy if */ /* spit out the gather */ efread(&tr, 1, HDRBYTES, headerfp); puttr(&tr); if(verbose) fprintf(stderr," %d %d\n",i2,i3); } /* end of i2 loop */ } /* end of i3 loop */ /* This should be the last thing */ efclose(headerfp); /* Free memory */ free2int(igtr); free2float(mgd); free2int(mgdnz); free1int(apt); bmfree(data); bmfree(vel); free1float(velfi); free1float(velf); free1float(ddt); free1float(vdt); free1float(ap); free1int(mtnz); free1float(migt); free1float(rt); free1complex(ct); free1complex(filt); free2((void **) dummyi); return EXIT_SUCCESS; }