Beispiel #1
0
void tabtrcoefs(int ninf, float *rho, float *v, float **theta,
					float *dip, float *trcoefs)
/*****************************************************************************
table transmission coefficients
******************************************************************************
Input:
ninf		x coordinate of source (must be within x samples)
xxxx

Output:
trcoefs		array[1..ninf] containing transmission coefficients
******************************************************************************
Notes:
The parameters are exactly the same as in the main program.
This is a subroutine so that the temporary arrays may be disposed
of after exit. This routine is only called once.
******************************************************************************
Author:  Brian Sumner, Colorado School of Mines, 1985
******************************************************************************/
{
/**
* Local variables:
*    TEMP, TMP - temporary arrays
*    I, J - loop variables
*    T1, T2 - temporaries
*    R, P - more temporaries
**/

	int i,j;
	float t1, t2, r, p, *temp, **tmp;

	temp = ealloc1float(ninf+1);
	tmp = ealloc2float(ninf+1, ninf+1);
	
	for (i = 0; i <= ninf; ++i)  temp[i] = rho[i]*v[i];

	for (j = 1; j <= ninf; ++j) {
		for (i = 1; i < j; ++i) {
			t1 = temp[i]*cos(theta[j][i-1]+dip[i]);
			t2 = temp[i-1]*cos(theta[j][i]+dip[i]);
			r = (t1 - t2)/(t1 + t2);
			tmp[j][i] = 1.0 - r*r;
		}
	}

	for (j = 1; j <= ninf; ++j) {
		t1 = temp[j];
		t2 = temp[j-1];
		p = (t1 - t2)/(t1 + t2);
		for (i = 1; i < j; ++i)  p *= tmp[j][i];
		trcoefs[j] = p;
	}
	
	/* free space */
	free1float(temp);
	free2float(tmp);
}
Beispiel #2
0
void find_p(float *ym,float *yp,float *a,int *b,int n)
{
#define NP 1

	float *y,**x;
	int n_s;
	
	int k;
	float *res;
	int jpvt[NP];
	float qraux[NP];
	float work[NP]; 
	
														
	x = ealloc2float(n,1);
	y = ealloc1float(n);
	res = ealloc1float(n);
	
	memcpy((void *) &x[0][0], (const void *) &ym[0], n*FSIZE);
	memset((void *) y, (int) '\0', n*FSIZE);	
	
	/* Solve for shift */
 	xcor (n,0,ym,n,0,yp,n,-n/2,y);
	/* pick the maximum */
	*b = -max_index(n,y,1)+n/2;

	n_s = n-abs(*b);
 	
	if (*b < 0) {
		memcpy((void *) &x[0][0], (const void *) &ym[*b], n_s*FSIZE);
	} else {
		memcpy((void *) &x[0][*b], (const void *) &ym[0], n_s*FSIZE);
													
	}
	/* Solve for scaler */
	sqrst(x, n_s, 1,yp,0.0,a,res,&k,&jpvt[0],&qraux[0],&work[0]);
	
	
	
	free2float(x);
	free1float(res);
	free1float(y);
}
Beispiel #3
0
int
main(int argc, char **argv)
{

	int i,j,k;		/* counters */
	int ns=0;		/* number of samples in input data */
	int nwavelet=1024;	/* number of samples in mother wavelet */

	float base=0.0;		/* base */
	float first=0.0;	/* first exponent */
	float expinc=0.0;	/* exponent increment */
	float last=0.0;		/* last exponent */
	float exponent=0.0;	/* each exponent */
	float maxscale=0.0;	/* maximum scale value */
	float minscale=0.0;	/* minimum scale value */

	float x=0.0;
	float dx=0.0;		/* xvalues incr */

	float xmin=0.0;		/* last xvalues - first vval */
	float xcenter=0.0;	/* x value of center of wavelet */
	float xmax=0.0;		/* last xvalues - first vval */
	float sigma=1.0;	/* sharpening parameter */

	float waveletinc=0.0;		/* wavelet interval */
	float fmin=0.0;		/* min, max filt value (debug) */
	float *xvalues=NULL;	/* wavelet xvalues */
	float **filt=NULL;	/* filter used for each conv */

	float *f=NULL;		/* scratch for filter fliplr */
	float *sucwt_buff=NULL;	/* scratch for convolution */
	float *scales=NULL;	/* scales */
	float *waveletsum=NULL;	/* cumulative sum of wavelet */

	float *rt=NULL;		/* temp data storage */
	float *qt=NULL;		/* temp hilbert transformed data storage */
	float **tmpdata=NULL;	/* temp data storage */

	int wtype=0;		/* type of wavelet selected */
	float *wavelet=NULL;	/* pointer to data constituting the wavelet */

	int verbose=0;		/* verbose flag */
	int *index=NULL;	/* wavelet subscripts to use for filter */
	int *nconv=NULL;	/* length of each filter */
	int nscales=0;		/* number of scales */

	int holder=0;		/* =1 compute the Holder-Lipschitz regularity */
	float divisor=1.0;	/* divisor used in Holder exponent calculation*/

	/* Initialize */
	initargs(argc, argv);
	requestdoc(1);


	/* Get parameters */
	if(!getparfloat("base",&base))			base = 10.;
	if(!getparfloat("first",&first))		first = -1.0;
	if(!getparfloat("expinc",&expinc))			expinc = 0.01;
	if(!getparfloat("last",&last))			last = 1.5;

	if(!getparint("wtype",&wtype))			wtype = 0;
	if(!getparint("nwavelet",&nwavelet))		nwavelet = 1024;
	if(!getparfloat("xmin",&xmin))			xmin = -20.0;
	if(!getparfloat("xcenter",&xcenter))		xmin = 0.0;
	if(!getparfloat("xmax",&xmax))			xmax = 20.0;
	if(!getparfloat("sigma",&sigma))		sigma = 1.0;

	if(!getparint("holder",&holder))		holder = 0;
	if(!getparfloat("divisor",&divisor))		divisor = 1.0;

	if(!getparint("verbose",&verbose))		verbose = 0;

	if(verbose)
	 warn("base=%f, first=%f, expinc=%f, last=%f",base,first,expinc,last);


	/* Allocate space */
	xvalues = ealloc1float(nwavelet);
	wavelet = ealloc1float(nwavelet);
	memset((void *) xvalues, 0, nwavelet*FSIZE);
	memset((void *) wavelet, 0, nwavelet*FSIZE);

	/* Compute wavelet */
	if (wtype == 0 ) { /* so far only Mex. Hat function */
		MexicanHatFunction(nwavelet, xmin, xcenter,
					xmax, sigma, wavelet);
	} else {
		err("%d  type of wavelet not yet implemented",wtype); 
	}

	/* wavelet increment */
	waveletinc = (xmax - xmin)/(nwavelet - 1);

	/* verbose  warning */
	if(verbose)
	 warn("xmin=%f, xmax=%f, nwavelet=%d, waveletinc=%f",
			xmin,xmax,nwavelet,waveletinc);

	/* form xvalues[] array */
	for(i=0,x=xmin; i<nwavelet; ++i,x+=waveletinc) xvalues[i] = x;

	xvalues[nwavelet-1] = xmax;

	/* compute scales */
	scales = ealloc1float(SHRT_MAX);
	memset((void *) scales, 0, SHRT_MAX*FSIZE);

	exponent = first;
	x = 0;
	nscales = 0;
	minscale = pow(base,first);
	maxscale = pow(base,last);
	while(x <= maxscale) {
		x = pow(base,exponent);
		scales[nscales] = x;
		exponent+=expinc;
		++nscales;

		if(nscales == SHRT_MAX)
			err("Too many scales, change params and re-run\n");
	}
	--nscales;


	/* Allocate space */
	nconv = ealloc1int(nscales);
	index = ealloc1int(nwavelet);
	waveletsum = ealloc1float(nwavelet);
	filt = ealloc2float(nwavelet,nscales);
	f = ealloc1float(nwavelet);

	/* Zero out arrays */
	memset((void *) nconv, 0, nscales*ISIZE);
	memset((void *) index, 0, nwavelet*ISIZE);
	memset((void *) waveletsum, 0, nwavelet*FSIZE);
	memset((void *) filt[0], 0, nwavelet*nscales*FSIZE);
	memset((void *) f, 0, nwavelet*FSIZE);

	/* Form difference of xvalues */
	for(i=nwavelet-1; i>=0; --i)
		xvalues[i] = xvalues[i] - xvalues[0];	

	dx = xvalues[1];
	xmax = xvalues[nwavelet-1];

	/* verbose warning */
	if(verbose) {
		warn("first xvalues=%f, last xvalues=%f",
				xvalues[0],xvalues[nwavelet-1]);
		warn("dx=%f, xmax=%f",dx,xmax);
	}
	
	/* waveletsum is cumulative sum of wavelet multipled by dx */
	fmin = 0;

	for(i=0; i<nwavelet; ++i) {
		fmin += wavelet[i];
		waveletsum[i] = fmin * dx;
	}

	/* Build filters from summed wavelet */
	for(i=0; i<nscales; ++i) {
		nconv[i] = 1 + (int)(scales[i] * xmax);

		for(j=0; j<nconv[i]; ++j)
			index[j] = 1 + j / (scales[i] * dx);

		for(j=0; j<nconv[i]; ++j)
			f[j] = waveletsum[index[j]-1];

		/* flip left right */
		for(j=0,k=nconv[i]-1; j<nconv[i]; ++j,--k)
			filt[i][j] = f[k];
	}

	/* Verbose warning */
	if(verbose) {
		warn("Convolution Lengths");
		for(i=0; i<nscales; ++i) warn("%d ",nconv[i]);
	}
	if(verbose) warn("%d scales will be used for transforms",nscales);

	/* Get information from first trace */
	if(!gettr(&tr))
		err("Cannot get first trace\n");
	ns = tr.ns;

	/* Allocate temporary storage space */
	rt = ealloc1float(ns);
	qt = ealloc1float(ns);
	tmpdata = ealloc2float(nscales,ns);

	/* Zero out rt and qt */
	memset((void *) rt, 0, ns*FSIZE);
	memset((void *) qt, 0, ns*FSIZE);

	/* Alloc sucwt_buffer for longest convolution */
	sucwt_buff = ealloc1float(ns+nconv[nscales-1]+1);
	
	do {  /* main loop over traces */

		outtr.d2 = waveletinc;
		outtr.f2 = minscale;

		memcpy((void *)&outtr,(const void *)&tr,HDRBYTES);

		/* Apply filters to produce wavelet transform */
		for(i=0; i<nscales; ++i) { /* loop over scales */

			for(j=0; j<ns+nconv[nscales-1]+1; ++j)
			sucwt_buff[j] = 0;

			/* convolve wavelet with data */
			conv(ns,0,tr.data,nconv[i],0,
					filt[i],ns,0,sucwt_buff);

			for(j=0; j<ns; ++j) 
				rt[j] = sucwt_buff[j+nconv[i]/2-1];

			for(j=ns-1; j>0; --j) 
				rt[j] = rt[j] - rt[j-1];

			for(j=0; j<ns; ++j)
				rt[j] = -sqrt(scales[i]) * rt[j];

				/* form the hilbert transform of rt */
				hilbert(ns,rt,qt);

			/* If not holder, then output envelope */
			if (!holder) {
				
				for (j=0 ; j<ns; ++j) {			
			  		outtr.data[j] = sqrt(rt[j]*rt[j] 
						               + qt[j]*qt[j]);
				}

				outtr.cdpt = i + 1;
				puttr(&outtr);
			} else {
				/* compute the modulus */
				for (j=0 ; j<ns; ++j) {			
			  		tmpdata[j][i] = sqrt(rt[j]*rt[j] + qt[j]*qt[j]);
				}

			}
		}


		if (holder) { /* compute the Holder regularity traces */
			float *x;
			float *y;
			float lrcoeff[4];

			x = ealloc1float(nscales);
			y = ealloc1float(nscales);
			
	                /* Compute an estimate of the Lipschitz (Holder)
			* regularity. Following Mallat (1992)	
                        *				
                        * ln | Wf(x,s)| <  ln C + alpha * ln|s|
                        *					
                        * alpha here is the Holder or Lipschitz exponent
                        * s is the length scale, and Wf(x,s) is f in the
                        * wavelet basis.			         
                        *					         
			* Here we merely fit a straight line		 
			* through the log-log graph of the of our wavelet
			* transformed data and return the slope as      
			* the regularity measure. 			
                        *					         
			*/

                	for ( j =0 ; j< ns ; ++j ) {
				int icount=0;
                        	x[0]=0;
                        	for ( i = 1 ; i < nscales ; ++i ) {
				
					
			          /* We stay away from values that will make */
				  /*  NANs in the output */
				  if ((i>1) && 
				      (tmpdata[j][i-1] - tmpdata[j][1] > 0.0)) {
                               		y[icount] = log(ABS(tmpdata[j][i]
                                     	              - tmpdata[j][1]));
                                			x[icount] = log(scales[i]-scales[1]);
						
						++icount;
					}

                        	}
				--icount;

				/* straight line fit, return slope */
				if ((icount> 10) && (divisor==1.0) ) {
                        	   linear_regression(y, x, icount, lrcoeff);
                        	   /* lrcoeff[0] is the slope of the line */
				   /* which is the Holder (Lipschitz) */
				   /* exponent */

                        	   outtr.data[j] = lrcoeff[0];

				} else  if ((icount> 10) && (divisor>1.0) ) {

				   float maxalpha=0.0;
				   float interval=icount/divisor;

				   for ( k = interval; k < icount; k+=interval){
                        	   	   linear_regression(y, x, k, lrcoeff);
					   maxalpha = MAX(lrcoeff[0],maxalpha);
					}
				   outtr.data[j] = maxalpha;		

				} else if ((icount < 10) && (divisor>=1.0)) {
				   outtr.data[j] = 0.0;		
				} else if ( divisor < 1.0 ) {
				   err("divisor = %f < 1.0!", divisor);	
				}
				



                	}

			puttr(&outtr); /* output holder regularity traces */
		}
	} while(gettr(&tr));

	return(CWP_Exit());
}
Beispiel #4
0
int
main(int argc, char **argv)
{
	int ix,it;		/* loop counters */
	int i,j,k;
	int ntr;		/* number of input traces */
	int nt;			/* number of time samples */
	int nx;			/* number of horizontal samples */
	float dt;               /* Time sample interval */
        float dx=1;               /* horizontal sample interval */
        float pminf;             /* Minimum slope for Tau-P transform */
        float pmaxf;             /* Maximum slope for Tau-P transform */
	float dpf;		/* slope sampling interval */
	int np;			/* number of slopes for slant stack */
	int nwin;		/* spatial window length */
	int npoints;		/* number of points for rho filter */
	float **twin;		/* array[nwin][nt] of window traces */	
	float **pwin;		/* array[np][nt] of sl traces */
	int ntrw;		/* number of traces in processing window */
				/* full multiple of nwin */	
	int ist;		/* start processing from this window */
	int ntfft;
	float **traces;
	int w;			/* flag to apply semblance weights */
	int s;			/* flag to apply smoothing weights */
	int sl1;		/* length of smoothing window */
	int sl2;		/* length of smoothing window */
	float *smb;		/* semblance weights */
	double pw;
	float smbwin;
	int sn;
	float *spw;		/* array of spatial weights */
	float **out_traces;	/* array[nx][nt] of output traces */	
	int verbose;		/* flag for echoing information */
	char *tmpdir;		/* directory path for tmp files */
	cwp_Bool istmpdir=cwp_false;/* true for user-given path */
	float fh1;		/* maximum frequency before taper */
	float fh2;		/* maximum frequency */
	float prw;		/* prewithening */
	
	
        /* hook up getpar to handle the parameters */
        initargs(argc,argv);
        requestdoc(1);

	if (!getparint("verbose", &verbose))	verbose = 0;

	/* Look for user-supplied tmpdir */
	if (!getparstring("tmpdir",&tmpdir) &&
	    !(tmpdir = getenv("CWP_TMPDIR"))) tmpdir="";
	if (!STREQ(tmpdir, "") && access(tmpdir, WRITE_OK))
		err("you can't write in %s (or it doesn't exist)", tmpdir);

        /* get info from first trace */
        if (!gettr(&tr))  err("can't get first trace");
        nt = tr.ns;
        dt = (float) tr.dt/1000000.0;

        /* Store traces in tmpfile while getting a count */
	if (STREQ(tmpdir,"")) {
		tracefp = etmpfile();
		headerfp = etmpfile();
		if (verbose) warn("using tmpfile() call");
	} else { /* user-supplied tmpdir */
		char directory[BUFSIZ];
		strcpy(directory, tmpdir);
		strcpy(tracefile, temporary_filename(directory));
		strcpy(headerfile, temporary_filename(directory));
		/* Trap signals so can remove temp files */
		signal(SIGINT,  (void (*) (int)) closefiles);
		signal(SIGQUIT, (void (*) (int)) closefiles);
		signal(SIGHUP,  (void (*) (int)) closefiles);
		signal(SIGTERM, (void (*) (int)) closefiles);
		tracefp = efopen(tracefile, "w+");
		headerfp = efopen(headerfile, "w+");
      		istmpdir=cwp_true;		
		if (verbose) warn("putting temporary files in %s", directory);
	}
        ntr = 0;
        do {
                ++ntr;
                efwrite(&tr, 1, HDRBYTES, headerfp);
                efwrite(tr.data, FSIZE, nt, tracefp);
        } while (gettr(&tr));

        /* get general flags and parameters and set defaults */
        if (!getparint("np",&np))             	np = 25;
        if (!getparfloat("pminf",&pminf))	pminf = -0.01;
        if (!getparfloat("pmaxf",&pmaxf))	pmaxf = 0.01;
        if (!getparfloat("fh1",&fh1))		fh1 = 100;
        if (!getparfloat("fh2",&fh2))		fh2 = 120;
        if (!getparfloat("prw",&prw))		prw = 0.01;
	if (!getparfloat("dx",&dx))		dx = 1.0;
	if (!getparint("npoints",&npoints))	npoints = 71;
	if (!getparint("nwin",&nwin))		nwin= 5;
	if (!getparfloat("dt",&dt))		dt = dt;
	if (!getparfloat("smbwin",&smbwin))	smbwin = 0.05;
	if (!getpardouble("pw",&pw))		pw = 1.0;
	if (!getparint("w",&w))			w = 0;
	if (!getparint("s",&s))			s = 0;
	if (!getparint("sl1",&sl1))		sl1 = 2*nwin;
	if (!getparint("sl2",&sl2))		sl2 = nwin;
	
        nx = ntr;
	if (dt == 0.0)
		err("header field dt not set, must be getparred");

	/* allocate space */
	ntfft=npfar(nt);
	ntrw=nwin;
	while (ntrw < ntr) {
		ntrw+=nwin;
	}
	ist = ntrw-ntr/2;
	twin = alloc2float(nt, nwin);
	pwin = ealloc2float(ntfft,np);
        traces = alloc2float(nt, ntr);
        out_traces = alloc2float(nt, ntr);
	smb = ealloc1float(nt);
	
	/* Set up some constans*/
	dpf=(pmaxf-pminf)/(np-1);
	sn = (int)(smbwin/dt+0.5);
	if(sn%2==0) sn++;
	if(nwin%2==0) nwin++;
	
	/* spatial trace weigths */
	spw = ealloc1float(nwin);
	for(k=0,i=1;k<nwin/2+1;k++,i++) spw[k] = (float)i; 
	for(k=nwin/2+1,i=nwin/2;k<nwin;k++,i--) spw[k] = (float)i; 
/*	for(k=0,i=1;k<nwin;k++,i++) spw[k] =1.0; */
	
	
	
        /* load traces into an array and close temp file */
	erewind(headerfp);
        erewind(tracefp);
	
	memset( (void *) traces[0], (int) '\0', (nt*ntr)*FSIZE);
	memset( (void *) out_traces[0], (int) '\0', (nt*ntr)*FSIZE);
	
        for (ix=0; ix<ntr; ix++)
                fread (traces[ix], FSIZE, nt, tracefp);
        efclose (tracefp);
	if (istmpdir) eremove(tracefile);

	/* do requested operation */
	 
	for(i=0; i<ntr; i+=nwin/2) {
		memcpy( (void *) twin[0], (const void *) traces[i],
			nt*nwin*FSIZE);	

		/* compute forward slant stack */
/*		fwd_tx_sstack (dt, nt, nwin, -nwin/2*dx, dx, np, pminf, dpf,
	       		twin, pwin);
*/		forward_p_transform(nwin,nt,dt,pmaxf*1000.0,pminf*1000.0,dpf*1000.0,
                                    0.0,fh1,fh2,3.0,30.0,400,5,1,0,0,1,prw,
				    0.0,nwin*dx,1,dx,0.0,0.0,0.0,twin,pwin);
/*		fwd_FK_sstack (dt, nt, nwin, -nwin/2*dx, dx, np, pminf, dpf,0,
	       		twin, pwin);
*/		
		/* compute semplance */
		if(w==1) {
			semb(sn,pwin,np,nt,smb);
			/* apply weights */
			for(j=0;j<nt;j++)
				for(k=0;k<np;k++) pwin[k][j] *=smb[j];
		}
		if(s==1) {
			gaussian2d_smoothing (np,nt,sl2,sl1,pwin);
		}
		if(s==2) {
			dlsq_smoothing (nt,np,0,nt,0,np,sl1,sl2,0,pwin);
		}
		/* compute inverse slant stack */
/*		inv_tx_sstack (dt, nt, nwin, npoints,-nwin/2*dx, dx, np,pminf,dpf,
			pwin, twin);
*/		inverse_p_transform(nwin,nt,dt,pmaxf*1000.0,pminf*1000.0,dpf*1000.0,
                                    0.0,fh1,fh2,0.0,nwin*dx,1,dx,0.0,
				    pwin,twin);
/*		inv_FK_sstack (dt, nt, nwin,-nwin/2*dx, dx, np,pminf,dpf,0,
			pwin, twin);
*/			
		{ register int itr,it,spind;;
			for(itr=0;itr<nwin;itr++) {
				spind=i+itr;
				for(it=0;it<nt;it++) {
					if(spind>0 && spind<ntr) 
							out_traces[spind][it] += spw[itr]*twin[itr][it];
						/*	out_traces[spind][it] = twin[itr][it]; */
				}
			}
		}

/*		fprintf(stderr," Trace #= %5d\n",i); */	
        }
	/* write output traces */
        erewind(headerfp);
	{       register int itr;
		for (itr=0; itr<ntr; itr++) {
			efread(&tr, 1, HDRBYTES, headerfp);
			for (it=0; it<nt; it++) 
				tr.data[it]=out_traces[itr][it];
			puttr(&tr);
		}
	}
	efclose(headerfp);
	if (istmpdir) eremove(headerfile);

	/* free allocated space */
	free2float(out_traces);
	free1float(spw);

	return EXIT_SUCCESS;

}
Beispiel #5
0
int main(int argc, char **argv)
{
    /* output file pointers */    
    FILE *rlfp=NULL, *taufp=NULL, *e21fp=NULL;
    FILE *e31fp=NULL, *e32fp=NULL, *plnfp=NULL;
    FILE *f1fp=NULL, *l1fp=NULL;
    FILE *thetafp=NULL, *phifp=NULL, *dirfp=NULL;
    FILE *erfp=NULL, *irfp=NULL, *qrfp=NULL;
    FILE *headerfp=NULL, *pfiltfp=NULL, *sfiltfp=NULL;
    FILE *nfiltfp=NULL, *efiltfp=NULL;
 //   FILE *pkur=NULL, *skur=NULL; 
        /* temporary file for trace headers */
			 /* (one 3C station only) */
        
    char *file=NULL;         /* base of output file name(s) */
    char *fname=NULL;        /* complete output file name */
    char *angle=NULL;        /* unit used for angles theta and phi */
    char *win=NULL;          /* shape of used time window */
    float fangle=0.0;   /* unit conversion factor applied to angles theta and phi */
    int iwin=0;           /* time window shape identifier */
        
    /* flags (see selfdoc) */
    int rl,theta,phi,tau,ellip,pln,f1,l1,dir,amp,verbose,all;
    
    int i,j,icomp;      /* indices for components (in loops) */
    int it;             /* index for time sample in main loop */
    int iwl;            /* correlation window length in samples */
    int nstat;          /* number of 3-component datasets */
    int nt;             /* number of time samples in one trace */
//    int kwl;            /* kurtosis window length in seconds */    


    float **data3c;     /* three-component data ([1..3][0..nt-1]) */
    float **a;          /* covariance matrix (a[1..3][1..3]) */
    float **v;          /* eigenvectors of covariance matrix (v[1..3][1..3]) */
    float *d;           /* the corresponding eigenvalues (d[1..3]) */
    float *w;           /* time window weights for correlation window */
    float dt;           /* sampling interval in seconds */
    float rlq;          /* contrast factor of rectilinearity */
    float wl;           /* correlation window length in seconds */
    

    float *data_e21=NULL;    /* main ellipticity */
    float *data_e31=NULL;    /* second ellipticity */
    float *data_e32=NULL;    /* transverse ellipticity */
    float *data_er=NULL;     /* eigenresultant */
    float *data_f1=NULL;     /* flatness coefficient */
    float *data_ir=NULL;     /* instantaneous resultant */
    float *data_l1=NULL;     /* linearity coefficient */
    float *data_phi=NULL;    /* horizontal azimuth phi */
    float *data_pln=NULL;    /* planarity */    
    float *data_qr=NULL;     /* quadratic resultant */
    float *data_rl=NULL;     /* rectilinearity factor */
    float *data_tau=NULL;    /* polarization parameter tau */
    float *data_theta=NULL;  /* inclination angle theta */
    float *data_pfilt=NULL;  /* P (vertical) polarization filter */
    float *data_sfilt=NULL;  /* S (horizontal) polarization filter */
    float *data_zfilt=NULL;  /* Z Filtered Trace */
    float *data_nfilt=NULL;  /* N Filtered Trace */
    float *data_efilt=NULL;  /* E Filtered Trace */
//    float *data_kwl=NULL;   /* Data for Kurtosis Window */
//    float *data_pkur=NULL;   /* Kurtosis detector for P */
//    float *data_skur=NULL;   /* Kurtosis detector for S */
    float **data3c_dir=NULL; /* 3 components of direction of polarization ([1..3][0..nt-1]) */
    
    /* initialize */
    initargs(argc, argv);
    requestdoc(1);
    
    /* get info from first trace */
    if(!gettr(&tr)) err("can't get first trace");
    nt = tr.ns;
       
    /* get parameters ... */
    if (!getparstring("file", &file)) file="polar";
    if (!getparstring("angle", &angle)) angle="rad";
    if (!getparstring("win", &win)) win="boxcar";
    if (!getparfloat("wl", &wl)) wl = 0.1;
    if (!getparfloat("dt", &dt)) dt = ((double) tr.dt)/1000000.0;
    if (!getparfloat("rlq", &rlq)) rlq = 1.0;
    if (!getparint("verbose", &verbose)) verbose = 0;
//    if (!getparint("kwl", &kwl)) kwl = 5 * ((int) 1/dt);    

    /* ... and output flags */
    if (!getparint("all", &all))       all = 0;
    if (!getparint("rl", &rl))          rl = (all) ? all : 1;
    if (!getparint("dir", &dir))       dir = (all) ? 1 : 1;
    if (!getparint("theta", &theta)) theta = (all) ? all : 0;
    if (!getparint("phi", &phi))       phi = (all) ? all : 0;
    if (!getparint("tau", &tau))       tau = (all) ? 1 : 0;
    if (!getparint("ellip", &ellip)) ellip = (all) ? 1 : 0;
    if (!getparint("pln", &pln))       pln = (all) ? 1 : 0;
    if (!getparint("f1", &f1))          f1 = (all) ? 1 : 0;
    if (!getparint("l1", &l1))          l1 = (all) ? 1 : 0;
    if (!getparint("amp", &amp))       amp = (all) ? 1 : 0;

    checkpars();


    /* get time window shape */
    if      (STREQ(win, "boxcar"))   iwin=WBOXCAR;
    else if (STREQ(win, "bartlett")) iwin=WBARTLETT;
    else if (STREQ(win, "hanning"))  iwin=WHANNING;
    else if (STREQ(win, "welsh"))    iwin=WWELSH;
    else err("unknown win=%s", win);
    
    /* get unit conversion factor for angles */
    if      (STREQ(angle, "rad")) fangle=1.0;
    else if (STREQ(angle, "deg")) fangle=180.0/PI;
    else if (STREQ(angle, "gon")) fangle=200.0/PI;
    else err("unknown angle=%s", angle);

    /* convert seconds to samples */
    if (!dt) {
        dt = 0.004;
        warn("dt not set, assuming dt=0.004");
    }

    iwl = NINT(wl/dt);
        
    /* data validation */
    if (iwl<1) err("wl=%g must be positive", wl);
    if (iwl>nt) err("wl=%g too long for trace", wl);
    if (!strlen(file)) err("file= not set and default overridden");

    /* echo some information */
    if (verbose && (theta || phi)) warn("computing angles in %s", angle);
    if (verbose) warn("%s window length = %d samples\n", win, iwl);
    
    if (rl && theta) warn("computing filtered phase");

    /* open temporary file for trace headers */
    headerfp = etmpfile();
    
    /* set filenames and open files */
    fname = malloc( strlen(file)+7 );
    sprintf(fname, "%s.rl", file);    if (rl) rlfp = efopen(fname, "w");
    sprintf(fname, "%s.theta", file); if (theta) thetafp = efopen(fname, "w");
    sprintf(fname, "%s.phi", file);   if (phi) phifp = efopen(fname, "w");
    sprintf(fname, "%s.tau", file);   if (tau) taufp = efopen(fname, "w");
    sprintf(fname, "%s.e21", file);   if (ellip) e21fp = efopen(fname, "w");
    sprintf(fname, "%s.e31", file);   if (ellip) e31fp = efopen(fname, "w");
    sprintf(fname, "%s.e32", file);   if (ellip) e32fp = efopen(fname, "w");
    sprintf(fname, "%s.pln", file);   if (pln) plnfp = efopen(fname, "w");
    sprintf(fname, "%s.f1", file);    if (f1) f1fp = efopen(fname, "w");
    sprintf(fname, "%s.l1", file);    if (l1) l1fp = efopen(fname, "w");
    sprintf(fname, "%s.dir", file);   if (dir) dirfp = efopen(fname, "w");
    sprintf(fname, "%s.er", file);    if (amp) erfp = efopen(fname, "w");
    sprintf(fname, "%s.ir", file);    if (amp) irfp = efopen(fname, "w");
    sprintf(fname, "%s.qr", file);    if (amp) qrfp = efopen(fname, "w");
    sprintf(fname, "%s.pfilt", file); if (rl && theta) pfiltfp = efopen(fname, "w");
    sprintf(fname, "%s.sfilt", file); if (rl && theta) sfiltfp = efopen(fname, "w");
    sprintf(fname, "%s.nfilt", file); if (rl && theta) nfiltfp = efopen(fname, "w");
    sprintf(fname, "%s.efilt", file); if (rl && theta) efiltfp = efopen(fname, "w");
  //  sprintf(fname, "%s.pkur", file); if (rl && theta) pkur = efopen(fname, "w");
  //  sprintf(fname, "%s.skur", file); if (rl && theta) skur = efopen(fname, "w");
    free(fname);
    
    /* allocate space for input data and analysis matrices */
    /* index ranges used here: data3c[1..3][0..nt-1],      */
    /* a[1..3][1..3], v[1..3][1..3], d[1..3]               */
    data3c = ealloc2float(nt,3); data3c-=1;
    a = ealloc2float(3,3); a[0]-=1; a-=1;
    v = ealloc2float(3,3); v[0]-=1; v-=1;    
    d = ealloc1float(3); d-=1;

    /* calculate time window weights */
    w = ealloc1float(iwl);
    memset((void *) w, 0, iwl*FSIZE);
    calc_window(w, iwl, iwin);

    /* allocate and zero out space for output data */
    if (rl) { 
        data_rl = ealloc1float(nt); 
        memset((void *) data_rl, 0, nt*FSIZE);
    }
    if (theta) {
        data_theta = ealloc1float(nt);
        memset((void *) data_theta, 0, nt*FSIZE);
    }
    if (phi) {
        data_phi = ealloc1float(nt);
        memset((void *) data_phi, 0, nt*FSIZE);
    }    
    if (tau) {
        data_tau = ealloc1float(nt);
        memset((void *) data_tau, 0, nt*FSIZE);
    }
    if (ellip) {
        data_e21 = ealloc1float(nt);
        data_e31 = ealloc1float(nt);
        data_e32 = ealloc1float(nt);
        memset((void *) data_e21, 0, nt*FSIZE);
        memset((void *) data_e31, 0, nt*FSIZE);
        memset((void *) data_e32, 0, nt*FSIZE);
    }
    if (pln) {
        data_pln = ealloc1float(nt);
        memset((void *) data_pln, 0, nt*FSIZE);
    }
    if (f1) {
        data_f1 = ealloc1float(nt);
        memset((void *) data_f1, 0, nt*FSIZE);
    }
    if (l1) {
        data_l1 = ealloc1float(nt);
        memset((void *) data_l1, 0, nt*FSIZE);
    }
    if (amp) {
        data_er = ealloc1float(nt);
        data_ir = ealloc1float(nt);
        data_qr = ealloc1float(nt);
        memset((void *) data_er, 0, nt*FSIZE);
        memset((void *) data_ir, 0, nt*FSIZE);
        memset((void *) data_qr, 0, nt*FSIZE);
    }
    if (dir) {
        data3c_dir = ealloc2float(nt,3); data3c_dir-=1;
        for (i=1;i<=3;i++) memset((void *) data3c_dir[i], 0, nt*FSIZE);
    }
    if (rl && theta) {
        data_pfilt = ealloc1float(nt);
        memset((void *) data_pfilt, 0, nt*FSIZE);
        data_sfilt = ealloc1float(nt);
        memset((void *) data_pfilt, 0, nt*FSIZE);
/*        data_3Cfilt = ealloc2float(nt,3);
        for (i=1;i<=3;i++) memset((void *) data_3Cfilt[i], 0, nt*FSIZE); */
        data_zfilt = ealloc1float(nt);
        data_nfilt = ealloc1float(nt);
        data_efilt = ealloc1float(nt);
        memset((void *) data_zfilt, 0, nt*FSIZE);
        memset((void *) data_nfilt, 0, nt*FSIZE);
        memset((void *) data_efilt, 0, nt*FSIZE);

 //       data_pkur = ealloc1float(nt);
//        data_skur = ealloc1float(nt);
//        memset((void *) data_pkur, 0, nt*FSIZE);
//        memset((void *) data_skur, 0, nt*FSIZE);
        /* Allocate data for kurtosis window arrays */
//        data_kwl = ealloc1float(iwl);
//        memset((void *) data_kwl, 0, kwl*FSIZE);        
    }        


/* ************************ BEGIN CALCULATION ******************************* */    

    /* loop over traces */
    icomp=0;
    nstat=0;
    // Need to convert this do while loop into a for loop so as to be easier
    // to parallelize
    warn("Trace Start Time: %d %d %d %d %d", tr.year, tr.day, tr.hour, tr.minute, tr.sec);    
    do {
        /* store trace header in temporary file and read data */
        efwrite(&tr, HDRBYTES, 1, headerfp);
        icomp++;
        memcpy((void *)data3c[icomp], (const void *) tr.data, nt*FSIZE);
                
        /* process 3-component dataset */
        if (icomp==3) {
            erewind(headerfp);
            icomp = 0;
            nstat++;
            if (verbose) 
                fprintf(stderr,"%s: analyzing station %d \r",argv[0], nstat);

            /* start loop over samples */

            for (it=iwl/2;it<nt-iwl/2;it++) {
                //warn("Sample %d", it);
                /* covariance matrix */

                for (i=1;i<=3;i++) 
                {
                    for (j=i;j<=3;j++) 
                    {
                        a[i][j]=a[j][i]=covar(data3c[i], data3c[j], it-iwl/2, iwl, w);                    
                    }
                }
                    
                /* compute eigenvalues and vectors */
                eig_jacobi(a,d,v,3);
                sort_eigenvalues(d,v,3);
                
                /* polarization parameters */
                if (rl) data_rl[it]=calc_rl(d,rlq,rl);
                if (theta) data_theta[it]=calc_theta(v, theta) * fangle;
                if (phi) data_phi[it]=calc_phi(v, phi) * fangle;
                if (tau) data_tau[it]=calc_tau(d);
                if (ellip) {
                    data_e21[it]=calc_ellip(d,2,1);
                    data_e31[it]=calc_ellip(d,3,1);
                    data_e32[it]=calc_ellip(d,3,2);
                }
                if (pln) data_pln[it]=calc_plan(d);
                if (f1) data_f1[it]=calc_f1(d);
                if (l1) data_l1[it]=calc_l1(d);
                if (amp) data_er[it]=calc_er(d);
                if (dir) calc_dir(data3c_dir,v,it);
                if (rl && theta) 
                {
                    data_zfilt[it] = data3c[1][it] * calc_pfilt(rl, theta);
                    data_nfilt[it] = data3c[2][it] * calc_sfilt(rl, theta);
                    data_efilt[it] = data3c[3][it] * calc_sfilt(rl, theta);
                    data_pfilt[it] = data_zfilt[it];
                    data_sfilt[it] = (data_nfilt[it] + data_efilt[it]) / 2;                 
//                    data_pkur[it] = kurtosiswindow(data_pfilt,data_kwl,it - kwl/2,kwl,nt);     
//                    data_skur[it] = kurtosiswindow(data_sfilt,data_kwl,it - kwl/2,kwl,nt);
               }             

            } /* end loop over samples */

            /* compute amplitude parameters */
            if (amp) ampparams(data3c, data_ir, data_qr, nt, iwl);

/* *************************** END CALCULATION ****************************** */
/* ***************************** BEGIN WRITE ******************************** */

            /* write polarization attributes to files */
            if (rl) fputdata(rlfp, headerfp, data_rl, nt);
            if (theta) fputdata(thetafp, headerfp, data_theta, nt);
            if (phi) fputdata(phifp, headerfp, data_phi, nt);
            if (tau) fputdata(taufp, headerfp, data_tau, nt);
            if (ellip) {
                fputdata(e21fp, headerfp, data_e21, nt);
                fputdata(e31fp, headerfp, data_e31, nt);
                fputdata(e32fp, headerfp, data_e32, nt);
            }
            if (pln) fputdata(plnfp, headerfp, data_pln, nt);
            if (f1) fputdata(f1fp, headerfp, data_f1, nt);
            if (l1) fputdata(l1fp, headerfp, data_l1, nt);
            if (amp) {
                fputdata(erfp, headerfp, data_er, nt);
                fputdata(irfp, headerfp, data_ir, nt);
                fputdata(qrfp, headerfp, data_qr, nt);
            }
            if (dir) fputdata3c(dirfp, headerfp, data3c_dir, nt);
            if (rl && theta) 
            {
                fputdata(pfiltfp, headerfp, data_pfilt, nt);
                fputdata(sfiltfp, headerfp, data_sfilt, nt);
                fputdata(nfiltfp, headerfp, data_nfilt, nt);
                fputdata(efiltfp, headerfp, data_efilt, nt);
               // fputdata(pkur, headerfp, data_pkur, nt);
               // fputdata(skur, headerfp, data_skur, nt);
            }

/* ****************************** END WRITE ********************************* */
            
        } /* end of processing three-component dataset */
        
    } while (gettr(&tr)); /* end loop over traces */
    
    if (verbose) {
        fprintf(stderr,"\n");
        if (icomp) warn("last %d trace(s) skipped", icomp);
    }
    
    /* close files */
    efclose(headerfp);
    if (rl) efclose(rlfp);
    if (theta) efclose(thetafp);
    if (phi) efclose(phifp);
    if (tau) efclose(taufp);
    if (ellip) {
        efclose(e21fp);
        efclose(e31fp);
        efclose(e32fp);
    }
    if (pln) efclose(plnfp);
    if (f1) efclose(f1fp);
    if (l1) efclose(l1fp);
    if (amp) {
        efclose(erfp);
        efclose(irfp);
        efclose(qrfp);
    }
    if (dir) efclose(dirfp);
    if (rl && theta) {
        efclose(pfiltfp);
        efclose(sfiltfp);
//        efclose(pkur);
//        efclose(skur);
        }  
    return(CWP_Exit());
}
Beispiel #6
0
int
main(int argc, char **argv)
{
	int nt;		/* number of time samples per trace */
	float dt;	/* time sampling interval */
	float ft;	/* time of first sample */
	int it;		/* time sample index */
	int cdpmin;	/* minimum cdp to process */
	int cdpmax;	/* maximum cdp to process */
	float dx;	/* cdp sampling interval */
	int nx;		/* number of cdps to process */
	int nxfft;	/* number of cdps after zero padding for fft */
	int nxpad;	/* minimum number of cdps for zero padding */
	int ix;		/* cdp index, starting with ix=0 */
	int noffmix;	/* number of offsets to mix */
	float *tdmo;	/* times at which rms velocities are specified */
	float *vdmo;	/* rms velocities at times specified in tdmo */
	float gamma;	/* upgoing to downging velocity ratio */
	float *zoh=NULL;/* tabulated z/h */
	float *boh=NULL;/* tabulated b/h */
	int ntable;	/* number of tabulated zoh and boh */
	float sdmo;	/* DMO stretch factor */
	float s1;	/* DMO stretch factor */
	float s2;	/* DMO stretch factor */
	float temps;	/* temp value used in excahnging s1 and s2 */
	int flip;	/* apply negative shifts and exchange s1 and s2 */
	float sign; 	/* + if flip=0, negative if flip=1 */
	int ntdmo;	/* number tnmo values specified */
	int itdmo;	/* index into tnmo array */
	int nvdmo;	/* number vnmo values specified */
	float fmax;	/* maximum frequency */
	float *vrms;	/* uniformly sampled vrms(t) */
	float **p;	/* traces for one offset - common-offset gather */
	float **q;	/* DMO-corrected and mixed traces to be output */
	float offset;	/* source-receiver offset of current trace */
	float oldoffset;/* offset of previous trace */
	int noff;	/* number of offsets processed in current mix */
	int ntrace;	/* number of traces processed in current mix */
	int itrace;	/* trace index */
	int gottrace;	/* non-zero if an input trace was read */
	int done;	/* non-zero if done */
	int verbose;	/* =1 for diagnostic print */
	FILE *hfp;	/* file pointer for temporary header file */

	/* hook up getpar */
	initargs(argc, argv);
	requestdoc(1);

	/* get information from the first header */
	if (!gettr(&tr)) err("can't get first trace");
	nt = tr.ns;
	dt = tr.dt/1000000.0;
	ft = tr.delrt/1000.0;
	offset = tr.offset;

	/* get parameters */
	if (!getparint("cdpmin",&cdpmin)) err("must specify cdpmin");
	if (!getparint("cdpmax",&cdpmax)) err("must specify cdpmax");
	if (cdpmin>cdpmax) err("cdpmin must not be greater than cdpmax");
	if (!getparfloat("dxcdp",&dx)) err("must specify dxcdp");
	if (!getparint("noffmix",&noffmix)) err("must specify noffmix");
	ntdmo = countparval("tdmo");
	if (ntdmo==0) ntdmo = 1;
	tdmo = ealloc1float(ntdmo);
	if (!getparfloat("tdmo",tdmo)) tdmo[0] = 0.0;
	nvdmo = countparval("vdmo");
	if (nvdmo==0) nvdmo = 1;
	if (nvdmo!=ntdmo) err("number of tdmo and vdmo must be equal");
	vdmo = ealloc1float(nvdmo);
	if (!getparfloat("vdmo",vdmo)) vdmo[0] = 1500.0;
	for (itdmo=1; itdmo<ntdmo; ++itdmo)
		if (tdmo[itdmo]<=tdmo[itdmo-1])
			err("tdmo must increase monotonically");
	if (!getparfloat("gamma",&gamma)) gamma = 0.5;
	if (!getparint("ntable",&ntable)) ntable = 1000;
	if (!getparfloat("sdmo",&sdmo)) sdmo = 1.0;
	if (!getparint("flip",&flip)) flip=0;
	if (flip)
		sign = -1.0;
	else
		sign = 1.0;
	if (!getparfloat("fmax",&fmax)) fmax = 0.5/dt;
	if (!getparint("verbose",&verbose)) verbose=0;
        checkpars();

	/* allocate and generate tables of b/h and z/h if gamma not equal 1 */
	if(gamma != 1.0){
		zoh=alloc1float(ntable);
		boh=alloc1float(ntable);
		table(ntable, gamma, zoh, boh);
	}

	/* make uniformly sampled rms velocity function of time */
	vrms = ealloc1float(nt);
	mkvrms(ntdmo,tdmo,vdmo,nt,dt,ft,vrms);

	/* determine number of cdps to process */
	nx = cdpmax-cdpmin+1;

	/* allocate and zero common-offset gather p(t,x) */
	nxpad = 0.5*ABS(offset/dx);
	nxfft = npfar(nx+nxpad);
	p = ealloc2float(nt,nxfft+2);
	for (ix=0; ix<nxfft; ++ix)
		for (it=0; it<nt; ++it)
			p[ix][it] = 0.0;

	/* allocate and zero offset mix accumulator q(t,x) */
	q = ealloc2float(nt,nx);
	for (ix=0; ix<nx; ++ix)
		for (it=0; it<nt; ++it)
			q[ix][it] = 0.0;

	/* open temporary file for headers */
	hfp = tmpfile();

	/* initialize */
	oldoffset = offset;
	gottrace = 1;
	done = 0;
	ntrace = 0;
	noff = 0;

	/* get DMO stretch/squeeze factors s1 and s2 */
	stretchfactor (sdmo,gamma,&s1,&s2);
	if(flip) {
		temps = s1;
		s1 = s2;
		s2 = temps;
	}

	/* print useful information if requested */
	if (verbose)fprintf(stderr,"stretching factors: s1=%f s2=%f\n",s1,s2);

	/* loop over traces */
	do {
		/* if got a trace, determine offset */
		if (gottrace) offset = tr.offset;

		/* if an offset is complete */
		if ((gottrace && offset!=oldoffset) || !gottrace) {

			/* do dmo for old common-offset gather */
			dmooff(oldoffset,fmax,nx,dx,nt,dt,ft,vrms,p,
			gamma,boh,zoh,ntable,s1,s2,sign);

			/* add dmo-corrected traces to mix */
			for (ix=0; ix<nx; ++ix)
				for (it=0; it<nt; ++it)
					q[ix][it] += p[ix][it];

			/* count offsets in mix */
			noff++;

			/* free space for old common-offset gather */
			free2float(p);

			/* if beginning a new offset */
			if (offset!=oldoffset) {

				/* allocate space for new offset */
				nxpad = 0.5*ABS(offset/dx);
				nxfft = npfar(nx+nxpad);
				p = ealloc2float(nt,nxfft+2);
				for (ix=0; ix<nxfft; ++ix)
					for (it=0; it<nt; ++it)
						p[ix][it] = 0.0;
			}
		}

		/* if a mix of offsets is complete */
		if (noff==noffmix || !gottrace) {

			/* rewind trace header file */
			efseeko(hfp, (off_t) 0,SEEK_SET);

			/* loop over all output traces */
			for (itrace=0; itrace<ntrace; ++itrace) {

				/* read trace header and determine cdp index */
				efread(&tro,HDRBYTES,1,hfp);

				/* get dmo-corrected data */
				memcpy((void *) tro.data,
					(const void *) q[tro.cdp-cdpmin],
					nt*sizeof(float));

				/* write output trace */
				puttr(&tro);
			}

			/* report */
			if (verbose)
				fprintf(stderr,"\tCompleted mix of "
					"%d offsets with %d traces\n",
					noff,ntrace);

			/* if no more traces, break */
			if (!gottrace) break;

			/* rewind trace header file */
			efseeko(hfp, (off_t) 0,SEEK_SET);

			/* reset number of offsets and traces in mix */
			noff = 0;
			ntrace = 0;

			/* zero offset mix accumulator */
			for (ix=0; ix<nx; ++ix)
				for (it=0; it<nt; ++it)
					q[ix][it] = 0.0;
		}

		/* if cdp is within range to process */
		if (tr.cdp>=cdpmin && tr.cdp<=cdpmax) {

			/* save trace header and update number of traces */
			efwrite(&tr,HDRBYTES,1,hfp);
			ntrace++;

			/* remember offset */
			oldoffset = offset;

			/* get trace samples */
			memcpy((void *) p[tr.cdp-cdpmin],
				(const void *) tr.data, nt*sizeof(float));
		}

		/* get next trace (if there is one) */
		if (!gettr(&tr)) gottrace = 0;

	} while (!done);

	return(CWP_Exit());
}
Beispiel #7
0
int
main (int argc, char **argv)
{
	int 	nr,ir,ls,smooth,ndpfz,ns,ixo,ixm,nxo,nxm,nt,
		nx,nz,nxb,nxd,ixd,i,ix,iz,nx1,nx0,nxd1,
		verbose,tracl,
		*nxz;
	float   tmin,temp,temp1,
		dsmax,fpeak,dx,dz,fx,ex,fx1,ex1,
		dxm,dxo,dt,fxm,fxo,ft,xo,xm,vs0,vg0,
		xs,xg,ez,
		*ar,**xr,**zr,
		**vold,**ts,**as,**sgs,**tg,**ag,**sgg,**bas,**bag,
		**v,**ts1=NULL,**as1=NULL,**sgs1=NULL,
		**tg1=NULL,**ag1=NULL,**sgg1=NULL;
	FILE *vfp=stdin;
	Reflector *r;
	Wavelet *w;

	/* hook up getpar to handle the parameters */
	initargs(argc,argv);
	requestdoc(0);

	/* get required parameters */
	if (!getparint("nx",&nx)) err("must specify nx!\n");
	if (!getparint("nz",&nz)) err("must specify nz!\n");
	
	/* get optional parameters */
	if (!getparint("nxb",&nxb)) nxb = nx;
	if (!getparint("nxd",&nxd)) nxd = 1;
	if (!getparfloat("dx",&dx)) dx = 100;
	if (!getparfloat("fx",&fx)) fx = 0.0;
	if (!getparfloat("dz",&dz)) dz = 100;
	if (!getparint("nt",&nt)) nt = 101; CHECK_NT("nt",nt);
	if (!getparfloat("dt",&dt)) dt = 0.04;
	if (!getparfloat("ft",&ft)) ft = 0.0;
	if (!getparint("nxo",&nxo)) nxo = 1;
	if (!getparfloat("dxo",&dxo)) dxo = 50;
	if (!getparfloat("fxo",&fxo)) fxo = 0.0;
	if (!getparint("nxm",&nxm)) nxm = 101;
	if (!getparfloat("dxm",&dxm)) dxm = 50;
	if (!getparfloat("fxm",&fxm)) fxm = 0.0;
	if (!getparfloat("fpeak",&fpeak)) fpeak = 0.2/dt;
	if (!getparint("ls",&ls)) ls = 0;
	if (!getparfloat("tmin",&tmin)) tmin = 10.0*dt;
	if (!getparint("ndpfz",&ndpfz)) ndpfz = 5;
	if (!getparint("smooth",&smooth)) smooth = 0;
	if (!getparint("verbose",&verbose)) verbose = 0;
	
	/* check the ranges of shots and receivers */
	ex = fx+(nx-1)*dx;
	ez = (nz-1)*dz;
 	for (ixm=0; ixm<nxm; ++ixm) 
		for (ixo=0; ixo<nxo; ++ixo) {
			/* compute source and receiver coordinates */
			xs = fxm+ixm*dxm-0.5*(fxo+ixo*dxo);
			xg = xs+fxo+ixo*dxo;
			if (fx>xs || ex<xs || fx>xg || ex<xg) 
		err("shot or receiver lie outside of specified (x,z) grid\n");
	} 
		
	
	decodeReflectors(&nr,&ar,&nxz,&xr,&zr);
	if (!smooth) breakReflectors(&nr,&ar,&nxz,&xr,&zr);

	/* allocate space */
	vold = ealloc2float(nz,nx);
	/* read velocities */
	if(fread(vold[0],sizeof(float),nx*nz,vfp)!=nx*nz)
		err("cannot read %d velocities from file %s",nx*nz,vfp);
	/* determine maximum reflector segment length */
	tmin = MAX(tmin,MAX(ft,dt));
	dsmax = vold[0][0]/(2*ndpfz)*sqrt(tmin/fpeak);
 	
	/* make reflectors */
	makeref(dsmax,nr,ar,nxz,xr,zr,&r);

	/* count reflector segments */
	for (ir=0,ns=0; ir<nr; ++ir)
		ns += r[ir].ns;

	/* make wavelet */
	makericker(fpeak,dt,&w);
	
	/* if requested, print information */
	if (verbose) {
		warn("\nSUSYNVXZ:");
		warn("Total number of small reflecting segments is %d.\n",ns);
	}
	
	/* set constant segy trace header parameters */
	memset( (void *) &tr, 0, sizeof(segy));
	tr.trid = 1;
	tr.counit = 1;
	tr.ns = nt;
	tr.dt = 1.0e6*dt;
	tr.delrt = 1.0e3*ft;
	
	/* allocate space */
	nx1 = 1+2*nxb;
	ts = ealloc2float(nz,nx1);
	as = ealloc2float(nz,nx1);
	sgs = ealloc2float(nz,nx1);
	tg = ealloc2float(nz,nx1);
	ag = ealloc2float(nz,nx1);
	sgg = ealloc2float(nz,nx1);
	v = ealloc2float(nz,nx1);
	bas = ealloc2float(nz,nx1);
	bag = ealloc2float(nz,nx1);
	if(nxd>1) {
		/* allocate space for interpolation */
		ts1 = ealloc2float(nz,nx1);
		as1 = ealloc2float(nz,nx1);
		sgs1 = ealloc2float(nz,nx1);
		tg1 = ealloc2float(nz,nx1);
		ag1 = ealloc2float(nz,nx1);
		sgg1 = ealloc2float(nz,nx1);
  	}
		

	/* loop over offsets and midpoints */
	for (ixo=0, tracl=0; ixo<nxo; ++ixo){
	    xo = fxo+ixo*dxo;
	    if(ABS(xo)>nxb*dx) err("\t band NXB is too small!\n");
	    nxd1 = nxd;
	    for (ixm=0; ixm<nxm; ixm +=nxd1){
		xm = fxm+ixm*dxm;
   		xs = xm-0.5*xo;
		xg = xs+xo;
		/* set range for traveltimes' calculation */
		fx1 = xm-nxb*dx;
		ex1 = MIN(ex+(nxd1-1)*dxm,xm+nxb*dx);
		nx1 = 1+(ex1-fx1)/dx;	
		nx0 = (fx1-fx)/dx;
		temp = (fx1-fx)/dx-nx0;
		/* transpose velocity such that the first row is at fx1 */
		for(ix=0;ix<nx1;++ix)
		    for(iz=0;iz<nz;++iz){
		    	if(ix<-nx0) 
			   	v[ix][iz] = vold[0][iz];
		    	else if(ix+nx0>nx-2) 
				v[ix][iz]=vold[nx-1][iz];
			else
				v[ix][iz] = vold[ix+nx0][iz]*(1.0-temp)
					+temp*vold[ix+nx0+1][iz];
		    }
			
		if(ixm==0 || nxd1==1){
		/* No interpolation */
	
			/* compute traveltimes, propagation angles, sigmas 
	  		 from shot and receiver respectively	*/
			eiktam(xs,0.,nz,dz,0.,nx1,dx,fx1,v,ts,as,sgs,bas);
			eiktam(xg,0.,nz,dz,0.,nx1,dx,fx1,v,tg,ag,sgg,bag);
			ixd = NINT((xs-fx)/dx);
			vs0 = vold[ixd][0];
			ixd = NINT((xg-fx)/dx);
			vg0 = vold[ixd][0];
				
			/* make one trace */
			ex1 = MIN(ex,xm+nxb*dx);
			makeone(ts,as,sgs,tg,ag,sgg,ex1,ez,dx,dz,fx1,vs0,vg0,
				ls,w,nr,r,nt,dt,ft,tr.data);
			/* set segy trace header parameters */
			tr.tracl = tr.tracr = ++tracl;
			tr.cdp = 1+ixm;
			tr.cdpt = 1+ixo;
			tr.offset = NINT(xo);
			tr.sx = NINT(xs);
			tr.gx = NINT(xg);
			/* write trace */
			puttr(&tr);
		}
		else {
			/* Linear interpolation */
			
			eiktam(xs,0,nz,dz,0,nx1,dx,fx1,v,ts1,as1,sgs1,bas);
			eiktam(xg,0,nz,dz,0,nx1,dx,fx1,v,tg1,ag1,sgg1,bag);
			ixd = NINT((xs-fx)/dx);
			vs0 = vold[ixd][0];
			ixd = NINT((xg-fx)/dx);
			vg0 = vold[ixd][0];
			
		    	xm -= nxd1*dxm;
		    for(i=1; i<=nxd1; ++i) {
		    	xm += dxm;
			xs = xm-0.5*xo;
			xg = xs+xo;
			fx1 = xm-nxb*dx;	
			ex1 = MIN(ex+(nxd1-1)*dxm,xm+nxb*dx);
			nx1 = 1+(ex1-fx1)/dx;	
			temp = nxd1-i;
			temp1 = 1.0/(nxd1-i+1);
			for(ix=0;ix<nx1;++ix)
			    for(iz=0;iz<nz;++iz){
			    if(i==nxd1){
			   	ts[ix][iz] = ts1[ix][iz];
			   	tg[ix][iz] = tg1[ix][iz];
			   	sgs[ix][iz] = sgs1[ix][iz];
			   	ag[ix][iz] = ag1[ix][iz];
			   	sgg[ix][iz] = sgg1[ix][iz];
			   	as[ix][iz] = as1[ix][iz];
				}
			    else{
			   	ts[ix][iz] = (temp*ts[ix][iz]
					+ts1[ix][iz])*temp1;
			   	tg[ix][iz] = (temp*tg[ix][iz]
					+tg1[ix][iz])*temp1;
			    	as[ix][iz] = (temp*as[ix][iz]
					+as1[ix][iz])*temp1;
			   	sgs[ix][iz] = (temp*sgs[ix][iz]
					+sgs1[ix][iz])*temp1;
			   	ag[ix][iz] = (temp*ag[ix][iz]
					+ag1[ix][iz])*temp1;
			   	sgg[ix][iz] = (temp*sgg[ix][iz]
					+sgg1[ix][iz])*temp1;
				}
			}
				
			/* make one trace */
			ex1 = MIN(ex,xm+nxb*dx);
			makeone(ts,as,sgs,tg,ag,sgg,ex1,ez,dx,dz,fx1,vs0,vg0,
				ls,w,nr,r,nt,dt,ft,tr.data);
			/* set segy trace header parameters */
			tr.tracl = tr.tracr = ++tracl;
			tr.cdp = 1+ixm-nxd1+i;
			tr.cdpt = 1+ixo;
			tr.offset = NINT(xo);
			tr.d2=dxm;
			tr.f2=fxm;
			tr.sx = NINT(xs);
			tr.gx = NINT(xg);
			/* write trace */
			puttr(&tr);
		    }
		}
		    /* set skip parameter */
		    if(ixm<nxm-1 && ixm>nxm-1-nxd1) nxd1 = nxm-1-ixm;

	    }
	    warn("\t finish offset %f\n",xo);
	}

	free2float(vold);
	free2float(ts);
	free2float(bas);
	free2float(sgs);
	free2float(as);
	free2float(v);
	free2float(tg);
	free2float(bag);
	free2float(sgg);
	free2float(ag);
	if(nxd>1) {
		free2float(ts1);
		free2float(as1);
		free2float(sgs1);
		free2float(tg1);
		free2float(ag1);
		free2float(sgg1);
  	}
	return(CWP_Exit());
}
Beispiel #8
0
int main (int argc, char **argv)
{
	int nt,it,np,ntau,itau,nx,ix,nk,nkmax,ik,
		ntfft,nxfft,nv,trans,norm,conv,verbose;
	float dt,dx,dpx,k,dk,kscl,t,*tt,*vt,*tmig,*vmig,
		(*vsind)[4],**ptx,**divcor;
	complex **ptk;
	char *vfile="";
	FILE *hfp,*tfp;
	
	/* hook up getpar */
	initargs(argc,argv);
	requestdoc(1);

	/* get information from the first header */
	if (!gettr(&tr)) err("can't get first trace");
	nt = tr.ns;
	dt = tr.dt/1000000.0;

	/* get parameters */
	if (!getparfloat("dxcdp",&dx)) err("dxcdp required");
	if (!getparint("np",&np)) np=50;
	if (!getparint("trans",&trans)) trans=0;
	if (!getparint("norm",&norm)) norm=1;
	if (!getparint("conv",&conv)) conv=0;
	if (!getparint("verbose",&verbose)) verbose=0;

	/* get velocity function */
	vt=ealloc1float(nt);
	tt=ealloc1float(nt);
	for (it=0; it<nt; it++)
		tt[it]=it*dt;
	if (!getparstring ("vfile",&vfile)){
		ntau = countparval("tmig");

		if (ntau==0) ntau=1;
		tmig = ealloc1float(ntau);

		if (!getparfloat("tmig",tmig)) tmig[0] = 0.0;

		nv = countparval("vmig");

		if (nv==0) nv=1;

		if (nv!=ntau) 
			err("number of tmig and vmig must be equal");

		vmig = ealloc1float(nv);

		if (!getparfloat("vmig",vmig)) vmig[0] = 1500.0;
		for (itau=1; itau<ntau; itau++)
			if (tmig[itau]<=tmig[itau-1])
			    err("tmig must increase monotonically");
		for (it=0,t=0.0; it<nt; ++it,t+=dt)
			intlin(ntau,tmig,vmig,vmig[0],vmig[ntau-1],
				1,&t,&vt[it]);
		if (ntau!=nt){
			vsind = (float (*)[4])ealloc1float(ntau*4);
			cmonot(ntau,tmig,vmig,vsind);
			intcub(0,ntau,tmig,vsind,nt,tt,vt);
		}
	} else{
		if (fread(vt,sizeof(float),nt,fopen(vfile,"r"))!=nt)
			err("Not %d velocities in file %s",nt,vfile);
	}

	/* copy traces and headers to temporary files */
	tfp = tmpfile();
	hfp = tmpfile();
	nx = 0;
	do {
		nx++;
		fwrite(&tr,HDRBYTES,1,hfp);
		fwrite(tr.data,sizeof(float),nt,tfp);

	} while(gettr(&tr));
  	fseek(hfp,0L,SEEK_SET);
	fseek(tfp,0L,SEEK_SET);
	if (verbose) fprintf(stderr,"\t%d traces input\n",nx);

	/* determine wavenumber and frequency sampling */
	nxfft = npfar(nx);
	ntfft = npfa(nt);
	nk = nxfft/2+1;
	dx *= 0.001;
	dk = 2.0*PI/(nxfft*dx);

	/* allocate space for Fourier transform */
	ptk = ealloc2complex(nt,nk);
	ptx = ealloc1(nxfft,sizeof(float*));
	for (ix=0; ix<nxfft; ++ix)
		ptx[ix] = (float*)ptk[0]+ix*nt;

	/* allocate space for divergence correction */
	divcor=ealloc2float(nt,np);

	/* build table of divergence corrections */
	divcortable(nt,np,dt,tt,vt,divcor,trans,norm);	

	/* apply conventional correction if required */
	if (conv==1){
		for (ix=0; ix<nx; ++ix){
			efread(ptx[ix],sizeof(float),nt,tfp);
		for (it=0; it<nt; ++it)
			ptx[ix][it] *= divcor[0][it];
		}
	} else {
		/* read and apply fft scaling to traces */ 
		kscl = 1.0/nxfft;
		for (ix=0; ix<nx; ++ix) {
			efread(ptx[ix],sizeof(float),nt,tfp);
			for (it=0; it<nt; ++it)
				ptx[ix][it] *= kscl;
		}
		/* pad with zeros */
		for (ix=nx; ix<nxfft; ++ix)
			for (it=0; it<nt; ++it)
				ptx[ix][it] = 0.0;

		/* Fourier transform ptx(t,x) to ptk(t,k) */
		pfa2rc(-1,2,nt,nxfft,ptx[0],ptk[0]);
		if (verbose) fprintf(stderr,"\tFourier transform done\n");

		/* define relevant k range */
		nkmax = MIN(nk,NINT(PI/dt/vt[0]/dk));
		dpx = 1.0/(np-1)/vt[0];
		fprintf(stderr,
			"nkmax %d nk %d dk %f dpx %f \n",nkmax,nk,dk,dpx);

		/* special case k=0 */
		for (it=0; it<nt; it++){
			ptk[0][it].r *= divcor[0][it];
			ptk[0][it].i *= divcor[0][it];
		}
	
		/* loop over wavenumbers */
		for (ik=1,k=dk; ik<nkmax; ++ik,k+=dk){

			/* report */
			if (verbose && ik%(nkmax/10>0?nkmax/10:1)==0)
				fprintf(stderr,"\t%d of %d wavenumbers done\n",
						ik,nkmax);
		
			/* dip filter divergence correction */
			dipfilt(k,dpx,dt,np,ntfft,nt,divcor,ptk[ik],ptk[ik]);
		}

		/* Fourier transform p(t,k) to p(t,x) */
		pfa2cr(1,2,nt,nxfft,ptk[0],ptx[0]);
		if (verbose) 
			fprintf(stderr,"\tinverse Fourier transform done\n");
	} /* end else dipdivcor */

	/* output migrated traces with headers */
	for (ix=0; ix<nx; ++ix) {
		efread(&tr,HDRBYTES,1,hfp);
		memcpy((void *) tr.data,
			(const void *) ptx[ix], nt*sizeof(float));
		puttr(&tr);
	}
	
	return EXIT_SUCCESS;
}
Beispiel #9
0
int main(int argc, char **argv)
{
  int verbose;
  time_t start,finish;
  double elapsed_time;
  int ix,nt,nx,nx_out;
  float dt,dh,hmin,hmax;
  float *h,*h_out;
  float **din,**dout,**din_tw,**dout_tw;
  int *ih,*ih_out;
  int padt,padx;
  int Ltw,Dtw;   
  int twstart;
  float taper;
  int itw,Itw,Ntw,niter;
  float fmin,fmax;

  /********/    
  fprintf(stderr,"*******SUALFT*********\n");
  /* Initialize */
  initargs(argc, argv);
  requestdoc(1);
  start=time(0);    
  /* Get parameters */
  if (!getparint("verbose", &verbose)) verbose = 0;
  if (!getparint("nx", &nx)) nx = 10000;
  if (!getparfloat("dh", &dh)) dh = 10;
  if (!gettr(&tr)) err("can't read first trace");
  if (!tr.dt) err("dt header field must be set");
  if (!tr.ns) err("ns header field must be set");
  if (!getparint("Ltw", &Ltw))  Ltw = 200; /* length of time window in samples */
  if (!getparint("Dtw", &Dtw))  Dtw = 10; /* overlap of time windows in samples	*/
  dt   = ((float) tr.dt)/1000000.0;
  nt = (int) tr.ns;
  if (!getparint("padt", &padt)) padt = 2; /* padding factor in time dimension*/
  if (!getparint("padx", &padx)) padx = 2; /* padding factor in spatial dimension*/
  if (!getparfloat("fmin",&fmin)) fmin = 0;
  if (!getparfloat("fmax",&fmax)) fmax = 0.5/dt;
  if (!getparint("niter", &niter)) niter = 100;
  fmax = MIN(fmax,0.5/dt);

  din   = ealloc2float(nt,nx);
  h        = ealloc1float(nx);
  ih       = ealloc1int(nx);
  /* ***********************************************************************
  input data
  *********************************************************************** */
  ix=0;
  do {
    h[ix]=(float)  tr.offset;
    memcpy((void *) din[ix],(const void *) tr.data,nt*sizeof(float));
    ix++;
    if (ix > nx) err("Number of traces > %d\n",nx); 
  } while (gettr(&tr));
  erewind(stdin);
  nx=ix;
  if (verbose) fprintf(stderr,"processing %d traces \n", nx);
  hmin = h[0];
  hmax = h[0];  
 
  for (ix=0;ix<nx;ix++){
  	if (hmin>h[ix]) hmin = h[ix]; 
  	if (hmax<h[ix]) hmax = h[ix]; 
  }
  for (ix=0;ix<nx;ix++){
  	ih[ix] = (int) truncf((h[ix]-hmin)/dh);
  }
  nx_out = 0;
  for (ix=0;ix<nx;ix++){
  	if (nx_out<ih[ix]) nx_out = ih[ix] + 1; 
  }
  nx_out = nx_out + 1;
  ih_out = ealloc1int(nx_out);
  h_out = ealloc1float(nx_out);

  for (ix=0;ix<nx_out;ix++){
  	ih_out[ix] = ix;
  	h_out[ix] = ix*dh + hmin;
  }

  dout  = ealloc2float(nt,nx_out);

  Ntw = 9999;	
  /* number of time windows (will be updated during first 
  iteration to be consistent with total number of time samples
  and the length of each window) */
  
  din_tw = ealloc2float(Ltw,nx);
  dout_tw = ealloc2float(Ltw,nx_out);

/***********************************************************************
process using sliding time windows
***********************************************************************/
 twstart = 0;
 taper = 0;
 for (Itw=0;Itw<Ntw;Itw++){	
   if (Itw == 0){
	 Ntw = (int) truncf(nt/(Ltw-Dtw));
	 if ( (float) nt/(Ltw-Dtw) - (float) Ntw > 0) Ntw++;
   }		
   twstart = (int) Itw * (int) (Ltw-Dtw);
   if ((twstart+Ltw-1 >nt) && (Ntw > 1)){
   	 twstart=nt-Ltw;
   }
   if (Itw*(Ltw-Dtw+1) > nt){
      Ltw = (int) Ltw + nt - Itw*(Ltw-Dtw+1);
   }
   for (ix=0;ix<nx;ix++){ 
     for (itw=0;itw<Ltw;itw++){
       din_tw[ix][itw] = din[ix][twstart+itw];
     }
   }
   fprintf(stderr,"processing time window %d of %d\n",Itw+1,Ntw);
   if (verbose) fprintf(stderr,"Ltw=%d\n",Ltw);
   if (verbose) fprintf(stderr,"Dtw=%d\n",Dtw);
   process_time_window(din_tw,dout_tw,h,h_out,hmin,hmax,dt,Ltw,nx,nx_out,fmin,fmax,niter,padt,padx,verbose); 
   if (Itw==0){ 
     for (ix=0;ix<nx_out;ix++){ 
       for (itw=0;itw<Ltw;itw++){   
	     dout[ix][twstart+itw] = dout_tw[ix][itw];
       }	 	 
     }
   }
   else{
     for (ix=0;ix<nx_out;ix++){ 
       for (itw=0;itw<Dtw;itw++){   /* taper the top of the time window */
	     taper = (float) ((Dtw-1) - itw)/(Dtw-1); 
	     dout[ix][twstart+itw] = dout[ix][twstart+itw]*(taper) + dout_tw[ix][itw]*(1-taper);
       }
       for (itw=Dtw;itw<Ltw;itw++){   
	     dout[ix][twstart+itw] = dout_tw[ix][itw];
       }
     }	 	 
   }
 }
 /***********************************************************************
 end of processing time windows
 ***********************************************************************/

  /* ***********************************************************************
  output data
  *********************************************************************** */
  rewind(stdin);
  for (ix=0;ix<nx_out;ix++){ 
    memcpy((void *) tr.data,(const void *) dout[ix],nt*sizeof(float));
    tr.offset=(int) h_out[ix];
    tr.ntr=nx_out;
    tr.ns=nt;
    tr.dt = NINT(dt*1000000.);
    tr.tracl = ix+1;
    tr.tracr = ix+1;    
    fputtr(stdout,&tr);
  }
  
  /******** End of output **********/
  finish=time(0);
  elapsed_time=difftime(finish,start);
  fprintf(stderr,"Total time required: %6.2fs\n", elapsed_time);
  
  free1float(h);
  free1float(h_out);
  free2float(din);
  free2float(dout);
  free1int(ih);
  free1int(ih_out);
  free2float(din_tw);
  free2float(dout_tw);
  
  return EXIT_SUCCESS;
}
Beispiel #10
0
int main( int argc, char *argv[] )
{
	cwp_String key;		/* header key word from segy.h		*/
	cwp_String type;	/* ... its type				*/
	Value val;
	segy **rec_o;		/* trace header+data matrix */
	int first=0;		/* true when we passed the first gather */
	int ng=0;
	float dt;
	int nt;
	int ntr;
	
	int nfft=0;		/* lenghth of padded array */
	float snfft;		/* scale factor for inverse fft */
	int nf=0;		/* number of frequencies */
	float d1;		/* frequency sampling int. */
	float *rt;		/* real trace */
	complex *ct;	       /* complex trace */
	complex **fd;		/* frequency domain data */
	float **cc;		/* correlation coefficinet matrix */
	
	float padd;
	float cch;
	float ccl;
		
	/* Initialize */
	initargs(argc, argv);
	requestdoc(1);
	
	if (!getparstring("key", &key))	key = "ep";
	if (!getparfloat("padd", &padd)) padd = 25.0;
	padd = 1.0+padd/100.0;
	
	if (!getparfloat("cch", &cch)) cch = 1.0;
	if (!getparfloat("ccl", &ccl)) ccl = 0.3;
	
	/* get the first record */
	rec_o = get_gather(&key,&type,&val,&nt,&ntr,&dt,&first);
	if(ntr==0) err("Can't get first record\n");
	
	/* set up the fft */
	nfft = npfar(nt*padd);
        if (nfft >= SU_NFLTS || nfft >= PFA_MAX)
               	err("Padded nt=%d--too big", nfft);
        nf = nfft/2 + 1;
        snfft=1.0/nfft;
	
	rt = ealloc1float(nfft);
	
	do {
		ng++;

		fd = ealloc2complex(nf,ntr); 
       		cc = ealloc2float(nf,ntr);

		/* transform the data into FX domain */
		{ unsigned int itr;
			for(itr=0;itr<ntr;itr++) {
				memcpy( (void *) rt, (const void *) (*rec_o[itr]).data,nt*FSIZE);
                		memset( (void *) &rt[nt], (int) '\0', (nfft - nt)*FSIZE);
				
				pfarc(1, nfft, rt, fd[itr]);
			}
		}
		
		/* Compute correlation coefficients */
		{ unsigned int itr,ifr;
			for(itr=0;itr<ntr-1;itr++) {
				for(ifr=0;ifr<nf-1;ifr++) { 
					cc[itr][ifr] = cos(PHSSP(fd[itr][ifr])-PHSSP(fd[itr+1][ifr])); 
				}			
			}
		
		}
		
		/* Filter */
		{ unsigned int itr,ifr;
			for(itr=0;itr<ntr-1;itr++) {
				for(ifr=0;ifr<nf-1;ifr++) { 
					if(cc[itr][ifr]> cch || cc[itr][ifr]<ccl) {
						fd[itr][ifr].r = 0.0; 
						fd[itr][ifr].i = 0.0;
					} 
				}			
			}
		
		}
		
		{ unsigned int itr,it;
			for(itr=0;itr<ntr;itr++) {
				
				pfacr(-1, nfft, fd[itr], rt);
				
				for(it=0;it<nt;it++) 		
                			(*rec_o[itr]).data[it]=rt[it]*snfft;
			}
		}
			
		free2complex(fd);
		free2float(cc);

	    	rec_o = put_gather(rec_o,&nt,&ntr);
	    	rec_o = get_gather(&key,&type,&val,&nt,&ntr,&dt,&first);
		
		fprintf(stderr," %d %d\n",ng,ntr);
		
	} while(ntr);
	
	free1float(rt);

	warn("Number of gathers %10d\n",ng);
	 
	return EXIT_SUCCESS;
}
Beispiel #11
0
/* the main program */
int main (int argc, char **argv)
{
	int nx,nz,ix,iz;
	float dx,fx,dz,fz,x,z,xmin,xmax,zmin,zmax;
	float **a1111,**a3333,**a1133,**a1313,**a1113;
	float **a1212,**a2323,**a1223,**rho;
	float **a3313;
	Tri *t;
	TriAttributes *ta;
	Model *m;
	char *a1111file, *a3333file, *a1133file;
	char *a1313file, *a1113file, *a3313file;
	char *a1212file, *a1223file, *a2323file;
	char *rhofile;
	FILE *a1111fp=NULL, *a3333fp=NULL, *a1133fp=NULL;
	FILE *a1313fp=NULL, *a1113fp=NULL, *a3313fp=NULL;
	FILE *a1212fp=NULL, *a1223fp=NULL, *a2323fp=NULL;
	FILE *rhofp=NULL;
	/* hook up getpar to handle the parameters */
	initargs(argc,argv);
	requestdoc(0);
	
	/* get required parameters */
	if (!getparint("nx",&nx)) err("must specify nx!");
	if (!getparint("nz",&nz)) err("must specify nz!");
	
	/* get optional parameters */
	if (!getparfloat("dx",&dx)) dx = 1.0;
	if (!getparfloat("dz",&dz)) dz = 1.0;
	if (!getparfloat("fx",&fx)) fx = 0.0;
	if (!getparfloat("fz",&fz)) fz = 0.0;
	
	if(getparstring("a1111file",&a1111file))
		 	a1111fp = efopen(a1111file,"w");
	else a1111fp = efopen("a1111.bin","w");

	if(getparstring("a3333file",&a3333file))
		 	a3333fp = efopen(a3333file,"w");
	else a3333fp = efopen("a3333.bin","w");

	if(getparstring("a1133file",&a1133file))
		 	a1133fp = efopen(a1133file,"w");
	else a1133fp = efopen("a1133.bin","w");

	if(getparstring("a1313file",&a1313file))
		 	a1313fp = efopen(a1313file,"w");
	else a1313fp = efopen("a1313.bin","w");

	if(getparstring("a1113file",&a1113file))
		 	a1113fp = efopen(a1113file,"w");
	else a1113fp = efopen("a1113.bin","w");

	if(getparstring("a3313file",&a3313file))
		 	a3313fp = efopen(a3313file,"w");
	else a3313fp = efopen("a3313.bin","w");

	if(getparstring("a1212file",&a1212file))
		 	a1212fp = efopen(a1212file,"w");
	else a1212fp = efopen("a1212.bin","w");

	if(getparstring("a2323file",&a2323file))
		 	a2323fp = efopen(a2323file,"w");
	else a2323fp = efopen("a2323.bin","w");

	if(getparstring("a1223file",&a1223file))
		 	a1223fp = efopen(a1223file,"w");
	else a1223fp = efopen("a1223.bin","w");

	if(getparstring("rhofile",&rhofile))
		 	rhofp = efopen(rhofile,"w");
	else rhofp = efopen("rho.bin","w");

        checkpars();


	/* read input triangulated sloth model */
	m = readModel(stdin);
	
	/* determine min and max x and z coordinates */
	xmin = m->ymin;
	xmax = m->ymax;
	zmin = m->xmin;
	zmax = m->xmax;
	
	/* allocate space for uniformly sampled stiffnesses */
	a1111 = ealloc2float(nz,nx);
	a3333 = ealloc2float(nz,nx);
	a1133 = ealloc2float(nz,nx);
	a1313 = ealloc2float(nz,nx);
	a1113 = ealloc2float(nz,nx);
	a3313 = ealloc2float(nz,nx);
	a1212 = ealloc2float(nz,nx);
	a1223 = ealloc2float(nz,nx);
	a2323 = ealloc2float(nz,nx);
	rho = ealloc2float(nz,nx);

	/* loop over all samples */
	for (ix=0,x=fx,t=NULL; ix<nx; ++ix,x+=dx) {
		if (x<xmin || x>xmax)
			err("x=%g must be between xmin=%g and xmax=%g",
				x,xmin,xmax);
		for (iz=0,z=fz; iz<nz; ++iz,z+=dz) {
			if (z<zmin || z>zmax)
				err("z=%g must be between zmin=%g and zmax=%g",
					z,zmin,zmax);
			t = insideTriInModel(m,t,z,x);
			ta = (TriAttributes*)t->fa;
			a1111[ix][iz] = ta->a1111;
			a3333[ix][iz] = ta->a3333;
			a1133[ix][iz] = ta->a1133;
			a1313[ix][iz] = ta->a1313;
			a1113[ix][iz] = ta->a1113;
			a3313[ix][iz] = ta->a3313;
			a1212[ix][iz] = ta->a1212;
			a2323[ix][iz] = ta->a2323;
			a1223[ix][iz] = ta->a1223;
			rho[ix][iz] = ta->rho;

		}
	}
	
	/* write uniformly sampled sloth */
	fwrite(a1111[0],sizeof(float),nz*nx,a1111fp);
	fwrite(a3333[0],sizeof(float),nz*nx,a3333fp);
	fwrite(a1133[0],sizeof(float),nz*nx,a1133fp);
	fwrite(a1313[0],sizeof(float),nz*nx,a1313fp);
	fwrite(a1113[0],sizeof(float),nz*nx,a1113fp);
	fwrite(a3313[0],sizeof(float),nz*nx,a3313fp);
	fwrite(a1212[0],sizeof(float),nz*nx,a1212fp);
	fwrite(a2323[0],sizeof(float),nz*nx,a2323fp);
	fwrite(a1223[0],sizeof(float),nz*nx,a1223fp);
	fwrite(rho[0],sizeof(float),nz*nx,rhofp);

	return 1;
}
Beispiel #12
0
int
main(int argc, char **argv)
{
	int nv;		/* number of velocities */
	float dv;	/* velocity sampling interval */
	float fv;	/* first velocity */
        float anis1;    /* quartic term, or numerator of an extended one */
        float anis2;    /* inside denominator of an extended quartic term */
	int iv;		/* velocity index */
	int dtratio;	/* ratio of output to input sampling intervals */
	int nsmooth;	/* length in samples of num and den smoothing window */
	int nt;		/* number of time samples per input trace */
	float dt;	/* time sampling interval for input traces */
	float ft;	/* time of first sample input and output */
	int ntout;	/* number of output samples */
	float dtout;	/* time sampling interval for output traces */
	int it;		/* input time sample index */
	int itout;	/* output time sample index */
	int is;		/* time sample index for smoothing window */
	int ismin;	/* lower limit on is */
	int ismax;	/* upper limit on is */
	int itmute;	/* time sample index of first sample not muted */
	int iti;	/* time sample index used in linear interpolation */
	float ti;	/* normalized time for linear interpolation */
	float frac;	/* fractional distance from sample in interpolation */
	int gottrace;	/* =1 if an input trace was read */
	int verbose;	/* =1 for diagnostic print */
	long cdp;	/* cdp from current input trace header */
	long cdpprev;	/* cdp from previous input trace header */
	float smute;	/* NMO stretch mute factor */
	float offset;	/* offset from input trace header */
	float offovs;	/* (offset/velocity)^2 */
        float offan=0.0;    /* shift of tnmo due to anisotropy */
	float tn;	/* time after NMO */
	float tnmute;	/* mute time after NMO */
	float nsum;	/* semblance numerator sum */
	float dsum;	/* semblance denominator sum */
	float v;	/* velocity */
	float temp;	/* temporary scalar */
	float *data;	/* array[nt] of input trace */
	float *sem;	/* array[ntout] of semblance */
	float **num;	/* array[nv][nt] of semblance numerators */
	float **den;	/* array[nv][nt] of semblance denominators */
	float **nnz;	/* array[nv][nt] for counting non-zero samples */
	float pwr;      /* power of semblance */

	/* hook up getpar */
	initargs(argc,argv);
	requestdoc(0);

	/* get parameters from the first trace */
	if (!gettr(&tr)) err("can't get first trace");
	nt = tr.ns;
	dt = ((double) tr.dt)/1000000.0;
	ft = tr.delrt/1000.0;
	cdp = tr.cdp;
	offset = tr.offset;

	/* get optional parameters */
	if (!getparint("nv",&nv)) nv = 50;
	if (!getparfloat("dv",&dv)) dv = 50.0;
	if (!getparfloat("fv",&fv)) fv = 1500.0;
	if (!getparfloat("anis1",&anis1)) anis1 = 0.0;
	if (!getparfloat("anis2",&anis2)) anis2 = 0.0;
	if (!getparfloat("smute",&smute)) smute = 1.5;
	if (smute<=1.0) err("smute must be greater than 1.0");
	if (!getparint("dtratio",&dtratio)) dtratio = 5;
	if (!getparint("nsmooth",&nsmooth)) nsmooth = dtratio*2+1;
	if (!getparint("verbose",&verbose)) verbose = 0;
	if (!getparfloat("pwr",&pwr)) pwr = 1.0;
	if (pwr < 0.0)   
	  err("we are not looking for noise: pwr < 0");
	if (pwr == 0.0)   
	  err("we are creating an all-white semblance: pwr = 0");

        checkpars();
	/* determine output sampling */
	ntout = 1+(nt-1)/dtratio;   CHECK_NT("ntout",ntout);
	dtout = dt*dtratio;
	if (verbose) {
		fprintf(stderr,
			"\tnumber of output time samples is %d\n",ntout);
		fprintf(stderr,
			"\toutput time sampling interval is %g\n",dtout);
		fprintf(stderr,
			"\toutput time of first sample is %g\n",ft);
	}

	/* allocate memory */
	data = ealloc1float(nt);
	num = ealloc2float(nt,nv);
	den = ealloc2float(nt,nv);
	nnz = ealloc2float(nt,nv);
	sem = ealloc1float(ntout);

	/* zero accumulators */
	for (iv=0; iv<nv; ++iv) {
		for (it=0; it<nt; ++it) {
			num[iv][it] = 0.0;
			den[iv][it] = 0.0;
			nnz[iv][it] = 0.0;
		}
	}

	/* initialize flag */
	gottrace = 1;

	/* remember previous cdp */
	cdpprev = tr.cdp;

	/* loop over input traces */
	while (gottrace|(~gottrace)/*True*/) { /* middle exit loop */

		/* if got a trace */
		if (gottrace) {

			/* determine offset and cdp */
			offset = tr.offset;
			cdp = tr.cdp;
                        if ((1.0 + offset*offset*anis2) <= 0.0)
                           err ("anis2 too small");
                        offan = (offset*offset*offset*offset*anis1) / 
                                (1.0 + offset*offset*anis2);

			/* get trace samples */
			memcpy( (void *) data,
				(const void *) tr.data,nt*sizeof(float));
		}

		/* if cdp has changed or no more input traces */
		if (cdp!=cdpprev || !gottrace) {

			/* set output trace header fields */
			tr.offset = 0;
			tr.cdp = (int) cdpprev;
			tr.ns = ntout;
			tr.dt = dtout*1000000.0;

			/* loop over velocities */
			for (iv=0; iv<nv; ++iv) {

				/* compute semblance quotients */
				for (itout=0; itout<ntout; ++itout) {
					it = itout*dtratio;
					ismin = it-nsmooth/2;
					ismax = it+nsmooth/2;
					if (ismin<0) ismin = 0;
					if (ismax>nt-1) ismax = nt-1;
					nsum = dsum = 0.0;
					for (is=ismin; is<ismax; ++is) {
						nsum += num[iv][is]*
							num[iv][is];
						dsum += nnz[iv][is]*
							den[iv][is];
					}
					sem[itout] = (dsum!=0.0?nsum/dsum:0.0);
				}

				/* powering the semblance */
				if (pwr != 1.0) {
				  for (itout=0; itout<ntout; ++itout)
				    sem[itout] = pow (sem[itout], pwr);
				};

				/* output semblances */
				memcpy((void *) tr.data,
				       (const void *) sem,ntout*sizeof(float));
				puttr(&tr);

				/* zero accumulators */
				for (it=0; it<nt; ++it) {
					num[iv][it] = 0.0;
					den[iv][it] = 0.0;
					nnz[iv][it] = 0.0;
				}
			}

			/* diagnostic print */
			if (verbose) 
				warn("semblance output for cdp=%d",cdpprev);

			/* if no more input traces, break input trace loop */
			if (!gottrace) break;

			/* remember previous cdp */
			cdpprev = cdp;
		}

		/* loop over velocities */
		for (iv=0,v=fv; iv<nv; ++iv,v+=dv) {
			
			/* compute offset/velocity squared */
			offovs = (offset*offset)/(v*v) + offan;
                        /* decrease of traveltime with distance due to highly
                           increasing velocity cannot be handled yet
                        */
                        if (offovs < 0.0)
                           warn("no moveout; check anis1 and anis2");

			/* determine mute time after nmo */
			tnmute = sqrt(offovs/(smute*smute-1.0));
			if (tnmute > ft) {
				itmute = (tnmute-ft)/dt;
			} else {
				itmute = 0 ;
			}
			
			/* do nmo via quick and dirty linear interpolation
			   (accurate enough for velocity analysis) and
			   accumulate semblance numerator and denominator
			*/

			for (it=itmute,tn=ft+itmute*dt; it<nt; ++it,tn+=dt) {
				ti = (sqrt(tn*tn+offovs)-ft)/dt;
				iti = ti;
				if (iti<nt-1) {
					frac = ti-iti;
					temp = (1.0-frac)*data[iti]+
						frac*data[iti+1];
					if (temp!=0.0) {
						num[iv][it] += temp;
						den[iv][it] += temp*temp;
						nnz[iv][it] += 1.0;
					}
				}
			}
		}

		/* get next trace (if there is one) */
		if (!gettr(&tr)) gottrace = 0;

	}

	return(CWP_Exit());
}
Beispiel #13
0
int
main(int argc, char **argv)
{
	int nline;		/* number of (identical) lines		*/
	int ntr;		/* number of traces			*/
	int nt;			/* number of time samples		*/
	int ninf;		/* number of subsurface reflectors	*/
	int itr;		/* trace number index			*/
	int iline;		/* line number index			*/
	int i, j;		/* counters				*/

	float dx;		/* trace interval			*/
	float dt;		/* time interval			*/
	float tdelay;		/* recording time delay			*/
	float x;		/* zero offset position			*/
	float xmin, xmax;	/* min and max horizontal coordinates 	*/

	float *zint;		/* reflector intercepts at x = 0 	*/
	float *dip;		/* reflector dips 			*/
	float *v;		/* speeds in layers 			*/
	float *rho;		/* densities in layers 			*/
	float *xl, *xr;		/* min and max horiz coords in a layer 	*/
	float *meet;		/* horizontal intercept for two adjacent
				 * layers, if layers not parallel */
	float *trcoefs;		/* transmission coefficients		*/
	float *data;		/* temp for holding synthetic trace	*/
	float *tout;		/* times[nt] for interpolation		*/
	float **theta;		/* angle at which the specular ray to
				 * interface j hits interface i */
	float **m;		/* slope for horiz travel calculation	*/
	float **b;		/* intercept for horiz travel calc	*/
	float **d;		/* distance traveled in a layer, if layers
				 * are parallel */
	float **k;		/* factor for finding horizontal distance
				 * in a layer */
	float **w;		/* for spreading factor calculation	*/

	cwp_Bool *dorow;	/* are adjacent reflectors parallel?	*/


	/* hook up getpar */
	initargs(argc, argv);
	requestdoc(0);
	
	
	/* get scalar parameters */
	if (!getparint("nline",&nline))	     nline=1;
	if (!getparint("ntr",&ntr))	     ntr=32;	 tr.ntr = ntr*nline;
	if (!getparfloat("dx",&dx))	     dx=10.0;
	if (!getparint("nt",&nt))	     nt=128;
	CHECK_NT("nt",nt);					 tr.ns = nt;
	if (!getparfloat("dt",&dt))	     dt=0.004;	 tr.dt = dt*1000000;
	if (!getparfloat("tdelay",&tdelay))  tdelay=0.0;
						tr.delrt = tdelay*1000;
	if (!getparint("ninf",&ninf))	ninf=4;
	
	/* allocate space */
	data = ealloc1float(nt);
	v = ealloc1float(ninf+1);
	rho = ealloc1float(ninf+1);
	zint = ealloc1float(ninf+1);
	dip = ealloc1float(ninf+1);
	meet = ealloc1float(ninf+1);
	xl = ealloc1float(ninf+1);
	xr = ealloc1float(ninf+1);
	trcoefs = ealloc1float(ninf+1);
	theta = ealloc2float(ninf+1,ninf+1);
	m = ealloc2float(ninf+1,ninf+1);
	b = ealloc2float(ninf+1,ninf+1);
	d = ealloc2float(ninf+1,ninf+1);
	w = ealloc2float(ninf+1,ninf+1);
	k = ealloc2float(ninf+1,ninf+1);
	dorow = ealloc1(ninf+1,sizeof(cwp_Bool));
	
	/* compute output times for interpolation */
	tout = ealloc1float(nt);
	for (i=0; i<nt; i++) tout[i]=i*dt;

	 	
	/* getpar the reflector intercepts, dips, v's, and rho's */
	zint[0] = 0.0; /* surface hard wired */
	if (!getparfloat("zint", zint+1))
 		for (i = 1; i <= ninf; ++i) zint[i] = 100.0 * i;
	dip[0] = 0.0; /* surface hard wired */
	if (!getparfloat("dip", dip+1))
 		for (i = 1; i <= ninf; ++i) dip[i] = 5.0 * i;
	if (!getparfloat("v", v))
 		for (i = 0; i <= ninf; ++i) v[i] = 1500.+ 500*i;
	if (!getparfloat("rho", rho))
 		for (i = 0; i <= ninf; ++i) rho[i] = 1.0;

	/* check dips and intercepts */
	for (i = 1; i <= ninf; ++i) {
		if (dip[i] < -90.0 || dip[i] > 90.0) err("dips1");
		if (ABS(dip[i] - dip[i-1]) > 90.0) err("dips2");
		dip[i] *= TORADS;
		
		if (zint[i] <= zint[i-1]) err("intercept");
	}
	for (i = 0; i < ninf; ++i) {
		if (v[i] <= 0.0) err("velocities");
		if (rho[i] <= 0.0) err("density");
	}


	/* Table theta[j][i], the angle at which the specular to layer j
	 * leaves interface i. 						*/
	for (j = 1; j < ninf; ++j) {
		theta[j][j-1] = -dip[j];
		for (i = j-1; i > 0; --i) {
		    float temp;
		    
		    temp = v[i-1]*sin(theta[j][i]+dip[i])/v[i];
		    if (ABS(temp) > 1.0) err("specular %d", j);
			
		    theta[j][i-1] = asin(temp) - dip[i];
		}
	}


	/* Table m and b, which are used to find the x coordinate of
	 * the specular at the next interface from its x coordinate at
	 * the previous interface. 					*/
	for (j = 1; j <= ninf; ++j) {
		for (i = 0; i < j; ++i) {
			float s, temp;
			
			s = sin(theta[j][i]);
			temp = 1.0 - tan(dip[i+1])*s;
			m[j][i] = (1.0 - tan(dip[i])*s)/temp;
			b[j][i] = (zint[i+1]-zint[i])*s/temp;
		}
	}

	/* Make the final checks on the substructure specification.
	 * The strategy is to check the x coordinates of the
	 * interface intercepts against x coordinates of
	 * specular-interface intercepts. Only the rays from the
	 * left and right endpoints need to be checked, since they define
	 * the area being "observed". 					*/
	xmin = 0.0;
	xmax = dx * (ntr - 1);
	for (j = 1; j <= ninf; ++j) {
		xl[j] = xmin;  /*** changed Brian's code here! */
		xr[j] = xmax;
	}

	for (j=0; j<ninf; ++j) {
		int j2;
		for (j2=j+1; j2<=ninf; ++j2) {
		    if (dip[j2] != dip[j]) {
			float intercept;
			
			intercept = (zint[j2]-zint[j])/
						(tan(dip[j])-tan(dip[j2]));
			if (intercept > xmin && intercept < xmax) {
				err("intercept");
			}
		    }
		}

		for (j2=j+1; j<=ninf; ++j) {
			xl[j2] = m[j2][j]*xl[j2] + b[j2][j];
			xr[j2] = m[j2][j]*xr[j2] + b[j2][j];

			if (xl[j2] < xmin) xmin = xl[j2];
			if (xr[j2] > xmax) xmax = xr[j2];
		}
	}

	/* Table the arrays meet and k if two adjacent interfaces
	 * intersect, otherwise d if they are parallel.
	 * Meet is the x coordinate of intersection if there is
	 * an intersection, k is a value which will be used to
	 * find the distance travelled in that layer. If there is no
	 * intersection d will be a constant for all j and can be
	 * stored now. */
	for (i = 0; i < ninf; ++i) {
		if (dip[i+1] == dip[i]) {
			dorow[i] = cwp_false;
			for (j = i + 1; j <= ninf; ++j) {
				d[j][i] = (zint[i+1]-zint[i])*cos(dip[i]) /
						cos(theta[j][i] + dip[i]);
			}
		} else {
			dorow[i] = cwp_true;
			meet[i] = (zint[i+1]-zint[i])/
					(tan(dip[i])-tan(dip[i+1]));
			for (j = i + 1; j <= ninf; ++j) {
				k[j][i] = sin(dip[i]-dip[i+1]) /
				      (cos(theta[j][i]+dip[i+1])*cos(dip[i]));
			}
		}
	}

	/* Table t, the transmission coefficients. */
	tabtrcoefs(ninf, rho, v, theta, dip, trcoefs);

	/* Table w, the coefficients for the spreading factor calculation. */
	for (j = 1; j <= ninf; ++j) {
		w[j][0] = 1.0;
		for (i = 1; i < j; ++i) {
			float c1, c2;
			
			c1 = cos(theta[j][i-1] + dip[i]);
			c2 = cos(theta[j][i] + dip[i]);
			w[j][i] = w[j][i-1]*c1*c1/(c2*c2);
		}
	}

	/* Now ready to make synthetic traces. */
	for (iline = 0; iline < nline; ++iline) {
		x = 0.0;
		for (itr = 0; itr < ntr; itr++) {
			float ex, t0, time, tmp, p1, p2, dist, amp[1];
			int itime, it;
			
			memset( (void *) tr.data, 0, nt * FSIZE);
			memset( (void *) data, 0, nt * FSIZE);
	
			for (j = 1; j <= ninf; ++j) {
				ex = x;
				if (dorow[0]) {
					dist = (meet[0]-ex)*k[j][0];
				} else {
					dist = d[j][0];
				}
				t0 = dist/v[0];
				tmp = dist*v[0];
				p1 = tmp;
				p2 = tmp;
				for (i = 1; i < j; ++i) {
					ex = m[j][i-1]*ex + b[j][i-1];
					if (dorow[i]) {
						dist = (meet[i] - ex)*k[j][i];
					} else {
						dist = d[j][i];
					}
	
					t0 += dist/v[i];
					tmp = dist*v[i];
					p1 += tmp;
					p2 += tmp*w[j][i];
				}
	
				/* set strength and time of spike response */
				amp[0] = v[0]*trcoefs[j]/
						(dt*FOURPI*2.0*sqrt(p1*p2));
				time = 2.0*t0 - tdelay;
				itime = NINT(time/dt);
				if (itime >= 0 && itime < nt)
						data[itime] = amp[0];
				
				/* distribute spike response over full trace */
				ints8r(1,dt,time,amp,0,0,nt,tout,data);
				
				/* add distributed spike to segy trace */
				for (it = 0; it < nt; ++it)
					tr.data[it] += data[it];
			}
			
			/* fill tracl header and put trace out */
			tr.tracl = itr + 1;
			tr.gx = x;
			tr.sx = x;
			puttr(&tr);

			/* set horizontal location of next trace */
			x += dx;
	
		}
	}
	
	/**
	* Thats all folks.
	**/

	return(CWP_Exit());	
}
Beispiel #14
0
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;
}
Beispiel #15
0
int 
main(int argc, char **argv)
{
      int ix;	/*index for nx*/
      int iy;	/*index for ny*/
      int nx;	/*number of sampels in horizon*/
      int ny;	/*number of sampels in horizon*/

      float xmin,xmax;
      float ymin,ymax;
      float zmin,zmax;

      float ***databot;	/*data for plotting*/
      float ***datatop;
      float ***emisbot; /*color on top horizon*/
      float ***emistop; /*color right above base horizon*/
      float v0;

      int verbose;    /*if =1 print some useful information*/

      float eyez;

      int ihz;	/*index for interfaces*/

      int *ntris;	/*number of triangles*/
      int nt;		/*number of samples in each ray*/
      int iray; 	/*index for nrays*/
      int it;		/*index for nt*/
      int iw,iwf,nwf;
      int is,ns;        /*number of sources*/

      float q0[4];

      char *rayfile=""; /*ray file*/
      char *wffile="";
      char *sttfile="";

      FILE *rayfp=NULL;
      FILE *wffp=NULL;
      FILE *sttfp=NULL;

      Layer *horz;
      Layer *ray;
      Layer *wf;
      float vmin=99999.0;
      float vmax=0.0;

      float tt; /*debugging information in the ray file*/

      int itri;

      char names[10];

      int iflag;	/*flag: =1 means ray effective*/
      float emission[4];
      float tmax=0.0,tmin=FLT_MAX;
      float ***stt=NULL;
      float **ttt=NULL;
      int ntr;

      /* hook up getpar */
      initargs(argc,argv);
      requestdoc(1);

      /* get parameters */
      if (!getparint("verbose",&verbose)) verbose=0;

      /******************************************
      Read model parameters from hzfile
      ******************************************/
      fread(&nhz,sizeof(int),1,stdin);
      fread(&nx,sizeof(int),1,stdin);
      fread(&ny,sizeof(int),1,stdin);
      fread(&xmin,sizeof(float),1,stdin);
      fread(&xmax,sizeof(float),1,stdin);
      fread(&ymin,sizeof(float),1,stdin);
      fread(&ymax,sizeof(float),1,stdin);
      fread(&zmin,sizeof(float),1,stdin);
      fread(&zmax,sizeof(float),1,stdin);

      if (verbose)
            fprintf(stderr,"xmin=%f\nxmax=%f\nymin=%f\nymax=%f\nzmin=%f\nzmax=%f\n",
	          xmin,xmax,ymin,ymax,zmin,zmax);

      if (getparstring("rayfile",&rayfile))  
	    if ((rayfp=fopen(rayfile,"r"))==NULL)
		  err("Can not open rayfile %s",rayfile);

      if (getparstring("wffile",&wffile))
	    if ((wffp=fopen(wffile,"r"))==NULL)
		  err("Can not open wffile %s",wffile);

      if (getparstring("sttfile",&sttfile))
	    if ((sttfp=fopen(sttfile,"r"))==NULL)
		  err("Can not open sttfile %s",sttfile);

      if (!getparfloat("tbs",&tbs))   tbs=0.8;
      if (!getparint("hue",&glb_hue)) glb_hue=1; /*1 for glb_hue*/

      if (verbose) 
	    warn("nhz=%d, nx=%d, ny=%d\n",nhz,nx,ny);

      glb_on_or_off=(enum On_or_Off *)ealloc1int(3*nhz+6);
      for (ihz=0;ihz<nhz;ihz++) glb_on_or_off[ihz]=ON;

      horz=(Layer *)alloc1float(sizeof(Layer)*(nhz+1));

      /*********************************************************
      Do not use GLUT_INDEX, which gives no image;
      GLUT_SINGLE will cause redrawing every time you retate it;
      *********************************************************/
      glutInit(&argc, argv);
      glutInitWindowSize(768,768);
      glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
      glutCreateWindow("viewer3");
      glutDisplayFunc(redraw);
      glutIdleFunc(NULL);

      if (!getparfloat("q",q0)){
	    q0[0]=-0.6; 
	    q0[1]=0.05;
	    q0[2]=-0.06;
	    q0[3]=0.8;
      }
      checkpars();

      normalize_quat(q0);

      curquat[0]=q0[0];
      curquat[1]=q0[1];
      curquat[2]=q0[2];
      curquat[3]=q0[3];

      glutReshapeFunc(myReshape);
      glutVisibilityFunc(vis);
      glutMouseFunc(mouse);
      glutMotionFunc(motion);
      glutCreateMenu(controlLights);
      glutAddMenuEntry("Quit",-1);
      glutAddMenuEntry("Full Screen",0);      
      glutAddMenuEntry("White/Color Rays",1);
      glutAddMenuEntry("Plot Rays",2);
      glutAddMenuEntry("Surface Traveltimes",3);
      glutAddMenuEntry("Wired or Solid WFs",4);
      glutAddMenuEntry("Plot Wavefronts",5);
      glutAddMenuEntry("TRI or TETRA or LAYER or HORZ",6);
      for (ihz=0;ihz<nhz;ihz++) {
	    sprintf(names,"Layer %d",ihz+1);
	    glutAddMenuEntry(names,ihz+7);
      }
      glutAttachMenu(GLUT_RIGHT_BUTTON);

      glShadeModel(GL_SMOOTH);
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_LIGHTING);

      eyez=25;
      glMatrixMode(GL_PROJECTION);
      gluPerspective(
            40.0,   /*fovy: view angle in y direction*/
            1.0,    /*aspect: ratio of width (x) to y (height)*/
            eyez-DIAMETER,  /*near clipping plane*/
            eyez+DIAMETER); /*far clipping plane*/

      glMatrixMode(GL_MODELVIEW);
      gluLookAt(
            0.0, 0.0, eyez, /*(eyex,eyey,eyez): the eye position*/
            0.0, 0.0, 0.0,  /*(centerx,centery,centerz): the center*/
            0.0, 1.0, 0.0); /*(upx,upy,upz): the up direction*/
      glPushMatrix();

      /*the order that tetramod uses is like this*/
      for (ihz=0;ihz<nhz;ihz++) {
            fprintf(stderr,"reading horizon information %d\n",ihz);
	    /**********************************************************
	    input the horizon information from file hzfile:
	    **********************************************************/
	    horz[ihz].x=ealloc2float(nx,ny);
	    horz[ihz].y=ealloc2float(nx,ny);
	    horz[ihz].z=ealloc2float(nx,ny);
	    horz[ihz].v0=ealloc2float(nx,ny);
	    horz[ihz].v1=ealloc2float(nx,ny);

            fprintf(stderr,"read horz[%d].x...\n",ihz);
	    if (fread(horz[ihz].x[0],sizeof(float),nx*ny,
		  stdin)!=nx*ny)
		  err("Can not read x to stdin");
           
            fprintf(stderr,"read horz[%d].y...\n",ihz);
	    if (fread(horz[ihz].y[0],sizeof(float),nx*ny,
		  stdin)!=nx*ny)
		  err("Can not read y to stdin");

	    fprintf(stderr,"read horz[%d].z...\n",ihz);
	    if (fread(horz[ihz].z[0],sizeof(float),nx*ny,
		  stdin)!=nx*ny)
		  err("Can not read z to stdin");

	    fprintf(stderr,"read horz[%d].v0...\n",ihz);
	    if (fread(horz[ihz].v0[0],sizeof(float),nx*ny,stdin)!=
		  nx*ny)
		  err("Can not read v0 to stdin");
 
	    fprintf(stderr,"read horz[%d].v1...\n",ihz);
	    if (fread(horz[ihz].v1[0],sizeof(float),nx*ny,stdin)!=
	      	  nx*ny)
		  err("Can not read v1 to stdin");

	    for (iy=0;iy<ny;iy++) {
	          for (ix=0;ix<nx;ix++) {
		        vmin=MIN(vmin,horz[ihz].v0[iy][ix]);
			vmax=MAX(vmax,horz[ihz].v0[iy][ix]);
		        vmin=MIN(vmin,horz[ihz].v1[iy][ix]);
			vmax=MAX(vmax,horz[ihz].v1[iy][ix]);
		  }
	    }
      }

      if (verbose)
	    fprintf(stderr,"vmin=%f, vmax=%f\n",vmin,vmax);

      horz[nhz].x=ealloc2float(nx,ny);
      horz[nhz].y=ealloc2float(nx,ny);
      horz[nhz].z=ealloc2float(nx,ny);

      fprintf(stderr,"assign horz[%d].x,y,z\n",nhz);
      for (ix=0;ix<nx;ix++) {
	    for (iy=0;iy<ny;iy++) {
	       	  horz[nhz].x[iy][ix]=horz[nhz-1].x[iy][ix];
		  horz[nhz].y[iy][ix]=horz[nhz-1].y[iy][ix];
		  horz[nhz].z[iy][ix]=zmax;
	    }
      }

      databot=ealloc3float(3,nx,ny);
      emisbot=ealloc3float(4,nx,ny);
      datatop=ealloc3float(3,nx,ny);
      emistop=ealloc3float(4,nx,ny);

      for (ihz=0;ihz<nhz;ihz++) {
	    fprintf(stderr,"assigning datatop for ihz=%d\n",ihz);
	    for (ix=0;ix<nx;ix++) {
		  for (iy=0;iy<ny;iy++) {

			datatop[iy][ix][0]=(
			      (horz[ihz].x[iy][ix]-xmin)/
			      (xmax-xmin)-0.5)*DIAMETER;
			datatop[iy][ix][1]=-(
			      (horz[ihz].y[iy][ix]-ymin)/
			      (ymax-ymin)-0.5)*DIAMETER;
			datatop[iy][ix][2]=(
			      (horz[ihz].z[iy][ix]-zmin)/
			      (zmax-zmin)-0.5)*DIAMETER;
			v0=horz[ihz].v0[iy][ix];

			vEmission(v0,vmin,vmax,emistop[iy][ix]);
		  }
	    }

	    fprintf(stderr,"assigning databot for ihz=%d\n",ihz);
	    for (ix=0;ix<nx;ix++) {
		  for (iy=0;iy<ny;iy++) {
			databot[iy][ix][0]=(
			      (horz[ihz+1].x[iy][ix]-xmin)
			      /(xmax-xmin)-0.5)*DIAMETER;
			databot[iy][ix][1]=-(
			      (horz[ihz+1].y[iy][ix]-ymin)
			      /(ymax-ymin)-0.5)*DIAMETER;	
			databot[iy][ix][2]=(
			      (horz[ihz+1].z[iy][ix]-zmin)
			      /(zmax-zmin)-0.5)*DIAMETER;
                                
			v0=horz[ihz].v1[iy][ix];
			vEmission(v0,vmin,vmax,emisbot[iy][ix]);
		  }
	    }

	    showLayer(ihz,databot,datatop,nx,ny,emisbot,emistop);
	    showHorz(ihz,datatop,nx,ny,emistop);
	    showTetra(ihz,databot,datatop,nx,ny,emisbot,emistop);
	    showTri(ihz,datatop,nx,ny,emistop);
      }

      free3float(databot);
      free3float(datatop);
      free3float(emisbot);
      free3float(emistop);

      /*******************************************************************
      The ray positions are generated by sutetraray, named by rayfile. 
      This part will be ignored if rayfile not specified.
      ********************************************************************/
      if (rayfp!=NULL) {

            fscanf(rayfp,
                 "%d =Number of shots\n",&ns);
            fprintf(stderr,"ns=%d\n",ns);

            if (ns<=0 || ns>100) {
                 ns=0;
                 rayfp=NULL;
            }

            ray=(Layer *)alloc1float(sizeof(Layer)*ns);

	    tmax=0.0;
            for (is=0;is<ns;is++) {
                  fscanf(rayfp,
                        "%d =Maximum number of segments\n",&nt);

                  fprintf(stderr,"%d =Maximum number of segments\n",nt);
                  fscanf(rayfp,
                        "%d =Number of rays\n",&ray[is].nrays);
		  fprintf(stderr,"%d =Number of rays\n",ray[is].nrays);

	          ray[is].x=ealloc2float(ray[is].nrays,nt);
	          ray[is].y=ealloc2float(ray[is].nrays,nt);
	          ray[is].z=ealloc2float(ray[is].nrays,nt);
                  ray[is].v0=ealloc2float(ray[is].nrays,nt);
		  ray[is].nseg=ealloc1int(ray[is].nrays);

                  for (iray=0;iray<ray[is].nrays;iray++) {
                        fscanf(rayfp,"%d=nseg %f=ttotal\n",&ray[is].nseg[iray],&tt);

                        if (nt<ray[is].nseg[iray]) err("nt should >=ray[is].nseg[iray]");
                        for (it=0;it<ray[is].nseg[iray];it++) {
                              fscanf(rayfp,"%f %f %f %f %f\n",
                                    &ray[is].x[it][iray],
                                    &ray[is].y[it][iray],
                                    &ray[is].z[it][iray],
				    &ray[is].v0[it][iray],&tt);
			      tmax=MAX(tmax,ray[is].v0[it][iray]);
                        }
                        
                        ray[is].z[ray[is].nseg[iray]-1][iray]=
			      MAX(0.001,ray[is].z[ray[is].nseg[iray]-1][iray]);
           
	                for (it=0;it<ray[is].nseg[iray];it++) {
		      	      ray[is].x[it][iray]=((ray[is].x[it][iray]-xmin)/
		       	            (xmax-xmin)-0.5)*DIAMETER;
		    	      ray[is].y[it][iray]=-((ray[is].y[it][iray]-ymin)/
		       	            (ymax-ymin)-0.5)*DIAMETER;
		       	      ray[is].z[it][iray]=((ray[is].z[it][iray]-zmin)/
		       	            (zmax-zmin)-0.5)*DIAMETER;
	                }
                  }
            }
	    fclose(rayfp);

            /*white rays*/
            glNewList(nhz*4+3,GL_COMPILE);
            emission[0]=emission[1]=emission[2]=emission[3]=1.0;
            glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emission);
            for (is=0;is<ns;is++) {
                  for (iray=0;iray<ray[is].nrays;iray++) {
                        iflag=0;
	                glBegin(GL_LINE_STRIP);
	                for (it=0;it<ray[is].nseg[iray];it++) {
		              if (fabs(ray[is].x[it][iray])<RADIUS &&
		                  fabs(ray[is].y[it][iray])<RADIUS &&
		                  fabs(ray[is].z[it][iray])<RADIUS) {
			            glVertex3f(ray[is].x[it][iray],ray[is].y[it][iray],
			                  ray[is].z[it][iray]);

                                    iflag=1;
		              } else if (iflag) break; /*once good, now bad*/    
	                }
	                glEnd();
                  }
            }
            glEndList();

            /*colored rays*/
            glNewList(nhz*4+4,GL_COMPILE);
            for (is=0;is<ns;is++) {
                  for (iray=0;iray<ray[is].nrays;iray++) {
                        iflag=0;
	                glBegin(GL_LINE_STRIP);
	                for (it=0;it<ray[is].nseg[iray];it++) {
		              if (fabs(ray[is].x[it][iray])<RADIUS &&
		                  fabs(ray[is].y[it][iray])<RADIUS &&
		                  fabs(ray[is].z[it][iray])<RADIUS) {

				    tEmission(
					  ray[is].v0[it][iray],
					  0.0,     /*tmin*/
					  tmax,
					  emission);
				    glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emission);
				    glVertex3f(
					  ray[is].x[it][iray],
					  ray[is].y[it][iray],
			                  ray[is].z[it][iray]);
                                    iflag=1;
		              } else if (iflag) break; /*once good, now bad*/    
	                }
	                glEnd();
                  }
            }
            glEndList();
      }

      /*************************************************************
      Plot the wavefront if it is given. If the wffile does not 
      contain effective data, ntris may be wild. In this case, do
      thing about the wavefront.
      *************************************************************/
      if (wffp!=NULL) {
            fscanf(wffp,"%d = nwf2dump\n",&nwf);
            fprintf(stderr,"nwf2dump=%d\n",nwf);
            if (nwf>200) wffp=NULL;
      }

      if (wffp!=NULL) {

   	    emission[0]=1.0;
            emission[1]=1.0; 
	    emission[2]=0.0;
	    emission[3]=1.0;

	    wf=(Layer *)alloc1float(sizeof(Layer)*nwf);
            ntris=ealloc1int(sizeof(int)*nwf);

	    for (iwf=0;iwf<nwf;iwf++) {

	          if (1!=fscanf(wffp,"%d = ntris\n",&ntris[iwf])) {
		        nwf=iwf;
                        break;
	          }

		  if (ntris[iwf]==0) {
                        nwf=iwf;
                        break;
	          }

		  if (verbose)
			warn("ntris=%d of nwf=%d\n",ntris[iwf],nwf);

		  wf[iwf].x=ealloc2float(3,ntris[iwf]);
		  wf[iwf].y=ealloc2float(3,ntris[iwf]);
		  wf[iwf].z=ealloc2float(3,ntris[iwf]);

		  for (it=0;it<ntris[iwf];it++) {
                        fscanf(wffp,"%f %f %f %f %f %f %f %f %f\n",
                              wf[iwf].x[it],  wf[iwf].y[it],  wf[iwf].z[it],
                              wf[iwf].x[it]+1,wf[iwf].y[it]+1,wf[iwf].z[it]+1,
                              wf[iwf].x[it]+2,wf[iwf].y[it]+2,wf[iwf].z[it]+2);
		  }

		  fprintf(stderr,"Totally read in %d wavefront triangles\n",ntris[iwf]);

		  for (it=0;it<ntris[iwf];it++) {
			for (iw=0;iw<3;iw++) {
			      wf[iwf].x[it][iw]=((wf[iwf].x[it][iw]-xmin)/
					(xmax-xmin)-0.5)*DIAMETER;
			      wf[iwf].y[it][iw]=-((wf[iwf].y[it][iw]-ymin)/
					(ymax-ymin)-0.5)*DIAMETER;
			      wf[iwf].z[it][iw]=((wf[iwf].z[it][iw]-zmin)/
					(zmax-zmin)-0.5)*DIAMETER;
			}
       	          }
	    }
	    fclose(wffp);

            fprintf(stderr,"Click right MB to get menu\n");
            fprintf(stderr,"Click left MB and drag to rotate\n");
            fprintf(stderr,"Press shift and push left MB to scale\n");

  	    glNewList(nhz*4+6,GL_COMPILE);
	    glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emission);

	    for (iwf=0;iwf<nwf;iwf++) {
	          for (itri=0;itri<ntris[iwf];itri++) {
		        glBegin(GL_LINE_LOOP);
		        if (fabs(wf[iwf].x[itri][0])<RADIUS &&
		            fabs(wf[iwf].y[itri][0])<RADIUS &&
		            fabs(wf[iwf].z[itri][0])<RADIUS && 
		            fabs(wf[iwf].x[itri][1])<RADIUS &&
		            fabs(wf[iwf].y[itri][1])<RADIUS &&
		            fabs(wf[iwf].z[itri][1])<RADIUS &&
                            fabs(wf[iwf].x[itri][2])<RADIUS &&
                            fabs(wf[iwf].y[itri][2])<RADIUS &&
                            fabs(wf[iwf].z[itri][2])<RADIUS) {
			          glVertex3f(wf[iwf].x[itri][0],
					     wf[iwf].y[itri][0],
					     wf[iwf].z[itri][0]);
			          glVertex3f(wf[iwf].x[itri][1],
					     wf[iwf].y[itri][1],
					     wf[iwf].z[itri][1]);
			          glVertex3f(wf[iwf].x[itri][2],
					     wf[iwf].y[itri][2],
					     wf[iwf].z[itri][2]);
		        } else {
                            fprintf(stderr,"warning: some triangles ignored\n");
                            glEnd();
                            break;
                        }                        
		        glEnd();
	          }
            }
            glEndList();

            /*solid wavefronts*/
  	    glNewList(nhz*4+7,GL_COMPILE);
	    glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emission);

	    for (iwf=0;iwf<nwf;iwf++) {
	          for (itri=0;itri<ntris[iwf];itri++) {
		        glBegin(GL_TRIANGLE_STRIP);
		        if (fabs(wf[iwf].x[itri][0])<RADIUS &&
		            fabs(wf[iwf].y[itri][0])<RADIUS &&
		            fabs(wf[iwf].z[itri][0])<RADIUS && 
		            fabs(wf[iwf].x[itri][1])<RADIUS &&
		            fabs(wf[iwf].y[itri][1])<RADIUS &&
		            fabs(wf[iwf].z[itri][1])<RADIUS &&
                            fabs(wf[iwf].x[itri][2])<RADIUS &&
                            fabs(wf[iwf].y[itri][2])<RADIUS &&
                            fabs(wf[iwf].z[itri][2])<RADIUS) {
			          glVertex3f(wf[iwf].x[itri][0],
					     wf[iwf].y[itri][0],
					     wf[iwf].z[itri][0]);
			          glVertex3f(wf[iwf].x[itri][1],
					     wf[iwf].y[itri][1],
					     wf[iwf].z[itri][1]);
			          glVertex3f(wf[iwf].x[itri][2],
					     wf[iwf].y[itri][2],
					     wf[iwf].z[itri][2]);
		        } else {
                            fprintf(stderr,"warning: some triangles ignored\n");
                            glEnd();
                            break;
                        }                        
		        glEnd();
	          }
            }
            glEndList();
      }

      /*surface traveltimes*/
      if (sttfp!=NULL) {
            fscanf(sttfp,"%d = ntris\n",&ntr);
            fprintf(stderr,"ntr=%d\n",ntr);
            if (ntr>2000) sttfp=NULL;
      }      

      if (sttfp!=NULL && ntr>0) {
            stt=ealloc3float(3,3,ntr);
            ttt=ealloc2float(3,ntr);

            tmax=0.0;
            tmin=1.0e+10;
	    for (itri=0;itri<ntr;itri++) {
	          fscanf(sttfp,"%f %f %f %f %f %f %f %f %f %f %f %f\n",
		       &stt[itri][0][0],
		       &stt[itri][0][1],
		       &stt[itri][0][2],
		       &ttt[itri][0],
		       &stt[itri][1][0],
		       &stt[itri][1][1],
		       &stt[itri][1][2],
		       &ttt[itri][1],
		       &stt[itri][2][0],
		       &stt[itri][2][1],
		       &stt[itri][2][2],
		       &ttt[itri][2]);

                  tmax=MAX(tmax,ttt[itri][0]);
		  tmax=MAX(tmax,ttt[itri][1]);
		  tmax=MAX(tmax,ttt[itri][2]);

                  tmin=MIN(tmin,ttt[itri][0]);
		  tmin=MIN(tmin,ttt[itri][1]);
		  tmin=MIN(tmin,ttt[itri][2]);

		  stt[itri][0][0]=((stt[itri][0][0]-xmin)/
		       (xmax-xmin)-0.5)*DIAMETER;
		  stt[itri][0][1]=-((stt[itri][0][1]-ymin)/
		       (ymax-ymin)-0.5)*DIAMETER;
		  stt[itri][0][2]=((stt[itri][0][2]-zmin)/
		       (zmax-zmin)-0.5)*DIAMETER;

		  stt[itri][1][0]=((stt[itri][1][0]-xmin)/
		       (xmax-xmin)-0.5)*DIAMETER;
		  stt[itri][1][1]=-((stt[itri][1][1]-ymin)/
		       (ymax-ymin)-0.5)*DIAMETER;
		  stt[itri][1][2]=((stt[itri][1][2]-zmin)/
		       (zmax-zmin)-0.5)*DIAMETER;

		  stt[itri][2][0]=((stt[itri][2][0]-xmin)/
		       (xmax-xmin)-0.5)*DIAMETER;
		  stt[itri][2][1]=-((stt[itri][2][1]-ymin)/
		       (ymax-ymin)-0.5)*DIAMETER;
		  stt[itri][2][2]=((stt[itri][2][2]-zmin)/
		       (zmax-zmin)-0.5)*DIAMETER;
            }
      }

      tmax=MAX(tmax,tmin+0.01);

      glNewList(nhz*4+5,GL_COMPILE);
      for (itri=0;itri<ntr;itri++) {
	    glBegin(GL_TRIANGLE_STRIP);

            tEmission(ttt[itri][0],tmin,tmax,emission);
	    glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emission);
	    glVertex3fv(stt[itri][0]);

	    tEmission(ttt[itri][1],tmin,tmax,emission);
	    glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emission);
	    glVertex3fv(stt[itri][1]);

	    tEmission(ttt[itri][2],tmin,tmax,emission);
	    glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emission);
	    glVertex3fv(stt[itri][2]);

	    glEnd();
      }
      glEndList();

      glutMainLoop();
      return 0;
}
Beispiel #16
0
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;
}
Beispiel #17
0
int
main(int argc, char **argv)
{
	int nt;		/* number of time samples per trace */
	float dt;	/* time sampling interval */
	float ft;	/* time of first sample */
	int it;		/* time sample index */
	int cdpmin;	/* minimum cdp to process */
	int cdpmax;	/* maximum cdp to process */
	float dxcdp;	/* cdp sampling interval */
	int noffmix;	/* number of offsets to mix */
	float offmax;	/* maximum offset */
	float tmute;	/* mute time at far offset */
	float vrms;	/* rms velocity at mute time */
	int nsmax;	/* maximum number of time shifts per trace in DMO */
	int ns;		/* actual number of time shifts per trace in DMO */
	float *p;	/* input trace */
	float **q;	/* output DMO-corrected traces */
	float *temp;	/* temporary array */
	float *ts;	/* table of time shifts for DMO */
	float *as;	/* table of amplitudes for DMO */
	float offset=0.0;/* source-receiver offset of current trace */
	float oldoffset;/* offset of previous trace */
	int cdp=0;	/* cdp number of current trace */
	int ncdp;	/* number of cdps */
	int icdp;	/* cdp index */
	int jcdp;	/* cdp index */
	int jcdplo;	/* lower bound for jcdp */
	int jcdphi;	/* upper bound for jcdp */
	int ntrace;	/* number of traces processed in current mix */
	int itrace;	/* trace index */
	int noff;	/* number of offsets processed in current mix */
	int gottrace;	/* non-zero if an input trace was read */
	int done;	/* non-zero if done */
	float *ds;	/* shaping filter to complete DMO processing */
	int lds=125;	/* length of shaping filter */
	int ifds=-100;	/* time index of first sample in shaping filter */
	int verbose;	/* =1 for diagnostic print */
	char *tmpdir;	/* directory path for tmp files	*/
	cwp_Bool istmpdir=cwp_false;/* true for user given path */

	/* hook up getpar */
	initargs(argc, argv);
	requestdoc(1);

	/* get information from the first header */
	if (!gettr(&tr)) err("can't get first trace");
	nt = tr.ns;
	dt = ((double) tr.dt)/1000000.0;
	ft = tr.delrt/1000.0;

	/* get parameters */
	if (!getparint("cdpmin",&cdpmin)) err("must specify cdpmin");
	if (!getparint("cdpmax",&cdpmax)) err("must specify cdpmax");
	if (cdpmin>cdpmax) err("cdpmin must be less than cdpmax");
	if (!getparfloat("dxcdp",&dxcdp)) err("must specify dxcdp");
	if (!getparint("noffmix",&noffmix)) err("must specify noffmix");
	if (!getparfloat("offmax",&offmax)) offmax=3000.0;
	if (!getparfloat("tmute",&tmute)) tmute=2.0;
	if (!getparfloat("vrms",&vrms)) vrms=1500.0;
	if (!getparint("nsmax",&nsmax)) nsmax=400;
	if (!getparint("verbose",&verbose)) verbose=0;

	/* Look for user-supplied tmpdir */
	if (!getparstring("tmpdir",&tmpdir) &&
	    !(tmpdir = getenv("CWP_TMPDIR"))) tmpdir="";
	if (!STREQ(tmpdir, "") && access(tmpdir, WRITE_OK))
		err("you can't write in %s (or it doesn't exist)", tmpdir);
	
        checkpars();

	/* determine number of cdps */
	ncdp = cdpmax-cdpmin+1;

	/* allocate workspace */
	q = ealloc2float(nt,ncdp);
	p = ealloc1float(nt);
	temp = ealloc1float(nt);
	ts = ealloc1float(nsmax);
	as = ealloc1float(nsmax);
	ds = ealloc1float(lds);
	
	/* tabulate time shifts and amplitudes for dmo */
	maketa(dxcdp,dt,offmax,tmute,vrms,nsmax,&ns,ts,as);
	if (verbose) 
		fprintf(stderr,"\tDMO will be performed via %d time shifts\n",
			ns);
	
	/* compute shaping filter for dmo horizontal reflection response */
	makeds(ns,ts,as,lds,ifds,ds);
	
	/* open temporary file for headers */
	if (STREQ(tmpdir,"")) {
		headerfp = etmpfile();
		if (verbose) warn("using tmpfile() call");
	} else { /* user-supplied tmpdir */
		char directory[BUFSIZ];
		strcpy(directory, tmpdir);
		strcpy(headerfile, temporary_filename(directory));
		/* Trap signals so can remove temp files */
		signal(SIGINT,  (void (*) (int)) closefiles);
		signal(SIGQUIT, (void (*) (int)) closefiles);
		signal(SIGHUP,  (void (*) (int)) closefiles);
		signal(SIGTERM, (void (*) (int)) closefiles);
		headerfp = efopen(headerfile, "w+");
      		istmpdir=cwp_true;		
		if (verbose)
			warn("putting temporary header file in %s", directory);
	}
	
	/* initialize */
	oldoffset = tr.offset;
	gottrace = 1;
	done = 0;
	ntrace = 0;
	noff = 0;
	for (icdp=0; icdp<ncdp; ++icdp)
		for (it=0; it<nt; ++it)
			q[icdp][it] = 0.0;

	/* loop over traces */
	do {
		
		/* if got a trace */
		if (gottrace) {

			/* determine offset and cdp */
			offset = tr.offset;
			cdp = tr.cdp;
		
			/* update number of offsets mixed */
			if (offset!=oldoffset) noff++;

			/* get trace samples */
 			memcpy( (void *) p,
				  (const void *) tr.data, nt*sizeof(float));
		}
		
		/* if a mix of offsets is complete */
		if (noff==noffmix || !gottrace) {
			
			/* update number of offsets mixed */
			if (!gottrace) noff++; 
			
			/* apply shaping filter to complete dmo processing */
			for (icdp=0; icdp<ncdp; ++icdp) {
				convolve_cwp(lds,ifds,ds,nt,0,q[icdp],nt,0,temp);
				memcpy( (void *) q[icdp],
					(const void *) temp, nt*sizeof(float));
			}
			
			/* rewind trace header file */
			erewind(headerfp);
			
			/* loop over all output traces */
			for (itrace=0; itrace<ntrace; ++itrace) {
			
				/* read trace header and determine cdp index */
				efread(&tro,HDRBYTES,1,headerfp);
				icdp = tro.cdp-cdpmin;
				
				/* get dmo-corrected data */
				memcpy((void *) tro.data,
				      (const void *) q[icdp],nt*sizeof(float));
				
				/* write output trace */
				puttr(&tro);
			}
			
			/* report */
			if (verbose) 
				fprintf(stderr,"\tCompleted mix of "
					"%d offsets with %d traces\n",
					noff,ntrace);
			
			/* if no more traces, break */
			if (!gottrace) break;
			
			/* rewind trace header file */
			erewind(headerfp);
			
			/* reset number of offsets and traces */
			noff = 0;
			ntrace = 0;
			
			/* zero dmo accumulators */
			for (icdp=0; icdp<ncdp; ++icdp)
				for (it=0; it<nt; ++it)
					q[icdp][it] = 0.0;
		}
				
		/* if cdp is within range of cdps to process */
		if (cdp>=cdpmin && cdp<=cdpmax) {
		
			/* save trace header and update number of traces */
			efwrite(&tr,HDRBYTES,1,headerfp);
			ntrace++;
			
			/* determine output traces potentially modified
			   by input */
			icdp = cdp-cdpmin;
			jcdplo = MAX(0,icdp-0.5*ABS(offset/dxcdp));
			jcdphi = MIN(ncdp-1,icdp+0.5*ABS(offset/dxcdp));
			
			/* loop over potentially modified output traces */
			for (jcdp=jcdplo; jcdp<=jcdphi; ++jcdp) {
		
				/* do dmo for one output trace */
				dmotx(ns,ts,as,offset,(jcdp-icdp)*dxcdp,dxcdp,
				      0,nt,dt,ft,p,q[jcdp]);
			}

			/* remember offset */
			oldoffset = offset;
		}

		/* get next trace (if there is one) */
		if (!gettr(&tr)) gottrace = 0;
		
	} while (!done);

	/* clean up */
	efclose(headerfp);
	if (istmpdir) eremove(headerfile);
	return(CWP_Exit());
}
Beispiel #18
0
int
main (int argc, char **argv)
{
	int 	nt,nzt,nxt,nz,nxo,nxs,ns,nx,nr,is,ixo,ixs;
	int 	ls,jtr,mtr,tracl,mzmax;
	off_t nseek;
	float   ft,fzt,fxt,fz,fx,fxo,fs,fxs,dt,dzt,dxt,dz,dx,dxo,ds,dxs,
		ext,ezt,es,ex,ez,xo,s,xs,xg,fpeak;	
	float 	v0,dvz;
	float 	fmax,angmax,offmax,rmax,aperx;
	float **mig,**migi,***ttab,**tb,**pb,**cosb,**sigb,**tsum,**tt;
	
	char *infile="stdin",*outfile="stdout",*ttfile,*jpfile;
	FILE *infp,*outfp,*ttfp,*jpfp;
	Wavelet *w;


	/* hook up getpar to handle the parameters */
	initargs(argc, argv);
	requestdoc(1);

	/* open input and output files	*/
	if( !getparstring("infile",&infile)) {
		infp = stdin;
	} else  
		if ((infp=fopen(infile,"r"))==NULL)
			err("cannot open infile=%s\n",infile);
	if( !getparstring("outfile",&outfile)) {
		outfp = stdout;
	} else { 
		outfp = fopen(outfile,"w");
	}
	efseeko(infp,(off_t) 0,SEEK_CUR);
	warn("Got A");
	efseeko(outfp,(off_t) 0,SEEK_CUR);
	if( !getparstring("ttfile",&ttfile))
		err("must specify ttfile!\n");
	if ((ttfp=fopen(ttfile,"r"))==NULL)
		err("cannot open ttfile=%s\n",ttfile);
	if( !getparstring("jpfile",&jpfile)) {
		jpfp = stderr;
	} else  
		jpfp = fopen(jpfile,"w");

	/* get information for seismogram traces */
	if (!getparint("nt",&nt)) nt = 501;
	if (!getparfloat("dt",&dt)) dt = 0.004;
	if (!getparfloat("ft",&ft)) ft = 0.0;
	if (!getparfloat("fmax",&fmax)) fmax = 1.0/(4*dt);
	fpeak = 0.2/dt;
	if (!getparint("nxs",&nxs)) nxs = 101;
	if (!getparfloat("dxs",&dxs)) dxs = 15;
	if (!getparfloat("fxs",&fxs)) fxs = 0.0;
	if (!getparint("nxo",&nxo)) nxo = 1;
	if (!getparfloat("dxo",&dxo)) dxo = 50;
	if (!getparfloat("fxo",&fxo)) fxo = 0.0;
	
	/* get traveltime table parameters	*/
	if (!getparint("nxt",&nxt)) err("must specify nxt!\n");
	if (!getparfloat("fxt",&fxt)) err("must specify fxt!\n");
	if (!getparfloat("dxt",&dxt)) err("must specify dxt!\n");
	if (!getparint("nzt",&nzt)) err("must specify nzt!\n");
	if (!getparfloat("fzt",&fzt)) err("must specify fzt!\n");
	if (!getparfloat("dzt",&dzt)) err("must specify dzt!\n");
	if (!getparint("ns",&ns)) err("must specify ns!\n");
	if (!getparfloat("fs",&fs)) err("must specify fs!\n");
	if (!getparfloat("ds",&ds)) err("must specify ds!\n");
	ext = fxt+(nxt-1)*dxt;
	ezt = fzt+(nzt-1)*dzt;
	es = fs+(ns-1)*ds;

	/* check source and receiver coordinates */
 	for (ixs=0; ixs<nxs; ++ixs) {
		xs = fxs+ixs*dxs;
		for (ixo=0; ixo<nxo; ++ixo) {
			xg = xs+fxo+ixo*dxo;
			if (fs>xs || es<xs || fs>xg || es<xg) 
		err("shot or receiver lie outside of specified (x,z) grid\n");
		}
	} 

	/* get migration section parameters	*/
	if (!getparint("nx",&nx)) err("must specify nx!\n");
	if (!getparfloat("fx",&fx)) err("must specify fx!\n");
	if (!getparfloat("dx",&dx)) err("must specify dx!\n");
	if (!getparint("nz",&nz)) err("must specify nz!\n");
	if (!getparfloat("fz",&fz)) err("must specify fz!\n");
	if (!getparfloat("dz",&dz)) err("must specify dz!\n");
	ex = fx+(nx-1)*dx;
	ez = fz+(nz-1)*dz;
	if(fxt>fx || ext<ex || fzt>fz || ezt<ez) 
		err(" migration section is out of traveltime table!\n");

	if (!getparfloat("v0",&v0)) v0 = 1500;
	if (!getparfloat("dvz",&dvz)) dvz = 0;
	if (!getparfloat("angmax",&angmax)) angmax = 60.;
	if  (angmax<0.00001) err("angmax must be positive!\n");
	mzmax = dx*sin(angmax*PI/180.)/dz;
	if(mzmax<1) mzmax = 1;
	if (!getparfloat("aperx",&aperx)) aperx = 0.5*nxt*dxt;

	if (!getparint("ls",&ls))	ls = 1;
	if (!getparint("mtr",&mtr))	mtr = 100;

	fprintf(jpfp,"\n");
	fprintf(jpfp," Modeling parameters\n");
	fprintf(jpfp," ================\n");
	fprintf(jpfp," infile=%s \n",infile);
	fprintf(jpfp," outfile=%s \n",outfile);
	fprintf(jpfp," ttfile=%s \n",ttfile);
	fprintf(jpfp," \n");
	fprintf(jpfp," nzt=%d fzt=%g dzt=%g\n",nzt,fzt,dzt);
	fprintf(jpfp," nxt=%d fxt=%g dxt=%g\n",nxt,fxt,dxt);
 	fprintf(jpfp," ns=%d fs=%g ds=%g\n",ns,fs,ds);
	fprintf(jpfp," \n");
	fprintf(jpfp," nz=%d fz=%g dz=%g\n",nz,fz,dz);
	fprintf(jpfp," nx=%d fx=%g dx=%g\n",nx,fx,dx);
	fprintf(jpfp," \n");
	fprintf(jpfp," nxs=%d fxs=%g dxs=%g\n",nxs,fxs,dxs);
	fprintf(jpfp," nxo=%d fxo=%g dxo=%g\n",nxo,fxo,dxo);
	fprintf(jpfp," \n");
	
	/* compute reference traveltime and slowness  */
	offmax = MAX(ABS(fxo),ABS(fxo+(nxo-1)*dxo));
	rmax = MAX(es-fxt,ext-fs);
	rmax = MIN(rmax,0.5*offmax+aperx);
	nr = 2+(int)(rmax/dx);
	tb = ealloc2float(nzt,nr);
	pb = ealloc2float(nzt,nr);
	sigb = ealloc2float(nzt,nr);
	cosb = ealloc2float(nzt,nr);
	timeb(nr,nzt,dx,dzt,fzt,dvz,v0,tb,pb,sigb,cosb);

	fprintf(jpfp," nt=%d ft=%g dt=%g fpeak=%g \n",nt,ft,dt,fpeak);
	fprintf(jpfp," v0=%g dvz=%g \n",v0,dvz);
 	fprintf(jpfp," aperx=%g angmax=%g offmax=%g\n",aperx,angmax,offmax);
 	fprintf(jpfp," mtr=%d ls=%d\n",mtr,ls);
	fprintf(jpfp," ================\n");
	fflush(jpfp);

	/* allocate space */
	mig = ealloc2float(nz,nx);
	migi = ealloc2float(nz+2*mzmax,nx);
	ttab = ealloc3float(nzt,nxt,ns);
	tt = ealloc2float(nzt,nxt);
	tsum = ealloc2float(nzt,nxt);


	/* make wavelet */
	makericker(fpeak,dt,&w);

	/* set constant segy trace header parameters */
	memset((void *) &tr, 0, sizeof(segy));
	tr.trid = 1;
	tr.counit = 1;
	tr.ns = nt;
	tr.dt = 1.0e6*dt;
	tr.delrt = 1.0e3*ft;

	fprintf(jpfp," read traveltime tables \n");
	fflush(jpfp);

	/* compute traveltime residual	*/
	for(is=0; is<ns; ++is){
		nseek = (off_t) nxt*nzt*is;
		efseeko(ttfp,nseek*((off_t) sizeof(float)),SEEK_SET);
		fread(ttab[is][0],sizeof(float),nxt*nzt,ttfp);
		s = fs+is*ds;
		resit(nxt,fxt,dxt,nzt,nr,dx,tb,ttab[is],s);		
	}

	fprintf(jpfp," read migration section \n");
	fflush(jpfp);

	/* read migration section	*/
	fread(mig[0],sizeof(float),nx*nz,infp);

	/* integrate migration section for constructing anti-aliasing 
		filter	*/
	integ(mig,nz,dz,nx,mzmax,migi);
                       
	fprintf(jpfp," start synthesis ... \n");
	fprintf(jpfp," \n");
	fflush(jpfp);
	
	jtr = 0;
	/* loop over shots  */
	for (ixs=0,xs=fxs,tracl=0; ixs<nxs; ++ixs,xs+=dxs) {
		/* loop over offsets */
		for (ixo=0,xo=fxo; ixo<nxo; ++ixo,xo+=dxo) {
	    		float as,res;
	    		int is;
			xg = xs+xo; 
			/* set segy trace header parameters */
			tr.tracl = tr.tracr = ++tracl;
			tr.fldr = 1+ixs;
			tr.tracf = 1+ixo;
			tr.offset = NINT(xo);
			tr.sx = NINT(xs);
			tr.gx = NINT(xg);

	    		as = (xs-fs)/ds;
	    		is = (int)as;
			if(is==ns-1) is=ns-2;
			res = as-is;
			if(res<=0.01) res = 0.0;
			if(res>=0.99) res = 1.0;
			sum2(nxt,nzt,1-res,res,ttab[is],ttab[is+1],tsum);

	    		as = (xg-fs)/ds;
	    		is = (int)as;
			if(is==ns-1) is=ns-2;
			res = as-is;
			if(res<=0.01) res = 0.0;
			if(res>=0.99) res = 1.0;
			sum2(nxt,nzt,1-res,res,ttab[is],ttab[is+1],tt);
			sum2(nxt,nzt,1,1,tt,tsum,tsum);
				fflush(jpfp);

			/* make one trace */
			maketrace(tr.data,nt,ft,dt,xs,xg,mig,migi,aperx,
			  nx,fx,dx,nz,fz,dz,mzmax,ls,angmax,v0,fmax,w,
			  tb,pb,sigb,cosb,nr,tsum,nxt,fxt,dxt,nzt,fzt,dzt);
			
			/* write trace */
			fputtr(outfp,&tr);

	        	jtr++;
	        	if((jtr-1)%mtr ==0 ){
				fprintf(jpfp," generated trace %d\n",jtr);
				fflush(jpfp);
	    		}
		}
	}


	fprintf(jpfp," generated %d traces in total\n",jtr);


	fprintf(jpfp," \n");
	fprintf(jpfp," output done\n");
	fflush(jpfp);

	efclose(jpfp);
	efclose(outfp);

	    
	free2float(tsum);
	free2float(tt);
	free2float(pb);
	free2float(tb);
	free2float(cosb);
	free2float(sigb);
	free3float(ttab);
	free2float(mig);
	free2float(migi);

	return(CWP_Exit());
}
Beispiel #19
0
int
main(int argc, char **argv)
{
	float phase;		/* phase shift = phasefac*PI		*/
	float power;		/* phase shift = phasefac*PI		*/
	register float *rt;	/* real trace				*/
	register complex *ct;	/* complex transformed trace		*/
	complex *filt;		/* complex power	 		*/
	int nt;			/* number of points on input trace	*/
	size_t ntsize;		/* nt in bytes				*/
	int ncdp;               /* number of cdps specified */
	int icdp;       	/* index into cdp array */

	long oldoffset;         /* offset of previous trace */
        long oldcdp;    	/* cdp of previous trace */
        int newsloth;   	/* if non-zero, new sloth function was computed */
	int jcdp;       	/* index into cdp array */
	float dt;		/* sample spacing (secs) on input trace	*/
	float tn;		/* sample spacing (secs) on input trace	*/
	float omega;		/* circular frequency			*/
	float domega;		/* circular frequency spacing (from dt)	*/
	int nfft;		/* number of points in nfft		*/
	int ntnmo;      	/* number of tnmos specified            */
	float *cdp;     	/* array[ncdp] of cdps */

	float *vnmo;   		/* array[nvnmo] of vnmos               */
	float *ovvt;   		/* array[nvnmo] of vnmos               */
	int nvnmo;      	/* number of tnmos specified            */
	float *fnmo;   		 /* array[ntnmo] of tnmos               */
	float **ovv;   		 /* array[nf] of fnmos                  */
	float doffs;             /* offset                            */
	float acdp;     	/* temporary used to sort cdp array */
        float *aovv;    	/* temporary used to sort ovv array */

        int invert;              /*  if non-zero, do invers DLMO       */
        int cm;                  /*  if non-zero, the offset in cm     */
        int nf;                 /* number of frequencies (incl Nyq)     */
        int it;                 /* number of frequencies (incl Nyq)     */
	float onfft;		/* 1 / nfft				*/
	float v;                 /* velocity                            */
	size_t nzeros;		/* number of padded zeroes in bytes	*/
	
	
	/* Initialize */
	initargs(argc, argv);
	requestdoc(1);

	/* Set parameters */
	power=0.0;

	/* Get info from first trace*/ 
	if (!gettr(&tr))	err("can't get first trace");

	nt = tr.ns;

	if (!getparfloat("dt", &dt))	dt = ((double) tr.dt)/1000000.0;
	if (!dt)	err("dt field is zero and not getparred");
	ntsize = nt * FSIZE;

        if (!getparint("invert",&invert)) invert = 0;
        if (!getparint("cm",&cm)) cm = 0;

	/* Set up for fft */
	nfft = npfaro(nt, LOOKFAC * nt);
	if (nfft >= SU_NFLTS || nfft >= PFA_MAX)
		err("Padded nt=%d -- too big", nfft);

        nf = nfft/2 + 1;
        onfft = 1.0 / nfft;
	nzeros = (nfft - nt) * FSIZE;
	domega = TWOPI * onfft / dt;


	/* get velocity functions, linearly interpolated in frequency */

	ncdp = countparval("cdp");

	if (ncdp>0) {
                if (countparname("vnmo")!=ncdp)
                        err("a vnmo array must be specified for each cdp");
                if (countparname("fnmo")!=ncdp)
                        err("a tnmo array must be specified for each cdp");
        } else {
                ncdp = 1;
                if (countparname("vnmo")>1)
                        err("only one (or no) vnmo array must be specified");
                if (countparname("fnmo")>1)
                        err("only one (or no) tnmo array must be specified");
        }

	cdp = ealloc1float(ncdp);
        if (!getparfloat("cdp",cdp)) cdp[0] = tr.cdp;
        ovv = ealloc2float(nf,ncdp);

	for (icdp=0; icdp<ncdp; ++icdp) {
                nvnmo = countnparval(icdp+1,"vnmo");
                ntnmo = countnparval(icdp+1,"fnmo");
                if (nvnmo!=ntnmo && !(ncdp==1 && nvnmo==1 && ntnmo==0))
                        err("number of vnmo and tnmo values must be equal");
                if (nvnmo==0) nvnmo = 1;
                if (ntnmo==0) ntnmo = nvnmo;
                /* equal numbers of parameters vnmo, fnmo  */

                vnmo = ealloc1float(nvnmo);
                fnmo = ealloc1float(nvnmo);

                if (!getnparfloat(icdp+1,"vnmo",vnmo)) vnmo[0] = 400.0;
                if (!getnparfloat(icdp+1,"fnmo",fnmo)) fnmo[0] = 0.0;
		

		for (it=0; it<ntnmo; ++it)
			fnmo[it]*=TWOPI;

                for (it=1; it<ntnmo; ++it)
                        if (fnmo[it]<=fnmo[it-1])
                                err("tnmo values must increase monotonically");

		for (it=0,tn=0; it<nf; ++it,tn+=domega) {
			intlin(ntnmo,fnmo,vnmo,vnmo[0],vnmo[nvnmo-1],1,&tn,&v);
			ovv[icdp][it] = 1.0/(v); 
		}
                free1float(vnmo);
                free1float(fnmo);
        }



/* sort (by insertion) sloth and anis functions by increasing cdp */

        for (jcdp=1; jcdp<ncdp; ++jcdp) {
                acdp = cdp[jcdp];
                aovv = ovv[jcdp];
                for (icdp=jcdp-1; icdp>=0 && cdp[icdp]>acdp; --icdp) {
                        cdp[icdp+1] = cdp[icdp];
                        ovv[icdp+1] = ovv[icdp];
                }
                cdp[icdp+1] = acdp;
                ovv[icdp+1] = aovv;
        }

/* allocate workspace */


        ovvt = ealloc1float(nf);

/* interpolate sloth and anis function for first trace */

        interpovv(nf,ncdp,cdp,ovv,(float)tr.cdp,ovvt);

        /* set old cdp and old offset for first trace */
        oldcdp = tr.cdp;
        oldoffset = tr.offset-1;



	/* Allocate fft arrays */
	rt   = ealloc1float(nfft);
	ct   = ealloc1complex(nf);
	filt = ealloc1complex(nf);


		

	/* Loop over traces */
	do {

		/* if necessary, compute new sloth and anis function */
                	if (tr.cdp!=oldcdp && ncdp>1) {
                        interpovv(nt,ncdp,cdp,ovv,(float)tr.cdp,
                                  ovvt);
                        newsloth = 1;
                } else {
                        newsloth = 0;
                }

		/* if sloth and anis function or offset has changed */

                if (newsloth || tr.offset!=oldoffset) {

		doffs = (fabs)((float)(tr.offset));
		if (cm==1) doffs/=100;
		/* Load trace into rt (zero-padded) */
		memcpy( (void *) rt, (const void *) tr.data, ntsize);
		memset((void *) (rt + nt), (int) '\0', nzeros);

		/* FFT */
		pfarc(1, nfft, rt, ct);


		/* Apply filter */
		{ register int i;
		for (i = 0; i < nf; ++i){
			omega = i * domega;
			if (power < 0 && i == 0) omega = FLT_MAX;
			if (invert==0)
			phase = -1.0*omega*ovvt[i]*doffs;
			else
			phase = 1.0*omega*ovvt[i]*doffs;
			
		/*	filt[i] = cmplx(cos(phase),sin(phase)); */
			filt[i] = cwp_cexp(crmul(I,phase)); 
			filt[i] = crmul(filt[i], onfft);
			ct[i] = cmul(ct[i], filt[i]);
			
			}
		}
	  }
		/* Invert */
		pfacr(-1, nfft, ct, rt);


		/* Load traces back in, recall filter had nfft factor */
		{ register int i;
		for (i = 0; i < nt; ++i)  tr.data[i] = rt[i];
		}


		puttr(&tr);

	} while (gettr(&tr));


	return EXIT_SUCCESS;
}
Beispiel #20
0
void maketrace(float *trace,int nt,float ft,float dt,
	float xs,float xg,float **mig,float **migi,float aperx,
  	int nx,float fx,float dx,int nz,float fz,float dz,
	int mzmax,int ls,float angmax,float v0,float fmax,Wavelet *w,
	float **tb,float **pb,float **sigb,float **cosb,int nr,float **tsum,
	int nxt,float fxt,float dxt,int nzt,float fzt,float dzt)
