Exemple #1
0
int
main(int argc, char **argv)
{
	cwp_String key1,  key2;	/* x and y key header words	*/
	Value  val1,  val2;	/* ... their values		*/
	cwp_String type1, type2;/* ... their types		*/
	int index1, index2;	/* ... their indices in hdr.h	*/
	float x, y;		/* temps to hold current x & y 	*/
	cwp_String outpar;	/* name of par file		*/
	register int npairs;	/* number of pairs found	*/


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


	/* Prevent byte codes from spilling to screen */
	if (isatty(STDOUT)) err("must redirect or pipe binary output");


	/* Get parameters */
	if (!getparstring("key1", &key1))	key1 = "sx";
	if (!getparstring("key2", &key2))	key2 = "gx";

	type1 = hdtype(key1);
	type2 = hdtype(key2);

	index1 = getindex(key1);
	index2 = getindex(key2);


	/* Loop over traces */
	npairs = 0;
	while(gettr(&tr)) {

		gethval(&tr, index1, &val1);
		gethval(&tr, index2, &val2);

		x = vtof(type1, val1);
		y = vtof(type2, val2);

		efwrite(&x, FSIZE, 1, stdout);
		efwrite(&y, FSIZE, 1, stdout);

		++npairs;
	}


	/* Make parfile if needed */
	if (getparstring("outpar", &outpar))
		fprintf(efopen(outpar, "w"),
			"n=%d label1=%s label2=%s\n",
			npairs, key1, key2);

	return(CWP_Exit());
}
Exemple #2
0
/* Display non-null header field values */
void printheader(segy *tp)
{
	int i;			/* index over header fields		*/
	int j;			/* index over non-null header fields	*/
	Value val;		/* value in header field		*/
	String type;		/* ... its data type			*/
	String key;		/* ... the name of the header field	*/
	Value zeroval;		 /* zero value to compare with		*/

	zeroval.l = 0;
	j = 0;
	for (i = 0; i < SU_NKEYS; i++) {
		gethval(tp, i, &val);
		key = getkey(i);
		type = hdtype(key);
		if (valcmp(type, val, zeroval)) { /* not equal to zero */
			(void) printf(" %s=", key);
			printfval(type, val);
			if ((++j % 6) == 0) putchar('\n');
		}
	}
	putchar('\n');

	return;
}
Exemple #3
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;
}
Exemple #4
0
float n_distance(segy **rec_o,int *index,cwp_String *type,float *dx,unsigned int nd,unsigned int imx,unsigned int itr)
{

	float du;
	float o,x;
	int i;
	double dist=0;
	Value val;
	
	for(i=0;i<nd;i++) {
 		gethval(rec_o[imx], index[i],&val);
		o = vtof(type[i],val);
 		gethval(rec_o[itr], index[i],&val);
		x = vtof(type[i],val);
		du = (o-x)/dx[i];
		dist += du*du;
	}
	return((float)sqrt(dist));	
}
Exemple #5
0
int main(int argc, char **argv)
{
	char *plotcmd;		/* build pswigb command for popen	*/
	float *trbuf;		/* trace buffer				*/
	FILE *plotfp;		/* fp for plot data			*/
	int nt;			/* number of samples on trace	  	*/
	int ntr;		/* number of traces			*/
	int verbose;		/* verbose flag				*/
	float d1;		/* time/depth sample rate		*/
	float d2;		/* trace/dx sample rate			*/
	float f1;		/* tmin/zmin			   	*/
	float f2;		/* tracemin/xmin			*/
	cwp_Bool seismic;	/* is this seismic data?		*/
	cwp_Bool have_ntr=cwp_false;/* is ntr known from header or user?	*/
	char *tmpdir;		/* directory path for tmp files		*/
	cwp_Bool istmpdir=cwp_false;/* true for user given path		*/

	char *cwproot;		/* value of CWPROOT environment variable*/
	char *bindir;		/* directory path for tmp files		*/

	/* Support for irregularly spaced data */
	cwp_String key;		/* header key word with x2 information  */
	cwp_String type1=NULL;	/* ... its type				*/
	int index1=0;		/* ... its index			*/
	Value val;		/* value of key				*/
	Value scale;		/* Value of scaler			*/
	cwp_String type2=NULL;	/* ... its type				*/
	int index2=0;		/* ... its index			*/
	cwp_Bool isDepth=cwp_false;	/* Is this key a type of depth?		*/
	cwp_Bool isCoord=cwp_false;	/* Is this key a type of coordinate?	*/
	cwp_Bool irregular=cwp_false;  /* if true, reading x2 from header	*/ 
	cwp_String x2string;	/* string of x2 values			*/
	off_t x2len;		/* ... its length 			*/
	cwp_String style;	/* style parameter			*/


	/* Initialize */
	initargs(argc, argv);
	requestdoc(1);
	
	/* Get info from first trace */
	if (!gettr(&tr)) err("can't get first trace");
	seismic = ISSEISMIC(tr.trid); 
	nt = tr.ns;
	ntr = tr.ntr;
	if (ntr) have_ntr = cwp_true; 

	if (!getparint("verbose", &verbose))    verbose=0;
	if (!getparfloat("d1", &d1)) {
		if      (tr.d1)  d1 = tr.d1;
		else if (tr.dt)  d1 = ((double) tr.dt)/1000000.0;
		else {
			if (seismic) {
				d1 = 0.004;
				warn("tr.dt not set, assuming dt=0.004");
			} else { /* non-seismic data */
				d1 = 1.0;
				warn("tr.d1 not set, assuming d1=1.0");
			}
		}
	}


	if (!getparfloat("f1", &f1)) {
		if      (tr.f1)     f1 = tr.f1;
		else if (tr.delrt)  f1 = (float) tr.delrt/1000.0;
		else		f1 = 0.0;
	}

	/* Get or set ntr */
	if (getparint("n2", &ntr) || getparint("ntr", &ntr)) have_ntr = cwp_true;
	if (!getparfloat("d2", &d2)) d2 = (tr.d2) ? tr.d2 : 1.0;


	if (!getparfloat("f2", &f2)) {
		if	(tr.f2)	f2 = tr.f2;
		else if (tr.tracr)  f2 = (float) tr.tracr;
		else if (tr.tracl)  f2 = (float) tr.tracl;
		else if (seismic)   f2 = 1.0;
		else		    f2 = 0.0;
	}

	if (!getparstring("style", &style)) style = "seismic";
	 	
	if (getparstring("key", &key)) {
		type1 = hdtype(key);
		if ( (index1 = getindex(key)) == -1 )
			err("%s: keyword not in segy.h: '%s'", __FILE__, key);
		irregular = cwp_true;
		isDepth = IS_DEPTH(key);
		isCoord = IS_COORD(key);
		if (isDepth) {
		   index2 = getindex("scalel");
		   type2 = hdtype("scalel");
		} else if (isCoord) {
		   index2 = getindex("scalco");
		   type2 = hdtype("scalco");
		}
	}


	/* 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);

	/* See if CWPBIN environment variable is not set */
	if (!(bindir = getenv("CWPBIN"))) { /* construct bindir from CWPROOT */

		bindir = (char *) emalloc(BUFSIZ);

		/* Get value of CWPROOT environment variable */
		if (!(cwproot = getenv("CWPROOT"))) cwproot ="" ;
		if (STREQ(cwproot, "")) {
			warn("CWPROOT environment variable is not set! ");
			err("Set CWPROOT in shell environment as per instructions in CWP/SU Installation README files");
		}
		/* then bindir = $CWPROOT/bin */
		sprintf(bindir, "%s/bin", cwproot);
	}
	strcat(bindir,"/");   /* put / at the end of bindir */


	/* Allocate trace buffer */
	trbuf = ealloc1float(nt);


	if (!have_ntr || irregular ) { /* count traces */
		if (verbose) {
			if (irregular) {
				warn("trace spacing from header field %s",key);
				warn("... getting positions");

			} else {
				warn("n2 not getparred and "
				     "ntr header field not set");
				warn("....  counting traces");
			}
		}

		/* Create temporary "file" to hold data */
		if (STREQ(tmpdir,"")) {
			datafp = etmpfile();
			if (irregular) x2fp = etmpfile();
			if (verbose) warn("using tmpfile() call");
		} else { /* user-supplied tmpdir */
			char directory[BUFSIZ];
			strcpy(directory, tmpdir);
			strcpy(datafile, temporary_filename(directory));
			strcpy(x2file, temporary_filename(directory));
			/* Handle user interrupts */
			signal(SIGINT, (void (*) (int)) closefiles);
			signal(SIGQUIT, (void (*) (int)) closefiles);
			signal(SIGHUP,  (void (*) (int)) closefiles);
			signal(SIGTERM, (void (*) (int)) closefiles);
			datafp = efopen(datafile, "w+");
			if (irregular) x2fp = efopen(x2file, "w+");
			istmpdir=cwp_true;		
			if (verbose)
			      warn("putting temporary files in %s", directory);
		}

		/* Loop over input data and read to temporary file */
		ntr = 0;
		if(irregular ) {
		     float x,xmin=FLT_MAX,xmax=-FLT_MAX;

		     fprintf(x2fp,"x2=");
		     do {
			if(ntr) fprintf(x2fp,",");
			++ntr;
			gethval(&tr,index1,&val);
			if (isDepth || isCoord) {
			   gethval(&tr,index2,&scale);
			   x = (float) (vtod(type1,val) *
				 pow(10.0,vtod(type2,scale)));
			} else
			   x = vtof(type1,val);
			fprintf(x2fp,"%g",x);
			xmin = MIN(xmin,x);
			xmax = MAX(xmax,x);
			if (isDepth && STREQ(style,"vsp")) {
				int i;
				for (i = 0; i < nt; ++i) tr.data[i] *= -1.0;
			}
			efwrite(tr.data, FSIZE, nt, datafp);
		     } while (gettr(&tr));

		     /* Flip vertical axis if style = vsp */
		     if (isDepth && STREQ(style,"vsp")) {
			fprintf(x2fp," x2beg=%g x2end=%g",xmax,xmin);
			style = "normal";
		     }

		     if(xmin==xmax) {
			warn("values in header %s all equal,",key);
			warn("using f2=%f d2=%f",f2,d2);
			irregular=cwp_false;
			have_ntr=cwp_false;
			efclose(x2fp);
			if (istmpdir) eremove(x2file);
		     }

		} else {
			do {
				++ntr;
				efwrite(tr.data, FSIZE, nt, datafp);
			} while (gettr(&tr));
			/* Save naive user */
			if (STREQ(style,"vsp")) {
				style = "normal";
				warn("style=vsp requires key= to be set");
			}
		}


	}

	/* Set up pswigb command line */
	if (irregular ) {
		x2len = (off_t) eftell( x2fp );
		x2string = (char *) emalloc( ++x2len );
		rewind(x2fp);
		fread(x2string,sizeof(char),x2len,x2fp);
		plotcmd = (char *) emalloc(x2len+BUFSIZ);
		if (STREQ(style,"vsp")) {
			style = "normal";
		}
		sprintf(plotcmd, "%spswigb n1=%d d1=%f f1=%f %s style=%s", bindir,
			   nt, d1, f1, x2string, style);
		free(x2string);
	} else {
		if (STREQ(style,"vsp")) {
			style = "normal";
		}
		plotcmd = (char *) emalloc(BUFSIZ);
		sprintf(plotcmd,
			"%spswigb n1=%d n2=%d d1=%f d2=%f f1=%f f2=%f style=%s", bindir,
			   nt, ntr, d1, d2, f1, f2, style);
	}


	for (--argc, ++argv; argc; --argc, ++argv) {
		if (strncmp(*argv, "d1=", 3) && /* skip those already set */
		    strncmp(*argv, "d2=", 3) &&
		    strncmp(*argv, "f1=", 3) &&
		    strncmp(*argv, "f2=", 3) &&
		    strncmp(*argv, "style=", 6)){
		    
			strcat(plotcmd, " ");   /* put a space between args */
			strcat(plotcmd, "\"");  /* user quotes are stripped */
			strcat(plotcmd, *argv); /* add the arg */
			strcat(plotcmd, "\"");  /* user quotes are stripped */
		}
	}


	/* Open pipe to pswigb and send the traces */
	plotfp = epopen(plotcmd, "w");
	free(plotcmd);

	if (!have_ntr || irregular) { /* send out stored traces one by one */
		rewind(datafp);
		{ register int itr;
			for (itr = 0; itr < ntr; ++itr) {
				efread (trbuf, FSIZE, nt, datafp);
				efwrite(trbuf, FSIZE, nt, plotfp);
			}
		}
	} else { /* just pump out traces and let pswigb do the work */
		do {
			efwrite(tr.data, FSIZE, nt, plotfp);
		} while (gettr(&tr));
	}


	/* Clean up */
	epclose(plotfp);
	if (!have_ntr) {
		efclose(datafp);
		if (istmpdir) eremove(datafile);
	}
	if (irregular) {
		efclose(x2fp);
		if (istmpdir) eremove(x2file);
	}

	return EXIT_SUCCESS;
}
Exemple #6
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());
}
Exemple #7
0
int
main(int argc, char **argv)
{
	
	float **data;
        char **hdrdata;
	float ***workm;
	
        int ival2;              /* int value of key2                */
        int ival3;              /* int value of key3                */
        Value val2;      	/* ... its value                        */
        Value val3;      	/* ... its value                        */
	int index2;
	int index3;
	
	int n1,n2,n3;
	int i1,i2,i3;
        char *key2=NULL;      /* header key word from segy.h    */
        char *type2=NULL;     /* ... its type                   */
        char *key3=NULL;      /* header key word from segy.h    */
        char *type3=NULL;     /* ... its type                   */
	
	int lins;
	int line;
	int nl;
	int dir=2;
	int il;
	int su;
	float cdp;
	float t;
	float dt;
	
        /* Initialize */
        initargs(argc, argv);
        requestdoc(1);
       	
       MUSTGETPARINT("n2", &n2);
       MUSTGETPARINT("n3", &n3);
       
       /* get key2*/
        if (!getparstring("key2", &key2))  key2 = "fldr"; 
        type2 = hdtype(key2);
        index2 = getindex(key2);
	
       /* get key3*/
        if (!getparstring("key3", &key3))  key3 = "tracf"; 
        type3 = hdtype(key3);
        index3 = getindex(key3);

        if (!getparint("lins", &lins))  lins = 1;
	if(lins<1) err(" lins must be larger than 0");
        if (!getparint("line", &line)) line = n2; 
         if (!getparint("su", &su)) su = 1; 
      
	/* Get info from first trace */
        if (!gettr(&tr))  err ("can't get first trace");
        n1 = tr.ns;
	
	if (!getparfloat("dt", &dt))	dt = ((float) tr.dt)/1000000.0;
	if (!dt) {
		dt = .01;
		warn("dt not set, assumed to be .01");
	}



	data = bmalloc(n1*sizeof(float),n2,n3);
		
	hdrdata = bmalloc(HDRBYTES,n2,n3);
	
        do {
		gethval(&tr, index2, &val2);
		ival2 = vtoi(type2,val2);
		gethval(&tr, index3, &val3);
		ival3 = vtoi(type3,val3);

		if(ival2>n2 || ival2<0 ) err(" Array in dimension 2 out of bound\n");
		if(ival3>n3 || ival3<0 ) err(" Array in dimension 3 out of bound\n");
		
		
      		bmwrite(data,1,ival2-1,ival3-1,1,tr.data);
		bmwrite(hdrdata,1,ival2-1,ival3-1,1,&tr);
		
	} while (gettr(&tr));
	
	
	
	nl=line-lins+1;
	if(dir==2) {
		/* n2 direction */
		workm = alloc3float(n1,n3,nl);
		for(il=lins-1;il<nl;il++) {
			for(i3=0;i3<n3;i3++) bmread(data,1,lins+il-1,i3,1,workm[il][i3]);
		}
		
		if(su==1) {	
			fprintf(stdout,"cdp=");
			for(il=lins-1;il<nl;il++) {
				for(i3=0;i3<n3;i3++) fprintf(stdout,"%d,",(lins+il)*1000+i3+1);
			}
		
			for(il=lins-1;il<nl;il++) {
				for(i3=0;i3<n3;i3++){
					fprintf(stdout,"\ntnmo=");
					for(i1=0;i1<n1;i1++) fprintf(stdout,"%.3f,",dt*i1);
					fprintf(stdout,"\nvnmo=");
					for(i1=0;i1<n1;i1++)
						fprintf(stdout,"%.3f,",workm[il][i3][i1]);
				}
		
			}
		} else {
			for(il=lins-1;il<nl;il++) {
				for(i3=0;i3<n3;i3++) {
					 cdp=(lins+il)*1000+i3+1;
					 fwrite(&cdp,sizeof(float),1,stdout);
				}
			}
		
			for(il=lins-1;il<nl;il++) {
				for(i3=0;i3<n3;i3++){
					for(i1=0;i1<n1;i1++) {
						t=tr.d1*i1;
						fwrite(&t,sizeof(float),1,stdout);
					}
					for(i1=0;i1<n1;i1++)
						fwrite(&workm[il][i3][i1],sizeof(float),1,stdout);
				}
		
			}
		}
			free3float(workm);	
	}
	
	bmfree(data);
	bmfree(hdrdata);	
	return EXIT_SUCCESS;
}
Exemple #8
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());
}
Exemple #9
0
int readGather ( FILE* Fp ,segy** Gather ,int* MaxFold ,char* Header ){

   Value currentHeaderValue;
   Value previousHeaderValue;

   int i;
   int index;

   segy* gather = *Gather;

   static int first=1;
   static segy savedTrace;

   index = getindex(Header);

   /*---------------*/
   /* check for EOF */
   /*---------------*/

   if( feof( Fp ) ){
      return( 0 );
   }

   /*------------------*/
   /* copy saved trace */
   /*------------------*/

   if( !first ){
      memcpy( (char*) &(gather[0]) ,(char*)&savedTrace ,sizeof(segy ) );
   }else{
      first=0;
   }

   /*--------------------------------*/
   /* read data into existing buffer */
   /*--------------------------------*/

   
   gethval(&(gather[0]), getindex(Header) , &previousHeaderValue );

   for( i=1; i<*MaxFold; i++ ){
 
      if( !fgettr( Fp ,&(gather[i]) ) ){
         return( i );
      }

         gethval(&(gather[i]), index , &currentHeaderValue );
         if( currentHeaderValue.i != previousHeaderValue.i ){
         memcpy( (char*) &savedTrace ,(char*) &gather[i] ,sizeof(segy ) );
         return( i );
      }

   }

   /*-------------------------------------------------------------*/
   /* allocate additional space until entire gather has been read */
   /*-------------------------------------------------------------*/

   while( 1 ){

      *MaxFold += 10;

      if( !(gather = realloc( gather ,*MaxFold*sizeof(segy))) ){
         err( "realloc failed: %s %d" ,__FILE__ ,__LINE__ );
      }

      *Gather = gather;

      for( i=*MaxFold-10; i<*MaxFold; i++ ){ 
   
         if( !fgettr( Fp ,&(gather[i]) ) ){
            return( i );
         }
   
         gethval(&(gather[i]), index , &currentHeaderValue );
         if( currentHeaderValue.i != previousHeaderValue.i ){
            memcpy( (char*) &savedTrace ,(char*) &gather[i] ,sizeof(segy ) );

            return( i );
         }
   
      }    

   }

}
Exemple #10
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 twfval;		/* ... its value cast to float		*/
	float *xmute=NULL;	/* array of key mute curve values	*/
	float *tmute=NULL;	/* ...		mute curve time values 	*/
	float *twindow=NULL;	/* ...	mute window time values mode=4	*/
	float linvel;		/* linear velocity			*/
	float tm0;		/* time shift of mute=2 or 3 for 'key'=0*/
	float *taper=NULL;	/* ...		taper values		*/
	int nxmute=0;		/* number of key mute values		*/
	int ntmute;	/* ...		mute time values 	*/
	int ntwindow;	/* ...		mute time values 	*/
	int ntaper;	/* ...		taper values		*/

	int below;	/* mute below curve			*/
	int mode;	/* kind of mute (top, bottom, linear)	*/
	int absolute;    /* Take absolute value of key for mode=2 */
	int hmute=0;	/* read mute times from header		*/

	int nxtmute;	/* number of mute values 		*/
	cwp_String xfile="";	/* file containing positions by key	*/
	FILE *xfilep;		/* ... its file pointer			*/
	cwp_String tfile="";	/* file containing times	 	*/
	FILE *tfilep;		/* ... its file pointer			*/

	cwp_String twfile="";	/* file containing mute time windows 	*/
	FILE *twfilep;		/* ... its file pointer			*/

	cwp_Bool seismic;	/* cwp_true if seismic, cwp_false not seismic */

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


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

	if (getparstring("hmute", &key)) { hmute = 1; } else

	if (!(getparstring("xfile",&xfile) && getparstring("tfile",&tfile))) {
		if (!(nxmute = countparval("xmute")))
			err("must give xmute= vector");
		if (!(ntmute = countparval("tmute")))
			err("must give tmute= vector");
		if (nxmute != ntmute)
			err("lengths of xmute, tmute must be the same");
		xmute = ealloc1float(nxmute);	getparfloat("xmute", xmute);
		tmute = ealloc1float(nxmute);	getparfloat("tmute", tmute);
		if (mode==4) {
			if (!(ntwindow = countparval("twindow")))
				err("must give twindow= vector");
			if (nxmute != ntwindow)
				err("lengths of xmute, twindow must be the same");
			twindow = ealloc1float(nxmute);
			getparfloat("twindow", twindow);
		}
	} else {
		MUSTGETPARINT("nmute",&nxtmute);
		nxmute = nxtmute;
		xmute = ealloc1float(nxtmute);
		tmute = ealloc1float(nxtmute);

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

		if((tfilep=fopen(tfile,"r"))==NULL)
			err("cannot open tfile=%s\n",tfile);
		if (fread(tmute,sizeof(float),nxtmute,tfilep)!=nxtmute)
			err("error reading tfile=%s\n",tfile);
		fclose(tfilep);

		if (mode==4) {
			if((twfilep=fopen(twfile,"r"))==NULL)
				err("cannot open tfile=%s\n",twfile);
			if (fread(twindow,sizeof(float),nxtmute,twfilep)!=nxtmute)
				err("error reading tfile=%s\n",tfile);
			fclose(twfilep);
		}
	}

	if (!getparint("ntaper", &ntaper))	ntaper = 0;
	if (getparint("below", &below))	{
		mode = below; 
		warn ("use of below parameter is obsolete. mode value set to %d \n", mode);
	}
	if (!getparint("absolute", &absolute))		absolute = 1;
	if (hmute==0) if (!getparstring("key", &key))	key = "offset";
	if (!getparfloat("linvel", &linvel))	linvel = 330;
	if (!getparfloat("tm0", &tm0))		tm0 = 0;
        checkpars();
	if (linvel==0) err ("linear velocity can't be 0");

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

	/* Set up taper weights if tapering requested */
	if (ntaper) {
		register int k;
		taper = ealloc1float(ntaper);
		for (k = 0; k < ntaper; ++k) {
			float s = sin((k+1)*PI/(2*ntaper));
			taper[k] = s*s;
		}
	}

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

	seismic = ISSEISMIC(tr.trid);

	if (seismic) {
		if (!tr.dt) err("dt header field must be set");
	} else if (tr.trid==TRID_DEPTH) {   /* depth section */
		if (!tr.d1) err("d1 header field must be set");
	} else {
		err ("tr.trid = %d, unsupported trace id",tr.trid);
	}

	/* Loop over traces */
	do {
		int nt     = (int) tr.ns;
		float tmin = tr.delrt/1000.0;
		float dt = ((double) tr.dt)/1000000.0;
		float t;
		float tw;
		int nmute;
		int itaper;
		int topmute;
		int botmute;
		int ntair=0;
		register int i;

		if (!seismic) { 
			tmin = 0.0;
			dt = tr.d1;	
		}

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

		if (hmute==1) {
			t = fval/1000.;
		} else {
		/* linearly interpolate between (xmute,tmute) values */
		intlin(nxmute,xmute,tmute,tmin,tmute[nxmute-1],1,&fval,&t); 
		}

		if (absolute) fval = abs(fval);
		/* do the mute */
		if (mode==0) {	/* mute above */
			nmute = MIN(NINT((t - tmin)/dt),nt);
			if (nmute>0) memset( (void *) tr.data, 0, nmute*FSIZE);
			for (i = 0; i < ntaper; ++i)
				if (i+nmute>0) tr.data[i+nmute] *= taper[i];
			if (seismic) {
				tr.muts = NINT(t*1000);
			} else	{
				tr.muts = NINT(t);
			}
		} else if (mode==1){	/* mute below */
			nmute = MAX(0,NINT((tmin + nt*dt - t)/dt));
			memset( (void *) (tr.data+nt-nmute), 0, nmute*FSIZE);
			for (i = 0; i < ntaper; ++i)
				if (nt>nmute+i && nmute+i>0)
					tr.data[nt-nmute-1-i] *= taper[i];
			if (seismic) {
				tr.mute = NINT(t*1000);
			} else	{
				tr.mute = NINT(t);
			}
		} else if (mode==2){	/* air wave mute */
			nmute = NINT((tmin+t)/dt);
			ntair=NINT(tm0/dt+fval/linvel/dt);
			topmute=MIN(MAX(0,ntair-nmute/2),nt);
			botmute=MIN(nt,ntair+nmute/2);
			memset( (void *) (tr.data+topmute), 0,
					(botmute-topmute)*FSIZE);
			for (i = 0; i < ntaper; ++i){
				itaper=ntair-nmute/2-i;
				if (itaper > 0) tr.data[itaper] *=taper[i];
			}	
			for (i = 0; i < ntaper; ++i){
				itaper=ntair+nmute/2+i;
				if (itaper<nt) tr.data[itaper] *=taper[i];
			}
		} else if (mode==3) {	/* hyperbolic mute */
			nmute = NINT((tmin + t)/dt);
			ntair=NINT(sqrt( SQ((float)(tm0/dt))+SQ((float)(fval/linvel/dt)) ));
			topmute=MIN(MAX(0,ntair-nmute/2),nt);
			botmute=MIN(nt,ntair+nmute/2);
			memset( (void *) (tr.data+topmute), 0,
					(botmute-topmute)*FSIZE);
			for (i = 0; i < ntaper; ++i){
				itaper=ntair-nmute/2-i;
				if (itaper > 0) tr.data[itaper] *=taper[i];
			}	
			for (i = 0; i < ntaper; ++i){
				itaper=ntair+nmute/2+i;
				if (itaper<nt) tr.data[itaper] *=taper[i];
			}
		} else if (mode==4) {	/* polygonal mute */
			tmin=twindow[0];
			intlin(nxmute,xmute,twindow,tmin,twindow[nxmute-1],1,&twfval,&tw); 
			if (absolute) twfval = abs(twfval);

			nmute = NINT(tw/dt);
			ntair = NINT(t/dt);

			topmute=MIN(MAX(0,ntair-nmute/2),nt);
			botmute=MIN(nt,ntair+nmute/2);
			memset( (void *) (tr.data+topmute), 0,
					(botmute-topmute)*FSIZE);
			for (i = 0;i < ntaper; ++i){
				itaper=ntair-nmute/2-i;
				if (itaper > 0) tr.data[itaper] *=taper[i];
			}	
			for (i = 0; i < ntaper; ++i){
				itaper=ntair+nmute/2+i;
				if (itaper<nt) tr.data[itaper] *=taper[i];
			}


		}
		puttr(&tr);
	} while (gettr(&tr));
	
	return(CWP_Exit());
}
Exemple #11
0
int
main(int argc, char **argv)
{
	cwp_String key[SU_NKEYS];	/* array of keywords		*/
	cwp_String type[SU_NKEYS];	/* array of types for key	*/
	int index[SU_NKEYS];		/* array of indexes for key	*/
	int ikey;		/* key counter				*/
	int nkeys;		/* number of header fields set		*/
	int n;			/* number of min,max values   		*/
	Value val;		/* value of key field			*/
	double fval;		/* value of key field			*/
	float *min=NULL;	/* array of "min" values		*/
	float *max=NULL;	/* array of "max" 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 "min" values */
	if ((n=countparval("min"))!=0) { 
		if (n!=nkeys)
		err("number of a values not equal to number of keys");
		min=ealloc1float(n);
		getparfloat("min",min);
	} else {
		min=ealloc1float(nkeys);
		for (ikey=0; ikey<nkeys; ++ikey) min[ikey]=0.;
	}

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

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

	while (gettr(&tr)) {
		for (ikey=0; ikey<nkeys; ++ikey) {
			gethval(&tr, index[ikey], &val);
			fval = vtof(type[ikey], val);
			if (fval < min[ikey]) {
				changeval(type[ikey], &val, min[ikey]);
				puthval(&tr, index[ikey], &val);
			} else if (fval > max[ikey]) {
				changeval(type[ikey], &val, max[ikey]);
				puthval(&tr, index[ikey], &val);
			}
		}
		puttr(&tr);
	}

	return(CWP_Exit());
}
Exemple #12
0
int
main(int argc, char **argv)
{
	int cdpindex;	/* index of cdp header word		*/
	int offindex;	/* index of offset header word		*/
	int nt;		/* number of data points on trace	*/
	int nsegy;	/* number of bytes in the segy		*/
	Value cdpval;	/* value of cdp in current gather	*/
	Value offval;	/*  ... same for offset			*/
	Value cdpvalnew;/* value of cdp in trace being treated	*/
	Value offvalnew;/* ... same for offset			*/
	int newtracl;	/* tracl for stacked traces		*/
	int fold;	/* number of traces with same offset 	*/
	float ffold;	/* ... cast to float			*/
	int norm;	/* norm=1 => divide by fold		*/
	int itmp;	/* temporary for swap of sx, gx keys	*/


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


	/* Set parameter */
	if (!getparint("norm", &norm))	norm = 1;

	/* Get indices */
	cdpindex = getindex("cdp");
	offindex = getindex("offset");

	/* Set up for first trace */
	nsegy = gettr(&intrace);
	nt = intrace.ns;
	gethval(&intrace, cdpindex, &cdpval);
	gethval(&intrace, offindex, &offval);
	memcpy( (void *) &outtrace, (const void *) &intrace, nsegy);

	newtracl = 1;		/* Global initialization */
	fold = 1;		/* Will be re-initialized for each gather */

	/* Loop over traces */
	while (nsegy) {		     /* While previous trace non-empty */
		nsegy = gettr(&intrace);
		gethval(&intrace, cdpindex, &cdpvalnew);
		gethval(&intrace, offindex, &offvalnew);
		if (valcmp("l", cdpval, cdpvalnew) || !nsegy) {	
			/* Either cdpval and cdpvalnew differ,       */
			/* indicating a new gather or nsegy is zero, */
			/* indicating the end of the traces.         */

			/* Add header info and output leftover stack */
			outtrace.nhs = fold;
			outtrace.tracl = newtracl++;
			if (outtrace.sx > outtrace.gx) {
				itmp = outtrace.sx;
				outtrace.sx = outtrace.gx;
				outtrace.gx = itmp;
			}
			if (norm) {
				ffold = (float) fold;
				if (fold != 1) {
				    register int i;
				    for (i = 0; i < nt; ++i)
					outtrace.data[i] /= ffold;
				}
			}
			puttr(&outtrace);
	
			/* Set up for next gather */
			memcpy( (void *) &outtrace,
					(const void *) &intrace, nsegy);
			fold = 1;
			cdpval = cdpvalnew;
			offval = offvalnew;

		} else {	/* still in same cdp gather */
			if (valcmp("l", offval, offvalnew)) {
				/* offval and offvalnew differ */

				/* Add header info and output stack */
				outtrace.nhs = fold;
				outtrace.tracl = newtracl++;
				if (outtrace.sx > outtrace.gx) {
					itmp = outtrace.sx;
					outtrace.sx = outtrace.gx;
					outtrace.gx = itmp;
				}
				if (norm) {
					ffold = (float) fold;
					if (fold != 1) {
					    register int i;
					    for (i = 0; i < nt; ++i)
						outtrace.data[i] /= ffold;
					}
				}
				puttr(&outtrace);

				/* Set up for next offset */
				memcpy( (void *) &outtrace,
				       (const void *) &intrace, nsegy);
				fold = 1;
				offval = offvalnew;

			} else { /* same offset within this cdp */

				register int i;
				for (i = 0; i < nt; ++i)
					outtrace.data[i] += intrace.data[i];

				fold++;
			}
		}
	}


	return(CWP_Exit());
}
Exemple #13
0
main(int argc, char **argv)
{
    	int nt,nxi,nxo,ntfft,it,ix,nf,i,extrap;
	int nxipre;
    	float dt,fmin,fmax;
    	float *xi, *xo, *xipre;
    	float *yi, *yo;
    	char *hdrs;
    	float tol;
    	int niter, np;
    	string datain, dataout;
	complex *ccexp, *ccexpo;
	int iccexp=1, iccexpo=1;
	int nxip, ifmin, lftwk, npp, nph;
	float tmp, tmp2;
	float df; 

    	FILE *infp,*outfp;

	segytrace tr;

	String pkey="tracl", ptype, skey="tracr", stype;
     	Value pval, sval;
       	int indxp, indxs;

	int ofill=0, dfill=1, nfill=1;
	int iof=1, inf=1;
	int nsmax, ns; 
	int is, ip, ipre;


	float *sort;
	int *sortindex;
	char *hdrsort;



    	/* get parameters */
    	initargs(argc,argv);
    	askdoc(1);

	getparstring("pkey",&pkey);
	getparstring("skey",&skey);
	if(!getparint("ofill",&ofill)) iof = 0 ;
        if(!getparint("dfill",&dfill)) dfill = 1;
       	if(!getparint("nfill",&nfill))inf = 0;
     	if (!getparint("nsmax",&nsmax)) nsmax = 2000;

	/* open input/output */
    	if (!getparstring("datain",&datain)) {
		infp = stdin;
	} else {
		infp = fopen(datain,"r");
	}
    	if (!getparstring("dataout",&dataout)) {
		outfp = stdout;
	} else {
		outfp = fopen(dataout,"w");
	}
	/* make file size to be able to exceed 2 G on convex */
	file2g(infp);
	file2g(outfp);

	/* read in first trace for nt and dt */
        if (!fgettr(infp,&tr))  err("can't get first trace");
	nt = tr.ns; 
	dt = (float)tr.dt/1000000.;

	/* optional parameters */
    	if (!getparint("ntfft",&ntfft)) ntfft=(nt*3/2)/2*2;
    	if (ntfft < nt) ntfft = nt;
	nf = ntfft;
	radix_(&nf,&ntfft);
    	if (!getparfloat("fmin",&fmin)) fmin = 0.;
    	if (!getparfloat("fmax",&fmax)) fmax = .5 / dt * 2. / 3.;
    	if (!getparint("niter",&niter)) niter = 10; 
    	if (!getparfloat("tol",&tol)) tol = 0.000001;
    	if (!getparint("extrap",&extrap)) extrap = 1; 


        ptype  = hdtype(pkey);
	indxp = getindex(pkey);
	stype  = hdtype(skey);
	indxs = getindex(skey);
	gethval(&tr, indxp, &pval);
	ipre = vtoi(ptype,pval);
	gethval(&tr, indxs, &sval);
	is = vtoi(stype,sval);

	nxo = nfill;
	if(inf==0) nxo = nsmax;

    	xi = (float*)malloc(nsmax*sizeof(float));
    	xipre = (float*)malloc(nsmax*sizeof(float));
    	yi = (float*)malloc(nt*nsmax*sizeof(float));
    	xo = (float*)malloc(nxo*sizeof(float));
    	yo = (float*)malloc(nt*nxo*sizeof(float));
	hdrs = (char*) malloc(nsmax*HDRBYTES*sizeof(char));

	sort = (float*)malloc(nsmax*nt*sizeof(float));
	hdrsort = (char*)malloc(nsmax*HDRBYTES);
	sortindex = (int*)malloc(nsmax*sizeof(int));

	iccexp = 1;
	nxip = nsmax + 10;
    	initpf_(&ntfft,&dt,&fmin,&fmax,&nxip,&ifmin,&df,
		&nf,&lftwk,&npp,&nph);
	tmp = (nph*2+1)*nf*nxip;
	tmp = tmp*sizeof(complex);

	if( tmp > 1600000000. ) {
		fprintf(stderr," --- forward t-p coefficients --- " );
		fprintf(stderr," need memory %f MB \n",tmp/1000000.);
		fprintf(stderr," memory limit exceeded \n");
		fprintf(stderr," no pre-computation of coefficients \n");
		iccexp = -1; 
		ccexp = (complex*) malloc(sizeof(complex));
		tmp = 0.;
	} else {
		ccexp = (complex*) malloc((nph*2+1)*nf*nxip*sizeof(complex));
	}

	iccexpo = 1;
	tmp2 = (nph*2+1)*nf*nxo;
	tmp2 = tmp2*sizeof(complex);
	tmp = tmp + tmp2;
	if( tmp > 1600000000. ) {
		fprintf(stderr," --- inverse t-p coefficients --- " );
		fprintf(stderr," need memory %f MB \n",tmp/1000000.);
		fprintf(stderr," memory limit exceeded \n");
		fprintf(stderr," no pre-computation of coefficients \n");
		iccexpo = -1; 
		ccexpo = (complex*) malloc(sizeof(complex));
	} else {
		ccexpo = (complex*) malloc((nph*2+1)*nf*nxo*sizeof(complex));
	}

   	for(ix=0;ix<nxo;ix++) xo[ix] = ofill + ix*dfill;

	/* loop over input traces */
	ns = 0;
	nxipre = 0;

	do {

	
                gethval(&tr, indxp, &pval);
		ip = vtoi(ptype,pval);
		gethval(&tr, indxs, &sval);
		is = vtoi(stype,sval);

		if(ns>nsmax) 
			err("maximum number traces %d exceed %d \n",ns,nsmax);

		if(ip==ipre) {

			for(it=0;it<nt;it++) yi[it+ns*nt] = tr.data[it];
			xi[ns] = is;
			bcopy((char*)&tr,hdrs+ns*HDRBYTES,HDRBYTES);
			ns = ns + 1;

		} else if(ip!=ipre && ns>0) {

			nxi = ns;
			/* sort xi into ascending order */
			for(i=0;i<nxi;i++) sortindex[i] = i;
			qkisort(nxi,xi,sortindex);
			bcopy(yi,sort,nxi*nt*sizeof(float));

			for(i=0;i<nxi;i++) {
               			bcopy(sort+sortindex[i]*nt,yi+i*nt,
					nt*sizeof(float));
			}

			bcopy(xi,sort,nxi*sizeof(float));
			for(i=0;i<nxi;i++) {
               			xi[i] = sort[sortindex[i]];
			}

			bcopy(hdrs,hdrsort,nxi*HDRBYTES);
			for(i=0;i<nxi;i++) {
			    bcopy(hdrsort+sortindex[i]*HDRBYTES,
				hdrs+i*HDRBYTES,HDRBYTES);
			}

			np = nxi;

			if(inf==0) nxo = nxi;
			if(iof==0) {
				for(ix=0;ix<nxo;ix++) xo[ix] = xi[ix];
			}

			/* tau-p interpolation */
			intps(xi,yi,xo,yo,nxi,nxo,
				nt,dt,np,fmin,fmax,ntfft, 
				niter,tol,xipre,nxipre,ccexp,iccexp,
				ccexpo,iccexpo);
			for(ix=0;ix<nxi;ix++) {
				xipre[ix] = xi[ix];
			}
			nxipre = nxi;


			/* output gather */
			intout(xi,xo,yo,hdrs,nxi,nxo,nt,
				outfp,extrap,stype,indxs);

			fprintf(stderr,
" interpolation done from input %d live traces to output %d traces at %s=%d \n",nxi, nxo, pkey, ipre);

			ns = 0;
			for(it=0;it<nt;it++) yi[it+ns*nt] = tr.data[it];
			xi[ns] = is;
			bcopy((char*)&tr,hdrs+ns*HDRBYTES,HDRBYTES);

			ipre = ip;
			ns = ns + 1;
		}
			
	} while(fgettr(infp,&tr)); 
			

	if (ns>0) {

		nxi = ns;

		for(i=0;i<nxi;i++) sortindex[i] = i;
		qkisort(nxi,xi,sortindex);
		bcopy(yi,sort,nxi*nt*sizeof(float));

		for(i=0;i<nxi;i++) {
       			bcopy(sort+sortindex[i]*nt,yi+i*nt,
			nt*sizeof(float));
		}
		bcopy(xi,sort,nxi*sizeof(float));
		for(i=0;i<nxi;i++) {
               		xi[i] = sort[sortindex[i]];
		}
		bcopy(hdrs,hdrsort,nxi*HDRBYTES);
		for(i=0;i<nxi;i++) {
		    bcopy(hdrsort+sortindex[i]*HDRBYTES,
			hdrs+i*HDRBYTES,HDRBYTES);
		}

		np = nxi;

		if(inf==0) nxo = nxi;
		if(iof==0) {
			for(ix=0;ix<nxo;ix++) xo[ix] = xi[ix];
		}

		/* interpolation */
                intps(xi,yi,xo,yo,nxi,nxo,nt,dt,np,fmin,fmax,ntfft,niter,tol,
			xipre,nxipre,ccexp,iccexp,
			ccexpo,iccexpo);
		/* output gather */
		intout(xi,xo,yo,hdrs,nxi,nxo,nt,outfp,extrap,stype,indxs);

		fprintf(stderr,
" interpolation done from input %d live traces to output %d traces at %s=%d \n",nxi, nxo, pkey, ip);

	}


    	free(xi);
    	free(yi);
    	free(xo);
    	free(yo);
	free(hdrs);
	free(sort);
	free(sortindex);
	free(hdrs);

	return 0;

}
Exemple #14
0
int
main(int argc, char **argv)
{
	cwp_String key;	/* header key word from segy.h		*/
	cwp_String type;/* ... its type				*/
	int index;	/* ... its index			*/
	int nsegy;	/* number of bytes in the segy		*/
	Value val;	/* value of key in current gather	*/
	Value valnew;	/* value of key in trace being treated	*/
	int verbose;	/* verbose flag				*/
	int val_i;	/* key value as an integer		*/

	int ntr=0;	/* count of number of traces in an ensemble */
	int numlength;	/* length of numerical part of filenames */
	
	FILE *tmpfp=NULL;		/* file pointer			*/
	FILE *outfp=NULL;	/* file pointer			*/
	cwp_String dir;		/* directory name		*/
	cwp_String suffix;	/* suffix of output files	*/

	char directory[BUFSIZ];		/* storage for directory name	*/
 	char tempfilename[BUFSIZ];	/* 	...temporary filename	*/
	char middle[BUFSIZ];		/*      ...middle of final filename */
	char stem[BUFSIZ];		/*      ...stem of final filename */
 	char format[BUFSIZ];		/* 	...format of numeric part */
	char outfile[BUFSIZ];		/*      ...final filename	*/

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


	/* Get parameters */
	MUSTGETPARSTRING("dir", &dir);
	if (!getparint   ("verbose", &verbose))	 	verbose = 0;
	if (!getparstring("key", &key))		 	key = "ep";
	if (!getparstring("suffix", &suffix))		suffix = ".hsu";
	if (!getparint("numlength", &numlength)) 	numlength=7;

	/* Initialize */
	/* tempfilename = ealloc1(strlen(tmplt)+3,sizeof(char)); */
	type = hdtype(key);
	index = getindex(key);

	/* Set up for first trace (must compare new key field each time) */
	nsegy = gettr(&intrace);
	
	/* Create temporary filename for the first gather */
	strcpy(directory, dir);
	strcpy(tempfilename, temporary_filename(directory));
	if((tmpfp = fopen(tempfilename,"w+")) == NULL)
		err("Cannot open file %s\n",tempfilename);

	if (verbose) warn(" temporary filename = %s", tempfilename);

	/* get key header value */
	gethval(&intrace, index, &val);
	
	ntr=0;
	do {

		++ntr;
		/* Get header value */				 
		gethval(&intrace, index, &valnew);

		/* compare past header value to current value */
		if (valcmp(type, val, valnew) || !nsegy) {
		 /* Either val and valnew differ, indicating a  */
		 /* new gather or nsegy is zero, indicating the */
		 /* end of the traces.			  */
			
			/* capture key field value */
			/* and build output filename */
			val_i=vtoi(type,val);

			/* zero out name parts */
			strcpy(outfile,"");
			strcpy(middle,"");
			strcpy(stem,"");

			/* build output name parts */
			strcat(middle,directory);
			strcat(middle,"/");
			strcat(stem,key);
			strcat(stem,"=");

			/* format for numeric part */
			(void)sprintf(format, "%%s%%s%%0%dd%%s",numlength);
		
			/* build name of output file */
			(void)sprintf(outfile, format,middle,stem,
					val_i, suffix);

			if (verbose) warn(" outfile = %s", outfile);

			/* rewind file */
			rewind(tmpfp);

			/* open the new file */
			if((outfp = fopen(outfile,"w+")) == NULL)
				err("Cannot open file %s\n",outfile);
			/* loop over traces setting ntr field */
			/*  and write to finalfile */	
			while(fgettr(tmpfp,&tmptr))  {
				tmptr.ntr = ntr;
				fputtr(outfp,&tmptr);
			}
			/* Close files */
			fclose(tmpfp);
			fclose(outfp);
			remove(tempfilename);

			if (verbose) warn("val= %i", val_i);

			/* Set up for next gather */
			/* create new tempfname first */
			strcpy(tempfilename, temporary_filename(directory));
			
			/* open filename */
			if((tmpfp = fopen(tempfilename,"w+")) == NULL)
				err("Cannot open file %s\n",tempfilename);
			val = valnew;
			ntr=0;
		}
		fputtr(tmpfp,&intrace);
	} while(gettr(&intrace));
	
	/* Close file */
	rewind(tmpfp);
	val_i=vtoi(type,val);
	
	/* Move last gather into new location */
	/* capture key field value */
	/* and build output filename */

	/* null out name parts */
	strcpy(outfile,"");
	strcpy(middle,"");
	strcpy(stem,"");

	/* build name parts */
	strcat(middle,directory);
	strcat(middle,"/");
	strcat(stem,key);
	strcat(stem,"=");
	
	/* write format of numeric part of output filename */
	(void)sprintf(format, "%%s%%s%%0%dd%%s",numlength);

	/* write output filename */
	(void)sprintf(outfile, format,middle,stem,
				val_i, suffix);

	/* open the output file */
	if((outfp = fopen(outfile,"w+")) == NULL)
			err("Cannot open file %s\n",outfile);
	/* loop over traces setting ntr field */
	while(fgettr(tmpfp,&tmptr))  {
		tmptr.ntr = ntr;
		fputtr(outfp,&tmptr);
	}
	/* Close file */
	fclose(tmpfp);
	fclose(outfp);
	remove(tempfilename);

	if (verbose) warn(" outfile = %s", outfile);
	if (verbose) warn("val= %i",val_i);

	return(CWP_Exit());

}
Exemple #15
0
int
main(int argc, char **argv)
{
	FILE *headerfp=NULL;	/* temporary file for trace header	*/
				/*  ... (1st trace of ensemble);	*/
	char *key=NULL;		/* header key word from segy.h		*/
	char *type=NULL;	/* ... its type				*/
	int index;		/* ... its index			*/
	Value val;		/* ... its value			*/
	float fval = 0;		/* ... its value cast to float		*/
	float prevfval;		/* ... its value of the previous trace	*/

	complex *ct=NULL;	/* complex trace			*/
	complex *psct=NULL;	/* phase-stack data array		*/

	float *data=NULL;	/* input data array			*/
	float *hdata=NULL;	/* array of Hilbert transformed input data */
	float *stdata=NULL;	/* stacked data ("ordinary" stack)	*/
	float *psdata;	/* phase-stack data array (real weights for PWS)*/
	float a;	/* inst. amplitude				*/
	float dt;	/* time sample spacing in seconds		*/
	float pwr;	/* raise  phase stack to power pwr		*/
	float sl;	/* smoothing window length in seconds		*/

	int gottrace;	/* flag: set to 1, if trace is read from stdin	*/

	int i;		/* loop index					*/
	int isl;	/* smoothing window length in samples		*/
	int nt;		/* number of points on input trace		*/
	int ntr;	/* trace counter				*/
	int ps;		/* flag: output is PWS (0) or phase stack (1)	*/
	int verbose;	/* verbose flag					*/

	cwp_Bool pws_and_cdp=cwp_false;	/* are PWS and CDP set?		*/

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

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

	/* Get parameters */
	if (!getparstring("key", &key))			key="cdp";
	if (!getparfloat("pwr", &pwr))			pwr = 1.0;
	if (!getparfloat("dt", &dt)) dt = ((double) intr.dt)/1000000.0;
	if (!getparfloat("sl", &sl))			sl = 0.0;
	if (!getparint("ps", &ps))			ps = 0;
	if (!getparint("verbose", &verbose))		verbose = 0;

	if (STREQ(key, "cdp") &&  !(ps))
		pws_and_cdp = cwp_true;

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

	/* integerized smoothing window length */
	isl = NINT(fabs(sl)/dt);
	if (isl>nt)
		err("sl=%g too long for trace", fabs(sl));

	/* diagnostic print */
	if (verbose && isl)
		warn("smoothing window = %d samples", isl);

	/* initialize flag */
	gottrace = 1;

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

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

	/* remember previous value of key */
	prevfval = fval;

	/* allocate space for data, hilbert transformed data, */
	/* phase-stacked data and complex trace */
	data = ealloc1float(nt);
	hdata = ealloc1float(nt);
	stdata = ealloc1float(nt);
	psdata = ealloc1float(nt);
	psct = ealloc1complex(nt);
	ct = ealloc1complex(nt);


	/* zero out accumulators */
	memset( (void *) stdata, 0, nt*FSIZE);
	memset( (void *) psct, 0, nt*(sizeof(complex)));


	/* open temporary file for trace header   */
	headerfp = etmpfile();

	/* store trace header in temporary file and read data */
	efwrite(&intr, HDRBYTES, 1, headerfp);

	/* loop over input traces */
	ntr=0;
	while (gottrace|(~gottrace) ) { /* middle exit loop */

		/* if got a trace */
		if (gottrace) {
			/* get value of key */
			gethval(&intr, index, &val);
			fval = vtof(type,val);

			/* get data */
			memcpy((void *) data, (const void *) intr.data,
					nt*FSIZE);
		}

		/* construct quadrature trace with hilbert transform */
		hilbert(nt, data, hdata);

		/* build the complex trace and get rid of amplitude */
		for (i=0; i<nt; i++) {
			ct[i] = cmplx(data[i],hdata[i]);
			a = (rcabs(ct[i])) ? 1.0 / rcabs(ct[i]) : 0.0;
			ct[i] = crmul(ct[i], a);
		}

		/* stacking */
		if (fval==prevfval && gottrace) {
			++ntr;
			for (i=0; i<nt; ++i) {
				stdata[i] += data[i];
				psct[i] = cadd(psct[i],ct[i]);
			}
		}

		/* if key-value has changed or no more input traces */
		if (fval!=prevfval || !gottrace) {

			/* diagnostic print */
			if (verbose)
				warn("%s=%g, fold=%d\n", key, prevfval, ntr);

			/* convert complex phase stack to real weights */
			for (i=0; i<nt; ++i) {
				psdata[i] = rcabs(psct[i]) / (float) ntr;
				psdata[i] = pow(psdata[i], pwr);
			}

			/* smooth phase-stack (weights) */
			if (isl) do_smooth(psdata,nt,isl);

			/* apply weights to "ordinary" stack (do PWS) */
			if (!ps) {
				for (i=0; i<nt; ++i) {
					stdata[i] *= psdata[i] / (float) ntr;
				}
			}

			/* set header and write PS trace or */
			/* PWS trace to stdout */
			erewind(headerfp);
			efread(&outtr, 1, HDRBYTES, headerfp);
			outtr.nhs=ntr;
			if (ps) {
				memcpy((void *) outtr.data,
					(const void *) psdata, nt*FSIZE);
			} else {
				memcpy((void *) outtr.data,
					(const void *) stdata, nt*FSIZE);
			}

			/* zero offset field if a pws and cdp stack */
			if (pws_and_cdp) outtr.offset = 0;

			puttr(&outtr);

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


			/* remember previous value of key */
			prevfval = fval;

			/* zero out accumulators */
			ntr=0;
			memset( (void *) stdata, 0, nt*FSIZE);
			memset( (void *) psct, 0, nt*(sizeof(complex)));

			/* stacking */
			if (gottrace) {
				++ntr;
				for (i=0; i<nt; ++i) {
					stdata[i] += data[i];
					psct[i] = cadd(psct[i],ct[i]);
				}
			}

			/* save trace header for output trace */
			erewind(headerfp);
			efwrite(&intr, HDRBYTES, 1, headerfp);
		}

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

	} /* end loop over traces */

	return(CWP_Exit());
}