示例#1
0
void write1di(const char *file, int *dat, int n1) {
  sf_file f = sf_output(file);
  sf_putint(f, "n1", n1); sf_putint(f, "o1", 0); sf_putint(f, "d1", 0);
  sf_putint(f, "n2", 1); 
  sf_settype(f, SF_INT);
  sf_intwrite(dat, n1, f);
}
示例#2
0
void write2di(const char *file, int **dat, int n1, int n2) {
  sf_file f = sf_output(file);
  sf_putint(f, "n1", n1); sf_putint(f, "o1", 0); sf_putint(f, "d1", 0);
  sf_putint(f, "n2", n2); sf_putint(f, "o2", 0); sf_putint(f, "d2", 0);
  sf_putint(f, "n3", 1); 
  sf_settype(f, SF_INT);
  sf_intwrite(dat[0], n1*n2, f);
}
示例#3
0
文件: Macqgeo.c 项目: filippo82/src
int main(int argc, char* argv[])
{
    int nz,nx,ny,sou_z,sou_ox,sou_oy,sou_jx,sou_jy,sou_nx,sou_ny,rec_z,rec_nx,rec_ny,npad,noff,roll;
    int dim1, dim2;
    sf_axis ad1=NULL, ad2=NULL;
    sf_file Fgeo=NULL;
    int **geo=NULL;

    sf_init(argc,argv);

    if (!sf_getint("nz",&nz)) sf_error("Need nz="); /* dimension in z */
    if (!sf_getint("nx",&nx)) sf_error("Need nx="); /* dimension in x */
    if (!sf_getint("ny",&ny)) sf_error("Need ny="); /* dimension in y */
    if (!sf_getint("sou_z", &sou_z )) sf_error("Need sou_z=" ); /* source position in depth      */
    if (!sf_getint("sou_ox",&sou_ox)) sf_error("Need sou_ox="); /* source starting location in x */
    if (!sf_getint("sou_oy",&sou_oy)) sf_error("Need sou_oy="); /* source starting location in y */
    if (!sf_getint("sou_nx",&sou_nx)) sf_error("Need sou_nx="); /* number of sources in x        */
    if (!sf_getint("sou_ny",&sou_ny)) sf_error("Need sou_ny="); /* number of sources in y        */
    if (!sf_getint("sou_jx",&sou_jx)) sou_jx = (sou_nx>1)? (nx-sou_ox)/(sou_nx-1):0; /* source interval in x */
    if (!sf_getint("sou_jy",&sou_jy)) sou_jy = (sou_ny>1)? (ny-sou_oy)/(sou_ny-1):0; /* source interval in y */
    if (!sf_getint("rec_z", &rec_z )) sf_error("Need rec_z=" ); /* receiver position in depth */
    if (!sf_getint("rec_nx",&rec_nx)) sf_error("Need rec_nx="); /* number of receivers in x   */
    if (!sf_getint("rec_ny",&rec_ny)) sf_error("Need rec_ny="); /* number of receivers in y   */
    if (!sf_getint("npad",&npad)) sf_error("Need npad="); /* computational domain padding */
    if (!sf_getint("noff",&noff)) sf_error("Need noff="); /* near offset */
    if (!sf_getint("roll",&roll)) sf_error("Need roll="); /* acquisition pattern: 0-> fixed-spread, 1-> towed-streamer to the negative */

    /* double check dimension */
    if (sou_nx > (nx-sou_ox)/sou_jx+1) {
        sou_nx = (nx-sou_ox)/sou_jx+1;
        sf_warning("Setting sou_nx to %d",sou_nx);
    }
    if (sou_ny > 1 && sou_ny > (ny-sou_oy)/sou_jy+1) {
        sou_ny = (ny-sou_oy)/sou_jy+1;
        sf_warning("Setting sou_ny to %d",sou_ny);
    }

    /* do the work */
    dim1 = 14;
    dim2 = sou_nx*sou_ny;

    ad1 = sf_maxa(dim1,0,1); sf_setlabel(ad1,"acqpar"); sf_raxa(ad1);
    ad2 = sf_maxa(dim2,0,1); sf_setlabel(ad2,"shot");   sf_raxa(ad2);

    Fgeo = sf_output("out");
    sf_settype(Fgeo,SF_INT);
    sf_oaxa(Fgeo,ad1,1);
    sf_oaxa(Fgeo,ad2,2);

    geo = sf_intalloc2(dim1,dim2);
    geogen(geo,nz,nx,ny,sou_z,sou_ox,sou_oy,sou_jx,sou_jy,sou_nx,sou_ny,rec_z,rec_nx,rec_ny,npad,noff,roll);

    sf_intwrite(geo[0],dim1*dim2,Fgeo);

    exit(0);
}
示例#4
0
文件: tahsub.c 项目: housian0724/src
void put_tah(float* trace, float* header, 
	     int n1_traces, int n1_headers, sf_file file)
