Пример #1
0
int main (int argc, char **argv)
{
    bool verb;                /* verbosity flag */
    bool adj;
    int nt;                   /* number of time samples */
    int nx;                   /* number of offsets */ 
    int nf1, nf2, nf;         /* number of filter size */
    int n3, i3;
    float *x, *y, *filt;      /* input and output */
    sf_file in, out, fil;

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

    if (!sf_getbool("adj",&adj)) adj=true;
    /* if y, perform adjoint operation */

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

    /* read input file parameters */
    if (!sf_histint(in,"n1",&nt)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&nx)) sf_error("No n2= in input");
    n3 = sf_leftsize(in,2);

    if (!sf_histint(fil,"n1",&nf1)) sf_error("No nf1= in filt");
    if (!sf_histint(fil,"n2",&nf2)) sf_error("No nf2= in filt");
    if (!sf_histint(fil,"n3",&nf)) sf_error("No nf3= in filt");

    if (nt != nf1 || nx != nf2 ) sf_error("Nee n1==nf1 && n2==nf2");

    x = sf_floatalloc (nt*nx);
    y = sf_floatalloc (nt*nx);
    filt = sf_floatalloc (nt*nx*nf);

    for (i3=0; i3 < n3; i3++) {
	if(verb) sf_warning("i=%d of %d",i3+1,n3);

	sf_floatread(filt,nt*nx*nf,fil);

	matching_init(filt,nt,nx,nf);

	if (adj) {
	    sf_floatread(y,nt*nx,in);
	} else { /* modeling */
	    sf_floatread(x,nt*nx,in);
	}
    
	pmatching_lop (adj, false, nt*nx, nt*nx, x, y);
	
	if (adj) {
	    sf_floatwrite(x,nt*nx,out);
	} else { /* modeling */
	    sf_floatwrite(y,nt*nx,out);
	}
    }
    
    exit (0);
}
Пример #2
0
int main(int argc, char* argv[])
{
    bool adj, verb;
    int n1, n2, n12, n3, i3, order, ns;
    float *input, *smooth, **slope, eps;
    sf_file in, out, dip;

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

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

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

    if (!sf_getint("ns",&ns)) ns=0;
    /* smoothing radius */

    if (!sf_getbool("adj",&adj)) adj=false;
    /* adjoint flag */

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

    if (!sf_getint("order",&order)) order=1;
    /* accuracy order */	

    input = sf_floatalloc(n12);
    smooth = sf_floatalloc(n12);
    slope = sf_floatalloc2(n1,n2);

    pwsmooth_init(ns, n1, n2, order, eps);

    for (i3=0; i3 < n3; i3++) {
	if (verb) sf_warning("slice %d of %d;",i3+1,n3);

	sf_floatread(input,n12,in);
	sf_floatread(slope[0],n12,dip);

	pwsmooth_set(slope);

	if (adj) {
	    pwsmooth_lop(true,false,n12,n12,smooth,input);
	} else {
	    pwsmooth_lop(false,false,n12,n12,input,smooth);
	}

	sf_floatwrite(smooth,n12,out);
    }
    if (verb) sf_warning(".");

    exit(0);
}
Пример #3
0
int main(int argc, char* argv[])
{
    int nx, nf, i2, n2, lag, niter;
    bool single, verb;
    float eps;
    sf_complex *xx, *yy, *ff;
    sf_file in, out, filt;

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

    if (SF_COMPLEX != sf_gettype(in) ||
	SF_COMPLEX != sf_gettype(filt)) sf_error("Need float input");
    if (!sf_histint(in,"n1",&nx)) sf_error("No n1= in input");
    n2 = sf_leftsize(in,1);

    if (!sf_histint(filt,"n1",&nf)) sf_error("No n1= in filtin");

    xx = sf_complexalloc(nx);
    ff = sf_complexalloc(nf);

    if (!sf_getbool("single",&single)) single=true;
    /* single channel or multichannel */

    if (!sf_getint("lag",&lag)) lag=1;
    /* lag for internal convolution */

    yy = sf_complexalloc(nx);

    if (!sf_getfloat("eps",&eps)) eps=1.0f;
    /* regularizion parameter */

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

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

    if (!single) sf_complexread (ff,nf,filt);

    for (i2=0; i2 < n2; i2++) {
        if (single) sf_complexread (ff,nf,filt);
        sf_complexread (xx,nx,in);

	cicai1_init(nf,ff,lag);

	sf_csolver_reg(sf_ccopy_lop,sf_ccgstep,cicai1_lop,nx,nx,nx,yy,xx,niter,eps,"verb",verb,"end");
	sf_ccgstep_close();
	
	sf_complexwrite (yy,nx,out);
    }
    
    exit(0);
}
Пример #4
0
int
main (int argc, char *argv[])
{
    int i;
    int ndims;
    off_t nlarge[SF_MAX_DIM];
    int n[SF_MAX_DIM];
    bool parform, large;
    sf_file in=NULL;

    sf_init(argc,argv);

    in = sf_input("in");

    if (!sf_getbool("large", &large))
        large = false;
    /* if y, file with large dimensions. */

    if (large) {
	ndims = sf_largefiledims(in, nlarge);
    } else {
	ndims = sf_filedims(in, n);
    }

    if (!sf_getbool("parform", &parform))
        parform = true;
    /* If y, print out parameter=value. If n, print out value. */

    if (parform) printf( "ndims=" );

    printf( "%d", ndims );

    if (parform)
        printf( "\nn=" );
    else
        printf( ":" );

    for (i=0; i<ndims; i++) {
	if (large) {
#if defined(__cplusplus) || defined(c_plusplus)
	    printf( "%ld", (long) n[i] );
#else
	    printf( "%lld", (long long) n[i] );
#endif
	} else {
	    printf( "%d", n[i] );
	}
        if (i<ndims-1) printf(",");
    }

    printf( "\n" );


    exit(0);
}
Пример #5
0
void vp_coord_init (bool transp1, bool yreverse1)
{
    float crowd, marg;
    bool set;

    if (!sf_getfloat ("screenratio",&screenratio))
	screenratio = VP_SCREEN_RATIO;
    if (!sf_getfloat ("screenht",&screenht))
	screenht = VP_STANDARD_HEIGHT;
    if (!sf_getfloat ("screenwd",&screenwd))
	screenwd = screenht / screenratio;

    if (!sf_getfloat ("crowd",&crowd))   crowd  = 0.75;

    if (!sf_getfloat ("xinch",&inch1)) {
	if (!sf_getfloat ("crowd1",&inch1)) inch1 = crowd;
	inch1 *= screenwd;
    }
    if (!sf_getfloat ("yinch",&inch2)) {
	if (!sf_getfloat ("crowd2",&inch2)) inch2 = crowd;
	inch2 *= screenht;
    }

    set = sf_getfloat ("xll",&xll);
    if (!sf_getfloat ("xur",&xur)) {
	if (set) {
	    xur = xll + inch1;
	} else {
	    marg =  screenwd - inch1;
	    xll = marg * 2./3.;
	    xur = screenwd - marg * 1./3.;
	}
    } else if (!set) {
	xll = xur - inch1;
    }
	
    set = sf_getfloat ("yll",&yll);
    if (!sf_getfloat ("yur",&yur)) {
	if (set) {
	    yur = yll + inch2;
	} else {
	    marg =  screenht - inch1;
	    yll = marg * 1./2.;
	    yur = screenht - marg * 1./2.;
	}
    } else if (!set) {
	yll = yur - inch2;
    }

    if (!sf_getbool ("transp",&transp)) transp = transp1;
    if (!sf_getbool ("xreverse",&xreverse)) xreverse = false;
    if (!sf_getbool ("yreverse",&yreverse)) yreverse = yreverse1;
    if (!sf_getbool ("labelrot",&labelrot)) labelrot = false;
}
Пример #6
0
int
main (int argc, char *argv[])
{
  const bool su = true;
  bool verb, xdr;
  char *filename, *trace;
  int format=0, ns, ntr, itrace[SF_NKEYS];
  off_t pos, nsegy;
  float dt;
  FILE *file;
  extern int fseeko (FILE * stream, off_t offset, int whence);

/**** Start common code with segy2rsf (16 lines) ****/
  sf_init (argc, argv);

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

  if (!sf_getbool ("endian", &xdr))
    xdr = true;
  /* Whether to automatically estimate endianness or not */
  if (xdr)
    endian ();

  if (NULL == (filename = sf_getstring ("tape")))	/* input data */
    sf_error ("Need to specify tape=");

  if (NULL == (file = fopen (filename, "rb")))
    sf_error ("Cannot open \"%s\" for reading:", filename);

  pos = readfilesize (file);
/**** End common code with segy2rsf ****/

  /* Figure out ns and ntr */

  trace = sf_charalloc (SF_HDRBYTES);
  if (SF_HDRBYTES != fread (trace, 1, SF_HDRBYTES, file))
    sf_error ("Error reading first trace header");
  fseeko (file, 0, SEEK_SET);

  segy2head (trace, itrace, SF_NKEYS);
  ns = itrace[segykey ("ns")];
  dt = itrace[segykey ("dt")] / 1000000.;
  free (trace);

  nsegy = SF_HDRBYTES + ns * 4;
  ntr = pos / nsegy;

  su_or_segy_to_rsf (verb, su, ntr, format, ns, itrace, nsegy, file, dt);

  exit (0);
}
Пример #7
0
int main(int argc, char* argv[])
{
    int nx, nf, ny, i2, n2, lag;
    bool each, trans;
    float *xx, *yy, *ff;
    sf_file in, out, filt;

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

    if (SF_FLOAT != sf_gettype(in) ||
	SF_FLOAT != sf_gettype(filt)) sf_error("Need float input");
    if (!sf_histint(in,"n1",&nx)) sf_error("No n1= in input");
    n2 = sf_leftsize(in,1);

    if (!sf_histint(filt,"n1",&nf)) sf_error("No n1= in filtin");

    xx = sf_floatalloc(nx);
    ff = sf_floatalloc(nf);

    if (!sf_getbool("trans",&trans)) trans=false;
    /* if y, transient convolution; if n, internal */
    if (!sf_getbool("each",&each)) each=false;
    /* if y, new filter for each trace */
    if (!sf_getint("lag",&lag)) lag=1;
    /* lag for internal convolution */

    ny = trans? (nx+nf-1)*(nx+nf-1): nx;
    yy = sf_floatalloc(ny);
    if (trans){
        sf_putint(out,"n1",nx+nf-1);
        sf_putint(out,"n2",nx+nf-1);
    }

    if (!each) sf_floatread (ff,nf,filt);

    for (i2=0; i2 < n2; i2++) {
        if (each) sf_floatread (ff,nf,filt);
        sf_floatread (xx,nx,in);
        if (trans) {
	    tcai1_ns_init(nf,ff);
	    tcai1_ns_lop (false, false,nx,ny,xx,yy);
	} else {
	    icai1_init(nf,ff,lag);
	    icai1_lop (false, false,nx,ny,xx,yy);
	}
	sf_floatwrite (yy,ny,out);
    }

    exit(0);
}
Пример #8
0
int main(int argc, char* argv[])
{
    int n1, n2, n12, nv, inv;
    bool adj, add;
    float *modl, *data, o1,d1,o2,d2, vel,v0;
    sf_file in, out;

    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,"n2",&n2)) sf_error("No n2= in input");
    n12 = n1*n2;

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

    if (!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"d2",&d2)) sf_error("No d2= in input");

    if (!sf_getfloat("vel",&vel)) vel=0.75;
    /* final velocity */
    if (!sf_getfloat("v0",&v0)) v0=0.;
    /* starting velocity */
    if (!sf_getint("nv",&nv)) nv=n1;
    /* number of steps */

    if (!sf_getbool("adj",&adj)) adj=false;
    /* adjoint flag */
    if (!sf_getbool("add",&add)) add=false;
    /* addition flag */

    if (!sf_getint("inv",&inv)) inv=0;
    /* amplitude type */

    modl = sf_floatalloc(n12);
    data = sf_floatalloc(n12);

    sf_floatread (adj? data: modl,n12,in);

    velcon_init (inv,vel,v0,o1,n1,n2,nv,d1,d2);
    velcon_lop (adj, add, n12, n12, modl, data);

    sf_floatwrite (adj? modl: data,n12,out);

    exit(0);
}
Пример #9
0
int main(int argc, char* argv[])
{
    int n1, n2, i2;
    bool adj;
    float *pp, *qq;
    sf_file in=NULL, out=NULL;

    sf_init(argc,argv);

    in = sf_input("in");
    out = sf_output("out");

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

    pp = sf_floatalloc(n1);
    qq = sf_floatalloc(n1);

    if (!sf_getbool("adj",&adj)) adj=false;
    /* if y, do adjoint integration */

    for (i2=0; i2 < n2; i2++) {
	sf_floatread(pp,n1,in);
	if (adj) {
	    sf_causint_lop (true,false,n1,n1,qq,pp);
	} else {
	    sf_causint_lop (false,false,n1,n1,pp,qq);
	}
	sf_floatwrite(qq,n1,out);
    }


    exit(0);
}
Пример #10
0
int main(int argc, char* argv[])
{
    int n1,n2,n12;
    bool adj;
    float d1,d2,o1,o2,v, *model=NULL, *dat=NULL;
    sf_file in=NULL, out=NULL;

    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_histfloat(in,"d1",&d1)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"o1",&o1)) o1=0.;

    if (!sf_getbool("adj",&adj)) adj=false;
    /* adjoint flag */

    if (adj) {
	if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
	if (!sf_histfloat(in,"d2",&d2)) sf_error("No d2= in input");
	if (!sf_histfloat(in,"o2",&o2)) sf_error("No o2= in input");

	sf_putint(out,"n2",1);
    } else {
	if (!sf_getint("n2",&n2)) n2=20;
	/* number of offsets (if inv=n) */
	if (!sf_getfloat("d2",&d2)) d2=200.;
	/* offset sampling (if inv=n) */
	if (!sf_getfloat("o2",&o2)) o2=0.;
	/* offset origin (if inv=n) */

	sf_putint(out,"n2",n2);
	sf_putfloat(out,"d2",d2);
	sf_putfloat(out,"o2",o2);
    }
    n12 = n1*n2;

    if (!sf_getfloat ("v",&v)) v=1000.;
    /* velocity */

    dat = sf_floatalloc(n12);
    model =  sf_floatalloc(n1);

    imospray_init (1./v, o2,d2, o1,d1, n1,n2);

    if (adj) {
	sf_floatread(dat,n12,in);
	imospray_lop (true,false,n1,n12,model,dat);
	sf_floatwrite(model,n1,out);
    } else {	
	sf_floatread(model,n1,in);
	imospray_lop (false,false,n1,n12,model,dat);
	sf_floatwrite(dat,n12,out);
    }

    exit(0);
}
Пример #11
0
int main(int argc, char* argv[])
{
    bool adj, inv;
    int nn,n1,n2,i1,i2;
    float rho, *pp=NULL;
    sf_file in=NULL, out=NULL;

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

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

    if (!sf_getbool("adj",&adj)) adj=false;
    /* If y, apply adjoint */
    if (!sf_getbool("inv",&inv)) inv=false;
    /* If y, do differentiation instead of integration */
    if (!sf_getfloat("rho",&rho)) rho = 1.-1./n1;
    /* Leaky integration constant */

    if (inv) {
	sf_warning("%s half-order differentiation",adj? "anticausal":"causal");
    } else {
	sf_warning("%s half-order integration",adj? "anticausal":"causal");
    }

    nn = 2*kiss_fft_next_fast_size((n1+1)/2);
    pp = sf_floatalloc(nn);

    sf_halfint_init (inv, nn, rho);

    for (i2=0; i2 < n2; i2++) {
	sf_floatread (pp,n1,in);
	for (i1=n1; i1 < nn; i1++) {
	    pp[i1]=0.;
	}
	sf_halfint (adj, pp);
	sf_floatwrite (pp,n1,out);
    }

    exit(0);
}
Пример #12
0
int main(int argc, char* argv[])
{
    int n1, n2, i2;
    float *pp, *qq;
    bool square, adj;
    sf_file in, out;

    sf_init(argc,argv);

    in = sf_input("in");
    out = sf_output("out");

    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    n2 = sf_leftsize(in,1);

    if (!sf_getbool("square",&square)) square=false;
    /* if y, use gradient squared */

    if (!sf_getbool("adj",&adj)) adj=false;
    /* adjoint flag */

    pp = sf_floatalloc(n1);
    qq = sf_floatalloc(n1);

    for (i2=0; i2 < n2; i2++) {
	sf_floatread(pp,n1,in);
	if (square) {
	    sf_grad2 (n1,pp,qq);
	} else if (adj) {
	    sf_igrad1_lop (true,false,n1,n1,qq,pp);
	} else {
	    sf_igrad1_lop (false,false,n1,n1,pp,qq);
	}
	sf_floatwrite(qq,n1,out);
    }

    exit(0);
}
Пример #13
0
int main(int argc, char* argv[])
{
    bool verb;
    int i, dim, n[SF_MAX_DIM], nd, rect[SF_MAX_DIM], niter;
    float *noi, *sig, *rat, eps;
    char key[6];
    sf_file fnoi, fsig, flow;

    sf_init(argc,argv);
    fnoi = sf_input("in");
    fsig = sf_input("sig"); /* input signal */

    if (SF_FLOAT != sf_gettype(fnoi) ||
	SF_FLOAT != sf_gettype(fsig)) sf_error("Need float input");

    flow = sf_output("out");

    dim = sf_filedims (fnoi,n);
    nd = 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 )*/ 
	nd *= n[i];
    }

    noi = sf_floatalloc(nd);
    sig = sf_floatalloc(nd);
    rat = sf_floatalloc(nd);    

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

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

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

    sf_divn_init(dim, nd, n, rect, niter, verb);

    sf_floatread(noi,nd,fnoi);
    sf_floatread(sig,nd,fsig);

    sf_divne (noi, sig, rat, eps);

    sf_floatwrite(rat,nd,flow);

    exit(0);
}
Пример #14
0
int main(int argc, char* argv[])
{
    bool verb;
    int j, k, n, n2, i3, n3, iter, niter;
    sf_complex **a=NULL, *e=NULL;
    float s2;
    sf_file mat=NULL, val=NULL;

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

    if (SF_COMPLEX != sf_gettype(mat)) sf_error("Need complex input");
    if (!sf_histint(mat,"n1",&n)) sf_error("No n1= in input");
    if (!sf_histint(mat,"n2",&n2) || n2 != n) sf_error("Need n1=n2 in input");
    n3 = sf_leftsize(mat,2);

    sf_putint(val,"n2",1);

    if (!sf_getint("niter",&niter)) niter=10;
    if (!sf_getbool("verb",&verb)) verb=false;

    a = sf_complexalloc2(n,n);
    e = sf_complexalloc(n);
    jacobi2_init(n,verb);

    for (i3=0; i3 < n3; i3++) {
	sf_complexread(a[0],n*n,mat);
	
	for (iter=0; iter < niter; iter++) {
	    s2 = 0.;
	    for (j=0; j < n; j++) {
		for (k=0; k < n; k++) {
		    s2 += jacobi2(a,n,j,k);
		}
	    }
	    sf_warning("iter=%d s2=%g",iter+1,s2);
	}

	for (j=0; j < n; j++) {
	    e[j]=a[j][j];
	}
	
	sf_complexwrite(e,n, val);
    }

    exit(0);
}
Пример #15
0
int main (int argc, char* argv[]) 
{
    bool der;
    int i1, n1, i2, n2, irep, nrep;
    float *data, *data2, rect;
    sf_file in, out;

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

    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    n2 = sf_leftsize(in,1);

    if (!sf_getbool("der",&der)) der=false;
    /* compute derivative */

    if (!sf_getfloat("rect",&rect)) rect=1;
    /* smoothing radius */ 
    recgauss_init (n1,der,rect);

    data = sf_floatalloc (n1);
    data2 = der? sf_floatalloc (n1): NULL;

    if (!sf_getint("repeat",&nrep)) nrep=1;
    /* repeat filtering several times */

    for (i2=0; i2 < n2; i2++) {
	if (der) {
	    sf_floatread(data2,n1,in);
	    data[0] = data2[1]-data2[0];
	    for (i1=1; i1 < n1-1; i1++) {
		data[i1] = 0.5*(data2[i1+1]-data2[i1-1]);
	    }
	    data[n1-1] = data2[n1-1]-data2[n1-2];
	} else {
	    sf_floatread(data,n1,in);
	}

	for (irep=0; irep < nrep; irep++) {
	    recgauss (data);
	}
	sf_floatwrite(data,n1,out);
    }    

    exit (0);
}
Пример #16
0
int main (int argc, char* argv[])
{

    int i,n1,n2,n22,n;
    float *A, *x, *d;
    bool verb;
    sf_file in, rhs, out;

    sf_init (argc,argv);
    in = sf_input("in");
    rhs= sf_input("rhs");
    out = sf_output("out");
    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float");

    if(!sf_histint(in,"n1",&n1)) sf_error("No n1 in input matrix!");
    if(!sf_histint(in,"n2",&n2)) sf_error("No n2 in input matrix!");
    if(!sf_histint(rhs,"n2",&n22))sf_error("No n2 in input vector!");
    if(!sf_getbool("verb",&verb)) verb=false;
    if((n1!=n2) || (n1!=n22)) sf_error("Dimension mistake!");
    n=n1;

    x = sf_floatalloc(n);
    A=sf_floatalloc(n*n);
    d=sf_floatalloc(n);

    sf_floatread(A,n*n,in);
    sf_floatread(d,n,rhs);

    trid_init(n, A);
    trid_solve(d, x);
    
    if(verb)
    {
	for(i=0;i<n;i++)
	sf_warning("x[%d]=%f",i+1,x[i]);
    }
    
   sf_putint(out,"n2",n);
   sf_putint(out,"n1",1);
   sf_floatwrite(x,n,out);

   trid_close();
   free(A);
   free(d);
   free(x);
   exit(0);
}
Пример #17
0
int main(int argc, char* argv[])
{
    bool single;
    int n1, i2, n2, nf, nc;
    sf_complex *trace, *a;
    sf_file in, out;

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

    if (SF_COMPLEX != sf_gettype(in)) sf_error("Need complex input");
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");

    if (!sf_getbool("single",&single)) single=true;
    /* single channel or multichannel */

    if (single) {
	nc = 1;
	n2 = sf_leftsize(in,1);
    } else {
	if (!sf_histint(in,"n2",&nc)) nc=1;
	n2 = sf_leftsize(in,2);
	sf_putint(out,"n2",1);
    }

    if (!sf_getint("nf",&nf)) sf_error("Need nf=");
    /* filter length */
    sf_putint(out,"n1",nf);

    cburg_init(n1,nc,nf);

    trace = sf_complexalloc(n1*nc);
    a = sf_complexalloc(nf);

    for (i2=0; i2 < n2; i2++) {
	sf_complexread(trace,n1*nc,in);

	cburg_apply(trace,a);

	sf_complexwrite(a,nf,out);
    }

    exit(0);
}
Пример #18
0
void vp_title_init(sf_file file)
{
    char *where;

    if (NULL != (where = sf_getstring("wheretitle"))) {
	verttitle = (*where == 'l' || *where == 'r');
	wheretitle = (*where == 'l' || *where == 't');
    } else {
	verttitle = false;
	wheretitle = true;
    }
    if (!sf_getbool ("wanttitle",&wanttitle)) wanttitle=true;
    if (!sf_getint ("titlefat",&titlefat)) titlefat=0;
    if (!sf_getfloat ("titlesz",&titlesz)) titlesz=10.;
    if (NULL == (title = sf_getstring("title")) &&
	NULL == (title = sf_histstring(file,"title")) &&
	NULL == (title = sf_histstring(file,"in"))) 
	title = blank;
}
Пример #19
0
int main (int argc, char* argv[])
{
    bool scale;
    int n1,n2, i1,i2, n;
    float d1, *dat, *der;
    sf_file in=NULL, out=NULL;

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

    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    n2 = sf_leftsize(in,1);

    dat = sf_floatalloc(n1);
    der = sf_floatalloc(n1);

    if (!sf_getint("order",&n)) n=6;
    /* filter order */

    if (!sf_getbool("scale",&scale) || 
	!sf_histfloat(in,"d1",&d1)) scale=false;
    /*(scale=n if scale by 1/dx )*/

    sf_deriv_init(n1, n, 0.);

    for (i2=0; i2 < n2; i2++) {
	sf_floatread(dat,n1,in);
	sf_deriv(dat,der);

	if (scale) {
	    for (i1=0; i1 < n1; i1++) {
		der[i1] /= d1;
	    }
	}

	sf_floatwrite(der,n1,out);
    }


    exit(0);
}
Пример #20
0
int main(int argc, char* argv[])
{
    bool verb;

    sf_file Fu,Fw; /* I/O files */
    sf_axis a1,a2,a3; /* cube axes */

    float ***uu=NULL, ***ww=NULL;
    int *ii,ik;

    int nh1,nh2,nh3;
    int nb1,nb2,nb3;

    int ih1,ih2,ih3;
    int ib1,ib2,ib3;
    int  n3;
    int  i3;
    int  j1, j2, j3;
    int  k1, k2, k3;

    int ompchunk;

    int lo1,hi1;
    int lo2,hi2;
    int lo3,hi3;

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

    if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1;  /* OpenMP data chunk size */
    if(! sf_getbool("verb",&verb)) verb=false;         /* verbosity flag */

    Fu = sf_input ("in" ); /*  input field */
    Fw = sf_output("out"); /* wigner distribution */

    /* read axes */
    a1=sf_iaxa(Fu,1); sf_setlabel(a1,"a1"); if(verb) sf_raxa(a1);
    a2=sf_iaxa(Fu,2); sf_setlabel(a2,"a2"); if(verb) sf_raxa(a2);
    a3=sf_iaxa(Fu,3); sf_setlabel(a3,"a3"); if(verb) sf_raxa(a3);
    n3 = sf_n(a3);

    if(! sf_getint("nh1",&nh1)) nh1=0;
    if(! sf_getint("nh2",&nh2)) nh2=0;
    if(! sf_getint("nh3",&nh3)) nh3=0;
    if(n3<=1) nh3=0;
    if(verb) sf_warning("nh1=%d nh2=%d nh3=%d",2*nh1+1,2*nh2+1,2*nh3+1);

    nb1 = sf_n(a1);
    nb2 = sf_n(a2);
    nb3=2*nh3+1;

    uu=sf_floatalloc3(nb1,nb2,nb3);
    ww=sf_floatalloc3(nb1,nb2,nb3); 

    ii = sf_intalloc(nb3);
    for(ib3=0;ib3<nb3;ib3++) {
	ii[ib3]=ib3;
    }

    /*------------------------------------------------------------*/
    /* low end on axis 3 */
    /*------------------------------------------------------------*/
    for(         ib3=0; ib3<nb3; ib3++) {
	for(     ib2=0; ib2<nb2; ib2++) {
	    for( ib1=0; ib1<nb1; ib1++) {
		ww[ib3][ib2][ib1] = 0.;
	    }
	}
    }

    sf_floatread(uu[0][0],nb1*nb2*nb3,Fu);

    for(        ih3=-nh3; ih3<nh3+1; ih3++) { lo3=SF_ABS(ih3); hi3=nb3-lo3;
	for(    ih2=-nh2; ih2<nh2+1; ih2++) { lo2=SF_ABS(ih2); hi2=nb2-lo2;
	    for(ih1=-nh1; ih1<nh1+1; ih1++) { lo1=SF_ABS(ih1); hi1=nb1-lo1;
		
		for(        ib3=lo3; ib3<nh3+1;ib3++) { j3=ii[ib3-ih3]; k3=ii[ib3+ih3];
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) \
    private(ib1,ib2,j2,j1,k2,k1) \
    shared(ib3,j3,k3,ih2,ih1,lo2,hi2,lo1,hi1,uu,ww)
#endif
		    for(    ib2=lo2; ib2<hi2;  ib2++) { j2=ib2-ih2; k2=ib2+ih2;
			for(ib1=lo1; ib1<hi1;  ib1++) { j1=ib1-ih1; k1=ib1+ih1;
			    
			    ww[ib3][ib2][ib1] += uu[j3][j2][j1] 
				*                uu[k3][k2][k1];
			} /* nb1  */
		    }     /* nb2  */
		}         /* nb3  */

	    } /* nh1 */
	}     /* nh2 */
    }         /* nh3 */
    
    for(ih3=0;ih3<nh3+1;ih3++) {
	sf_floatwrite(ww[ih3][0],nb1*nb2,Fw);
    }

    /*------------------------------------------------------------*/
    /* loop on axis 3 */
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr," n3\n");
    if(verb) fprintf(stderr,"%5d\n",n3-1);
    for(i3=nh3+1;i3<n3-nh3;i3++) {
	if(verb) fprintf(stderr,"%5d",i3);

	/* zero WDF */
	ib3=nh3;
	for(     ib2=0; ib2<nb2; ib2++) {
	    for( ib1=0; ib1<nb1; ib1++) {
		ww[ib3][ib2][ib1] = 0.;
	    }
	}

	/* circulate index to slices */
	ik = ii[0];
	for(ib3=0;ib3<nb3-1;ib3++) {
	    ii[ib3]=ii[ib3+1];
	}
	ii[nb3-1]=ik;

	/* read new slice */
	sf_floatread(uu[ ii[nb3-1] ][0],nb1*nb2,Fu);

	for(        ih3=-nh3; ih3<nh3+1; ih3++) { 
	    for(    ih2=-nh2; ih2<nh2+1; ih2++) { lo2=SF_ABS(ih2); hi2=nb2-lo2;
		for(ih1=-nh1; ih1<nh1+1; ih1++) { lo1=SF_ABS(ih1); hi1=nb1-lo1;
		    
		    ib3=nh3; j3=ii[ib3-ih3]; k3=ii[ib3+ih3];
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) \
    private(ib1,ib2,j2,j1,k2,k1) \
    shared(ib3,j3,k3,ih2,ih1,lo2,hi2,lo1,hi1,uu,ww)
#endif
		    for(    ib2=lo2; ib2<hi2; ib2++) { j2=ib2-ih2; k2=ib2+ih2;
			for(ib1=lo1; ib1<hi1; ib1++) { j1=ib1-ih1; k1=ib1+ih1;
			    
			    ww[ib3][ib2][ib1] += uu[j3][j2][j1] 
				*                uu[k3][k2][k1];
			} /* nb1  */
		    }     /* nb2  */
		    
		} /* nh1 */
	    }     /* nh2 */
	}         /* nh3 */
	
	sf_floatwrite(ww[nh3][0],nb1*nb2,Fw);

	if(verb) fprintf(stderr,"\b\b\b\b\b");
    }

    /*------------------------------------------------------------*/
    /* high-end on axis 3*/
    /*------------------------------------------------------------*/
    for(        ih3=-nh3; ih3<nh3+1; ih3++) { lo3=SF_ABS(ih3); hi3=nb3-lo3;
	for(    ih2=-nh2; ih2<nh2+1; ih2++) { lo2=SF_ABS(ih2); hi2=nb2-lo2;
	    for(ih1=-nh1; ih1<nh1+1; ih1++) { lo1=SF_ABS(ih1); hi1=nb1-lo1;
		
		for(        ib3=nh3+1; ib3<hi3; ib3++) { j3=ii[ib3-ih3]; k3=ii[ib3+ih3];
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) \
    private(ib1,ib2,j2,j1,k2,k1) \
    shared(ib3,j3,k3,ih2,ih1,lo2,hi2,lo1,hi1,uu,ww)
#endif
		    for(    ib2=lo2; ib2<hi2; ib2++) { j2=ib2-ih2; k2=ib2+ih2;
			for(ib1=lo1; ib1<hi1; ib1++) { j1=ib1-ih1; k1=ib1+ih1;
			    
			    ww[ib3][ib2][ib1] += uu[j3][j2][j1] 
				*                uu[k3][k2][k1];
			} /* nb1  */
		    }     /* nb2  */
		}         /* nb3  */
		
	    } /* nh1 */
	}     /* nh2 */
    }         /* nh3 */
    
    for(ih3=nh3+1;ih3<2*nh3+1;ih3++) {
	sf_floatwrite(ww[ih3][0],nb1*nb2,Fw);
    }

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

    exit (0);
}
Пример #21
0
int main(int argc, char* argv[])
{
    int nz,nx, iz,ix, order;
    bool vel;
    float x1,x2,z1,z2,v1,v2,x0,dx,z0,dz,fx,fz;
    float **slow, *time, g1[2],g2[2],p1[2],p2[2];
    sf_eno2 cvel;
    sf_file in, out;

    sf_init(argc,argv);
    in = sf_input("in");
    /* velocity or slowness */

    out = sf_output("out");
    /* traveltime */

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

    if (!sf_histfloat(in,"d1",&dz)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"d2",&dx)) sf_error("No d2= in input");
  
    if (!sf_histfloat(in,"o1",&z0)) sf_error("No o1= in input");
    if (!sf_histfloat(in,"o2",&x0)) sf_error("No o2= in input");
    
    if(!sf_getbool("vel",&vel)) vel=true;
    /* y, input is velocity; n, slowness */

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

    if (!sf_getfloat("yshot",&x1)) x1=x0 + 0.5*(nx-1)*dx;
    if (!sf_getfloat("zshot",&z1)) z1=z0; 

    /* read velocity or slowness */
    slow  = sf_floatalloc2(nz,nx);
    sf_floatread(slow[0],nz*nx,in);    

    /* convert to slowness squared */
    for(ix = 0; ix < nx; ix++){
	for (iz = 0; iz < nz; iz++) {
	    v1 = slow[ix][iz];
	    v1 *= v1;
	    slow[ix][iz] = vel? 1/v1:v1;
	}
    }

    cvel = sf_eno2_init (order, nz, nx);
    sf_eno2_set (cvel, slow);

    time = sf_floatalloc(nz);

    fx = (x1-x0)/dx;
    ix = floorf(fx);
    fx -= ix;
    
    fz = (z1-z0)/dz;
    iz = floorf(fz);
    fz -= iz;

    sf_eno2_apply(cvel,iz,ix,fz,fx,&v1,g1,BOTH);
    g1[1] /= dx;
    g1[0] /= dz;
    
    for(ix = 0; ix < nx; ix++){
	x2 = x0+ix*dx;
	for (iz = 0; iz < nz; iz++) {
	    z2 = z0+iz*dz;

	    sf_eno2_apply(cvel,iz,ix,0.,0.,&v2,g2,BOTH);
	    g2[1] /= dx;
	    g2[0] /= dz;

	    time[iz] = analytical(x1,z1,v1,g1,p1,
				  x2,z2,v2,g2,p2);
	}
	sf_floatwrite(time,nz,out);
    }

    exit(0);
}
Пример #22
0
int main (int argc, char *argv[])
{
    bool verb;
    int n1,n2,n3, n12, n23, ref2, ref3, i2,i3,i1, ud, lr, order;
    float eps, ***dat, ***p, ***q, **p2, **q2, *trace;
    sf_file dip, out, seed, cost;

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

    if (!sf_histint(dip,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(dip,"n2",&n2)) sf_error("No n2= in input");
    if (!sf_histint(dip,"n3",&n3)) sf_error("No n3= in input");
    n23 = n2*n3;
    n12 = n1*n23;

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

    if (!sf_getint("ref2",&ref2)) ref2=0;
    if (!sf_getint("ref3",&ref3)) ref3=0;
    /* reference trace */

    sf_putint(out,"n4",1);

    p = sf_floatalloc3(n1,n2,n3);
    q = sf_floatalloc3(n1,n2,n3);
    dat = sf_floatalloc3(n1,n2,n3);

    sf_floatread(p[0][0],n12,dip);
    sf_floatread(q[0][0],n12,dip);

    p2 = sf_floatalloc2(n2,n3);
    q2 = sf_floatalloc2(n2,n3);

    sf_floatread(p2[0],n23,cost);
    sf_floatread(q2[0],n23,cost);

    dijkstra_init(n2,n3,p2,q2);
    dijkstra_source(ref2,ref3);
    sf_floatread(dat[ref3][ref2],n1,seed);
    if (verb) sf_warning("%d %d",ref2,ref3);

    if (!sf_getint("order",&order)) order=1;
    /* accuracy order */

    predict_init(n1,n2, eps*eps, order, 1, false);
    
    while (dijskstra_step(&i2,&i3,&ud,&lr)) {
	if (verb) sf_warning("%d %d",i2,i3);

	trace = dat[i3][i2];
	for (i1=0; i1 < n1; i1++) {
	    trace[i1] = dat[i3-lr][i2-ud][i1];
	} 
	if (ud > 0) {
	    predict_step(false,true,trace,p[i3][i2-ud]);
	} else if (ud < 0) {
	    predict_step(false,false,trace,p[i3][i2]);
	}

	if (lr > 0) {
	    predict_step(false,true,trace,q[i3-lr][i2]);
	} else if (lr < 0) {
	    predict_step(false,false,trace,q[i3][i2]);
	}
    }
	
    sf_floatwrite(dat[0][0],n12,out);

    exit (0);
}
Пример #23
0
int main(int argc, char* argv[])
{
    bool verb,isreversed;

    sf_file Fs,Fr,Fi;    /* I/O files */
    sf_axis az,ax,at,aa; /* cube axes */
    int     iz,ix,it;
    int     nz,nx,nt;
    off_t iseek;

    float **us=NULL,**ur=NULL,**ii=NULL;

    float scale;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);
    
    /* OMP parameters */
#ifdef _OPENMP
    omp_init();
#endif

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

    Fs = sf_input ("in" );
    Fr = sf_input ("ur" );
    Fi = sf_output("out");

    /*------------------------------------------------------------*/
    /* read axes */
    az=sf_iaxa(Fs,1); nz = sf_n(az);
    ax=sf_iaxa(Fs,2); nx = sf_n(ax);
    at=sf_iaxa(Fs,3); nt = sf_n(at);

    aa=sf_maxa(1,0,1); 
    sf_setlabel(aa,""); 
    sf_setunit (aa,""); 

    if(verb) {
	sf_raxa(az);
	sf_raxa(ax);
	sf_raxa(at);
    }

    /* write axes */
    sf_oaxa(Fi,az,1);
    sf_oaxa(Fi,ax,2);
    sf_oaxa(Fi,aa,3);
    
    /*------------------------------------------------------------*/
    /* allocate work arrays */
    ii = sf_floatalloc2(nz,nx); 
    us = sf_floatalloc2(nz,nx);
    ur = sf_floatalloc2(nz,nx);
    for    (ix=0; ix<nx; ix++) {
	for(iz=0; iz<nz; iz++) {
	    ii[ix][iz]=0.;
	}
    }

    /*------------------------------------------------------------*/
    if(isreversed) { /* receiver wavefield is reversed */

	if(verb) fprintf(stderr,"nt\n");
	for (it=0; it<nt; it++) {
	    if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b%04d",it);
	    
	    sf_floatread(us[0],nz*nx,Fs);
	    sf_floatread(ur[0],nz*nx,Fr);
	    
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic)				\
    private(ix,iz)							\
    shared (ii,us,ur,nx,nz)
#endif
	    for    (ix=0; ix<nx; ix++) {
		for(iz=0; iz<nz; iz++) {
		    ii[ix][iz] += us[ix][iz]*ur[ix][iz];
		}
	    }
	    
	} /* it */
	if(verb) fprintf(stderr,"\n");
	
    } else { /* receiver wavefield is NOT reversed */

	if(verb) fprintf(stderr,"nt\n");
	for (it=0; it<nt; it++) {
	    if(verb) fprintf(stderr,"\b\b\b\b\b%d",(nt-it-1));
	    
	    sf_floatread(us[0],nz*nx,Fs);
	    iseek=(off_t)(nt-1-it)*nz*nx*sizeof(float);
	    sf_seek(Fr,iseek,SEEK_SET);
	    sf_floatread(ur[0],nz*nx,Fr);
	    
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic)				\
    private(ix,iz)							\
    shared (ii,us,ur,nx,nz)
#endif
	    for    (ix=0; ix<nx; ix++) {
		for(iz=0; iz<nz; iz++) {
		    ii[ix][iz] += us[ix][iz]*ur[ix][iz];
		}
	    }

	} /* it */
	if(verb) fprintf(stderr,"\n");

    } /* end "is reversed" */
    /*------------------------------------------------------------*/
       
    /*------------------------------------------------------------*/
    /* scale image */
    scale = 1./nt;
    for    (ix=0; ix<nx; ix++) {
	for(iz=0; iz<nz; iz++) {
	    ii[ix][iz] *=scale;
	}
    }

    /*------------------------------------------------------------*/
    /* write image */
    sf_floatwrite(ii[0],nx*nz,Fi);
    
    /*------------------------------------------------------------*/
    /* deallocate arrays */
    free(*ii); free(ii);
    free(*us); free(us);
    free(*ur); free(ur);
    /*------------------------------------------------------------*/
    
    exit (0);
}
Пример #24
0
int
main(int argc, char** argv)
{

    bool verb, fsrf, snap, expl, dabc, cden, adj;
    bool optfd, hybrid, sinc; 
    int jsnap, jdata;

    /* I/O files */
    sf_file file_wav=NULL; /* wavelet */
    sf_file file_vel=NULL; /* velocity */
    sf_file file_den=NULL; /* density */
    sf_file file_wfl=NULL; /* wavefield */
    sf_file file_dat=NULL; /* data */
    sf_file file_src=NULL; /* sources */
    sf_file file_rec=NULL; /* receivers */

    /* cube axes */
    sf_axis at = NULL, az = NULL, ax = NULL, ay = NULL;
    sf_axis as = NULL, ar = NULL;

    int nbd;  /* ABC boundary size */
    int fdorder;  /* finite difference spatial accuracy order */
    int nzpad,nxpad,nypad; /* boundary padded model size */
    int ix,iy,it,is,nx,ny,nz,nt,ns,nr;
    float dx,dy,dz,dt,dt2;
    float* damp=NULL; /* damping profile for hybrid bc */
    float* ws;  /* wavelet */
    float*** vel=NULL;  /* velocity */
    float*** rho=NULL; /* density */
    float*** u0=NULL;  /* wavefield array u@t-1 (u@t+1) */
    float*** u1=NULL;  /* wavefield array u@t */
    float* u_dat=NULL; /* output data */
    float*** ptr_tmp=NULL;   
    pt3d* src3d=NULL;  /* source position */
    pt3d* rec3d=NULL;  /*receiver position*/
    scoef3d cssinc = NULL, crsinc = NULL; 
    lint3d cslint = NULL, crlint = NULL;

    /* FDM structure */
    fdm3d fdm = NULL;
    abcone3d abc = NULL;
    sponge spo = NULL;

    int nbell;

    float* fdcoef_d2;
    float* fdcoef_d1;

    sf_axis acz = NULL, acx = NULL, acy = NULL;
    int nqz, nqx, nqy;
    float oqz, oqx, oqy, dqz, dqx, dqy;

    float** oslice = NULL; /* output 3D wavefield slice-by-slice */
    float*** tmp_array;

    double wall_clock_time_s, wall_clock_time_e;

    const int SECOND_DERIV = 2;
    const int FIRST_DERIV = 1;

    int nop;

#if defined _OPENMP && _DEBUG
    double tic;
    double toc;
#endif

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

#ifdef _OPENMP
    omp_init();
    wall_clock_time_s = omp_get_wtime();
#else
    wall_clock_time_s = (double) clock() / CLOCKS_PER_SEC;
#endif

    if (!sf_getbool("verb",&verb))  verb=false; /* Verbosity flag */
    if (!sf_getbool("snap",&snap))  snap=false; /* Wavefield snapshots flag */
    if (!sf_getbool("expl",&expl))  expl=false; /* Multiple sources, one wvlt*/
    if (!sf_getbool("dabc",&dabc))  dabc=false; /* Absorbing BC */
    if (!sf_getbool("cden",&cden))  cden=false; /* Constant density */
    if (!sf_getbool("adj",&adj))    adj=false; /* adjoint flag */

    if (!sf_getbool("free",&fsrf) && !sf_getbool("fsrf",&fsrf)) fsrf=false; /* Free surface flag */

    if (!sf_getint("nbell",&nbell)) nbell=5; /* gaussian for source injection */

    if (!sf_getbool("optfd",&optfd))  optfd=false; /* optimized FD coefficients flag */
    if (!sf_getint("fdorder",&fdorder))  fdorder=4; /* spatial FD order */
    if (!sf_getbool("hybridbc",&hybrid))  hybrid=false;  /* hybrid Absorbing BC */
    if (!sf_getbool("sinc",&sinc)) sinc=false; /* sinc source injection */
  
    /* Initialize variables */
    file_wav = sf_input("in"); /* wavelet */
    file_vel = sf_input("vel"); /* velocity */ 
    file_src = sf_input("sou"); /* sources */
    file_rec = sf_input("rec"); /* receivers */
    file_dat = sf_output("out"); /* data */

    if (snap)  file_wfl = sf_output("wfl"); /* wavefield */
    if (!cden) {
	if (sf_getstring("cden")) {
	    file_den = sf_input ("den"); /* density */
	} else {
	    cden = true;
	    if (verb) sf_warning("No density file provided, running with constant density");
	}
    }
  
    at = sf_iaxa(file_wav,2); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
    az = sf_iaxa(file_vel,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
    ax = sf_iaxa(file_vel,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */
    ay = sf_iaxa(file_vel,3); sf_setlabel(ay,"y"); if(verb) sf_raxa(ay); /* space */

    as = sf_iaxa(file_src,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */
    ar = sf_iaxa(file_rec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */

    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(as);
    nr = sf_n(ar);

    /* other execution parameters */
    if (snap) {
	if (!sf_getint("jsnap",&jsnap))  jsnap=nt;
	/* # of t steps at which to save wavefield */
    }
    if (!sf_getint("jdata",&jdata)) jdata=1;
    /* # of t steps at which to save receiver data */

    /* setup output data header */
    sf_oaxa(file_dat,ar,1);
    sf_setn(at,(nt-1)/jdata+1);
    sf_setd(at,dt*jdata);
    sf_oaxa(file_dat,at,2);

    /* wavefield cut params */
    /* setup output wavefield header */
    if (snap) {
	if (!sf_getint  ("nqz",&nqz)) nqz=sf_n(az); /* Saved wfld window nz */
	if (!sf_getint  ("nqx",&nqx)) nqx=sf_n(ax); /* Saved wfld window nx */
	if (!sf_getint  ("nqy",&nqy)) nqy=sf_n(ay); /* Saved wfld window ny */

	if (!sf_getfloat("oqz",&oqz)) oqz=sf_o(az); /* Saved wfld window oz */
	if (!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax); /* Saved wfld window ox */
	if (!sf_getfloat("oqy",&oqy)) oqy=sf_o(ay); /* Saved wfld window oy */

	if (!sf_getfloat("dqz",&dqz)) dqz=sf_d(az); /* Saved wfld window dz */
	if (!sf_getfloat("dqx",&dqx)) dqx=sf_d(ax); /* Saved wfld window dx */
	if (!sf_getfloat("dqy",&dqy)) dqy=sf_d(ay); /* Saved wfld window dy */
    
	acz = sf_maxa(nqz,oqz,dqz); if (verb) sf_raxa(acz);
	acx = sf_maxa(nqx,oqx,dqx); if (verb) sf_raxa(acx);
	acy = sf_maxa(nqy,oqy,dqy); if (verb) sf_raxa(acy);
	/* check if the imaging window fits in the wavefield domain */
	sf_setn(at,(nt-1)/jsnap+1);
	sf_setd(at,dt*jsnap);
	if (verb) sf_raxa(at);
    
	sf_oaxa(file_wfl,acz,1);
	sf_oaxa(file_wfl,acx,2);
	sf_oaxa(file_wfl,acy,3);
	sf_oaxa(file_wfl,at,4);
    }

    /* 2-2N finite difference coefficient */
    nop = fdorder/2; /* fd half-length stencil */
    if (!sf_getint("nb",&nbd) || nbd<nop)  nbd=nop;
    if (dabc && hybrid && nbd<=nop) nbd = 2*nop;

    /* expand domain for FD operators and ABC */
    fdm = fdutil3d_init(verb,fsrf,az,ax,ay,nbd,1);

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

    /* Precompute coefficients */
    dt2 = dt*dt;
    nzpad = nz+2*nbd;  nxpad = nx+2*nbd;  nypad = ny+2*nbd;

    fdcoef_d2 = compute_fdcoef(nop,dz,dx,dy,optfd,SECOND_DERIV);
    fdcoef_d1 = compute_fdcoef(nop,dz,dx,dy,optfd,FIRST_DERIV);

    /* Allocate memories */
    if (expl) ws = sf_floatalloc(1);
    else      ws = sf_floatalloc(ns);
    vel = sf_floatalloc3(nzpad,nxpad,nypad);
    if (!cden) rho = sf_floatalloc3(nzpad,nxpad,nypad);
    u_dat = sf_floatalloc(nr);
    src3d = pt3dalloc1(ns);
    rec3d = pt3dalloc1(nr);
    if (snap) oslice = sf_floatalloc2(sf_n(acz),sf_n(acx));

    /* source and receiver position */
    pt3dread1(file_src,src3d,ns,3);  /* read format: (x,y,z) */
    if (sinc) cssinc = sinc3d_make(ns,src3d,fdm);
    else      cslint = lint3d_make(ns,src3d,fdm);

    pt3dread1(file_rec,rec3d,nr,3);  /* read format: (x,y,z) */
    if (sinc) crsinc = sinc3d_make(nr,rec3d,fdm);
    else      crlint = lint3d_make(nr,rec3d,fdm);

    if (!sinc) fdbell3d_init(nbell);

    /* temperary array */
    tmp_array = sf_floatalloc3(nz,nx,ny);

    /* read velocity and pad */
    sf_floatread(tmp_array[0][0],nz*nx*ny,file_vel);
    expand3d(tmp_array,vel,fdm);
    /* read density and pad */
    if (!cden) {
	sf_floatread(tmp_array[0][0],nz*nx*ny,file_den);
	expand3d(tmp_array,rho,fdm);
    }

    free(**tmp_array);  free(*tmp_array);  free(tmp_array);

    /* A1 one-way ABC implicit scheme coefficients  */
    if (dabc) {
	abc = abcone3d_make(nbd,dt,vel,fsrf,fdm);
	if (hybrid)
	    damp = damp_make(nbd-nop); /* compute damping profiles for hybrid bc */
	else
	    spo = sponge_make(fdm->nb);
    }

    /* allocate memory for wavefield variables */
    u0 = sf_floatalloc3(nzpad,nxpad,nypad);
    u1 = sf_floatalloc3(nzpad,nxpad,nypad);

    /* initialize variables */
    memset(u0[0][0],0,sizeof(float)*nzpad*nxpad*nypad);
    memset(u1[0][0],0,sizeof(float)*nzpad*nxpad*nypad);
    memset(u_dat,0,sizeof(float)*nr);

    /* v = (v*dt)^2 */
    for (ix=0;ix<nzpad*nxpad*nypad;ix++)
	*(vel[0][0]+ix) *= *(vel[0][0]+ix)*dt2;
    if (fsrf && !hybrid) {
	for (iy=0; iy<nypad; iy++)
	    for (ix=0; ix<nxpad; ix++)
		memset(vel[iy][ix],0,sizeof(float)*fdm->nb);
    }

    for (it=0; it<nt; it++) {
	if (verb)  sf_warning("it=%d;",it+1);
#if defined _OPENMP && _DEBUG
	tic=omp_get_wtime();
#endif
    
	step_forward(u0,u1,vel,rho,fdcoef_d2,fdcoef_d1,nop,nzpad,nxpad,nypad);
    
	if (adj) { /* backward inject source wavelet */
	    if (expl) {
		sf_seek(file_wav,(off_t)(nt-it-1)*sizeof(float),SEEK_SET);
		sf_floatread(ws,1,file_wav);
		ws[0] *= dt2;
		if (sinc) sinc3d_inject1(u0,ws[0],cssinc);
		else      lint3d_inject1(u0,ws[0],cslint);
	    } else { 
		sf_seek(file_wav,(off_t)(nt-it-1)*ns*sizeof(float),SEEK_SET);
		sf_floatread(ws,ns,file_wav);
		for (is=0; is<ns; is++) ws[is] *= dt2;
		if (sinc) sinc3d_inject(u0,ws,cssinc);
		else      lint3d_inject(u0,ws,cslint);
	    }
	} else { /* forward inject source wavelet */
	    if (expl) {
		sf_floatread(ws,1,file_wav);
		ws[0] *= dt2;
		if (sinc) sinc3d_inject1(u0,ws[0],cssinc);
		else      lint3d_inject1(u0,ws[0],cslint);
	    } else {
		sf_floatread(ws,ns,file_wav);
		for (is=0; is<ns; is++) ws[is] *= dt2;
		if (sinc) sinc3d_inject(u0,ws,cssinc);
		else      lint3d_inject(u0,ws,cslint);
	    }
	}

	/* apply abc */
	if (dabc) {
	    if (hybrid) apply_abc(u0,u1,nz,nx,ny,nbd,abc,nop,damp);
	    else {
		abcone3d_apply(u0,u1,nop,abc,fdm);
		sponge3d_apply(u0,spo,fdm);
		sponge3d_apply(u1,spo,fdm);
	    }
	}

	/* loop over pointers */
	ptr_tmp = u0;  u0 = u1;  u1 = ptr_tmp;

	/* extract snapshot */
	if (snap && it%jsnap==0) {
	    int fy = (floor)((sf_o(acy)-fdm->oypad)/fdm->dy);
	    int jy = floor(sf_d(acy)/fdm->dy);
	    float **ptr_slice;
	    for (iy=0; iy<sf_n(acy); iy++) {
		ptr_slice = u0[fy+iy*jy];
		cut3d_slice(ptr_slice,oslice,fdm,acz,acx);
		sf_floatwrite(oslice[0],sf_n(acz)*sf_n(acx),file_wfl);
	    }
	}

	/* extract receiver data */
	if (sinc) sinc3d_extract(u0,u_dat,crsinc);
	else      lint3d_extract(u0,u_dat,crlint);

	sf_floatwrite(u_dat,nr,file_dat);

#if defined _OPENMP && _DEBUG
	toc=omp_get_wtime(); 
	fprintf(stderr,"%5.2gs",(float)(toc-tic));
#endif
    }
#ifdef _OPENMP
    wall_clock_time_e = omp_get_wtime();
#else
    wall_clock_time_e = (double) clock() / CLOCKS_PER_SEC;
#endif
    if (verb)
	fprintf(stderr,"\nElapsed time: %lf s\n",wall_clock_time_e-wall_clock_time_s);

    free(**u0); free(*u0); free(u0);
    free(**u1); free(*u1); free(u1);
    free(**vel); free(*vel); free(vel);
    free(u_dat);
    free(ws);
    free(fdcoef_d2); free(fdcoef_d1);
    if (snap) { free(*oslice); free(oslice); }
    if(!cden) { free(**rho); free(*rho); free(rho); }
    if (hybrid) free(damp);
    free(src3d); free(rec3d);

    return 0;
}
Пример #25
0
int main(int argc, char* argv[])
{
    bool verb; /* verbosity flag */
    bool abc;  /* absorbing boundary conditions flag */
    bool free; /* free surface flag*/
    bool snap; /* wavefield snapshots flag */
    int  jsnap;/* save wavefield every *jsnap* time steps */

    /* cube axes */
    sf_axis at,az,ax,as,ar,bt;
    int it,iz,ix,is,ir, iop;
    int nt,nz,nx,ns,nr,nz2,nx2;
    float z0,x0,dt,dx,dz, idx,idz,dt2;

    /* Laplacian */
    int   nop=2;       /* Laplacian operator size */
    float c0, c1, c2;  /* Laplacian operator coefficients */
    float co,c1x,c2x,c1z,c2z;

    int  nbz,nbx; /* boundary size */
    float tz, tx; /* sponge boundary decay coefficients */
    float dp;
    float ws;     /* injected data */

    /* linear interpolation */
    float *fzs,*fxs,    *fzr,*fxr;
    int   *jzs,*jxs,    *jzr,*jxr;

    float *ws00,*ws01,*ws10,*ws11;
    float *wr00,*wr01,*wr10,*wr11;

    /* boundary */
    float  *bzl,*bzh,*bxl,*bxh;

    /* I/O files */
    sf_file Fw,Fs,Fr;
    float  *ww;      /* wavelet */
    pt2d   *ss, *rr; /* source/receiver locations */

    float **tt; /* taper */

    /* background */
    sf_file Bv,Bd,Bu; /* velocity, data, wavefield */
    float **bvv,**bvo;               /* velocity   */
    float  *bdd;                     /* data       */
    float **bum,**buo,**bup,**bud;   /* wavefields */

    /* perturbation */
    sf_file Pv,Pd,Pu;
    float **pvv,**pvo;
    float  *pdd;
    float **pum,**puo,**pup,**pud;

    int ompchunk;  /* OpenMP data chunk size */

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

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

    if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1;

    if(! sf_getbool("verb",&verb)) verb=false;
    if(! sf_getbool( "abc",&abc ))  abc=false;
    if(! sf_getbool("snap",&snap)) snap=false;
    if(! sf_getbool("free",&free)) free=false;

    Fw = sf_input ("in" ); /* wavelet */
    Fs = sf_input ("sou"); /* sources */
    Fr = sf_input ("rec"); /* receivers */

    Bv = sf_input ("vel"); /* velocity */
    Bu = sf_output("wfl"); /* wavefield */
    Bd = sf_output("out"); /* data */

    Pv = sf_input ("ref"); /* velocity */
    Pu = sf_output("liw"); /* linearized wavefield */
    Pd = sf_output("lid"); /* linearized data */

    /* read axes*/
    at=sf_iaxa(Fw,1); sf_setlabel(at,"t"); 
    nt=sf_n(at); dt=sf_d(at); if(verb) sf_raxa(at); /* time */
    as=sf_iaxa(Fs,2); sf_setlabel(as,"s"); 
    ns=sf_n(as); if(verb) sf_raxa(as); /* sources */
    ar=sf_iaxa(Fr,2); sf_setlabel(ar,"r"); 
    nr=sf_n(ar); if(verb) sf_raxa(ar); /* receivers */

    az=sf_iaxa(Bv,1); sf_setlabel(az,"z"); 
    nz=sf_n(az); dz=sf_d(az); if(verb) sf_raxa(az); /* depth */
    ax=sf_iaxa(Bv,2); sf_setlabel(ax,"x"); 
    nx=sf_n(ax); dx=sf_d(ax); if(verb) sf_raxa(ax); /* space */

    /* configure wavefield snapshots */
    if(snap) {
	if(! sf_getint("jsnap",&jsnap)) jsnap=nt;
    }

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

    /* expand domain for absorbing boundary conditions */
    if(abc) {
	if(! sf_getint("nbz",&nbz)) nbz=nop; if(nbz<nop) nbz=nop;
	if(! sf_getint("nbx",&nbx)) nbx=nop; if(nbx<nop) nbx=nop;
	
	if(! sf_getfloat("tz",&tz)) tz=0.025;
	if(! sf_getfloat("tx",&tx)) tx=0.025;
    } else {
	nbz=nop;
	nbx=nop;
    }
    /* expanded domain ( az+2 nz, ax+2 nx ) */
    nz2=nz+2*nbz; z0=sf_o(az)-nbz*dz; 
    nx2=nx+2*nbx; x0=sf_o(ax)-nbx*dx; 

    sf_setn(az,nz2); sf_seto(az,z0); if(verb) sf_raxa(az);
    sf_setn(ax,nx2); sf_seto(ax,x0); if(verb) sf_raxa(ax);
/*------------------------------------------------------------*/

    /* setup output wavefield header */
    if(snap) {
	bt = sf_maxa(nt/jsnap,sf_o(at),dt*jsnap);
	sf_setlabel(bt,"t");

	sf_oaxa(Bu,az,1);
	sf_oaxa(Bu,ax,2);
	sf_oaxa(Bu,bt,3);

	sf_oaxa(Pu,az,1);
	sf_oaxa(Pu,ax,2);
	sf_oaxa(Pu,bt,3);
    }

    /* setup output data header */
    sf_oaxa(Bd,ar,1);
    sf_oaxa(Bd,at,2);

    sf_oaxa(Pd,ar,1);
    sf_oaxa(Pd,at,2);

    dt2 =    dt*dt;
    idz = 1/(dz*dz);
    idx = 1/(dx*dx);

    /* Laplacian coefficients */
    c0=-30./12.; 
    c1=+16./12.;
    c2=- 1./12.;

    co = c0 * (idx+idz);
    c1x= c1 *  idx;
    c2x= c2 *  idx;
    c1z= c1 *      idz;
    c2z= c2 *      idz;

/*------------------------------------------------------------*/
     
    /* allocate arrays */
    ww=sf_floatalloc (nt);      sf_floatread(ww   ,nt     ,Fw);

    bvv=sf_floatalloc2(nz,nx); sf_floatread(bvv[0],nz*nx,Bv);
    pvv=sf_floatalloc2(nz,nx); sf_floatread(pvv[0],nz*nx,Pv);

    /* allocate source/receiver point arrays */
    ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); 
    rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); 

    pt2dread1(Fs,ss,ns,3); /* read 3 elements (x,z,v) */
    pt2dread1(Fr,rr,nr,2); /* read 2 elements (x,z)   */

    bdd=sf_floatalloc(nr);
    pdd=sf_floatalloc(nr);

    jzs=sf_intalloc(ns); fzs=sf_floatalloc(ns); 
    jzr=sf_intalloc(nr); fzr=sf_floatalloc(nr);
    jxs=sf_intalloc(ns); fxs=sf_floatalloc(ns);
    jxr=sf_intalloc(nr); fxr=sf_floatalloc(nr);

    ws00 = sf_floatalloc(ns); wr00 = sf_floatalloc(nr); 
    ws01 = sf_floatalloc(ns); wr01 = sf_floatalloc(nr);
    ws10 = sf_floatalloc(ns); wr10 = sf_floatalloc(nr);
    ws11 = sf_floatalloc(ns); wr11 = sf_floatalloc(nr);
/*------------------------------------------------------------*/

    for (is=0;is<ns;is++) {

	if(ss[is].z >= z0 && 
	   ss[is].z <  z0 + (nz2-1)*dz &&
	   ss[is].x >= x0 && 
	   ss[is].x <  x0 + (nx2-1)*dx) {
	    
	    jzs[is] = (int)( (ss[is].z-z0)/dz);
	    fzs[is] =        (ss[is].z-z0)/dz - jzs[is];	    
	    jxs[is] = (int)( (ss[is].x-x0)/dx);
	    fxs[is] =        (ss[is].x-x0)/dx - jxs[is];
	} else {
	    jzs[is] = 0; jxs[is] = 0;
	    fzs[is] = 1; fxs[is] = 0;
	    ss[is].v= 0;
	}

	ws00[is] = (1-fzs[is])*(1-fxs[is]);
	ws01[is] = (  fzs[is])*(1-fxs[is]);
	ws10[is] = (1-fzs[is])*(  fxs[is]);
	ws11[is] = (  fzs[is])*(  fxs[is]);

    }

    for (ir=0;ir<nr;ir++) {

	if(rr[ir].z >= z0 && 
	   rr[ir].z < z0 + (nz2-1)*dz &&
	   rr[ir].x >= x0 && 
	   rr[ir].x < x0 + (nx2-1)*dx) {
	    
	    jzr[ir] = (int)( (rr[ir].z-z0)/dz);
	    fzr[ir] =        (rr[ir].z-z0)/dz - jzr[ir];
	    jxr[ir] = (int)( (rr[ir].x-x0)/dx);
	    fxr[ir] =        (rr[ir].x-x0)/dx - jxr[ir];

	    rr[ir].v=1;
	} else {
	    jzr[ir] = 0;
	    fzr[ir] = 1;
	    rr[ir].v= 0;
	}

	wr00[ir] = (1-fzr[ir])*(1-fxr[ir]);
	wr01[ir] = (  fzr[ir])*(1-fxr[ir]);
	wr10[ir] = (1-fzr[ir])*(  fxr[ir]);
	wr11[ir] = (  fzr[ir])*(  fxr[ir]);
    }
    
/*------------------------------------------------------------*/
    
    /* allocate temporary arrays */
    bum=sf_floatalloc2(nz2,nx2);
    buo=sf_floatalloc2(nz2,nx2);
    bup=sf_floatalloc2(nz2,nx2);
    bud=sf_floatalloc2(nz2,nx2);

    pum=sf_floatalloc2(nz2,nx2);
    puo=sf_floatalloc2(nz2,nx2);
    pup=sf_floatalloc2(nz2,nx2);
    pud=sf_floatalloc2(nz2,nx2);

    tt=sf_floatalloc2(nz2,nx2);

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nx2,nz2,bum,buo,bup,bud,pum,puo,pup,pud,tt)
#endif
    for (iz=0; iz<nz2; iz++) {
	for (ix=0; ix<nx2; ix++) {
	    bum[ix][iz]=pum[ix][iz]=0;
	    buo[ix][iz]=puo[ix][iz]=0;
	    bup[ix][iz]=pup[ix][iz]=0;
	    bud[ix][iz]=pud[ix][iz]=0;
	    tt[ix][iz]=1;
	}
    }

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

    /* velocity in the expanded domain (vo=vv^2)*/
    bvo=sf_floatalloc2(nz2,nx2);
    pvo=sf_floatalloc2(nz2,nx2);

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nz,nx,bvv,pvv,bvo,pvo)
#endif
    for (iz=0; iz<nz; iz++) {
	for (ix=0; ix<nx; ix++) {
	    bvo[nbx+ix][nbz+iz] = bvv[ix][iz] * bvv[ix][iz];
	    pvo[nbx+ix][nbz+iz] = pvv[ix][iz];
	}
    }
    /* fill boundaries */
    for (iz=0; iz<nbz; iz++) {
	for (ix=0; ix<nx2; ix++) {
	    bvo[ix][    iz  ] = bvo[ix][    nbz  ];
	    bvo[ix][nz2-iz-1] = bvo[ix][nz2-nbz-1];

	    pvo[ix][    iz  ] = pvo[ix][    nbz  ];
	    pvo[ix][nz2-iz-1] = pvo[ix][nz2-nbz-1];
	}
    }
    for (iz=0; iz<nz2; iz++) {
	for (ix=0; ix<nbx; ix++) {
	    bvo[    ix  ][iz] = bvo[    nbx  ][iz];
	    bvo[nx2-ix-1][iz] = bvo[nx2-nbx-1][iz];

	    pvo[    ix  ][iz] = pvo[    nbx  ][iz];
	    pvo[nx2-ix-1][iz] = pvo[nx2-nbx-1][iz];
	}
    }
 
/*------------------------------------------------------------*/

    /* free surface */
    if(abc && free) {
	for (iz=0; iz<nbz; iz++) {
	    for (ix=0; ix<nx2; ix++) {
		bvo[ix][iz]=0;
		pvo[ix][iz]=0;
	    }
	}
    }

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

    /* sponge ABC setup */
    if(abc) {
	for (iz=0; iz<nbz; iz++) {
	    for (ix=0; ix<nx2; ix++) {
		tt[ix][    iz  ] = exp( - (tz*(nbz-iz))*(tz*(nbz-iz)) );
		tt[ix][nz2-iz-1] = tt[ix][iz];
	    }
	}
	for (iz=0; iz<nz2; iz++) {
	    for (ix=0; ix<nbx; ix++) {
		tt[    ix  ][iz] = exp( - (tx*(nbx-ix))*(tx*(nbx-ix)) );
		tt[nx2-ix-1][iz] = tt[ix][iz];
	    }
	}
    }

    /* one-way ABC setup */
    bzl=sf_floatalloc(nx2);
    bzh=sf_floatalloc(nx2);
    bxl=sf_floatalloc(nz2);
    bxh=sf_floatalloc(nz2);
    
    for (ix=0;ix<nx2;ix++) {
	dp = bvo[ix][     nop  ] *dt/dz; bzl[ix] = (1-dp)/(1+dp);
	dp = bvo[ix][nz2-nop-1] *dt/dz; bzh[ix] = (1-dp)/(1+dp);
    }
    for (iz=0;iz<nz2;iz++) {
	dp = bvo[    nop  ][iz] *dt/dx; bxl[iz] = (1-dp)/(1+dp);
	dp = bvo[nx2-nop-1][iz] *dt/dx; bxh[iz] = (1-dp)/(1+dp);
    }
/*------------------------------------------------------------*/

    /* 
     *  MAIN LOOP
     */
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<nt; it++) {
	if(verb) fprintf(stderr,"\b\b\b\b\b%d",it);
	
	/* 4th order Laplacian operator */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nop,nx2,nz2,bud,buo,pud,puo,co,c1x,c1z,c2x,c2z,idx,idz)