/*****************************************************************************
Make one synthetic seismogram 
******************************************************************************
Input:
**mig		migration section 
**migi		integrated migration section 
nt		number of time samples in seismic trace
ft		first time sample of seismic trace
dt		time sampleing interval in seismic trace
xs,xg		lateral coordinates of source and geophone 
aperx		lateral aperature in migration
nx,fx,dx,nz,fz,dz	dimension parameters of migration region
mzmax		number of depth samples in triangle filter
ls		=1 for line source; =0 for point source
w		wavelet to convolve with trace
angmax		migration angle aperature from vertical 	 
tb,pb,sigb,cosb		reference traveltime, lateral slowness, sigma 
		and cosine of emergent angle
nr		number of lateral samples in reference quantities
tsum		sum of residual traveltimes from shot and receiver
nxt,fxt,dxt,nzt,fzt,dzt		dimension parameters of traveltime table

Output:
trace		array[nt] containing synthetic seismogram
*****************************************************************************/
{
	int nxf,nxe,nxtf,nxte,ix,iz,iz0,izt0,nzp,jrs,jrg,jz,jt,jx,mz,iz1;
	float xm,x,dis,rxz,ar,srs,srg,srs0,srg0,sigp,z0,rdz,ampd,
	      sigs,sigg,coss,cosg,ax,ax0,pmin,
	      odt=1.0/dt,pd,az,sz,sz0,at,td,res,temp;
	float *zpt,**ampt,**ampti,**zmt,*amp,*ampi,*zm,*tzt,*work1;
	int lhd=LHD,nhd=NHD;
	static float hd[NHD];
	static int madehd=0;

	/* if half-derivative filter not yet made, make it */
	if (!madehd) {
		mkhdiff(dt,lhd,hd);
		madehd = 1;
	}
 
	/* zero trace */
	for (jt=0; jt<nt; ++jt)
		trace[jt] = 0.0;

	zmt = ealloc2float(nzt,nxt);
	ampt = ealloc2float(nzt,nxt);
	ampti = ealloc2float(nzt,nxt);
	amp = ealloc1float(nzt);
	ampi = ealloc1float(nzt);
	zm = ealloc1float(nzt);
	tzt = ealloc1float(nzt);
	zpt = ealloc1float(nxt);
	work1 = ealloc1float(nt);

	z0 = (fz-fzt)/dzt;
	pmin = 1.0/(2.0*dx*fmax);
	rdz = dz/dzt;

	xm = 0.5*(xs+xg);
	rxz = (angmax==90)?0.0:1.0/tan(angmax*PI/180.);
	nxtf = (xm-aperx-fxt)/dxt;
	if(nxtf<0) nxtf = 0;
	nxte = (xm+aperx-fxt)/dxt+1.0;
	if(nxte>=nxt) nxte = nxt-1;

	/* compute amplitudes 	*/
	for(ix=nxtf; ix<=nxte; ++ix){
		x = fxt+ix*dxt;
		dis = (xm>=x)?xm-x:x-xm;
		izt0 = ((dis-dxt)*rxz-fzt)/dzt-1;
		if(izt0<0) izt0 = 0;
		if(izt0>=nzt) izt0 = nzt-1;

		ar = (xs>=x)?(xs-x)/dx:(x-xs)/dx;
		jrs = (int)ar;
		if(jrs>nr-2) jrs = nr-2;
		srs = ar-jrs;
		srs0 = 1.0-srs;
		ar = (xg>=x)?(xg-x)/dx:(x-xg)/dx;
		jrg = (int)ar;
		if(jrg>nr-2) jrg = nr-2;
		srg = ar-jrg;
		srg0 = 1.0-srg;
		sigp = ((xs-x)*(xg-x)>0)?1.0:-1.0;
		zpt[ix] = fzt+(nzt-1)*dzt;

		for(iz=izt0; iz<nzt; ++iz){
			sigs = srs0*sigb[jrs][iz]+srs*sigb[jrs+1][iz]; 
			sigg = srg0*sigb[jrg][iz]+srg*sigb[jrg+1][iz]; 
			coss = srs0*cosb[jrs][iz]+srs*cosb[jrs+1][iz]; 
			cosg = srg0*cosb[jrg][iz]+srg*cosb[jrg+1][iz]; 
			ampd = v0*dx*(coss+cosg)*dz;
			if(ampd<0.0) ampd = -ampd;
			if(ls) 
			    ampt[ix][iz] = ampd/sqrt(v0*sigs*sigg);
			else 
			    ampt[ix][iz] = ampd/sqrt(sigs*sigg*(sigs+sigg));

			pd = srs0*pb[jrs][iz]+srs*pb[jrs+1][iz]+sigp 
			     *(srg0*pb[jrg][iz]+srg*pb[jrg+1][iz]);
			if(pd<0.0) pd = -pd;
			if(pd<0.0) pd = -pd;
			temp = 0.5*pd*v0*dx/dz;
			if(temp<1) temp = 1.0;
			if(temp>mzmax) temp = mzmax;
			ampti[ix][iz] = ampt[ix][iz]/(temp*temp);
			zmt[ix][iz] = temp;
			if(pd<pmin && zpt[ix]>fzt+(nzt-1.1)*dzt) 
				zpt[ix] = fzt+iz*dzt;

		}
	}
	
	nxf = (xm-aperx-fx)/dx+0.5;
	if(nxf<0) nxf = 0;
	nxe = (xm+aperx-fx)/dx+0.5;
	if(nxe>=nx) nxe = nx-1;
	
	/* interpolate amplitudes */
	for(ix=nxf; ix<=nxe; ++ix){
		x = fx+ix*dx;
		dis = (xm>=x)?xm-x:x-xm;
		izt0 = (dis*rxz-fzt)/dzt-1;
		if(izt0<0) izt0 = 0;
		if(izt0>=nzt) izt0 = nzt-1;
		iz0 = (dis*rxz-fz)/dz;
		if(iz0<0) iz0 = 0;
		if(iz0>=nz) iz0 = nz-1;

		ax = (x-fxt)/dxt;
		jx = (int)ax;
		ax = ax-jx;
		if(ax<=0.01) ax = 0.;
		if(ax>=0.99) ax = 1.0;
		ax0 = 1.0-ax;
		if(jx>nxte-1) jx = nxte-1;
		if(jx<nxtf) jx = nxtf;

		ar = (xs>=x)?(xs-x)/dx:(x-xs)/dx;
		jrs = (int)ar;
		if(jrs>=nr-1) jrs = nr-2;
		srs = ar-jrs;
		srs0 = 1.0-srs;
		ar = (xg>=x)?(xg-x)/dx:(x-xg)/dx;
		jrg = (int)ar;
		if(jrg>=nr-1) jrg = nr-2;
		srg = ar-jrg;
		srg0 = 1.0-srg;

		for(iz=izt0; iz<nzt; ++iz){
		    tzt[iz] = ax0*tsum[jx][iz]+ax*tsum[jx+1][iz]
				+srs0*tb[jrs][iz]+srs*tb[jrs+1][iz]
				+srg0*tb[jrg][iz]+srg*tb[jrg+1][iz];

		    amp[iz] = ax0*ampt[jx][iz]+ax*ampt[jx+1][iz];
		    ampi[iz] = ax0*ampti[jx][iz]+ax*ampti[jx+1][iz];
		    zm[iz] = ax0*zmt[jx][iz]+ax*zmt[jx+1][iz];
		
		}

		nzp = (ax0*zpt[jx]+ax*zpt[jx+1]-fz)/dz+1.5;
		if(nzp<iz0) nzp = iz0;
		if(nzp>nz) nzp = nz;

		/* interpolate along depth if operater aliasing 	*/
		for(iz=iz0; iz<nzp; ++iz) {
			az = z0+iz*rdz;
			jz = (int)az;
			if(jz>=nzt-1) jz = nzt-2;
			sz = az-jz;
			sz0 = 1.0-sz;
			td = sz0*tzt[jz]+sz*tzt[jz+1];
			at = (td-ft)*odt;
			jt = (int)at;
			if(jt > 0 && jt < nt-1){
			    ampd = sz0*ampi[jz]+sz*ampi[jz+1];
			    mz = (int)(0.5+sz0*zm[jz]+sz*zm[jz+1]);
			    res = at-jt;
			    iz1 = iz+mzmax;
 			    temp = (-migi[ix][iz1-mz]+2.0*migi[ix][iz1]
				-migi[ix][iz1+mz])*ampd;				
			    trace[jt] += (1.0-res)*temp;
			    trace[jt+1] += res*temp;
			}
		}

		/* interpolate along depth if not operater aliasing 	*/
		for(iz=nzp; iz<nz; ++iz) {
			az = z0+iz*rdz;
			jz = (int)az;
			if(jz>=nzt-1) jz = nzt-2;
			sz = az-jz;
			sz0 = 1.0-sz;
			td = sz0*tzt[jz]+sz*tzt[jz+1];
			at = (td-ft)*odt;
			jt = (int)at;
			if(jt > 0 && jt < nt-1){
			    ampd = sz0*amp[jz]+sz*amp[jz+1];
			    res = at-jt;
			    temp = mig[ix][iz]*ampd;
			    trace[jt] += (1.0-res)*temp;
			    trace[jt+1] += res*temp;
			}
		}
	}
					   
		
	
	/* apply half-derivative filter to trace */
	convolve_cwp(nhd,-lhd,hd,nt,0,trace,nt,0,work1);

	/* convolve wavelet with trace */
 	convolve_cwp(w->lw,w->iw,w->wv,nt,0,work1,nt,0,trace); 	
	
	/* free workspace */
	free2float(ampt);
	free2float(ampti);
	free1float(tzt);
	free1float(work1);
 	free1float(amp);
 	free1float(ampi);
 	free2float(zmt);
 	free1float(zpt);
 	free1float(zm);
}
Beispiel #21
0
int
main(int argc, char **argv)
{
	int nt;		/* number of time samples per trace */
	float dt;	/* time sampling interval */
	float ft;	/* time of first sample */
	int it;		/* time sample index */
	int ncdp;	/* number of cdps specified */
	float *cdp;	/* array[ncdp] of cdps */
	int icdp;	/* index into cdp array */
	int jcdp;	/* index into cdp array */
	int nvnmo;	/* number of vnmos specified */
	float *vnmo;	/* array[nvnmo] of vnmos */
	int ntnmo;	/* number of tnmos specified */
	float *tnmo;	/* array[ntnmo] of tnmos */
	float **ovv;	/* array[ncdp][nt] of sloth (1/velocity^2) functions */
	float *ovvt;	/* array[nt] of sloth for a particular trace */
	int nanis1;	/* number of anis1's specified */
	int nanis2;	/* number of anis2's specified */
	float *anis1;	/* array[nanis1] of anis1's */
	float *anis2;	/* array[nanis2] of anis2's */
	float **oa1;	/* array[ncdp][nt] of anis1 functions */
	float **oa2;	/* array[ncdp][nt] of anis2 functions */
	float *oa1t;	/* array[nt] of anis1 for a particular trace */
	float *oa2t;	/* array[nt] of anis2 for a particular trace */
	float smute;	/* zero samples with NMO stretch exceeding smute */
	float osmute;	/* 1/smute */
	int lmute;	/* length in samples of linear ramp for mute */
	int itmute=0;	/* zero samples with indices less than itmute */
	int sscale;	/* if non-zero, apply NMO stretch scaling */
	int invert;	/* if non-zero, do inverse NMO */
	float sy;	/* cross-line offset component */
	int ixoffset;	/* indes for cross-line offset component */
	long oldoffset;	/* offset of previous trace */
	long oldcdp;	/* cdp of previous trace */
	int newsloth;	/* if non-zero, new sloth function was computed */
	float tn;	/* NMO time (time after NMO correction) */
	float v;	/* velocity */
	float *qtn;	/* NMO-corrected trace q(tn) */
	float *ttn;	/* time t(tn) for NMO */
	float *atn;	/* amplitude a(tn) for NMO */
	float *qt;	/* inverse NMO-corrected trace q(t) */
	float *tnt;	/* time tn(t) for inverse NMO */
	float *at;	/* amplitude a(t) for inverse NMO */
	float acdp;	/* temporary used to sort cdp array */
	float *aovv;	/* temporary used to sort ovv array */
	float *aoa1;	/* temporary used to sort oa1 array */
	float *aoa2;	/* temporary used to sort oa2 array */
	float temp;	/* temporary float */
	float tsq;	/* temporary float */
	int i;		/* index used in loop */
	int upward;	/* scans upward if it's nonzero. */

	/* hook up getpar */
	initargs(argc, argv);
	requestdoc(1);

	/* get information from the first header */
	if (!gettr(&tr)) err("can't get first trace");
	nt = tr.ns;
	dt = ((double) tr.dt)/1000000.0;
	ft = tr.delrt/1000.0;
	sy  = tr.sy;

	/* get velocity functions, linearly interpolated in time */
	ncdp = countparval("cdp");
	if (ncdp>0) {
		if (countparname("vnmo")!=ncdp)
			err("a vnmo array must be specified for each cdp");
		if (countparname("tnmo")!=ncdp)
			err("a tnmo array must be specified for each cdp");
		if (countparname("anis1")!=ncdp &&
		    countparname("anis1")!=0)
			err("an anis1 array must be specified for each cdp, "
			    "or omitted at all");
		if (countparname("anis2")!=ncdp &&
		    countparname("anis2")!=0)
			err("an anis2 array must be specified for each cdp, "
			    "or omitted at all");
	} else {
		ncdp = 1;
		if (countparname("vnmo")>1)
			err("only one (or no) vnmo array must be specified");
		if (countparname("tnmo")>1)
			err("only one (or no) tnmo array must be specified");
		if (countparname("anis1")>1)
			err("only one (or no) anis1 array must be specified");
		if (countparname("anis2")>1)
			err("only one (or no) anis2 array must be specified");
	}
	cdp = ealloc1float(ncdp);
	if (!getparfloat("cdp",cdp)) cdp[0] = tr.cdp;
	ovv = ealloc2float(nt,ncdp);
	oa1 = ealloc2float(nt,ncdp);
	oa2 = ealloc2float(nt,ncdp);
	for (icdp=0; icdp<ncdp; ++icdp) {
		nvnmo = countnparval(icdp+1,"vnmo");
		ntnmo = countnparval(icdp+1,"tnmo");
		nanis1 = countnparval(icdp+1,"anis1");
		nanis2 = countnparval(icdp+1,"anis2");
		if (nvnmo!=ntnmo && !(ncdp==1 && nvnmo==1 && ntnmo==0))
			err("number of vnmo and tnmo values must be equal");
		if (nanis1!=nvnmo && nanis1 != 0)
			err("number of vnmo and anis1 values must be equal");
		if (nanis2!=nvnmo && nanis2 != 0)
			err("number of vnmo and anis2 values must be equal");
		if (nvnmo==0) nvnmo = 1;
		if (ntnmo==0) ntnmo = nvnmo;
		if (nanis1==0) nanis1 = nvnmo;
		if (nanis2==0) nanis2 = nvnmo;
		/* equal numbers of parameters vnmo, tnmo, anis1, anis2 */
		vnmo = ealloc1float(nvnmo);
		tnmo = ealloc1float(nvnmo);
		anis1 = ealloc1float(nvnmo);
		anis2 = ealloc1float(nvnmo);
		if (!getnparfloat(icdp+1,"vnmo",vnmo)) vnmo[0] = 1500.0;
		if (!getnparfloat(icdp+1,"tnmo",tnmo)) tnmo[0] = 0.0;
		if (!getnparfloat(icdp+1,"anis1",anis1)) 
			for (i=0; i<nvnmo; i++) anis1[i] = 0.0;
		if (!getnparfloat(icdp+1,"anis2",anis2))
			for (i=0; i<nvnmo; i++) anis2[i] = 0.0;
		for (it=1; it<ntnmo; ++it)
			if (tnmo[it]<=tnmo[it-1])
				err("tnmo values must increase monotonically");
		for (it=0,tn=ft; it<nt; ++it,tn+=dt) {
			intlin(ntnmo,tnmo,vnmo,vnmo[0],vnmo[nvnmo-1],1,&tn,&v);
			ovv[icdp][it] = 1.0/(v*v);
		}
		for (it=0,tn=ft; it<nt; ++it,tn+=dt) {
			intlin(ntnmo,tnmo,anis1,anis1[0],anis1[nanis1-1],1,&tn,
			       &oa1[icdp][it]);
		}
		for (it=0,tn=ft; it<nt; ++it,tn+=dt) {
			intlin(ntnmo,tnmo,anis2,anis2[0],anis2[nanis2-1],1,&tn,
			       &oa2[icdp][it]);
		}
		free1float(vnmo);
		free1float(tnmo);
		free1float(anis1);
		free1float(anis2);
	}

	/* sort (by insertion) sloth and anis functions by increasing cdp */
	for (jcdp=1; jcdp<ncdp; ++jcdp) {
		acdp = cdp[jcdp];
		aovv = ovv[jcdp];
		aoa1 = oa1[jcdp];
		aoa2 = oa2[jcdp];
		for (icdp=jcdp-1; icdp>=0 && cdp[icdp]>acdp; --icdp) {
			cdp[icdp+1] = cdp[icdp];
			ovv[icdp+1] = ovv[icdp];
			oa1[icdp+1] = oa1[icdp];
			oa2[icdp+1] = oa2[icdp];
		}
		cdp[icdp+1] = acdp;
		ovv[icdp+1] = aovv;
		oa1[icdp+1] = aoa1;
		oa2[icdp+1] = aoa2;
	}

	/* get other optional parameters */
	if (!getparfloat("smute",&smute)) smute = 1.5;
	if (!getparint("ixoffset",&ixoffset)) ixoffset=0; 
	  if (ixoffset==0) sy = 0.0;
	if (smute<=0.0) err("smute must be greater than 0.0");
	if (!getparint("lmute",&lmute)) lmute = 25;
	if (!getparint("sscale",&sscale)) sscale = 1;
	if (!getparint("invert",&invert)) invert = 0;
	if (!getparint("upward",&upward)) upward = 0;

	/* allocate workspace */
	ovvt = ealloc1float(nt);
	oa1t = ealloc1float(nt);
	oa2t = ealloc1float(nt);
	ttn = ealloc1float(nt);
	atn = ealloc1float(nt);
	qtn = ealloc1float(nt);
	tnt = ealloc1float(nt);
	at = ealloc1float(nt);
	qt = ealloc1float(nt);

	/* interpolate sloth and anis function for first trace */
	interpovv(nt,ncdp,cdp,ovv,oa1,oa2,(float)tr.cdp,ovvt,oa1t,oa2t);

	/* set old cdp and old offset for first trace */
	oldcdp = tr.cdp;
	oldoffset = tr.offset-1;

	warn("sy = %f",sy);

	/* loop over traces */
	do {
		/* if necessary, compute new sloth and anis function */
		if (tr.cdp!=oldcdp && ncdp>1) {
			interpovv(nt,ncdp,cdp,ovv,oa1,oa2,(float)tr.cdp,
				  ovvt,oa1t,oa2t);
			newsloth = 1;
		} else {
			newsloth = 0;
		}

		/* if sloth and anis function or offset has changed */
		if (newsloth || tr.offset!=oldoffset) {
			/* compute time t(tn) (normalized) */
			temp = ((float) tr.offset*(float) tr.offset + sy*sy)/(dt*dt);
			for (it=0,tn=ft/dt; it<nt; ++it,tn+=1.0) {
				tsq = temp*ovvt[it] + \
				      oa1t[it]*temp*temp / (1.0+oa2t[it]*temp);
				if (tsq<0.0)
					err("negative moveout; check anis1, "
					    "anis2, or suwind far-offset "
					    "traces");
				if ((1.0+oa2t[it]*temp)<=0.0)
					err("anis2 negative and too small; "
					    "check anis2, or suwind far-offset"
					    " traces");
				ttn[it] = sqrt (tn*tn + tsq);
				}
			/* compute inverse of stretch factor a(tn) */
			atn[0] = ttn[1]-ttn[0];
			for (it=1; it<nt; ++it)
				atn[it] = ttn[it]-ttn[it-1];
			
			/* determine index of first sample to survive mute */
			osmute = 1.0/smute;
			if( !upward ) {
				for (it=0; it<nt-1 && atn[it]<osmute; ++it)
					;
			} else {
				/* scan samples from bottom to top */
				for (it=nt-1; it>0 && atn[it]>=osmute; --it)
					;
			}
			itmute = it;

			/* if inverse NMO will be performed */
			if (invert) {
							
				/* compute tn(t) from t(tn) */
				yxtoxy(nt-itmute,1.0,ft/dt+itmute,&ttn[itmute],
					nt-itmute,1.0,ft/dt+itmute,
					ft/dt-nt,ft/dt+nt,&tnt[itmute]);
			
				/* adjust mute time */
				itmute = 1.0+ttn[itmute]-ft/dt;
				itmute = MIN(nt-2,itmute);
								
				/* compute a(t) */
				if (sscale) {
					for (it=itmute+1; it<nt; ++it)
						at[it] = tnt[it]-tnt[it-1];
					at[itmute] = at[itmute+1];
				}
			}
		}
		
		/* if forward (not inverse) nmo */
		if (!invert) {
	
			/* do nmo via 8-point sinc interpolation */
			ints8r(nt,1.0,ft/dt,tr.data,0.0,0.0,
				nt-itmute,&ttn[itmute],&qtn[itmute]);
			
			/* apply mute */
			for (it=0; it<itmute; ++it)
				qtn[it] = 0.0;
			
			/* apply linear ramp */
			for (it=itmute; it<itmute+lmute && it<nt; ++it)
				qtn[it] *= (float)(it-itmute+1)/(float)lmute;
			
			/* if specified, scale by the NMO stretch factor */
			if (sscale)
				for (it=itmute; it<nt; ++it)
					qtn[it] *= atn[it];
			
			/* copy NMO corrected trace to output trace */
			memcpy( (void *) tr.data,
					(const void *) qtn, nt*sizeof(float));
		
		/* else inverse nmo */
		} else {
	
			/* do inverse nmo via 8-point sinc interpolation */
			ints8r(nt,1.0,ft/dt,tr.data,0.0,0.0,
				nt-itmute,&tnt[itmute],&qt[itmute]);
			
			/* apply mute */
			for (it=0; it<itmute; ++it)
				qt[it] = 0.0;
			
			/* if specified, undo NMO stretch factor scaling */
			if (sscale)
				for (it=itmute; it<nt; ++it)
					qt[it] *= at[it];
			
			/* copy inverse NMO corrected trace to output trace */
			memcpy( (void *) tr.data,
					(const void *) qt,nt*sizeof(float));
		}

		/* write output trace */
		puttr(&tr);

		/* remember offset and cdp */
		oldoffset = tr.offset;
		oldcdp = tr.cdp;

	} while (gettr(&tr));

	return(CWP_Exit());
}
Beispiel #22
0
main(int argc, char **argv)
{
        float **filter;         /* filter arrays                        */
        float *tf;              /* times at which filters are centered  */
        int *itf;               /* ... as integers                      */
	int jmin;		/* index of first filter itf value	*/
	int jmax;		/* index of last filter itf value	*/
        int nfft;     	        /* fft sizes in each time gate          */
        int nfreq;     	        /* number of frequencies  	        */
        float **ftrace;         /* filtered sub-traces                  */
        int nfilter;            /* number of filters specified          */
      	float dt;               /* sample spacing                       */
        float tmin;             /* first time on traces                 */
        int nt;                 /* number of points on input trace      */
	float *data;
	FILE *infp=stdin, *outfp=stdout;
        
        /* Initialize */
        initargs(argc, argv);
        requestdoc(1);


        /* Get info from first trace */ 
	file2g(infp);
	file2g(outfp);
        if (!fgettr(infp,&tr))  err("can't get first trace");
        if (tr.trid && tr.trid != TREAL)
                err("input is not seismic data, trid=%d", tr.trid);
        nt = tr.ns;
        if (!getparfloat("dt", &dt))    dt = (float)tr.dt/1000000.0;
        if (!dt) err("dt field is zero and not getparred");
	tmin = tr.delrt/1000.0;


        /* Get number of filters and center times */
        if (!(nfilter = countparval("tf")))  MUSTGETPARFLOAT("tf", tf);
	if (countparname("f") != nfilter)
		err("must give one f 4-tuple for each"
		    " (%d) tf value", nfilter);
		
	/* Leave room for possibly missing filters at endpoints */
	tf = ealloc1float(nfilter+4);  /* never use ist2 or last 2 */
	itf = ealloc1int(nfilter+4);
        getparfloat("tf", tf+2); jmin = 2; jmax = nfilter + 1;
	{ register int j;
          for (j = jmin; j <= jmax; ++j)  itf[j] = NINT((tf[j] - tmin)/dt);
        }

	

        /* Make filters with scale for inverse transform */
	nfft = npfaro(nt, LOOKFAC * nt);
	if (nfft >= MIN(SU_NFLTS, PFA_MAX))
		err("Padded nt=%d -- too big", nfft);
	nfreq = nfft/2 + 1;
	filter = ealloc2float(nfreq, nfilter+4); /* never use 1st & last */
        { register int j;
          for (j = jmin; j <= jmax; ++j) {
                float *f = ealloc1float(4);

                if (getnparfloat(j-jmin+1, "f", f) != 4)
                        err("must give 4 corner frequencies in f=");
        	if (f[0] < 0.0 || f[0] > f[1] ||
				  f[1] >= f[2] || f[2] > f[3])
               		err("Filter #%d has bad frequencies", j - jmin + 1);
                makefilter(f, nfft, nfreq, dt, filter[j]);
          }
        }
	


	/* User may not have given a filter for tmin and/or tmax--	*/
	/* Extend array so can always assume these filters are present.	*/
	/* Note don't really use any of the extra storage in **filter!	*/
	if (itf[jmin] > 0) {
		filter[jmin-1] = filter[jmin]; 
		itf[jmin-1] = 0;
		--jmin;
	}
	if (itf[jmax] < nt - 1) {
		filter[jmax+1] = filter[jmax];
		itf[jmax+1] = nt - 1;
		++jmax;
	}
	
	
	/* Extend array so can always consider time points to be interior */
	itf[jmin-1] = 0;      /* now jmin - 1 is a valid index */
	itf[jmax+1] = nt - 1; /* now jmax + 1 is a valid index */


        /* Main loop over traces */
        ftrace = ealloc2float(nt, nfilter+4); /* never use 1st & last */
	data = ealloc1float(nt);
        do {
                register int i, j;
		
		/* Construct filtered sub-traces */
		for (j = jmin; j <= jmax; ++j) {			
			bzero(data, nt*FSIZE);
			for (i = itf[j-1]; i <= itf[j+1]; ++i)
				data[i] = tr.data[i];
                        bandpass(data,nt,nfft,nfreq,filter[j],ftrace[j]);
                }

               /* Compose filtered trace from sub-traces */
               for (j = jmin; j < jmax; ++j) {
	       		float fitfj;
                        for (fitfj = i = itf[j]; i <= itf[j+1]; ++i) {
                                float a = (i - fitfj)/(itf[j+1] - fitfj);
                                tr.data[i] = (1-a)*ftrace[j][i] +
                                                 a*ftrace[j+1][i];
			}
                }
                
                fputtr(outfp,&tr);
        } while (fgettr(infp,&tr));

        return EXIT_SUCCESS;
}
Beispiel #23
0
int
main(int argc, char **argv)
{
	char *key=NULL;		/* header key word from segy.h		*/
	char *type=NULL;	/* ... its type				*/
	int index;		/* ... its index			*/
	Value val;		/* ... its value			*/
	float fval;		/* ... its value cast to float		*/

	float *xshift=NULL;	/* array of key shift curve values	*/
	float *tshift=NULL;	/* ...		shift curve time values */

	int nxshift;		/* number of key shift values		*/
	int ntshift;		/* ...		shift time values 	*/

	int nxtshift;		/* number of shift values 		*/

	int it;			/* sample counter			*/
	int itr;		/* trace counter			*/
	int nt;			/* number of time samples 		*/
	int ntr=0;		/* number of traces			*/
	int *inshift=NULL;	/* array of (integer) time shift values
				   used for positioning shifted trace in
				   data[][]				*/

	float dt;		/* time sampling interval		*/

	cwp_String xfile="";	/* file containing positions by key	*/
	FILE *xfilep=NULL;	/* ... its file pointer			*/
	cwp_String tfile="";	/* file containing times	 	*/
	FILE *tfilep=NULL;	/* ... its file pointer			*/

	int verbose;		/* flag for printing information	*/
	char *tmpdir=NULL;	/* directory path for tmp files		*/
	cwp_Bool istmpdir=cwp_false;/* true for user-given path		*/

	int median;		/* flag for median filter		*/
	int nmed;		/* no. of traces to median filter	*/
	int nmix;		/* number of traces to mix over		*/
	int imix;		/* mixing counter			*/
	float *mix=NULL;	/* array of mix values			*/
	int sign;		/* flag for up/down shift		*/
	int shiftmin=0;		/* minimum time shift (in samples)	*/
	int shiftmax=0;		/* maximum time shift (in samples)	*/
	int ntdshift;		/* nt + shiftmax			*/

	size_t mixbytes;	/* size of mixing array			*/
	size_t databytes;	/* size of data array			*/
	size_t shiftbytes;	/* size of data array			*/
	float *temp=NULL;	/* temporary array			*/
	float *dtemp=NULL;	/* temporary array			*/
	float *stemp=NULL;	/* rwh median sort array		*/
	float **data=NULL;	/* mixing array 			*/
	int subtract;		/* flag for subtracting shifted data	*/

	/* rwh extra pointers for median sort */
	int first;		/* start pointer in ring buffer */
	int middle;		/* middle pointer in ring buffer */
	int last;		/* last pointer in ring buffer */
	int halfwidth;		/* mid point */
	int trcount;		/* pointer to current start trace number */
	float tmp;		/* temp storage for bubble sort */
	int rindex;		/* wrap around index for ring buffer */
	int jmix;		/* internal pointer for bubble sort */
	

	/* Initialize */
	initargs(argc, argv);
	requestdoc(1);

	/* Get parameters */
	if (!(getparstring("xfile",&xfile) && getparstring("tfile",&tfile))) {
		if (!(nxshift = countparval("xshift")))
			err("must give xshift= vector");
		if (!(ntshift = countparval("tshift")))
			err("must give tshift= vector");
		if (nxshift != ntshift)
			err("lengths of xshift, tshift must be the same");
		xshift = ealloc1float(nxshift);	getparfloat("xshift", xshift);
		tshift = ealloc1float(nxshift);	getparfloat("tshift", tshift);
	} else {
		MUSTGETPARINT("nshift",&nxtshift);
		nxshift = nxtshift;
		xshift = ealloc1float(nxtshift);
		tshift = ealloc1float(nxtshift);

		if((xfilep=fopen(xfile,"r"))==NULL)
			err("cannot open xfile=%s\n",xfile);
		if (fread(xshift,sizeof(float),nxtshift,xfilep)!=nxtshift)
			err("error reading xfile=%s\n",xfile);
		fclose(xfilep);

		if((tfilep=fopen(tfile,"r"))==NULL)
			err("cannot open tfile=%s\n",tfile);
		if (fread(tshift,sizeof(float),nxtshift,tfilep)!=nxtshift)
			err("error reading tfile=%s\n",tfile);
		fclose(tfilep);
	}
	if (!getparstring("key", &key))		key = "tracl";

	/* Get key type and index */
	type = hdtype(key);
	index = getindex(key);   

	/* Get mix weighting values values */
	if ((nmix = countparval("mix"))!=0) {
		mix = ealloc1float(nmix);
		getparfloat("mix",mix);
		/* rwh check nmix is odd */
		if (nmix%2==0) {
			err("number of mixing coefficients must be odd");
		}		
	} else {
		nmix = 5;
		mix = ealloc1float(nmix);
		mix[0] = VAL0;
		mix[1] = VAL1;
		mix[2] = VAL2;
		mix[3] = VAL3;
		mix[4] = VAL4;
	}
	
	/* Get remaning parameters */
	if (!getparint("median",&median))	median = 0;
	if (!getparint("nmed",&nmed) && median)	nmed = 5;
	if (!getparint("sign",&sign))		sign = -1;
	if (!getparint("subtract",&subtract))	subtract = 1;
	if (!getparint("verbose", &verbose))	verbose = 0;

	/* rwh check nmed is odd */
	if (median && nmed%2==0) {
		nmed=nmed+1;
		warn("increased nmed by 1 to ensure it is odd");
	}

	/* Look for user-supplied tmpdir */
	if (!getparstring("tmpdir",&tmpdir) &&
	    !(tmpdir = getenv("CWP_TMPDIR"))) tmpdir="";
	if (!STREQ(tmpdir, "") && access(tmpdir, WRITE_OK))
		err("you can't write in %s (or it doesn't exist)", tmpdir);

	/* rwh fix for median filter if median true set nmix=nmed */
	if (!median) {
		/* Divide mixing weights by number of traces to mix */
		for (imix = 0; imix < nmix; ++imix)
			mix[imix]=mix[imix]/((float) nmix);
	} else {
		nmix=nmed;
	}

	/* Get info from first trace */
	if (!gettr(&tr)) err("can't read first trace");
	if (!tr.dt) err("dt header field must be set");
	dt   = ((double) tr.dt)/1000000.0;
	nt = (int) tr.ns;
	databytes = FSIZE*nt;

	/* Tempfiles */
	if (STREQ(tmpdir,"")) {
		tracefp = etmpfile();
		headerfp = etmpfile();
		if (verbose) warn("using tmpfile() call");
	} else { /* user-supplied tmpdir */
		char directory[BUFSIZ];
		strcpy(directory, tmpdir);
		strcpy(tracefile, temporary_filename(directory));
		strcpy(headerfile, temporary_filename(directory));
		/* Trap signals so can remove temp files */
		signal(SIGINT,  (void (*) (int)) closefiles);
		signal(SIGQUIT, (void (*) (int)) closefiles);
		signal(SIGHUP,  (void (*) (int)) closefiles);
		signal(SIGTERM, (void (*) (int)) closefiles);
		tracefp = efopen(tracefile, "w+");
		headerfp = efopen(headerfile, "w+");
      		istmpdir=cwp_true;		
		if (verbose) warn("putting temporary files in %s", directory);
	}

	/* Read headers and data while getting a count */
	do {
		++ntr;
		efwrite(&tr, 1, HDRBYTES, headerfp);
		efwrite(tr.data, 1, databytes, tracefp);   

	} while (gettr(&tr));
	rewind(headerfp);
	rewind(tracefp);
	
	/* Allocate space for inshift vector */
	inshift = ealloc1int(ntr);

	/* Loop over headers */
 	for (itr=0; itr<ntr; ++itr) {
		float tmin=tr.delrt/1000.0;
		float t;

		/* Read header values */
		efread(&tr, 1, HDRBYTES, headerfp);

		/* Get value of key and convert to float */
		gethval(&tr, index, &val);
		fval = vtof(type,val);

		/* Linearly interpolate between (xshift,tshift) values */
		intlin(nxshift,xshift,tshift,tmin,tshift[nxshift-1],1,&fval,&t);
		
		/* allow for fractional shifts -> requires interpolation */ 
		inshift[itr] = NINT((t - tmin)/dt);
		
		/* Find minimum and maximum shifts */
		if (itr==0) {
			 shiftmax=inshift[0];
			 shiftmin=inshift[0];
		} else {
			shiftmax = MAX(inshift[itr],shiftmax);
			shiftmin = MIN(inshift[itr],shiftmin);
		}
	}
	rewind(headerfp);
	rewind(tracefp);

	if (verbose) {
		for (itr=0;itr<ntr;itr++)
			warn("inshift[%d]=%d",itr,inshift[itr]);
	}

	/* Compute databytes per trace and bytes in mixing panel */
	ntdshift = nt + shiftmax;
	shiftbytes = FSIZE*ntdshift;
	mixbytes = shiftbytes*nmix;
	if (verbose) {
		warn("nt=%d  shiftmax=%d  shiftmin=%d",nt,shiftmax,shiftmin);
		warn("ntdshift=%d  shiftbytes=%d  mixbytes=%d",
						ntdshift,shiftbytes,mixbytes);
	}
	
	/* Allocate space and zero  data array */
	data = ealloc2float(ntdshift,nmix);
	temp = ealloc1float(ntdshift);
	dtemp = ealloc1float(nt);
	memset( (void *) data[0], 0, mixbytes);

	/* rwh array for out of place bubble sort (so we do not corrupt order in ring buffer */ 
	stemp = ealloc1float(nmix);

	/* rwh first preload ring buffer symmetrically (now you know why nmix must be odd) */
	trcount=-1;
	halfwidth=(nmix-1)/2+1;
	first = 0;
	last  = nmix-1;
	middle = (nmix-1)/2;

	for (itr=0; itr<halfwidth; itr++) {
		efread(tr.data, 1, databytes, tracefp);
		trcount++;
		for(it=0; it<nt; ++it) {
			/* sign to account for positive or negative shift */
			/* tr.data needs to be interpolated for non-integer shifts */
			data[middle-itr][it + shiftmax + sign*inshift[itr]] = tr.data[it];
			data[middle+itr][it + shiftmax + sign*inshift[itr]] = tr.data[it];
		}
	}
	
	/* Loop over traces performing median filtering  */
 	for (itr=0; itr<ntr; ++itr) {

		/* paste header and data on output trace */
		efread(&tr, 1, HDRBYTES, headerfp);

		/* Zero out temp and dtemp */
		memset((void *) temp, 0, shiftbytes);
		memset((void *) dtemp, 0, databytes);

		/* Loop over time samples */
		for (it=0; it<nt; ++it) {

			/* Weighted moving average (mix) ? */
			if (!median) {
				for(imix=0; imix<nmix; ++imix) {
					temp[it] += data[imix][it] * mix[imix];
				}
			} else {
			
			/* inlcude median stack */
			/* rwh do bubble sort and choose median value */
				for(imix=0; imix<nmix; ++imix) {
					stemp[imix]=data[imix][it];
				}
				for (imix=0; imix<nmix-1; imix++) {
					for (jmix=0; jmix<nmix-1-imix; jmix++) {
						if (stemp[jmix+1] < stemp[jmix]) {
							tmp = stemp[jmix];
							stemp[jmix] = stemp[jmix+1];
							stemp[jmix+1] = tmp;
						}
					}
				}
				temp[it] = stemp[middle];
			}

			/* shift back mixed data and put into dtemp */
			if (subtract) {
				if ((it - shiftmax - sign*inshift[itr])>=0)
					dtemp[it - shiftmax - sign*inshift[itr]] = data[middle][it]-temp[it];
			} else {
				if ((it - shiftmax)>=0)
				dtemp[it - shiftmax - sign*inshift[itr]] = temp[it];
			}
		}
		memcpy((void *) tr.data,(const void *) dtemp,databytes);
			
		/* Bump rows of data[][] over by 1 to free first row for next tr.data */
		for (imix=nmix-1; 0<imix; --imix)
			memcpy((void *) data[imix],(const void *) data[imix-1],shiftbytes);
			/*for (it=0; it<nt; ++it)
				data[imix][it] = data[imix-1][it];*/

		/* Write output trace */
		tr.ns = nt;
		puttr(&tr);

		/* read next trace into buffer */
		if (trcount < ntr) {
			efread(tr.data, 1, databytes, tracefp);
			trcount++;

			/* read tr.data into first row of mixing array */
			/* WMH: changed ntdshift to nt */
			for(it=0; it<nt; ++it) {
				/* sign to account for positive or negative shift */
				/* tr.data needs to be interpolated for non-integer shifts */
				data[0][it + shiftmax + sign*inshift[trcount]] = tr.data[it];
			}
		} else {
			rindex=2*(trcount-ntr);
			memcpy((void *) data[0],(const void *) data[rindex],shiftbytes);
			trcount++;
		}

	}

	if (verbose && subtract)	warn("filtered data subtracted from input");

	/* Clean up */
	efclose(headerfp);
	if (istmpdir) eremove(headerfile);
	efclose(tracefp);
	if (istmpdir) eremove(tracefile);

	return(CWP_Exit());
}
Beispiel #24
0
int
main(int argc, char **argv)
{
	int nt;		/* number of time samples per trace */
	float dt;	/* time sampling interval */
	float ft;	/* time of first sample */
	int it;		/* time sample index */
	int cdpmin;	/* minimum cdp to process */
	int cdpmax;	/* maximum cdp to process */
	float dx;	/* cdp sampling interval */
	int nx;		/* number of cdps to process */
	int nxfft;	/* number of cdps after zero padding for fft */
	int nxpad;	/* minimum number of cdps for zero padding */
	int ix;		/* cdp index, starting with ix=0 */
	int noffmix;	/* number of offsets to mix */
	float *tdmo;	/* times at which rms velocities are specified */
	float *vdmo;	/* rms velocities at times specified in tdmo */
	float sdmo;	/* DMO stretch factor */
	int ntdmo;	/* number tnmo values specified */
	int itdmo;	/* index into tnmo array */
	int nvdmo;	/* number vnmo values specified */
	float fmax;	/* maximum frequency */
	float *vrms;	/* uniformly sampled vrms(t) */
	float **p;	/* traces for one offset - common-offset gather */
	float **q;	/* DMO-corrected and mixed traces to be output */
	float offset;	/* source-receiver offset of current trace */
	float oldoffset;/* offset of previous trace */
	int noff;	/* number of offsets processed in current mix */
	int ntrace;	/* number of traces processed in current mix */
	int itrace;	/* trace index */
	int gottrace;	/* non-zero if an input trace was read */
	int done;	/* non-zero if done */
	int verbose;	/* =1 for diagnostic print */
	char *tmpdir;	/* directory path for tmp files	*/
	cwp_Bool istmpdir=cwp_false;/* true for user given path */

	/* hook up getpar */
	initargs(argc, argv);
	requestdoc(1);

	/* get information from the first header */
	if (!gettr(&tr)) err("can't get first trace");
	nt = tr.ns;
	dt = ((double) tr.dt)/1000000.0;
	ft = tr.delrt/1000.0;
	offset = tr.offset;

	/* get parameters */
	if (!getparint("cdpmin",&cdpmin)) err("must specify cdpmin");
	if (!getparint("cdpmax",&cdpmax)) err("must specify cdpmax");
	if (cdpmin>cdpmax) err("cdpmin must not be greater than cdpmax");
	if (!getparfloat("dxcdp",&dx)) err("must specify dxcdp");
	if (!getparint("noffmix",&noffmix)) err("must specify noffmix");
	ntdmo = countparval("tdmo");
	if (ntdmo==0) ntdmo = 1;
	tdmo = ealloc1float(ntdmo);
	if (!getparfloat("tdmo",tdmo)) tdmo[0] = 0.0;
	nvdmo = countparval("vdmo");
	if (nvdmo==0) nvdmo = 1;
	if (nvdmo!=ntdmo) err("number of tdmo and vdmo must be equal");
	vdmo = ealloc1float(nvdmo);
	if (!getparfloat("vdmo",vdmo)) vdmo[0] = 1500.0;
	for (itdmo=1; itdmo<ntdmo; ++itdmo)
		if (tdmo[itdmo]<=tdmo[itdmo-1])
			err("tdmo must increase monotonically");
	if (!getparfloat("sdmo",&sdmo)) sdmo = 1.0;
	if (!getparfloat("fmax",&fmax)) fmax = 0.5/dt;
	if (!getparint("verbose",&verbose)) verbose=0;
	
	/* Look for user-supplied tmpdir */
	if (!getparstring("tmpdir",&tmpdir) &&
	    !(tmpdir = getenv("CWP_TMPDIR"))) tmpdir="";
	if (!STREQ(tmpdir, "") && access(tmpdir, WRITE_OK))
		err("you can't write in %s (or it doesn't exist)", tmpdir);
	
	/* make uniformly sampled rms velocity function of time */
	vrms = ealloc1float(nt);
	mkvrms(ntdmo,tdmo,vdmo,nt,dt,ft,vrms);
	
	/* determine number of cdps to process */
	nx = cdpmax-cdpmin+1;
	
	/* allocate and zero common-offset gather p(t,x) */
	nxpad = 0.5*ABS(offset/dx);
	nxfft = npfar(nx+nxpad);
	p = ealloc2float(nt,nxfft+2);
	for (ix=0; ix<nxfft; ++ix)
		for (it=0; it<nt; ++it)
			p[ix][it] = 0.0;

	/* allocate and zero offset mix accumulator q(t,x) */
	q = ealloc2float(nt,nx);
	for (ix=0; ix<nx; ++ix)
		for (it=0; it<nt; ++it)
			q[ix][it] = 0.0;
		
	/* open temporary file for headers */
	if (STREQ(tmpdir,"")) {
		headerfp = etmpfile();
		if (verbose) warn("using tmpfile() call");
	} else { /* user-supplied tmpdir */
		char directory[BUFSIZ];
		strcpy(directory, tmpdir);
		strcpy(headerfile, temporary_filename(directory));
		/* Trap signals so can remove temp files */
		signal(SIGINT,  (void (*) (int)) closefiles);
		signal(SIGQUIT, (void (*) (int)) closefiles);
		signal(SIGHUP,  (void (*) (int)) closefiles);
		signal(SIGTERM, (void (*) (int)) closefiles);
		headerfp = efopen(headerfile, "w+");
      		istmpdir=cwp_true;		
		if (verbose)
			warn("putting temporary header file in %s", directory);
	}

	/* initialize */
	oldoffset = offset;
	gottrace = 1;
	done = 0;
	ntrace = 0;
	noff = 0;

	/* loop over traces */
	do {
		/* if got a trace, determine offset */
		if (gottrace) offset = tr.offset;
		
		/* if an offset is complete */
		if ((gottrace && offset!=oldoffset) || !gottrace) {
		
			/* do dmo for old common-offset gather */
			dmooff(oldoffset,fmax,sdmo,nx,dx,nt,dt,ft,vrms,p);
			
			/* add dmo-corrected traces to mix */
			for (ix=0; ix<nx; ++ix)
				for (it=0; it<nt; ++it)
					q[ix][it] += p[ix][it];
			
			/* count offsets in mix */
			noff++;
							
			/* free space for old common-offset gather */
			free2float(p);
			
			/* if beginning a new offset */
			if (offset!=oldoffset) {
				
				/* allocate space for new offset */
				nxpad = 0.5*ABS(offset/dx);
				nxfft = npfar(nx+nxpad);
				p = ealloc2float(nt,nxfft+2);
				for (ix=0; ix<nxfft; ++ix)
					for (it=0; it<nt; ++it)
						p[ix][it] = 0.0;
			}
		}
		
		/* if a mix of offsets is complete */
		if (noff==noffmix || !gottrace) {
			
			/* rewind trace header file */
			erewind(headerfp);
			
			/* loop over all output traces */
			for (itrace=0; itrace<ntrace; ++itrace) {
			
				/* read trace header and determine cdp index */
				efread(&tro,HDRBYTES,1,headerfp);
				
				/* get dmo-corrected data */
				memcpy( (void *) tro.data,
					(const void *)	q[tro.cdp-cdpmin],
					nt*sizeof(float));
				
				/* write output trace */
				puttr(&tro);
			}
			
			/* report */
			if (verbose) 
				fprintf(stderr,"\tCompleted mix of "
					"%d offsets with %d traces\n",
					noff,ntrace);
			
			/* if no more traces, break */
			if (!gottrace) break;
			
			/* rewind trace header file */
			erewind(headerfp);

			/* reset number of offsets and traces in mix */
			noff = 0;
			ntrace = 0;
			
			/* zero offset mix accumulator */
			for (ix=0; ix<nx; ++ix)
				for (it=0; it<nt; ++it)
					q[ix][it] = 0.0;
		}
			
		/* if cdp is within range to process */
		if (tr.cdp>=cdpmin && tr.cdp<=cdpmax) {
	
			/* save trace header and update number of traces */
			efwrite(&tr,HDRBYTES,1,headerfp);
			ntrace++;

			/* remember offset */
			oldoffset = offset;

			/* get trace samples */
			memcpy( (void *) p[tr.cdp-cdpmin],
				   (const void *) tr.data, nt*sizeof(float));
		}

		/* get next trace (if there is one) */
		if (!gettr(&tr)) gottrace = 0;
		
	} while (!done);

	/* clean up */
	efclose(headerfp);
	if (istmpdir) eremove(headerfile);
	return(CWP_Exit());
}