Пример #1
0
void JE_loadPCX( const char *file ) // this is only meant to load tshp2.pcx
{
	Uint8 *s = (Uint8 *)VGAScreen->pixels; /* 8-bit specific */
	
	SDL_RWops *f = dir_fopen_die(data_dir(), file, "rb");
	
	efseek(f, -769, SEEK_END);
	
	if (efgetc(f) == 12)
	{
		for (int i = 0; i < 256; i++)
		{
			efread(f, &colors[i].r, 1, 1);
			efread(f, &colors[i].g, 1, 1);
			efread(f, &colors[i].b, 1, 1);
		}
	}
	
	efseek(f, 128, SEEK_SET);
	
	for (int i = 0; i < 320 * 200; )
	{
		Uint8 p = efgetc(f);
		if ((p & 0xc0) == 0xc0)
		{
			i += (p & 0x3f);
			memset(s, efgetc(f), (p & 0x3f));
			s += (p & 0x3f);
		} else {
			i++;
			*s = p;
			s++;
		}
		if (i && (i % 320 == 0))
		{
			s += VGAScreen->pitch - 320;
		}
	}
	
	efclose(f);
}
Пример #2
0
bool initauxtbl(char *aux) {
     char *filename;
     FILE * fp ;
     int pflen, nread, pfargc, targc, i, j ;
     char *argstr, **targv ;

     if( !getnpar(0,aux,"s",&filename) ) {
	fprintf(stderr,"Can't get auxiliary input %s definition\n",aux) ;
        return 0 ;
     }
     fp = fopen(filename, "r") ;
     if( fp == NULL ) {
        fprintf(stderr,"Can't open auxiliary input file %s=%s\n",aux,filename);
        return 0 ;
     }

     /* Get the length */
     efseek(fp, 0, SEEK_END);
     pflen = eftell(fp);
     rewind(fp);
     argstr = (char *) ealloc1(1+pflen+1, 1);

     /* Read the parfile */
     nread = efread(argstr+1, 1, pflen, fp);
     if (nread != pflen) {
   	  err("%s: fread only %d bytes out of %d from %s",
	      __FILE__,nread,pflen,filename);
     }
     efclose(fp);

     /* Zap whites in parfile to help in parsing */
     argstr[0] = '\0' ;
     pfargc = white2null(argstr, pflen);
     targc = pfargc ;

     /* Allocate space for total arg pointers */
     targv = (char **) ealloc1(targc, sizeof(char*));

     /* Parse the parfile.  Skip over multiple NULLs */
     for( j=1, i=0; j < pflen; j++) {
	if( argstr[j] && !argstr[j-1] ) {
	       targv[i++] = argstr + j;
	}
     }

     /* Allocate space for the pointer table */
     argtbl = (ArgStruct*) ealloc1(targc, sizeof(ArgStruct));

      /* Tabulate targv */
     tabulate(targc, targv);
     return 1 ;
}
Пример #3
0
/* Initialize getpar */
static int getparinit (void)
{
	static int targc;	/* total number of args			*/
	static char **targv;	/* pointer to arg strings		*/
	static char *pfname;	/* name of parameter file		*/
	FILE *pffd;		/* file id of parameter file		*/
	int pflen;		/* length of parameter file in bytes	*/ 
	static int pfargc;	/* arg count from parameter file	*/
	bool parfile;		/* parfile existence flag		*/
	int argstrlen;
	char *argstr, *pargstr;	/* storage for command line and
						parameter file args	*/
	int nread;		/* bytes fread				*/
	int i, j;		/* counters				*/
	char *getpfname();	/* return name of parameter file	*/
	int white2null();	/* deliminate arg strings from parameter
				   file with (possibly multiple) NULLs
				   and return a count of the strings	*/
	int tabulate();		/* install symbol table			*/


	tabled = true;		/* remember table is built		*/

	/* Check if xargc was initiated */
	if(!xargc)
		err("%s: xargc=%d -- not initiated in main", __FILE__, xargc);

	/* Space needed for command lines */
	for (i = 1, argstrlen = 0; i < xargc; i++) {
		argstrlen += strlen(xargv[i]) + 1;
	}

	/* Get parfile name if there is one */
	/* parfile = (pfname = getpfname()) ? true : false; */
	if (pfname = getpfname()) {
		parfile = true;
	} else {
		parfile = false;
	}

	if (parfile) {
	 	pffd = efopen(pfname, "r");

		/* Get the length */
		efseek(pffd, 0, SEEK_END);
		pflen = eftell(pffd);
		rewind(pffd);
		argstrlen += pflen;
	} else {
		pflen = 0;
	}

	/* Allocate space for command line and parameter file
		plus nulls at the ends to help with parsing. */
	/* argstr = (char *) calloc((size_t) (1+argstrlen+1), 1); */
	argstr = (char *) ealloc1(1+argstrlen+1, 1);

	if (parfile) {
		/* Read the parfile */
		nread = efread(argstr + 1, 1, pflen, pffd);
  		if (nread != pflen) {
  	 	    err("%s: fread only %d bytes out of %d from %s",
  					__FILE__,  nread, pflen, pfname);
		}
		efclose(pffd);

		/* Zap whites in parfile to help in parsing */
		argstr[0] = '\0' ;
		pfargc = white2null(argstr, pflen);

	} else {
		pfargc = 0;
	}

	/* Total arg count */
	targc = pfargc + xargc - 1;

	/* Allocate space for total arg pointers */
	targv = (char **) ealloc1(targc, sizeof(char*));

	if (parfile) {
		/* Parse the parfile.  Skip over multiple NULLs */
		for (j = 1, i = 0; j < pflen; j++) {
			if (argstr[j] && !argstr[j-1]) {
			       targv[i++] = argstr + j;
			}
		}
	} else {
		i = 0;
	}

	/* Copy command line arguments */
	for (j = 1, pargstr = argstr + pflen + 2; j < xargc; j++) {
		strcpy(pargstr,xargv[j]);
		targv[i++] = pargstr;
		pargstr += strlen(xargv[j]) + 1;
	}

	/* Allocate space for the pointer table */
	argtbl = (ArgStruct*) ealloc1(targc, sizeof(ArgStruct));

	/* Tabulate targv */
	tabulate(targc, targv);
}
Пример #4
0
main (int argc, char **argv)
{
	int n1,n2,n3,
		n1s,n2s,n3s,
		id1s,id2s,id3s,
		if1s,if2s,if3s,
		*ix1s,*ix2s,*ix3s,
		i1s,i2s,i3s,
		i1,i2,i3,
		offset;
	float *p,*ps;
	FILE *infp=stdin,*outfp=stdout;

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

	/* get optional parameters */
	if (!getparint("n1",&n1)) {
		if (fseek(infp,0L,2)==-1)
			err("input file size unknown; specify n1\n");
		n1 = eftell(infp)/sizeof(float);
	}
	if (!getparint("n2",&n2)) {
		if (fseek(infp,0L,2)==-1)
			err("input file size unknown; specify n2\n");
		n2 = eftell(infp)/(n1*sizeof(float));
	}
	if (!getparint("n3",&n3)) {
		if (fseek(infp,0L,2)==-1)
			err("input file size unknown; specify n3\n");
		n3 = eftell(infp)/(n2*n1*sizeof(float));
	}
	ix1s = alloc1int(countparval("ix1s"));
	if ((n1s=getparint("ix1s",ix1s))==0) {
		free1int(ix1s);
		if (!getparint("id1s",&id1s)) id1s = 1;
		if (!getparint("if1s",&if1s)) if1s = 0;
		if (!getparint("n1s",&n1s)) n1s = 1+(n1-if1s-1)/id1s;
		ix1s = alloc1int(n1s);
		for (i1s=0,i1=if1s; i1s<n1s; i1s++,i1+=id1s)
			ix1s[i1s] = i1;
	}
	ix2s = alloc1int(countparval("ix2s"));
	if ((n2s=getparint("ix2s",ix2s))==0) {
		free1int(ix2s);
		if (!getparint("id2s",&id2s)) id2s = 1;
		if (!getparint("if2s",&if2s)) if2s = 0;
		if (!getparint("n2s",&n2s)) n2s = 1+(n2-if2s-1)/id2s;
		ix2s = alloc1int(n2s);
		for (i2s=0,i2=if2s; i2s<n2s; i2s++,i2+=id2s)
			ix2s[i2s] = i2;
	}
	ix3s = alloc1int(countparval("ix3s"));
	if ((n3s=getparint("ix3s",ix3s))==0) {
		free1int(ix3s);
		if (!getparint("id3s",&id3s)) id3s = 1;
		if (!getparint("if3s",&if3s)) if3s = 0;
		if (!getparint("n3s",&n3s)) n3s = 1+(n3-if3s-1)/id3s;
		ix3s = alloc1int(n3s);
		for (i3s=0,i3=if3s; i3s<n3s; i3s++,i3+=id3s)
			ix3s[i3s] = i3;
	}

	/* check parameters */
	for (i1s=0; i1s<n1s; i1s++)
		if (ix1s[i1s]<0 || ix1s[i1s]>n1-1)
			err("ix1s[%d]=%d is out of bounds!\n",i1s,ix1s[i1s]);
	for (i2s=0; i2s<n2s; i2s++)
		if (ix2s[i2s]<0 || ix2s[i2s]>n2-1)
			err("ix2s[%d]=%d is out of bounds!\n",i2s,ix2s[i2s]);
	for (i3s=0; i3s<n3s; i3s++)
		if (ix3s[i3s]<0 || ix3s[i3s]>n3-1)
			err("ix3s[%d]=%d is out of bounds!\n",i3s,ix3s[i3s]);

	/* allocate space for input and output arrays */
	p = ealloc1float(n1);
	ps = ealloc1float(n1s);

	/* loop over 3rd dimension */
	for (i3s=0; i3s<n3s; i3s++) {

		/* loop over 2nd dimension */
		for (i2s=0; i2s<n2s; i2s++) {

			/* find beginning of input array */
			offset = (ix2s[i2s]+ix3s[i3s]*n2)*n1*sizeof(float);
			efseek(infp,offset,0);

			/* read input array, if it exists */
			if (fread(p,sizeof(float),n1,infp)==n1) {

				/* loop over 1st dimension */
				for (i1s=0; i1s<n1s; i1s++) {
					ps[i1s] = p[ix1s[i1s]];
				}

			/* if input does not exist */
			} else {
				err("no input for ix2s[%d]=%d ix3s[%d]=%d!\n",
				i2s,ix2s[i2s],
				i3s,ix3s[i3s]);
			}

			/* write trace to output file */
			efwrite(ps,sizeof(float),n1s,outfp);
		}
	}
}
Пример #5
0
int main(int argc, char **argv)
{
	usghed usghin, usghtop, usghbot, usghlayer;
	usghed usghvtop, usghvbot, usghglayer;
	FILE *infp,*outfp,*topfp,*botfp,*layerfp;
	FILE *vtopfp,*vbotfp,*glayerfp;
	char *infile,*outfile,*layertop, *layerbot;
	char *gtopgrid, *gbotgrid;
	char *layers, *glayers;
	int ibot, itop;
	int ierr;
	int nz, iz;
	float *dzrl, sm2top, sm2bot, sm3top, sm3bot;
	float gabovetop, gbelowbot;
	int igabovetop, igbelowbot;
	float g0, r0;
	int ginterp=0;

	int n1,n2,n3;
	int i1,i2,i3;
	float d1,o1,d2,d3;

	float *grid, *ztop, *zbot, gmin, gmax;
	float *vtop, *vbot;
	float top, bot;
	float tmp;
	int i1top, i1bot, itmp;
	int ivtop, ivbot;

	float *sm2s, *sm3s;
	float *work, *fsmx, *fsmy, *vs, *zs;
	float z, scale, zscale, vscale;
	int ismx, ismy;

	int nlayer, nglayer;


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

	/* get parameters */
	if(getparstring("infile",&infile)) {
		infp = efopen(infile,"r");
	} else {
		infp = stdin;
	}
	ierr = fgetusghdr(infp,&usghin);
    	if(ierr!=0) err(" input grid header error ");
	if(getparstring("outfile",&outfile)) {
		outfp = efopen(outfile,"w");
	} else {
		outfp = stdout;
	}
	file2g(infp);
	file2g(outfp);

	nlayer = 0;
	nglayer = 0;
        nlayer = countparname("layers");
	if(nlayer==1) err(" at least 2 layers are needed \n");
        nglayer = countparname("glayers");

	if(nlayer==0) {
		if (getparstring("layertop",&layertop)) {
			topfp = efopen(layertop,"r");
			ierr = fgetusghdr(topfp,&usghtop);
      			if(ierr!=0) err(" layertop grid header error ");
		} else {
			err(" layertop missing ");
		}
		if (getparstring("layerbot",&layerbot)) {
			botfp = efopen(layerbot,"r");
			ierr = fgetusghdr(botfp,&usghbot);
      			if(ierr!=0) err(" layerbot grid header error ");
		} else {
			err(" layerbot missing ");
		}
	} else {
		if(nlayer!=nglayer && nglayer>0 ) 
		   err(" %d layers not matching %d glayers \n",nlayer,nglayer);
	}

	ivtop = 0;
	if (getparstring("gtopgrid",&gtopgrid)) {
		vtopfp = efopen(gtopgrid,"r");
		ierr = fgetusghdr(vtopfp,&usghvtop);
      		if(ierr!=0) err(" gtopgrid header error ");
		ivtop = 1;
	}
	ivbot = 0;
	if(getparstring("gbotgrid",&gbotgrid) ) {
		vbotfp = efopen(gbotgrid,"r");
		ierr = fgetusghdr(vbotfp,&usghvbot);
      		if(ierr!=0) err(" gbotgrid header error ");
		ivbot = 1;
	}
	if(!getparint("ginterp",&ginterp)) ginterp=0;

	if(ivtop==0 || ivbot==0) {
		if ( getparfloat("g0",&g0) && getparfloat("r0",&r0) ) {
			ivtop = -1; ivbot = -1;
		}
	}

	if( !getparint("nz",&nz) ) nz=10;
	if(nlayer!=0) nz = nlayer - 1;
	dzrl = emalloc(nz*sizeof(float));
	sm2s = emalloc((nz+1)*sizeof(float));
	sm3s = emalloc((nz+1)*sizeof(float));

	if( countparval("dzrl")>0 && nz!=countparval("dzrl") ) {
		err( " number of dzrl elements must match nz=%d \n",nz);
	} else if( countparval("dzrl")==0 ) {
		for(iz=0;iz<nz;iz++) dzrl[iz] = 1.;
	} else if( countparval("dzrl")==nz) {
		getparfloat("dzrl",dzrl);
	}

	if( !getparfloat("sm2top",&sm2top) ) sm2top=0.;
	if( !getparfloat("sm3top",&sm3top) ) sm3top=0.;
	if( !getparfloat("sm2bot",&sm2bot) ) sm2bot=0.;
	if( !getparfloat("sm3bot",&sm3bot) ) sm3bot=0.;

	igabovetop = 1;
	if( !getparfloat("gabovetop",&gabovetop) ) igabovetop=0;
	igbelowbot = 1;
	if( !getparfloat("gbelowbot",&gbelowbot) ) igbelowbot=0;


	n1 = usghin.n1;
	n2 = usghin.n2;
	n3 = usghin.n3;
	o1 = usghin.o1;
	d1 = usghin.d1;
	d2 = usghin.d2;
	d3 = usghin.d3;
	gmin = usghin.gmin;
	gmax = usghin.gmax;

	/* memory allocations */
	ztop = (float*) emalloc(n2*n3*sizeof(float));
	zbot = (float*) emalloc(n2*n3*sizeof(float));
	vtop = (float*) emalloc(n2*n3*sizeof(float));
	vbot = (float*) emalloc(n2*n3*sizeof(float));
	zs = (float*) emalloc(n2*n3*(nz+1)*sizeof(float));
	vs = (float*) emalloc(n2*n3*(nz+1)*sizeof(float));
	work = (float*) emalloc(n2*n3*sizeof(float));
	grid = (float*) emalloc(n1*sizeof(float));
	
	if(nlayer==0) {
		if(usghin.n2!=usghtop.n1) err("check layertop header n1");
		if(usghin.n3!=usghtop.n2) err("check layertop header n2");
		if(usghin.o2!=usghtop.o1) err("check layertop header o1");
		if(usghin.o3!=usghtop.o2) err("check layertop header o2");
		if(usghin.d2!=usghtop.d1) err("check layertop header d1");
		if(usghin.d3!=usghtop.d2) err("check layertop header d2");
		efseek(topfp,0,0);
		efread(ztop,sizeof(float),n2*n3,topfp);

		if(usghin.n2!=usghbot.n1) err("check layerbot header n1");
		if(usghin.n3!=usghbot.n2) err("check layerbot header n2");
		if(usghin.o2!=usghbot.o1) err("check layerbot header o1");
		if(usghin.o3!=usghbot.o2) err("check layerbot header o2");
		if(usghin.d2!=usghbot.d1) err("check layerbot header d1");
		if(usghin.d3!=usghbot.d2) err("check layerbot header d2");
		efseek(botfp,0,0);
		efread(zbot,sizeof(float),n2*n3,botfp);

		if(ivtop==1) {
		if(usghin.n2!=usghvtop.n1) err("check gtopgrid header n1");
		if(usghin.n3!=usghvtop.n2) err("check gtopgrid header n2");
		if(usghin.o2!=usghvtop.o1) err("check gtopgrid header o1");
		if(usghin.o3!=usghvtop.o2) err("check gtopgrid header o2");
		if(usghin.d2!=usghvtop.d1) err("check gtopgrid header d1");
		if(usghin.d3!=usghvtop.d2) err("check gtopgrid header d2");
		efseek(vtopfp,0,0);
		efread(vtop,sizeof(float),n2*n3,vtopfp);
		} 

		if(ivbot==1) {
		if(usghin.n2!=usghvbot.n1) err("check gbotgrid header n1");
		if(usghin.n3!=usghvbot.n2) err("check gbotgrid header n2");
		if(usghin.o2!=usghvbot.o1) err("check gbotgrid header o1");
		if(usghin.o3!=usghvbot.o2) err("check gbotgrid header o2");
		if(usghin.d2!=usghvbot.d1) err("check gbotgrid header d1");
		if(usghin.d3!=usghvbot.d2) err("check gbotgrid header d2");
		efseek(vbotfp,0,0);
		efread(vbot,sizeof(float),n2*n3,vbotfp);
		}
	} else {
		for(iz=0;iz<nlayer;iz++) {
			getnparstring(iz+1,"layers",&layers);
			layerfp=efopen(layers,"r");
			ierr = fgetusghdr(layerfp,&usghlayer);
			if(ierr!=0) err(" error open layers=%s \n",layers);
			if(usghin.n2!=usghlayer.n1) err("check %s header n1",layers);
			if(usghin.n3!=usghlayer.n2) err("check %s header n2",layers);
			if(usghin.o2!=usghlayer.o1) err("check %s header o1",layers);
			if(usghin.o3!=usghlayer.o2) err("check %s header o2",layers);
			if(usghin.d2!=usghlayer.d1) err("check %s header d1",layers);
			if(usghin.d3!=usghlayer.d2) err("check %s header d2",layers);
			efseek(layerfp,0,0);
			efread(zs+iz*n2*n3,sizeof(float),n2*n3,layerfp);
			efclose(layerfp);
		}
		for(iz=0;iz<nglayer;iz++) {
			getnparstring(iz+1,"glayers",&glayers);
			glayerfp=efopen(glayers,"r");
			ierr = fgetusghdr(glayerfp,&usghglayer);
			if(ierr!=0) err(" error open layers=%s \n",layers);
			if(usghin.n2!=usghglayer.n1) err("check %s header n1",glayers);
			if(usghin.n3!=usghglayer.n2) err("check %s header n2",glayers);
			if(usghin.o2!=usghglayer.o1) err("check %s header o1",glayers);
			if(usghin.o3!=usghglayer.o2) err("check %s header o2",glayers);
			if(usghin.d2!=usghglayer.d1) err("check %s header d1",glayers);
			if(usghin.d3!=usghglayer.d2) err("check %s header d2",glayers);
			efseek(glayerfp,0,0);
			efread(vs+iz*n2*n3,sizeof(float),n2*n3,glayerfp);
			efclose(glayerfp);
		}
		nz = nlayer - 1;
	}

/* compute mini layer depth and grid values */
	if(nlayer==0) {
		for(i2=0;i2<n2*n3;i2++) {
			zs[i2] = ztop[i2];
			vs[i2] = vtop[i2];
		}
		tmp = 0.;
		for(iz=0;iz<nz;iz++) tmp = tmp + dzrl[iz];
		vscale = 0.;
		zscale = 0.;
		for(iz=1;iz<nz;iz++) {
			zscale += dzrl[iz-1]/tmp;
			if(ginterp==0) {
				vscale += dzrl[iz-1]/tmp;
			} else {
				vscale = (float)iz / nz;
			}

			for(i2=0;i2<n2*n3;i2++) {
				zs[i2+iz*n2*n3] = ztop[i2] +
					zscale*(zbot[i2]-ztop[i2]);
				vs[i2+iz*n2*n3] = vtop[i2] + 
					vscale*(vbot[i2]-vtop[i2]);
			}
		}
		for(i2=0;i2<n2*n3;i2++) {
			zs[i2+nz*n2*n3] = zbot[i2];
			vs[i2+nz*n2*n3] = vbot[i2];
		}
	}

/* compute smoothing window for mini layers */
	tmp = 0.;
	for(iz=0;iz<nz;iz++) tmp = tmp + dzrl[iz];
	scale = 0.;
	sm2s[0] = sm2top;
	sm3s[0] = sm3top;
	for(iz=1;iz<nz+1;iz++) {
		scale += dzrl[iz-1]/tmp;
		sm2s[iz] = sm2top + scale*(sm2bot-sm2top);
		sm3s[iz] = sm3top + scale*(sm3bot-sm3top);
	} 

/* compute grid values at the mini layers if not specified */
	fseek2g(infp,0,0);
	if( ( (ivtop==0 || ivbot==0) && nlayer==0 ) || (nglayer==0 && nlayer>0) ) {
		for(i3=0;i3<n3;i3++) {
			for(i2=0;i2<n2;i2++) {
				efread(grid,sizeof(float),n1,infp);
				for(iz=0;iz<nz+1;iz++) {
					tmp = (zs[i2+i3*n2+iz*n2*n3] - o1)/d1 + 0.5; 
					i1 = tmp;
					if(i1<0) {
						vs[i2+i3*n2+iz*n2*n3] = grid[0];
					} else if(i1>=n1-1) {
						vs[i2+i3*n2+iz*n2*n3] = grid[n1-1];
					} else {
						vs[i2+i3*n2+iz*n2*n3] = grid[i1]+
								(tmp-i1)*(grid[i1+1]-grid[i1]);
					}
				}
			}
		}
	} else if(ivtop==-1 && ivbot==-1) {
		for(i3=0;i3<n3;i3++) {
			for(i2=0;i2<n2;i2++) {
				for(iz=0;iz<nz+1;iz++) {
					tmp = (zs[i2+i3*n2+iz*n2*n3] - ztop[i3*n2+i2]);
					vs[i2+i3*n2+iz*n2*n3] = g0 + r0 * tmp;
				}	
			}
		}
	}
/* smooth mini layer grids */
	for(iz=0;iz<nz+1;iz++) {
		tmp = sm2s[iz]/d2;
		ismx = tmp + 1.5;
		tmp = sm3s[iz]/d3;
		ismy = tmp + 1.5;
		fsmx = (float*) emalloc(ismx*sizeof(float));
		fsmy = (float*) emalloc(ismy*sizeof(float));
		/* 2d smoothing */
		smth2d_(vs+iz*n2*n3,work,fsmx,fsmy,&n2,&n3,&ismx,&ismy);
		/*
		dump2xplot(vs+iz*n2*n3,n2,n3,0,"vsmoth");
		*/
		free(fsmx);
		free(fsmy);
	}


/* output grid */
	fseek2g(infp,0,0);
	for(i3=0;i3<n3;i3++) {
		for(i2=0;i2<n2;i2++) {

			efread(grid,sizeof(float),n1,infp);
			itmp = i2+i3*n2;
			if(nlayer==0) {
				top = ztop[itmp];
				bot = zbot[itmp];
			} else {
				top = zs[itmp];
				bot = zs[itmp+(nlayer-1)*n2*n3];
			}
			tmp = (top - o1)/d1 + 0.5; 
			i1top = tmp;
			tmp = (bot - o1)/d1 + 0.5; 
			i1bot = tmp;
			if(i1top<0) i1top = 0;
			if(i1bot>n1-1) i1bot = n1-1;
			for(i1=i1top;i1<=i1bot;i1++) {
				z = o1 + i1*d1;
                                if(z>=zs[itmp] && z<=zs[itmp+nz*n2*n3]) {
					for(iz=0;iz<nz;iz++) {
						if( z == zs[itmp+iz*n2*n3] && z == zs[itmp+(iz+1)*n2*n3] ){
							grid[i1] = vs[itmp+iz*n2*n3];

						}else if(z>=zs[itmp+iz*n2*n3] && z<zs[itmp+(iz+1)*n2*n3]) {
							tmp = (z-zs[itmp+iz*n2*n3]) /
					  			(zs[itmp+(iz+1)*n2*n3]-zs[itmp+iz*n2*n3]);
							grid[i1] = vs[itmp+iz*n2*n3] + 
								tmp*(vs[itmp+(iz+1)*n2*n3]-vs[itmp+iz*n2*n3]);
                                                	if( grid[i1] != grid[i1] ){
                                                   		fprintf( stderr ,"NaN at i3=%d " ,i3 );
                                                   		fprintf( stderr ,"i2=%d i1=%d\n" ,i2, i3);
                                                	}
							break;
						}
					}
				}
			}

			if(igabovetop==1) {
				for(i1=0;i1<i1top;i1++) {
					grid[i1] = gabovetop;
				}
			}
			if(igbelowbot==1) {
				for(i1=i1bot;i1<n1;i1++) {
					grid[i1] = gbelowbot;
				}
			}

			if(i2==0 && i3==0) {
				gmin = grid[0];
				gmax = grid[0];
			}
			for(i1=0;i1<n1;i1++) {
				if(gmin>grid[i1]) gmin = grid[i1];
				if(gmax<grid[i1]) gmax = grid[i1];
			}

                        if( grid[0] != grid[0] ){
                           fprintf( stderr ,"NaN\n" );
                        }
			efwrite(grid,sizeof(float),n1,outfp);

		}
	}

	usghin.gmin = gmin;
	usghin.gmax = gmax;

	ierr = fputusghdr(outfp,&usghin);
	
	free(ztop);
	free(zbot);
	free(vtop);
	free(vbot);
	free(zs);
	free(vs);
	free(work);
	free(grid);
	exit(0);
}
Пример #6
0
main(int argc, char **argv)
{
   FILE    *infp = stdin, *outfp = stdout, *vfp;
   string   vgrid;

   int      maxp = 250000, xlpos = 2, slpos = 1, tzpos = 5, torz =
	 0, vtorz = 0;
   float    ocdp2, dcdp2, oline3, dline3, o1, d1;
   int      vgtype = 1;
   float    xl, sl;
   usghed   usgh;
   int      n1, n2, n3, i1;

   int      ierr;
   float   *vs, *tv, *zv, *v;
   float   *fbuf;
   char    *cbuf;
   int      one = 1, indx, nc, nf;
   float    tmp;
   float    to, zo, dtdzn, dtdz0;
   float    dzdt0, dzdtn;

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

   /* get input parameters */
   if (!getparstring("vgrid", &vgrid))
      err("vgrid missing");
   vfp = efopen(vgrid, "r");
   ierr = fgetusghdr(vfp, &usgh);
   if (ierr != 0)
      err("error get grid header ");


   if (!getparfloat("o1", &o1))
      o1 = usgh.o1;
   if (!getparfloat("d1", &d1))
      d1 = usgh.d1;
   if (!getparfloat("ocdp2", &ocdp2))
      ocdp2 = usgh.ocdp2;
   if (!getparfloat("dcdp2", &dcdp2))
      dcdp2 = usgh.dcdp2;
   if (!getparfloat("oline3", &oline3))
      oline3 = usgh.oline3;
   if (!getparfloat("dline3", &dline3))
      dline3 = usgh.dline3;
   if (dcdp2 == 0)
      err("dcdp2 equals 0");
   if (dline3 == 0)
      err("dline3 equals 0");

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

   if (!getparint("xlpos", &xlpos))
      xlpos = 2;
   xlpos -= 1;
   if (!getparint("slpos", &slpos))
      slpos = 1;
   slpos -= 1;
   if (!getparint("tzpos", &tzpos))
      tzpos = 5;
   tzpos -= 1;
   if (!getparint("maxp", &maxp))
      maxp = 250000;
   if (!getparint("torz", &torz))
      torz = 0;
   if (!getparint("vtorz", &vtorz))
      vtorz = 0;
   o1 = o1;
   d1 = d1;

   n1 = usgh.n1;
   n2 = usgh.n2;
   n3 = usgh.n3;
   nc = 200;
   nf = 10;

   /* memory allocations */
   vs = (float *) emalloc(n1 * n2 * n3 * sizeof(float));
   v = (float *) emalloc(n1 * sizeof(float));
   tv = (float *) emalloc(n1 * sizeof(float));
   zv = (float *) emalloc(n1 * sizeof(float));
   fbuf = (float *) malloc(nf * sizeof(float));
   cbuf = (char *) emalloc(nc * sizeof(char));

   for (i1 = 0; i1 < n1; i1++)
      tv[i1] = o1 + i1 * d1;
   for (i1 = 0; i1 < n1; i1++)
      zv[i1] = o1 + i1 * d1;
   efseek(vfp, 0, 0);
   efread(vs, sizeof(float), n1 * n2 * n3, vfp);

   fgets(cbuf, nc, infp);
   do {
      sscanf(cbuf, "%f %f %f %f %f",
	     &fbuf[0], &fbuf[1], &fbuf[2], &fbuf[3], &fbuf[4]);
      xl = fbuf[xlpos];
      sl = fbuf[slpos];

      bilint_(&n1, &n2, &n3, &ocdp2, &oline3, &dcdp2, &dline3, &xl, &sl,
	      vs, v);

      if (torz == 0) {
	 to = fbuf[tzpos];
	 if (vgtype == 1) {
	    if (vtorz == 0) {
	       zv[0] = o1 * v[0] * 0.0005;
	       for (i1 = 1; i1 < n1; i1++)
		  zv[i1] = zv[i1 - 1] + v[i1] * d1 * 0.0005;
	    } else if (vtorz == 1) {
	       for (i1 = 0; i1 < n1; i1++)
		  zv[i1] = o1 + i1 * d1;
	       tv[0] = o1 / v[0] * 2000.;
	       for (i1 = 1; i1 < n1; i1++)
		  tv[i1] =
			tv[i1 - 1] + d1 / (v[i1 - 1] + v[i1]) * 4000.;
	    }
	 } else {
	    for (i1 = 0; i1 < n1; i1++)
	       zv[i1] = (o1 + i1 * d1) * v[i1] * 0.0005;
	 }
	 dzdt0 = (zv[1] - zv[0]) / (tv[1] - tv[0]);
	 dzdtn = (zv[n1 - 1] - zv[n1 - 2]) / (tv[n1 - 1] - tv[n1 - 2]);
	 lin1dn_(tv, zv, &n1, &to, &zo, &one, &indx, &dzdt0, &dzdtn);
	 fbuf[tzpos] = zo;
      } else {
	 zo = fbuf[tzpos];
	 if (vgtype == 1) {
	    if (vtorz == 1) {
	       tv[0] = o1 / v[0] * 2000.;
	       for (i1 = 1; i1 < n1; i1++)
		  tv[i1] = tv[i1 - 1] + d1 / v[i1] * 2000.;
//		  tv[i1] = tv[i1 - 1] + d1 / (v[i1 - 1] + v[i1]) * 4000.;
	    } else if (vtorz == 0) {
	       for (i1 = 0; i1 < n1; i1++)
		  tv[i1] = o1 + d1 * i1;
	       zv[0] = o1 * v[0] * 0.0005;
	       for (i1 = 1; i1 < n1; i1++)
		  zv[i1] = zv[i1 - 1] + v[i1] * d1 * 0.0005;
//		  zv[i1] = zv[i1 - 1] + (v[i1 - 1] + v[i1]) * d1 * 0.00025;
	    }
	 } else {
	    for (i1 = 0; i1 < n1; i1++)
	       tv[i1] = o1 + d1 * i1;
	    for (i1 = 0; i1 < n1; i1++)
	       zv[i1] = (o1 + i1 * d1) * v[i1] * 0.0005;
	 }
	 dtdz0 = (tv[1] - tv[0]) / (zv[1] - zv[0]);
	 dtdzn = (tv[n1 - 1] - tv[n1 - 2]) / (zv[n1 - 1] - zv[n1 - 2]);
	 lin1dn_(zv, tv, &n1, &zo, &to, &one, &indx, &dtdz0, &dtdzn);
	 fbuf[tzpos] = to;
      }


      fprintf(outfp,
	      "                    %10.2f%10.2f%12.2f%12.2f%12.4f   \n",
	      fbuf[0], fbuf[1], fbuf[2], fbuf[3], fbuf[4]);
      bzero(cbuf, nc);
   } while (fgets(cbuf, nc, infp));

   free(cbuf);
   free(fbuf);
   free(vs);
   free(v);
   free(tv);
   free(zv);

   exit(0);
}
Пример #7
0
int kirmigs(char *datain, char *dataout, int lseekin, int lseekout, 
	    char *timefile, char *ampfile, int iamp,
	    int nt, int nx, int nz, int lt, 
	    float itmin, float iix0, float iz0, 
	    float idt, float idx, float idz,
	    float ismint, float ixmint, float izmint, 
	    float idst, float idxt, float idzt,
	    int nst, int nxt, int nzt,
	    float iofimin, float iofimax,
	    float iofomin, float idofo, int nofo,
	    int *mtrace, int i2p5,
	    int intps, int intpx, int intpz,int intype, float idcdp,
	    int amptype,int mlimit, int lagc, float itpow,
            float iaper, float iapanl, float iapanr, float iv0, 
	    int cdpx0, int dcdpx, char *dipfile, float idxdip, float idxtol) {

/* subroutine kmigs */

	int ix, iz, it, i;
	float tminl;
	float *trace, *trt, *migs, *fold, *tr, *ts, *ar, *as, *sigxt, *sigzt;
	int *inxt, *inzt;
	short *ttbl, *atbl;
	float ascale, tscale;
	long sx, gx;
	int ntrace, iofo;
	int ix0, iof, latbl;
	float xs, xr, tmp, *trz, *trr,dldt,dl,offout,dlm;
	float *twk1,*twk2,*awk1,*awk2,*trwk;
	FILE *tfp, *afp;
	FILE *infp, *outfp;
	int mute;
	int chkcdp;
	long sy, gy, icdp1;
	float offset;
	char *afile, *tfile;
	int lttbl,llimit;
	int lmem ;
	int lwin, mt, notrace, itmp;
	float rmso, zw;
	float *dips;
	int idip;
	int isize;

        /* convert double to float */
	float tmin = itmin , x0 = iix0 , z0 = iz0 ; 
	float dt = idt , dx = idx , dz =idz ;
	float smint = ismint , xmint = ixmint , zmint = izmint ;
	float dst = idst , dxt = idxt , dzt = idzt ;
	float ofimin = iofimin , ofimax = iofimax ;
	float ofomin = iofomin , dofo = idofo ;
	float dcdp = idcdp;
	float tpow = itpow; 
	float v0 = iv0;
	float aper = iaper;
	float apanl = iapanl;
	float apanr = iapanr;
	float dxdip  = idxdip;
	float dxtol = idxtol;

        char chost[MAXHOSTNAMELEN] ;
	int nxdip;

        gethostname(chost,MAXHOSTNAMELEN) ;
	fprintf(stderr,"starting migration at %s for\t%f < offsets <= %f \n",
			chost, ofomin-dofo*.5,ofomin+(nofo-.5)*dofo);
	
	tmp = nx * dx/dxdip;
	nxdip = (int)tmp;
	if(nxdip<1) nxdip = 1;
	if(nxdip>nx) nxdip = nx;
	dxdip = dx * nx/nxdip;
	 
	lwin = lagc/(int)(dt*1000.);

	/* in case the dp starts the migration again, do not remigrate */ 
	isize = 0;
	if((outfp = fopen(dataout,"r"))!=NULL) {
                 fseek(outfp,0L,SEEK_END);
                 isize= (int) ftell(outfp);
                 fclose(outfp);
        }
	if( isize==(nz*sizeof(float)+HDRBYTES)*nx*nofo ) {
	   fprintf(stderr,"output %s already migrated \n",dataout);
           return 0 ;
	}

	/* set mtrace to be -1 in case of error return to kirmig */
	*mtrace = -1;
	
        infp = fopen(datain,"r");
        if( infp == NULL ) {
	   fprintf(stderr,"kirmigs datain=%s not found\n",datain);
           return 1 ;
        }

        outfp = fopen(dataout,"w");
        if( outfp == NULL ) {
	   fprintf(stderr,"kirmigs dataout=%s not found\n",dataout);
           return 1 ;
        }

	/* check to see if there is any input data */
	efseek(infp,0L,2);
	notrace = 0;
        if((eftell(infp)-lseekin)<=0) {
		warn("no trace in input %s \n",datain); 
		notrace = 1;
		*mtrace = 0;
	}

	if (notrace==1) goto skipmig; 

        /* turn off buffering for outfp */
	/* setbuf(outfp,NULL); */

	/*
	fprintf(stderr,"within kirmigs mlimit=%d \n",mlimit);
	fprintf(stderr,"x0=%f z0=%f nx=%d nz=%d \n",x0,z0,nx,nz);
	fprintf(stderr,"dx=%f dz=%f \n",dx,dz);
	fprintf(stderr,"timefile=%s \n",timefile);
	fprintf(stderr,"ampfile=%s \n",ampfile);
	fprintf(stderr,"nt=%d dt=%f tmin=%f \n",nt,dt,tmin);
	fprintf(stderr,"ofomin=%f dofo=%f nofo=%d \n",ofomin,dofo,nofo);
	fprintf(stderr,"xmint=%f zmint=%f smint=%f \n",xmint,zmint,smint);
	fprintf(stderr,"dxt=%f dzt=%f dst=%f \n",dxt,dzt,dst);
	fprintf(stderr,"nxt=%d nzt=%d nst=%d \n",nxt,nzt,nst);
	fprintf(stderr,"lt=%d \n",lt);
	fprintf(stderr,"dcdp=%f dxtol=%f \n",dcdp,dxtol);
	fprintf(stderr,"lseekin=%d lseekout=%d \n",lseekin,lseekout);
	*/

	idip = 1;
	if(dipfile[0]=='N' && dipfile[1]=='U',
		dipfile[2]=='L' && dipfile[3]=='L') idip=0;

	/* memory allocations */
	lmem = (2*nzt*nxt + nofo*nx*nz + nofo + 4*nx*nz + nx + 2*nz 
		+ nzt + 2*lt + nt)*sizeof(float) 
	      +(nx + nz) * sizeof(int) + idip*2*nxdip*nz*sizeof(float);

	llimit = mlimit * 1024 * 1024;	
	if ( lmem > llimit ) {
	   fprintf(stderr,"Need at least memory size mlimit=%d (Bytes)\n",
	 	   lmem);
	   return 1;
	} 

        twk1 = (float*) malloc(nzt*nxt*sizeof(float));
	if( twk1 == 0 ) return 1 ;
        twk2 = (float*) malloc(nzt*nxt*sizeof(float));
	if( twk2 == 0 ) return 1 ;
	migs = (float*) malloc(nofo*nx*nz*sizeof(float));
	if( migs == 0 ) return 1 ;
	fold = (float*) malloc(nofo*sizeof(float));
	if( fold == 0 ) return 1 ;
	ts = (float*) malloc(nx*nz*sizeof(float));
	if( ts == 0 ) return 1 ;
	tr = (float*) malloc(nx*nz*sizeof(float));
	if( tr == 0 ) return 1 ;
	as = (float*) malloc(nx*nz*sizeof(float));
	if( as == 0 ) return 1 ;
	ar = (float*) malloc(nx*nz*sizeof(float));
	if( ar == 0 ) return 1 ;
	sigxt = (float*) malloc(nx*sizeof(float));
	if( sigxt == 0 ) return 1 ;
	sigzt = (float*) malloc(nz*sizeof(float));
	if( sigzt == 0 ) return 1 ;
	inxt = (int*) malloc(nx*sizeof(int));
	if( inxt == 0 ) return 1 ;
	inzt = (int*) malloc(nz*sizeof(int));
	if( inzt == 0 ) return 1 ;

	trt = (float*) malloc(nzt*sizeof(float));
	if( trt == 0 ) return 1 ;
	trz = (float*) malloc(nz*sizeof(float));
	if( trz == 0 ) return 1 ;
	trace = (float*) malloc(lt*sizeof(float));
	if( trace == 0 ) return 1 ;
        trwk = (float*) malloc(lt*sizeof(float));
	if( trwk == 0 ) return 1 ;
	trr = (float*) malloc(nt*sizeof(float));
	if( trr == 0 ) return 1 ;
	dips = (float*) malloc(idip*2*nxdip*nz*sizeof(float));
	if( dips == 0 ) return 1 ;


	lttbl = nst*nxt*nzt*sizeof(short);
	if ( lmem + lttbl > llimit ) {  
	   lttbl = 1 * sizeof(short);
	   tfile = (char*) malloc(strlen(timefile)+1);
	   sprintf(tfile,"%s\0",timefile);
	}
	else {
	   tfile = "null";
	}
	lmem +=lttbl;
	ttbl = (short*) malloc(lttbl);
	if( ttbl == 0 ) return 1 ;
        if ( iamp == 1 ) {
           latbl = nst*nxt*nzt*sizeof(short);
	   if ( lmem + latbl > llimit ) {  
	      latbl = 1 * sizeof(short);
	      afile = (char*) malloc(strlen(ampfile)+1);
	      sprintf(afile,"%s\0",ampfile);
	   }
	   else {
	      afile = "null";
	   }
           awk2 = (float*) malloc(nzt*nxt*sizeof(float));
	   lmem += nzt*nxt*sizeof(float); 
	   if( awk2 == 0 ) return 1 ;
           awk1 = (float*) malloc(nzt*sizeof(float));
	   lmem += nzt*sizeof(float); 
	   if( awk1 == 0 ) return 1 ;
        } else {
           latbl = 1 * sizeof(short);
	   afile = "null";
           awk2 = (float*) malloc(latbl);
	   lmem += latbl; 
	   if( awk2 == 0 ) return 1 ;
           awk1 = (float*) malloc(latbl);
	   lmem += latbl; 
	   if( awk1 == 0 ) return 1 ;
        }
	atbl = (short*) malloc(latbl);
	lmem += latbl; 
	if( atbl == 0 ) return 1 ;
	fprintf(stderr,"total memory used (Byte) =%d \n",lmem); 
	if(lttbl==1) fprintf(stderr,"travel time table disk i/o used \n"); 
	if(iamp==1 && latbl==1) 
	   fprintf(stderr,"amplitude table disk i/o used \n");

	/* read in travel time table and amplitude table */
	tfp = fopen(timefile,"r");
        if( tfp == NULL ) {
	   fprintf(stderr,"kirmigs: timefile=%s not found\n",timefile);
           return 1 ;
        }

	if ( iamp == 1 && latbl > 1 ) { 
	   afp = fopen(ampfile,"r");
           if( afp == NULL ) {
	       fprintf(stderr,"kirmigs: ampfile=%s not found\n",ampfile);
               return 1 ;
           }
	   /* find scale to scale amplitudes */
	   ascale = 0.;
           for(ix=0;ix<nxt*nst;ix++) {
              fread((char *)trt,sizeof(float),nzt,afp);
	      for(iz=0;iz<nzt;iz++) {
	         if(fabs(trt[iz]) > ascale) ascale=fabs(trt[iz]); 
	      }
	   }
	   /* read in amplitudes, scale and store in short *atbl */ 
	   fseek(afp,0,0);
	   if(ascale>0.) ascale = 32000./ascale;
           for(ix=0;ix<nxt*nst;ix++) {
              fread((char *)trt,sizeof(float),nzt,afp);
	      ix0 = ix*nzt;
	      for(iz=0;iz<nzt;iz++) {
	      tmp = trt[iz]*ascale; 
	      atbl[ix0+iz] = (short)tmp;
	      }
	   }
	}
	else {
	   ascale = 1.;
	}
	
	if(lttbl > 1 ) {
	   /* read in times, scale and store in short *ttbl */ 
           /* times in ms */
	   if (dt>=1.) {
              tscale = 32000./(nt*dt);
           }
           else {
              tscale = 1000. * 32000. / (nt*dt*1000.);
           }
           for(ix=0;ix<nxt*nst;ix++) {
              fread((char *)trt,sizeof(float),nzt,tfp);
	      ix0 = ix*nzt;
	      for(iz=0;iz<nzt;iz++) {
	         tmp = trt[iz]*tscale;
	         if ( tmp < 32000 ) {
	            ttbl[ix0+iz] = (short)tmp;
	         }
	         else {
	            ttbl[ix0+iz] = 32500;
	         }
	      }
	   }   

           if (dt>=1.) {
              tmin = tmin * 1000.;
              dl = dt * nt / lt;
	      dlm = dl;
           } else {
              tscale = tscale/1000.;
              dt = dt * 1000.;
              tmin = tmin * 1000.;
              dl = dt * nt / lt;
	      dlm = dl;
           }
	} else {
	   dl = dt*nt/lt;
	   dlm = dl * 1000.;
	   tscale = 1.;
	}
	
	

	/* Main loop over traces */
	ix = 0;
	dldt = dl/dt;
	tminl = tmin / dt; 
	ntrace =0;

/* skip lseekin bytes in infp */
	fseek(infp,lseekin,0);

/* see if cdp and offset are used in migration, instead of sx and gx */
/* read first trace */
	fgettr(infp,&tra);
	sy = tra.sy;
	gy = tra.gy;
	if ( sy == gy && dcdp==0. ) {
	   chkcdp = 0;
	} else {
	   chkcdp = 1;
	   if( dcdp == 0. ) {
		fprintf(stderr,"dcdp must be specified ! \n");
		return 1;
	   }
	   icdp1 = cdpx0;
	}
	
	/* read in dips grid */
	
	if(idip==1) {

		tmp = dcdpx*nx/nxdip;
		itmp = (int) tmp;	
		dipsgrid_cpp(dipfile,dips,dips+nz*nxdip,z0,dz,nz,
			cdpx0,itmp,nxdip);
		for(iz=0;iz<nxdip*nz*2;iz++) {
			tmp=dips[iz];
			if(tmp>=90.) {
		 		tmp = 89.9;
			} else if (tmp<=-90.) {
				tmp = -89.9;
			} 
			dips[iz]=tan(tmp*3.141592654/180.);
		}
	}

	/* initialize mtrace */
	*mtrace = 0;

	do {

                int nonzerotrace ;
		/* Load trace into trace (zero-padded) */
		memcpy(trr, tra.data, nt*sizeof(float));

	        /* check if this is a zero trace */
		nonzerotrace = 0 ;	
		for(it=0;it<nt;it++) {
		  if(trr[it] != 0.0) { nonzerotrace = 1; break ; } 
		} 
		if( nonzerotrace == 0 ) tra.trid = 0;
		if( tra.trid != 0 ) {

/* apply 2.5-D filter */
	            if(i2p5==1) f2p5_(trr,&nt);
/* apply agc */
		    if(lwin>0) {
                        mute = (tra.mute-tra.delrt)/tra.dt;
                        if(mute<0) mute=0;
                        if(mute>nt) mute=nt;
                        mt = nt - mute;
                        rmso = 2000.;
                        agc_(trr+mute,&mt,&lwin,trwk,&rmso);
                    }
/* apply tpow */
                    if(fabs(tpow)>0.0001) tp_(trr,&nt,&tpow,&tmin,&dt);


/* linearly interpolate input trace */
	            for(it=0;it<lt;it++) {
		       tmp = tminl+it*dldt; 
	               i = (int)tmp;
	               tmp = tmp - i;
	               if(i>=0 && i<nt-1) {
		          trace[it] = (1.-tmp)*trr[i]+tmp*trr[i+1];
		       }
		       else {
		          trace[it] = 0.;
		       }	
		    }
	

	            /* obtain source and receiver x's from trace hader */
		    if ( chkcdp == 0 ) {
		       sx = tra.sx;
		       gx = tra.gx;	
		       xs = sx;
		       xr = gx;
		       if(tra.scalco>1) {
				xs = xs*tra.scalco;
				xr = xr*tra.scalco;
		       } else if (tra.scalco<0) {
				xs = xs/(-tra.scalco);
				xr = xr/(-tra.scalco);
		       }		
		    }
		    else {
		       xs = (tra.cdp-icdp1)*dcdp + x0 - tra.offset/2.;
		       xr = (tra.cdp-icdp1)*dcdp + x0 + tra.offset/2.;

		    }
	            tmp = (tra.mute-tra.delrt)/dlm + 1.;
	            mute = (int)tmp ; 
		    tmp = tra.mute * 0.5 * v0 * 0.001;
                    tmp = tmp*tmp - 0.25 * fabs(xs-xr) * fabs(xs-xr);
                    if ( tmp > 0. ) {
                        zw = sqrt(tmp);
                    } else {
			zw = 0.;
		    }
		    ix = ix+ 1;

		    /* migration */
		    tmp = fabs(xs-xr);
		    offset = (tmp-ofomin)/dofo+1.5 ;   
		    iofo = (int)offset;
		    if ( (iofo<1 || iofo>nofo) && intype==1 ) break; 

/*
 fprintf(stderr,"xr=%f xs=%f at cdp=%d dcdp=%f icdp1=%d x0=%f offset=%d\n",
	xr,xs,tra.cdp,dcdp,icdp1,x0,tra.offset);	
*/

		}
		ntrace = ntrace + 1;
		if ( ntrace%1000 == 0 )
   	fprintf(stderr,"input %d traces processed at %s \n",ntrace,chost);

	        if(tmp>=ofimin && tmp<=ofimax && tra.trid==1 && mute<lt) {

		   	kirmig_(trace,&xs,&xr,&tmin,&lt,&dl,
                           	migs,&x0,&z0,&dx,&dz,&nx,&nz,
                           	&nofo,&ofomin,&dofo,fold,
                           	ttbl,atbl,&xmint,&zmint,&dxt,&dzt,&nxt,&nzt,
                           	&nst,&smint,&dst,ts,tr,as,ar,&dxtol,
                           	sigxt,sigzt,inxt,inzt,&iamp,mtrace,
                           	twk1,twk2,awk1,awk2,&tscale,trwk,
                           	&intps,&intpx,&intpz,&mute,
			   	&ascale,&amptype,
			   	tfile,afile,&aper,&apanl,&apanr,&zw,
				dips,&idip,&nxdip,&dxdip);
		}

	} while (fgettr(infp,&tra));


   fprintf(stderr,"input %d traces processed at %s \n",ntrace,chost);
	
        /* free spaces */
	free((char *)trt);
	free((char *)trr);
	free((char *)ttbl);
	free((char *)atbl);
	free((char *)twk1);
	free((char *)twk2);
	free((char *)awk1);
	free((char *)awk2);
	free((char *)trwk);
	free((char *)tr);
	free((char *)ts);
	free((char *)ar);
	free((char *)as);
	free((char *)sigxt);
	free((char *)sigzt);
	free((char *)inxt);
	free((char *)inzt);
	free((char *)trace);
	free((char*)dips);

	if(fabs(tpow)>0.0001) {
                trace = (float *) malloc(nz*sizeof(float));
                for(i=0;i<nz;i++) {
                        tmp = (z0+i*dz)/(z0+nz*0.5*dz);
                        tmp = fabs(tmp);
                        if(tmp==0.) {
                                trace[i] = 0.;
                        } else {
                                trace[i] = pow(tmp,-tpow);
                        }
                }
        }

	ascale=1./ascale;
	if ( amptype == 0 ) {
	   ascale = ascale/2.;
	}
	else {
	   ascale=ascale*ascale;
	}
	if ( iamp == 0 ) ascale = 1.;
	
	skipmig:

/* output traces */

	/* skip lseekout bytes in outfp */
	fseek(outfp,lseekout,0);
	
	/* if no input trace, zero output */
	if(notrace==1) {
		bzero((char*)&tra,(240+nz*sizeof(float)));
	}

	/* update trace headers */
	tra.ns = nz;
	tra.trid = 1;

	if ( dz < 30. ) {
	   tmp = dz * 1000.;
           tra.dt = (unsigned short) tmp;
	   tmp = z0 * 1000.;
	   tra.delrt = (unsigned short) tmp;
	} else if (dz <300.) {
	   tmp = dz * 100.;
           tra.dt = (unsigned short) tmp;
	   tmp = z0 * 100.;
	   tra.delrt = (unsigned short) tmp;
	} else {
           tra.dt = (unsigned short) dz;
	   tra.delrt = (unsigned short) z0;
	}
	tra.sy = 0;
	tra.gy = 0;
	/* scale x coordinate if needed */  
	itmp = (int) dx;
	tmp = dx - itmp;
	if(fabs(tmp)>0.01) {
		tra.scalco = -100;
	} else {
		tra.scalco = 1;
	} 

	for(iof=0;iof<nofo;iof++) {
	   offout = ofomin + iof*dofo;
	   if(notrace==0) {
	   	if(fold[iof]>1.) {
	      		tmp = ascale / fold[iof];
	   	}
	   	else {
	      		tmp = ascale;
	   	}
	   }
	   for(ix=0;ix<nx;ix++) {
/* update trace headers */
	      tra.offset = offout;
              tra.cdp = ix+1 ;
              tra.tracf = ix+1 ;
              xs = x0 + ix*dx - offout/2;  
              xr = x0 + ix*dx + offout/2;  
	      if (tra.scalco==-100) {
              	xs = xs * 100.;
              	xr = xr * 100.;
	      }
	      tra.sx = xs;
	      tra.gx = xr;
	      tra.ep = tra.sx;	
	      tra.fldr = tra.sx;	

	      if(notrace==0) {
	      	ix0 = ix*nz+iof*nx*nz;
	      	for (i=0;i<nz;i++) {
	         	trz[i] = migs[ix0+i]*tmp;
	      	}
	      	if(fabs(tpow)>0.0001)
                        for(i=0;i<nz;i++) trz[i] = trz[i]*trace[i];
	      	memcpy(tra.data, trz, nz*sizeof(float));
	      }
	      fputtr(outfp,&tra);
	      /* fflush(outfp); */
	   }
	} 

	if(notrace==0) {
		free((char *)migs);
		free((char *)fold);
		free((char *)trz);
		if(fabs(tpow)>0.0001) free((char *)trace);
	}

	fclose(infp);
	fclose(outfp);

   fprintf(stderr,
	"kirmig done at %s for\t%f < offsets <= %f  for %d live traces\n",
		  chost,ofomin-0.5*dofo,ofomin+(nofo-0.5)*dofo,*mtrace);

	return 0;
}
Пример #8
0
main (int argc, char **argv)
{
	int nt,nz,nin,nout,nx,ix;
	float dt,ft,dz,fz,*din,*dout,*zt,*tz;
	char *intype="",*outtype="";
	FILE *infp=stdin,*outfp=stdout;

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

	/* get required parameters */
	if (!getparstring("intype",&intype)) err("Must specify intype!\n");
	if (!getparstring("outtype",&outtype)) err("Must specify outtype!\n");

	/* get optional parameters */
	if (!getparint("nt",&nt)) {
		if (getparint("nz",&nz)) {
			nt = nz;
		} else {
			if (fseek(infp,0L,2)==-1)
				err("input file size unknown; specify nt or nz\n");
			nt = eftell(infp)/sizeof(float);
		}
	}
	if (!getparfloat("dt",&dt)) dt = 1.0;
	if (!getparfloat("ft",&ft)) ft = 0.0;
	if (!getparint("nz",&nz)) {
		if (getparint("nt",&nt)) {
			nz = nt;
		} else {
			if (fseek(infp,0L,2)==-1)
				err("input file size unknown; specify nt or nz\n");
			nz = eftell(infp)/sizeof(float);
		}
	}
	if (!getparfloat("dz",&dz)) dz = 1.0;
	if (!getparfloat("fz",&fz)) fz = 0.0;

	/* determine number of samples per input and output trace */
	if (
		STREQ(intype,"vintt") || 
		STREQ(intype,"vrmst") || 
		STREQ(intype,"zt") ) {
		nin = nt;
	} else if (
		STREQ(intype,"vintz") || 
		STREQ(intype,"tz") ) {
		nin = nz;
	} else {
		err("invalid intype=%s!\n",intype);
	}
	if (
		STREQ(outtype,"vintt") || 
		STREQ(outtype,"vrmst") || 
		STREQ(outtype,"zt") ) {
		nout = nt;
	} else if (
		STREQ(outtype,"vintz") || 
		STREQ(outtype,"tz") ) {
		nout = nz;
	} else {
		err("invalid outtype=%s!\n",outtype);
	}

	/* determine number of traces to process */
	if (!getparint("nx",&nx)) nx = -1;

	/* allocate space */
	tz = ealloc1float(nz);
	zt = ealloc1float(nt);
	din = ealloc1float(nin);
	dout = ealloc1float(nout);

	/* set input file pointer to beginning of file */
	efseek(infp,0L,0);

	/* loop over traces */
	for (ix=0; ix<nx || nx<0; ix++) {

		/* read input data */
		if (efread(din,sizeof(float),nin,infp)!=nin) break;

		/* convert input data to zt and tz */
		if (STREQ(intype,"vintt"))
			in_vintt(nt,dt,ft,nz,dz,fz,din,zt,tz);
		else if (STREQ(intype,"vrmst"))
			in_vrmst(nt,dt,ft,nz,dz,fz,din,zt,tz);
		else if (STREQ(intype,"zt"))
			in_zt(nt,dt,ft,nz,dz,fz,din,zt,tz);
		else if (STREQ(intype,"vintz"))
			in_vintz(nt,dt,ft,nz,dz,fz,din,zt,tz);
		else if (STREQ(intype,"tz"))
			in_tz(nt,dt,ft,nz,dz,fz,din,zt,tz);
		else
			err("invalid intype=%s!\n",intype);

		/* convert zt and tz to output data */
		if (STREQ(outtype,"vintt"))
			out_vintt(nt,dt,ft,nz,dz,fz,zt,tz,dout);
		else if (STREQ(outtype,"vrmst"))
			out_vrmst(nt,dt,ft,nz,dz,fz,zt,tz,dout);
		else if (STREQ(outtype,"zt"))
			out_zt(nt,dt,ft,nz,dz,fz,zt,tz,dout);
		else if (STREQ(outtype,"vintz"))
			out_vintz(nt,dt,ft,nz,dz,fz,zt,tz,dout);
		else if (STREQ(outtype,"tz"))
			out_tz(nt,dt,ft,nz,dz,fz,zt,tz,dout);
		else
			err("invalid outtype=%s!\n",outtype);

		/* write output data */
		efwrite(dout,sizeof(float),nout,outfp);
	}
}
Пример #9
0
main(int argc, char **argv)
{
    	FILE *vfp1, *vfp2, *vfp3;
	FILE *datafp;

	char *in1, *in2, *in3, *vgrid1, *vgrid2, *vgrid3;
	char cname[80];
	int i1, i2, i3, iv1, iv2, iv3, it;
	float xqc, yqc, dt; 
	int vstype1, vstype2, vstype3, vgtype1, vgtype2, vgtype3, nt;
	int paper, vauxplot, ia, ii, viplot;

    	float *time, *work;
	float *t1s, *t2s, *t3s, *t4s, *t5s, *t6s;
	float *v1s, *v2s, *v3s, *v4s, *v5s, *v6s;
	int n1s, n2s, n3s, n4s, n5s, n6s;

	float *t1, *v1, *t2, *v2, *t3, *v3, *t4, *v4, *t5, *v5, *t6, *v6; 
	float x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6;
	int np1=0, np2=0, np3=0, np4=0, np5=0, np6=0;
	int ntmax=4096;
	float tmp, tmax;
	int ix, iy, iseek;
	char cmd[1024];
	int ntvmax,nvfmax;
	char *title;
	

	float x4min,x4max,y4min,y4max;
	float x5min,x5max,y5min,y5max;
	float x6min,x6max,y6min,y6max;

	usghed usgh;


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

	/* required parameters */
	if (!getparfloat("xqc",&xqc)) err(" xqc missing ");
	if (!getparfloat("yqc",&yqc)) err(" yqc missing ");

	/* optional parameters */

	if (!getparstring("vs3d1",&in1)) {
		i1 = 0;
	} else {
		i1 = 1;
	}

	if (!getparstring("vs3d2",&in2)) {
		i2 = 0; 
	} else {
		i2 = 1;
	}

	if (!getparstring("vs3d3",&in3)) {
		i3 = 0; 
	} else {
		i3 = 1;
	}

	if (!getparstring("vgrid1",&vgrid1)) {
		iv1 = 0; 
	} else {
		iv1 = 1;
		vfp1 = efopen(vgrid1,"r");
	}
	if (!getparstring("vgrid2",&vgrid2)) {
		iv2 = 0; 
	} else {
		iv2 = 1;
		vfp2 = efopen(vgrid2,"r");
	}
	if (!getparstring("vgrid3",&vgrid3)) {
		iv3 = 0; 
	} else {
		iv3 = 1;
		vfp3 = efopen(vgrid3,"r");
	}

	if (!getparfloat("dt",&dt)) dt = 100.;
	if (!getparint("vgtype1",&vgtype1)) vgtype1 = 2;
	if (!getparint("vgtype2",&vgtype2)) vgtype2 = 2;
	if (!getparint("vgtype3",&vgtype3)) vgtype3 = 2;
	if (!getparint("vstype1",&vstype1)) vstype1 = 0;
	if (!getparint("vstype2",&vstype2)) vstype2 = 0;
	if (!getparint("vstype3",&vstype3)) vstype3 = 0;
	if (!getparint("paper",&paper)) paper = 0;
	if (!getparint("vauxplot",&vauxplot)) vauxplot = 0;
	if (!getparint("viplot",&viplot)) viplot = 1;
	ia = vauxplot;
	if(ia==2) ia = 1;
	ii = viplot;
	if (!getparint("nvfmax",&nvfmax)) nvfmax = 4096;
	if (!getparint("ntvmax",&ntvmax)) ntvmax = 256;
	if (!getparfloat("tmax",&tmax)) tmax = 100000.;
	if (!getparstring("title",&title)) {
		title = (char*) malloc(30*sizeof(char));
		strcpy(title,"velocity qc ");
	}		

	time = (float*) malloc(ntmax*sizeof(float));
	work = (float*) malloc(4*ntmax*sizeof(float));
	t1 = (float*) malloc(ntmax*sizeof(float));
	t2 = (float*) malloc(ntmax*sizeof(float));
	t3 = (float*) malloc(ntmax*sizeof(float));
	t4 = (float*) malloc(ntmax*sizeof(float));
	t5 = (float*) malloc(ntmax*sizeof(float));
	t6 = (float*) malloc(ntmax*sizeof(float));
	v1 = (float*) malloc(ntmax*sizeof(float));
	v2 = (float*) malloc(ntmax*sizeof(float));
	v3 = (float*) malloc(ntmax*sizeof(float));
	v4 = (float*) malloc(ntmax*sizeof(float));
	v5 = (float*) malloc(ntmax*sizeof(float));
	v6 = (float*) malloc(ntmax*sizeof(float));

	/* read in t, v at closest location of data in1 */

	n1s = 0;
	if(i1==1) {
		findvf(in1, xqc, yqc, t1, v1, &np1, &x1, &y1, ntvmax, nvfmax); 
		times(dt, t1, np1, time, &nt, tmax);
		t1s = (float*) malloc(nt*sizeof(float));
		v1s = (float*) malloc(nt*sizeof(float));
		bcopy(time,t1s,nt*sizeof(float));
		bcopy(v1,v1s,nt*sizeof(float));
		if(vstype1==0) {
			vrms2vint(t1, v1, np1, t1s, v1s, nt);
		} else if(vstype1==1) {
			vavg2vint(t1, v1, np1, t1s, v1s, nt);
		}
		n1s = nt;
	}

	n2s = 0;
	if(i2==1) {
		findvf(in2, xqc, yqc, t2, v2, &np2, &x2, &y2, ntvmax, nvfmax); 
		times(dt, t2, np2, time, &nt, tmax);
		t2s = (float*) malloc(nt*sizeof(float));
		v2s = (float*) malloc(nt*sizeof(float));
		bcopy(time,t2s,nt*sizeof(float));
		bcopy(v2,v2s,nt*sizeof(float));
		if(vstype2==0) {
			vrms2vint(t2, v2, np2, t2s, v2s, nt);
		} else if (vstype2==1) {
			vavg2vint(t2, v2, np2, t2s, v2s, nt);
		}
		n2s = nt;
	}

	n3s = 0;
	if(i3==1) { 
		findvf(in3, xqc, yqc, t3, v3, &np3, &x3, &y3, ntvmax, nvfmax); 
		times(dt, t3, np3, time, &nt, tmax);
		t3s = (float*) malloc(nt*sizeof(float));
		v3s = (float*) malloc(nt*sizeof(float));
		bcopy(time,t3s,nt*sizeof(float));
		bcopy(v3,v3s,nt*sizeof(float));
		if(vstype3==0) {
			vrms2vint(t3, v3, np3, t3s, v3s, nt);
		} else if (vstype3==1) {
			vavg2vint(t3, v3, np3, t3s, v3s, nt);
		}
		n3s = nt;
	}

	/* read in volocity function from vgrid */
	n4s = 0;
	if(iv1==1) {
		fgetusghdr(vfp1, &usgh);
		tmp = (xqc - usgh.o2)/usgh.d2 + .5;
		ix = tmp;
		if(ix<0) ix = 0;
		if(ix>=usgh.n2) ix = usgh.n2 - 1;
		x4 = usgh.o2 + ix*usgh.d2;
		tmp = (yqc - usgh.o3)/usgh.d3 + .5;
		iy = tmp;
		if(iy<0) iy = 0;
		if(iy>=usgh.n3) iy = usgh.n3 - 1;
		y4 = usgh.o3 + iy*usgh.d3;
		fprintf(stderr,"Velociy grid %s: x=%g y=%g t0=%g dt=%g nt=%d\n",
				vgrid1,x4,y4,usgh.o1,usgh.d1,usgh.n1);
		iseek = (iy*usgh.n2+ix)*usgh.n1*usgh.dtype;
		efseek(vfp1,iseek,0);
		efread(v4,sizeof(float),usgh.n1,vfp1);
		for(it=0;it<usgh.n1;it++) t4[it] = usgh.o1 + it*usgh.d1;
		np4 = usgh.n1; 
		if(vgtype1==2) {
			if(tmax < 100000.) {
				tmp = (tmax - usgh.o1)/usgh.d1;
				nt = tmp;
			} else {
				nt = np4;
			}
			t4s = (float*) malloc(nt*sizeof(float));
			v4s = (float*) malloc(nt*sizeof(float));
			bcopy(t4,t4s,nt*sizeof(float));
			bcopy(v4,v4s,nt*sizeof(float));
		} else if(vgtype1==0) {
			times(dt, t4, np4, time, &nt, tmax);
			t4s = (float*) malloc(nt*sizeof(float));
			v4s = (float*) malloc(nt*sizeof(float));
			bcopy(time,t4s,nt*sizeof(float));
			vrms2vint(t4, v4, np4, t4s, v4s, nt);
		} else if(vgtype1=1) {
			times(dt, t4, np4, time, &nt, tmax);
			t4s = (float*) malloc(nt*sizeof(float));
			v4s = (float*) malloc(nt*sizeof(float));
			bcopy(time,t4s,nt*sizeof(float));
			vavg2vint(t4, v4, np4, t4s, v4s, nt);
		}
		n4s = nt;
		x4min = usgh.o2;
		x4max = usgh.o2 + (usgh.n2 - 1)*usgh.d2;
		y4min = usgh.o3;
		y4max = usgh.o3 + (usgh.n3 - 1)*usgh.d3;
	}

	n5s = 0;
	if(iv2==1) {
		fgetusghdr(vfp2, &usgh);
		tmp = (xqc - usgh.o2)/usgh.d2 + .5;
		ix = tmp;
		if(ix<0) ix = 0;
		if(ix>=usgh.n2) ix = usgh.n2 - 1;
		x5 = usgh.o2 + ix*usgh.d2;
		tmp = (yqc - usgh.o3)/usgh.d3 + .5;
		iy = tmp;
		if(iy<0) iy = 0;
		if(iy>=usgh.n3) iy = usgh.n3 - 1;
		y5 = usgh.o3 + iy*usgh.d3;
		fprintf(stderr,"Velociy grid %s: x=%g y=%g t0=%g dt=%g nt=%d\n",
				vgrid2,x5,y5,usgh.o1,usgh.d1,usgh.n1);
		iseek = (iy*usgh.n2+ix)*usgh.n1*usgh.dtype;
		efseek(vfp2,iseek,0);
		efread(v5,sizeof(float),usgh.n1,vfp2);
		for(it=0;it<usgh.n1;it++) t5[it] = usgh.o1 + it*usgh.d1;
		np5 = usgh.n1; 
		if(vgtype2==2) {	
			if(tmax < 100000.) {
				tmp = (tmax - usgh.o1)/usgh.d1;
				nt = tmp;
			} else {
				nt = np5;
			}
			t5s = (float*) malloc(nt*sizeof(float));
			v5s = (float*) malloc(nt*sizeof(float));
			bcopy(t5,t5s,nt*sizeof(float));
			bcopy(v5,v5s,nt*sizeof(float));
		} else if(vgtype2==0) {
			times(dt, t5, np5, time, &nt, tmax);
			t5s = (float*) malloc(nt*sizeof(float));
			v5s = (float*) malloc(nt*sizeof(float));
			bcopy(time,t5s,nt*sizeof(float));
			vrms2vint(t5, v5, np5, t5s, v5s, nt);
		} else if(vgtype2==1) {
			times(dt, t5, np5, time, &nt, tmax);
			t5s = (float*) malloc(nt*sizeof(float));
			v5s = (float*) malloc(nt*sizeof(float));
			bcopy(time,t5s,nt*sizeof(float));
			vavg2vint(t5, v5, np5, t5s, v5s, nt);
		}
		n5s = nt;
		x5min = usgh.o2;
		x5max = usgh.o2 + (usgh.n2 - 1)*usgh.d2;
		y5min = usgh.o3;
		y5max = usgh.o3 + (usgh.n3 - 1)*usgh.d3;
	}

	n6s = 0;
	if(iv3==1) {
		fgetusghdr(vfp3, &usgh);
		tmp = (xqc - usgh.o2)/usgh.d2 + .5;
		ix = tmp;
		if(ix<0) ix = 0;
		if(ix>=usgh.n2) ix = usgh.n2 - 1;
		x6 = usgh.o2 + ix*usgh.d2;
		tmp = (yqc - usgh.o3)/usgh.d3 + .5;
		iy = tmp;
		if(iy<0) iy = 0;
		if(iy>=usgh.n3) iy = usgh.n3 - 1;
		y6 = usgh.o3 + iy*usgh.d3;
		fprintf(stderr,"Velociy grid %s: x=%g y=%g t0=%g dt=%g nt=%d\n",
				vgrid3,x6,y6,usgh.o1,usgh.d1,usgh.n1);
		iseek = (iy*usgh.n2+ix)*usgh.n1*usgh.dtype;
		efseek(vfp3,iseek,0);
		efread(v6,sizeof(float),usgh.n1,vfp3);
		for(it=0;it<usgh.n1;it++) t6[it] = usgh.o1 + it*usgh.d1;
		np6 = usgh.n1; 
		if(vgtype3==2) {
			if(tmax < 100000.) {
				tmp = (tmax - usgh.o1)/usgh.d1;
				nt = tmp;
			} else {
				nt = np6;
			}
			t6s = (float*) malloc(nt*sizeof(float));
			v6s = (float*) malloc(nt*sizeof(float));
			bcopy(t6,t6s,nt*sizeof(float));
			bcopy(v6,v6s,nt*sizeof(float));
		} else if(vgtype3==0) {
			times(dt, t6, np6, time, &nt, tmax);
			t6s = (float*) malloc(nt*sizeof(float));
			v6s = (float*) malloc(nt*sizeof(float));
			bcopy(time,t6s,nt*sizeof(float));
			vrms2vint(t6, v6, np6, t6s, v6s, nt);
		} else if(vgtype3==1) {
			times(dt, t6, np6, time, &nt, tmax);
			t6s = (float*) malloc(nt*sizeof(float));
			v6s = (float*) malloc(nt*sizeof(float));
			bcopy(time,t6s,nt*sizeof(float));
			vavg2vint(t6, v6, np6, t6s, v6s, nt);
		}
		n6s = nt;
		x6min = usgh.o2;
		x6max = usgh.o2 + (usgh.n2 - 1)*usgh.d2;
		y6min = usgh.o3;
		y6max = usgh.o3 + (usgh.n3 - 1)*usgh.d3;
	}

	bzero(cmd,1024);
	bzero(cname,80);
	sprintf(cname,"coordinate.%d\0",getpid());

	if(paper==0) {
        	sprintf(cmd, "( <%s xgraph n=%d,%d,%d,%d,%d,%d,0,0,0,%d,%d,%d nplot=12 marksize=12,12,12,12,12,12,12,12,12,12,12,12 mark=0,1,2,3,4,5,0,1,2,3,4,5 label1=\"x (inline)\" label2=\"y (crossline)\" width=400 height=400 grid1=dash grid2=dash title=\"+=vs3d1 *=vs3d2 x=vs3d3 tri=vgrid1 sq=vgrid2 o=vgrid3\" ; /bin/rm -f %s ) & ", cname, i1, i2, i3, 5*iv1, 5*iv2, 5*iv3, iv1, iv2, iv3, cname);
	} else {
        	sprintf(cmd, "( <%s psgraph n=%d,%d,%d,%d,%d,%d,%d,%d,%d nplot=9 mark=0,1,2,3,4,5,3,4,5 marksize=12,12,12,12,12,12,12,12,12 label1=\"x (inline)\" label2=\"y (crossline)\" wbox=5 hbox=5 grid1=dash grid2=dash title=\"+=vs3d1 *=vs3d2 x=vs3d3 tri=vgrid1 sq=vgrid2 o=vgrid3\" labelsize=14 titlesize=16 | lpr ; /bin/rm -f %s ) & ", cname, i1, i2, i3, 5*iv1, 5*iv2, 5*iv3, iv1, iv2, iv3, cname);
	}


	datafp = efopen(cname,"w");

	if(i1==1) {
		efwrite(&x1,sizeof(float),1,datafp);
		efwrite(&y1,sizeof(float),1,datafp);
	}
	if(i2==1) {
		efwrite(&x2,sizeof(float),1,datafp);
		efwrite(&y2,sizeof(float),1,datafp);
	}
	if(i3==1) {
		efwrite(&x3,sizeof(float),1,datafp);
		efwrite(&y3,sizeof(float),1,datafp);
	}
	if(iv1==1) {
		efwrite(&x4min,sizeof(float),1,datafp);
		efwrite(&y4min,sizeof(float),1,datafp);
		efwrite(&x4max,sizeof(float),1,datafp);
		efwrite(&y4min,sizeof(float),1,datafp);
		efwrite(&x4max,sizeof(float),1,datafp);
		efwrite(&y4max,sizeof(float),1,datafp);
		efwrite(&x4min,sizeof(float),1,datafp);
		efwrite(&y4max,sizeof(float),1,datafp);
		efwrite(&x4min,sizeof(float),1,datafp);
		efwrite(&y4min,sizeof(float),1,datafp);
	}
	if(iv2==1) {
		efwrite(&x5min,sizeof(float),1,datafp);
		efwrite(&y5min,sizeof(float),1,datafp);
		efwrite(&x5max,sizeof(float),1,datafp);
		efwrite(&y5min,sizeof(float),1,datafp);
		efwrite(&x5max,sizeof(float),1,datafp);
		efwrite(&y5max,sizeof(float),1,datafp);
		efwrite(&x5min,sizeof(float),1,datafp);
		efwrite(&y5max,sizeof(float),1,datafp);
		efwrite(&x5min,sizeof(float),1,datafp);
		efwrite(&y5min,sizeof(float),1,datafp);
	}
	if(iv3==1) {
		efwrite(&x6min,sizeof(float),1,datafp);
		efwrite(&y6min,sizeof(float),1,datafp);
		efwrite(&x6max,sizeof(float),1,datafp);
		efwrite(&y6min,sizeof(float),1,datafp);
		efwrite(&x6max,sizeof(float),1,datafp);
		efwrite(&y6max,sizeof(float),1,datafp);
		efwrite(&x6min,sizeof(float),1,datafp);
		efwrite(&y6max,sizeof(float),1,datafp);
		efwrite(&x6min,sizeof(float),1,datafp);
		efwrite(&y6min,sizeof(float),1,datafp);
	}
	if(iv1==1) {
		efwrite(&x4,sizeof(float),1,datafp);
		efwrite(&y4,sizeof(float),1,datafp);
	if(iv2==1) {
		efwrite(&x5,sizeof(float),1,datafp);
		efwrite(&y5,sizeof(float),1,datafp);
	}
	if(iv3==1) {
		efwrite(&x6,sizeof(float),1,datafp);
		efwrite(&y6,sizeof(float),1,datafp);
	}
	}
	efclose(datafp);

	fprintf(stderr,"\n");
	fprintf(stderr,"Displaying Command: \n");
	fprintf(stderr,"%s \n",cmd);

	system(cmd);
	bzero(cname,80);
	sprintf(cname,"data.to.plot.%d\0",getpid());
	datafp = efopen(cname,"w");
	bzero(cmd,1024);
	
	if(paper==0) {
        	sprintf(cmd, "( <%s xgraph n=%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d nplot=12 marksize=6,6,6,6,6,6,6,6,6,6,6,6 mark=0,1,2,3,4,5,0,1,2,3,4,5 linecolor=2,3,7,5,6,4,2,3,7,5,6,4 style=seismic label1=time label2=velocity width=900 height=1000 grid1=dash grid2=dash title=\"%s at x=%g y=%g\" ; /bin/rm -f %s ) & ", cname,ii*2*n1s,ii*2*n2s,ii*2*n3s,ii*2*n4s,ii*2*n5s,ii*2*n6s,ia*n1s,ia*n2s,ia*n3s,ia*n4s,ia*n5s,ia*n6s,title,xqc,yqc,cname);
	} else {
        	sprintf(cmd, "( <%s psgraph n=%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d nplot=12 marksize=6,6,6,6,6,6,6,6,6,6,6,6 style=seismic label1=time label2=velocity wbox=6 hbox=8 grid1=dash grid2=dash title=\"%s at x=%g y=%g\" titlesize=16 labelsize=14 | lpr ; /bin/rm -f %s ) & ", cname,ii*2*n1s,ii*2*n2s,ii*2*n3s,ii*2*n4s,ii*2*n5s,ii*2*n6s,ia*n1s,ia*n2s,ia*n3s,ia*n4s,ia*n5s,ia*n6s,title,xqc,yqc,cname);
	}

	fprintf(stderr,"%s \n",cmd);

	free(work);
	work = (float*) malloc(n1s*4*sizeof(float));
	
	for(it=0;it<n1s;it++) {
		work[it*4] = t1s[it];
		work[it*4+1] = v1s[it];
		work[it*4+2] = t1s[it];
		if(it==n1s-1) {
			work[it*4+3] = v1s[it];
		} else {
			work[it*4+3] = v1s[it+1];
		}
	}
	if(viplot==1) efwrite(work,sizeof(float),4*n1s,datafp);

	free(work);
	work = (float*) malloc(n2s*4*sizeof(float));
	for(it=0;it<n2s;it++) {
		work[it*4] = t2s[it];
		work[it*4+1] = v2s[it];
		work[it*4+2] = t2s[it];
		if(it==n2s-1) {
			work[it*4+3] = v2s[it];
		} else {
			work[it*4+3] = v2s[it+1];
		}
	}
	if(viplot==1) efwrite(work,sizeof(float),4*n2s,datafp);


	free(work);
	work = (float*) malloc(n3s*4*sizeof(float));
	for(it=0;it<n3s;it++) {
		work[it*4] = t3s[it];
		work[it*4+1] = v3s[it];
		work[it*4+2] = t3s[it];
		if(it==n3s-1) {
			work[it*4+3] = v3s[it];
		} else {
			work[it*4+3] = v3s[it+1];
		}
	}
	if(viplot==1) efwrite(work,sizeof(float),4*n3s,datafp);

	free(work);
	work = (float*) malloc(n4s*4*sizeof(float));
	for(it=0;it<n4s;it++) {
		work[it*4] = t4s[it];
		work[it*4+1] = v4s[it];
		work[it*4+2] = t4s[it];
		if(it==n4s-1) {
			work[it*4+3] = v4s[it];
		} else {
			work[it*4+3] = v4s[it+1];
		}
	}
	if(viplot==1) efwrite(work,sizeof(float),4*n4s,datafp);

	free(work);
	work = (float*) malloc(n5s*4*sizeof(float));
	for(it=0;it<n5s;it++) {
		work[it*4] = t5s[it];
		work[it*4+1] = v5s[it];
		work[it*4+2] = t5s[it];
		if(it==n5s-1) {
			work[it*4+3] = v5s[it];
		} else {
			work[it*4+3] = v5s[it+1];
		}
	}
	if(viplot==1) efwrite(work,sizeof(float),4*n5s,datafp);

	free(work);
	work = (float*) malloc(n6s*4*sizeof(float));
	for(it=0;it<n6s;it++) {
		work[it*4] = t6s[it];
		work[it*4+1] = v6s[it];
		work[it*4+2] = t6s[it];
		if(it==n6s-1) {
			work[it*4+3] = v6s[it];
		} else {
			work[it*4+3] = v6s[it+1];
		}
	}
	if(viplot==1) efwrite(work,sizeof(float),4*n6s,datafp);

	if(vauxplot==1) {
		free(work);
		work = (float*) malloc(2*ntmax*sizeof(float));
		if(n1s>0) vrmsout(datafp,t1s,v1s,work,n1s);
		if(n2s>0) vrmsout(datafp,t2s,v2s,work,n2s);
		if(n3s>0) vrmsout(datafp,t3s,v3s,work,n3s);
		if(n4s>0) vrmsout(datafp,t4s,v4s,work,n4s);
		if(n5s>0) vrmsout(datafp,t5s,v5s,work,n5s);
		if(n6s>0) vrmsout(datafp,t6s,v6s,work,n6s);
	
	} else if(vauxplot==2) {
		free(work);
		work = (float*) malloc(2*ntmax*sizeof(float));
		if(n1s>0) vavgout(datafp,t1s,v1s,work,n1s);
		if(n2s>0) vavgout(datafp,t2s,v2s,work,n2s);
		if(n3s>0) vavgout(datafp,t3s,v3s,work,n3s);
		if(n4s>0) vavgout(datafp,t4s,v4s,work,n4s);
		if(n5s>0) vavgout(datafp,t5s,v5s,work,n5s);
		if(n6s>0) vavgout(datafp,t6s,v6s,work,n6s);
	}

	efclose(datafp);

	system(cmd);

	exit(0);

}
Пример #10
0
main(int argc, char **argv)
{
    	FILE *infp=stdin,*outfp;
	FILE *datafp;

	float fx,fy,dx,dy,dvs,tmax,dt,x,y,tmp;
	int nx,ny,nvs,ivs,votype;
	int smx,smy,sms,one;
	int n1,n2,nxy,nxny,np;
	int *nps, *indx;
	int i, j, ix, iy, is, j0, ip;
    	float *xs, *ys, *tpicks, *vpicks, *vv;
    	float *work, *vx, *vtx, *vxy, *vi;
    	float *fsms,*fsmx,*fsmy,*depth,*time,*zs;
	float *vgrid;
	float vmin, vmax, vscale;
	int vintpr, intype, jj;

	int verbose=1;

	int tslice;
	ghed gh;
	float gmin, gmax;
	int ierr;
	int orient=4, gtype=0;

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

	/* required parameters */
	if (!getparfloat("fx",&fx)) 
		err(" fx missing ");
	if (!getparfloat("fy",&fy)) 
		err(" fy missing ");
	if (!getparfloat("dx",&dx)) 
		err(" dx missing ");
	if (!getparfloat("dy",&dy)) 
		err(" dy missing ");
	if (!getparint("nx",&nx)) 
		err(" nx missing ");
	if (!getparint("ny",&ny)) 
		err(" ny missing ");
	if (!getparint("nvs",&nvs)) 
		err(" nvs missing ");
	if (!getparfloat("dvs",&dvs)) 
		err(" dvs missing ");
	/* optional parameters */
	if (!getparint("ivs",&ivs)) ivs = 0;
	if (!getparint("votype",&votype)) votype = 1;
	if (!getparint("smx",&smx)) smx = 1;
	if (!getparint("smy",&smy)) smy = 1;
	if (!getparint("sms",&sms)) sms = 1;
	if (!getparfloat("vscale",&vscale)) vscale = 1.0; 
	if (!getparfloat("vmin",&vmin)) vmin = 1000.0; 
	if (!getparfloat("vmax",&vmax)) vmax = 100000.0; 
	if (!getparint("tslice",&tslice)) tslice = 1;
	if (!getparint("vintpr",&vintpr)) vintpr = 0;
	if (!getparint("intype",&intype)) intype = 1;
	if (!getparint("verbose",&verbose)) verbose=1;

	if (tslice==1) {
		orient = 4;
	} else if (tslice==0) {
		orient = 1;
	}

	if (votype==0) {
		gtype = 1;
	} else if(votype==1) {
		gtype = 3;
	} else if(votype==2) {
		gtype = 2;
	}
	

    	/* at most 4096 input (x,y) VS3D cards with at most 256 time-vel 
		pairs each */

	if (!getparint("nvfmax",&n2)) n2 = 4096;
	if (!getparint("ntvmax",&n1)) n1 = 256;

    	/* arrays used to store all VELF card's cdp, time and velocity */
    	xs = (float*)emalloc(n2*sizeof(float));
    	ys = (float*)emalloc(n2*sizeof(float));
    	tpicks = (float*)emalloc(n1*n2*sizeof(float));
    	vpicks = (float*)emalloc(n1*n2*sizeof(float));
    	vv = (float*)emalloc(n1*sizeof(float));
    	time = (float*)emalloc(nvs*sizeof(float));
    	nps = (int*)emalloc(n2*sizeof(int));
	
	bzero(nps,n2*sizeof(int));
    	/* read in VS3D cards */
    	nxy = 0;
	vs3dread(infp,xs,ys,tpicks,vpicks,&nxy,nps,n1,n2);

	fprintf(stderr," %d VS3D cards read \n",nxy);

	/*
	for(i=0;i<nxy;i++) {
		for(j=0;j<nps[i];j++)
			fprintf(stderr,"x=%f y=%f t=%f v=%f \n",
				xs[i],ys[i],tpicks[j+i*n1],vpicks[j+i*n1]); 
		fprintf(stderr,"\n");
	}
	*/

   	if (nxy==0) err("No VS3D card input ! Job aborted");
	
	/* find out the maximum time */
	tmax = 0.; 
	for(i=0;i<nxy;i++) {
		if(tpicks[i*n1+nps[i]-1] > tmax) tmax = tpicks[i*n1+nps[i]-1];
	}
	/* compute constant time intervals*/
	dt = tmax/(nvs-1);
	for(i=0;i<nvs;i++) time[i] = i*dt;

	vi = (float*) emalloc(nxy*nvs*sizeof(float));
	work = (float*) emalloc(nvs*sizeof(float));
	indx = (int*) emalloc(nvs*sizeof(int));
	fsmx = (float*) emalloc(smx*sizeof(float));
	fsmy = (float*) emalloc(smy*sizeof(float));
	fsms = (float*) emalloc(sms*sizeof(float));
	if(tslice==0) vgrid = (float*) emalloc(nvs*nx*ny*sizeof(float));

	depth = (float*) emalloc(nvs*sizeof(float));
	zs = (float*) emalloc(nvs*sizeof(float));
	for(j=0;j<nvs;j++) zs[j] = j * dvs;
	
	one = 1;

	for(i=0;i<nxy;i++) {
		if(verbose==1) fprintf(stderr, 	
			" %d t-v pairs read at x=%f y=%f location\n",
			nps[i],xs[i],ys[i]);
		j0 = i*nvs;
		np = nps[i];

		if(ivs==0) {
			if(intype==1) {	
			/* interpolate vs3d to nvs times */
				lin1d_(tpicks+i*n1,vpicks+i*n1,&np,zs,
                       			vi+j0,&nvs,indx);
				tmax = tpicks[i*n1+np-1];
				/* compute interval velocities using
					dix formula */

				work[0] = vi[j0]*vi[j0];
				for(j=1;j<nvs;j++) {
					if(zs[j]<=tmax) {
						work[j]=(vi[j+j0]*vi[j+j0]*
								zs[j] -  	
				   			vi[j-1+j0]*vi[j-1+j0]*
								zs[j-1]) /dvs;
					} else {
						work[j] = work[j-1]; 
					}
				}
				for(j=0;j<nvs;j++) vi[j0+j] = sqrt(work[j]);
			} else {
				vv[0] = vpicks[i*n1];
				for(j=1;j<np;j++) {
					vv[j]=(vpicks[i*n1+j]*vpicks[i*n1+j]*
						tpicks[i*n1+j] -  	
					      vpicks[i*n1+j-1]*vpicks[i*n1+j-1]*
						tpicks[i*n1+j-1]) /  
				   	      (tpicks[i*n1+j]-tpicks[i*n1+j-1]);

					vv[j] = sqrt(vv[j]);
				}
				bisear_(&np,&nvs,tpicks+i*n1,zs,indx);
				for(j=0;j<nvs;j++) {
					jj = indx[j] - 1;
					if(jj<0 || zs[j]<= tpicks[i*n1] ) {
						vi[j0+j] = vv[0];
					} else if(jj>np-2) {
						vi[j0+j] = vv[np-1];
					} else if(abs(zs[j]-tpicks[i*n1+jj])
						<0.001*dvs){
						vi[j0+j] = vv[jj];
					} else {
						vi[j0+j] = vv[jj+1];
					}   
				}
			}
		} else {
		/* output in depth */
			if(intype==1) {
                        /* interpolate vs3d to nvs times */
                                lin1d_(tpicks+i*n1,vpicks+i*n1,&np,time,
                                        vi+j0,&nvs,indx);
                                tmax = tpicks[i*n1+np-1];
                                /* compute interval velocities using
                                        dix formula */

                                work[0] = vi[j0]*vi[j0];
                                for(j=1;j<nvs;j++) {
                                        if(time[j]<=tmax) {
                                                work[j]=(vi[j+j0]*vi[j+j0]*
                                                                time[j] -
                                                        vi[j-1+j0]*vi[j-1+j0]*
                                                                time[j-1]) /dt;
                                        } else {
                                                work[j] = work[j-1];
                                        }
                                }
                                for(j=0;j<nvs;j++) work[j] = sqrt(work[j]);
			} else {
				vv[0] = vpicks[i*n1];
                                for(j=1;j<np;j++) {
                                        vv[j]=(vpicks[i*n1+j]*vpicks[i*n1+j]*
                                                tpicks[i*n1+j] -
                                              vpicks[i*n1+j-1]*vpicks[i*n1+j-1]*
                                                tpicks[i*n1+j-1]) / 
                                              (tpicks[i*n1+j]-tpicks[i*n1+j-1]);
					vv[j] = sqrt(vv[j]);
                                }
                                bisear_(&np,&nvs,tpicks+i*n1,time,indx);
                                for(j=0;j<nvs;j++) {
                                        jj = indx[j] - 1;
                                        if(jj<0 || time[j]<= tpicks[i*n1] ) {
                                                work[j] = vv[0];
                                        } else if(jj>np-2) {
                                                work[j] = vv[np-1];
                                        } else if(abs(time[j]-tpicks[i*n1+jj])
                                                <0.001*dt){
                                                work[j] = vv[jj];
                                        } else {
                                                work[j] = vv[jj+1];
                                        }  
                                }

			}
			depth[0] = time[0]*work[0]*0.5*0.001;
			for(j=1;j<nvs;j++) {
				depth[j] = depth[j-1]+
					(time[j]-time[j-1])*work[j]*0.5*0.001;
			}
			lin1d_(depth,work,&nvs,zs,vi+j0,&nvs,indx);
		}
		if(vintpr==1) {
			for(j=0;j<nvs;j++) {
				fprintf(stderr, "  time/depth=%g   vint=%g \n",
						 zs[j],vi[j+j0]);
			}
		}
		/* convert to rms velocity if needed */
		if(votype==0) {
			tmp = 0.;
			for(j=1;j<nvs;j++) {
				tmp = tmp + vi[j0+j]*vi[j0+j]*dvs;
				vi[j0+j] = sqrt(tmp/zs[j]);
			}
		/* convert to average velocity if needed */
		} else if(votype==2) {
			tmp = 0.;
			for(j=1;j<nvs;j++) {
				tmp = tmp + vi[j0+j]*dvs;
				vi[j0+j]=tmp/zs[j];
			}
		}
		/* check velocity range and apply scale */
		for(j=0;j<nvs;j++) {
			tmp = vi[j0+j]*vscale;
			if(tmp<vmin) tmp = vmin;	
			if(tmp>vmax) tmp = vmax;	
			vi[j0+j] = tmp;
		}
		/* smooth if needed */
		if(sms>1) smth2d_(vi+j0,work,fsms,fsmx,&nvs,&one,&sms,&one);

	}

	free(tpicks);
	free(vpicks);
	free(nps);
	free(zs);
	free(depth);
	free(time);
	free(fsms);
	
	
	/* output time/depth slices */
	free(work);
	free(indx);
	work = (float*) malloc(nxy*sizeof(float));
	vtx = (float*) malloc(nvs*nx*sizeof(float));
	vx = (float*) malloc(nx*sizeof(float));
	indx = (int*) malloc(nxy*sizeof(int));

	if(smx==1 && smy==1) {
		datafp = stdout;
	} else {
		datafp = etempfile(NULL);
	}

	np = 3;

	fprintf(stderr," Start x-y plane interpolation \n"); 

	/* 2d interpolation */
	for(iy=0;iy<ny;iy++) {
		y = fy + iy*dy;
		for(ix=0;ix<nx;ix++) {
			x = fx + ix*dx;
			intp2d_(xs,ys,vi,&nvs,&nxy,&x,&y,
					vtx+ix*nvs,&np,indx,work);
		}
		if(tslice==1) {	
			for(is=0;is<nvs;is++) {
				for(ix=0;ix<nx;ix++) vx[ix]=vtx[is+ix*nvs];
				ip = (is*nx*ny+iy*nx)*sizeof(float);
				efseek(datafp,ip,0);
				efwrite(vx,sizeof(float),nx,datafp);
			}
		} else {
			bcopy(vtx,vgrid+iy*nx*nvs,nx*nvs*sizeof(float));
		} 
		if(smx==1 && smy==1) {
			fminmax(vtx,nvs*nx,&gmin,&gmax);
			if(iy==0) {
				vmin = gmin;
				vmax = gmax;
			} else {
				if(vmin>gmin) vmin = gmin;
				if(vmax<gmax) vmax = gmax;
			}
		}
	}

	fprintf(stderr," x-y plane interpolation done \n"); 

	free(work);
	free(indx);
	free(vtx);
	free(xs);
	free(ys);
	free(vi);
	free(vx);
	
	if(smx>1 || smy>1) {
		if(tslice==1) rewind(datafp);
		outfp = stdout;
		nxny = nx * ny;
		vxy = (float*) emalloc(nxny*sizeof(float));
		work = (float*) emalloc(nxny*sizeof(float));
		for(is=0;is<nvs;is++) {
			if(tslice==1) {
				efread(vxy,sizeof(float),nxny,datafp);
			} else {
				for(iy=0;iy<ny;iy++)
					for(ix=0;ix<nx;ix++)
						vxy[ix+iy*nx]=
						   vgrid[(iy*nx+ix)*nvs+is];
			}
			smth2d_(vxy,work,fsmx,fsmy,&nx,&ny,&smx,&smy);
			if(tslice==1) {
				efwrite(vxy,sizeof(float),nxny,outfp);
			} else {
				for(iy=0;iy<ny;iy++)
					for(ix=0;ix<nx;ix++)
						vgrid[(iy*nx+ix)*nvs+is]=
							vxy[ix+iy*nx];
			}
			fminmax(vxy,ny*nx,&gmin,&gmax);
			if(is==0) {
				vmin = gmin;
				vmax = gmax;
			} else {
				if(vmin>gmin) vmin = gmin;
				if(vmax<gmin) vmax = gmax;
			}
		}
		free(vxy);
		free(work);
	}
	free(fsmx);
	free(fsmy);

	if(tslice==0) {
		if(smx==1 && smy==1) outfp = datafp; 
		efwrite(vgrid,sizeof(float),nx*ny*nvs,outfp);
	} else {
		if(smx==1 && smy==1) outfp = datafp; 
	}

	fflush(outfp);


	bzero((char*)&gh,GHDRBYTES);
	gh.scale = 1.e-6;
	if(tslice==1) {
		putgval(&gh,"n1",(float)nx);
		putgval(&gh,"n2",(float)ny);
		putgval(&gh,"n3",(float)nvs);
		putgval(&gh,"o1",fx);
		putgval(&gh,"o2",fy);
		putgval(&gh,"o3",0.);
		putgval(&gh,"d1",dx);
		putgval(&gh,"d2",dy);
		putgval(&gh,"d3",dvs);
	} else {
		putgval(&gh,"n1",(float)nvs);
		putgval(&gh,"n2",(float)nx);
		putgval(&gh,"n3",(float)ny);
		putgval(&gh,"o1",0.);
		putgval(&gh,"o2",fx);
		putgval(&gh,"o3",fy);
		putgval(&gh,"d1",dvs);
		putgval(&gh,"d2",dx);
		putgval(&gh,"d3",dy);
	}


	putgval(&gh,"dtype",4.);
	putgval(&gh,"gmin",vmin);
	putgval(&gh,"gmax",vmax);
	putgval(&gh,"gtype",gtype);
	putgval(&gh,"orient",orient);


	ierr = fputghdr(outfp,&gh);
	if(ierr!=0) err("error output grid header ");
	return 0;

}
Пример #11
0
int fgettra(FILE *fp, segy *tp, int itr)
{
	static int nsfirst;		/* samples on first trace	*/
	static int nsegy;		/* size of trace in bytes	*/
	static int ntr;			/* number of traces in file	*/
	static bool first=true;		/* flag for first entry		*/
	int nread;			/* bytes read			*/
	static char card[EBCBYTES];
	static char bhdr[BNYBYTES];
	static int hdbytes;

	if (first) {	/* first entry; get number of traces */
		unsigned short bytesper;/* bytes per float (packed?)	*/
		long length;		/* length of file in bytes	*/
		filetype ftype = filestat(fileno(fp));

		first = false;

		switch(ftype) {
		case TTY:
			warn("stdin not redirected");
			selfdoc();
		break;
		case DISK:	/* correct */
		break;
		default:
			err("%s: input must be disk file", __FILE__);
		break;
		}
		
		efread(card, 1, 10, fp);
                if (strncmp(card, "C 1 CLIENT",10)==0 ) {
			efseek(fp, EBCBYTES+BNYBYTES, 0);
			hdbytes = EBCBYTES + BNYBYTES;
		}
		else {
			efseek(fp, 0, 0);
			hdbytes = 0;
			
		}
		   
		
		if (HDRBYTES != (nread = efread(tp, 1, HDRBYTES, fp))) {
			err("%s: read only %d bytes of header",
							__FILE__, nread);
		}

		nsfirst = tp->ns;
		if (nsfirst > SU_NFLTS) {
			err("%s: trace too long: nsfirst=%d>SU_NFLTS=%d",
				__FILE__, nsfirst, SU_NFLTS);
		}

		if      (tp->trid == CHARPACK)  bytesper = sizeof(char);
		else if (tp->trid == SHORTPACK) bytesper = sizeof(short);
		else                            bytesper = sizeof(float);

		nsegy = HDRBYTES + nsfirst * bytesper;
		efseek(fp, 0, SEEK_END);
		length = eftell(fp);
		ntr = (length-hdbytes) / nsegy;

	} /* end first entry */


	/* Check on requested trace number */
	if (itr >= ntr)  err("%s, trying to read off end of file", __FILE__);


	/* Position file pointer at start of requested trace */
	efseek(fp, itr*nsegy+hdbytes, SEEK_SET);

	nread = efread(tp, 1, nsegy, fp);
	if (nread != nsegy)
		err("%s: read only %d of %d bytes in trace",
						__FILE__, nread, nsegy);

	if (tp->ns != nsfirst)
	    warn("%s: header ns field = %d differs from first trace = %d",
						__FILE__, tp->ns, nsfirst);

	return ntr;
}
Пример #12
0
int main(int argc, char **argv)
{
	FILE *infp=stdin, *outfp=stdout;

    	int n1,n2,n3,n4,n5;
    	float o1,o2,o3,o4,o5,d1,d2,d3,d4,d5;
	float scale; 
	int dtype;
	float ocdp2,dcdp2,oline3,dline3,gmin,gmax;
	int orient, gtype=0;
	
    int n1o,n2o,n3o,n4o,n5o;
    float o1o,o2o,o3o,o4o,o5o;
	float d1o,d2o,d3o,d4o,d5o;
	int i1,i2,i3,i4,i5;

	int ierr, iseek, oaxes, ltrace, larray;
	float *trace, tmp, *array;
	float sscale;
	int mlimit, m3, k3, j3, m2, k2, j2;

	ghed gh;

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

	/* get input grid parameters */
    	ierr = fgetghdr(infp,&gh);
	if(ierr==0) {
		fromghdr(&gh,&scale,&dtype,&n1,&n2,&n3,&n4,&n5,
               	 		&d1,&d2,&d3,&d4,&d5,&o1,&o2,&o3,&o4,&o5,
                		&dcdp2,&dline3,&ocdp2,&oline3,&gmin,&gmax,
				&orient,&gtype);
	} else {
		err(" use program transp for nonstandard grid file"); 
	}

	/* get update parameters */
	if (!getparint("oaxes", &oaxes)) oaxes = 312;
	if (!getparint("mlimit", &mlimit)) mlimit = 512;


	if(n2==0) n2=1;
	if(n3==0) n3=1;
	if(n4==0) n4=1;
	if(n5==0) n5=1;

	if (oaxes==123) {
		trace = (float*) malloc(n1*sizeof(float));
		for(i2=0;i2<n2*n3*n4*n5;i2++) {
			efread(trace,sizeof(float),n1,infp);
			efwrite(trace,sizeof(float),n1,outfp);
		}
		free(trace);
		fflush(outfp);
		ierr = fputghdr(outfp,&gh);
		if(ierr!=0) warn(" fputghdr error \n");
	} else if(oaxes==213) {
		trace = (float*) malloc(n2*sizeof(float));
		array = (float*) malloc(n1*n2*sizeof(float));
		for(i3=0;i3<n3*n4*n5;i3++) {
			efread(array,sizeof(float),n1*n2,infp);
			for(i1=0;i1<n1;i1++) {
				for(i2=0;i2<n2;i2++) 
					trace[i2] = array[i1+i2*n1];
				efwrite(trace,sizeof(float),n2,outfp);
			}
		}
		free(trace);
		free(array);
		n1o = n2;
		n2o = n1;
		n3o = n3;
		d1o = d2;
		d2o = d1;
		d3o = d3;
		o1o = o2;
		o2o = o1;
		o3o = o3;
		if(orient==1) {
			orient = 3;
		} else if (orient==2) {
		        orient = 5;
		} else if (orient==3) {
		      	orient = 1;
		} else if (orient==4) {
		       	orient = 6;
		} else if (orient==5) {
		       	orient = 2;
		} else if (orient==6) {
		       	orient = 4;
		}
	
		fflush(outfp);
		toghdr(&gh,&scale,&dtype,&n1o,&n2o,&n3o,&n4,&n5,
               		&d1o,&d2o,&d3o,&d4,&d5,&o1o,&o2o,&o3o,&o4,&o5,
               		&dcdp2,&dline3,&ocdp2,&oline3,&gmin,&gmax,
			&orient,&gtype);
		ierr = fputghdr(outfp,&gh);
		if(ierr!=0) warn(" fputghdr error \n");
	} else if(oaxes==132) {
		trace = (float*) malloc(n1*sizeof(float));
		for(i5=0;i5<n5;i5++) {
			for(i4=0;i4<n4;i4++) {
				for(i2=0;i2<n2;i2++) {
					for(i3=0;i3<n3;i3++) {
						iseek = sizeof(float)*n1*
							(i2+i3*n2 
							+ i4*n3*n2+i5*n4*n3*n2);
						efseek(infp,iseek,0);
						efread(trace,sizeof(float),
							n1,infp);
						efwrite(trace,sizeof(float),
							n1,outfp);
					}
				}
			}
		}
		free(trace);
		n1o = n1;
		n2o = n3;
		n3o = n2;
		d1o = d1;
		d2o = d3;
		d3o = d2;
		o1o = o1;
		o2o = o3;
		o3o = o2;
		if(orient==1) {
			orient = 2;
		} else if (orient==2) {
		        orient = 1;
		} else if (orient==3) {
		      	orient = 4;
		} else if (orient==4) {
		       	orient = 3;
		} else if (orient==5) {
		       	orient = 6;
		} else if (orient==6) {
		       	orient = 5;
		}
		fflush(outfp);
		toghdr(&gh,&scale,&dtype,&n1o,&n2o,&n3o,&n4,&n5,
               		&d1o,&d2o,&d3o,&d4,&d5,&o1o,&o2o,&o3o,&o4,&o5,
               		&dcdp2,&dline3,&ocdp2,&oline3,&gmin,&gmax,
			&orient,&gtype);
		ierr = fputghdr(outfp,&gh);
		if(ierr!=0) warn(" fputghdr error \n");
	} else if(oaxes==312) {
		tmp = mlimit * 1024*1024;
		tmp = tmp / (n1*n3*sizeof(float));
		m2 = tmp;
		if (m2>n2) m2 = n2;
		trace = (float*) malloc(n1*n3*sizeof(float));
		array = (float*) malloc(m2*n1*n3*sizeof(float));
		for(i5=0;i5<n5;i5++) {
			for(i4=0;i4<n4;i4++) {

				for(i2=0;i2<n2;i2=i2+m2) {
					k2 = m2;
					if(i2+k2>n2) k2 = n2 - i2;

					for(i3=0;i3<n3;i3++) {

						iseek = sizeof(float)*n1*(i2+i3*n2 
							+ i4*n3*n2+i5*n4*n3*n2);
						efseek(infp,iseek,0);
						efread(array+i3*n1*k2,sizeof(float),n1*k2,infp);
					}

					for(j2=0;j2<k2;j2++) {
						for(i1=0;i1<n1;i1++)
							for(i3=0;i3<n3;i3++)
								trace[i3+i1*n3]=array[i3*n1*k2+j2*n1+i1];
						efwrite(trace,sizeof(float),n1*n3,outfp);
					}
				}
			}
		}
		free(trace);
		free(array);
		n1o = n3;
		n2o = n1;
		n3o = n2;
		d1o = d3;
		d2o = d1;
		d3o = d2;
		o1o = o3;
		o2o = o1;
		o3o = o2;
		if(orient==1) {
			orient = 5;
		} else if (orient==2) {
		        orient = 3;
		} else if (orient==3) {
		      	orient = 6;
		} else if (orient==4) {
		       	orient = 1;
		} else if (orient==5) {
		       	orient = 4;
		} else if (orient==6) {
		       	orient = 2;
		}
		fflush(outfp);
		toghdr(&gh,&scale,&dtype,&n1o,&n2o,&n3o,&n4,&n5,
               		&d1o,&d2o,&d3o,&d4,&d5,&o1o,&o2o,&o3o,&o4,&o5,
               		&dcdp2,&dline3,&ocdp2,&oline3,&gmin,&gmax,
			&orient,&gtype);
		ierr = fputghdr(outfp,&gh);
		if(ierr!=0) warn(" fputghdr error \n");
	} else if(oaxes==231) {
		tmp = mlimit * 1024*1024;
		tmp = tmp / (n1*n2*sizeof(float));
		m3 = tmp;
		if (m3>n3) m3 = n3;
		array = (float*) malloc(m3*n1*n2*sizeof(float));
		trace = (float*) malloc(n2*m3*sizeof(float));
		for(i2=0;i2<n2;i2++) trace[i2] = 0.;
		for(i5=0;i5<n5*n4*n3*n1;i5++) efwrite(trace,sizeof(float),n2,outfp);

		for(i5=0;i5<n5;i5++) {
			for(i4=0;i4<n4;i4++) {
				for(i3=0;i3<n3;i3=i3+m3) {
					k3 = m3;
					if(i3+k3>n3) k3 = n3 - i3;

					iseek = sizeof(float)*n1*n2*(i3+i4*n3+i5*n4*n3);
					efseek(infp,iseek,0);
					efread(array,sizeof(float),k3*n1*n2,infp);

					for(i1=0;i1<n1;i1++) {

						for(j3=0;j3<k3;j3++)
						   	for(i2=0;i2<n2;i2++)
								trace[i2+j3*n2]=array[j3*n1*n2+i2*n1+i1];

						iseek = sizeof(float)*n2*(i3+i1*n3+i4*n3*n1+
													i5*n4*n3*n1);

						efseek(outfp,iseek,0);
						efwrite(trace,sizeof(float),n2*k3,outfp);

					}
				}
			}
		}
		free(trace);
		free(array);
		n1o = n2;
		n2o = n3;
		n3o = n1;
		d1o = d2;
		d2o = d3;
		d3o = d1;
		o1o = o2;
		o2o = o3;
		o3o = o1;
		if(orient==1) {
			orient = 4;
		} else if (orient==2) {
		        orient = 6;
		} else if (orient==3) {
		      	orient = 2;
		} else if (orient==4) {
		       	orient = 5;
		} else if (orient==5) {
			orient = 1;
		} else if (orient==6) {
			orient = 3;
		}
		fflush(outfp);
		toghdr(&gh,&scale,&dtype,&n1o,&n2o,&n3o,&n4,&n5,
               		&d1o,&d2o,&d3o,&d4,&d5,&o1o,&o2o,&o3o,&o4,&o5,
               		&dcdp2,&dline3,&ocdp2,&oline3,&gmin,&gmax,
			&orient,&gtype);
		ierr = fputghdr(outfp,&gh);
		if(ierr!=0) warn(" fputghdr error \n");
	}

	efclose(outfp);
	efclose(infp);

	exit(0);
}
Пример #13
0
void sc3dread(FILE *infp, float *xs, float *ys, float *ts, float *ss,
        int *nxy, int *nps, int maxp, int maxnxy) {

        int icmax, ic, cdpnow=0, cdppre=-1, cdpchange=0, jc=0;
        int i1, i;
	float ftime, fscale;
        char *cbuf, x[9], y[9], scale[9], time[9];
        int cardfound=0;
        float xnow, ynow, xpre, ypre;

        icmax = maxnxy * maxp;

        cbuf = (char *) malloc(81*sizeof(char));

        /* rewind infp */
        efseek(infp,0,0);


        for (ic=0;ic<icmax;ic++) {
                if(feof(infp) !=0) break;
                bzero(cbuf,81);
                fgets(cbuf,81,infp);

                if(strncmp(cbuf, "SC3D",4)==0) {
                        cardfound = 1;

			x[8] = '\0';
                        strncpy(x,&cbuf[8],8);
                        if(strncmp(x, "        ",8)!=0) {
                                xnow = atof(x);
                        } else {
                                xnow = xpre;
                        }
			y[8] = '\0';
                        strncpy(y,&cbuf[16],8);
                        if(strncmp(y, "        ",8)!=0) {
                                ynow = atof(y);
                        } else {
                                ynow = ypre;
                        }

                        if(cdppre == -1) {
                                xpre = xnow;
                                ypre = ynow;
                                cdppre = 0;
                        }

                        if( (xpre==xnow && ypre==ynow) ) {
                                cdpnow = cdppre;
                        } else {
                                cdpnow = cdppre + 1;
                        }

                        /* if cdp changes */
                        if (cdpnow != cdppre ) {
                                nps[cdpchange] = jc;
                                xs[cdpchange] = xpre;
                                ys[cdpchange] = ypre;
                                cdpchange += 1;
                                jc = 0;
                                cdppre = cdpnow;
                                xpre = xnow;
                                ypre = ynow;
                        }

                        /* store read values in tpicks and vpicks arrays */
                        for(i=0;i<3;i++) {
				ftime = 0.;
				fscale = 0.;
				time[8] = '\0';
                                strncpy(time,&cbuf[24+i*16],8);
				sscanf(time,"%f",&ftime);
				scale[8] = '\0';
                                strncpy(scale,&cbuf[32+i*16],8);
				sscanf(scale,"%f",&fscale);

                                if(ftime==0. && fscale==0. && jc>0) break;

                                ts[jc+cdpchange*maxp] = ftime;
                                ss[jc+cdpchange*maxp] = fscale;
                                jc = jc + 1;
                        }
                }
        }
/* last input cdp location */
        if(cardfound==1) {
                xs[cdpchange] = xpre;
                ys[cdpchange] = ypre;
                nps[cdpchange] = jc;
                *nxy = cdpchange + 1;
        } else {
                *nxy = 0;
        }
        if(*nxy>maxnxy)
                err("number of (x,y) of SC3D cards exceeds %d \n",maxnxy);
        free(cbuf);
}
Пример #14
0
bool lds_load(SDL_RWops *f, unsigned int music_offset, unsigned int music_size)
{
	SoundBank *sb;
	
	efseek(f, music_offset, SEEK_SET);

	/* load header */
	mode = efgetc(f);
	if (mode > 2)
	{
		fprintf(stderr, "error: failed to load music\n");
		return false;
	}
	efread(f, &speed, 2, 1);
	tempo = efgetc(f);
	pattlen = efgetc(f);
	for (unsigned int i = 0; i < 9; i++)
		chandelay[i] = efgetc(f);
	regbd = efgetc(f);

	/* load patches */
	efread(f, &numpatch, 2, 1);

	free(soundbank);
	soundbank = (SoundBank *)malloc(sizeof(SoundBank) * numpatch);

	for (unsigned int i = 0; i < numpatch; i++)
	{
		sb = &soundbank[i];
		sb->mod_misc = efgetc(f);
		sb->mod_vol = efgetc(f);
		sb->mod_ad = efgetc(f);
		sb->mod_sr = efgetc(f);
		sb->mod_wave = efgetc(f);
		sb->car_misc = efgetc(f);
		sb->car_vol = efgetc(f);
		sb->car_ad = efgetc(f);
		sb->car_sr = efgetc(f);
		sb->car_wave = efgetc(f);
		sb->feedback = efgetc(f);
		sb->keyoff = efgetc(f);
		sb->portamento = efgetc(f);
		sb->glide = efgetc(f);
		sb->finetune = efgetc(f);
		sb->vibrato = efgetc(f);
		sb->vibdelay = efgetc(f);
		sb->mod_trem = efgetc(f);
		sb->car_trem = efgetc(f);
		sb->tremwait = efgetc(f);
		sb->arpeggio = efgetc(f);
		for (unsigned int j = 0; j < 12; j++)
			sb->arp_tab[j] = efgetc(f);
		efread(f, &sb->start, 2, 1);
		efread(f, &sb->size, 2, 1);
		sb->fms = efgetc(f);
		efread(f, &sb->transp, 2, 1);
		sb->midinst = efgetc(f);
		sb->midvelo = efgetc(f);
		sb->midkey = efgetc(f);
		sb->midtrans = efgetc(f);
		sb->middum1 = efgetc(f);
		sb->middum2 = efgetc(f);
	}
	
	/* load positions */
	efread(f, &numposi, 2, 1);
	
	free(positions);
	positions = (Position *)malloc(sizeof(Position) * 9 * numposi);
	
	for (unsigned int i = 0; i < numposi; i++)
	{
		for (unsigned int j = 0; j < 9; j++)
		{
			/*
			* patnum is a pointer inside the pattern space, but patterns are 16bit
			* word fields anyway, so it ought to be an even number (hopefully) and
			* we can just divide it by 2 to get our array index of 16bit words.
			*/
			Uint16 temp;
			efread(f, &temp, 2, 1);
			positions[i * 9 + j].patnum = temp / 2;
			positions[i * 9 + j].transpose = efgetc(f);
		}
	}
	
	/* load patterns */
	efseek(f, 2, SEEK_CUR); /* ignore # of digital sounds (dunno what this is for) */
	
	unsigned int remaining = music_size - (eftell(f) - music_offset);
	
	free(patterns);
	patterns = (Uint16 *)malloc(sizeof(Uint16) * (remaining / 2));
	
	for (unsigned int i = 0; i < remaining / 2; i++)
		efread(f, &patterns[i], 2, 1);
	
	lds_rewind();
	
	return true;
}
Пример #15
0
main(int argc, char **argv)
{
    	FILE *infp=stdin,*outfp=stdout;

	int ncdpvgrid,ntvgrid,ncdpvelf,ntvelf;
	float fcdpvgrid,dcdpvgrid,ftvgrid,dtvgrid,tmp;
	float fcdpvelf,dcdpvelf,ftvelf,dtvelf;
	int ivtype,ovtype;
	int ittype,ottype;

    	float *tin, *tout, *vin, *vout;
	float ocdp; 
	int icdp, p1, p2, ic, nc, ip, it, ivgrid; 

	ghed gh;
	int ierr;
	float vmax, vmin; 

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

	ierr = fgetghdr(infp,&gh);
	if (!getparfloat("fcdpvgrid",&fcdpvgrid)) { 
		if(ierr==0) {
			getgval(&gh,"ocdp2",&fcdpvgrid);
		} else {
			err(" fcdpvgrid missing ");
		}  
	}
	if (!getparfloat("dcdpvgrid",&dcdpvgrid)) { 
		if(ierr==0) {
			getgval(&gh,"dcdp2",&dcdpvgrid);
		} else {
			err(" dcdpvgrid missing ");
		}  
	}
	if (!getparint("ncdpvgrid",&ncdpvgrid)) { 
		if(ierr==0) {
			getgval(&gh,"n2",&tmp);
			ncdpvgrid = (int)tmp;
		} else {
			err(" ncdpvgrid missing ");
		}  
	}
	if (!getparfloat("ftvgrid",&ftvgrid)) { 
		if(ierr==0) {
			getgval(&gh,"o1",&ftvgrid);
		} else {
			err(" ftvgrid missing ");
		}  
	}
	if (!getparfloat("dtvgrid",&dtvgrid)) { 
		if(ierr==0) {
			getgval(&gh,"d1",&dtvgrid);
		} else {
			err(" dtvgrid missing ");
		}  
	}
	if (!getparint("ntvgrid",&ntvgrid)) { 
		if(ierr==0) {
			getgval(&gh,"n1",&tmp);
			ntvgrid = (int)tmp;
		} else {
			err(" ntvgrid missing ");
		}  
	}

	if (!getparfloat("fcdpvelf",&fcdpvelf)) fcdpvelf = fcdpvgrid;
	if (!getparfloat("dcdpvelf",&dcdpvelf)) dcdpvelf = 1.; 
	if (!getparint("ncdpvelf",&ncdpvelf)) ncdpvelf = 1;
	if (!getparfloat("ftvelf",&ftvelf)) ftvelf = ftvgrid;
	if (!getparfloat("dtvelf",&dtvelf)) dtvelf = 10. * dtvgrid;
	if (!getparint("ntvelf",&ntvelf)) ntvelf = ntvgrid/10;

	if (!getparint("ivtype",&ivtype)) ivtype=0;
	if (!getparint("ovtype",&ovtype)) ovtype=0;
	if (!getparint("ittype",&ittype)) ittype=0;
	if (!getparint("ottype",&ottype)) ottype=0;

    	tin = (float*)malloc(ntvgrid*sizeof(float));
    	vin = (float*)malloc(ntvgrid*sizeof(float));
    	tout = (float*)malloc(ntvelf*sizeof(float));
    	vout = (float*)malloc(ntvelf*sizeof(float));

	for(it=0;it<ntvgrid;it++) tin[it] = ftvgrid + it*dtvgrid;
	for(it=0;it<ntvelf;it++) tout[it] = ftvelf + it*dtvelf;

	for(icdp=0;icdp<ncdpvelf;icdp++) {
		ocdp = fcdpvelf + icdp * dcdpvelf;
		tmp = (ocdp - fcdpvgrid)/dcdpvgrid;
		ivgrid = (int) tmp;
		if(ivgrid<0) ivgrid=0;
		if(ivgrid>=ncdpvgrid)  ivgrid=ncdpvgrid-1;
		efseek(infp,ivgrid*ntvgrid*sizeof(float),0);
		efread(vin,sizeof(float),ntvgrid,infp);
		/* time/depth conversion if needed */
		vconvert(tin,vin,ntvgrid,ivtype,ittype,
				tout,vout,ntvelf,ovtype,ottype);
		for(ic=0;ic<ntvelf;ic=ic+5) {
			if(ic==0) {
				fprintf(outfp,"VELF %10d     ",(int)ocdp);
			} else {
				fprintf(outfp,"VELF                ");
			}
			nc = 5;
			if(ic+nc>ntvelf) nc = ntvelf - ic;
			for(ip=0;ip<nc;ip++) {
				p1 = (int) tout[ic+ip];
				p2 = (int) vout[ic+ip];
				fprintf(outfp,"%5d%5d",p1,p2);
			} 
			fprintf(outfp,"\n");
		}
		fprintf(stderr,"Output VELF at cdp=%d \n",(int)ocdp);
	}
	
}
Пример #16
0
main(int argc, char **argv)
{
    	FILE *infp=stdin,*outfp=stdout;
		float *deldt, *mw, *dtn;
		float *del, *mwo, *vi;
		int *indx, nmw, unit, otype;
		float *z, *logdtn;
		float a, b;
		float zmin, mwzmin, mwfz, temp;
		int izmin;
		int nzread=0;
		char *jpfile;
		FILE *jpfp;

		float gmin, gmax;
		int n2, n3;
		usghed usgh;
		int ierr; 

		int i1, i2, i3;
		float fz, dz;
		int nz;

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

		if (!getparfloat("a",&a) || !getparfloat("b",&b)) {
			nzread = countparval("logdtn");
			i1 = countparval("z");
			if(i1!=nzread) err(" check z and logdtn \n");
			if(nzread==0) err(" parameters a and b or logdtn and z missing");
			z = (float*) malloc(nzread*sizeof(float));
			logdtn = (float*) malloc(nzread*sizeof(float));
			getparfloat("z",z);
			getparfloat("logdtn",logdtn);
	    }
		nmw = 0;
		nmw = countparval("deldt");
		if(nmw != countparval("mw")) err(" check deldt and mw \n");
		if (nmw>0) {
			deldt = (float*) malloc(nmw*sizeof(float));
			mw = (float*) malloc(nmw*sizeof(float));
			getparfloat("deldt",deldt);
			getparfloat("mw",mw);
		} else {
			nmw = 14;
			deldt = (float*) malloc(nmw*sizeof(float));
			mw = (float*) malloc(nmw*sizeof(float));
			deldt[0] = 0; mw[0] = 8.7736;
			deldt[1] = 10; mw[1] = 12.8298;
			deldt[2] = 13; mw[2] = 13.7872;
			deldt[3] = 15; mw[3] = 14.3617;
			deldt[4] = 17; mw[4] = 14.9362;
			deldt[5] = 20; mw[5] = 15.5106;
			deldt[6] = 23; mw[6] = 16.1809;
			deldt[7] = 27; mw[7] = 16.7553;
			deldt[8] = 31; mw[8] = 17.1383;
			deldt[9] = 35; mw[9] = 17.5213;
			deldt[10] = 40; mw[10] = 17.8085;
			deldt[11] = 51; mw[11] = 18.4787;
			deldt[12] = 58; mw[12] = 18.7660;
			deldt[13] = 66; mw[13] = 19.1489;
		}
		if (!getparint("unit",&unit)) unit=0;
		if (!getparint("otype",&otype)) otype=0;
		if (!getparstring("jpfile",&jpfile)) {
			jpfp = stderr;
		} else {
			jpfp = fopen(jpfile,"w");
		}
		ierr = fgetusghdr(infp,&usgh);
		fz = usgh.o1;
		dz = usgh.d1;
		nz = usgh.n1;
		n2 = usgh.n2; if(n2==0) n2=1;
		n3 = usgh.n3; if(n3==0) n3=1;

		if(!getparfloat("zmin",&zmin)) zmin = fz;
		zmin = (zmin-fz)/dz+0.5;
		izmin = zmin;
		if(!getparfloat("mwzmin",&mwzmin)) mwzmin = 0.;
		if(!getparfloat("mwfz",&mwfz)) mwfz = 0.;

/* memory allocation */
    	dtn = (float*)malloc(nz*sizeof(float));
		vi = (float*) malloc(nz*sizeof(float));
		del = (float*) malloc(nz*sizeof(float));
		mwo = (float*) malloc(nz*sizeof(float));
		indx = (int*) malloc(nz*sizeof(int));

/* compute normal-compaction trend dtn */
		if(nzread==0) {
			for(i1=0;i1<nz;i1++)
				dtn[i1] = exp(a + b*(fz+i1*dz));
		} else {
			for(i1=0;i1<nz;i1++) {
				vi[i1] = fz + i1*dz;
			}
			bisear_(&nzread,&nz,z,vi,indx);
			linin_(&nzread,&nz,z,vi,indx,logdtn,dtn);
			for(i1=0;i1<nz;i1++) {
				dtn[i1] = exp(dtn[i1]);
			}
		}

/* convert vint to mw */
		efseek(infp,0,0);
		for(i3=0;i3<n3;i3++) {
			for(i2=0;i2<n2;i2++) {
				efread(vi,sizeof(float),nz,infp);
				if(unit==1) {
					for(i1=0;i1<nz;i1++) 
						vi[i1] = vi[i1] * 3.2804;
				}
				for(i1=0;i1<nz;i1++) {
					del[i1] = 1000000./vi[i1] - dtn[i1];
				}
				bisear_(&nmw,&nz,deldt,del,indx);
				linin_(&nmw,&nz,deldt,del,indx,mw,mwo);
				if(mwzmin>0.) { 
					for(i1=0;i1<izmin-1;i1++) {
						mwo[i1] = mwzmin;
					}
				} else if(mwfz>0.) {
					if(izmin>0) temp = (mwo[izmin] - mwfz)/izmin;
					for(i1=0;i1<izmin-1;i1++) {
						mwo[i1] = mwfz + i1*temp;
					}
				} else {
					for(i1=0;i1<izmin-1;i1++) {
						mwo[i1] = mwo[izmin];
					}
				}
				if(otype==1) {
					for(i1=0;i1<nz;i1++)
						mwo[i1] = mwo[i1] * 0.0522222;
				} else if(otype==2) {
					for(i1=0;i1<nz;i1++)
						mwo[i1] = mwo[i1]*0.0522222*(fz+i1*dz);
				}
				if(i2==0 && i3==0) { gmin=mwo[0]; gmax=mwo[0]; }
				for(i1=0;i1<nz;i1++) {
					if(gmin>mwo[i1]) gmin = mwo[i1];
					if(gmax<mwo[i1]) gmax = mwo[i1];
				}
				efwrite(mwo,sizeof(float),nz,outfp);
			}
		}
		usgh.gmin = gmin;
		usgh.gmax = gmax;
		ierr = fputusghdr(outfp,&usgh);
		if(ierr!=0) err(" output grid header error ");
		
		fprintf(jpfp,"   Normal-compaction dt trend: log(dtn)=a+b*z \n");
		if(nzread==0) {
			fprintf(jpfp,"    a=%f    b=%g  \n",a,b);
		} else {
			for(i1=0;i1<nzread;i1++)
			fprintf(jpfp,"    z = %f    logdtn=%f  \n",z[i1],logdtn[i1]);
		}
		fprintf(jpfp," \n");
		fprintf(jpfp,"       Depth          dtn     \n");
		fprintf(jpfp,"     --------------------------- \n");
		for(i1=0;i1<nz;i1++) {
			fprintf(jpfp,"     %10.3f    %10.3f\n",fz+i1*dz,dtn[i1]);
		}
		fprintf(jpfp," \n");
		fprintf(jpfp,"   Delta(dt) versus Mud Weight Relation \n");
		fprintf(jpfp,"   ------------------------------------ \n");
		for(i1=0;i1<nmw;i1++)
		fprintf(jpfp,"   delta(dt)=%10.4f us/ft      mw=%10.4f lb/gal \n", 
			deldt[i1],mw[i1]);
		fprintf(jpfp,"   ------------------------------------ \n");

     free(mwo);
     free(del);
     free(deldt);
     free(dtn);
     free(mw);
     free(vi);
     free(indx);

     return (0);
}