void sf_setformat (sf_file file, const char* format) /*< Set file format. --- format has a form "form_type", i.e. native_float, ascii_int, etc. >*/ { if (NULL != strstr(format,"float")) { sf_settype(file,SF_FLOAT); } else if (NULL != strstr(format,"int")) { sf_settype(file,SF_INT); } else if (NULL != strstr(format,"complex")) { sf_settype(file,SF_COMPLEX); } else if (NULL != strstr(format,"uchar") || NULL != strstr(format,"byte")) { sf_settype(file,SF_UCHAR); } else if (NULL != strstr(format,"short")) { sf_settype(file,SF_SHORT); } else if (NULL != strstr(format,"long")) { sf_settype(file,SF_LONG); } else if (NULL != strstr(format,"double")) { sf_settype(file,SF_DOUBLE); } else { sf_settype(file,SF_CHAR); } if (0 == strncmp(format,"ascii_",6)) { sf_setform(file,SF_ASCII); } else if (0 == strncmp(format,"xdr_",4)) { sf_setform(file,SF_XDR); } else { sf_setform(file,SF_NATIVE); } }
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); }
int main(int argc, char* argv[]) { int i, id, dim, n[SF_MAX_DIM], nd; kiss_fft_cpx *din; float *dout; sf_file fin, fout; sf_init(argc,argv); fin = sf_input("in"); fout = sf_output("out"); if (SF_COMPLEX != sf_gettype(fin)) sf_error("Need complex input"); dim = sf_filedims (fin,n); nd = 1; for (i=0; i < dim; i++) nd *= n[i]; din = (kiss_fft_cpx*) sf_complexalloc(nd); dout = sf_floatalloc(nd); sf_floatread((float*)din,nd*2,fin); for (id=0; id < nd; id++) { dout[id] = hypotf(din[id].r,din[id].i); } sf_settype(fout, SF_FLOAT); sf_setform(fout, SF_NATIVE); sf_floatwrite(dout,nd,fout); exit(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); }
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); }
int main(int argc, char* argv[]) { sf_file out; FILE *tiffin; TIFF *tiffout; char *tiffname, buf[BUFSIZ]; unsigned char *grey; int n1, i2, n2, nbuf; short nc; sf_init(argc,argv); out = sf_output("out"); sf_settype(out,SF_UCHAR); fclose(sf_tempfile(&tiffname,"w")); tiffin = fopen(tiffname,"wb"); while (1) { nbuf = fread(buf,1,BUFSIZ,stdin); if (nbuf <= 0) break; fwrite(buf,1,nbuf,tiffin); } fclose(tiffin); tiffout = TIFFOpen(tiffname,"rb"); /* Find the width and height of the image */ TIFFGetField(tiffout, TIFFTAG_IMAGEWIDTH, &n1); TIFFGetField(tiffout, TIFFTAG_IMAGELENGTH, &n2); TIFFGetField(tiffout,TIFFTAG_SAMPLESPERPIXEL,&nc); if (1==nc) { sf_putint(out,"n1",n1); sf_putint(out,"n2",n2); } else { sf_putint(out,"n1",nc); sf_putint(out,"n2",n1); sf_putint(out,"n3",n2); } grey = sf_ucharalloc(n1*nc); for (i2 = 0; i2 < n2; i2++) { if (TIFFReadScanline(tiffout, grey, i2, 0) < 0) sf_error("Trouble reading TIFF file"); sf_ucharwrite(grey,n1*nc,out); } TIFFClose(tiffout); unlink(tiffname); exit(0); }
int main(int argc, char*argv[]) { sf_file in, out ; int i1, i2, n1, n2, *v; float o1, d1, **u; char *l1, *u1; sf_axis ax; sf_init(argc, argv); in = sf_input("in"); /* delay file (int) */ out = sf_output("out"); if(!sf_histint(in, "n1", &n2)) sf_error("n1 needed"); sf_shiftdim(in, out, 1); if(!sf_getint("n1", &n1)) n1=1000; /* samples */ if(!sf_getfloat("o1", &o1)) o1=0.0; /* sampling interval */ if(!sf_getfloat("d1", &d1)) d1=0.004; /* original */ if((l1=sf_getstring("l1")) == NULL) l1="Time"; /* label "Time" */ if((u1=sf_getstring("u1")) == NULL) u1="s"; /* unit "s" */ ax = sf_maxa(n1, o1, d1); sf_setlabel(ax, l1); sf_setunit(ax, u1); sf_oaxa(out, ax, 1); sf_putint(out, "n2", n2); sf_settype(out, SF_FLOAT); v = sf_intalloc(n2); u = sf_floatalloc2(n1, n2); sf_intread(v, n2, in); for(i2=0; i2<n2; i2++) for(i1=0; i1<n1; i1++) if(i1==v[i2]) u[i2][i1] = 1; else u[i2][i1] = 0; sf_floatwrite(u[0], n1*n2, out); free(v); free(u[0]); free(u); return 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); }
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); }
int main(int argc, char* argv[]) { int i,j; int n,iseed; int nbin; float dbin,obin; float m1,m2,s1,s2,r; float **hist; sf_axis axis_1,axis_2; sf_file out; sf_init (argc,argv); out = sf_output("out"); sf_settype(out,SF_FLOAT); if (!sf_getint("n",&n)) n=100; /* number of random deviates pairs */ if (!sf_getfloat("m1",&m1)) m1=0.0; /* mean for deviate 1 */ if (!sf_getfloat("m2",&m2)) m2=0.0; /* mean for deviate 2 */ if (!sf_getfloat("s1",&s1)) s1=1.0; /* standard deviation for deviate 1 */ if (!sf_getfloat("s2",&s2)) s2=1.0; /* standard deviation for deviate 2 */ if (!sf_getfloat("r",&r)) r=0.0; /* correlation coefficient */ if (!sf_getint("nbin",&nbin)) nbin=51; /* number of bins for histogram */ if (!sf_getfloat("dbin",&dbin)) dbin=0.1; /* histogram bin size */ if (!sf_getfloat("obin",&obin)) obin=0.0; /* histogram origin */ if (!sf_getint("iseed",&iseed)) iseed=-33; /* random generator seed */ if (iseed >= 0) sf_error("Need strictly negative iseed"); /* output file parameters */ axis_1 = sf_maxa(nbin,obin,dbin); axis_2 = sf_maxa(nbin,obin,dbin); sf_oaxa(out,axis_1,1); sf_oaxa(out,axis_2,2); /* Histogram */ hist = sf_floatalloc2(nbin,nbin); for (j = 0; j < nbin; j++) { for (i = 0; i < nbin; i++) { hist[j][i] = 0.0; } } histgauss(hist,n,r,m1,m2,s1,s2,nbin,dbin,obin,iseed); /* output */ sf_floatwrite(hist[0],nbin*nbin,out); exit(0); }
int main(int argc, char* argv[]) { /*survey parameters*/ int nx, nz; float dx, dz; int n_srcs; int *spx, *spz; int gpz, gpx, gpl; int gpz_v, gpx_v, gpl_v; int snap; /*fft related*/ bool cmplx; int pad1; /*absorbing boundary*/ bool abc; int nbt, nbb, nbl, nbr; float ct,cb,cl,cr; /*source parameters*/ int src; /*source type*/ int nt,ntsnap; float dt,*f0,*t0,*A; /*misc*/ bool verb, ps, mig; float vref; pspar par; int nx1, nz1; /*domain of interest*/ int it; float *vel,**dat,**dat_v,**wvfld,*img; /*velocity profile*/ sf_file Fi,Fo,Fd,Fd_v,snaps; /* I/O files */ sf_axis az,ax; /* cube axes */ sf_init(argc,argv); if (!sf_getint("snap",&snap)) snap=0; /* interval for snapshots */ if (!sf_getbool("cmplx",&cmplx)) cmplx=true; /* use complex fft */ if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */ if(!sf_getbool("abc",&abc)) abc=false; /* absorbing flag */ if (abc) { if(!sf_getint("nbt",&nbt)) sf_error("Need nbt!"); if(!sf_getint("nbb",&nbb)) nbb = nbt; if(!sf_getint("nbl",&nbl)) nbl = nbt; if(!sf_getint("nbr",&nbr)) nbr = nbt; if(!sf_getfloat("ct",&ct)) sf_error("Need ct!"); if(!sf_getfloat("cb",&cb)) cb = ct; if(!sf_getfloat("cl",&cl)) cl = ct; if(!sf_getfloat("cr",&cr)) cr = ct; } else { nbt = 0; nbb = 0; nbl = 0; nbr = 0; ct = 0; cb = 0; cl = 0; cr = 0; } if (!sf_getbool("verb",&verb)) verb=false; /* verbosity */ if (!sf_getbool("ps",&ps)) ps=false; /* use pseudo-spectral */ if (ps) sf_warning("Using pseudo-spectral..."); else sf_warning("Using pseudo-analytical..."); if (!sf_getbool("mig",&mig)) mig=false; /* use pseudo-spectral */ if (mig) sf_warning("Time-reversal propagation"); else sf_warning("Forward modeling"); if (!sf_getfloat("vref",&vref)) vref=1500; /* reference velocity (default using water) */ /* setup I/O files */ Fi = sf_input ("in"); Fo = sf_output("out"); if (mig) { gpl = -1; gpl_v = -1; if (NULL==sf_getstring("dat") && NULL==sf_getstring("dat_v")) sf_error("Need Data!"); if (NULL!=sf_getstring("dat")) { Fd = sf_input("dat"); sf_histint(Fd,"n1",&nt); sf_histfloat(Fd,"d1",&dt); sf_histint(Fd,"n2",&gpl); } else Fd = NULL; if (NULL!=sf_getstring("dat_v")) { Fd_v = sf_input("dat_v"); sf_histint(Fd_v,"n1",&nt); sf_histfloat(Fd_v,"d1",&dt); sf_histint(Fd_v,"n2",&gpl_v); } else Fd_v = NULL; src = -1; n_srcs = -1; spx = NULL; spz = NULL; f0 = NULL; t0 = NULL; A = NULL; } else { Fd = NULL; if (!sf_getint("nt",&nt)) sf_error("Need nt!"); if (!sf_getfloat("dt",&dt)) sf_error("Need dt!"); if (!sf_getint("gpl",&gpl)) gpl = -1; /* geophone length */ if (!sf_getint("gpl_v",&gpl_v)) gpl_v = -1; /* geophone height */ if (!sf_getint("src",&src)) src=0; /* source type */ if (!sf_getint("n_srcs",&n_srcs)) n_srcs=1; /* source type */ spx = sf_intalloc(n_srcs); spz = sf_intalloc(n_srcs); f0 = sf_floatalloc(n_srcs); t0 = sf_floatalloc(n_srcs); A = sf_floatalloc(n_srcs); if (!sf_getints("spx",spx,n_srcs)) sf_error("Need spx!"); /* shot position x */ if (!sf_getints("spz",spz,n_srcs)) sf_error("Need spz!"); /* shot position z */ if (!sf_getfloats("f0",f0,n_srcs)) sf_error("Need f0! (e.g. 30Hz)"); /* wavelet peak freq */ if (!sf_getfloats("t0",t0,n_srcs)) sf_error("Need t0! (e.g. 0.04s)"); /* wavelet time lag */ if (!sf_getfloats("A",A,n_srcs)) sf_error("Need A! (e.g. 1)"); /* wavelet amplitude */ } if (!sf_getint("gpx",&gpx)) gpx = -1; /* geophone position x */ if (!sf_getint("gpz",&gpz)) gpz = -1; /* geophone position z */ if (!sf_getint("gpx_v",&gpx_v)) gpx_v = -1; /* geophone position x */ if (!sf_getint("gpz_v",&gpz_v)) gpz_v = -1; /* geophone position z */ if (SF_FLOAT != sf_gettype(Fi)) sf_error("Need float input"); /* Read/Write axes */ az = sf_iaxa(Fi,1); nz = sf_n(az); dz = sf_d(az); ax = sf_iaxa(Fi,2); nx = sf_n(ax); dx = sf_d(ax); nz1 = nz-nbt-nbb; nx1 = nx-nbl-nbr; if (gpx==-1) gpx = nbl; if (gpz==-1) gpz = nbt; if (gpl==-1) gpl = nx1; if (gpx_v==-1) gpx_v = nbl; if (gpz_v==-1) gpz_v = nbt; if (gpl_v==-1) gpl_v = nz1; ntsnap=0; if (snap) for (it=0;it<nt;it++) if (it%snap==0) ntsnap++; if (mig) { /*output final wavefield*/ sf_setn(az,nz1); sf_setn(ax,nx1); sf_oaxa(Fo,az,1); sf_oaxa(Fo,ax,2); sf_settype(Fo,SF_FLOAT); } else { /*output data*/ sf_setn(ax,gpl); /*output horizontal data is mandatory*/ sf_putint(Fo,"n1",nt); sf_putfloat(Fo,"d1",dt); sf_putfloat(Fo,"o1",0.); sf_putstring(Fo,"label1","Time"); sf_putstring(Fo,"unit1","s"); sf_oaxa(Fo,ax,2); sf_settype(Fo,SF_FLOAT); /*output vertical data is optional*/ if (NULL!=sf_getstring("dat_v")) { Fd_v = sf_output("dat_v"); sf_setn(az,gpl_v); sf_putint(Fd_v,"n1",nt); sf_putfloat(Fd_v,"d1",dt); sf_putfloat(Fd_v,"o1",0.); sf_putstring(Fd_v,"label1","Time"); sf_putstring(Fd_v,"unit1","s"); sf_oaxa(Fd_v,az,2); sf_settype(Fd_v,SF_FLOAT); } else Fd_v = NULL; } if (snap > 0) { snaps = sf_output("snaps"); /* (optional) snapshot file */ sf_setn(az,nz1); sf_setn(ax,nx1); sf_oaxa(snaps,az,1); sf_oaxa(snaps,ax,2); sf_putint(snaps,"n3",ntsnap); sf_putfloat(snaps,"d3",dt*snap); sf_putfloat(snaps,"o3",0.); sf_putstring(snaps,"label3","Time"); sf_putstring(snaps,"unit3","s"); } else snaps = NULL; par = (pspar) sf_alloc(1,sizeof(*par)); vel = sf_floatalloc(nz*nx); if (mig && NULL==Fd) dat = NULL; else dat = sf_floatalloc2(nt,gpl); if (NULL!=Fd_v) dat_v = sf_floatalloc2(nt,gpl_v); else dat_v = NULL; if (mig) img = sf_floatalloc(nz1*nx1); else img = NULL; if (snap>0) wvfld = sf_floatalloc2(nx1*nz1,ntsnap); else wvfld = NULL; sf_floatread(vel,nz*nx,Fi); if (mig) { if (NULL!=Fd) sf_floatread(dat[0],gpl*nt,Fd); if (NULL!=Fd_v) sf_floatread(dat_v[0],gpl_v*nt,Fd_v); } /*passing the parameters*/ par->nx = nx; par->nz = nz; par->dx = dx; par->dz = dz; par->n_srcs= n_srcs; par->spx = spx; par->spz = spz; par->gpz = gpz; par->gpx = gpx; par->gpl = gpl; par->gpz_v = gpz_v; par->gpx_v = gpx_v; par->gpl_v = gpl_v; par->snap = snap; par->cmplx = cmplx; par->pad1 = pad1; par->abc = abc; par->nbt = nbt; par->nbb = nbb; par->nbl = nbl; par->nbr = nbr; par->ct = ct; par->cb = cb; par->cl = cl; par->cr = cr; par->src = src; par->nt = nt; par->dt = dt; par->f0 = f0; par->t0 = t0; par->A = A; par->verb = verb; par->ps = ps; par->vref = vref; /*do the work*/ psp(wvfld, dat, dat_v, img, vel, par, mig); if (mig) { sf_floatwrite(img,nz1*nx1,Fo); } else { sf_floatwrite(dat[0],gpl*nt,Fo); if (NULL!=Fd_v) sf_floatwrite(dat_v[0],gpl_v*nt,Fd_v); } if (snap>0) sf_floatwrite(wvfld[0],nz1*nx1*ntsnap,snaps); exit (0); }
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); }
int main (int argc, char* argv[]) { int nz, nx, ny, ic, nc; float dz, oz, dx, ox, dy, oy; size_t i, n, sz; float *buf = NULL, *buf2 = NULL; #ifdef HAVE_SSE unsigned char pad[64]; #endif sf_file velz, velx = NULL, theta = NULL, phi = NULL, eta = NULL, out; bool verb; Ugrid z_grid, x_grid, y_grid; BCtype_s zBC, xBC, yBC; multi_UBspline_3d_s *velspline = NULL; sf_init (argc, argv); velz = sf_input ("in"); /* Vertical velocity */ out = sf_output ("out"); /* Spline coefficients */ /* Spatial dimensions */ if (!sf_histint (velz, "n1", &nz)) sf_error ("No n1= in input"); if (!sf_histint (velz, "n2", &nx)) sf_error ("No n2= in input"); if (!sf_histint (velz, "n3", &ny)) sf_error ("No n3= in input"); if (!sf_histfloat (velz, "d1", &dz)) sf_error ("No d1= in input"); if (!sf_histfloat (velz, "o1", &oz)) oz = 0.0; if (!sf_histfloat (velz, "d2", &dx)) sf_error ("No d2= in input"); if (!sf_histfloat (velz, "o2", &ox)) ox = 0.0; if (!sf_histfloat (velz, "d3", &dy)) sf_error ("No d3= in input"); if (!sf_histfloat (velz, "o3", &oy)) oy = 0.0; if (!sf_getbool ("verb", &verb)) verb = false; /* verbosity flag */ n = (size_t)nz*(size_t)nx*(size_t)ny; buf = sf_floatalloc (n); nc = 1; if (sf_getstring ("vx")) { /* Horizontal velocity */ velx = sf_input ("vx"); nc++; } if (sf_getstring ("eta")) { /* Anellipticity */ if (NULL == velx) sf_error ("Need vx=, if eta= is given"); eta = sf_input ("eta"); nc++; } else if (velx) sf_error ("Need eta=, if vx= is given"); if (sf_getstring ("theta")) { /* Tilt angle elevation */ if (NULL == velx) sf_error ("Need vx=, if theta= is given"); theta = sf_input ("theta"); nc++; } if (sf_getstring ("phi")) { /* Tilt angle azimuth */ if (NULL == theta) sf_error ("Need theta=, if phi= is given"); phi = sf_input ("phi"); nc++; } z_grid.start = oz; z_grid.end = oz + (nz - 1)*dz; z_grid.num = nz; x_grid.start = ox; x_grid.end = ox + (nx - 1)*dx; x_grid.num = nx; y_grid.start = oy; y_grid.end = oy + (ny - 1)*dy; y_grid.num = ny; zBC.lCode = zBC.rCode = NATURAL; xBC.lCode = xBC.rCode = NATURAL; yBC.lCode = yBC.rCode = NATURAL; velspline = create_multi_UBspline_3d_s (y_grid, x_grid, z_grid, yBC, xBC, zBC, nc); /* Read data and compute spline coefficients */ if (verb) sf_warning ("Processing V_z"); sf_floatread (buf, n, velz); if (1 == nc) { /* Isotropic case - convert velocity to slowness */ if (verb) sf_warning ("Converting to slowness for isotropic case"); for (i = 0; i < n; i++) buf[i] = 1.0/buf[i]; } else { /* Convert to V_z^2 */ for (i = 0; i < n; i++) buf[i] *= buf[i]; } ic = 0; set_multi_UBspline_3d_s (velspline, ic, buf); ic++; if (velx) { if (verb) sf_warning ("Processing V_x"); buf2 = sf_floatalloc (n); sf_floatread (buf2, n, velx); sf_fileclose (velx); /* Convert to V_x^2 */ for (i = 0; i < n; i++) buf2[i] *= buf2[i]; set_multi_UBspline_3d_s (velspline, ic, buf2); ic++; /* Convert to (V_z*V_x)^2 */ for (i = 0; i < n; i++) buf[i] *= buf2[i]; } if (eta) { if (verb) sf_warning ("Processing Eta"); sf_floatread (buf2, n, eta); sf_fileclose (eta); /* Convert to -8*eta/(1 + 2*eta)*(V_z*V_x)^2 */ for (i = 0; i < n; i++) { buf2[i] = -8.0*buf2[i]/(1.0 + 2.0*buf2[i]); buf2[i] *= buf[i]; } set_multi_UBspline_3d_s (velspline, ic, buf2); ic++; } if (theta) { if (verb) sf_warning ("Processing Theta"); sf_floatread (buf, n, theta); sf_fileclose (theta); /* Convert to radians */ for (i = 0; i < n; i++) buf[i] = buf[i]*SF_PI/180.0; set_multi_UBspline_3d_s (velspline, ic, buf); ic++; } if (phi) { if (verb) sf_warning ("Processing Phi"); sf_floatread (buf, n, phi); sf_fileclose (phi); /* Convert to radians */ for (i = 0; i < n; i++) buf[i] = buf[i]*SF_PI/180.0; set_multi_UBspline_3d_s (velspline, ic, buf); ic++; } if (buf2) free (buf2); free (buf); sz = (size_t)sizeof(multi_UBspline_3d_s) + (size_t)velspline->nc; #ifdef HAVE_SSE if (sizeof(multi_UBspline_3d_s) % 64) sz += 64 - (sizeof(multi_UBspline_3d_s) % 64); #endif /* Make output a 1-D file of coefficients */ sf_unshiftdim2 (velz, out, 1); /* Set up output */ sf_settype (out, SF_UCHAR); sf_putlargeint (out, "n1", sz); sf_putfloat (out, "o1", 0.0); sf_putfloat (out, "d1", 1.0); sf_putstring (out, "label1", "Spline coefficients"); sf_putstring (out, "unit1", ""); sf_putstring (out, "label2", ""); sf_putstring (out, "unit2", ""); sf_putint (out, "Nz", nz); sf_putfloat (out, "Oz", oz); sf_putfloat (out, "Dz", dz); sf_putint (out, "Nx", nx); sf_putfloat (out, "Ox", ox); sf_putfloat (out, "Dx", dx); sf_putint (out, "Ny", ny); sf_putfloat (out, "Oy", oy); sf_putfloat (out, "Dy", dy); sf_putint (out, "Nc", nc); sf_putstring (out, "splines", "y"); if (verb) { sf_warning ("Number of spline coefficients: %lu", velspline->nc/(size_t)sizeof(float)); sf_warning ("Writing spline coefficients"); } sf_ucharwrite ((unsigned char*)velspline, (size_t)sizeof(multi_UBspline_3d_s), out); #ifdef HAVE_SSE if (sizeof(multi_UBspline_3d_s) % 64) sf_ucharwrite (pad, (size_t)(64 - (sizeof(multi_UBspline_3d_s) % 64)), out); #endif sf_ucharwrite ((unsigned char*)velspline->coefs, (size_t)velspline->nc, out); destroy_Bspline (velspline); return 0; }
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); }
int main(int argc, char* argv[]) { int n1, n2, i1, i2, niter; float **b, *h, ***rt, d, maxd; sf_complex **data; sf_file inp, out, bad; sf_init (argc,argv); inp = sf_input("in"); out = sf_output("out"); if (SF_COMPLEX != sf_gettype(inp)) sf_error("Need complex input"); if (!sf_histint(inp,"n1",&n1)) sf_error("No n1= in input"); if (!sf_histint(inp,"n2",&n2)) sf_error("No n2= in input"); sf_settype(out,SF_FLOAT); if (!sf_getint("niter",&niter)) niter=0; /* number of iterations */ if (NULL != sf_getstring("badness")) { bad = sf_output("badness"); /* (optional) badness attribute file */ sf_settype(bad,SF_FLOAT); } else { bad = NULL; } data = sf_complexalloc2(n1,n2); sf_complexread(data[0],n1*n2,inp); /* normalize */ maxd = 0; for (i2=0; i2 < n2; i2++) { for (i1=0; i1 < n1; i1++) { d = cabsf(data[i2][i1]); if (maxd < d) maxd=d; } } for (i2=0; i2 < n2; i2++) { for (i1=0; i1 < n1; i1++) { #ifdef SF_HAS_COMPLEX_H data[i2][i1] = data[i2][i1]/maxd; #else data[i2][i1] = sf_crmul(data[i2][i1],1.0f/maxd); #endif } } if (NULL != bad) { b = sf_floatalloc2(n1,n2); rt = sf_floatalloc3(n1,n2,2); grad2init (n1, n2, data, rt); for (i2=0; i2 < n2; i2++) { b[i2][n1-1] = 0.; b[i2][n1-2] = 0.; } for (i1=0; i1 < n1; i1++) { b[n2-1][i1] = 0.; b[n2-2][i1] = 0.; } for (i2=0; i2 < n2-2; i2++) { for (i1=0; i1 < n1-2; i1++) { b[i2][i1] = (rt[0][i2+1][i1] - rt[0][i2][i1]) - (rt[1][i2][i1+1] - rt[1][i2][i1]); } } sf_floatwrite(b[0],n1*n2,bad); } if (niter > 0) { h = sf_floatalloc(n1*n2); unwraper (n1,n2, data, h, niter); sf_floatwrite(h,n1*n2,out); } exit(0); }
int main (int argc, char *argv[]) { bool verb; /* verbosity */ bool incore; /* in core execution */ float eps; /* dip filter constant */ int nrmax; /* number of reference velocities */ float dtmax; /* time error */ int pmx,pmy; /* padding in the k domain */ int tmx,tmy; /* boundary taper size */ bool cw; /* converted waves flag */ sf_axis az,ax,ay,aw,alx,aly; int n,nz,nw; sf_file Fs_s=NULL,Fs_r=NULL; /* slowness file S (nlx,nly,nz) */ sf_file Fw_s=NULL,Fw_r=NULL; /* wavefield file D or U ( nx, ny,nw) */ sf_file Fr=NULL; /* reflectivity */ fslice wfl_s=NULL,wfl_r=NULL,refl=NULL; fslice slo_s=NULL,slo_r=NULL; /*------------------------------------------------------------*/ sf_init(argc,argv); /* converted waves flag */ if (NULL != sf_getstring("sls")) { cw=true; } else { cw=false; } if (!sf_getbool("verb", &verb)) verb = true; /* verbosity flag */ if (!sf_getbool("incore",&incore))incore = false; /* in core execution */ if (!sf_getfloat("eps", &eps )) eps = 0.01; /* stability parameter */ if (!sf_getint( "nrmax",&nrmax)) nrmax = 1; /* maximum number of refs */ if (!sf_getfloat("dtmax",&dtmax)) dtmax = 0.004; /* time error */ if (!sf_getint( "pmx", &pmx )) pmx = 0; /* padding on x */ if (!sf_getint( "pmy", &pmy )) pmy = 0; /* padding on y */ if (!sf_getint( "tmx", &tmx )) tmx = 0; /* taper on x */ if (!sf_getint( "tmy", &tmy )) tmy = 0; /* taper on y */ /*------------------------------------------------------------*/ /* SLOWNESS */ ; Fs_s = sf_input("slo"); if(cw) Fs_r = sf_input("sls"); alx = sf_iaxa(Fs_s,1); sf_setlabel(alx,"lx"); aly = sf_iaxa(Fs_s,2); sf_setlabel(aly,"ly"); az = sf_iaxa(Fs_s,3); sf_setlabel(az , "z"); /* test here if slo and sls have similar sizes */ n = sf_n(alx)*sf_n(aly); nz = sf_n(az); ; slo_s = fslice_init(n,nz,sizeof(float)); if(cw) slo_r = fslice_init(n,nz,sizeof(float)); ; fslice_load(Fs_s,slo_s,SF_FLOAT); if(cw) fslice_load(Fs_r,slo_r,SF_FLOAT); /*------------------------------------------------------------*/ /* WAVEFIELD/IMAGE */ Fw_s = sf_input ( "in"); Fw_r = sf_output("out"); sf_settype(Fw_r,SF_COMPLEX); Fr = sf_input ("ref"); if (SF_COMPLEX !=sf_gettype(Fw_s)) sf_error("Need complex source wavefield"); ax = sf_iaxa(Fw_s,1); sf_setlabel(ax,"x"); sf_oaxa(Fw_r,ax,1); ay = sf_iaxa(Fw_s,2); sf_setlabel(ay,"y"); sf_oaxa(Fw_r,ay,2); aw = sf_iaxa(Fw_s,3); sf_setlabel(aw,"w"); sf_oaxa(Fw_r,aw,3); n = sf_n(ax)*sf_n(ay); nw = sf_n(aw); /* slice management (temp files) */ wfl_s = fslice_init(n,nw,sizeof(sf_complex)); wfl_r = fslice_init(n,nw,sizeof(sf_complex)); refl = fslice_init(n,nz,sizeof(float)); fslice_load(Fw_s,wfl_s,SF_COMPLEX); fslice_load(Fr, refl, SF_FLOAT); /*------------------------------------------------------------*/ /* MODELING */ srmod_init (verb,incore,eps,dtmax, az,aw,ax,ay,alx,aly, tmx,tmy,pmx,pmy); if(cw) { srmod_cw_init (dtmax,nrmax,slo_s,slo_r); srmod_cw (wfl_s,wfl_r,refl); srmod_cw_close(); } else { srmod_pw_init (dtmax,nrmax,slo_s); srmod_pw (wfl_s,wfl_r,refl); srmod_pw_close(); } srmod_close(); /*------------------------------------------------------------*/ /* slice management (temp files) */ fslice_dump(Fw_r,wfl_r,SF_COMPLEX); ; fslice_close(slo_s); if(cw) fslice_close(slo_r); ; fslice_close(wfl_s); ; fslice_close(wfl_r); ; fslice_close(refl); exit (0); }
int main(int argc, char* argv[]) { bool adj,timer,verb; int nt, nx, nz, nx2, nz2, nzx, nzx2, ntx, pad1, snap, wfnt; int m2, n2, nk, nth=1; float dt, dx, dz, ox; sf_complex **img, **dat, **lt, **rt, ***wvfld, *ww; sf_file data, image, left, right, snaps, src; double time=0.,t0=0.,t1=0.; geopar geop; sf_init(argc,argv); if (!sf_getbool("adj",&adj)) adj=false; /* if n, modeling; if y, migration */ if(! sf_getbool("timer",&timer)) timer=false; if (!sf_getbool("verb",&verb)) verb=false; if (!sf_getint("snap",&snap)) snap=0; /* interval for snapshots */ if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */ if (adj) { /* migration */ data = sf_input("in"); // data here is just a refl file image = sf_output("out"); sf_settype(image,SF_COMPLEX); if (!sf_histint(data,"n1",&nz)) sf_error("No n1= in input"); if (!sf_histfloat(data,"d1",&dz)) sf_error("No d1= in input"); if (!sf_histint(data,"n2",&nx)) sf_error("No n2= in input"); if (!sf_histfloat(data,"d2",&dx)) sf_error("No d2= in input"); if (!sf_histfloat(data,"o2",&ox)) ox=0.; if (!sf_getint("nt",&nt)) sf_error("Need nt="); /* time samples */ if (!sf_getfloat("dt",&dt)) sf_error("Need dt="); /* time sampling */ sf_putint(image,"n1",nz); sf_putfloat(image,"d1",dz); sf_putfloat(image,"o1",0.); sf_putstring(image,"label1","Depth"); sf_putint(image,"n2",nx); sf_putfloat(image,"d2",dx); sf_putfloat(image,"o2",ox); sf_putstring(image,"label2","Distance"); } else { /* modeling */ image = sf_input("in"); data = sf_output("out"); src = sf_input("src"); sf_settype(data,SF_COMPLEX); if (!sf_histint(image,"n1",&nz)) sf_error("No n1= in input"); if (!sf_histfloat(image,"d1",&dz)) sf_error("No d1= in input"); if (!sf_histint(image,"n2",&nx)) sf_error("No n2= in input"); if (!sf_histfloat(image,"d2",&dx)) sf_error("No d2= in input"); if (!sf_histfloat(image,"o2",&ox)) ox=0.; if (!sf_getint("nt",&nt)) sf_error("Need nt="); /* time samples */ if (!sf_getfloat("dt",&dt)) sf_error("Need dt="); /* time sampling */ if (!sf_histint(src,"n1",&n2) || n2 != nt) sf_error("nt doesn't match!"); sf_putint(data,"n1",nz); sf_putfloat(data,"d1",dz); sf_putfloat(data,"o1",0.); sf_putstring(data,"label1","Depth"); sf_putint(data,"n2",nx); sf_putfloat(data,"d2",dx); sf_putfloat(data,"o2",ox); sf_putstring(data,"label2","Distance"); } nz2 = kiss_fft_next_fast_size(nz*pad1); nx2 = kiss_fft_next_fast_size(nx); nk = nz2*nx2; /*wavenumber*/ nzx = nz*nx; nzx2 = nz2*nx2; ntx = nt*nx; wfnt = (int)(nt-1)/snap+1; if (snap > 0) { snaps = sf_output("snaps"); /* (optional) snapshot file */ sf_settype(snaps,SF_COMPLEX); sf_putint(snaps,"n1",nz); sf_putfloat(snaps,"d1",dz); sf_putfloat(snaps,"o1",0.); sf_putstring(snaps,"label1","Depth"); sf_putint(snaps,"n2",nx); sf_putfloat(snaps,"d2",dx); sf_putfloat(snaps,"o2",ox); sf_putstring(snaps,"label2","Distance"); sf_putint(snaps,"n3",wfnt); sf_putfloat(snaps,"d3",dt*snap); sf_putfloat(snaps,"o3",0.); sf_putstring(snaps,"label3","Time"); } else { snaps = NULL; } /* propagator matrices */ left = sf_input("left"); right = sf_input("right"); if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx); if (!sf_histint(left,"n2",&m2)) sf_error("No n2= in left"); if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2); if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk); lt = sf_complexalloc2(nzx,m2); rt = sf_complexalloc2(m2,nk); img = sf_complexalloc2(nz,nx); dat = sf_complexalloc2(nz,nx); if (snap > 0) wvfld = sf_complexalloc3(nz,nx,wfnt); else wvfld = NULL; geop = (geopar) sf_alloc(1, sizeof(*geop)); if (!adj) { ww=sf_complexalloc(nt); sf_complexread(ww,nt,src); } else ww=NULL; sf_complexread(lt[0],nzx*m2,left); sf_complexread(rt[0],m2*nk,right); if (adj) sf_complexread(dat[0],nzx,data); else sf_complexread(img[0],nzx,image); /*close RSF files*/ sf_fileclose(left); sf_fileclose(right); #ifdef _OPENMP #pragma omp parallel { nth = omp_get_num_threads(); } sf_warning(">>>> Using %d threads <<<<<", nth); #endif if (timer) t0 = gtod_timer(); /*load constant geopar elements*/ geop->nx = nx; geop->nz = nz; geop->dx = dx; geop->dz = dz; geop->ox = ox; geop->nt = nt; geop->dt = dt; geop->snap= snap; geop->nzx2= nzx2; geop->nk = nk; geop->m2 = m2; geop->wfnt= wfnt; lrexp(img, dat, adj, lt, rt, ww, geop, pad1, verb, snap, wvfld); if (timer) { t1 = gtod_timer(); time = t1-t0; sf_warning("Time = %lf\n",time); } if (adj) { sf_complexwrite(img[0],nzx,image); } else { sf_complexwrite(dat[0],ntx,data); } if (snap > 0 && NULL != snaps) { sf_complexwrite(wvfld[0][0],wfnt*nx*nz,snaps); } exit(0); }
/* main function */ int main(int argc, char* argv[]) { /*geopar variables*/ int nx, nz; int nxb, nzb; float dx, dz, ox, oz; int spz, gpz, gpl; /*source/geophone location*/ int snpint; int top, bot, lft, rht; /*abc boundary*/ int nt; float dt; float trunc; bool verb; /* verbosity flag */ bool illum; /* source illumination flag*/ int m2, m2b, pad1; sf_complex **ltf, **rtf; sf_complex **ltb, **rtb; sf_complex *ww; float *rr; /*extras*/ bool roll; /* survey strategy */ int rectz,rectx,repeat; /*refl smoothing parameters*/ int sht0,shtbgn,shtend,shtnum,shtnum0,shtint; /*mpi*/ int cpuid, numprocs; /*misc*/ int mode; int nzx, nx2, nz2, n2, nk; int ix, iz, it, is; int wfnt; float wfdt; int niter; /*Data/Image*/ sf_complex ***record, **imginv; /*tmp*/ int tmpint; /*I/O*/ sf_file Fvel; sf_file left, right, leftb, rightb; sf_file Fsrc, Frcd/*source and record*/; sf_file Fimg; sf_file Fstart; /*axis*/ sf_axis at, ax, az; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &cpuid); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); sf_init(argc, argv); if(cpuid==0) sf_warning("numprocs=%d",numprocs); if (!sf_getbool("verb", &verb)) verb=false; /*verbosity*/ if (!sf_getint("niter",&niter)) niter=1; if (!sf_getint("mode", &mode)) mode = 0; if (!sf_getbool("illum", &illum)) illum=false; /*if n, no source illumination applied */ if (!sf_getbool("roll", &roll)) roll=false; /*if n, receiver is independent of source location and gpl=nx*/ /* source/receiver info */ if (!sf_getint("shtbgn", &shtbgn)) sf_error("Need shot starting location on grid!"); if (!sf_getint("sht0", &sht0)) sht0=shtbgn; /*actual shot origin on grid*/ if (!sf_getint("shtend", &shtend)) sf_error("Need shot ending location on grid!"); if (!sf_getint("shtint", &shtint)) sf_error("Need shot interval on grid!"); shtnum = (int)((shtend-shtbgn)/shtint) + 1; shtnum0 = shtnum; if (shtnum%numprocs!=0) { shtnum += numprocs-shtnum%numprocs; if (verb) sf_warning("Total shot number is not divisible by total number of nodes! shunum padded from %d to %d.",shtnum0,shtnum); } if (!sf_getint("spz", &spz)) sf_error("Need source depth!"); if (!sf_getint("gpz", &gpz)) sf_error("Need receiver depth!"); if (roll) if (!sf_getint("gpl", &gpl)) sf_error("Need receiver length"); if (!sf_getint("snapinter", &snpint)) snpint=1; /* snap interval */ /*--- parameters of source ---*/ if (!sf_getfloat("srctrunc", &trunc)) trunc=0.4; if (!sf_getint("rectz", &rectz)) rectz=1; if (!sf_getint("rectx", &rectx)) rectx=1; if (!sf_getint("repeat", &repeat)) repeat=0; /* abc parameters */ if (!sf_getint("top", &top)) top=40; if (!sf_getint("bot", &bot)) bot=40; if (!sf_getint("lft", &lft)) lft=40; if (!sf_getint("rht", &rht)) rht=40; /*Set I/O file*/ Frcd = sf_input("--input"); /*record from elsewhere*/ Fsrc = sf_input("src"); /*source wavelet*/ Fimg = sf_output("--output"); left = sf_input("left"); right = sf_input("right"); leftb = sf_input("leftb"); rightb = sf_input("rightb"); Fvel = sf_input("vel"); /*velocity - just for model dimension*/ /*--- Axes parameters ---*/ at = sf_iaxa(Fsrc, 1); nt = sf_n(at); dt = sf_d(at); az = sf_iaxa(Fvel, 1); nzb = sf_n(az); dz = sf_d(az); oz = sf_o(az); ax = sf_iaxa(Fvel, 2); nxb = sf_n(ax); dx = sf_d(ax); ox = sf_o(ax); nzx = nzb*nxb; nz = nzb - top - bot; nx = nxb - lft - rht; if (!roll) gpl = nx; /* global survey setting */ /* wavefield axis */ wfnt = (int)(nt-1)/snpint+1; wfdt = dt*snpint; /* propagator matrices */ if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */ nz2 = kiss_fft_next_fast_size(nzb*pad1); nx2 = kiss_fft_next_fast_size(nxb); nk = nz2*nx2; /*wavenumber*/ if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx); if (!sf_histint(left,"n2",&m2)) sf_error("Need n2= in left"); if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2); if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk); if (!sf_histint(leftb,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx); if (!sf_histint(leftb,"n2",&m2b)) sf_error("Need n2= in left"); if (!sf_histint(rightb,"n1",&n2) || n2 != m2b) sf_error("Need n1=%d in right",m2b); if (!sf_histint(rightb,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk); /*check record data*/ sf_histint(Frcd,"n1", &tmpint); if (tmpint != nt ) sf_error("Error parameter n1 in record!"); sf_histint(Frcd,"n2", &tmpint); if (tmpint != gpl ) sf_error("Error parameter n2 in record!"); sf_histint(Frcd,"n3", &tmpint); if (tmpint != shtnum0 ) sf_error("Error parameter n3 in record!"); /*check starting model for cg*/ if (NULL!=sf_getstring("start")) Fstart = sf_input("start"); else Fstart = NULL; /*allocate memory*/ ww=sf_complexalloc(nt); rr=sf_floatalloc(nzx); ltf = sf_complexalloc2(nzx,m2); rtf = sf_complexalloc2(m2,nk); ltb = sf_complexalloc2(nzx,m2b); rtb = sf_complexalloc2(m2b,nk); geop = (geopar) sf_alloc(1, sizeof(*geop)); record = sf_complexalloc3(nt, gpl, shtnum); imginv = sf_complexalloc2(nz,nx); /*read from files*/ sf_complexread(ww,nt,Fsrc); sf_complexread(ltf[0],nzx*m2,left); sf_complexread(rtf[0],m2*nk,right); sf_complexread(ltb[0],nzx*m2b,leftb); sf_complexread(rtb[0],m2b*nk,rightb); /*read data*/ sf_complexread(record[0][0], shtnum0*gpl*nt, Frcd); if (shtnum0%numprocs!=0) { #ifdef _OPENMP #pragma omp parallel for private(is,ix,it) #endif for (is=shtnum0; is<shtnum; is++) for (ix=0; ix<gpl; ix++) for (it=0; it<nt; it++) record[is][ix][it] = sf_cmplx(0.,0.); } /*read starting model*/ if (NULL != Fstart) { sf_complexread(imginv[0],nx*nz,Fstart); } else { /*transform the dimension to 1d*/ #ifdef _OPENMP #pragma omp parallel for private(iz) #endif for (iz=0; iz<nz; iz++) { for (ix=0; ix<nx; ix++) { imginv[ix][iz] = sf_cmplx(0.,0.); } } } /* output RSF files */ if (cpuid==0) { sf_setn(az, nz); sf_setn(ax, nx); sf_oaxa(Fimg, az, 1); sf_oaxa(Fimg, ax, 2); sf_settype(Fimg,SF_COMPLEX); } /*close RSF files*/ sf_fileclose(Fvel); sf_fileclose(Fsrc); sf_fileclose(left); sf_fileclose(right); sf_fileclose(leftb); sf_fileclose(rightb); /*load geopar elements*/ geop->nx = nx; geop->nz = nz; geop->nxb = nxb; geop->nzb = nzb; geop->dx = dx; geop->dz = dz; geop->ox = ox; geop->oz = oz; geop->snpint = snpint; geop->spz = spz; geop->gpz = gpz; geop->gpl = gpl; geop->top = top; geop->bot = bot; geop->lft = lft; geop->rht = rht; geop->nt = nt; geop->dt = dt; geop->trunc = trunc; /*geop->adj = adj; */ geop->verb = verb; geop->illum = illum; geop->m2 = m2; geop->m2b = m2b; geop->pad1 = pad1; /*pointers*/ geop->ltf = ltf; geop->rtf = rtf; geop->ltb = ltb; geop->rtb = rtb; geop->ww = ww; geop->rr = rr; /*extra*/ geop->roll = roll; geop->rectz=rectz; geop->rectx=rectx; geop->repeat=repeat; geop->sht0=sht0; geop->shtbgn=shtbgn; geop->shtend=shtend; geop->shtnum=shtnum; geop->shtnum0=shtnum0; geop->shtint=shtint; geop->cpuid=cpuid; geop->numprocs=numprocs; /*switch*/ geop->mode=mode; sf_csolver(psrtm_lop,sf_ccgstep,nz*nx,nt*gpl*shtnum,imginv[0],record[0][0],niter,"verb",true,"end"); //psrtm_lop(true,false,nz*nx,nt*gpl*shtnum,imginv[0],record[0][0]); if (cpuid==0) sf_complexwrite(imginv[0], nz*nx, Fimg); /*free memory*/ free(geop); free(ww); free(rr); free(*ltf); free(ltf); free(*rtf); free(rtf); free(*ltb); free(ltb); free(*rtb); free(rtb); free(*imginv); free(imginv); free(**record); free(*record); free(record); MPI_Finalize(); exit(0); }
int main(int argc, char* argv[]) { clock_t tstart,tend; double duration; /*flag*/ bool verb, adj; /* migration(adjoint) flag */ bool wantwf; bool wantrecord; /* actually means "need record" */ /*I/O*/ sf_file Fvel; sf_file left, right, leftb, rightb; sf_file Fsrc, Frcd/*source and record*/; sf_file Ftmpwf, Ftmpbwf; sf_file Fimg; sf_axis at, ax, az; /*grid index variables*/ int nx, nz, nt, wfnt; int nzx, nx2, nz2, n2, m2, pad1, nk; int ix, it; int nxb, nzb; float dt, dx, dz, wfdt; float ox, oz; /*source/geophone location*/ float slx, slz; int spx, spz; float gdep; int gpz,gpx,gpl; /*geophone depth/x-crd/length*/ /*Model*/ sf_complex **lt, **rt; sf_complex **ltb, **rtb; /*Data*/ sf_complex ***wavefld, ***wavefld2; sf_complex **record, **img; float **sill; int snpint; /*source*/ sf_complex *ww; float *rr; int rectz,rectx,repeat; /*smoothing parameters*/ float trunc; /*abc boundary*/ int top,bot,lft,rht; /*memoray*/ int tmpint; tstart = clock(); sf_init(argc, argv); if (!sf_getbool("verb", &verb)) verb=false; /*verbosity*/ if (!sf_getbool("adj", &adj)) adj=true; /*migration*/ if (!sf_getbool("wantwf", &wantwf)) wantwf=false; /*output forward and backward wavefield*/ if (!sf_getbool("wantrecord", &wantrecord)) wantrecord=true; /*if n, using record data generated by this program */ /*Set I/O file*/ if (adj) { /* migration */ if (wantrecord) { Frcd = sf_input("in"); /*record from elsewhere*/ Fsrc = sf_input("src"); /*source wavelet*/ } else { Frcd = sf_output("rec"); /*record produced by forward modeling*/ Fsrc = sf_input("in"); /*source wavelet*/ } Fimg = sf_output("out"); } else { /* modeling */ Fimg = sf_input("in"); Frcd = sf_output("out"); Fsrc = sf_input("src"); /*source wavelet*/ } Fvel = sf_input("vel"); /*velocity - just for model dimension*/ if (wantwf) { Ftmpwf = sf_output("tmpwf");/*wavefield snap*/ Ftmpbwf = sf_output("tmpbwf"); } /*--- Axes parameters ---*/ at = sf_iaxa(Fsrc, 1); nt = sf_n(at); dt = sf_d(at); az = sf_iaxa(Fvel, 1); nzb = sf_n(az); dz = sf_d(az); oz = sf_o(az); ax = sf_iaxa(Fvel, 2); nxb = sf_n(ax); dx = sf_d(ax); ox = sf_o(ax); nzx = nzb*nxb; /*--- parameters of source ---*/ if (!sf_getfloat("srctrunc", &trunc)) trunc=0.4; if (!sf_getint("rectz", &rectz)) rectz=1; if (!sf_getint("rectx", &rectx)) rectx=1; if (!sf_getint("repeat", &repeat)) repeat=0; ww=sf_complexalloc(nt); rr=sf_floatalloc(nzx); /* propagator matrices */ left = sf_input("left"); right = sf_input("right"); leftb = sf_input("leftb"); rightb = sf_input("rightb"); if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */ nz2 = kiss_fft_next_fast_size(nzb*pad1); nx2 = kiss_fft_next_fast_size(nxb); nk = nz2*nx2; /*wavenumber*/ if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx); if (!sf_histint(left,"n2",&m2)) sf_error("Need n2= in left"); if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2); if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk); lt = sf_complexalloc2(nzx,m2); rt = sf_complexalloc2(m2,nk); sf_complexread(lt[0],nzx*m2,left); sf_complexread(rt[0],m2*nk,right); ltb = sf_complexalloc2(nzx,m2); rtb = sf_complexalloc2(m2,nk); sf_complexread(ltb[0],nzx*m2,leftb); sf_complexread(rtb[0],m2*nk,rightb); sf_fileclose(left); sf_fileclose(right); sf_fileclose(leftb); sf_fileclose(rightb); /* abc parameters */ if (!sf_getint("top", &top)) top=40; if (!sf_getint("bot", &bot)) bot=40; if (!sf_getint("lft", &lft)) lft=40; if (!sf_getint("rht", &rht)) rht=40; /* Width of abc layer */ nz = nzb - top - bot; nx = nxb - lft - rht; /*Geometry parameters*/ geopar geop; geop = creategeo(); /*source loaction parameters*/ if (!sf_getfloat("slx", &slx)) slx=-1.0; /*source location x */ if (!sf_getint("spx", &spx)) spx=-1; /*source location x (index)*/ if((slx<0 && spx <0) || (slx>=0 && spx >=0 )) sf_error("Need src location"); if (slx >= 0 ) spx = (int)((slx-ox)/dx+0.5); if (!sf_getfloat("slz", &slz)) slz=-1.0; /* source location z */ if (!sf_getint("spz", &spz)) spz=-1; /*source location z (index)*/ if((slz<0 && spz <0) || (slz>=0 && spz >=0 )) sf_error("Need src location"); if (slz >= 0 ) spz = (int)((slz-ox)/dz+0.5); if (!sf_getfloat("gdep", &gdep)) gdep=-1.0; /* recorder depth on grid*/ if (!sf_getint("gpz", &gpz)) gpz=0; /* recorder depth on index*/ if (!sf_getint("gpx", &gpx)) sf_error("Need receiver starting location"); /* recorder starting location on index*/ if (!sf_getint("gpl", &gpl)) sf_error("Need receiver length"); /* recorder length on index*/ if ( gdep>=oz) { gpz = (int)((gdep-oz)/dz+0.5);} if (gpz < 0.0) sf_error("gdep need to be >=oz"); /*source and receiver location*/ if (!sf_getint("snapinter", &snpint)) snpint=10; /* snap interval */ /*load source wavelet and reflectivity map*/ ww=sf_complexalloc(nt); sf_complexread(ww,nt,Fsrc); sf_fileclose(Fsrc); reflgen(nzb, nxb, spz+top, spx+lft, rectz, rectx, repeat, rr); /*check record data*/ if (adj && wantrecord){ sf_histint(Frcd,"n1", &tmpint); if (tmpint != nt ) sf_error("Error parameter n1 in record!"); sf_histint(Frcd,"n2", &tmpint); if (tmpint != gpl ) sf_error("Error parameter n2 in record!"); } geop->nx = nx; geop->nz = nz; geop->nxb = nxb; geop->nzb = nzb; geop->dx = dx; geop->dz = dz; geop->ox = ox; geop->oz = oz; geop->snpint = snpint; geop->spx = spx; geop->spz = spz; geop->gpz = gpz; geop->gpx = gpx; geop->gpl = gpl; geop->top = top; geop->bot = bot; geop->lft = lft; geop->rht = rht; geop->nt = nt; geop->dt = dt; geop->trunc = trunc; /* wavefield and record */ wfnt = (int)(nt-1)/snpint+1; wfdt = dt*snpint; record = sf_complexalloc2(nt, gpl); wavefld = sf_complexalloc3(nz, nx, wfnt); sill = sf_floatalloc2(nz, nx); if (wantwf) wavefld2= sf_complexalloc3(nz, nx, wfnt); else wavefld2=NULL; /*image*/ img = sf_complexalloc2(nz, nx); if (verb) { sf_warning("============================"); sf_warning("nx=%d nz=%d nt=%d", geop->nx, geop->nz, geop->nt); sf_warning("nxb=%d nzb=%d ", geop->nxb, geop->nzb); sf_warning("dx=%f dz=%f dt=%f", geop->dx, geop->dz, geop->dt); sf_warning("top=%d bot=%d lft=%d rht=%d", geop->top, geop->bot, geop->lft, geop->rht); sf_warning("rectz=%d rectx=%d repeat=%d srctrunc=%f",rectz,rectx,repeat,geop->trunc); sf_warning("spx=%d spz=%d gpz=%d gpx=%d gpl=%d snpint=%d", spx, spz, gpz, gpx, gpl, snpint); sf_warning("wfdt=%f wfnt=%d ", wfdt, wfnt); sf_warning("============================"); } /* write record */ sf_setn(ax, gpl); sf_setn(az, nz); if (adj) { /* migration */ if(!wantrecord) { sf_oaxa(Frcd, at, 1); sf_oaxa(Frcd, ax, 2); sf_settype(Frcd,SF_COMPLEX); } sf_setn(ax, nx); /*write image*/ sf_oaxa(Fimg, az, 1); sf_oaxa(Fimg, ax, 2); sf_settype(Fimg,SF_COMPLEX); } else { /* modeling */ sf_oaxa(Frcd, at, 1); sf_oaxa(Frcd, ax, 2); sf_settype(Frcd,SF_COMPLEX); } if (wantwf) { sf_setn(ax, nx); /*write temp wavefield */ sf_setn(at, wfnt); sf_setd(at, wfdt); sf_oaxa(Ftmpwf, az, 1); sf_oaxa(Ftmpwf, ax, 2); sf_oaxa(Ftmpwf, at, 3); sf_settype(Ftmpwf,SF_COMPLEX); /*write temp wavefield */ sf_oaxa(Ftmpbwf, az, 1); sf_oaxa(Ftmpbwf, ax, 2); sf_oaxa(Ftmpbwf, at, 3); sf_settype(Ftmpbwf,SF_COMPLEX); } if (!adj) sf_complexread(img[0],nx*nz,Fimg); lrosfor2(wavefld, sill, record, verb, lt, rt, m2, geop, ww, rr, pad1); if(adj && wantrecord) sf_complexread(record[0], gpl*nt, Frcd); lrosback2(img, wavefld, sill, record, adj, verb, wantwf, ltb, rtb, m2, geop, pad1, wavefld2); if (!wantrecord || !adj) { for (ix=0; ix<gpl; ix++) { sf_complexwrite(record[ix], nt, Frcd); } } if (adj) { for (ix=0; ix<nx; ix++) sf_complexwrite(img[ix], nz, Fimg); } if (wantwf) { for (it=0; it<wfnt; it++) for ( ix=0; ix<nx; ix++) { sf_complexwrite(wavefld[it][ix], nz, Ftmpwf); sf_complexwrite(wavefld2[it][ix],nz, Ftmpbwf); } } tend = clock(); duration=(double)(tend-tstart)/CLOCKS_PER_SEC; sf_warning(">> The CPU time of single shot migration is: %f seconds << ", duration); exit(0); }
int main(int argc, char* argv[]) { bool inv, verb; int i1, n1, iw, nt, nw, i2, n2, rect0, niter, n12, n1w; int m[SF_MAX_DIM], *rect; float t, d1, w, w0, dw, mean=0.0f, alpha; float *trace, *kbsc, *mkbsc, *sscc, *mm, *ww; sf_complex *outp, *cbsc; sf_file in, out, mask, weight, basis; sf_init(argc,argv); in = sf_input("in"); out = sf_output("out"); if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input"); if (!sf_histfloat(in,"d1",&d1)) d1=1.; if (!sf_getbool("inv",&inv)) inv=false; /* if y, do inverse transform */ if (!sf_getbool("verb",&verb)) verb = false; /* verbosity flag */ if (NULL != sf_getstring("basis")) { basis = sf_output("basis"); sf_settype(basis,SF_COMPLEX); } else { basis = NULL; } if (!inv) { if (!sf_getint("nw",&nw)) { /* number of frequencies */ nt = 2*kiss_fft_next_fast_size((n1+1)/2); nw = nt/2+1; dw = 1./(nt*d1); w0 = 0.; } else { if (!sf_getfloat("dw",&dw)) { /* frequency step */ nt = 2*kiss_fft_next_fast_size((n1+1)/2); dw = 1./(nt*d1); } if (!sf_getfloat("w0",&w0)) w0=0.; /* first frequency */ } n2 = sf_leftsize(in,1); sf_shiftdim(in, out, 2); sf_putint(out,"n2",nw); sf_putfloat(out,"d2",dw); sf_putfloat(out,"o2",w0); sf_putstring(out,"label2","Frequency"); sf_putstring(out,"unit2","Hz"); sf_settype(out,SF_COMPLEX); if (!sf_getint("rect",&rect0)) rect0=10; /* smoothing radius (in time, samples) */ if (!sf_getint("niter",&niter)) niter=100; /* number of inversion iterations */ if (!sf_getfloat("alpha",&alpha)) alpha=0.; /* frequency adaptivity */ for(i2=0; i2 < SF_MAX_DIM; i2 ++) { m[i2] = 1; } m[0] = n1; } else { n2 = sf_leftsize(in,2); if (!sf_histint(in,"n2",&nw)) sf_error("No n2= in input"); if (!sf_histfloat(in,"d2",&dw)) sf_error("No d2= in input"); if (!sf_histfloat(in,"o2",&w0)) sf_error("No o2= in input"); sf_unshiftdim(in, out, 2); sf_settype(out,SF_FLOAT); } if (NULL != basis) { sf_shiftdim(in, basis, 2); sf_putint(basis,"n2",nw); sf_putfloat(basis,"d2",dw); sf_putfloat(basis,"o2",w0); sf_putstring(basis,"label2","Frequency"); sf_putstring(basis,"unit2","Hz"); } n1w = n1*nw; n12 = 2*n1w; dw *= 2.*SF_PI; w0 *= 2.*SF_PI; trace = sf_floatalloc(n1); kbsc = sf_floatalloc(n12); outp = sf_complexalloc(n1w); cbsc = sf_complexalloc(n1w); rect = sf_intalloc(2*nw); for (iw=0; iw < nw; iw++) { rect[iw+nw] = rect[iw] = SF_MAX(1, (int) rect0/(1.0+alpha*iw/nw)); } if (!inv) { sscc = sf_floatalloc(n12); nmultidivn_init(2*nw, 1, n1, m, rect, kbsc, (bool) (verb && (n2 < 500))); } else { sscc = NULL; } if (NULL != sf_getstring("mask")) { /* data weight */ mask = sf_input("mask"); mm = sf_floatalloc(n1); } else { mask = NULL; mm = NULL; } if (NULL != sf_getstring("weight")) { /* model weight */ weight = sf_input("weight"); ww = sf_floatalloc(n1w); } else { weight = NULL; ww = NULL; } /* sin and cos basis */ for (iw=0; iw < nw; iw++) { w = w0 + iw*dw; for (i1=0; i1 < n1; i1++) { if (0.==w) { /* zero frequency */ kbsc[iw*n1+i1] = 0.; } else { t = i1*d1; kbsc[iw*n1+i1] = sinf(w*t); } } } for (iw=0; iw < nw; iw++) { w = w0 + iw*dw; for (i1=0; i1 < n1; i1++) { if (0.==w) { /* zero frequency */ kbsc[(iw+nw)*n1+i1] = 0.5; } else { t = i1*d1; kbsc[(iw+nw)*n1+i1] = cosf(w*t); } cbsc[iw*n1+i1] = sf_cmplx(kbsc[(iw+nw)*n1+i1], kbsc[iw*n1+i1]); } } if (NULL != mm || NULL != ww) { mkbsc = sf_floatalloc(n12); for (i1=0; i1 < n12; i1++) { mkbsc[i1] = kbsc[i1]; } } else { mkbsc = NULL; mean = 0.; for (i1=0; i1 < n12; i1++) { mean += kbsc[i1]*kbsc[i1]; } mean = sqrtf (mean/(n12)); for (i1=0; i1 < n12; i1++) { kbsc[i1] /= mean; } } for (i2=0; i2 < n2; i2++) { sf_warning("slice %d of %d;",i2+1,n2); if (NULL != basis) sf_complexwrite(cbsc,n1w,basis); if (NULL != mm || NULL != ww) { for (i1=0; i1 < n12; i1++) { kbsc[i1] = mkbsc[i1]; } if (NULL != mm) { sf_floatread(mm,n1,mask); for (iw=0; iw < 2*nw; iw++) { for (i1=0; i1 < n1; i1++) { kbsc[iw*n1+i1] *= mm[i1]; } } } if (NULL != ww) { sf_floatread(ww,n1w,weight); for (iw=0; iw < nw; iw++) { for (i1=0; i1 < n1; i1++) { kbsc[iw*n1+i1] *= ww[iw*n1+i1]; kbsc[(iw+nw)*n1+i1] *= ww[iw*n1+i1]; } } } mean = 0.; for (i1=0; i1 < n12; i1++) { mean += kbsc[i1]*kbsc[i1]; } mean = sqrtf (mean/(n12)); for (i1=0; i1 < n12; i1++) { kbsc[i1] /= mean; } } if (!inv) { sf_floatread(trace,n1,in); if (NULL != mm) { for (i1=0; i1 < n1; i1++) { trace[i1] *= mm[i1]; } } for(i1=0; i1 < n1; i1++) { trace[i1] /= mean; } nmultidivn (trace,sscc,niter); for (iw=0; iw < nw; iw++) { for (i1=0; i1 < n1; i1++) { outp[iw*n1+i1] = sf_cmplx(sscc[(iw+nw)*n1+i1], sscc[iw*n1+i1]); } } if (NULL != ww) { for (i1=0; i1 < n1w; i1++) { #ifdef SF_HAS_COMPLEX_H outp[i1] *= ww[i1]; #else outp[i1] = sf_crmul(outp[i1],ww[i1]); #endif } } sf_complexwrite(outp,n1w,out); } else { for (i1=0; i1 < n1; i1++) { trace[i1] = 0.; } sf_complexread(outp,n1w,in); for (iw=0; iw < nw; iw++) { for (i1=0; i1 < n1; i1++) { trace[i1] += crealf(outp[iw*n1+i1])*kbsc[(iw+nw)*n1+i1] *mean+cimagf(outp[iw*n1+i1])*kbsc[iw*n1+i1]*mean; if (NULL != mm) trace[i1] *= mm[i1]; } } sf_floatwrite(trace,n1,out); } } sf_warning("."); exit(0); }
int main (int argc, char *argv[]) { int adj; /* forward or adjoint */ int eic; /* EIC or CIC */ bool verb; /* verbosity */ float eps; /* dip filter constant */ int nrmax; /* number of reference velocities */ float dtmax; /* time error */ int pmx,pmy; /* padding in the k domain */ int tmx,tmy; /* boundary taper size */ int nhx, nhy, nhz, nht, nc; int nhx2,nhy2,nhz2,nht2; float dht, oht; sf_axis amx,amy,az; sf_axis alx,aly; sf_axis aw,ae,ac,aa; sf_axis ahx,ahy,ahz,aht; /* I/O files */ sf_file Bws=NULL; /* background wavefield file Bws */ sf_file Bwr=NULL; /* background wavefield file Bwr */ sf_file Bs=NULL; /* background slowness file Bs */ sf_file Ps=NULL; /* slowness perturbation file Ps */ sf_file Pi=NULL; /* image perturbation file Pi */ sf_file Fc=NULL; /* CIP coordinates */ sf_file Pws=NULL; /* perturbed wavefield file Pws */ sf_file Pwr=NULL; /* perturbed wavefield file Pwr */ sf_file Pti=NULL; int ompnth=1; wexcub3d cub; /* wavefield hypercube */ wexcip3d cip; /* CIP gathers */ wextap3d tap; /* tapering */ wexssr3d ssr; /* SSR operator */ wexlsr3d lsr; /* LSR operator */ wexslo3d slo; /* slowness */ wexmvaop3d mva; float dsmax; /*------------------------------------------------------------*/ sf_init(argc,argv); /* OMP parameters */ #ifdef _OPENMP ompnth=omp_init(); #endif if (!sf_getbool( "verb",&verb )) verb = true; /* verbosity flag */ if (!sf_getint( "adj",&adj )) sf_error("Specify adjoint!"); /* y=ADJ Back-projection; n=FWD Forward Scattering */ if (!sf_getint( "feic",&eic )) sf_error("Specify EIC!"); /* extended I.C. flag */ if (!sf_getfloat( "eps",&eps )) eps = 0.01; /* stability parameter */ if (!sf_getint( "nrmax",&nrmax)) nrmax = 1; /* max number of refs */ if (!sf_getfloat("dtmax",&dtmax)) dtmax = 0.004; /* max time error */ if (!sf_getint( "pmx",&pmx )) pmx = 0; /* padding on x */ if (!sf_getint( "pmy",&pmy )) pmy = 0; /* padding on y */ if (!sf_getint( "tmx",&tmx )) tmx = 0; /* taper on x */ if (!sf_getint( "tmy",&tmy )) tmy = 0; /* taper on y */ ae = sf_maxa(1,0,1); nhx=nhy=nhz=nht=nc=nhx2=nhy2=nhz2=nht2=0; oht = dht = 0.0; /*------------------------------------------------------------*/ /* slowness */ Bs = sf_input("slo"); alx = sf_iaxa(Bs,1); sf_setlabel(alx,"lx"); aly = sf_iaxa(Bs,2); sf_setlabel(aly,"ly"); az = sf_iaxa(Bs,3); sf_setlabel(az, "z"); /*------------------------------------------------------------*/ /* input file */ if(adj) Pi = sf_input("in"); else Ps = sf_input("in"); /*------------------------------------------------------------*/ /* wavefield */ Bws = sf_input("swfl"); Bwr = sf_input("rwfl"); amx = sf_iaxa(Bws,1); sf_setlabel(amx,"mx"); amy = sf_iaxa(Bws,2); sf_setlabel(amy,"my"); aw = sf_iaxa(Bws,4); sf_setlabel(aw ,"w" ); Pws = sf_tmpfile(NULL); sf_settype(Pws,SF_COMPLEX); Pwr = sf_tmpfile(NULL); sf_settype(Pwr,SF_COMPLEX); /*------------------------------------------------------------*/ cub = wex_cube(verb, amx,amy,az, alx,aly, aw, ae, eps, ompnth); dsmax = dtmax/cub->az.d; /*------------------------------------------------------------*/ /* init structures */ tap = wextap_init(cub->amx.n, cub->amy.n, 1, SF_MIN(tmx,cub->amx.n-1), /* tmx */ SF_MIN(tmy,cub->amy.n-1), /* tmy */ 0, true,true,false); slo = wexslo_init(cub,Bs,nrmax,dsmax); ssr = wexssr_init(cub,slo,pmx,pmy,tmx,tmy,dsmax); lsr = wexlsr_init(cub,pmx,pmy,dsmax); /*------------------------------------------------------------*/ Pti = sf_tmpfile(NULL); sf_settype(Pti,SF_COMPLEX); /*------------------------------------------------------------*/ /* WEMVA */ if(adj) { sf_warning("adjoint operator..."); if(eic){ ahx = sf_iaxa(Pi,1); sf_setlabel(ahx,"hx"); ahy = sf_iaxa(Pi,2); sf_setlabel(ahy,"hy"); ahz = sf_iaxa(Pi,3); sf_setlabel(ahz,"hz"); aht = sf_iaxa(Pi,4); sf_setlabel(aht,"ht"); dht = sf_d(aht); oht = sf_o(aht); nhx2 = sf_n(ahx); nhx = (nhx2-1)/2; nhy2 = sf_n(ahy); nhy = (nhy2-1)/2; nhz2 = sf_n(ahz); nhz = (nhz2-1)/2; nht2 = sf_n(aht); nht = (nht2-1)/2; /* CIP coordinates */ Fc = sf_input ("cc" ); ac = sf_iaxa(Fc,2); sf_setlabel(ac,"cc"); sf_setunit(ac,""); nc = sf_n(ac); } cip = wexcip_init(cub,nhx,nhy,nhz,nht,nhx2,nhy2,nhz2,nht2,nc,dht,oht,Fc,eic); mva = wexmva_init(cub,cip); Ps = sf_output("out"); sf_settype(Ps,SF_COMPLEX); sf_oaxa(Ps,amx,1); sf_oaxa(Ps,amy,2); sf_oaxa(Ps,az, 3); if(eic){ sf_oaxa(Ps,ae, 4); sf_oaxa(Ps,ae, 5);} /* Adjoint I.C. operator, dI -> dW */ wexcip_adj(cub,cip,Bwr,Pws,Pi,eic,1,1); /* Ws dR */ wexcip_adj(cub,cip,Bws,Pwr,Pi,eic,0,0); /* Wr dR */ sf_filefresh(Pws); sf_filefresh(Pwr); /* Adjoint WEMVA operator, dW -> dS */ wexmva(mva,adj,cub,ssr,lsr,tap,slo,Bws,Bwr,Pws,Pwr,Ps); } else { /* set up the I/O of output CIP gathers */ Pi = sf_output("out"); sf_settype(Pi,SF_COMPLEX); if(eic){ /* CIP coordinates */ Fc = sf_input ("cc" ); ac = sf_iaxa(Fc,2); sf_setlabel(ac,"cc"); sf_setunit(ac,""); nc = sf_n(ac); if(! sf_getint("nhx",&nhx)) nhx=0; /* number of lags on the x axis */ if(! sf_getint("nhy",&nhy)) nhy=0; /* number of lags on the y axis */ if(! sf_getint("nhz",&nhz)) nhz=0; /* number of lags on the z axis */ if(! sf_getint("nht",&nht)) nht=0; /* number of lags on the t axis */ if(! sf_getfloat("dht",&dht)) sf_error("need dht"); oht = -nht*dht; nhx2=2*nhx+1; nhy2=2*nhy+1; nhz2=2*nhz+1; nht2=2*nht+1; aa=sf_maxa(nhx2,-nhx*cub->amx.d,cub->amx.d); sf_setlabel(aa,"hx"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Pi,aa,1); aa=sf_maxa(nhy2,-nhy*cub->amy.d,cub->amy.d); sf_setlabel(aa,"hy"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Pi,aa,2); aa=sf_maxa(nhz2,-nhz*cub->az.d,cub->az.d); sf_setlabel(aa,"hz"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Pi,aa,3); aa=sf_maxa(nht2,-nht*dht,dht); sf_setlabel(aa,"ht"); sf_setunit(aa,"s"); if(verb) sf_raxa(aa); sf_oaxa(Pi,aa,4); sf_oaxa(Pi,ac,5); } else{ sf_oaxa(Pi,amx,1); sf_oaxa(Pi,amy,2); sf_oaxa(Pi,az, 3); } cip = wexcip_init(cub,nhx,nhy,nhz,nht,nhx2,nhy2,nhz2,nht2,nc,dht,oht,Fc,eic); mva = wexmva_init(cub,cip); /* WEMVA operator, dS -> dW */ wexmva(mva,adj,cub,ssr,lsr,tap,slo,Bws,Bwr,Pws,Pwr,Ps); sf_filefresh(Pws); sf_filefresh(Pwr); /* I.C. operator, dW -> dI */ wexcip_for(cub,cip,Bws,Pwr,Pti,eic,0,0); /* CONJ( Ws) dWr */ sf_seek(Pti,(off_t)0,SEEK_SET); wexcip_for(cub,cip,Pws,Bwr,Pti,eic,0,1); /* CONJ(dWs) Wr */ sf_filefresh(Pti); sf_filecopy(Pi,Pti,SF_COMPLEX); } /*------------------------------------------------------------*/ /* close structures */ wexslo_close(slo); wexssr_close(cub,ssr); wextap2D_close(tap); wexmva_close(mva); wexcip_close(cip,eic); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* close files */ if (Ps!=NULL) sf_fileclose(Ps); if (Fc!=NULL) sf_fileclose(Fc); if (Pi!=NULL) sf_fileclose(Pi); if (Bws!=NULL) sf_fileclose(Bws); if (Bwr!=NULL) sf_fileclose(Bwr); if (Pws!=NULL) sf_tmpfileclose(Pws); if (Pwr!=NULL) sf_tmpfileclose(Pwr); if (Pti!=NULL) sf_tmpfileclose(Pti); /*------------------------------------------------------------*/ exit (0); }
int main(int argc, char* argv[]) { /*------------------------------------------------------------*/ /* Execution control, I/O files and geometry */ /*------------------------------------------------------------*/ bool verb,fsrf,snap,back,esou; /* execution flags */ int jsnap,ntsnap,jdata; /* jump along axes */ int shft; /* time shift for wavefield matching in RTM */ /* I/O files */ sf_file Fwav=NULL; /* wavelet */ sf_file Fdat=NULL; /* data */ sf_file Fsou=NULL; /* sources */ sf_file Frec=NULL; /* receivers */ sf_file Fccc=NULL; /* velocity */ sf_file Frkp=NULL; /* app. rank */ sf_file Fltp=NULL; /* left mat */ sf_file Frtp=NULL; /* right mat */ sf_file Fwfp=NULL; /* wavefield */ sf_file Frks=NULL; /* app. rank */ sf_file Flts=NULL; /* left mat */ sf_file Frts=NULL; /* right mat */ sf_file Fwfs=NULL; /* wavefield */ /* cube axes */ sf_axis at,ax,ay,az; /* time, x, y, z */ sf_axis asx,asy,arx,ary,ac; /* sou, rec-x, rec-y, component */ /* dimension, index and interval */ int nt,nz,nx,ny,ns,nr,nc,nb; int it,iz,ix,iy; float dt,dz,dx,dy; int nxyz, nk; /* FDM and KSP structure */ //!!!JS fdm3d fdm=NULL; dft3d dft=NULL; clr3d clr_p=NULL, clr_s=NULL; /* I/O arrays for sou & rec */ sf_complex***ww=NULL; /* wavelet */ pt3d *ss=NULL; /* sources */ pt3d *rr=NULL; /* receivers */ sf_complex **dd=NULL; /* data */ /*------------------------------------------------------------*/ /* displacement: uo = U @ t; up = U @ t+1 */ /*------------------------------------------------------------*/ sf_complex ***uox, ***uoy, ***uoz, **uo; sf_complex ***uox_p, ***uoy_p, ***uoz_p, **uo_p; sf_complex ***uox_s, ***uoy_s, ***uoz_s, **uo_s; /*sf_complex ***upx, ***upy, ***upz, **up;*/ /*------------------------------------------------------------*/ /* lowrank decomposition arrays */ /*------------------------------------------------------------*/ int ntmp, *n2s_p, *n2s_s; sf_complex **lt_p, **rt_p, **lt_s, **rt_s; /*------------------------------------------------------------*/ /* linear interpolation weights/indices */ /*------------------------------------------------------------*/ lint3d cs,cr; /* for injecting source and extracting data */ /* Gaussian bell */ int nbell; /*------------------------------------------------------------*/ /* wavefield cut params */ /*------------------------------------------------------------*/ sf_axis acz=NULL,acx=NULL,acy=NULL; int nqz,nqx,nqy; float oqz,oqx,oqy; float dqz,dqx,dqy; sf_complex***uc=NULL; /* tmp array for output wavefield snaps */ /*------------------------------------------------------------*/ /* init RSF */ /*------------------------------------------------------------*/ sf_init(argc,argv); /*------------------------------------------------------------*/ /* OMP parameters */ /*------------------------------------------------------------*/ #ifdef _OPENMP omp_init(); #endif /*------------------------------------------------------------*/ /* read execution flags */ /*------------------------------------------------------------*/ if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */ if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */ if(! sf_getbool("back",&back)) back=false; /* backward extrapolation flag (for rtm) */ if(! sf_getbool("esou",&esou)) esou=false; /* explosive force source */ /*------------------------------------------------------------*/ /* I/O files */ /*------------------------------------------------------------*/ Fwav = sf_input ("in" ); /* wavelet */ Fdat = sf_output("out"); /* data */ Fsou = sf_input ("sou"); /* sources */ Frec = sf_input ("rec"); /* receivers */ Fccc = sf_input ("ccc"); /* stiffness */ Frkp = sf_input ("rkp"); /* app. rank */ Fltp = sf_input ("ltp"); /* left mat */ Frtp = sf_input ("rtp"); /* right mat */ Fwfp = sf_output("wfp"); /* wavefield */ Frks = sf_input ("rks"); /* app. rank */ Flts = sf_input ("lts"); /* left mat */ Frts = sf_input ("rts"); /* right mat */ Fwfs = sf_output("wfs"); /* wavefield */ /*------------------------------------------------------------*/ /* axes */ /*------------------------------------------------------------*/ at = sf_iaxa(Fwav,4); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */ az = sf_iaxa(Fccc,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */ ax = sf_iaxa(Fccc,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space x */ ay = sf_iaxa(Fccc,3); sf_setlabel(ay,"y"); if(verb) sf_raxa(ay); /* space y */ asx = sf_iaxa(Fsou,2); sf_setlabel(asx,"sx"); if(verb) sf_raxa(asx); /* sources x */ asy = sf_iaxa(Fsou,3); sf_setlabel(asy,"sy"); if(verb) sf_raxa(asy); /* sources y */ arx = sf_iaxa(Frec,2); sf_setlabel(arx,"rx"); if(verb) sf_raxa(arx); /* receivers x */ ary = sf_iaxa(Frec,3); sf_setlabel(ary,"ry"); if(verb) sf_raxa(ary); /* receivers y */ nt = sf_n(at); dt = sf_d(at); nz = sf_n(az); dz = sf_d(az); nx = sf_n(ax); dx = sf_d(ax); ny = sf_n(ay); dy = sf_d(ay); ns = sf_n(asx)*sf_n(asy); nr = sf_n(arx)*sf_n(ary); /*------------------------------------------------------------*/ /* other execution parameters */ /*------------------------------------------------------------*/ if(! sf_getint("nbell",&nbell)) nbell=NOP; /* bell size */ if(verb) sf_warning("nbell=%d",nbell); if(! sf_getint("jdata",&jdata)) jdata=1; if(snap) { /* save wavefield every *jsnap* time steps */ if(! sf_getint("jsnap",&jsnap)) jsnap=nt; } if(back) { shft = (nt-1)%jsnap; sf_warning("For backward extrapolation, make sure nbell(%d)=0",nbell); } else shft = 0; /*------------------------------------------------------------*/ /* expand domain for FD operators and ABC */ /*------------------------------------------------------------*/ if( !sf_getint("nb",&nb)) nb=NOP; fdm=fdutil3d_init(verb,fsrf,az,ax,ay,nb,1); if(nbell) fdbell3d_init(nbell); sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az); sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax); sf_setn(ay,fdm->nypad); sf_seto(ay,fdm->oypad); if(verb) sf_raxa(ay); /*------------------------------------------------------------*/ /* 3D vector components */ /*------------------------------------------------------------*/ nc=3; ac=sf_maxa(nc,0,1); /* output 3 cartesian components */ /*------------------------------------------------------------*/ /* setup output data header */ /*------------------------------------------------------------*/ sf_settype(Fdat,SF_COMPLEX); sf_oaxa(Fdat,arx,1); sf_oaxa(Fdat,ary,2); sf_oaxa(Fdat,ac,3); sf_setn(at,nt/jdata); sf_setd(at,dt*jdata); sf_oaxa(Fdat,at,4); /* setup output wavefield header */ if(snap) { if(!sf_getint ("nqz",&nqz)) nqz=sf_n(az); if(!sf_getint ("nqx",&nqx)) nqx=sf_n(ax); if(!sf_getint ("nqy",&nqy)) nqy=sf_n(ay); if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az); if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax); if(!sf_getfloat("oqy",&oqy)) oqy=sf_o(ay); dqz=sf_d(az); dqx=sf_d(ax); dqy=sf_d(ay); acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz); acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx); acy = sf_maxa(nqy,oqy,dqy); sf_raxa(acy); uc=sf_complexalloc3(sf_n(acz),sf_n(acx),sf_n(acy)); ntsnap=0; /* ntsnap = it/jsnap+1; */ for(it=0; it<nt; it++) { if(it%jsnap==0) ntsnap++; } sf_setn(at, ntsnap); sf_setd(at,dt*jsnap); if(verb) sf_raxa(at); sf_settype(Fwfp,SF_COMPLEX); sf_oaxa(Fwfp,acz,1); sf_oaxa(Fwfp,acx,2); sf_oaxa(Fwfp,acy,3); sf_oaxa(Fwfp,ac, 4); sf_oaxa(Fwfp,at, 5); sf_settype(Fwfs,SF_COMPLEX); sf_oaxa(Fwfs,acz,1); sf_oaxa(Fwfs,acx,2); sf_oaxa(Fwfs,acy,3); sf_oaxa(Fwfs,ac, 4); sf_oaxa(Fwfs,at, 5); } /*------------------------------------------------------------*/ /* source and data array */ /*------------------------------------------------------------*/ ww=sf_complexalloc3(ns,nc,nt); /* Fast axis: n_sou > n_comp > n_time */ sf_complexread(ww[0][0],nt*nc*ns,Fwav); dd=sf_complexalloc2(nr,nc); /*------------------------------------------------------------*/ /* setup source/receiver coordinates */ /*------------------------------------------------------------*/ ss = (pt3d*) sf_alloc(ns,sizeof(*ss)); rr = (pt3d*) sf_alloc(nr,sizeof(*rr)); pt3dread1(Fsou,ss,ns,3); /* read (x,y,z) coordinates */ pt3dread1(Frec,rr,nr,3); /* read (x,y,z) coordinates */ /* calculate 3d linear interpolation coef for sou & rec */ cs = lint3d_make(ns,ss,fdm); cr = lint3d_make(nr,rr,fdm); /*------------------------------------------------------------*/ /* allocate and initialize wavefield arrays */ /*------------------------------------------------------------*/ /* z-component */ uoz=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uoz_p=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uoz_s=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); /*upz=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);*/ /* x-component */ uox=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uox_p=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uox_s=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); /*upx=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);*/ /* y-component */ uoy=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uoy_p=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uoy_s=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); /*upy=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);*/ /* wavefield vector */ uo = (sf_complex**) sf_alloc(3,sizeof(sf_complex*)); uo[0] = uox[0][0]; uo[1] = uoy[0][0]; uo[2] = uoz[0][0]; uo_p = (sf_complex**) sf_alloc(3,sizeof(sf_complex*)); uo_p[0] = uox_p[0][0]; uo_p[1] = uoy_p[0][0]; uo_p[2] = uoz_p[0][0]; uo_s = (sf_complex**) sf_alloc(3,sizeof(sf_complex*)); uo_s[0] = uox_s[0][0]; uo_s[1] = uoy_s[0][0]; uo_s[2] = uoz_s[0][0]; /*up = (sf_complex**) sf_alloc(3,sizeof(sf_complex*));*/ /*up[0] = upx[0][0]; up[1] = upy[0][0]; up[2] = upz[0][0];*/ /* initialize fft and lrk */ dft = dft3d_init(1,false,false,fdm); nxyz= fdm->nypad*fdm->nxpad*fdm->nzpad; nk = dft->nky *dft->nkx *dft->nkz; /*------------------------------------------------------------*/ /* allocation I/O arrays */ /*------------------------------------------------------------*/ n2s_p = sf_intalloc(9); sf_intread(n2s_p,9,Frkp); clr_p = clr3d_make2(n2s_p,fdm); n2s_s = sf_intalloc(9); sf_intread(n2s_s,9,Frks); clr_s = clr3d_make2(n2s_s,fdm); if (clr_p->n2_max > clr_s->n2_max) clr3d_init(fdm,dft,clr_p); else clr3d_init(fdm,dft,clr_s); /* check the dimension */ if (!sf_histint(Fltp,"n1",&ntmp) || ntmp != nxyz) sf_error("Need n1=%d in left",nxyz); if (!sf_histint(Fltp,"n2",&ntmp) || ntmp != clr_p->n2_sum) sf_error("Need n2=%d in left",clr_p->n2_sum); if (!sf_histint(Frtp,"n1",&ntmp) || ntmp != nk) sf_error("Need n1=%d in right",nk); if (!sf_histint(Frtp,"n2",&ntmp) || ntmp != clr_p->n2_sum) sf_error("Need n2=%d in right",clr_p->n2_sum); lt_p = sf_complexalloc2(nxyz,clr_p->n2_sum); rt_p = sf_complexalloc2(nk ,clr_p->n2_sum); sf_complexread(lt_p[0],nxyz*clr_p->n2_sum,Fltp); sf_complexread(rt_p[0],nk *clr_p->n2_sum,Frtp); if (!sf_histint(Flts,"n1",&ntmp) || ntmp != nxyz) sf_error("Need n1=%d in left",nxyz); if (!sf_histint(Flts,"n2",&ntmp) || ntmp != clr_s->n2_sum) sf_error("Need n2=%d in left",clr_s->n2_sum); if (!sf_histint(Frts,"n1",&ntmp) || ntmp != nk) sf_error("Need n1=%d in right",nk); if (!sf_histint(Frts,"n2",&ntmp) || ntmp != clr_s->n2_sum) sf_error("Need n2=%d in right",clr_s->n2_sum); lt_s = sf_complexalloc2(nxyz,clr_s->n2_sum); rt_s = sf_complexalloc2(nk ,clr_s->n2_sum); sf_complexread(lt_s[0],nxyz*clr_s->n2_sum,Flts); sf_complexread(rt_s[0],nk *clr_s->n2_sum,Frts); /* initialize to zero */ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,1) \ private(iy,ix,iz) \ shared(fdm,uoz,uox,uoy,uoz_p,uox_p,uoy_p,uoz_s,uox_s,uoy_s) #endif for (iy=0; iy<fdm->nypad; iy++) { for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { uoz [iy][ix][iz]=sf_cmplx(0.,0.); uox [iy][ix][iz]=sf_cmplx(0.,0.); uoy [iy][ix][iz]=sf_cmplx(0.,0.); uoz_p[iy][ix][iz]=sf_cmplx(0.,0.); uox_p[iy][ix][iz]=sf_cmplx(0.,0.); uoy_p[iy][ix][iz]=sf_cmplx(0.,0.); uoz_s[iy][ix][iz]=sf_cmplx(0.,0.); uox_s[iy][ix][iz]=sf_cmplx(0.,0.); uoy_s[iy][ix][iz]=sf_cmplx(0.,0.); /*upz[iy][ix][iz]=sf_cmplx(0.,0.); upx[iy][ix][iz]=sf_cmplx(0.,0.); upy[iy][ix][iz]=sf_cmplx(0.,0.);*/ } } } /*------------------------------------------------------------*/ /*------------------------ MAIN LOOP -------------------------*/ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"\n"); for (it=0; it<nt; it++) { if(verb) sf_warning("it=%d/%d;",it,nt); /*fprintf(stderr,"\b\b\b\b\b%d",it);*/ /*------------------------------------------------------------*/ /* apply lowrank matrix to wavefield vector */ /*------------------------------------------------------------*/ clr3d_apply(uo_p, uo, lt_p, rt_p, fdm, dft, clr_p); clr3d_apply(uo_s, uo, lt_s, rt_s, fdm, dft, clr_s); /*------------------------------------------------------------*/ /* combine P and S wave modes */ /*------------------------------------------------------------*/ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,1) \ private(iy,ix,iz) \ shared(fdm,uoz,uox,uoy,uoz_p,uox_p,uoy_p,uoz_s,uox_s,uoy_s) #endif for (iy=0; iy<fdm->nypad; iy++) { for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { uoz[iy][ix][iz] = uoz_p[iy][ix][iz] + uoz_s[iy][ix][iz]; uox[iy][ix][iz] = uox_p[iy][ix][iz] + uox_s[iy][ix][iz]; uoy[iy][ix][iz] = uoy_p[iy][ix][iz] + uoy_s[iy][ix][iz]; /*upz[iy][ix][iz]=sf_cmplx(0.,0.); upx[iy][ix][iz]=sf_cmplx(0.,0.); upy[iy][ix][iz]=sf_cmplx(0.,0.);*/ } } } /*------------------------------------------------------------*/ /* free surface */ /*------------------------------------------------------------*/ if(fsrf) { /* need to do something here */ } /*------------------------------------------------------------*/ /* inject displacement source */ /*------------------------------------------------------------*/ if(esou) { /* exploding force source */ lint3d_expl_complex(uoz,uox,uoy,ww[it],cs); } else { if(nbell) { lint3d_bell_complex(uoz,ww[it][0],cs); lint3d_bell_complex(uox,ww[it][1],cs); lint3d_bell_complex(uoy,ww[it][2],cs); } else { lint3d_inject_complex(uoz,ww[it][0],cs); lint3d_inject_complex(uox,ww[it][1],cs); lint3d_inject_complex(uoy,ww[it][2],cs); } } /*------------------------------------------------------------*/ /* cut wavefield and save */ /*------------------------------------------------------------*/ if(snap && (it-shft)%jsnap==0) { /* P wave */ cut3d_complex(uoz_p,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfp); cut3d_complex(uox_p,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfp); cut3d_complex(uoy_p,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfp); /* S wave */ cut3d_complex(uoz_s,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfs); cut3d_complex(uox_s,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfs); cut3d_complex(uoy_s,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfs); } lint3d_extract_complex(uoz,dd[0],cr); lint3d_extract_complex(uox,dd[1],cr); lint3d_extract_complex(uoy,dd[2],cr); if(it%jdata==0) sf_complexwrite(dd[0],nr*nc,Fdat); } if(verb) sf_warning("."); if(verb) fprintf(stderr,"\n"); /*------------------------------------------------------------*/ /* deallocate arrays */ free(dft); dft3d_finalize(); free(clr_p); free(clr_s); clr3d_finalize(); free(**ww); free(*ww); free(ww); free(ss); free(rr); free(*dd); free(dd); free(n2s_p); free(n2s_s); free(*lt_p); free(lt_p); free(*rt_p); free(rt_p); free(*lt_s); free(lt_s); free(*rt_s); free(rt_s); free(**uoz ); free(*uoz ); free(uoz) ; free(**uoz_p); free(*uoz_p); free(uoz_p); free(**uoz_s); free(*uoz_s); free(uoz_s); /*free(**upz); free(*upz); free(upz);*/ free(uo); free(uo_p); free(uo_s); /*free(up);*/ if (snap) { free(**uc); free(*uc); free(uc); } /*------------------------------------------------------------*/ exit (0); }
int main (int argc, char *argv[]) { bool verb; /* verbosity */ int ompnth=1; /* number of threads */ bool down; /* adjoint flag */ bool causal; sf_file Fslo=NULL; /* slowness file S(nlx,nly,nz ) */ sf_file Fsou=NULL; /* data file Ds(nmx,nmy, nw) */ sf_file Fwfl=NULL; /* data file Dr(nmx,nmy, nw) */ weicub3d cub; /* hypercube */ weitap3d tap; /* tapering */ weissr3d ssr; /* SSR operator */ weislo3d slo; /* slowness */ weiop3d weop; /* WEI operator */ /*------------------------------------------------------------*/ sf_init(argc,argv); #ifdef _OPENMP ompnth=omp_init(); /* OMP parameters */ #endif if (!sf_getbool( "verb",&verb )) verb = false; /* verbosity flag */ if (!sf_getbool( "down",&down )) down = true; /* up/down flag */ if (!sf_getbool("causal",&causal)) sf_error("Specify causal!"); /* causality flag */ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"init cube..."); cub = wei_cube(verb,ompnth); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ Fslo = sf_input ("slo"); if (SF_FLOAT !=sf_gettype(Fslo)) sf_error("Need float slowness"); weislo_inp(cub,Fslo); /* input slowness */ /*------------------------------------------------------------*/ Fsou = sf_input ( "in"); if (SF_COMPLEX !=sf_gettype(Fsou)) sf_error("Need complex sdat"); weiwfl_inp(cub,Fsou); /* input adjoint source dimensions */ /*------------------------------------------------------------*/ Fwfl = sf_output("out"); sf_settype(Fwfl,SF_COMPLEX); /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"init slo..."); slo = weislo_init(cub,Fslo); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"init tap..."); tap = weitap_init(cub); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"init ssr..."); ssr = weissr_init(cub,slo); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"init weop..."); weop = weiwfl_init(cub); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ adjwfl(weop,cub,ssr,tap,slo,Fsou,Fwfl,down,causal); weiwfl_close(weop); /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"close structures..."); weislo_close(slo); weissr_close(cub,ssr); weitap_close(tap); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* close files */ if (Fsou!=NULL) sf_fileclose(Fsou); if (Fwfl!=NULL) sf_fileclose(Fwfl); if (Fslo!=NULL) sf_fileclose(Fslo); /*------------------------------------------------------------*/ exit (0); }
int main(int argc, char* argv[]) { bool verb; int it,iz,im,ik,ix,i,j; /* index variables */ int nt,nz,nx, m2, nk, nzx, nz2, nx2, nzx2, n2, pad1; sf_complex c; float *rr; /* I/O arrays*/ sf_complex *ww, *cwave, *cwavem; sf_complex **wave, *curr; float *rcurr; sf_file Fw,Fr,Fo; /* I/O files */ sf_axis at,az,ax; /* cube axes */ sf_complex **lt, **rt; sf_file left, right; sf_init(argc,argv); if(!sf_getbool("verb",&verb)) verb=false; /* verbosity */ /* setup I/O files */ Fw = sf_input ("in" ); Fo = sf_output("out"); Fr = sf_input ("ref"); if (SF_COMPLEX != sf_gettype(Fw)) sf_error("Need complex input"); if (SF_FLOAT != sf_gettype(Fr)) sf_error("Need float ref"); sf_settype(Fo,SF_FLOAT); /* Read/Write axes */ at = sf_iaxa(Fw,1); nt = sf_n(at); az = sf_iaxa(Fr,1); nz = sf_n(az); ax = sf_iaxa(Fr,2); nx = sf_n(ax); sf_oaxa(Fo,az,1); sf_oaxa(Fo,ax,2); sf_oaxa(Fo,at,3); if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */ nk = cfft2_init(pad1,nz,nx,&nz2,&nx2); nzx = nz*nx; nzx2 = nz2*nx2; /* propagator matrices */ left = sf_input("left"); right = sf_input("right"); if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx); if (!sf_histint(left,"n2",&m2)) sf_error("Need n2= in left"); if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2); if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk); // if (!sf_histint(Fw,"n1",&nxx)) sf_error("No n1= in input"); lt = sf_complexalloc2(nzx,m2); rt = sf_complexalloc2(m2,nk); sf_complexread(lt[0],nzx*m2,left); sf_complexread(rt[0],m2*nk,right); // sf_fileclose(left); // sf_fileclose(right); /* read wavelet & reflectivity */ ww=sf_complexalloc(nt); sf_complexread(ww,nt ,Fw); rr=sf_floatalloc(nzx); sf_floatread(rr,nzx,Fr); curr = sf_complexalloc(nzx2); rcurr = sf_floatalloc(nzx2); cwave = sf_complexalloc(nk); cwavem = sf_complexalloc(nk); wave = sf_complexalloc2(nzx2,m2); for (iz=0; iz < nzx2; iz++) { curr[iz] = sf_cmplx(0.,0.); rcurr[iz]= 0.; } /* MAIN LOOP */ for (it=0; it<nt; it++) { if(verb) sf_warning("it=%d;",it); /* matrix multiplication */ cfft2(curr,cwave); for (im = 0; im < m2; im++) { for (ik = 0; ik < nk; ik++) { #ifdef SF_HAS_COMPLEX_H cwavem[ik] = cwave[ik]*rt[ik][im]; #else cwavem[ik] = sf_cmul(cwave[ik],rt[ik][im]); //complex multiplies complex #endif // sf_warning("realcwave=%g, imagcwave=%g", crealf(cwavem[ik]),cimagf(cwavem[ik])); } icfft2(wave[im],cwavem); } for (ix = 0; ix < nx; ix++) { for (iz=0; iz < nz; iz++) { i = iz+ix*nz; /* original grid */ j = iz+ix*nz2; /* padded grid */ #ifdef SF_HAS_COMPLEX_H c = ww[it] * rr[i]; // source term #else c = sf_crmul(ww[it], rr[i]); // source term #endif for (im = 0; im < m2; im++) { #ifdef SF_HAS_COMPLEX_H c += lt[im][i]*wave[im][j]; #else c = sf_cadd(c,sf_cmul(lt[im][i], wave[im][j])); #endif } curr[j] = c; rcurr[j] = crealf(c); // rcurr[j] = cimagf(c); } /* write wavefield to output */ sf_floatwrite(rcurr+ix*nz2,nz,Fo); } } if(verb) sf_warning("."); exit (0); }
int main(int argc, char* argv[]) { sf_file in=NULL, out=NULL; int n1_traces; int n1_headers; char* header_format=NULL; sf_datatype typehead; /* kls do I need to add this? sf_datatype typein; */ float* fheader=NULL; float* intrace=NULL; int dim; off_t n_in[SF_MAX_DIM]; int iaxis; int dim_output; int *indx_of_keys; bool label_argparmread,n_argparmread,o_argparmread,d_argparmread; char parameter[13]; char* label[SF_MAX_DIM]; off_t n_output[SF_MAX_DIM]; off_t n_outheaders[SF_MAX_DIM]; float o_output[SF_MAX_DIM]; float d_output[SF_MAX_DIM]; sf_axis output_axa_array[SF_MAX_DIM]; sf_file output=NULL, outheaders=NULL; char* output_filename=NULL; char* outheaders_filename=NULL; sf_init (argc,argv); /*****************************/ /* initialize verbose switch */ /*****************************/ /* verbose flag controls ammount of print */ /*( verbose=1 0 terse, 1 informative, 2 chatty, 3 debug ) */ /* fprintf(stderr,"read verbose switch. getint reads command line.\n"); */ if(!sf_getint("verbose",&verbose))verbose=1; /* \n flag to control amount of print 0 terse, 1 informative, 2 chatty, 3 debug */ fprintf(stderr,"verbose=%d\n",verbose); /******************************************/ /* input and output data are stdin/stdout */ /******************************************/ if(verbose>0)fprintf(stderr,"read infile name\n"); in = sf_input ("in"); if(verbose>0) fprintf(stderr,"read outfile name (probably default to stdout\n"); out = sf_output ("out"); if (!sf_histint(in,"n1_traces",&n1_traces)) sf_error("input data not define n1_traces"); if (!sf_histint(in,"n1_headers",&n1_headers)) sf_error("input data does not define n1_headers"); header_format=sf_histstring(in,"header_format"); if(strcmp (header_format,"native_int")==0) typehead=SF_INT; else typehead=SF_FLOAT; fprintf(stderr,"allocate headers. n1_headers=%d\n",n1_headers); fheader = sf_floatalloc(n1_headers); fprintf(stderr,"allocate intrace. n1_traces=%d\n",n1_traces); intrace= sf_floatalloc(n1_traces); /* maybe I should add some validation that n1== n1_traces+n1_headers+2 and the record length read in the second word is consistent with n1. */ /* put the history from the input file to the output */ sf_fileflush(out,in); /********************************************************************/ /* set up the output and outheaders files for the traces and headers */ /********************************************************************/ output_filename=sf_getstring("output"); /* \n output trace filename. Required parameter with no default. */ if(NULL==output_filename) sf_error("output is a required parameter"); output=sf_output(output_filename); outheaders_filename=sf_getstring("outheaders"); /* \n Output trace header file name. Default is the input data file name, with the final .rsf changed to _hdr.rsf. */ if(outheaders_filename==NULL){ /* compute headers_filename from output_filename by replacing the final .rsf with _hdr.rsf */ if(!(0==strcmp(output_filename+strlen(output_filename)-4,".rsf"))){ fprintf(stderr,"parameter output, the name of the output file,\n"); fprintf(stderr,"does not end with .rsf, so header filename cannot\n"); fprintf(stderr,"be computed by replacing the final .rsf with\n"); fprintf(stderr,"_hdr.rsf.\n"); sf_error("default for outheaders parameter cannot be computed."); } outheaders_filename=malloc(strlen(output_filename)+60); strcpy(outheaders_filename,output_filename); strcpy(outheaders_filename+strlen(output_filename)-4,"_hdr.rsf\0"); if(verbose>1) fprintf(stderr,"parameter outheader defaulted. Computed to be #%s#\n", outheaders_filename); } if(verbose>1)fprintf(stderr,"parameter outheader input or computed #%s#\n", outheaders_filename); outheaders=sf_output(outheaders_filename); /* get each of the axis information: label2, n2, o2, d2, label3, n3, o3, d3, etc label1, n1, o1, d1 is always defaulted from input */ sf_putint (output ,"n1" ,n1_traces ); sf_putint (outheaders,"n1" ,n1_headers); sf_putfloat (outheaders,"d1" ,1 ); sf_putfloat (outheaders,"o1" ,0 ); sf_putstring(outheaders,"label1","none" ); sf_putstring(outheaders,"unit1" ,"none" ); dim_output=1; for (iaxis=1; iaxis<SF_MAX_DIM; iaxis++){ label_argparmread=n_argparmread=o_argparmread=d_argparmread=false; sprintf(parameter,"label%d",iaxis+1); fprintf(stderr,"try to read %s\n",parameter); if ((label[iaxis]=sf_getstring(parameter))) { /*(label#=(2,...) name of each of the axes. label1 is not changed from input. Each label must be a header key like cdp, cdpt, or ep. The trace header values are used to define the output trace location in the output file. )*/ fprintf(stderr,"got %s=%s\n",parameter,label[iaxis]); sf_putstring(output ,parameter,label[iaxis]); sf_putstring(outheaders,parameter,label[iaxis]); label_argparmread=true; } sprintf(parameter,"n%d",iaxis+1); fprintf(stderr,"try to read %s\n",parameter); if (sf_getlargeint (parameter,&n_output[iaxis])) { /*( n#=(2,...) number of locations in the #-th dimension )*/ fprintf(stderr,"got %s=%lld\n",parameter,(long long) n_output[iaxis]); sf_putint(output ,parameter,n_output[iaxis]); sf_putint(outheaders,parameter,n_output[iaxis]); n_argparmread=true; } sprintf(parameter,"o%d",iaxis+1); if (sf_getfloat(parameter,&o_output[iaxis])) { /*( o#=(2,...) origin of the #-th dimension )*/ sf_putfloat(output ,parameter,o_output[iaxis]); sf_putfloat(outheaders,parameter,o_output[iaxis]); o_argparmread=true; } sprintf(parameter,"d%d",iaxis+1); if (sf_getfloat(parameter,&d_output[iaxis])) { /*( d#=(2,...) delta in the #-th dimension )*/ sf_putfloat(output ,parameter,d_output[iaxis]); sf_putfloat(outheaders,parameter,d_output[iaxis]); d_argparmread=true; } if(!label_argparmread && !n_argparmread && ! o_argparmread && !d_argparmread){ /* none of the parameter were read you read all the parameters in the previous iteration compute the output dimension and exit the loop */ dim_output=iaxis; break; } if(label_argparmread && n_argparmread && o_argparmread && d_argparmread){ /* all the parameters for thisi axis were read. loop for next iaxis */ if(verbose>0){ fprintf(stderr,"label, n, i, and d read for iaxis%d\n",iaxis+1); } } else { sf_warning("working on iaxis=%d. Program expects to read\n",iaxis+1); sf_warning("label%d, n%d, i%d, and d%d from command line.\n", iaxis+1,iaxis+1,iaxis+1,iaxis+1); if(!label_argparmread) sf_error("unable to read label%d\n",iaxis+1); if(!n_argparmread ) sf_error("unable to read n%d \n",iaxis+1); if(!o_argparmread ) sf_error("unable to read o%d \n",iaxis+1); if(!d_argparmread ) sf_error("unable to read d$d \n",iaxis+1); } } /* if the input file is higher dimention than the output, then the size of all the higher dimensions must be set to 1. */ /* kls use sf_axis to get structure for each axis with n, o, d, l, and u this can be used to compute the index for sf_seek */ dim = sf_largefiledims(in,n_in); for (iaxis=dim_output; iaxis<dim; iaxis++){ sprintf(parameter,"n%d",iaxis+1); sf_putint(output,parameter,1); sf_putint(outheaders,parameter,1); } /* for a test zero n_output and dim_output and see it you can read the history file */ sf_largefiledims(output,n_output); sf_largefiledims(outheaders,n_outheaders); if(verbose>1){ for (iaxis=0; iaxis<SF_MAX_DIM; iaxis++){ fprintf(stderr,"from sf_largefiledims(output.. n%d=%lld outheaders=%lld\n", iaxis+1,(long long) n_output[iaxis],(long long) n_outheaders[iaxis]); } } /* the list header keys (including any extras) and get the index into the header vector */ segy_init(n1_headers,in); indx_of_keys=sf_intalloc(dim_output); for (iaxis=1; iaxis<dim_output; iaxis++){ /* kls need to check each of these key names are in the segy header and make error message for invalid keys. Of does segykey do this? NO, just segmentation fault. */ if(verbose>0)fprintf(stderr,"get index of key for %s\n",label[iaxis]); indx_of_keys[iaxis]=segykey(label[iaxis]); } sf_fileflush(output,in); sf_putint(outheaders,"n1",n1_headers); if(0==strcmp("native_int",sf_histstring(in,"header_format"))){ sf_settype(outheaders,SF_INT); } else { sf_settype(outheaders,SF_FLOAT); } sf_fileflush(outheaders,in); fprintf(stderr,"start trace loop\n"); /* kls maybe this should be in function sf_tahwritemapped_init */ { off_t file_offset; off_t i_output[SF_MAX_DIM]; float temp_float=0.0; for(iaxis=0; iaxis<SF_MAX_DIM; iaxis++){ i_output[iaxis]=n_output[iaxis]-1; } file_offset=sf_large_cart2line(dim_output,n_output,i_output)*sizeof(float); sf_seek(output,file_offset,SEEK_SET); sf_floatwrite(&temp_float,1,output); i_output[0]=n_outheaders[0]-1; file_offset=sf_large_cart2line(dim_output,n_outheaders,i_output)* sizeof(float); sf_seek(outheaders,file_offset,SEEK_SET); sf_floatwrite(&temp_float,1,outheaders); } for (iaxis=0; iaxis<SF_MAX_DIM; iaxis++){ /* sf_axis temp; */ output_axa_array[iaxis]=sf_iaxa(output,iaxis+1); if(verbose>2){ /* temp=output_axa_array[iaxis]; */ fprintf(stderr,"axis=%d sf_n(output_axa_array[iaxis])=%d\n", iaxis+1,sf_n(output_axa_array[iaxis])); } /* kls why does this fail? fprintf(stderr,"temp->n=%d\n",temp->n); */ } /***************************/ /* start trace loop */ /***************************/ fprintf(stderr,"start trace loop\n"); while (0==get_tah(intrace, fheader, n1_traces, n1_headers, in)){ if(verbose>1){ for(iaxis=2; iaxis<dim_output; iaxis++){ fprintf(stderr,"label[%d]=%s", iaxis,label[iaxis]); if(typehead == SF_INT)fprintf(stderr,"%d", ((int*)fheader)[indx_of_keys[iaxis]]); else fprintf(stderr,"%f", fheader[indx_of_keys[iaxis]]); } fprintf(stderr,"\n"); } tahwritemapped(verbose,intrace, fheader, n1_traces, n1_headers, output, outheaders, typehead, output_axa_array, indx_of_keys, dim_output, n_output,n_outheaders); /**********************************************/ /* write trace and headers to the output pipe */ /**********************************************/ put_tah(intrace, fheader, n1_traces, n1_headers, out); } exit(0); }
int main(int argc, char* argv[]) { bool adj,timer,verb,gmres; int nt, nx, nz, nx2, nz2, nzx, nzx2, ntx, pad1, snap, gpz, wfnt, i; int m2, n2, nk, nth=1; int niter, mem; float dt, dx, dz, ox; sf_complex *img, *imgout, *dat, **lt1, **rt1, **lt2, **rt2, ***wvfld; sf_file data, image, leftf, rightf, leftb, rightb, snaps; double time=0.,t0=0.,t1=0.; geopar geop; sf_init(argc,argv); /* essentially doing imaging */ adj = true; if (!sf_getbool("gmres",&gmres)) gmres=false; if (gmres) { if (!sf_getint("niter",&niter)) niter=10; if (!sf_getint("mem",&mem)) mem=20; } if(! sf_getbool("timer",&timer)) timer=false; if (!sf_getbool("verb",&verb)) verb=false; if (!sf_getint("snap",&snap)) snap=0; /* interval for snapshots */ if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */ if(!sf_getint("gpz",&gpz)) gpz=0; /* geophone surface */ /* adj */ if (!sf_getint("nz",&nz)) sf_error("Need nz="); /* depth samples */ if (!sf_getfloat("dz",&dz)) sf_error("Need dz="); /* depth sampling */ /* for */ if (!sf_getint("nt",&nt)) sf_error("Need nt="); /* time samples */ if (!sf_getfloat("dt",&dt)) sf_error("Need dt="); /* time sampling */ if (adj) { /* migration */ data = sf_input("in"); image = sf_output("out"); sf_settype(image,SF_COMPLEX); if (!sf_histint(data,"n1",&nt)) sf_error("No n1= in input"); if (!sf_histfloat(data,"d1",&dt)) sf_error("No d1= in input"); if (!sf_histint(data,"n2",&nx)) sf_error("No n2= in input"); if (!sf_histfloat(data,"d2",&dx)) sf_error("No d2= in input"); if (!sf_histfloat(data,"o2",&ox)) ox=0.; sf_putint(image,"n1",nz); sf_putfloat(image,"d1",dz); sf_putfloat(image,"o1",0.); sf_putstring(image,"label1","Depth"); sf_putint(image,"n2",nx); sf_putfloat(image,"d2",dx); sf_putfloat(image,"o2",ox); sf_putstring(image,"label2","Distance"); } else { /* modeling */ image = sf_input("in"); data = sf_output("out"); sf_settype(data,SF_COMPLEX); if (!sf_histint(image,"n1",&nz)) sf_error("No n1= in input"); if (!sf_histfloat(image,"d1",&dz)) sf_error("No d1= in input"); if (!sf_histint(image,"n2",&nx)) sf_error("No n2= in input"); if (!sf_histfloat(image,"d2",&dx)) sf_error("No d2= in input"); if (!sf_histfloat(image,"o2",&ox)) ox=0.; if (!sf_getint("nt",&nt)) sf_error("Need nt="); /* time samples */ if (!sf_getfloat("dt",&dt)) sf_error("Need dt="); /* time sampling */ sf_putint(data,"n1",nt); sf_putfloat(data,"d1",dt); sf_putfloat(data,"o1",0.); sf_putstring(data,"label1","Time"); sf_putstring(data,"unit1","s"); sf_putint(data,"n2",nx); sf_putfloat(data,"d2",dx); sf_putfloat(data,"o2",ox); sf_putstring(data,"label2","Distance"); } nz2 = kiss_fft_next_fast_size(nz*pad1); nx2 = kiss_fft_next_fast_size(nx); nk = nz2*nx2; /*wavenumber*/ nzx = nz*nx; nzx2 = nz2*nx2; ntx = nt*nx; if (snap > 0) { wfnt = (int)(nt-1)/snap+1; snaps = sf_output("snaps"); /* (optional) snapshot file */ sf_settype(snaps,SF_COMPLEX); sf_putint(snaps,"n1",nz); sf_putfloat(snaps,"d1",dz); sf_putfloat(snaps,"o1",0.); sf_putstring(snaps,"label1","Depth"); sf_putint(snaps,"n2",nx); sf_putfloat(snaps,"d2",dx); sf_putfloat(snaps,"o2",ox); sf_putstring(snaps,"label2","Distance"); sf_putint(snaps,"n3",wfnt); sf_putfloat(snaps,"d3",dt*snap); sf_putfloat(snaps,"o3",0.); sf_putstring(snaps,"label3","Time"); } else { wfnt = 0; snaps = NULL; } /* propagator matrices */ leftf = sf_input("leftf"); rightf = sf_input("rightf"); if (!sf_histint(leftf,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in leftf",nzx); if (!sf_histint(leftf,"n2",&m2)) sf_error("No n2= in leftf"); if (!sf_histint(rightf,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in rightf",m2); if (!sf_histint(rightf,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in rightf",nk); leftb = sf_input("leftb"); rightb = sf_input("rightb"); if (!sf_histint(leftb,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in leftb",nzx); if (!sf_histint(leftb,"n2",&m2)) sf_error("No n2= in leftb"); if (!sf_histint(rightb,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in rightb",m2); if (!sf_histint(rightb,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in rightb",nk); lt1 = sf_complexalloc2(nzx,m2); /* propagator for forward modeling */ rt1 = sf_complexalloc2(m2,nk); lt2 = sf_complexalloc2(nzx,m2); /* propagator for backward imaging */ rt2 = sf_complexalloc2(m2,nk); img = sf_complexalloc(nz*nx); dat = sf_complexalloc(nt*nx); imgout = sf_complexalloc(nz*nx); if (snap > 0) wvfld = sf_complexalloc3(nz,nx,wfnt); else wvfld = NULL; geop = (geopar) sf_alloc(1, sizeof(*geop)); sf_complexread(lt1[0],nzx*m2,leftf); sf_complexread(rt1[0],m2*nk,rightf); sf_complexread(lt2[0],nzx*m2,leftb); sf_complexread(rt2[0],m2*nk,rightb); if (adj) sf_complexread(dat,ntx,data); else sf_complexread(img,nzx,image); /*close RSF files*/ sf_fileclose(leftf); sf_fileclose(rightf); sf_fileclose(leftb); sf_fileclose(rightb); #ifdef _OPENMP #pragma omp parallel { nth = omp_get_num_threads(); } sf_warning(">>>> Using %d threads <<<<<", nth); #endif if (timer) t0 = gtod_timer(); /*load constant geopar elements*/ geop->nx = nx; geop->nz = nz; geop->dx = dx; geop->dz = dz; geop->ox = ox; geop->gpz = gpz; geop->nt = nt; geop->dt = dt; geop->snap= snap; geop->nzx2= nzx2; geop->nk = nk; geop->m2 = m2; geop->wfnt= wfnt; geop->pad1= pad1; geop->verb= verb; /* first get the Q-compensated image: B[d] */ lrexp(img, dat, adj, lt2, rt2, geop, wvfld); /* performing the least-squares optimization: ||{BF}[m] - B[d]|| */ if (gmres) { /* disabling snapshots */ geop->snap= 0; lrexp_init(lt1,rt1,lt2,rt2); sf_warning(">>>>>> Using GMRES(m) <<<<<<"); cgmres_init(nzx,mem); cgmres( img, imgout, lrexp_op, geop, niter, 0.01*SF_EPS, true); /*lrexp_op(nzx, img, imgout, geop);*/ lrexp_close(); } else { for (i=0; i<nzx; i++) imgout[i] = img[i]; } if (timer) { t1 = gtod_timer(); time = t1-t0; sf_warning("Time = %lf\n",time); } if (adj) { sf_complexwrite(imgout,nzx,image); } else { sf_complexwrite(dat,ntx,data); } if (snap > 0 && NULL != snaps) { sf_complexwrite(wvfld[0][0],wfnt*nx*nz,snaps); } exit(0); }
int main(int argc, char* argv[]) { bool verb,correct; int nt,nz,nx,m2,nk,nzx,nz2,nx2,n2,pad1; int snap, wfnt; float dt, wfdt; char *mode; sf_complex **ini, **cc, **dd; sf_file Fi,Fo,Fs,Fa,Fb; /* I/O files */ sf_axis az,ax,at; /* cube axes */ sf_complex **lt, **rt,***wvfld,*alpha,*beta; sf_file left, right; sf_init(argc,argv); if(!sf_getbool("verb",&verb)) verb=true; /* verbosity */ if(!sf_getint("nt",&nt)) sf_error("Need nt!"); if(!sf_getfloat("dt",&dt)) sf_error("Need dt!"); if(!sf_getint("snap",&snap)) snap=0; /* interval for snapshots */ if(!sf_getbool("correct",&correct)) correct=false; /*jingwei's correction*/ mode=sf_getstring("mode"); /* default mode is pspi */ if (mode[0]=='p') {sf_warning(">>>>> Using PSPI+PSPI! <<<<< \n");} else if (mode[0]=='n') {sf_warning(">>>>> Using NSPS+NSPS! <<<<< \n");} else if (mode[0]=='m') {sf_warning(">>>>> Using NSPS+PSPI! <<<<< \n");} else if (mode[0]=='x') {sf_warning(">>>>> Using PSPI+NSPS! <<<<< \n");} else sf_error("Specify mode!"); /* setup I/O files */ Fi = sf_input ("in" ); Fo = sf_output("out"); sf_settype(Fo,SF_COMPLEX); /* Read/Write axes */ az = sf_iaxa(Fi,1); nz = sf_n(az); ax = sf_iaxa(Fi,2); nx = sf_n(ax); at = sf_iaxa(Fi,3); sf_oaxa(Fo,az,1); sf_oaxa(Fo,ax,2); if (snap > 0) { Fs = sf_output("snaps"); /* (optional) snapshot file */ wfnt = (int)(nt-1)/snap + 1; wfdt = dt*snap; sf_oaxa(Fs,az,1); sf_oaxa(Fs,ax,2); sf_setn(at,wfnt); sf_setd(at,wfdt); sf_oaxa(Fs,at,3); sf_settype(Fs,SF_COMPLEX); } else Fs=NULL; if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */ nz2 = kiss_fft_next_fast_size(nz*pad1); nx2 = kiss_fft_next_fast_size(nx); nk = nz2*nx2; /*wavenumber*/ nzx = nz*nx; /* propagator matrices */ left = sf_input("left"); right = sf_input("right"); if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx); if (!sf_histint(left,"n2",&m2)) sf_error("Need n2= in left"); if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2); if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk); lt = sf_complexalloc2(nzx,m2); rt = sf_complexalloc2(m2,nk); sf_complexread(lt[0],nzx*m2,left); sf_complexread(rt[0],m2*nk,right); sf_fileclose(left); sf_fileclose(right); ini=sf_complexalloc2(nz,nx); cc=sf_complexalloc2(nz,nx); dd=sf_complexalloc2(nz,nx); sf_complexread(ini[0],nzx,Fi); sf_fileclose(Fi); if (snap>0) wvfld = sf_complexalloc3(nz,nx,wfnt); else wvfld = NULL; if (correct) { Fa = sf_input("alpha"); Fb = sf_input("beta"); if (!sf_histint(Fa,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in alpha",nzx); if (!sf_histint(Fb,"n1",&n2) || n2 != nk) sf_error("Need n1=%d in beta",nk); alpha = sf_complexalloc(nzx); beta = sf_complexalloc(nk); sf_complexread(alpha,nzx,Fa); sf_complexread(beta,nk,Fb); sf_fileclose(Fa); sf_fileclose(Fb); } else { Fa = NULL; Fb = NULL; alpha = NULL; beta = NULL; } /* wave propagation*/ propnewc(ini, lt, rt, nz, nx, nt, m2, nk, mode, 1, snap, cc, wvfld, verb,correct,alpha,beta); /* output result */ sf_complexwrite(cc[0], nzx, Fo); if (snap>0) sf_complexwrite(wvfld[0][0], nzx*wfnt, Fs); exit (0); }
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); }
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); }
int main(int argc, char* argv[]) { bool verb; int n[SF_MAX_DIM], n0[SF_MAX_DIM], rect[SF_MAX_DIM]; int a[SF_MAX_DIM], center[SF_MAX_DIM], gap[SF_MAX_DIM]; int ndim, dim, n123, n123s, i, ia, ns, i1, niter, na, i4, n4, *kk; float *d, *f, *dd; double mean; char *lagfile, key[6]; sf_filter aa; sf_file in, filt, lag; sf_init(argc,argv); in = sf_input("in"); filt = 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(filt,"lag",lagfile); ndim = sf_filedims(in,n); if (!sf_getint("dim",&dim)) dim=ndim; /* number of dimensions */ n4 = sf_leftsize(in,dim); 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_getint("na",&na)) na=0; /* filter size */ if (0 == na) { if (!sf_getints("gap",gap,dim)) { for (i=0; i < dim; i++) { gap[i] = 0; } } aa = createhelix(dim, n, center, gap, a); /* allocate PEF */ for (i=0; i < dim; i++) { n0[i] = n[i]; } } else { aa = sf_allocatehelix (na); if (!sf_getints ("lags", aa->lag, na)) sf_error("Need lags="); if (!sf_getints ("n", n0, dim)) { for (i=0; i < dim; i++) { n0[i] = n[i]; } } } n123 = 1; for (i=0; i < dim; i++) { n123 *= n[i]; snprintf(key,6,"rect%d",i+1); if (!sf_getint(key,rect+i)) rect[i]=1; } dd = sf_floatalloc(n123); kk = sf_intalloc(n123); for (i1=0; i1 < n123; i1++) { kk[i1] = 1; } bound (dim, n0, n, a, aa); find_mask(n123, kk, aa); na = aa->nh; snprintf(key,3,"n%d",dim+1); sf_putint(filt,key,na); sf_shiftdim(in, filt, dim+1); if (!sf_getint("niter",&niter)) niter=100; /* number of iterations */ if (!sf_getbool("verb",&verb)) verb = true; /* verbosity flag */ n123s = n123*na; d = sf_floatalloc(n123s); f = sf_floatalloc(n123s); sf_multidivn_init(na, dim, n123, n, rect, d, NULL, verb); for (i4=0; i4 < n4; i4++) { sf_floatread(dd,n123,in); /* apply shifts: dd -> d */ mean = 0.; for (i=ia=0; ia < na; ia++) { ns = aa->lag[ia]; for (i1=0; i1 < n123; i1++,i++) { if (i1 < ns) { d[i] = 0.0f; } else { d[i] = dd[i1-ns]; mean += d[i]*d[i]; } } } if (mean == 0.) { sf_floatwrite(d,n123s,filt); continue; } mean = sqrt (n123s/mean); /* -> apply mask */ for(i=0; i < n123s; i++) { d[i] *= mean; } for(i1=0; i1 < n123; i1++) { dd[i1] *= mean; } sf_multidivn (dd,f,niter); sf_floatwrite(f,n123s,filt); } exit(0); }