Beispiel #1
0
void rtm(sf_file Fdat, sf_file Fimg, sf_mpi *mpipar, sf_sou soupar, sf_acqui acpar, sf_vec_s array, bool verb)
/*< acoustic rtm >*/
{
	int ix, iz, is, ir, it, wit;
	int sx, rx, sz, rz, frectx, frectz;
	int nz, nx, nzx, padnz, padnx, padnzx, nt, nr, nb, wnt;

	float dx2, dz2, dt2, dt;
	float **vv, **dd, **mm;
	float **p0, **p1, **p2, **term, **tmparray, *rr, ***wave;
	float *sendbuf, *recvbuf;

	//sf_file Fwfl1, Fwfl2;
	//Fwfl1=sf_output("Fwfl1");
	//Fwfl2=sf_output("Fwfl2");

	MPI_Comm comm=MPI_COMM_WORLD;

	nz=acpar->nz;
	nx=acpar->nx;
	nzx=nz*nx;
	padnz=acpar->padnz;
	padnx=acpar->padnx;
	padnzx=padnz*padnx;
	nr=acpar->nr;
	nb=acpar->nb;
	sz=acpar->sz;
	rz=acpar->rz;
	frectx=soupar->frectx;
	frectz=soupar->frectz;

	nt=acpar->nt;
	wnt=(nt-1)/acpar->interval+1;

	dx2=acpar->dx*acpar->dx;
	dz2=acpar->dz*acpar->dz;
	dt2=acpar->dt*acpar->dt;
	dt=acpar->dt;

	//sf_putint(Fwfl1, "n1", padnz);
	//sf_putint(Fwfl1, "n2",padnx);
	//sf_putint(Fwfl1, "n3",(nt-1)/50+1);
	//sf_putint(Fwfl2, "n1", padnz);
	//sf_putint(Fwfl2, "n2",padnx);
	//sf_putint(Fwfl2, "n3",(nt-1)/50+1);

	/* memory allocation */
	vv = sf_floatalloc2(padnz, padnx);
	dd=sf_floatalloc2(nt, nr);
	mm=sf_floatalloc2(nz, nx);

	p0=sf_floatalloc2(padnz, padnx);
	p1=sf_floatalloc2(padnz, padnx);
	p2=sf_floatalloc2(padnz, padnx);
	term=sf_floatalloc2(padnz, padnx);
	rr=sf_floatalloc(padnzx);
	wave=sf_floatalloc3(nz, nx, wnt);

	/* padding and convert vector to 2-d array */
	pad2d(array->vv, vv, nz, nx, nb);

	memset(mm[0], 0., nzx*sizeof(float));

	for(is=mpipar->cpuid; is<acpar->ns; is+=mpipar->numprocs){
		sf_warning("###### is=%d ######", is+1);

		memset(p0[0], 0., padnzx*sizeof(float));
		memset(p1[0], 0., padnzx*sizeof(float));
		memset(p2[0], 0., padnzx*sizeof(float));
		
		sx=acpar->s0_v+is*acpar->ds_v;
		source_map(sx, sz, frectx, frectz, padnx, padnz, padnzx, rr);

		wit=0;
		/* forward propagation */
		for(it=0; it<nt; it++){
			if(verb) sf_warning("Forward propagation is=%d; it=%d;", is+1, it);

			/* save wavefield */
			if(it%acpar->interval==0){
#ifdef _OPENMP 
#pragma omp parallel for \
			private(ix,iz) \
			shared(wave,p1,wit,nb,nx,nz)
#endif
				for(ix=0; ix<nx; ix++)
					for(iz=0; iz<nz; iz++)
						wave[wit][ix][iz]=p1[ix+nb][iz+nb];
				wit++;
			}

			//if(is==acpar->ns/2 && it%50==0) sf_floatwrite(p1[0],padnzx, Fwfl1);

			/* laplacian operator */
			laplace(p1, term, padnx, padnz, dx2, dz2);
			
			/* load source */
#ifdef _OPENMP 
#pragma omp parallel for \
			private(ix,iz) \
			shared(term,rr,padnx,padnz,it)
#endif
			for(ix=0; ix<padnx; ix++){
				for(iz=0; iz<padnz; iz++){
					term[ix][iz] += rr[ix*padnz+iz]*array->ww[it];
				}
			}

			/* update */
#ifdef _OPENMP 
#pragma omp parallel for \
			private(ix,iz) \
			shared(p0,p1,p2,vv,term,padnx,padnz,dt2)
#endif
			for(ix=4; ix<padnx-4; ix++){
				for(iz=4; iz<padnz-4; iz++){
					p2[ix][iz]=2*p1[ix][iz]-p0[ix][iz]+vv[ix][iz]*vv[ix][iz]*dt2*term[ix][iz];
				}
			}
			
			/* swap wavefield pointer of different time steps */
			tmparray=p0; p0=p1; p1=p2; p2=tmparray;

			/* boundary condition */
			apply_sponge(p0, acpar->bc, padnx, padnz, nb);
			apply_sponge(p1, acpar->bc, padnx, padnz, nb);
		} // end of time loop

		/* check */
		if(wit != wnt) sf_error("Incorrect number of wavefield snapshots");
		wit--;

		/* read data */
		sf_seek(Fdat, is*nr*nt*sizeof(float), SEEK_SET);
		sf_floatread(dd[0], nr*nt, Fdat);

		/* initialization */
		memset(p0[0], 0., padnzx*sizeof(float));
		memset(p1[0], 0., padnzx*sizeof(float));
		memset(p2[0], 0., padnzx*sizeof(float));
		memset(term[0], 0., padnzx*sizeof(float));
		
		/* backward propagation */
		for(it=nt-1; it>=0; it--){
			if(verb) sf_warning("Backward propagation is=%d; it=%d;", is+1, it);

			/* laplacian operator */
			laplace(p1, term, padnx, padnz, dx2, dz2);
			
			/* load data */
			for(ir=0; ir<acpar->nr2[is]; ir++){
				rx=acpar->r0_v[is]+ir*acpar->dr_v;
				term[rx][rz] += dd[acpar->r02[is]+ir][it];
			}

			/* update */
#ifdef _OPENMP 
#pragma omp parallel for \
			private(ix,iz) \
			shared(p0,p1,p2,vv,term,padnx,padnz,dt2)
#endif
			for(ix=4; ix<padnx-4; ix++){
				for(iz=4; iz<padnz-4; iz++){
					p2[ix][iz]=2*p1[ix][iz]-p0[ix][iz]+vv[ix][iz]*vv[ix][iz]*dt2*term[ix][iz];
				}
			}

			//if(is==acpar->ns/2 && it%50==0) sf_floatwrite(p1[0],padnzx, Fwfl2);

			/* calculate image */
			if(it%acpar->interval==0){
#ifdef _OPENMP 
#pragma omp parallel for \
			private(ix,iz) \
			shared(p1,wave,nx,nz,wit,mm,nb)
#endif
				for(ix=0; ix<nx; ix++)
					for(iz=0; iz<nz; iz++)
						mm[ix][iz] += wave[wit][ix][iz]*p1[ix+nb][iz+nb];
				wit--;
			}
			
			/* swap wavefield pointer of different time steps */
			tmparray=p0; p0=p1; p1=p2; p2=tmparray;

			/* boundary condition */
			apply_sponge(p0, acpar->bc, padnx, padnz, nb);
			apply_sponge(p1, acpar->bc, padnx, padnz, nb);
		} // end of time loop
	}// end of shot loop
	MPI_Barrier(comm);

	//sf_fileclose(Fwfl1);
	//sf_fileclose(Fwfl2);

	if(mpipar->cpuid==0){
		sendbuf=MPI_IN_PLACE;
		recvbuf=mm[0];
	}else{
		sendbuf=mm[0];
		recvbuf=NULL;
	}
	MPI_Reduce(sendbuf, recvbuf, nzx, MPI_FLOAT, MPI_SUM, 0, comm);

	if(mpipar->cpuid==0) sf_floatwrite(mm[0], nzx, Fimg);
	MPI_Barrier(comm);

	/* release allocated memory */
	free(*p0); free(p0); free(*p1); free(p1);
	free(*p2); free(p2); free(*vv); free(vv);
	free(*dd); free(dd); free(*mm); free(mm);
	free(rr); free(*term); free(term);
	free(**wave); free(*wave); free(wave);
}
Beispiel #2
0
int main(int argc, char* argv[])
{
    int n1, n2, n3, i2, i3, iw, n, dim;
    float o1, d1, m1, a1, m2, a2, f;
    sf_complex *data, *data2=NULL;
    char key[5];
    sf_file in, in2, ma, ma2, out, out2=NULL;

    sf_init(argc,argv);
    in = sf_input("in");

    if (NULL != sf_getstring("in2")) {
	/* optional second input file */
	in2 = sf_input("in2");
    } else {
	in2 = NULL;
    }
    
    ma = sf_input("ma");
    ma2 = sf_input("ma2");

    out = sf_output("out");

    if (NULL != in2) out2 = sf_output("out2");

    if ((SF_COMPLEX != sf_gettype (in)) ||
	(NULL != in2 && 
	 SF_COMPLEX != sf_gettype (in2))) sf_error("Need complex data");

    if (!sf_histint(in,"n1",&n1)) n1=1;

    if (NULL != in2 && sf_histint(in2,"n1",&n) && n != n1)
	sf_error("Size mismatch in in2: %d != %d",n,n1);

    if (!sf_getint("dim",&dim)) dim=1;
    /* data dimensionality */
    sprintf(key,"n%d",dim+1);

    if (!sf_histint(in,key,&n3)) n3=1;

    if (NULL != in2 && sf_histint(in2,key,&n) && n != n3)
	sf_error("Size mismatch in in2 [%s]: %d != %d",key,n,n3);

    n2 = sf_leftsize(in,1);
    n2 /= n3;

    /* n3 is the number of windows, n2xn1 is the window size */

    if (!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"o1",&o1)) o1=0.;
    
    if (!sf_histint(ma,"n1",&n) || n != 2)
	sf_error("Wrong n1= in ma");
    if (!sf_histint(ma2,"n1",&n) || n != 2)
	sf_error("Wrong n1= in ma2");
    if (!sf_histint(ma,"n2",&n) || n != n3)
	sf_error("Wrong n2= in ma");
    if (!sf_histint(ma2,"n2",&n) || n != n3)
	sf_error("Wrong n2= in ma2");
    
    data = sf_complexalloc(n1);
    if (NULL != in2) data2 = sf_complexalloc(n1);

    for (i3=0; i3 < n3; i3++) { /* loop over windows */
	sf_floatread(&m1,1,ma);
	sf_floatread(&a1,1,ma);

	sf_floatread(&m2,1,ma2);
	sf_floatread(&a2,1,ma2);
	
	for (i2=0; i2 < n2; i2++) { /* loop over traces in a window */
	    sf_complexread(data,n1,in);
	    if (NULL != in2) sf_complexread(data2,n1,in2);
	
	    /* Frequency-domain Gaussian scaling and smoothing */
	    for (iw=0; iw < n1; iw++) {
		f = iw*d1;
		f *= f;
		if (m1 > m2) {
		    f = (a2*m1)/(a1*m2)*expf(f*(1./m1-1./m2));
#ifdef SF_HAS_COMPLEX_H
		    data[iw] *= f;
#else
		    data[iw] = sf_crmul(data[iw],f);
#endif
		} else if (NULL != in2) {
		    f = (a1*m2)/(a2*m1)*expf(f*(1./m2-1./m1));
#ifdef SF_HAS_COMPLEX_H
		    data2[iw] *= f;
#else
		    data2[iw] = sf_crmul(data2[iw],f);
#endif
		}
	    }
	    
	    sf_complexwrite(data,n1,out);
	    if (NULL != in2) sf_complexwrite(data2,n1,out2);
	}
    }
    
    exit(0);
}
Beispiel #3
0
int main(int argc,char *argv[])
{
    float x0,y0,x00,y00,t0,d1,d2,d3,o1,o2,o3,k11,k21,k31,k41,k12,k22,k32,k42;
    float kxi,gamma,omega,phi;/*parameter define*/
    int i,j,k,n1,n2,n3,sn1,pow1,pow2;
    double h;
    float  *inpsg,*xt,*xt1,*re;
    sf_complex *out1;
    sf_file in, out, restor=NULL;
    bool verb,cosine;
    
    sf_init (argc, argv); 
    in = sf_input("in");
    out = sf_output("out");

    if (NULL != sf_getstring ("restor")) {
        restor = sf_input("restor");
    } else {
	restor = NULL;
	sn1=1;
    }
    
   
    sf_setformat(out,"native_complex");
    
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"o1",&o1)) sf_error("No o1= in input");
    if (!sf_histint(in,"n2",&n2)) n2=1;
    if (!sf_histfloat(in,"d2",&d2)) d2=1;
    if (!sf_histfloat(in,"o2",&o2)) o2=1;
    if (!sf_histint(in,"n3",&n3)) n3=1;
    if (!sf_histfloat(in,"d3",&d3)) d3=1;
    if (!sf_histfloat(in,"o3",&o3)) o3=1;
    
    if(!sf_getfloat("gamma",&gamma)) gamma=0.75;
    /*strength of external force*/
    if(!sf_getfloat("omega",&omega)) omega=1;
    /*angular frequence of external force*/
    if(!sf_getfloat("kxi",&kxi)) kxi=1;
    /*adjustment for input signal*/
    if(!sf_getfloat("x0",&x0)) x0=0;
    /*initial value of x0*/
    if(!sf_getfloat("y0",&y0)) y0=0;
    /*initial value of y0*/
    if(!sf_getint("pow1",&pow1)) pow1=1;
    /*power of first non-linear restitution term*/
    if(!sf_getint("pow2",&pow2)) pow2=3;
    /*power of second non-linear restitution term*/
    if(!sf_getfloat("phi",&phi)) phi=0.;
    /*phase of cosine signal unit=pi*/
    if(!sf_getbool("verb",&verb)) verb = false;
    /* verbosity flag */
    if(!sf_getbool("cosine",&cosine)) cosine = true;
    /* if n need extenal input for periodic restoring force */
    
    if (NULL != restor){
	
	if (!sf_histint(restor,"n1",&sn1)) sf_error("No n1= in s");
	if (n1>sn1) sf_error("insufficient lenth of external force");
    }
    
    
    h=d1;
    /*step lenth of R-K4*/
    
    re = sf_floatalloc(sn1);
    inpsg = sf_floatalloc(n1*n2);
    xt = sf_floatalloc(n1*n2);
    xt1 = sf_floatalloc(n1*n2);
    out1 = sf_complexalloc(n1*n2);
    
    
    if (NULL != restor) {
	sf_floatread(re,sn1,restor);
    }else {
	for (i=0;i<sn1; i++)
	    re[i] = 0;
    }

    
    if(cosine) {
	for(k = 0; k < n3 ; k++) {
	    sf_floatread(inpsg,n1*n2,in);
	    x00=x0,y00=y0;
	    t0=0;
	    if(verb) sf_warning("step %d of %d;",k,n3);
	    for(j = 0;j < n2;j++) {
		x00=x0,y00=y0;
		t0=0;
		for(i = 0;i < n1;i++){
		    
		    k11 = function1(t0,x00,y00,omega);
		    k12 = function2(t0,x00,y00,inpsg[i+j*n1],kxi,
				    gamma,omega,pow1,pow2,phi);
		    k21 = function1(t0+h/2,x00+k11*h/2,y00+k12*h/2,omega);
		    k22 = function2(t0+h/2,x00+k11*h/2,
				    y00+k12*h/2,inpsg[i+j*n1],kxi,
				    gamma,omega,pow1,pow2,phi);
		    k31 = function1(t0+h/2,x00+k21*h/2,y00+k22*h/2,omega);
		    k32 = function2(t0+h/2,x00+k21*h/2,
				    y00+k22*h/2,inpsg[i+j*n1],kxi,
				    gamma,omega,pow1,pow2,phi);
		    k41 = function1(t0+h,x00+k31*h,y00+k32*h,omega);
		    k42 = function2(t0+h,x00+k31*h,y00+k32*h,inpsg[i+j*n1],
				    kxi,gamma,omega,pow1,pow2,phi);
		    
		    x00+= h*(k11+2*k21+2*k31+k41)/6;
		    y00+= h*(k12+2*k22+2*k32+k42)/6;
		    t0+= h;
		    
		    xt[i+j*n1]=x00,xt1[i+j*n1]=y00;
		}	
	    }
	    for(i=0;i < n1*n2;i++) {
		out1[i] = sf_cmplx(xt[i],xt1[i]);
	    }
	    sf_complexwrite(out1,n1*n2,out);
	}
    }else {
	for(k = 0; k < n3 ; k++) {
	    sf_floatread(inpsg,n1*n2,in);
	    x00=x0,y00=y0;
	    t0=0;
	    if(verb) sf_warning("step %d of %d;",k,n3);
	    for(j = 0;j < n2;j++) {
		x00=x0,y00=y0;
		t0=0;
		for(i = 0;i < n1;i++) {
		    	    
		    k11 = function1(t0,x00,y00,omega);
		    k12 = function3(t0,x00,y00,inpsg[i+j*n1],kxi,
				    gamma,omega,pow1,pow2,re[i]);
		    k21 = function1(t0+h/2,x00+k11*h/2,y00+k12*h/2,omega);
		    k22 = function3(t0+h/2,x00+k11*h/2,
				    y00+k12*h/2,inpsg[i+j*n1],kxi,
				    gamma,omega,pow1,pow2,re[i]);
		    k31 = function1(t0+h/2,x00+k21*h/2,y00+k22*h/2,omega);
		    k32 = function3(t0+h/2,x00+k21*h/2,
				    y00+k22*h/2,inpsg[i+j*n1],kxi,
				    gamma,omega,pow1,pow2,re[i]);
		    k41 = function1(t0+h,x00+k31*h,y00+k32*h,omega);
		    k42 = function3(t0+h,x00+k31*h,y00+k32*h,inpsg[i+j*n1],
				    kxi,gamma,omega,pow1,pow2,re[i]);
		    
		    x00+= h*(k11+2*k21+2*k31+k41)/6;
		    y00+= h*(k12+2*k22+2*k32+k42)/6;
		    t0+= h;
		    
		    
		    xt[i+j*n1]=x00,xt1[i+j*n1]=y00;
		}
	    }
	    for(i=0;i < n1*n2;i++) {
		out1[i] = sf_cmplx(xt[i],xt1[i]);
	    }
	    sf_complexwrite(out1,n1*n2,out);
	}
    }
    
    exit(0);
}
Beispiel #4
0
int main(int argc, char* argv[])
{
    bool verb;
    int i1, i3, n12, n3, niter, n[2], rect[2];
    float *u1, *u2, *u3, norm;
    sf_file inp, out, den;

    sf_init(argc,argv);
    inp = sf_input("in");
    den = sf_input("den");
    out = sf_output("out");

    if (SF_FLOAT != sf_gettype(inp)) sf_error("Need float input");
    
    if (!sf_histint(inp,"n1",n)) sf_error("No n1= in input");
    if (!sf_histint(inp,"n2",n+1)) sf_error("No n2= in input");
    n3 = sf_leftsize(inp,2);
    n12 = n[0]*n[1];

    if (!sf_getint("rect1",&rect[0])) rect[0]=1;
    if (!sf_getint("rect2",&rect[1])) rect[1]=1;
    /* smoothing radius */

    if (!sf_getint("niter",&niter)) niter=100;
    /* number of iterations */

    if (!sf_getbool("verb",&verb)) verb=false;
    /* verbosity */

    u1 = sf_floatalloc(n12);
    u2 = sf_floatalloc(n12);
    u3 = sf_floatalloc(n12);

	runtime_init(n12*sizeof(float));
    for (i3=0; i3 < n3; i3++) 
	{
		sf_floatread(u1, n12, inp);
		sf_floatread(u2, n12, den);

    	sf_divn_init(2, n12, n, rect, niter, verb);
		/* smooth division */
		norm = 0.;
		for (i1=0; i1 < n12; i1++) 
		    norm += u2[i1] * u2[i1];
		norm = sqrtf(n12/norm);

		for (i1=0; i1 < n12; i1++) 
		{
			u1[i1] *= norm;
			u2[i1] *= norm;
		}
		sf_divn (u1, u2, u3);

		sf_floatwrite(u3, n12, out);

		sf_divn_close();
		norm = runtime(1);
		sf_warning("%d of %d, %f MB/sec;", i3, n3, norm);
	}

	free(u1);
	free(u2);
	free(u3);
    exit(0);
}
Beispiel #5
0
int main(int argc, char* argv[])
{
    int nt, nt2, nx, i1, i2, ch, n12, n122, fk;
    bool adj, sm, domod;
    float dt, dt2, dx, ot, ot2, ox, epst2;
    float v_1, v_2, v_3, v_4, eps, passthr;
    float * data, * output, * datat2, * outputt2, * smooth, * model;
    sf_file inp, out, pifk;
    /* smoothing variables */
    int nrep, dim, dim1, n[SF_MAX_DIM], rect[SF_MAX_DIM], s[SF_MAX_DIM], i0, i, j, nvar;
    bool diff[SF_MAX_DIM], box[SF_MAX_DIM];
    int irep;
    char key[6];
    sf_triangle tr;
    /* kirchhoff params */
    bool hd;
    int sw;
    float *vrms, v0;
    char *test;
    sf_file vel;

    //MADAGASCAR C API
    /* initialize */
    sf_init(argc,argv);
    inp = sf_input("in");
    out = sf_output("out");

    /* get dimensions from input */
    if (!sf_histint(inp,"n1",&nt)) sf_error("No n1= in inp");
    if (!sf_histint(inp,"n2",&nx)) sf_error("No n2= in inp");
    if (!sf_histfloat(inp,"d1",&dt)) sf_error("No d1= in inp");
    if (!sf_histfloat(inp,"d2",&dx)) sf_error("No d2= in inp");
    if (!sf_histfloat(inp,"o1",&ot)) sf_error("No o1= in inp");
    if (!sf_histfloat(inp,"o2",&ox)) sf_error("No o2= in inp");
    
    /* kirchhoff parameters */
    if (!sf_getbool("hd",&hd)) hd=true;
    if (!sf_getbool("domod",&domod)) domod=true;
    /* if y, apply half-derivative filter */
    if (!sf_getint("sw",&sw)) sw=0;
    /* if > 0, select a branch of the antialiasing operation */
    
    /* smoothing part - determines various params including dimension along
    which smoothing is performed */
    dim = sf_filedims (inp,n);
    dim1 = -1;
    for (i=0; i < dim; i++) {
	snprintf(key,6,"rect%d",i+1);
	if (!sf_getint(key,rect+i)) rect[i]=1;
	/*( rect#=(1,1,...) smoothing radius on #-th axis )*/ 
	if (rect[i] > 1) dim1 = i;
	snprintf(key,6,"diff%d",i+1);
	if (!sf_getbool(key,diff+i)) diff[i]=false;
	/*( diff#=(n,n,...) differentiation on #-th axis )*/
	snprintf(key,5,"box%d",i+1);
	if (!sf_getbool(key,box+i)) box[i]=false;
	/*( box#=(n,n,...) box (rather than triangle) on #-th axis )*/
    }
    
    /* creating parameters for smoothing filter */
    s[0] = 1;
    s[1] = nt;
    //s[2] = ny; for 3D case
    nvar = nt*nx; // 2D 
    // nvar = nt*nx*ny; // 3D
    
    /* to output f-k pi filter */
    if (NULL != sf_getstring("pifk")) {
        pifk = sf_output("pifk");
    } else {
        pifk = NULL;
    }    

    /* get parameters from command line */
    if (!sf_getbool("adj",&adj)) adj=false;

    /* if perform derivative filtering */

    if (!sf_getbool("sm",&sm)) sm=true;

    /* if y, do adjoint integration */
    
    if (!sf_getfloat("v_1",&v_1)) sf_error("No integration range specified"); 
    if (!sf_getfloat("v_2",&v_2)) sf_error("No integration range specified"); 
    if (!sf_getfloat("v_3",&v_3)) sf_error("No integration range specified"); 
    if (!sf_getfloat("v_4",&v_4)) sf_error("No integration range specified");  
    if (!sf_getfloat("passthr",&passthr)) passthr = 0.001; // threshold for tail elimination
    
    if (!sf_getfloat("eps",&eps)) eps = 0.001; // damper for pi
    if (!sf_getfloat("epst2",&epst2)) epst2 = 0.001; // damper for t2warp
    
    /* new axis length */
	if (!sf_getint("pad",&nt2)) nt2=nt; /* output time samples */
	
	if (!sf_getint("repeat",&nrep)) nrep=1;
    /* repeat filtering several times */
	
	n12 = nt2*nx;
    
    data = sf_floatalloc(nt*nx);
    model = sf_floatalloc(nt*nx);
    datat2 = sf_floatalloc(nt2*nx); 
    outputt2 = sf_floatalloc(nt2*nx);
    output = sf_floatalloc(nt*nx);

    /* allocate space for smoothing */
    if(sm){
    	smooth = sf_floatalloc(nt*nx);
    }
    
    /* allocating and reading velocity */
    vrms = sf_floatalloc(nt);
    if (NULL != (test = sf_getstring("velocity"))) { 
	/* velocity file */
	free(test);
	vel = sf_input("velocity");
	sf_floatread(vrms,nt,vel);
	sf_fileclose(vel);
    } else {
	if (!sf_getfloat("v0",&v0)) sf_error("Need velocity= or v0=");
	/* constant velocity (if no velocity=) */
	for (i1=0; i1 < nt; i1++) {
	    vrms[i1] = v0;
	}
    }
    
    if(domod){// if perform modelling
	/* initialize kirchhoff */
    	kirchnew_init (vrms, ot, dt, dx, nt, nx, sw, true, hd);
    	n122 = nt*nx;
    }

    if(!adj) {
    
    /* read data currently 2D */
    	if(domod){
		for (i2=0; i2 < nx; i2++) {
	    		sf_floatread(model+i2*nt,nt,inp);
		}
	kirchnew_lop (false,false,n122,n122,model,data);
    	} else
    	{
    		sf_warning("modelling is disabled");
    		for (i2=0; i2 < nx; i2++) {
	    		sf_floatread(data+i2*nt,nt,inp);
		}
	} // internal else
    } else {// adj flag
	
    /* read data currently 2D */
    for (i2=0; i2 < nx; i2++) {
	    sf_floatread(data+i2*nt,nt,inp);
	}
	
	}

	if (adj){	
	if (sm) {
	
		for (j = 0; j < nx; j++) {
			for (i = 0; i < nt; i++) {
		
				smooth[i + j*nt] = data [i + j*nt];
			
			}
		}
	
		/* browse through dimensions and smooth*/
	
		for (i=0; i <= dim1; i++) {
			if (rect[i] <= 1) continue;
			tr = sf_triangle_init (rect[i],n[i],box[i]);
			for (j=0; j < nvar/n[i]; j++) {
				i0 = sf_first_index (i,j,dim1+1,n,s);
				for (irep=0; irep < nrep; irep++) {
					sf_smooth (tr,i0,s[i],diff[i],smooth);	
			    	}
			}		
		}
	
		sf_warning("closing triangle");
		sf_triangle_close(tr);
	
		for (j = 0; j < nx; j++) {
			for (i = 0; i < nt; i++) {
			data[i + j*nt] -= smooth[i + j*nt];	
			}
		}

	}// sm flag
	}
	
	/* t2warping axis evaluation */
	ot2 = ot*ot;
	dt2 = ot+(nt-1)*dt;
	dt2 = (dt2*dt2 - ot2)/(nt2-1);	
		
	/* take in account different output trace length */
	t2warp_init(nt,nt2,nx,ot,dt,ot2,dt2,epst2);
	
	sf_warning("t2warp_init(nt,nt2,nx,ot,dt,ot2,dt2,epst2);\n");
	
	/* compute pi filter */
	flatpifilt_init(nt2, nx, dt2, dx, 0.001, v_1, v_2, v_3, v_4, eps);
	
	sf_warning("pifilt_init(nt2, nx, dt2, dx, v_a, v_b, v_0, beta, eps);\n");
	
	if(adj) {

	sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,adj,false,nt*nx,nt2*nx,nt2*nx,nt*nx,output,data,outputt2,datat2);

	} else {
	
	sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,false,false,nt*nx,nt2*nx,nt2*nx,nt*nx,data,output,datat2,outputt2);

	//smoothing	
	if (sm) {
	
		for (j = 0; j < nx; j++) {
			for (i = 0; i < nt; i++) {
		
				smooth[i + j*nt] = output [i + j*nt];
			
			}
		}
	
		/* browse through dimensions and smooth*/

		for (i=0; i <= dim1; i++) {
	    		if (rect[i] <= 1) continue;
	    		tr = sf_triangle_init (rect[i],n[i],box[i]);
	    		for (j=0; j < nvar/n[i]; j++) {
				i0 = sf_first_index (i,j,dim1+1,n,s);
				for (irep=0; irep < nrep; irep++) {
					sf_smooth2 (tr,i0,s[i],diff[i],smooth);		
	    			}		
			}			
		}
	    
		sf_triangle_close(tr);
	
		for (j = 0; j < nx; j++) {
			for (i = 0; i < nt; i++) {
		
				 output [i + j*nt] -= smooth[i + j*nt];
			 
			}
		}

	}// smoothing flag

	}
	
	if(adj) {

	kirchnew_lop (true,false,n122,n122,model,output);
	sf_warning("chain is done;\n");
	} // adj flag
	
	sf_warning("done with output");
	
	if (!adj) {
	
		/* write */
		for (i2=0; i2 < nx; i2++) {
	    	sf_floatwrite(output+i2*nt,nt,out);
	
		}
	} else {
	
		/* write */
		for (i2=0; i2 < nx; i2++) {
	    	sf_floatwrite(model+i2*nt,nt,out);
	    }
	
	}

    exit(0);
}
Beispiel #6
0
static void
useek_pipe_rw_double (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read)
{	static double buffer [PIPE_TEST_LEN] ;
	static double data [PIPE_TEST_LEN] ;
	SNDFILE *outfile ;
	SNDFILE *infile_piped ;

	int k, status = 0 ;
	int pipefd [2] ;
	pid_t pida ;

	for (k = 0 ; k < PIPE_TEST_LEN ; k++)
		data [k] = PIPE_INDEX (k) ;

	/*
	** Create the pipe.
	*/
	pipe (pipefd) ;

	/*
	** Attach the write end of the pipe to be written to.
	*/
	if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL)
	{	printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ;
		printf ("\t%s\n\n", sf_strerror (outfile)) ;
		exit (1) ;
		} ;

	if (sf_error (outfile) != SF_ERR_NO_ERROR)
	{	printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ;
		exit (1) ;
		} ;

	/*
	** Attach the read end of the pipe to be read from.
	*/
	if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL)
	{	printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ;
		exit (1) ;
		} ;

	if (sf_error (infile_piped) != SF_ERR_NO_ERROR)
	{	printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ;
		exit (1) ;
		} ;

	/* Fork a child process that will write directly into the pipe. */
	if ((pida = fork ()) == 0) /* child process */
	{	test_writef_double_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
		exit (0) ;
		} ;

	/* In the parent process, read from the pipe and compare what is read
	** to what is written, if they match everything went as planned.
	*/
	test_readf_double_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ;
	if (memcmp (buffer, data, sizeof (buffer)) != 0)
	{	printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ;
		exit (1) ;
		} ;

	/* Wait for the child process to return. */
	waitpid (pida, &status, 0) ;
	status = WEXITSTATUS (status) ;
	sf_close (outfile) ;
	sf_close (infile_piped) ;

	if (status != 0)
	{	printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ;
		exit (1) ;
		} ;

	return ;
} /* useek_pipe_rw_double */
Beispiel #7
0
int main (int argc, char* argv[]) 
{
    int ix, curxapp, leftShift;
   
/* Initialize RSF */
    sf_init (argc,argv);
/* Input files */
    inDags_   = sf_input("in");
    inDagsSq_ = sf_input("dataSq");

/* check that the input is float */
    if ( SF_FLOAT != sf_gettype (inDags_) )   sf_error ("Need float input: dip-angle gathers");
    /* dip-angle gathers - stacks in the scattering-angle direction */
    if ( SF_FLOAT != sf_gettype (inDagsSq_) ) sf_error ("Need float input: dip-angle gathers in squares");
    /* stacks of amplitude squares in the scattering-angle direction */

/* Output file */
    sembFile_ = sf_output("out");

/* Depth/time axis */
    if ( !sf_histint   (inDags_, "n1", &zNum_) )   sf_error ("Need n1= in input");
    if ( !sf_histfloat (inDags_, "d1", &zStep_) )  sf_error ("Need d1= in input");
    if ( !sf_histfloat (inDags_, "o1", &zStart_) ) sf_error ("Need o1= in input");
/* Dip angle axis */
    if ( !sf_histint   (inDags_, "n2", &dipNum_) )   sf_error ("Need n2= in input");
    if ( !sf_histfloat (inDags_, "d2", &dipStep_) )  sf_error ("Need d2= in input");
    if ( !sf_histfloat (inDags_, "o2", &dipStart_) ) sf_error ("Need o2= in input");
/* x axis */
    if ( !sf_histint   (inDags_, "n3", &xNum_) )     sf_error ("Need n3= in input");
    if ( !sf_histfloat (inDags_, "d3", &xStep_) )    sf_error ("Need d3= in input");
    if ( !sf_histfloat (inDags_, "o3", &xStart_) )   sf_error ("Need o3= in input");
	
    if ( !sf_getint ("xapp",    &xapp_) )    xapp_ = 1;
    /* number of CIGs in the inline-direction processed simultaneously */
    if (!xapp_) {sf_warning ("xapp value is changed to 1"); xapp_ = 1;}

    if ( !sf_getint ("dipapp",    &xdipapp_) ) xdipapp_ = 11;
    /* number of traces in the x-dip direction processed simultaneously */
    if (!xdipapp_) {sf_warning ("dipapp value is changed to 11"); xdipapp_ = 11;}

    if ( !sf_getint ("coher",   &coher_) )   coher_ = 11;
    /* height of a vertical window for semblance calculation */
    if (!coher_) {sf_warning ("coher value is changed to 1"); coher_ = 1;}

    if ( !sf_getint ("scatnum", &scatnum_) ) scatnum_ = 1;
    /* shows how many traces were stacked in the scattering angle direction; 
       if the stack was normalized use the default value 
    */ 

	makeWeight_ = true;
    if ( !sf_getfloat ("s1", &s1_) ) {s1_ = -1.f; makeWeight_ = false; }
    /* minimum semblance value */ 
    if ( !sf_getfloat ("s2", &s2_) ) {s2_ = -1.f; makeWeight_ = false; }
    /* maximum semblance value */ 
	ds_ = s2_ - s1_;

    dagSize_ = zNum_ * dipNum_;
    halfCoher_ = coher_ / 2;    /* yes - this is the integer division */	
    halfXapp_  = xapp_ / 2;     /* this is the integer division too	*/

    for (ix = 0; ix < xNum_; ++ix) {
		
		sf_warning ("CIG %d of %d;", ix + 1, xNum_);
		
		/* xapp for the currect core CIG; it can be changed by the checkBoundary () */
		curxapp = xapp_; 
		/* distance between the core gather and the left side of the aperture */
		leftShift = 0;	

		ptrToSembPanel_ = sf_floatalloc (dagSize_);
		memset (ptrToSembPanel_, 0, dagSize_ * sizeof (float));

		readBlockAroundPoint (ix, halfXapp_, &curxapp, &leftShift);
		buildFilter (curxapp, leftShift, ptrToSembPanel_);

		free (ptrToDags_);
		free (ptrToDagsSq_);

		free (ptrToData_);
		free (ptrToDataSq_);

		sf_floatwrite (ptrToSembPanel_, dagSize_, sembFile_);
		free (ptrToSembPanel_);
    }

    sf_warning (".");

    exit(0);
}
Beispiel #8
0
sf_file sf_output (/*@[email protected]*/ const char* tag)
/*< Create an output file structure.
  ---
  Should do output after the first call to sf_input. >*/
{
    sf_file file;
    char *headname, *dataname, *path, *name, *format;
    size_t namelen;
    extern int mkstemp (char *tmpl);
    extern off_t ftello (FILE *stream);
	
    file = (sf_file) sf_alloc(1,sizeof(*file));
	
    if (NULL == tag || 0 == strcmp(tag,"out")) {
	file->stream = stdout;
	headname = NULL;
    } else {
	headname = sf_getstring (tag);
	if (NULL == headname) {
	    namelen = strlen(tag)+1;
	    headname = sf_charalloc (namelen);
	    memcpy(headname,tag,namelen);
	}
		
	file->stream = fopen(headname,"w+");
	if (NULL == file->stream) 
        {
            free(file);
	    sf_error ("%s: Cannot write to header file %s:",__FILE__,headname);
        }
    }
	
    file->buf = NULL;
    /*    setbuf(file->stream,file->buf); */
	
    file->pars = sf_simtab_init (tabsize);
    file->head = NULL;
    file->headname = NULL;
	
    file->pipe = (bool) (-1 == ftello(file->stream));
    if (file->pipe && ESPIPE != errno) 
    {
        free(file);
	sf_error ("%s: pipe problem:",__FILE__);
    }
 
    dataname = sf_getstring("out");
    if (NULL == dataname)
	dataname = sf_getstring("--out");
	
    if (file->pipe) {
	file->dataname = sf_charalloc (7);
	memcpy(file->dataname,"stdout",7);
    } else if (NULL == dataname) {
	path = getdatapath();
	file->dataname = sf_charalloc (PATH_MAX+NAME_MAX+1);
	strcpy (file->dataname,path);
	name = file->dataname+strlen(path);
	free (path);
	if (getfilename (file->stream,name)) {
	  if(0==strcmp(name,"/dev/null")){
	      file->dataname = sf_charalloc (7);
	      memcpy(file->dataname,"stdout",7);
	    } else {
	      namelen = strlen(file->dataname);
	      file->dataname[namelen]='@';
	      file->dataname[namelen+1]='\0';
	    }
	} else { /* Invent a name */
	  /* stdout is not a pipe, not /dev/null, not a file in this 
	     directory. 
	     One way to get here is to redirect io to a file not in this 
	     directory.  For example >../myfile.rsf.  In this case getfilename
	     cannot find the file from file->stream by looking in the current 
	     directory.  The function mkstemp is used to create a unique name 
	     to contain the binary data. */
	    sprintf(name,"%sXXXXXX",sf_getprog());
	    (void) close(mkstemp(file->dataname));
	    /* (void) unlink(file->dataname); */
	    /* old code for named pipes below */
	    /*
	      if (NULL == headname &&
	      -1L == fseek(file->stream,0L,SEEK_CUR) &&
	      ESPIPE == errno && 
	      0 != mkfifo (file->dataname, S_IRUSR | S_IWUSR))
	      sf_error ("%s: Cannot make a pipe %s:",
	      __FILE__,file->dataname);
	    */
	}  
    } else {
	namelen = strlen(dataname)+1;
	file->dataname = sf_charalloc (namelen);
	memcpy(file->dataname,dataname,namelen);
	free (dataname);
    }
	
    sf_putstring(file,"in",file->dataname);    
	
    file->op = XDR_ENCODE;
	
    if (NULL == infiles) {
	infiles = (sf_file *) sf_alloc(1,sizeof(sf_file));
	infiles[0] = NULL;
	nfile=1;
    } 
	
    if (NULL != infiles[0]) { 
	if (NULL == infiles[0]->pars) sf_error("The input file was closed prematurely.");
	if (NULL != (format = sf_histstring(infiles[0],"data_format"))) {
	    sf_setformat(file,format);
	    free (format);
	}
    } else {
	sf_setformat(file,"native_float");
    }
	
    if (NULL != headname) free(headname);
	
    if (!sf_getbool("--readwrite",&(file->rw))) file->rw=false;
    if (!sf_getbool("--dryrun",&(file->dryrun))) file->dryrun=false;
	
    return file;
}
Beispiel #9
0
void sf_fileflush (sf_file file, sf_file src)
/*< outputs parameter to a file (initially from source src)
  ---
  Prepares file for writing binary data >*/ 
{
    time_t tm;
    char line[BUFSIZ];
    /* if already flushed, do nothing */
    if (NULL == file->dataname) return;
	
    if (NULL != src && NULL != src->head) {
	rewind(src->head);
		
	while (NULL != fgets(line,BUFSIZ,src->head)) {
	    fputs(line,file->stream);
	}
    }
    
    tm = time (NULL);
    if (0 >= fprintf(file->stream,"%s\t%s\t%s:\t%[email protected]%s\t%s\n",
		     RSF_VERSION,
		     sf_getprog(),
		     sf_getcdir(),
		     sf_getuser(),
		     sf_gethost(),
		     ctime(&tm)))
	sf_error ("%s: cannot flush header:",__FILE__);
	
    switch (file->type) {
	case SF_FLOAT: 
	    switch (file->form) {
		case SF_ASCII:		    
		    sf_putstring(file,"data_format","ascii_float");
		    break;
		case SF_XDR:
		    sf_putstring(file,"data_format","xdr_float");
		    break;
		default:
		    sf_putstring(file,"data_format","native_float");
		    break;
	    }
	    break;
	case SF_COMPLEX:
	    switch (file->form) {
		case SF_ASCII:		    
		    sf_putstring(file,"data_format","ascii_complex");
		    break;
		case SF_XDR:
		    sf_putstring(file,"data_format","xdr_complex");
		    break;
		default:
		    sf_putstring(file,"data_format","native_complex");
		    break;
	    }
	    break;
	case SF_INT:
	    switch (file->form) {
		case SF_ASCII:		    
		    sf_putstring(file,"data_format","ascii_int");
		    break;
		case SF_XDR:
		    sf_putstring(file,"data_format","xdr_int");
		    break;
		default:
		    sf_putstring(file,"data_format","native_int");
		    break;
	    }
	    break;
        case SF_SHORT:
	    switch (file->form) {
		case SF_ASCII:
		    sf_putstring(file,"data_format","ascii_short");
		    break;
		case SF_XDR:
		    sf_putstring(file,"data_format","xdr_short");
		    break;
		default:
		    sf_putstring(file,"data_format","native_short");
		    break;
	    }
	    break;
	case SF_LONG:
	    switch (file->form) {
		case SF_ASCII:
		    sf_putstring(file,"data_format","ascii_long");
		    break;
		case SF_XDR:
		    sf_putstring(file,"data_format","xdr_long");
		    break;
		default:
		    sf_putstring(file,"data_format","native_long");
		    break;
	    }
	    break;
        case SF_DOUBLE:
	    switch (file->form) {
		case SF_ASCII:
		    sf_putstring(file,"data_format","ascii_double");
		    break;
		case SF_XDR:
		    sf_putstring(file,"data_format","xdr_double");
		    break;
		default:
		    sf_putstring(file,"data_format","native_double");
		    break;
	    }
	    break;
	case SF_UCHAR:
	    switch (file->form) {
		case SF_ASCII:
		    sf_putstring(file,"data_format","ascii_uchar");
		    break;
		case SF_XDR:
		    sf_putstring(file,"data_format","xdr_uchar");
		    break;
		default:
		    sf_putstring(file,"data_format","native_uchar");
		    break;
	    }
	    break;
	case SF_CHAR:
	    switch (file->form) {
		case SF_ASCII:		    
		    sf_putstring(file,"data_format","ascii_char");
		    break;
		case SF_XDR:
		    sf_putstring(file,"data_format","xdr_char");
		    break;
		default:
		    sf_putstring(file,"data_format","native_char");
		    break;
	    }
	    break;
	default:
	    sf_putstring(file,"data_format",
			 (NULL==file->buf)? "native_byte":"xdr_byte");
	    break;
    }    
    
    sf_simtab_output(file->pars,file->stream);
    (void) fflush(file->stream);
	
    if (0==strcmp(file->dataname,"stdout")) { 
	/* keep stream, write the header end code */
	fprintf(file->stream,"\tin=\"stdin\"\n\n%c%c%c",
		SF_EOL,SF_EOL,SF_EOT);
    } else {
	file->stream = freopen(file->dataname,file->rw? "w+b":"w+b",file->stream);       
	if (NULL == file->stream) 
	    sf_error ("%s: Cannot write to data file %s:",
		      __FILE__,file->dataname);	
    }
    
    free (file->dataname);
    file->dataname=NULL;
	
    if (file->dryrun) exit(0);
}
Beispiel #10
0
static void sf_input_error(sf_file file, const char* message, const char* name)
{
    if (error) 
	sf_error ("%s: %s %s:",__FILE__,message,name);
    sf_fileclose(file);
}
Beispiel #11
0
sf_file sf_input (/*@[email protected]*/ const char* tag)
/*< Create an input file structure >*/
{
    int esize;
    sf_file file;
    char *filename, *format;
    size_t len;
    extern off_t ftello (FILE *stream);
	
    file = (sf_file) sf_alloc(1,sizeof(*file));
    file->dataname = NULL;
    
    if (NULL == tag || 0 == strcmp(tag,"in")) {
	file->stream = stdin;
	filename = NULL;
    } else {
	filename = sf_getstring (tag);
	if (NULL == filename) {
	    /* this option allows you to call function with 
	       sf_input("mydir/myinput.rsf");  Karl  */
	    len = strlen(tag)+1;
	    filename = sf_charalloc(len);
	    /* should change next line to strcpy or strncpy Karl */
	    memcpy(filename,tag,len);
	}
		
	file->stream = fopen(filename,"r+");
	if (NULL == file->stream) {
	    sf_input_error(file,"Cannot read input (header) file",filename);
	    return NULL;
	}
    }
	
    file->buf = NULL;
    /*    setbuf(file->stream,file->buf); */
	
    /* create a parameter table */
    file->pars = sf_simtab_init (tabsize);
    file->head = sf_tempfile(&file->headname,"w+");
	
    /* read the parameter table from the file */
    sf_simtab_input (file->pars,file->stream,file->head);
	
    if (NULL == infiles) {
	infiles = (sf_file *) sf_alloc(1,sizeof(sf_file));
	infiles[0] = NULL;
	nfile=1;
    }
	
    if (NULL == filename) {
	infiles[0] = file;
    } else {
	free (filename);
	ifile++;
	if (ifile >= nfile) {
	    /* grow array */
	    nfile *= 2;
	    infiles = (sf_file *) realloc(infiles, nfile * sizeof(sf_file));
	    if (NULL == infiles) sf_error("%s: reallocation error",__FILE__);
	}
	infiles[ifile] = file;
    }
	
    filename = sf_histstring(file,"in");
    if (NULL == filename) {
    	sf_input_error (file,"No in= in file",tag);
	return NULL;
    }
    len = strlen(filename)+1;
    file->dataname = sf_charalloc(len);
    memcpy(file->dataname,filename,len);
	
    if (0 != strcmp(filename,"stdin")) {
	file->stream = freopen(filename,"r+b",file->stream);
	if (NULL == file->stream) {
	    sf_input_error(file,"Cannot read data file",filename);
	    return NULL;
	}
    }
    free (filename);
	
    file->pipe = (bool) (-1 == ftello(file->stream));
    if (file->pipe && ESPIPE != errno) 
	sf_error ("%s: pipe problem:",__FILE__);
	
    file->op = XDR_DECODE;
	
    format = sf_histstring(file,"data_format");
    if (NULL == format) {
	if (!sf_histint(file,"esize",&esize) || 0 != esize) {
	    sf_input_error (file,"Unknown format in",tag);
	    return NULL;
	}
	sf_setformat(file,"ascii_float");
    } else {    
	sf_setformat(file,format);
	free (format);
    }
	
    return file;
}
Beispiel #12
0
void sf_putline (sf_file file, const char* line)
/*< put a string line to a file >*/
{
    if (0 >= fprintf(file->stream,"\t%s\n",line))
	sf_error ("%s: cannot put line '%s':",__FILE__,line);
}
Beispiel #13
0
int main(int argc, char* argv[])
{
    int nt, ns, nx, n2, it, nstr, ifix;
    float dt, t0, eps, v0, *v=NULL, *trace=NULL, *out=NULL, *ww=NULL, wsum;
    char buffer[20];
    map4 stolt;
    fint1 istolt;
    bool inv;
    sf_file in=NULL, st=NULL, vel=NULL;

    sf_init (argc,argv);
    in = sf_input("in");
    st = sf_output("out");
    vel = sf_input("velocity");

    if (!sf_getbool ("inv", &inv)) inv=false;
    /* if y, inverse stretch */

    if (!sf_getint("nstretch",&nstr)) nstr=1;
    /* number of steps */

    if (inv) {
        if (!sf_histint(in,"n1",&ns)) sf_error("No n1= in input");
        if (!sf_histint(vel,"n1",&nt)) sf_error("No n1= in velocity");
        sf_putint(st,"n1",nt);
    } else {
        if (!sf_histint(in,"n1",&nt)) sf_error("No n1= in input");
        if (!sf_getint("pad",&ns)) ns=nt;
        /* time axis padding */
        sf_putint(st,"n1",ns);
        sf_putint(st,"nstretch", nstr);
    }
    nx = sf_leftsize(in,1);
    n2 = sf_leftsize(vel,1);

    if (n2 != 1 && n2 != nx) sf_error("Wrong number of traces in velocity");

    if (!sf_histfloat(in,"d1",&dt)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"o1",&t0) || 0. != t0) sf_error("Need o1=0 in input");
    if (!sf_getfloat("eps",&eps)) eps=0.01;
    /* stretch regularization */
    if (!sf_getfloat("vel",&v0)) sf_error("Need vel=");
    /* reference velocity */

    trace = sf_floatalloc(nt);
    v = sf_floatalloc(nt);
    str = sf_floatalloc2(nt,nx);
    out = sf_floatalloc(ns);
    ww = sf_floatalloc(nx);

    if (inv) {
        stolt = NULL;
        istolt = fint1_init(4,ns,0);
    } else {
        stolt =  stretch4_init (ns,t0,dt,nt,eps);
        istolt = NULL;
    }

    wsum = 0.;
    for (ix=0; ix < nx; ix++) {
        if (0==ix || n2 > 1) {
            sf_floatread(v,nt,vel);
            ww[ix] = vt2w (nt,v,str[ix]);
        } else if (1 == n2) {
            ww[ix] = ww[0];
            for (it=0; it < nt; it++) {
                str[ix][it] = str[0][it];
            }
        }
        wsum += ww[ix];
    }

    sf_warning("%d traces processed",nx);

    if (1==nstr) {
        sf_putfloat(st,"stretch", wsum/nx);
    } else {
        for (ix=0; ix < nstr; ix++) {
            ifix=SF_MAX(1,(int) (nx*ix/(nstr-1)));
            sf_warning("%d %d",ix,ifix);

            snprintf(buffer,20,"stretch%d",ix);
            sf_putfloat (st,buffer,ww[ifix]);

            snprintf(buffer,20,"node%d",ix);
            sf_putint (st,buffer,ifix);
        }
    }

    for (ix=0; ix < nx; ix++) {
        for (it=0; it < nt; it++) {
            str[ix][it] *= dt/v0;
        }
        if (inv) {
            sf_floatread(out,ns,in);
            fint1_set(istolt,out);
            stretch(istolt, map, ns, dt, t0, nt, dt, t0, trace, 0.);
            sf_floatwrite (trace,nt,st);
        } else {
            sf_floatread (trace,nt,in);
            stretch4_define (stolt, str[ix]);
            stretch4_apply (false,stolt, trace, out);
            sf_floatwrite (out,ns,st);
        }
    }

    exit(0);
}
Beispiel #14
0
int main(int argc, char* argv[])
{
  sf_file infile=NULL, out=NULL, inheaders=NULL;
  int n1_traces;
  int n1_headers;

  int n_traces, n_headers; 
  sf_datatype typehead;
  sf_datatype typein;
  float* header=NULL;
  float* intrace=NULL;
  int i_trace;
  char* infile_filename=NULL;
  char* headers_filename=NULL;
  bool makeheader;
  int iaxis;
  char parameter[13];
  char* label_in[SF_MAX_DIM+1];
  int n_in[SF_MAX_DIM+1];
  float o_in[SF_MAX_DIM+1];
  float d_in[SF_MAX_DIM+1];
  int indx_of_keys[SF_MAX_DIM+1];
  int traceindx[SF_MAX_DIM+1];
  float tracecoord[SF_MAX_DIM+1];
  int indxleft;
  
  sf_init (argc,argv);

  /*****************************/
  /* initialize verbose switch */
  /*****************************/
  if(!sf_getint("verbose",&verbose))verbose=1;
  /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  if(verbose>0)fprintf(stderr,"verbose=%d\n",verbose);
 
  /*****************************************/
  /* initialize the input and output files */
  /*****************************************/
  if(verbose>0)fprintf(stderr,"read name of input file name\n");
  
  infile_filename=sf_getstring("input");
  /* \n
     Input file for traces amplitudes.  You can list a file here, has the 
     input file name will be used to compute the name of the header files.

     The input trace amplitudes can also be read from standard input by
     just supplying standard input and omitting this paramater,  This 
     is useful it you wish to do sopme initial processing of the input
     rsf file containing the trace amplitudes.  This is useful if you need 
     to change input axis labels to use the makeheader=yes.
  */
  if(infile_filename==NULL) infile = sf_input ("in");
  else infile = sf_input (infile_filename);

  /* get the axis labels on the input file */
  /* these will be used if users requests makeheaders */
  for (iaxis=2; iaxis<SF_MAX_DIM+1; iaxis++){
    sprintf(parameter,"label%d",iaxis);
    if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
    /* get label, o, n, and i for each axis.  These will be used to 
       make headers from axis (if that option is requested) */
    if (!(label_in[iaxis]=sf_histstring(infile,parameter))) {
      label_in[iaxis]="none";
    }
    sprintf(parameter,"o%d",iaxis);
    if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
    if (!sf_histfloat(infile,parameter,&o_in[iaxis])){
      label_in[iaxis]="none";
    }
    sprintf(parameter,"d%d",iaxis);
    if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
    if (!sf_histfloat(infile,parameter,&d_in[iaxis])){
      label_in[iaxis]="none";
    }
    sprintf(parameter,"n%d",iaxis);
    if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
    if (!sf_histint(infile,parameter,&n_in[iaxis])){
      n_in[iaxis]=1;
    }
    if(verbose>1){
      fprintf(stderr,"on axis=%d got label,o,d,n=%s,%f,%f,%d \n",iaxis,
	      label_in[iaxis],o_in[iaxis],d_in[iaxis],n_in[iaxis]);
    }
  }
  /* compute index_of_keys after calling segy_init */ 






  if(verbose>0)
    fprintf(stderr,"set up output file for tah - should be stdout\n");
  out = sf_output ("out");

  if(verbose>0)fprintf(stderr,"read name of input headers file\n");
  headers_filename=sf_getstring("headers");
  /* \n
     Trace header file name.  Default is the input data file
     name, with the final .rsf changed to _hdr.rsf 
  */

  if(headers_filename==NULL){
    /* compute headers_filename from infile_filename by replacing the final
       .rsf with _hdr.rsf */
    if(!(0==strcmp(infile_filename+strlen(infile_filename)-4,".rsf"))){
	fprintf(stderr,"parameter input, the name of the input file, does\n");
	fprintf(stderr,"not end with .rsf, so header filename cannot be\n");
	fprintf(stderr,"computed by replacing the final .rsf with _hdr.rsf.\n");
	sf_error("default for headers parameter cannot be computed.");
    }
    headers_filename=malloc(strlen(infile_filename)+60);
    strcpy(headers_filename,infile_filename);
    strcpy(headers_filename+strlen(infile_filename)-4,"_hdr.rsf\0");
    if(verbose>2){
      fprintf(stderr,"parameter header defaulted.  Computed to be #%s#\n",
	      headers_filename);
    }
  }
  if(verbose>2)fprintf(stderr,"parameter header input or computed  #%s#\n",
		       headers_filename);

  if(!sf_getbool("makeheader",&makeheader))makeheader=false;
  /* \n
     Option to load headers using the input file axis labels.  If axis 
     label2 through label9 match a header key then that coordinate is
     loaded to the traces header.  This can be used to load the source
     coordinate to the sx header location.  This may require changing
     the axis label because Madagascar axis labels are not the same as
     segy trace headers.  for example axis 2 coordiante can be loaded in
     sx trace header by:
        <spike.rsf sfput label2=sx \\
           | sftahread headers=spike_hdr.rsf makeheader=y \\ 
           | sftahgethw key=sx >/dev/null
 */
  
  inheaders = sf_input(headers_filename);

  if (!sf_histint(infile,"n1",&n1_traces))
    sf_error("input file does not define n1");
  if (!sf_histint(inheaders,"n1",&n1_headers)) 
    sf_error("input headers file does not define n1");






  n_traces=sf_leftsize(infile,1);
  n_headers=sf_leftsize(inheaders,1);
  if(verbose>0)
    fprintf(stderr,"n_traces=%d, n_headers=%d\n",n_traces,n_headers);

  if(n_traces!=n_headers){
    fprintf(stderr,"n_traces=%d, n_headers=%d\n",n_traces,n_headers);
    sf_error("number of traces and headers must be the same");
  }

  typehead = sf_gettype(inheaders);
  if (SF_INT != typehead && SF_FLOAT != typehead ) 
    sf_error("Need float or int input headers.");

  typein = sf_gettype(infile);
  if (SF_FLOAT != typein ) 
    sf_error("input file must contain floats.");
  
  /* must be float or int */
  if(verbose>1)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  header = sf_floatalloc(n1_headers);
 
  if(verbose>1)fprintf(stderr,"allocate intrace.  n1_traces=%d\n",n1_traces);

  intrace= sf_floatalloc(n1_traces);

  if(verbose>1)fprintf(stderr,"need to add 2 words.  Record type and record length\n");
  sf_putint(out,"n1",n1_traces+n1_headers+2);
  sf_putint(out,"n1_traces",n1_traces);

  /*************************************************************************/
  /* put info into history so later tah programs can figure out the headers*/
  /*************************************************************************/
  if(verbose>1)fprintf(stderr,"put n1_headers to history\n");
  sf_putint(out,"n1_headers",n1_headers);
  if(verbose>1)fprintf(stderr,"test typehead\n");
  if(SF_INT == typehead) sf_putstring(out,"header_format","native_int");
    else                 sf_putstring(out,"header_format","native_float");
  sf_putstring(out,"headers",headers_filename);
  /* the list of any extra header keys */
  if(verbose>1)fprintf(stderr,"segy_init\n");
  segy_init(n1_headers,inheaders);
  if(verbose>1)fprintf(stderr,"segy2hist\n");
  segy2hist(out,n1_headers);
  if(0)  {
    int tempint;
    tempint=segykey("ep");
    fprintf(stderr,"ep tempint=%d\n",tempint);
    tempint=segykey("epx");
    fprintf(stderr,"epx tempint=%d\n",tempint);
  }
  if(makeheader){
    for (iaxis=2; iaxis<SF_MAX_DIM+1; iaxis++){
      if(0==strcmp("none",label_in[iaxis])){
	indx_of_keys[iaxis]=-1;
      } else {
	indx_of_keys[iaxis]=segykey(label_in[iaxis]);
	if(indx_of_keys[iaxis]<0){
	  sf_warning("************************************************");
	  sf_warning("************************************************");
	  sf_warning("axis %d has label that is not a header key",iaxis);
	  sf_warning("This axis label will not be loaded");
	  sf_warning("************************************************");
	  sf_warning("************************************************");
	}
      }
      if(verbose>1){
	fprintf(stderr,"indx_of_keys[%d]=%d\n",iaxis,indx_of_keys[iaxis]);
      }
    }
  }
  /* put the history from the input file to the output */
  if(verbose>1)fprintf(stderr,"fileflush out\n");
  sf_fileflush(out,infile);

  if(verbose>0)fprintf(stderr,"start trace loop n_traces=%d\n",n_traces);
  for (i_trace=0; i_trace<n_traces; i_trace++){
    if(verbose>2 ||(verbose>0 && i_trace<5)){
      fprintf(stderr,"i_trace=%d\n",i_trace);
    }
    /**************************/
    /* read trace and headers */
    /**************************/
    sf_floatread(header,n1_headers,inheaders);
    sf_floatread(intrace,n1_traces,infile);

    if(makeheader){
      indxleft=i_trace;
      for (iaxis=2; iaxis<SF_MAX_DIM+1; iaxis++){
	traceindx[iaxis]=indxleft%n_in[iaxis];
	indxleft/=n_in[iaxis];
	tracecoord[iaxis]=o_in[iaxis]+traceindx[iaxis]*d_in[iaxis];
	if(verbose>1){
	  fprintf(stderr,"i_trace=%d,iaxis=%d,tracecoord[iaxis]=%f\n",
		  i_trace,   iaxis,   tracecoord[iaxis]);
	}
	if(indx_of_keys[iaxis]>=0){
	  /* kls what's needed to add to make this work with integer headers?*/
	  header[indx_of_keys[iaxis]]=tracecoord[iaxis];
	}
      }
    }
    /***************************/
    /* write trace and headers */
    /***************************/
    /* trace and header will be preceeded with words:
       1- type record: 4 charactors 'tah '.  This will support other
          type records like 'htah', hidden trace and header.
       2- the length of the length of the trace and header. */
    put_tah(intrace, header, n1_traces, n1_headers, out);
  }

  exit(0);
}
Beispiel #15
0
int main(int argc, char* argv[]) 
{
    bool sub;
    int nx, nx2, nk, nt, m, ix, ik, it, im, n2;
    sf_complex *curr, **wave, c;
    float *rcurr, dt;
    sf_complex **lft, **rht, *cwave, *cwavem, **mat;
    sf_file Fw, Fo, right, left, prop;

    sf_init(argc,argv);
    Fw = sf_input("in");
    Fo = sf_output("out");

    if (SF_COMPLEX != sf_gettype(Fw)) sf_error("Need complex input");

    sf_settype(Fo,SF_FLOAT);

    /* Read/Write axes */

    if (!sf_getbool("sub",&sub)) sub=false;
    /* if -1 is included in the matrix */

    if (!sf_histint(Fw,"n1",&nx)) sf_error("No n1= in input");

    if (!sf_getint("nt",&nt)) sf_error("No nt= in input");
    if (!sf_getfloat("dt",&dt)) sf_error("No dt= in input");

    sf_putint(Fo,"n2",nt);
    sf_putfloat(Fo,"d2",dt);
    sf_putfloat(Fo,"o2",0.);
    sf_putstring(Fo,"label2","Time");
    sf_putstring(Fo,"unit2","s");

    nk = cfft1_init(nx,&nx2);
    sf_warning("nk=%d\n", nk);

    curr = sf_complexalloc(nx2);
    rcurr= sf_floatalloc(nx2);
    cwave = sf_complexalloc(nk);
 
    if (NULL != sf_getstring("right")) {
	left = sf_input("left");   /* Left matrix */
	right = sf_input("right"); /* Right matrix */
	
	if (SF_COMPLEX != sf_gettype(left) ||
	    SF_COMPLEX != sf_gettype(right)) sf_error("Need complex left and right");
	
	if (!sf_histint(left,"n1",&n2) || n2 != nx) sf_error("Need n1=%d in left",nx);
	if (!sf_histint(left,"n2",&m)) sf_error("Need n2= in left");
	
	if (!sf_histint(right,"n1",&n2) || n2 != m) sf_error("Need n1=%d in right",m);
	if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right, now n2=%d",nk,n2);
	
	lft = sf_complexalloc2(nx,m);
	rht = sf_complexalloc2(m,nk);
	
	sf_complexread(lft[0],nx*m,left);
	sf_complexread(rht[0],nk*m,right);
	
	sf_fileclose(left);
	sf_fileclose(right);
	
	cwavem = sf_complexalloc(nk);
	wave = sf_complexalloc2(nx2,m);

	mat = NULL;
    } else { /* Use propagator matrix (for testing) */
	 prop = sf_input("prop");
	 if (SF_COMPLEX != sf_gettype(prop)) sf_error("Need complex prop");

	lft = NULL;
	rht = NULL;

	mat = sf_complexalloc2(nk,nx);
	sf_complexread(mat[0],nx*nk,prop);
	sf_fileclose(prop);

	cwavem = NULL;
	wave = NULL;
    }
	
    sf_complexread (curr,nx,Fw);

    for (ix = nx; ix < nx2; ix++) {
	curr[ix] = sf_cmplx(0.,0.);
    }

    for (ix = 0; ix < nx2; ix++) {
	rcurr[ix]=0.;
    }

    /* propagation in time */
    for (it=0; it < nt; it++) {
	sf_warning("it=%d;",it);

        /* FFT: curr -> cwave */
	cfft1(curr,cwave);

	if (NULL == mat) {
	    for (im = 0; im < m; im++) {
		for (ik = 0; ik < nk; ik++) {
#ifdef SF_HAS_COMPLEX_H
		    cwavem[ik] = cwave[ik]*rht[ik][im];
#else
		    cwavem[ik] = sf_cmul(cwave[ik],rht[ik][im]);
#endif
		    
		}
		/* Inverse FFT: cwavem -> wave[im] */
		icfft1(wave[im],cwavem);
	    }
	}

	for (ix = 0; ix < nx; ix++) {

	    c = sub? curr[ix]: sf_cmplx(0.,0.);

	    if (NULL == mat) {
		for (im = 0; im < m; im++) {
#ifdef SF_HAS_COMPLEX_H
		    c += lft[im][ix]*wave[im][ix];
#else
		    c = sf_cadd(c,sf_cmul(lft[im][ix], wave[im][ix]));
#endif
		}
	    } else {
		for (ik = 0; ik < nk; ik++) {
#ifdef SF_HAS_COMPLEX_H
		    c += mat[ix][ik] * cwave[ik];
#else
		    c = sf_cadd(c,sf_cmul(mat[ix][ik],cwave[ik]));
#endif
		}
	    }

	    curr[ix] = c;
	    rcurr[ix] = crealf(c);
	} 

	sf_floatwrite(rcurr,nx,Fo);
    }
    
    exit(0);
}
Beispiel #16
0
int main (int argc, char *argv[])
{
    bool left;
    int ir, nr, n1,n2,n3, m1, m2, m3, n12, nw, nj1, nj2, i3;
    float *u1, *u2, *p;
    sf_file in, out, dip;
    allpass ap;

    sf_init(argc,argv);
    in = sf_input ("in");
    dip = sf_input ("dip");
    out = sf_output ("out");

    if (SF_FLOAT != sf_gettype(in) ||
	SF_FLOAT != sf_gettype(dip)) sf_error("Need float type");

    if (!sf_histint(in,"n1",&n1)) sf_error("Need n1= in input");
    if (!sf_histint(in,"n2",&n2)) n2=1;
    if (!sf_histint(in,"n3",&n3)) n3=1;
    n12 = n1*n2;
    nr = sf_leftsize(in,3);

    if (!sf_getbool("left",&left)) left=true;
    /* if using left or right side of PWD */

    if (!sf_histint(dip,"n1",&m1) || m1 != n1) 
	sf_error("Need n1=%d in dip",n1);
    if (1 != n2 && (!sf_histint(dip,"n2",&m2) || m2 != n2)) 
	sf_error("Need n2=%d in dip",n2);
    if (1 != n3 && (!sf_histint(dip,"n3",&m3) || m3 != n3)) 
	sf_error("Need n3=%d in dip",n3);

    if (!sf_getint("order",&nw)) nw=1;
    /* accuracy */
    if (!sf_getint("nj1",&nj1)) nj1=1;
    /* in-line aliasing */
    if (!sf_getint("nj2",&nj2)) nj2=1;
    /* cross-line aliasing */

    for (ir=0; ir < nr; ir++) {
	u1 = sf_floatalloc(n12);
	u2 = sf_floatalloc(n12);
	p  = sf_floatalloc(n12);
	    
	for (i3=0; i3 < n3; i3++) {
	    /* read data */
	    sf_floatread(u1,n12,in);
	    
	    /* read t-x dip */
	    sf_floatread(p,n12,dip);
		
	    ap = allpass_init (nw,nj1,n1,n2,1,p);
		
	    /* apply */
	    if (left) {
		left1(false, false, ap, u1, u2);
	    } else {
		right1(false, false, ap, u1, u2);
	    }
		
	    /* write t-x destruction */
	    sf_floatwrite(u2,n12,out);
	}
	
	free(u1);
	free(u2);
	free(p);
	    
    }
    
    exit (0);
}
Beispiel #17
0
int main(int argc, char* argv[])
{
    bool inv;
    int nt, nx, nv, it, ix, iv, nw, n3, i3, ntr, ntm, im;
    float *trace=NULL, *modl=NULL, *r=NULL;
    float vmin, vmax, dv, dx, x0, t0, t, dt, tp, xp;
    char *unit=NULL, *space=NULL, *time=NULL;
    size_t len;
    sf_bands spl=NULL;
    sf_file in, out;

    sf_init (argc,argv);
    in = sf_input("in");
    out = sf_output("out");

    if (!sf_getbool("inv",&inv)) inv=false;
    /* if y, do inverse transform */

    if (!sf_getint("nw",&nw)) nw=2;
    /* accuracy level */

    if (!sf_histint(in,"n2",&nt)) sf_error("No n2= in input");
    if (!sf_histfloat(in,"d2",&dt)) sf_error("No d2= in input");
    if (!sf_histfloat(in,"o2",&t0)) sf_error("No o2= in input");
 
    if (!sf_getfloat("tp",&tp)) tp=t0;
    if (!sf_getfloat("xp",&xp)) xp=0.;

    if (inv) {
	if (!sf_histint(in,"n1",&nv)) sf_error("No n1= in input");
	if (!sf_histfloat(in,"d1",&dv)) sf_error("No d1= in input"); 
	if (!sf_histfloat(in,"o1",&vmin)) sf_error("No o1= in input"); 

	if (!sf_getint("nx",&nx) && !sf_histint(in,"nx",&nx)) 
	    sf_error("Need nx=");
	/* number of offsets (if inv=y) */

	if (!sf_getfloat("x0",&x0) && !sf_histfloat(in,"x0",&x0)) 
	    sf_error("Need x0=");
	/* offset origin (if inv=y) */

	if (!sf_getfloat("dx",&dx) && !sf_histfloat(in,"dx",&dx)) 
	    sf_error("Need dx=");
	/* offset sampling (if inv=y) */

	sf_putint(out,"n1",nx);
	sf_putfloat(out,"d1",dx);
	sf_putfloat(out,"o1",x0);
	sf_putstring(out,"label1","Offset");

	if (NULL != (unit=sf_histstring(in,"unit1"))) {
	    space=strchr(unit,'/');
	    if (*space == '\0') sf_putstring(out,"unit1",unit);
	}

	if (nw > 2) spl = sf_spline_init (nw, nv);

	ntr = nv;
	ntm = nx;
    } else {
	if (!sf_histint(in,"n1",&nx)) sf_error("No n1= in input");
	if (!sf_histfloat(in,"d1",&dx)) sf_error("No d1= in input"); 
	if (!sf_histfloat(in,"o1",&x0)) sf_error("No o1= in input"); 

	if (!sf_getint("nv",&nv)) sf_error("Need nv=");
	/* number of velocities (if inv=n) */

	if (!sf_getfloat("vmin",&vmin)) sf_error("Need vmin=");
	/* minimum velocity (if inv=n) */

	if (!sf_getfloat("vmax",&vmax)) sf_error("Need vmax=");
	/* maximum velocity (if inv=n) */

	dv = (vmax - vmin)/(nv-1.);

	sf_putint(out,"n1",nv);
	sf_putfloat(out,"d1",dv);
	sf_putfloat(out,"o1",vmin);

	sf_putstring(out,"label1","Velocity");

	if (NULL != (time = sf_histstring(in,"unit2")) &&
	    NULL != (space = sf_histstring(in,"unit1"))) {
	    len = strlen(time)+strlen(space)+2;
	    unit = sf_charalloc(len);
	    snprintf(unit,len,"%s/%s",space,time);
	    sf_putstring(out,"unit1",unit);
	    free(time);
	    free(space);
	}

	sf_putint(out,"nx",nx);
	sf_putfloat(out,"x0",x0);
	sf_putfloat(out,"dx",dx);

	if (nw > 2) spl = sf_spline_init (nw, nx);

	ntr = nx;
	ntm = nv;
    }
    n3 = sf_leftsize(in,2);

    trace = sf_floatalloc(ntr);
    modl =  sf_floatalloc(ntm);
    r = sf_floatalloc(ntm);

    for (i3=0; i3 < n3; i3++) {
	for (it=0; it < nt; it++) {
	    t = t0 + it*dt;

	    sf_floatread (trace,ntr,in);
	    if (nw > 2) sf_banded_solve (spl,trace);

	    if (t > tp) {
		if (inv) {
		    for (ix=0; ix < nx; ix++) {
			r[ix] = (x0-xp+ix*dx)/(t-tp);
		    }

		    sf_int1_init (r, vmin, dv, nv, sf_spline_int, nw, nx, 0.0);
		} else {
		    for (iv=0; iv < nv; iv++) {
			r[iv] = xp+(vmin+iv*dv)*(t-tp);
		    }

		    sf_int1_init (r, x0,   dx, nx, sf_spline_int, nw, nv, 0.0);
		}

		sf_int1_lop (false,false,ntr,ntm,trace,modl);
	    } else {
		for (im=0; im < ntm; im++) {
		    modl[im] = 0.;
		}
	    }

	    sf_floatwrite (modl,ntm,out);
	}
    }

    exit(0);
}
Beispiel #18
0
int main(int argc, char* argv[])
{
    int n1, n2, n3, gainstep, panel, it, nreserve, i1, i2, i3, j, orient;
    float o1, o2, o3, d1, d2, d3, gpow, clip, pclip, phalf, bias=0., minmax[2];
    float pbias, gain=0., x1, y1, x2, y2, **data=NULL, f, barmin, barmax, dat;
    bool transp, yreverse, xreverse, allpos, polarity, symcp, verb;
    bool eclip=false, egpow=false, barreverse, mean=false;
    bool scalebar, nomin=true, nomax=true, framenum, sfbyte, sfbar, charin;
    char *gainpanel, *color, *barfile;
    unsigned char tbl[TSIZE+1], **buf, tmp, *barbuf[1];
    enum {GAIN_EACH=-3,GAIN_ALL=-2,NO_GAIN=-1};
    off_t pos;
    sf_file in, out=NULL, bar=NULL;
    
    sf_init(argc,argv);
    in = sf_input("in");

    sfbyte = (bool) (NULL != strstr (sf_getprog(),"byte"));
    sfbar = (bool) (NULL != strstr (sf_getprog(),"bar"));

    if (sfbyte) {
	out = sf_output("out");
	sf_settype(out,SF_UCHAR);
    } else if (sfbar) {
	bar = sf_output("out");
	sf_settype(bar,SF_UCHAR);
    } else {
	vp_init();
    }

    charin = (bool) (SF_UCHAR == sf_gettype(in));

    if (charin && sfbyte) sf_error("Cannot input uchar to byte");

    if (!charin && SF_FLOAT != sf_gettype(in)) sf_error("Need float input");

    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&n2)) n2=1;
    n3 = sf_leftsize(in,2);

    if (!sf_histfloat(in,"o1",&o1)) o1=0.;
    if (!sf_histfloat(in,"o2",&o2)) o2=0.;
    if (!sf_histfloat(in,"o3",&o3)) o3=0.;

    if (!sf_histfloat(in,"d1",&d1)) d1=1.;
    if (!sf_histfloat(in,"d2",&d2)) d2=1.;
    if (!sf_histfloat(in,"d3",&d3)) d3=1.;

    if (!sf_getbool("transp",&transp)) transp=true;
    /* if y, transpose the display axes */
    if (!sf_getbool("yreverse",&yreverse)) yreverse=true;
    /* if y, reverse the vertical axis */
    if (!sf_getbool("xreverse",&xreverse)) xreverse=false;
    /* if y, reverse the horizontal axis */

    if (transp) {
	orient = 3;
    } else {	
	orient = (xreverse==yreverse)? 0:2;
    }

    if (!charin) {
	panel = NO_GAIN; /* no need for gain */
	
	phalf=85.;
	egpow = false;
	if (!sf_getfloat("gpow",&gpow)) {
	    gpow=1.;
	    /*( gpow=1 raise data to gpow power for display )*/
	} else if (gpow <= 0.) {
	    gpow=0.;
	    egpow = true;
	    sf_getfloat("phalf",&phalf);
	    /* percentage for estimating gpow */
	    if (phalf <=0. || phalf > 100.)
		sf_error("phalf=%g should be > 0 and <= 100",phalf);
	    panel = 0;
	}
	
	pclip=99.;
	eclip = (bool) (!sf_getfloat("clip",&clip));
	/* data clip */
	if (eclip) {	    
	    clip = 0.;
	    sf_getfloat("pclip",&pclip);
	    /* data clip percentile (default is 99) */
	    if (pclip <=0. || pclip > 100.)
		sf_error("pclip=%g should be > 0 and <= 100",pclip);
	    panel = 0;
	} else if (clip <= 0.) {
	    sf_warning("clip=%g <= 0",clip);
	    clip = FLT_EPSILON;
	}

	if (0==panel) {
	    if (!sf_getint("gainstep",&gainstep)) gainstep=0.5+n1/256.;
	    /* subsampling for gpow and clip estimation */
	    if (gainstep <= 0) gainstep=1;

	    gainpanel = sf_getstring("gainpanel");
	    /* gain reference: 'a' for all, 'e' for each, or number */
	    if (NULL != gainpanel) {
		switch (gainpanel[0]) {
		    case 'a': 
			panel=GAIN_ALL; 
			break;
		    case 'e': 
			panel=GAIN_EACH;
			break;
		    default:
			if (0 ==sscanf(gainpanel,"%d",&panel) || 
			    panel < 1 || panel > n3) 
			    sf_error("gainpanel= should be all,"
				     " each, or a number"
				     " between 1 and %d",n3);
			panel--;
			break;
		}
		free (gainpanel); 
	    } 

	    sf_unpipe(in,sf_filesize(in)*sizeof(float));
	} 

	if (!sf_getbool("allpos",&allpos)) allpos=false;
	/* if y, assume positive data */
	if (!sf_getbool("mean",&mean)) mean=false;
	/* if y, bias on the mean value */
	if (!sf_getfloat("bias",&pbias)) pbias=0.;
	/* value mapped to the center of the color table */
	if (!sf_getbool("polarity",&polarity)) polarity=false;
	/* if y, reverse polarity (white is high by default) */
	if (!sf_getbool("symcp",&symcp)) symcp=false;
	/* if y, assume symmetric color palette of 255 colors */
	if (!sf_getbool("verb",&verb)) verb=false;
	/* verbosity flag */
    } /* if !charin */

    barfile = sf_getstring("bar");
    /* file for scalebar data */

    if (sfbyte) {
	scalebar = (bool) (NULL != barfile);
	if (scalebar) sf_putstring(out,"bar",barfile);
    } else if (sfbar) {
	scalebar = true;
    } else {
	if (!sf_getbool ("wantscalebar",&scalebar) && 
	    !sf_getbool ("scalebar",&scalebar)) scalebar = false;
	/* if y, draw scalebar */	
    }
    if (scalebar) {
	nomin = (bool) (!sf_getfloat("minval",&barmin));
	/* minimum value for scalebar (default is the data minimum) */
	nomax = (bool) (!sf_getfloat("maxval",&barmax));
	/* maximum value for scalebar (default is the data maximum) */
	
	barbuf[0] = sf_ucharalloc(VP_BSIZE);

	if (!sf_getbool("barreverse",&barreverse)) barreverse=false;
	/* if y, go from small to large on the bar scale */

	if (sfbyte || sfbar) {
	    if (sfbyte) {
		bar = sf_output("bar");
		sf_settype(bar,SF_UCHAR);
	    }
	    sf_putint(bar,"n1",VP_BSIZE+2*sizeof(float));
	    sf_putint(bar,"n2",1);
	    sf_putint(bar,"n3",n3);

	    if (!nomin) sf_putfloat(bar,"minval",barmin);
	    if (!nomax) sf_putfloat(bar,"maxval",barmax);
	} else if (charin) {
	    if (NULL == barfile) {
		barfile=sf_histstring(in,"bar");
		if (NULL == barfile) sf_error("Need bar=");
	    }

	    bar = sf_input(barfile);
	    if (SF_UCHAR != sf_gettype(bar)) sf_error("Need uchar in bar");

	    if (nomin) nomin = (bool) (!sf_histfloat(bar,"minval",&barmin));
	    if (nomax) nomax = (bool) (!sf_histfloat(bar,"maxval",&barmax));
	}
    }

    if (!sf_getbool("wantframenum",&framenum)) framenum = (bool) (n3 > 1);
    /* if y, display third axis position in the corner */

    x1 = o1-0.5*d1;
    x2 = o1+(n1-1)*d1+0.5*d1;
    y1 = o2-0.5*d2;
    y2 = o2+(n2-1)*d2+0.5*d2;

    if (!sfbyte && !sfbar) {
	vp_stdplot_init (x1, x2, y1, y2, transp, false, yreverse, false);
	vp_frame_init(in,"tlb",false);
/*	if (scalebar && !nomin && !nomax) 
	vp_barframe_init (in,barmin,barmax); */
    }

    if (transp) {
	f=x1; x1=y1; y1=f;
	f=x2; x2=y2; y2=f;
    }

    if (yreverse) {
	f=y1; y1=y2; y2=f;
    }

    if (xreverse) {
	f=x1; x1=x2; x2=f;
    }

    buf = sf_ucharalloc2(n1,n2);

    if (!charin) {
	data = sf_floatalloc2(n1,n2);

	if (GAIN_ALL==panel || panel >= 0) {
	    pos = sf_tell(in);
	    if (panel > 0) sf_seek(in,
				   pos+panel*n1*n2*sizeof(float),
				   SEEK_SET);
	    vp_gainpar (in,data,n1,n2,gainstep,
			pclip,phalf,&clip,&gpow,mean,&pbias,
			n3,panel,panel);
	    if (verb) sf_warning("panel=%d bias=%g clip=%g gpow=%g",
				 panel,pbias,clip,gpow);
	    if (sfbyte) sf_putfloat(out,"clip",clip);
	    sf_seek(in,pos,SEEK_SET); /* rewind */
	}
    }

    if (!sfbyte && !sfbar) {
	/* initialize color table */
	if (NULL == (color = sf_getstring("color"))) color="i";
	/* color scheme (default is i) */
	if (!sf_getint ("nreserve",&nreserve)) nreserve = 8;
	/* reserved colors */
	vp_rascoltab (nreserve, color);
    }

    for (i3=0; i3 < n3; i3++) {	
	if (!charin) {
	    if (GAIN_EACH == panel) {
		if (eclip) clip=0.;
		if (egpow) gpow=0.;
		vp_gainpar (in,data,n1,n2,gainstep,
			    pclip,phalf,&clip,&gpow,
			    mean,&pbias,n3,0,n3);
		if (verb) sf_warning("bias=%g clip=%g gpow=%g",pbias,clip,gpow);
	    } else {
		sf_floatread(data[0],n1*n2,in);
	    }
	    
	    if (1 == panel || GAIN_EACH == panel || 0==i3) { 
		/* initialize the conversion table */
		if(!allpos) { /* negative and positive values */
		    for (it=1; it<=TSIZE/2; it++) {
		        if (symcp) {
			    tbl[TSIZE-it] = (gpow != 1.)?
			        254*(pow(((TSIZE-2.0*it)/TSIZE),gpow)+1.)/2.+1.:
			        254*(    ((TSIZE-2.0*it)/TSIZE)      +1.)/2.+1.;
			    tbl[it] = 255 - tbl[TSIZE-it] + 1.0;
			} else {
			    tbl[TSIZE-it] = (gpow != 1.)?
			        252*(pow(((TSIZE-2.0*it)/TSIZE),gpow)+1.)/2.+3.:
			        252*(    ((TSIZE-2.0*it)/TSIZE)      +1.)/2.+3.;
			    tbl[it] = 255 - tbl[TSIZE-it] + 2.0;
			}
		    }
		    bias = TSIZE/2.;
		    gain = TSIZE/(2.*clip);
		} else { /* all positive */
		    if (symcp) {
			for (it=1; it < TSIZE ; it++) {
			    tbl[it] = 255*((it-1.0)/TSIZE) + 1.0;
			}
		    } else {
			for (it=1; it < TSIZE ; it++) {
			    tbl[it] = 256*((it-1.0)/TSIZE);
			}
		    }
		    bias = 0.;
		    gain = TSIZE/clip;		
		}
		tbl[0] = tbl[1];
		tbl[TSIZE] = tbl[TSIZE-1];
		if (polarity) { /* switch polarity */
		    for (it=0; it<=TSIZE; it++) {
			tbl[it]=255-tbl[it];
		    }
		}
	    }
	    
	    /* convert to bytes */
	    for (i2=0; i2 < n2; i2++) {
		for (i1=0; i1 < n1; i1++) {
		    j = (data[i2][i1]-pbias)*gain + bias;
		    if      (j < 0) j=0;
		    else if (j > TSIZE) j=TSIZE;
		    buf[i2][i1] = tbl[j];
		}
	    }
	} else {
	    sf_ucharread(buf[0],n1*n2,in);
	}

	if (!sfbyte && !sfbar) {
	    if (yreverse) {
		for (i2=0; i2 < n2; i2++) {
		    for (i1=0; i1 < n1/2; i1++) {			
			tmp = buf[i2][i1];
			buf[i2][i1] = buf[i2][n1-1-i1];
			buf[i2][n1-1-i1] = tmp;
		    }
		}
	    } 
	    
	    if ((xreverse && transp) || (!xreverse && !transp)) {
		for (i2=0; i2 < n2/2; i2++) {
		    for (i1=0; i1 < n1; i1++) {
			tmp = buf[i2][i1];
			buf[i2][i1] = buf[n2-1-i2][i1];
			buf[n2-1-i2][i1] = tmp;
		    }
		}
	    }
	
	    if (i3 > 0) vp_erase (); 	

	    if (framenum) vp_framenum(o3+i3*d3);
	    vp_frame(); 
	    vp_uraster (buf, false, 256, n1, n2, 
			x1, y1, x2, y2, orient);
	    vp_simpleframe();
	}
	
	if (scalebar) {
	    if (!charin) {
		if (nomin) barmin = data[0][0];
		if (nomax) barmax = data[0][0];
		if (nomin || nomax) {
		    for (i2=0; i2 < n2; i2++) {
			for (i1=0; i1 < n1; i1++) {
			    dat = data[i2][i1];
			    if (nomin && barmin > dat) barmin = dat;
			    if (nomax && barmax < dat) barmax = dat;
			}
		    }
		}
		
		for (it=0; it < VP_BSIZE; it++) {
		    if (barreverse) {
			dat = (barmin*it + barmax*(VP_BSIZE-1-it))/(VP_BSIZE-1);
		    } else {
			dat = (barmax*it + barmin*(VP_BSIZE-1-it))/(VP_BSIZE-1);
		    }
		    j = (dat-pbias)*gain + bias;
		    if      (j < 0) j=0;
		    else if (j > TSIZE) j=TSIZE;
		    barbuf[0][it] = tbl[j];
		} 
	    } else {
		sf_floatread(minmax,2,bar);
		sf_ucharread(barbuf[0],VP_BSIZE,bar);

		if (nomin) barmin=minmax[0];
		if (nomax) barmax=minmax[1];
	    }

	    if (sfbyte || sfbar) {
		sf_floatwrite(&barmin,1,bar);
		sf_floatwrite(&barmax,1,bar);
		sf_ucharwrite(barbuf[0],VP_BSIZE,bar);
	    } else {
		if (barreverse) {
		    vp_barframe_init (in,barmax,barmin);
		} else {
		    vp_barframe_init (in,barmin,barmax);
		}
		vp_barraster(VP_BSIZE, barbuf);
	    }
	} /* if scalebar */

	if (sfbyte) {
	    sf_ucharwrite(buf[0],n1*n2,out);
	} else if (!sfbar) {
	    vp_purge();
	} 
    } /* i3 loop */


    exit (0);
}
Beispiel #19
0
int main(int argc, char* argv[])
{
    bool verb;
    char *what;
    int nz, nx, na, iz, ix, ia, order, jx, ja, scheme;
    float x, dx, x0, a, da, a0, dz, dz2, t, sx, sa;
    float amax, xmax, x2, a1, a2, s2, tn;
    float **next, **vxz, s, grad[2], grad2[2];
    sf_eno2 prev, slow;
    sf_file vel, out;

    sf_init(argc,argv);
    vel = sf_input("in");
    out = sf_output("out");

    if (!sf_histint(vel,"n2",&nx)) sf_error("No n2=");
    if (!sf_histfloat(vel,"d2",&dx)) sf_error("No d2=");
    if (!sf_histfloat(vel,"o2",&x0)) sf_error("No o2=");

    if (!sf_histint(vel,"n1",&nz)) sf_error("No n2=");
    if (!sf_histfloat(vel,"d1",&dz)) sf_error("No d2=");

    if (!sf_getint("na",&na)) sf_error("Need na=");
    /* angle samples */
    if (!sf_getfloat("da",&da)) sf_error("Need da=");
    /* angle sampling */
    if (!sf_getfloat("a0",&a0)) sf_error("Need a0=");
    /* starting velocity */

    sf_putint(out,"n1",na);
    sf_putint(out,"n2",nx);
    sf_putint(out,"n3",nz);
    sf_putfloat(out,"d1",da);
    sf_putfloat(out,"d2",dx);
    sf_putfloat(out,"d3",dz);
    sf_putfloat(out,"o1",a0);
    sf_putfloat(out,"o2",x0);
    sf_putfloat(out,"o3",0.);
    sf_putstring(out,"label1","Angle");
    sf_putstring(out,"unit1","\\^o\\_");	

    /* convert to radians */
    a0 *= SF_PI/180.;
    da *= SF_PI/180.;

    amax = a0 + (na-1)*da;
    xmax = x0 + (nx-1)*dx;

    if (!sf_getint("order",&order)) order=3;
    /* interpolation order */

    if (!sf_getint("scheme",&scheme)) scheme=2;
    /* finite-difference order */

    if (!sf_getbool("verb",&verb)) verb=false;
    /* verbosity */

    what = sf_getstring("what");
    /* what to compute (t,x,z,a) */

    if (NULL == what) {
	what = "time";
    } else {
	if (what[0] != 't' &&
	    what[0] != 'x' &&
	    what[0] != 'z' &&
	    what[0] != 'a') 
	    sf_error("Need what=t|x|z|a");
    }

    slow = sf_eno2_init (order,nz,nx);
    vxz = sf_floatalloc2(nz,nx);

    sf_floatread(vxz[0],nz*nx,vel);
    /* convert velocity to slowness */
    for (ix=0; ix < nx; ix++) {
	for (iz=0; iz < nz; iz++) {
	    vxz[ix][iz] = 1./vxz[ix][iz];
	}
    }
    sf_eno2_set(slow,vxz);
    free(*vxz);
    free(vxz);

    prev = sf_eno2_init(order,na,nx);
    next = sf_floatalloc2(na,nx);
    for (ix=0; ix < nx; ix++) {
	x = x0+ix*dx;
	for (ia=0; ia < na; ia++) {
	    switch (what[0]) {
		case 't':
		case 'z':
		    next[ix][ia] = 0.;
		    break;
		case 'x':
		    next[ix][ia] = x;
		    break;
		case 'a':
		    a = a0+ia*da;
		    next[ix][ia] = a*180./SF_PI;
	    }
	}
    }
    sf_eno2_set(prev,next);
    sf_floatwrite(next[0],na*nx,out);

    for (iz=1; iz < nz; iz++) {
	if (verb) sf_warning("depth %d of %d",iz,nz);
	for (ix=0; ix < nx; ix++) {	
	    x = x0+ix*dx;

	    /* slowness and gradient at starting point */
	    sf_eno2_apply (slow,iz,ix,0.,0.,&s,grad,BOTH);

	    for (ia=0; ia < na; ia++) {
		a = a0+ia*da;

		if (scheme > 1) {
		    /* half a step in a */
		    a1 = a + 0.5*(grad[1]/dx+
				  grad[0]*tanf(a)/dz)*dz/s;
	
		    /* check if angle is in range */
		    if (a1 < a0) {
			a1=a0;
		    } else if (a1 > amax) {
			a1=amax;
		    } 
		} else {
		    a1 = a;
		}

		tn = tanf(a1);

		/* full step in x */
		x2 = x + tn*dz;
		
		/* check if space is in range */
		if (x2 < x0) {
		    x2 = x0;
		    jx = 0;
		    sx = 0.;
		    dz2 = (x0-x)/tn;
		} else if (x2 > xmax) {
		    x2 = xmax;
		    jx = nx-1;
		    sx = 0.;
		    dz2 = (xmax-x)/tn;
		} else {
		    sx = (x2-x0)/dx; 
		    jx=floorf(sx);
		    sx -= jx;
		    dz2 = dz;
		}

		/* slowness and gradient at new location */
		sf_eno2_apply (slow,iz-1,jx,1.-dz2/dz,sx,
			       &s2,grad2,BOTH);
		
		if (scheme > 1) {
		    /* half a step in a */
		    a2 = a1 + 0.5*(grad2[1]/dx+
				   grad2[0]*tn/dz)*dz2/s2;
		} else {
		    /* full step in a */
		    a2 = a1 + (grad2[1]/dx+
			       grad2[0]*tn/dz)*dz2/s2;
		}

		/* check if angle is in range */
		if (a2 < a0) {
		    a2=a0;
		} else if (a2 > amax) {
		    a2=amax;
		} 

		if (dz2 != dz) { /* exit from the side */
		    switch (what[0]) {
			case 't':			    
			    t = deltat(scheme,dz,dz,dx,
				       s,s2,a,a2,grad,grad2);
			    break;
			case 'x':
			    t = x2;
			    break;
			case 'z':
			    t = iz*dz-dz2;
			    break;
			case 'a':
			    t = a2*180./SF_PI;
			    break;
		    }
		} else { /* exit to the previous level */
		    sa = (a2-a0)/da; 
		    ja=floorf(sa);
		    sa -= ja;

		    sf_eno2_apply (prev,ja,jx,sa,sx,
				   &t,NULL,FUNC);
		    if ('t'==what[0]) 
			t += deltat(scheme,dz,dz,dx,
				    s,s2,a,a2,grad,grad2);
		}
		
		next[ix][ia] = t;
	    }
	}
	sf_eno2_set(prev,next);
	sf_floatwrite(next[0],na*nx,out);
    }
    
    exit(0);
}
Beispiel #20
0
int main(int argc, char* argv[])
{
    bool adj, snap; /* adjoint flag */
    int ix, iz, it; /* index variables */
    int nt, nx, nz, n0, jt, n12, padx, padz, n2;
    float dt, dx, dz, dt2, idz2, idx2;
    
    float **dd, **mm, **vv;
    float **u0, **u1, **u2, **tmp; /* temporary arrays */

    sf_file in, out, vel, wave; /* I/O files */

    /* initialize Madagascar */
    sf_init(argc,argv);
    
    /* initialize OpenMP support */
#ifdef _OPENMP
    omp_init();
#endif

    if(!sf_getbool("adj", &adj)) adj=true;
    /* adjoint flag, 0: modeling, 1: migration */
    if(!sf_getbool("snap", &snap)) snap=false;
    /* wavefield snapshot flag */
    if(!sf_getint("n0", &n0)) n0=0;
    /* surface */
    if(!sf_getint("jt", &jt)) jt=50;
    /* time interval of wavefield snapshot */
	
    /* setup I/O files */
    in=sf_input("in");
    out=sf_output("out");
    vel=sf_input("velocity");
    /* velocity model */
    
    /* Dimensions */
    if(!sf_histint(vel, "n1", &nz)) sf_error("No n1= in velocity");
    if(!sf_histint(vel, "n2", &nx)) sf_error("No n2= in velocity");
    if(!sf_histfloat(vel, "d1", &dz)) sf_error("No d1= in velocity");
    if(!sf_histfloat(vel, "d2", &dx)) sf_error("No d2= in velocity");

    if(adj){ /* migration */
        if(!sf_histint(in, "n1", &nt)) sf_error("No n1= in data");
        if(!sf_histfloat(in, "d1", &dt)) sf_error("No d1= in data");
        if(!sf_histint(in, "n2", &n2) || n2!=nx) sf_error("Need n2=%d in data", nx);
        
        sf_putint(out, "n1", nz);
        sf_putfloat(out, "d1", dz);
        sf_putfloat(out, "o1", 0.0);
        sf_putstring(out, "label1", "Depth");
        sf_putstring(out, "unit1", "km");
        sf_putstring(out, "label2", "Lateral");
        sf_putstring(out, "unit2", "km");
    }else{ /* modeling */
        if(!sf_getint("nt", &nt)) sf_error("Need nt=");
        if(!sf_getfloat("dt", &dt)) sf_error("Need dt=");
        
        sf_putint(out, "n1", nt);
        sf_putfloat(out, "d1", dt);
        sf_putfloat(out, "o1", 0.0);
        sf_putstring(out, "label1", "Time");
        sf_putstring(out, "unit1", "s");
        sf_putstring(out, "label2", "Lateral");
        sf_putstring(out, "unit2", "km");
    }
    
    /* lengths of padding boundary */
    if(!sf_getint("padx", &padx)) padx=nz/2;
    if(!sf_getint("padz", &padz)) padz=nz/2;
    padnx=nx+2*padx;
    padnz=nz+2*padz;
    n0=n0+padz;
    n12=padnz*padnx;

    /* set Laplacian coefficients */
    idz2=1.0/(dz*dz);
    idx2=1.0/(dx*dx);
    
    c11=4.0*idz2/3.0;
    c12=-idz2/12.0;
    c21=4.0*idx2/3.0;
    c22=-idx2/12.0;
    c0=-2.0*(c11+c12+c21+c22);
    
    /* wavefield snapshot */
    if(snap){
        wave=sf_output("wave");
        
        sf_putint(wave, "n1", padnz);
        sf_putint(wave, "d1", 1);
        sf_putint(wave, "o1", -padz);
            
        sf_putint(wave, "n2", padnx);
        sf_putint(wave, "d2", 1);
        sf_putint(wave, "o2", -padx);
        
        sf_putint(wave, "n3", 1+(nt-1)/jt);
        if(adj){
            sf_putfloat(wave, "d3", -jt*dt);
            sf_putfloat(wave, "o3", (nt-1)*dt);
        }else{
            sf_putfloat(wave, "d3", jt*dt);
            sf_putfloat(wave, "o3", 0.0);
        }
    }
        
    /* allocate arrays */
    vv=sf_floatalloc2(nz, nx);
    dd=sf_floatalloc2(nt, nx);
    mm=sf_floatalloc2(nz, nx);
    padvv=sf_floatalloc2(padnz, padnx);
    u0=sf_floatalloc2(padnz, padnx);
    u1=sf_floatalloc2(padnz, padnx);
    u2=sf_floatalloc2(padnz, padnx);
    
    /* read velocity */
    sf_floatread(vv[0], nz*nx, vel);
    
    /* pad boundary */
    dt2=dt*dt;
    for(ix=0; ix<nx; ix++)
        for(iz=0; iz<nz; iz++)
            padvv[ix+padx][iz+padz]=vv[ix][iz]*vv[ix][iz]*dt2;
    for(iz=0; iz<padz; iz++){
        for(ix=padx; ix<nx+padx; ix++){
            padvv[ix][iz]=padvv[ix][padz];
            padvv[ix][iz+nz+padz]=padvv[ix][nz+padz-1];
        }
    }
    for(ix=0; ix<padx; ix++){
        for(iz=0; iz<padnz; iz++){
            padvv[ix][iz]=padvv[padx][iz];
            padvv[ix+nx+padx][iz]=padvv[nx+padx-1][iz];
        }
    }
    
    memset(u0[0], 0.0, n12*sizeof(float));
    memset(u1[0], 0.0, n12*sizeof(float));
    memset(u2[0], 0.0, n12*sizeof(float));

    if(adj){ /* migration */
        
        /* read data */
        sf_floatread(dd[0], nt*nx, in);
        
        for(it=nt-1; it>=0; it--){
            sf_warning("Migration: %d;", it);
            
            laplacian(adj, u0, u1, u2);
            tmp=u0; u0=u1; u1=u2; u2=tmp;

#ifdef _OPENMP
#pragma omp parallel for default(none) private(ix) shared(padx, nx, dd, u1, it, n0)
#endif
            for(ix=padx; ix<padx+nx; ix++)
            /* inject data */
            u1[ix][n0]+=dd[ix-padx][it];
            
            if(snap && it%jt==0) sf_floatwrite(u1[0], n12, wave);
        }
        sf_warning(".");
        
        /* output image */
        for(ix=0; ix<nx; ix++)
            for(iz=0; iz<nz; iz++)
                mm[ix][iz]=u1[ix+padx][iz+padz];
        sf_floatwrite(mm[0], nz*nx, out);
    
    }else{/* modeling */
    	
        /* read reflector */
    	sf_floatread(mm[0], nz*nx, in);
        for(ix=0; ix<nx; ix++)
            for(iz=0; iz<nz; iz++)
                u1[ix+padx][iz+padz]=mm[ix][iz];
    	    
    	for(it=0; it<nt; it++){
            sf_warning("Modeling: %d;", it);
            
            if(snap && it%jt==0) sf_floatwrite(u1[0], n12, wave);

#ifdef _OPENMP
#pragma omp parallel for default(none) private(ix) shared(padx, nx, dd, u1, it, n0)
#endif
            for(ix=padx; ix<padx+nx; ix++)
            /* record data */
	        dd[ix-padx][it]=u1[ix][n0];
            
            laplacian(adj, u0, u1, u2);
            tmp=u0; u0=u1; u1=u2; u2=tmp;
        }
    	sf_warning(".");
        
    	/* output data */
    	sf_floatwrite(dd[0], nt*nx, out);
    }
    
    free(*padvv); free(padvv); free(*vv); free(vv);
    free(*dd); free(dd); free(*mm); free(mm);
    free(*u0); free(u0); free(*u1); free(u1); free(*u2); free(u2);
    exit (0);
}
Beispiel #21
0
int main(int argc, char* argv[])
{
    int j, n1, n2, i2, ni, axis, esize;
    float f;
    off_t i3, n3;
    size_t n;
    sf_file in, out;
    char key1[7], key2[7], *val, *trace;

    sf_init (argc, argv);
    in = sf_input ("in");
    out = sf_output ("out");

    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"esize",&esize)) sf_error("No esize= in input");

    if (!sf_getint("axis",&axis)) axis=2;
    /* which axis to spray */

    n = (size_t) esize;
    for (j=0; j < axis-1; j++) {
	sprintf(key2,"n%d",j+1);
	if (!sf_histint(in,key2,&ni)) break;
	n *= ni;
    }

    if (!sf_getint("n",&n2)) sf_error("Need n=");
    /* Size of the newly created dimension */    
    sprintf(key1,"n%d",axis);
    sf_putint(out,key1,n2);

    if (sf_getfloat("d",&f)) {
	/* Sampling of the newly created dimension */ 
	sprintf(key1,"d%d",axis);
	sf_putfloat(out,key1,f);
    }
    
    if (sf_getfloat("o",&f)) {
	/* Origin of the newly created dimension */
	sprintf(key1,"o%d",axis);
	sf_putfloat(out,key1,f);
    }

    if (NULL != (val = sf_getstring("label"))) {
	/* Label of the newly created dimension */
	sprintf(key1,"label%d",axis);
	sf_putstring(out,key1,val);
    }

    if (NULL != (val = sf_getstring("unit"))) {
	/* Units of the newly created dimension */
	sprintf(key1,"unit%d",axis);
	sf_putstring(out,key1,val);
    }

    n3 = sf_shiftdim(in, out, axis);
    
    sf_fileflush(out,in);
    sf_setform(in,SF_NATIVE);
    sf_setform(out,SF_NATIVE);

    trace = sf_charalloc (n);
    
    for (i3=0; i3 < n3; i3++) {
	sf_charread (trace, n, in);
	for (i2=0; i2 < n2; i2++) {
	    sf_charwrite(trace, n, out);
	} 
    }


    exit (0);
}
Beispiel #22
0
int main (int argc, char* argv[]) {
    int nz, nx, na, nc;
    int iz, ix, ia;
    float dz, oz, dx, ox, da, oa, a;
    float savg = 0.0, smin = SF_HUGE, smax = -SF_HUGE;
    float corr_nterm = 0.0, corr = 0.0, new_savg = 0.0;
    float **s;
    bool verb;
    sf_file spdom, vspline = NULL, out;
    sf_esc_slowness2 esc_slow;

    sf_init (argc, argv);

    if (!sf_stdin ()) {
        spdom = NULL;
    } else {
        spdom = sf_input ("in");
        /* Spatial (z,x) domain */
    }

    out = sf_output ("out");
    /* Slowness values */

    /* Spatial dimensions */
    if (spdom) {
        if (!sf_histint (spdom, "n1", &nz)) sf_error ("No n1= in input");
        if (!sf_histint (spdom, "n2", &nx)) sf_error ("No n2= in input");
        if (!sf_histfloat (spdom, "d1", &dz)) sf_error ("No d1= in input");
        if (!sf_histfloat (spdom, "o1", &oz)) sf_error ("No o1= in input");
        if (!sf_histfloat (spdom, "d2", &dx)) sf_error ("No d2= in input");
        if (!sf_histfloat (spdom, "o2", &ox)) sf_error ("No o2= in input");
    }
    if (!sf_getint ("nz", &nz) && !spdom) sf_error ("Need nz=");
    /* Number of samples in z axis */
    if (!sf_getfloat ("oz", &oz) && !spdom) sf_error ("Need oz=");
    /* Beginning of z axis */
    if (!sf_getfloat ("dz", &dz) && !spdom) sf_error ("Need oz=");
    /* Sampling of z axis */
    if (!sf_getint ("nx", &nx) && !spdom) sf_error ("Need nx=");
    /* Number of samples in x axis */
    if (!sf_getfloat ("ox", &ox) && !spdom) sf_error ("Need ox=");
    /* Beginning of x axis */
    if (!sf_getfloat ("dx", &dx) && !spdom) sf_error ("Need ox=");
    /* Sampling of x axis */

    if (!sf_getint ("na", &na)) na = 360;
    /* Number of phase angles */

    da = 2.0*SF_PI/(float)na;
    oa = -SF_PI + 0.5*da;

    if (!sf_getbool ("verb", &verb)) verb = false;
    /* verbosity flag */

    if (spdom)
        sf_shiftdim (spdom, out, 1);

    /* Set up output */
    sf_putint (out, "n1", 4);
    sf_putfloat (out, "o1", 0.0);
    sf_putfloat (out, "d1", 1.0);
    sf_putstring (out, "label1", "Slowness components");
    sf_putstring (out, "unit1", "");
    sf_putint (out, "n2", nz);
    sf_putfloat (out, "o2", oz);
    sf_putfloat (out, "d2", dz);
    if (!spdom) {
        sf_putstring (out, "label2", "Depth");
        sf_putstring (out, "unit2", "");
    }
    sf_putint (out, "n3", nx);
    sf_putfloat (out, "o3", ox);
    sf_putfloat (out, "d3", dx);
    if (!spdom) {
        sf_putstring (out, "label3", "Lateral");
        sf_putstring (out, "unit3", "");
    }
    sf_putint (out, "n4", na);
    sf_putfloat (out, "d4", da*180.0/SF_PI);
    sf_putfloat (out, "o4", oa*180.0/SF_PI);
    sf_putstring (out, "label4", "Angle");
    sf_putstring (out, "unit4", "Degrees");

    if (!sf_getstring ("vspl")) sf_error ("Need vspl=");
    /* Spline coefficients for velocity model */
    vspline = sf_input ("vspl");

    if (!sf_histint (vspline, "Nc", &nc)) sf_error ("No Nc= in vspl");

    /* Slowness components module [(an)isotropic] */
    esc_slow = sf_esc_slowness2_init (vspline, verb);

    s = sf_floatalloc2 (4, nz);

    for (ia = 0; ia < na; ia++) {
        a = oa + ia*da;
        if (verb)
            sf_warning ("Computing angle plane %g;", a*180.0/SF_PI);
        for (ix = 0; ix < nx; ix++) {
            for (iz = 0; iz < nz; iz++) {
                 sf_esc_slowness2_get_components (esc_slow,
                                                  oz + iz*dz, ox + ix*dx, a,
                                                  &s[iz][0], &s[iz][3],
                                                  &s[iz][1], &s[iz][2]);
                if (s[iz][0] < smin)
                    smin = s[iz][0];
                if (s[iz][0] > smax)
                    smax = s[iz][0];
                /* Kahan summation algorithm to avoid roundoff errors
                   while accumulating the total sum */
                corr_nterm = s[iz][0] - corr;
                new_savg = savg + corr_nterm;
                corr = (new_savg - savg) - corr_nterm;
                savg = new_savg;
            }
            sf_floatwrite (&s[0][0], 4*nz, out);
        }
    }
    if (verb) {
        sf_warning (".");
        sf_warning ("Average velocity: %g [min=%g, max=%g]",
                    1.0/(savg/(float)((size_t)na*(size_t)nx*(size_t)nz)),
                    1.0/smax, 1.0/smin);
    }

    sf_esc_slowness2_close (esc_slow);

    free (s[0]);
    free (s);

    sf_fileclose (vspline);

    return 0;
}
Beispiel #23
0
int main(int argc, char* argv[])
{
    bool segy;
    int i, i1, i2, n1, n2, n3, n, nt, len, nkey, row;
    sf_file in, out;
    int mem; /* for avoiding int to off_t typecast warning */
    off_t memsize;
    char *eq, *output, *key, *arg;
    float **ftra=NULL, **fbuf=NULL, **fst=NULL, d2, o2;
    int **itra=NULL, **ibuf=NULL, **ist=NULL;
    sf_datatype type;

    sf_init (argc,argv);
    in = sf_input ("in");
    out = sf_output ("out");

    sf_putint(out,"N",0);
    sf_putint(out,"T",1);
    sf_putint(out,"input",2);

    type = sf_gettype(in);

    if (SF_FLOAT != type && SF_INT != type) sf_error("Need float or int input");

    if (!sf_getbool("segy",&segy)) segy=true;
    /* if SEGY headers */

    if (!sf_histint(in,"n1",&n1)) n1=1;
    if (!sf_histint(in,"n2",&n2)) n2=1;
    n3 = sf_leftsize(in,2); /* left dimensions after the first two */

    if (segy) {
	segy_init(n1,in);
    } else {
	other_init(n1,in);
    }

    if (NULL != (key = sf_getstring("key"))) { 
	/* key to replace */
	row = segykey(key);
	free(key);
    } else {
	if (!sf_getint("nkey",&row)) row=-1;
	/* number of key to replace */
    }	
    if (row > n1) sf_error("nkey=%d is too large, need nkey <= %d",row,n1);
    
    if (n1 > 1) {
	if (n2 > 1) { /* input: many keys */
	    if (row < 0) sf_putint(out,"n1",1);
	} else { /* input: one key, arranged in n1 */
	    n2 = n1;
	    n1 = 1;
	}
    }


    for (i=0; i < n1; i++) {
	sf_putint(out,segykeyword(i),i+3);
    }

    for (i=1; i< argc; i++) { /* collect inputs */
	arg = argv[i];
	eq =  strchr(arg,'=');
	if (NULL == eq) continue; /* not a parameter */
	if (0 == strncmp(arg,"output",6) ||
	    0 == strncmp(arg,    "--",2)) continue; /* not a key */

	len = (size_t) (eq-arg);
	key = sf_charalloc(len+1);
	memcpy(key,arg,len);
	key[len]='\0';

	if (sf_getint(key,&nkey))
	    sf_putint(out,key,nkey+3);
	free(key);
    }
  
    if (!sf_histfloat(in,n1>1? "d2":"d1",&d2)) d2=1.;
    if (!sf_histfloat(in,n1>1? "o2":"o1",&o2)) o2=0.;

    if (NULL == (output = sf_getstring("output"))) sf_error("Need output=");
    /* Describes the output in a mathematical notation. */

    if (!sf_getint("memsize",&mem))
        mem=sf_memsize();
    /* Max amount of RAM (in Mb) to be used */
    memsize = mem * (1<<20); /* convert Mb to bytes */

    len = sf_math_parse (output,out,type);

    /* number of traces for optimal I/O */
    nt = SF_MAX(1,memsize/((2*n1+len+6)*sizeof(float)));

    if (SF_FLOAT == type) { /* float type */
	ftra = sf_floatalloc2(n1,nt);
	fbuf = sf_floatalloc2(nt,n1+3);
	fst  = sf_floatalloc2(nt,len+3);
    } else {               /* int type */
	itra = sf_intalloc2(n1,nt);
	ibuf = sf_intalloc2(nt,n1+3);
	ist  = sf_intalloc2(nt,len+3);
    }

    for (n=n2*n3; n > 0; n -= nt) {
	if (n < nt) nt=n;

	if (SF_FLOAT == type) { 
	    sf_floatread(ftra[0],n1*nt,in);
	} else {
	    sf_intread(itra[0],n1*nt,in);
	}

	for (i2=0; i2 < nt; i2++) {
	    if (SF_FLOAT == type) { 
		fbuf[0][i2]=(float) i2;  /* N */
		fbuf[1][i2]=o2+i2*d2;    /* T */
		fbuf[2][i2]=ftra[0][i2]; /* input */
	    } else {
		ibuf[0][i2]=i2;          /* N */
		ibuf[1][i2]=o2+i2*d2;    /* T */
		ibuf[2][i2]=itra[0][i2]; /* input */
	    }
	}
	for (i1=0; i1 < n1; i1++) {
	    for (i2=0; i2 < nt; i2++) {
		if (SF_FLOAT == type) { 
		    fbuf[i1+3][i2]=ftra[i2][i1];
		} else {
		    ibuf[i1+3][i2]=itra[i2][i1];
		}
	    }
	}
	
	if (SF_FLOAT == type) { 
	    sf_math_evaluate (len, nt, fbuf, fst);
	    if (row < 0) {
		sf_floatwrite(fst[1],nt,out);
	    } else {
		for (i2=0; i2 < nt; i2++) {
		    ftra[i2][row] = fst[1][i2];
		}
		sf_floatwrite(ftra[0],n1*nt,out);
	    }
	} else {
	    sf_int_math_evaluate (len, nt, ibuf, ist);
	    if (row < 0) {
		sf_intwrite(ist[1],nt,out);
	    } else {
		for (i2=0; i2 < nt; i2++) {
		    itra[i2][row] = ist[1][i2];
		}
		sf_intwrite(itra[0],n1*nt,out);
	    }
	}
    }

    exit(0);
}
Beispiel #24
0
int main(int argc, char* argv[])
{
    /*------------------------------------------------------------*/
    /* Execution control, I/O files and geometry                  */
    /*------------------------------------------------------------*/
    bool verb,fsrf,snap,back,esou,tstp; /* execution flags */
    int  jsnap,ntsnap,jdata; /* jump along axes */
    int  shft; /* time shift for wavefield matching in RTM */

    /* I/O files */
    sf_file Fwav=NULL; /* wavelet   */
    sf_file Fsou=NULL; /* sources   */
    sf_file Frec=NULL; /* receivers */
    sf_file Fccc=NULL; /* velocity  */
    sf_file Fdat=NULL; /* data      */
    sf_file Fwfl=NULL; /* wavefield */
    sf_file Frnk=NULL; /* app. rank */
    sf_file Flft=NULL; /* left mat  */
    sf_file Frht=NULL; /* right mat */

    /* cube axes */
    sf_axis at,ax,ay,az; /* time, x, y, z */ 
    sf_axis asx,asy,arx,ary,ac;    /* sou, rec-x, rec-y, component */ 

    /* dimension, index and interval */
    int     nt,nz,nx,ny,ns,nr,nc,nb;
    int     it,iz,ix,iy;
    float   dt,dz,dx,dy;
    int     nxyz, nk;

    /* FDM and KSP structure */ //!!!JS
    fdm3d    fdm=NULL;
    dft3d    dft=NULL;
    clr3d    clr=NULL;

    /* I/O arrays for sou & rec */
    sf_complex***ww=NULL;    /* wavelet   */
    pt3d        *ss=NULL;    /* sources   */
    pt3d        *rr=NULL;    /* receivers */
    sf_complex **dd=NULL;    /* data      */

    /*------------------------------------------------------------*/
    /* displacement: um = U @ t-1; uo = U @ t; up = U @ t+1       */
    /*------------------------------------------------------------*/
    sf_complex ***uox, ***uoy, ***uoz, **uo;
    sf_complex ***upx=NULL, ***upy=NULL, ***upz=NULL, **up=NULL;
    sf_complex ***umx=NULL, ***umy=NULL, ***umz=NULL, **um=NULL;
    //sf_complex ***utmp;

    /*------------------------------------------------------------*/
    /* lowrank decomposition arrays                               */
    /*------------------------------------------------------------*/
    int ntmp, *n2s;
    sf_complex **lt, **rt;

    /*------------------------------------------------------------*/
    /* linear interpolation weights/indices                       */
    /*------------------------------------------------------------*/
    lint3d cs,cr; /* for injecting source and extracting data */

    /* Gaussian bell */
    int nbell;
    
    /*------------------------------------------------------------*/
    /* wavefield cut params                                       */
    /*------------------------------------------------------------*/
    sf_axis   acz=NULL,acx=NULL,acy=NULL;
    int       nqz,nqx,nqy;
    float     oqz,oqx,oqy;
    float     dqz,dqx,dqy;
    sf_complex***uc=NULL; /* tmp array for output wavefield snaps */

    /*------------------------------------------------------------*/
    /* init RSF                                                   */
    /*------------------------------------------------------------*/
    sf_init(argc,argv);

    /*------------------------------------------------------------*/
    /* OMP parameters                                             */
    /*------------------------------------------------------------*/
#ifdef _OPENMP
    omp_init();
#endif

    /*------------------------------------------------------------*/
    /* read execution flags                                       */
    /*------------------------------------------------------------*/
    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */
    if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */
    if(! sf_getbool("back",&back)) back=false; /* backward extrapolation flag (for rtm) */
    if(! sf_getbool("esou",&esou)) esou=false; /* explosive force source */
    if(! sf_getbool("tstp",&tstp)) tstp=false; /* two-step propagator */

    /*------------------------------------------------------------*/
    /* I/O files                                                  */
    /*------------------------------------------------------------*/
    Fwav = sf_input ("in" ); /* wavelet   */
    Fccc = sf_input ("ccc"); /* stiffness */
    Fsou = sf_input ("sou"); /* sources   */
    Frec = sf_input ("rec"); /* receivers */
    Frnk = sf_input ("rnk"); /* app. rank */
    Flft = sf_input ("lft"); /* left mat  */
    Frht = sf_input ("rht"); /* right mat */
    Fwfl = sf_output("wfl"); /* wavefield */
    Fdat = sf_output("out"); /* data      */

    /*------------------------------------------------------------*/
    /* axes                                                       */
    /*------------------------------------------------------------*/
    at = sf_iaxa(Fwav,4); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
    az = sf_iaxa(Fccc,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
    ax = sf_iaxa(Fccc,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space x */
    ay = sf_iaxa(Fccc,3); sf_setlabel(ay,"y"); if(verb) sf_raxa(ay); /* space y */

    asx = sf_iaxa(Fsou,2); sf_setlabel(asx,"sx"); if(verb) sf_raxa(asx); /* sources x */
    asy = sf_iaxa(Fsou,3); sf_setlabel(asy,"sy"); if(verb) sf_raxa(asy); /* sources y */
    arx = sf_iaxa(Frec,2); sf_setlabel(arx,"rx"); if(verb) sf_raxa(arx); /* receivers x */
    ary = sf_iaxa(Frec,3); sf_setlabel(ary,"ry"); if(verb) sf_raxa(ary); /* receivers y */

    nt = sf_n(at); dt = sf_d(at);
    nz = sf_n(az); dz = sf_d(az);
    nx = sf_n(ax); dx = sf_d(ax);
    ny = sf_n(ay); dy = sf_d(ay);

    ns = sf_n(asx)*sf_n(asy);
    nr = sf_n(arx)*sf_n(ary);

    /*------------------------------------------------------------*/
    /* other execution parameters                                 */
    /*------------------------------------------------------------*/
    if(! sf_getint("nbell",&nbell)) nbell=NOP;  /* bell size */
    if(verb) sf_warning("nbell=%d",nbell);
    if(! sf_getint("jdata",&jdata)) jdata=1;
    if(snap) {  /* save wavefield every *jsnap* time steps */
	if(! sf_getint("jsnap",&jsnap)) jsnap=nt;
    }
    if(back) {
        shft = (nt-1)%jsnap;
        sf_warning("For backward extrapolation, make sure nbell(%d)=0",nbell);
    } else shft = 0;

    /*------------------------------------------------------------*/
    /* expand domain for FD operators and ABC                     */
    /*------------------------------------------------------------*/
    if( !sf_getint("nb",&nb)) nb=NOP;

    fdm=fdutil3d_init(verb,fsrf,az,ax,ay,nb,1);
    if(nbell) fdbell3d_init(nbell);

    sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az);
    sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax);
    sf_setn(ay,fdm->nypad); sf_seto(ay,fdm->oypad); if(verb) sf_raxa(ay);

    /*------------------------------------------------------------*/
    /* 3D vector components                                       */
    /*------------------------------------------------------------*/
    nc=3;
    ac=sf_maxa(nc,0,1); /* output 3 cartesian components */

    /*------------------------------------------------------------*/
    /* setup output data header                                   */
    /*------------------------------------------------------------*/
    sf_settype(Fdat,SF_COMPLEX);
    sf_oaxa(Fdat,arx,1);
    sf_oaxa(Fdat,ary,2);
    sf_oaxa(Fdat,ac,3);

    sf_setn(at,nt/jdata);
    sf_setd(at,dt*jdata);
    sf_oaxa(Fdat,at,4);

    /* setup output wavefield header */
    if(snap) {
	if(!sf_getint  ("nqz",&nqz)) nqz=sf_n(az);
	if(!sf_getint  ("nqx",&nqx)) nqx=sf_n(ax);
	if(!sf_getint  ("nqy",&nqy)) nqy=sf_n(ay);

	if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az);
	if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax);
	if(!sf_getfloat("oqy",&oqy)) oqy=sf_o(ay);

	dqz=sf_d(az);
	dqx=sf_d(ax);
	dqy=sf_d(ay);

	acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz);
	acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx);
	acy = sf_maxa(nqy,oqy,dqy); sf_raxa(acy);

	uc=sf_complexalloc3(sf_n(acz),sf_n(acx),sf_n(acy));

	ntsnap=0; /* ntsnap = it/jsnap+1; */
	for(it=0; it<nt; it++) {
	    if(it%jsnap==0) ntsnap++;
	}
	sf_setn(at,  ntsnap);
	sf_setd(at,dt*jsnap);
	if(verb) sf_raxa(at);

        sf_settype(Fwfl,SF_COMPLEX);
	sf_oaxa(Fwfl,acz,1);
	sf_oaxa(Fwfl,acx,2);
	sf_oaxa(Fwfl,acy,3);
	sf_oaxa(Fwfl,ac, 4);
	sf_oaxa(Fwfl,at, 5);
    }

    /*------------------------------------------------------------*/
    /* source and data array                                      */
    /*------------------------------------------------------------*/
    ww=sf_complexalloc3(ns,nc,nt); /* Fast axis: n_sou > n_comp > n_time */
    sf_complexread(ww[0][0],nt*nc*ns,Fwav);

    dd=sf_complexalloc2(nr,nc);

    /*------------------------------------------------------------*/
    /* setup source/receiver coordinates                          */
    /*------------------------------------------------------------*/
    ss = (pt3d*) sf_alloc(ns,sizeof(*ss)); 
    rr = (pt3d*) sf_alloc(nr,sizeof(*rr)); 

    pt3dread1(Fsou,ss,ns,3); /* read (x,y,z) coordinates */
    pt3dread1(Frec,rr,nr,3); /* read (x,y,z) coordinates */

    /* calculate 3d linear interpolation coef for sou & rec */
    cs = lint3d_make(ns,ss,fdm);
    cr = lint3d_make(nr,rr,fdm);

    /*------------------------------------------------------------*/
    /* allocate and initialize wavefield arrays                   */
    /*------------------------------------------------------------*/
    /* z-component */
    uoz=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    if (tstp) {
        upz=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
        umz=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    }

    /* x-component */
    uox=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    if (tstp) {
        upx=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
        umx=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    }

    /* y-component */
    uoy=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    if (tstp) {
        upy=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
        umy=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    }

    /* wavefield vector */
    uo = (sf_complex**) sf_alloc(3,sizeof(sf_complex*));
    uo[0] = uox[0][0]; uo[1] = uoy[0][0]; uo[2] = uoz[0][0];
    if (tstp) {
        up = (sf_complex**) sf_alloc(3,sizeof(sf_complex*));
        up[0] = upx[0][0]; up[1] = upy[0][0]; up[2] = upz[0][0];
        um = (sf_complex**) sf_alloc(3,sizeof(sf_complex*));
        um[0] = umx[0][0]; um[1] = umy[0][0]; um[2] = umz[0][0];
    }

    /* initialize fft and lrk */
    dft = dft3d_init(1,false,false,fdm);
    nxyz= fdm->nypad*fdm->nxpad*fdm->nzpad;
    nk  = dft->nky  *dft->nkx  *dft->nkz;

    /*------------------------------------------------------------*/ 
    /* allocation I/O arrays                                      */
    /*------------------------------------------------------------*/ 
    n2s = sf_intalloc(6);
    sf_intread(n2s,6,Frnk);
    clr = clr3d_make(n2s,fdm);
    clr3d_init(fdm,dft,clr);

    /* check the dimension */
    if (!sf_histint(Flft,"n1",&ntmp) || ntmp != nxyz)        sf_error("Need n1=%d in left",nxyz);
    if (!sf_histint(Flft,"n2",&ntmp) || ntmp != clr->n2_sum) sf_error("Need n2=%d in left",clr->n2_sum);
    if (!sf_histint(Frht,"n1",&ntmp) || ntmp != nk)          sf_error("Need n1=%d in right",nk);
    if (!sf_histint(Frht,"n2",&ntmp) || ntmp != clr->n2_sum) sf_error("Need n2=%d in right",clr->n2_sum);
  
    lt  = sf_complexalloc2(nxyz,clr->n2_sum); 
    rt  = sf_complexalloc2(nk  ,clr->n2_sum); 
    sf_complexread(lt[0],nxyz*clr->n2_sum,Flft);
    sf_complexread(rt[0],nk  *clr->n2_sum,Frht);

    /* initialize to zero */
