Esempio n. 1
0
void fwi(sf_file Fdat, sf_file Finv, sf_file Ferr, sf_file Fgrad, sf_mpi *mpipar, sf_sou soupar, sf_acqui acpar, sf_vec_s array, sf_fwi_s fwipar, sf_optim optpar, bool verb1, int seislet)
/*< fwi >*/
{
	int iter=0, flag;
	float fcost;
	float *x, *direction, *grad;
	sf_gradient gradient;
	FILE *fp;

	/* initialize */
	gradient_init(Fdat, mpipar, soupar, acpar, array, fwipar, verb1);

	/* gradient type */
	gradient=gradient_standard;
	x=array->vv;

	/* calculate first gradient */
	grad=sf_floatalloc(nzx);
	gradient(x, &fcost, grad);

	/* output first gradient */
	if(mpipar->cpuid==0) sf_floatwrite(grad, nzx, Fgrad);

	/* if onlygrad=y, program terminates */
	if(fwipar->onlygrad) return; 

	if(mpipar->cpuid==0) fp=fopen("iterate.txt","a");

	direction=sf_floatalloc(nzx);
	optpar->sk=sf_floatalloc2(nzx, optpar->npair);
	optpar->yk=sf_floatalloc2(nzx, optpar->npair);

	optpar->igrad=1;
	optpar->ipair=0;
	optpar->ils=0;
	optpar->fk=fcost;
	optpar->f0=fcost;
	optpar->alpha=1.;
	/* initialize data error vector */
	for(iter=0; iter<optpar->nerr; iter++){
		optpar->err[iter]=0.;
	}
	optpar->err[0]=optpar->fk;
	if (optpar->err_type==1) optpar->err[optpar->nerr/2]=swap;

	iter=0;
	if(mpipar->cpuid==0){
		l2norm(nzx, grad, &optpar->gk_norm);
		print_iteration(fp, iter, optpar);
	}

	/* optimization loop */
	for(iter=0; iter<optpar->niter; iter++){
		if(mpipar->cpuid==0) sf_warning("-------------------iter=%d----------------------", iter+1);
		
		optpar->ils=0;
		if(iter%optpar->repeat==0) optpar->alpha=1.;

		/* search direction */
		if(iter==0){
			reverse(nzx, grad, direction);
		}else{
			lbfgs_update(nzx, x, grad, optpar->sk, optpar->yk, optpar);
			lbfgs_direction(nzx, grad, direction, optpar->sk, optpar->yk, optpar);
		}

		/* line search */
		lbfgs_save(nzx, x, grad, optpar->sk, optpar->yk, optpar);
		line_search(nzx, x, grad, direction, gradient, optpar, threshold, &flag, mpipar->cpuid, 1);
		optpar->err[iter+1]=optpar->fk;
		if (optpar->err_type==1) optpar->err[optpar->nerr/2+iter+1]=swap;
		
		if(mpipar->cpuid==0){
			l2norm(nzx, grad, &optpar->gk_norm);
			print_iteration(fp, iter+1, optpar);
			fclose(fp); /* get written to disk right away */
			fp=fopen("iterate.txt","a");
		}

		if(mpipar->cpuid==0 && flag==2){
			fprintf(fp, "Line Search Failed\n");
			break;
		}

		if(mpipar->cpuid==0 && optpar->fk/optpar->f0 < optpar->conv_error){
			fprintf(fp, "Convergence Criterion Reached\n");
			break;
		}
	} // end of iter

	if(mpipar->cpuid==0 && iter==optpar->niter){
		fprintf(fp, "Maximum Iteration Number Reached\n");
	}

	/* output vel & misfit */
	if(mpipar->cpuid==0) sf_floatwrite(x, nzx, Finv);
	if(mpipar->cpuid==0) sf_floatwrite(optpar->err, optpar->nerr, Ferr);
	if(mpipar->cpuid==0) fclose(fp);

	return;
}
Esempio n. 2
0
int main(int argc, char* argv[])
{


  int nt, nx, it, ix, niter, iter, ntfft, nxfft,np, ip, ikt, ikx, iktn, ikxn, ifsnr; /* iktn, ikxn, iNyquist*/
  float dt, dx, pmin, pmax, dp, p, cmax, scalar, sembpmax, num, den;
  float *sembp, *mask, *gy, *fden, *fshift, *SNR;
  float **fdata, **taup, **odata, **tdata, **odatat, **semb; /* tdata is the true data */
  kiss_fft_cpx **cdata, **cdatat;
  char *type;
  sf_file inp, outp, m, spec1, spec2, trued, snr; 


  sf_init(argc,argv);

  inp=sf_input("in");
  m=sf_input("mask");
  outp=sf_output("out");

  if(!sf_histint(inp,"n1",&nt)) sf_warning("No n1 in input");
  if(!sf_histint(inp,"n2",&nx)) sf_warning("No n2 in input");
  if(!sf_histfloat(inp,"d1",&dt)) sf_warning("No n1 in input");
  if(!sf_histfloat(inp,"d2",&dx)) sf_warning("No n2 in input");

  ntfft = 2*kiss_fft_next_fast_size((nt+1)/2);
  nxfft = 2*kiss_fft_next_fast_size((nx+1)/2);
  scalar = 1./(ntfft*nxfft);
  iktn=ntfft/2; ikxn=nxfft/2;
  float dkt = 1.0/(ntfft*dt), fkt = 0.0,kt;
  float dkx = 1.0/(nxfft*dx), fkx = 0.0,kx;

    if (NULL == (type=sf_getstring("type"))) type="amplitude";
    /* [amplitude, semblance] thresholding type, the default is amplitude thresholding  */ 

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

  	if(!sf_getint("ifsnr",&ifsnr)) ifsnr = 0;
	/* If compute SNR during iteration */


	if(type[0]=='s')
	{

  		if(!sf_getfloat("pmin",&pmin)) pmin=-2;
        /* minimum p */		
  		if(!sf_getfloat("pmax",&pmin)) pmax=2;
        /* maximum p */			
  		if(!sf_getint("np",&np)) np=nx;
        /* number of p */
		dp=(pmax-pmin)/(np-1);		
  		sembp =sf_floatalloc(np);
  		semb  =sf_floatalloc2(nt,np);
 		taup  =sf_floatalloc2(nt,np);	
	}

	/* output files */
  	if (NULL!=sf_getstring("spec2")) 
  	{
		spec2=sf_output("spec2");
		sf_putint(spec2, "n1", ntfft);
		sf_putint(spec2, "n2", nxfft);
	}	       		
  	if (NULL!=sf_getstring("spec1")) 
  	{
		spec1=sf_output("spec1");
		sf_putint(spec1, "n1", ntfft);
		sf_putint(spec1, "n2", nxfft);
	}	  
  	if (ifsnr==1 && (NULL!=sf_getstring("true"))) 
  	{
		snr=sf_output("snr");
		trued=sf_input("true");
		tdata=sf_floatalloc2(nt,nx);
		SNR=sf_floatalloc(niter);
		sf_floatread(tdata[0],nt*nx,trued);

		sf_putint(snr,"n1",niter);
		sf_putint(snr,"d1",1);
		sf_putint(snr,"n2",1);
	}	

  /* Allocate memory */
  cdata =(kiss_fft_cpx**) sf_complexalloc2(ntfft,nxfft);
  cdatat =(kiss_fft_cpx**) sf_complexalloc2(ntfft,nxfft); /* temporary file */
  fshift= sf_floatalloc(ntfft);
  fden 	= sf_floatalloc(ntfft);
  gy 	= sf_floatalloc(nxfft);
  mask  =sf_floatalloc(nx);

  odata =sf_floatalloc2(nt,nx); 
  odatat =sf_floatalloc2(ntfft,nxfft); 
  fdata =sf_floatalloc2(ntfft,nxfft);
  memset(&odata[0][0],0,ntfft*nxfft*sizeof(float)); 

  /* Read data */
  sf_floatread(odata[0],nt*nx,inp);
  sf_floatread(mask,nx,m);

	if(type[0]=='s')
	{
   	 slant(dt,nt,nx,gy,pmin,dp,np,odata,taup,semb,sembp,&sembpmax,fshift,fden);
	}

  for (iter=niter-1; iter>=0; iter--) {
    tfft(odata, cdatat, nx, ntfft);
    xfft(cdatat, cdata, ntfft, nxfft);
    cmax = findmax(nxfft,ntfft,cdata);

    if (iter==0 || iter==niter-1) { // beginning and ending spectra
  		for (ix=0; ix<nxfft; ix++)
  		for (it=0; it<ntfft; it++)
    	fdata[ix][it] = sf_cabsf(cdata[ix][it]);

  		if (iter==0 && (NULL!=sf_getstring("spec2")))        		sf_floatwrite(fdata[0],ntfft*nxfft,spec2);
  		if (iter==niter-1 && (NULL!=sf_getstring("spec1")))   	sf_floatwrite(fdata[0],ntfft*nxfft,spec1);

    }



	if(type[0]=='a')
	{
    	for (ix=0; ix<nxfft; ix++) // Abma Kabir FT amplitude thresholding
    		for (it=0; it<ntfft; it++)
      			if (sf_cabsf(cdata[ix][it])<iter*1./niter*cmax) cdata[ix][it] = cmplx(0.,0.);
	}
	else
	{
    	for (ix=0; ix<nxfft; ix++) // Abma Kabir FT amplitude thresholding
    		for (it=0; it<ntfft; it++)
      			if (sf_cabsf(cdata[ix][it])<iter*1./niter*cmax) cdata[ix][it] = cmplx(0.,0.);


   		for (ikx=0,kx=fkx; ikx<=ikxn; ++ikx,kx+=dkx) {
    		for (ikt=0,kt=fkt; ikt<=iktn; ++ikt,kt+=dkt) {
      		if (kx==0) {
        		if (sf_cabsf(cdata[ikx][ikt])<iter*1./niter*cmax) cdata[ikx][ikt] = cmplx(0.,0.);
        		continue;
      		}

      p = -kx/kt; ip = round((p-pmin)/dp);
      //if (ip<0 || ip>=np) { cdata[ikx][ikt] = 0.;continue; }
      if (ip<0 || ip>=np) {  }
      else if (sembp[ip] <iter*1./niter*sembpmax) cdata[ikx][ikt] = cmplx(0.,0.);

      if (ikx>0 && ikx<(nxfft+1)/2) { // kx>=0, kx<0
      p = kx/kt; ip = round((p-pmin)/dp);
      //if (ip<0 || ip>=np) { cdata[nxfft-ikx][ikt] = 0.;continue; }
      if (ip<0 || ip>=np) {  }
      else if (sembp[ip] <iter*1./niter*sembpmax) cdata[nxfft-ikx][ikt] = cmplx(0.,0.);
      }

      if (ikt>0 && ikt<(ntfft+1)/2) { // kt<0, kx>=0
      p = kx/kt; ip = round((p-pmin)/dp);
      //if (ip<0 || ip>=np) { cdata[ikx][ntfft-ikt] = 0.;continue; }
      if (ip<0 || ip>=np) {  }
      else if (sembp[ip] <iter*1./niter*sembpmax) cdata[ikx][ntfft-ikt] = cmplx(0.,0.);
      }

      if (ikx>0 && ikx<(nxfft+1)/2 && ikt>0 && ikt<(ntfft+1)/2) { // kt<0, kx<0
      p = -kx/kt; ip = round((p-pmin)/dp);
      //if (ip<0 || ip>=np) { cdata[nxfft-ikx][ntfft-ikt] = 0.;continue; }
      if (ip<0 || ip>=np) {  }
      else if (sembp[ip] <iter*1./niter*sembpmax) cdata[nxfft-ikx][ntfft-ikt] =cmplx(0.,0.);
      }
    }}
	
	}

	ixfft(cdata, cdatat, ntfft, nxfft);
	itfft(cdatat, odatat, nxfft, ntfft);

    for (ix=0; ix<nx; ix++) { // put in ORIGINAL KNOWN data
      if (mask[ix]==1) continue;
      for (it=0; it<nt; it++) odata[ix][it]=odatat[ix][it];
    }

	num=0;den=0;
	/* If output the SNR file. */
  	if (ifsnr==1 && (NULL!=sf_getstring("true"))) 
  	{
		for(ix=0;ix<nx;ix++)
			for(it=0;it<nt;it++)
			{
				num+=tdata[ix][it]*tdata[ix][it];
				den+=(tdata[ix][it]-odata[ix][it])*(tdata[ix][it]-odata[ix][it]);
			}	
		SNR[niter-iter-1]=10*logf(num/den);
	}	
  }

  	if (ifsnr==1 && (NULL!=sf_getstring("true"))) 
	{ sf_floatwrite(SNR,niter,snr);	}

  	sf_floatwrite(odata[0],nt*nx,outp);
    exit (0);
}
Esempio n. 3
0
int main(int argc, char* argv[])
{
    bool verb;
    bool  adj;
    bool anis;
    sf_file Fcip=NULL;	/*   lag-domain CIPs */
    sf_file Fang=NULL;	/* angle-domain CIPs */
    sf_file Fvel=NULL;  /*   velocity @ CIPs */
    sf_file Fnor=NULL;	/*    normals @ CIPs */
    sf_file Ftlt=NULL;	/*       tilt @ CIPs */
    sf_file Fani=NULL;  /* anisotropy @ CIPs */

    sf_axis ahx,ahy,ahz,aht,ac,ath,aph,aps,aj;
    int     ihx,ihy,ihz,iht,ic,ith,iph;

    
    /* angle parameters */
    int   nth,nph,nps,nhx,nhy,nhz,nht;
    float oth,oph,ops,ohx,ohy,ohz,oht;
    float dth,dph,dps,dhx,dhy,dhz,dht;
    float phi;
    float tht;
    float psi;
    float v_s,v_r;
    float cosum,codif,sitovel;

    /*  arrays                  1   2   3   4  */
    float     ****cip;      /* nhx-nhy-nhz-nht */
    float       **ang;      /* nph-nth         */
    float        *vep;      /* nc              */
    float        *ves;      /* nc              */
    float        *eps=NULL; /* nc              */
    float        *dlt=NULL; /* nc              */

    vc3d           vv;      /* azimuth reference vector */
    vc3d          *nn;      /* normal vectors  */
    vc3d          *tt=NULL; /*   tilt vectors  */
    vc3d          *aa;      /* in-plane reference vector */
    vc3d           qq;
    vc3d           jk;      /* temp vector */
    float    hx,hy,hz;

    float tau; /* time lag */
    int   jht; /* tau axis index */
    float fht; /* tau axis weight */

    float ssn; /* slant-stack normalization */

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

    if(! sf_getbool("verb",&verb)) verb=false;	/* verbosity flag */
    if(! sf_getbool("anis",&anis)) anis=false;	/* anisotropy flag */
    if(! sf_getbool("adj", &adj))   adj=true;	/* adj flag */    
    /* 
     * ADJ: cip to ang
     * FOR: ang to cip
     */

    sf_warning("verb=%d",verb);
    sf_warning("anis=%d",anis);

    /* select anisotropy model */
    if(anis) sf_warning("ANI model");
    else     sf_warning("ISO model");

    if(adj) {
	Fcip=sf_input ( "in"); /* CIP file */
	Fang=sf_output("out"); /* ANG file */
    } else {
	Fcip=sf_output("out"); /* CIP file */
	Fang=sf_input ("in");  /* ANG file */
    }
    Fvel=sf_input ("vel");     /* velocity file  */
    Fnor=sf_input ("nor");     /* normal vectors */
    if(anis) {
	Ftlt=sf_input ("tlt"); /*   tilt vectors */
	Fani=sf_input ("ani"); /*     anisotropy */
    }

    aj  = sf_maxa(1,0,1);

    if(adj) {
	/* input axes */
	ahx = sf_iaxa(Fcip,1); sf_setlabel(ahx,"hx");
	ahy = sf_iaxa(Fcip,2); sf_setlabel(ahy,"hy");
	ahz = sf_iaxa(Fcip,3); sf_setlabel(ahz,"hz");
	aht = sf_iaxa(Fcip,4); sf_setlabel(aht,"ht");

	/* CIP axis */
	ac  = sf_iaxa(Fcip,5); sf_setlabel(ac ,"c ");
		
	/* reflection angle */
	if(! sf_getint  ("nth",&nth)) nth=90;
	if(! sf_getfloat("oth",&oth)) oth=0;
	if(! sf_getfloat("dth",&dth)) dth=1.;
	ath = sf_maxa(nth,oth,dth);
	sf_setlabel(ath,"th");
	sf_setunit (ath,"deg");
	
	/* azimuth angle */
	if(! sf_getint  ("nph",&nph)) nph=360;
	if(! sf_getfloat("oph",&oph)) oph=-180;
	if(! sf_getfloat("dph",&dph)) dph=1.;
	aph = sf_maxa(nph,oph,dph);
	sf_setlabel(aph,"ph");
	sf_setunit (aph,"deg");

	/* output axes */
	sf_oaxa(Fang,ath,1);
	sf_oaxa(Fang,aph,2);
	sf_oaxa(Fang,ac ,3);
	sf_oaxa(Fang,aj ,4);
	sf_oaxa(Fang,aj ,5);

    } else {

	/* lag in x */
	if(! sf_getint  ("nhx",&nhx)) nhx=1;
	if(! sf_getfloat("ohx",&ohx)) ohx=0;
	if(! sf_getfloat("dhx",&dhx)) dhx=1.;
	ahx = sf_maxa(nhx,ohx,dhx);
	sf_setlabel(ahx,"hx");
	sf_setunit (ahx,"");

	/* lag in y */
	if(! sf_getint  ("nhy",&nhy)) nhy=1;
	if(! sf_getfloat("ohy",&ohy)) ohy=0;
	if(! sf_getfloat("dhy",&dhy)) dhy=1.;
	ahy = sf_maxa(nhy,ohy,dhy);
	sf_setlabel(ahy,"hy");
	sf_setunit (ahy,"");

	/* lag in z */
	nhz=1;
	ohz=0.;
	dhz=1.;
	ahz = sf_maxa(nhz,ohz,dhz);
	sf_setlabel(ahz,"hz");
	sf_setunit (ahz,"");

	/* lag in t */
	if(! sf_getint  ("nht",&nht)) nht=1;
	if(! sf_getfloat("oht",&oht)) oht=0.;
	if(! sf_getfloat("dht",&dht)) dht=1.;
	aht = sf_maxa(nht,oht,dht);
	sf_setlabel(aht,"ht");
	sf_setunit (aht,"");

	/* reflection angle */
	ath = sf_iaxa(Fang,1); sf_setlabel(ath,"th");
	/* azimuth angle */
	aph = sf_iaxa(Fang,2); sf_setlabel(aph,"ph");
	/* CIP axis */
	ac  = sf_iaxa(Fang,3); sf_setlabel(ac ,"c ");

	/* output axes */
	sf_oaxa(Fcip,ahx,1);
	sf_oaxa(Fcip,ahy,2);
	sf_oaxa(Fcip,ahz,3);
	sf_oaxa(Fcip,aht,4);
	sf_oaxa(Fcip,ac ,5);
    }

    if (verb){
	sf_raxa(ahx);
	sf_raxa(ahy);
	sf_raxa(ahz);
	sf_raxa(aht);
	sf_raxa(ac);
	sf_raxa(ath);
	sf_raxa(aph);
    }

    if(anis) {
	/* deviation angle */
	if(! sf_getint  ("nps",&nps)) nps=251;
	if(! sf_getfloat("ops",&ops)) ops=-25;
	if(! sf_getfloat("dps",&dps)) dps=0.2;
	aps = sf_maxa(nps,ops,dps);
	sf_setlabel(aps,"ps");
	sf_setunit (aps,"deg");

	if(verb) sf_raxa(aps);
    } else {
	aps = NULL;
    }

    /*------------------------------------------------------------*/
    /* allocate arrays */
    cip = sf_floatalloc4  (sf_n(ahx),sf_n(ahy),sf_n(ahz),sf_n(aht));
    ang = sf_floatalloc2  (sf_n(ath),sf_n(aph));

    /* read velocity */
    vep = sf_floatalloc  (sf_n(ac));    
    sf_floatread(vep,sf_n(ac),Fvel);

    ves = sf_floatalloc  (sf_n(ac));    
    sf_floatread(ves,sf_n(ac),Fvel);
	
    /*------------------------------------------------------------*/
    /* read normals */
    nn  = (vc3d*) sf_alloc(sf_n(ac),sizeof(*nn)); /* normals  */
    vc3dread1(Fnor,nn,sf_n(ac));

    if(anis) {
	/* read anisotropy */
	eps = sf_floatalloc   (sf_n(ac));
	sf_floatread(eps,sf_n(ac),Fani);

	dlt = sf_floatalloc   (sf_n(ac));
	sf_floatread(dlt,sf_n(ac),Fani);

	/* read tilts */	
	tt  = (vc3d*) sf_alloc(sf_n(ac),sizeof(*tt));
	vc3dread1(Ftlt,tt,sf_n(ac));
    }

    /*------------------------------------------------------------*/
    /* in-plane azimuth reference */
    vv.dx=1;
    vv.dy=0;
    vv.dz=0;

    aa  = (vc3d*) sf_alloc(sf_n(ac),sizeof(*aa));
    for(ic=0;ic<sf_n(ac);ic++) {
	jk    =vcp3d(&nn[ic],&vv);
	aa[ic]=vcp3d(&jk,&nn[ic]);
    }

    /*------------------------------------------------------------*/
    ssn = 1./sqrt(sf_n(ahx)*sf_n(ahy)*sf_n(ahz));

    /*------------------------------------------------------------*/
    /* loop over CIPs */
/*    if(verb) fprintf(stderr,"ic\n");*/
    for(ic=0;ic<sf_n(ac);ic++) {
/*	if(verb) fprintf(stderr,"\b\b\b\b\b%d",ic);*/

	if(adj) {

	    /* read CIP */
	    sf_floatread(cip[0][0][0],sf_n(ahx)*sf_n(ahy)*sf_n(ahz)*sf_n(aht),Fcip);
	    
	    /* init ANG */
	    for    (iph=0;iph<sf_n(aph);iph++) {
		for(ith=0;ith<sf_n(ath);ith++) {
		    ang[iph][ith]=0;
		}
	    }
	} else {
	    
	    /* init CIP */
	    for            (iht=0;iht<sf_n(aht);iht++) {
		for        (ihz=0;ihz<sf_n(ahz);ihz++) {
		    for    (ihy=0;ihy<sf_n(ahy);ihy++) {
			for(ihx=0;ihx<sf_n(ahx);ihx++) {
			    cip[iht][ihz][ihy][ihx]=0;
			}
		    }
		}
	    }

	    /* read ANG */
	    sf_floatread(ang[0],sf_n(ath)*sf_n(aph),Fang);
	}

	/* phi loop */

	nph = sf_n(aph);

#ifdef _OPENMP
#pragma omp parallel for schedule(static)				\
    private(iph,phi,jk,qq,						\
	    ith,tht,							\
	    ihy,ihx,hy,hx,hz,						\
	    tau,jht,fht,cosum,codif,v_s,v_r,psi,sitovel)	\
    shared( nph,aph,ath,aps,ahy,ahx,aht,cip,ang,vep,ves,eps,dlt)
#endif
	for(iph=0;iph<nph;iph++) {
	    phi=(180+sf_o(aph)+iph*sf_d(aph))/180.*SF_PI;
	    /* use '180' to reverse illumination direction: */
	    /* at a CIP, look toward the source */

	    /* reflection azimuth vector */
	    jk = rot3d(nn,aa,phi);
	    qq = nor3d(&jk);
	    
	    /* theta loop */
	    for(ith=0;ith<sf_n(ath);ith++) {
		tht=(sf_o(ath)+ith*sf_d(ath))/180.*SF_PI;
		
		if(anis) {

		    ttipar = psitti(nn,&qq,tt,aa,
				    tht,phi,aps,
				    vep[ic],ves[ic],eps[ic],dlt[ic]);
		    psi = ttipar[0];
		    v_s = ttipar[1];
		    v_r = ttipar[2];

		    psi *= SF_PI/180.;
                    cosum = cosf(tht+psi);
                    codif = cosf(tht-psi);

                    sitovel = sinf(2*tht)/(v_s*cosum + v_r*codif);
		} else {
		    sitovel = sinf(tht)/vep[ic];
		}

		/* lag loops */
		if(adj) {
		    for    (ihy=0;ihy<sf_n(ahy);ihy++) { hy=sf_o(ahy)+ihy*sf_d(ahy);
			for(ihx=0;ihx<sf_n(ahx);ihx++) { hx=sf_o(ahx)+ihx*sf_d(ahx);
 
			    hz = -(hx*(nn[ic].dx)+hy*(nn[ic].dy))/(nn[ic].dz);
			    tau = -((qq.dx)*hx+(qq.dy)*hy+(qq.dz)*hz)*sitovel;			    
			    jht=0.5+(tau-sf_o(aht))/sf_d(aht);

			    if(jht>=0 && jht<sf_n(aht)-1) {
				fht= (tau-sf_o(aht))/sf_d(aht)-jht;
				ang[iph][ith] += (1-fht)*ssn*cip[jht  ][0][ihy][ihx]
				    +               fht *ssn*cip[jht+1][0][ihy][ihx]; 
			    }
			    
			} /* hx */
		    } /* hy */
		} else {
		    for    (ihy=0;ihy<sf_n(ahy);ihy++) { hy=sf_o(ahy)+ihy*sf_d(ahy);
			for(ihx=0;ihx<sf_n(ahx);ihx++) { hx=sf_o(ahx)+ihx*sf_d(ahx);
		    
			    hz = -(hx*(nn[ic].dx)+hy*(nn[ic].dx))/(nn[ic].dz);
			    tau = -((qq.dx)*hx+(qq.dy)*hy+(qq.dz)*hz)*sitovel; 		    
			    jht=0.5+(tau-sf_o(aht))/sf_d(aht);

			    if(jht>=0 && jht<sf_n(aht)-1) {
				fht= (tau-sf_o(aht))/sf_d(aht)-jht;
				cip[jht  ][0][ihy][ihx] += (1-fht)*ssn*ang[iph][ith];
				cip[jht+1][0][ihy][ihx] +=    fht *ssn*ang[iph][ith];
			    }
			    
			} /* hx */
		    } /* hy */
		}

	    } /* th */
	} /* ph */
	
	if(adj) {
	    /* write ANG */
	    sf_floatwrite(ang[0],sf_n(ath)*sf_n(aph),Fang);
	} else {
	    /* write CIP */
	    sf_floatwrite(cip[0][0][0],sf_n(ahx)*sf_n(ahy)*sf_n(ahz)*sf_n(aht),Fcip);
	}

    }
    if(verb) fprintf(stderr,"\n");
    /*------------------------------------------------------------*/
 
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr,"free memory...");
    free(***cip);free(**cip);free(*cip);free(cip);
    ;                        free(*ang);free(ang);
    ;                                   free(vep);
    ;                                   free (nn);
    ;                                   free (aa);
    if(anis) {
	free(ves);
	free(eps);
	free(dlt);
	free(tt);
    }
    if(verb) fprintf(stderr,"OK\n");
    /*------------------------------------------------------------*/

    exit(0);
}		
Esempio n. 4
0
int main(int argc, char* argv[])
{
 
    bool hermite_false, hermite_true;
    int n1, n2, npml, pad1, pad2, ns, nw;
    float d1, d2, **v, ds, os, dw, ow;
    sf_complex ****f,  ****obs; 
    sf_file in, out, misfit, source, receiver, record;
    char *order;
    int uts, mts, is, i, j, iw, iter, niter;
    float **recloc;
    float **m_old, **m_new;
    float **d_new, **d_old;
    float **g_old, **g_new; 
    sf_complex ****r_new, ****r_old;
    sf_complex ****Fg; 
    float alpha, beta, gnorm, rnorm; 
    float *datamisfit;

    sf_init(argc, argv);

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

    if (!sf_getint("uts",&uts)) uts=0;
//#ifdef _OPENMP
//    mts = omp_get_max_threads();
//#else
    mts = 1;
//#endif
    uts = (uts < 1)? mts: uts;

    hermite_false=false;
    hermite_true=true;
    /* Hermite operator */
    
    if (!sf_getint("npml",&npml)) npml=20;
    /* PML width */
    
    if (!sf_getint("niter",&niter)) niter=0; 
    /* Number of iterations */

    if (NULL == (order = sf_getstring("order"))) order="c";
    /* discretization scheme (default optimal 9-point) */

    fdprep_order(order);

    /* read input dimension */
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input.");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input.");

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

    v = sf_floatalloc2(n1,n2);
    sf_floatread(v[0],n1*n2,in);
    
	/* PML padding */
	pad1 = n1+2*npml;
	pad2 = n2+2*npml;

    /* read receiver */ 
    if (NULL == sf_getstring("receiver")) sf_error("Need receiver="); 
    receiver = sf_input("receiver"); 
    recloc=sf_floatalloc2(n1,n2);
    sf_floatread(recloc[0],n1*n2,receiver);

    /* read source */
    if (NULL == sf_getstring("source")) sf_error("Need source=");
    source = sf_input("source");

    if (!sf_histint(source,"n3",&ns)) sf_error("No ns=.");
    if (!sf_histfloat(source,"d3",&ds)) ds=d2;
    if (!sf_histfloat(source,"o3",&os)) os=0.;

    /* read observed data */
    if (NULL == sf_getstring("record")) sf_error("Need record=");
    record = sf_input("record");

    if (!sf_histint(record,"n4",&nw)) sf_error("No nw=.");
    if (!sf_histfloat(record,"d4",&dw)) sf_error("No dw=.");
    if (!sf_histfloat(record,"o4",&ow)) sf_error("No ow=."); 

    f = sf_complexalloc4(n1,n2,ns,nw);
    obs = sf_complexalloc4(n1,n2,ns,nw);

    sf_complexread(f[0][0][0],n1*n2*ns*nw,source);
    sf_complexread(obs[0][0][0],n1*n2*ns*nw,record);
   
    /* allocate variables */
    m_old = sf_floatalloc2(n1,n2);
    m_new = sf_floatalloc2(n1,n2);
    d_old = sf_floatalloc2(n1,n2);
    d_new = sf_floatalloc2(n1,n2);
    g_old = sf_floatalloc2(n1,n2);
    g_new = sf_floatalloc2(n1,n2);
    
    r_old = sf_complexalloc4(n1,n2,ns,nw);
    r_new = sf_complexalloc4(n1,n2,ns,nw);
    Fg = sf_complexalloc4(n1,n2,ns,nw);

    /* set output dimension */
    sf_putint(out,"n1",n1);
    sf_putint(out,"n2",n2);
    sf_putint(out,"n3",niter);
    
    sf_putint(misfit,"n1",niter);
    sf_putfloat(misfit,"d1",1);
    sf_putint(misfit,"n2",1);
    datamisfit = sf_floatalloc(niter);

    rnorm = 0.0; 
    for ( iw = 0; iw < nw; iw ++ ) { 
    for ( is = 0; is < ns; is ++) { 
    for ( j = 0; j < n2 ; j++ ) { 
    for ( i = 0; i < n1; i++ ) { 
        r_old[iw][is][j][i] = obs[iw][is][j][i];
        if ( recloc[j][i] > 0.0 ) { 
            rnorm += cabsf( r_old[iw][is][j][i] * r_old[iw][is][j][i] ); 
        }
    }
    }
    }
    }
    sf_warning("rnorm = %g.",rnorm);


    sf_warning("Adjoint calculation for the first iteration.");
    /* adjoint calculation */
    adjlsm_operator(nw, ow, dw, ns, n1, n2, 
                     uts, pad1, pad2, npml, d1, d2,
                     hermite_false, hermite_true, v, f, recloc, 
                     r_old, g_old);

    /* set starting valuables */
    for (j = 0; j < n2; j++) { 
    for (i = 0; i < n2; i++ ) { 
        d_old[j][i] = g_old[j][i];
        m_old[j][i] = 0.0;
    }
    }
    
    for ( iter = 0; iter < niter; iter ++ ) { 

        sf_warning("Calculating iteration %d out of %d.",iter,niter);

        /* born forward operator */
        bornsyn_operator(nw, ow, dw, ns, n1, n2, 
                         uts, pad1, pad2, npml, d1, d2,
                         hermite_false, v, f, recloc, 
                         d_old, Fg); 

        /* calculate alpha value */
        alpha = calc_alpha(g_old, Fg, recloc, n1, n2, ns, nw);
        
        /* update model */ 
        update_model_lsm(m_old, m_new, d_old, alpha, n1, n2); 

        /* update residual */ 
        update_residual(r_old, r_new, Fg, recloc, alpha, n1, n2, ns, nw);  

        /* adjoint operator */ 
        adjlsm_operator(nw, ow, dw, ns, n1, n2, 
                         uts, pad1, pad2, npml, d1, d2,
                         hermite_false, hermite_true, v, f, recloc, 
                         r_new, g_new);

        /* update direction */
        beta = direction_cg_fletcher(g_old, d_old, g_new, d_new, n1, n2); 

        sf_warning("alpha = %g, beta = %g.",alpha, beta);

        /* update vectors */
        gnorm = 0.0 ;  
        for (j = 0; j < n2; j++ ) { 
        for (i = 0; i < n1; i++ ) { 
            d_old[j][i] = d_new[j][i]; 
            g_old[j][i] = g_new[j][i];
            m_old[j][i] = m_new[j][i]; 
            gnorm += g_old[j][i] * g_old[j][i]; 
        }
        }

        rnorm = 0.0 ; 
        for (iw = 0; iw < nw; iw ++ ) { 
        for (is = 0; is < ns; is ++ ) { 
        for (j = 0; j < n2; j ++ ) { 
        for (i = 0; i < n1; i ++ ) { 
            r_old[iw][is][j][i] = r_new[iw][is][j][i]; 
            if ( recloc[j][i] > 0.0 ) { 
                rnorm += cabsf( r_old[iw][is][j][i] * r_old[iw][is][j][i] ); 
            }
        }
        }
        }
        }
        sf_warning("gnorm = %g; rnorm = %g.",gnorm, rnorm);

        datamisfit[iter] = rnorm; 

        sf_floatwrite(m_old[0],n1*n2,out);

    }  /* end iteration */

    sf_floatwrite(datamisfit, niter, misfit); 

    exit(0);
}
Esempio n. 5
0
int main(int argc, char* argv[])
{
	int   i;
        float *data;
        char *fn;

        sf_init(argc,argv);

        FILE *Fi;
        sf_file Fo;
        
        cjbsegy *tr;
 
        tr = calloc(sizeof(cjbsegy), 1);

        int nx, ny, nazim, nang, ntau;
        float dazim, dang, dtau, fazim=0.0, fang=0.0, ftau=0;
        int dx, dy, fx=0, fy=0;

        if (!sf_getint("nx",&nx)) nx=101;
        if (!sf_getint("ny",&ny)) ny=101;
        if (!sf_getint("nazim",&nazim)) nazim=8;
        if (!sf_getint("nang",&nang)) nang=21;
        if (!sf_getint("ntau",&ntau)) ntau=101;
        if (!sf_getint("dx",&dx)) dx=1;
        if (!sf_getint("dy",&dy)) dy=1;
        if (!sf_getfloat("dazim",&dazim)) dazim=22.5;
        if (!sf_getfloat("dang",&dang)) dang=2.0;
        if (!sf_getfloat("dtau",&dtau)) dtau=0.002;
        if (!sf_getint("fx",&fx)) fx=1;
        if (!sf_getint("fy",&fy)) fy=1;
        if (!sf_getfloat("ftau",&ftau)) ftau=0;
        if (NULL==(fn=sf_getstring("fn"))) fn="kpstm.ladcig.su.agc";

        /* setup I/O files */
        Fo = sf_output("out");

        if((Fi=fopen(fn,"rb"))==NULL)
        {
           printf("File %s open error!\n",fn);
           exit(0);
        }

        fread(tr,sizeof(cjbsegy),1,Fi);
        int iline0=tr->ep;
        sf_warning("ns=%d dt=%f iLineNo=%d ",tr->ns, tr->dt,iline0);


        if(fseek(Fi, 0L, 2) ==-1)
          printf("input file size unknown; Please specify n2\n");
        int nxy=(int) (ftell(Fi)/((60+ntau)*sizeof(float)));
       
        sf_warning("nxy=%d nx=%d ny=%d ",nxy, nx,ny);
        sf_warning("nazim=%d nang=%d ntau=%d",nazim,nang,ntau);
        sf_warning("dx=%d dy=%d dazim=%f dang=%f dtau=%f",dx,dy,dazim,dang,dtau);
        sf_warning("fx=%d fy=%d fazim=%f fang=%f ftau=%f",fx,fy,fazim,fang,ftau);

        if(nxy!=nx*ny*nazim*nang) {
          sf_warning("nx * ny * nazim * nang != nxy ");
          exit(0);  
         };

        sf_putint(Fo,"n1",ntau);
        sf_putint(Fo,"n2",nang);
        sf_putint(Fo,"n3",nazim);
        sf_putint(Fo,"n4",nx);
        sf_putint(Fo,"n5",ny);
        sf_putfloat(Fo,"d1",dtau);
        sf_putfloat(Fo,"o1",ftau);
        sf_putfloat(Fo,"d2",dang);
        sf_putfloat(Fo,"o2",fang);
        sf_putfloat(Fo,"d3",dazim);
        sf_putfloat(Fo,"o3",fazim);
        sf_putfloat(Fo,"d4",dx);
        sf_putfloat(Fo,"o4",fx);
        sf_putfloat(Fo,"d5",dy);
        sf_putfloat(Fo,"o5",fy);
        sf_putstring(Fo,"label1","z");
        sf_putstring(Fo,"label2","angle");
        sf_putstring(Fo,"label3","azimuth");
        sf_putstring(Fo,"label4","x");
        sf_putstring(Fo,"label5","y");
        sf_putstring(Fo,"unit1","ms");
        sf_putstring(Fo,"unit2","degree");
        sf_putstring(Fo,"unit3","degree");
        sf_putstring(Fo,"unit4","m");
        sf_putstring(Fo,"unit5","m");

        data = sf_floatalloc(ntau);

        rewind(Fi);
        for(i=0;;i++)
        {
          fread(tr,sizeof(cjbsegy),1,Fi);
          if(tr->ep != iline0){
            sf_warning("Read iLineNo=%d finished",iline0);
            iline0=tr->ep;
          }
          fread(data,sizeof(float),ntau,Fi);
          if(feof(Fi))break;

          sf_floatwrite(data, ntau, Fo);
        }
        sf_warning("Read iLineNo=%d finished",tr->ep);

        fclose(Fi);
        free(data);
        free(tr);
        exit(0);
}
Esempio n. 6
0
int main(int argc, char* argv[])
{
  int verbose;
  sf_file in=NULL, out=NULL;
  int n1_traces;
  int n1_headers;

  char* header_format=NULL;
  sf_datatype typehead;
  /* kls do I need to add this?  sf_datatype typein; */
  float* fheader=NULL;
  float* intrace=NULL;
  float* fprevheader=NULL;
  int numkeys;
  int ikey;
  char** list_of_keys;
  int *indx_of_keys;
  char* skey;
  int indx_of_skey;
  int skeyvalue;
  bool pkeychanged;
  int itrace=0;

  /*****************************/
  /* initialize verbose switch */
  /*****************************/
  sf_init (argc,argv);

  /* verbose flag controls ammount of print */
  /*( verbose=1 0 terse, 1 informative, 2 chatty, 3 debug ) */
  /* fprintf(stderr,"read verbose switch.  getint reads command line.\n"); */
  if(!sf_getint("verbose",&verbose))verbose=1;
    /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  sf_warning("verbose=%d",verbose);
 
  /******************************************/
  /* input and output data are stdin/stdout */
  /******************************************/

  if(verbose>0)fprintf(stderr,"read in file name\n");  
  in = sf_input ("in");

  if(verbose>0)fprintf(stderr,"read out file name\n");
  out = sf_output ("out");

  if (!sf_histint(in,"n1_traces",&n1_traces))
    sf_error("input data not define n1_traces");
  if (!sf_histint(in,"n1_headers",&n1_headers)) 
    sf_error("input data does not define n1_headers");

  header_format=sf_histstring(in,"header_format");
  if(strcmp (header_format,"native_int")==0) typehead=SF_INT;
  else                                       typehead=SF_FLOAT;

  if(verbose>0)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  fheader = sf_floatalloc(n1_headers);
  fprevheader = sf_floatalloc(n1_headers);
 
  if(verbose>0)fprintf(stderr,"allocate intrace.  n1_traces=%d\n",n1_traces);
  intrace= sf_floatalloc(n1_traces);

  if(verbose>0)fprintf(stderr,"call list of keys\n");
 
  /* this sf_getstring will create parameter descrpiton in the self doc */
  sf_getstring("pkey"); 
  /* \n
     A comma seperated list of primary header keys to monitor to determine 
     gathers.  The trace number in the gather is counted and put in the
     skey header location.
     \n
  */
  list_of_keys=sf_getnstring("pkey",&numkeys);
  /* List of the primary keys monitored to determine gathers. */

  if(list_of_keys==NULL)
    sf_error("The required parameter \"pkey\" was not found.");
  /* I wanted to use sf_getstrings, but it seems to want a colon seperated
     list of keys (eg key=offset:ep:fldr:cdp) and I wanted a comma seperated
     list of keys (eg key=offset:ep:fldr:cdp).
  numkeys=sf_getnumpars("pkey");
  if(numkeys==0)
    sf_error("The required parameter \"pkey\" was not found.");
  fprintf(stderr,"alloc list_of_keys numkeys=%d\n",numkeys);
  list_of_keys=(char**)sf_alloc(numkeys,sizeof(char*)); 
  sf_getstrings("pkey",list_of_keys,numkeys);
  */
  /* print the list of keys */
  if(verbose>1){
    fprintf(stderr,"numkeys=%d\n",numkeys);
    for(ikey=0; ikey<numkeys; ikey++){
      fprintf(stderr,"list_of_keys[%d]=%s\n",ikey,list_of_keys[ikey]);
    }
  }
  if(NULL==(skey=sf_getstring("skey")))
    sf_error("the required parameter \"skey\" was not found");
  /* The name of the secondary key created by the program. */
  
  /* maybe I should add some validation that n1== n1_traces+n1_headers+2
     and the record length read in the second word is consistent with 
     n1.  */

  /**********************************************************/
  /* end code block for standard tah Trace And Header setup */
  /* continue with any sf_puthist this tah program calls to */
  /* add to the history file                                */
  /**********************************************************/

  /* put the history from the input file to the output */
  sf_fileflush(out,in);

  /********************************************************/
  /* continue initialization specific to this tah program */
  /********************************************************/

  /* segy_init gets the list header keys required by segykey function  */
  segy_init(n1_headers,in);
  indx_of_keys=sf_intalloc(numkeys);
  for (ikey=0; ikey<numkeys; ikey++){
    /* kls need to check each of these key names are in the segy header and
       make error message for invalid keys.  Of does segykey do this? NO, just
       segmentation fault. */
    indx_of_keys[ikey]=segykey(list_of_keys[ikey]);
  }
  indx_of_skey=segykey(skey);

  /***************************/
  /* start trace loop        */
  /***************************/
  if(verbose>0)fprintf(stderr,"start trace loop\n");
  skeyvalue=0;
 
  itrace=0;
  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. */
    /********************/
    /* this program computes a secondary key.  If one of the headers in 
       pkey changes, skey is set to 1.  Otherwise skey is the previous skey+1
    */
    pkeychanged=false;
    if(itrace>0){
      for(ikey=0; ikey<numkeys; ikey++){
	if(typehead == SF_INT){
	  if(((int*)fheader    )[indx_of_keys[ikey]]!=
	     ((int*)fprevheader)[indx_of_keys[ikey]]){
	    pkeychanged=true;
	    break;
	  }
	} else {
	  if(fheader[indx_of_keys[ikey]]!=fprevheader[indx_of_keys[ikey]]){
	    pkeychanged=true;
	    break;
	  }
	}
      }
    }
    if(pkeychanged) skeyvalue=1;
    else skeyvalue++;
    if(typehead == SF_INT) ((int*)fheader)[indx_of_skey]=skeyvalue;
    else                          fheader [indx_of_skey]=skeyvalue;
    
    if(skeyvalue==1){
      /* this is a new pkey, save the header so you know when it changes */
      memcpy(fprevheader,fheader,n1_headers*sizeof(int));
    }
    
    /***************************/
    /* write trace and headers */
    /***************************/
    put_tah(intrace, fheader, n1_traces, n1_headers, out);
    itrace++;
  }

  exit(0);
}
Esempio n. 7
0
File: Mmig2.c Progetto: Seislet/src
int main(int argc, char* argv[])
{
    bool adj, half, verb, normalize;
    int nt, nx, nh, nh2, ix, ih, iy, i, nn, it, **fold, apt;
    float *trace, **image, **v, rho, **stack, *pp, *off;
    float h, x, t, h0, dh, dx, ti, tx, t0, t1, t2, dt, vi, aal, angle;
    sf_file inp, out, vel, gather, offset;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    sf_halfint_init (true, nn, rho);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    exit(0);
}
Esempio n. 8
0
int main (int argc, char ** argv)
{
   /* RSF variables */
   bool verb;
   float *dat=NULL, **datf=NULL, **image=NULL, **tr=NULL, **ts=NULL, **trs=NULL;
   float **px=NULL, **pz=NULL;
   sf_file Fin=NULL, Fout=NULL, Ftt=NULL;
   sf_axis a1,a2,a1t,a2t,a3t,ax,az;
   int     nf,ntaper,i1,i2,i2t,itt;
   float   fmax,df,theta,dtheta,tg,tgtap,tmin,xs,xr,eps;
   aalias  aa;
   fslice  tabtt=NULL;
   int     n1,n2,n1t,n2t,n3t,nx,nz;
   float   o1,d1,o2,d2,o1t,d1t,o2t,d2t,o3t,d3t,ox,oz,dx,dz;

   sf_init(argc,argv);

   Fin  = sf_input  ("in" );
   Fout = sf_output ("out" );
   Ftt  = sf_input  ("ttfile" );

   /* axes */
   a1  = sf_iaxa(Fin,1);   /* data */
   a2  = sf_iaxa(Fin,2);   /* data */
   a1t = sf_iaxa(Ftt,1);   /* traveltime */
   a2t = sf_iaxa(Ftt,2);   /* traveltime */
   a3t = sf_iaxa(Ftt,3);   /* traveltime */

   o1  = sf_o(a1);  n1   = sf_n(a1);  d1  = sf_d(a1);
   o2  = sf_o(a2);  n2   = sf_n(a2);  d2  = sf_d(a2);
   o1t = sf_o(a1t); n1t  = sf_n(a1t); d1t = sf_d(a1t);
   o2t = sf_o(a2t); n2t  = sf_n(a2t); d2t = sf_d(a2t);
   o3t = sf_o(a3t); n3t  = sf_n(a3t); d3t = sf_d(a3t);

   /* migration parameters */
   if(! sf_getbool("verb",&verb))     verb = false; /* verbosity flag */
   if(! sf_getfloat("theta",&theta))  theta = 30.;  /* maximum dip */
   if(! sf_getfloat("dtheta",&dtheta))  dtheta = theta/3;  /* taper zone */
   if(dtheta>theta) dtheta=theta;
   if(! sf_getfloat("df",&df))        df = 5.;    /* anti-aliasing sampling */
   if(!sf_getfloat("fmax",&fmax)) fmax=.5/d1;
   if(fmax>(.5/d1)) fmax=.5/d1;
   if (!sf_getint("ntaper",&ntaper)) ntaper=11;
   if(!sf_getfloat("tmin",&tmin)) tmin=3*d1;
   if(!sf_getfloat("xs",&xs)) sf_error("missing xs parameter\n");

   /* image parameters */
   if (!sf_getint("nx",&nx))   nx=n2t;
   if(!sf_getfloat("ox",&ox))  ox=o2t;
   if(!sf_getfloat("dx",&dx))  dx=d2t;
   if (!sf_getint("nz",&nz))   nz=n1t;
   if(!sf_getfloat("oz",&oz))  oz=o1t;
   if(!sf_getfloat("dz",&dz))  dz=d1t;

   /* checking dimensions */
   if((dx!=d2t)||(dz!=d1t)) 
     sf_error("sampling interval have to be the same in:\n"
	      " image and traveltime file\n");
   if(ox<o2t) ox=o2t; 
   if(oz<o1t) oz=o1t;
   if((ox+(nx-1)*dx)>(o2t+(n2t-1)*d2t)) nx=floor(((o2t+(n2t-1)*d2t)-ox)/dx)+1;
   if((oz+(nz-1)*dz)>(o1t+(n1t-1)*d1t)) nz=floor(((o1t+(n1t-1)*d1t)-oz)/dz)+1;

   /* output axis */
   ax = sf_maxa(nx,ox,dx); if(verb) sf_raxa(ax);
   az = sf_maxa(nz,oz,dz); if(verb) sf_raxa(az);
   sf_oaxa(Fout,az,1);
   sf_oaxa(Fout,ax,2);

   /* anti-aliasing */
   /* df = fmax; */
   nf = initAalias(-1,verb,fmax,df,n1,d1,&aa);
   /* fprintf(stderr,"forcing nf=%d df=%f\n",nf,df); */

   /* aperture angle */
   tg    = tan(SF_PI*theta/180);
   tgtap = tan(SF_PI*(theta-dtheta)/180);
   if(verb) sf_warning("tgmax=%f tgtap=%f",tg,tgtap);

   /* allocating */
   dat   = sf_floatalloc(n1);
   image = sf_floatalloc2(nz,nx);
   datf  = sf_floatalloc2 (n1,nf);
   ts    = sf_floatalloc2(n1t,n2t);
   tr    = sf_floatalloc2(n1t,n2t);
   trs   = sf_floatalloc2(n1t,n2t);
   px    = sf_floatalloc2(n1t,n2t);
   pz    = sf_floatalloc2(n1t,n2t);

   if(verb) sf_warning("initializing traveltime loading");
   /* initializing traveltime maps */
   tabtt = fslice_init(n1t*n2t,n3t,sizeof(float));
   fslice_load(Ftt,tabtt,SF_FLOAT);
   if(verb) sf_warning("traveltime loading has finished");

   /* reading the source-slice from traveltime table */
   eps = .01*d2;
   itt = floor((xs+eps-o3t)/d3t);
   fslice_get(tabtt,itt,ts[0]);
   if(verb) sf_warning("traveltime table from source was read");

   for(i2=0;i2<n2;i2++) {

     sf_floatread(dat,n1,Fin);
     xr = o2+i2*d2;

     if((xr>=o3t)&&(xr<(o3t+n3t*d3t))) {

       for (i1=n1-ntaper;i1<n1;i1++) dat[i1]=(n1-i1-1)*dat[i1]/ntaper;

       loadBank(aa,dat,datf);

       /* reading the receiver-slice of traveltime table */
       /* itt = floor((o2-o2t)/d2)+i2; */
       itt = floor((xr+eps-o3t)/d3t);
       fslice_get(tabtt,itt,tr[0]);

       for(i2t=0;i2t<n2t;i2t++)
         for(i1=0;i1<n1t;i1++)
            trs[i2t][i1]=ts[i2t][i1]+tr[i2t][i1];

       derive_1(n1t,n2t,d1t,trs,pz);

       derive_2(n1t,n2t,d2t,trs,px);

       spreadSR(nf,fmax,df,tg,tgtap,
                n1,o1,d1, nx,ox,dx, nz,oz,dz, o1t,o2t, 
                px,pz,tr,ts,datf,image);

       if(verb) fprintf(stderr,"+");

     }else{ if(verb) fprintf(stderr,".");}

   }

   sf_floatwrite(image[0],n1*n2,Fout);
   fprintf( stderr," \n finished processing \n");

 
   exit(0);
}
Esempio n. 9
0
int main(int argc, char* argv[])
{
    bool sym;
    float o1,d1, o2,d2, tol;
    int nd, n1, n2, n12, niter, rect1, rect2, nw;
    float **xy, *z, *m, *m2, *d;
    sf_file in, out, coord, pattern;

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

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

    if (NULL != sf_getstring("pattern")) {
	/* pattern file for output dimensions */
	pattern = sf_input("pattern");
	
	if (!sf_histint(pattern,"n1",&n1)) sf_error("No n1= in pattern");
	if (!sf_histint(pattern,"n2",&n2)) sf_error("No n2= in pattern");
	if (!sf_histfloat(pattern,"d1",&d1)) d1=1.;
	if (!sf_histfloat(pattern,"d2",&d2)) d2=1.;
	if (!sf_histfloat(pattern,"o1",&o1)) o1=0.;
	if (!sf_histfloat(pattern,"o2",&o2)) o2=0.;
	
	sf_fileclose(pattern);
    } else {
	if (!sf_getint("n1",&n1)) sf_error("Need n1=");
	if (!sf_getint("n2",&n2)) sf_error("Need n2=");
	if (!sf_getfloat("d1",&d1)) d1=1.;
	if (!sf_getfloat("d2",&d2)) d2=1.;
	if (!sf_getfloat("o1",&o1)) o1=0.;
	if (!sf_getfloat("o2",&o2)) o2=0.;
    }

    n12 = n1*n2;

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

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

    xy = sf_floatalloc2(2,nd);
    sf_floatread(xy[0],nd*2,coord);

    if (!sf_getint("rect1",&rect1)) rect1=1;
    if (!sf_getint("rect2",&rect2)) rect2=1;
    /* smoothing regularization */
    
    if (!sf_getint("nw",&nw)) nw=2;
    /* interpolator size */
    
    if (!sf_getbool("sym",&sym)) sym=false;
    /* if y, use symmetric shaping */
    if (!sf_getfloat("tol",&tol)) tol=1e-3;
    /* tolerance for stopping iteration */
    
    nnshape_init(sym,nd, n1,n2, o1,o2, d1,d2, 
		 rect1,rect2, nw, 2, xy);
    sf_gmres_init(n12,niter); 
 
    z = sf_floatalloc (n12);
    m = sf_floatalloc (n12);
    d = sf_floatalloc(nd);

    sf_floatread(d,nd,in);
 
    /* make right-hand side */
    nnshape_back(d,z);
    nnshape_smooth(z);
    /* invert */
    sf_gmres(z,m,nnshape,NULL,niter,tol,true);
    if (sym) nnshape_smooth(m);
    
    sf_floatwrite (m, n12, out);

    exit(0);
}
Esempio n. 10
0
int main(int argc, char* argv[])
{
    int i, niter, n1, n2, n12, i3, n3, rect1, rect2, order;
    float *mm, *dd, **pp, lam;
    bool *known;
    sf_file in, out, dip, mask;

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

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

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

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

    pp = sf_floatalloc2(n1,n2);
    mm = sf_floatalloc(n12);
    known = sf_boolalloc(n12);
    
    if (NULL != sf_getstring ("mask")) {
	mask = sf_input("mask");
	dd = sf_floatalloc(n12);
    } else {
	mask = NULL;
	dd = NULL;
    }

    if (!sf_getint("rect1",&rect1)) rect1=3;
    if (!sf_getint("rect2",&rect2)) rect2=3;
    /* smoothing radius */
    
    pwdsl_init(n1,n2,order,rect1,rect2,0.01);
    pwdsl_set(pp);
    sf_mask_init(known);
    
    for (i3=0; i3 < n3; i3++) {
	sf_warning("slice %d of %d",i3+1,n3);

	sf_floatread(mm,n12,in);

	if (NULL != mask) {
	    sf_floatread(dd,n12,mask);
	} else {
	    dd = mm;
	}

	/* figure out scaling and make known data mask */
	lam = 0.;
	for (i=0; i < n12; i++) {
	    if (dd[i] != 0.) {
		known[i] = true;
		lam += 1.;
	    } else {
		known[i] = false;
	    }
	}
	lam = sqrtf(lam/n12);

	/* read dip */
	sf_floatread(pp[0],n12,dip);

	sf_conjgrad_init(n12, n12, n12, n12, lam, 10*FLT_EPSILON, true, true); 
	sf_conjgrad(NULL,sf_mask_lop,pwdsl_lop,dd,mm,mm,niter);
	sf_conjgrad_close();

	sf_floatwrite (mm,n12,out);
    }

    exit(0);
}
Esempio n. 11
0
int main (int argc, char *argv[])
{
    bool both;
    int ir, nr, n1,n2,n3,n4, m1, m2, m3, n12, n123, nw, nj1, nj2, i3;
    float *u1, *u2, *p;
    sf_file in, out, dip;
    off_t pos=0;
    allpass ap;

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

    if (SF_FLOAT != sf_gettype(in) ||
            SF_FLOAT != sf_gettype(dip)) sf_error("Need float type");

    if (!sf_histint(in,"n1",&n1)) sf_error("Need n1= in input");
    if (!sf_histint(in,"n2",&n2)) n2=1;
    if (!sf_histint(in,"n3",&n3)) n3=1;
    n12 = n1*n2;
    n123 = n12*n3;
    nr = sf_leftsize(in,3);

    if (!sf_histint(dip,"n1",&m1) || m1 != n1)
        sf_error("Need n1=%d in dip",n1);
    if (1 != n2 && (!sf_histint(dip,"n2",&m2) || m2 != n2))
        sf_error("Need n2=%d in dip",n2);
    if (1 != n3 && (!sf_histint(dip,"n3",&m3) || m3 != n3))
        sf_error("Need n3=%d in dip",n3);

    if (!sf_getbool("both",&both)) both=false;
    /* if y, compute both left and right predictions */

    if (1 == n3) {
        n4=0;
        if (both) sf_putint(out,"n3",2);
    } else {
        if (!sf_getint("n4",&n4)) n4=2;
        /* what to compute in 3-D. 0: in-line, 1: cross-line, 2: both */

        if (n4 > 2) n4=2;
        if (2==n4) sf_putint(out,"n4",both? 4:2);

        if (0 != n4 || both) {
            sf_unpipe(in,(off_t) n123*sizeof(float));
            pos = sf_tell(in);
        }
    }

    if (!sf_getint("order",&nw)) nw=1;
    /* accuracy */
    if (!sf_getint("nj1",&nj1)) nj1=1;
    /* in-line aliasing */
    if (!sf_getint("nj2",&nj2)) nj2=1;
    /* cross-line aliasing */

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

        if (1 != n4) { /* in-line */
            u1 = sf_floatalloc(n12);
            u2 = sf_floatalloc(n12);
            p  = sf_floatalloc(n12);

            for (i3=0; i3 < n3; i3++) {
                /* read data */
                sf_floatread(u1,n12,in);

                /* read t-x dip */
                sf_floatread(p,n12,dip);

                ap = allpass_init (nw,nj1,n1,n2,1,p);

                /* apply */
                allpass1(false, false, ap, u1, u2);

                /* write t-x destruction */
                sf_floatwrite(u2,n12,out);
            }

            free(u1);
            free(u2);
            free(p);

        }

        if (0 != n4) { /* cross-line */
            u1 = sf_floatalloc(n123);
            u2 = sf_floatalloc(n123);
            p  = sf_floatalloc(n123);

            /* read data */
            sf_seek(in,pos,SEEK_SET);
            sf_floatread(u1,n123,in);

            /* read t-y dip */
            sf_floatread(p,n123,dip);

            ap = allpass_init(nw,nj2,n1,n2,n3,p);

            /* apply */
            allpass2(false, false, ap, u1, u2);

            /* write t-y destruction */
            sf_floatwrite(u2,n123,out);
        }

        if (!both) continue;

        if (1 != n4) { /* in-line */
            u1 = sf_floatalloc(n12);
            u2 = sf_floatalloc(n12);
            p  = sf_floatalloc(n12);

            sf_seek(in,pos,SEEK_SET);

            for (i3=0; i3 < n3; i3++) {
                /* read data */
                sf_floatread(u1,n12,in);

                /* read t-x dip */
                sf_floatread(p,n12,dip);

                ap = allpass_init (nw,nj1,n1,n2,1,p);

                /* apply */
                allpass1(true, false, ap, u1, u2);

                /* write t-x destruction */
                sf_floatwrite(u2,n12,out);
            }

            free(u1);
            free(u2);
            free(p);
        }

        if (0 != n4) { /* cross-line */
            u1 = sf_floatalloc(n123);
            u2 = sf_floatalloc(n123);
            p  = sf_floatalloc(n123);

            /* read data */
            sf_seek(in,pos,SEEK_SET);
            sf_floatread(u1,n123,in);

            /* read t-y dip */
            sf_floatread(p,n123,dip);

            ap = allpass_init(nw,nj2,n1,n2,n3,p);

            /* apply */
            allpass2(true, false, ap, u1, u2);

            /* write t-y destruction */
            sf_floatwrite(u2,n123,out);
        }
    }

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

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

    sf_init(argc,argv);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	    to = sf_output("out");

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

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

		Rfile = fopen(R,"wb");
		DLOOP( sf_floatread(buf,dbuf,dat); 
		       for (i=0; i < dbuf; i++) { buf[i] = -buf[i]; }
		       sf_floatwrite(buf,dbuf,to);
		       DWRITE (Rfile); );
