Esempio n. 1
0
int
main(int argc, char **argv)
{
	FILE *fp;
	int fd,j=0,verbose;

	initargs(argc,argv);
	requestdoc(1);

	if(!getparint("fd",&fd)) fd=-1;
	if(!getparint("verbose",&verbose)) verbose=0; 
	warn("File descriptor passed to suget = %d",fd);
	if( (fp = (FILE *) fdopen(fd,"r"))==NULL) err("Bad file descriptor");
	warn("About to read first trace");
	if(!fgettr(fp,&tr)) err("Can't get first trace");

	do{
		if(verbose>0){
			warn("read trace %d",j);
			j++;
		}

		puttr(&tr);

	} while(fgettr(fp,&tr));

	return(CWP_Exit());
}
Esempio n. 2
0
int
main(int argc, char **argv)
{

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


	while (gettr(&tr)) {
		tr.f1 = 0.0;
		tr.d1 = 0.0;
		tr.f2 = 0.0;
		tr.d2 = 0.0;
		tr.ungpow = 0.0;
		tr.unscale = 0.0;
		tr.ntr = 0;
		tr.mark = 0;

		puttr(&tr);
	}


	return(CWP_Exit());
}
Esempio n. 3
0
int
main(int argc, char **argv)
{
	int ns;		/* samples on output traces	*/


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


        /* Get info from first trace */ 
        if (!fvgettr(stdin, &tr))  err("can't get first trace"); 
        if (!getparint("ns", &ns)) ns = tr.ns;
        checkpars();


	/* Loop over the traces */
	do {
		int nt = tr.ns;
				
		if (nt < ns) /* pad with zeros */
                	memset((void *)(tr.data + nt), 0, (ns-nt)*FSIZE);
		tr.ns = ns;
		puttr(&tr);
	} while (fvgettr(stdin, &tr));
	
	return(CWP_Exit());
}
Esempio n. 4
0
main(int argc, char **argv)
{
	String key;	/* header key word from segy.h		*/
	String type;	/* ... its type				*/
	int index;	/* ... its index in hdr.h		*/
	Value val;	/* ... its value			*/
	void absval(String type, Value *valp);


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


	/* Get key parameter */
	if (!getparstring("key", &key))	key = KEY;

	file2g(stdin);
	file2g(stdout);

	type = hdtype(key);
	index = getindex(key);

	while (gettr(&tr)) {
		gethval(&tr, index, &val);
		absval(type, &val);
		puthval(&tr, index, &val);
		puttr(&tr);
	}


	return EXIT_SUCCESS;
}
Esempio n. 5
0
int
main(int argc, char **argv)
{

	int i;			/* counter			*/
	int itr=0;		/* trace counter		*/
	int verbose;		/* =0 silent,  =1 chatty	*/
	int interp;		/* =1 interpolate to get NaN	*/
				/* and Inf replacement values	*/
			
	float value;		/* value to set NaN and Infs to */

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

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

	/* Get parameters */
	if(!getparint("verbose",&verbose))	verbose = 1;
	if(!getparint("interp",&interp))	interp = 0;
	if(!getparfloat("value",&value))	value = 0.0;
        checkpars();

	/* Loop over traces */
	do{
		++itr;
      		for(i=0; i<tr.ns; ++i){
		    if(!isfinite(tr.data[i])) {
		       if (verbose)
	                warn("found NaN trace = %d  sample = %d", itr, i);

			if (interp) { /* interpolate nearest neighbors */
				      /* for NaN replacement value     */
				if (i==0 && isfinite(tr.data[i+1])) { 
					tr.data[i]=tr.data[i+1];
				} else if(i==tr.ns-1 && isfinite(tr.data[i-2])) {
					tr.data[i]= tr.data[i-2];
				} else if( isfinite(tr.data[i-1]) &&
						isfinite(tr.data[i+1]) ) {
					tr.data[i]=(tr.data[i-1]+tr.data[i+1])/2.0;
				}
			}
				
			/* use user defined NaNs replacement value */
            	       	tr.data[i] = value;
			}
		    }

      		puttr(&tr);
	} while(gettr(&tr));

	return(CWP_Exit());
}
Esempio n. 6
0
int
main(int argc, char **argv)
{
	int nt;				/* number of samples on input */
	int ntout;			/* number of samples on output */
	int it;				/* counter */
	int istart;			/* beginning sample */
	int izero;			/* - istart */
	int norm;			/* user defined normalization value */
	int sym;			/* symmetric plot? */
	float scale;			/* scale factor computed from norm */
	float *temp=NULL;		/* temporary array */
	float dt;			/* time sampling interval (sec) */

	/* 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 (!getparint("ntout",&ntout)) ntout=101;
	if (!getparint("norm",&norm)) norm = 1;
	if (!getparint("sym",&sym)) sym = 1;
        checkpars();
	
	/* allocate workspace */
	temp = ealloc1float(ntout);
	
	/* index of first sample */
	if (sym == 0) istart = 0;
	else istart = -(ntout-1)/2;

	/* index of sample at time zero */
	izero = -istart;
	
	/* loop over traces */
	do {
		xcor(nt,0,tr.data,nt,0,tr.data,ntout,istart,temp);
		if (norm) {
			scale = 1.0/(temp[izero]==0.0?1.0:temp[izero]);
			for (it=0; it<ntout; ++it)  temp[it] *= scale;
		}
		memcpy((void *) tr.data, (const void *) temp, ntout*FSIZE);
		tr.ns = ntout;
		tr.f1 = -dt*ntout/2.0;
		tr.delrt = 0;
		puttr(&tr);
	} while(gettr(&tr));

	return(CWP_Exit());
}
Esempio n. 7
0
main(int argc, char **argv)
{
	initargs(argc, argv);
	requestdoc(1);

 	while (gettr(&tr)) {
 		tr.offset = abs(tr.offset);
 		puttr(&tr);
 	}

	return EXIT_SUCCESS;
}
Esempio n. 8
0
int
main(int argc, char **argv)
{
	int nt;				/* number of time samples	*/
	int ntr;			/* number of traces		*/
	int itr;			/* trace counter		*/
	int nspk;			/* number of spikes		*/
	int it1;			/* time of 1st spike		*/
	int ix1;			/* position of 1st spike	*/
	int it2;			/* time of 2nd spike		*/
	int ix2;			/* position of 2nd spike	*/
	int ix3;			/* position of 3rd spike	*/
	int it3;			/* time of 3rd spike		*/
	int ix4;			/* position of 4th spike	*/
	int it4;			/* time of 4th spike		*/
	float dt;			/* time sampling interval	*/
	float offset;			/* offset			*/


	/* Initialize */
	initargs(argc, argv);
	requestdoc(0); /* stdin not used */


	nt = 64;	getparint("nt", &nt);
	CHECK_NT("nt",nt);				tr.ns = nt;
	ntr = 32;	getparint("ntr", &ntr);
	dt = 0.004;	getparfloat("dt", &dt);		tr.dt = dt*1000000;
	offset = 400;	getparfloat("offset", &offset);	tr.offset = offset;
	nspk = 4;	getparint("nspk", &nspk);
	ix1 = ntr/4;	getparint("ix1", &ix1); 
	it1 = nt/4;	getparint("it1", &it1);
	ix2 = ntr/4;	getparint("ix2", &ix2);
	it2 = 3*nt/4;	getparint("it2", &it2);
	ix3 = 3*ntr/4;	getparint("ix3", &ix3);
	it3 = nt/4;	getparint("it3", &it3);
	ix4 = 3*ntr/4;	getparint("ix4", &ix4);
	it4 = 3*nt/4;	getparint("it4", &it4);

	for (itr = 0; itr < ntr; itr++) {
		memset( (void *) tr.data, 0, nt * FSIZE);
		if (itr == ix1-1) tr.data[it1-1] = 1.0;  
		if (nspk > 1 && itr == ix2-1) tr.data[it2-1] = 1.0;
		if (nspk > 2 && itr == ix3-1) tr.data[it3-1] = 1.0;
		if (nspk > 3 && itr == ix4-1) tr.data[it4-1] = 1.0;
		tr.tracl = itr + 1;
		puttr(&tr);
	}


	return(CWP_Exit());
}
Esempio n. 9
0
int main( int argc, char *argv[] )
{
	/* Segy data constans */
	int nt;                 /* number of time samples               */
        int ntr=0;              /* number of traces                     */
	
	float *filter;
	int fnl,fnr;
	int fnp;
	int fld;
	int fm;
        float dt;               /* sample interval in secs              */
	float prw;		/* pre-withening */
	
	initargs(argc, argv);
   	requestdoc(1);
	
        /* get information from the first header */
        if (!gettr(&tr)) err("can't get first trace");
        nt = tr.ns;

        if (!getparfloat("dt", &dt)) dt = ((double) tr.dt)/1000000.0;
        if (!dt) {
                dt = .002;
                warn("dt not set, assumed to be .002");
        }
	
	if(!getparint ("fnl", &fnl)) fnl=15;
	fnr=fnl;
	if(!getparint ("fnp", &fnp)) fnp=fnr+fnl+fnr/2;
	if(!getparfloat ("prw", &prw)) prw=1.0;
		
	if(fnl!=0) {
		fld=0; fm=0; fnr=fnl;
		filter = ealloc1float(fnp);
		SG_smoothing_filter(fnp,fnl,fnr,fld,fm,filter); 
/*		rwa_smoothing_filter(1,fnl,fnr,filter); */ 
	} else {
		filter= NULL;
	}
	
	do {
		do_minphdec(tr.data,nt,filter,fnl,fnr,prw);
		
		tr.ns=nt;
		ntr++;		
		puttr(&tr);
	} while(gettr(&tr));
	
   return EXIT_SUCCESS;
}
Esempio n. 10
0
int
main(int argc, char **argv)
{
	int nt,nshot,noff;
        int ishot,ioff,it;
        float dt,dshot,doff,sx,gx,offset,cmp;

	/* Initialize */
	initargs(argc, argv);
	requestdoc(0); /* stdin not used */

	nt = 100;	getparint("nt", &nt);
	CHECK_NT("nt",nt);				tr.ns = nt;
	nshot = 10;	getparint("nshot", &nshot);
	noff  = 24;	getparint("noff", &noff);
	dt = 0.004;	getparfloat("dt", &dt);		tr.dt = dt*1000000;
	dshot = 10;	getparfloat("dshot", &dshot);
	doff = 20;	getparfloat("doff", &doff);

	for (ishot = 0; ishot < nshot; ishot++) {
          sx = ishot*dshot;
	  for (ioff = 0; ioff < noff; ioff++) {
                offset = (ioff+1)*doff;
                gx = sx + offset; 
                cmp = (sx + gx)/2.;
		memset( (void *) tr.data, 0, nt * FSIZE);
                for (it = 0; it < nt/4; it++) {
		  tr.data[it] = sx;  
                }
                for (it = nt/4; it < nt/2; it++) {
		  tr.data[it] = gx;  
                }
                for (it = nt/2; it < 3*nt/4; it++) {
		  tr.data[it] = offset;  
                }
                for (it = 3*nt/4; it < nt; it++) {
		  tr.data[it] = cmp;  
                }
		tr.sx = sx;
		tr.gx = gx;
		tr.offset = offset;
		tr.cdp = cmp;
		tr.tracl = ishot*nshot + ioff + 1;
		puttr(&tr);
          }
        }


	return(CWP_Exit());
}
Esempio n. 11
0
int
main(int argc, char **argv)
{
	int j,nt,flag,ntout;
	float *buf,*ttn,dt,dtout=0.0,tmin,tmax;

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

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

	if (!getparfloat("tmin", &tmin)) tmin=0.1*nt*dt;
	if (!getparint("flag", &flag)) flag=1;
	if(flag==1) {
		dtout=tmin*2.*dt;
		tmax=nt*dt;
		ntout=1+tmax*tmax/dtout; CHECK_NT("ntout",ntout);
		ttn=ealloc1float(ntout);
		for(j=0;j<ntout;j++) ttn[j]=sqrt(j*dtout);
	}else{
		if (!getparfloat("dt", &dt)) dtout=0.004;
		ntout=1+sqrt(nt*dt)/dtout; CHECK_NT("ntout",ntout);
		ttn=ealloc1float(ntout);
		for(j=0;j<ntout;j++) ttn[j]=j*j*dtout*dtout;
	}
	buf = ealloc1float(nt);

	fprintf(stderr,"sutsq: ntin=%d dtin=%f ntout=%d dtout=%f\n",
		nt,dt,ntout,dtout);

	/* Main loop over traces */
	do {
		for(j=0;j<nt;j++) buf[j]=tr.data[j];
		tr.ns = ntout;
		tr.dt = dtout*1000000.;			
		ints8r(nt,dt,0.,buf,0.0,0.0,
			ntout,ttn,tr.data);
		puttr(&tr);
	} while (gettr(&tr));
	
	return(CWP_Exit());
}
Esempio n. 12
0
main(int argc, char **argv)
{
	String key;
	String type;
	int index;
	double a, c, b, d, i, j;
	int itr = 0;
	Value val;
	FILE *infp=stdin, *outfp=stdout; 


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


	/* Get parameters */
	if (!getparstring("key", &key))	 key = "cdp";
	if (!getpardouble("a"  , &a))	 a = 0;
	if (!getpardouble("b"  , &b))	 b = 0;
	if (!getpardouble("c"  , &c))	 c = 0;
	if (!getpardouble("d"  , &d))	 d = 0;
	if (!getpardouble("j"  , &j))	 j = ULONG_MAX;

	type = hdtype(key);
	index = getindex(key);

	file2g(infp);
	file2g(outfp);

	while (gettr(&tr)) {
		i = (double) itr++ + d;
		setval(type, &val, a, b, c, i, j);
		puthval(&tr, index, &val);
		puttr(&tr);
	}


	return EXIT_SUCCESS;
}
Esempio n. 13
0
int
main(int argc, char **argv)
{
	int itmin;		/* first sample to zero out		*/
	int itmax;		/* last sample to zero out	 	*/
	float value;		/* value to set within window		*/
	int nt;			/* time samples per trace in input data	*/

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

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

	/* Get params from user */
	MUSTGETPARINT("itmax", &itmax);
	if (!getparint("itmin", &itmin))	itmin = 0;
	if (!getparfloat("value", &value))	value = 0.0;

	/* Error checking */
	if (itmax > nt)    err("itmax = %d, must be < nt", itmax);
	if (itmin < 0)     err("itmin = %d, must not be negative", itmin);
	if (itmax < itmin) err("itmax < itmin, not allowed");

	/* Main loop over traces */
	do { 
		register int i;
		for (i = itmin; i <= itmax; ++i)  tr.data[i] = value;
		
		puttr(&tr);
	} while(gettr(&tr));


	return(CWP_Exit());
}
Esempio n. 14
0
int
main(int argc, char **argv)
{
	int nt;		/* number of samples on output trace	*/
	float dt;	/* sample rate on outpu trace		*/
	int min;		/* min time shift (samples) */
	int max;		/* max time shift (samples) */
	unsigned int seed;      /* random number seed */
	int it;			/* time sample counter */
	int itr;		/* trace counter */
	int its=0;		/* local shift in time samples */
	int sits;		/* sign of local shift */
	int fldr;		/* fldr use flag		*/
	int ishot;		/* shot counter (based on tr.fldr) */
	int ishotold;
	int pon;		/* flag for pos or neg shift */


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

	/* Get information from first trace */
	if (!gettr(&tr)) err("can't get first trace");
	nt   = tr.ns;
	ishotold = tr.fldr;
	dt   = ((double) tr.dt)/1000000.0;
	
	/* Get parameters */
	if (!getparint("min", &min))    min = 1;
	if (!getparint("max", &max))    max = 1;
	if (!getparint("pon", &pon))    pon = 1;
	if (!getparint("fldr", &fldr))  fldr = 0;

	if (min>max) err("min>max... exit");

	/* Set seed */
	if (!getparuint("seed", &seed)) { /* if not supplied, use clock */
		if (-1 == (seed = (unsigned int) time((time_t *) NULL))) {
			err("time() failed to set seed");
		}
	}
	sranuni(seed);

	/* Loop on traces */	
	itr = 1;
	do {

		/* get fldr (shot) number */
		ishot = tr.fldr;

		if (itr==1) {
			/* initial shift for shot 1 
			   (used if fldr==1) */	
	       		its = min + (max-min)*franuni();
			if (pon==1) {
				/* include random sign to shift */
				sits = SGN(franuni()-0.5);
				its = its * sits;
			}
		}
		
		if (fldr==0) {
			/* each trace gets random shift */
	       		its = min + (max-min)*franuni();
			if (pon==1) {
				/* include random sign to shift */
				sits = SGN(franuni()-0.5);
				its = its * sits;
			}
		}

		if (fldr==1 && ishot!=ishotold) {
			/* new shot needs new shift */
	       		its = min + (max-min)*franuni();
			if (pon==1) {
				/* include random sign to shift */
				sits = SGN(franuni()-0.5);
				its = its * sits;
			}
			ishotold = ishot;
		}

		/* apply shift and output trace */
		if (its <= 0 ) {
			/* loop over output times */
			for (it=0; it<nt-its; ++it) {
				tr.data[it] = tr.data[it-its];	
			}
		} else {
			/* loop over output times */
			for (it=nt; it>its; --it) {
				tr.data[it] = tr.data[it-its];	
			}

		}
		puttr(&tr);

		itr += 1;

	} while (gettr(&tr));


	return(CWP_Exit());
}
Esempio n. 15
0
main(int argc, char **argv)
{
	int nt;			/* number of points on trace		*/
	float dt;		/* time sample interval (sec)		*/
	float *wiener;		/* Wiener error filter coefficients	*/
	float pnoise;		/* pef additive noise level		*/
	float minlag;		/* start of error filter (sec)		*/
	int iminlag;		/* ... in samples			*/
	float maxlag;		/* end of error filter (sec)		*/
	int imaxlag;		/* ... in samples			*/
	int nlag;		/* length of error filter in samples	*/
	int ncorr;		/* length of corr window in samples	*/
	float *crosscorr;	/* right hand side of Wiener eqs	*/
	float *autocorr;	/* vector of autocorrelations		*/
	float *spiker;		/* spiking decon filter			*/
	float mincorr;		/* start time of correlation window	*/
	int imincorr;		/* .. in samples			*/
	float maxcorr;		/* end time of correlation window	*/
	int imaxcorr;		/* .. in samples			*/
	int showspiker;		/* flag to display spiking filter	*/
	int showwiener;		/* flag to display pred. error filter	*/



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


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


	/* Get parameters */
	if (!getparint("showwiener",  &showwiener))	showwiener = 0;
	if (!getparint("showspiker",  &showspiker))	showspiker = 0;

	if (!getparfloat("pnoise",  &pnoise))	pnoise = PNOISE;

	if (getparfloat("minlag", &minlag))	iminlag = NINT(minlag/dt);
	else					iminlag = 1;
	if (iminlag < 1) err("minlag=%g too small", minlag);

	if (getparfloat("maxlag", &maxlag))	imaxlag = NINT(maxlag/dt);
	else					imaxlag = NINT(0.05 * nt);
	if (imaxlag >= nt) err("maxlag=%g too large", maxlag);
	
	if (iminlag >= imaxlag)
		err("minlag=%g, maxlag=%g", minlag, maxlag);
	
	if (getparfloat("mincorr", &mincorr))	imincorr = NINT(mincorr/dt);
	else					imincorr = 0;
	if (imincorr < 0) err("mincorr=%g too small", mincorr);
	
	if (getparfloat("maxcorr", &maxcorr))	imaxcorr = NINT(maxcorr/dt);
	else					imaxcorr = nt-1;
	if (imaxcorr >= nt) err("maxcorr=%g too large", maxcorr);

	if (imincorr >= imaxcorr)
		err("mincorr=%g, maxcorr=%g", mincorr, maxcorr);
	
	nlag  = imaxlag - iminlag + 1;
	ncorr = imaxcorr - imincorr + 1;


	/* Allocate memory */
	wiener	 = ealloc1float(nlag);
	spiker	 = ealloc1float(nlag);
	autocorr = ealloc1float(imaxlag);


	/* Set pointer to "cross" correlation */
	crosscorr = autocorr + iminlag;



	/* Main loop over traces */
	do {
		static int itr = 0;
		++itr;

		/* Form autocorrelation vector */
		xcor(ncorr, imincorr, intrace.data,
		     ncorr, imincorr, intrace.data,
		     imaxlag, 0, autocorr);


		/* Leave trace alone if autocorr[0] vanishes */
		if (autocorr[0] == 0.0) {
			puttr(&intrace);
			if (showwiener)
				warn("NO Wiener filter, trace: %d", itr);
			if (showspiker)
				warn("NO spiking decon filter, trace: %d", itr);

			continue;
		}


		/* Whiten */
		autocorr[0] *= 1.0 + pnoise;


		/* Get inverse filter by Wiener-Levinson */
		stoepf(nlag, autocorr, crosscorr, wiener, spiker);
		

		/* Convolve pefilter with trace - don't do zero multiplies */
		{ register int i;
		  for (i = 0; i < nt; ++i) {
			register int j;
			register int n = MIN(i, imaxlag); 
			register float sum = intrace.data[i];

			for (j = iminlag; j <= n; ++j)
				sum -= wiener[j-iminlag] * intrace.data[i-j];

			outtrace.data[i] = sum;
		  }
		}


		/* Output filtered trace */
		memcpy((char*)&outtrace, (char*)&intrace, HDRBYTES);
		puttr(&outtrace);


		/* Show pefilter and/or spiker on request */
		if (showwiener) {
			register int i;
			warn("Wiener filter, trace: %d", itr);
			for (i = 0; i < imaxlag; ++i)
				fprintf(stderr, "%10g%c", wiener[i],
					(i%6==5 || i==nlag-1) ? '\n' : ' ');
		}
		
		if (showspiker) {
			register int i;
			warn("spiking decon filter, trace: %d", itr);
			for (i = 0; i < nlag; ++i)
				fprintf(stderr, "%10g%c", spiker[i],
					(i%6==5 || i==nlag-1) ? '\n' : ' ');
		}

	} while (gettr(&intrace));


	return EXIT_SUCCESS;
}
Esempio n. 16
0
int
main(int argc, char **argv) {
	cwp_String op="mult";	/* operation: add, sub, ..., 		*/
	int iop=MULT;		/* integer abbrev. for op in switch	*/
	int nt;			/* number of samples on input trace	*/
	float a;
	int copy, j, tracl;


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


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

	/* Get operation, recall iop initialized to the default FABS */
	getparstring("op", &op);
	if 	(STREQ(op, "add"))	iop = ADD;
	else if (STREQ(op, "sub"))	iop = SUB;
	else if (STREQ(op, "div"))	iop = DIV;
	else if (STREQ(op, "pow"))	iop = POW;
	else if (STREQ(op, "spow"))	iop = SPOW;
	else if (!STREQ(op, "mult"))	
		err("unknown operation=\"%s\", see self-doc", op);
	if (!getparfloat("a", &a)) a = 1;
	if (!getparint("copy", &copy)) copy = 1;
	if (copy > 1) tracl = 1;

	/* Main loop over traces */
	do {

		switch(iop) { register int i;
		case ADD:
			for (i = 0; i < nt; ++i)
				tr.data[i] += a;
		break;
		case SUB:
			for (i = 0; i < nt; ++i)
				tr.data[i] -= a;
		break;
		case MULT:
			for (i = 0; i < nt; ++i)
				tr.data[i] *= a;
		break;
		case DIV:
			for (i = 0; i < nt; ++i)
				tr.data[i] /= a;
		break;
		case POW:
			for (i = 0; i < nt; ++i)
				tr.data[i] = pow(tr.data[i],a);
		break;
		case SPOW:
			for (i = 0; i < nt; ++i)
				tr.data[i] = SGN(tr.data[i])*pow(ABS(tr.data[i]),a);
		break;
		default:  /* defensive programming */
			err("mysterious operation=\"%s\"", op);
		} /* end scope of i */
		
		if (copy == 1) {
			puttr(&tr);
		} else {
			for (j = 1; j <= copy; ++j) {
				tr.tracl = tracl;
				puttr(&tr);
				++tracl;
			}
		}

	} while (gettr(&tr));


	return(CWP_Exit());
}
Esempio n. 17
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());
}
Esempio n. 18
0
int
main(int argc, char **argv)
{
	int nz;		/* numer of depth samples */
	int iz;		/* counter */
	int nt;		/* number of time samples */

	int nzpar;	/* number of getparred depth values for velocities */
	int nvpar;	/* number of getparred velocity values */
	int izpar;	/* counter */

	int verbose;	/* verbose flag, =0 silent, =1 chatty */

	float dz=0.0;	/* depth sampling interval */
	float fz=0.0;	/* first depth value */
	float dt=0.0;	/* time sampling interval for velocities */
	float ft=0.0;	/* first time value */
	float z=0.0;	/* depth values for times */
	float vmin=0.0;	/* minimum velocity */
	float vmax=0.0;	/* maximum velocity */

	float *zpar=NULL;	/* values of z getparred */
	float *vpar=NULL;	/* values of v getparred */
	float *vz=NULL;		/* v(z) velocity as a function of z */
	float *zt=NULL;		/* z(t) depth as a function of t */
	float *temp=NULL;	/* temporary storage array */
	char *vfile="";		/* name of the velocity file */

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

	/* get time sampling from first header */
	if (!gettr(&tr)) err("can't get first trace");
	nz = tr.ns;

	/* get depth sampling */
	if (!getparfloat("dz",&dz)) dz = ((float) tr.d1);

	/* determine velocity function v(t) */
	vz = ealloc1float(nz);
	if (!getparstring("vfile",&vfile)) {
		nzpar = countparval("z");
		if (nzpar==0) nzpar = 1;
		zpar = ealloc1float(nzpar);
		if (!getparfloat("z",zpar)) zpar[0] = 0.0;
		nvpar = countparval("v");
		if (nvpar==0) nvpar = 1;
		if (nvpar!=nzpar)err("number of t and v values must be equal");
		vpar = ealloc1float(nvpar);
		if (!getparfloat("v",vpar)) vpar[0] = 1500.0;
		for (izpar=1; izpar<nzpar; ++izpar)
			if (zpar[izpar]<=zpar[izpar-1])
				err("zpar must increase monotonically");
		for (iz=0,z=0.0; iz<nz; ++iz,z+=dz)
			intlin(nzpar,zpar,vpar,vpar[0],vpar[nzpar-1],
				1,&z,&vz[iz]);
	} else { /* read from a file */
		if (fread(vz,sizeof(float),nz,fopen(vfile,"r"))!=nz)
			err("cannot read %d velocities from file %s",nz,vfile);
	}

	/* determine minimum and maximum velocities */
	for (iz=1,vmin=vmax=vz[0]; iz<nz; ++iz) {
		if (vz[iz]<vmin) vmin = vz[iz];
		if (vz[iz]>vmax) vmax = vz[iz];
	}

	/* get parameters */
	if (!getparfloat("dt",&dt)) dt = 2.0*dz/vmin;
	if (!getparfloat("ft",&ft)) ft = 2.0*ft/vz[0];
	if (!getparint("nt",&nt)) nt = 1+(nz-1)*dz*2.0/(dt*vmax);
	if (!getparint("verbose",&verbose)) verbose = 0;
	CHECK_NT("nt",nt);

	/* if requested, print time sampling, etc */
	if (verbose) {
		warn("Input:");
		warn("\tnumber of depth samples = %d",nz);
		warn("\tdepth sampling interval = %g",dz);
		warn("\tfirst depth sample = %g",fz);
		warn("Output:");
		warn("\tnumber of time samples = %d",nt);
		warn("\ttime sampling interval = %g",dt);
		warn("\tfirst time sample = %g",ft);
	}

	/* allocate workspace */
	zt = ealloc1float(nt);
	temp = ealloc1float(nz);

	/* make z(t) function */
	makezt(nz,dz,fz,vz,nt,dt,ft,zt);
	
	/* loop over traces */
	do {
		/* update header fields */
		tr.trid = TREAL;
		tr.ns = nt;
		tr.dt = dt*1000000.0;
		tr.f1 = ft;
		tr.d1 = 0.0;

		/* resample */
		memcpy((void *) temp, (const void *) tr.data,nz*sizeof(float));
		ints8r(nz,dz,fz,temp,0.0,0.0,nt,zt,tr.data);

		/* put this trace before getting another */
		puttr(&tr);

	} while(gettr(&tr));

	return(CWP_Exit());
}
Esempio n. 19
0
int main(int argc, char **argv)      /*argc, argv - the arguments to the main() function*/
{ 
int nt;                              /*number of time samples*/
int nz;			             /*number of migrated depth samples*/
int nx;                              /*number of midpoints (traces)*/
int ix;
int iz;

float dt;                            /*time sampling interval*/                
float dx;                            /*spatial sampling interval*/
float dz;                            /*migrated depth sampling interval*/           
float **data;                        /*input seismic data*/
complex **image;                     /*migrated image*/      
float **rimage;                      /*migrated image*/ 
float **v;                           /*velocity model*/
FILE *vfp;

char *vfile="";                      /*name of velocity file*/
int verbose=1;
char *tmpdir;		             /* directory path for tmp files*/
cwp_Bool istmpdir=cwp_false;         /* true for user-given path*/

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

/********************************* Get parameters **************************************/
/*get info from first trace*/
if (!gettr(&tr))  err("can't get first trace");  /*fgettr: get a fixed-length segy trace from a file by file pointer*/
nt = tr.ns;                         /*nt*/       /*gettr: macro using fgettr to get a trace from stdin*/

if (!getparfloat("dt", &dt)) {      /*dt*/
if (tr.dt) { 
dt = ((double) tr.dt)/1000000.0;
} 
else {err("dt is not set");}
}

if (!getparfloat("dx", &dx)) {       /*dx*/
if (tr.d2) {
dx = tr.d2;
} 
else {
err("dx is not set");
}
}

/*get optional parameters*/
if (!getparint("nz",&nz)) err("nz must be specified"); 
if (!getparfloat("dz",&dz)) err("dz must be specified");
if (!getparstring("vfile", &vfile)) err("velocity file must be specified");
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();

/**************************** Count trace number nx ******************************/
/* store traces and headers in tempfiles 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);
	}

	nx = 0;
	do {
		 ++nx;                                   /*get the number of traces nx*/
		efwrite(&tr,HDRBYTES,1,headerfp);
		efwrite(tr.data, FSIZE, nt, tracefp);
	} while (gettr(&tr));

	erewind(tracefp);                    /*Set position of stream to the beginning*/
	erewind(headerfp);