#ifdef _OPENMP
#pragma omp parallel for              \
    schedule(dynamic,1)               \
    private(iy,ix,iz)                 \
    shared(fdm,uoz,uox,uoy,upz,upx,upy,umz,umx,umy)
#endif
    for        (iy=0; iy<fdm->nypad; iy++) {
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nzpad; iz++) {
		uoz[iy][ix][iz]=sf_cmplx(0.,0.); uox[iy][ix][iz]=sf_cmplx(0.,0.); uoy[iy][ix][iz]=sf_cmplx(0.,0.);
                if (tstp) {
                    upz[iy][ix][iz]=sf_cmplx(0.,0.); upx[iy][ix][iz]=sf_cmplx(0.,0.); upy[iy][ix][iz]=sf_cmplx(0.,0.);
                    umz[iy][ix][iz]=sf_cmplx(0.,0.); umx[iy][ix][iz]=sf_cmplx(0.,0.); umy[iy][ix][iz]=sf_cmplx(0.,0.);
                }
	    }
	}
    }

    /*------------------------------------------------------------*/ 
    /*------------------------ MAIN LOOP -------------------------*/ 
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<nt; it++) {
        if(verb) sf_warning("it=%d/%d;",it,nt); /*fprintf(stderr,"\b\b\b\b\b%d",it);*/

	/*------------------------------------------------------------*/
	/* apply lowrank matrix to wavefield vector                   */
        /*------------------------------------------------------------*/
        if (tstp) {
            clr3d_apply2(up, uo, um, lt, rt, fdm, dft, clr);
#ifdef _OPENMP
#pragma omp parallel for                      \
            schedule(dynamic,1)               \
            private(iy,ix,iz)                 \
            shared(fdm,uoz,uox,uoy,upz,upx,upy,umz,umx,umy)
#endif
            for        (iy=0; iy<fdm->nypad; iy++) {
                for    (ix=0; ix<fdm->nxpad; ix++) {
                    for(iz=0; iz<fdm->nzpad; iz++) {
                        umz[iy][ix][iz]=uoz[iy][ix][iz]; umx[iy][ix][iz]=uox[iy][ix][iz]; umy[iy][ix][iz]=uoy[iy][ix][iz];
                        uoz[iy][ix][iz]=upz[iy][ix][iz]; uox[iy][ix][iz]=upx[iy][ix][iz]; uoy[iy][ix][iz]=upy[iy][ix][iz];
                    }
                }
            }
            //utmp = umz; umz = uoz; uoz = upz; upz = utmp;
            //utmp = umx; umx = uox; uox = upx; upx = utmp;
            //utmp = umy; umy = uoy; uoy = upy; upy = utmp;
        } else clr3d_apply(uo, uo, lt, rt, fdm, dft, clr);

	/*------------------------------------------------------------*/
	/* free surface */
	/*------------------------------------------------------------*/
	if(fsrf) { /* need to do something here */ }

        /*------------------------------------------------------------*/
	/* inject displacement source                                 */
	/*------------------------------------------------------------*/
        if(esou) {
            /* exploding force source */
            lint3d_expl_complex(uoz,uox,uoy,ww[it],cs);
        } else {
            if(nbell) {
                lint3d_bell_complex(uoz,ww[it][0],cs);
                lint3d_bell_complex(uox,ww[it][1],cs);
                lint3d_bell_complex(uoy,ww[it][2],cs);
            } else {
                lint3d_inject_complex(uoz,ww[it][0],cs);
                lint3d_inject_complex(uox,ww[it][1],cs);
                lint3d_inject_complex(uoy,ww[it][2],cs);
            }
        }

	/*------------------------------------------------------------*/
	/* cut wavefield and save */
	/*------------------------------------------------------------*/
        if(snap && (it-shft)%jsnap==0) {
            cut3d_complex(uoz,uc,fdm,acz,acx,acy);
            sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl);

            cut3d_complex(uox,uc,fdm,acz,acx,acy);
            sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl);

            cut3d_complex(uoy,uc,fdm,acz,acx,acy);
            sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl);
        }

        lint3d_extract_complex(uoz,dd[0],cr);
        lint3d_extract_complex(uox,dd[1],cr);
        lint3d_extract_complex(uoy,dd[2],cr);
        if(it%jdata==0) sf_complexwrite(dd[0],nr*nc,Fdat);

    }
    if(verb) sf_warning(".");
    if(verb) fprintf(stderr,"\n");    
    
    /*------------------------------------------------------------*/
    /* deallocate arrays */
    
    free(dft);
    dft3d_finalize();
    free(clr);
    clr3d_finalize();

    free(**ww); free(*ww); free(ww);
    free(ss);
    free(rr);
    free(*dd);  free(dd);

    free(n2s);
    free(*lt); free(lt); free(*rt); free(rt);

    free(**uoz); free(*uoz); free(uoz);
    free(**uox); free(*uox); free(uox);
    free(**uoy); free(*uoy); free(uoy);
    free(uo);
    if (tstp) {
        free(**upz); free(*upz); free(upz);
        free(**upx); free(*upx); free(upx);
        free(**upy); free(*upy); free(upy);
        free(up);
        free(**umz); free(*umz); free(umz);
        free(**umx); free(*umx); free(umx);
        free(**umy); free(*umy); free(umy);
        free(um);
    }

    if (snap) {
       free(**uc);  free(*uc);  free(uc);    
    }

    /*------------------------------------------------------------*/


    exit (0);
}
Beispiel #25
0
int main(int argc, char* argv[])
{
    int n,k,nc,i,j;

    int done;       /* boolean for more combinations to compute */
    int *a;         /* list of elements in the current combination (not needed at startup) */
    int **mask;

    sf_axis areplic;
    sf_file in,out;

    sf_init(argc,argv);

    in = sf_input("in");
    out = sf_output("out");
    sf_settype(out,SF_INT);

    if (!sf_getint("k",&k)) sf_error("Need k=");
    /* combination of k elements */

    /* input file */
    if (!sf_histint(in,"n1",&n)) sf_error("No n1=");

    nc = binomial(n,k);
    sf_warning("Number of combinations is %3d",nc);

    /* output file parameters */
    areplic = sf_maxa(nc,0,1);
    sf_oaxa(out,areplic,2);

    sf_putstring (out,"label2", "replication");

    /* memory allocations */
    a = sf_intalloc(k);
    mask = sf_intalloc2(n,nc);

    done = 1;
    j = 0;

    while (1) {

        /* Combination of k elements out of n */
	comb_next(n,k,a,&done);

	if (done) break;
        /* done = 1 if more combinations to compute */
        /* done = 0 when the list is exhausted. */

	for (i = 0; i < k; i++) fprintf(stderr," %3d",a[i]);
	fprintf(stderr," \n");

	for (i = 0; i < n; i++) mask[j][i] = 1;
	for (i = 0; i < k; i++) mask[j][a[i]-1] = 0;

	j++;

    }

    /* output */ 
    sf_warning("Number of combinations is %3d",nc);
    sf_intwrite(mask[0],n*nc,out);

    exit(0);
}
Beispiel #26
0
int main (int argc, char* argv[])
{
    sf_map4 nmo; /* using cubic spline interpolation */
    bool half, slow;
    int it,ix,ih, nt,nx, nh, CDPtype;
    float dt, t0, h, h0, f, dh, eps, dy;
    float *trace, *vel, *off, *str, *out;
    sf_file cmp, nmod, velocity, offset;

    sf_init (argc,argv);
    cmp = sf_input("in");
    velocity = sf_input("velocity");
    nmod = sf_output("out");

    if (SF_FLOAT != sf_gettype(cmp)) sf_error("Need float input");
    if (!sf_histint(cmp,"n1",&nt)) sf_error("No n1= in input");
    if (!sf_histfloat(cmp,"d1",&dt)) sf_error("No d1= in input");
    if (!sf_histfloat(cmp,"o1",&t0)) sf_error("No o1= in input");

    if (!sf_histint(cmp,"n2",&nh)) sf_error("No n2= in input");

    off = sf_floatalloc(nh);

    if (!sf_getbool("half",&half)) half=true;
    /* if y, the second axis is half-offset instead of full offset */


    CDPtype=1;
    if (NULL != sf_getstring("offset")) {
	offset = sf_input("offset");
	sf_floatread (off,nh,offset);
	sf_fileclose(offset);
    } else {
	if (!sf_histfloat(cmp,"d2",&dh)) sf_error("No d2= in input");
	if (!sf_histfloat(cmp,"o2",&h0)) sf_error("No o2= in input");
	
	if (sf_histfloat(cmp,"d3",&dy)) {
	    CDPtype=half? 0.5+dh/dy : 0.5+0.5*dh/dy;
	    if (CDPtype < 1) {
		CDPtype=1;
	    } else if (1 != CDPtype) {
		sf_histint(cmp,"CDPtype",&CDPtype);
	    	sf_warning("CDPtype=%d",CDPtype);
	    }
	} 

	for (ih = 0; ih < nh; ih++) {
	    off[ih] = h0 + ih*dh; 
	}
    }

    if (!sf_getbool("slowness",&slow)) slow=false;
    /* if y, use slowness instead of velocity */

    nx = sf_leftsize(cmp,2);

    if (!sf_getfloat ("h0",&h0)) h0=0.;
    /* reference offset */
    if (half) h0 *= 2.;
    if (!sf_getfloat("eps",&eps)) eps=0.01;
    /* stretch regularization */

    trace = sf_floatalloc(nt);
    vel = sf_floatalloc(nt);
    str = sf_floatalloc(nt);
    out = sf_floatalloc(nt);

    nmo = sf_stretch4_init (nt, t0, dt, nt, eps);
    
    for (ix = 0; ix < nx; ix++) {
	sf_floatread (vel,nt,velocity);	

	for (ih = 0; ih < nh; ih++) {
	    sf_floatread (trace,nt,cmp);
	    
	    h = off[ih] + (dh/CDPtype)*(ix%CDPtype); 
	    if (half) h *= 2;
	    h = h*h - h0*h0;
	    
	    for (it=0; it < nt; it++) {
		f = t0 + it*dt;
		if (slow) {
		    f = f*f + h*vel[it]*vel[it];
		} else {
		    f = f*f + h/(vel[it]*vel[it]);
		}
		if (f < 0.) {
		    str[it]=t0-10.*dt;
		} else {
		    str[it] = sqrtf(f);
		}
	    }

	    sf_stretch4_define (nmo,str);
	    sf_stretch4_apply (false,nmo,trace,out);
	    
	    sf_floatwrite (out,nt,nmod);
	}
    }


    exit (0);
}
Beispiel #27
0
void vofz_table(char   type  /* type of distribution */,
		float z      /* depth */,
		float h      /* offset */,
		ktable table /* output traveltime attributes */)