#endif
	for (ix=nop; ix<nx2-nop; ix++) {
	    for (iz=nop; iz<nz2-nop; iz++) {
		bud[ix][iz] = 
		    co * buo[ix  ][iz  ] + 
		    c1x*(buo[ix-1][iz  ] + buo[ix+1][iz  ]) +
		    c2x*(buo[ix-2][iz  ] + buo[ix+2][iz  ]) +
		    c1z*(buo[ix  ][iz-1] + buo[ix  ][iz+1]) +
		    c2z*(buo[ix  ][iz-2] + buo[ix  ][iz+2]);	  

		pud[ix][iz] = 
		    co * puo[ix  ][iz  ] + 
		    c1x*(puo[ix-1][iz  ] + puo[ix+1][iz  ]) +
		    c2x*(puo[ix-2][iz  ] + puo[ix+2][iz  ]) +
		    c1z*(puo[ix  ][iz-1] + puo[ix  ][iz+1]) +
		    c2z*(puo[ix  ][iz-2] + puo[ix  ][iz+2]);	 
	    }
	}

	/* inject source */
	for (is=0;is<ns;is++) {
	    ws = ww[it] * ss[is].v;
	    bud[ jxs[is]  ][ jzs[is]  ] -= ws * ws00[is];
	    bud[ jxs[is]  ][ jzs[is]+1] -= ws * ws01[is];
	    bud[ jxs[is]+1][ jzs[is]  ] -= ws * ws10[is];
	    bud[ jxs[is]+1][ jzs[is]+1] -= ws * ws11[is];
	}

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,pud,bud,pvo)
#endif
	for (ix=0; ix<nx2; ix++) {
	    for (iz=0; iz<nz2; iz++) {
		pud[ix][iz] -= bud[ix][iz] * 2*pvo[ix][iz];
	    }
	}

	/* velocity scale */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,bud,pud,bvo)
