Exemplo n.º 1
0
int main (int argc, char* argv[]) {
    int i, n, nbuf;
    float upper, lower, upperval, lowerval, *trace=NULL;
    sf_file in, out;

    /* Initialize RSF */
    sf_init (argc, argv);
    /* standard input */
    in = sf_input ("in");
    /* standard output */
    out = sf_output ("out");

    /* check that the input is float */
    if (SF_FLOAT != sf_gettype (in)) sf_error ("Need float input");

    n = sf_filesize (in);

    if (!sf_getfloat ("upper", &upper)) upper = +FLT_MAX;
    /* upper range limit */
    if (!sf_getfloat ("lower", &lower)) lower = -FLT_MAX;
    /* lower range limit */
    if (!sf_getfloat ("upperval", &upperval)) upperval = +FLT_MAX;
    /* upper range value */
    if (!sf_getfloat ("lowerval", &lowerval)) lowerval = -FLT_MAX;
    /* lower range value */

    nbuf = BUFSIZ/sizeof(float);
    trace = sf_floatalloc (nbuf);

    /* loop over traces */
    for (n = sf_filesize (in); n > 0; n -= nbuf) {
        if (nbuf > n) nbuf=n;

        sf_floatread (trace, nbuf, in);

        for (i = 0; i < nbuf; i++) {
            if (upper > lower) {
                if (trace[i] > upper)
                    trace[i] = upperval;
                if (trace[i] < lower)
                    trace[i] = lowerval;
            } else {
                if (trace[i] > upper &&
                        trace[i] < lower)
                    trace[i] = lowerval;
            }
        }

        sf_floatwrite (trace, nbuf, out);
    }

    return 0;
}
Exemplo n.º 2
0
int main(int argc, char* argv[])
{
    int n1, n2, i2, esize;
    off_t pos;
    struct skey *sorted;
    float *unsorted;
    char *trace, *header;
    sf_file in, head, out;

    sf_init (argc,argv);
    in = sf_input ("in");
    out = sf_output ("out");
 
    header = sf_getstring("head");
    /* header file */
    if (NULL == header) { 
	header = sf_histstring(in,"head");
	if (NULL == header) sf_error("Need head=");
    }

    head = sf_input(header);
    if (SF_FLOAT != sf_gettype(head))
	sf_error("Need float header");
    n2 = sf_filesize(head);
 
    unsorted = sf_floatalloc(n2);
    sorted = (struct skey*) sf_alloc(n2,sizeof(struct skey));
    
    sf_floatread(unsorted,n2,head);
    for (i2 = 0; i2 < n2; i2++) {
	sorted[i2].key = unsorted[i2];
	sorted[i2].pos = i2;
    }
    free (unsorted);
    sf_fileclose(head);

    qsort(sorted,n2,sizeof(struct skey),key_compare);
 
    if (!sf_histint(in,"n1",&n1)) n1=1;
    esize = sf_esize(in);
    n1 *= esize;

    trace = sf_charalloc(n1);

    sf_unpipe(in,((off_t) n1)*((off_t) n2));
    sf_fileflush(out,in);
    sf_setform(in,SF_NATIVE);
    sf_setform(out,SF_NATIVE);

    pos = sf_tell(in);
    for (i2=0; i2<n2; i2++) {
	sf_seek(in,pos+(sorted[i2].pos)*n1,SEEK_SET);
	sf_charread(trace,n1,in);
	sf_charwrite(trace,n1,out);
    }


    exit(0);
}
Exemplo n.º 3
0
int main(int argc, char* argv[])
{
    off_t nsiz;
    int nbuf, k, order;
    char *type;
    float *fbuf;
    sf_file inp, out;
    double (*func)(double);

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

    if (SF_FLOAT != sf_gettype(inp)) sf_error("Need float input");
    
    if (NULL == (type = sf_getstring("type"))) type="J";
    if (!sf_getint("order",&order)) order=0;

    switch(type[0]) {
	case 'i':
	case 'I':
	    switch (order) {
		case 0:
		    func = bessel_I0;
		    break;
		case 1:
		    func = bessel_I1; 
		    break;
		default:
		    sf_error("Order %d for type %s is not implemented",order,type);
	    }
	    break;
	default:
	    sf_error("Type %s is not implemented",type);
	    break;
    }

    nbuf = BUFSIZ/sizeof(float);
    fbuf = sf_floatalloc(nbuf);

    for (nsiz = sf_filesize(inp); nsiz > 0; nsiz -= nbuf) {
	if (nbuf > nsiz) nbuf = nsiz;
	sf_floatread(fbuf,nbuf,inp);
	for (k=0; k < nbuf; k++) {
	    fbuf[k] = func(fbuf[k]);
	}
	sf_floatwrite(fbuf,nbuf,out);
    }

    exit(0);
}
Exemplo n.º 4
0
int main(int argc, char* argv[])
{
    bool nan;
    int i, n, nbuf;
    float clip, value, *trace;
    sf_file in=NULL, out=NULL; /* Input and output files */

    /* Initialize RSF */
    sf_init(argc,argv);
    /* standard input */
    in = sf_input("in");
    /* standard output */
    out = sf_output("out");

    /* check that the input is float */
    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");

    if (!sf_getfloat("clip",&clip)) sf_error("Need clip=");
    /* clip value */

    if (!sf_getfloat("value",&value)) value=clip;
    /* replacement value */

    /* allocate floating point buffer */
    nbuf = sf_bufsiz(in)/sizeof(float);
    trace = sf_floatalloc (nbuf);

    /* process buffers */
    for (n = sf_filesize(in); n > 0; n -= nbuf) {
	if (nbuf > n) nbuf=n;

	sf_floatread(trace,nbuf,in);

	for (i=0; i < nbuf; i++) {
	    nan = (bool) !isfinite(trace[i]);

	    if (nan) trace[i] = SF_SIG(trace[i])*value; 
	    else if (trace[i] >  clip) trace[i]= value;
	    else if (trace[i] < -clip) trace[i]=-value;
	}

	sf_floatwrite(trace,nbuf,out);
    }


    exit(0);
}
Exemplo n.º 5
0
int main(int argc, char* argv[])
{
    int nt, ncmp, ncdp, nh, nh2, nm, nd, memsize, niter, reg, ix, ih, i3, i2, i1, iter, filt, nw, np;
    float t0, cmp0, cdp0, h0, dt, dcmp, dcdp, dh, apt, rho, aal, norm;
    bool verb, half, amp;
    float ***data, ***modl, **vrms, **mask, *off, *error=NULL;
    float **pp, **qq, *aa;
    char *errfile;
    sf_file in, out, vel, offset, err=NULL;
    sf_file fdip;

    int ompchunk = 1;
    int ompnth = 1;

#ifdef _OPENMP
    int ompath=1;
#endif

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

    if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1;
    /* OpenMP data chunk size */
#ifdef _OPENMP
    if(! sf_getint("ompnth",  &ompnth))     ompnth=0;
    /* OpenMP available threads */

#pragma omp parallel
    ompath=omp_get_num_threads();
    if(ompnth<1) ompnth=ompath;
    omp_set_num_threads(ompnth);
    sf_warning("using %d threads of a total of %d",ompnth,ompath);
#endif

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

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

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

    if (!sf_getbool("amp",&amp)) amp = true;
    /* if y, use amplitue factor */

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

    if (!sf_histint(in,"n2",&ncmp)) sf_error("No n2= in input");
    if (!sf_histfloat(in,"d2",&dcmp)) sf_error("No d2= in input");
    if (!sf_histfloat(in,"o2",&cmp0)) sf_error("No o2= in input");

    if (!sf_getint("ncdp",&ncdp)) ncdp = ncmp;
    if (!sf_getfloat("dcdp",&dcdp)) dcdp = dcmp;
    if (!sf_getfloat("cdp0",&cdp0)) cdp0 = cmp0;

    sf_putint(out,"n2",ncdp);
    sf_putfloat(out,"d2",dcdp);
    sf_putfloat(out,"o2",cdp0);

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

    if (NULL != sf_getstring("offset")) {
        offset = sf_input("offset");
        nh2 = sf_filesize(offset);

        if (nh2 != nh*ncmp) sf_error("Wrong dimensions in offset, it should be %d",nh*ncmp);

        off = sf_floatalloc(nh2);
        sf_floatread (off,nh2,offset);
        sf_fileclose(offset);
        if (!half) {
           for (ih = 0; ih < nh2; ih++) {
               off[ih] *= 0.5;
            }
        }
    } else {
        if (!sf_histfloat(in,"o3",&h0)) sf_error("No o3=");
        if (!sf_histfloat(in,"d3",&dh)) sf_error("No d3=");

        if (!half) dh *= 0.5,h0 *= 0.5;

        off = sf_floatalloc(nh*ncmp);
        for (ix = 0; ix < ncmp; ix++) {
            for (ih = 0; ih < nh; ih++) {
                off[ih*ncmp+ix] = h0 + ih*dh;
            }
        }
        offset = NULL;
    }

    if (!sf_getint("reg",&reg)) reg=0;
    /* regularization type */ 

    if (!sf_getfloat("antialias",&aal)) aal = 1.0;
    /* antialiasing */

    if (!sf_getfloat("apt",&apt)) apt=ncmp;
    /* migration aperture */

    if (!sf_getfloat("rho",&rho)) rho = 1.-1./nt;
    /* Leaky integration constant */

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


    nm = nt*ncdp*nh;
    nd = nt*ncmp*nh;

    vrms = sf_floatalloc2(nt,ncdp);
    mask = sf_floatalloc2(ncmp,nh);
    data = sf_floatalloc3(nt,ncmp,nh);
    modl = sf_floatalloc3(nt,ncdp,nh);

    /* read velocity file */
    sf_floatread(vrms[0],nt*ncdp,vel);
    sf_fileclose(vel);

    memsize = nm+nd+nt*ncdp+ncmp*nh;
    if (verb) sf_warning("memory needs: %f G (%f M)",4.*memsize/1024/1024/1024,4.*memsize/1024/1024);

    if (niter > 0) {
        errfile = sf_getstring("err");
        /* output file for error */
        if (NULL != errfile) {
            err = sf_output(errfile);
            sf_putint(err,"n1",niter);
            sf_putfloat(err,"d1",1);
            sf_putfloat(err,"o1",1);
            sf_putstring(err,"label1","Iteration Number");
            sf_putstring(err,"label2","Relative Squared Error");
            sf_putint(err,"n2",1);
            sf_putint(err,"n3",1);
        }
        error = sf_floatalloc(niter);
    }

    sf_floatread(data[0][0],nd,in);

    for (i3=0; i3 < nh; i3++) {
        for (i2=0; i2 < ncmp; i2++) {
            mask[i3][i2]=cblas_sdot(nt,data[i3][i2],1,data[i3][i2],1);
        }
    }

    tkirmig_init(ompnth,ompchunk,nt,dt,t0,ncmp,dcmp,cmp0,ncdp,dcdp,cdp0,nh,dh,h0,apt,aal,rho,vrms,off,mask,amp,verb);

    sf_cdstep_init();

    if (verb) sf_warning("Iteration begin...");

    if (reg == 0)
       sf_solver(tkirmig_lop,sf_cdstep,nm,nd,modl[0][0],data[0][0],
                 niter,"nmem",0,"nfreq",niter,"err",error,"end");

    else if (reg == 1) {
       filt=2;
       aa=sf_floatalloc(filt);
       aa[0]=1.;
       aa[1]=-1.;
       tcaih_init(filt,aa,nt,ncdp,nh);
       sf_solver_reg(tkirmig_lop,sf_cdstep,tcaih_lop,nm+filt*nt*ncdp,nm,nd,
                    modl[0][0],data[0][0],niter,0.01,"nmem",0,"nfreq",niter,
                    "err",error,"end");
    }
    else if (reg == 2) {
       sf_causinth_init(nt,ncdp,nh);
       sf_solver_prec(tkirmig_lop,sf_cdstep,sf_causinth_lop,nm,nm,nd,
                      modl[0][0],data[0][0],niter,0.01,"nmem",0,"nfreq",niter,
                      "err",error,"end");
    }
    else if (reg == 3) {
       sf_triangleh_init(3,nt,ncdp,nh);
       sf_solver_prec(tkirmig_lop,sf_cdstep,sf_triangleh_lop,nm,nm,nd,
                      modl[0][0],data[0][0],niter,0.01,"nmem",0,"nfreq",niter,
                      "err",error,"end");
    }

    else if (reg == 4) {
       sf_warning("pwd constraints along t-x plane and smoothing along offset axis");
       if (!sf_getstring("fdip")) sf_error("Need input dip file!");
       if (!sf_getint("nw",&nw)) nw=3;
       fdip = sf_input("fdip");

       if (!sf_histint(fdip,"n3",&np)) np=1;
       sf_warning("np=%d",np);
       pp = sf_floatalloc2(nt,ncdp);

       if (np > 1) {
          qq = sf_floatalloc2(nt,ncdp);
       } else {
          qq = NULL;
       }

       if (NULL != qq) {
          predicth2_init(nt,ncdp,nh,0.1,nw,pp,qq);
       } else {
          predicth_init(nt,ncdp,nh,0.1,nw,1,false);
          predict_set(pp);
       }

       sf_floatread(pp[0],nt*ncdp,fdip);

       if (NULL != qq) {
          sf_floatread(qq[0],nt*ncdp,fdip);
          sf_solver_prec(tkirmig_lop,sf_cdstep,predicth2_lop,nm,nm,nd,
                      modl[0][0],data[0][0],niter,0.01,"nmem",0,"nfreq",niter,
                      "err",error,"end");
         predict2_close();
       } else {
         sf_solver_prec(tkirmig_lop,sf_cdstep,predicth_lop,nm,nm,nd,
                      modl[0][0],data[0][0],niter,0.01,"nmem",0,"nfreq",niter,
                      "err",error,"end");
         predict_close();
      }
    }

    sf_cdstep_close();
    sf_floatwrite(modl[0][0],nm,out);

    if (NULL != err) {
       for (i3=0; i3 < nh; i3++) {
           for (i2=0; i2 < ncmp; i2++) {
               for (i1=0; i1 < nt; i1++) {
                   norm += data[i3][i2][i1]*data[i3][i2][i1];
               }
           }
        }
        
        for (iter=0; iter < niter; iter++) error[iter] /=norm;
        sf_floatwrite(error,niter,err);
    }

    sf_warning("iter/niter=%d/%d, err=%f",iter,niter,error);

    exit(0);
}
Exemplo n.º 6
0
int main (int argc, char* argv[])
{
    float mean, var, range, a, b, *dat;
    size_t nbuf, nsiz;
    int seed;
    size_t i;
    bool normal, rep;
    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_getint("seed",&seed)) seed = time(NULL);
    /* random seed */
    init_genrand((unsigned long) seed);

    if (!sf_getbool("type",&normal)) normal=true;
    /* noise distribution, y: normal, n: uniform */
    if (!sf_getfloat("var",&var)) {
	/* noise variance */
	if (!sf_getfloat("range",&range)) {
	    /* noise range (default=1) */
	    a = 1.;
	} else {
	    a = normal? 2.*range/9. : 2.*range;
	}
    } else {
	a = normal? sqrtf(var): sqrtf(12*var);
    }

    if (!sf_getfloat("mean",&mean)) mean=0;
    /* noise mean */
    b = normal? mean: mean - 0.5*a;

    if (!sf_getbool("rep",&rep)) rep=false;
    /* if y, replace data with noise */

    nbuf = BUFSIZ/sizeof(float);
    dat = sf_floatalloc (nbuf);

    for (nsiz = sf_filesize(in); nsiz > 0; nsiz -= nbuf) {
	if (nbuf > nsiz) nbuf = nsiz;

	if (rep) {
	    if (normal) {
		for (i=0; i < nbuf; i++) {
		    dat[i] = a*sf_randn_one_bm() + b;
		}
	    } else {
		for (i=0; i < nbuf; i++) {
		    dat[i] = a*genrand_real1() + b;
		}
	    }
	} else {
	    sf_floatread(dat,nbuf,in);
	    
	    if (normal) {
		for (i=0; i < nbuf; i++) {
		    dat[i] += a*sf_randn_one_bm() + b;
		}
	    } else {
		for (i=0; i < nbuf; i++) {
		    dat[i] += a*genrand_real1() + b;
		}
	    }
	}

	sf_floatwrite(dat,nbuf,out);  
    }


    exit(0);
}
Exemplo n.º 7
0
int main(int argc, char* argv[])
{
    bool adj, verb;
    int nd, nm, nx, im, min, max, id, i, ix, sx;
    int **indx, *size;
    float *model, *data;
    sf_file inp, index, out;

    sf_init(argc,argv);
  
    if (!sf_getbool("adj",&adj)) adj=true;
    /* adjoint flag */
    if (!sf_getbool("verb",&verb)) verb=false;
    /* verbosity flag */

    inp = sf_input("in");
    if (SF_FLOAT != sf_gettype(inp)) 
	sf_error("Need float input");

    out = sf_output("out");

    index = sf_input("index");
    if (SF_INT != sf_gettype(index)) 
	sf_error("Need int index");

    if (!sf_histint(index,"n1",&nd)) 
	sf_error("No n1= in index");
    nm = sf_leftsize(index,1);

    if (adj) {
	if (nd != sf_filesize(inp)) 
	    sf_error("Wrong data size");
    } else {
	sf_putint(out,"n1",nd);
    }

    data = sf_floatalloc(nd);
    indx = sf_intalloc2(nd,nm);
    size = sf_intalloc(nm);

    sf_intread(indx[0],nd*nm,index);

    nx = 0;
    for (im=0; im < nm; im++) {
	min = max = indx[im][0];
	for (id=1; id < nd; id++) {
	    i = indx[im][id];
	    if (i < min) min=i;
	    if (i > max) max=i;
	}
	if (min) {
	    for (id=0; id < nd; id++) {
		indx[im][id] -= min;
	    }
	}
	size[im]=max-min+1;
	nx += size[im];
	if (verb) sf_warning("size%d=%d",im+1,size[im]);
    }

    if (adj) {
	sf_putint(out,"n1",nx);
    } else {
	if (nx != sf_filesize(inp)) 
	    sf_error("Wrong model size");
    }

    model = sf_floatalloc(nx);
    
    if (adj) {
	sf_floatread(data,nd,inp);
	for (ix=0; ix < nx; ix++) {
	    model[ix] = 0.0f;
	}
    } else {
	sf_floatread(model,nx,inp);
	for (id=0; id < nd; id++) {
	    data[id] = 0.0f;
	}
    }

    sx=0;
    for (im=0; im < nm; im++) {
	for (id=0; id < nd; id++) {
	    ix = indx[im][id]+sx;
	    
	    if (adj) {
		model[ix] += data[id];
	    } else {
		data[id] += model[ix];
	    }
	}
	sx += size[im];
    }

    if (adj) {
	sf_floatwrite(model,nx,out);
    } else {	
	sf_floatwrite(data,nd,out);
    } 
    
    exit(0);
}
Exemplo n.º 8
0
int main(int argc, char* argv[])
{
    int i, iter, niter, p[6][2], status, *mask;
    float *buf, *buf2, *wht;
    double rn, rnp, alpha, beta;
    pid_t pid[6]={1,1,1,1,1,1};
    off_t nm, nd, msiz, dsiz, pos;
    size_t nbuf, mbuf, dbuf;
    FILE *xfile, *Rfile, *gfile, *sfile, *Sfile;
    char *x, *R, *g, *s, *S, *prog;
    sf_file mod, dat, from, mwt, x0, known;  /* input */
    sf_file to, out; /* output */

    extern int fseeko(FILE *stream, off_t offset, int whence);
    extern off_t ftello (FILE *stream);

    sf_init(argc,argv);

    dat = sf_input("in");
    mod = sf_input("mod");

    if (SF_FLOAT != sf_gettype(mod) ||
	SF_FLOAT != sf_gettype(dat)) 
	sf_error("Need float type in mod and dat");

    for (i=0; i < argc-1; i++) {
	argv[i]=argv[i+1];
    }
    for (i=0; i < argc-1; i++) {	
	/* find the program to run */
	if (NULL == strchr(argv[i],'=')) {
	    /* first one without the '=' */
	    prog = argv[0];
	    argv[0] = argv[i];
	    argv[i] = prog;
	    break;
	}
    }

    argv[argc-1] = sf_charalloc(6);
    snprintf(argv[argc-1],6,"adj=X");

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

    Rfile = sf_tempfile(&R,"w+b"); 
    xfile = sf_tempfile(&x,"w+b"); 
    gfile = sf_tempfile(&g,"w+b");
    sfile = sf_tempfile(&s,"w+b");
    Sfile = sf_tempfile(&S,"w+b");

    fclose(Rfile);
    fclose(xfile);
    fclose(gfile);
    fclose(sfile);
    fclose(Sfile);

    nm = sf_filesize(mod);
    nd = sf_filesize(dat);

    /* I/O buffers */
    nbuf = BUFSIZ/sizeof(float);
    buf  = sf_floatalloc(nbuf);
    buf2 = sf_floatalloc(nbuf);

    if (NULL != sf_getstring("mwt")) {
	mwt = sf_input("mwt"); /* model weight */
	wht = sf_floatalloc(nbuf);
    } else {
	mwt = NULL;
	wht = NULL;
    }

    if (NULL != sf_getstring("known")) {
	known = sf_input("known"); /* known model mask */
	if (SF_INT != sf_gettype(known)) sf_error("Need int type in known");
	mask = sf_intalloc(nbuf);
    } else {
	known = NULL;
	mask = NULL;
    }

    if (NULL != sf_getstring("x0")) {
	x0 = sf_input("x0"); /* initial model */
    } else {
	x0 = NULL;
    }

    for (i=0; i < 6; i++) { /* make six pipes */
	if (pipe(p[i]) < 0) sf_error("pipe error:");
    }

    for (iter=0; iter < niter; iter++) {
	for (i=0; i < 6; i++) { /* fork six children */
	    if ((pid[i] = fork()) < 0) sf_error("fork error:");
	    if (0 == pid[i]) break;
	}
	
	if (0 == pid[0]) {	
	    /* feeds rr to p[0] */
	    close(p[0][0]);
	    close(STDOUT_FILENO);
	    DUP(p[0][1]);

	    to = sf_output("out");

	    if (0 == iter) {
		xfile = fopen(x,"wb");

		if (NULL == x0) {
		    for (i=0; i < nbuf; i++) { buf[i] = 0.0f; }
		}
		
		MLOOP( if (NULL != x0) sf_floatread(buf,mbuf,x0);
		       MWRITE(xfile); );
		
		fclose(xfile);

		Rfile = fopen(R,"wb");
		DLOOP( sf_floatread(buf,dbuf,dat); 
		       for (i=0; i < dbuf; i++) { buf[i] = -buf[i]; }
		       sf_floatwrite(buf,dbuf,to);
		       DWRITE (Rfile); );
Exemplo n.º 9
0
Arquivo: Mmig2.c Projeto: Seislet/src
int main(int argc, char* argv[])
{
    bool adj, half, verb, normalize;
    int nt, nx, nh, nh2, ix, ih, iy, i, nn, it, **fold, apt;
    float *trace, **image, **v, rho, **stack, *pp, *off;
    float h, x, t, h0, dh, dx, ti, tx, t0, t1, t2, dt, vi, aal, angle;
    sf_file inp, out, vel, gather, offset;

    sf_init (argc,argv);
    inp = sf_input("in");
    vel = sf_input("vel");  
    out = sf_output("out");
       
    if (!sf_getbool("adj",&adj)) adj=true;
    /* adjoint flag (y for migration, n for modeling) */

    if (!sf_getbool("normalize",&normalize)) normalize=true;
    /* normalize for the fold */
 

    if (!sf_histint(inp,"n1",&nt)) sf_error("No n1=");
    if (!sf_histint(inp,"n2",&nx)) sf_error("No n2=");

    if (!sf_histfloat(inp,"o1",&t0)) sf_error("No o1=");
    if (!sf_histfloat(inp,"d1",&dt)) sf_error("No d1=");
    if (!sf_histfloat(inp,"d2",&dx)) sf_error("No d2=");

    if (adj) {
	if (!sf_histint(inp,"n3",&nh)) sf_error("No n3=");
       
	sf_putint(out,"n3",1);
    } else {
	if (!sf_getint("nh",&nh)) sf_error("Need nh=");
	/* number of offsets (for modeling) */
	
	sf_putint(out,"n3",nh);
    }	

    if (NULL != sf_getstring("gather")) {
	gather = sf_output("gather");
    } else {
	gather = NULL;
    }

    if (!sf_getfloat("antialias",&aal)) aal = 1.0;
    /* antialiasing */

    if (!sf_getint("apt",&apt)) apt = nx;
    /* integral aperture */

    if (!sf_getfloat("angle",&angle)) angle = 90.0;
    /* angle aperture */

    angle = fabsf(tanf(angle*SF_PI/180.0));

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

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

    if (!sf_getfloat("rho",&rho)) rho = 1.-1./nt;
    /* Leaky integration constant */

    if (NULL != sf_getstring("offset")) {
	offset = sf_input("offset");
	nh2 = sf_filesize(offset);

	if (nh2 != nh*nx) sf_error("Wrong dimensions in offset");

	off = sf_floatalloc(nh2);	
	sf_floatread (off,nh2,offset);
	sf_fileclose(offset);
    } else {
	if (adj) {
	    if (!sf_histfloat(inp,"o3",&h0)) sf_error("No o3=");
	    if (!sf_histfloat(inp,"d3",&dh)) sf_error("No d3=");
	    sf_putfloat(out,"d3",1.);
	    sf_putfloat(out,"o3",0.);
	} else {
	    if (!sf_getfloat("dh",&dh)) sf_error("Need dh=");
	    /* offset sampling (for modeling) */
	    if (!sf_getfloat("h0",&h0)) sf_error("Need h0=");
	    /* first offset (for modeling) */
	    sf_putfloat(out,"d3",dh);
	    sf_putfloat(out,"o3",h0);
	}
	
	if (!half) dh *= 0.5;

	off = sf_floatalloc(nh*nx);
	for (ix = 0; ix < nx; ix++) {
	    for (ih = 0; ih < nh; ih++) {
		off[ih*nx+ix] = h0 + ih*dh; 
	    }
	}
	offset = NULL;
    }

    v = sf_floatalloc2(nt,nx);
    sf_floatread(v[0],nt*nx,vel);

    trace = sf_floatalloc(nt);
    image = sf_floatalloc2(nt,nx);
    stack = sf_floatalloc2(nt,nx);

    if (normalize) {
	fold = sf_intalloc2(nt,nx);
    } else {
	fold = NULL;
    }

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

    sf_halfint_init (true, nn, rho);

    if (adj) {
	for (i=0; i < nt*nx; i++) {
	    stack[0][i] = 0.;  
	}
    } else {
	sf_floatread(stack[0],nt*nx,inp); 
    }

    if (NULL != fold) {
	for (i=0; i < nt*nx; i++) {
	    fold[0][i] = 0;  
	}
    }

    for (ih=0; ih < nh; ih++) {
        if (verb) sf_warning("offset %d of %d;",ih+1,nh);

	if (adj) {
	    for (i=0; i < nt*nx; i++) {
		image[0][i] = 0.;
	    }
	} else {
	    for (iy=0; iy < nx; iy++) {
		for (it=0; it < nt; it++) {
		    image[iy][it] = stack[iy][it];
		}
	    }
	}

	if (!adj) {
	    for (iy=0; iy < nx; iy++) {
		for (it=0; it < nt; it++) {
		    pp[it] = image[iy][it];
		}
		for (it=nt; it < nn; it++) {
		    pp[it] = 0.;
		}
		sf_halfint (false, pp);
		for (it=0; it < nt; it++) {
		    image[iy][it] = pp[it];
		}
	    }
	}

	for (iy=0; iy < nx; iy++) { 
	    if (adj) {
		sf_floatread (trace,nt,inp);
		sf_doubint(true, nt,trace);
	    } else {
		for (it=0; it < nt; it++) {
		    trace[it]=0.0f;
		}
	    }

	    h = fabsf(off[ih*nx+iy]);

	    for (ix=0; ix < nx; ix++) { 
	        x = (ix-iy)*dx;
		if (SF_ABS(ix-iy) > apt) continue;

		for (it=0; it < nt; it++) {
		    t = t0 + it*dt;  
		    vi = v[ix][it];

		    if (fabsf(x) > angle*vi*t) continue;

		    /* hypot(a,b) = sqrt(a*a+b*b) */
		    t1 = hypotf(0.5*t,(x-h)/vi);
		    t2 = hypotf(0.5*t,(x+h)/vi);
		    ti = t1+t2;

		    /* tx = |dt/dx| */
		    tx = fabsf(x-h)/(vi*vi*(t1+dt))+
		         fabsf(x+h)/(vi*vi*(t2+dt));

		    pick(adj,ti,fabsf(tx*dx*aal),trace,image[ix],it,nt,dt,t0);
		} 
	    } 

	    if (!adj) {
		sf_doubint(true, nt,trace);
		sf_floatwrite (trace,nt,out);
	    }
	} 

	if (adj) {
	    for (iy=0; iy < nx; iy++) {
		for (it=0; it < nt; it++) {
		    pp[it] = image[iy][it];
		}
		for (it=nt; it < nn; it++) {
		    pp[it] = 0.;
		}
		sf_halfint (true, pp);
		for (it=0; it < nt; it++) {
		    image[iy][it] = pp[it];
		}
	    }

	    if (NULL != gather) sf_floatwrite(image[0],nt*nx,gather);

	    for (iy=0; iy < nx; iy++) {
		for (it=0; it < nt; it++) {
		    stack[iy][it] += image[iy][it];
		    if (NULL != fold && 0.!=image[iy][it]) fold[iy][it]++; 
		}
	    }
	}
    }
    if (verb) sf_warning(".");

    if (NULL != fold) {
	for (i=0; i < nt*nx; i++) {
	    stack[0][i] /= (fold[0][i]+FLT_EPSILON);  
	}
    }
    
    if (adj) sf_floatwrite(stack[0],nt*nx,out); 

    exit(0);
}
Exemplo n.º 10
0
int main(int argc, char* argv[])
{
    int i, i2, n1, nbuf, **buf, *buf1, nk;
    float o1, d1;
    off_t n2, nleft;
    const char *key;
    char *arg;
    sf_file in, keys[SF_MAXKEYS], out, tfile;

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

    if (!sf_histint(in,"n1",&n1) &&
	!sf_getint("n1",&n1)) sf_error("Need n1=");
    /* number of samples in a trace */
    if (!sf_histfloat(in,"d1",&d1) &&
	!sf_getfloat("d1",&d1)) sf_error("Need d1=");
    /* trace sampling */
    if (!sf_histfloat(in,"o1",&o1) &&
	!sf_getfloat("o1",&o1)) o1=0;
    /* trace origin */

    n2 = sf_leftsize(in,1);

    nbuf = BUFSIZ/sizeof(int);

    if (NULL != sf_getstring("tfile")) {
	tfile = sf_input("tfile"); /* trace header file */
	if (SF_INT != sf_gettype(tfile))
	    sf_error("Need integer data in tfile");
	if (!sf_histint(tfile,"n1",&nk) || (SF_NKEYS > nk))
	    sf_error ("Need at least n1=%d keys in tfile",SF_NKEYS);
	if (nk*n2 != sf_filesize(tfile))
	    sf_error ("Wrong number of traces in tfile");
    } else {
	tfile = NULL;
	nk = SF_NKEYS;
    }

    sf_putint(out,"n1",nk);
    sf_settype(out,SF_INT);

    if (NULL != tfile) sf_fileflush(out,tfile);

    buf = sf_intalloc2(nk,nbuf);
    buf1 = sf_intalloc(nbuf);

    segy_init(nk,tfile);

    for (i=0; i < nk; i++) {
	key = segykeyword(i);
	if (NULL != (arg = sf_getstring(key))) {
	    keys[i] = sf_input(key);
	    if (SF_INT != sf_gettype(keys[i]))
		sf_error("Need integer data in file \"%s\"",arg); 
	    if (n2 != sf_filesize(keys[i])) 
		sf_error("Need filesize=%lld in file \"%s\"",n2,arg); 
	    free(arg);
	} else {
	    keys[i] = NULL;
	    for (i2=0; i2 < nbuf; i2++) {
		buf[i2][i] = 0;
	    }
	}
    }

    for (nleft=n2; nleft > 0; nleft -= nbuf) {
	if (nbuf > nleft) nbuf = nleft;

	/* read from initial trace header file */
	if (NULL != tfile) sf_intread(buf[0],nk*nbuf,tfile);

	for (i=0; i < nk; i++) {
	    key = segykeyword(i);

	    if (NULL != keys[i]) {
		sf_intread(buf1,nbuf,keys[i]);
		for (i2=0; i2 < nbuf; i2++) {
		    buf[i2][i] = buf1[i2];
		}
	    } else { /* change ns, dt, and delrt */
		if (0==strcmp(key,"ns")) {
		    for (i2=0; i2 < nbuf; i2++) {
			buf[i2][i] = n1;
		    }
		} else if (0==strcmp(key,"dt")) {
		    for (i2=0; i2 < nbuf; i2++) {
			buf[i2][i] = (int) (d1*1000000. + 0.5);
		    }
		} else if (0==strcmp(key,"delrt") && o1 != 0) {
		    keys[i] = NULL;
		    for (i2=0; i2 < nbuf; i2++) {
			buf[i2][i] = (o1>0)? (int) (o1*1000. + 0.5): (int) (o1*1000. - 0.5);
		    }
		}
	    }
	}
	
	sf_intwrite(buf[0],nk*nbuf,out);
    }

    free(buf1);
    free(*buf); free(buf);

    exit(0);
}
Exemplo n.º 11
0
int main(int argc, char* argv[]) 
{
    int i, n, nbuf;
    int glob_i = 0;   /* Global counter in file */
    int   *ibuf=NULL; /* Input reading buffer */
    float *fbuf=NULL; /* Input reading buffer */
    sf_file in=NULL;
    bool float_inp=false, int_inp=false; /* Input flags */
    sf_datatype inp_type; /* Input data type */
    int i_fac;     /* Integer division factor */
    float f_fac; /* Non-integer division factor */ 
    bool istat; /* Return value of CLI read function */

    sf_init(argc,argv);
    in = sf_input("in");
    inp_type = sf_gettype(in);
    n = sf_filesize(in);

    istat = sf_getint ("i_fac",&i_fac);
    
    if (inp_type == SF_FLOAT) {
        float_inp = true;
        nbuf = BUFSIZ/sizeof(float);
        fbuf = sf_floatalloc(nbuf);
        if (!istat) {
            if (!sf_getfloat("f_fac",&f_fac)) \
                sf_error("Need either i_fac= or f_fac=");
        }
        else f_fac = i_fac;
    }
    else if (inp_type == SF_INT) {
        int_inp = true;
        nbuf = BUFSIZ/sizeof(int);
        ibuf = sf_intalloc(nbuf);
        if (!istat) sf_error("Need i_fac=");
    }
    else sf_error("Need float or int input");

    /* Duplicating boilerplate code to avoid conditionals inside loops */

    if (float_inp) {

        for (; n > 0; n -= nbuf) {

            if (nbuf > n) nbuf = n;

            sf_floatread(fbuf, nbuf, in);

            for (i=0; i < nbuf; i++) {
                glob_i++;
                if (0 != fmod(fbuf[i],f_fac)) \
                    sf_error("Mismatch at element %d", glob_i);
            }
        }
    }

    else if (int_inp) {

        for (; n > 0; n -= nbuf) {

            if (nbuf > n) nbuf = n;

            sf_intread(ibuf, nbuf, in);

            for (i=0; i < nbuf; i++) {
                glob_i++;
                if (0 != (ibuf[i] % i_fac )) \
                    sf_error("Mismatch at element %d", glob_i);
            }
        }
    }


    exit(0);
}
Exemplo n.º 12
0
int main(int argc, char* argv[])
{
    int i, n, n1;
    float *dat=NULL, *adat=NULL, t, pclip, d;
    sf_complex *cdat=NULL;
    sf_file in=NULL, out=NULL;

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

    n = sf_filesize(in);
    adat = sf_floatalloc(n);

    if (!sf_getfloat("pclip",&pclip)) sf_error("Need pclip=");
    /* percentage to clip */
    n1 = 0.5+n*(1.-0.01*pclip);
    if (n1 < 0) n1=0;
    if (n1 >= n) n1=n-1;

    if (SF_FLOAT == sf_gettype(in)) {
	dat = sf_floatalloc(n);
	sf_floatread(dat,n,in);
	for (i=0; i < n; i++) {
	    adat[i] = fabsf(dat[i]);
	}
    } else if (SF_COMPLEX == sf_gettype(in)) {
	cdat = sf_complexalloc(n);
	sf_complexread(cdat,n,in);
	for (i=0; i < n; i++) {
	    adat[i] = cabsf(cdat[i]);
	}
    } else {
	sf_error("Need float or complex input");
    }

    t = sf_quantile(n1,n,adat);

    if (NULL != dat) {
	for (i=0; i < n; i++) {
	    d = dat[i];
	    if (d < -t) {
		dat[i] = d+t;
	    } else if (d > t) {
		dat[i] = d-t;
	    } else {
		dat[i] = 0.;
	    }
	}
	sf_floatwrite(dat,n,out);
    } else {
	for (i=0; i < n; i++) {
	    d = cabsf(cdat[i]);
	    if (d < -t) {
#ifdef SF_HAS_COMPLEX_H
		cdat[i] *= (d+t)/d;
#else
		cdat[i] = sf_crmul(cdat[i],(d+t)/d);
#endif
	    } else if (d > t) {		
#ifdef SF_HAS_COMPLEX_H
		cdat[i] *= (d-t)/d;
#else
		cdat[i] = sf_crmul(cdat[i],(d-t)/d);
#endif
	    } else {
		cdat[i] = sf_cmplx(0.,0.);
	    }
	}
	sf_complexwrite(cdat,n,out);
    }

    exit(0);
}
Exemplo n.º 13
0
int main(int argc, char* argv[])
{
    int p[4][2], i, im, id, status;
    unsigned long mseed, dseed;
    off_t nm, nd, msiz, dsiz;
    size_t nbuf, mbuf, dbuf;
    float *buf;
    double dp;
    pid_t pid[6]={1,1,1,1,1,1};
    sf_file mod=NULL;
    sf_file dat=NULL;
    sf_file pip=NULL;

    sf_init(argc,argv);

    mod = sf_input("mod");
    dat = sf_input("dat");

    if (SF_FLOAT != sf_gettype(mod) ||
	SF_FLOAT != sf_gettype(dat))
	sf_error("Need float type in mod and dat");

    nm = sf_filesize(mod);
    nd = sf_filesize(dat);

    nbuf = BUFSIZ/sizeof(float);
    buf = sf_floatalloc(nbuf);

    mseed = (unsigned long) time(NULL);
    init_genrand(mseed);
    mseed = genrand_int32();
    dseed = genrand_int32();

    for (i=0; i < argc-1; i++) {
	argv[i]=argv[i+1];
    }
    argv[argc-1] = sf_charalloc(6);
    snprintf(argv[argc-1],6,"adj=X");

    for (i=0; i < 4; i++) { /* make four pipes */
	if (pipe(p[i]) < 0) sf_error("pipe error:");
    }

    for (i=0; i < 6; i++) { /* fork six children */
	if ((pid[i] = fork()) < 0) sf_error("fork error:");
	if (0 == pid[i]) break;
    }

    if (0 == pid[0]) {	
	/* makes random model and writes it to p[0] */

	close(p[0][0]);
	close(STDOUT_FILENO);
	DUP(p[0][1]);

	pip = sf_output("out");
	sf_fileflush(pip,mod);

	init_genrand(mseed);
	for (msiz=nm, mbuf=nbuf; msiz > 0; msiz -= mbuf) {
	    if (msiz < mbuf) mbuf=msiz;

	    sf_random(mbuf,buf);

	    sf_floatwrite(buf,mbuf,pip);
	}
    } 

    if (0 == pid[1]) {
	/* reads from p[0], runs the program, and writes to p[1] */

	close(p[0][1]);
	close(STDIN_FILENO);
	DUP(p[0][0]);

	close(p[1][0]);
	close(STDOUT_FILENO);
	DUP(p[1][1]);

	argv[argc-1][4]='0';
	execvp(argv[0],argv);

	_exit(1);
    }

    if (0 == pid[2]) {
	/* reads from p[1] and multiplies it with random data */
	
	close(p[1][1]);
	close(STDIN_FILENO);
	DUP(p[1][0]);

	pip = sf_input("in");

	init_genrand(dseed);
	dp = 0.;
	for (dsiz=nd, dbuf=nbuf; dsiz > 0; dsiz -= dbuf) {
	    if (dsiz < dbuf) dbuf=dsiz;

	    sf_floatread(buf,dbuf,pip);
	    for (id=0; id < dbuf; id++) {
		dp += buf[id]*genrand_real1 ();
	    }	
	}
	sf_warning(" L[m]*d=%g",dp);

	_exit(2);
    }

    if (0 == pid[3]) {	
	/* makes random data and writes it to p[2] */

	close(p[2][0]);
	close(STDOUT_FILENO);
	DUP(p[2][1]);

	pip = sf_output("out");
	sf_fileflush(pip,dat);

	init_genrand(dseed);
	for (dsiz=nd, dbuf=nbuf; dsiz > 0; dsiz -= dbuf) {
	    if (dsiz < dbuf) dbuf=dsiz;

	    sf_random(dbuf,buf);

	    sf_floatwrite(buf,dbuf,pip);
	}
    } 

    if (0 == pid[4]) {
	/* reads from p[2], runs the adjoint, and writes to p[3] */

	close(p[2][1]);
	close(STDIN_FILENO);
	DUP(p[2][0]);

	close(p[3][0]);
	close(STDOUT_FILENO);
	DUP(p[3][1]);

	argv[argc-1][4]='1';
	execvp(argv[0],argv);

	_exit(4);
    }

    if (0 == pid[5]) {
	/* reads from p[3] and multiplies it with random model */
	
	close(p[3][1]);
	close(STDIN_FILENO);
	DUP(p[3][0]);

	pip = sf_input("in");

	init_genrand(mseed);
	dp = 0.;
	for (msiz=nm, mbuf=nbuf; msiz > 0; msiz -= mbuf) {
	    if (msiz < mbuf) mbuf=msiz;

	    sf_floatread(buf,mbuf,pip);


	    for (im=0; im < mbuf; im++) {
		dp += buf[im]*genrand_real1 ();
	    }	
	}
	sf_warning("L'[d]*m=%g",dp);
	
	_exit(5);
    }

    for (i=0; i < 6; i++) {
	if (0 == pid[i]) break;
    }

    if (6==i) {
	/* parent waits */
	waitpid(pid[2],&status,0);
	waitpid(pid[5],&status,0);
    
	exit(0);
    }
}
Exemplo n.º 14
0
int main(int argc, char* argv[])
{
    int nt, nt2, nx, i1, i2, n12, i, j, nx2;
    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, * model, * pwdmodel, * outputext, * pwdmodelext;
    sf_file inp, out;
    /* PWD parameters */
    int nw, nj1;
    float *pp, *pwdata, *pwdataext;
    sf_file dip,outpwdcheck,outdipcheck;
    /* kirchhoff params */
    bool half, verb,normalize,debug;
    int nh, **fold, apt;
    float **v, rho, *off;
    float h0, dh, aal, angle;
    int ix, ih, nh2;
    sf_file vel, gather, offset;
    /* regularization weight */
    float reg;
    /* files needed for the extended operator */
    float * modelext, * dataext;
    // diffmod - diffraction image from the previous iteration
    // dipim - dip distribution in the image
    sf_file dipim; //diffmod,

    //MADAGASCAR C API
    /* initialize */
    sf_init(argc,argv);
    inp = sf_input("in");
    out = sf_output("out");
    vel = sf_input("vel");
    dip = sf_input("dip"); //initialize file with dip
    dipim = sf_input("dipim"); //initialize file with image dip
    //diffmod = sf_input("diffmod"); //diffraction model from the previous iteration

    /* get dimensions from input */
    if (!sf_histint(inp,"n1",&nt)) sf_error("No n1= in inp");
    if (!sf_histint(inp,"n2",&nx2)) sf_error("No n2= in inp");
    // half of the traces - actual data
    // half of the traces - model penalization
    nx = (int)(nx2)/2;
    sf_warning("nx=%d nx2=%d \n",nx,nx2);
    sf_warning("nx=%d nx2=%d \n",nx,nx2);
    sf_warning("nx=%d nx2=%d \n",nx,nx2);
    sf_warning("nx=%d nx2=%d \n",nx,nx2);
    sf_warning("nx=%d nx2=%d \n",nx,nx2);
    
    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");

    /* get parameters from command line */
    /* adjoint flag */
    if (!sf_getbool("adj",&adj)) adj=false;
    /* if perform derivative filtering = PWD */
    if (!sf_getbool("sm",&sm)) sm=true;
    /* if perform modelling via Kirchhoff */
    if (!sf_getbool("domod",&domod)) domod=true;
    /* get regularization parameter */
    if (!sf_getfloat("reg",&reg)) reg=0.0;

    /* debug flag */
    if (!sf_getbool("debug",&debug)){
		
		debug=false;
		outpwdcheck = NULL;
		outdipcheck = NULL;

	} else {

		outpwdcheck = sf_output("outpwd");
    		outdipcheck = sf_output("outdip");

		}
    
    /* kirchhoff parameters */
////////////////////////////////////////////////////////////////////////////////////////

    if (!sf_getbool("normalize",&normalize)) normalize=true;
    /* normalize for the fold */

    if (normalize) {
	fold = sf_intalloc2(nt,nx);
    } else {
	fold = NULL;
    }

    if (adj) {
	if (!sf_histint(inp,"n3",&nh)) sf_error("No n3=");
       
	sf_putint(out,"n3",1);
    } else {
	if (!sf_getint("nh",&nh)) sf_error("Need nh=");
	/* number of offsets (for modeling) */
	
	sf_putint(out,"n3",nh);
    }	

    if (NULL != sf_getstring("gather")) {
	gather = sf_output("gather");
    } else {
	gather = NULL;
    }

    if (!sf_getfloat("antialias",&aal)) aal = 1.0;
    /* antialiasing */

    if (!sf_getint("apt",&apt)) apt = nx;
    /* integral aperture */

    if (!sf_getfloat("angle",&angle)) angle = 90.0;
    /* angle aperture */

    angle = fabsf(tanf(angle*SF_PI/180.0));

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

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

    if (!sf_getfloat("rho",&rho)) rho = 1.-1./nt;
    /* Leaky integration constant */

    if (NULL != sf_getstring("offset")) {
	offset = sf_input("offset");
	nh2 = sf_filesize(offset);

	if (nh2 != nh*nx) sf_error("Wrong dimensions in offset");

	off = sf_floatalloc(nh2);	
	sf_floatread (off,nh2,offset);
	sf_fileclose(offset);
    } else {
	if (adj) {
	    if (!sf_histfloat(inp,"o3",&h0)) sf_error("No o3=");
	    if (!sf_histfloat(inp,"d3",&dh)) sf_error("No d3=");
	    sf_putfloat(out,"d3",1.);
	    sf_putfloat(out,"o3",0.);
	} else {
	    if (!sf_getfloat("dh",&dh)) sf_error("Need dh=");
	    /* offset sampling (for modeling) */
	    if (!sf_getfloat("h0",&h0)) sf_error("Need h0=");
	    /* first offset (for modeling) */
	    sf_putfloat(out,"d3",dh);
	    sf_putfloat(out,"o3",h0);
	}
	
	if (!half) dh *= 0.5;

	off = sf_floatalloc(nh*nx);
	for (ix = 0; ix < nx; ix++) {
	    for (ih = 0; ih < nh; ih++) {
		off[ih*nx+ix] = h0 + ih*dh; 
	    }
	}
	offset = NULL;
    }
////////////////////////////////////////////////////////////////////////////////////////       
    
    /* path-integral range */
    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 */
	
    n12 = nt2*nx;   

    /*^^^*/
    /* extended operator arrays */
    dataext = sf_floatalloc(nt*nx);    
    modelext = sf_floatalloc(nt*nx);
    pwdmodel = sf_floatalloc(nt*nx);  
    pwdmodelext = sf_floatalloc(nt*nx);
    outputext = sf_floatalloc(nt*nx);    
    
    /* chain arrays */
    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);
    

    pwdata = NULL;
    
    // allocate dip   
    if (sm){

	pp = sf_floatalloc(nt*nx); //allocate space for dip
	sf_floatread(pp,nt*nx,dip); //read dip
	pwdata = sf_floatalloc(nt*nx); //allocate space for pwd data (modelled with kichhoff operator usually)
	pwdataext = sf_floatalloc(nt*nx); //allocate space for pwd data
	
	if (!sf_getint("order",&nw)) nw=1;
        /* [1,2,3] accuracy order */
	
        if (nw < 1 || nw > 3) 
	    sf_error ("Unsupported nw=%d, choose between 1 and 3",nw);

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

	allpass32d_init(allpass_init(nw, nj1, nt,nx,1, pp)); //initialize all-pass-filter structure

	}    

    // allocating and reading velocity
    v = sf_floatalloc2(nt,nx);
    sf_floatread(v[0],nt*nx,vel);

    if(!adj) {
    
    	if(domod){// perform modelling

                /*^^^*/
		// reading data
                // first we read actual model
    		sf_floatread(model,nt*nx,inp);
                // here we read diffractivity from the
                // previous iteration (should be catenated along
		// the second axis)
                sf_floatread(modelext,nt*nx,inp);

		// modelling via mig2
		//^^^^^
		mig2_lop(false,half,verb,normalize,nt,nx,nh,fold,apt,data,v,rho,model,off,h0,dh,dx,ot,dt,aal,angle);
		mig2_lop(false,half,verb,normalize,nt,nx,nh,fold,apt,dataext,v,rho,modelext,off,h0,dh,dx,ot,dt,aal,angle);

    	} else {// just read the data
    		sf_warning("modelling is disabled");
    		
    		sf_floatread(data,nt*nx,inp);
                //sf_floatread(modelext,nt*nx,inp);

	} // internal else

	if (sm){// perform PWD

		//^^^^^
		allpass32d_lop(adj,false,nt*nx,nt*nx,data,pwdata);
                allpass32d_lop(adj,false,nt*nx,nt*nx,dataext,pwdataext);

		/*if (debug){
			sf_floatwrite(pwdata,nt*nx,outpwdcheck);
			sf_floatwrite(pp,nt*nx,outdipcheck);
		}*/
		
		//change the address
		for(i=0;i<nt*nx;i++){
			data[i]=pwdata[i];
			dataext[i]=pwdataext[i];
		}

	}//PWD flag

    } else {// adj flag
	
        /*^^^*/
    	// read data currently 2D
    	sf_floatread(data,nt*nx,inp);
        // read extended data - zero PWD over reflection	
        sf_floatread(dataext,nt*nx,inp);
        // since adj PWD applied to zero is zero
	// lets just put zeroes in the correspoding part of the output
	/*for (i1=0;i1<nt*nx;i1++) {
		dataext[i1]=0.0;		
		}*/

    	}// adj 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
	sf_warning("be4 filter");
	flatpifilt_init(nt2, nx, dt2, dx, 0.001, v_1, v_2, v_3, v_4, eps);
	sf_warning("after filter");
	
	sf_warning("pifilt_init(nt2, nx, dt2, dx, v_a, v_b, v_0, beta, eps);\n");
	
	if(adj) {
	sf_warning("be4 the chain: params %d %d %d %d",nt*nx,nt2*nx,nt2*nx,nt*nx);
	sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,adj,false,nt*nx,nt2*nx,nt2*nx,nt*nx,output,data,outputt2,datat2);

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

	sf_warning("running chain");

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

	sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,false,false,nt*nx,nt2*nx,nt2*nx,nt*nx,dataext,outputext,datat2,outputt2);

	}
	
	if(adj) {

		if (sm){
			sf_warning("performing PWD");

			//^^^^^
			allpass32d_lop(adj,false,nt*nx,nt*nx,pwdata,output);
			//allpass32d_lop(adj,false,nt*nx,nt*nx,pwdataext,outputext);

			//change the address
			for (i=0;i<nt*nx;i++){
				output[i]=pwdata[i];
				//outputext[i]=pwdataext[i];
			}
			
		}

		if (domod) {
			sf_warning("performing Kirchhoff migration");
			//^^^^^
			mig2_lop(true,half,verb,normalize,nt,nx,nh,fold,apt,output,v,rho,model,off,h0,dh,dx,ot,dt,aal,angle);
			//mig2_lop(true,half,verb,normalize,nt,nx,nh,fold,apt,outputext,v,rho,modelext,off,h0,dh,dx,ot,dt,aal,angle);
	
		} else {
			sf_warning("changing the address");
			//change the address
			for (i=0;i<nt*nx;i++){
				model[i]=output[i];
			}
		}		

	} // adj flag
	
	sf_warning("done with output");
	
        if (sm){

	// pp = sf_floatalloc(nt*nx); //allocate space for dip
	sf_floatread(pp,nt*nx,dipim); //read dipim

	// we need to re-initialize allpass filter
        // so that it operates in the image domain 
	allpass32d_init(allpass_init(nw, nj1, nt,nx,1, pp)); //initialize all-pass-filter structure

	// all other parameters are supposed to be initialized previously

		if(!adj) {

        		// perform adj pwd in the image domain on the model and diffraction model
        		allpass32d_lop(adj,false,nt*nx,nt*nx,model,pwdmodel);

			for(j=0;j<nx*nt;j++){

				output[j] = output[j] + outputext[j];
				outputext[j] = reg*pwdmodel[j];

			}
	
        	} else {

			allpass32d_lop(adj,false,nt*nx,nt*nx,pwdmodelext,dataext);

			for(j=0;j<nx*nt;j++){
				modelext[j] = model[j];
				model[j] = model[j] + reg*pwdmodelext[j]; 
			}//for

			

		}

	}

        if (!adj) {
	
		// write
	    	sf_floatwrite(output,nt*nx,out);
		// write extended output
		sf_floatwrite(outputext,nt*nx,out);
	
	} else {
	
		// write
		sf_floatwrite(model,nt*nx,out);
		// equivalent to setting reflection component to zero
		sf_floatwrite(modelext,nt*nx,out);

	}

    exit(0);
}
Exemplo n.º 15
0
int main (int argc, char* argv[])
{
    fint1 nmo;
    bool half;
    int ix,ih, nt,nx,nw, nh, noff, nmask, CDPtype, mute, *mask;
    float dt, t0, h0, dh, dy, str;
    float *trace, *off;
    mapfunc nmofunc;
    sf_file cmp, nmod, velocity, offset, msk, het;

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

    if (NULL != sf_getstring("s")) {
      	het = sf_input("s");
	nmofunc = shifted_nmo_map;	
    } else if (NULL != sf_getstring("a")) {
        het = sf_input("a");
        nmofunc = taner_nmo_map;
    } else {
	het = NULL;
	nmofunc =  nmo_map;
    } 

    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");
    nx = sf_leftsize(cmp,2);

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

    if (!sf_getint("mute",&mute)) mute=12;
    /* mute zone */

    CDPtype=1;
    off = sf_floatalloc(nh);

    if (NULL != sf_getstring("offset")) {
	offset = sf_input("offset");
	if (SF_FLOAT != sf_gettype(offset)) sf_error("Need float offset");
	noff = sf_filesize(offset);
	if (noff == nh) {
	    sf_floatread (off,nh,offset);
	} else if (noff != nh*nx) {
	    sf_error("Wrong dimensions in 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) && !sf_getint("CDPtype",&CDPtype)) {
	    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; 
	}

	noff = nh;
	offset = NULL;
    }
    
    if (NULL != sf_getstring("mask")) {
	msk = sf_input("mask");

	if (SF_INT != sf_gettype(msk)) sf_error("Need integer mask");
	nmask = sf_filesize(msk);
	mask = sf_intalloc(nh);

	if (nmask == nh) {
	    sf_intread (mask,nh,msk);
	} else if (nmask != nh*nx) {
	    sf_error("Wrong dimensions in mask");
	}
    } else {
	nmask = nh;

	msk = NULL;
	mask = NULL;
    }

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

    if (!sf_getbool("squared",&squared)) squared=false;
    /* if y, the slowness or velocity is squared */

    if (!sf_getfloat ("h0",&h0)) h0=0.;
    /* reference offset */
    if (half) h0 *= 2.;
    if (!sf_getint("extend",&nw)) nw=4;
    /* trace extension */

    trace = sf_floatalloc(nt);
    vel   = sf_floatalloc(nt);
    if (NULL != het) {
	par = sf_floatalloc(nt);
    } else {
	par = NULL;
    }

    nmo = fint1_init (nw, nt, mute);
    
    for (ix = 0; ix < nx; ix++) {
	sf_warning("CMP %d of %d;",ix+1,nx);

	sf_floatread (vel,nt,velocity);	
	if (NULL != het) sf_floatread(par,nt,het);
	if (NULL != offset && noff != nh) sf_floatread (off,nh,offset);
	if (NULL != msk && nmask != nh) sf_intread (mask,nh,msk);

	for (ih = 0; ih < nh; ih++) {
	    sf_floatread (trace,nt,cmp);
	    
	    /* skip dead traces */
	    if (NULL != msk && 0==mask[ih]) {
		sf_floatwrite (trace,nt,nmod);
		continue;
	    }
	    
	    fint1_set(nmo,trace);

	    h = off[ih];
	    if (NULL == offset) h += (dh/CDPtype)*(ix%CDPtype); 
	    if (half) h *= 2;
	    h = h*h - h0*h0;
	    
	    stretch(nmo,nmofunc,nt,dt,t0,nt,dt,t0,trace,str);
	    sf_floatwrite (trace,nt,nmod);
	}
    }
    sf_warning(".");

    exit (0);
}
Exemplo n.º 16
0
int main(int argc, char* argv[])
{
    sf_map4 mo;
    int n1, i2, n2, iw, nw, ns;
    float o1, d1, t, eps;
    float *trace, *move, *str, *amp;
    sf_file out, warp;

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

    if (!sf_getint("n1",&n1)) sf_error("Need n1="); /* time samples */
    if (!sf_getfloat("d1",&d1)) d1=1.; /* time sampling */
    if (!sf_getfloat("o1",&o1)) o1=0.; /* time origin */

    sf_shiftdim(warp, out, 1);
	
    sf_putint(out,"n1",n1);
    sf_putfloat(out,"d1",d1);
    sf_putfloat(out,"o1",o1);
    sf_putstring(out,"label1","Time");
    sf_putstring(out,"unit1","s");

    n2 = sf_filesize(warp);

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

    if (!sf_getint("nw",&nw)) nw=10;
    /* wavelet length */
    ns = 2*nw+1;

    move = sf_floatalloc(n2);
    sf_floatread(move,n2,warp);

    trace = sf_floatalloc(n1);
    str = sf_floatalloc(ns);
    amp = sf_floatalloc(ns);

    mo = sf_stretch4_init (n1, o1, d1, ns, eps);

    for (i2=0; i2 < n2; i2++) {
	t = move[i2];
	str[nw] = t;
	amp[nw] = 1.0;

	for (iw=0; iw < nw; iw++) {
	    str[iw] = t - (nw-iw)*d1;
	    amp[iw] = 0.0;

	    str[nw+iw+1] = t+iw*d1;
	    amp[nw+iw+1] = 0.0;
	}

	sf_stretch4_define (mo,str);
	sf_stretch4_apply (false,mo,amp,trace);
	sf_floatwrite (trace,n1,out);
    }

    exit(0);
}
Exemplo n.º 17
0
int main(int argc, char* argv[])
{
    int esize, shift;
    off_t size;
    size_t i, nleft, nbuf, e_size, len, bufsiz;
    sf_file real, cmplx;
    char *rbuf, *cbuf, *rformat, *cformat, *prog;

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

    if (SF_COMPLEX != sf_gettype(cmplx))
      {
	
	sf_error("wrong input type");
      }
    
    cformat = sf_histstring(cmplx,"data_format");
    len = strlen(cformat)+1;
    rformat = sf_charalloc(len);
    memcpy(rformat,cformat,len);
    strcpy(strstr(rformat,"complex"),"float");
    sf_setformat(real,rformat);

    if (!sf_histint(real,"esize",&esize)) esize=4;
    if (esize <= 0) sf_error("wrong esize=%d",esize);
    e_size = (size_t) esize;

    size = sf_filesize (cmplx);
    
    sf_fileflush(real,cmplx);
    sf_setform(real ,SF_NATIVE);
    sf_setform(cmplx,SF_NATIVE);
    
    prog = sf_getprog();
    if (       NULL != strstr(prog,"real")) {
	shift=0;
    } else if (NULL != strstr(prog,"imag")) {
	shift=esize;
    } else {
	sf_warning("neither real nor imag, assume real");
	shift=0;
    }

    bufsiz = sf_bufsiz(cmplx);
    rbuf = sf_charalloc(bufsiz);
    cbuf = sf_charalloc(2*bufsiz);
    
    for (nleft=size*e_size; nleft > 0; nleft -= nbuf) {
	nbuf = (bufsiz < nleft)? bufsiz: nleft;
	sf_charread(cbuf,2*nbuf,cmplx);
	for (i=0; i < nbuf; i += e_size) {
	    memcpy(rbuf+i,cbuf+2*i+shift,e_size);
	}
	sf_charwrite(rbuf,nbuf,real);
    }
    

    exit (0);
}
Exemplo n.º 18
0
int main(int argc, char* argv[])
{
    int i, iter, niter;
    sf_complex *buf, *buf2;
    double rn, rnp, alpha, beta;
    off_t nm, nd, msiz, dsiz, pos;
    size_t nbuf, mbuf, dbuf, len, iolen, cmdlen;
    FILE *xfile, *Rfile, *gfile, *Gfile, *sfile, *Sfile;
    char *x, *R, *g, *G, *s, *S, *prog, *cmdline, *iostring, *arg;
    sf_file mod, dat, x0;  /* input */
    sf_file Rrsf, Grsf, grsf, out; /* output */

    extern int fseeko(FILE *stream, off_t offset, int whence);
    extern off_t ftello (FILE *stream);

    sf_init(argc,argv);

    dat = sf_input("--input");
    mod = sf_input("mod");

    out = sf_output("--output");
    sf_fileflush(out,mod);
    sf_settype(out,SF_COMPLEX);

    if (SF_COMPLEX != sf_gettype(mod) ||
	SF_COMPLEX != sf_gettype(dat)) 
	sf_error("Need complex type in mod and dat");

    for (i=0; i < argc-1; i++) {
	argv[i]=argv[i+1];
    }
    for (i=0; i < argc-1; i++) {	
	/* find the program to run */
	if (NULL == strchr(argv[i],'=')) {
	    /* first one without the '=' */
	    prog = argv[0];
	    argv[0] = argv[i];
	    argv[i] = prog;
	    break;
	}
    }

    argv[argc-1] = sf_charalloc(6);
    snprintf(argv[argc-1],6,"adj=X");

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

    Rfile = sf_tempfile(&R,"w+"); 
    xfile = sf_tempfile(&x,"w+b"); 
    gfile = sf_tempfile(&g,"w+");
    Gfile = sf_tempfile(&G,"w+");
    sfile = sf_tempfile(&s,"w+b");
    Sfile = sf_tempfile(&S,"w+b");

    fclose(Rfile); Rrsf = sf_output(R); sf_readwrite(Rrsf,true); sf_fileflush(Rrsf,dat);
    fclose(xfile);
    fclose(gfile); 
    fclose(Gfile); 
    fclose(sfile);
    fclose(Sfile);

    nm = sf_filesize(mod);
    nd = sf_filesize(dat);

    /* I/O buffers */
    nbuf = BUFSIZ/sizeof(sf_complex);
    buf  = sf_complexalloc(nbuf);
    buf2 = sf_complexalloc(nbuf);

    cmdline = sf_charalloc(SF_CMDLEN);
    iostring = sf_charalloc(SF_CMDLEN);
    cmdlen = 0;
    for (i=0; i < argc; i++) {
	arg = argv[i];
	len = strlen(arg);
	if (cmdlen+len > SF_CMDLEN-2) sf_error("command line is too long");

	strncpy(cmdline+cmdlen,arg,len);
	cmdline[cmdlen+len]=' ';
	cmdlen += len+1;
    }

    if (NULL != sf_getstring("x0")) {
	x0 = sf_input("x0"); /* initial model */
    } else {
	x0 = NULL;
    }

    for (iter=0; iter < niter; iter++) {
	if (0 == iter) {
	    xfile = fopen(x,"wb");
	    
	    if (NULL == x0) {
		for (i=0; i < nbuf; i++) { buf[i] = sf_cmplx(0.0f,0.0f); }
	    }
	    
	    MLOOP( if (NULL != x0) sf_complexread(buf,mbuf,x0);
		   MWRITE(xfile); );
	    
	    fclose(xfile);
#ifdef SF_HAS_COMPLEX_H 
	    DLOOP( sf_complexread(buf,dbuf,dat); 
		   for (i=0; i < dbuf; i++) { buf[i] = -buf[i]; }
		   sf_complexwrite(buf,dbuf,Rrsf); );
Exemplo n.º 19
0
int main(int argc, char* argv[])
{
    int n123, n1, i, ik, dim, nk, nf, sf, niter, nw;
    int n[SF_MAX_DIM], w[SF_MAX_DIM], k[SF_MAX_DIM];
    int sa[SF_MAX_DIM], na[SF_MAX_DIM], sc[SF_MAX_DIM], nc[SF_MAX_DIM];
    int ma[SF_MAX_DIM], mc[SF_MAX_DIM]; 
    float *data, *wind, *sign, eps, di, dabs;
    char varname[6], *lagfile;
    sf_filter saa, naa, sbb, nbb, scc, ncc;
    sf_file dat, signal, spef, npef, slag, nlag;

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

    spef = sf_input("sfilt");
    npef = sf_input("nfilt");

    n123 = sf_filesize(dat);
    if (!sf_histint(spef,"dim",&dim)) sf_error("No dim= in sfilt");

    n1 = 1;
    for (i=0; i < dim; i++) {
	sprintf(varname,"n%d",i+1);
	if (!sf_histint(dat,varname,n+i)) 
	    sf_error("No %s= in input",varname);
	n1 *= n[i];
    }

    if (!sf_histints(spef,"w",w,dim)) sf_error("No w= in sfilt");
    if (!sf_histints(spef,"k",k,dim)) sf_error("No k= in sfilt");

    if (!sf_histints(spef,"a",sa,dim)) sf_error("No a= in sfilt");
    if (!sf_histints(npef,"a",na,dim)) sf_error("No a= in nfilt");

    if (!sf_histints(spef,"center",sc,dim)) sf_error("No center= in sfilt");
    if (!sf_histints(npef,"center",nc,dim)) sf_error("No center= in nfilt");

    nk=nw=1;
    for (i=0; i < dim; i++) {
	nw *= w[i];
	nk *= k[i];
    }

    if (!sf_histint(spef,"n1",&sf)) sf_error("No n1= in sfilt");
    if (!sf_histint(npef,"n1",&nf)) sf_error("No n1= in nfilt");

    sbb = sf_allocatehelix(sf);
    nbb = sf_allocatehelix(nf);

    if (NULL == (lagfile = sf_histstring(spef,"lag")) &&
	NULL == (lagfile = sf_getstring("slag"))) 
	sf_error("Need slag=");
    slag = sf_input(lagfile);
    if (NULL == (lagfile = sf_histstring(npef,"lag")) &&
	NULL == (lagfile = sf_getstring("nlag"))) 
	sf_error("Need nlag=");
    nlag = sf_input(lagfile);

    sf_intread(sbb->lag,sf,slag);
    sf_intread(nbb->lag,nf,nlag);

    if (!sf_getfloat("eps",&eps)) sf_error("Need eps=");
    /* regularization parameter */
    if (!sf_getint("niter",&niter)) niter=20;
    /* number of iterations */

    data = sf_floatalloc(n123);
    sign = sf_floatalloc(n123);

    sf_floatread(data,n123,dat);

    dabs = fabsf(data[0]);
    for (i=1; i < n123; i++) {
	di = fabsf(data[i]);
	if (di > dabs) dabs=di;
    }

    for (i=0; i < n123; i++) {
	data[i] /= dabs;
    }

    saa = (sf_filter) sf_alloc(nk,sizeof(*saa));
    naa = (sf_filter) sf_alloc(nk,sizeof(*naa));

    for (ik=0; ik < nk; ik++) {
	scc = saa+ik;
	ncc = naa+ik;
	scc->nh = sf;
	ncc->nh = nf;
	scc->flt = sf_floatalloc(sf);
	ncc->flt = sf_floatalloc(nf);
	scc->lag = sbb->lag;
	ncc->lag = nbb->lag;
	scc->mis = NULL;
	ncc->mis = NULL;
    }

    wind = sf_floatalloc(nw);

    for (i=0; i < dim; i++) {
	mc[i] = SF_MIN(sc[i],nc[i]);
	ma[i] = SF_MIN(sa[i],na[i]);
    }

    tent (dim, w, mc, ma, wind);
 
    for (i=0; i < n123-n1+1; i += n1) {
	signoi_init (naa, saa, niter, nw, eps, false);
	for (ik=0; ik < nk; ik++) {
	    sf_floatread((naa+ik)->flt,nf,npef);
	    sf_floatread((saa+ik)->flt,sf,spef);
	}
	patching (signoi_lop, data+i, sign+i, dim, k, n, w, wind);
    }

    sf_floatwrite (sign,n123,signal);

    exit(0);
}
Exemplo n.º 20
0
int main(int argc, char* argv[])
{
    int nt, nt2, nx, i1, i2, n12, i, j;
    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, * model;
    sf_file inp, out;
    /* PWD parameters */
    int nw, nj1;
    float *pp, *pwdata;
    sf_file dip,outpwdcheck,outdipcheck;
    /* kirchhoff params */
    bool half, verb,normalize,debug;
    int nh, **fold, apt;
    float **v, rho, *off;
    float h0, dh, aal, angle;
    int ix, ih, nh2;
    sf_file vel, gather, offset;

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

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

    /* get parameters from command line */
    /* adjoint flag */
    if (!sf_getbool("adj",&adj)) adj=false;
    /* if perform derivative filtering = PWD */
    if (!sf_getbool("sm",&sm)) sm=true;
    /* if perform modelling via Kirchhoff */
    if (!sf_getbool("domod",&domod)) domod=true;

    /* debug flag */
    if (!sf_getbool("debug",&debug)){
		
		debug=false;
		outpwdcheck = NULL;
		outdipcheck = NULL;

	} else {

		outpwdcheck = sf_output("outpwd");
    		outdipcheck = sf_output("outdip");

		}
    
    /* kirchhoff parameters */
////////////////////////////////////////////////////////////////////////////////////////

    if (!sf_getbool("normalize",&normalize)) normalize=true;
    /* normalize for the fold */

    if (normalize) {
	fold = sf_intalloc2(nt,nx);
    } else {
	fold = NULL;
    }

    if (adj) {
	if (!sf_histint(inp,"n3",&nh)) sf_error("No n3=");
       
	sf_putint(out,"n3",1);
    } else {
	if (!sf_getint("nh",&nh)) sf_error("Need nh=");
	/* number of offsets (for modeling) */
	
	sf_putint(out,"n3",nh);
    }	

    if (NULL != sf_getstring("gather")) {
	gather = sf_output("gather");
    } else {
	gather = NULL;
    }

    if (!sf_getfloat("antialias",&aal)) aal = 1.0;
    /* antialiasing */

    if (!sf_getint("apt",&apt)) apt = nx;
    /* integral aperture */

    if (!sf_getfloat("angle",&angle)) angle = 90.0;
    /* angle aperture */

    angle = fabsf(tanf(angle*SF_PI/180.0));

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

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

    if (!sf_getfloat("rho",&rho)) rho = 1.-1./nt;
    /* Leaky integration constant */

    if (NULL != sf_getstring("offset")) {
	offset = sf_input("offset");
	nh2 = sf_filesize(offset);

	if (nh2 != nh*nx) sf_error("Wrong dimensions in offset");

	off = sf_floatalloc(nh2);	
	sf_floatread (off,nh2,offset);
	sf_fileclose(offset);
    } else {
	if (adj) {
	    if (!sf_histfloat(inp,"o3",&h0)) sf_error("No o3=");
	    if (!sf_histfloat(inp,"d3",&dh)) sf_error("No d3=");
	    sf_putfloat(out,"d3",1.);
	    sf_putfloat(out,"o3",0.);
	} else {
	    if (!sf_getfloat("dh",&dh)) sf_error("Need dh=");
	    /* offset sampling (for modeling) */
	    if (!sf_getfloat("h0",&h0)) sf_error("Need h0=");
	    /* first offset (for modeling) */
	    sf_putfloat(out,"d3",dh);
	    sf_putfloat(out,"o3",h0);
	}
	
	if (!half) dh *= 0.5;

	off = sf_floatalloc(nh*nx);
	for (ix = 0; ix < nx; ix++) {
	    for (ih = 0; ih < nh; ih++) {
		off[ih*nx+ix] = h0 + ih*dh; 
	    }
	}
	offset = NULL;
    }
////////////////////////////////////////////////////////////////////////////////////////       
    
    /* path-integral range */
    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 */
	
    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);

    pwdata = NULL;
    
    // allocate dip   
    if (sm){

	pp = sf_floatalloc(nt*nx); //allocate space for dip
	sf_floatread(pp,nt*nx,dip); //read dip
	pwdata = sf_floatalloc(nt*nx); //allocate space for pwd data
	
	if (!sf_getint("order",&nw)) nw=1;
        /* [1,2,3] accuracy order */
	
        if (nw < 1 || nw > 3) 
	    sf_error ("Unsupported nw=%d, choose between 1 and 3",nw);

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

	allpass32d_init(allpass_init(nw, nj1, nt,nx,1, pp)); //initialize all-pass-filter structure

	}    

    // allocating and reading velocity
    v = sf_floatalloc2(nt,nx);
    sf_floatread(v[0],nt*nx,vel);

    if(!adj) {
    
    	if(domod){// perform modelling

		// reading data
    		sf_floatread(model,nt*nx,inp);

		// modelling via mig2
		mig2_lop(false,half,verb,normalize,nt,nx,nh,fold,apt,data,v,rho,model,off,h0,dh,dx,ot,dt,aal,angle);

    	} else {// just read the data
    		sf_warning("modelling is disabled");
    		
    		sf_floatread(data,nt*nx,inp);

	} // internal else

	if (sm){// perform PWD

		allpass32d_lop(adj,false,nt*nx,nt*nx,data,pwdata);

		if (debug){
			sf_floatwrite(pwdata,nt*nx,outpwdcheck);
			sf_floatwrite(pp,nt*nx,outdipcheck);
		}
		
		//change the address
		for(i=0;i<nt*nx;i++){
			data[i]=pwdata[i];
		}

	}//PWD flag

    } else {// adj flag
	
    	// read data currently 2D
    	sf_floatread(data,nt*nx,inp);
	
    	}// adj 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
	sf_warning("be4 filter");
	flatpifilt_init(nt2, nx, dt2, dx, 0.001, v_1, v_2, v_3, v_4, eps);
	sf_warning("after filter");
	
	sf_warning("pifilt_init(nt2, nx, dt2, dx, v_a, v_b, v_0, beta, eps);\n");
	
	if(adj) {
	sf_warning("be4 the chain: params %d %d %d %d",nt*nx,nt2*nx,nt2*nx,nt*nx);
	sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,adj,false,nt*nx,nt2*nx,nt2*nx,nt*nx,output,data,outputt2,datat2);

	sf_warning("running chain");

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

	}
	
	if(adj) {

		if (sm){
			sf_warning("performing PWD");
			allpass32d_lop(adj,false,nt*nx,nt*nx,pwdata,output);

			//change the address
			for (i=0;i<nt*nx;i++){
				output[i]=pwdata[i];
			}
			
		}

		if (domod) {
			sf_warning("performing Kirchhoff migration");
			mig2_lop(true,half,verb,normalize,nt,nx,nh,fold,apt,output,v,rho,model,off,h0,dh,dx,ot,dt,aal,angle);
		} else {
			sf_warning("changing the address");
			//change the address
			for (i=0;i<nt*nx;i++){
				model[i]=output[i];
			}
		}		

	} // adj flag
	
	sf_warning("done with output");
	
	if (!adj) {
	
		// write
	    	sf_floatwrite(output,nt*nx,out);
	
	} else {
	
		// write
		sf_floatwrite(model,nt*nx,out);

	}

    exit(0);
}
Exemplo n.º 21
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);
}
Exemplo n.º 22
0
int main (int argc, char* argv[])
{
    fint1 nmo;
    bool half, squared, slow;
    int ix,ih,it,iv, nt,nx,nw, nh, nh2, m, CDPtype, mute, *mask, nv, *fold;
    float dt, t0, h0,dh,h, dy, str, v0,dv,v;
    float **traces, *trace, *off, *stack;
    double *dstack;
    mapfunc nmofunc;
    sf_file cmp, stk, offset, msk;

    sf_init (argc,argv);
    cmp = sf_input("in");
    stk = sf_output("out");
    nmofunc =  nmo_map;
    
    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");
    nx = sf_leftsize(cmp,2);

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

    if (!sf_getint("mute",&mute)) mute=12;
    /* mute zone */

    if (!sf_getint("nv",&nv)) sf_error("Need nv=");
    /* number of velocities */
    
    if (!sf_getfloat("v0",&v0)) sf_error("Need v0=");
    /* first velocity */
    
    if (!sf_getfloat("dv",&dv)) sf_error("Need dv=");
    /* step in velocity */

    sf_putint(stk,"n2",nv);
    sf_putfloat(stk,"o2",v0);
    sf_putfloat(stk,"d2",dv);

    CDPtype=1;
    if (NULL != sf_getstring("offset")) {
	offset = sf_input("offset");
	if (SF_FLOAT != sf_gettype(offset)) sf_error("Need float offset");
	nh2 = sf_filesize(offset);
	if (nh2 != nh && nh2 != nh*nx) sf_error("Wrong dimensions in offset");

	off = sf_floatalloc(nh2);	
	sf_floatread (off,nh2,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) && !sf_getint("CDPtype",&CDPtype)) {
	    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);
	    }
	} 	    

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

	offset = NULL;
    }
    
    if (NULL != sf_getstring("mask")) {
	msk = sf_input("mask");
	if (SF_INT != sf_gettype(msk)) sf_error("Need integer mask");
	nh2 = sf_filesize(msk);
	if (nh2 != nh && nh2 != nh*nx) sf_error("Wrong dimensions in mask");
	mask = sf_intalloc(nh2);
	sf_intread (mask,nh2,msk);
	sf_fileclose(msk);
    } else {
	msk = NULL;
	mask = NULL;
    }

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

    if (!sf_getbool("squared",&squared)) squared=false;
    /* if y, the slowness or velocity is squared */

    if (!sf_getfloat ("h0",&h0)) h0=0.;
    /* reference offset */
    if (half) h0 *= 2.;
    if (!sf_getint("extend",&nw)) nw=4;
    /* trace extension */

    traces = sf_floatalloc2(nt,nh);
    trace  = sf_floatalloc(nt);
    stack  = sf_floatalloc(nt);
    fold   = sf_intalloc(nt);
    dstack = (double*) sf_alloc(nt,sizeof(double));

    nmo = fint1_init (nw, nt, mute);
    
    for (ix = 0; ix < nx; ix++) {
	sf_warning("CMP %d of %d;",ix+1,nx);
	sf_floatread (traces[0],nt*nh,cmp);

	for (iv=0; iv < nv; iv++) {
	    v = v0+iv*dv;
	    if (!squared) v *=v;

	    for (it=0; it < nt; it++) {
		dstack[it] = 0.0;
		fold[it] = 0;
	    }

	    for (ih = 0; ih < nh; ih++) {
		/* skip dead traces */
		if (NULL != msk) {
		    m = (nh2 == nh)? mask[ih] + (dh/CDPtype)*(ix%CDPtype) : 
			mask[ix*nh+ih];	
		    if (0==m) continue;
		}
		
		for (it=0; it < nt; it++) {
		    trace[it] = traces[ih][it];
		}
	    
		fint1_set(nmo,trace);

		h = (nh2 == nh)? off[ih] + (dh/CDPtype)*(ix%CDPtype) : 
		    off[ix*nh+ih];
		if (half) h *= 2;
		h = h*h - h0*h0;
		v2 = slow ? h*v : h/v;

		stretch(nmo,nmofunc,nt,dt,t0,nt,dt,t0,trace,str);

		for (it=0; it < nt; it++) {
		    if (trace[it] != 0.0f) {
			fold[it]++;
			dstack[it] += trace[it];
		    }
		}
	    }

	    for (it=0; it < nt; it++) {
		if (fold[it] > 0) {
		    stack[it] = dstack[it]/fold[it];
		} else {
		    stack[it] = 0.0f;
		}
	    }
			
	    sf_floatwrite (stack,nt,stk);
	}
    }
    sf_warning(".");

    exit (0);
}