Esempio n. 13
0
int main(int argc, char* argv[])
{
  int nx, ny, nt, npara, nc;
  float x, y, dx, dy, dt, x0, y0, tini, t0;
  float **t0sq,**time, *coeff, ***dtime; 
  float w1,w2,w3,A1,A2,A3,A4,A5,B1,B2,B3,C1,C2,C3,C4,C5,A,B,C;
  int i,j,k,test;
  int count = 0;
  bool verb;

  sf_file inp, out, fit ,inicoef;

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

    /* input vector t0^2' */
    if (!sf_histint(inp,"n1",&nx)) sf_error("No n1=");
    if (!sf_histint(inp,"n2",&ny)) sf_error("No n2=");
    if (!sf_histint(inp,"n3",&nt)) sf_error("No n3=");
    if (!sf_histfloat(inp,"d1",&dx)) sf_error("No d1=");
    if (!sf_histfloat(inp,"d2",&dy)) sf_error("No d2=");
    if (!sf_histfloat(inp,"d3",&dt)) sf_error("No d3=");
    if (!sf_histfloat(inp,"o1",&x0)) sf_error("No o1=");
    if (!sf_histfloat(inp,"o2",&y0)) sf_error("No o2=");
    if (!sf_histfloat(inp,"o3",&tini)) sf_error("No o2=");

    /*Number of fitting parameters*/
    npara=16;
    
    /*Verbal flag*/
    if (!sf_getbool("verb",&verb)) verb=false;
    
    /*Memory allocation*/
    coeff = sf_floatalloc(npara);
    t0sq = sf_floatalloc2(nx,ny);
    time = sf_floatalloc2(nx,ny);
    dtime = sf_floatalloc3(nx,ny,npara);
    
    /*Output dimension*/
    if (!sf_histint(inicoef,"n1",&nc) || nc != npara) 
	sf_error("Need n1=%d in inicoef",npara);
    
    /*Shift the third dimension to 4th to insert coefficients*/
     sf_shiftdim(inp, fit, 3);
     sf_putint(fit,"n3",npara);
     sf_putint(fit,"d3",1);
     sf_putint(fit,"o3",0);
    
    /* Loop over time slices */
      for(k=0; k < nt; k++) {
        /*Read intial parameters*/
        sf_floatread(coeff,npara,inicoef);
        w1 = coeff[0];w2 = coeff[1];w3 = coeff[2];
        A1 = coeff[3];A2 = coeff[4];A3 = coeff[5];A4 = coeff[6];A5 = coeff[7];
        B1 = coeff[8];B2 = coeff[9];B3 = coeff[10];
        C1 = coeff[11];C2 = coeff[12];C3 = coeff[13];C4 = coeff[14];C5 = coeff[15];
        sf_floatread(t0sq[0],nx*ny,inp);
        

        /* Loops for each x and y*/
            for(j=0;j<ny;j++){
                y = y0 + j*dy;
                
                for(i=0;i<nx;i++){
                    x = x0 + i*dx;
                    t0 = sqrt(t0sq[j][i]);
                    
                    /* Avoid dividing by zero*/
                    if (x!=0 || y!=0 || t0!=0) {
                    
                    A = A1*pow(x,4) + A2*pow(x,3)*y + A3*pow(x,2)*pow(y,2) + A4*x*pow(y,3) + A5*pow(y,4);
                    B = B1*pow(x,2) + B2*x*y + B3*pow(y,2);
                    C = C1*pow(x,4) + C2*pow(x,3)*y + C3*pow(x,2)*pow(y,2) + C4*x*pow(y,3) + C5*pow(y,4);
                    
                    /* Compute traveltime (t^2)*/
                    time[j][i] = pow(t0,2) + w1*pow(x,2) + w2*x*y + w3*pow(y,2) + A/(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B));
                    /* Compute Derivatives */
                    dtime[0][j][i] = pow(x,2); // Wx
                    dtime[1][j][i] = x*y; // Wxy
                    dtime[2][j][i] = pow(y,2); //Wy

                    dtime[3][j][i] = pow(x,4)/(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B)); // A1

                    dtime[4][j][i] = (pow(x,3)*y)/(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B)); // A2

                    dtime[5][j][i] = (pow(x,2)*pow(y,2))/(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B)); // A3

                    dtime[6][j][i] = (x*pow(y,3))/(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B)); // A4

                    dtime[7][j][i] = pow(y,4)/(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B)); // A5

                    dtime[8][j][i] = (-A*x*x*(1+pow(t0,2)/(sqrt(pow(t0,4) + C + 2*pow(t0,2)*B))))/pow(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B),2); // B1

                    dtime[9][j][i] = (-A*x*y*(1+pow(t0,2)/(sqrt(pow(t0,4) + C + 2*pow(t0,2)*B))))/pow(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B),2); // B2

                    dtime[10][j][i] = (-A*y*y*(1+pow(t0,2)/(sqrt(pow(t0,4) + C + 2*pow(t0,2)*B))))/pow(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B),2); // B3
                    
                    dtime[11][j][i] = (-pow(x,4)*A)/(2*sqrt(pow(t0,4) + C + 2*pow(t0,2)*B)*pow(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B),2)); // C1

                    dtime[12][j][i] = (-pow(x,3)*y*A)/(2*sqrt(pow(t0,4) + C + 2*pow(t0,2)*B)*pow(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B),2)); // C2

                    dtime[13][j][i] = (-pow(x,2)*pow(y,2)*A)/(2*sqrt(pow(t0,4) + C + 2*pow(t0,2)*B)*pow(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B),2)); // C3

                    dtime[14][j][i] = (-x*pow(y,3)*A)/(2*sqrt(pow(t0,4) + C + 2*pow(t0,2)*B)*pow(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B),2)); // C4

                    dtime[15][j][i] = (-pow(y,4)*A)/(2*sqrt(pow(t0,4) + C + 2*pow(t0,2)*B)*pow(pow(t0,2) + B + sqrt(pow(t0,4) + C + 2*pow(t0,2)*B),2)); // C5
                    
                    for (test=0;test<16;test++) {
                    
                            if(verb && isnan(dtime[test][j][i]) != 0) {
                                sf_warning("The sqrt is NaN at dtime %d k %d j %d i %d",test,k+1,j,i);
                                sf_warning("x %f y %f B %f %f %f C %f %f %f %f %f",x,y,B1,B2,B3,C1,C2,C3,C4,C5);
                                sf_warning("pow(t0,4) + C + 2*pow(t0,2)*B : %f \n",pow(t0,4) + C + 2*pow(t0,2)*B);
                            }
                    }
                    
                    } else {
                    
                    time[j][i] = 0;
                    dtime[0][j][i] = 0;
                    dtime[1][j][i] = 0;
                    dtime[2][j][i] = 0;
                    dtime[3][j][i] = 0;
                    dtime[4][j][i] = 0;
                    dtime[5][j][i] = 0;
                    dtime[6][j][i] = 0;
                    dtime[7][j][i] = 0;
                    dtime[8][j][i] = 0;
                    dtime[9][j][i] = 0;
                    dtime[10][j][i] = 0;
                    dtime[11][j][i] = 0;
                    dtime[12][j][i] = 0;
                    dtime[13][j][i] = 0;
                    dtime[14][j][i] = 0;
                    dtime[15][j][i] = 0;
                    
                    }
                    
                    count++;
    /*                sf_warning("%d of %d surface locations ;",count,nx*ny);*/
            }
        }
        
        sf_warning(" Time step: %d; of %d;",k+1,nt);
        sf_floatwrite(time[0],nx*ny,out);
        sf_floatwrite(dtime[0][0],nx*ny*npara,fit);
    
    }

  exit(0);
}
Esempio n. 14
0
int main(int argc, char* argv[])
{
    int n1,n2,n3, jump, i1,i2,i3, j;
    float d, *pp=NULL, *zero=NULL, *one=NULL;
    sf_file in=NULL, out=NULL, mask=NULL;

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

    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_getint("jump",&jump)) jump=2;
    /* aliasing */

    if (n2 > 1) {
	sf_putint(out,"n2",n2*jump);
	sf_putint(mask,"n2",n2*jump);
	if (sf_histfloat(in,"d2",&d)) {
	    sf_putfloat(out,"d2",d/jump);
	    sf_putfloat(mask,"d2",d/jump);
	}
    }
    if (n3 > 1) {
	sf_putint(out,"n3",n3*jump);
	sf_putint(mask,"n3",n3*jump);
	if (sf_histfloat(in,"d3",&d)) {
	    sf_putfloat(out,"d3",d/jump);
	    sf_putfloat(mask,"d3",d/jump);
	}
    }

    pp = sf_floatalloc(n1);
    zero = sf_floatalloc(n1);
    one = sf_floatalloc(n1);

    for (i1=0; i1 < n1; i1++) {
	zero[i1] = 0.;
	one[i1] = 1.;
    }

    for (i3=0; i3 < n3; i3++) {
	for (i2=0; i2 < n2; i2++) {
	    sf_floatread(pp,n1,in);
	    sf_floatwrite(pp,n1,out);
	    sf_floatwrite(one,n1,mask);
	    if (n2 >1) {
		for (j=0; j < jump-1; j++) {
		    sf_floatwrite(zero,n1,out);
		    sf_floatwrite(zero,n1,mask);
		}
	    }
	}
	if (n3 > 1) {
	    for (i2=0; i2 < n2*jump; i2++) {
		sf_floatwrite(zero,n1,out);
		sf_floatwrite(zero,n1,mask);
	    }
	}
    }

    exit(0);
}
Esempio n. 15
0
int main (int argc, char* argv[])
{
    int nh, n1,n2, i1,i2, i, n12, niter, dim, n[SF_MAX_DIM], rect[SF_MAX_DIM];
    int shrt, lng;
    float *trace, *hilb, *num, *den, *phase, mean, c;
    char key[6];
    sf_triangle ts, tl;
    sf_file in, out;

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

    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");
    
    dim = sf_filedims (in,n);
    n1 = n[0];
    n12 = 1;
    for (i=0; i < dim; i++) {
	snprintf(key,6,"rect%d",i+1);
	if (!sf_getint(key,rect+i)) rect[i]=1;
	n12 *= n[i];
    }
    n2 = n12/n1;

    num = sf_floatalloc(n12);
    den = sf_floatalloc(n12);
    phase = sf_floatalloc(n12);

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

    if (!sf_getint("order",&nh)) nh=10;
    /* Hilbert transformer order */
    if (!sf_getfloat("ref",&c)) c=1.;
    /* Hilbert transformer reference (0.5 < ref <= 1) */

    sf_hilbert_init(n1, nh, c);

    if (!sf_getint("short",&shrt)) shrt=1;
    /* short smoothing radius */
    if (!sf_getint("long",&lng)) lng=10;
    /* long smoothing radius */

    ts = sf_triangle_init(shrt,n1,false);
    tl = sf_triangle_init(lng,n1,false);

    mean=0.;
    for (i2=0; i2 < n2; i2++) {
	trace = num+n1*i2;
	hilb = den+n1*i2;

	sf_floatread(trace,n1,in);
	sf_hilbert(trace,hilb);

	for (i1=0; i1 < n1; i1++) {
	    trace[i1] = hypotf(trace[i1],hilb[i1]);
	    hilb[i1] = trace[i1];
	}

	sf_smooth2 (ts, 0, 1, false, trace);
	sf_smooth2 (tl, 0, 1, false, hilb);
	
	for (i1=0; i1 < nh; i1++) {
	    trace[i1] = 0.;
	    hilb[i1] = 0.;
	}
	for (i1=nh; i1 < n1-nh; i1++) {
	    mean += hilb[i1]*hilb[i1];
	}
	for (i1=n1-nh; i1 < n1; i1++) {
	    trace[i1] = 0.;
	    hilb[i1] = 0.;
	}
    }
    mean = sqrtf(n12/mean);

    for (i=0; i < n12; i++) {
	num[i] *= mean;
	den[i] *= mean;
    }

    sf_divn_init(dim, n12, n, rect, niter, true);
    sf_divn (num, den, phase);

    sf_floatwrite(phase,n12,out);

    exit(0);
}
Esempio n. 16
0
int main (int argc, char* argv[])
{
    bool top;

    fint1 sft;
    int  ext;

    float a,n,f,da,a0,t0,dt,s;
    int   fint,na,nx,nz,nt;

    sf_axis ax,az,at,aa;
    int ix,iz,it,ia;

    float   **stk=NULL, **ang=NULL, *tmp=NULL, *vel=NULL;
    sf_file  Fstk=NULL,  Fang=NULL, velocity=NULL;

    sf_init (argc,argv);

    /*------------------------------------------------------------*/
    Fstk = sf_input("in");
    Fang = sf_output("out");

    if (SF_FLOAT != sf_gettype(Fstk)) sf_error("Need float input");

    az=sf_iaxa(Fstk,1); nz=sf_n(az);
    at=sf_iaxa(Fstk,2); nt=sf_n(at); t0=sf_o(at); dt=sf_d(at);
    ax=sf_iaxa(Fstk,3); nx=sf_n(ax);

    if (!sf_getint  ("na",&na)) na=nt;          /* number of angles*/
    if (!sf_getfloat("da",&da)) da=90/(nt-1);   /* angle sampling */
    if (!sf_getfloat("a0",&a0)) a0=0.;          /* angle origin */
    aa = sf_maxa(na,a0,da);
    sf_oaxa(Fang,aa,2);

    if (!sf_getint("extend",&ext)) ext=4;       /* tmp extension */
    /*------------------------------------------------------------*/

    if (!sf_getbool("top",&top)) top=false;     /* velocity scaling option */

    if (top) {
	velocity = sf_input("velocity");
	vel = sf_floatalloc(nz);
    } else {
	velocity = NULL;
	vel = NULL;
    }

    stk = sf_floatalloc2(nz,nt);
    ang = sf_floatalloc2(nz,na);
    tmp = sf_floatalloc(nt);

    sft = fint1_init(ext,nt, 0);

    for (ix = 0; ix < nx; ix++) {
	sf_floatread(stk[0],nz*nt,Fstk);
	if (top) sf_floatread(vel,nz,velocity);

	/*------------------------------------------------------------*/
	for (iz = 0; iz < nz; iz++) {
	    for (it = 0; it < nt; it++) {
		tmp[it] = stk[it][iz];
	    }
	    fint1_set(sft,tmp);

	    for (ia=0; ia < na; ia++) {
		a = a0+ia*da;          /* ang or p */

		if (top) {
		    s = a*vel[iz];
		    if (s >= 1.) {
			n = t0 - 10.*dt;
		    } else {
			n = s/sqrtf(1.0-s*s);
		    }
		} else {
		    n = 1.0/(sinf(a/180.0*SF_PI)); /* 1/sin : no angle close to 0 */
		}

		f = (n - t0) / dt;
		fint = f;

		if (fint >= 0 && fint < nt) {
		    ang[ia][iz] = fint1_apply(sft,fint,f-fint,false);
		} else {
		    ang[ia][iz] = 0.;
		}
	    }
	}
	/*------------------------------------------------------------*/

	sf_floatwrite(ang[0],nz*na,Fang);
    }

    exit (0);
}
Esempio n. 17
0
    int main(int argc, char* argv[])
{
    int i, n1, n2, n12, nj1, nj2, niter, nw, n3, i3;
    float eps, *d, *s, *nd, **nn, **ss;
    bool verb;
    sf_file in, out, ndip, sdip;

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

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

    n3 = sf_leftsize(in,2);
    if (1==n3) sf_putint (out,"n3",2);

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

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

    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 for noise dip */
    if (!sf_getint("nj2",&nj2)) nj2=1;
    /* antialiasing for signal dip */

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

    nd = sf_floatalloc(2*n12);
    s = sf_floatalloc(n12);
    d = sf_floatalloc(n12);
    nn = sf_floatalloc2(n1,n2);
    ss = sf_floatalloc2(n1,n2);

    for (i=0; i < n12; i++) {
	nd[n12+i] = 0.;
    }

    planesignoi_init (nw, nj1,nj2, n1,n2, nn, ss, eps);

    for (i3=0; i3 < n3; i3++) {
	sf_floatread (d,n12,in);
	sf_floatread (nn[0],n12,ndip);
	sf_floatread (ss[0],n12,sdip);

	allpass22_init(allpass2_init(nw,nj1,n1,n2,nn));
	allpass21_lop (false,false,n12,n12,d,nd);
	

	sf_solver (planesignoi_lop, sf_cgstep, n12, n12*2, s, nd, niter, 
		   "verb", verb, "end");

	sf_cgstep_close();

	sf_floatwrite(s,n12,out);

	if (1==n3) {
	    for (i=0; i < n12; i++) {
		d[i] -= s[i];
	    }
	    sf_floatwrite(d,n12,out);
	}
    }

    exit(0);
}
Esempio n. 18
0
int main(int argc, char* argv[]) 
{
    int nx, nt, nkx, nkz, ix, it, ikx, ikz, nz, iz, nbt, nbb, nbl, nbr, nxb, nzb, isx, isz;
    float dt, dx, dkx, kx, dz, dkz, kz, tmpdt, pi=SF_PI, o1, o2, kx0, kz0;
    float **nxt,  **old,  **cur, **ukr, **dercur, **derold, *wav;
    float **vx, vx2, vx0, vx02, **vz, vz2, vz0, vz02, **yi, yi0, **se, se0;
    float ***aa, dx2, dz2, dx4, dz4, ct, cb, cl, cr; //top, bottom, left, right 
    float w1, w10, w2, w20, w3, w30, h1, h10, h2, h20, h3, h30;
    float cosg, cosg0, cosg2, cosg02, sing, sing0, sing2, sing02;
    float vk, vk2, tmpvk, k2, err, dt2, kx1, kz1;
    kiss_fft_cpx **uk, *ctracex, *ctracez;
    kiss_fft_cfg cfgx, cfgxi, cfgz, cfgzi;
    sf_file out, velx, velz, source, yita, seta;
    bool opt;    /* optimal padding */
   // #ifdef _OPENMP
   // int nth;
   // #endif
     

    sf_init(argc,argv);
    out = sf_output("out");
    velx = sf_input("velx");   /* velocity */
    velz = sf_input("velz");   /* velocity */
    yita = sf_input("yita");   /* anistropic parameter*/
    source = sf_input("in");   /* source wavlet*/
    seta = sf_input("seta");   /* TTI angle*/

//    if (SF_FLOAT != sf_gettype(inp)) sf_error("Need float input");
    if (SF_FLOAT != sf_gettype(velx)) sf_error("Need float input");
    if (SF_FLOAT != sf_gettype(velz)) sf_error("Need float input");
    if (SF_FLOAT != sf_gettype(source)) sf_error("Need float input");
    if (SF_FLOAT != sf_gettype(seta)) sf_error("Need float input");

    if (!sf_histint(velx,"n1",&nx)) sf_error("No n1= in input");
    if (!sf_histfloat(velx,"d1",&dx)) sf_error("No d1= in input");
    if (!sf_histint(velx,"n2",&nz)) sf_error("No n2= in input");
    if (!sf_histfloat(velx,"d2",&dz)) sf_error("No d2= in input");
    if (!sf_histfloat(velx,"o1",&o1)) o1=0.0;
    if (!sf_histfloat(velx,"o2",&o2)) o2=0.0;
  //  if (!sf_histint(inp,"n2",&nt)) sf_error("No n2= in input");
  //  if (!sf_histfloat(inp,"d2",&dt)) sf_error("No d2= in input");
    if (!sf_getbool("opt",&opt)) opt=true;
    /* if y, determine optimal size for efficiency */
    if (!sf_getfloat("dt",&dt)) sf_error("Need dt input");
    if (!sf_getint("nt",&nt)) sf_error("Need nt input");
    if (!sf_getint("isx",&isx)) sf_error("Need isx input");
    if (!sf_getint("isz",&isz)) sf_error("Need isz input");
    if (!sf_getfloat("err",&err)) err = 0.0001;

    if (!sf_getint("nbt",&nbt)) nbt=44;
    if (!sf_getint("nbb",&nbb)) nbb=44;
    if (!sf_getint("nbl",&nbl)) nbl=44;
    if (!sf_getint("nbr",&nbr)) nbr=44;

    if (!sf_getfloat("ct",&ct)) ct = 0.01; /*decaying parameter*/
    if (!sf_getfloat("cb",&cb)) cb = 0.01; /*decaying parameter*/
    if (!sf_getfloat("cl",&cl)) cl = 0.01; /*decaying parameter*/
    if (!sf_getfloat("cr",&cr)) cr = 0.01; /*decaying parameter*/

    

    sf_putint(out,"n1",nx);
    sf_putfloat(out,"d1",dx);
//    sf_putfloat(out,"o1",x0);
    sf_putint(out,"n2",nz);
    sf_putfloat(out,"d2",dz);
    sf_putint(out,"n3",nt);
    sf_putfloat(out,"d3",dt);
    sf_putfloat(out,"o1",o1); 
    sf_putfloat(out,"o2",o2); 
    sf_putfloat(out,"o3",0.0); 

    nxb = nx + nbl + nbr;
    nzb = nz + nbt + nbb;


    nkx = opt? kiss_fft_next_fast_size(nxb): nxb;
    nkz = opt? kiss_fft_next_fast_size(nzb): nzb;
    if (nkx != nxb) sf_warning("nkx padded to %d",nkx);
    if (nkz != nzb) sf_warning("nkz padded to %d",nkz);
    dkx = 1./(nkx*dx);
    kx0 = -0.5/dx;
    dkz = 1./(nkz*dz);
    kz0 = -0.5/dz;
    cfgx = kiss_fft_alloc(nkx,0,NULL,NULL);
    cfgxi = kiss_fft_alloc(nkx,1,NULL,NULL);
    cfgz = kiss_fft_alloc(nkz,0,NULL,NULL);
    cfgzi = kiss_fft_alloc(nkz,1,NULL,NULL);


    uk = (kiss_fft_cpx **) sf_complexalloc2(nkx,nkz);
    ctracex = (kiss_fft_cpx *) sf_complexalloc(nkx);
    ctracez = (kiss_fft_cpx *) sf_complexalloc(nkz);

    wav    =  sf_floatalloc(nt);
    sf_floatread(wav,nt,source);

    old    =  sf_floatalloc2(nxb,nzb);
    cur    =  sf_floatalloc2(nxb,nzb);
    nxt    =  sf_floatalloc2(nxb,nzb);
    ukr    =  sf_floatalloc2(nxb,nzb);
    derold    =  sf_floatalloc2(nxb,nzb);
    dercur    =  sf_floatalloc2(nxb,nzb);
    aa     =  sf_floatalloc3(6,nxb,nzb);
    
    bd_init(nx,nz,nbt,nbb,nbl,nbr,ct,cb,cl,cr);

    vx = sf_floatalloc2(nxb,nzb);
    vz = sf_floatalloc2(nxb,nzb);

    /*input & extend velocity model*/
    for (iz=nbt; iz<nz+nbt; iz++){
        sf_floatread(vx[iz]+nbl,nx,velx);
        sf_floatread(vz[iz]+nbl,nx,velz);
         for (ix=0; ix<nbl; ix++){
             vx[iz][ix] = vx[iz][nbl];
             vz[iz][ix] = vz[iz][nbl];
         }
         for (ix=0; ix<nbr; ix++){
             vx[iz][nx+nbl+ix] = vx[iz][nx+nbl-1];
             vz[iz][nx+nbl+ix] = vz[iz][nx+nbl-1];
         }     
    }
    for (iz=0; iz<nbt; iz++){
        for (ix=0; ix<nxb; ix++){
            vx[iz][ix] = vx[nbt][ix];
            vz[iz][ix] = vz[nbt][ix];
        }
    }
    for (iz=0; iz<nbb; iz++){
        for (ix=0; ix<nxb; ix++){
            vx[nz+nbt+iz][ix] = vx[nz+nbt-1][ix];
            vz[nz+nbt+iz][ix] = vz[nz+nbt-1][ix];
        }
    }

    vx0 =0.0;
    vz0 =0.0;
    for (iz=0; iz < nzb; iz++) {
        for (ix=0; ix < nxb; ix++) {
            vx0 += vx[iz][ix]*vx[iz][ix];
            vz0 += vz[iz][ix]*vz[iz][ix];
         }
    }
    vx0 = sqrtf(vx0/(nxb*nzb));
    vz0 = sqrtf(vz0/(nxb*nzb));
    
    vx02=vx0*vx0; 
    vz02=vz0*vz0; 

    /*input & extend anistropic model*/
    yi = sf_floatalloc2(nxb,nzb);
    for (iz=nbt; iz<nz+nbt; iz++){
        sf_floatread(yi[iz]+nbl,nx,yita);
         for (ix=0; ix<nbl; ix++){
             yi[iz][ix] = yi[iz][nbl];
         }
         for (ix=0; ix<nbr; ix++){
             yi[iz][nx+nbl+ix] = yi[iz][nx+nbl-1];
         }     
    }
    for (iz=0; iz<nbt; iz++){
        for (ix=0; ix<nxb; ix++){
            yi[iz][ix] = yi[nbt][ix];
        }
    }
    for (iz=0; iz<nbb; iz++){
        for (ix=0; ix<nxb; ix++){
            yi[nz+nbt+iz][ix] = yi[nz+nbt-1][ix];
        }
    }

    yi0 = 0.0;
    for (iz=0; iz < nzb; iz++) {
        for (ix=0; ix < nxb; ix++) {
            yi0+= yi[iz][ix]*yi[iz][ix];
         }
    }
    yi0 = sqrtf(yi0/(nxb*nzb));
    
    se = sf_floatalloc2(nxb,nzb);
    for (iz=nbt; iz<nz+nbt; iz++){
        sf_floatread(se[iz]+nbl,nx,seta);
         for (ix=0; ix<nbl; ix++){
             se[iz][ix] = se[iz][nbl];
         }
         for (ix=0; ix<nbr; ix++){
             se[iz][nx+nbl+ix] = se[iz][nx+nbl-1];
         }     
    }
    for (iz=0; iz<nbt; iz++){
        for (ix=0; ix<nxb; ix++){
            se[iz][ix] = se[nbt][ix];
        }
    }
    for (iz=0; iz<nbb; iz++){
        for (ix=0; ix<nxb; ix++){
            se[nz+nbt+iz][ix] = se[nz+nbt-1][ix];
        }
    }

    se0 = 0.0;
    for (iz=0; iz < nzb; iz++) {
        for (ix=0; ix < nxb; ix++) {
            se0+= se[iz][ix];
         }
    }
    se0 /= (nxb*nzb);
    se0 *= pi/180.0;

    for (iz=0; iz < nzb; iz++) {
        for (ix=0; ix < nxb; ix++) {
            se[iz][ix] *= pi/180.0;
        }
    }

    cosg0 = cosf(se0);
    cosg02 = cosg0*cosg0;
    sing0 = sinf(se0);
    sing02 = sing0*sing0; 

    w10 = vx02*cosg02+vz02*sing02;
    w20 = vz02*cosg02+vx02*sing02;
    w30 = vx02+vz02+(vx02-vz02)*sinf(2.0*se0);
    h10 = sqrtf(-8.0*yi0*vx02*vz02*cosg02*sing02/(1.0+2.0*yi0)+w10*w10);
    h20 = sqrtf(-8.0*yi0*vx02*vz02*cosg02*sing02/(1.0+2.0*yi0)+w20*w20);
    h30 = sqrtf(-2.0*yi0*vx02*vz02*cosf(2.0*se0)*cosf(2.0*se0)/(1.0+2.0*yi0)+0.25*w30*w30);
    dt2 = dt*dt;
    dx2 = dx*dx;
    dx4 = dx2*dx2;
    dz2 = dz*dz;
    dz4 = dz2*dz2;

    for (iz=0; iz < nzb; iz++){
         for (ix=0; ix < nxb; ix++) {
         vx2 = vx[iz][ix]*vx[iz][ix];
         vz2 = vz[iz][ix]*vz[iz][ix];
	 cosg = cosf(se[iz][ix]);
         sing = sinf(se[iz][ix]);
         cosg2 = cosg*cosg;
         sing2 = sing*sing;
         w1 = vx2*cosg2+vz2*sing2;
         w2 = vz2*cosg2+vx2*sing2;
         w3 = vx2+vz2+(vx2-vz2)*sinf(2.0*se[iz][ix]);
         h1 = sqrtf(-8.0*yi[iz][ix]*vx2*vz2*cosg2*sing2/(1.0+2.0*yi[iz][ix])+w1*w1);
         h2 = sqrtf(-8.0*yi[iz][ix]*vx2*vz2*cosg2*sing2/(1.0+2.0*yi[iz][ix])+w2*w2);
         h3 = sqrtf(-2.0*yi[iz][ix]*vx2*vz2*cosf(2.0*se[iz][ix])*cosf(2.0*se[iz][ix])/(1.0+2.0*yi[iz][ix])+0.25*w3*w3);
         aa[iz][ix][4] = (w1+h1)*(dt2+(2.0*dx2-dt2*(w1+h1))/(w10+h10))/(24.0*dx4);
         aa[iz][ix][5] = (w2+h2)*(dt2+(2.0*dz2-dt2*(w2+h2))/(w20+h20))/(24.0*dz4);
         aa[iz][ix][3] = -aa[iz][ix][4]*dx2/dz2-aa[iz][ix][5]*dz2/dx2+(dt2*(w3+2.0*h3)+dx2*(w1+h1)/(w10+h10)+dz2*(w2+h2)/(w20+h20)-dt2*(w3+2.0*h3)*(w3+2.0*h3)/(w30+2.0*h30))/(12.0*dx2*dz2);
         aa[iz][ix][1] = -2.0*aa[iz][ix][3]-4.0*aa[iz][ix][4]-(w1+h1)/(dx2*(w10+h10));
         aa[iz][ix][2] = -2.0*aa[iz][ix][3]-4.0*aa[iz][ix][5]-(w2+h2)/(dz2*(w20+h20));
         aa[iz][ix][0] = -2.0*aa[iz][ix][1]-2.0*aa[iz][ix][2]-4.0*aa[iz][ix][3]-2.0*aa[iz][ix][4]-2.0*aa[iz][ix][5];
        }
      }

    for (iz=0; iz < nzb; iz++) {
        for (ix=0; ix < nxb; ix++) {
            cur[iz][ix] = 0.0;
        }
    }
    cur[isz+nbt][isx+nbl] = wav[0];
    for (iz=0; iz < nzb; iz++) {
        for (ix=0; ix < nxb; ix++) {
            old[iz][ix] =  0.0; 
            derold[iz][ix] =cur[iz][ix]/dt;
           }
         }
    for (iz=nbt; iz<nz+nbt; iz++){
        sf_floatwrite(cur[iz]+nbl,nx,out);
    }
/*
    #ifdef _OPENMP
    #pragma omp parallel
   {nth = omp_get_num_threads();}
    sf_warning("using %d threads",nth);
    #endif
*/
    /* propagation in time */
    for (it=1; it < nt; it++) {

         for (iz=0; iz < nzb; iz++){
             for (ix=0; ix < nxb; ix++){ 
                  nxt[iz][ix] = 0.0; 
                  uk[iz][ix].r = cur[iz][ix];
                  uk[iz][ix].i = 0.0; 
                }
         }  


/* compute u(kx,kz) */
         for (iz=0; iz < nzb; iz++){
             /* Fourier transform x to kx */
                for (ix=1; ix < nxb; ix+=2){
                    uk[iz][ix] = sf_cneg(uk[iz][ix]);
                    }
                kiss_fft_stride(cfgx,uk[iz],ctracex,1); 
                for (ikx=0; ikx<nkx; ikx++) uk[iz][ikx] = ctracex[ikx]; 
             }
         for (ikx=0; ikx < nkx; ikx++){
             /* Fourier transform z to kz */
                for (ikz=1; ikz<nkz; ikz+=2){
                    uk[ikz][ikx] = sf_cneg(uk[ikz][ikx]);
                    }
                kiss_fft_stride(cfgz,uk[0]+ikx,ctracez,nkx); 
                for (ikz=0; ikz<nkz; ikz++) uk[ikz][ikx] = ctracez[ikz]; 
             }

/*    #ifdef _OPENMP
    #pragma omp parallel for private(ik,ix,x,k,tmp,tmpex,tmpdt) 
    #endif
*/

         for (ikz=0; ikz < nkz; ikz++) {
             kz1 = (kz0+ikz*dkz)*2.0*pi;

             for (ikx=0; ikx < nkx; ikx++) {
                 kx1 = (kx0+ikx*dkx)*2.0*pi;
                 kx = kx1*cosg0+kz1*sing0;
                 kz = kz1*cosg0-kx1*sing0;
                 tmpvk = (vx02*kx*kx+vz02*kz*kz);
                 k2 = kx1*kx1+kz1*kz1;
                 vk2 = 0.5*tmpvk+0.5*sqrtf(tmpvk*tmpvk-8.0*yi0/(1.0+2.0*yi0)*vx02*vz02*kx*kx*kz*kz);
                 vk = sqrtf(vk2);
                 tmpdt = 2.0*(cosf(vk*dt)-1.0);
                 if(!k2) 
                   tmpdt /=(k2+err);
                 else 
                   tmpdt /= (k2);
                 uk[ikz][ikx] = sf_crmul(uk[ikz][ikx],tmpdt);
             }

         }   
/* Inverse FFT*/
         for (ikx=0; ikx < nkx; ikx++){
         /* Inverse Fourier transform kz to z */
             kiss_fft_stride(cfgzi,(kiss_fft_cpx *)uk[0]+ikx,(kiss_fft_cpx *)ctracez,nkx); 
             for (ikz=0; ikz < nkz; ikz++) uk[ikz][ikx] = sf_crmul(ctracez[ikz],ikz%2?-1.0:1.0); 
              }
             for (ikz=0; ikz < nkz; ikz++){
             /* Inverse Fourier transform kx to x */
                 kiss_fft_stride(cfgxi,(kiss_fft_cpx *)uk[ikz],(kiss_fft_cpx *)ctracex,1); 
                 for (ikx=0; ikx < nkx; ikx++) uk[ikz][ikx] = sf_crmul(ctracex[ikx],ikx%2?-1.0:1.0); 
             }

         for (iz=0; iz < nzb; iz++){
             for (ix=0; ix < nxb; ix++){ 
                  ukr[iz][ix] = sf_crealf(uk[iz][ix]); 
                  ukr[iz][ix] /= (nkx*nkz); 
                }
         }  

	 for (iz=2; iz < nzb-2; iz++) {  
	     for (ix=2; ix < nxb-2; ix++) {  
                 nxt[iz][ix]  = ukr[iz][ix]*aa[iz][ix][0]
                              + (ukr[iz][ix-1]+ukr[iz][ix+1])*aa[iz][ix][1]
                              + (ukr[iz-1][ix]+ukr[iz+1][ix])*aa[iz][ix][2]
                              + (ukr[iz-1][ix-1]+ukr[iz-1][ix+1]+ukr[iz+1][ix-1]+ukr[iz+1][ix+1])*aa[iz][ix][3]
                              + (ukr[iz][ix-2]+ukr[iz][ix+2])*aa[iz][ix][4]
                              + (ukr[iz-2][ix]+ukr[iz+2][ix])*aa[iz][ix][5];
             }
         }  
         
/* 
         nxt[0][0] = uk[0][0]*aa[0][0][0] + uk[0][1]*aa[0][0][1] + uk[1][0]*aa[0][0][2];
         nxt[0][nxb-1] = uk[0][nxb-1]*aa[0][nxb-1][0] + uk[0][nxb-2]*aa[0][nxb-1][1] + uk[1][nxb-1]*aa[0][nxb-1][2];
         nxt[nzb-1][0] = uk[nzb-1][0]*aa[nzb-1][0][0] + uk[nzb-1][1]*aa[nzb-1][0][1] + uk[nzb-2][0]*aa[nzb-1][0][2];
         nxt[nzb-1][nxb-1] = uk[nzb-1][nxb-1]*aa[nzb-1][nxb-1][0] + uk[nzb-1][nxb-2]*aa[nzb-1][nxb-1][1] + uk[nzb-2][nxb-1]*aa[nzb-1][nxb-1][2];
          
	 for (ix=1; ix < nxb-1; ix++) {  
             nxt[0][ix] = uk[0][ix]*aa[0][ix][0] + (uk[0][ix-1]+uk[0][ix+1])*aa[0][ix][1] + uk[1][ix]*aa[0][ix][2];
             nxt[nz-1][ix] = uk[nz-1][ix]*aa[nz-1][ix][0] + (uk[nz-1][ix-1]+uk[nz-1][ix+1])*aa[nz-1][ix][1] + uk[nz-2][ix]*aa[nz-1][ix][2];
         }
	 for (iz=1; iz < nzb-1; iz++) {  
             nxt[iz][0] = uk[iz][0]*aa[iz][0][0] + uk[iz][1]*aa[iz][0][1] + (uk[iz-1][0]+uk[iz+1][0])*aa[iz][0][2]; 
             nxt[iz][nx-1] = uk[iz][nx-1]*aa[iz][nx-1][0] + uk[iz][nx-2]*aa[iz][nx-1][1] + (uk[iz-1][nx-1]+uk[iz+1][nx-1])*aa[iz][nx-1][2]; 
         }
 */         
      //   nxt[isz+nbt][isx+nbl] += wav[it];

	 for (iz=0; iz < nzb; iz++) {  
             for (ix=0; ix < nxb; ix++) {
                 dercur[iz][ix]= derold[iz][ix] + nxt[iz][ix]/dt;
                 nxt[iz][ix] = cur[iz][ix] + dercur[iz][ix]*dt; 
            //     nxt[iz][ix] += 2.0*cur[iz][ix] -old[iz][ix]; 
             }
         }
 
           nxt[isz+nbt][isx+nbl] += wav[it];
           bd_decay(nxt); 
           bd_decay(dercur); 
                 
	 for (iz=0; iz < nzb; iz++) {  
             for(ix=0; ix < nxb; ix++) {
	        old[iz][ix] = cur[iz][ix]; 
	        cur[iz][ix] = nxt[iz][ix]; 
	        derold[iz][ix] = dercur[iz][ix]; 
             }
         }
         for (iz=nbt; iz<nz+nbt; iz++){
             sf_floatwrite(nxt[iz]+nbl,nx,out);
         }  
    }
    bd_close();
    free(**aa);
    free(*aa);
    free(aa);
    free(*vx);     
    free(*vz);     
    free(*yi);     
    free(*se);     
    free(*nxt);     
    free(*cur);     
    free(*old);     
    free(*dercur);     
    free(*derold);     
    free(*uk);     
    free(*ukr);     
    free(vx);     
    free(vz);     
    free(yi);     
    free(se);     
    free(nxt);     
    free(cur);     
    free(old);     
    free(dercur);     
    free(derold);     
    free(uk);     
    free(ukr);     
 //   sf_fileclose(vel);
 //   sf_fileclose(inp);
 //   sf_fileclose(out);
 
    exit(0); 
}           
Esempio n. 19
0
int main (int argc, char* argv[])
{
    map4 nmo; /* using cubic spline interpolation */
    bool half;
    int it,ix,iy, nt, nx, ny, nw, i4, n4, n;
    float dt, t0, x, y, x0, y0, f, dx, dy, eps;
    float *trace=NULL, *vx=NULL, *vy=NULL, *vxy=NULL, *str=NULL, *out=NULL;
    sf_file cmp=NULL, nmod=NULL, vel=NULL;

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

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

    if (!sf_histint(cmp,"n2",&nx)) sf_error("No n2= in input");
    if (!sf_histint(cmp,"n3",&ny)) sf_error("No n3= in input");
    n4 = sf_leftsize(cmp,3);

    vel = sf_input("velocity");
    if (SF_FLOAT != sf_gettype(vel)) sf_error("Need float velocity");
    if (!sf_histint(vel,"n1",&n) || nt != n) sf_error("Need n1=%d in velocity",nt);
    if (!sf_histint(vel,"n2",&n) || 3 != n) sf_error("Need n2=3 in velocity",nt);
    if (n4 != sf_leftsize(vel,2)) sf_error("Wrong dimensions in velocity");

    if (!sf_histfloat(cmp,"d2",&dx)) sf_error("No d2= in input");
    if (!sf_histfloat(cmp,"o2",&x0)) sf_error("No o2= in input");
    if (!sf_histfloat(cmp,"d3",&dy)) sf_error("No d3= in input");
    if (!sf_histfloat(cmp,"o3",&y0)) sf_error("No o3= in input");

    if (!sf_getbool("half",&half)) half=true;
    /* if y, the second and third axes are half-offset instead of full offset */

    if (half) { /* get full offset - multiply by 2 */
      dx *= 2.;
      dy *= 2.;
      x0 *= 2.;
      y0 *= 2.;
      sf_warning("Since half=y, offsets were doubled.");
    }else{
      sf_warning("Since half=n, offsets not doubled.");
    }

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

    trace = sf_floatalloc(nt);
    vx = sf_floatalloc(nt);
    vy = sf_floatalloc(nt);
    vxy = sf_floatalloc(nt);

    str = sf_floatalloc(nt);
    out = sf_floatalloc(nt);

    if (!sf_getint("extend",&nw)) nw=8;
    /* trace extension */

    nmo = stretch4_init (nt, t0, dt, nt, eps);

    for (i4=0; i4 < n4; i4++) { /* loop over cmps */
      sf_floatread (vx,nt,vel);
      sf_floatread (vy,nt,vel);
      sf_floatread (vxy,nt,vel);

      for (iy = 0; iy < ny; iy++) {
	y = y0+iy*dy;

	for (ix = 0; ix < nx; ix++) {
	  x = x0 + ix*dx;

	  sf_floatread (trace,nt,cmp);

	  for (it=0; it < nt; it++) {
	    f = t0 + it*dt;
	    f = f*f + x*x*vx[it]+y*y*vy[it]+2*x*y*vxy[it];

	    if (f < 0.) {
	      str[it]=t0-10.*dt;
	    } else {
	      str[it] = sqrtf(f);
	    }
	  }

	  stretch4_define (nmo,str);
	  stretch4_apply (nmo,trace,out);

	  sf_floatwrite (out,nt,nmod);
	} /* ix */
      } /* iy */
    } /* i4 */

    exit (0);
}
Esempio n. 20
0
int main(int argc, char* argv[])
{
    int i, ia, na, nx, ns, dim, n[SF_MAX_DIM], m[SF_MAX_DIM];
    float a0, *pp, *qq;
    bool adj;
    sf_filter aa;
    char* lagfile;
    sf_file in, out, filt, lag;

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

    dim = sf_filedims (in,n);

    if (!sf_histint(filt,"n1",&na)) sf_error("No n1= in filt");
    aa = sf_allocatehelix (na);

    if (!sf_histfloat(filt,"a0",&a0)) a0=1.;
    sf_floatread (aa->flt,na,filt);
    for( ia=0; ia < na; ia++) {
	aa->flt[ia] /= a0;
    }

    if (NULL != (lagfile = sf_getstring("lag")) /* file with filter lags */
	|| 
	NULL != (lagfile = sf_histstring(filt,"lag"))) {
	lag = sf_input(lagfile);

	sf_intread(aa->lag,na,lag);
    } else {
	lag = NULL;
	for( ia=0; ia < na; ia++) {
	    aa->lag[ia] = ia+1;
	}
    }

    sf_fileclose(filt);
    
    if (!sf_getints ("n",m,dim) && (NULL == lag ||
				    !sf_histints (lag,"n",m,dim))) {
	for (i=0; i < dim; i++) {
	    m[i] = n[i];
	}
    }
 
    if (NULL != lag) sf_fileclose(lag);

    regrid (dim, m, n, aa);

    if (!sf_getbool ("adj",&adj)) adj=false;
    /* if y, do adjoint operation */
    if (!sf_getint ("ns",&ns)) sf_error("Need ns=");
    /* scaling */

    nx = 1;
    for( i=0; i < dim; i++) {
	nx *= n[i];
    }
  
    pp = sf_floatalloc (nx);
    qq = sf_floatalloc (nx);

    if (adj) {
	sf_floatread (qq,nx,in);
    } else {
	sf_floatread (pp,nx,in);
    }

    hshape_init (nx,ns,aa);
    hshape_lop (adj,false,nx,nx,pp,qq);

    if (adj) {
	sf_floatwrite (pp,nx,out);
    } else {
	sf_floatwrite (qq,nx,out);
    }


    exit (0);
}
Esempio n. 21
0
int main (int argc, char* argv[])
{
    int n1,n2,i3,i2,n4,j,n12,fold,ndim, mdim;
    int n[SF_MAX_DIM], m[SF_MAX_DIM], *mk;
    float *d, *sht, *x, mean, std, sum, amp=0.0;
    bool verb;
    sf_file in, out, msk, scl;

    sf_init (argc,argv);
    in  = sf_input("in");
    out = sf_output("out");
    
    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float");
    
    if (!sf_histint(in,"n1",&n1)) sf_error("Need n1=");
    if (!sf_histint(in,"n2",&n2)) sf_error("Need n2=");
    n4 = sf_leftsize(in,2);
    n12=n1*n2;

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

    /* input file*/
    if (! (sf_getstring("mask") || 
               sf_histstring(in,"mask")))
         sf_error("Need mask=");

    msk = sf_input("mask");
    if (SF_INT != sf_gettype(msk)) sf_error("Need integer mask");

    sf_getfloat("amp",&amp);
    /*Exclude amplitudes greater than amp && less than -amp for statistics computations*/

    ndim = sf_filedims(in,n);
    mdim = sf_filedims(msk,m);

    if (mdim != ndim) 
	sf_error("Wrong dimensions: %d != %d",mdim,ndim);
    
    for (j=0; j < ndim; j++) {
	if (m[j] != n[j])
            sf_error("Size mismatch [n%d]: %d != %d",
		     j+1,m[j],n[j]);
    }

    /* output file*/
    if (NULL != sf_getstring("scaler")){
        scl = sf_output("scaler");
        sf_unshiftdim(in, scl, 2);
        sf_putint(scl,"n1",2);
    } else {
	scl = NULL;
    }
    
    d   = sf_floatalloc(n12*n4);
    x   = sf_floatalloc(2*n4);
    sht = sf_floatalloc(n12);
    mk  = sf_intalloc(n12);
    
    /* loop through time samples */
    for (i3=0; i3 < n4; i3++) {
        mean=0.0;
        fold=0;
        std =0.0;
        sum=0.0;
	sf_floatread(sht,n12,in);
        sf_intread(mk,n12,msk);
        /* compute mean */
	for (i2=0; i2 < n12; i2++) {
           if (sht[i2]!=0.0 && mk[i2]!=0 ) {
                if ( amp==0.0 || fabsf(sht[i2]) < fabsf(amp) ){        
                    sum  +=sht[i2];
                    fold +=1;
                }
           }     
        }
        if (fold > 0) mean=sum/fold;
        
        /* compute standard deviation */
        for (i2=0; i2 < n12; i2++) {
           if (sht[i2]!=0.0 && mk[i2]!=0 ) {
               //if (!(amp && fabsf(sht[i2]) < fabsf(amp)))
               //     continue;
               if ( amp==0.0 || fabsf(sht[i2]) < fabsf(amp) )
                  std += (sht[i2]-mean)*(sht[i2]-mean);
           }     
        }
        if (fold > 0) std =sqrtf(std/(fold-1));
        
        /* scale time samples*/
        for (i2=0; i2 < n12; i2++) 
            if (sht[i2]!=0.0 && mean!=0.0 && std!=0.0)
                d[i2+i3*n12]=(sht[i2]-mean)/std;
            else
                d[i2+i3*n12]=sht[i2];
        x[0+i3*2]=mean;
        x[1+i3*2]=std;

        if (verb) sf_warning("shot %8d-> mean:%8g std:%8g, fold:%8d\n;",i3+1,mean,std,fold);
    }
    sf_floatwrite (d,n4*n12,out);
    if (scl)
       sf_floatwrite (x,n4*2,scl);
    exit(0);
}
Esempio n. 22
0
int main(int argc, char* argv[]) 
{
    int nx, nt, ix, it, isx;
    float dt, dx;
    float *old, *nxt, *cur, *sig, *a, *b1, *b2, *b3, *b4, *b5;
    sf_file in, out, Gmatrix, vel;
    int im,im2,im3,im4,im5,ip,ip2,ip3,ip4,ip5;

    sf_init(argc,argv);
    in  = sf_input("in");
    Gmatrix = sf_input("G");   /* velocity */
    vel = sf_input("vel");   /* velocity */
    out = sf_output("out");

    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");
    if (SF_FLOAT != sf_gettype(Gmatrix)) sf_error("Need float input");
    if (!sf_histint(vel,"n1",&nx)) sf_error("No n1= in input");
    if (!sf_histfloat(vel,"d1",&dx)) sf_error("No d1= in input");
    if (!sf_getint("isx",&isx)) isx=(int)(nx/2);
    if (!sf_getint("nt",&nt)) sf_error("No nt in input");
    if (!sf_getfloat("dt",&dt)) sf_error("No dt in input");

    sf_putint(out,"n1",nx);
    sf_putfloat(out,"d1",dx);
    sf_putint(out,"n2",nt);
    sf_putfloat(out,"d2",dt);
    sf_putfloat(out,"o2",0.0); 

    sig = sf_floatalloc(nx);
    old = sf_floatalloc(nx);
    nxt = sf_floatalloc(nx);
    cur = sf_floatalloc(nx);
    a = sf_floatalloc(nx);
    b1 = sf_floatalloc(nx);
    b2 = sf_floatalloc(nx);
    b3 = sf_floatalloc(nx);
    b4 = sf_floatalloc(nx);
    b5 = sf_floatalloc(nx);
    
 
    sf_floatread(a,nx,Gmatrix);
    sf_floatread(b1,nx,Gmatrix);
    sf_floatread(b2,nx,Gmatrix);
 /*   sf_floatread(b3,nx,Gmatrix);
    sf_floatread(b4,nx,Gmatrix);
    sf_floatread(b5,nx,Gmatrix); */
    sf_floatread(sig,nx,in);		

	/* initial conditions */
    for (ix=0; ix < nx; ix++){
        cur[ix] =  sig[ix];
        old[ix] =  0.0; 
	nxt[ix] = 0.;
    }

    /* propagation in time */
    for (it=0; it < nt; it++) {
	sf_floatwrite(cur,nx,out);
	/* Stencil */
	for (ix=0; ix < nx; ix++) {
            im = ix-1 < 0? ix-1+nx:ix-1; 
            im2 = ix-2 < 0? ix-2+nx:ix-2; 
            im3 = ix-3 < 0? ix-3+nx:ix-3; 
            im4 = ix-4 < 0? ix-4+nx:ix-4; 
            im5 = ix-5 < 0? ix-5+nx:ix-5; 
            ip = ix+1 > nx-1? ix+1-nx:ix+1;
            ip2 = ix+2 > nx-1? ix+2-nx:ix+2;
            ip3 = ix+3 > nx-1? ix+3-nx:ix+3;
            ip4 = ix+4 > nx-1? ix+4-nx:ix+4;
            ip5 = ix+5 > nx-1? ix+5-nx:ix+5;

	    nxt[ix] = ( 0.5* (cur[im]+cur[ip])*b1[ix] +  0.5*(cur[im2]+cur[ip2])*b2[ix]) 
                       - old[ix] + 2.0*cur[ix];

	    old[ix] = cur[ix];
	    cur[ix] = nxt[ix];
	}
    }
    exit(0);
}
Esempio n. 23
0
int main(int argc, char* argv[])
{

        int    nx, nz, na, nb, i, j;
		float  fx, fz, dx, dz, da, db;

        sf_init(argc,argv);

        sf_file Fo1, Fo2, Fo3, Fo4, Fo5, Fo6;
        Fo1 = sf_input("in"); // wavefront1
        Fo2 = sf_input("wave2"); // wavefront2
        Fo3 = sf_output("out"); // wavetrans1
        Fo4 = sf_output("wavetrans2"); // wavetrans2 
        Fo5 = sf_output("amp1"); // amplitue1
        Fo6 = sf_output("amp2"); // amplitue2

		float **snap1, **snap2;
		float **sn1, **sn2;
		float *amp1, *amp2;

        float amax, amax1, amax2;

        /* Read/Write axes */
	    sf_axis az, ax;
	    az = sf_iaxa(Fo1,1); nz = sf_n(az); dz = sf_d(az)*1000;
	    ax = sf_iaxa(Fo1,2); nx = sf_n(ax); dx = sf_d(ax)*1000;
        fx=sf_o(ax);
        fz=sf_o(az);

        sf_warning("nx= %d nz= %d",nx,nz);
        sf_warning("dx= %f dz= %f",dx,dz);

		na = 720;
		da = 180.0/na;
		nb = nx*2;
		db = dx/2.0;

        sf_warning("da= %f db= %f",da,db);

		sf_putint(Fo3,"n1",nb);
		sf_putint(Fo3,"n2",na);
		sf_putfloat(Fo3,"d1",db);
		sf_putfloat(Fo3,"o1",0.0f);
		sf_putfloat(Fo3,"d2",da);
		sf_putfloat(Fo3,"o2",-90.0f);

		sf_putint(Fo4,"n1",nb);
		sf_putint(Fo4,"n2",na);
		sf_putfloat(Fo4,"d1",db);
		sf_putfloat(Fo4,"o1",0.0f);
		sf_putfloat(Fo4,"d2",da);
		sf_putfloat(Fo4,"o2",-90.0f);

		sf_putint(Fo5,"n2",1);
		sf_putint(Fo5,"n1",na);
		sf_putfloat(Fo5,"d1",da);
		sf_putfloat(Fo5,"o1",-90.0f);
		sf_putstring(Fo5,"label1","Phase angle");
		sf_putstring(Fo5,"unit1","Degree");

		sf_putint(Fo6,"n2",1);
		sf_putint(Fo6,"n1",na);
		sf_putfloat(Fo6,"d1",da);
		sf_putfloat(Fo6,"o1",-90.0f);
		sf_putstring(Fo6,"label1","Phase angle");
		sf_putstring(Fo6,"unit1","Degree");

		snap1=sf_floatalloc2(nz, nx);
		snap2=sf_floatalloc2(nz, nx);
		sn1=sf_floatalloc2(nb, na);
		sn2=sf_floatalloc2(nb, na);
		amp1=sf_floatalloc(na);
		amp2=sf_floatalloc(na);

		for(i=0;i<nx;i++){
            sf_floatread(snap1[i], nz, Fo1);
            sf_floatread(snap2[i], nz, Fo2);
		}
		for(i=0;i<na;i++){
		    amp1[i]=0.0;
		    amp2[i]=0.0;
			for(j=0;j<nb;j++){
				  sn1[i][j]=0.0;
				  sn2[i][j]=0.0;
			}
		}
		for(i=0;i<na;i++){
			float a=i*da;
			a *= SF_PI/180.0;
			for(j=0;j<nb;j++){
				float b=j*db;
				float x=(nx-1)*dx*0.5-b*cos(a);
				float z=(nz-1)*dz*0.5+b*sin(a);
				int ix, iz;
				ix=x/dx;
				iz=z/dz;
				if(ix>=0&&ix<nx&&iz>=nz/2&&iz<nz){
			     //sf_warning("i=%d a=%f j=%d b=%f x=%f z=%f ix=%d iz=%d",i,a,j,b,x,z,ix,iz);
				  sn1[i][j]=snap1[ix][iz];
				  sn2[i][j]=snap2[ix][iz];
				}
			}
            sf_floatwrite(sn1[i], nb, Fo3);
            sf_floatwrite(sn2[i], nb, Fo4);
		}
		for(i=0;i<na;i++){
			amax=0.0;
		    for(j=0;j<nb;j++)
			   if(fabs(sn1[i][j])>amax) amax=fabs(sn1[i][j]);
			amp1[i]=amax;
            amax=0.0;
			for(j=0;j<nb;j++)
			   if(fabs(sn2[i][j])>amax) amax=fabs(sn2[i][j]);
			amp2[i]=amax;
        }
		
        amax1=0.0;
        amax2=0.0;
		for(i=na/2-2;i<na/2+2;i++){
			if(amp1[i]>amax1) amax1=amp1[i];
			if(amp2[i]>amax2) amax2=amp2[i];
		}
		for(i=0;i<na;i++){
            amp1[i] /= amax1;
            amp2[i] /= amax2;
		}

        sf_floatwrite(amp1, na, Fo5);
        sf_floatwrite(amp2, na, Fo6);
        free(*snap1);
        free(*snap2);
        free(*sn1);
        free(*sn2);
        free(amp1);
        free(amp2);
}
Esempio n. 24
0
int main(int argc, char* argv[])
{
    int n2, i2, i1, ic, id, nc, n;
    double xp;
    float *roots, *sol, *dat, x, o, d;
    sf_file inp, coef, out;

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

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

    if (NULL != sf_getstring("coef")) {
        /* (optional) coefficients */
	coef = sf_output("coef"); 
	sf_putint(coef,"n1",nc);
    } else {
	coef = NULL;
    }

    roots = sf_floatalloc(nc-1);

    out = sf_output("out");
    if (!sf_getint("n1",&n)) sf_error("Need n1=");
    /* number of samples */
    if (!sf_getfloat("d1",&d)) sf_error("Need d1=");
    /* sampling */
    if (!sf_getfloat("o1",&o)) sf_error("Need o1=");
    /* origin */
    sf_putint(out,"n1",n);
    sf_putfloat(out,"d1",d);
    sf_putfloat(out,"o1",o);

    dat = sf_floatalloc(n);
    sol = sf_floatalloc(nc);
    sol[0]=1.0;

    for (i2=0; i2 < n2; i2++) {
	sf_floatread(roots,nc-1,inp);
	
	for (ic=1; ic < nc; ic++) {
	    sol[ic]=0.0;
	}

	for (ic=0; ic < nc-1; ic++) {
	    for (id=0; id <= ic; id++) {
		sol[id+1] -= roots[ic]*sol[id];
	    }
	}

	if (NULL != coef) sf_floatwrite(sol,nc,coef);

	for (i1=0; i1 < n; i1++) {
	    dat[i1] = 0.;
	    x = o+i1*d;
	    xp = 1.0;
	    for (ic=0; ic < nc; ic++) {
		dat[i1] += xp*sol[ic];
		xp *= x;
	    }
	}

	sf_floatwrite(dat,n,out);
    }

    exit(0);
}
Esempio n. 25
0
int main(int argc, char* argv[])
{
    int dim, n[SF_MAX_DIM], rect;
    int it0, it1, niter, n1, n2, i2, n3, i3, i1, gate1, gate2, k2, k3, i5, n5;
    float ***scan, ***weight, **pick, *ampl, **pick2;
    float o2, d2, o3, d3, an1, an2, asum, a, ct0, ct1, vel2, vel3;
    bool smooth, verb;
    sf_file scn, pik;

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

    if (SF_FLOAT != sf_gettype(scn)) sf_error("Need float input");
    dim = sf_filedims (scn,n);
    /* if (dim != 3) sf_error("Need three dimensions"); */

    n1 = n[0];
    n2 = n[1];
    n3 = n[2];

    n5 = sf_leftsize(scn,3);

    if (!sf_histfloat(scn,"o2",&o2)) o2=0.;
    if (!sf_histfloat(scn,"d2",&d2)) d2=1.;

    if (!sf_histfloat(scn,"o3",&o3)) o3=0.;
    if (!sf_histfloat(scn,"d3",&d3)) d3=1.;
 
    if (!sf_getfloat("vel1",&vel2)) vel2=o2;
    if (!sf_getfloat("vel2",&vel3)) vel3=o3;
    /* surface velocity */

    k2 = 0.5 + (vel2-o2)/d2;
    if (k2 < 0) k2=0;
    if (k2 >= n2) k2=n2-1;

    k3 = 0.5 + (vel3-o3)/d2;
    if (k3 < 0) k3=0;
    if (k3 >= n3) k3=n3-1;

    sf_putint(pik,"n2",1);
    sf_putint(pik,"n3",1);
    sf_putint(pik,"n4",2);
    sf_putint(pik,"n5",n5);

    
    if (!sf_getint("rect1",&rect)) rect=1;
    /* smoothing radius */

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

    if (!sf_getfloat("an1",&an1)) an1=1.;
    if (!sf_getfloat("an2",&an2)) an2=1.;
    /* axes anisotropy */
    if (!sf_getint("gate1",&gate1)) gate1=3;
    if (!sf_getint("gate2",&gate2)) gate2=3;
    /* picking gate */
    if (!sf_getbool("smooth",&smooth)) smooth=true;
    /* if apply smoothing */

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

    scan = sf_floatalloc3(n1,n2,n3);
    weight = sf_floatalloc3(n2,n3,n1);

    (void) dynprog3_init(n1,n2,n3,gate1,gate2,an1,an2,false);

    if (smooth) {
	pick = sf_floatalloc2(n1,2);
	pick2 = sf_floatalloc2(n1,2);	
	ampl = sf_floatalloc(n1);

	sf_divn_init(1,n1,&n1,&rect,niter,verb);
    } else {
	pick = NULL;
	pick2 = sf_floatalloc2(n1,2);
	ampl = NULL;
    }

    for(i5=0; i5 < n5; i5++) {
	sf_warning("slice %d of %d;",i5+1,n5);

	sf_floatread(scan[0][0],n1*n2*n3,scn);
	
	/* transpose and reverse */
	for (i3=0; i3 < n3; i3++) {
	    for (i2=0; i2 < n2; i2++) {
		for (i1=0; i1 < n1; i1++) {
		    weight[i1][i3][i2] = expf(-scan[i3][i2][i1]);
		}
	    }
	}
	
	dynprog3(k2, k3, weight);
	dynprog3_traj(pick2);
	
	if (smooth) {
	    for (i1=0; i1 < n1; i1++) {
		ct0 = pick2[0][i1];
		it0 = floorf(ct0);
		ct0 -= it0;
		
		if (it0 >= n2-1) {
		    it0 = n2-2;
		    ct0 = 0.;
		} else if (it0 < 0) {
		    it0 = 0;
		    ct0 = 0.;
		}
		
		ct1 = pick2[1][i1];
		it1 = floorf(ct1);
		ct1 -= it1;
		
		if (it1 >= n3-1) {
		    it1 = n3-2;
		    ct1 = 0.;
		} else if (it1 < 0) {
		    it1 = 0;
		    ct1 = 0.;
		}
		
		ampl[i1]=
		    scan[it1  ][it0  ][i1]*(1.-ct0)*(1.-ct1) +
		    scan[it1+1][it0  ][i1]*(1.-ct0)*ct1 +
		    scan[it1  ][it0+1][i1]*ct0*(1.-ct1) +
		    scan[it1+1][it0+1][i1]*ct0*ct1;
	    }
	} else {
	    for (i1=0; i1 < n1; i1++) {
		pick2[0][i1] = o2+pick2[0][i1]*d2;
		pick2[1][i1] = o3+pick2[1][i1]*d3;
	    }
	}
	
	if (smooth) {
	    /* normalize amplitudes */
	    asum = 0.;
	    for (i1 = 0; i1 < n1; i1++) {
		a = ampl[i1];
		asum += a*a;
	    }
	    asum = sqrtf (asum/n1);
	    for(i1=0; i1 < n1; i1++) {
		ampl[i1] /= asum;
		pick[0][i1] = (o2+pick2[0][i1]*d2-vel2)*ampl[i1];
		pick[1][i1] = (o3+pick2[1][i1]*d3-vel3)*ampl[i1];
	    }
	    
	    sf_divn(pick[0],ampl,pick2[0]);
	    sf_divn(pick[1],ampl,pick2[1]);
	    
	    for(i1=0; i1 < n1; i1++) {
		pick2[0][i1] += vel2;
		pick2[1][i1] += vel3;
	    }
	}
	
	sf_floatwrite(pick2[0],n1*2,pik);
    }
    sf_warning(".");
    exit(0);
}
Esempio n. 26
0
int main(int argc, char* argv[])
{
    sf_map4 mo;
    bool inv, each=true;
    int i, nt, n1, i2, n2, nw;
    float o1, d1, t0, dt, eps;
    sf_complex *ctrace, *ctrace2;
    float *trace, *str, *trace2;
    sf_file in, out, warp;

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

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

    if (inv) {
	if (!sf_histint(in,"n1",&nt)) sf_error("No n1= in input");
	
	if (!sf_getint("n1",&n1)) n1=nt; /* output samples - for inv=y */
	if (!sf_getfloat("d1",&d1) && !sf_histfloat(in,"d1",&d1)) d1=1.;
	/*( d1=1 output sampling - for inv=y )*/
	if (!sf_getfloat("o1",&o1) && !sf_histfloat(in,"o1",&o1)) o1=0.;
	/*( o1=0 output origin - for inv=y )*/ 

	sf_putint(out,"n1",n1);
	sf_putfloat(out,"d1",d1);
	sf_putfloat(out,"o1",o1);
    } else {
	if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
	if (!sf_histfloat(in,"d1",&d1)) d1=1.;
	if (!sf_histfloat(in,"o1",&o1)) o1=0.;

	if (!sf_histint(warp,"n1",&nt)) sf_error("No n1= in warp");
	if (!sf_histfloat(warp,"d1",&dt)) dt=d1;
	if (!sf_histfloat(warp,"o1",&t0)) t0=o1;
	
	sf_putint(out,"n1",nt);
	sf_putfloat(out,"d1",dt);
	sf_putfloat(out,"o1",t0);
    }

    n2 = sf_leftsize(in,1);
    nw = sf_leftsize(warp,1);
    if (1 == nw) {
	each = false;
    } else if (n2 != nw) {
	sf_error("Need %d traces in warp, got %d",n2,nw);
    } 
    
    if (!sf_getfloat("eps",&eps)) eps=0.01;
    /* stretch regularization */

    trace = sf_floatalloc(nt);
    str = sf_floatalloc(nt);
    trace2 = sf_floatalloc(n1);

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

    if (SF_COMPLEX == sf_gettype(in)) {
	ctrace = sf_complexalloc(nt);
	ctrace2 = sf_complexalloc(n1);
    } else {
	ctrace = ctrace2 = NULL;
    }

    for (i2=0; i2 < n2; i2++) {
	if (each || 0==i2) {
	    sf_floatread(str,nt,warp);
	    sf_stretch4_define (mo,str);
	}

	if (inv) {
	    if (SF_COMPLEX == sf_gettype(in)) {
		sf_complexread(ctrace,nt,in);
		for (i=0; i < nt; i++) {
		    trace[i] = crealf(ctrace[i]);
		}
		sf_stretch4_apply (false,mo,trace,trace2);
		for (i=0; i < n1; i++) {
		    ctrace2[i] = sf_cmplx(trace2[i],0.0f);
		}
		for (i=0; i < nt; i++) {
		    trace[i] = cimagf(ctrace[i]);
		}
		sf_stretch4_apply (false,mo,trace,trace2);
		for (i=0; i < n1; i++) {
#ifdef SF_HAS_COMPLEX_H
		    ctrace2[i] += sf_cmplx(0.0f,trace2[i]);
#else
		    ctrace2[i] = sf_cadd(ctrace2[i],sf_cmplx(0.0f,trace2[i]));
#endif
		}
		sf_complexwrite (ctrace2,n1,out);
	    } else {
		sf_floatread(trace,nt,in);
		sf_stretch4_apply (false,mo,trace,trace2);
		sf_floatwrite (trace2,n1,out);
	    }
	} else {
	    if (SF_COMPLEX == sf_gettype(in)) {
		sf_complexread (ctrace2,n1,out);
		for (i=0; i < n1; i++) {
		    trace2[i] = crealf(ctrace2[i]);
		}
		sf_stretch4_invert (false,mo,trace,trace2);
		for (i=0; i < nt; i++) {
		    ctrace[i] = sf_cmplx(trace[i],0.0f);
		}
		for (i=0; i < n1; i++) {
		    trace2[i] = cimagf(ctrace2[i]);
		}
		sf_stretch4_invert (false,mo,trace,trace2);
		for (i=0; i < nt; i++) {
#ifdef SF_HAS_COMPLEX_H
		    ctrace[i] += sf_cmplx(0.0f,trace[i]);
#else
		    ctrace[i] = sf_cadd(ctrace[i],sf_cmplx(0.0f,trace[i]));
#endif
		}
		sf_complexwrite(ctrace,nt,in);
	    } else {
		sf_floatread(trace2,n1,in);
		sf_stretch4_invert (false,mo,trace,trace2);
		sf_floatwrite (trace,nt,out);
	    }
	}
    }

    exit(0);
}
Esempio n. 27
0
int main (int argc, char* argv[]) 
{
    int n1, n2, n3, n12, i1, i2, i3, j, m, *coor, xc, yc, nw, rect;
    bool boundary, verb, var;
    
    float *input, *output, *dd, *data, *weight;
    sf_file in, out, dip;
    
    sf_init (argc, argv); 
    in = sf_input("in");
    out = sf_output("out");
    dip = sf_input("dip");
    
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    n3 = sf_leftsize(in,2);
    n12 = n1*n2;
    /* get the trace length (n1) and the number of traces (n2) and n3*/
    
    if (!sf_getint("nw",&nw)) sf_error("nw needs integer input");
    /* data-window length (positive and odd integer) */

    if (0 == nw%2) nw++;
    m = (nw-1)/2;

    if (!sf_getint("rect",&rect)) rect=nw;
    /* Correlation window */
    if (0 == rect%2) rect++;

    if (rect > nw) rect=nw;

    if (!sf_getbool("boundary",&boundary)) boundary = true;
    /* if y, boundary is data, whereas zero*/

    if (!sf_getbool("var",&var)) var = false;
    /* if y, variance weights, whereas correlation weights*/

    if (!sf_getbool("verb",&verb)) verb = false;
    /* verbosity flag */
    
    input = sf_floatalloc(n12);
    output = sf_floatalloc(n12);
    dd = sf_floatalloc(n12);
    coor = sf_intalloc(nw*2);
    data = sf_floatalloc(nw);
    weight = sf_floatalloc(nw);

    for(i3=0; i3 < n3; i3++) {
	if (verb) sf_warning("3rd axis: %d of %d",i3+1,n3);
	sf_floatread(input,n12,in);
	sf_floatread(dd,n12,dip);

	for(i2=0; i2 < n2; i2++) {
	    if (verb && (10*i2)%n2==0) sf_warning("2nd axis: %d of %d",i2+1,n2);
	    for(i1=0; i1 < n1; i1++) {
		coor[0*nw+m] = i1;
		coor[1*nw+m] = i2;
		data[m] = input[coor[1*nw+m]*n1+coor[0*nw+m]];
		for(j=1; j < m+1; j++) {
		    xc = (int) coor[0*nw+j+m-1]+dd[coor[1*nw+j+m-1]*n1+coor[0*nw+j+m-1]]+0.5;
		    yc = coor[1*nw+j+m-1]+1;
		    if (xc < 0 || xc >= n1 || yc < 0 || yc >= n2) {
			coor[1*nw+j+m] = coor[1*nw+j+m-1];
			coor[0*nw+j+m] = coor[0*nw+j+m-1];
			if (boundary) {
			    data[j+m] = input[coor[1*nw+j+m]*n1+coor[0*nw+j+m]]; 
			} else {
			    data[j+m] = 0.;
			}
		    } else {
			coor[1*nw+j+m] = yc;
			coor[0*nw+j+m] = xc;
			data[j+m] = input[coor[1*nw+j+m]*n1+coor[0*nw+j+m]];
		    }
		}
		for(j=-1; j > (-1*(m+1)); j--) {
		    xc = (int) coor[0*nw+j+m+1]-dd[coor[1*nw+j+m+1]*n1+coor[0*nw+j+m+1]]+0.5;
		    yc = coor[1*nw+j+m+1]-1;
		    if (xc < 0 || xc >= n1 || yc < 0 || yc >= n2) {
			coor[1*nw+j+m] = coor[1*nw+j+m+1];
			coor[0*nw+j+m] = coor[0*nw+j+m+1];
			if (boundary) {
			    data[j+m] = input[coor[1*nw+j+m]*n1+coor[0*nw+j+m]]; 
			} else {
			    data[j+m] = 0.;
			}
		    } else {
			coor[1*nw+j+m] = yc;
			coor[0*nw+j+m] = xc;
			data[j+m] = input[coor[1*nw+j+m]*n1+coor[0*nw+j+m]];
		    }
		}
		if (var) {
		    lvweight(data,weight,nw,rect);
		} else {
		    cweight(data,weight,nw,rect);
		}
		output[i2*n1+i1] = wmedianfilter(data,weight,nw);
	    }
	}
	sf_floatwrite(output,n12,out);
    }
    exit (0);
}
Esempio n. 28
0
int main(int argc, char* argv[])
{
    int	ix, iz, jx, jz, ixf, izf,ixx, izz, i,j,im, jm,nx,nz,nxf,nzf,nxpad,nzpad,it,ii,jj;
    float   kxmax,kzmax;

    float   A, f0, t, t0, dx, dz, dxf, dzf, dt, dkx, dkz, dt2;
    int     mm, nvx, nvz, ns;
    int     hnkx, hnkz, nkx, nkz, nxz, nkxz;
    int     hnkx1, hnkz1, nkx1, nkz1;
    int     isx, isz, isxm, iszm; /*source location */

    int     itaper; /* tapering or not for spectrum of oprtator*/
    int     nstep;            /* every nstep in spatial grids to calculate filters sparsely*/

    float   *coeff_1dx, *coeff_1dz, *coeff_2dx, *coeff_2dz; /* finite-difference coefficient */

    float **apx, **apz, **apxx, **apzz;        /* polarization operator of P-wave for a location */
    float **apxs, **apzs, **apxxs, **apzzs;    /* polarization operator of SV-wave for a location */

    float ****ex, ****ez;                      /* operator for whole model for P-wave*/
    float ****exs, ****ezs;                    /* operator for whole model for SV-wave*/
    float **exx, **ezz;                        /* operator for constant model for P-wave*/
    float **exxs, **ezzs;                      /* operator for constant model for SV-wave*/

    float **vp0, **vs0, **epsi, **del;         /* velocity model */
    float **p1, **p2, **p3, **q1, **q2, **q3, **p3c, **q3c, **sum;  /* wavefield array */

    float *kx, *kz, *kkx, *kkz, *kx2, *kz2, **taper;

    clock_t t1, t2, t3, t4, t5;
    float   timespent;
    float   fx, fz;

    int     isep=1;
    int     ihomo=1;

    char    *tapertype;

    double  vp2, vs2, ep2, de2;

    sf_init(argc,argv);

    sf_file Fo1, Fo2, Fo3, Fo4, Fo5, Fo6, Fo7, Fo8, Fo9, Fo10, Fo11, Fo12;

    t1=clock();

    /*  wavelet parameter for source definition */
    f0=30.0;
    t0=0.04;
    A=1.0;

    /* time samping paramter */
    if (!sf_getint("ns",&ns)) ns=301;
    if (!sf_getfloat("dt",&dt)) dt=0.001;
    if (!sf_getint("isep",&isep)) isep=0;             /* if isep=1, separate wave-modes */
    if (!sf_getint("ihomo",&ihomo)) ihomo=0;          /* if ihomo=1, homogeneous medium */
    if (NULL== (tapertype=sf_getstring("tapertype"))) tapertype="D"; /* taper type*/
    if (!sf_getint("nstep",&nstep)) nstep=1; /* grid step to calculate operators: 1<=nstep<=5 */

    sf_warning("isep=%d",isep);
    sf_warning("ihomo=%d",ihomo);
    sf_warning("tapertype=%s",tapertype);
    sf_warning("nstep=%d",nstep);

    sf_warning("ns=%d dt=%f",ns,dt);
    sf_warning("read velocity model parameters");

    /* setup I/O files */
    sf_file Fvp0, Fvs0, Feps, Fdel;

    Fvp0 = sf_input ("in");  /* vp0 using standard input */
    Fvs0 = sf_input ("vs0");  /* vs0 */
    Feps = sf_input ("epsi");  /* epsi */
    Fdel = sf_input ("del");  /* delta */

    /* Read/Write axes */
    sf_axis az, ax;
    az = sf_iaxa(Fvp0,1);
    nvz = sf_n(az);
    dz = sf_d(az)*1000.0;
    ax = sf_iaxa(Fvp0,2);
    nvx = sf_n(ax);
    dx = sf_d(ax)*1000.0;
    fx=sf_o(ax)*1000.0;
    fz=sf_o(az)*1000.0;

    /* source definition */
    isx=nvx/2;
    isz=nvz/2;
    //isz=nvz*2/5;

    /* wave modeling space */
    nx=nvx;
    nz=nvz;
    nxpad=nx+2*m;
    nzpad=nz+2*m;

    sf_warning("fx=%f fz=%f dx=%f dz=%f",fx,fz,dx,dz);

    sf_warning("nx=%d nz=%d nxpad=%d nzpad=%d", nx,nz,nxpad,nzpad);

    vp0=sf_floatalloc2(nz,nx);
    vs0=sf_floatalloc2(nz,nx);
    epsi=sf_floatalloc2(nz,nx);
    del=sf_floatalloc2(nz,nx);

    nxz=nx*nz;
    mm=2*m+1;

    dt2=dt*dt;
    isxm=isx+m;  /* source's x location */
    iszm=isz+m;  /* source's z-location */

    /* read velocity model */
    sf_floatread(vp0[0],nxz,Fvp0);
    sf_floatread(vs0[0],nxz,Fvs0);
    sf_floatread(epsi[0],nxz,Feps);
    sf_floatread(del[0],nxz,Fdel);

    t2=clock();

    Fo1 = sf_output("out");      /* Elastic-wave x-component */
    Fo2 = sf_output("Elasticz"); /* Elastic-wave z-component */
    /* setup I/O files */
    puthead3(Fo1, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz/1000.0, fx/1000.0, 0.0);
    puthead3(Fo2, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz/1000.0, fx/1000.0, 0.0);
    /*****************************************************************************
     *  Calculating polarization operator for wave-mode separation
     * ***************************************************************************/
    if(isep==1)
    {
        sf_warning("==================================================");
        sf_warning("==      Calculating Polarization Operator       ==");
        sf_warning("==================================================");
        /* calculate spatial steps for operater in sparsely sampling grid point */
        dxf=dx*nstep;
        dzf=dz*nstep;
        nxf=nx/nstep+1;
        nzf=nz/nstep+1;

        /* operators length for calculation */
        hnkx=400.0/dx;
        hnkz=400.0/dz;
        nkx=2*hnkx+1;   /* operator length in kx-direction */
        nkz=2*hnkz+1;   /* operator length in kz-direction */

        /* truncated spatial operators length for filtering*/
        hnkx1=155.0/dx;
        hnkz1=155.0/dz;
        nkx1=2*hnkx1+1;
        nkz1=2*hnkz1+1;

        sf_warning("nx=%d nz=%d nxf=%d nzf=%d", nx,nz,nxf,nzf);
        sf_warning("dx=%f dz=%f dxf=%f dzf=%f", dx,dz,dxf,dzf);

        sf_warning("hnkx=%d hnkz=%d nkx=%d nkz=%d", hnkx, hnkz, nkx, nkz);
        sf_warning("hnkx1=%d hnkz1=%d nkx1=%d nkz1=%d", hnkx1, hnkz1, nkx1, nkz1);

        dkx=2*PI/dx/nkx;
        dkz=2*PI/dz/nkz;
        kxmax=PI/dx;
        kzmax=PI/dz;

        kx=sf_floatalloc(nkx);
        kz=sf_floatalloc(nkx);
        kkx=sf_floatalloc(nkx);
        kkz=sf_floatalloc(nkx);
        kx2=sf_floatalloc(nkx);
        kz2=sf_floatalloc(nkx);

        taper=sf_floatalloc2(nkz, nkx);

        // define axis samples and taper in wavenumber domain
        kxkztaper(kx, kz, kkx, kkz, kx2, kz2, taper, nkx, nkz, hnkx, hnkz, dkx, dkz, kxmax, kzmax, tapertype);

        nkxz=nkx*nkz;

        /* truncation of spatial filter */
        if(ihomo==1)
        {
            exx=sf_floatalloc2(nkz1, nkx1);
            ezz=sf_floatalloc2(nkz1, nkx1);
            exxs=sf_floatalloc2(nkz1, nkx1);
            ezzs=sf_floatalloc2(nkz1, nkx1);
        } else {
            ex=sf_floatalloc4(nkz1, nkx1, nzf, nxf);
            ez=sf_floatalloc4(nkz1, nkx1, nzf, nxf);
            exs=sf_floatalloc4(nkz1, nkx1, nzf, nxf);
            ezs=sf_floatalloc4(nkz1, nkx1, nzf, nxf);
        }
        /*****************************************************************************
        *  Calculating polarization operator for wave-mode separation
        * ***************************************************************************/
        apx=sf_floatalloc2(nkz, nkx);
        apz=sf_floatalloc2(nkz, nkx);

        apxs=sf_floatalloc2(nkz, nkx);
        apzs=sf_floatalloc2(nkz, nkx);

        apxx=sf_floatalloc2(nkz, nkx);
        apzz=sf_floatalloc2(nkz, nkx);

        apxxs=sf_floatalloc2(nkz, nkx);
        apzzs=sf_floatalloc2(nkz, nkx);

        /* setup I/O files for wavenumber-domain operators */
        Fo3 = sf_output("apx"); /*  P-wave's polarization x-comp */
        Fo4 = sf_output("apz"); /*  P-wave's polarization z-comp */
        Fo5 = sf_output("apxs"); /*  SV-wave's polarization x-comp */
        Fo6 = sf_output("apzs"); /*  SV-wave's polarization z-comp */

        puthead1(Fo3, nkz, nkx, dkz, -kzmax, dkx, -kxmax);
        puthead1(Fo4, nkz, nkx, dkz, -kzmax, dkx, -kxmax);
        puthead1(Fo5, nkz, nkx, dkz, -kzmax, dkx, -kxmax);
        puthead1(Fo6, nkz, nkx, dkz, -kzmax, dkx, -kxmax);

        /* setup I/O files for space-domain operators */
        Fo7 = sf_output("apxx");  /* P-wave's polarization x-comp in (x,z) domain */
        Fo8 = sf_output("apzz");  /* P-wave's polarization z-comp in (x,z) domain */
        Fo9 = sf_output("apxxs"); /* SV-wave's polarization x-comp in (x,z) domain */
        Fo10 = sf_output("apzzs"); /* SV-wave's polarization z-comp in (x,z) domain */

        puthead2(Fo7, nkz, nkx, dz/1000.0, 0.0, dx/1000.0, 0.0);
        puthead2(Fo8, nkz, nkx, dz/1000.0, 0.0, dx/1000.0, 0.0);
        puthead2(Fo9, nkz, nkx, dz/1000.0, 0.0, dx/1000.0, 0.0);
        puthead2(Fo10, nkz, nkx, dz/1000.0, 0.0, dx/1000.0, 0.0);

        /*************calculate projection deviation grid-point by grid-point **********/
        for(ix=0,ixf=0; ix<nx; ix+=nstep,ixf++)
        {
            for(iz=0,izf=0; iz<nz; iz+=nstep,izf++)
            {
                vp2=vp0[ix][iz]*vp0[ix][iz];
                vs2=vs0[ix][iz]*vs0[ix][iz];
                ep2=1.0+2*epsi[ix][iz];
                de2=1.0+2*del[ix][iz];

                if(ixf%10==0&&izf%100==0) sf_warning("Operator: nxf=%d ixf=%d izf=%d vp2=%f vs2=%f",nxf, ixf,izf,vp2,vs2);

                /*************calculate projection operrate with tapering **********/
                zero2float(apx, nkz, nkx);
                zero2float(apz, nkz, nkx);
                zero2float(apxs, nkz, nkx);
                zero2float(apzs, nkz, nkx);

                /* polvtipsv: P- and SV-wave polarization operators in VTI media  */
                itaper=1;
                polvtipsv(apx,apz,apxs,apzs,kx,kz,kkx,kkz,kx2,kz2,taper,hnkx,hnkz,dkx,dkz,
                          vp2,vs2,ep2,de2,itaper);

                ikxkz2xz(apx, apxx, hnkx, hnkz, nkx, nkz);
                ikxkz2xz(apz, apzz, hnkx, hnkz, nkx, nkz);
                ikxkz2xz(apxs, apxxs, hnkx, hnkz, nkx, nkz);
                ikxkz2xz(apzs, apzzs, hnkx, hnkz, nkx, nkz);

                // truncation and saving of operator in space-domain
                if(ihomo==1)
                {
                    for(jx=-hnkx1,ixx=hnkx-hnkx1; jx<=hnkx1; jx++,ixx++)
                        for(jz=-hnkz1,izz=hnkz-hnkz1; jz<=hnkz1; jz++,izz++)
                        {
                            exx[jx+hnkx1][jz+hnkz1]=apxx[ixx][izz];
                            ezz[jx+hnkx1][jz+hnkz1]=apzz[ixx][izz];
                            exxs[jx+hnkx1][jz+hnkz1]=apxxs[ixx][izz];
                            ezzs[jx+hnkx1][jz+hnkz1]=apzzs[ixx][izz];
                        }
                } else {
                    for(jx=-hnkx1,ixx=hnkx-hnkx1; jx<=hnkx1; jx++,ixx++)
                        for(jz=-hnkz1,izz=hnkz-hnkz1; jz<=hnkz1; jz++,izz++)
                        {
                            ex[ixf][izf][jx+hnkx1][jz+hnkz1]=apxx[ixx][izz];
                            ez[ixf][izf][jx+hnkx1][jz+hnkz1]=apzz[ixx][izz];
                            exs[ixf][izf][jx+hnkx1][jz+hnkz1]=apxxs[ixx][izz];
                            ezs[ixf][izf][jx+hnkx1][jz+hnkz1]=apzzs[ixx][izz];
                        }
                }

                if((ixf==nxf/2&&izf==nzf/2&&ihomo==0)||ihomo==1)
                {
                    //write-disk operators in kx-kz domain
                    sf_floatwrite(apx[0], nkxz, Fo3);
                    sf_floatwrite(apz[0], nkxz, Fo4);
                    sf_floatwrite(apxs[0], nkxz, Fo5);
                    sf_floatwrite(apzs[0], nkxz, Fo6);

                    //write-disk operators in x-z domain
                    sf_floatwrite(apxx[0], nkxz, Fo7);
                    sf_floatwrite(apzz[0], nkxz, Fo8);
                    sf_floatwrite(apxxs[0], nkxz, Fo9);
                    sf_floatwrite(apzzs[0], nkxz, Fo10);
                }
                if(ihomo==1) goto loop;
            }// iz loop
        }//ix loop
loop:
        ;

        free(kx);
        free(kz);
        free(kx2);
        free(kz2);
        free(kkx);
        free(kkz);

        free(*taper);

        free(*apx);
        free(*apz);
        free(*apxs);
        free(*apzs);
        free(*apxx);
        free(*apzz);
        free(*apxxs);
        free(*apzzs);
    }// isep loop
    /****************End of Calculating Projection Deviation Operator****************/
    t3=clock();
    timespent=(float)(t3-t2)/CLOCKS_PER_SEC;
    sf_warning("Computation time (operators): %f (second)",timespent);

    /****************begin to calculate wavefield****************/
    /****************begin to calculate wavefield****************/
    sf_warning("==================================================");
    sf_warning("==      Propagation Using Elastic Wave Eq.      ==");
    sf_warning("==================================================");

    coeff_2dx=sf_floatalloc(mm);
    coeff_2dz=sf_floatalloc(mm);
    coeff_1dx=sf_floatalloc(mm);
    coeff_1dz=sf_floatalloc(mm);

    coeff2d(coeff_2dx,dx);
    coeff2d(coeff_2dz,dz);

    p1=sf_floatalloc2(nzpad, nxpad);
    p2=sf_floatalloc2(nzpad, nxpad);
    p3=sf_floatalloc2(nzpad, nxpad);

    q1=sf_floatalloc2(nzpad, nxpad);
    q2=sf_floatalloc2(nzpad, nxpad);
    q3=sf_floatalloc2(nzpad, nxpad);

    zero2float(p1, nzpad, nxpad);
    zero2float(p2, nzpad, nxpad);
    zero2float(p3, nzpad, nxpad);

    zero2float(q1, nzpad, nxpad);
    zero2float(q2, nzpad, nxpad);
    zero2float(q3, nzpad, nxpad);

    coeff1dmix(coeff_1dx,dx);
    coeff1dmix(coeff_1dz,dz);

    if(isep==1)
    {
        Fo11 = sf_output("ElasticSepP"); /*  scalar wavefield using P-wave's polarization projection oprtator*/
        Fo12 = sf_output("ElasticSepSV"); /*  scalar wavefield using SV-wave's polarization projection oprtator*/

        puthead3(Fo11, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz/1000.0, fx/1000.0, 0.0);
        puthead3(Fo12, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz/1000.0, fx/1000.0, 0.0);

        p3c=sf_floatalloc2(nz,nx);
        q3c=sf_floatalloc2(nz,nx);
        sum=sf_floatalloc2(nz,nx);
    }

    for(it=0; it<ns; it++)
    {
        t=it*dt;

        // 2D exploding force source (e.g., Wu's PhD
        for(i=-1; i<=1; i++)
            for(j=-1; j<=1; j++)
            {
                if(fabs(i)+fabs(j)==2)
                {
                    p2[isxm+i][iszm+j]+=i*Ricker(t, f0, t0, A);
                    q2[isxm+i][iszm+j]+=j*Ricker(t, f0, t0, A);
                }
            }
        // 2D equil-energy force source (e.g., Wu's PhD)
        /*
        for(i=-1;i<=1;i++)
        for(j=-1;j<=1;j++)
        {
             if(fabs(i)+fabs(j)==2)
             {
                  if(i==-1&&j==1)
                    q2[isxm+i][iszm+j]+=sqrt(2.0)*Ricker(t, f0, t0, A);
                  if(i==-1&&j==-1)
                   p2[isxm+i][iszm+j]+=-sqrt(2.0)*Ricker(t, f0, t0, A);
                  if(i==1&&j==1)
                   p2[isxm+i][iszm+j]+=sqrt(2.0)*Ricker(t, f0, t0, A);
                  if(i==1&&j==-1)
                    q2[isxm+i][iszm+j]+=-sqrt(2.0)*Ricker(t, f0, t0, A);
             }
        }
        */
        /* fwpvtielastic: forward-propagating using original elastic equation of displacement in VTI media*/
        fwpvtielastic(dt2, p1, p2, p3, q1, q2, q3, coeff_2dx, coeff_2dz, coeff_1dx, coeff_1dz,
                      dx, dz, nx, nz, nxpad, nzpad, vp0, vs0, epsi, del);

        /******* output wavefields: component and divergence *******/

        if(it==ns-1)
        {
            for(i=0; i<nx; i++)
            {
                im=i+m;
                for(j=0; j<nz; j++)
                {
                    jm=j+m;
                    sf_floatwrite(&p3[im][jm],1,Fo1);
                    sf_floatwrite(&q3[im][jm],1,Fo2);
                }
            }/* i loop*/


            if(isep==1)
            {
                t4=clock();
                //////////////////////////////////////////////////////////////////////////////////////////
                /* applying P-wave polarization projection operator in spatial domain */
                zero2float(p3c,nz,nx);
                zero2float(q3c,nz,nx);
                zero2float(sum, nz, nx);

                if(ihomo==1)
                    filter2dsepglobal(p3, q3, p3c, q3c, exx, ezz, nx, nz, hnkx1, hnkz1);
                else
                    filter2dsep(p3, q3, p3c, q3c, ex, ez, nx, nz, nstep, hnkx1, hnkz1);

                for(i=0; i<nx; i++)
                    for(j=0; j<nz; j++)
                        sum[i][j]=p3c[i][j]+q3c[i][j];

                sf_floatwrite(sum[0],nx*nz, Fo11);

                //////////////////////////////////////////////////////////////////////////////////////////
                /* applying SV-wave polarization projection operator in spatial domain */
                zero2float(p3c,nz,nx);
                zero2float(q3c,nz,nx);
                zero2float(sum, nz, nx);

                if(ihomo==1)
                    filter2dsepglobal(p3, q3, p3c, q3c, exxs, ezzs, nx, nz, hnkx1, hnkz1);
                else
                    filter2dsep(p3, q3, p3c, q3c, exs, ezs, nx, nz, nstep, hnkx1, hnkz1);

                for(i=0; i<nx; i++)
                    for(j=0; j<nz; j++)
                        sum[i][j]=p3c[i][j]+q3c[i][j];

                sf_floatwrite(sum[0],nx*nz, Fo12);

                t5=clock();
                timespent=(float)(t5-t4)/CLOCKS_PER_SEC;
                sf_warning("Computation time (separation): %f (second)",timespent);
            }// isep==1

        }/* (it+1)%ntstep==0 */

        /**************************************/
        for(i=0,ii=m; i<nx; i++,ii++)
            for(j=0,jj=m; j<nz; j++,jj++)
            {
                p1[ii][jj]=p2[ii][jj];
                p2[ii][jj]=p3[ii][jj];

                q1[ii][jj]=q2[ii][jj];
                q2[ii][jj]=q3[ii][jj];
            }

        if(it%100==0)
            sf_warning("Elastic: it= %d",it);
    }/* it loop */
    timespent=(float)(t5-t3)/CLOCKS_PER_SEC;
    sf_warning("Computation time (propagation + separation): %f (second)",timespent);

    if(isep==1)
    {
        free(*p3c);
        free(*q3c);
        free(*sum);

        if(ihomo==1)
        {
            free(*exx);
            free(*ezz);
            free(*exxs);
            free(*ezzs);
        } else {
            free(***ex);
            free(***ez);
            free(***exs);
            free(***ezs);
        }
    }
    free(coeff_2dx);
    free(coeff_2dz);
    free(coeff_1dx);
    free(coeff_1dz);

    free(*p1);
    free(*p2);
    free(*p3);
    free(*q1);
    free(*q2);
    free(*q3);

    free(*vp0);
    free(*vs0);
    free(*epsi);
    free(*del);

    exit(0);
}
Esempio n. 29
0
int main(int argc, char* argv[])
{
    bool verb;
    int n[SF_MAX_DIM], n0[SF_MAX_DIM], rect[SF_MAX_DIM];
    int a[SF_MAX_DIM], center[SF_MAX_DIM], gap[SF_MAX_DIM];
    int ndim, dim, n123, n123s, i, ia, ns, i1, niter, na, i4, n4, *kk;
    float *d, *f, *dd;
    double mean;
    char *lagfile, key[6];
    sf_filter aa;
    sf_file in, filt, lag;
 
    sf_init(argc,argv);

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

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

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

    sf_putstring(filt,"lag",lagfile);

    ndim = sf_filedims(in,n);

    if (!sf_getint("dim",&dim)) dim=ndim; /* number of dimensions */

    n4 = sf_leftsize(in,dim);
    
    sf_putints (lag,"n",n,dim);

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

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

    if (!sf_getint("na",&na)) na=0;
    /* filter size */

    if (0 == na) {
	if (!sf_getints("gap",gap,dim)) {
	    for (i=0; i < dim; i++) {
		gap[i] = 0;
	    }
	}
	
	aa = createhelix(dim, n, center, gap, a); /* allocate PEF */
	
	for (i=0; i < dim; i++) {	    
	    n0[i] = n[i];
	}
    } else {
	aa =  sf_allocatehelix (na);
	if (!sf_getints ("lags", aa->lag, na)) sf_error("Need lags=");
	if (!sf_getints ("n", n0, dim)) {
	    for (i=0; i < dim; i++) {	    
		n0[i] = n[i];
	    }
	}
    }

    n123 = 1;
    for (i=0; i < dim; i++) {
	n123 *= n[i];
	
	snprintf(key,6,"rect%d",i+1);
	if (!sf_getint(key,rect+i)) rect[i]=1;
    }

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

    for (i1=0; i1 < n123; i1++) {
	kk[i1] = 1;
    }

    bound (dim, n0, n, a, aa);
    find_mask(n123, kk, aa);

    na = aa->nh;

    snprintf(key,3,"n%d",dim+1);
    sf_putint(filt,key,na);
    sf_shiftdim(in, filt, dim+1);    
    
    if (!sf_getint("niter",&niter)) niter=100;
    /* number of iterations */

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

    n123s = n123*na;
    
    d = sf_floatalloc(n123s);
    f = sf_floatalloc(n123s);
    
    sf_multidivn_init(na, dim, n123, n, rect, d, NULL, verb); 

    for (i4=0; i4 < n4; i4++) {

	sf_floatread(dd,n123,in);
	
	/* apply shifts: dd -> d */

	mean = 0.;
	for (i=ia=0; ia < na; ia++) {
	    ns = aa->lag[ia];
	    for (i1=0; i1 < n123; i1++,i++) {
		if (i1 < ns) {
		    d[i] = 0.0f;
		} else {
		    d[i] = dd[i1-ns];
		    mean += d[i]*d[i];
		}
	    }
	}

	if (mean == 0.) {
	    sf_floatwrite(d,n123s,filt);
	    continue;
	}
	
	mean = sqrt (n123s/mean);

	/* -> apply mask */
	
	for(i=0; i < n123s; i++) {
	    d[i] *= mean;
	}
	for(i1=0; i1 < n123; i1++) {
	    dd[i1] *= mean;
	}

	sf_multidivn (dd,f,niter);
	sf_floatwrite(f,n123s,filt);
	
    }
    
    exit(0);
}
Esempio n. 30
0
void gradient_standard(float *x, float *fcost, float *grad)
/*< standard velocity gradient >*/
{
	int ix, iz, is, ir, it, wit, iturn;
	int sx, rx;

	float temp, dmax;
	float **p0, **p1, **p2, **term, **tmparray, *rr, ***wave, **pp;
	float *sendbuf, *recvbuf;

	/* residual file */
	sf_file Fres;
	Fres=sf_output("Fres");
	sf_putint(Fres,"n1",nt);
	sf_putint(Fres,"n2",nr);

	/* initialize fcost */
	*fcost=0.;
	/* update velocity */
	pad2d(x, vv, nz, nx, nb);
	/* initialize gradient */
	memset(grad, 0., nzx*sizeof(float));
	/* initialize data misfit */
	swap=0.;

	/* memory allocation */
	p0=sf_floatalloc2(padnz, padnx);
	p1=sf_floatalloc2(padnz, padnx);
	p2=sf_floatalloc2(padnz, padnx);
	term=sf_floatalloc2(padnz, padnx);
	rr=sf_floatalloc(padnzx);
	wave=sf_floatalloc3(nz, nx, wnt);
	pp=sf_floatalloc2(nt, nr);

	iturn=0;
	for(is=cpuid; is<ns; is+=numprocs){
		if(cpuid==0) sf_warning("###### is=%d ######", is+1);

		memset(p0[0], 0., padnzx*sizeof(float));
		memset(p1[0], 0., padnzx*sizeof(float));
		memset(p2[0], 0., padnzx*sizeof(float));
		memset(pp[0], 0., nr*nt*sizeof(float));
		
		sx=s0_v+is*ds_v;
		source_map(sx, sz, frectx, frectz, padnx, padnz, padnzx, rr);

		wit=0;
		/* forward propagation */
		for(it=0; it<nt; it++){
			if(verb) sf_warning("Forward propagation is=%d; it=%d;", is+1, it);

			/* output predicted data */
			for(ir=0; ir<nr2[is]; ir++){
				rx=r0_v[is]+ir*dr_v;
				pp[r02[is]+ir][it]=p1[rx][rz];
			}

			/* save wavefield */
			if(it%interval==0){
#ifdef _OPENMP 
#pragma omp parallel for \
			private(ix,iz) \
			shared(wave,p1,wit,nb,nx,nz)
#endif
				for(ix=0; ix<nx; ix++)
					for(iz=0; iz<nz; iz++)
						wave[wit][ix][iz]=p1[ix+nb][iz+nb];
				wit++;
			}

			/* laplacian operator */
			laplace(p1, term, padnx, padnz, dx2, dz2);
			
			/* load source */
#ifdef _OPENMP 
#pragma omp parallel for \
			private(ix,iz) \
			shared(term,rr,padnx,padnz,ww,it)
#endif
			for(ix=0; ix<padnx; ix++){
				for(iz=0; iz<padnz; iz++){
					term[ix][iz] += rr[ix*padnz+iz]*ww[it];
				}
			}

			/* update */
#ifdef _OPENMP 
#pragma omp parallel for \
			private(ix,iz) \
			shared(p0,p1,p2,vv,term,padnx,padnz,dt2)
#endif
			for(ix=4; ix<padnx-4; ix++){
				for(iz=4; iz<padnz-4; iz++){
					p2[ix][iz]=2*p1[ix][iz]-p0[ix][iz]+vv[ix][iz]*vv[ix][iz]*dt2*term[ix][iz];
				}
			}
			
			/* swap wavefield pointer of different time steps */
			tmparray=p0; p0=p1; p1=p2; p2=tmparray;

			/* boundary condition */
			apply_sponge(p0, bc, padnx, padnz, nb);
			apply_sponge(p1, bc, padnx, padnz, nb);
		} // end of time loop

		/* check */
		if(wit != wnt) sf_error("Incorrect number of wavefield snapshots");
		wit--;
		
		/* calculate data residual and data misfit */
		for(ir=0; ir<nr; ir++){
			for(it=0; it<nt; it++){
				pp[ir][it]=dd[iturn][ir][it]-pp[ir][it];
				pp[ir][it] *= weight[ir][it];
				swap += 0.5*pp[ir][it]*pp[ir][it];
			}
		}
		smooth_misfit(pp, fcost, nr, nt, drectx, drectz, nrepeat, ider);

		iturn++;

		/* check the data residual */
		if(is==ns/2) sf_floatwrite(pp[0], nr*nt, Fres);
		sf_fileclose(Fres);

		/* initialization */
		memset(p0[0], 0., padnzx*sizeof(float));
		memset(p1[0], 0., padnzx*sizeof(float));
		memset(p2[0], 0., padnzx*sizeof(float));
		memset(term[0], 0., padnzx*sizeof(float));
		
		/* backward propagation */
		for(it=nt-1; it>=0; it--){
			if(verb) sf_warning("Backward propagation is=%d; it=%d;", is+1, it);
			
			/* laplacian operator */
			laplace(p1, term, padnx, padnz, dx2, dz2);
			
			/* load data residual */
			for(ir=0; ir<nr2[is]; ir++){
				rx=r0_v[is]+ir*dr_v;
				term[rx][rz] += pp[r02[is]+ir][it];
			}

			/* update */
#ifdef _OPENMP 
#pragma omp parallel for \
			private(ix,iz) \
			shared(p0,p1,p2,vv,term,padnx,padnz,dt2)
#endif
			for(ix=4; ix<padnx-4; ix++){
				for(iz=4; iz<padnz-4; iz++){
					p2[ix][iz]=2*p1[ix][iz]-p0[ix][iz]+vv[ix][iz]*vv[ix][iz]*dt2*term[ix][iz];
				}
			}
			
			/* calculate gradient  */
			if(it%interval==0){
				if(wit != wnt-1 && wit != 0){ // avoid the first and last time step
#ifdef _OPENMP 
#pragma omp parallel for \
			private(ix,iz,temp) \
			shared(nx,nz,vv,wave,p1,wit,wdt2,grad)
#endif
					for(ix=0; ix<nx; ix++){
						for(iz=0; iz<nz; iz++){
							temp=vv[ix+nb][iz+nb];
							temp=temp*temp*temp;
							temp=-2./temp;
							grad[ix*nz+iz] += (wave[wit+1][ix][iz]-2.*wave[wit][ix][iz]+wave[wit-1][ix][iz])/wdt2*p1[ix+nb][iz+nb]*temp;
						}
					}
				}
				wit--;
			}
			
			/* swap wavefield pointer of different time steps */
			tmparray=p0; p0=p1; p1=p2; p2=tmparray;

			/* boundary condition */
			apply_sponge(p0, bc, padnx, padnz, nb);
			apply_sponge(p1, bc, padnx, padnz, nb);
		} // end of time loop
	}// end of shot loop
	MPI_Barrier(comm);
	
	/* misfit reduction */
	//MPI_ALLreduce(sendbuf, recvbuf, 1, MPI_FLOAT, MPI_SUM, comm);
	
	if(cpuid==0){
		sendbuf=MPI_IN_PLACE;
		recvbuf=fcost;
	}else{
		sendbuf=fcost;
		recvbuf=NULL;
	}
	MPI_Reduce(sendbuf, recvbuf, 1, MPI_FLOAT, MPI_SUM, 0, comm);
	MPI_Bcast(fcost, 1, MPI_FLOAT, 0, comm);
	
	if(cpuid==0){
		sendbuf=MPI_IN_PLACE;
		recvbuf=&swap;
	}else{
		sendbuf=&swap;
		recvbuf=NULL;
	}
	MPI_Reduce(sendbuf, recvbuf, 1, MPI_FLOAT, MPI_SUM, 0, comm);
	MPI_Bcast(&swap, 1, MPI_FLOAT, 0, comm);

	/* gradient reduction */
	//MPI_ALLreduce(sendbuf, recvbuf, nzx, MPI_FLOAT, MPI_SUM, comm);
	if(cpuid==0){
		sendbuf=MPI_IN_PLACE;
		recvbuf=grad;
	}else{
		sendbuf=grad;
		recvbuf=NULL;
	}
	MPI_Reduce(sendbuf, recvbuf, nzx, MPI_FLOAT, MPI_SUM, 0, comm);
	MPI_Bcast(grad, nzx, MPI_FLOAT, 0, comm);

	/* scaling gradient */
	if(first){
		dmax=0.;
		for(ix=0; ix<nzx; ix++)
			if(fabsf(grad[ix])>dmax)
				dmax=fabsf(grad[ix]);
		scaling=0.1/dmax;
		first=false;
	}

	/* smooth gradient */
	gradient_smooth2(grectx, grectz, nx, nz, waterz, scaling, grad);

	/* free allocated memory */
	free(*p0); free(p0); free(*p1); free(p1);
	free(*p2); free(p2); free(*pp); free(pp);
	free(**wave); free(*wave); free(wave);
	free(rr); free(*term); free(term);
}