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); }
void tomo2_init (float*** rays, int *raylen, int nrays, float o1, float o2, float d1, float d2, int n1, int n2, interpolator interp, int nf_in) { int ir, id, i1, i2, nd; float x1, x2, rx; nf = nf_in; m1 = n1; m2 = n2; nr = nrays; rl = raylen; nxy = (int***) sf_alloc(nr,sizeof(int**)); mask = (bool**) sf_alloc(nr,sizeof(bool*)); w1 = (float***) sf_alloc(nr,sizeof(float**)); w2 = (float***) sf_alloc(nr,sizeof(float**)); for (ir = 0; ir < nr; ir++) { nd = rl[ir]; nxy[ir] = sf_intalloc2(2,nd); mask[ir] = sf_boolalloc(nd); w1[ir] = sf_floatalloc2(nf,nd); w2[ir] = sf_floatalloc2(nf,nd); for (id = 0; id < nd; id++) { rx = (rays[ir][id][0] - o1)/d1; i1 = (int) floor(rx + 1. - 0.5*nf); x1 = rx - floor(rx); rx = (rays[ir][id][1] - o2)/d2; i2 = (int) floor(rx + 1. - 0.5*nf); x2 = rx - floor(rx); if (i1 > - nf && i1 < n1 && i2 > - nf && i2 < n2) { mask[ir][id] = false; interp (x1, nf, w1[ir][id]); interp (x2, nf, w2[ir][id]); nxy[ir][id][0] = i1; nxy[ir][id][1] = i2; } else { mask[ir][id] = true; } } } }
void fastmarch_init(int *n1 /* grid samples [3] */, float *o1 /* grid origin [3] */, float *d1 /* grid sampling [3] */, int order1 /* accuracy order */) /*< initialize model dimensions and upwind order >*/ { int its, mts; int maxband; #ifdef _OPENMP mts = omp_get_max_threads(); #else mts = 1; #endif /* model dimensions */ n = n1; order = order1; o = o1; d = d1; s[0] = 1; s[1] = n[0]; s[2] = n[0]*n[1]; /* allocate shared memory */ in = sf_intalloc2(n[0]*n[1]*n[2],mts); x = (float ***) sf_alloc(mts,sizeof (float **)); xn = (float ***) sf_alloc(mts,sizeof (float **)); x1 = (float ***) sf_alloc(mts,sizeof (float **)); offsets = (int **) sf_alloc(mts,sizeof (int *)); t = (float **) sf_alloc(mts,sizeof (float *)); maxband = 0; if (n[0] > 1) maxband += 2*n[1]*n[2]; if (n[1] > 1) maxband += 2*n[0]*n[2]; if (n[2] > 1) maxband += 2*n[0]*n[1]; for (its=0; its < mts; its++) { x[its] = (float **) sf_alloc ((10*maxband+1),sizeof (float *)); offsets[its] = (int *) sf_alloc (n[0]*n[1]*n[2],sizeof (int)); } }
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 n1, n2, ns, nw; int is, iw, **pp, i, ip; double omega; float dw, ow; float *ahess, **ahesss, **ahessr; sf_complex **f, ***swave, ***rwave; sf_complex **stemp, **rtemp; sf_file in, out, list, us, ur, wvlt; int uts, mts; sf_init(argc,argv); in = sf_input("in"); out = sf_output("out"); if (!sf_getint("uts",&uts)) uts=0; /* number of OMP threads */ #ifdef _OPENMP mts = omp_get_max_threads(); #else mts = 1; #endif uts = (uts < 1)? mts: uts; /* read model dimensions */ if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input."); if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input."); /* read source wavefield */ if (NULL == sf_getstring("us")) sf_error("Need source wavefield us="); us = sf_input("us"); if (!sf_histint(us,"n3",&ns)) sf_error("No ns=."); if (!sf_histint(us,"n4",&nw)) sf_error("No nw=."); if (!sf_histfloat(us,"d4",&dw)) sf_error("No dw=."); if (!sf_histfloat(us,"o4",&ow)) sf_error("No ow=."); /* read receiver wavefield */ if (NULL == sf_getstring("ur")) sf_error("Need receiver wavefield ur="); ur = sf_input("ur"); /* read wavelet */ if (NULL == sf_getstring("wvlt")) sf_error("Need wvlt="); wvlt = sf_input("wvlt"); f = sf_complexalloc2(nw,ns); sf_complexread(f[0],nw*ns,wvlt); sf_fileclose(wvlt); /* read list */ if (NULL == sf_getstring("list")) sf_error("Need list="); list = sf_input("list"); pp = sf_intalloc2(2,ns); sf_intread(pp[0],2*ns,list); sf_fileclose(list); /* allocate memory */ swave = sf_complexalloc3(n1,n2,ns); rwave = sf_complexalloc3(n1,n2,ns); stemp = sf_complexalloc2(ns,ns); rtemp = sf_complexalloc2(ns,ns); ahesss = sf_floatalloc2(n1*n2,ns); ahessr = sf_floatalloc2(n1*n2,ns); ahess = sf_floatalloc(n1*n2); /* loop over frequency */ for (iw=0; iw < nw; iw++) { omega = (double) 2.*SF_PI*(ow+iw*dw); /* read wavefields */ sf_complexread(swave[0][0],n1*n2*ns,us); sf_complexread(rwave[0][0],n1*n2*ns,ur); #ifdef _OPENMP #pragma omp parallel num_threads(uts) private(is,ip,i) #endif { #ifdef _OPENMP #pragma omp for #endif for (is=0; is < ns; is++) { for (ip=0; ip < ns; ip++) { /* temps */ stemp[is][ip] = -omega*omega/conjf(f[ip][iw]) *rwave[is][pp[ip][1]][pp[ip][0]]; /* tempr */ rtemp[is][ip] = -omega*omega/conjf(f[ip][iw]) *conjf(swave[is][pp[ip][1]][pp[ip][0]]); } } /* loop over model */ #ifdef _OPENMP #pragma omp for #endif for (i=0; i < n1*n2; i++) { for (is=0; is < ns; is++) { for (ip=0; ip < ns; ip++) { ahesss[ip][i] += crealf( conjf(swave[ip][0][i]*swave[is][0][i])*stemp[is][ip]); ahessr[ip][i] += crealf( conjf(swave[ip][0][i])*rwave[is][0][i]*rtemp[is][ip]); } } } } } /* assemble */ #ifdef _OPENMP #pragma omp parallel for num_threads(uts) private(i,ip) #endif for (i=0; i < n1*n2; i++) { for (ip=0; ip < ns; ip++) { ahess[i] += powf(ahesss[ip][i]+ahessr[ip][i],2.); } } /* output hessian */ sf_floatwrite(ahess,n1*n2,out); exit(0); }
surface kirmodnewton2_init(int ns, float s0, float ds /* source/midpoint axis */, int nh, float h0, float dh /* offset axis */, int nx1, float x01, float dx1 /* reflector axis */, int nc1 /* number of reflectors */, bool cmp /* if CMP instead of shot gather */, bool absoff /* use absolute offset */) /*< Initialize surface locations same as in kirmod2 >*/ { int is, ih, iy; float s, h; surface yi, y; nx = nx1; dx = dx1; x0 = x01; nc = nc1; if (cmp) { ny = 2*ns*nh; map = sf_intalloc2(2*nh,ns); } else { ny = ns*(nh+1); map = sf_intalloc2(nh+1,ns); } y = (surface) sf_alloc(ny,sizeof(*y)); yi = y; for (is=0; is < ns; is++) { s = s0 + is*ds; if (cmp) { for (ih=0; ih < nh; ih++, yi++) { h = 0.5*(h0 + ih*dh); yi->x = s - h; yi->is = is; yi->ih = 2*ih; yi++; yi->x = s + h; yi->is = is; yi->ih = 2*ih+1; } } else { for (ih=0; ih < nh; ih++, yi++) { yi->x = absoff ? h0 + ih*dh : s + h0 + ih*dh; yi->is = is; yi->ih = ih; } yi->x = s; yi->is = is; yi->ih = nh; yi++; } } qsort(y,ny,sizeof(*y),surface_comp); for (iy=0; iy < ny; iy++) { yi = y+iy; map[yi->is][yi->ih] = iy; } return y; }
/*------------------------------------------------------------*/ wexcip3d wexcip_init(wexcub3d cub, int nhx_, int nhy_, int nhz_, int nht_, int nhx2_, int nhy2_, int nhz2_, int nht2_, int nc_, float dht_, float oht_, sf_file Fc, int eic) /*< initialize I.C. >*/ { int iw, ic, ihx, ihy, ihz, iht, icx, icy, icz; float ht, w; /*------------------------------------------------------------*/ wexcip3d cip; cip = (wexcip3d) sf_alloc(1,sizeof(*cip)); /*------------------------------------------------------------*/ /* allocate wavefields storage */ cip->ws = sf_complexalloc3(cub->amx.n,cub->amy.n,cub->az.n); cip->wr = sf_complexalloc3(cub->amx.n,cub->amy.n,cub->az.n); cip->ci = sf_complexalloc3(cub->amx.n,cub->amy.n,cub->az.n); if(eic) { cip->nhx = nhx_; cip->nhy = nhy_; cip->nhz = nhz_; cip->nht = nht_; cip->nhx2= nhx2_; cip->nhy2= nhy2_; cip->nhz2= nhz2_; cip->nht2= nht2_; cip->nc = nc_; cip->oht = oht_; cip->dht = dht_; /*------------------------------------------------------------*/ /* precompute phase for time delay */ cip->tt = sf_complexalloc2(cip->nht2,cub->aw.n); for(iw=0; iw<cub->aw.n; iw++) { w = -( cub->aw.o+iw*cub->aw.d ); for(iht=0; iht<cip->nht2; iht++) { ht = cip->oht + (iht+0)*cip->dht; cip->tt[iw][iht] = sf_cmplx(cosf(2*w*ht),sinf(2*w*ht)); } } /*------------------------------------------------------------*/ /* allocate image storage */ cip->ei = sf_complexalloc5(cip->nhx2,cip->nhy2,cip->nhz2,cip->nht2,cip->nc); cip->di = sf_complexalloc5(cip->nhx2,cip->nhy2,cip->nhz2,cip->nht2,cip->nc); for(ic=0; ic<cip->nc; ic++) { for(iht=0; iht<cip->nht2; iht++) { for(ihz=0; ihz<cip->nhz2; ihz++) { for(ihy=0; ihy<cip->nhy2; ihy++) { for(ihx=0; ihx<cip->nhx2; ihx++) { cip->ei[ic][iht][ihz][ihy][ihx] = sf_cmplx(0.0,0.0); cip->di[ic][iht][ihz][ihy][ihx] = sf_cmplx(0.0,0.0); } } } } } /*------------------------------------------------------------*/ /* CIP coordinates */ cip->cc= (pt3d*) sf_alloc(cip->nc,sizeof(*cip->cc)); pt3dread1(Fc,cip->cc,cip->nc,3); /* read coordinates */ cip->mcxall=sf_intalloc2(cip->nhx2,cip->nc); cip->pcxall=sf_intalloc2(cip->nhx2,cip->nc); cip->mcyall=sf_intalloc2(cip->nhy2,cip->nc); cip->pcyall=sf_intalloc2(cip->nhy2,cip->nc); cip->mczall=sf_intalloc2(cip->nhz2,cip->nc); cip->pczall=sf_intalloc2(cip->nhz2,cip->nc); cip->ccin=sf_intalloc(cip->nc); cip->cxmin = cub->amx.o + cip->nhx *cub->amx.d; cip->cxmax = cub->amx.o + (cub->amx.n-1-cip->nhx)*cub->amx.d; cip->cymin = cub->amy.o + cip->nhy *cub->amy.d; cip->cymax = cub->amy.o + (cub->amy.n-1-cip->nhy)*cub->amy.d; cip->czmin = cub->az.o + cip->nhz *cub->az.d; cip->czmax = cub->az.o + (cub->az.n -1-cip->nhz)*cub->az.d; for(ic=0; ic<cip->nc; ic++) { cip->ccin[ic]=(cip->cc[ic].x>=cip->cxmin && cip->cc[ic].x<=cip->cxmax && cip->cc[ic].y>=cip->cymin && cip->cc[ic].y<=cip->cymax && cip->cc[ic].z>=cip->czmin && cip->cc[ic].z<=cip->czmax)?1:0; if(cip->ccin[ic]) { icx = 0.5+(cip->cc[ic].x-cub->amx.o)/cub->amx.d; for(ihx=-cip->nhx; ihx<cip->nhx+1; ihx++) { cip->mcxall[ic][cip->nhx+ihx] = icx-ihx; cip->pcxall[ic][cip->nhx+ihx] = icx+ihx; } icy = 0.5+(cip->cc[ic].y-cub->amy.o)/cub->amy.d; for(ihy=-cip->nhy; ihy<cip->nhy+1; ihy++) { cip->mcyall[ic][cip->nhy+ihy] = icy-ihy; cip->pcyall[ic][cip->nhy+ihy] = icy+ihy; } icz = 0.5+(cip->cc[ic].z-cub->az.o)/cub->az.d; for(ihz=-cip->nhz; ihz<cip->nhz+1; ihz++) { cip->mczall[ic][cip->nhz+ihz] = icz-ihz; cip->pczall[ic][cip->nhz+ihz] = icz+ihz; } for(ihx=-cip->nhx; ihx<cip->nhx+1; ihx++) { for(ihy=-cip->nhy; ihy<cip->nhy+1; ihy++) { for(ihz=-cip->nhz; ihz<cip->nhz+1; ihz++) { // sf_warning("ihx=%d,ihy=%d,ihz=%d,mcx=%d,pcx=%d,mcy=%d,pcy=%d,mcz=%d,pcz=%d",ihx,ihy,ihz,cip->mcxall[ic][cip->nhx+ihx],cip->pcxall[ic][cip->nhx+ihx],cip->mcyall[ic][cip->nhy+ihy],cip->pcyall[ic][cip->nhy+ihy],cip->mczall[ic][cip->nhz+ihz],cip->pczall[ic][cip->nhz+ihz]); } } } } } /* loop over nc */ } return cip; }
int main(int argc, char* argv[]) { int verbose; int i, i1, n1_headers, n1_traces, len, tempint, outkeyindx; sf_file in, out; char *output=NULL, *outputkey=NULL; float /* *ftra=NULL, */ **fbuf=NULL, **fst=NULL; int /* *itra=NULL, */ **ibuf=NULL, **ist=NULL; char* header_format; sf_datatype typehead; float* fheader=NULL; int* iheader=NULL; float* intrace=NULL; sf_init (argc,argv); /*****************************/ /* initialize verbose switch */ /*****************************/ if(!sf_getint("verbose",&verbose))verbose=1; /* \n flag to control amount of print 0 terse, 1 informative, 2 chatty, 3 debug */ sf_warning("verbose=%d",verbose); /******************************************/ /* input and output data are stdin/stdout */ /******************************************/ in = sf_input ("in"); 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"); /* kls change type to header_format and read from history */ header_format=sf_histstring(in,"header_format"); if(strcmp (header_format,"native_int")==0) typehead=SF_INT; else typehead=SF_FLOAT; if(verbose>0)fprintf(stderr,"allocate headers. n1_headers=%d\n",n1_headers); fheader = sf_floatalloc(n1_headers); iheader = (int*)fheader; if(verbose>0)fprintf(stderr,"allocate intrace. n1_traces=%d\n",n1_traces); intrace= sf_floatalloc(n1_traces); segy_init(n1_headers,in); for (i=0; i < n1_headers; i++) { /* see if the segy keywords are in the input history file. If they are missing or different than I think they should be add them to the output file history */ /* no idea why this always has to be added to history, but I get errors it I remove the forcing condition below (i.e. 1 || ) */ if(1 || !sf_histint(in,segykeyword(i),&tempint) || tempint!=i){ sf_putint(out,segykeyword(i),i); } } if (NULL == (output = sf_getstring("output"))) sf_error("Need output="); /* Describes the output in a mathematical notation. */ len = sf_math_parse (output,out,typehead); if (NULL==(outputkey=sf_getstring("outputkey")))sf_error("Need outputkey="); /* name of the header key to put the results of the output equation */ if(!sf_histint(out,outputkey,&outkeyindx)){ sf_error("user parameter outputkey is not an input data header key."); } if(verbose>0)fprintf(stderr,"outkeyindx=%d\n",outkeyindx); /* I do not like these 2d arrays with one of the lengths is 1. I have done this so I can continue to use sf_math_parse and sf_math_evaluate. Perhaps someday these can be refactored and the alloc2 below can become sf_floatalloc (without the 2). Karl S */ if (SF_FLOAT == typehead) { /* float typehead */ /* ftra = sf_floatalloc(n1_headers); */ fbuf = sf_floatalloc2(1,n1_headers); fst = sf_floatalloc2(1,len+3); } else { /* int typehead */ /* itra = sf_intalloc(n1_headers); */ ibuf = sf_intalloc2(1,n1_headers); ist = sf_intalloc2(1,len+3); } /* put the history from the input file to the output */ sf_fileflush(out,in); /***************************/ /* start trace loop */ /***************************/ if(verbose>0)fprintf(stderr,"start trace loop\n"); while (0==get_tah(intrace, fheader, n1_traces, n1_headers, in)){ if(verbose>1)fprintf(stderr,"process the tah in sftahgethw\n"); /********************/ /* process the tah. */ /********************/ if (SF_FLOAT == typehead) { for (i1=0; i1 < n1_headers; i1++) { fbuf[i1][0]=fheader[i1]; } sf_math_evaluate (len, 1, fbuf, fst); if(verbose>2){ fprintf(stderr,"after math_evaluate fst[1][0]=%f\n",fst[1][0]); } fheader[outkeyindx]=fst[1][0]; } else { for (i1=0; i1 < n1_headers; i1++) { /* iheader point to same place as fheader */ ibuf[i1][0]=iheader[i1]; if(verbose>2)fprintf(stderr,"iheader[i1]=%d\n",iheader[i1]); } sf_int_math_evaluate (len, 1, ibuf, ist); if(verbose>2){ fprintf(stderr,"after int_math_evaluate ist[1][0]=%d\n",ist[1][0]); } iheader[outkeyindx]=ist[1][0]; } /***************************/ /* write trace and headers */ /***************************/ put_tah(intrace, fheader, n1_traces, n1_headers, out); if(verbose>1)fprintf(stderr,"returned from writing the tah\n"); } 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[]) { bool segy; int i, i1, i2, n1, n2, n3, n, nt, len, nkey, row; sf_file in, out; int mem; /* for avoiding int to off_t typecast warning */ off_t memsize; char *eq, *output, *key, *arg; float **ftra=NULL, **fbuf=NULL, **fst=NULL, d2, o2; int **itra=NULL, **ibuf=NULL, **ist=NULL; sf_datatype type; sf_init (argc,argv); in = sf_input ("in"); out = sf_output ("out"); sf_putint(out,"N",0); sf_putint(out,"T",1); sf_putint(out,"input",2); type = sf_gettype(in); if (SF_FLOAT != type && SF_INT != type) sf_error("Need float or int input"); if (!sf_getbool("segy",&segy)) segy=true; /* if SEGY headers */ if (!sf_histint(in,"n1",&n1)) n1=1; if (!sf_histint(in,"n2",&n2)) n2=1; n3 = sf_leftsize(in,2); /* left dimensions after the first two */ if (segy) { segy_init(n1,in); } else { other_init(n1,in); } if (NULL != (key = sf_getstring("key"))) { /* key to replace */ row = segykey(key); free(key); } else { if (!sf_getint("nkey",&row)) row=-1; /* number of key to replace */ } if (row > n1) sf_error("nkey=%d is too large, need nkey <= %d",row,n1); if (n1 > 1) { if (n2 > 1) { /* input: many keys */ if (row < 0) sf_putint(out,"n1",1); } else { /* input: one key, arranged in n1 */ n2 = n1; n1 = 1; } } for (i=0; i < n1; i++) { sf_putint(out,segykeyword(i),i+3); } for (i=1; i< argc; i++) { /* collect inputs */ arg = argv[i]; eq = strchr(arg,'='); if (NULL == eq) continue; /* not a parameter */ if (0 == strncmp(arg,"output",6) || 0 == strncmp(arg, "--",2)) continue; /* not a key */ len = (size_t) (eq-arg); key = sf_charalloc(len+1); memcpy(key,arg,len); key[len]='\0'; if (sf_getint(key,&nkey)) sf_putint(out,key,nkey+3); free(key); } if (!sf_histfloat(in,n1>1? "d2":"d1",&d2)) d2=1.; if (!sf_histfloat(in,n1>1? "o2":"o1",&o2)) o2=0.; if (NULL == (output = sf_getstring("output"))) sf_error("Need output="); /* Describes the output in a mathematical notation. */ if (!sf_getint("memsize",&mem)) mem=sf_memsize(); /* Max amount of RAM (in Mb) to be used */ memsize = mem * (1<<20); /* convert Mb to bytes */ len = sf_math_parse (output,out,type); /* number of traces for optimal I/O */ nt = SF_MAX(1,memsize/((2*n1+len+6)*sizeof(float))); if (SF_FLOAT == type) { /* float type */ ftra = sf_floatalloc2(n1,nt); fbuf = sf_floatalloc2(nt,n1+3); fst = sf_floatalloc2(nt,len+3); } else { /* int type */ itra = sf_intalloc2(n1,nt); ibuf = sf_intalloc2(nt,n1+3); ist = sf_intalloc2(nt,len+3); } for (n=n2*n3; n > 0; n -= nt) { if (n < nt) nt=n; if (SF_FLOAT == type) { sf_floatread(ftra[0],n1*nt,in); } else { sf_intread(itra[0],n1*nt,in); } for (i2=0; i2 < nt; i2++) { if (SF_FLOAT == type) { fbuf[0][i2]=(float) i2; /* N */ fbuf[1][i2]=o2+i2*d2; /* T */ fbuf[2][i2]=ftra[0][i2]; /* input */ } else { ibuf[0][i2]=i2; /* N */ ibuf[1][i2]=o2+i2*d2; /* T */ ibuf[2][i2]=itra[0][i2]; /* input */ } } for (i1=0; i1 < n1; i1++) { for (i2=0; i2 < nt; i2++) { if (SF_FLOAT == type) { fbuf[i1+3][i2]=ftra[i2][i1]; } else { ibuf[i1+3][i2]=itra[i2][i1]; } } } if (SF_FLOAT == type) { sf_math_evaluate (len, nt, fbuf, fst); if (row < 0) { sf_floatwrite(fst[1],nt,out); } else { for (i2=0; i2 < nt; i2++) { ftra[i2][row] = fst[1][i2]; } sf_floatwrite(ftra[0],n1*nt,out); } } else { sf_int_math_evaluate (len, nt, ibuf, ist); if (row < 0) { sf_intwrite(ist[1],nt,out); } else { for (i2=0; i2 < nt; i2++) { itra[i2][row] = ist[1][i2]; } sf_intwrite(itra[0],n1*nt,out); } } } exit(0); }
void interpfield_(float ***oldf, float ***newf, bool extend, int on1, float oo1, float od1, /* old */ int on2, float oo2, float od2, int on3, float oo3, float od3, int nn1, float no1, float nd1, /* new */ int nn2, float no2, float nd2, int nn3, float no3, float nd3) { float *x1 = sf_floatalloc(nn1); float *x2 = sf_floatalloc(nn2); float *x3 = sf_floatalloc(nn3); int b1 = nn1; int b2 = nn2; int b3 = nn3; int e1 = 0; int e2 = 0; int e3 = 0; int ns = gs_ns; memset(newf[0][0], 0, sizeof(float)*nn1*nn2*nn3); x_b_e(on1, oo1, od1, nn1, no1, nd1, x1, &b1, &e1); x_b_e(on2, oo2, od2, nn2, no2, nd2, x2, &b2, &e2); x_b_e(on3, oo3, od3, nn3, no3, nd3, x3, &b3, &e3); /*sf_warning("b1,b2,b3: %d, %d, %d", b1, b2, b3);*/ /*sf_warning("e1,e2,e3: %d, %d, %d", e1, e2, e3);*/ /*for (int i = 0; i < nn3; i++) {*/ /*sf_warning("%f", x3[i]);*/ /*}*/ int *k1 = sf_intalloc(nn1); assert(k1); int *k2 = sf_intalloc(nn2); int *k3 = sf_intalloc(nn3); int *t1 = sf_intalloc(nn1); int *t2 = sf_intalloc(nn2); int *t3 = sf_intalloc(nn3); int **c1 = sf_intalloc2(ns, nn1); int **c2 = sf_intalloc2(ns, nn2); int **c3 = sf_intalloc2(ns, nn3); calc_t(x1, k1, t1, nn1, gs_npts); calc_t(x2, k2, t2, nn2, gs_npts); calc_t(x3, k3, t3, nn3, gs_npts); up_clip(t1, nn1, gs_npts - 1); up_clip(t2, nn2, gs_npts - 1); up_clip(t3, nn3, gs_npts - 1); /*sf_warning("sumk1, sumk2, sumk3: %d, %d, %d", isum1(k1,nn1), isum1(k2,nn2), isum1(k3, nn3));*/ /*sf_warning("sumt1, sumt2, sumt3: %d, %d, %d", isum1(t1,nn1), isum1(t2,nn2), isum1(t3, nn3));*/ for (int is = 0; is < ns; is++) { for (int i1 = 0; i1 < nn1; i1++) { c1[i1][is] = k1[i1] - 4 + is; } for (int i2 = 0; i2 < nn2; i2++) { c2[i2][is] = k2[i2] - 4 + is; } for (int i3 = 0; i3 < nn3; i3++) { c3[i3][is] = k3[i3] - 4 + is; } } updown_cip(c1, ns, nn1, 0, on1-1); updown_cip(c2, ns, nn2, 0, on2-1); updown_cip(c3, ns, nn3, 0, on3-1); /*for (int i = 0; i < ns; i++) {*/ /*sf_warning("c1[0][%d]: %d", i, c1[0][i]);*/ /*}*/ /*sf_warning("write c1, c2, c3");*/ /*write2di("c1.rsf", c1, ns, nn1);*/ /*write2di("c2.rsf", c2, ns, nn2);*/ /*write2di("c3.rsf", c3, ns, nn3);*/ /*write1di("t1.rsf", t1, nn1);*/ /*exit(0);*/ if (extend) { /*sf_warning("computation of interpolation with extend = true");*/ #ifdef _OPENMP #pragma omp parallel for #endif for (int i3 = 0; i3 < nn3; i3++) { for (int i2 = 0; i2 < nn2; i2++) { for (int i1 = 0; i1 < nn1; i1++) { for (int ic = 0; ic < ns; ic++) { for (int ib = 0; ib < ns; ib++) { for (int ia = 0; ia < ns; ia++) { newf[i3][i2][i1] += oldf[c3[i3][ic]][c2[i2][ib]][c1[i1][ia]] * gs_sinc_table[t1[i1]][ia] * gs_sinc_table[t2[i2]][ib] * gs_sinc_table[t3[i3]][ic]; } } } } } } } else { /*sf_warning("computation of interpolation without extend");*/ #ifdef _OPENMP #pragma omp parallel for #endif for (int i3 = b3; i3 <= e3; i3++) { for (int i2 = b2; i2 <= e2; i2++) { for (int i1 = b1; i1 <= e1; i1++) { for (int ic = 0; ic < ns; ic++) { for (int ib = 0; ib < ns; ib++) { for (int ia = 0; ia < ns; ia++) { newf[i3][i2][i1] += oldf[c3[i3][ic]][c2[i2][ib]][c1[i1][ia]] * gs_sinc_table[t1[i1]][ia] * gs_sinc_table[t2[i2]][ib] * gs_sinc_table[t3[i3]][ic]; } } } } } } } free(x1); free(x2); free(x3); free(k1); free(k2); free(k3); free(t1); free(t2); free(t3); free(*c1); free(*c2); free(*c3); free(c1); free(c2); free(c3); }
/* main function */ int main(int argc, char* argv[]) { clock_t tstart,tend; double duration; /*flags*/ bool verb, adj; /* migration(adjoint) flag */ bool wantwf; /* outputs wavefield snapshots */ bool wantrecord; /* actually means "need record" */ bool illum; /* source illumination flag */ bool roll; /* survey strategy */ bool fm; /* forward modeling */ /*I/O*/ sf_file Fvel; sf_file left, right, leftb, rightb; sf_file Fsrc, Frcd/*source and record*/; sf_file Ftmpwf; sf_file Fimg; sf_file mask; /*axis*/ sf_axis at, ax, az, as; /*grid index variables*/ int nx, nz, nt, wfnt; int nzx, nx2, nz2, n2, m2, pad1, nk; int ix, iz, it, is; int nxb, nzb; int snpint, wfint; float dt, dx, dz, wfdt; float ox, oz; /*source/geophone location*/ int spx, spz; int gpz,gpx,gpl; /*geophone depth/x-crd/length*/ /*Model*/ sf_complex **lt, **rt; sf_complex **ltb, **rtb; /*Data*/ sf_complex ***wavefld; sf_complex ***record, **tmprec, **img, **imgsum; float **sill; /*source*/ sf_complex *ww; float **rr; int **kill=NULL; int rectz,rectx,repeat; /*smoothing parameters*/ float trunc; int sht0,shtbgn,shtend,shtnum,shtint; /*abc boundary*/ int top,bot,lft,rht; /*tmp*/ int tmpint; /*parameter structs*/ geopar geop; mpipar mpip; /*MPI*/ int rank, nodes; sf_complex *sendbuf, *recvbuf; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nodes); sf_init(argc, argv); if(rank==0) sf_warning("nodes=%d",nodes); 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 */ if (!sf_getbool("illum", &illum)) illum=false; /*if n, no source illumination applied */ if (!sf_getbool("roll", &roll)) roll=true; /*if n, receiver is independent of source location and gpl=nx*/ if (!sf_getbool("fm", &fm)) fm=false; /* if n, Born modelling */ if (!sf_getbool("incom", &incom)) incom=false; /* if n, use complete data */ /* 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; 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 */ if (!sf_getint("wfint", &wfint)) wfint=50; /* snap interval */ /*--- parameters of source ---*/ if (!sf_getfloat("srctrunc", &trunc)) trunc=0.4; if (!sf_getint("rectz", &rectz)) rectz=2; if (!sf_getint("rectx", &rectx)) rectx=2; if (!sf_getint("repeat", &repeat)) repeat=2; /* 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; /* simultaneous sources parameter */ if (!sf_getint("nsource", &nsource)) nsource=1; if (!sf_getint("dsource", &dsource)) dsource=0; if (!sf_getfloat("tdelay", &tdelay)) tdelay=0; if (!sf_getint("choose", &choose)) choose=nsource; /*Set I/O file*/ if (adj) { /* migration */ if (wantrecord) { Frcd = sf_input("input"); /*record from elsewhere*/ Fsrc = sf_input("src"); /*source wavelet*/ } else { Frcd = sf_output("rec"); /*record produced by forward modeling*/ Fsrc = sf_input("input"); /*source wavelet*/ } Fimg = sf_output("output"); } else { /* modeling */ Fimg = sf_input("input"); Frcd = sf_output("output"); Fsrc = sf_input("src"); /*source wavelet*/ } 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*/ if (wantwf) { Ftmpwf = sf_output("tmpwf");/*wavefield snap*/ } if (incom) { mask=sf_input("mask"); /*mask operator*/ } /*--- 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; ndelay=tdelay/dt; /* 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); /*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!"); sf_histint(Frcd,"n3", &tmpint); if (tmpint != shtnum ) sf_error("Error parameter n3 in record!"); } /*allocate memory*/ ww=sf_complexalloc(nt); rr=sf_floatalloc2(nzx,nsource); lt = sf_complexalloc2(nzx,m2); rt = sf_complexalloc2(m2,nk); ltb = sf_complexalloc2(nzx,m2); rtb = sf_complexalloc2(m2,nk); geop = (geopar) sf_alloc(1, sizeof(*geop)); mpip = (mpipar) sf_alloc(1, sizeof(*mpip)); tmprec = sf_complexalloc2(nt, gpl); record = sf_complexalloc3(nt, gpl, shtnum); if (incom) { kill = sf_intalloc2(gpl, shtnum); sf_intread(kill[0],gpl*shtnum,mask); } wavefld = sf_complexalloc3(nz, nx, wfnt); if (illum ) sill = sf_floatalloc2(nz, nx); else sill = NULL; img = sf_complexalloc2(nz, nx); if (adj) { imgsum = sf_complexalloc2(nz, nx); #ifdef _OPENMP #pragma omp parallel for private(ix,iz) #endif for (ix=0; ix<nx; ix++) for (iz=0; iz<nz; iz++) { imgsum[ix][iz] = sf_cmplx(0.,0.); } } /*read from files*/ sf_complexread(ww,nt,Fsrc); sf_complexread(lt[0],nzx*m2,left); sf_complexread(rt[0],m2*nk,right); sf_complexread(ltb[0],nzx*m2,leftb); sf_complexread(rtb[0],m2*nk,rightb); if(!adj) sf_complexread(img[0],nx*nz,Fimg); if(adj && wantrecord) { sf_complexread(record[0][0], shtnum*gpl*nt, Frcd); if(incom) { for (is=0; is<shtnum; is++) for (ix=0; ix<gpl; ix++) if(kill[is][ix]==0) for (it=0; it<nt; it++) record[is][ix][it]=sf_cmplx(0.,0.); } } else { #ifdef _OPENMP #pragma omp parallel for private(is,ix,it) #endif for (is=0; is<shtnum; is++) for (ix=0; ix<gpl; ix++) for (it=0; it<nt; it++) record[is][ix][it] = sf_cmplx(0.,0.); } /*close RSF files*/ sf_fileclose(Fsrc); sf_fileclose(left); sf_fileclose(right); sf_fileclose(leftb); sf_fileclose(rightb); /*load constant geopar elements*/ mpip->cpuid=rank; mpip->numprocs=nodes; /*load constant 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->shtnum = shtnum; /* output RSF files */ if (rank==0) { sf_setn(ax, gpl); sf_setn(az, nz); as = sf_iaxa(Fvel, 2); sf_setn(as,shtnum); sf_setd(as,shtint*dx); sf_seto(as,shtbgn*dx+ox); if (adj) { /* migration */ if(!wantrecord) { sf_oaxa(Frcd, at, 1); sf_oaxa(Frcd, ax, 2); sf_oaxa(Frcd, as, 3); 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_oaxa(Frcd, as ,3); sf_settype(Frcd,SF_COMPLEX); } if (wantwf) { sf_setn(ax, nx); /*write temp wavefield */ sf_setn(at, (wfnt-1)/wfint+1); sf_setd(at, wfdt*wfint); sf_oaxa(Ftmpwf, az, 1); sf_oaxa(Ftmpwf, ax, 2); sf_oaxa(Ftmpwf, at, 3); sf_settype(Ftmpwf,SF_COMPLEX); } } tstart = clock(); for (is=rank; is<shtnum; is+=nodes) { spx = shtbgn + shtint*is; if (roll) gpx = spx - (int)(gpl/2); else gpx = 0; geop->spx = spx; geop->gpx = gpx; if (verb) { sf_warning("============================"); sf_warning("processing shot #%d", is); 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("spz=%d spx=%d gpz=%d gpx=%d gpl=%d", spz, spx, gpz, gpx, gpl); sf_warning("snpint=%d wfdt=%f wfnt=%d ", snpint, wfdt, wfnt); sf_warning("sht0=%d shtbgn=%d shtend=%d shtnum=%d", sht0, shtbgn, shtend, shtnum); if (roll) sf_warning("Rolling survey!"); else sf_warning("Global survey (gpl=nx)!"); if (illum) sf_warning("Using source illumination!"); else sf_warning("No source illumination!"); sf_warning("============================"); } /*generate reflectivity map*/ reflgen(nzb, nxb, spz+top, spx+lft, rectz, rectx, repeat, rr); lrosfor2(wavefld, sill, tmprec, verb, lt, rt, m2, geop, ww, rr, pad1, illum); if(adj && wantrecord) #ifdef _OPENMP #pragma omp parallel for private(ix,it) #endif for (ix=0; ix<gpl; ix++) for (it=0; it<nt; it++) tmprec[ix][it] = record[is][ix][it]; if(!fm) { lrosback2(img, wavefld, sill, tmprec, adj, verb, wantwf, ltb, rtb, m2, geop, pad1, illum); } if (adj) { #ifdef _OPENMP #pragma omp parallel for private(ix,iz) #endif for (ix=0; ix<nx; ix++) for (iz=0; iz<nz; iz++) imgsum[ix][iz] += img[ix][iz]; } if (!adj || !wantrecord) #ifdef _OPENMP #pragma omp parallel for private(ix,it) #endif for (ix=0; ix<gpl; ix++) for (it=0; it<nt; it++) record[is][ix][it] = tmprec[ix][it]; if (wantwf && is==0) for (it=0; it<wfnt; it++) { if (it%wfint == 0) { sf_complexwrite(wavefld[it][0], nx*nz, Ftmpwf); } } } /*shot iteration*/ MPI_Barrier(MPI_COMM_WORLD); /*write record/image*/ if (adj) { if (rank==0) { sendbuf = (sf_complex *) MPI_IN_PLACE; recvbuf = imgsum[0]; } else { sendbuf = imgsum[0]; recvbuf = NULL; } MPI_Reduce(sendbuf, recvbuf, nx*nz, MPI_COMPLEX, MPI_SUM, 0, MPI_COMM_WORLD); if (rank==0) sf_complexwrite(imgsum[0], nx*nz, Fimg); } if (!adj || !wantrecord) { if (rank==0) { sendbuf = (sf_complex *) MPI_IN_PLACE; recvbuf = record[0][0]; } else { sendbuf = record[0][0]; recvbuf = NULL; } MPI_Reduce(sendbuf, recvbuf, shtnum*gpl*nt, MPI_COMPLEX, MPI_SUM, 0, MPI_COMM_WORLD); if (rank==0) { if(incom) { for (is=0; is<shtnum; is++) for (ix=0; ix<gpl; ix++) if(kill[is][ix]==0) for (it=0; it<nt; it++) record[is][ix][it]=sf_cmplx(0.,0.); } sf_complexwrite(record[0][0], shtnum*gpl*nt, Frcd); } } /*free memory*/ free(ww); free(rr); free(*lt); free(lt); free(*rt); free(rt); free(*ltb); free(ltb); free(*rtb); free(rtb); free(geop); free(mpip); free(*tmprec); free(tmprec); free(**record); free(*record); free(record); free(**wavefld); free(*wavefld); free(wavefld); if (illum) { free(*sill); free(sill); } free(*img); free(img); if (adj) { free(*imgsum); free(imgsum); } if (incom) { free(* kill); free(kill); } tend = clock(); duration=(double)(tend-tstart)/CLOCKS_PER_SEC; sf_warning(">> The CPU time of single shot migration is: %f seconds << ", duration); MPI_Finalize(); exit(0); }
int main(int argc, char* argv[]) { bool adj, verb; int nd, nm, nx, im, min, max, id, i, ix, sx; int **indx, *size; float *model, *data; sf_file inp, index, out; sf_init(argc,argv); if (!sf_getbool("adj",&adj)) adj=true; /* adjoint flag */ if (!sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ inp = sf_input("in"); if (SF_FLOAT != sf_gettype(inp)) sf_error("Need float input"); out = sf_output("out"); index = sf_input("index"); if (SF_INT != sf_gettype(index)) sf_error("Need int index"); if (!sf_histint(index,"n1",&nd)) sf_error("No n1= in index"); nm = sf_leftsize(index,1); if (adj) { if (nd != sf_filesize(inp)) sf_error("Wrong data size"); } else { sf_putint(out,"n1",nd); } data = sf_floatalloc(nd); indx = sf_intalloc2(nd,nm); size = sf_intalloc(nm); sf_intread(indx[0],nd*nm,index); nx = 0; for (im=0; im < nm; im++) { min = max = indx[im][0]; for (id=1; id < nd; id++) { i = indx[im][id]; if (i < min) min=i; if (i > max) max=i; } if (min) { for (id=0; id < nd; id++) { indx[im][id] -= min; } } size[im]=max-min+1; nx += size[im]; if (verb) sf_warning("size%d=%d",im+1,size[im]); } if (adj) { sf_putint(out,"n1",nx); } else { if (nx != sf_filesize(inp)) sf_error("Wrong model size"); } model = sf_floatalloc(nx); if (adj) { sf_floatread(data,nd,inp); for (ix=0; ix < nx; ix++) { model[ix] = 0.0f; } } else { sf_floatread(model,nx,inp); for (id=0; id < nd; id++) { data[id] = 0.0f; } } sx=0; for (im=0; im < nm; im++) { for (id=0; id < nd; id++) { ix = indx[im][id]+sx; if (adj) { model[ix] += data[id]; } else { data[id] += model[ix]; } } sx += size[im]; } if (adj) { sf_floatwrite(model,nx,out); } else { sf_floatwrite(data,nd,out); } exit(0); }
int main(int argc, char* argv[]) { int i, i2, n1, nbuf, **buf, *buf1, nk; float o1, d1; off_t n2, nleft; const char *key; char *arg; sf_file in, keys[SF_MAXKEYS], out, tfile; sf_init (argc,argv); in = sf_input ("in"); out = sf_output ("out"); if (!sf_histint(in,"n1",&n1) && !sf_getint("n1",&n1)) sf_error("Need n1="); /* number of samples in a trace */ if (!sf_histfloat(in,"d1",&d1) && !sf_getfloat("d1",&d1)) sf_error("Need d1="); /* trace sampling */ if (!sf_histfloat(in,"o1",&o1) && !sf_getfloat("o1",&o1)) o1=0; /* trace origin */ n2 = sf_leftsize(in,1); nbuf = BUFSIZ/sizeof(int); if (NULL != sf_getstring("tfile")) { tfile = sf_input("tfile"); /* trace header file */ if (SF_INT != sf_gettype(tfile)) sf_error("Need integer data in tfile"); if (!sf_histint(tfile,"n1",&nk) || (SF_NKEYS > nk)) sf_error ("Need at least n1=%d keys in tfile",SF_NKEYS); if (nk*n2 != sf_filesize(tfile)) sf_error ("Wrong number of traces in tfile"); } else { tfile = NULL; nk = SF_NKEYS; } sf_putint(out,"n1",nk); sf_settype(out,SF_INT); if (NULL != tfile) sf_fileflush(out,tfile); buf = sf_intalloc2(nk,nbuf); buf1 = sf_intalloc(nbuf); segy_init(nk,tfile); for (i=0; i < nk; i++) { key = segykeyword(i); if (NULL != (arg = sf_getstring(key))) { keys[i] = sf_input(key); if (SF_INT != sf_gettype(keys[i])) sf_error("Need integer data in file \"%s\"",arg); if (n2 != sf_filesize(keys[i])) sf_error("Need filesize=%lld in file \"%s\"",n2,arg); free(arg); } else { keys[i] = NULL; for (i2=0; i2 < nbuf; i2++) { buf[i2][i] = 0; } } } for (nleft=n2; nleft > 0; nleft -= nbuf) { if (nbuf > nleft) nbuf = nleft; /* read from initial trace header file */ if (NULL != tfile) sf_intread(buf[0],nk*nbuf,tfile); for (i=0; i < nk; i++) { key = segykeyword(i); if (NULL != keys[i]) { sf_intread(buf1,nbuf,keys[i]); for (i2=0; i2 < nbuf; i2++) { buf[i2][i] = buf1[i2]; } } else { /* change ns, dt, and delrt */ if (0==strcmp(key,"ns")) { for (i2=0; i2 < nbuf; i2++) { buf[i2][i] = n1; } } else if (0==strcmp(key,"dt")) { for (i2=0; i2 < nbuf; i2++) { buf[i2][i] = (int) (d1*1000000. + 0.5); } } else if (0==strcmp(key,"delrt") && o1 != 0) { keys[i] = NULL; for (i2=0; i2 < nbuf; i2++) { buf[i2][i] = (o1>0)? (int) (o1*1000. + 0.5): (int) (o1*1000. - 0.5); } } } } sf_intwrite(buf[0],nk*nbuf,out); } free(buf1); free(*buf); free(buf); exit(0); }
int main(int argc, char* argv[]) { int i, j, is, ip, dim, n[SF_MAX_DIM], ii[SF_MAX_DIM]; int nsp, **k=NULL, **l=NULL, n1, n2, i1, i2, kk, ll; char key[7]; const char *label, *unit; float f, *trace, *mag=NULL, **p=NULL, pp; sf_file in, spike; sf_init (argc,argv); if (!sf_stdin()) { /* no input file in stdin */ in = NULL; } else { in = sf_input("in"); } spike = sf_output("out"); if (NULL == in) { sf_setformat(spike,"native_float"); } else if (SF_FLOAT != sf_gettype(in)) { sf_error("Need float input"); } /* dimensions */ for (i=0; i < SF_MAX_DIM; i++) { snprintf(key,3,"n%d",i+1); if (!sf_getint(key,n+i) && (NULL == in || !sf_histint(in,key,n+i))) break; /*( n# size of #-th axis )*/ sf_putint(spike,key,n[i]); } if (0==i) sf_error("Need n1="); dim=i; /* basic parameters */ for (i=0; i < dim; i++) { snprintf(key,3,"o%d",i+1); if (!sf_getfloat(key,&f) && (NULL == in || !sf_histfloat(in,key,&f))) f=0.; /*( o#=[0,0,...] origin on #-th axis )*/ sf_putfloat(spike,key,f); snprintf(key,3,"d%d",i+1); if (!sf_getfloat(key,&f) && (NULL == in || !sf_histfloat(in,key,&f))) f = (i==0)? 0.004: 0.1; /*( d#=[0.004,0.1,0.1,...] sampling on #-th axis )*/ sf_putfloat(spike,key,f); snprintf(key,7,"label%d",i+1); if (NULL == (label = sf_getstring(key)) && (NULL == in || NULL == (label = sf_histstring(in,key)))) label = (i==0)? "Time":"Distance"; /*( label#=[Time,Distance,Distance,...] label on #-th axis )*/ if (*label != '\0' && (*label != ' ' || *(label+1) != '\0')) sf_putstring(spike,key,label); snprintf(key,6,"unit%d",i+1); if (NULL == (unit = sf_getstring(key)) && (NULL == in || NULL == (unit = sf_histstring(in,key)))) unit = (i==0)? "s":"km"; /*( unit#=[s,km,km,...] unit on #-th axis )*/ if (*unit != '\0' && (*unit != ' ' || *(unit+1) != '\0')) sf_putstring(spike,key,unit); } if (NULL != (label = sf_getstring("title"))) sf_putstring(spike,"title",label); /* title for plots */ if (!sf_getint("nsp",&nsp)) nsp=1; /* Number of spikes */ if (nsp >= 1) { mag = sf_floatalloc (nsp); k = sf_intalloc2 (nsp,dim); l = sf_intalloc2 (nsp,dim); p = sf_floatalloc2 (nsp,dim); for (i=0; i < dim; i++) { snprintf(key,3,"k%d",i+1); if ( !sf_getints(key,k[i],nsp)) { /*( k#=[0,...] spike starting position )*/ for (is=0; is < nsp; is++) { k[i][is]=-1; } } else { for (is=0; is < nsp; is++) { if (k[i][is] > n[i]) sf_error("Invalid k%d[%d]=%d > n%d=%d", i+1,is+1,k[i][is],i+1,n[i]); k[i][is]--; /* C notation */ } } snprintf(key,3,"l%d",i+1); if (!sf_getints(key,l[i],nsp)) { /*( l#=[k1,k2,...] spike ending position )*/ for (is=0; is < nsp; is++) { l[i][is]=k[i][is]; } } else { for (is=0; is < nsp; is++) { if (l[i][is] > n[i]) sf_error("Invalid l%d[%d]=%d > n%d=%d", i+1,is+1,l[i][is],i+1,n[i]); l[i][is]--; /* C notation */ } } snprintf(key,3,"p%d",i+1); if (!sf_getfloats(key,p[i],nsp)) { /*( p#=[0,...] spike inclination (in samples) )*/ for (is=0; is < nsp; is++) { p[i][is]=0.; } } } if (!sf_getfloats("mag",mag,nsp)) { /* spike magnitudes */ for (is=0; is < nsp; is++) { mag[is]=1.; } } } n1 = n[0]; n2 = sf_leftsize(spike,1); trace = sf_floatalloc (n[0]); for (i2=0; i2 < n2; i2++) { /* loop over traces */ sf_line2cart(dim-1, n+1, i2, ii+1); /* zero trace */ for (i1=0; i1 < n1; i1++) trace[i1]=0.; /* put spikes in it */ for (is=0; is < nsp; is++) { /* loop over spikes */ pp = 0.; for (i=1; i < dim; i++) { kk = k[i][is]; ll = l[i][is]; if ((kk < -1 && ll < -1) || (kk >= 0 && ll >= 0 && (kk > ii[i] || ll < ii[i]))) break; pp += p[i][is]*(ii[i]-k[i][is]-1); } if (i < dim) continue; /* skip this spike */ /* linear interpolation */ ip = floorf(pp); pp = 1.-(pp-ip); kk = k[0][is]; ll = l[0][is]; if (kk >= 0) { /* one segment per trace */ kk = SF_MAX(kk+ip,0); ll = SF_MIN(ll+ip,n1-1); } else { kk = SF_MAX(ip,0); ll = SF_MIN(n1-1+ip,n1-1); } for (j=kk; j <= ll; j++) { trace[j] += pp*mag[is]; if (j+1 < n1) trace[j+1] += (1.-pp)*mag[is]; } } sf_floatwrite(trace,n1,spike); } exit (0); }
int main (int argc,char* argv[]) { int i, j, ip, n1, n2, np, ndim, order, n123, *pp, n[2], box[2], **shift, b; float o1, o2, d1, d2, slow, *dd, **pts, *vv, *h, *bin, *vor, d[2], **rect; bool isvel, dist, voro; sf_upgrad upg; sf_file coord, ord, grid, vel; sf_init (argc, argv); ord = sf_input("in"); coord = sf_input("coord"); grid = sf_output("out"); if (NULL != sf_getstring("velocity")) { vel = sf_input("velocity"); if(!sf_histint(vel,"n1",&n1)) sf_error("No n1= in vel"); if(!sf_histint(vel,"n2",&n2)) sf_error("No n2= in vel"); /* dimensions */ if(!sf_histfloat(vel,"d1",&d1)) sf_error("No d1= in vel"); if(!sf_histfloat(vel,"d2",&d2)) sf_error("No d2= in vel"); /* sampling */ if(!sf_histfloat(vel,"o1",&o1)) o1=0.; if(!sf_histfloat(vel,"o2",&o2)) o2=0.; /* origin */ } else { vel = NULL; if(!sf_getint("n1",&n1)) sf_error("Need n1="); if(!sf_getint("n2",&n2)) sf_error("Need n2="); /* dimensions */ if(!sf_getfloat("d1",&d1)) sf_error("Need d1="); if(!sf_getfloat("d2",&d2)) sf_error("Need d2="); /* sampling */ if(!sf_getfloat("o1",&o1)) o1=0.; if(!sf_getfloat("o2",&o2)) o2=0.; /* origin */ } sf_putint(grid,"n1",n1); sf_putint(grid,"n2",n2); sf_putfloat(grid,"d1",d1); sf_putfloat(grid,"d2",d2); sf_putfloat(grid,"o1",o1); sf_putfloat(grid,"o2",o2); n[0]=n1; d[0]=d1; n[1]=n2; d[1]=d2; if(!sf_getint("order",&order)) order=2; /* [1,2] Accuracy order for distance calculation */ if(!sf_getbool("vel",&isvel)) isvel=true; /* if y, the input is velocity; n, slowness squared */ if (SF_FLOAT != sf_gettype(coord)) sf_error("Need float input"); if(!sf_histint(coord,"n2",&np)) sf_error("No n2= in input"); if(!sf_histint(coord,"n1",&ndim) || ndim > 3) sf_error("Need n1 <= 3 in input"); pts = sf_floatalloc2 (3,np); for (ip=0; ip < np; ip++) { sf_floatread(pts[ip],ndim,coord); pts[ip][2] = 0.0f; } n123 = n1*n2; dd = sf_floatalloc (n123); vv = sf_floatalloc (n123); pp = sf_intalloc (n123); if (NULL != vel) { sf_floatread(vv,n123,vel); sf_fileclose(vel); /* transform velocity to slowness squared */ if (isvel) { for(i = 0; i < n123; i++) { slow = vv[i]; vv[i] = 1./(slow*slow); } } } else { for(i = 0; i < n123; i++) { vv[i] = 1.; } } /* 1. find distance */ distance_init (1,n2,n1,np); distance(np,pts,dd,vv,pp, 1,n2,n1, 0.,o2,o1, 1.,d2,d1, order); if (!sf_getbool("dist",&dist)) dist=false; /* if output distance */ if (dist) { sf_floatwrite(dd,n123,grid); exit(0); } /* 2. binning */ sf_int2_init (pts, o1,o2,d1,d2,n1,n2, sf_bin_int, 1, np); h = sf_floatalloc(np); for (ip=0; ip<np; ip++) { h[ip]=1.0f; } sf_int2_lop (true,false,n123,np,vv,h); if (SF_FLOAT != sf_gettype(ord)) sf_error("Need float input"); sf_floatread(h,np,ord); bin = sf_floatalloc(n123); sf_int2_lop (true,false,n123,np,bin,h); for (i=0; i < n123; i++) { /* normalize by the fold */ if (vv[i] > FLT_EPSILON) bin[i] /=vv[i]; vv[i]=0.0f; } /* 3. voronoi interpolation */ vor = sf_floatalloc(n123); upg = sf_upgrad_init(2,n,d); sf_upgrad_set(upg,dd); sf_upgrad_solve(upg,vv,vor,bin); if (!sf_getbool("voro",&voro)) voro=false; /* if output Voronoi diagram */ if (voro) { sf_floatwrite(vor,n123,grid); exit(0); } /* 4. smoothing */ rect = sf_floatalloc2(n123,2); shift = sf_intalloc2(n123,2); for (j=0; j < 2; j++) { box[j] = 1; for (i=0; i < n123; i++) { rect[j][i] = 1.0f+dd[i]/d[j]; b = ceilf(rect[j][i]); if (b > box[j]) box[j] = b; shift[j][i] = 0; } } ntrianglen_init(2,box,n,rect,shift,1); ntrianglen_lop(false,false,n123,n123,vor,bin); sf_floatwrite(bin,n123,grid); exit (0); }
int main(int argc, char* argv[]) { int n,k,nc,i,j; int done; /* boolean for more combinations to compute */ int *a; /* list of elements in the current combination (not needed at startup) */ int **mask; sf_axis areplic; sf_file in,out; sf_init(argc,argv); in = sf_input("in"); out = sf_output("out"); sf_settype(out,SF_INT); if (!sf_getint("k",&k)) sf_error("Need k="); /* combination of k elements */ /* input file */ if (!sf_histint(in,"n1",&n)) sf_error("No n1="); nc = binomial(n,k); sf_warning("Number of combinations is %3d",nc); /* output file parameters */ areplic = sf_maxa(nc,0,1); sf_oaxa(out,areplic,2); sf_putstring (out,"label2", "replication"); /* memory allocations */ a = sf_intalloc(k); mask = sf_intalloc2(n,nc); done = 1; j = 0; while (1) { /* Combination of k elements out of n */ comb_next(n,k,a,&done); if (done) break; /* done = 1 if more combinations to compute */ /* done = 0 when the list is exhausted. */ for (i = 0; i < k; i++) fprintf(stderr," %3d",a[i]); fprintf(stderr," \n"); for (i = 0; i < n; i++) mask[j][i] = 1; for (i = 0; i < k; i++) mask[j][a[i]-1] = 0; j++; } /* output */ sf_warning("Number of combinations is %3d",nc); sf_intwrite(mask[0],n*nc,out); exit(0); }
int main(int argc, char* argv[]) { bool adj, half, verb, normalize; int nt, nx, nh, nh2, ix, ih, iy, i, nn, it, **fold, apt; float *trace, **image, **v, rho, **stack, *pp, *off; float h, x, t, h0, dh, dx, ti, tx, t0, t1, t2, dt, vi, aal, angle; sf_file inp, out, vel, gather, offset; sf_init (argc,argv); inp = sf_input("in"); vel = sf_input("vel"); out = sf_output("out"); if (!sf_getbool("adj",&adj)) adj=true; /* adjoint flag (y for migration, n for modeling) */ if (!sf_getbool("normalize",&normalize)) normalize=true; /* normalize for the fold */ if (!sf_histint(inp,"n1",&nt)) sf_error("No n1="); if (!sf_histint(inp,"n2",&nx)) sf_error("No n2="); if (!sf_histfloat(inp,"o1",&t0)) sf_error("No o1="); if (!sf_histfloat(inp,"d1",&dt)) sf_error("No d1="); if (!sf_histfloat(inp,"d2",&dx)) sf_error("No d2="); if (adj) { if (!sf_histint(inp,"n3",&nh)) sf_error("No n3="); sf_putint(out,"n3",1); } else { if (!sf_getint("nh",&nh)) sf_error("Need nh="); /* number of offsets (for modeling) */ sf_putint(out,"n3",nh); } if (NULL != sf_getstring("gather")) { gather = sf_output("gather"); } else { gather = NULL; } if (!sf_getfloat("antialias",&aal)) aal = 1.0; /* antialiasing */ if (!sf_getint("apt",&apt)) apt = nx; /* integral aperture */ if (!sf_getfloat("angle",&angle)) angle = 90.0; /* angle aperture */ angle = fabsf(tanf(angle*SF_PI/180.0)); if (!sf_getbool("half",&half)) half = true; /* if y, the third axis is half-offset instead of full offset */ if (!sf_getbool("verb",&verb)) verb = true; /* verbosity flag */ if (!sf_getfloat("rho",&rho)) rho = 1.-1./nt; /* Leaky integration constant */ if (NULL != sf_getstring("offset")) { offset = sf_input("offset"); nh2 = sf_filesize(offset); if (nh2 != nh*nx) sf_error("Wrong dimensions in offset"); off = sf_floatalloc(nh2); sf_floatread (off,nh2,offset); sf_fileclose(offset); } else { if (adj) { if (!sf_histfloat(inp,"o3",&h0)) sf_error("No o3="); if (!sf_histfloat(inp,"d3",&dh)) sf_error("No d3="); sf_putfloat(out,"d3",1.); sf_putfloat(out,"o3",0.); } else { if (!sf_getfloat("dh",&dh)) sf_error("Need dh="); /* offset sampling (for modeling) */ if (!sf_getfloat("h0",&h0)) sf_error("Need h0="); /* first offset (for modeling) */ sf_putfloat(out,"d3",dh); sf_putfloat(out,"o3",h0); } if (!half) dh *= 0.5; off = sf_floatalloc(nh*nx); for (ix = 0; ix < nx; ix++) { for (ih = 0; ih < nh; ih++) { off[ih*nx+ix] = h0 + ih*dh; } } offset = NULL; } v = sf_floatalloc2(nt,nx); sf_floatread(v[0],nt*nx,vel); trace = sf_floatalloc(nt); image = sf_floatalloc2(nt,nx); stack = sf_floatalloc2(nt,nx); if (normalize) { fold = sf_intalloc2(nt,nx); } else { fold = NULL; } nn = 2*kiss_fft_next_fast_size((nt+1)/2); pp = sf_floatalloc(nn); sf_halfint_init (true, nn, rho); if (adj) { for (i=0; i < nt*nx; i++) { stack[0][i] = 0.; } } else { sf_floatread(stack[0],nt*nx,inp); } if (NULL != fold) { for (i=0; i < nt*nx; i++) { fold[0][i] = 0; } } for (ih=0; ih < nh; ih++) { if (verb) sf_warning("offset %d of %d;",ih+1,nh); if (adj) { for (i=0; i < nt*nx; i++) { image[0][i] = 0.; } } else { for (iy=0; iy < nx; iy++) { for (it=0; it < nt; it++) { image[iy][it] = stack[iy][it]; } } } if (!adj) { for (iy=0; iy < nx; iy++) { for (it=0; it < nt; it++) { pp[it] = image[iy][it]; } for (it=nt; it < nn; it++) { pp[it] = 0.; } sf_halfint (false, pp); for (it=0; it < nt; it++) { image[iy][it] = pp[it]; } } } for (iy=0; iy < nx; iy++) { if (adj) { sf_floatread (trace,nt,inp); sf_doubint(true, nt,trace); } else { for (it=0; it < nt; it++) { trace[it]=0.0f; } } h = fabsf(off[ih*nx+iy]); for (ix=0; ix < nx; ix++) { x = (ix-iy)*dx; if (SF_ABS(ix-iy) > apt) continue; for (it=0; it < nt; it++) { t = t0 + it*dt; vi = v[ix][it]; if (fabsf(x) > angle*vi*t) continue; /* hypot(a,b) = sqrt(a*a+b*b) */ t1 = hypotf(0.5*t,(x-h)/vi); t2 = hypotf(0.5*t,(x+h)/vi); ti = t1+t2; /* tx = |dt/dx| */ tx = fabsf(x-h)/(vi*vi*(t1+dt))+ fabsf(x+h)/(vi*vi*(t2+dt)); pick(adj,ti,fabsf(tx*dx*aal),trace,image[ix],it,nt,dt,t0); } } if (!adj) { sf_doubint(true, nt,trace); sf_floatwrite (trace,nt,out); } } if (adj) { for (iy=0; iy < nx; iy++) { for (it=0; it < nt; it++) { pp[it] = image[iy][it]; } for (it=nt; it < nn; it++) { pp[it] = 0.; } sf_halfint (true, pp); for (it=0; it < nt; it++) { image[iy][it] = pp[it]; } } if (NULL != gather) sf_floatwrite(image[0],nt*nx,gather); for (iy=0; iy < nx; iy++) { for (it=0; it < nt; it++) { stack[iy][it] += image[iy][it]; if (NULL != fold && 0.!=image[iy][it]) fold[iy][it]++; } } } } if (verb) sf_warning("."); if (NULL != fold) { for (i=0; i < nt*nx; i++) { stack[0][i] /= (fold[0][i]+FLT_EPSILON); } } if (adj) sf_floatwrite(stack[0],nt*nx,out); exit(0); }
int main(int argc, char* argv[]) { int nt, nt2, nx, i1, i2, n12, i, j, nx2; bool adj, sm, domod; float dt, dt2, dx, ot, ot2, ox, epst2; float v_1, v_2, v_3, v_4, eps, passthr; float * data, * output, * datat2, * outputt2, * model, * pwdmodel, * outputext, * pwdmodelext; sf_file inp, out; /* PWD parameters */ int nw, nj1; float *pp, *pwdata, *pwdataext; sf_file dip,outpwdcheck,outdipcheck; /* kirchhoff params */ bool half, verb,normalize,debug; int nh, **fold, apt; float **v, rho, *off; float h0, dh, aal, angle; int ix, ih, nh2; sf_file vel, gather, offset; /* regularization weight */ float reg; /* files needed for the extended operator */ float * modelext, * dataext; // diffmod - diffraction image from the previous iteration // dipim - dip distribution in the image sf_file dipim; //diffmod, //MADAGASCAR C API /* initialize */ sf_init(argc,argv); inp = sf_input("in"); out = sf_output("out"); vel = sf_input("vel"); dip = sf_input("dip"); //initialize file with dip dipim = sf_input("dipim"); //initialize file with image dip //diffmod = sf_input("diffmod"); //diffraction model from the previous iteration /* get dimensions from input */ if (!sf_histint(inp,"n1",&nt)) sf_error("No n1= in inp"); if (!sf_histint(inp,"n2",&nx2)) sf_error("No n2= in inp"); // half of the traces - actual data // half of the traces - model penalization nx = (int)(nx2)/2; sf_warning("nx=%d nx2=%d \n",nx,nx2); sf_warning("nx=%d nx2=%d \n",nx,nx2); sf_warning("nx=%d nx2=%d \n",nx,nx2); sf_warning("nx=%d nx2=%d \n",nx,nx2); sf_warning("nx=%d nx2=%d \n",nx,nx2); if (!sf_histfloat(inp,"d1",&dt)) sf_error("No d1= in inp"); if (!sf_histfloat(inp,"d2",&dx)) sf_error("No d2= in inp"); if (!sf_histfloat(inp,"o1",&ot)) sf_error("No o1= in inp"); if (!sf_histfloat(inp,"o2",&ox)) sf_error("No o2= in inp"); /* get parameters from command line */ /* adjoint flag */ if (!sf_getbool("adj",&adj)) adj=false; /* if perform derivative filtering = PWD */ if (!sf_getbool("sm",&sm)) sm=true; /* if perform modelling via Kirchhoff */ if (!sf_getbool("domod",&domod)) domod=true; /* get regularization parameter */ if (!sf_getfloat("reg",®)) reg=0.0; /* debug flag */ if (!sf_getbool("debug",&debug)){ debug=false; outpwdcheck = NULL; outdipcheck = NULL; } else { outpwdcheck = sf_output("outpwd"); outdipcheck = sf_output("outdip"); } /* kirchhoff parameters */ //////////////////////////////////////////////////////////////////////////////////////// if (!sf_getbool("normalize",&normalize)) normalize=true; /* normalize for the fold */ if (normalize) { fold = sf_intalloc2(nt,nx); } else { fold = NULL; } if (adj) { if (!sf_histint(inp,"n3",&nh)) sf_error("No n3="); sf_putint(out,"n3",1); } else { if (!sf_getint("nh",&nh)) sf_error("Need nh="); /* number of offsets (for modeling) */ sf_putint(out,"n3",nh); } if (NULL != sf_getstring("gather")) { gather = sf_output("gather"); } else { gather = NULL; } if (!sf_getfloat("antialias",&aal)) aal = 1.0; /* antialiasing */ if (!sf_getint("apt",&apt)) apt = nx; /* integral aperture */ if (!sf_getfloat("angle",&angle)) angle = 90.0; /* angle aperture */ angle = fabsf(tanf(angle*SF_PI/180.0)); if (!sf_getbool("half",&half)) half = true; /* if y, the third axis is half-offset instead of full offset */ if (!sf_getbool("verb",&verb)) verb = true; /* verbosity flag */ if (!sf_getfloat("rho",&rho)) rho = 1.-1./nt; /* Leaky integration constant */ if (NULL != sf_getstring("offset")) { offset = sf_input("offset"); nh2 = sf_filesize(offset); if (nh2 != nh*nx) sf_error("Wrong dimensions in offset"); off = sf_floatalloc(nh2); sf_floatread (off,nh2,offset); sf_fileclose(offset); } else { if (adj) { if (!sf_histfloat(inp,"o3",&h0)) sf_error("No o3="); if (!sf_histfloat(inp,"d3",&dh)) sf_error("No d3="); sf_putfloat(out,"d3",1.); sf_putfloat(out,"o3",0.); } else { if (!sf_getfloat("dh",&dh)) sf_error("Need dh="); /* offset sampling (for modeling) */ if (!sf_getfloat("h0",&h0)) sf_error("Need h0="); /* first offset (for modeling) */ sf_putfloat(out,"d3",dh); sf_putfloat(out,"o3",h0); } if (!half) dh *= 0.5; off = sf_floatalloc(nh*nx); for (ix = 0; ix < nx; ix++) { for (ih = 0; ih < nh; ih++) { off[ih*nx+ix] = h0 + ih*dh; } } offset = NULL; } //////////////////////////////////////////////////////////////////////////////////////// /* path-integral range */ if (!sf_getfloat("v_1",&v_1)) sf_error("No integration range specified"); if (!sf_getfloat("v_2",&v_2)) sf_error("No integration range specified"); if (!sf_getfloat("v_3",&v_3)) sf_error("No integration range specified"); if (!sf_getfloat("v_4",&v_4)) sf_error("No integration range specified"); if (!sf_getfloat("passthr",&passthr)) passthr = 0.001; // threshold for tail elimination if (!sf_getfloat("eps",&eps)) eps = 0.001; // damper for pi if (!sf_getfloat("epst2",&epst2)) epst2 = 0.001; // damper for t2warp /* new axis length */ if (!sf_getint("pad",&nt2)) nt2=nt; /* output time samples */ n12 = nt2*nx; /*^^^*/ /* extended operator arrays */ dataext = sf_floatalloc(nt*nx); modelext = sf_floatalloc(nt*nx); pwdmodel = sf_floatalloc(nt*nx); pwdmodelext = sf_floatalloc(nt*nx); outputext = sf_floatalloc(nt*nx); /* chain arrays */ data = sf_floatalloc(nt*nx); model = sf_floatalloc(nt*nx); datat2 = sf_floatalloc(nt2*nx); outputt2 = sf_floatalloc(nt2*nx); output = sf_floatalloc(nt*nx); pwdata = NULL; // allocate dip if (sm){ pp = sf_floatalloc(nt*nx); //allocate space for dip sf_floatread(pp,nt*nx,dip); //read dip pwdata = sf_floatalloc(nt*nx); //allocate space for pwd data (modelled with kichhoff operator usually) pwdataext = sf_floatalloc(nt*nx); //allocate space for pwd data if (!sf_getint("order",&nw)) nw=1; /* [1,2,3] accuracy order */ if (nw < 1 || nw > 3) sf_error ("Unsupported nw=%d, choose between 1 and 3",nw); if (!sf_getint("nj1",&nj1)) nj1=1; /* antialiasing */ allpass32d_init(allpass_init(nw, nj1, nt,nx,1, pp)); //initialize all-pass-filter structure } // allocating and reading velocity v = sf_floatalloc2(nt,nx); sf_floatread(v[0],nt*nx,vel); if(!adj) { if(domod){// perform modelling /*^^^*/ // reading data // first we read actual model sf_floatread(model,nt*nx,inp); // here we read diffractivity from the // previous iteration (should be catenated along // the second axis) sf_floatread(modelext,nt*nx,inp); // modelling via mig2 //^^^^^ mig2_lop(false,half,verb,normalize,nt,nx,nh,fold,apt,data,v,rho,model,off,h0,dh,dx,ot,dt,aal,angle); mig2_lop(false,half,verb,normalize,nt,nx,nh,fold,apt,dataext,v,rho,modelext,off,h0,dh,dx,ot,dt,aal,angle); } else {// just read the data sf_warning("modelling is disabled"); sf_floatread(data,nt*nx,inp); //sf_floatread(modelext,nt*nx,inp); } // internal else if (sm){// perform PWD //^^^^^ allpass32d_lop(adj,false,nt*nx,nt*nx,data,pwdata); allpass32d_lop(adj,false,nt*nx,nt*nx,dataext,pwdataext); /*if (debug){ sf_floatwrite(pwdata,nt*nx,outpwdcheck); sf_floatwrite(pp,nt*nx,outdipcheck); }*/ //change the address for(i=0;i<nt*nx;i++){ data[i]=pwdata[i]; dataext[i]=pwdataext[i]; } }//PWD flag } else {// adj flag /*^^^*/ // read data currently 2D sf_floatread(data,nt*nx,inp); // read extended data - zero PWD over reflection sf_floatread(dataext,nt*nx,inp); // since adj PWD applied to zero is zero // lets just put zeroes in the correspoding part of the output /*for (i1=0;i1<nt*nx;i1++) { dataext[i1]=0.0; }*/ }// adj flag // t2warping axis evaluation ot2 = ot*ot; dt2 = ot+(nt-1)*dt; dt2 = (dt2*dt2 - ot2)/(nt2-1); // take in account different output trace length t2warp_init(nt,nt2,nx,ot,dt,ot2,dt2,epst2); sf_warning("t2warp_init(nt,nt2,nx,ot,dt,ot2,dt2,epst2);\n"); // compute pi filter sf_warning("be4 filter"); flatpifilt_init(nt2, nx, dt2, dx, 0.001, v_1, v_2, v_3, v_4, eps); sf_warning("after filter"); sf_warning("pifilt_init(nt2, nx, dt2, dx, v_a, v_b, v_0, beta, eps);\n"); if(adj) { sf_warning("be4 the chain: params %d %d %d %d",nt*nx,nt2*nx,nt2*nx,nt*nx); sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,adj,false,nt*nx,nt2*nx,nt2*nx,nt*nx,output,data,outputt2,datat2); //sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,adj,false,nt*nx,nt2*nx,nt2*nx,nt*nx,outputext,data,outputt2,datat2); sf_warning("running chain"); } else { sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,false,false,nt*nx,nt2*nx,nt2*nx,nt*nx,data,output,datat2,outputt2); sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,false,false,nt*nx,nt2*nx,nt2*nx,nt*nx,dataext,outputext,datat2,outputt2); } if(adj) { if (sm){ sf_warning("performing PWD"); //^^^^^ allpass32d_lop(adj,false,nt*nx,nt*nx,pwdata,output); //allpass32d_lop(adj,false,nt*nx,nt*nx,pwdataext,outputext); //change the address for (i=0;i<nt*nx;i++){ output[i]=pwdata[i]; //outputext[i]=pwdataext[i]; } } if (domod) { sf_warning("performing Kirchhoff migration"); //^^^^^ mig2_lop(true,half,verb,normalize,nt,nx,nh,fold,apt,output,v,rho,model,off,h0,dh,dx,ot,dt,aal,angle); //mig2_lop(true,half,verb,normalize,nt,nx,nh,fold,apt,outputext,v,rho,modelext,off,h0,dh,dx,ot,dt,aal,angle); } else { sf_warning("changing the address"); //change the address for (i=0;i<nt*nx;i++){ model[i]=output[i]; } } } // adj flag sf_warning("done with output"); if (sm){ // pp = sf_floatalloc(nt*nx); //allocate space for dip sf_floatread(pp,nt*nx,dipim); //read dipim // we need to re-initialize allpass filter // so that it operates in the image domain allpass32d_init(allpass_init(nw, nj1, nt,nx,1, pp)); //initialize all-pass-filter structure // all other parameters are supposed to be initialized previously if(!adj) { // perform adj pwd in the image domain on the model and diffraction model allpass32d_lop(adj,false,nt*nx,nt*nx,model,pwdmodel); for(j=0;j<nx*nt;j++){ output[j] = output[j] + outputext[j]; outputext[j] = reg*pwdmodel[j]; } } else { allpass32d_lop(adj,false,nt*nx,nt*nx,pwdmodelext,dataext); for(j=0;j<nx*nt;j++){ modelext[j] = model[j]; model[j] = model[j] + reg*pwdmodelext[j]; }//for } } if (!adj) { // write sf_floatwrite(output,nt*nx,out); // write extended output sf_floatwrite(outputext,nt*nx,out); } else { // write sf_floatwrite(model,nt*nx,out); // equivalent to setting reflection component to zero sf_floatwrite(modelext,nt*nx,out); } exit(0); }
/*------------------------------------------------------------*/ cam3d cam3_init(cub3d cub, int pmx, int pmy, int phx, int tmx, int tmy, int thx, float dsmax ) /*< initialize >*/ { int imy, imx, ihx; float kmx, khx; int jmx, jhx; int ilx, ily; float my, mx, hx, k; /*------------------------------------------------------------*/ cam3d cam; cam = (cam3d) sf_alloc(1,sizeof(*cam)); X2K(cub->amx,cam->bmx,pmx); X2K(cub->amy,cam->bmy,pmy); X2K(cub->ahx,cam->bhx,phx); /* allocate K-domain storage */ cam->wk = sf_complexalloc4 (cam->bmx.n,cam->bmy.n,cam->bhx.n,cub->ompnth); cam->pk = sf_complexalloc4 (cam->bmx.n,cam->bmy.n,cam->bhx.n,cub->ompnth); /* allocate X-domain storage */ cam->wt = sf_floatalloc4 (cub->amx.n,cub->amy.n,cub->ahx.n,cub->ompnth); cam->ksx = sf_floatalloc2(cam->bmx.n,cam->bhx.n); cam->krx = sf_floatalloc2(cam->bmx.n,cam->bhx.n); for (imx=0; imx<cam->bmx.n; imx++) { jmx = KMAP(imx,cam->bmx.n); kmx = cam->bmx.o + jmx*cam->bmx.d; for (ihx=0; ihx<cam->bhx.n; ihx++) { jhx = KMAP(ihx,cam->bhx.n); khx = cam->bhx.o + jhx*cam->bhx.d; k = 0.5*(kmx-khx); cam->ksx[ihx][imx] = k*k; /* ksx^2 */ k = 0.5*(kmx+khx); cam->krx[ihx][imx] = k*k; /* krx^2 */ } } /* precompute indices */ cam->jx = sf_intalloc(cub->amx.n); cam->jy = sf_intalloc(cub->amy.n); cam->is = sf_intalloc2(cub->amx.n,cub->ahx.n); /* source index */ cam->ir = sf_intalloc2(cub->amx.n,cub->ahx.n); /* receiver index */ for (imy=0; imy<cub->amy.n; imy++) { my = cub->amy.o + imy*cub->amy.d; ily = INDEX( my,cub->aly); cam->jy[imy] = BOUND(ily,cub->aly.n); /* x-line index */ } for (imx=0; imx<cub->amx.n; imx++) { mx = cub->amx.o + imx*cub->amx.d; ilx = INDEX( mx,cub->alx); cam->jx[imx] = BOUND(ilx,cub->alx.n); /* i-line index */ for (ihx=0; ihx<cub->ahx.n; ihx++) { hx = cub->ahx.o + ihx*cub->ahx.d; ilx = INDEX(mx-hx,cub->alx); cam->is[ihx][imx] = BOUND( ilx,cub->alx.n); /* source index */ ilx = INDEX(mx+hx,cub->alx); cam->ir[ihx][imx] = BOUND( ilx,cub->alx.n); /* receiver index */ } } /* initialize FFT */ cam->f3d = ompfft3_init(cub,cam->bmx.n,cam->bmy.n,cam->bhx.n); cam->dsmax2 = dsmax*dsmax; cam->dsmax2*= cam->dsmax2; return cam; }
int main(int argc, char* argv[]) { bool verb,isreversed; sf_file Fs,Fr,Fi,Fc; /* I/O files */ sf_axis az,ax,at,ac,aa; /* cube axes */ int nz,nx,nt, nhx, nhz, nht,nc; int it, ihx, ihz, iht,ic; int nhx2,nhz2,nht2; off_t iseek; float ***us=NULL,***ur=NULL,****ii=NULL; pt2d *cc=NULL; bool *ccin=NULL; float cxmin,czmin; float cxmax,czmax; int icx, icz; int mcx, mcz, mct; int pcx, pcz, pct; int **mcxall, **pcxall; int **mczall, **pczall; int *mctall, *pctall; int lht; float scale; /* gaussian taper */ bool gaus; float gsx,gsz,gst; /* std dev */ float gx, gz, gt; /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); /* OMP parameters */ #ifdef _OPENMP omp_init(); #endif if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("isreversed",&isreversed)) isreversed=false; /* reversed rec wfld? */ Fs = sf_input ("in" ); /* source wavefield */ Fr = sf_input ("ur" ); /* receiver wavefield */ Fc = sf_input ("cc" ); /* CIP coordinates */ Fi = sf_output("out"); /* image */ /*------------------------------------------------------------*/ /* read axes */ az=sf_iaxa(Fs,1); nz = sf_n(az); ax=sf_iaxa(Fs,2); nx = sf_n(ax); at=sf_iaxa(Fs,3); nt = sf_n(at); /* CIP coordinates */ ac = sf_iaxa(Fc,2); sf_setlabel(ac,"cc"); sf_setunit(ac,""); nc = sf_n(ac); if(! sf_getint("nhz",&nhz)) nhz=0; nhz2=2*nhz+1; /* z lags */ if(! sf_getint("nhx",&nhx)) nhx=0; nhx2=2*nhx+1; /* x lags */ if(! sf_getint("nht",&nht)) nht=0; nht2=2*nht+1; /* t lags */ lht=2*nht; if(verb) { sf_warning("nhx=%3d nhz=%3d nht=%3d",nhx2,nhz2,nht2); sf_raxa(az); sf_raxa(ax); sf_raxa(at); sf_raxa(ac); } /* set output axes */ aa=sf_maxa(nhz2,-nhz*sf_d(az),sf_d(az)); sf_setlabel(aa,"hz"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Fi,aa,1); aa=sf_maxa(nhx2,-nhx*sf_d(ax),sf_d(ax)); sf_setlabel(aa,"hx"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Fi,aa,2); aa=sf_maxa(nht2,-nht*sf_d(at),sf_d(at)); sf_setlabel(aa,"ht"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Fi,aa,3); sf_oaxa(Fi,ac,4); if(! sf_getbool("gaus",&gaus)) gaus=false; /* Gaussian taper flag */ if(gaus) { if(! sf_getfloat("gsx",&gsx)) gsx=nhx*sf_d(ax); gsx=1./(2*gsx*gsx); if(! sf_getfloat("gsz",&gsz)) gsz=nhz*sf_d(az); gsz=1./(2*gsz*gsz); if(! sf_getfloat("gst",&gst)) gst=nht*sf_d(at); gst=1./(2*gst*gst); } /*------------------------------------------------------------*/ /* allocate work arrays */ us=sf_floatalloc3(nz,nx,nht2); ur=sf_floatalloc3(nz,nx,nht2); ii=sf_floatalloc4(nhz2,nhx2,nht2,nc); /* zero output */ for(ic=0; ic<nc; ic++) { for (iht=0; iht<nht2; iht++) { for (ihx=0; ihx<nhx2; ihx++) { for(ihz=0; ihz<nhz2; ihz++) { ii[ic][iht][ihx][ihz] = 0; } } } } /*------------------------------------------------------------*/ /* CIP coordinates */ cc= (pt2d*) sf_alloc(nc,sizeof(*cc)); pt2dread1(Fc,cc,nc,2); mcxall=sf_intalloc2(nhx2,nc); pcxall=sf_intalloc2(nhx2,nc); mczall=sf_intalloc2(nhz2,nc); pczall=sf_intalloc2(nhz2,nc); ccin=sf_boolalloc(nc); cxmin = sf_o(ax) + nhx *sf_d(ax); cxmax = sf_o(ax) + (sf_n(ax)-1-nhx)*sf_d(ax); czmin = sf_o(az) + nhz *sf_d(az); czmax = sf_o(az) + (sf_n(az)-1-nhz)*sf_d(az); if(verb) { sf_warning("cxmin=%f,cxmax=%f",cxmin,cxmax); sf_warning("czmin=%f,czmax=%f",czmin,czmax); } for(ic=0; ic<nc; ic++) { ccin[ic]=(cc[ic].x>=cxmin && cc[ic].x<=cxmax && cc[ic].z>=czmin && cc[ic].z<=czmax)?true:false; if(ccin[ic]) { icx = 0.5+(cc[ic].x-sf_o(ax))/sf_d(ax); for(ihx=-nhx; ihx<nhx+1; ihx++) { mcxall[ic][nhx+ihx] = icx-ihx; pcxall[ic][nhx+ihx] = icx+ihx; } icz = 0.5+(cc[ic].z-sf_o(az))/sf_d(az); for(ihz=-nhz; ihz<nhz+1; ihz++) { mczall[ic][nhz+ihz] = icz-ihz; pczall[ic][nhz+ihz] = icz+ihz; } } } mctall=sf_intalloc(nht2); pctall=sf_intalloc(nht2); for (iht=0; iht<nht2; iht++) { mctall[iht]=iht; pctall[iht]=2*nht-iht; } /*------------------------------------------------------------*/ if(isreversed) { /* receiver wavefield is reversed */ /* read wavefield @ [0...2nht-1]*/ for(iht=0;iht<2*nht;iht++) { sf_floatread(us[iht][0],nz*nx,Fs); sf_floatread(ur[iht][0],nz*nx,Fr); } if(verb) fprintf(stderr,"nt\n"); for(it=nht;it<nt-nht;it++) { if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b%04d",it); /* read wavefield @ [2nht]*/ sf_floatread(us[ lht ][0],nz*nx,Fs); sf_floatread(ur[ lht ][0],nz*nx,Fr); #ifdef _OPENMP #pragma omp parallel for schedule(dynamic) \ private(ic, \ ihx,ihz,iht, \ mcx,mcz,mct, \ pcx,pcz,pct) \ shared (nc,ii,us,ur, \ nhx2, nhz2, nht2, \ mcxall,mczall,mctall, \ pcxall,pczall,pctall,ccin) #endif for(ic=0; ic<nc; ic++) { if(ccin[ic]) { for (iht=0; iht<nht2; iht++) { mct=mctall [iht]; pct=pctall [iht]; for (ihx=0; ihx<nhx2; ihx++) { mcx=mcxall[ic][ihx]; pcx=pcxall[ic][ihx]; for(ihz=0; ihz<nhz2; ihz++) { mcz=mczall[ic][ihz]; pcz=pczall[ic][ihz]; ii[ic][iht][ihx][ihz] += us[mct][mcx][mcz]*ur[pct][pcx][pcz]; } /* ihz */ } /* ihx */ } /* iht */ } } /* ic */ /* update wavefield index (cycle) */ for(iht=0;iht<nht2;iht++) { mctall[iht] = (mctall[iht]+1) % nht2; pctall[iht] = (pctall[iht]+1) % nht2; } lht = (lht+1) % nht2; } /* it */ if(verb) fprintf(stderr,"\n"); } else { /* receiver wavefield is NOT reversed */ /* read wavefield @ [0...2nht-1]*/ for(iht=0;iht<2*nht;iht++) { sf_floatread(us[iht][0],nz*nx,Fs); iseek = (off_t)(nt-1-iht)*nz*nx*sizeof(float); sf_seek(Fr,iseek,SEEK_SET); sf_floatread(ur[iht][0],nz*nx,Fr); } if(verb) fprintf(stderr,"nt\n"); for(it=nht;it<nt-nht;it++) { if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b%04d",nt-nht-1-it); /* read wavefield @ [2nht]*/ sf_floatread(us[ lht ][0],nz*nx,Fs); iseek=(off_t)(nt-nht-1-it)*nz*nx*sizeof(float); sf_seek(Fr,iseek,SEEK_SET); sf_floatread(ur[ lht ][0],nz*nx,Fr); #ifdef _OPENMP #pragma omp parallel for schedule(dynamic) \ private(ic, \ ihx,ihz,iht, \ mcx,mcz,mct, \ pcx,pcz,pct) \ shared (nc,ii,us,ur, \ nhx2, nhz2, nht2, \ mcxall,mczall,mctall, \ pcxall,pczall,pctall,ccin) #endif for(ic=0; ic<nc; ic++) { if(ccin[ic]) { for (iht=0; iht<nht2; iht++) { mct=mctall [iht]; pct=pctall [iht]; for (ihx=0; ihx<nhx2; ihx++) { mcx=mcxall[ic][ihx]; pcx=pcxall[ic][ihx]; for(ihz=0; ihz<nhz2; ihz++) { mcz=mczall[ic][ihz]; pcz=pczall[ic][ihz]; ii[ic][iht][ihx][ihz] += us[mct][mcx][mcz]*ur[pct][pcx][pcz]; } /* ihz */ } /* ihx */ } /* iht */ } } /* ic */ /* update wavefield index (cycle) */ for(iht=0;iht<nht2;iht++) { mctall[iht] = (mctall[iht]+1) % nht2; pctall[iht] = (pctall[iht]+1) % nht2; } lht = (lht+1) % nht2; } /* it */ if(verb) fprintf(stderr,"\n"); } /* end "is reversed" */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* scale image */ scale = 1./nt; for(ic=0; ic<nc; ic++) { for (iht=0; iht<nht2; iht++) { for (ihx=0; ihx<nhx2; ihx++) { for(ihz=0; ihz<nhz2; ihz++) { ii[ic][iht][ihx][ihz] *= scale; } } } } /*------------------------------------------------------------*/ /* apply Gaussian taper */ if(gaus) { for(ic=0; ic<nc; ic++) { for (iht=0;iht<nht2;iht++) { gt=(iht-nht)*sf_d(at); gt*=gt; for (ihx=0;ihx<nhx2;ihx++) { gx=(ihx-nhx)*sf_d(ax); gx*=gx; for(ihz=0;ihz<nhz2;ihz++) { gz=(ihz-nhz)*sf_d(az); gz*=gz; ii[ic][iht][ihx][ihz] *= exp(-gt*gst - gx*gsx - gz*gsz); } } } } } /*------------------------------------------------------------*/ /* write image */ sf_floatwrite(ii[0][0][0],nc*(nhx2)*(nhz2)*(nht2),Fi); /*------------------------------------------------------------*/ /* deallocate arrays */ free(***ii); free(**ii); free(*ii); free(ii); free(*us); free(us); free(*ur); free(ur); free(cc); free(ccin); free(*mcxall); free(mcxall); free(*pcxall); free(pcxall); free(*mczall); free(mczall); free(*pczall); free(pczall); /*------------------------------------------------------------*/ exit (0); }
int main(int argc, char* argv[]) { int nt, nt2, nx, i1, i2, n12, i, j; bool adj, sm, domod; float dt, dt2, dx, ot, ot2, ox, epst2; float v_1, v_2, v_3, v_4, eps, passthr; float * data, * output, * datat2, * outputt2, * model; sf_file inp, out; /* PWD parameters */ int nw, nj1; float *pp, *pwdata; sf_file dip,outpwdcheck,outdipcheck; /* kirchhoff params */ bool half, verb,normalize,debug; int nh, **fold, apt; float **v, rho, *off; float h0, dh, aal, angle; int ix, ih, nh2; sf_file vel, gather, offset; //MADAGASCAR C API /* initialize */ sf_init(argc,argv); inp = sf_input("in"); out = sf_output("out"); vel = sf_input("vel"); dip = sf_input("dip"); //initialize file with dip /* get dimensions from input */ if (!sf_histint(inp,"n1",&nt)) sf_error("No n1= in inp"); if (!sf_histint(inp,"n2",&nx)) sf_error("No n2= in inp"); if (!sf_histfloat(inp,"d1",&dt)) sf_error("No d1= in inp"); if (!sf_histfloat(inp,"d2",&dx)) sf_error("No d2= in inp"); if (!sf_histfloat(inp,"o1",&ot)) sf_error("No o1= in inp"); if (!sf_histfloat(inp,"o2",&ox)) sf_error("No o2= in inp"); /* get parameters from command line */ /* adjoint flag */ if (!sf_getbool("adj",&adj)) adj=false; /* if perform derivative filtering = PWD */ if (!sf_getbool("sm",&sm)) sm=true; /* if perform modelling via Kirchhoff */ if (!sf_getbool("domod",&domod)) domod=true; /* debug flag */ if (!sf_getbool("debug",&debug)){ debug=false; outpwdcheck = NULL; outdipcheck = NULL; } else { outpwdcheck = sf_output("outpwd"); outdipcheck = sf_output("outdip"); } /* kirchhoff parameters */ //////////////////////////////////////////////////////////////////////////////////////// if (!sf_getbool("normalize",&normalize)) normalize=true; /* normalize for the fold */ if (normalize) { fold = sf_intalloc2(nt,nx); } else { fold = NULL; } if (adj) { if (!sf_histint(inp,"n3",&nh)) sf_error("No n3="); sf_putint(out,"n3",1); } else { if (!sf_getint("nh",&nh)) sf_error("Need nh="); /* number of offsets (for modeling) */ sf_putint(out,"n3",nh); } if (NULL != sf_getstring("gather")) { gather = sf_output("gather"); } else { gather = NULL; } if (!sf_getfloat("antialias",&aal)) aal = 1.0; /* antialiasing */ if (!sf_getint("apt",&apt)) apt = nx; /* integral aperture */ if (!sf_getfloat("angle",&angle)) angle = 90.0; /* angle aperture */ angle = fabsf(tanf(angle*SF_PI/180.0)); if (!sf_getbool("half",&half)) half = true; /* if y, the third axis is half-offset instead of full offset */ if (!sf_getbool("verb",&verb)) verb = true; /* verbosity flag */ if (!sf_getfloat("rho",&rho)) rho = 1.-1./nt; /* Leaky integration constant */ if (NULL != sf_getstring("offset")) { offset = sf_input("offset"); nh2 = sf_filesize(offset); if (nh2 != nh*nx) sf_error("Wrong dimensions in offset"); off = sf_floatalloc(nh2); sf_floatread (off,nh2,offset); sf_fileclose(offset); } else { if (adj) { if (!sf_histfloat(inp,"o3",&h0)) sf_error("No o3="); if (!sf_histfloat(inp,"d3",&dh)) sf_error("No d3="); sf_putfloat(out,"d3",1.); sf_putfloat(out,"o3",0.); } else { if (!sf_getfloat("dh",&dh)) sf_error("Need dh="); /* offset sampling (for modeling) */ if (!sf_getfloat("h0",&h0)) sf_error("Need h0="); /* first offset (for modeling) */ sf_putfloat(out,"d3",dh); sf_putfloat(out,"o3",h0); } if (!half) dh *= 0.5; off = sf_floatalloc(nh*nx); for (ix = 0; ix < nx; ix++) { for (ih = 0; ih < nh; ih++) { off[ih*nx+ix] = h0 + ih*dh; } } offset = NULL; } //////////////////////////////////////////////////////////////////////////////////////// /* path-integral range */ if (!sf_getfloat("v_1",&v_1)) sf_error("No integration range specified"); if (!sf_getfloat("v_2",&v_2)) sf_error("No integration range specified"); if (!sf_getfloat("v_3",&v_3)) sf_error("No integration range specified"); if (!sf_getfloat("v_4",&v_4)) sf_error("No integration range specified"); if (!sf_getfloat("passthr",&passthr)) passthr = 0.001; // threshold for tail elimination if (!sf_getfloat("eps",&eps)) eps = 0.001; // damper for pi if (!sf_getfloat("epst2",&epst2)) epst2 = 0.001; // damper for t2warp /* new axis length */ if (!sf_getint("pad",&nt2)) nt2=nt; /* output time samples */ n12 = nt2*nx; data = sf_floatalloc(nt*nx); model = sf_floatalloc(nt*nx); datat2 = sf_floatalloc(nt2*nx); outputt2 = sf_floatalloc(nt2*nx); output = sf_floatalloc(nt*nx); pwdata = NULL; // allocate dip if (sm){ pp = sf_floatalloc(nt*nx); //allocate space for dip sf_floatread(pp,nt*nx,dip); //read dip pwdata = sf_floatalloc(nt*nx); //allocate space for pwd data if (!sf_getint("order",&nw)) nw=1; /* [1,2,3] accuracy order */ if (nw < 1 || nw > 3) sf_error ("Unsupported nw=%d, choose between 1 and 3",nw); if (!sf_getint("nj1",&nj1)) nj1=1; /* antialiasing */ allpass32d_init(allpass_init(nw, nj1, nt,nx,1, pp)); //initialize all-pass-filter structure } // allocating and reading velocity v = sf_floatalloc2(nt,nx); sf_floatread(v[0],nt*nx,vel); if(!adj) { if(domod){// perform modelling // reading data sf_floatread(model,nt*nx,inp); // modelling via mig2 mig2_lop(false,half,verb,normalize,nt,nx,nh,fold,apt,data,v,rho,model,off,h0,dh,dx,ot,dt,aal,angle); } else {// just read the data sf_warning("modelling is disabled"); sf_floatread(data,nt*nx,inp); } // internal else if (sm){// perform PWD allpass32d_lop(adj,false,nt*nx,nt*nx,data,pwdata); if (debug){ sf_floatwrite(pwdata,nt*nx,outpwdcheck); sf_floatwrite(pp,nt*nx,outdipcheck); } //change the address for(i=0;i<nt*nx;i++){ data[i]=pwdata[i]; } }//PWD flag } else {// adj flag // read data currently 2D sf_floatread(data,nt*nx,inp); }// adj flag // t2warping axis evaluation ot2 = ot*ot; dt2 = ot+(nt-1)*dt; dt2 = (dt2*dt2 - ot2)/(nt2-1); // take in account different output trace length t2warp_init(nt,nt2,nx,ot,dt,ot2,dt2,epst2); sf_warning("t2warp_init(nt,nt2,nx,ot,dt,ot2,dt2,epst2);\n"); // compute pi filter sf_warning("be4 filter"); flatpifilt_init(nt2, nx, dt2, dx, 0.001, v_1, v_2, v_3, v_4, eps); sf_warning("after filter"); sf_warning("pifilt_init(nt2, nx, dt2, dx, v_a, v_b, v_0, beta, eps);\n"); if(adj) { sf_warning("be4 the chain: params %d %d %d %d",nt*nx,nt2*nx,nt2*nx,nt*nx); sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,adj,false,nt*nx,nt2*nx,nt2*nx,nt*nx,output,data,outputt2,datat2); sf_warning("running chain"); } else { sf_chain3(t2warp_inv,freqfilt4pi_lop,t2warp,false,false,nt*nx,nt2*nx,nt2*nx,nt*nx,data,output,datat2,outputt2); } if(adj) { if (sm){ sf_warning("performing PWD"); allpass32d_lop(adj,false,nt*nx,nt*nx,pwdata,output); //change the address for (i=0;i<nt*nx;i++){ output[i]=pwdata[i]; } } if (domod) { sf_warning("performing Kirchhoff migration"); mig2_lop(true,half,verb,normalize,nt,nx,nh,fold,apt,output,v,rho,model,off,h0,dh,dx,ot,dt,aal,angle); } else { sf_warning("changing the address"); //change the address for (i=0;i<nt*nx;i++){ model[i]=output[i]; } } } // adj flag sf_warning("done with output"); if (!adj) { // write sf_floatwrite(output,nt*nx,out); } else { // write sf_floatwrite(model,nt*nx,out); } exit(0); }
int main(int argc, char* argv[]) { bool adj, velocity, l1norm, plane[3], verb; int dim, i, count, n[SF_MAX_DIM], it, nt, **m, nrhs, is, nshot=1, *flag, order, iter, niter, stiter, *k, nfreq, nmem; float o[SF_MAX_DIM], d[SF_MAX_DIM], **t, *t0, *s, *temps, **source, *rhs, *ds; float rhsnorm, rhsnorm0, rhsnorm1, rate, eps, gama; char key[4], *what; sf_file sinp, sout, shot, time, reco, rece, topo, grad, norm; sf_weight weight=NULL; sf_init(argc,argv); sinp = sf_input("in"); sout = sf_output("out"); if (NULL == (what = sf_getstring("what"))) what="tomo"; /* what to compute (default tomography) */ switch (what[0]) { case 'l': /* linear operator */ if (NULL == sf_getstring("time")) sf_error("Need time="); time = sf_input("time"); /* read operator dimension from time table */ dim = sf_filedims(time,n); nt = 1; for (i=0; i < 3; i++) { sprintf(key,"d%d",i+1); if (!sf_histfloat(time,key,d+i)) sf_error("No %s= in input",key); sprintf(key,"o%d",i+1); if (!sf_histfloat(time,key,o+i)) o[i]=0.; nt *= n[i]; plane[i] = false; } if (dim < 3) { n[2] = 1; o[2] = o[1]; d[2] = d[1]; plane[2] = false; } dim = 2; /* read in shot file */ if (NULL == sf_getstring("shot")) sf_error("Need source shot="); shot = sf_input("shot"); if (!sf_histint(shot,"n2",&nshot)) nshot=1; sf_fileclose(shot); /* read in receiver file */ m = sf_intalloc2(nt,nshot); if (NULL == sf_getstring("receiver")) { for (is=0; is < nshot; is++) { for (it=0; it < nt; it++) { m[is][it] = 1; } } } else { rece = sf_input("receiver"); sf_intread(m[0],nt*nshot,rece); sf_fileclose(rece); } /* number of right-hand side */ nrhs = 0; for (is=0; is < nshot; is++) { for (it=0; it < nt; it++) { if (m[is][it] == 1) nrhs++; } } rhs = sf_floatalloc(nrhs); t = sf_floatalloc2(nt,nshot); sf_floatread(t[0],nt*nshot,time); if (!sf_getbool("adj",&adj)) adj=false; /* adjoint flag (for what=linear) */ /* initialize fatomo */ fatomo_init(dim,n,d,nshot); /* set operators */ fatomo_set(t,m); t0 = sf_floatalloc(nt); if (adj) { sf_floatread(rhs,nrhs,sinp); fatomo_lop(true,false,nt,nrhs,t0,rhs); sf_putint(sout,"n1",nt); sf_putint(sout,"n2",1); sf_putint(sout,"n3",1); sf_floatwrite(t0,nt,sout); } else { sf_floatread(t0,nt,sinp); fatomo_lop(false,false,nt,nrhs,t0,rhs); sf_putint(sout,"n1",nrhs); sf_putint(sout,"n2",1); sf_putint(sout,"n3",1); sf_floatwrite(rhs,nrhs,sout); } break; case 't': /* tomography */ /* read input dimension */ dim = sf_filedims(sinp,n); nt = 1; for (i=0; i < dim; i++) { sprintf(key,"d%d",i+1); if (!sf_histfloat(sinp,key,d+i)) sf_error("No %s= in input",key); sprintf(key,"o%d",i+1); if (!sf_histfloat(sinp,key,o+i)) o[i]=0.; nt *= n[i]; plane[i] = false; } if (dim < 3) { n[2] = 1; o[2] = o[1]; d[2] = d[1]; plane[2] = false; } /* read initial guess */ s = sf_floatalloc(nt); sf_floatread(s,nt,sinp); if (!sf_getbool("velocity",&velocity)) velocity=true; /* if y, the input is velocity; n, slowness squared */ if (velocity) { for (it=0; it < nt; it++) { s[it] = 1./s[it]*1./s[it]; } } /* allocate memory for temporary data */ ds = sf_floatalloc(nt); flag = sf_intalloc(nt); temps = sf_floatalloc(nt); for (it=0; it < nt; it++) { temps[it] = s[it]; } if (!sf_getbool("l1norm",&l1norm)) l1norm=false; /* norm for minimization (default L2 norm) */ if (!sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ /* read in shot file */ if (NULL == sf_getstring("shot")) sf_error("Need source shot="); shot = sf_input("shot"); if (!sf_histint(shot,"n2",&nshot)) nshot=1; source = sf_floatalloc2(3,nshot); sf_floatread(source[0],3*nshot,shot); sf_fileclose(shot); /* allocate memory for time table */ t = sf_floatalloc2(nt,nshot); /* read in receiver file */ m = sf_intalloc2(nt,nshot); if (NULL == sf_getstring("receiver")) { for (is=0; is < nshot; is++) { for (it=0; it < nt; it++) { m[is][it] = 1; } } } else { rece = sf_input("receiver"); sf_intread(m[0],nt*nshot,rece); sf_fileclose(rece); } /* number of right-hand side */ nrhs = 0; for (is=0; is < nshot; is++) { for (it=0; it < nt; it++) { if (m[is][it] == 1) nrhs++; } } rhs = sf_floatalloc(nrhs); /* read in record file */ if (NULL == sf_getstring("record")) sf_error("Need data record="); reco = sf_input("record"); t0 = sf_floatalloc(nrhs); sf_floatread(t0,nrhs,reco); sf_fileclose(reco); /* read in topography file */ if (NULL != sf_getstring("topo")) { topo = sf_input("topo"); k = sf_intalloc(nt); sf_intread(k,nt,topo); sf_fileclose(topo); } else { k = NULL; } if (!sf_getint("order",&order)) order=2; /* fast marching accuracy order */ if (!sf_getint("niter",&niter)) niter=10; /* number of slowness inversion iterations */ if (!sf_getint("stiter",&stiter)) stiter=200; /* number of step iterations */ if (!sf_getfloat("eps",&eps)) eps=0.; /* regularization parameter */ /* output gradient at each iteration */ if (NULL != sf_getstring("gradient")) { grad = sf_output("gradient"); sf_putint(grad,"n3",n[2]); sf_putfloat(grad,"d3",d[2]); sf_putfloat(grad,"o3",o[2]); sf_putint(grad,"n4",niter); } else { grad = NULL; } /* output misfit L2 norm at each iteration */ if (NULL != sf_getstring("misnorm")) { norm = sf_output("misnorm"); sf_putint(norm,"n1",niter+1); sf_putfloat(norm,"d1",1.); sf_putfloat(norm,"o1",0.); sf_putint(norm,"n2",1); sf_putint(norm,"n3",1); } else { norm = NULL; } /* initialize fatomo */ fatomo_init(dim,n,d,nshot); /* initialize 2D gradient operator */ sf_igrad2_init(n[0],n[1]); if (l1norm) { /* if (!sf_getfloat("perc",&perc)) perc=90.; l1_init(nt,stiter,perc,false); */ if (!sf_getint("nfreq",&nfreq)) nfreq=1; /* l1-norm weighting nfreq */ if (!sf_getint("nmem",&nmem)) nmem=1; /* l1-norm weighting nmem */ weight = sf_l1; sf_irls_init(nt); } /* initial misfit */ fastmarch_init(n[2],n[1],n[0]); i = 0; for (is=0; is < nshot; is++) { fastmarch(t[is],s,flag,plane, n[2],n[1],n[0],o[2],o[1],o[0],d[2],d[1],d[0], source[is][2],source[is][1],source[is][0],1,1,1,order); for (it=0; it < nt; it++) { if (m[is][it] == 1) { rhs[i] = t0[i]-t[is][it]; i++; } } } fastmarch_close(); /* calculate L2 data-misfit */ rhsnorm0 = cblas_snrm2(nrhs,rhs,1); rhsnorm = rhsnorm0; rhsnorm1 = rhsnorm; rate = rhsnorm1/rhsnorm0; if (l1norm) sf_warning("L1 misfit after iteration 0 of %d: %g",niter,rate); else sf_warning("L2 misfit after iteration 0 of %d: %g",niter,rate); if (norm != NULL) sf_floatwrite(&rate,1,norm); /* iterations over inversion */ for (iter=0; iter < niter; iter++) { /* clean-up */ for (it=0; it < nt; it++) { ds[it] = 0.; } /* prepare for CG */ fatomo_set(t,m); /* solve ds */ if (l1norm) { /* sf_solver_reg(fatomo_lop,l1step,sf_igrad2_lop,2*nt, nt,nrhs,ds,rhs,stiter,eps,"verb",verb,"end"); */ sf_solver_reg(fatomo_lop,sf_cgstep,sf_igrad2_lop,2*nt,nt,nrhs,ds,rhs,stiter,eps,"wght",weight,"nfreq",nfreq,"nmem",nmem,"verb",verb,"end"); /* l1step_close(); */ sf_cgstep_close(); } else { sf_solver_reg(fatomo_lop,sf_cgstep,sf_igrad2_lop,2*nt,nt,nrhs,ds,rhs,stiter,eps,"verb",verb,"end"); sf_cgstep_close(); } /* line search */ gama = 1.; for (count=0; count < 10; count++) { /* update slowness */ for (it=0; it < nt; it++) { if (k == NULL || k[it] != 1) temps[it] = (s[it]+gama*ds[it])*(s[it]+gama*ds[it])/s[it]; } /* forward fast-marching for stencil time */ fastmarch_init(n[2],n[1],n[0]); i = 0; for (is=0; is < nshot; is++) { fastmarch(t[is],temps,flag,plane, n[2],n[1],n[0],o[2],o[1],o[0],d[2],d[1],d[0], source[is][2],source[is][1],source[is][0],1,1,1,order); for (it=0; it < nt; it++) { if (m[is][it] == 1) { rhs[i] = t0[i]-t[is][it]; i++; } } } fastmarch_close(); rhsnorm = cblas_snrm2(nrhs,rhs,1); rate = rhsnorm/rhsnorm1; if (rate < 1.) { for (it=0; it < nt; it++) { s[it] = temps[it]; } rhsnorm1 = rhsnorm; rate = rhsnorm1/rhsnorm0; break; } gama *= 0.5; } if (count == 10) { sf_warning("Line-search Failure. Iteration terminated at %d of %d.",iter+1,niter); sf_warning("Dimensions for GRAD and NORM need to be fixed before read."); break; } if (l1norm) sf_warning("L1 misfit after iteration %d of %d: %g (line-search %d)",iter+1,niter,rate,count); else sf_warning("L2 misfit after iteration %d of %d: %g (line-search %d)",iter+1,niter,rate,count); if (grad != NULL) sf_floatwrite(ds,nt,grad); if (norm != NULL) sf_floatwrite(&rate,1,norm); } /* convert to velocity */ if (velocity) { for (it=0; it < nt; it++) { s[it] = 1./sqrtf(s[it]); } } sf_floatwrite(s,nt,sout); break; } exit(0); }
void dsr2_init(int nz1, float dz1 /* depth */, int nh1, float dh1, float h01 /* half-offset */, int nx1, float dx1, float x01 /* midpoint */, int nu1, float du, float u0 /* slowness grid */, int ntx, int nth /* taper size */, int nr1 /* number of references */, int npad /* padding on nh */) /*< initialize >*/ { int ix, ih, iu; int jx, jh; float x, h; float kx, kh, k; float dx, dh; float x0, h0; nz = nz1; dz = dz1; nx = nx1; dx = 2.0*SF_PI/(nx*dx1); x0 = -SF_PI/ dx1 ; nh = nh1; nh2 = nh+npad; dh = 2.0*SF_PI/(nh2*dh1); h0 = -SF_PI/ dh1 ; nu = nu1; nrmax = nr1; fft2_init(nh2,nx); /* allocate workspace */ sz = sf_floatalloc2 (nrmax,nz); /* reference slowness */ sm = sf_floatalloc2 (nrmax,nz); /* reference slowness squared*/ nr = sf_intalloc ( nz); /* number of reference slownesses */ qq = sf_floatalloc2 (nh,nu); /* image */ ks = sf_floatalloc2 (nh2,nx); /* source wavenumber */ kr = sf_floatalloc2 (nh2,nx); /* receiver wavenumber */ is = sf_intalloc2 (nh, nx); /* source reference */ ir = sf_intalloc2 (nh, nx); /* receiver reference */ ii = sf_intalloc (nx); /* midpoint reference */ pk = sf_complexalloc2 (nh2,nx); /* padded wavefield */ wx = sf_complexalloc2 (nh, nx); /* x wavefield */ wk = sf_complexalloc2 (nh2,nx); /* k wavefield */ ms = sf_intalloc2 (nh, nx); /* MRS map source */ mr = sf_intalloc2 (nh, nx); /* MRS map receiver */ ma = sf_floatalloc2 (nh, nx); /* MRS mask */ skip = sf_boolalloc3 (nrmax,nrmax,nz); /* skip slowness combination */ /* precompute wavenumbers */ for (ix=0; ix<nx; ix++) { jx = (ix < nx/2)? ix + nx/2: ix - nx/2; kx = x0 + jx*dx; x = x01 + ix*dx1; iu = 0.5+(x-u0)/du; if (iu < 0) iu=0; else if (iu >= nu) iu=nu-1; ii[ix] = iu; for (ih=0; ih<nh; ih++) { h = h01 + ih*dh1; iu = 0.5+(x-h-u0)/du; if (iu < 0) iu=0; else if (iu >= nu) iu=nu-1; is[ix][ih] = iu; iu = 0.5+(x+h-u0)/du; if (iu < 0) iu=0; else if (iu >= nu) iu=nu-1; ir[ix][ih] = iu; } for (ih=0; ih<nh2; ih++) { jh = (ih < nh2/2)? ih + nh2/2: ih - nh2/2; kh = h0 + jh*dh; k = 0.5*(kx-kh); ks[ix][ih] = k*k; k = 0.5*(kx+kh); kr[ix][ih] = k*k; } } /* precompute taper array */ taper2_init(nx,nh,ntx,nth,true,false); mms = sf_fslice_init(nh*nx,nz,sizeof(int)); mmr = sf_fslice_init(nh*nx,nz,sizeof(int)); }
/*------------------------------------------------------------*/ int main(int argc, char* argv[]) { bool verb; /* verbosity flag */ bool pos; /* direction of spraying */ bool adj; /* adjoint operator flag */ bool wflcausal, oprcausal; /* causal wfl?, opr? */ sf_file Fopr, Fwfl, Fimg, Fcip; /* I/O files */ float ****opr=NULL,****wfl=NULL,*****img=NULL; int itO,itW; sf_axis az,ax,ay,at,ac,aa; /* wfld axes */ int nz,nx,ny,nt,nc; int iz,ix,iy,it,ic; sf_axis ahx, ahy, ahz, aht; /* EIC axes */ int nhx, nhy, nhz, nht; int ihx, ihy, ihz, iht; float dhx, dhy, dhz, dht; pt3d *cc=NULL; bool *ccin=NULL; float cxmin,czmin,cymin; float cxmax,czmax,cymax; int icx, icz, icy; int mcx, mcz, mcy, mct; int pcx, pcz, pcy, pct; int **mcxall, **pcxall; int **mcyall, **pcyall; int **mczall, **pczall; int *mctall, *pctall; int lht,fht; /* last buffer index */ float scale; /* time summation scaling */ int nslice; /* wavefield slice size */ bool gaus; /* gaussian taper */ float gsx,gsy,gsz,gst; /* std dev */ /*------------------------------------------------------------*/ sf_init(argc,argv); #ifdef _OPENMP omp_init(); #endif if(! sf_getbool( "verb",&verb )) verb=false; /* verbosity flag */ if(! sf_getbool( "positive",&pos )) pos=true; /* if positive sprays opr to positive shits, else, sprays to negative shifts */ if(! sf_getbool( "adj",&adj )) adj=false; /* adjoint flag */ if(! sf_getbool("wflcausal",&wflcausal)) wflcausal=false; /* causal wfl? */ if(! sf_getbool("oprcausal",&oprcausal)) oprcausal=false; /* causal opr? */ /*------------------------------------------------------------*/ Fopr = sf_input ("opr" ); /* operator */ az=sf_iaxa(Fopr,1); if(verb) sf_raxa(az); nz = sf_n(az); ax=sf_iaxa(Fopr,2); if(verb) sf_raxa(ax); nx = sf_n(ax); ay=sf_iaxa(Fopr,3); if(verb) sf_raxa(ay); ny = sf_n(ay); at=sf_iaxa(Fopr,4); if(verb) sf_raxa(at); nt = sf_n(at); scale = 1./nt; /* time summation scaling */ nslice = nz*nx*ny*sizeof(float); /* wavefield slice */ Fcip = sf_input ("cip" ); /* CIP coordinates */ ac = sf_iaxa(Fcip,2); sf_setlabel(ac,"c"); sf_setunit(ac,""); if(verb) sf_raxa(ac); nc = sf_n(ac); /*------------------------------------------------------------*/ /* setup output */ if(adj) { Fimg = sf_input ("in"); /* read img */ ahz=sf_iaxa(Fimg,1); nhz=(sf_n(ahz)-1)/2; if(verb) sf_raxa(ahz); ahx=sf_iaxa(Fimg,2); nhx=(sf_n(ahx)-1)/2; if(verb) sf_raxa(ahx); ahy=sf_iaxa(Fimg,3); nhy=(sf_n(ahy)-1)/2; if(verb) sf_raxa(ahy); aht=sf_iaxa(Fimg,4); nht=(sf_n(aht)-1)/2; if(verb) sf_raxa(aht); aa=sf_maxa(1,0,1); sf_setlabel(aa,""); sf_setunit(aa,""); /* set output axes */ Fwfl = sf_output("out"); /* write wfl */ sf_oaxa(Fwfl,az,1); sf_oaxa(Fwfl,ax,2); sf_oaxa(Fwfl,ay,3); sf_oaxa(Fwfl,at,4); sf_oaxa(Fwfl,aa,5); } else { Fwfl = sf_input ( "in"); /* read wfl */ if(! sf_getint("nhz",&nhz)) nhz=0; /* z lags */ dhz=2*sf_d(az); ahz=sf_maxa(2*nhz+1,-nhz*dhz,dhz); sf_setlabel(ahz,"hz"); sf_setunit(ahz,""); if(verb) sf_raxa(ahz); if(! sf_getint("nhx",&nhx)) nhx=0; /* x lags */ dhx=2*sf_d(ax); ahx=sf_maxa(2*nhx+1,-nhx*dhx,dhx); sf_setlabel(ahx,"hx"); sf_setunit(ahx,""); if(verb) sf_raxa(ahx); if(! sf_getint("nhy",&nhy)) nhy=0; /* y lags */ dhy=2*sf_d(ay); ahy=sf_maxa(2*nhy+1,-nhy*dhy,dhy); sf_setlabel(ahy,"hy"); sf_setunit(ahy,""); if(verb) sf_raxa(ahy); if(! sf_getint("nht",&nht)) nht=0; /* t lags */ dht=2*sf_d(at); aht=sf_maxa(2*nht+1,-nht*dht,dht); sf_setlabel(aht,"ht"); sf_setunit(aht,""); if(verb) sf_raxa(aht); Fimg = sf_output("out"); /* write img */ sf_oaxa(Fimg,ahz,1); sf_oaxa(Fimg,ahx,2); sf_oaxa(Fimg,ahy,3); sf_oaxa(Fimg,aht,4); sf_oaxa(Fimg, ac,5); } /*------------------------------------------------------------*/ if(! sf_getbool("gaus",&gaus)) gaus=false; /* Gaussian taper */ if(gaus) { if(! sf_getfloat("gsx",&gsx)) gsx=0.25*sf_n(ahx)*sf_d(ahx); gsx=(nhx==0)?1:1./(2*gsx*gsx); if(! sf_getfloat("gsy",&gsy)) gsy=0.25*sf_n(ahy)*sf_d(ahy); gsy=(nhy==0)?1:1./(2*gsy*gsy); if(! sf_getfloat("gsz",&gsz)) gsz=0.25*sf_n(ahz)*sf_d(ahz); gsz=(nhz==0)?1:1./(2*gsz*gsz); if(! sf_getfloat("gst",&gst)) gst=0.25*sf_n(aht)*sf_d(aht); gst=(nht==0)?1:1./(2*gst*gst); } /*------------------------------------------------------------*/ /* allocate arrays */ opr=sf_floatalloc4(nz,nx,ny,sf_n(aht)); wfl=sf_floatalloc4(nz,nx,ny,sf_n(aht)); img=sf_floatalloc5(sf_n(ahz),sf_n(ahx),sf_n(ahy),sf_n(aht),sf_n(ac)); /*------------------------------------------------------------*/ /* CIP coordinates */ cc= (pt3d*) sf_alloc(nc,sizeof(*cc)); pt3dread1(Fcip,cc,nc,3); mcxall=sf_intalloc2(sf_n(ahx),sf_n(ac)); pcxall=sf_intalloc2(sf_n(ahx),sf_n(ac)); mcyall=sf_intalloc2(sf_n(ahy),sf_n(ac)); pcyall=sf_intalloc2(sf_n(ahy),sf_n(ac)); mczall=sf_intalloc2(sf_n(ahz),sf_n(ac)); pczall=sf_intalloc2(sf_n(ahz),sf_n(ac)); ccin=sf_boolalloc(sf_n(ac)); cxmin = sf_o(ax) + nhx *sf_d(ax); cxmax = sf_o(ax) + (sf_n(ax)-1-nhx)*sf_d(ax); cymin = sf_o(ay) + nhy *sf_d(ay); cymax = sf_o(ay) + (sf_n(ay)-1-nhy)*sf_d(ay); czmin = sf_o(az) + nhz *sf_d(az); czmax = sf_o(az) + (sf_n(az)-1-nhz)*sf_d(az); for(ic=0;ic<nc;ic++) { ccin[ic]=(cc[ic].x>=cxmin && cc[ic].x<=cxmax && cc[ic].y>=cymin && cc[ic].y<=cymax && cc[ic].z>=czmin && cc[ic].z<=czmax)?true:false; if(ccin[ic]) { icx = 0.5+(cc[ic].x-sf_o(ax))/sf_d(ax); for(ihx=-nhx; ihx<nhx+1; ihx++) { mcxall[ic][nhx+ihx] = icx-ihx; pcxall[ic][nhx+ihx] = icx+ihx; } icy = 0.5+(cc[ic].y-sf_o(ay))/sf_d(ay); for(ihy=-nhy; ihy<nhy+1; ihy++) { mcyall[ic][nhy+ihy] = icy-ihy; pcyall[ic][nhy+ihy] = icy+ihy; } icz = 0.5+(cc[ic].z-sf_o(az))/sf_d(az); for(ihz=-nhz; ihz<nhz+1; ihz++) { mczall[ic][nhz+ihz] = icz-ihz; pczall[ic][nhz+ihz] = icz+ihz; } } } mctall=sf_intalloc(sf_n(aht)); pctall=sf_intalloc(sf_n(aht)); for (iht=0; iht<sf_n(aht); iht++) { mctall[iht]= iht; pctall[iht]=sf_n(aht)-1-iht; } if(adj) { /* ADJIONT OPERATOR */ for(iht=0;iht<sf_n(aht);iht++) CICLOOP( wfl[iht][iy][ix][iz]=0; ); /* zero wfl */ for(it=0;it<nt;it++) sf_floatwrite(wfl[0][0][0],nz*nx*ny,Fwfl); /* reserve wfl */ sf_seek(Fwfl,0,SEEK_SET); /* seek back */ sf_floatread(img[0][0][0][0],sf_n(ac)*sf_n(ahy)*sf_n(ahx)*sf_n(ahz)*sf_n(aht),Fimg); /* read img */ ; applyScaling (img,ac,aht,ahx,ahy,ahz,scale); /* scaling */ if(gaus) applyGaussian(img,ac,aht,ahx,ahy,ahz,gst,gsx,gsy,gsz); /* Gaussian */ lht=0; itO=-999999; itW=-999999; for(it=-nht;it<nt+nht;it++) { if(verb) fprintf(stderr,"\b\b\b\b\b\b%04d",it); fht=(lht+1) % sf_n(aht); if(it<nt-nht) { itO = it + nht; if( !oprcausal ) sf_seek(Fopr,(off_t)(nt-1-itO)*nslice,SEEK_SET); else sf_seek(Fopr,(off_t) itO *nslice,SEEK_SET); sf_floatread(opr[ lht ][0][0],nz*nx*ny,Fopr); } for(iht=0;iht<sf_n(aht);iht++) { mctall[iht] = (mctall[iht]+1) % sf_n(aht); /* cycle iht index */ pctall[iht] = (pctall[iht]+1) % sf_n(aht); } if(it>=0+nht && it<nt-nht) { #ifdef _OPENMP #pragma omp parallel for schedule(dynamic) \ private(ic, ihx,ihy,ihz,iht,mcx, mcy, mcz, mct, pcx, pcy, pcz, pct) \ shared (nc,ccin,ahx,ahy,ahz,aht,mcxall,mcyall,mczall,mctall,pcxall,pcyall,pczall,pctall) #endif for(ic=0;ic<nc;ic++){ if(ccin[ic]) { /* sum over c only! */ if(pos){ EICLOOP( wfl [mct][mcy][mcx][mcz] += opr [pct][pcy][pcx][pcz] * img[ic][iht][ihy][ihx][ihz]; ); }else{ EICLOOP( wfl [pct][pcy][pcx][pcz] += opr [mct][mcy][mcx][mcz] * img[ic][iht][ihy][ihx][ihz]; ); } } }