/******************************************************************************************/

/*allocate memory*/
data = alloc2float(nt,nx);                   /*2D array nx by nt*/
image = alloc2complex(nz,nx);                /*2D array nx by nz*/
rimage = alloc2float(nz,nx);                 /*2D array nx by nz*/
v= alloc2float(nz,nx);                       /*2D array, in Fortran the velocity model is nz by nx 2D array*/ 
                                             /*in binary, it is actually 1D*/

/* load traces into the zero-offset array and close tmpfile */
efread(*data, FSIZE, nt*nx, tracefp);        /*read traces to data*/
efclose(tracefp);                 

/*load velicoty file*/
vfp=efopen(vfile,"r");	
efread(v[0],FSIZE,nz*nx,vfp);                    /*load velocity*/
efclose(vfp);			

/***********************finish reading data*************************************************/
/* call pspi migration function*/
pspimig(data,image,v,nt,nx,nz,dt,dx,dz);

/*get real part of image*/
for (iz=0;iz<nz;iz++){
for (ix=0;ix<nx;ix++){
rimage[ix][iz] = image[ix][iz].r;
}
}

/* restore header fields and write output */
for (ix=0; ix<nx; ix++) {
efread(&tr,HDRBYTES,1,headerfp);
tr.ns = nz;
tr.d1 = dz;
memcpy( (void *) tr.data, (const void *) rimage[ix],nz*FSIZE);
puttr(&tr);
}
	