#endif
	for (ix=0; ix<nx2; ix++) {
	    for (iz=0; iz<nz2; iz++) {
		bud[ix][iz] *= bvo[ix][iz];
		pud[ix][iz] *= bvo[ix][iz];
	    }
	}
	
	/* time step */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,bud,buo,bum,bup,pud,puo,pum,pup,dt2)
#endif
	for (ix=0; ix<nx2; ix++) {
	    for (iz=0; iz<nz2; iz++) {
		bup[ix][iz] = 2*buo[ix][iz] - bum[ix][iz] + bud[ix][iz] * dt2; 
		bum[ix][iz] =   buo[ix][iz];
		buo[ix][iz] =   bup[ix][iz];

		pup[ix][iz] = 2*puo[ix][iz] - pum[ix][iz] + pud[ix][iz] * dt2; 
		pum[ix][iz] =   puo[ix][iz];
		puo[ix][iz] =   pup[ix][iz];
	    }
	}
	
	/* one-way ABC apply */
	if(abc) {
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz,iop) shared(nx2,nz2,nop,buo,bum,puo,pum,bzl,bzh)
#endif
	    for(ix=0;ix<nx2;ix++) {
		for(iop=0;iop<nop;iop++) {
		    iz = nop-iop;
		    buo      [ix][iz  ] 
			= bum[ix][iz+1] 
			+(bum[ix][iz  ]
			- buo[ix][iz+1]) * bzl[ix];
		    puo      [ix][iz  ] 
			= pum[ix][iz+1] 
			+(pum[ix][iz  ]
			- puo[ix][iz+1]) * bzl[ix];
		    
		    iz = nz2-nop+iop-1;
		    buo      [ix][iz  ] 
			= bum[ix][iz-1]
			+(bum[ix][iz  ]
			- buo[ix][iz-1]) * bzh[ix];
		    puo      [ix][iz  ] 
			= pum[ix][iz-1]
			+(pum[ix][iz  ]
			- puo[ix][iz-1]) * bzh[ix];
		}
	    }

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,1) private(ix,iz,iop) shared(nx2,nz2,nop,buo,bum,puo,pum,bzl,bzh)
#endif
	    for(iop=0;iop<nop;iop++) {
		for(iz=0;iz<nz2;iz++) {
		    ix = nop-iop;
		    buo      [ix  ][iz] 
			= bum[ix+1][iz] 
			+(bum[ix  ][iz]
			- buo[ix+1][iz]) * bxl[iz];
		    puo      [ix  ][iz] 
			= pum[ix+1][iz] 
			+(pum[ix  ][iz]
			- puo[ix+1][iz]) * bxl[iz];
		    
		    ix = nx2-nop+iop-1;
		    buo      [ix  ][iz] 
			= bum[ix-1][iz]
			+(bum[ix  ][iz]
			- buo[ix-1][iz]) * bxh[iz];
		    puo      [ix  ][iz] 
			= pum[ix-1][iz]
			+(pum[ix  ][iz]
			- puo[ix-1][iz]) * bxh[iz];
		}
	    }
	}
	
	/* sponge ABC apply */
	if(abc) {
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,buo,bum,bud,puo,pum,pud,tt)
#endif
	    for (ix=0; ix<nx2; ix++) {
		for (iz=0; iz<nz2; iz++) {
		    buo[ix][iz] *= tt[ix][iz];
		    bum[ix][iz] *= tt[ix][iz];
		    bud[ix][iz] *= tt[ix][iz];

		    puo[ix][iz] *= tt[ix][iz];
		    pum[ix][iz] *= tt[ix][iz];
		    bud[ix][iz] *= tt[ix][iz];
		}
	    }
	}
	
	/* write wavefield */
	if(snap && it%jsnap==0) {
	    sf_floatwrite(buo[0],nz2*nx2,Bu);
	    sf_floatwrite(puo[0],nz2*nx2,Pu);
	}

	/* write data */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,1) private(ir) shared(bdd,pdd,rr,buo,puo,jzr,wr00,wr01,wr10,wr11)