/*< compute traveltime attributes >*/
{
    int iz, nz, iter;
    float p,vz,t,hp,dhp,zn,vn,cos2,q;

    nz = SF_MIN(floorf(z/d1),n1);
    zn = z-nz*d1;
    vn = (nz < n1)? v[nz]:v[n1-1];

    switch(type) {
	case 'h': /* hyperbolic approximation */


	    t = vz = 0.0;
	    for (iz=0; iz < nz; iz++) {
		vz += v[iz];
		t += 1.0f/v[iz];
	    }
	    if (iz == n1) iz--;

	    vz = vz*d1 + vn*zn; 
	    t  =  t*d1 + zn/vn;
	    
	    table->t = sqrtf(t*(t+h*h/vz));
	    break;
	case 'e': /* exact */
	    p = 0.0;
	    
	    for (iter=0; iter < niter; iter++) {
		hp = 0.0;
		dhp = 0.0;
		
		for (iz=0; iz < nz; iz++) {
		    cos2 = fabsf(1.0f-p*p*v[iz]*v[iz]);
		    q = v[iz]/sqrtf(cos2);

		    hp += q;
		    dhp += q/cos2;
		}
		cos2 = fabsf(1.0f-p*p*vn*vn);
		q = vn/sqrtf(cos2);

		hp = p*(hp*d1+q*zn);
		dhp = dhp*d1+q*zn/cos2;
		
		/* Newton step */			
		p -= (hp-h)/dhp;

		/* Check convergence */
	    }
	    
	    t = 0.0;
	    for (iz=0; iz < nz; iz++) {
		cos2 = fabsf(1.0f-p*p*v[iz]*v[iz]);
		t += 1.0f/(v[iz]*sqrtf(cos2));
	    }
	    cos2 = fabsf(1.0f-p*p*vn*vn);
	    t = t*d1 + zn/(vn*sqrtf(cos2));
	    
	    table->t = t;
	    break;
	default:
	    sf_error("Unknown type");
	    break;
    }
}
Beispiel #28
0
int main(int argc, char *argv[])
{
    int i, j, n1, n2, n3, n12; /*n1 is trace length, n2 is the number of traces, n3 is the number of 3th axis*/
    int order1, cut;
    float *data1, *data2, /* *adata, */ **dd;
    sf_file in, out, dip, slet, sletcut;
    float eps;
    char *type;
    bool unit=false, inv=true;

    sf_init(argc,argv);

    in = sf_input("in");
    dip=sf_input("dip");

    out = sf_output("out");
    slet=sf_output("slet");
    sletcut=sf_output("sletcut");

    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    n12=n1*n2;
    n3 = sf_leftsize(in,2);
    /* get the trace length (n1) and the number of traces (n2) and n3*/

    if(!sf_getint("cut",&cut)) cut=n2/4;
    /* cut threshold value */

    if(!sf_getfloat("eps",&eps)) eps=0.01;
    /* regularization */

    if(!sf_getint("order1",&order1))order1=1;
    /* accuracy order for seislet transform*/

    if (NULL == (type=sf_getstring("type"))) type="linear";
    /* [haar,linear,biorthogonal] wavelet type, the default is linear  */

    if(!sf_getfloat("eps",&eps)) eps=0.01;
    /* regularization */
   

    data1 = sf_floatalloc(n1*n2);	/*allocate memory*/
    data2 = sf_floatalloc(n1*n2);	/*allocate memory*/
    /*   adata = sf_floatalloc(n1*n2);	allocate memory*/
    dd=sf_floatalloc2(n1,n2);

    seislet_init(n1,n2,inv,unit,eps,order1,type[0]);  /* unit=false inv=true */

    for(i=0;i<n3;i++)  {
	sf_floatread(data1,n1*n2,in);
	seislet_set(dd);
	sf_floatread(dd[0],n12,dip);

	seislet_lop(true,false,n12,n12,data2,data1); 
 	/*data1 is the input raw data and data2 is the unsorted seislet domain seismic data.*/

	if(NULL!=sf_getstring("slet"))	
	sf_floatwrite(data2,n1*n2,slet);
	/* seismic domain */

	/***********************************************************/
	/*applying mask operator*/
	for(i=cut; i<n2; i++)
		for(j=0;j<n1;j++)
			data2[i*n1+j]=0;
	/***********************************************************/

	if(NULL!=sf_getstring("sletcut"))	
	sf_floatwrite(data2,n1*n2,sletcut);
	/* cutted seislet domain */

	seislet_lop(false,false,n12,n12,data2,data1);	
	/*data2 is sorted seislet domain seismic data and data1 is denoised t-x domain seismic data*/

	sf_floatwrite(data1,n1*n2,out);
    }

    exit(0);
}
Beispiel #29
0
int main(int argc, char* argv[])
{
  sf_file infile=NULL, out=NULL, inheaders=NULL;
  int n1_traces;
  int n1_headers;

  int n_traces, n_headers; 
  sf_datatype typehead;
  sf_datatype typein;
/*  float* header=NULL; */
  char* infile_filename=NULL;
  char* headers_filename=NULL;
  /*
  indxkey** pntrs_to_indxkeys;
  char* sort;
  char** sortheadernames;
  char** sorttokens1;
  double* signsortname;
  double* sortmin;
  double* sortmax;
  double* sortinc;
  int n_traces_sort;
  int n_pntrs_to_indxkeys;
  int i1_headers;
  int* indx_of_keys;
  int isortheader;
  int sizeof_my_indxkey;
  bool allzero;
  bool passrangetest;
  */
  char* ilinekey;
/*  char* xlinekey; */
  int ilineinc;
  int indx_ilinekey;
/*  int indx_xlinekey; */
  float ilinemin;
  float ilinemax;
  float xlinemin;
  float xlinemax;
  int headers_in_buffer;
  int i_header;
  int ii_header;
  int n_headers_read;
  int n_headers_left;
  double doubleiline;
  float** header_buf=NULL;
  int indx_number;
  int min_indx_number;
  int max_indx_number;

  
  sf_init (argc,argv);

  /*****************************/
  /* initialize verbose switch */
  /*****************************/
  /* verbose flag controls amount of print */
  /*( verbose=1 0 terse, 1 informative, 2 chatty, 3 debug ) */
  /* fprintf(stderr,"read verbose switch.  getint reads command line.\n"); */
  if(!sf_getint("verbose",&verbose))verbose=1;
  /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  if(verbose>0)fprintf(stderr,"verbose=%d\n",verbose);
 
  /*****************************************/
  /* initialize the input and output files */
  /*****************************************/
  if(verbose>0)fprintf(stderr,"read name of input file name\n");
  
  infile_filename=sf_getstring("input");
  /* \n
     Input file for traces amplitudes
  */
  if(infile_filename==NULL) infile = sf_input ("in");
  else infile = sf_input (infile_filename);

  if(verbose>0)
    fprintf(stderr,"set up output file for tah - should be stdout\n");
  out = sf_output ("out");

  if(verbose>0)fprintf(stderr,"read name of input headers file\n");
  headers_filename=sf_getstring("headers");
  /* \n
     Trace header file name.  Default is the input data file
     name, with the final .rsf changed to _hdr.rsf 
  */

  if(headers_filename==NULL){
    /* compute headers_filename from infile_filename by replacing the final
       .rsf with _hdr.rsf */
    if(!(0==strcmp(infile_filename+strlen(infile_filename)-4,".rsf"))){
	fprintf(stderr,"parameter input, the name of the input file, does\n");
	fprintf(stderr,"not end with .rsf, so header filename cannot be\n");
	fprintf(stderr,"computed by replacing the final .rsf with _hdr.rsf.\n");
	sf_error("default for headers parameter cannot be computed.");
    }
    /* allocate header file name to be length of infile_filename +4+1 to allow 
       _hdr to be inserted and \0 to be appended.  */
    headers_filename=malloc(strlen(infile_filename)+4+1);
    headers_filename[0]='\0'; /* make string zero length, null terminated */
    strcpy(headers_filename,infile_filename);
    strcpy(headers_filename+strlen(infile_filename)-4,"_hdr.rsf\0");
    if(verbose>2)
      fprintf(stderr,"parameter header defaulted.  Computed to be #%s#\n",
			 headers_filename);
  }
  if(verbose>2)
    fprintf(stderr,"parameter header input or computed  #%s#\n",
		       headers_filename);

  inheaders = sf_input(headers_filename);

  if (!sf_histint(infile,"n1",&n1_traces))
    sf_error("input file does not define n1");
  if (!sf_histint(inheaders,"n1",&n1_headers)) 
    sf_error("input headers file does not define n1");

  n_traces=sf_leftsize(infile,1);
  n_headers=sf_leftsize(inheaders,1);
  if(verbose>0)
    fprintf(stderr,"n_traces=%d, n_headers=%d\n",n_traces,n_headers);

  if(n_traces!=n_headers){
    fprintf(stderr,"n_traces=%d, n_headers=%d\n",n_traces,n_headers);
    sf_error("number of traces and headers must be the same");
  }

  typehead = sf_gettype(inheaders);
  if (SF_INT != typehead && SF_FLOAT != typehead ) 
    sf_error("Need float or int input headers.");

  typein = sf_gettype(infile);
  if (SF_FLOAT != typein ) 
    sf_error("input file must contain floats.");
  
  /* must be float or int */
  if(verbose>1)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  /* header = sf_floatalloc(n1_headers); */
 
  /* I make stdout just trace header and one trace amplitude for now.
     I do not think it will have a use and will delete it later */
  if(verbose>1)
    fprintf(stderr,"need to add 2 words, record type and record length.\n");
  sf_putint(out,"n1",1+n1_headers+2);
  sf_putint(out,"n1_traces",1);

  /*************************************************************************/
  /* put info into history so later tah programs can figure out the headers*/
  /*************************************************************************/
  if(verbose>1)fprintf(stderr,"put n1_headers to history\n");
  sf_putint(out,"n1_headers",n1_headers);
  if(verbose>1)fprintf(stderr,"test typehead\n");
  if(SF_INT == typehead) sf_putstring(out,"header_format","native_int");
    else                 sf_putstring(out,"header_format","native_float");
  sf_putstring(out,"headers",headers_filename);
  /* the list of any extra header keys */
  if(verbose>1)fprintf(stderr,"segy_init\n");
  segy_init(n1_headers,inheaders);
  if(verbose>1)fprintf(stderr,"segy2hist\n");
  segy2hist(out,n1_headers);
  
  /* put the history from the input file to the output */
  if(verbose>1)fprintf(stderr,"fileflush out\n");
  sf_fileflush(out,infile);
  
  /* get the index keys.  Normally this will be iline and xline */
  if(!(ilinekey=sf_getstring("iline")))ilinekey="iline";
  /* \n
     header key for the main index key.  This should be iline, but you 
     may have non-standard trace headers or a wierd use of this program 
  */
/*  if(!(xlinekey=sf_getstring("xline")))xlinekey="xline"; */
  /* \n
     header key for the secondary index key.  This should be xline, but you
     may have non-standard trace headers or a wierd use of this program 
  */
  
  /* ilinemin, ilinemax, xlinemin, and xlinemaxcan be used to remove
     be trace headers with nulls. They default to -1e31, +1e31 */
  if(!sf_getfloat("ilinemin",&ilinemin))ilinemin=-1e31;
  /* \n
     minimum "iline" header key to include in the index.  Use this parameter
     to remove null trace headers or traces outside project area.
  */
  if(!sf_getfloat("ilinemax",&ilinemax))ilinemax=-1e31;
  /* \n
     maximum "iline" header key to include in the index.  Use this parameter
     to remove null trace headers or traces outside project area.
  */
  if(!sf_getfloat("xlinemin",&xlinemin))xlinemin=-1e31;
  /* \n
     minimum "xline" header key to include in the index.  Use this parameter
     to remove null trace headers or traces outside project area.
  */
  if(!sf_getfloat("xlinemax",&xlinemax))xlinemax=-1e31;
  /* \n
     maximum "xline" header key to include in the index.  Use this parameter
     to remove null trace headers or traces outside project area.
  */

  sf_getstring("indxdir");
  /* \n
     The name of the directory containing the iline,xline index.  This 
     directory will be in DATAPATH (probably the environment variable). The 
     directory also continues a file "filenames", a list of the trace and 
     header files that contributes to this index. The directory contains files 
     with names "indx#" here # is an integer multiple of ilineinc. These files 
     contains a record for each contributing trace with filenumber, 
     tracenumber, and the trace header. The file containing the trace is 
     determined using the  can be read by using the filenumber and the 
     "filenames" file.  The tracenumber defines the location of the trace 
     in the file.
  */

  if(!sf_getint("ilineinc",&ilineinc))ilineinc=10;
  /* \n
     incrment in iline for the index
  */

  indx_ilinekey=segykey(ilinekey);
/*  indx_xlinekey=segykey(xlinekey); */

  fprintf(stderr,"process the filenames file\n");  
  /* kls process the file, "filenames"
     -if it does not exist create it
     -if it already exists, count the number files already in "filenemae"
     -add the traceheaders and traces files to "filenames"
     -remenber the file_number of this trace/header input file
  */

  /* make buffer 1 gbyte, about 25 million traces. that should be reasonable */
  headers_in_buffer=1000*1000*1000/(4*n1_headers);
  header_buf=sf_floatalloc2(n1_headers,headers_in_buffer);
  /* sf_alloc2 for array of trace headers */
  if(verbose>0)fprintf(stderr,"start loop to read headers to build sort key\n");
  for (i_header=0; i_header<n_headers; i_header+=headers_in_buffer){
    /* read smaller of headers_in_buffer and number of headers left to read */
    fprintf(stderr,"read a buffer of trace headers\n");
    n_headers_read=headers_in_buffer;
    n_headers_left=n_headers-i_header;
    if(n_headers_read>n_headers_left)n_headers_read=n_headers_left;
    fprintf(stderr,"read words=%d\n",n1_headers*n_headers_read);
    sf_floatread(&header_buf[0][0],n1_headers*n_headers_read,inheaders);
    fprintf(stderr,"read the headers\n");
    /* find the minimum and maximum indx_number this buffer contributes to */
    if(typehead==SF_FLOAT)doubleiline=        header_buf [0][indx_ilinekey];
    else                  doubleiline=((int**)header_buf)[0][indx_ilinekey];
    indx_number=rounddouble(doubleiline/((double)ilineinc))*ilineinc;
    min_indx_number=max_indx_number=indx_number;
    fprintf(stderr,"loop to find min/max\n");
    for (ii_header=0; ii_header<n_headers_read; ii_header++){
      if(typehead==SF_FLOAT){
	doubleiline=        header_buf [ii_header][indx_ilinekey];
      }else{                  
	doubleiline=((int**)header_buf)[ii_header][indx_ilinekey];
      }
      fprintf(stderr,"ii_header=%d,doubleiline=%f %d\n",
              ii_header,doubleiline,
	      ((int**)header_buf)[ii_header][indx_ilinekey]);
      indx_number=rounddouble(doubleiline/((double)ilineinc))*ilineinc;
      if(min_indx_number>indx_number)min_indx_number=indx_number;
      if(max_indx_number<indx_number)max_indx_number=indx_number;
    }
    fprintf(stderr,"min_indx_number=%d, max_indx_number=%d\n",
	    min_indx_number   ,max_indx_number);
    /* kls pass over each indx_file:
       -open indx_file (create it if it does not exist)
       -loop over all the headers in header_buf
          -write filenumber, traceindx, and the trace header to the end of
	     indx_file 
       -close the indx_file   */
    /* to convert relative to absolute path:
       #include <stdio.h>
       #include <stdlib.h>
       int main(int c,char**v) {
  
         fprintf(stderr,"%s\n",realpath(v[1],0));
         return 0;
       }

    */







  } /* end of the for loop that reads a buffer of headers */









  exit(0);
}
Beispiel #30
0
void rtm_q(sf_file Fdat, sf_file Fimg, sf_mpi *mpipar, sf_sou soupar, sf_acqui acpar, sf_vec_q array, bool verb)
/*< visco-acoustic rtm >*/
{
	int ix, iz, is, ir, it, wit;
	int sx, rx, sz, rz, frectx, frectz;
	int nz, nx, nzx, padnz, padnx, padnzx, nt, nr, nb, wnt;

	float dx2, dz2, dt2, dt;
	float **vv, **tau, **taus, **dd, **mm;
	float **p0, **p1, **p2, **r1, **r2, **term, **tmparray, *rr, ***wave;
	float *sendbuf, *recvbuf;

	MPI_Comm comm=MPI_COMM_WORLD;

	nz=acpar->nz;
	nx=acpar->nx;
	nzx=nz*nx;
	padnz=acpar->padnz;
	padnx=acpar->padnx;
	padnzx=padnz*padnx;
	nr=acpar->nr;
	nb=acpar->nb;
	sz=acpar->sz;
	rz=acpar->rz;
	frectx=soupar->frectx;
	frectz=soupar->frectz;

	nt=acpar->nt;
	wnt=(nt-1)/acpar->interval+1;

	dx2=acpar->dx*acpar->dx;
	dz2=acpar->dz*acpar->dz;
	dt2=acpar->dt*acpar->dt;
	dt=acpar->dt;

	/* memory allocation */
	vv = sf_floatalloc2(padnz, padnx);
	tau= sf_floatalloc2(padnz, padnx);
	taus=sf_floatalloc2(padnz, padnx);
	dd=sf_floatalloc2(nt, nr);
	mm=sf_floatalloc2(nz, nx);

	p0=sf_floatalloc2(padnz, padnx);
	p1=sf_floatalloc2(padnz, padnx);
	p2=sf_floatalloc2(padnz, padnx);
	r1=sf_floatalloc2(padnz, padnx);
	r2=sf_floatalloc2(padnz, padnx);
	term=sf_floatalloc2(padnz, padnx);
	rr=sf_floatalloc(padnzx);
	wave=sf_floatalloc3(nz, nx, wnt);

	/* padding and convert vector to 2-d array */
	pad2d(array->vv, vv, nz, nx, nb);
	pad2d(array->tau, tau, nz, nx, nb);
	pad2d(array->taus, taus, nz, nx, nb);

	memset(mm[0], 0., nzx*sizeof(float));

	for(is=mpipar->cpuid; is<acpar->ns; is+=mpipar->numprocs){
		sf_warning("###### is=%d ######", is+1);

		memset(p0[0], 0., padnzx*sizeof(float));
		memset(p1[0], 0., padnzx*sizeof(float));
		memset(p2[0], 0., padnzx*sizeof(float));
		memset(r1[0], 0., padnzx*sizeof(float));
		memset(r2[0], 0., padnzx*sizeof(float));
		memset(term[0], 0., padnzx*sizeof(float));
		
		sx=acpar->s0_v+is*acpar->ds_v;
		source_map(sx, sz, frectx, frectz, padnx, padnz, padnzx, rr);

		wit=0;
		/* forward propagation */
		for(it=0; it<nt; it++){
			if(verb) sf_warning("Forward propagation is=%d; it=%d;", is+1, it);

			/* save wavefield */
			if(it%acpar->interval==0){
				for(ix=0; ix<nx; ix++)
					for(iz=0; iz<nz; iz++)
						wave[wit][ix][iz]=p1[ix+nb][iz+nb];
				wit++;
			}

			/* load source */
			for(ix=0; ix<padnx; ix++){
				for(iz=0; iz<padnz; iz++){
					p1[ix][iz] += rr[ix*padnz+iz]*array->ww[it];
				}
			}

			/* laplacian operator */
			laplace(p1, term, padnx, padnz, dx2, dz2);
			
			/* update */
			for(ix=4; ix<padnx-4; ix++){
				for(iz=4; iz<padnz-4; iz++){
					r2[ix][iz]=
						(-tau[ix][iz]/taus[ix][iz]*term[ix][iz]
						 + (1./dt-0.5/taus[ix][iz])*r1[ix][iz])
						/(1./dt+0.5/taus[ix][iz]);
					term[ix][iz]=term[ix][iz]*(1.+tau[ix][iz])+(r2[ix][iz]+r1[ix][iz])*0.5;
					p2[ix][iz]=2*p1[ix][iz]-p0[ix][iz]+vv[ix][iz]*vv[ix][iz]*dt2*term[ix][iz];
				}
			}
			
			/* swap wavefield pointer of different time steps */
			tmparray=p0; p0=p1; p1=p2; p2=tmparray;
			tmparray=r1; r1=r2; r2=tmparray;

			/* boundary condition */
			apply_sponge(p0, acpar->bc, padnx, padnz, nb);
			apply_sponge(p1, acpar->bc, padnx, padnz, nb);
			apply_sponge(r1, acpar->bc, padnx, padnz, nb);
		} // end of time loop

		/* check */
		if(wit != wnt) sf_error("Incorrect number of wavefield snapshots");
		/* read data */
		sf_seek(Fdat, is*nr*nt*sizeof(float), SEEK_SET);
		sf_floatread(dd[0], nr*nt, Fdat);
		/* initialization */
		memset(p0[0], 0., padnzx*sizeof(float));
		memset(p1[0], 0., padnzx*sizeof(float));
		memset(p2[0], 0., padnzx*sizeof(float));
		memset(r1[0], 0., padnzx*sizeof(float));
		memset(r2[0], 0., padnzx*sizeof(float));
		memset(term[0], 0., padnzx*sizeof(float));
		
		/* backward propagation */
		for(it=nt-1; it>=0; it--){
			if(verb) sf_warning("Backward propagation is=%d; it=%d;", is+1, it);

			/* load data */
			for(ir=0; ir<acpar->nr2[is]; ir++){
				rx=acpar->r0_v[is]+ir*acpar->dr_v;
				p1[rx][rz]=dd[acpar->r02[is]+ir][it];
			}

			/* laplacian operator */
			laplace(p1, term, padnx, padnz, dx2, dz2);
			
			/* update */
			for(ix=4; ix<padnx-4; ix++){
				for(iz=4; iz<padnz-4; iz++){
					r2[ix][iz]=
						(-tau[ix][iz]/taus[ix][iz]*term[ix][iz]
						 + (1./dt-0.5/taus[ix][iz])*r1[ix][iz])
						/(1./dt+0.5/taus[ix][iz]);
					term[ix][iz]=term[ix][iz]*(1.+tau[ix][iz])+(r2[ix][iz]+r1[ix][iz])*0.5;
					p2[ix][iz]=2*p1[ix][iz]-p0[ix][iz]+vv[ix][iz]*vv[ix][iz]*dt2*term[ix][iz];
				}
			}

			/* calculate image */
			if(it%acpar->interval==0){
				for(ix=0; ix<nx; ix++)
					for(iz=0; iz<nz; iz++)
						mm[ix][iz] += wave[wit-1][ix][iz]*p2[ix+nb][iz+nb];
				wit--;
			}
			
			/* swap wavefield pointer of different time steps */
			tmparray=p0; p0=p1; p1=p2; p2=tmparray;
			tmparray=r1; r1=r2; r2=tmparray;

			/* boundary condition */
			apply_sponge(p0, acpar->bc, padnx, padnz, nb);
			apply_sponge(p1, acpar->bc, padnx, padnz, nb);
			apply_sponge(r1, acpar->bc, padnx, padnz, nb);
		} // end of time loop
	}// end of shot loop
	MPI_Barrier(comm);

	if(mpipar->cpuid==0){
		sendbuf=MPI_IN_PLACE;
		recvbuf=mm[0];
	}else{
		sendbuf=mm[0];
		recvbuf=NULL;
	}
	MPI_Reduce(sendbuf, recvbuf, nzx, MPI_FLOAT, MPI_SUM, 0, comm);

	if(mpipar->cpuid==0) sf_floatwrite(mm[0], nzx, Fimg);
	MPI_Barrier(comm);
}