/*< put tah >*/
{
  int input_record_length;
  
  sf_charwrite("tah ",4,file);

  input_record_length=sizeof(int)*(n1_traces+n1_headers);
  /*  fprintf(stderr,"sf_put_tah write input_record_length=%d\n",
      input_record_length); */

  sf_intwrite(&input_record_length,1,file);
  sf_floatwrite(trace,n1_traces,file);
  sf_floatwrite(header,n1_headers,file);
}
示例#5
0
int main(int argc, char* argv[])
{
    int i1, n1, i2, n2, nv, two, *trace;
    float o1, o2, d1, d2, x, y, **vert;
    sf_file inp, out, poly;

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

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

    if (!sf_histfloat(inp,"d1",&d1)) d1=1.;
    if (!sf_histfloat(inp,"d2",&d2)) d2=1.;
    
    if (!sf_histfloat(inp,"o1",&o1)) o1=0.;
    if (!sf_histfloat(inp,"o2",&o2)) o2=0.;

    poly = sf_input("poly"); 
    /* list of polygon vertices */

    if (SF_FLOAT != sf_gettype(poly)) sf_error("Need float type in poly");
    if (!sf_histint(poly,"n1",&two) || 2 != two)
	sf_error("Need n1=2 in poly");
    if (!sf_histint(poly,"n2",&nv))
	sf_error("No n2= in poly");
    
    trace = sf_intalloc(n1);
    vert = sf_floatalloc2(2,nv);

    sf_floatread(vert[0],2*nv,poly);

    sf_settype(out,SF_INT);

    for (i2=0; i2 < n2; i2++) {
	y = o2+i2*d2;
	for (i1=0; i1 < n1; i1++) {
	    x = o1+i1*d1;
	    trace[i1] = pnpoly(nv, vert, x, y);
	}
	sf_intwrite(trace,n1,out);
    }

    exit(0);
}
示例#6
0
int main(int argc, char* argv[])
{
    int n2, n3, i2, i3, is, **known=NULL;
    float *chance=NULL, perc;
    sf_file in=NULL, mask=NULL;

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

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

    sf_putint(mask,"n1",n2);
    sf_putint(mask,"n2",n3);
    sf_putint(mask,"n3",1);
    sf_settype(mask,SF_INT);

    if (!sf_getfloat("perc",&perc)) perc=0.75;
    /* how many shots to remove */

    known = sf_intalloc2(n2,n3);
    chance = sf_floatalloc(n2+n3);

    init_genrand(2003);
    sf_random (n2+n3,chance);

    for (i3=0; i3 < n3; i3++) { /* half-offset */
	for (i2=0; i2 < n2; i2++) { /* midpoint */
	    is = i2 - i3 + n3-1; /* shot */
	    known[i3][i2] = (chance[is] > perc);
	}
    }
    sf_intwrite (known[0],n2*n3,mask);

    exit(0);
}
示例#7
0
文件: Mnpef.c 项目: krushev36/src
int main(int argc, char* argv[])
{
    int n[SF_MAX_DIM], a[SF_MAX_DIM], center[SF_MAX_DIM], gap[SF_MAX_DIM];
    int *pch, *nh, dim, n123, nf, i, niter, nbf, nbp, id, ip, ig, np;
    int *kk, *pp;
    float *dd, eps, dabs, di;
    nfilter aa, bb;
    char varname[6], *lagfile;
    sf_file in, flt, lag, mask, patch, reg;

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

    dim = sf_filedims(in,n);

    if (NULL == (lagfile = sf_getstring("lag"))) sf_error("Need lag=");
    /* output file for filter lags */

    lag = sf_output(lagfile);
    sf_settype(lag,SF_INT);

    sf_putstring(flt,"lag",lagfile);

    sf_putints(lag,"n",n,dim);

    if (!sf_getints("a",a,dim)) sf_error("Need a=");

    if (!sf_getints("center",center,dim)) {
        for (i=0; i < dim; i++) {
            center[i] = (i+1 < dim && a[i+1] > 1)? a[i]/2: 0;
        }
    }

    if (!sf_getints("gap",gap,dim)) {
        for (i=0; i < dim; i++) {
            gap[i] = 0;
        }
    }

    n123 = 1;
    for (i=0; i < dim; i++) {
        n123 *= n[i];
    }

    dd = sf_floatalloc(n123);
    kk = sf_intalloc(n123);

    if (NULL != sf_getstring("maskin")) {
        /* optional input mask file */
        mask = sf_input("maskin");

        switch (sf_gettype(mask)) {
        case SF_INT:
            sf_intread (kk,n123,mask);
            break;
        case SF_FLOAT:
            sf_floatread (dd,n123,mask);
            for (i=0; i < n123; i++) {
                kk[i] = (dd[i] != 0.);
            }
            break;
        default:
            sf_error ("Wrong data type in maskin");
            break;
        }

        sf_fileclose (mask);
    } else {
        for (i=0; i < n123; i++) {
            kk[i] = 1;
        }
    }

    sf_floatread(dd,n123,in);

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

    random_init(2004);
    for (i=0; i < n123; i++) {
        dd[i] = dd[i]/dabs+ 100.*FLT_EPSILON*(random0()-0.5);;
    }

    pp = sf_intalloc(n123);
    if (NULL != sf_getstring("pch")) {
        patch = sf_input("pch");
        if (SF_INT != sf_gettype(patch)) sf_error("Need int pch");

        sf_intread(pp,n123,patch);

        np = pp[0];
        for (i=1; i < n123; i++) {
            if (pp[i] > np) np = pp[i];
        }

        sf_fileclose(patch);
    } else {
        np = n123;
        for (i=0; i < n123; i++) {
            pp[i] = i;
        }
    }

    aa = createnhelix(dim, n, center, gap, a, pp);
    free (pp);

    nf = aa->hlx[0]->nh;
    nfind_mask(n123, kk, aa);

    if(!sf_getint("niter",&niter)) niter=100;
    /* number of iterations */
    if (!sf_getfloat("epsilon",&eps)) eps=0.01;
    /* regularization parameter */

    sf_putint(flt,"n1",nf);
    sf_putint(flt,"n2",np);

    sf_putint(lag,"n1",nf);
    sf_putint(lag,"n2",np);

    for (i=2; i < dim; i++) {
        sprintf(varname,"n%d",i+1);
        sf_putint(flt,varname,1);
        sf_putint(lag,varname,1);
    }

    for (ip=0; ip < np; ip++) {
        sf_intwrite(aa->hlx[ip]->lag,nf,lag);
    }
    sf_fileclose(lag);

    if (NULL != sf_getstring("maskout")) {
        /* optional output mask file */
        mask = sf_output("maskout");

        for (i=0; i < n123; i++) {
            kk[i] = aa->mis[i]? 0.: 1.;
        }

        sf_settype(mask,SF_INT);
        sf_intwrite (kk,n123,mask);
    }

    reg = sf_input("filt");
    if (!sf_histint(reg,"n1",&nbf)) sf_error("No n1= in filt");
    if (!sf_histint(reg,"n2",&nbp)) sf_error("No n2= in filt");

    if (NULL != sf_getstring("filt_pch")) {
        patch = sf_input("filt_pch");
        if (SF_INT != sf_gettype(patch)) sf_error("Need int filt_pch");


        pp = sf_intalloc(np);
        sf_intread(pp,np,patch);
    } else {
        if (nbp != np) sf_error ("Wrong filter size: %d != %d",nbp,np);
        pp = NULL;
    }

    pch = sf_intalloc(nf*np);
    nh = sf_intalloc(nbp);

    for (i=0; i < nbp; i++) {
        nh[i] = nbf;
    }

    for (id=ig=0; ig < nf; ig++) {
        for (ip=0; ip < np; ip++, id++) {
            pch[id] = (NULL != pp)? pp[ip]: ip;
        }
    }

    bb = nallocate (nbp, nf*np, nh, pch);

    if (NULL == (lagfile = sf_getstring("filt_lag")) &&
            NULL == (lagfile = sf_histstring(reg,"lag")))
        sf_error("Need filt_lag=");
    /* input file for double-helix filter lags */

    lag = sf_input(lagfile);
    if (SF_INT != sf_gettype(lag)) sf_error("Need int filt_lag");

    for (ip=0; ip < nbp; ip++) {
        sf_intread (kk,nbf,lag);
        for (i=0; i < nbf; i++) {
            bb->hlx[ip]->lag[i] = kk[i]*nf;
        }
    }

    for (ip=0; ip < nbp; ip++) {
        sf_floatread (bb->hlx[ip]->flt,nbf,reg);
    }

    nfind_pef (n123, dd, aa, bb, niter, eps, nf);

    for (ip=0; ip < np; ip++) {
        sf_floatwrite (aa->hlx[ip]->flt,nf,flt);
    }


    exit(0);
}
示例#8
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);
}
示例#9
0
文件: Mcanny.c 项目: 1014511134/src
int main(int argc, char* argv[])
{
    int n1, n2, n3, i1, i2, i3, j1, j2, k1, k2, nedge, nold, nmin, nmax, n12;
    int **edge;
    float **pp, **ww, **w1, **w2, g1, g2, w, min, max;
    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_histint(in,"n2",&n2)) sf_error("No n2= in input");
    n3 = sf_leftsize(in,2);

    if (!sf_getfloat("min",&min)) min=5.0;
    /* minimum threshold */
    if (!sf_getfloat("max",&max)) max=95.0;
    /* maximum threshold */

    n12 = n1*n2;
    nmin = min*0.01*n12;
    if (nmin < 0) nmin=0;
    if (nmin >= n12) nmin=n12-1;
    nmax = max*0.01*n12;
    if (nmax < 0) nmax=0;
    if (nmax >= n12) nmax=n12-1;

    pp = sf_floatalloc2(n1,n2);
    w1 = sf_floatalloc2(n1,n2);
    w2 = sf_floatalloc2(n1,n2);
    ww = sf_floatalloc2(n1,n2);
    edge = sf_intalloc2(n1,n2);

    sf_settype(out,SF_INT);

    for (i3=0; i3 < n3; i3++) {
	sf_floatread(pp[0],n12,in);
	/* gradient computation */
	sf_sobel(n1,n2,pp,w1,w2);

	for (i2=0; i2 < n2; i2++) {
	    for (i1=0; i1 < n1; i1++) {
		/* gradient norm */
		g1 = w1[i2][i1];
		g2 = w2[i2][i1];
		ww[i2][i1] = g1*g1+g2*g2;
	    }
	}
	/* edge thinning */
	for (i2=0; i2 < n2; i2++) {
	    for (i1=0; i1 < n1; i1++) {
		g1 = w1[i2][i1];
		g2 = w2[i2][i1];
		if (fabsf(g1) > fabsf(g2)) {
		    j1=1;
		    if (g2/g1 > 0.5) { 
			j2=1;
		    } else if (g2/g1 < - 0.5) {
			j2=-1; 
		    } else {
			j2=0;
		    }
		} else if (fabsf(g2) > fabsf(g1)) {
		    j2=1;
		    if (g1/g2 > 0.5) { 
			j1=1;
		    } else if (g1/g2 < - 0.5) {
			j1=-1; 
		    } else {
			j1=0;
		    }
		} else {
		    j1=0;
		    j2=0;
		}
		k1 = i1+j1; if (j1 && (k1 < 0 || k1 >= n1)) k1=i1;
		k2 = i2+j2; if (j2 && (k2 < 0 || k2 >= n2)) k2=i2;
		if (ww[i2][i1] <= ww[k2][k1]) {
		    pp[i2][i1] = 0.;
		    continue;
		} 
		k1 = i1-j1; if (k1 < 0 || k1 >= n1) k1=i1;
		k2 = i2-j2; if (k2 < 0 || k2 >= n2) k2=i2;
		if (ww[i2][i1] <= ww[k2][k1]) {
		    pp[i2][i1] = 0.;
		    continue;
		} 
		pp[i2][i1] = ww[i2][i1];
	    }
	}
	/* edge selection */
	max = sf_quantile(nmax,n12,ww[0]);
	min = sf_quantile(nmin,n12,ww[0]);

	nedge=0;
	for (i2=0; i2 < n2; i2++) {
	    for (i1=0; i1 < n1; i1++) {
		w = pp[i2][i1];
		if (w > max) {
		    edge[i2][i1] = SF_IN;
		    nedge++;
		} else if (w < min) {
		    edge[i2][i1] = SF_OUT;
		} else {
		    edge[i2][i1] = SF_FRONT;
		}
	    }
	}

	nold=0;
	while (nedge != nold) {
	    nold = nedge;
	    for (i2=0; i2 < n2; i2++) {
		for (i1=0; i1 < n1; i1++) {
		    if (SF_FRONT == edge[i2][i1]) {
			if (i2 > 0) {
			    if (SF_IN == edge[i2-1][i1] || 
				(i1 > 0 && SF_IN == edge[i2-1][i1-1]) ||
				(i1 < n1-1 && SF_IN == edge[i2-1][i1+1])) {
				edge[i2][i1] = SF_IN;
				nedge++;
				continue;
			    }
			}
			if (i2 < n2-1) {
			    if (SF_IN == edge[i2+1][i1] || 
				(i1 > 0 && SF_IN == edge[i2+1][i1-1]) ||
				(i1 < n1-1 && SF_IN == edge[i2+1][i1+1])) {
				edge[i2][i1] = SF_IN;
				nedge++;
				continue;
			    }
			}
			if ((i1 > 0 && SF_IN == edge[i2][i1-1]) ||
			    (i1 < n1-1 && SF_IN == edge[i2][i1+1])) {
			    edge[i2][i1] = SF_IN;
			    nedge++;
			    continue;
			}
		    }
		}
	    }
	}
	for (i2=0; i2 < n2; i2++) {
	    for (i1=0; i1 < n1; i1++) {
		if (SF_FRONT == edge[i2][i1]) edge[i2][i1] = SF_OUT;
	    }
	}
	
	sf_intwrite(edge[0],n12,out);
    }


    exit(0);
}
示例#10
0
文件: Mtrace2.c 项目: 1014511134/src
int main(int argc, char* argv[])
{
    int nx, na, na2, ia, nz, order, maxsplit, ix, iz, *siz;
    float **place, *slow, **out, dx,dz, x0,z0, x[2];
    float max1, min1, max2, min2;
    bool isvel, lsint;
    agrid grd;
    sf_file vel, outp, size, grid;
        
    sf_init (argc,argv);

    /* get 2-D grid parameters */
    vel = sf_input("in");
    if (!sf_histint(vel,"n1",&nz)) sf_error("No n1= in input");
    if (!sf_histint(vel,"n2",&nx)) sf_error("No n2= in input");
    if (!sf_histfloat(vel,"d1",&dz)) sf_error("No d1= in input");
    if (!sf_histfloat(vel,"d2",&dx)) sf_error("No d2= in input");
    if (!sf_histfloat(vel,"o1",&z0)) z0=0.;
    if (!sf_histfloat(vel,"o2",&x0)) x0=0.;

    outp = sf_output("out");
    
    sf_putint(outp,"n4",nz);
    sf_putfloat(outp,"d4",dz);
    sf_putfloat(outp,"o4",z0);

    sf_putint(outp,"n3",nx);
    sf_putfloat(outp,"d3",dx);
    sf_putfloat(outp,"o3",x0);

    if (!sf_getint("na",&na)) na=60;
    /* number of angles */
    if (!sf_getfloat("da",&da)) da=3.1;
    /* angle increment (in degrees) */
    if (!sf_getfloat("a0",&a0)) a0=-90.;
    /* initial angle (in degrees) */

    if (!sf_getint("maxsplit",&maxsplit)) maxsplit=10;
    /* maximum splitting for adaptive grid */

    if (!sf_getfloat("minx",&min1)) min1=0.5*dx;
    /* parameters for adaptive grid */
    if (!sf_getfloat("maxx",&max1)) max1=2.*dx;
    if (!sf_getfloat("mina",&min2)) min2=0.5*da;
    if (!sf_getfloat("maxa",&max2)) max2=2.*da;

    sf_putint(outp,"n2",na);
    sf_putfloat(outp,"d2",da);
    sf_putfloat(outp,"o2",a0);

    da *= (SF_PI/180.);
    a0 *= (SF_PI/180.);

    sf_putint(outp,"n1",5);

    size = sf_output("size");
    sf_putint(size,"n1",nx);
    sf_putint(size,"n2",nz);
    sf_settype(size,SF_INT);

    grid = sf_output("grid");

    /* additional parameters */
    if(!sf_getbool("vel",&isvel)) isvel=true;
    /* y: velocity, n: slowness */
    if(!sf_getint("order",&order)) order=3;
    /* velocity interpolation order */
    if (!sf_getbool("lsint",&lsint)) lsint=false;
    /* if use least-squares interpolation */

    slow  = sf_floatalloc(nz*nx);
    place = sf_floatalloc2(5,na);
    siz  = sf_intalloc(nx);

    sf_floatread(slow,nx*nz,vel);

    if (isvel) {
      for(ix = 0; ix < nx*nz; ix++){
	slow[ix] = 1./slow[ix];
      }
    }

    ct = sf_celltrace_init (lsint, order, nz*nx, nz, nx, dz, dx, z0, x0, slow);
    free (slow);

    grd = agrid_init (na, 5, maxsplit);
    agrid_set (grd,place);

    for (iz = 0; iz < nz; iz++) {
	x[0] = z0+iz*dz;

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

	    fill_grid(grd,min1,max1,min2,max2,(void*) x, raytrace);
	
	    na2 = grid_size (grd);
	    out = write_grid (grd);

	    siz[ix] = na2;
	
	    for (ia=0; ia < na2; ia++) {
	      if (ia < na)
		  sf_floatwrite (place[ia], 5, outp);

	      sf_floatwrite(out[ia], 5, grid);
	    }
	    free (out);
	}
	
	sf_intwrite (siz,nx,size);
    }
    sf_warning(".");

    exit (0);
}
示例#11
0
int main (int argc, char* argv[])
{
	bool verb;
	float o1,o2,d1,d2;	
	int n1,	n2,n3, shift;
	int i2,i3;	
	char *unit1,*label1;
	float *column=NULL,*max=NULL; 
	int *index=NULL;	
	sf_file in=NULL,out=NULL,max_val=NULL;
	
	sf_init (argc,argv);
	
	in=sf_input("in");
	out=sf_output("out");
	sf_settype (out, SF_INT);
	if (NULL != sf_getstring("max_val")) max_val=sf_output("max_val");
	
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"o1",&o1)) sf_error("No o1= in input");
	
	label1=sf_charalloc(100);	
	unit1=sf_charalloc(100);	

	label1=sf_histstring(in,"label2");
	unit1=sf_histstring(in,"unit2");

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

	if (!sf_getint("shift",&shift)) shift=0;
	/* shift */
	if (!sf_getbool("verb",&verb)) verb=false;
	

	column = sf_floatalloc(n1);
    max = sf_floatalloc(n2);
    index = sf_intalloc(n2);

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

    sf_putfloat(out,"d1",d2);

	if (!(label1==NULL)) sf_putstring(out,"label1",label1);
	if (!(unit1==NULL))  sf_putstring(out,"unit1",unit1);
	//sf_warning("Son qua d2=%s",label1);

	sf_putint(out,"n2",1);
	sf_putstring(out,"o2","");
    sf_putstring(out,"d2","");
	sf_putstring(out,"label2","");
	sf_putstring(out,"unit2","");
	
	if (!(max_val==NULL)) {
		sf_putint(out,"n1",n2);
    	sf_putfloat(out,"o1",o2);
		sf_putint(max_val,"n2",1);
		sf_putstring(max_val,"o2","");
    	sf_putstring(max_val,"d2","");
		sf_putstring(max_val,"label2","");
		sf_putstring(max_val,"unit2","");
	}
	/* reading the number of gahters in data*/
    n3 = sf_leftsize(in,2);	

	for (i3=0;i3<n3;i3++) { /*gahters loop */
	    sf_warning("Gather %d/%d",i3+1,n3);
		for (i2=0;i2<n2;i2++) {

			sf_floatread(column,n1,in);

			max[i2]=find_max (column, n1, index+i2);
			if (d1<0)
			index[i2]=n1-index[i2]+shift;
			else
			index[i2]-=shift;
	    	//sf_warning("Son qua, max=%f index=%d",max[i2],index[i2]);
		}
	
	if (!(max_val==NULL)) {
		sf_floatwrite(max,n2,max_val);
	}
		sf_intwrite(index,n2,out);	
	} /* END gahters loop */
    exit (0);
}
示例#12
0
void sf_add(sf_file out, int jobs)
/*< add outputs >*/
{
    int *ibuf=NULL, job, i;
    float *fbuf=NULL;
    char buffout[BUFSIZ];
    sf_complex *cbuf=NULL;
    sf_datatype type;
    size_t nbuf=BUFSIZ;
    off_t nsiz;
    char *oname;
    sf_file *ins;

    type = sf_gettype(out);

    switch(type) {
	case SF_FLOAT:
	    fbuf = (float*) buffout;
	    nbuf /= sizeof(float);
	    for (i=0; i < nbuf; i++) {
		fbuf[i] = 0.0f;
	    }
	    break;
	default:
	    sf_error("wrong type");
	    break;
    }

    ins = (sf_file*) sf_alloc(jobs,sizeof(sf_file));

    for (job=0; job < jobs; job++) {
	oname = onames[job];
	ins[job] = sf_input(oname);
    }

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

	for (job=0; job < jobs; job++) {
	    switch(type) {
		case SF_FLOAT:
		    sf_floatread((float*) buffer,nbuf,ins[job]);
		    for (i=0; i < nbuf; i++) {
			if (job) {
			    fbuf[i] += ((float*) buffer)[i];
			} else {
			    fbuf[i] = ((float*) buffer)[i];
			}
		    }
		    break;
		default:
		    sf_error("wrong type");
		    break;  
	    }
	}

	switch(type) {
	    case SF_FLOAT:
		sf_floatwrite(fbuf,nbuf,out);
		break;
	    case SF_COMPLEX:
		sf_complexwrite(cbuf,nbuf,out);
		break;
	    case SF_INT:
		sf_intwrite(ibuf,nbuf,out);
		break;
	    default:
		sf_error("wrong type");
		break;
	}
    }

    for (job=0; job < jobs; job++) {
	sf_fileclose(ins[job]);
	sf_rm(inames[job],true,false,false);
	sf_rm(onames[job],true,false,false);
	for (i=0; i < inpargc; i++) {
	    oname = inpnames[job][i];
	    sf_rm(oname,true,false,false);
	}
    }

    free(ins);
}
示例#13
0
文件: Mboolcmp.c 项目: krushev36/src
int main(int argc, char *argv[])
{
    off_t n[SF_MAX_DIM],n_r[SF_MAX_DIM], nsiz,nsiz_r=0,nleft;
    int qq[BUFSIZ];
    char buf[BUFSIZ],buf_r[BUFSIZ],*right=0,*sign;
    float eps,fl=0,fr;
    size_t bufsiz=BUFSIZ,dim,dim_r,i,nbuf;
    sf_complex c;
    sf_file in,in_r=0,out;
    sf_datatype type;
    bool cmp_num=false;

    sf_init(argc,argv);

    cmp_num = sf_getfloat("right_f",&fr);
    /* compare input (left) to a single float value (right) */

    if (!cmp_num && NULL == (right=sf_getstring("right"))) sf_error("No right or right_f parameter set.");
    /* the rsf file you will be comparing to */

    if (NULL == (sign=sf_getstring("sign"))) sign="eq";
    /* 'eq'(default),'gt','ge','lq','lt','ne'
        sign=   'eq' equal-to ( == )
        sign=   'gt' greater-than ( > )
        sign=   'ge' greater-than or equal-to ( >= )
        sign=   'lq' less-than or equal-to ( <= )
        sign=   'lt' less-than ( < )
        sign=   'ne' not-equal ( != )
    sign=   'and' the values are both non-zero ( && )
    sign=   'or' one value is non-zero ( !! )
    */

    if (!sf_getfloat("eps",&eps)) eps=0;
    /* comparing within this range epsilon */

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

    dim = sf_largefiledims(in,n);
    for (nsiz=1, i=0; i < dim; i++) nsiz *= n[i];

    if (!cmp_num) {
        in_r = sf_input(right);
        dim_r = (size_t) sf_largefiledims(in_r,n_r);
        for (nsiz_r=1, i=0; i < dim_r; i++) nsiz_r *= n_r[i];
    }

    bufsiz /= sf_esize(in);
    type = sf_gettype(in);

    if (!cmp_num && type != sf_gettype(in_r)) sf_error("Type of input and right files do not match.");
    if (!cmp_num && nsiz != nsiz_r) sf_error("Size of input and right files do not match.");


    for (nleft=nsiz; nleft>0; nleft -= nbuf) {
        nbuf = (bufsiz < nleft)? bufsiz: nleft;
        switch (type) {
        case SF_FLOAT:
            sf_floatread((float*) buf,nbuf,in);
            if (!cmp_num) sf_floatread((float*) buf_r,nbuf,in_r);
            break;
        case SF_INT:
            sf_intread((int*) buf,nbuf,in);
            if (!cmp_num) sf_intread((int*) buf_r,nbuf,in_r);
            break;
        case SF_COMPLEX:
            sf_complexread((sf_complex*) buf,nbuf,in);
            if (!cmp_num) sf_complexread((sf_complex*) buf_r,nbuf,in_r);
            break;
        default:
            sf_error("Type not understood.");
            break;
        }
        for (i=0; i<nbuf; i++) {
            switch (type) {
            case SF_FLOAT:
                fl = ((float*)buf)[i];
                if (!cmp_num) fr = ((float*)buf_r)[i];
                break;
            case SF_INT:
                fl = (float) ((int*)buf)[i];
                if (!cmp_num) fr = (float) ((int*)buf_r)[i];
                break;
            case SF_COMPLEX:
                c=((sf_complex*)buf)[i];
                fl=cabsf(c);
                if (!cmp_num) {
                    c=((sf_complex*)buf_r)[i];
                    fr=cabsf(c);
                }
                break;
            default:
                sf_error("Type not understood.");
                break;
            }

            if      (0==strcmp(sign,"ge")) qq[i] = ((fl-fr) >= -eps);
            else if (0==strcmp(sign,"gt")) qq[i] = ((fl-fr) > -eps);
            else if (0==strcmp(sign,"eq")) qq[i] = (fabs(fl-fr) <= eps);
            else if (0==strcmp(sign,"lt")) qq[i] = ((fl-fr) < eps);
            else if (0==strcmp(sign,"lq")) qq[i] = ((fl-fr) <= eps);
            else if (0==strcmp(sign,"ne")) qq[i] = (fabs(fl-fr) > eps);
            else if (0==strcmp(sign,"and")) qq[i] = ((fabs(fl) > eps) && (fabs(fr) > eps));
            else if (0==strcmp(sign,"or")) qq[i] = ((fabs(fl) > eps) || (fabs(fr) > eps));
            else sf_error("Sign not recognized. Please specify: gt,ge,eq,lq,lt,ne,and,or");
        }
        sf_intwrite(qq,nbuf,out);
    }

    exit(0);
}
示例#14
0
int main(int argc, char* argv[])
{
    bool velocity, verb, shape;
    int dim, i, j, n[3], rect[3], it, nt, order, nt0, nx0;
    int iter, niter, iline, nline, cgiter, *f0, *m0=NULL;
    float d[3], o[3], dt0, dx0, ot0, ox0, eps, tol, *p=NULL, *p0=NULL, thres;
    float *vd, *vdt, *vdx, *s, *t0, *x0, *ds, *rhs, *rhs0, *rhs1=NULL, error0, error1, error, scale;
    char key[6];
    sf_file in, out, dix, t_0=NULL, x_0=NULL, f_0=NULL, grad=NULL, cost=NULL, mini=NULL, prec=NULL;

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

    /* read input dimension */
    dim = sf_filedims(in,n);

    nt = 1;
    for (i=0; i < dim; i++) {
	sprintf(key,"d%d",i+1);
	if (!sf_histfloat(in,key,d+i)) sf_error("No %s= in input",key);
	sprintf(key,"o%d",i+1);
	if (!sf_histfloat(in,key,o+i)) o[i]=0.;
	nt *= n[i];
    }
    if (dim < 3) {
	n[2] = 1; d[2] = d[1]; o[2] = o[1];
    }

    /* read initial guess */
    s = sf_floatalloc(nt);
    sf_floatread(s,nt,in);

    if (!sf_getbool("velocity",&velocity)) velocity=true;
    /* y, input is velocity / n, slowness-squared */

    if (velocity) {
	for (it=0; it < nt; it++) {
	    s[it] = 1./s[it]*1./s[it];
	}
    }

    /* read Dix velocity */
    if (NULL == sf_getstring("dix")) sf_error("No Dix input dix=");
    dix = sf_input("dix");

    if(!sf_histint(dix,"n1",&nt0)) sf_error("No n1= in dix");
    if(!sf_histint(dix,"n2",&nx0)) sf_error("No n2= in dix");
    if(!sf_histfloat(dix,"d1",&dt0)) sf_error("No d1= in dix");
    if(!sf_histfloat(dix,"d2",&dx0)) sf_error("No d2= in dix");
    if(!sf_histfloat(dix,"o1",&ot0)) sf_error("No o1= in dix");
    if(!sf_histfloat(dix,"o2",&ox0)) sf_error("No o2= in dix");

    vd = sf_floatalloc(nt0*nx0);
    sf_floatread(vd,nt0*nx0,dix);
    sf_fileclose(dix);

    /* Dix velocity derivative in t0 (2nd order FD) */
    vdt = sf_floatalloc(nt0*nx0);
    for (i=0; i < nt0; i++) {
	for (j=0; j < nx0; j++) {
	    if (i == 0)
		vdt[j*nt0+i] = (-vd[j*nt0+i+2]+4.*vd[j*nt0+i+1]-3.*vd[j*nt0+i])/(2.*dt0);
	    else if (i == nt0-1)
		vdt[j*nt0+i] = (3.*vd[j*nt0+i]-4.*vd[j*nt0+i-1]+vd[j*nt0+i-2])/(2.*dt0);
	    else
		vdt[j*nt0+i] = (vd[j*nt0+i+1]-vd[j*nt0+i-1])/(2.*dt0);
	}
    }

    /* Dix velocity derivative in x0 (2nd order FD) */
    vdx = sf_floatalloc(nt0*nx0);
    for (j=0; j < nx0; j++) {
	for (i=0; i < nt0; i++) {
	    if (j == 0)
		vdx[j*nt0+i] = (-vd[(j+2)*nt0+i]+4.*vd[(j+1)*nt0+i]-3.*vd[j*nt0+i])/(2.*dx0);
	    else if (j == nx0-1)
		vdx[j*nt0+i] = (3.*vd[j*nt0+i]-4.*vd[(j-1)*nt0+i]+vd[(j-2)*nt0+i])/(2.*dx0);
	    else
		vdx[j*nt0+i] = (vd[(j+1)*nt0+i]-vd[(j-1)*nt0+i])/(2.*dx0);
	}
    }

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

    if (!sf_getfloat("thres",&thres)) thres=10.;
    /* thresholding for caustics */

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

    if (!sf_getint("cgiter",&cgiter)) cgiter=200;
    /* number of CG iterations */

    if (!sf_getbool("shape",&shape)) shape=false;
    /* regularization (default Tikhnov) */

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

    if (!sf_getint("nline",&nline)) nline=0;
    /* maximum number of line search (default turned-off) */

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

    if (shape) {
	if (!sf_getfloat("tol",&tol)) tol=1.e-6;
	/* tolerance for shaping regularization */

	for (i=0; i < dim; i++) {
	    sprintf(key,"rect%d",i+1);
	    if (!sf_getint(key,rect+i)) rect[i]=1;
	    /*( rect#=(1,1,...) smoothing radius on #-th axis )*/
	}
	
	/* triangle smoothing operator */
	sf_trianglen_init(dim,rect,n);
	sf_repeat_init(nt,1,sf_trianglen_lop);
	
	sf_conjgrad_init(nt,nt,nt,nt,eps,tol,verb,false);
	p = sf_floatalloc(nt);
    } else {
	/* initialize 2D gradient operator */
	sf_igrad2_init(n[0],n[1]);
    }
    
    /* allocate memory for fastmarch */
    t0 = sf_floatalloc(nt);
    x0 = sf_floatalloc(nt);
    f0 = sf_intalloc(nt);

    /* allocate memory for update */
    ds  = sf_floatalloc(nt);
    rhs = sf_floatalloc(nt);

    /* output transformation matrix */
    if (NULL != sf_getstring("t0")) {
	t_0 = sf_output("t0");
	sf_putint(t_0,"n3",niter+1);
    }
    if (NULL != sf_getstring("x0")) {
	x_0 = sf_output("x0");
	sf_putint(x_0,"n3",niter+1);
    }

    /* output auxiliary label */
    if (NULL != sf_getstring("f0")) {
	f_0 = sf_output("f0");
	sf_settype(f_0,SF_INT);
	sf_putint(f_0,"n3",niter+1);
    }

    /* output gradient */
    if (NULL != sf_getstring("grad")) {
	grad = sf_output("grad");
	sf_putint(grad,"n3",niter);
    }

    /* output cost */
    if (NULL != sf_getstring("cost")) {
	cost = sf_output("cost");
	sf_putint(cost,"n3",niter+1);
    }

    /* read mask (desired minimum) */
    m0 = sf_intalloc(nt);

    if (NULL != sf_getstring("mask")) {
	mini = sf_input("mask");
	sf_intread(m0,nt,mini);
	sf_fileclose(mini);
    } else {
	for (it=0; it < nt; it++) m0[it] = -1;
    }

    /* read cost (desired minimum) */
    rhs0 = sf_floatalloc(nt);

    if (NULL != sf_getstring("mval")) {
	mini = sf_input("mval");
	sf_floatread(rhs0,nt,mini);
	sf_fileclose(mini);
    } else {
	for (it=0; it < nt; it++) rhs0[it] = 0.;
    }

    /* read preconditioner */
    if (NULL != sf_getstring("prec")) {
	prec = sf_input("prec");
	p0 = sf_floatalloc(nt);
	sf_floatread(p0,nt,prec);
	sf_fileclose(prec);

	rhs1 = sf_floatalloc(nt);
    }

    /* fastmarch initialization */
    fastmarch_init(n,o,d,order);

    /* update initialization */
    t2d_init(dim,n,d,nt0,dt0,ot0,nx0,dx0,ox0);

    /* fastmarch */
    fastmarch(t0,x0,f0,s);

    /* caustic region (2D) */
    t2d_caustic(x0,f0,n,d,thres);

    /* set up operator */
    t2d_set(t0,x0,f0,s,vd,vdt,vdx,m0,p0);

    /* evaluate cost */
    t2d_cost(rhs);

    for (it=0; it < nt; it++) {
	if (f0[it] >= 0 || m0[it] >= 0)
	    rhs[it] = 0.;
	else
	    rhs[it] -= rhs0[it];
    }

    if (p0 == NULL) {
	error0 = error1 = cblas_snrm2(nt,rhs,1);
    } else {
	for (it=0; it < nt; it++) rhs1[it] = p0[it]*rhs[it];
	error0 = error1 = cblas_snrm2(nt,rhs1,1);
    }

    /* write optional outputs */    
    if (NULL!=t_0)  sf_floatwrite(t0,nt,t_0);
    if (NULL!=x_0)  sf_floatwrite(x0,nt,x_0);
    if (NULL!=f_0)  sf_intwrite(f0,nt,f_0);
    if (NULL!=cost) sf_floatwrite(rhs,nt,cost);

    sf_warning("Start conversion, cost %g",1.);

    /* nonlinear loop */
    for (iter=0; iter < niter; iter++) {
	
	/* solve ds */
	if (shape) {
	    if (p0 == NULL)
		sf_conjgrad(NULL,t2d_oper,sf_repeat_lop,p,ds,rhs,cgiter);
	    else
		sf_conjgrad(t2d_prec,t2d_oper,sf_repeat_lop,p,ds,rhs,cgiter);
	} else {
	    sf_solver_reg(t2d_oper,sf_cgstep,sf_igrad2_lop,2*nt,nt,nt,ds,rhs,cgiter,eps,"verb",verb,"end");
	    sf_cgstep_close();
	}

	/* add ds */
	for (it=0; it < nt; it++) {
	    s[it] = s[it]+ds[it]+0.25*ds[it]*ds[it]/s[it];
	}

	/* fastmarch */
	fastmarch(t0,x0,f0,s);

	/* caustic region (2D) */
	t2d_caustic(x0,f0,n,d,thres);

	/* set up operator */
	t2d_set(t0,x0,f0,s,vd,vdt,vdx,m0,p0);

	/* evaluate cost */
	t2d_cost(rhs);

	for (it=0; it < nt; it++) {
	    if (f0[it] >= 0 || m0[it] >= 0)
		rhs[it] = 0.;
	    else
		rhs[it] -= rhs0[it];
	}
	
	if (p0 == NULL) {
	    error = cblas_snrm2(nt,rhs,1);
	} else {
	    for (it=0; it < nt; it++) rhs1[it] = p0[it]*rhs[it];
	    error = cblas_snrm2(nt,rhs1,1);
	}

	error = cblas_snrm2(nt,rhs,1);

	/* line search */
	if (nline > 0 && error >= error1) {

	    scale = 0.5;
	    for (iline=0; iline < nline; iline++) {

		for (it=0; it < nt; it++) {
		    s[it] = s[it]+(scale*ds[it])+0.25*(scale*ds[it])*(scale*ds[it])/s[it];
		}
		
		fastmarch(t0,x0,f0,s);
		t2d_caustic(x0,f0,n,d,thres);

		t2d_set(t0,x0,f0,s,vd,vdt,vdx,m0,p0);
		t2d_cost(rhs);

		for (it=0; it < nt; it++) {
		    if (f0[it] >= 0 || m0[it] >= 0)
			rhs[it] = 0.;
		    else
			rhs[it] -= rhs0[it];
		}
		
		if (p0 == NULL) {
		    error = cblas_snrm2(nt,rhs,1);
		} else {
		    for (it=0; it < nt; it++) rhs1[it] = p0[it]*rhs[it];
		    error = cblas_snrm2(nt,rhs1,1);
		}
		
		error = cblas_snrm2(nt,rhs,1);
		if (error < error1) {
		    sf_warning("Exist line search %d of %d",iline+1,nline);
		} else {
		    scale *= 0.5;
		}
	    }
	}

	error1 = error;

	/* write optional outputs */
	if (NULL!=t_0)  sf_floatwrite(t0,nt,t_0);
	if (NULL!=x_0)  sf_floatwrite(x0,nt,x_0);
	if (NULL!=f_0)  sf_intwrite(f0,nt,f_0);
	if (NULL!=cost) sf_floatwrite(rhs,nt,cost);
	if (NULL!=grad) sf_floatwrite(ds,nt,grad);

	sf_warning("Cost after iteration %d: %g",iter+1,error/error0);
    }

    /* write output */
    if (velocity) {
	for (it=0; it < nt; it++) {
	    s[it] = 1./sqrtf(s[it]);
	}
    }

    sf_floatwrite(s,nt,out);

    exit(0);
}
示例#15
0
文件: Mirays.c 项目: 1014511134/src
int main(int argc, char* argv[])
{
    bool velocity;
    int dim, i, n[3], it, nt, order, j;
    float d[3], o[3], thres;
    float *s, *t0, *x0;
    int *f0;
    char key[3];
    sf_file in, out, ot0=NULL, ox0=NULL, of0=NULL;

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

    /* read input dimension */
    dim = sf_filedims(in,n);

    nt = 1;
    for (i=0; i < dim; i++) {
	sprintf(key,"d%d",i+1);
	if (!sf_histfloat(in,key,d+i)) sf_error("No %s= in input",key);
	sprintf(key,"o%d",i+1);
	if (!sf_histfloat(in,key,o+i)) o[i]=0.;
	nt *= n[i];
    }
    if (dim < 3) {
	n[2] = 1; d[2] = d[1]; o[2] = o[1];
    }

    /* read initial guess */
    s = sf_floatalloc(nt);
    sf_floatread(s,nt,in);

    if (!sf_getbool("velocity",&velocity)) velocity=true;
    /* y, inputs are velocity / n, slowness-squared */

    if (velocity) {
	for (it=0; it < nt; it++) {
	    s[it] = 1./s[it]*1./s[it];
	}
    }

    /* allocate temporary memory */
    t0 = sf_floatalloc(nt);
    x0 = sf_floatalloc(nt);
    f0 = sf_intalloc(nt);

    /* output transformation matrix */
    if (NULL != sf_getstring("t0"))
	ot0 = sf_output("t0");
    if (NULL != sf_getstring("x0"))
	ox0 = sf_output("x0");

    /* output upwind neighbor */
    if (NULL != sf_getstring("f0")) {
	of0 = sf_output("f0");
	sf_settype(of0,SF_INT);
    }

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

    if (!sf_getfloat("thres",&thres)) thres=10.;
    /* thresholding for caustics */

    /* initialization */
    fastmarch_init(n,o,d,order);

    /* fastmarch */
    fastmarch(t0,x0,f0,s);

    /* write output */
    sf_floatwrite(s,nt,out);
    if (NULL!=ot0) sf_floatwrite(t0,nt,ot0);
    if (NULL!=ox0) sf_floatwrite(x0,nt,ox0);

    /* caustic region (2D) */
    for (i=0; i < n[0]; i++) {
	for (j=0; j < n[1]; j++) {
	    if (j > 0) {
		if (x0[j*n[0]+i] <= x0[(j-1)*n[0]+i]) {
		    f0[j*n[0]+i] = 0;
		    continue;
		}
		if ((x0[j*n[0]+i]-x0[(j-1)*n[0]+i]) > thres*d[1]) {
		    f0[j*n[0]+i] = 0;
		    continue;
		}
	    }
	    if (j < n[1]-1) {
		if (x0[(j+1)*n[0]+i] <= x0[j*n[0]+i]) {
		    f0[j*n[0]+i] = 0;
		    continue;
		}
		if ((x0[(j+1)*n[0]+i]-x0[j*n[0]+i]) > thres*d[1]) {
		    f0[j*n[0]+i] = 0;
		    continue;
		}
	    }
	}
    }

    /* write flag */
    if (NULL!=of0) sf_intwrite(f0,nt,of0);

    exit(0);
}
示例#16
0
文件: Mlopef.c 项目: 1014511134/src
int main(int argc, char* argv[])
{
    int n[SF_MAX_DIM], w[3], k[3], a[3], gap[3], center[3];
    int n123, n1, dim, dim1, nk, i, j, ik, na;
    bool stp;
    float *data, d[3], o[3], *mask, vel, tgap, dabs, di;
    char varname[6], *lagfile;
    sf_filter aa, bb, ak;
    sf_file dat, pef, lag, known;

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

    if (NULL == (lagfile = sf_getstring("lag"))) sf_error("Need lag=");
    /* output file for filter lags */
    lag = sf_output(lagfile);
    sf_settype(lag,SF_INT);

    sf_putstring(pef,"lag",lagfile);
    
    dim = sf_filedims(dat,n);
    if (dim > 3) sf_error("Need 3 dimensions or less");

    if (!sf_getint("dim",&dim1)) dim1=dim;
    /* PEF dimensionality */
    if (dim1 > dim) dim1=dim;
    sf_putint(pef,"dim",dim1);

    if (!sf_getints("w",w,dim1)) sf_error("Need w=");
    /* window size */
    for (j=0; j < dim1; j++) {
	if (w[j] > n[j]) w[j] = n[j];
    }
    sf_putints(pef,"w",w,dim1);

    if (!sf_getints("a",a,dim1)) sf_error("Need a=");
    /* filter size */
    sf_putints(pef,"a",a,dim1);

    if (!sf_getints("k",k,dim1)) {
	/* number of windows */
	for (j=0; j < dim1; j++) {
	    k[j] = 1.5 * n[j] / (w[j] - a[j] + 1.);
	}
    }
    sf_putints(pef,"k",k,dim1);

    if (!sf_getints("gap",gap,dim1)) {
	/* filter gap */
	for (j=0; j < dim1; j++) {
	    gap[j] = 0;
	}
    }
    sf_putints(pef,"gap",gap,dim1);

    if (!sf_getints("center",center,dim1)) {
	/* filter center */
	for (j=0; j < dim1-1; j++) {
	    center[j] = (a[j+1] > 1)? a[j]/2: 0;	    
	}
	center[dim1-1] = 0;
    }
    sf_putints(pef,"center",center,dim1);

    if (!sf_getbool("steepdip",&stp)) stp=false;
    /* if y, do steep-dip PEF estimation */

    for (j=0; j < dim1; j++) {
	sprintf(varname,"d%d",j+1);
	if (!sf_histfloat(dat,varname,d+j)) 
	    sf_error("No %s= in input",varname);
	sprintf(varname,"o%d",j+1);
	if (!sf_histfloat(dat,varname,o+j)) 
	    sf_error("No %s= in input",varname);
    }

    if (stp) {
	if (!sf_getfloat ("vel",&vel)) vel=1.7;
	/* velocity for steep-dip decon */

	if (!sf_getfloat("tgap",&tgap)) tgap=0.030;
	/* time gap for steep-dip decon */
 
	bb = steep(dim1, w, a, d, vel, tgap);
    } else {
	bb = createhelix(dim1, w, center, gap, a); 
    }

    sf_putints (lag,"n",w,dim1);

    bound (dim1, w, w, a, bb);
    for (i=0; i < bb->nh; i++) {
	bb->flt[i] = 2.;
    }
    print(dim1, w, center, a, bb);

    n123=n1=nk=1;
    for (j=0; j < dim; j++) {
	n123 *= n[j];
	if (j < dim1) {
	    n1 = n123;
	    nk *= k[j];
	}
    }
    na = bb->nh;

    sf_putint(pef,"n1",na);
    sf_putint(lag,"n1",na);

    for (j=0; j < dim; j++) {
	sprintf(varname,"n%d",j+2);
	sf_putint(lag,varname,1);
	if (j < dim1) {
	    sf_putint(pef,varname,k[j]);
	    sprintf(varname,"o%d",j+2);
	    sf_putfloat(pef,varname,o[j]+0.5*w[j]*d[j]);
	    sprintf(varname,"d%d",j+2);
	    sf_putfloat(pef,varname,(n[j]-w[j])/(k[j]-1.)*d[j]);
	} else if (j == dim1) {
	    sf_putint(pef,varname,n123/n1);
	} else {
	    sf_putint(pef,varname,1);
	}
    }

    sf_intwrite(bb->lag,na,lag);
    
    data = sf_floatalloc(n123);

    aa = (sf_filter) sf_alloc(nk,sizeof(*aa));

    if (NULL != sf_getstring ("mask")) {
	known = sf_input("mask");
	mask = sf_floatalloc(n123);
	sf_floatread(mask,n123,known);
	sf_fileclose(known);
    } else {
	mask = NULL;
    }

    sf_floatread(data,n123,dat);

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

    random_init (2004);
    for (i=0; i < n123; i++) {
	data[i] = data[i]/dabs + 100.*FLT_EPSILON*(random0()-0.5);
    }

    for (ik=0; ik < nk; ik++) {
	ak = aa+ik;
	ak->nh = na;
	ak->flt = sf_floatalloc(na);
	for (i=0; i < na; i++) {
	    ak->flt[i] = 0.;
	}
	ak->lag = bb->lag;
	ak->mis = bb->mis;
    }

    for (i=0; i < n123-n1+1; i += n1) {
	find_lopef (dim1, data+i, aa, k, n, w, mask);

	for (ik=0; ik < nk; ik++) {
	    sf_floatwrite ((aa+ik)->flt,na,pef);
	}
    }



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

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

    sf_axis areplic;
    sf_file in,out;

    sf_init(argc,argv);

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

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

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

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

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

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

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

    done = 1;
    j = 0;

    while (1) {

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

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

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

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

	j++;

    }

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

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

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

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

    type = sf_gettype(in);

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

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

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

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

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


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

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

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

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

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

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

    len = sf_math_parse (output,out,type);

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

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

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

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

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

    exit(0);
}
示例#19
0
文件: tahsub.c 项目: housian0724/src
void tahwritemapped(int verbose, float* trace, void* iheader, 
		    int n1_traces, int n1_headers,
		    sf_file output,sf_file outheaders,
		    sf_datatype typehead,
		    sf_axis* output_axa_array,
		    int* indx_of_keys, int dim_output,
		    off_t* n_output, off_t* n_outheaders)