#endif
	for (ir=0;ir<nr;ir++) {
	    bdd[ir] =
		buo[ jxr[ir]  ][ jzr[ir]  ] * wr00[ir] +
		buo[ jxr[ir]  ][ jzr[ir]+1] * wr01[ir] +
		buo[ jxr[ir]+1][ jzr[ir]  ] * wr10[ir] +
		buo[ jxr[ir]+1][ jzr[ir]+1] * wr11[ir];
	    bdd[ir] *= rr[ir].v;

	    pdd[ir] =
		puo[ jxr[ir]  ][ jzr[ir]  ] * wr00[ir] +
		puo[ jxr[ir]  ][ jzr[ir]+1] * wr01[ir] +
		puo[ jxr[ir]+1][ jzr[ir]  ] * wr10[ir] +
		puo[ jxr[ir]+1][ jzr[ir]+1] * wr11[ir];
	    pdd[ir] *= rr[ir].v;
	}
	/* write data */
	sf_floatwrite(bdd,nr,Bd);
	sf_floatwrite(pdd,nr,Pd);
    }
    if(verb) fprintf(stderr,"\n");    

    exit (0);
}
Пример #26
0
int main(int argc, char* argv[])
{
    bool verb;  /* verbosity flag */
    float clip; /* threshold (clip value) */
    sf_file Fc; /* cube file */
    sf_file Fl; /* list file */
    extern int fseeko(FILE *stream, off_t offset, int whence);

    sf_axis   ax,ay,az;
    sf_axis   aa;
    int   ix,iy,iz;
    int   nx,ny,nz,nj,na;
    int   nk=0,jk;

    float **cube;
    float dx,dy,dz;
    float x0,y0,z0;

    FILE* tfile;
    char* tname;
    float t2[3],t3[4];

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

    /* init RSF */
    sf_init(argc,argv);
    if(! sf_getbool("verb",&verb)) verb=false;
    if(! sf_getfloat("clip",&clip)) clip=0;

    Fc = sf_input ( "in"); /* input  cube */
    Fl = sf_output("out"); /* output list */

    /* read axes*/
    az=sf_iaxa(Fc,1);
    sf_setlabel(az,"z");
    ax=sf_iaxa(Fc,2);
    sf_setlabel(ax,"x");
    ay=sf_iaxa(Fc,3);
    sf_setlabel(ay,"y");

    nz=sf_n(az);
    z0=sf_o(az);
    dz=sf_d(az);
    nx=sf_n(ax);
    x0=sf_o(ax);
    dx=sf_d(ax);
    ny=sf_n(ay);
    y0=sf_o(ay);
    dy=sf_d(ay);

    na=0;
    if(ny>1) {
        if(verb) sf_warning("initiating 3D points");
        nj=4;
    } else {
        if(verb) sf_warning("initiating 2D points");
        nj=3;
    }
    /*------------------------------------------------------------*/

    cube = sf_floatalloc2(nz,nx);

    tfile = sf_tempfile(&(tname), "w+b");

    for (iy=0; iy<ny; iy++) {
        /*	if(verb) sf_warning("iy=%d",iy);*/

        sf_floatread(cube[0],nz*nx,Fc);

        nk=0;
        for (ix=0; ix<nx; ix++) {
            for (iz=0; iz<nz; iz++) {
                if( fabs(cube[ix][iz]) > clip) {
                    nk++;
                }
            }
        }

        if(ny>1) {
            jk=0;
            for (ix=0; ix<nx; ix++) {
                for (iz=0; iz<nz; iz++) {
                    if( fabs(cube[ix][iz]) > clip) {
                        t3[0] = x0 + ix * dx;
                        t3[1] = y0 + iy * dy;
                        t3[2] = z0 + iz * dz;
                        t3[3] = cube[ix][iz];

                        fseeko(tfile,jk*4*sizeof(float),SEEK_SET);
                        fwrite(   t3,     sizeof(float),4,tfile);
                        jk++;
                    }
                }
            }
        } else {
            jk=0;
            for (ix=0; ix<nx; ix++) {
                for (iz=0; iz<nz; iz++) {
                    if( fabs(cube[ix][iz]) > clip) {
                        t2[0] = x0 + ix * dx;
                        t2[1] = z0 + iz * dz;
                        t2[2] = cube[ix][iz];

                        fseeko(tfile,jk*3*sizeof(float),SEEK_SET);
                        fwrite(   t2,     sizeof(float),3,tfile);
                        jk++;
                    }
                }
            }
        } /* else ny=1 */

        na += nk;
    } /* iy */

    /* output axes */
    aa = sf_maxa(nj,0,1);
    sf_oaxa(Fl,aa,1);
    if(verb) sf_raxa(aa);
    free(aa);
    aa = sf_maxa(na,0,1);
    sf_oaxa(Fl,aa,2);
    if(verb) sf_raxa(aa);
    free(aa);

    if( ny>1) {
        for( jk=0; jk<nk; jk++) {
            fseeko(tfile,jk*4*sizeof(float),SEEK_SET);
            fread(    t3,     sizeof(float),4,tfile);
            /*	    if(verb) sf_warning("%d, %g %g %g %g",jk,t3[0],t3[1],t3[2],t3[3]);*/

            sf_floatwrite(t3,4,Fl);
        }
    } else {
        for( jk=0; jk<nk; jk++) {
            fseeko(tfile,jk*3*sizeof(float),SEEK_SET);
            fread(    t2,     sizeof(float),3,tfile);
            /*	    if(verb) sf_warning("%d, %g %g %g",jk,t2[0],t2[1],t2[2]);*/

            sf_floatwrite(t2,3,Fl);
        }
    }

    free(cube);
    unlink(tname);

    exit (0);
}
Пример #27
0
int main (int argc, char *argv[])
{
    bool verb;            /* verbosity */
    bool incore;          /* in core execution */
    float eps;            /* dip filter constant */  
    int   nrmax;          /* number of reference velocities */
    float dtmax;          /* time error */
    int   pmx,pmy;        /* padding in the k domain */
    int   tmx,tmy;        /* boundary taper size */
    bool cw;              /* converted waves flag */

    sf_axis az,ax,ay,aw,alx,aly;

    int n,nz,nw;

    sf_file Fs_s=NULL,Fs_r=NULL;  /*  slowness file S      (nlx,nly,nz) */
    sf_file Fw_s=NULL,Fw_r=NULL;  /* wavefield file D or U ( nx, ny,nw) */
    sf_file Fr=NULL;              /* reflectivity */

    fslice wfl_s=NULL,wfl_r=NULL,refl=NULL;
    fslice slo_s=NULL,slo_r=NULL;

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

    /* converted waves flag */
    if (NULL != sf_getstring("sls")) {
	cw=true;
    } else {
	cw=false;
    }

    if (!sf_getbool("verb",  &verb))    verb =  true; /* verbosity flag */
    if (!sf_getbool("incore",&incore))incore = false; /* in core execution */
    if (!sf_getfloat("eps",  &eps ))     eps =  0.01; /* stability parameter */
    if (!sf_getint(  "nrmax",&nrmax))  nrmax =     1; /* maximum number of refs */
    if (!sf_getfloat("dtmax",&dtmax))  dtmax = 0.004; /* time error */
    if (!sf_getint(  "pmx",  &pmx ))     pmx =     0; /* padding on x */
    if (!sf_getint(  "pmy",  &pmy ))     pmy =     0; /* padding on y */
    if (!sf_getint(  "tmx",  &tmx ))     tmx =     0; /* taper on x   */
    if (!sf_getint(  "tmy",  &tmy ))     tmy =     0; /* taper on y   */
    
    /*------------------------------------------------------------*/
    /* SLOWNESS */
    ;      Fs_s = sf_input("slo");
    if(cw) Fs_r = sf_input("sls");
    alx = sf_iaxa(Fs_s,1); sf_setlabel(alx,"lx");
    aly = sf_iaxa(Fs_s,2); sf_setlabel(aly,"ly");
    az  = sf_iaxa(Fs_s,3); sf_setlabel(az , "z");
    /* test here if slo and sls have similar sizes */

    n = sf_n(alx)*sf_n(aly);
    nz = sf_n(az);

    ;      slo_s = fslice_init(n,nz,sizeof(float));
    if(cw) slo_r = fslice_init(n,nz,sizeof(float));
    ;      fslice_load(Fs_s,slo_s,SF_FLOAT);
    if(cw) fslice_load(Fs_r,slo_r,SF_FLOAT);
    
    /*------------------------------------------------------------*/
    /* WAVEFIELD/IMAGE */
    Fw_s = sf_input ( "in");
    Fw_r = sf_output("out"); sf_settype(Fw_r,SF_COMPLEX);
    Fr   = sf_input ("ref");

    if (SF_COMPLEX !=sf_gettype(Fw_s)) 
	sf_error("Need complex source wavefield");
    
    ax = sf_iaxa(Fw_s,1); sf_setlabel(ax,"x"); sf_oaxa(Fw_r,ax,1);
    ay = sf_iaxa(Fw_s,2); sf_setlabel(ay,"y"); sf_oaxa(Fw_r,ay,2);
    aw = sf_iaxa(Fw_s,3); sf_setlabel(aw,"w"); sf_oaxa(Fw_r,aw,3);

    n = sf_n(ax)*sf_n(ay);
    nw = sf_n(aw);

    /* slice management (temp files) */
    wfl_s = fslice_init(n,nw,sizeof(sf_complex));
    wfl_r = fslice_init(n,nw,sizeof(sf_complex));
    refl  = fslice_init(n,nz,sizeof(float));

    fslice_load(Fw_s,wfl_s,SF_COMPLEX);
    fslice_load(Fr,  refl, SF_FLOAT);

    /*------------------------------------------------------------*/
    /* MODELING */
    srmod_init (verb,incore,eps,dtmax,
		az,aw,ax,ay,alx,aly,
		tmx,tmy,pmx,pmy);
    
    if(cw) { 
	srmod_cw_init (dtmax,nrmax,slo_s,slo_r);
	srmod_cw      (wfl_s,wfl_r,refl);
	srmod_cw_close();
    } else { 
	srmod_pw_init (dtmax,nrmax,slo_s);
	srmod_pw      (wfl_s,wfl_r,refl);
	srmod_pw_close();
    }

    srmod_close();

    /*------------------------------------------------------------*/
    /* slice management (temp files) */
    fslice_dump(Fw_r,wfl_r,SF_COMPLEX);
    ;      fslice_close(slo_s);
    if(cw) fslice_close(slo_r);
    ;      fslice_close(wfl_s);
    ;      fslice_close(wfl_r);
    ;      fslice_close(refl);

    exit (0);
}
Пример #28
0
int main (int argc, char *argv[])
{
    int ir, nr, n1,n2,n3, m1, m2, m3, n12, nw, nj1, i3;
    float *u1, *u2, *p;
    sf_file in, out, dip;
    bool verb;
    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,2);

    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_getbool("verb",&verb)) verb = false;
    /* verbosity flag */

    if (!sf_getint("nj1",&nj1)) nj1=1;
    /* aliasing */

    n3 = 1;

    for (ir=0; ir < nr; ir++) {
	if (verb) sf_warning("slice %d of %d", ir+1, nr);
	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 */
	    allpass1(false, false, ap, u1, u2);
	    
	    /* write t-x destruction */
	    sf_floatwrite(u2,n12,out);
	}
	
	free(u1);
	free(u2);
	free(p);
	
    }
    
    
    exit (0);
}
Пример #29
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);
}
Пример #30
0
int main(int argc, char* argv[])
{
    map4 mo;
    bool inv;
    int n1, i2, n2, i3, n3;
    float o1, d1, o2, d2, eps, t, t0;
    float *trace, *t2, *trace2;
    sf_file in, out;

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

    if (!sf_getbool("inv",&inv)) inv=false;
    /* inversion flag */

    if (inv) {
	if (!sf_histint(in,"n1",&n2)) sf_error("No n1= in input");
	if (!sf_histfloat(in,"d1",&d2)) d2=1.;
	if (!sf_histfloat(in,"o1",&o2)) o2=0.;

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

	if (!sf_getfloat("t0",&t0) && 
	    !sf_histfloat(in,"t0_logwarp",&t0)) sf_error("Need t0=");

	o1 = t0*expf(o2);
	d1 = o2+(n2-1)*d2;
	d1 = (t0*expf(d1)-o1)/(n1-1);
	
	sf_putint(out,"n1",n1);
	sf_putfloat(out,"d1",d1);
	sf_putfloat(out,"o1",o1);
    } else {
	if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
	if (!sf_getint("pad",&n2)) n2=n1; /* output time samples */

	if (!sf_histfloat(in,"d1",&d1)) d1=1.;
	if (!sf_histfloat(in,"o1",&o1)) o1=0.;

	if (!sf_getfloat("t0",&t0)) t0=o1;
	sf_putfloat(out,"t0_logwarp",t0);

	o2 = logf(o1/t0);
	d2 = o1+(n1-1)*d1;
	d2 = (logf(d2/t0) - o2)/(n2-1);

	sf_putint(out,"n1",n2);
	sf_putfloat(out,"d1",d2);
	sf_putfloat(out,"o1",o2);

	sf_putint(out,"n1_t2warp",n1);
    } 

    n3 = sf_leftsize(in,1);

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

    trace = sf_floatalloc(n2);
    t2 = sf_floatalloc(n2);
    trace2 = sf_floatalloc(n1);

    mo = stretch4_init (n1, o1, d1, n2, eps);

    for (i2=0; i2 < n2; i2++) {
	t = o2+i2*d2;
	t2[i2] = t0*expf(t);
    }    

    stretch4_define (mo,t2);
    
    for (i3=0; i3 < n3; i3++) {
	if (inv) {
	    sf_floatread(trace,n2,in);
	    stretch4_apply (false,mo,trace,trace2);
	    sf_floatwrite (trace2,n1,out);
	} else {
	    sf_floatread(trace2,n1,in);
	    stretch4_invert (false,mo,trace,trace2);
	    sf_floatwrite (trace,n2,out);
	}
    }

    exit(0);
}