/* Clean up */
efclose(headerfp);
if (istmpdir) eremove(headerfile);
if (istmpdir) eremove(tracefile);
return(CWP_Exit());	
}
Esempio n. 20
0
int main( int argc, char *argv[] )
{
	cwp_String keyg;	/* header key word from segy.h		*/
	cwp_String typeg;	/* ... its type				*/
	Value valg;
	cwp_String key[SU_NKEYS];	/* array of keywords		 */
	cwp_String type[SU_NKEYS];	/* array of keywords		 */
	int index[SU_NKEYS];	/* name of type of getparred key	 */
	
	segy **rec_o;		/* trace header+data matrix */	
	
	int first=0;	/* true when we passed the first gather */
	int ng=0;
	float dt;	/* time sampling interval		*/
	int nt;		/* number of time samples per trace	*/
	int ntr;	/* number of traces per ensemble	*/
	
	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 *ctmix;		/* complex trace			*/
	complex **fd;		/* frequency domain data		*/

	
	float padd;
	
	int nd;			/* number of dimensions */
	float *dx=NULL;
	float fac;
	float vmin;
	int vf;
	
	/* Trimming arrays */
	float *itrm=NULL;
	float *rtrm=NULL;
	float *wht=NULL;
	float trimp=15;
		
	/* Initialize */
	initargs(argc, argv);
	requestdoc(1);
	
	if (!getparstring("keyg", &keyg)) keyg ="ep";
	if (!getparint("vf", &vf)) vf = 1;
	if (!getparfloat("vmin", &vmin)) vmin = 5000;
	if (!getparfloat("padd", &padd)) padd = 25.0;
	padd = 1.0+padd/100.0;
	
	/* Get "key" values */
	nd=countparval("key");
	getparstringarray("key",key);

	/* get types and indexes corresponding to the keys */
	{ int ikey;
		for (ikey=0; ikey<nd; ++ikey) {
			type[ikey]=hdtype(key[ikey]);
			index[ikey]=getindex(key[ikey]);
		}
	}

	dx = ealloc1float(nd);
	MUSTGETPARFLOAT("dx",(float *)dx);
	
	if (!getparfloat("fac", &fac)) fac = 1.0;
	fac = MAX(fac,1.0);

	/* get the first record */
	rec_o = get_gather(&keyg,&typeg,&valg,&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;
	d1 = 1.0/(nfft*dt);
	
	rt = ealloc1float(nfft);
	ctmix = ealloc1complex(nf);
	
	
	do {
		ng++;
		 	
		fd = ealloc2complex(nf,ntr); 
		memset( (void *) ctmix, (int) '\0', nf*sizeof(complex));
		
		itrm = ealloc1float(ntr);
		rtrm = ealloc1float(ntr);
		wht = ealloc1float(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]);
			
			}
		}
		
		/* Do the mixing */
		{ unsigned int imx=0,itr,ifr;
		  float dist;
		  
		  	
			/* Find the trace to mix */
			for(itr=0;itr<ntr;itr++) 
				if((*rec_o[itr]).mark) {
					imx = itr;
					break;
				}
			
			memcpy( (void *) ctmix, (const void *) fd[imx],nf*sizeof(complex));
			
			/* Save the header */
			memcpy( (void *) &tr, (const void *) rec_o[imx],HDRBYTES);
 		  	
			/* weights */
			wht[imx] = 1.0;
			for(itr=0;itr<imx;itr++) {
				 dist=n_distance(rec_o,index,type,dx,nd,imx,itr);
				 wht[itr] = MIN(1.0/dist,1.0);
				 wht[itr] = 1.0;
			}
			
			for(itr=imx+1;itr<ntr;itr++) {
				 dist=n_distance(rec_o,index,type,dx,nd,imx,itr);
				 wht[itr] = MIN(1.0/dist,1.0);
				 wht[itr] = 1.0;
			}
				 
			
			/* Do the alpha trim for each trace */			
			for(ifr=0;ifr<nf;ifr++) {
 		  		for(itr=0;itr<ntr;itr++) {
					itrm[itr] = fd[itr][ifr].i;
					rtrm[itr] = fd[itr][ifr].r;
				}
				ctmix[ifr].i = alpha_trim_w(itrm,wht,ntr,trimp);
				ctmix[ifr].r = alpha_trim_w(rtrm,wht,ntr,trimp);
			}
			
					
		}
		
		
		{ unsigned int it;
			pfacr(-1, nfft, ctmix, rt);
				for(it=0;it<nt;it++) 		
					tr.data[it]=rt[it]*snfft;
		}
			
		free2complex(fd);

		{ unsigned int itr;
			for(itr=0;itr<ntr;itr++) {
				free1((void *)rec_o[itr]);
			}
		}
		
		puttr(&tr);
		
	    	rec_o = get_gather(&keyg,&typeg,&valg,&nt,&ntr,&dt,&first);
		
		fprintf(stderr," %d %d\n",ng,ntr);
		
		free1float(rtrm);
		free1float(itrm);
		free1float(wht);
		
	} while(ntr);
		
	
	free1float(rt);

	warn("Number of gathers %10d\n",ng);
	 
	return EXIT_SUCCESS;
}
Esempio n. 21
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());
}
Esempio n. 22
0
int
main(int argc, char **argv)
{
	cwp_String op="abs";	/* operation: abs, exp, ..., 		*/
	int iop=FABS;		/* integer abbrev. for op in switch	*/
	int nt;			/* number of samples on input trace	*/
	float dt;		/* time sampling interval */
	float twobydt;		/* 2*dt */

        float *tmp=NULL;        /* temp trace for some calcs */
        float *tmp1=NULL;       /* temp trace for some calcs */
        int nw;                 /* number of time samples in window */
        int mt;                 /* half-width of window */
        int j;		/* counter */
        float val;	/* value */
        float mean;	/* mean */



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


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


	/* Get operation, recall iop initialized to the default FABS */
	getparstring("op", &op);
	if      (STREQ(op, "ssqrt"))	iop = SSQRT;
	else if (STREQ(op, "sqr"))	iop = SQR;
	else if (STREQ(op, "ssqr"))	iop = SSQR;
	else if (STREQ(op, "sgn"))	iop = SIGN;
	else if (STREQ(op, "exp"))	iop = EXP;
	else if (STREQ(op, "slog"))	iop = SLOG;
	else if (STREQ(op, "slog2"))	iop = SLOG2;
	else if (STREQ(op, "slog10"))	iop = SLOG10;
	else if (STREQ(op, "cos"))	iop = COS;
	else if (STREQ(op, "sin"))	iop = SIN;
 	else if (STREQ(op, "tan"))	iop = TAN;
	else if (STREQ(op, "cosh"))	iop = COSH;
	else if (STREQ(op, "sinh"))	iop = SINH;
	else if (STREQ(op, "tanh"))	iop = TANH;
	else if (STREQ(op, "norm"))     iop = NORM;
	else if (STREQ(op, "db"))       iop = DB;
	else if (STREQ(op, "neg"))      iop = NEG;
	else if (STREQ(op, "nop"))      iop = NOP;
	else if (STREQ(op, "posonly"))  iop = ONLY_POS;
	else if (STREQ(op, "negonly"))  iop = ONLY_NEG;
	else if (STREQ(op, "sum"))      iop = SUM;
	else if (STREQ(op, "refl"))     iop = REFL;
	else if (STREQ(op, "diff"))     iop = DIFF;
	else if (STREQ(op, "mod2pi"))  	iop = MOD_2PI;
	else if (STREQ(op, "inv"))  	iop = INV;
 	else if (STREQ(op, "avg"))  	iop = AVG;
 	else if (STREQ(op, "rmsamp"))  	iop = RMS_AMP;
	else if (STREQ(op, "std"))      iop = STD;
	else if (STREQ(op, "var"))      iop = VAR;
	else if (STREQ(op, "mean"))     iop = MEAN;
	else if (STREQ(op, "s2v"))      iop = S2V;
	else if (STREQ(op, "s2vm"))     iop = S2VM;
	else if (STREQ(op, "d2m"))      iop = D2M;
	else if (STREQ(op, "drv2"))     iop = DRV2;
	else if (STREQ(op, "drv4"))     iop = DRV4;
	else if (STREQ(op, "integ"))    iop = INTEG;
	else if (STREQ(op, "spike"))    iop = SPIKE;
	else if (STREQ(op, "freq"))     iop = FREQ;
	else if (STREQ(op, "lnza"))     iop = LNZA;
	else if (STREQ(op, "saf"))      iop = SAF;
	else if (!STREQ(op, "abs"))
		err("unknown operation=\"%s\", see self-doc", op);

        if (!getparint("nw", &nw)) nw = 21;
        /* make sure nw is odd */
        if (nw%2 != 0) nw += 1;

	/* Main loop over traces */
	do {

		switch(iop) { register int i;
		case FABS:
			for (i = 0; i < nt; ++i)
				tr.data[i] = ABS(tr.data[i]);
		break;
		case SSQRT:
			for (i = 0; i < nt; ++i) {
				float x = tr.data[i];
				tr.data[i] = SGN(x) * sqrt(ABS(x));
			}
		break;
		case SQR:
			for (i = 0; i < nt; ++i) {
				float x = tr.data[i];
				tr.data[i] = x * x;
			}
		break;
		case SSQR:
			for (i = 0; i < nt; ++i) {
				float x = tr.data[i];
				tr.data[i] = SGN(x) * x * x;
			}
		break;
		case SIGN:
			for (i = 0; i < nt; ++i) {
				float x = tr.data[i];
				tr.data[i] = SGN(x);
			}
		break;
		case EXP:
			for (i = 0; i < nt; ++i)
				tr.data[i] = exp(tr.data[i]);
		break;
		case SLOG:
			for (i = 0; i < nt; ++i) {
				float x = tr.data[i];

				if (ABS(x) > 0) {
					tr.data[i] = SGN(x) * log(ABS(x));
				} else {
					tr.data[i] = 0;
				}
			}
		break;
               case SLOG2:
                        for (i = 0; i < nt; ++i) {
                                float x = tr.data[i];

                                if (ABS(x) > 0) {
                                        tr.data[i] = SGN(x) * log(ABS(x))/log(2.0);
                                } else {
                                        tr.data[i] = 0;
                                }
                        }

		case SLOG10:
			for (i = 0; i < nt; ++i) {
				float x = tr.data[i];
				if (ABS(x) > 0) {
					tr.data[i] = SGN(x) * log10(ABS(x));
				} else {
					tr.data[i] = 0;
				}
					
			}
		break;
		case COS:
			for (i = 0; i < nt; ++i)
				tr.data[i] = cos(tr.data[i]);
		break;
		case SIN:
			for (i = 0; i < nt; ++i)
				tr.data[i] = sin(tr.data[i]);
		break;
		case TAN:
			for (i = 0; i < nt; ++i)
				tr.data[i] = tan(tr.data[i]);
		break;
		case COSH:
			for (i = 0; i < nt; ++i)
				tr.data[i] = cosh(tr.data[i]);
		break;
		case SINH:
			for (i = 0; i < nt; ++i)
				tr.data[i] = sinh(tr.data[i]);
		break;
		case TANH:
			for (i = 0; i < nt; ++i)
				tr.data[i] = tanh(tr.data[i]);
		break;
		case NORM:
		      { float x, max;
			max = 0.0;
			for (i = 0; i < nt; ++i) {
				x = ABS (tr.data [i]);
				if (max < x) max = x;
			}
			if (max != 0.0)
				for (i = 0; i < nt; ++i) tr.data [i] /= max;
		      }	/* end scope of x, max */
		break;
 
		case DB:
			if (iop == DB)  {
				float x;
				for (i = 0; i < nt; ++i) {
					x = tr.data[i];
					tr.data[i] = (ABS(x) > 0) ?
					   20.0*SGN(x)*log10(ABS(x)) : 0;
				}
			}
		break;
		
		case NEG:
			for (i = 0; i < nt; ++i)
				tr.data[i] = -tr.data[i];
		break;
		case NOP:
		break;
		case ONLY_POS:
		{
			float x;		     
	       		for (i = 0; i < nt; ++i) {   
				x = tr.data[i];      
				tr.data[i] = (x > 0) ? x : 0;  
			}
		}			      
		break; 
		case ONLY_NEG:
		{
			float x;		     
	       		for (i = 0; i < nt; ++i) {   
				x = tr.data[i];      
				tr.data[i] = (x < 0) ? x : 0;  
			}
		}			      
		break;
		case SUM:
		{
	       		for (i = 1; i < nt; ++i) {   
				tr.data[i] += tr.data[i-1];
			}
		}			      
		break; 
		case REFL:
		{
	       		for (i = nt-1; i > 0; --i) {   
				float numer=(tr.data[i] - tr.data[i-1]);
				float denom=(tr.data[i] + tr.data[i-1]);
				if (denom == 0.0) denom = 1.0;
				tr.data[i] = ( numer/denom ) ;
			}
			tr.data[0] = 0.0;
		}			      
		break;
		case DIFF:
		{
			float *datatmp=NULL; /* temporary data storage */

			/* allocate space for temporary data */
			datatmp = ealloc1float(nt);

			/* copy data from tr.data */
			memcpy((void *)datatmp, (const void *) tr.data, nt*FSIZE);
			/* do centered differences for the rest */
	       		for (i = 2; i < nt-2; ++i) {   
				float numer;
				numer=(datatmp[i+1] - datatmp[i-1]);
				tr.data[i] = ( numer/twobydt ) ;
			}
	
			/* simple diffrence for tr.data[0] */
			tr.data[0] = (datatmp[1] - datatmp[0])/dt;
			tr.data[nt-1] = (datatmp[nt-1] - datatmp[nt-2])/dt;

			/* centered difference for tr.data[1] */
			tr.data[1] = (datatmp[2] - datatmp[0])/twobydt;
			tr.data[nt-2] = (datatmp[nt-1] - datatmp[nt-3])/twobydt;
		}			      
		break;

 		case AVG:
 	        {
 			float sum = 0;float avg = 0 ;
	        for (i = 0; i < nt; ++i)
		        sum = sum + tr.data[i];
 			avg = sum / nt ;
 			for (i = 0; i < nt; ++i)
 				tr.data[i] = tr.data[i] - avg;
 			}
 		break;

 		case RMS_AMP:
	        {
 			float sum = 0;float rmsamp = 0 ;
	        for (i = 0; i < nt; ++i)
		        sum = sum + tr.data[i]*tr.data[i];
 			rmsamp = sqrt(sum / nt) ;
			
			memset((void *) &tr.data, 0, nt*FSIZE);
			tr.data[0] = rmsamp;
			}
 		break;

		case MOD_2PI:
			for (i = 0; i < nt; ++i)
			{	while(tr.data[i]<0) tr.data[i] += TWOPI;
				while(tr.data[i]>=TWOPI) tr.data[i] -= TWOPI;
	        }
		break;
		case INV:
			for (i = 0; i < nt; ++i)
				if (tr.data[i]) tr.data[i] = 1.0 / tr.data[i];
		break;
		case LNZA:
		{
			float x, y, z;
			
			/* copy a trace to tmp */
			for (i = 0; i < nt; ++i) {
				tmp[i] = tr.data[i];
			}
			
			/*
			here are the three cases
			
			x y z    x y       x           +
			--------------------------> t  0
			x y z        z       y z       -
			
			*/
			for (i = 1; i < nt-1; ++i) {
				x = tr.data[i-1];
				y = tr.data[i];
				z = tr.data[i+1];
				if ( SGN(x) == SGN(y) && SGN(y) == SGN(z)) {
					tmp[i] = 0.0;
				} 
				if ( SGN(x) == SGN(y) && SGN(y) != SGN(z)) {
					if ( abs(y) < abs(z) ) {
						tmp[i] = y;
					} else {
						tmp[i] = z;
					}
				} 
				if ( SGN(x) != SGN(y) && SGN(y) == SGN(z)) {
					if ( abs(x) < abs(y) ) {
						tmp[i] = x;
					} else {
						tmp[i] = y;
					}
				} 
			}
			
			/* fix first and last sample */
			tmp[0]  = 0.0;
			tmp[nt] = 0.0;
			
			/* copy values back to trace */
			for (i = 0; i < nt; ++i) {
				tr.data[i] = tmp[i];
			}
		}			      
		break; 
		case SPIKE:
		{
			float x1, x2, x3;
			for (i = 1; i < nt-1; ++i) {
				x1 = tr.data[i-1];
				x2 = tr.data[i];
				x3 = tr.data[i+1];
				/* local min or max */
				if ( ( (x1 < x2) && (x3 < x2) ) || 
				     ( (x1 > x2) && (x3 > x2) ) ){
					tmp[i] = x2;
				} else {
					/* neither */
					tmp[i] = 0.0;
				}
			}
			/* edge effects */
			tmp[0] = 0.0;
			tmp[nt] = 0.0;
			/* reload trace for output */
			for (i = 0; i < nt; ++i) {
				tr.data[i] = tmp[i];
			}
		}			      
		break;
		case SAF:
		{
			float x1, x2, x3;
			int iold;    /* index of last spike */
			int j;	     /* fill counter	    */
			float vold;  /* value of last spike */
			iold = 0;
			vold = 0.0;
			for (i = 1; i < nt-1; ++i) {
				x1 = tr.data[i-1];
				x2 = tr.data[i];
				x3 = tr.data[i+1];
				/* local min or max */
				if ( ( (x1 < x2) && (x3 < x2) ) || 
				     ( (x1 > x2) && (x3 > x2) ) ){
					tmp[i] = x2;
					/* fill from last spike */
					for (j = iold; j < i; ++j ) {
						tmp[j] = vold;
					}
					/* reset old values */
					iold = i;
					vold = x2;
				} else {
					/* neither */
					tmp[i] = 0.0;
				}
			}
			/* edge effects */
			tmp[0] = 0.0;
			tmp[nt] = 0.0;
			/* reload trace for output */
			for (i = 0; i < nt; ++i) {
				tr.data[i] = tmp[i];
			}
		}			      
		break;
		case FREQ:
		{
			float x1, x2, x3, delay, freq;
			register int iold, j;
			/*
			  scan for extrema
			  local max:  (x2 > x1) and (x2 > x3)... assign +1
			  local min:  (x2 < x1) and (x2 < x3)... assign -1
			*/
			for (i = 1; i < nt-1; ++i) {
				x1 = tr.data[i-1];
				x2 = tr.data[i];
				x3 = tr.data[i+1];
				/* local min or max */
				if ( (x2 > x1) && (x2 > x3) ) {
					tmp[i] =  1.0; 
				} else if ( (x2 < x1) && (x2 < x3) ) {
					tmp[i] = -1.0; 
				} else {
					/* neither */
					tmp[i] =  0.0;
				}
			}
			/* edge effects */
			tmp[0] = 0.0;
			tmp[nt] = 0.0;
			/*
			  delay between consecutive local minima 
			  is local dominant period, which is inverse 
			  of local dom frequency
			*/
			/* assume first sample is a minima... kludge */
			iold = 0;
			/* skip a few samples to avoid false high freq at start */
			for (i = 5; i < nt; ++i) {
				if ( tmp[i] == -1.0 ) {
						/* calc delay from last min */
						delay = (i - iold) * dt;
						/* calc freq Hz 
						   This delay is the period */
						freq  = 1 / ( delay);
						/* fill with this freq from last min to here */
						for (j = iold; j < i; ++j) {
							tmp1[j] = freq;
						}
					/* hold loc of this minima */
					iold = i;
				}
			}
			/* reload trace for output */
			for (i = 0; i < nt; ++i) {
				tr.data[i] = tmp1[i];
			}
		}			      
		break;
		case DRV4:
			for (i = 2; i < nt-2; ++i) {
				float x =         tr.data[i-2] 
					  - 8.0 * tr.data[i-1]
					  + 8.0 * tr.data[i+1] 
					        - tr.data[i+2];
				tmp[i] = - x / (12 * dt);
			}
			tmp[0]    = tmp[2];
			tmp[1]    = tmp[2];
			tmp[nt-1] = tmp[nt-2];
			tmp[nt]   = tmp[nt-2];
			for (i = 2; i < nt-2; ++i) {
				tr.data[i] = tmp[i];
			}
		break;
		case DRV2:
			for (i = 1; i < nt-1; ++i) {
				float x = tr.data[i-1] - tr.data[i];
				tmp[i] = x / (2.0 * dt);
			}
			tmp[0] = tmp[1];
			tmp[nt] = tmp[nt-1];
			for (i = 1; i < nt-1; ++i) {
				tr.data[i] = tmp[i];
			}
		break;
		case INTEG:
			/* copy trace into temp array */
			for (i = 0; i < nt; ++i) {
				tmp[i] = tr.data[i];
			}
			/* loop over output times */
			val = 0.0;
			for (i = 0; i < nt; ++i) {
				val = val + tmp[i];
				tr.data[i] = val;
			}
		break;
		case S2V:
			for (i = 0; i < nt; ++i) {
				float x = tr.data[i];
				tr.data[i] = 1000000./x;
			}
		break;
		case S2VM:
			for (i = 0; i < nt; ++i) {
				float x = tr.data[i];
				tr.data[i] = 304800./x;
			}
		break;
		case D2M:
			for (i = 0; i < nt; ++i) {
				float x = tr.data[i];
				tr.data[i] = 1000.0*x;
			}
		break;
		case STD:
			/* copy trace into temp array */
			for (i = 0; i < nt; ++i) {
				tmp[i] = tr.data[i];
			}
			/* half width of window */
			mt = (nw-1)/2;
			/* loop over output times */
			for (i = 0; i < nt; ++i) {
				val = 0.0;
				/* check we are not off the data ends */
				if (i-mt > 0 && i+mt < nt) {
					/* calc mean */
					for (j = i-mt; j <= i+mt; ++j) {
						val += tmp[j];
					}
					mean = val/nw;
					/* calc std deviation */
					for (j = i-mt; j <= i+mt; ++j) {
						val += (tmp[j] - mean)*(tmp[j] - mean);
					}
					tr.data[i] = sqrt(val/ (float) nw);
				} else {
					tr.data[i] = 0.0;
				}
			}
		break;
		case VAR:
			/* copy trace into temp array */
			for (i = 0; i < nt; ++i) {
				tmp[i] = tr.data[i];
			}
			/* half width of window */
			mt = (nw-1)/2;
			/* loop over output times */
			for (i = 0; i < nt; ++i) {
				val = 0.0;
				/* check we are not off the data ends */
				if (i-mt > 0 && i+mt < nt) {
					/* calc mean */
					for (j = i-mt; j <= i+mt; ++j) {
						val += tmp[j];
					}
					mean = val/nw;
					/* calc variance */
					for (j = i-mt; j <= i+mt; ++j) {
						val += (tmp[j] - mean)*(tmp[j] - mean);
					}
					tr.data[i] = val/ (float) nw;
				} else {
					tr.data[i] = 0.0;
				}
			}
		break;
		case MEAN:
			/* copy trace into temp array */
			for (i = 0; i < nt; ++i) {
				tmp[i] = tr.data[i];
			}
			/* half width of window */
			mt = (nw-1)/2;
			/* loop over output times */
			for (i = 0; i < nt; ++i) {
				val = 0.0;
				/* check we are not off the data ends */
				if (i-mt > 0 && i+mt < nt) {
					/* calc mean */
					for (j = i-mt; j <= i+mt; ++j) {
						val += tmp[j];
					}
					tr.data[i] = val/nw;
				} else {
					tr.data[i] = 0.0;
				}
			}
		break;

		default:  /* defensive programming */
			err("mysterious operation=\"%s\"", op);
		} /* end scope of i */
		
		
		
		puttr(&tr);

	} while (gettr(&tr));


	return(CWP_Exit());
}
Esempio n. 23
0
int
main(int argc, char **argv)
{
	cwp_String key1,key2,key3;	/* panel/trace/flag key		*/
	cwp_String type1,type2,type3;	/* type for panel/trace/flag key*/
	int index1,index2,index3;	/* indexes for key1/2/3		*/
	Value val1,val2,val3;		/* value of key1/2/3		*/
	double dval1=0.0,dval2=0.0,dval3=0.0;	/* value of key1/2/3	*/
	double c;			/* trace key spacing		*/
	double dmin,dmax,dx;		/* trace key start/end/spacing	*/
	double panelno=0.0,traceno=0.0;
	int nt;				/* number of samples per trace	*/
	int isgn;			/* sort order			*/
	int iflag;			/* internal flag:		*/
					/* -1	exit			*/
					/* 0	regular mode		*/
					/* 1	first time		*/
					/* 2	trace out of range	*/

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

	/* get parameters */
	if (!getparstring("key1", &key1)) key1 = "ep";
	if (!getparstring("key2", &key2)) key2 = "tracf";
	if (!getparstring("key3", &key3)) key3 = "trid";
	if (!getpardouble("val3", &dval3)) dval3 = 2.;
	if (!getpardouble("d", &dx)) dx = 1.;
	if (!getpardouble("min", &dmin)) err("need lower panel boundary MIN");
	if (!getpardouble("max", &dmax)) err("need upper panel boundary MAX");
        checkpars();

	/* check parameters */
	if (dx==0) err("trace spacing d cannot be zero");
	if (dmax<dmin) err("max needs to be greater than min");

	if (dx<0) {
		isgn = -1;
	} else {
		isgn = 1;
	}

	/* get types and index values */
	type1  = hdtype(key1);
	type2  = hdtype(key2);
	type3  = hdtype(key3);
	index1 = getindex(key1);
	index2 = getindex(key2);
	index3 = getindex(key3);

	/* loop over traces */
	iflag = 1;
	while (iflag>=0) {

		if (gettr(&tr)) {
			/* get header values */
			gethval(&tr, index1, &val1);
			gethval(&tr, index2, &val2);
			dval1 = vtod(type1, val1);
			dval2 = vtod(type2, val2);
			/* Initialize zero trace */
			nt = tr.ns;
			memset( (void *) nulltr.data, 0, nt*FSIZE);
			if ( iflag==1 ) {
				panelno = dval1;
				traceno = dmin - dx;
			}
			iflag = 0;
			if ( dval2<dmin || dval2>dmax ) iflag = 2;
/*	fprintf(stderr,"if=%d, dmin=%8.0f, dmax=%8.0f\n",iflag,dmin,dmax);*/
		} else {
			iflag = -1;	/* exit flag */
		}
/*	fprintf(stderr,"if=%d, dval1=%8.0f, dval2=%8.0f\n",iflag,dval1,dval2);*/

		/* if new panel or last trace --> finish the previous panel */
		if ( panelno!=dval1 || iflag==-1 ) {
/*	fprintf(stderr,"finish previous\n");*/
			for (c=traceno+dx; isgn*c<=isgn*dmax; c=c+dx) {
					assgnval(type2, &val2, c);
					puthval(&nulltr, index2, &val2);
					assgnval(type3, &val3, dval3);
					puthval(&nulltr, index3, &val3);
					puttr(&nulltr);
			}
			traceno = dmin - dx;	/* reset to pad present panel */

			panelno = dval1; /* added by Ted Stieglitz 28Nov2012*/

		}

		/* if trace within boundaries --> pad the present panel */
		if ( iflag==0 ) {
/*	fprintf(stderr,"pad present, trn=%5.0f,dval2=%5.0f\n",traceno,dval2);*/
			memcpy( (void *) &nulltr, (const void *) &tr, 240);
			for (c=traceno+dx; isgn*c<isgn*dval2; c=c+dx) {
					assgnval(type2, &val2, c);
					puthval(&nulltr, index2, &val2);
					assgnval(type3, &val3, dval3);
					puthval(&nulltr, index3, &val3);
					puttr(&nulltr);
			}
		}

		/* write the present trace and save header indices */
		if ( iflag==0 ) {
			puttr(&tr);
			panelno = dval1;
			traceno = dval2;
		}

	}

	return(CWP_Exit());
}
Esempio n. 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 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());
}
Esempio n. 25
0
int
main(int argc, char **argv)
{
	register float *rt;	/* real trace				*/
	register float *mt;	/* magnitude trace			*/
	register float *ct;	/* resampled centroid trace 		*/

	int nt;			/* number of points on input trace	*/
	int verbose;		/* flag to get advisory messages	*/
	float dt;		/* sampling interval in secs		*/
	float invdt;		/* inverse dt				*/
	float hdt;		/* half dt				*/

	cwp_Bool inflect=cwp_false;	/* inflection point flag	  */
	cwp_Bool zero_cross=cwp_false; /* zero-crossing flag		*/
	cwp_Bool max_passed=cwp_false; /* maximum value passed flag	*/

	float *time;		/* array of trace sample time values	*/
	float sum_amp;	  /* sum of amplitudes in lobe		*/
	float t_cen;		/* centroid about amplitude axis	*/

	int isamp;		/* t_cen time sample number		*/

	float a_cen;		/* centroid about time axis		*/
	float a_mom;		/* moment about time axis		*/
	float t_mom;		/* moment about amplitude axis	  */
	float a_height;	 /* height of region for moment calc	*/
	float t_width;	  /* width of region for moment calc	*/

	int first;		/* number of first sample in lobe	*/
	int last;		/* number of last sample in lobe	*/
	int prev;		/* number of past sample in amp moment  */

	int small;		/* sample number of current smaller mag */
	int nvals;		/* number of samples in current lobe	*/
	int nvals_min;	  /* minimum samples in lobe for inclusion*/

	int i,k;		/* counter indices			*/

	/* Initialize */
	initargs(argc, argv);
	requestdoc(1);
	if (!getparint("verbose", &verbose))	verbose=1;

	if (!getparint("nvals_min", &nvals_min))	nvals_min = 1;

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


	/* dt is used only to set output header value d1 */
	if (!getparfloat("dt", &dt))	dt = ((double) tr.dt)/1000000.0;
	if (!dt) {
		dt = .004;
		if (verbose) warn("dt not set, assumed to be .004");
	}
	invdt = 1.0 / dt;
	hdt = 0.5 * dt;


	/* Allocate space */
	rt = ealloc1float(nt);
	ct = ealloc1float(nt);
	mt = ealloc1float(nt);
	time = ealloc1float(nt);

	/* create an array of times */
	for (i = 0; i < nt; ++i) {
		time[i] = (float)(i + 1) * dt;
	}

	/* Main loop over traces */
	do {
	 	register int i;

		/* Load trace into rt and zero ct */
		memcpy((void *) rt, (const void *) tr.data, nt*FSIZE);
		memset((void *) ct, 0, nt*FSIZE);

		first = 0;
		mt[0] = fabs(rt[0]);
		sum_amp = rt[0];
		t_mom = time[0] * mt[0];

		for (i = 1; i < nt; ++i) {
			mt[i] = fabs(rt[i]);

			/* test for zero-crossing or inflection point */
			if(rt[i] * rt[i-1] > (float)0) {
				if(mt[i] > mt[i-1]) {
					if(max_passed) inflect = cwp_true;
			} else {
				max_passed = cwp_true;
			}
		} else {
			zero_cross = cwp_true;
		}

		/* if a zero-crossing or inflection point is not	*/
		/* encountered on the current trace sample,		*/
		/* accumulate the time moment				*/
		/* and sum of the lobe amplitude			*/
		if(!zero_cross && !inflect) {
			sum_amp = sum_amp + rt[i];
			t_mom = t_mom + (time[i] * mt[i]);
		} else {

			/* otherwise a zero-crossing or inflection has	*/
			/* occured, so stop and determine amplitude 	*/
			/* centroid and store results as a centroid	*/
			/* sample for the current lobe		   */

			/* determine the amplitude centroid */
			last = i - 1;

			/* if inflection point has been found divide it */
			/* between the calcs for this lobe and the next */
			if (inflect) {
				last = i;
				rt[last] = rt[last] * 0.5;
				mt[last] = fabs(rt[last]);
				sum_amp = sum_amp + rt[last];
				t_mom = t_mom + (time[last] * mt[last]);
			}

			nvals = last - first + 1;

			if(nvals == 1) {

				/* check to see if lobe is big enough	*/
				/* to be included			*/
				if(nvals >= nvals_min) {
		  			ct[i] = rt[i] * 0.5;
				}

				first = i;

				inflect = cwp_false;
				max_passed = cwp_false;
				zero_cross = cwp_false;

				sum_amp = rt[i];
				t_mom = time[i] * mt[i];
			} else {
				a_height = mt[first];

				if(mt[first] > mt[last]) {
		  			a_height = mt[last];
				}

				t_width = time[last] - time[first] + dt;
				a_mom = a_height * 0.5;
				a_cen = t_width * a_height * a_mom;
				small = first;

				if (mt[first] > mt[last]) small = last;
				for (k = 1; k < nvals; k ++) {
					prev = small;

		  			if (prev == first) first = first + 1;
		  			else last = last - 1; 

		  			small = first;
		  			if(mt[first] > mt[last]) small = last;

		  			a_height = rt[small] - rt[prev];
					a_mom = rt[prev] + (a_height * 0.5);
		  			t_width = t_width - dt;
		  			a_cen = a_cen + a_mom*t_width*a_height;
				}
				if(sum_amp == 0.0) {
					warn("i = %d, sum_amp = %f,divide by zero !",
						i,sum_amp);
				}
				a_cen = a_cen / (sum_amp * dt);

				/* determine the time centroid */
				t_cen = t_mom / fabs(sum_amp);

				/* start accumulating amplitude sum	*/
				/* and time moment for next lobe	*/
				sum_amp = rt[i];
				t_mom = time[i] * mt[i];
 
				/* set sample corresponding to t_cen	*/
				/* to amplitude a_cen 			*/
				isamp = (int) ((t_cen * invdt) + hdt);
		
				/* check to see if lobe is big enough	*/
				/* to be included			*/
				if(nvals >= nvals_min) {
					ct[isamp] = a_cen;
				}

				first = i;
				inflect = cwp_false;
				max_passed = cwp_false;
				zero_cross = cwp_false;
			}  /* end if block for case of nvals > 1 */
		}  /* end calc of new centroid */
	  }  /* end of loop down input trace */
		
	  /* Store values */

	  for (i = 0; i < nt; ++i) {
		tr.data[i] = ct[i];
	  }

	  /* Set header values  and write centroid trace */
	  tr.ns = nt;
	  puttr(&tr);

	} while (gettr(&tr));


	return(CWP_Exit());
}
Esempio n. 26
0
int
main(int argc, char **argv)
{
	cwp_String key[SU_NKEYS];  /* array of keywords			*/
	cwp_String type[SU_NKEYS]; /* array of keywords			*/
	int index[SU_NKEYS];	/* name of type	of getparred key	*/

	int ikey;		/* key counter 				*/
	int nkeys;		/* number of header fields set		*/
	int count=0;		/* number of header fields from file	*/
	double i;		/* parameters for computing fields	*/
	int itr = 0;		/* trace counter 			*/
	Value val;		/* value of key field 			*/

	char *infile="";	/* name of input file of header values	*/
	FILE *infp=NULL;	/* pointer to input file		*/
	cwp_Bool from_file=cwp_false; /* is the data from infile?	*/

	float *afile=NULL;	/* array of "a" values from file	*/
	double *a=NULL;		/* array of "a" values			*/
	double *b=NULL;		/* array of "b" values			*/
	double *c=NULL;		/* array of "c" values			*/
	double *d=NULL;		/* array of "d" values			*/
	double *j=NULL;		/* array of "j" values			*/
	int n;			/* number of a,b,c,d,j values		*/

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

	/* Get "key" values */
	if ((nkeys=countparval("key"))!=0) {
		getparstringarray("key",key);

	} else {
		key[0]="cdp";
	}

	/* get types and indexes corresponding to the keys */
	for (ikey=0; ikey<nkeys; ++ikey) {
		type[ikey]=hdtype(key[ikey]);
		index[ikey]=getindex(key[ikey]);
	}


	/* get name of infile */
	getparstring("infile",&infile);

	/* if infile is specified get specified keys from file */
	if (*infile!='\0') {

		/* open infile */
		if((infp=efopen(infile,"r"))==NULL)
			err("cannot open infile=%s\n",infile);

		/* set from_file flag */
		from_file=cwp_true;
	}

	/* If not from file, getpar a,b,c,d,j */
	if (!from_file) { 
		/* get "a" values */
		if ((n=countparval("a"))!=0) { 
			if (n!=nkeys)
			err("number of a values not equal to number of keys");

			a=ealloc1double(n);
			getpardouble("a",a);
		} else {
			a=ealloc1double(nkeys);
			for (ikey=0; ikey<nkeys; ++ikey) a[ikey]=0.;
		}
		
		/* get "b" values */
		if ((n=countparval("b"))!=0) { 
			if (n!=nkeys)
			err("number of b values not equal to number of keys");

			b=ealloc1double(n);
			getpardouble("b",b);
		} else {
			b=ealloc1double(nkeys);
			for (ikey=0; ikey<nkeys; ++ikey) b[ikey]=0.;
		}
		
		/* get "c" values */
		if ((n=countparval("c"))!=0) { 
			if (n!=nkeys)
			err("number of c values not equal to number of keys");

			c=ealloc1double(n);
			getpardouble("c",c);
		} else {
			c=ealloc1double(nkeys);
			for (ikey=0; ikey<nkeys; ++ikey) c[ikey]=0.;
		}

		/* get "d" values */
		if ((n=countparval("d"))!=0) { 
			if (n!=nkeys)
			err("number of d values not equal to number of keys");

			d=ealloc1double(n);
			getpardouble("d",d);
		} else {
			d=ealloc1double(nkeys);
			for (ikey=0; ikey<nkeys; ++ikey) d[ikey]=0.;
		}

		/* get "j" values */
		if ((n=countparval("j"))!=0) { 
			if (n!=nkeys)
			err("number of j values not equal to number of keys");

			j=ealloc1double(n);
			getpardouble("j",j);

			/* make sure that j!=0 */
			for (ikey=0; ikey<nkeys; ++ikey)
				if(j[ikey]==0) j[ikey]=ULONG_MAX;
		} else {
			j=ealloc1double(nkeys);
			for (ikey=0; ikey<nkeys; ++ikey) j[ikey]=ULONG_MAX;
		}
	} else { /* if reading from a file */
		/* allocate space for afile */
		afile=ealloc1float(nkeys);
	}

        checkpars();

	/* loop over traces */
	while (gettr(&tr)) {

		if (from_file) {
			/* use the "a" value from file to trace by trace */
			if (efread(afile,FSIZE,nkeys,infp)!=0) {
				for (ikey=0; ikey<nkeys; ++ikey) {
					double a_in;
					a_in=(double) afile[ikey];
					setval(type[ikey],&val,a_in,
							 0,0,0,ULONG_MAX);
					puthval(&tr,index[ikey],&val);
				++count;
				}
			}
		} else { /* use getparred values of a,b,c,d,j */
			for (ikey=0; ikey<nkeys; ++ikey) {
				i = (double) itr + d[ikey];
				
				setval(type[ikey],&val,a[ikey],b[ikey],
						c[ikey],i,j[ikey]);
				puthval(&tr,index[ikey],&val);
			}

		}

		++itr;
		puttr(&tr);
	}

	if (from_file) {
		efclose(infp);
		if (count < (int)(itr*nkeys) ) {
		   warn("itr=%d > count=%d %s",(int) itr*count,count);
		   warn("n traces=%d > data count =%d",(itr*nkeys),count);
		}
	}


	return(CWP_Exit());
}
Esempio n. 27
0
int
main(int argc, char **argv)
{
	float ungpow;
	int nt;
	cwp_Bool isone, istwo;
	float f_one = 1.0;
	float f_two = 2.0;

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


	/* Get info from first trace */
	if (!gettr(&tr)) err("can't get first trace");
	if (tr.trid != CHARPACK) err("Not char packed traces");
	nt = tr.ns;
	ungpow = tr.ungpow;
	isone = CLOSETO(ungpow, f_one);
	istwo = CLOSETO(ungpow, f_two);


	/* Main loop over segy traces */
	do {
		/* Point input char trace at the trace data and unpack.
		   Since the floats take more room than the chars,
		   we load in from back end.
		
		   Note that the segy field tr.data is declared as
		   floats, so we need to invent a pointer for the
		   char array which is actually there.
		*/

		register int i;
		register float val;
		register signed char *itr = (signed char *) tr.data;

		if (istwo) {
			for (i = nt-1; i >= 0; --i) { 
				val = (float) itr[i];
				val *= tr.unscale;
				tr.data[i] = val * ABS(val);
			}
		} else if (isone) {
			for (i = nt-1; i >= 0; --i) { 
				val = (float) itr[i];
				val *= tr.unscale;
				tr.data[i] = val;
			}
		} else {
			for (i = nt-1; i >= 0; --i) { 
				val = (float) itr[i];
				val *= tr.unscale;
				tr.data[i] = (val >= 0.0) ?
					pow(val, ungpow) : -pow(-val, ungpow);
			}
		}


		/* Mark as seismic data and remove now unnecessary fields */
		tr.trid = 1;
		tr.ungpow = 0.0;
		tr.unscale = 0.0;


		/* Write out restored (unpacked) segy */
		puttr(&tr);


	} while (gettr(&tr));


	return(CWP_Exit());
}
Esempio n. 28
0
int
main(int argc, char **argv)
{
	int nx1,nx2;		/* numbers of samples			*/
	int ix1, ix2;		/* sample indices			*/
	float a1,a2;		/* filter dimensions			*/

	float pi;		/* pi number				*/
	float vmax;		/* maximum value of the data		*/
	float vfmax;		/* scale factor after filtering		*/
	float c1,c2;
	float **v=NULL;		/* array of velocities			*/
	float *k1=NULL,*k2=NULL;	/* wavenumber arrays		*/
	float *kfilt1=NULL,*kfilt2=NULL;/* intermediate filter arrays   */
	float dk1,dk2;			/* wavenumber interval		*/
	float **kfilter=NULL;		/* array of filter values	*/

	int nx1fft,nx2fft;	/* dimensions after padding for FFT	*/
	int nK1,nK2;	  /* transform dimension			*/
	int ik1,ik2;	  /* wavenumber indices				*/

	register complex **ct=NULL;	/* complex FFT workspace	*/
	register float **rt=NULL;	/* float FFT workspace		*/
	FILE *tracefp=NULL;	/* temp file to hold traces		*/
	FILE *hfp=NULL;		/* temp file to hold trace headers	*/
	
	/* hook up getpar to handle the parameters */
	initargs(argc, argv);
	requestdoc(1);

	/* Get parameters from command line */
	if (!getparfloat("a1",&a1)) a1=0.;
	if (!getparfloat("a2",&a2)) a2=0.;

	/* Get info from first trace */
	if (!gettr(&tr))  err("can't get first trace");
	if (tr.trid != TRID_DEPTH)  warn("tr.trid=%d",tr.trid);
	nx1=tr.ns;

	/* Store traces in tmpfile while getting a count */
	tracefp=etmpfile();
	hfp=etmpfile();
	nx2=0;
	do {
		++nx2;
		efwrite(&tr,HDRBYTES, 1, hfp);
		efwrite(tr.data, FSIZE, nx1, tracefp);
	} while (gettr(&tr));
	
	/* Determine number of wavenumbers in K1 and K2 */
	nx1fft=npfaro(nx1, LOOKFAC*nx1);
	nx2fft=npfa(nx2);
	if (nx1fft >=SU_NFLTS || nx1fft >= PFA_MAX)
	  err("Padded nx1=%d--too big",nx1fft);
	if (nx2fft >= PFA_MAX)
	  err("Padded nx2=%d--too big",nx2fft);

	/* Determine number of wavenumbers in K1 and K2 */
	nK1=nx1fft/2 + 1;
	nK2=nx2fft/2 + 1;

	/* Allocate space */
	v=alloc2float(nx1,nx2);
	rt=alloc2float(nx1fft,nx2fft);
	ct=alloc2complex(nK1,nx2fft);
	kfilter=alloc2float(nx1fft,nx2fft);
	k1=alloc1float(nK1);
	k2=alloc1float(nK2);
	kfilt1= alloc1float(nK1);
	kfilt2= alloc1float(nK2);

	/* Zero all arrays */
	memset((void *) rt[0], 0, nx1fft*nx2fft*FSIZE);
	memset((void *) kfilter[0], 0, nx1fft*nx2fft*FSIZE);
	memset((void *) ct[0], 0, nK1*nx2fft*sizeof(complex));
	memset((void *) k1, 0, nK1*FSIZE);
	memset((void *) k2, 0, nK2*FSIZE);
	memset((void *) kfilt1, 0,  nK1*FSIZE);
	memset((void *) kfilt2, 0, nK2*FSIZE);

	/* Determine wavenumber arrays for the filter */
	pi=PI;
	dk1=2*pi / nx1fft;
	dk2=2*pi / nx2fft;
	for (ik1=0; ik1<nK1; ++ik1) {
	  c1=a1*ik1*dk1/ 2;
	  kfilt1[ik1]= exp(-pow(c1,2));
	}
	for (ik2=0; ik2<nK2; ++ik2) {
	  c2= a2*ik2*dk2/2;
	  kfilt2[ik2]= exp(-pow(c2,2));
	}
	
	/* Build Gaussian filter */
	/* positive k1, positive k2 */
	for (ik2=0; ik2<nK2; ++ik2) {
	  for (ik1=0; ik1<nK1; ++ik1) {
		kfilter[ik2][ik1]=kfilt2[ik2]*kfilt1[ik1];
	  }
	}
	/* positive k1, negative k2 */
	for (ik2=nK2; ik2<nx2fft; ++ik2) {
	  for (ik1=0; ik1<nK1; ++ik1) {
		kfilter[ik2][ik1]=kfilt2[nx2fft-ik2]*kfilt1[ik1];
	  }
	}

	/* Read velocities from temp file and determine maximum */
	rewind(tracefp);
	fread(v[0],sizeof(float),nx2*nx1,tracefp);
	vmax=v[0][0];
	for (ix2=0; ix2<nx2; ++ix2) {
	  for (ix1=0; ix1<nx1; ++ix1) {
		vmax=MAX(vmax,v[ix2][ix1]);
	  }
	}

	/* Load data into FFT arrays */
	rewind(tracefp);
	for (ix2=0; ix2<nx2; ++ix2) {
	  efread(rt[ix2], FSIZE, nx1, tracefp);
	}   
	
	/* Fourier transform dimension 1 */
	pfa2rc(-1,1,nx1fft,nx2,rt[0],ct[0]);
	
	/* Fourier transform dimension 2 */
	pfa2cc(-1,2,nK1,nx2fft,ct[0]);

	/* Apply filter to the data */
	for (ik2=0; ik2<nx2fft; ++ik2) {
	  	for (ik1=0; ik1<nK1;  ++ik1) {
			ct[ik2][ik1]=crmul(ct[ik2][ik1], kfilter[ik2][ik1]) ;
		}
	}

	/* Inverse Fourier transformation dimension 2 */
	pfa2cc(1,2,nK1,nx2fft,ct[0]);

	/* Inverse Fourier transformation dimension 1 */
	pfa2cr(1,1,nx1fft,nx2,ct[0],rt[0]);

	/* Find maximum of filtered data */
	vfmax=rt[0][0];
	for (ix2=0; ix2<nx2; ++ix2) {
		for (ix1=0; ix1<nx1; ++ix1) {
			vfmax=MAX(vfmax,rt[ix2][ix1]);
		}
	}

	/* Rescale and output filtered data */
	erewind(hfp);
	for (ix2=0; ix2<nx2; ++ix2) {
		efread(&tr, HDRBYTES, 1, hfp);
		for (ix1=0; ix1<nx1; ++ix1)
			tr.data[ix1]=(rt[ix2][ix1]) * vmax / vfmax;
	  puttr(&tr);
	}
	efclose(hfp);

	return(CWP_Exit());
}
Esempio n. 29
0
int main (int argc, char **argv) {

   char *coeff_x, *coeff_x2, *coeff_x3, file[BUFSIZ];
   cwp_Bool active = TRUE;
	
   struct GRD_HEADER grd_x, grd_x2, grd_x3;
   struct GMT_EDGEINFO edgeinfo_x, edgeinfo_x2, edgeinfo_x3;
   struct GMT_BCR bcr_x, bcr_x2, bcr_x3;

   short  check, verbose;
   int    nz, ntr, ns;
   double value, scale_factor, dz, x_loc, y_loc;
   double weight_x, weight_x2, weight_x3;
   double value_coeff_x, value_coeff_x2, value_coeff_x3, tr_sec, dt_sec;
   float  depth_input, amp_output, *tr_amp, *depth;
   register int k, n;

   initargs(argc, argv);
   argc = GMT_begin (argc, argv);

   if (!getparstring("coeff_x", &coeff_x)) {
      fprintf ( stderr, "Must supply Coefficient_X GMT grid (COEFF_X Parameter) --> exiting\n" );
      return EXIT_FAILURE;
   }

   if (!getparstring("coeff_x2", &coeff_x2)) {
      fprintf ( stderr, "Must supply Coefficient_X2 GMT grid (COEFF_X2 Parameter)--> exiting\n" );
      return EXIT_FAILURE;
   }

   if (!getparstring("coeff_x3", &coeff_x3)) {
      fprintf ( stderr, "Must supply Coefficient_X3 GMT grid (COEFF_X3 Parameter)--> exiting\n" );
      return EXIT_FAILURE;
   }

   if (!getparshort("verbose" , &verbose)) verbose = 0;
   if (!getpardouble("weight_x", &weight_x)) weight_x  = 1.0;
   if (!getpardouble("weight_x2", &weight_x2)) weight_x2  = 1.0;
   if (!getpardouble("weight_x3", &weight_x3)) weight_x3  = 1.0;

   if ( verbose ) {
      fprintf ( stderr, "\n" );
      fprintf ( stderr, "X1 Coefficient GMT grid file name = %s\n", coeff_x );
      fprintf ( stderr, "X2 Coefficient GMT grid file name = %s\n", coeff_x2 );
      fprintf ( stderr, "X3 Coefficient GMT grid file name = %s\n", coeff_x3 );
      fprintf ( stderr, "X1 Grid Weighting Value = %f\n", weight_x );
      fprintf ( stderr, "X2 Grid Weighting Value = %f\n", weight_x2 );
      fprintf ( stderr, "X3 Grid Weighting Value = %f\n", weight_x3 );
      fprintf ( stderr, "\n" );
   }

   weight_x  = 1.0 / weight_x;
   weight_x2 = 1.0 / weight_x2;
   weight_x3 = 1.0 / weight_x3;

   GMT_boundcond_init (&edgeinfo_x);
   GMT_boundcond_init (&edgeinfo_x2);
   GMT_boundcond_init (&edgeinfo_x3);

   GMT_grd_init (&grd_x,  argc, argv, FALSE);
   GMT_grd_init (&grd_x2, argc, argv, FALSE);
   GMT_grd_init (&grd_x3, argc, argv, FALSE);

   if (GMT_read_grd_info (coeff_x,  &grd_x))  fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_x2, &grd_x2)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
   if (GMT_read_grd_info (coeff_x3, &grd_x3)) fprintf (stderr, "%s: Error opening file %s\n", GMT_program, file);
		
   f1 = (float *) GMT_memory (VNULL, (size_t)((grd_x.nx  + 4) * (grd_x.ny  + 4)), sizeof(float), GMT_program);
   f2 = (float *) GMT_memory (VNULL, (size_t)((grd_x2.nx + 4) * (grd_x2.ny + 4)), sizeof(float), GMT_program);
   f3 = (float *) GMT_memory (VNULL, (size_t)((grd_x3.nx + 4) * (grd_x3.ny + 4)), sizeof(float), GMT_program);

   GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 2;

   GMT_boundcond_param_prep (&grd_x,  &edgeinfo_x);
   GMT_boundcond_param_prep (&grd_x2, &edgeinfo_x2);
   GMT_boundcond_param_prep (&grd_x3, &edgeinfo_x3);

   GMT_boundcond_set (&grd_x,  &edgeinfo_x,  GMT_pad, f1);
   GMT_boundcond_set (&grd_x2, &edgeinfo_x2, GMT_pad, f2);
   GMT_boundcond_set (&grd_x3, &edgeinfo_x3, GMT_pad, f3);

   value = 0.0;
   GMT_bcr_init (&grd_x,  GMT_pad, active, value, &bcr_x);
   GMT_bcr_init (&grd_x2, GMT_pad, active, value, &bcr_x2);
   GMT_bcr_init (&grd_x3, GMT_pad, active, value, &bcr_x3);

   GMT_read_grd (coeff_x,  &grd_x,  f1, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_x2, &grd_x2, f2, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);
   GMT_read_grd (coeff_x3, &grd_x3, f3, 0.0, 0.0, 0.0, 0.0, GMT_pad, FALSE);

   /* Get info from first trace */
   ntr     = gettra (&tr, 0);
   ns      = tr.ns;
   dt_sec  = tr.dt * 0.000001;
   scale_factor = tr.scalco;
   if (scale_factor < 0.0 ) scale_factor *= -1.0;
   if (scale_factor == 0.0 ) scale_factor = 1.0;

   if (!getpardouble ("dz",&dz)) dz = 2.0;
   if (!getparint    ("nz",&nz)) nz = ns;

   if ( verbose ) {
      fprintf ( stderr, "Output depth sample rate = %f\n", dz );
      fprintf ( stderr, "Coordinate scale factor = %f\n", scale_factor );
      fprintf ( stderr, "Number of output depth samples per trace = %d\n", nz );
      fprintf ( stderr, "number of traces = %d, number of samples per trace = %d\n", ntr, ns );
      fprintf ( stderr, "time sample rate (seconds) = %f\n", dt_sec );
   }

   rewind (stdin);

   depth  = ealloc1float ( ns );
   tr_amp = ealloc1float ( nz );

   /* Main loop over traces */
   for ( k = 0; k < ntr; ++k ) {
      gettr (&tr);
      x_loc = tr.sx / scale_factor;
      y_loc = tr.sy / scale_factor;

      check = 0;
      if ( x_loc >= grd_x.x_min && x_loc <= grd_x.x_max && y_loc >= grd_x.y_min && y_loc <= grd_x.y_max ) check = 1;

      if ( check ) {
         value_coeff_x  = GMT_get_bcr_z (&grd_x,  x_loc, y_loc, f1, &edgeinfo_x,  &bcr_x);
         value_coeff_x2 = GMT_get_bcr_z (&grd_x2, x_loc, y_loc, f2, &edgeinfo_x2, &bcr_x2);
         value_coeff_x3 = GMT_get_bcr_z (&grd_x3, x_loc, y_loc, f3, &edgeinfo_x3, &bcr_x3);

	 if ( verbose ) fprintf ( stderr, "Trace num = %d, X-Loc = %f, Y-Loc = %f, X Coefficient = %0.10f, X2 Coefficient = %0.10f, X3 Coefficient = %0.10f\n", 
         k+1, x_loc, y_loc, value_coeff_x, value_coeff_x2, value_coeff_x3 );

	 for ( n=0; n < ns; ++n ) {
            tr_amp[n] = tr.data[n];
	    tr_sec   = n * dt_sec;
	    depth[n] = (((value_coeff_x*tr_sec)*weight_x) + ((value_coeff_x2*pow(tr_sec,2))*weight_x2) + ((value_coeff_x3*pow(tr_sec,3))*weight_x3)) * -1.0;
            if ( verbose == 2 ) fprintf ( stderr, "Trace no. = %5d, Sample = %5d, TWT (secs.) = %.4f, Depth (feet) = %.4f\n", k, n, tr_sec, depth[n] ); 
	 }
	 for ( n=0; n < nz; ++n ) {
	    depth_input = n * dz;
	    intlin ( ns, depth, tr_amp, tr_amp[0], tr_amp[ns-1], 1, &depth_input, &amp_output );
	    dtr.data[n] = amp_output; 
         }
	 dtr.tracl  = tr.tracl;
	 dtr.tracr  = tr.tracr;
	 dtr.ep     = tr.ep;
	 dtr.ns     = nz;
	 dtr.dt     = nint (dz * 1000.0);
	 dtr.sx     = tr.sx;
	 dtr.sy     = tr.sy;
	 dtr.trid   = 1;
	 dtr.fldr   = tr.fldr;
	 dtr.cdp    = tr.cdp ;
	 puttr (&dtr);
      } else {
         fprintf ( stderr, "input trace = %d, xloc = %.0f yloc = %.0f is out of bounds\n", k, x_loc, y_loc);
      }
   }

   GMT_free ((void *)f1);
   GMT_free ((void *)f2);
   GMT_free ((void *)f3);
   GMT_end  (argc, argv);

   free1float (depth);
   free1float (tr_amp);

   return (0);
}
int main(int argc, char **argv) {

int  gelev_vmin, gelev_vmax, selev_vmin, selev_vmax;
int  gelev_min, gelev_max, selev_min, selev_max;
int  gelev_globalmin, gelev_globalmax, selev_globalmin, selev_globalmax;
int  gelev_num, selev_num;
int  imin, iminv, ifirst, nz1, nz, ns, ntr, ntrv, nsv;
double dfirst, delrt, delrtv, twt, dt, dtv, factor, z, amp_out;
double *depth, *tr_amp;
float ***velocity;
short verbose;
register int i, j, k, l;
cwp_String vfile;
FILE *fv;

initargs(argc, argv);
requestdoc (0);

if (!getparshort("verbose", &verbose)) verbose = 1;
if (!getparstring("vfile",&vfile)) vfile = "output.su";

ntr    = gettra (&tr,  0);
fv     = efopen (vfile, "r");
ntrv   = fgettra (fv, &vtr, 0);
delrt  = tr.delrt;
delrtv = vtr.delrt;
if (!getparint("ns",  &ns))  ns  = tr.ns;
if (!getparint("nsv", &nsv)) nsv = vtr.ns;
if (!getparint("nz", &nz)) nz=2000;

dt    = tr.dt  * 0.001;
dtv   = vtr.dt * 0.001;
imin  = nint ( delrt / dt );
iminv = nint ( delrtv / dtv );

depth  = ealloc1double ( ns );
tr_amp = ealloc1double ( ns );

factor = 0.0005;

if ( verbose ) {
   fprintf ( stderr, "\n" );
   fprintf ( stderr, "Number of input traces = %d\n", ntr );
   fprintf ( stderr, "Number of input samples per trace = %d\n", ns );
   fprintf ( stderr, "Number of output samples per trace = %d\n", nz );
   fprintf ( stderr, "Sample rate = %f ms.\n", dt );
   fprintf ( stderr, "Average velocity SU file name = %s\n", vfile );
   fprintf ( stderr, "Number of VAVG input traces = %d\n", ntrv );
   fprintf ( stderr, "Number of VAVG input samples per trace = %d\n", nsv );
   fprintf ( stderr, "VAVG Sample rate = %f ms.\n", dtv );
   fprintf ( stderr, "Input Delay = %f ms., First sample = %d\n", delrt, imin );
   fprintf ( stderr, "Velocity Delay = %f ms., First sample = %d\n", delrtv, iminv );
   fprintf ( stderr, "\n" );
}

rewind ( fv );
gelev_vmin = selev_vmin = INT_MAX;
gelev_vmax = selev_vmax = INT_MIN;
for ( i = 0; i < ntrv; ++i ) {
   fgettr (fv, &vtr);
   gelev_vmin = min ( gelev_vmin, vtr.gelev );
   gelev_vmax = max ( gelev_vmax, vtr.gelev );
   selev_vmin = min ( selev_vmin, vtr.selev );
   selev_vmax = max ( selev_vmax, vtr.selev );
}

if ( verbose ) fprintf ( stderr, "VELOCITY DATA - gelev_vmin = %d, gelev_vmax = %d, selev_vmin = %d, selev_vmax = %d\n", gelev_vmin, gelev_vmax, selev_vmin, selev_vmax );

rewind (stdin);
gelev_min = selev_min = INT_MAX;
gelev_max = selev_max = INT_MIN;
for ( i = 0; i < ntr; ++i ) {
   gettr (&tr);
   gelev_min = min ( gelev_min, tr.gelev );
   gelev_max = max ( gelev_max, tr.gelev );
   selev_min = min ( selev_min, tr.selev );
   selev_max = max ( selev_max, tr.selev );
}

if ( verbose ) fprintf ( stderr, "INPUT DATA -    gelev_min =  %d, gelev_max =  %d, selev_min =  %d, selev_max =  %d\n", gelev_min, gelev_max, selev_min, selev_max );

gelev_globalmin = max ( gelev_min, gelev_vmin );
selev_globalmin = max ( selev_min, selev_vmin );
gelev_globalmax = min ( gelev_max, gelev_vmax );
selev_globalmax = min ( selev_max, selev_vmax );

gelev_num = gelev_globalmax - gelev_globalmin + 1;
selev_num = selev_globalmax - selev_globalmin + 1;

if ( verbose ) {
   fprintf ( stderr, "\n" );
   fprintf ( stderr, "gelev_globalmin = %d, gelev_globalmax = %d, selev_globalmin = %d, selev_globalmax = %d\n", gelev_globalmin, gelev_globalmax, selev_globalmin, selev_globalmax );
   fprintf ( stderr, "gelev_num = %d, selev_num = %d\n", gelev_num, selev_num );
}

velocity = ealloc3float ( nsv, selev_num, gelev_num );

rewind ( fv );
ifirst = 0;
dfirst = 9999999999999999.9;
for ( k = 0; k < ntrv; ++k ) {
   fgettr (fv, &vtr);
   if ( vtr.gelev >= gelev_globalmin && vtr.gelev <= gelev_globalmax && vtr.selev >= selev_globalmin && vtr.selev <= selev_globalmax ) {
      i = vtr.gelev - gelev_globalmin;
      j = vtr.selev - selev_globalmin;
      memcpy( (void *) &velocity[i][j][0], (const void *) &vtr.data, nsv * FSIZE );
      dfirst = min ( dfirst, velocity[i][j][0] * delrtv * factor );
   }
}
efclose (fv);
ifirst = nint ( dfirst / dt );
nz1 = nz - ifirst;
if ( verbose ) fprintf ( stderr, "ifirst = %d, dfirst = %f, nz1 = %d\n", ifirst, dfirst, nz1 );

float zero;
double depth_max;

zero = 0.0;

rewind (stdin);
for ( l = 0; l < ntr; ++l ) {
   gettr (&tr);
   if ( tr.gelev >= gelev_globalmin && tr.gelev <= gelev_globalmax && tr.selev >= selev_globalmin && tr.selev <= selev_globalmax ) {
      i = tr.gelev - gelev_globalmin;
      j = tr.selev - selev_globalmin;
      for ( k = 0; k < nsv; ++k ) {
         twt = ( k * dtv ) + delrtv;
         tr_amp[k] = tr.data[k+iminv];
         depth[k] = velocity[i][j][k] * twt * factor;
      }
      depth_max = depth[nsv-1];
      fprintf ( stderr, "trace number = %5d, depth_max = %f\n", l, depth_max );
      for ( k=ifirst; k < nz1; ++k ) {
         z = k * dt;
         if ( z <= depth_max ) {
            dintlin ( nsv, depth, tr_amp, tr_amp[0], tr_amp[nsv-1], 1, &z, &amp_out );
            tr.data[k-ifirst] = (float) amp_out;
         } else {
            tr.data[k-ifirst] = zero;
         }
      }
      tr.trid = 1;
      tr.ns = nsv;
      tr.delrt = nint ( dfirst );
      tr.dt = nint(dt*1000);
      puttr (&tr);
   } 
} 

free1double (tr_amp);
free1double (depth);
free3float  (velocity);

return EXIT_SUCCESS;

}