/*< tah write mapped >*/
{

  int iaxis;
  double header[SF_MAX_DIM];
  off_t i_output[SF_MAX_DIM];
  off_t file_offset;
  bool trace_fits_in_output_file;
  static int number_traces_rejected=0;

  if(verbose>2)
    for (iaxis=0; iaxis<SF_MAX_DIM; iaxis++){
    fprintf(stderr,"axis=%d sf_n(output_axa_array[iaxis])=%d\n",
 	    iaxis+1,sf_n(output_axa_array[iaxis]));
  }

  /* compute the i_output, the output trace index array.  Use the trace 
     headers and axis definitions.  Axis definitions are label#, n#, o#, 
     and d# */
  /* make sure the trace fits in the output file.  A trace does not fit if
     if falls outside the range defined by n#, o#, and d#.  A trace must fall
     on the o#, d# locations (ie if you say o1=10 d1=10 n1=10, traces -500, 
     10000, and 15 do not fit in the output file. */

  trace_fits_in_output_file=true;

  i_output[0]=0;
  for (iaxis=1; iaxis<dim_output; iaxis++){
    double doubleindex;
    if(SF_INT == typehead) 
      header[iaxis]=(double)(((int  *)iheader)[indx_of_keys[iaxis]]);
    else                   
      header[iaxis]=(double)(((float*)iheader)[indx_of_keys[iaxis]]);
    doubleindex=(header[iaxis]-sf_o(output_axa_array[iaxis]))/
                sf_d(output_axa_array[iaxis]);
    i_output[iaxis]=llround(doubleindex);
    if(0.01<fabs(doubleindex-i_output[iaxis]) ||
       i_output[iaxis]<0 ||
       i_output[iaxis]>=sf_n(output_axa_array[iaxis]) ){
	trace_fits_in_output_file=false;
	number_traces_rejected++;
	if(number_traces_rejected<10){
	  fprintf(stderr,"trace rejection #%d\n",number_traces_rejected);
	}
	if(number_traces_rejected==10){
	  fprintf(stderr,"another rejected trace.  reporting will stop.\n");
	}
	break;
    }
    if(verbose>2){
      fprintf(stderr,"iaxis=%d n=%d o=%f d=%f header=%f \n",
	      iaxis,
	      sf_n(output_axa_array[iaxis]),
	      sf_o(output_axa_array[iaxis]),
	      sf_d(output_axa_array[iaxis]),
	      header[iaxis]);
    }
  }
  /* kls need to check doubleindex is about the same as i_output[iaxis]
     this will allow you to write every third trace to an output file
     by using a large increment.  This is good to write gathers every km 
     for velocity analysis of qc.  
     kls also need to check index is in the range 
     [0,sf_n(output_axa_array[iaxis])
  */
    /* kls why does this fail?
      fprintf(stderr,"axis=%d output_axa_array[iaxis]->n=%d\n",
	  iaxis+1,output_axa_array[iaxis]->n); */
  if(trace_fits_in_output_file){
    file_offset=sf_large_cart2line(dim_output,n_output,i_output)*sizeof(float);
    if(verbose>2)fprintf(stderr,"file_offset=%lld\n",(long long) file_offset);
    sf_seek(output,file_offset,SEEK_SET);
    sf_floatwrite(trace,n1_traces,output);
    
    file_offset=sf_large_cart2line(dim_output,n_outheaders,i_output)*
                sizeof(float);
    sf_seek(outheaders,file_offset,SEEK_SET);
    if(SF_INT == typehead) sf_intwrite  ((int  *)iheader,n1_headers,outheaders);
    else                   sf_floatwrite((float*)iheader,n1_headers,outheaders);
    if(verbose>2){
      for(iaxis=2; iaxis<dim_output; iaxis++){
	fprintf(stderr,"indx_of_keys[%d]=%d ",
		iaxis,indx_of_keys[iaxis]);
	if(typehead == SF_INT)fprintf(stderr,"%d\n",
				      ((int  *)iheader)[indx_of_keys[iaxis]]);
	else                  fprintf(stderr,"%g\n",
				      ((float*)iheader)[indx_of_keys[iaxis]]);
      }
      fprintf(stderr,"\n");
    }
  }
}