Beispiel #1
0
int disp_fileinfo(char *file, int n1, int n2, float f1, float f2, float d1, float d2, segy *hdrs)
{
	vmess("file %s contains", file);
	vmess("*** n1 = %d n2 = %d", n1, n2);
	vmess("*** d1 = %.5f d2 = %.5f", d1, d2);
	vmess("*** f1 = %.5f f2 = %.5f", f1, f2);
	vmess("*** fldr = %d sx = %d", hdrs[0].fldr, hdrs[0].sx);

	return 0;
}
void homogeneousg(float *HomG, complex *cshot, complex *Refl, int nx, int nt, int nxs, int nts, float dt, float *xsyn, int Nsyn, float *xrcv, float *xsrc, float fxs2, float fxs, float dxs, float dxsrc, float dx, int ixa, int ixb, int ntfft, int nw, int nw_low, int nw_high,  int mode, int reci, int nshots, int *ixpossyn, int npossyn, float *pmin, float *f1min, float *f1plus, float *f2p, float *G_d, int *muteW, int smooth, int shift, int above, int pad, int nt0, int *synpos, int verbose)
{
    int     i, j, l, ret;
    int     iter, niter, ix, nfreq;
	float   *iRN, *rtrace;
	complex	*Fop, *ctrace, *chom;
    double  t0, t2, tfft;

	tfft = 0.0;
	ret = 0;
    t0   = wallclock_time();
	nfreq = ntfft/2+1;

#pragma omp parallel default(shared) \
  private(i,j,ctrace,chom,rtrace) 
{	
	ctrace	= (complex *)calloc(nfreq,sizeof(complex));
	chom    = (complex *)calloc(nfreq,sizeof(complex));
	rtrace	= (float *)calloc(ntfft,sizeof(float));

#pragma omp for
	for (l = 0; l < Nsyn; l++) {

		if (verbose > 2) vmess("Creating Homogeneous G at location %d out of %d",l+1,Nsyn);

		/* Construct the image */
		for (i = 0; i < nxs; i++) {
			rc1fft(&f2p[l*nxs*ntfft+i*ntfft],ctrace,nt,-1);
            for (j = 0; j < nfreq; j++) {
				chom[j].r +=  2*(ctrace[j].r*cshot[i*nfreq+j].r - ctrace[j].i*cshot[i*nfreq+j].i);
            }
        }
		cr1fft(&chom[0],rtrace,nt,1);
		for (i = 0; i < ntfft; i++) {
            HomG[i*Nsyn+synpos[l]] = rtrace[i];
        }
		/*for (i = 0; i < ntfft/2; i++) {
			HomG[i*Nsyn+synpos[l]] = rtrace[ntfft/2+i];
		}
		for (i = ntfft/2; i < ntfft; i++) {
			HomG[i*Nsyn+synpos[l]] = rtrace[i-ntfft/2];
		}*/
	}
    free(rtrace);free(chom);free(ctrace);
}
		
	//free(Gmin);

    t2 = wallclock_time();
    if (verbose) {
        vmess("Total Homogeneous G time = %.3f", t2-t0);
    }

    return;
}
Beispiel #3
0
/* SaveError() takes printf style args and saves the result in LastError */
static void
SaveError(pTHXo_ char* pat, ...)
{
    va_list args;
    SV *msv;
    char *message;
    STRLEN len;

    /* This code is based on croak/warn, see mess() in util.c */

    va_start(args, pat);
    msv = vmess(pat, &args);
    va_end(args);

    message = SvPV(msv,len);
    len++;		/* include terminating null char */

    /* Allocate some memory for the error message */
    if (LastError)
        LastError = (char*)saferealloc(LastError, len) ;
    else
        LastError = (char *) safemalloc(len) ;

    /* Copy message into LastError (including terminating null char)	*/
    strncpy(LastError, message, len) ;
    DLDEBUG(2,PerlIO_printf(Perl_debug_log, "DynaLoader: stored error msg '%s'\n",LastError));
}
Beispiel #4
0
static void array_define_yrange(t_glist *x, t_floatarg ylo, t_floatarg yhi)
{
    t_glist *gl = (x->gl_list ? pd_checkglist(&x->gl_list->g_pd) : 0);
    if (gl && gl->gl_list && pd_class(&gl->gl_list->g_pd) == garray_class)
    {
        int n = garray_getarray((t_garray *)gl->gl_list)->a_n;
        vmess(&x->gl_list->g_pd, gensym("bounds"),
            "ffff", 0., yhi, (double)(n == 1 ? n : n-1), ylo);
        vmess(&x->gl_list->g_pd, gensym("xlabel"),
            "fff", ylo + glist_pixelstoy(gl, 2) - glist_pixelstoy(gl, 0),
                0., (float)(n-1));
        vmess(&x->gl_list->g_pd, gensym("ylabel"),
            "fff", glist_pixelstox(gl, 0) - glist_pixelstox(gl, 5), ylo, yhi);
    }
    else bug("array_define_yrange");
}
void threadAffinity(void)
{
  int thread;
  cpu_set_t coremask;
  char clbuf[7 * CPU_SETSIZE], hnbuf[64];
  char prefix[200];

  memset(clbuf, 0, sizeof(clbuf));
  memset(hnbuf, 0, sizeof(hnbuf));
  (void)gethostname(hnbuf, sizeof(hnbuf));

  strcpy(prefix,"Hello world from");

//  #pragma omp parallel private(thread, coremask, clbuf)
/* for use inside parallel region */
  #pragma omp critical
  {
#ifdef _OPENMP
    thread = omp_get_thread_num();
#else
    thread = 1;
#endif
    (void)sched_getaffinity(0, sizeof(coremask), &coremask);
    cpuset_to_cstr(&coremask, clbuf);
    vmess("%s thread %d, on %s. (core affinity = %s)", prefix, thread, hnbuf, clbuf);

  }
  return;
}
Beispiel #6
0
/* SaveError() takes printf style args and saves the result in dl_last_error */
static void
SaveError(pTHX_ char* pat, ...)
{
    dMY_CXT;
    va_list args;
    SV *msv;
    char *message;
    STRLEN len;

    /* This code is based on croak/warn, see mess() in util.c */

    va_start(args, pat);
    msv = vmess(pat, &args);
    va_end(args);

    message = SvPV(msv,len);
    len++;		/* include terminating null char */

    /* Copy message into dl_last_error (including terminating null char) */
    sv_setpvn(MY_CXT.x_dl_last_error, message, len) ;
    DLDEBUG(2,PerlIO_printf(Perl_debug_log, "DynaLoader: stored error msg '%s'\n",dl_last_error));
}
int main (int argc, char **argv)
{
    FILE    *fp_out, *fp_f1plus, *fp_f1min;
    FILE    *fp_gmin, *fp_gplus, *fp_f2, *fp_pmin;
    int     i, j, l, ret, nshots, Nsyn, nt, nx, nts, nxs, ngath;
    int     size, n1, n2, ntap, tap, di, ntraces, nb, ib;
    int     nw, nw_low, nw_high, nfreq, *xnx, *xnxsyn, *synpos;
    int     reci, mode, ixa, ixb, n2out, verbose, ntfft;
    int     iter, niter, niterh, tracf, *muteW, pad, nt0, ampest, *hmuteW, *hxnxsyn;
    int     hw, smooth, above, shift, *ixpossyn, npossyn, ix, first=1;
    float   fmin, fmax, *tapersh, *tapersy, fxf, dxf, fxs2, *xsrc, *xrcv, *zsyn, *zsrc, *xrcvsyn;
	float	*hzsyn, *hxsyn, *hxrcvsyn, *hG_d, xloc, zloc, *HomG;
    double  t0, t1, t2, t3, tsyn, tread, tfft, tcopy, energyNi, *J;
    float   d1, d2, f1, f2, fxs, ft, fx, *xsyn, dxsrc, Q, f0, *Costdet;
    float   *green, *f2p, *pmin, *G_d, dt, dx, dxs, scl, mem, *Image, *Image2;
    float   *f1plus, *f1min, *iRN, *Ni, *trace, *Gmin, *Gplus, *Gm0;
    float   xmin, xmax, weight, tsq, *Gd, *amp, bstart, bend, db, *bdet, bp, b, bmin;
    complex *Refl, *Fop, *cshot;
    char    *file_tinv, *file_shot, *file_green, *file_iter, *file_wav, *file_ray, *file_amp, *file_img, *file_cp, *file_rays, *file_amps;
    char    *file_f1plus, *file_f1min, *file_gmin, *file_gplus, *file_f2, *file_pmin, *wavtype, *wavtype2, *file_homg, *file_tinvs;
    segy    *hdrs_im, *hdrs_homg;
	WavePar WP,WPs;
	modPar mod;
    recPar rec;
    srcPar src;
    shotPar shot;
    rayPar ray;

    initargs(argc, argv);
    requestdoc(1);

    tsyn = tread = tfft = tcopy = 0.0;
    t0   = wallclock_time();

	if (!getparstring("file_img", &file_img)) file_img = "img.su";
	if (!getparstring("file_homg", &file_homg)) file_homg = NULL;
    if (!getparstring("file_shot", &file_shot)) file_shot = NULL;
    if (!getparstring("file_tinv", &file_tinv)) file_tinv = NULL;
	if (!getparstring("file_tinvs", &file_tinvs)) file_tinvs = NULL;
    if (!getparstring("file_f1plus", &file_f1plus)) file_f1plus = NULL;
    if (!getparstring("file_f1min", &file_f1min)) file_f1min = NULL;
    if (!getparstring("file_gplus", &file_gplus)) file_gplus = NULL;
    if (!getparstring("file_gmin", &file_gmin)) file_gmin = NULL;
    if (!getparstring("file_pplus", &file_f2)) file_f2 = NULL;
    if (!getparstring("file_f2", &file_f2)) file_f2 = NULL;
    if (!getparstring("file_pmin", &file_pmin)) file_pmin = NULL;
    if (!getparstring("file_iter", &file_iter)) file_iter = NULL;
	if (!getparstring("file_wav", &file_wav)) file_wav=NULL;
	if (!getparstring("file_ray", &file_ray)) file_ray=NULL;
	if (!getparstring("file_amp", &file_amp)) file_amp=NULL;
	if (!getparstring("file_rays", &file_rays)) file_rays=NULL;
    if (!getparstring("file_amps", &file_amps)) file_amps=NULL;
	if (!getparstring("file_cp", &file_cp)) file_cp = NULL;
    if (!getparint("verbose", &verbose)) verbose = 0;
    if (file_tinv == NULL && file_shot == NULL) 
        verr("file_tinv and file_shot cannot be both input pipe");
    if (!getparstring("file_green", &file_green)) {
        if (verbose) vwarn("parameter file_green not found, assume pipe");
        file_green = NULL;
    }
    if (!getparfloat("fmin", &fmin)) fmin = 0.0;
    if (!getparfloat("fmax", &fmax)) fmax = 70.0;
    if (!getparint("ixa", &ixa)) ixa = 0;
    if (!getparint("ixb", &ixb)) ixb = ixa;
//    if (!getparint("reci", &reci)) reci = 0;
	reci=0; // source-receiver reciprocity is not yet fully build into the code
    if (!getparfloat("weight", &weight)) weight = 1.0;
	if (!getparfloat("tsq", &tsq)) tsq = 0.0;
	if (!getparfloat("Q", &Q)) Q = 0.0;
	if (!getparfloat("f0", &f0)) f0 = 0.0;
    if (!getparint("tap", &tap)) tap = 0;
    if (!getparint("ntap", &ntap)) ntap = 0;
	if (!getparint("pad", &pad)) pad = 0;

    if(!getparint("hw", &hw)) hw = 15;
    if(!getparint("smooth", &smooth)) smooth = 5;
    if(!getparint("above", &above)) above = 0;
    if(!getparint("shift", &shift)) shift=12;
	if(!getparint("ampest", &ampest)) ampest=0;
	if(!getparint("nb", &nb)) nb=0;
	if (!getparfloat("bstart", &bstart)) bstart = 1.0;
    if (!getparfloat("bend", &bend)) bend = 1.0;

    if (reci && ntap) vwarn("tapering influences the reciprocal result");

	/* Reading in wavelet parameters */
    if(!getparfloat("fpw", &WP.fp)) WP.fp = -1.0;
    if(!getparfloat("fminw", &WP.fmin)) WP.fmin = 10.0;
    if(!getparfloat("flefw", &WP.flef)) WP.flef = 20.0;
    if(!getparfloat("frigw", &WP.frig)) WP.frig = 50.0;
    if(!getparfloat("fmaxw", &WP.fmax)) WP.fmax = 60.0;
    else WP.fp = -1;
    if(!getparfloat("dbw", &WP.db)) WP.db = -20.0;
    if(!getparfloat("t0w", &WP.t0)) WP.t0 = 0.0;
    if(!getparint("shiftw", &WP.shift)) WP.shift = 0;
    if(!getparint("invw", &WP.inv)) WP.inv = 0;
    if(!getparfloat("epsw", &WP.eps)) WP.eps = 1.0;
    if(!getparfloat("scalew", &WP.scale)) WP.scale = 1.0;
    if(!getparint("scfftw", &WP.scfft)) WP.scfft = 1;
    if(!getparint("cmw", &WP.cm)) WP.cm = 10;
    if(!getparint("cnw", &WP.cn)) WP.cn = 1;
	if(!getparint("wav", &WP.wav)) WP.wav = 0;
	if(!getparstring("file_wav", &WP.file_wav)) WP.file_wav=NULL;
    if(!getparstring("w", &wavtype)) strcpy(WP.w, "g2");
    else strcpy(WP.w, wavtype);

	if(!getparfloat("fpws", &WPs.fp)) WPs.fp = -1.0;
    if(!getparfloat("fminws", &WPs.fmin)) WPs.fmin = 10.0;
    if(!getparfloat("flefws", &WPs.flef)) WPs.flef = 20.0;
    if(!getparfloat("frigws", &WPs.frig)) WPs.frig = 50.0;
    if(!getparfloat("fmaxws", &WPs.fmax)) WPs.fmax = 60.0;
    else WPs.fp = -1;
    if(!getparfloat("dbw", &WPs.db)) WPs.db = -20.0;
    if(!getparfloat("t0ws", &WPs.t0)) WPs.t0 = 0.0;
    if(!getparint("shiftws", &WPs.shift)) WPs.shift = 0;
    if(!getparint("invws", &WPs.inv)) WPs.inv = 0;
    if(!getparfloat("epsws", &WPs.eps)) WPs.eps = 1.0;
    if(!getparfloat("scalews", &WPs.scale)) WPs.scale = 1.0;
    if(!getparint("scfftws", &WPs.scfft)) WPs.scfft = 1;
    if(!getparint("cmws", &WPs.cm)) WPs.cm = 10;
    if(!getparint("cnws", &WPs.cn)) WPs.cn = 1;
    if(!getparint("wavs", &WPs.wav)) WPs.wav = 0;
    if(!getparstring("file_wavs", &WPs.file_wav)) WPs.file_wav=NULL;
    if(!getparstring("ws", &wavtype2)) strcpy(WPs.w, "g2");
    else strcpy(WPs.w, wavtype2);
	if(!getparint("niter", &niter)) niter = 10;
	if(!getparint("niterh", &niterh)) niterh = niter;

/*================ Reading info about shot and initial operator sizes ================*/

    ngath = 0; /* setting ngath=0 scans all traces; n2 contains maximum traces/gather */
	if (file_ray!=NULL && file_tinv==NULL) {
		ret = getFileInfo(file_ray, &n2, &n1, &ngath, &d1, &d2, &f2, &f1, &xmin, &xmax, &scl, &ntraces);
		n1 = 1;
		ntraces = n2*ngath;
		scl = 0.0010;
		d1 = -1.0*xmin;
		xmin = -1.0*xmax;
		xmax = d1;
		WP.wav = 1;
        WP.xloc = -123456.0;
        WP.zloc = -123456.0;
		synpos = (int *)calloc(ngath,sizeof(int));
		shot.nz = 1;
		shot.nx = ngath;
		shot.n = shot.nx*shot.nz;
		for (l=0; l<shot.nz; l++) {
            for (j=0; j<shot.nx; j++) {
                synpos[l*shot.nx+j] = j*shot.nz+l;
            }
        }
	}
	else if (file_ray==NULL && file_tinv==NULL) {
		getParameters(&mod, &rec, &src, &shot, &ray, verbose);
		n1 = 1;
		n2 = rec.n;
		ngath = shot.n;
		d1 = mod.dt;
		d2 = (rec.x[1]-rec.x[0])*mod.dx;
		f1 = 0.0;
		f2 = mod.x0+rec.x[0]*mod.dx;
		xmin = mod.x0+rec.x[0]*mod.dx;
		xmax = mod.x0+rec.x[rec.n-1]*mod.dx;
		scl = 0.0010;
		ntraces = n2*ngath;
		WP.wav = 1;
		WP.xloc = -123456.0;
		WP.zloc = -123456.0;
		synpos = (int *)calloc(ngath,sizeof(int));
		for (l=0; l<shot.nz; l++) {
			for (j=0; j<shot.nx; j++) {
				synpos[l*shot.nx+j] = j*shot.nz+l;
			}
		}
	}
	else {
    	ret = getFileInfo(file_tinv, &n1, &n2, &ngath, &d1, &d2, &f1, &f2, &xmin, &xmax, &scl, &ntraces);
	}

    Nsyn = ngath;
    nxs = n2; 
    nts = n1;
	nt0 = n1;
    dxs = d2; 
    fxs = f2;

    ngath = 0; /* setting ngath=0 scans all traces; nx contains maximum traces/gather */
    ret = getFileInfo(file_shot, &nt, &nx, &ngath, &d1, &dx, &ft, &fx, &xmin, &xmax, &scl, &ntraces);
    nshots = ngath;
	assert (nxs >= nshots);

    if (!getparfloat("dt", &dt)) dt = d1;

    ntfft = optncr(MAX(nt+pad, nts+pad)); 
    nfreq = ntfft/2+1;
    nw_low = (int)MIN((fmin*ntfft*dt), nfreq-1);
    nw_low = MAX(nw_low, 1);
    nw_high = MIN((int)(fmax*ntfft*dt), nfreq-1);
    nw  = nw_high - nw_low + 1;
    scl   = 1.0/((float)ntfft);

	if (nb > 1) {
		db	= (bend-bstart)/((float)(nb-1));
	}
	else if (nb == 1) {
		db = 0;
		bend = bstart;
	}
    
/*================ Allocating all data arrays ================*/

    green   = (float *)calloc(Nsyn*nxs*ntfft,sizeof(float));
    f2p     = (float *)calloc(Nsyn*nxs*ntfft,sizeof(float));
    pmin    = (float *)calloc(Nsyn*nxs*ntfft,sizeof(float));
    f1plus  = (float *)calloc(Nsyn*nxs*ntfft,sizeof(float));
    f1min   = (float *)calloc(Nsyn*nxs*ntfft,sizeof(float));
    G_d     = (float *)calloc(Nsyn*nxs*ntfft,sizeof(float));
    muteW   = (int *)calloc(Nsyn*nxs,sizeof(int));
    trace   = (float *)malloc(ntfft*sizeof(float));
    ixpossyn = (int *)malloc(nxs*sizeof(int));
    xrcvsyn = (float *)calloc(Nsyn*nxs,sizeof(float));
    xsyn    = (float *)malloc(Nsyn*sizeof(float));
    zsyn    = (float *)malloc(Nsyn*sizeof(float));
    xnxsyn  = (int *)calloc(Nsyn,sizeof(int));
    tapersy = (float *)malloc(nxs*sizeof(float));

    Refl    = (complex *)malloc(nw*nx*nshots*sizeof(complex));
    tapersh = (float *)malloc(nx*sizeof(float));
    xsrc    = (float *)calloc(nshots,sizeof(float));
    zsrc    = (float *)calloc(nshots,sizeof(float));
    xrcv    = (float *)calloc(nshots*nx,sizeof(float));
    xnx     = (int *)calloc(nshots,sizeof(int));

/*================ Read and define mute window based on focusing operator(s) ================*/
/* G_d = p_0^+ = G_d (-t) ~ Tinv */

	WPs.nt = ntfft;
	WPs.dt = dt;
	WP.nt = ntfft;
	WP.dt = dt;

	if (file_ray!=NULL || file_cp!=NULL) {
		makeWindow(WP, file_ray, file_amp, dt, xrcvsyn, xsyn, zsyn, xnxsyn,
             Nsyn, nxs, ntfft, mode, muteW, G_d, hw, verbose);
	}
	else {
    	mode=-1; /* apply complex conjugate to read in data */
    	readTinvData(file_tinv, dt, xrcvsyn, xsyn, zsyn, xnxsyn, 
			 Nsyn, nxs, ntfft, mode, muteW, G_d, hw, verbose);
	}
	/* reading data added zero's to the number of time samples to be the same as ntfft */
    nts   = ntfft;
                         
	/* define tapers to taper edges of acquisition */
    if (tap == 1 || tap == 3) {
        for (j = 0; j < ntap; j++)
            tapersy[j] = (cos(PI*(j-ntap)/ntap)+1)/2.0;
        for (j = ntap; j < nxs-ntap; j++)
            tapersy[j] = 1.0;
        for (j = nxs-ntap; j < nxs; j++)
            tapersy[j] =(cos(PI*(j-(nxs-ntap))/ntap)+1)/2.0;
    }
    else {
        for (j = 0; j < nxs; j++) tapersy[j] = 1.0;
    }
    if (tap == 1 || tap == 3) {
        if (verbose) vmess("Taper for operator applied ntap=%d", ntap);
        for (l = 0; l < Nsyn; l++) {
            for (i = 0; i < nxs; i++) {
                for (j = 0; j < nts; j++) {
                    G_d[l*nxs*nts+i*nts+j] *= tapersy[i];
                }   
            }   
        }   
    }

	/* check consistency of header values */
    dxf = (xrcvsyn[nxs-1] - xrcvsyn[0])/(float)(nxs-1);
    if (NINT(dxs*1e3) != NINT(fabs(dxf)*1e3)) {
        vmess("dx in hdr.d1 (%.3f) and hdr.gx (%.3f) not equal",d2, dxf);
        if (dxf != 0) dxs = fabs(dxf);
        vmess("dx in operator => %f", dxs);
    }
    if (xrcvsyn[0] != 0 || xrcvsyn[1] != 0 ) fxs = xrcvsyn[0];
    fxs2 = fxs + (float)(nxs-1)*dxs;

/*================ Reading shot records ================*/

    mode=1;
    readShotData(file_shot, xrcv, xsrc, zsrc, xnx, Refl, nw, nw_low, ngath, nx, nx, ntfft, 
         mode, weight, tsq, Q, f0, verbose);

    tapersh = (float *)malloc(nx*sizeof(float));
    if (tap == 2 || tap == 3) {
        for (j = 0; j < ntap; j++)
            tapersh[j] = (cos(PI*(j-ntap)/ntap)+1)/2.0;
        for (j = ntap; j < nx-ntap; j++)
            tapersh[j] = 1.0;
        for (j = nx-ntap; j < nx; j++)
            tapersh[j] =(cos(PI*(j-(nx-ntap))/ntap)+1)/2.0;
    }
    else {
        for (j = 0; j < nx; j++) tapersh[j] = 1.0;
    }
    if (tap == 2 || tap == 3) {
        if (verbose) vmess("Taper for shots applied ntap=%d", ntap);
        for (l = 0; l < nshots; l++) {
            for (j = 1; j < nw; j++) {
                for (i = 0; i < nx; i++) {
                    Refl[l*nx*nw+j*nx+i].r *= tapersh[i];
                    Refl[l*nx*nw+j*nx+i].i *= tapersh[i];
                }   
            }   
        }
    }
    free(tapersh);

	/* check consistency of header values */
    fxf = xsrc[0];
    if (nx > 1) dxf = (xrcv[0] - xrcv[nx-1])/(float)(nx-1);
    else dxf = d2;
    if (NINT(dx*1e3) != NINT(fabs(dxf)*1e3)) {
        vmess("dx in hdr.d1 (%.3f) and hdr.gx (%.3f) not equal",dx, dxf);
        if (dxf != 0) dx = fabs(dxf);
        else verr("gx hdrs not set");
        vmess("dx used => %f", dx);
    }
    
    dxsrc = (float)xsrc[1] - xsrc[0];
    if (dxsrc == 0) {
        vwarn("sx hdrs are not filled in!!");
        dxsrc = dx;
    }

/*================ Check the size of the files ================*/

    if (NINT(dxsrc/dx)*dx != NINT(dxsrc)) {
        vwarn("source (%.2f) and receiver step (%.2f) don't match",dxsrc,dx);
        if (reci == 2) vwarn("step used from operator (%.2f) ",dxs);
    }
    di = NINT(dxf/dxs);
    if ((NINT(di*dxs) != NINT(dxf)) && verbose) 
        vwarn("dx in receiver (%.2f) and operator (%.2f) don't match",dx,dxs);
    if (nt != nts) 
        vmess("Time samples in shot (%d) and focusing operator (%d) are not equal",nt, nts);
    if (verbose) {
        vmess("Number of focusing operators   = %d", Nsyn);
        vmess("Number of receivers in focusop = %d", nxs);
        vmess("number of shots                = %d", nshots);
        vmess("number of receiver/shot        = %d", nx);
        vmess("first model position           = %.2f", fxs);
        vmess("last model position            = %.2f", fxs2);
        vmess("first source position fxf      = %.2f", fxf);
        vmess("source distance dxsrc          = %.2f", dxsrc);
        vmess("last source position           = %.2f", fxf+(nshots-1)*dxsrc);
        vmess("receiver distance     dxf      = %.2f", dxf);
        vmess("direction of increasing traces = %d", di);
        vmess("number of time samples (nt,nts) = %d (%d,%d)", ntfft, nt, nts);
        vmess("time sampling                  = %e ", dt);
		if (ampest > 0) 		vmess("Amplitude correction estimation is switched on");
		if (nb > 0)				vmess("Scaling estimation in %d step(s) from %.3f to %.3f (db=%.3f)",nb,bstart,bend,db);
        if (file_green != NULL) vmess("Green output file              = %s ", file_green);
        if (file_gmin != NULL)  vmess("Gmin output file               = %s ", file_gmin);
        if (file_gplus != NULL) vmess("Gplus output file              = %s ", file_gplus);
        if (file_pmin != NULL)  vmess("Pmin output file               = %s ", file_pmin);
        if (file_f2 != NULL)    vmess("f2 (=pplus) output file        = %s ", file_f2);
        if (file_f1min != NULL) vmess("f1min output file              = %s ", file_f1min);
        if (file_f1plus != NULL)vmess("f1plus output file             = %s ", file_f1plus);
        if (file_iter != NULL)  vmess("Iterations output file         = %s ", file_iter);
    }

/*================ initializations ================*/

    if (ixa || ixb) n2out = ixa + ixb + 1;
    else if (reci) n2out = nxs;
    else n2out = nshots;
    mem = Nsyn*n2out*ntfft*sizeof(float)/1048576.0;
    if (verbose) {
        vmess("number of output traces        = %d", n2out);
        vmess("number of output samples       = %d", ntfft);
        vmess("Size of output data/file       = %.1f MB", mem);
    }

    //memcpy(Ni, G_d, Nsyn*nxs*ntfft*sizeof(float));
    
	if (file_homg!=NULL) {
		hG_d     = (float *)calloc(nxs*ntfft,sizeof(float));
    	hmuteW   = (int *)calloc(nxs,sizeof(int));
		hxrcvsyn = (float *)calloc(nxs,sizeof(float));
		hxsyn 	 = (float *)calloc(1,sizeof(float));
		hzsyn    = (float *)calloc(1,sizeof(float));
		hxnxsyn  = (int *)calloc(1,sizeof(int));
		cshot 	 = (complex *)calloc(nxs*nfreq,sizeof(complex));

		if(!getparfloat("xloc", &WPs.xloc)) WPs.xloc = -123456.0;
    	if(!getparfloat("zloc", &WPs.zloc)) WPs.zloc = -123456.0;
		if (WPs.xloc == -123456.0 && WPs.zloc == -123456.0) file_cp = NULL;
		if (WPs.xloc == -123456.0) WPs.xloc = 0.0;
		if (WPs.zloc == -123456.0) WPs.zloc = 0.0;
		xloc = WPs.xloc;
		zloc = WPs.zloc;
		ngath = 1;

		if (file_rays!=NULL || file_cp!=NULL) {
			WPs.wav=1;
			makeWindow(WPs, file_rays, file_amps, dt, hxrcvsyn, hxsyn, hzsyn, hxnxsyn, ngath, nxs, ntfft, mode, hmuteW, hG_d, hw, verbose);
    	}
    	else {
        	mode=-1; /* apply complex conjugate to read in data */
        	readTinvData(file_tinvs, dt, hxrcvsyn, hxsyn, hzsyn, hxnxsyn,
            	ngath, nxs, ntfft, mode, hmuteW, hG_d, hw, verbose);
    	}

		WPs.xloc = -123456.0;
		WPs.zloc = -123456.0;

		if (tap == 1 || tap == 3) {
        	if (verbose) vmess("Taper for operator applied ntap=%d", ntap);
            for (i = 0; i < nxs; i++) {
                for (j = 0; j < nts; j++) {
                    hG_d[i*nts+j] *= tapersy[i];
                }
            }
        }

		ngath   = omp_get_max_threads();
		
		synthesisPosistions(nx, nt, nxs, nts, dt, hxsyn, 1, xrcv, xsrc, fxs2, fxs,
        	dxs, dxsrc, dx, ixa, ixb, reci, nshots, ixpossyn, &npossyn, verbose);

		iterations(Refl,nx,nt,nxs,nts,dt,hxsyn,1,xrcv,xsrc,fxs2,fxs,dxs,dxsrc,dx,ixa,ixb,
        	ntfft,nw,nw_low,nw_high,mode,reci,nshots,ixpossyn,npossyn,pmin,f1min,f1plus,
        	f2p,hG_d,hmuteW,smooth,shift,above,pad,nt0,&first,niterh,verbose);

		/* compute full Green's function G = int R * f2(t) + f2(-t) = Pplus + Pmin */
        for (i = 0; i < npossyn; i++) {
            j = 0;
            /* set green to zero if mute-window exceeds nt/2 */
            if (hmuteW[ixpossyn[i]] >= nts/2) {
                memset(&green[i*nts],0, sizeof(float)*nt);
                continue;
            }
            green[i*nts+j] = f2p[i*nts+j] + pmin[i*nts+j];
            for (j = 1; j < nts; j++) {
                green[i*nts+j] = f2p[i*nts+nts-j] + pmin[i*nts+j];
            }
        }

		applyMute(green, hmuteW, smooth, 4, 1, nxs, nts, ixpossyn, npossyn, shift, pad, nt0);

        omp_set_num_threads(ngath);

        /* Transform the green position to the frequency domain */
        /*for (i = 0; i < npossyn; i++) {
        	rc1fft(&green[i*nts],&cshot[i*nfreq],ntfft,-1);
    	}*/
		//free(hG_d);free(hmuteW);free(hxrcvsyn);
		free(hmuteW);free(hxrcvsyn);
		free(hxsyn);free(hzsyn);free(hxnxsyn);free(cshot);
	}

    /* dry-run of synthesis to get all x-positions calcalated by the integration */
    synthesisPosistions(nx, nt, nxs, nts, dt, xsyn, Nsyn, xrcv, xsrc, fxs2, fxs, 
        dxs, dxsrc, dx, ixa, ixb,  reci, nshots, ixpossyn, &npossyn, verbose);
    if (verbose) {
        vmess("synthesisPosistions: nshots=%d npossyn=%d", nshots, npossyn);
    }


    t1    = wallclock_time();
    tread = t1-t0;

	iterations(Refl,nx,nt,nxs,nts,dt,xsyn,Nsyn,xrcv,xsrc,fxs2,fxs,dxs,dxsrc,dx,ixa,ixb,
		ntfft,nw,nw_low,nw_high,mode,reci,nshots,ixpossyn,npossyn,pmin,f1min,f1plus,
		f2p,G_d,muteW,smooth,shift,above,pad,nt0,&first,niter,verbose);

	/*if (niter==0) {
		for (l = 0; l < Nsyn; l++) {
        	for (i = 0; i < npossyn; i++) {
            	j = 0;
                ix = ixpossyn[i];
                f2p[l*nxs*nts+i*nts+j] = G_d[l*nxs*nts+ix*nts+j];
				f1plus[l*nxs*nts+i*nts+j] = G_d[l*nxs*nts+ix*nts+j];
                for (j = 1; j < nts; j++) {
                	f2p[l*nxs*nts+i*nts+j] = G_d[l*nxs*nts+ix*nts+j];
					f1plus[l*nxs*nts+i*nts+j] = G_d[l*nxs*nts+ix*nts+j];
                }
            }
    	}
	}*/

	

	if (niterh==0) {
        for (l = 0; l < Nsyn; l++) {
            for (i = 0; i < npossyn; i++) {
                j = 0;
                ix = ixpossyn[i];
                green[i*nts+j] = hG_d[ix*nts+j];
                for (j = 1; j < nts; j++) {
                    green[i*nts+j] = hG_d[ix*nts+nts-j];
                }
            }
        }
    }

	if (file_img!=NULL) {
	
		/*================ set variables for output data ================*/

    	hdrs_im = (segy *) calloc(shot.nx,sizeof(segy));
    	if (hdrs_im == NULL) verr("allocation for hdrs_out");
		Image   = (float *)calloc(Nsyn,sizeof(float));

		first=0;
		imaging(Image,WPs,Refl,nx,nt,nxs,nts,dt,xsyn,Nsyn,xrcv,xsrc,fxs2,fxs,dxs,dxsrc,dx,ixa,ixb,
       		ntfft,nw,nw_low,nw_high,mode,reci,nshots,ixpossyn,npossyn,pmin,f1min,f1plus,
       		f2p,G_d,muteW,smooth,shift,above,pad,nt0,synpos,verbose);

		/*============= write output files ================*/

		fp_out = fopen(file_img, "w+");

    	for (i = 0; i < shot.nx; i++) {
            hdrs_im[i].fldr    = 1;
            hdrs_im[i].tracl   = 1;
            hdrs_im[i].tracf   = i+1;
            hdrs_im[i].scalco  = -1000;
            hdrs_im[i].scalel  = -1000;
            hdrs_im[i].sdepth  = 0;
            hdrs_im[i].trid    = 1;
            hdrs_im[i].ns      = shot.nz;
            hdrs_im[i].trwf    = shot.nx;
            hdrs_im[i].ntr     = hdrs_im[i].fldr*hdrs_im[i].trwf;
            hdrs_im[i].f1      = zsyn[0];
            hdrs_im[i].f2      = xsyn[0];
            hdrs_im[i].dt      = dt*(1E6);
            hdrs_im[i].d1      = (float)zsyn[shot.nx]-zsyn[0];
            hdrs_im[i].d2      = (float)xsyn[1]-xsyn[0];
            hdrs_im[i].sx      = (int)roundf(xsyn[0] + (i*hdrs_im[i].d2));
            hdrs_im[i].gx      = (int)roundf(xsyn[0] + (i*hdrs_im[i].d2));
            hdrs_im[i].offset  = (hdrs_im[i].gx - hdrs_im[i].sx)/1000.0;
    	}
    	ret = writeData(fp_out, &Image[0], hdrs_im, shot.nz, shot.nx);
    	if (ret < 0 ) verr("error on writing output file.");

    	fclose(fp_out);
	}

	if (file_homg!=NULL) {

		/*================ set variables for output data ================*/

        hdrs_homg = (segy *) calloc(shot.nx,sizeof(segy));
        if (hdrs_homg == NULL) verr("allocation for hdrs_out");
        HomG	= (float *)calloc(Nsyn*ntfft,sizeof(float));

        homogeneousg(HomG,green,Refl,nx,nt,nxs,nts,dt,xsyn,Nsyn,xrcv,xsrc,fxs2,fxs,dxs,dxsrc,dx,ixa,ixb,
           	ntfft,nw,nw_low,nw_high,mode,reci,nshots,ixpossyn,npossyn,pmin,f1min,f1plus,
           	f2p,G_d,muteW,smooth,shift,above,pad,nt0,synpos,verbose);

        /*============= write output files ================*/

		 fp_out = fopen(file_homg, "w+");

		for (j = 0; j < ntfft; j++) {
        	for (i = 0; i < shot.nx; i++) {
            	hdrs_homg[i].fldr    = j+1;
            	hdrs_homg[i].tracl   = j*shot.nx+i+1;
            	hdrs_homg[i].tracf   = i+1;
            	hdrs_homg[i].scalco  = -1000;
            	hdrs_homg[i].scalel  = -1000;
            	hdrs_homg[i].sdepth  = (int)(zloc*1000.0);
            	hdrs_homg[i].trid    = 1;
            	hdrs_homg[i].ns      = shot.nz;
            	hdrs_homg[i].trwf    = shot.nx;
            	hdrs_homg[i].ntr     = hdrs_homg[i].fldr*hdrs_homg[i].trwf;
            	hdrs_homg[i].f1      = zsyn[0];
            	hdrs_homg[i].f2      = xsyn[0];
            	hdrs_homg[i].dt      = dt*(1E6);
            	hdrs_homg[i].d1      = (float)zsyn[shot.nx]-zsyn[0];
            	hdrs_homg[i].d2      = (float)xsyn[1]-xsyn[0];
            	hdrs_homg[i].sx      = (int)roundf(xsyn[0] + (i*hdrs_homg[i].d2));
            	hdrs_homg[i].gx      = (int)roundf(xsyn[0] + (i*hdrs_homg[i].d2));
            	hdrs_homg[i].offset  = (hdrs_homg[i].gx - hdrs_homg[i].sx)/1000.0;
        	}
        	ret = writeData(fp_out, &HomG[j*shot.n], hdrs_homg, shot.nz, shot.nx);
        	if (ret < 0 ) verr("error on writing output file.");
		}

        fclose(fp_out);
    }

    if (verbose) {
        t1 = wallclock_time();
        vmess("and CPU-time write data  = %.3f", t1-t2);
    }


    free(tapersy);

    exit(0);
}
Beispiel #8
0
void synthesis(complex *Refl, complex *Fop, float *Top, float *iRN, int nx, int nt, int nxs, int nts, float dt, float *xsyn, int
Nfoc, float *xrcv, float *xsrc, int *xnx, float fxse, float fxsb, float dxs, float dxsrc, float dx, int ntfft, int
nw, int nw_low, int nw_high,  int mode, int reci, int nshots, int *ixpos, int npos, double *tfft, int *isxcount, int
*reci_xsrc,  int *reci_xrcv, float *ixmask, int verbose)
{
    int     nfreq, size, inx;
    float   scl;
    int     i, j, l, m, iw, ix, k, ixsrc, il, ik;
    float   *rtrace, idxs;
    complex *sum, *ctrace;
    int     npe;
    static int first=1, *ixrcv;
    static double t0, t1, t;

    size  = nxs*nts;
    nfreq = ntfft/2+1;
    /* scale factor 1/N for backward FFT,
     * scale dt for correlation/convolution along time, 
     * scale dx (or dxsrc) for integration over receiver (or shot) coordinates */
    scl   = 1.0*dt/((float)ntfft);

#ifdef _OPENMP
    npe   = omp_get_max_threads();
    /* parallelisation is over number of shot positions (nshots) */
    if (npe > nshots) {
        vmess("Number of OpenMP threads set to %d (was %d)", nshots, npe);
        omp_set_num_threads(nshots);
    }
#endif

    t0 = wallclock_time();

    /* reset output data to zero */
    memset(&iRN[0], 0, Nfoc*nxs*nts*sizeof(float));
    ctrace = (complex *)calloc(ntfft,sizeof(complex));

/* this first check is done to support an acquisition geometry that has more receiver than source
 * postions. In the first iteration the int R(x_r,x_s) Fop(x_r) d x_r results in a grid on x_s. 
 * so for the next interations onlt x_s traces have to be computed on Fop */
    if (!first) {
    /* transform muted Ni (Top) to frequency domain, input for next iteration  */
        for (l = 0; l < Nfoc; l++) {
            /* set Fop to zero, so new operator can be defined within ixpos points */
            memset(&Fop[l*nxs*nw].r, 0, nxs*nw*2*sizeof(float));
            for (i = 0; i < npos; i++) {
                rc1fft(&Top[l*size+i*nts],ctrace,ntfft,-1);
                ix = ixpos[i];
                for (iw=0; iw<nw; iw++) {
                    Fop[l*nxs*nw+iw*nxs+ix].r = ctrace[nw_low+iw].r;
                    Fop[l*nxs*nw+iw*nxs+ix].i = mode*ctrace[nw_low+iw].i;
                }
            }
        }
    }
    else { /* only for first call to synthesis using all nxs traces in G_d */
    /* transform G_d to frequency domain, over all nxs traces */
        first=0;
        for (l = 0; l < Nfoc; l++) {
            /* set Fop to zero, so new operator can be defined within all ix points */
            memset(&Fop[l*nxs*nw].r, 0, nxs*nw*2*sizeof(float));
            for (i = 0; i < nxs; i++) {
                rc1fft(&Top[l*size+i*nts],ctrace,ntfft,-1);
                for (iw=0; iw<nw; iw++) {
                    Fop[l*nxs*nw+iw*nxs+i].r = ctrace[nw_low+iw].r;
                    Fop[l*nxs*nw+iw*nxs+i].i = mode*ctrace[nw_low+iw].i;
                }
            }
        }
        idxs = 1.0/dxs;
        ixrcv = (int *)malloc(nshots*nx*sizeof(int));
        for (k=0; k<nshots; k++) {
            for (i = 0; i < nx; i++) {
                ixrcv[k*nx+i] = NINT((xrcv[k*nx+i]-fxsb)*idxs);
            }
        }
    }
    free(ctrace);
    t1 = wallclock_time();
    *tfft += t1 - t0;

    if (reci == 0 || reci == 1) {

/*================ SYNTHESIS ================*/

#pragma omp parallel default(none) \
 shared(iRN, dx, npe, nw, verbose, nshots, xnx) \
 shared(Refl, Nfoc, reci, xsrc, xsyn, fxsb, fxse, nxs, dxs) \
 shared(nx, dxsrc, nfreq, nw_low, nw_high) \
 shared(Fop, size, nts, ntfft, scl, ixrcv) \
 private(l, ix, j, m, i, sum, rtrace, k, ixsrc, inx)
{ /* start of parallel region */
        sum   = (complex *)malloc(nfreq*sizeof(complex));
        rtrace = (float *)calloc(ntfft,sizeof(float));

/* Loop over total number of shots */
#pragma omp for schedule(guided,1)
        for (k=0; k<nshots; k++) {
            if ((xsrc[k] < 0.999*fxsb) || (xsrc[k] > 1.001*fxse)) continue;
            ixsrc = NINT((xsrc[k] - fxsb)/dxs);
            inx = xnx[k]; /* number of traces per shot */

            for (l = 0; l < Nfoc; l++) {
                /* compute integral over receiver positions */
                /* multiply R with Fop and sum over nx */
                memset(&sum[0].r,0,nfreq*2*sizeof(float));
                for (j = nw_low, m = 0; j <= nw_high; j++, m++) {
                    for (i = 0; i < inx; i++) {
                        ix = ixrcv[k*nx+i];
                        sum[j].r += Refl[k*nw*nx+m*nx+i].r*Fop[l*nw*nxs+m*nxs+ix].r -
                                    Refl[k*nw*nx+m*nx+i].i*Fop[l*nw*nxs+m*nxs+ix].i;
                        sum[j].i += Refl[k*nw*nx+m*nx+i].i*Fop[l*nw*nxs+m*nxs+ix].r +
                                    Refl[k*nw*nx+m*nx+i].r*Fop[l*nw*nxs+m*nxs+ix].i;
                    }
                }

                /* transfrom result back to time domain */
                cr1fft(sum, rtrace, ntfft, 1);

                /* place result at source position ixsrc; dx = receiver distance */
                for (j = 0; j < nts; j++) 
                    iRN[l*size+ixsrc*nts+j] += rtrace[j]*scl*dx;
            
            } /* end of parallel Nfoc loop */

            if (verbose>4) vmess("*** Shot gather %d processed ***", k);

        } /* end of nshots (k) loop */
        free(sum);
        free(rtrace);

} /* end of parallel region */


    }     /* end of if reci */

/* if reciprocal traces are enabled start a new loop over reciprocal shot positions */
    if (reci != 0) {

#pragma omp parallel default(none) \
 shared(iRN, dx, nw, verbose) \
 shared(Refl, Nfoc, reci, xsrc, xsyn, fxsb, fxse, nxs, dxs) \
 shared(nx, dxsrc, nfreq, nw_low, nw_high) \
 shared(reci_xrcv, reci_xsrc, ixmask, isxcount) \
 shared(Fop, size, nts, ntfft, scl, ixrcv) \
 private(l, ix, j, m, i, k, sum, rtrace, ik, il, ixsrc, inx)
{ /* start of parallel region */
        sum   = (complex *)malloc(nfreq*sizeof(complex));
        rtrace = (float *)calloc(ntfft,sizeof(float));

#pragma omp for schedule(guided,1)
        for (k=0; k<nxs; k++) {
            if (isxcount[k] == 0) continue;
            ixsrc = k;
            inx = isxcount[ixsrc]; /* number of traces per reciprocal shot */

            for (l = 0; l < Nfoc; l++) {
                /* compute integral over (reciprocal) source positions */
                /* multiply R with Fop and sum over nx */
                memset(&sum[0].r,0,nfreq*2*sizeof(float));
                for (j = nw_low, m = 0; j <= nw_high; j++, m++) {
                    for (i = 0; i < inx; i++) {
                        il = reci_xrcv[ixsrc*nxs+i];
                        ik = reci_xsrc[ixsrc*nxs+i];
                        ix = NINT((xsrc[il] - fxsb)/dxs);
                        sum[j].r += Refl[il*nw*nx+m*nx+ik].r*Fop[l*nw*nxs+m*nxs+ix].r -
                                    Refl[il*nw*nx+m*nx+ik].i*Fop[l*nw*nxs+m*nxs+ix].i;
                        sum[j].i += Refl[il*nw*nx+m*nx+ik].i*Fop[l*nw*nxs+m*nxs+ix].r +
                                    Refl[il*nw*nx+m*nx+ik].r*Fop[l*nw*nxs+m*nxs+ix].i;
                    }
                }

                /* transfrom result back to time domain */
                cr1fft(sum, rtrace, ntfft, 1);

                /* place result at source position ixsrc; dxsrc = shot distance */
                for (j = 0; j < nts; j++) 
                    iRN[l*size+ixsrc*nts+j] = ixmask[ixsrc]*(iRN[l*size+ixsrc*nts+j]+rtrace[j]*scl*dxsrc);
                
            } /* end of Nfoc loop */

        } /* end of parallel reciprocal shots (k) loop */
        free(sum);
        free(rtrace);

 } /* end of parallel region */

    } /* end of if reci */

    t = wallclock_time() - t0;
    if (verbose>2) {
        vmess("OMP: parallel region = %f seconds (%d threads)", t, npe);
    }

    return;
}
Beispiel #9
0
void synthesisPositions(int nx, int nt, int nxs, int nts, float dt, float *xsyn, int Nfoc, float *xrcv, float *xsrc, int *xnx,
float fxse, float fxsb, float dxs, float dxsrc, float dx, int nshots, int *ixpos, int *npos, int *isxcount, int countmin, int reci, int verbose)
{
    int     i, j, l, ixsrc, ixrcv, dosrc, k, *count;
    float   x0, x1;

    count   = (int *)calloc(nxs,sizeof(int)); // number of traces that contribute to the integration over x

/*================ SYNTHESIS ================*/

    /* assuming all focal operators cover the same lateral area */
    *npos=0;

    if (reci == 0 || reci == 1) {
        for (k=0; k<nshots; k++) {

            ixsrc = NINT((xsrc[k] - fxsb)/dxs);
            if (verbose>=3) {
                vmess("source position:     %.2f in operator %d", xsrc[k], ixsrc);
                vmess("receiver positions:  %.2f <--> %.2f", xrcv[k*nx+0], xrcv[k*nx+nx-1]);
                vmess("focal point positions:  %.2f <--> %.2f", fxsb, fxse);
            }
    
            if ((NINT(xsrc[k]-fxse) > 0) || (NINT(xrcv[k*nx+nx-1]-fxse) > 0) ||
                (NINT(xrcv[k*nx+nx-1]-fxsb) < 0) || (NINT(xsrc[k]-fxsb) < 0) || 
                (NINT(xrcv[k*nx+0]-fxsb) < 0) || (NINT(xrcv[k*nx+0]-fxse) > 0) ) {
                vwarn("source/receiver positions are outside synthesis aperture");
                vmess("xsrc = %.2f xrcv_1 = %.2f xrvc_N = %.2f", xsrc[k], xrcv[k*nx+0], xrcv[k*nx+nx-1]);
                vmess("source position:     %.2f in operator %d", xsrc[k], ixsrc);
                vmess("receiver positions:  %.2f <--> %.2f", xrcv[k*nx+0], xrcv[k*nx+nx-1]);
                vmess("focal point positions:  %.2f <--> %.2f", fxsb, fxse);
            }
    
			//fprintf(stderr,"k=%d xsrc[k]=%f 0.999*fxsb=%f, 1.001*fxse=%f %f %f\n",k, xsrc[k], 0.999*fxsb, 1.001*fxse, fxsb, fxse);
            //if ( (xsrc[k] >= 0.999*fxsb) && (xsrc[k] <= 1.001*fxse) ) {
            if ( (ixsrc < nxs) && (ixsrc >= 0) ) {
                j = linearsearch(ixpos, *npos, ixsrc);
                if (j < *npos) { /* the position (at j) is already included */
                    count[j] += xnx[k];
                }
                else { /* add new postion */
                    ixpos[*npos]=ixsrc;
                    count[*npos] += xnx[k];
                    *npos += 1;
                }
                if (verbose>=3) {
                    vmess("source position %d is inside synthesis model %f *npos=%d count=%d", k, xsrc[k], *npos, count[*npos]);
                    vmess("ixpos[%d] = %d ixsrc=%d", *npos-1, ixpos[*npos-1], ixsrc);
                }
            }
            else {
                if (verbose>=2) {
                    vwarn("source position %d is outside synthesis model %f ixsrc=%d", k, xsrc[k], ixsrc);
                }
           }

        } /* end of nshots (k) loop */
    } /* end of reci branch */

    /* if reci=1 or reci=2 source-receive reciprocity is used and new (reciprocal-)sources are added */
    if (reci != 0) {
        for (k=0; k<nxs; k++) { /* check count in total number of shots added by reciprocity */
            if (isxcount[k] >= countmin) {
                j = linearsearch(ixpos, *npos, k);
                if (j < *npos) { /* the position (at j) is already included */
                    count[j] += isxcount[k];
                }
                else { /* add new postion */
                    ixpos[*npos]=k;
                    count[*npos] += isxcount[k];
                       *npos += 1;
                }
            }
            else {
                isxcount[k] = 0;
            }
        }
    } /* end of reci branch */

    if (verbose>=4) {
        for (j=0; j < *npos; j++) { 
            vmess("ixpos[%d] = %d count=%d", j, ixpos[j], count[j]);
        }
    }
    free(count);

/* sort ixpos into increasing values */
    qsort(ixpos, *npos, sizeof(int), compareInt);

    return;
}
void synthesisPositions3D(int nx, int ny, int nxs, int nys, int Nfoc, float *xrcv, float *yrcv,
float *xsrc, float *ysrc, int *xnx, float fxse, float fyse, float fxsb, float fysb, float dxs, float dys,
int nshots, int nxsrc, int nysrc, int *ixpos, int *npos, int reci, int verbose)
{
    int     j, l, ixsrc, iysrc, isrc, k, *count, nxy;
    float   fxb, fxe, fyb, fye;

    if (fxsb < 0) fxb = 1.001*fxsb;
    else          fxb = 0.999*fxsb;
    if (fysb < 0) fyb = 1.001*fysb;
    else          fyb = 0.999*fysb;
    if (fxse > 0) fxe = 1.001*fxse;
    else          fxe = 0.999*fxse;
    if (fyse > 0) fye = 1.001*fyse;
    else          fye = 0.999*fyse;

    nxy = nx*ny;

    count   = (int *)calloc(nxs*nys,sizeof(int)); // number of traces that contribute to the integration over x

/*================ SYNTHESIS ================*/

    for (l = 0; l < 1; l++) { /* assuming all focal operators cover the same lateral area */
//    for (l = 0; l < Nfoc; l++) {
        *npos=0;

        if (reci == 0 || reci == 1) {
            for (k=0; k<nshots; k++) {

                ixsrc = NINT((xsrc[k] - fxsb)/dxs);
                iysrc = NINT((ysrc[k] - fysb)/dys);
                isrc  = iysrc*nxs + ixsrc;
                if (verbose>=3) {
                    vmess("source position:         x=%.2f y=%.2f in operator x=%d y=%d pos=%d", xsrc[k], ysrc[k], ixsrc, iysrc, isrc);
                    vmess("receiver positions:      x:%.2f <--> %.2f y:%.2f <--> %.2f", xrcv[k*nxy+0], xrcv[k*nxy+nxy-1], yrcv[k*nxy+0], yrcv[k*nxy+nxy-1]);
                    vmess("focal point positions:   x:%.2f <--> %.2f y:%.2f <--> %.2f", fxsb, fxse, fysb, fyse);
                }
        
                if ((NINT(xsrc[k]-fxse) > 0)           || (NINT(xrcv[k*nxy+nxy-1]-fxse) > 0) ||
                    (NINT(xrcv[k*nxy+nxy-1]-fxsb) < 0) || (NINT(xsrc[k]-fxsb) < 0)           || 
                    (NINT(xrcv[k*nxy+0]-fxsb) < 0)     || (NINT(xrcv[k*nxy+0]-fxse) > 0)     || 
                    (NINT(ysrc[k]-fyse) > 0)           || (NINT(yrcv[k*nxy+nxy-1]-fyse) > 0) ||
                    (NINT(yrcv[k*nxy+nxy-1]-fysb) < 0) || (NINT(ysrc[k]-fysb) < 0)           || 
                    (NINT(yrcv[k*nxy+0]-fysb) < 0)     || (NINT(yrcv[k*nxy+0]-fyse) > 0)       ) {
                    vwarn("source/receiver positions are outside synthesis aperture");
                    vmess("xsrc = %.2f xrcv_1 = %.2f xrvc_N = %.2f", xsrc[k], xrcv[k*nxy+0], xrcv[k*nxy+nxy-1]);
                    vmess("ysrc = %.2f yrcv_1 = %.2f yrvc_N = %.2f", ysrc[k], yrcv[k*nxy+0], yrcv[k*nxy+nxy-1]);
                    vmess("source position x:       %.2f in operator %d", xsrc[k], ixsrc);
                    vmess("source position y:       %.2f in operator %d", ysrc[k], iysrc);
                    vmess("receiver positions x:    %.2f <--> %.2f", xrcv[k*nxy+0], xrcv[k*nxy+nxy-1]);
                    vmess("receiver positions y:    %.2f <--> %.2f", yrcv[k*nxy+0], yrcv[k*nxy+nxy-1]);
                    vmess("focal point positions x: %.2f <--> %.2f", fxsb, fxse);
                    vmess("focal point positions y: %.2f <--> %.2f", fysb, fyse);
                }
                
                if ( (xsrc[k] >= fxb) && (xsrc[k] <= fxe) &&
                     (ysrc[k] >= fyb) && (ysrc[k] <= fye) ) {
                    
				    j = linearsearch(ixpos, *npos, isrc);
				    if (j < *npos) { /* the position (at j) is already included */
					    count[j] += xnx[k];
				    }
				    else { /* add new postion */
            		    ixpos[*npos] =  isrc;
					    count[*npos] += xnx[k];
                   	    *npos += 1;
				    }
    //                vmess("source position %d is inside synthesis model %f *npos=%d count=%d", k, xsrc[k], *npos, count[*npos]);
			    }
    
    	    } /* end of nshots (k) loop */
   	    } /* end of reci branch */
    } /* end of Nfoc loop */

    if (verbose>=4) {
	    for (j=0; j < *npos; j++) { 
            vmess("ixpos[%d] = %d count=%d", j, ixpos[j], count[j]);
		}
    }
    free(count);

/* sort ixpos into increasing values */
    qsort(ixpos, *npos, sizeof(int), compareInt);


    return;
}
Beispiel #11
0
void kxwdecomp(complex *rp, complex *rvz, complex *up, complex *down,
               int nkx, float dx, int nt, float dt, float fmin, float fmax,
               float cp, float rho, int verbose)
{
	int      iom, iomin, iomax, ikx, nfreq, a, av;
	float    omin, omax, deltom, om, kp, df, dkx;
	float    alpha, eps, *angle, avrp, avrvz, maxrp, maxrvz;
	float    fangle, pangle, vangle, kangle;
	complex  *pu, w;
	complex  ax, az;
	
	df     = 1.0/((float)nt*dt);
	dkx    = 2.0*M_PI/(nkx*dx);
	deltom = 2.*M_PI*df;
	omin   = 2.*M_PI*fmin;
	omax   = 2.*M_PI*fmax;
	nfreq  = nt/2+1;
	eps    = 0.01;
    alpha  = 0.1;
	
	iomin  = (int)MIN((omin/deltom), (nfreq-1));
	iomin  = MAX(iomin, 1);
	iomax  = MIN((int)(omax/deltom), (nfreq-1));

	pu     = (complex *)malloc(nkx*sizeof(complex));
	angle  = (float *)calloc(2*90,sizeof(float));

	/* estimate maximum propagation angle in wavefields P and Vz */
	for (a=1; a<90; a++) {
		for (iom = iomin; iom <= iomax; iom++) {
			om  = iom*deltom;
			ikx = MIN(NINT( ((om/cp)*sin(a*M_PI/180.0))/dkx ), nkx/2);
			if (ikx < nkx/2 && ikx != 0) {
				ax.r = rp[iom*nkx+ikx].r + rp[iom*nkx+nkx-1-ikx].r;
				ax.i = rp[iom*nkx+ikx].i + rp[iom*nkx+nkx-1-ikx].i;
				angle[a] += sqrt(ax.r*ax.r + ax.i*ax.i);
				ax.r = rvz[iom*nkx+ikx].r + rvz[iom*nkx+nkx-1-ikx].r;
				ax.i = rvz[iom*nkx+ikx].i + rvz[iom*nkx+nkx-1-ikx].i;
				angle[90+a] += sqrt(ax.r*ax.r + ax.i*ax.i);
			}
		}
	}

	avrp  =0.0;
	avrvz =0.0;
	maxrp =0.0;
	maxrvz=0.0;
	for (a=1; a<90; a++) {
		avrp += angle[a];
		maxrp = MAX(angle[a], maxrp);
		avrvz += angle[90+a];
		maxrvz = MAX(angle[90+a], maxrvz);
	}
	avrp  = avrp/89.0;
	avrvz = avrvz/89.0;
	if (verbose>=4)  {
		writesufile("anglerp.su", angle, 90, 1, 0, 0, 1, 1);
		writesufile("anglervz.su", &angle[90], 90, 1, 0, 0, 1, 1);
	}
	for (av=0; av<90; av++) {
		if (angle[89-av] <= avrp) angle[89-av] = 0.0;
		else {
			pangle=1.0*(90-av);
			break;
		}
	}
	for (av=0; av<90; av++) {
		 if (angle[179-av] <= avrvz) angle[179-av] = 0.0;
		else {
			vangle=1.0*(90-av);
			break;
		}
	}
	if (verbose>=4)  {
		writesufile("anglerp0.su", angle, 90, 1, 0, 0, 1, 1);
		writesufile("anglervz0.su", &angle[90], 90, 1, 0, 0, 1, 1);
	}
	fangle=pangle;
	if (verbose>=2) vmess("Up-down going: P max=%e average=%e => angle at average %f", maxrp, avrp, pangle);
	if (verbose>=2) vmess("Up-down going: Vz max=%e average=%e => angle at average %f", maxrvz, avrvz, vangle);
	if (pangle >= 90 || pangle <= 1) { /* use angle in Vz data, P might be placed on free surface */
		fangle = vangle;
	}
	if(!getparfloat("kangle",&kangle)) kangle=fangle;
	if (verbose>=2) vmess("Up-down going: maximum angle in decomposition= %f", kangle);

	for (iom = iomin; iom <= iomax; iom++) {
		om  = iom*deltom;

		decud(om, rho, cp, dx, nkx, kangle, alpha, eps, pu);
/*
		kxwfilter(dpux, kp, dx, nkx, alfa1, alfa2, perc); 
		kxwfilter(dpuz, kp, dx, nkx, alfa1, alfa2, perc); 
*/
		for (ikx = 0; ikx < nkx; ikx++) {
			ax.r = 0.5*rp[iom*nkx+ikx].r;
			ax.i = 0.5*rp[iom*nkx+ikx].i;
			az.r = 0.5*(rvz[iom*nkx+ikx].r*pu[ikx].r - rvz[iom*nkx+ikx].i*pu[ikx].i);
			az.i = 0.5*(rvz[iom*nkx+ikx].i*pu[ikx].r + rvz[iom*nkx+ikx].r*pu[ikx].i);

			down[iom*nkx+ikx].r = ax.r + az.r;
			down[iom*nkx+ikx].i = ax.i + az.i;
			up[iom*nkx+ikx].r   = ax.r - az.r;
			up[iom*nkx+ikx].i   = ax.i - az.i;
		}

	}

	free(pu);
	free(angle);

	return;
}
void synthesis(complex *Refl, complex *Fop, float *Top, float *iRN, int nx, int nt, int nxs, int nts, float dt, float *xsyn, int Nsyn, float *xrcv, float *xsrc, float fxs2, float fxs, float dxs, float dxsrc, float dx, int ixa, int ixb, int ntfft, int nw, int nw_low, int nw_high,  int mode, int reci, int nshots, int *ixpossyn, int npossyn, double *tfft, int *first, int verbose)
{
    int     nfreq, size, iox, inx;
    float   scl;
    int     i, j, l, m, iw, ix, k;
    float   *rtrace, idxs;
    complex *sum, *ctrace;
    int     npe;
	static int *ixrcv;
    static double t0, t1, t;

    size  = nxs*nts;
    nfreq = ntfft/2+1;
    /* scale factor 1/N for backward FFT,
     * scale dt for correlation/convolution along time, 
     * scale dx (or dxsrc) for integration over receiver (or shot) coordinates */
    scl   = 1.0*dt/((float)ntfft);

#ifdef _OPENMP
    npe   = omp_get_max_threads();
    /* parallelisation is over number of virtual source positions (Nsyn) */
    if (npe > Nsyn) {
        vmess("Number of OpenMP threads set to %d (was %d)", Nsyn, npe);
        omp_set_num_threads(Nsyn);
    }
#endif

    t0 = wallclock_time();

    /* reset output data to zero */
    memset(&iRN[0], 0, Nsyn*nxs*nts*sizeof(float));

	idxs = 1.0/dxs;
	if (ixrcv == NULL) {
		ixrcv = (int *)malloc(nshots*nx*sizeof(int));
	}
   	for (k=0; k<nshots; k++) {
           for (i = 0; i < nx; i++) {
               ixrcv[k*nx+i] = NINT((xrcv[k*nx+i]-fxs)*idxs);
		}
	}
	ctrace = (complex *)calloc(ntfft,sizeof(complex));
	if (!*first) {
    /* transform muted Ni (Top) to frequency domain, input for next iteration  */
    	for (l = 0; l < Nsyn; l++) {
        	/* set Fop to zero, so new operator can be defined within ixpossyn points */
            //memset(&Fop[l*nxs*nw].r, 0, nxs*nw*2*sizeof(float));
            bzero(&Fop[l*nxs*nw].r, nxs*nw*2*sizeof(float));
            for (i = 0; i < npossyn; i++) {
               	rc1fft(&Top[l*size+i*nts],ctrace,ntfft,-1);
               	ix = ixpossyn[i];
               	for (iw=0; iw<nw; iw++) {
                   	Fop[l*nxs*nw+iw*nxs+ix].r = ctrace[nw_low+iw].r;
                   	Fop[l*nxs*nw+iw*nxs+ix].i = mode*ctrace[nw_low+iw].i;
               	}
            }
		}
	}
	else { /* only for first call to synthesis */
    /* transform G_d to frequency domain, over all nxs traces */
		*first=0;
    	for (l = 0; l < Nsyn; l++) {
        	/* set Fop to zero, so new operator can be defined within all ix points */
            //memset(&Fop[l*nxs*nw].r, 0, nxs*nw*2*sizeof(float));
            bzero(&Fop[l*nxs*nw].r, nxs*nw*2*sizeof(float));
            for (i = 0; i < nxs; i++) {
               	rc1fft(&Top[l*size+i*nts],ctrace,ntfft,-1);
               	for (iw=0; iw<nw; iw++) {
                   	Fop[l*nxs*nw+iw*nxs+i].r = ctrace[nw_low+iw].r;
                   	Fop[l*nxs*nw+iw*nxs+i].i = mode*ctrace[nw_low+iw].i;
               	}
            }
		}
	}
	free(ctrace);
    t1 = wallclock_time();
	*tfft += t1 - t0;

    for (k=0; k<nshots; k++) {

/*        if (verbose>=3) {
            vmess("source position:     %.2f ixpossyn=%d", xsrc[k], ixpossyn[k]);
            vmess("receiver positions:  %.2f <--> %.2f", xrcv[k*nx+0], xrcv[k*nx+nx-1]);
        }
*/
        if ((NINT(xsrc[k]-fxs2) > 0) || (NINT(xrcv[k*nx+nx-1]-fxs2) > 0) ||
            (NINT(xrcv[k*nx+nx-1]-fxs) < 0) || (NINT(xsrc[k]-fxs) < 0) || 
            (NINT(xrcv[k*nx+0]-fxs) < 0) || (NINT(xrcv[k*nx+0]-fxs2) > 0) ) {
            vwarn("source/receiver positions are outside synthesis model");
            vwarn("integration calculation is stopped at gather %d", k);
            vmess("xsrc = %.2f xrcv_1 = %.2f xrvc_N = %.2f", xsrc[k], xrcv[k*nx+0], xrcv[k*nx+nx-1]);
            break;
        }
    

		iox = 0; inx = nx;

/*================ SYNTHESIS ================*/


#pragma omp parallel default(none) \
 shared(iRN, dx, npe, nw, verbose) \
 shared(Refl, Nsyn, reci, xrcv, xsrc, xsyn, fxs, nxs, dxs) \
 shared(nx, ixa, ixb, dxsrc, iox, inx, k, nfreq, nw_low, nw_high) \
 shared(Fop, size, nts, ntfft, scl, ixrcv, stderr) \
 private(l, ix, j, m, i, sum, rtrace)
    { /* start of parallel region */
    sum   = (complex *)malloc(nfreq*sizeof(complex));
    rtrace = (float *)calloc(ntfft,sizeof(float));

#pragma omp for schedule(guided,1)
    for (l = 0; l < Nsyn; l++) {

            ix = k; 

			/* multiply R with Fop and sum over nx */
			memset(&sum[0].r,0,nfreq*2*sizeof(float));
            //for (j = 0; j < nfreq; j++) sum[j].r = sum[j].i = 0.0;
                for (j = nw_low, m = 0; j <= nw_high; j++, m++) {
            for (i = iox; i < inx; i++) {
                    sum[j].r += Refl[k*nw*nx+m*nx+i].r*Fop[l*nw*nxs+m*nxs+ixrcv[k*nx+i]].r -
                                Refl[k*nw*nx+m*nx+i].i*Fop[l*nw*nxs+m*nxs+ixrcv[k*nx+i]].i;
                    sum[j].i += Refl[k*nw*nx+m*nx+i].i*Fop[l*nw*nxs+m*nxs+ixrcv[k*nx+i]].r +
                                Refl[k*nw*nx+m*nx+i].r*Fop[l*nw*nxs+m*nxs+ixrcv[k*nx+i]].i;
                }
            }

			/* transfrom result back to time domain */
            cr1fft(sum, rtrace, ntfft, 1);

            /* dx = receiver distance */
            for (j = 0; j < nts; j++) 
                iRN[l*size+ix*nts+j] += rtrace[j]*scl*dx;

    } /* end of parallel Nsyn loop */

    free(sum);
    free(rtrace);

#pragma omp single 
{ 
#ifdef _OPENMP
    npe   = omp_get_num_threads();
#endif
}
    } /* end of parallel region */

	if (verbose>3) vmess("*** Shot gather %d processed ***", k);

    } /* end of nshots (k) loop */

    t = wallclock_time() - t0;
    if (verbose) {
        vmess("OMP: parallel region = %f seconds (%d threads)", t, npe);
    }

    return;
}
Beispiel #13
0
int writeRec(recPar rec, modPar mod, bndPar bnd, wavPar wav, int ixsrc, int izsrc, int nsam, int ishot, int fileno, 
			 float *rec_vx, float *rec_vz, float *rec_txx, float *rec_tzz, float *rec_txz, 
			 float *rec_p, float *rec_pp, float *rec_ss, float *rec_udp, float *rec_udvz, int verbose)
{
    FILE    *fpvx, *fpvz, *fptxx, *fptzz, *fptxz, *fpp, *fppp, *fpss, *fpup, *fpdown;
	float *rec_up, *rec_down, *trace, *rec_vze, *rec_pe;
	float dx, dt, cp, rho, fmin, fmax;
	complex *crec_vz, *crec_p, *crec_up, *crec_dw;
    int irec, ntfft, nfreq, nkx, xorig, ix, iz, it, ibndx;
	int append;
	double ddt;
	char number[16], filename[1024];
    segy hdr;
    
	if (!rec.n) return 0;
	if (ishot) append=1;
	else append=0;

	/* if the total number of samples exceeds rec_ntsam then a new (numbered) file is opened */
	/* fileno has a non-zero value (from fdelmodc.c) if the number of samples exceeds rec_ntsam. */
	strcpy(filename, rec.file_rcv);
	if (fileno) {
		sprintf(number,"_%03d",fileno);
		name_ext(filename, number);
	}

	if (verbose>2) vmess("Writing receiver data to file %s", filename);
	if (nsam != rec.nt && verbose) vmess("Number of samples written to last file = %d",nsam);

	memset(&hdr,0,TRCBYTES);
	ddt = (double)mod.dt;/* to avoid rounding in 32 bit precision */
    dt  = (float)ddt*rec.skipdt;
	dx  = (rec.x[1]-rec.x[0])*mod.dx;
	hdr.dt     = (unsigned short)lround((((double)1.0e6*ddt*rec.skipdt)));
	hdr.scalco = -1000;
	hdr.scalel = -1000;
	hdr.sx     = 1000*(mod.x0+ixsrc*mod.dx);
	hdr.sdepth = 1000*(mod.z0+izsrc*mod.dz);
    hdr.selev  = (int)(-1000.0*(mod.z0+izsrc*mod.dz));
	hdr.fldr   = ishot+1;
	hdr.trid   = 1;
	hdr.ns     = nsam;
	hdr.trwf   = rec.n;
	hdr.ntr    = (ishot+1)*rec.n;
	hdr.f1     = 0.0;
	hdr.d1     = mod.dt*rec.skipdt;
	hdr.d2     = (rec.x[1]-rec.x[0])*mod.dx;
	hdr.f2     = mod.x0+rec.x[0]*mod.dx;

	if (rec.type.vx)  fpvx  = fileOpen(filename, "_rvx", append);
	if (rec.type.vz)  fpvz  = fileOpen(filename, "_rvz", append);
	if (rec.type.p)   fpp   = fileOpen(filename, "_rp", append);
	if (rec.type.txx) fptxx = fileOpen(filename, "_rtxx", append);
	if (rec.type.tzz) fptzz = fileOpen(filename, "_rtzz", append);
	if (rec.type.txz) fptxz = fileOpen(filename, "_rtxz", append);
	if (rec.type.pp)  fppp  = fileOpen(filename, "_rpp", append);
	if (rec.type.ss)  fpss  = fileOpen(filename, "_rss", append);

	/* decomposed wavefield */
	if (rec.type.ud && (mod.ischeme==1 || mod.ischeme==2) )  {
		fpup   = fileOpen(filename, "_ru", append);
		fpdown = fileOpen(filename, "_rd", append);
		ntfft = optncr(nsam);
		nfreq = ntfft/2+1;
		fmin = 0.0;
		fmax = wav.fmax;
		nkx = optncc(2*mod.nax);
		ibndx = mod.ioPx;
    	if (bnd.lef==4 || bnd.lef==2) ibndx += bnd.ntap;
		cp  = rec.cp;
		rho = rec.rho;
		if (verbose) vmess("Decomposition array at z=%.2f with cp=%.2f rho=%.2f", rec.zr[0]+mod.z0, cp, rho);
		rec_up  = (float *)calloc(ntfft*nkx,sizeof(float));
		rec_down= (float *)calloc(ntfft*nkx,sizeof(float));
		crec_vz = (complex *)malloc(nfreq*nkx*sizeof(complex));
		crec_p  = (complex *)malloc(nfreq*nkx*sizeof(complex));
		crec_up = (complex *)malloc(nfreq*nkx*sizeof(complex));
		crec_dw = (complex *)malloc(nfreq*nkx*sizeof(complex));

		rec_vze = rec_up;
		rec_pe  = rec_down;
		/* copy input data into extended arrays with padded zeroes */
		for (ix=0; ix<mod.nax; ix++) {
			memcpy(&rec_vze[ix*ntfft],&rec_udvz[ix*rec.nt],nsam*sizeof(float));
			memcpy(&rec_pe[ix*ntfft], &rec_udp[ix*rec.nt], nsam*sizeof(float));
		}

		/* transform from t-x to kx-w */
		xorig = ixsrc+ibndx;
		xt2wkx(rec_vze, crec_vz, ntfft, nkx, ntfft, nkx, xorig);
		xt2wkx(rec_pe, crec_p, ntfft, nkx, ntfft, nkx, xorig);

		/* apply decomposition operators */
		kxwdecomp(crec_p, crec_vz, crec_up, crec_dw,
               nkx, mod.dx, nsam, dt, fmin, fmax, cp, rho, verbose);

		/* transform back to t-x */
		wkx2xt(crec_up, rec_up, ntfft, nkx, nkx, ntfft, xorig);
		wkx2xt(crec_dw, rec_down, ntfft, nkx, nkx, ntfft, xorig);

		/* reduce array to rec.nt samples rec.n traces */
		for (irec=0; irec<rec.n; irec++) {
			ix = rec.x[irec]+ibndx;
			for (it=0; it<rec.nt; it++) {
				rec_up[irec*rec.nt+it]   = rec_up[ix*ntfft+it];
				rec_down[irec*rec.nt+it] = rec_down[ix*ntfft+it];
			}
		}
		free(crec_vz);
		free(crec_p);
		free(crec_up);
		free(crec_dw);
	}
	if (rec.type.ud && (mod.ischeme==3 || mod.ischeme==4) )  {
	}

	for (irec=0; irec<rec.n; irec++) {
		hdr.tracf  = irec+1;
		hdr.tracl  = ishot*rec.n+irec+1;
		hdr.gx     = 1000*(mod.x0+rec.x[irec]*mod.dx);
		hdr.offset = (rec.x[irec]-ixsrc)*mod.dx;
        hdr.gelev  = (int)(-1000*(mod.z0+rec.z[irec]*mod.dz));

		if (rec.type.vx) {
			traceWrite( &hdr, &rec_vx[irec*rec.nt], nsam, fpvx) ;
		}
		if (rec.type.vz) {
			traceWrite( &hdr, &rec_vz[irec*rec.nt], nsam, fpvz) ;
		}
		if (rec.type.p) {
			traceWrite( &hdr, &rec_p[irec*rec.nt], nsam, fpp) ;
		}
		if (rec.type.txx) {
			traceWrite( &hdr, &rec_txx[irec*rec.nt], nsam, fptxx) ;
		}
		if (rec.type.tzz) {
			traceWrite( &hdr, &rec_tzz[irec*rec.nt], nsam, fptzz) ;
		}
		if (rec.type.txz) {
			traceWrite( &hdr, &rec_txz[irec*rec.nt], nsam, fptxz) ;
		}
		if (rec.type.pp) {
			traceWrite( &hdr, &rec_pp[irec*rec.nt], nsam, fppp) ;
		}
		if (rec.type.ss) {
			traceWrite( &hdr, &rec_ss[irec*rec.nt], nsam, fpss) ;
		}
		if (rec.type.ud && mod.ischeme==1)  {
			traceWrite( &hdr, &rec_up[irec*rec.nt], nsam, fpup) ;
			traceWrite( &hdr, &rec_down[irec*rec.nt], nsam, fpdown) ;
		}
	}

	if (rec.type.vx) fclose(fpvx);
	if (rec.type.vz) fclose(fpvz);
	if (rec.type.p) fclose(fpp);
	if (rec.type.txx) fclose(fptxx);
	if (rec.type.tzz) fclose(fptzz);
	if (rec.type.txz) fclose(fptxz);
	if (rec.type.pp) fclose(fppp);
	if (rec.type.ss) fclose(fpss);
	if (rec.type.ud) {
		fclose(fpup);
		fclose(fpdown);
		free(rec_up);
		free(rec_down);
	}

    return 0;
}
Beispiel #14
0
int main (int argc, char **argv)
{
    FILE    *fp;
	char    *file_gp, *file_fp, *file_wav;
    int     nx, nt, ngath, ntraces, ret, size, nxwav;
    int     ntfft, nfreq, nxfft, nkx, i, j, n;
    float   dx, dt, fx, ft, xmin, xmax, scl, *den, dentmp;
    float   df, dw, dkx, eps, reps, leps, sclfk;
    float   *Gpd, *f1pd, *G_pad, *f_pad, *wav, *wav_pad, *outdata;
    complex *G_w, *f_w, *Gf, *amp, *wav_w, *S, *ZS, *SS;
    segy    *hdr_gp, *hdr_fp, *hdr_wav, *hdr_out;

	initargs(argc, argv);
	requestdoc(1);

	if(!getparstring("file_gp", &file_gp)) file_gp=NULL;
    if (file_gp==NULL) verr("file %s does not exist",file_gp);
    if(!getparstring("file_fp", &file_fp)) file_fp=NULL;
    if (file_fp==NULL) verr("file %s does not exist",file_fp);
    if(!getparstring("file_wav", &file_wav)) file_wav=NULL;
    if (file_wav==NULL) verr("file %s does not exist",file_wav);
	if(!getparfloat("eps", &eps)) eps=0.00;
	if(!getparfloat("reps", &reps)) reps=0.01;

    ngath = 1;
    ret = getFileInfo(file_gp, &nt, &nx, &ngath, &dt, &dx, &ft, &fx, &xmin, &xmax, &scl, &ntraces);

    size    = nt*nx;

	Gpd     = (float *)malloc(size*sizeof(float));
	hdr_gp  = (segy *) calloc(nx,sizeof(segy));
    fp      = fopen(file_gp, "r");
	if (fp == NULL) verr("error on opening input file_in1=%s", file_gp);
    nx      = readData(fp, Gpd, hdr_gp, nt);
    fclose(fp);

	f1pd    = (float *)malloc(size*sizeof(float));
	hdr_fp  = (segy *) calloc(nx,sizeof(segy));
    fp      = fopen(file_fp, "r");
	if (fp == NULL) verr("error on opening input file_in1=%s", file_fp);
    nx      = readData(fp, f1pd, hdr_fp, nt);
    fclose(fp);

    wav     = (float *)malloc(nt*sizeof(float));
	hdr_wav = (segy *) calloc(1,sizeof(segy));
    fp      = fopen(file_wav, "r");
	if (fp == NULL) verr("error on opening input file_in1=%s", file_fp);
    nxwav   = readData(fp, wav, hdr_wav, nt);
    fclose(fp);

    /* Start the scaling */
    ntfft   = optncr(nt);
	nfreq   = ntfft/2+1;
	df      = 1.0/(ntfft*dt);
    dw      = 2.0*PI*df;
	nkx     = optncc(nx);
	dkx     = 2.0*PI/(nkx*dx);
	sclfk   = dt*(dt*dx)*(dt*dx);

    vmess("ntfft:%d, nfreq:%d, nkx:%d dx:%.3f dt:%.3f",ntfft,nfreq,nkx,dx,dt);

    /* Allocate the arrays */
    G_pad = (float *)calloc(ntfft*nkx,sizeof(float));
	if (G_pad == NULL) verr("memory allocation error for G_pad");
    f_pad = (float *)calloc(ntfft*nkx,sizeof(float));
	if (f_pad == NULL) verr("memory allocation error for f_pad");
    wav_pad = (float *)calloc(ntfft,sizeof(float));
	if (wav_pad == NULL) verr("memory allocation error for wav_pad");
    G_w   = (complex *)calloc(nfreq*nkx,sizeof(complex));
	if (G_w == NULL) verr("memory allocation error for G_w");
    f_w   = (complex *)calloc(nfreq*nkx,sizeof(complex));
	if (f_w == NULL) verr("memory allocation error for f_w");
    Gf    = (complex *)calloc(nfreq*nkx,sizeof(complex));
	if (Gf == NULL) verr("memory allocation error for Gf");
    wav_w = (complex *)calloc(nfreq*nkx,sizeof(complex));
	if (wav_w == NULL) verr("memory allocation error for wav_w");
    amp   = (complex *)calloc(nfreq*nkx,sizeof(complex));
	if (amp == NULL) verr("memory allocation error for amp");
    S   = (complex *)calloc(nfreq*nkx,sizeof(complex));
	if (S == NULL) verr("memory allocation error for S");
    ZS   = (complex *)calloc(nfreq*nkx,sizeof(complex));
	if (ZS == NULL) verr("memory allocation error for ZS");
    SS   = (complex *)calloc(nfreq*nkx,sizeof(complex));
	if (SS == NULL) verr("memory allocation error for SS");
    den   = (float *)calloc(nfreq*nkx,sizeof(float));
	if (den == NULL) verr("memory allocation error for den");

    /* pad zeroes in 2 directions to reach FFT lengths */
	pad2d_data(Gpd, nt,nx,ntfft,nkx,G_pad);
	pad2d_data(f1pd,nt,nx,ntfft,nkx,f_pad);
    pad_data(  wav, nt, 1,ntfft,  wav_pad);

    /* double forward FFT */
	xt2wkx(&G_pad[0], &G_w[0], ntfft, nkx, ntfft, nkx, 0);
	xt2wkx(&f_pad[0], &f_w[0], ntfft, nkx, ntfft, nkx, 0);
    rcmfft(&wav_pad[0], &Gf[0], ntfft, 1, ntfft, nfreq, -1);

    for (i=0; i<nkx; i++) {
        for (j=0; j<nfreq; j++) {
            wav_w[j*nkx+i].r = Gf[j].r;
            wav_w[j*nkx+i].i = Gf[j].i;	
		}
    }

	for (i = 0; i < nkx*nfreq; i++) {
		Gf[i].r = (G_w[i].r*f_w[i].r - G_w[i].i*f_w[i].i);
		Gf[i].i = (G_w[i].r*f_w[i].i + G_w[i].i*f_w[i].r);

		S[i].r = (wav_w[i].r*wav_w[i].r + wav_w[i].i*wav_w[i].i);
		S[i].i = (wav_w[i].r*wav_w[i].i - wav_w[i].i*wav_w[i].r);

		ZS[i].r = (Gf[i].r*S[i].r + Gf[i].i*S[i].i);
		ZS[i].i = (Gf[i].r*S[i].i - Gf[i].i*S[i].r);

		SS[i].r = (S[i].r*S[i].r + S[i].i*S[i].i);
		SS[i].i = (S[i].r*S[i].i - S[i].i*S[i].r);

		if (i==0) dentmp=SS[i].r;
		else dentmp=MAX(dentmp,SS[i].r);
	}

	leps = reps*dentmp+eps;
	vmess("dentmp:%.4e leps:%.4e",dentmp,leps);

	for (i = 0; i < nkx*nfreq; i++) {
		S[i].r = (ZS[i].r*SS[i].r+ZS[i].i*SS[i].i)/(SS[i].r*SS[i].r+SS[i].i*SS[i].i+leps);
		S[i].i = (ZS[i].i*SS[i].r-ZS[i].r*SS[i].i)/(SS[i].r*SS[i].r+SS[i].i*SS[i].i+leps);

		amp[i].r = sqrtf(S[i].r*S[i].r+S[i].i*S[i].i);
		amp[i].i = 0.0;
		
		// complex_sqrt(&amp[i]);
		if (isnan(amp[i].r)) amp[i].r = 0;
		if (isnan(amp[i].i)) amp[i].i = 0;
		if (isinf(amp[i].r)) amp[i].r = 0;
		if (isinf(amp[i].i)) amp[i].i = 0;

		Gf[i].r = (G_w[i].r*amp[i].r - G_w[i].i*amp[i].i);
		Gf[i].i = (G_w[i].r*amp[i].i + G_w[i].i*amp[i].r);
	}

	// for (i=0; i<nfreq; i++) {
	// 	for (j=0; j<nkx; j++) {
	// 		Gpd[j*nfreq+i] = sqrtf(amp[i*nkx+j].r*amp[i*nkx+j].r+amp[i*nkx+j].i*amp[i*nkx+j].i);
	// 	}
	// }
    
    // conv_small(G_w, amp, Gf, nkx, nfreq); // Scaled data

    /* inverse double FFT */
	wkx2xt(&Gf[0], &G_pad[0], ntfft, nkx, nkx, ntfft, 0);
	/* select original samples and traces */
	scl = (1.0)/(nkx*ntfft);
	scl_data(G_pad,ntfft,nx,scl,Gpd ,nt);

    fp      = fopen("out.su", "w+");
    ret = writeData(fp, Gpd, hdr_gp, nt, nx);
	if (ret < 0 ) verr("error on writing output file.");
    fclose(fp);

	// fp      = fopen("wav.su", "w+");
	// for (j=0; j<nkx; j++) {
	// 	hdr_gp[j].ns = nfreq;
	// }
    // ret = writeData(fp, Gpd, hdr_gp, nfreq, nkx);
	// if (ret < 0 ) verr("error on writing output file.");
    // fclose(fp);

    free(f1pd);free(Gpd);free(hdr_gp);free(hdr_fp);

	return 0;
}
Beispiel #15
0
void xwBeam(float *data, int nx, int nt, float dt, float *velmod, int ndepth, float fmin, float fmax, int opl, int ntap, int conjg, float *beams, int nxm, float ox, float dxm, float *xrcv, int verbose)
{
	int    	iomin, iomax, iom, ix, d, hopl, hopl2, i, j, ixrcv;
	int	index1, nfreq, optn, lenx, i1, i2, hoplen;
	float  	dom, om, c, cprev, df, scale, scl;
	float	*taper, *locbeam, *pdata;
	complex	*opx, *cdata, *tmp1, *tmp2, wa;

	optn  = optncr(nt);
	nfreq = optn/2 + 1;
	tmp1 = (complex *)calloc(nx*nfreq,sizeof(complex)); 

	/* pad zero's to get fourier length */
	if( nt != optn ) {
		if (verbose >1) vmess("xwBeam: padding zeros to data from %d to %d",nt,optn);
			pdata = (float *)calloc(optn*nx,sizeof(float));
			for (i=0; i<nx; i++) {
				for (j=0; j<nt; j++) pdata[i*optn+j] = data[i*nt+j];
				for ( ; j<optn; j++) pdata[i*optn+j] = 0.0;
			}
	}
	else {
		pdata = &data[0];
	}

	xt2wx(pdata, tmp1, optn, nx, optn, nx);

	if( nt != optn ) free(pdata);

	if (conjg) scl = -1.0;
	else scl = 1.0;
	df    = 1.0/(optn*dt);
	dom   = 2.*PI*df;
	iomin = (int)MIN((fmin*dt*optn), (nfreq-1));
	iomin = MAX(iomin, 1);
	iomax = MIN((int)(fmax*dt*optn), (nfreq-1));
	hopl  = (opl+1)/2;
	hopl2 = hopl-1;
	lenx  = 2*hopl2+nxm;
	scale = 1.0/(float)(iomax-iomin+1);

/* plane wave correction (rotation) at the surface 

    for (iom = 0; iom < nfreq; iom++) {
        for (ix = 0; ix < nx; ix++) {
            xoff = -1500.0+ix*15.0;
            shift = sin(15.0*M_PI/180)*xoff/2000.;
            if (iom ==0 ) fprintf(stderr,"xoff = %f shift = %f \n",xoff, shift);
            om = iom*dom*shift;
            wa = cdata[iom*nx+ix];
            cdata[iom*nx+ix].r = wa.r*cos(-om) - wa.i*sin(-om);
            cdata[iom*nx+ix].i = wa.i*cos(-om) + wa.r*sin(-om);
        }
    }
*/

	taper = (float *)malloc(nxm*sizeof(float));
	for (ix = 0; ix < nxm; ix++) taper[ix] = 1.0;
	for (ix = 0; ix < ntap; ix++) {
		taper[ix] = exp(-1.0*(pow((0.4*(ntap-ix)/ntap), 2)));
		taper[nxm-1-ix] = taper[ix];
	}

	cdata = (complex *)calloc(nxm*nfreq,sizeof(complex));
	for (iom = iomin; iom <= iomax; iom++) {
		for (ix = 0; ix < nx; ix++) {
			ixrcv = NINT((xrcv[ix]-ox)/dxm);
			cdata[iom*nxm+ixrcv].r += tmp1[iom*nx+ix].r;
			cdata[iom*nxm+ixrcv].i += tmp1[iom*nx+ix].i*scl;
		}
	}
	free(tmp1);

	if(verbose) {
		fprintf(stderr,"    xwBeam: number of depth steps = %d\n", ndepth);
		fprintf(stderr,"    xwBeam: number of frequencies = %d\n", iomax-iomin+1);
	}


	tmp1  = (complex *)calloc(lenx, sizeof(complex));
	tmp2  = (complex *)calloc(lenx, sizeof(complex));
	opx   = (complex *)calloc(hopl, sizeof(complex));
	locbeam = (float *)calloc(nxm*ndepth, sizeof(float));

	for (iom = iomin; iom <= iomax; iom++) {
		om = iom*dom;
		cprev = 0;
		for (j = 0; j < nxm; j++) {
			tmp1[hopl2+j].r = cdata[iom*nxm+j].r*taper[j];
			tmp1[hopl2+j].i = cdata[iom*nxm+j].i*taper[j];
		}

		for (d = 0; d < ndepth; d++) {

			for (ix = 0; ix < nxm; ix++) {
				locbeam[d*nxm+ix] += sqrt(tmp1[hopl2+ix].r*tmp1[hopl2+ix].r+
						tmp1[hopl2+ix].i*tmp1[hopl2+ix].i)*scale;
			}

			for (ix = 0; ix < nxm; ix++) {
				c = velmod[d*nxm+ix];
				if (c != cprev && c!=0.0) {
					readtable_opt(opx, om/c, &hoplen);
					cprev = c;
				}
				if (c==0.0) hoplen=0;

				wa.r = wa.i = 0.0;
				index1 = ix + hopl2;
				for (j = 0; j < hoplen; j++) {
					i1 = index1+j;
					i2 = index1-j;
					wa.r += (tmp1[i1].r + tmp1[i2].r)*opx[j].r;
					wa.r -= (tmp1[i1].i + tmp1[i2].i)*opx[j].i;
					wa.i += (tmp1[i1].i + tmp1[i2].i)*opx[j].r;
					wa.i += (tmp1[i1].r + tmp1[i2].r)*opx[j].i;
				}
				if (hoplen != 0) tmp2[index1] = wa;
				else tmp2[index1] = tmp1[index1];
			}
			for (j = 0; j < lenx; j++) tmp1[j] = tmp2[j];
		}
	}

	for (d = 0; d < ndepth; d++) {
		for (ix = 0; ix < nxm; ix++) {
			beams[ix*ndepth+d] += locbeam[d*nxm+ix];
		}
	}

	free(opx);
	free(tmp1);
	free(tmp2);
	free(locbeam);
	free(cdata);
	free(taper);

	return;
}
Beispiel #16
0
int applySource(modPar mod, srcPar src, wavPar wav, bndPar bnd, int itime, int ixsrc, int izsrc, float *vx, float *vz, float *tzz, float *txx, float *txz, float *rox, float *roz, float *l2m, float **src_nwav, int verbose)
{
	int is0, ibndz, ibndx;
	int isrc, ix, iz, n1;
	int id1, id2;
	float src_ampl, time, scl, dt, sdx;
	static int first=1;

	if (src.type==6) {
    	ibndz = mod.ioXz;
    	ibndx = mod.ioXx;
	}
	else if (src.type==7) {
    	ibndz = mod.ioZz;
    	ibndx = mod.ioZx;
	}
	else if (src.type==2) {
    	ibndz = mod.ioTz;
    	ibndx = mod.ioTx;
    	if (bnd.lef==4 || bnd.lef==2) ibndx += bnd.ntap;
    	if (bnd.top==4 || bnd.top==2) ibndz += bnd.ntap;
	}
	else {	
    	ibndz = mod.ioPz;
    	ibndx = mod.ioPx;
    	if (bnd.lef==4 || bnd.lef==2) ibndx += bnd.ntap;
    	if (bnd.top==4 || bnd.top==2) ibndz += bnd.ntap;
	}


	n1   = mod.naz;
	dt   = mod.dt;
	sdx  = 1.0/mod.dx;

	/* special txz source activated? */

	if ((bnd.top==1) && (src.type==2)) {
		iz = izsrc + ibndz;
		if (iz==ibndz) {
            if (src.orient != 1) {
				if (first) {
					vmess("Only monopole Txz source allowed at surface. Reset to monopole");
					first = 0;
				}
				src.orient=1;
			}
		}
	}
             
/*
* for plane wave sources the sources are placed 
* around the central shot position 
* the first source position has an offset in x of is0
*
* itime = 0 corresponds with time=0
* itime = 1 corresponds with time=dt
* src[0] (the first sample) corresponds with time = 0
*/

	is0 = -1*floor((src.n-1)/2);
#pragma omp	for private (isrc, src_ampl, ix, iz, time, id1, id2, scl) 
	for (isrc=0; isrc<src.n; isrc++) {
		src_ampl=0.0;
		/* calculate the source position */
		if (src.random || src.multiwav) {
			ix = src.x[isrc] + ibndx;
			iz = src.z[isrc] + ibndz;
		}
		else { /* plane wave and point sources */
            ix = ixsrc + ibndx + is0 + isrc;
            iz = izsrc + ibndz;
		}
		time = itime*dt - src.tbeg[isrc];
		id1 = floor(time/dt);
		id2 = id1+1;
        
		/* delay not reached or no samples left in source wavelet? */
		if ( (time < 0.0) || ( (itime*dt) >= src.tend[isrc]) ) continue;

//		fprintf(stderr,"isrc=%d ix=%d iz=%d src.x=%d src.z=%d\n", isrc, ix, iz, src.x[isrc], src.z[isrc]);

		if (!src.multiwav) { /* only one wavelet for all sources */
			src_ampl = src_nwav[0][id1]*(id2-time/dt) + src_nwav[0][id2]*(time/dt-id1);
		}
		else { /* multi-wavelet sources */
			src_ampl = src_nwav[isrc][id1]*(id2-time/dt) + src_nwav[isrc][id2]*(time/dt-id1);
		}

		if (src_ampl==0.0) continue;
		if ( ((ix-ibndx)<0) || ((ix-ibndx)>mod.nx) ) continue; /* source outside grid */

		if (verbose>=4 && itime==0) {
			vmess("Source %d positioned at grid ix=%d iz=%d",isrc, ix, iz);
		}
		if (verbose>5) {
			vmess("Source %d at grid [ix=%d,iz=%d] at itime %d has value %e",isrc, ix,iz, itime, src_ampl);
		}

		/* cosine squared windowing to reduce edge effects on shot arrays */
		if ( (src.n>1) && src.window) {
			if (isrc < src.window) {
				scl = cos(0.5*M_PI*(src.window - isrc)/src.window);
			}
			else if (isrc > src.n-src.window+1) {
				scl = cos(0.5*M_PI*(src.window - (src.n-isrc+1))/src.window);
			}
			src_ampl *= scl*scl;
		}

		/* source scaling factor to compensate for discretisation */

		/* old amplitude setting does not obey reciprocity */
		// src_ampl *= rox[ix*n1+iz]*l2m[ix*n1+iz]/(dt);

/* added factor 2.0 to be compliant with defined Green's functions */
		src_ampl *= (2.0/mod.dx)*l2m[ix*n1+iz];

		/* Force source */

		if (src.type == 6) {
//			vx[ix*n1+iz] += src_ampl*(dt/mod.dx)/(l2m[ix*n1+iz]);
			vx[ix*n1+iz] += src_ampl*rox[ix*n1+iz]/(l2m[ix*n1+iz]);
		}
		else if (src.type == 7) {
		/* old amplitude setting does not obey reciprocity */
//			vz[ix*n1+iz] += src_ampl*(dt/mod.dx)/(l2m[ix*n1+iz]);
			vz[ix*n1+iz] += src_ampl*roz[ix*n1+iz]/(l2m[ix*n1+iz]);
//			fprintf(stderr,"ix=%d iz=%d l2m=%e rox=%e rho=%e dt=%e dx=%e\n", ix, iz, l2m[ix*n1+iz], roz[ix*n1+iz], dt/(mod.dx*roz[ix*n1+iz]), dt, mod.dx);
        } /* src.type */

        
		/* Stress source */

		if (mod.ischeme <= 2) { /* Acoustic scheme */
			/* Compressional source */
			if (src.type == 1) {
				if (src.orient != 1) src_ampl=src_ampl/mod.dx;

				if (src.orient==1) { /* monopole */
					tzz[ix*n1+iz] += src_ampl;
				}
				else if (src.orient==2) { /* dipole +/- */
					tzz[ix*n1+iz] += src_ampl;
					tzz[ix*n1+iz+1] -= src_ampl;
				}
				else if (src.orient==3) { /* dipole - + */
					tzz[ix*n1+iz] += src_ampl;
					tzz[(ix-1)*n1+iz] -= src_ampl;
				}
				else if (src.orient==4) { /* dipole +/0/- */
					if (iz > ibndz) 
						tzz[ix*n1+iz-1]+= 0.5*src_ampl;
					if (iz < mod.nz+ibndz-1) 
						tzz[ix*n1+iz+1] -= 0.5*src_ampl;
				}
				else if (src.orient==5) { /* dipole + - */
					tzz[ix*n1+iz] += src_ampl;
					tzz[(ix+1)*n1+iz] -= src_ampl;
				}
			}
		}
		else { /* Elastic scheme */
			/* Compressional source */
			if (src.type == 1) {
				if (src.orient==1) { /* monopole */
					txx[ix*n1+iz] += src_ampl;
					tzz[ix*n1+iz] += src_ampl;
				}
				else if (src.orient==2) { /* dipole +/- */
					txx[ix*n1+iz] += src_ampl;
					tzz[ix*n1+iz] += src_ampl;
					txx[ix*n1+iz+1] -= src_ampl;
					tzz[ix*n1+iz+1] -= src_ampl;
				}
				else if (src.orient==3) { /* dipole - + */
					txx[ix*n1+iz] += src_ampl;
					tzz[ix*n1+iz] += src_ampl;
					txx[(ix-1)*n1+iz] -= src_ampl;
					tzz[(ix-1)*n1+iz] -= src_ampl;
				}
				else if (src.orient==4) { /* dipole +/0/- */
					if (iz > ibndz) {
						txx[ix*n1+iz-1]+= 0.5*src_ampl;
						tzz[ix*n1+iz-1]+= 0.5*src_ampl;
					}
					if (iz < mod.nz+ibndz-1) {
						txx[ix*n1+iz+1] -= 0.5*src_ampl;
						tzz[ix*n1+iz+1] -= 0.5*src_ampl;
					}
				}
				else if (src.orient==5) { /* dipole + - */
					txx[ix*n1+iz] += src_ampl;
					tzz[ix*n1+iz] += src_ampl;
					txx[(ix+1)*n1+iz] -= src_ampl;
					tzz[(ix+1)*n1+iz] -= src_ampl;
				}
			}
			else if (src.type == 2) {
				/* Txz source */
				if ((iz == ibndz) && bnd.top==1) {
					txz[(ix-1)*n1+iz-1] += src_ampl;
					txz[ix*n1+iz-1] += src_ampl;
				}
				else {
					txz[ix*n1+iz] += src_ampl;
				}
				/* possible dipole orientations for a txz source */
				if (src.orient == 2) { /* dipole +/- */
					txz[ix*n1+iz+1] -= src_ampl;
				}
				else if (src.orient == 3) { /* dipole - + */
					txz[(ix-1)*n1+iz] -= src_ampl;
				}
				else if (src.orient == 4) { /*  dipole +/O/- */
					/* correction: subtrace previous value to prevent z-1 values. */
					txz[ix*n1+iz] -= 2.0*src_ampl;
					txz[ix*n1+iz+1] += src_ampl;
				}
				else if (src.orient == 5) { /* dipole + - */
					txz[(ix+1)*n1+iz] -= src_ampl;
				}
			}
			/* Tzz source */
			else if(src.type == 3) {
				tzz[ix*n1+iz] += src_ampl;
			} 
			/* Txx source */
			else if(src.type == 4) {
				txx[ix*n1+iz] += src_ampl;
			} 

/***********************************************************************
* pure potential shear S source (experimental)
* Curl S-pot = CURL(F) = dF_x/dz - dF_z/dx
***********************************************************************/
			else if(src.type == 5) {
				src_ampl = src_ampl*rox[ix*n1+iz]/(l2m[ix*n1+iz]);
				if (src.orient == 3) src_ampl = -src_ampl;
                /* first order derivatives */
				vx[ix*n1+iz]     += src_ampl*sdx;
				vx[ix*n1+iz-1]   -= src_ampl*sdx;
				vz[ix*n1+iz]     -= src_ampl*sdx;
				vz[(ix-1)*n1+iz] += src_ampl*sdx;
                
                /* second order derivatives */
                /*
				vx[ix*n1+iz]     += c1*src_ampl*sdx;
                vx[ix*n1+iz-1]   -= c1*src_ampl*sdx;
				vx[ix*n1+iz+1]   += c2*src_ampl*sdx;
                vx[ix*n1+iz-2]   -= c2*src_ampl*sdx;

                vz[ix*n1+iz]     -= c1*src_ampl*sdx;
				vz[(ix-1)*n1+iz] += c1*src_ampl*sdx;
				vz[(ix+1)*n1+iz] -= c2*src_ampl*sdx;
				vz[(ix-2)*n1+iz] += c2*src_ampl*sdx;
                 */

				/* determine second position of dipole */
				if (src.orient == 2) { /* dipole +/- vertical */
					iz += 1;
                    vx[ix*n1+iz]     -= src_ampl*sdx;
                    vx[ix*n1+iz-1]   += src_ampl*sdx;
                    vz[ix*n1+iz]     += src_ampl*sdx;
                    vz[(ix-1)*n1+iz] -= src_ampl*sdx;
				}
				else if (src.orient == 3) { /* dipole - + horizontal */
					ix += 1;
                    vx[ix*n1+iz]     -= src_ampl*sdx;
                    vx[ix*n1+iz-1]   += src_ampl*sdx;
                    vz[ix*n1+iz]     += src_ampl*sdx;
                    vz[(ix-1)*n1+iz] -= src_ampl*sdx;
				}
            }
/***********************************************************************
* pure potential pressure P source (experimental)
* Divergence P-pot = DIV(F) = dF_x/dx + dF_z/dz
***********************************************************************/
                else if(src.type == 8) {
				    src_ampl = src_ampl*rox[ix*n1+iz]/(l2m[ix*n1+iz]);
                    if (src.orient == 3) src_ampl = -src_ampl;
                    vx[(ix+1)*n1+iz] += src_ampl*sdx;
                    vx[ix*n1+iz]     -= src_ampl*sdx;
                    vz[ix*n1+iz+1]   += src_ampl*sdx;
                    vz[ix*n1+iz]     -= src_ampl*sdx;
                    /* determine second position of dipole */
                    if (src.orient == 2) { /* dipole +/- */
                        iz += 1;
                        vx[(ix+1)*n1+iz] -= src_ampl*sdx;
                        vx[ix*n1+iz]     += src_ampl*sdx;
                        vz[ix*n1+iz+1]   -= src_ampl*sdx;
                        vz[ix*n1+iz]     += src_ampl*sdx;
                    }
                    else if (src.orient == 3) { /* dipole - + */
                        ix += 1;
                        vx[(ix+1)*n1+iz] -= src_ampl*sdx;
                        vx[ix*n1+iz]     += src_ampl*sdx;
                        vz[ix*n1+iz+1]   -= src_ampl*sdx;
                        vz[ix*n1+iz]     += src_ampl*sdx;
                    }
                
			} /* src.type */
		} /* ischeme */
	} /* loop over isrc */

	return 0;
}
Beispiel #17
0
int writeBeams(modPar mod, snaPar sna, int ixsrc, int izsrc, int ishot, int fileno, 
			   float *beam_vx, float *beam_vz, float *beam_txx, float *beam_tzz, float *beam_txz, 
			   float *beam_p, float *beam_pp, float *beam_ss, int verbose)
{
	FILE    *fpvx, *fpvz, *fptxx, *fptzz, *fptxz, *fpp, *fppp, *fpss;
	int append;
	int ix;
	char number[16], filename[1024];
	segy hdr;

	if (sna.beam==0) return 0;
	/* all beam snapshots are written to the same output file(s) */
	if (ishot) append=1;
	else append=0;
	
	strcpy(filename, sna.file_beam);
	if (fileno) {
		sprintf(number,"_%03d",fileno);
		name_ext(filename, number);
	}
	if (verbose>2) vmess("Writing beam data to file %s", filename);


	if (sna.type.vx)  fpvx  = fileOpen(filename, "_bvx", append);
	if (sna.type.vz)  fpvz  = fileOpen(filename, "_bvz", append);
	if (sna.type.p)   fpp   = fileOpen(filename, "_bp", append);
	if (sna.type.txx) fptxx = fileOpen(filename, "_btxx", append);
	if (sna.type.tzz) fptzz = fileOpen(filename, "_btzz", append);
	if (sna.type.txz) fptxz = fileOpen(filename, "_btxz", append);
	if (sna.type.pp)  fppp  = fileOpen(filename, "_bpp", append);
	if (sna.type.ss)  fpss  = fileOpen(filename, "_bss", append);
	
	memset(&hdr,0,TRCBYTES);
	hdr.dt     = 1000000*(mod.dt);
	hdr.scalco = -1000;
	hdr.scalel = -1000;
	hdr.sx     = 1000*(mod.x0+ixsrc*mod.dx);
	hdr.sdepth = 1000*(mod.z0+izsrc*mod.dz);
	hdr.fldr   = ishot+1;
	hdr.trid   = 1;
	hdr.ns     = sna.nz;
	hdr.trwf   = sna.nx;
	hdr.ntr    = sna.nx;
	hdr.f1     = sna.z1*mod.dz+mod.z0;
	hdr.f2     = sna.x1*mod.dx+mod.x0;
	hdr.d1     = mod.dz*sna.skipdz;
	hdr.d2     = mod.dx*sna.skipdx;

	for (ix=0; ix<sna.nx; ix++) {
		hdr.tracf  = ix+1;
		hdr.tracl  = ix+1;
		hdr.gx     = 1000*(mod.x0+(sna.x1+ix)*mod.dx);

		if (sna.type.vx) {
			traceWrite( &hdr, &beam_vx[ix*sna.nz], sna.nz, fpvx) ;
		}
		if (sna.type.vz) {
			traceWrite( &hdr, &beam_vz[ix*sna.nz], sna.nz, fpvz) ;
		}
		if (sna.type.p) {
			traceWrite( &hdr, &beam_p[ix*sna.nz], sna.nz, fpp) ;
		}
		if (sna.type.tzz) {
			traceWrite( &hdr, &beam_tzz[ix*sna.nz], sna.nz, fptzz) ;
		}
		if (sna.type.txx) {
			traceWrite( &hdr, &beam_txx[ix*sna.nz], sna.nz, fptxx) ;
		}
		if (sna.type.txz) {
			traceWrite( &hdr, &beam_txz[ix*sna.nz], sna.nz, fptxz) ;
		}
		if (sna.type.pp) {
			traceWrite( &hdr, &beam_pp[ix*sna.nz], sna.nz, fppp) ;
		}
		if (sna.type.ss) {
			traceWrite( &hdr, &beam_ss[ix*sna.nz], sna.nz, fpss) ;
		}

	}

	if (sna.type.vx) fclose(fpvx);
	if (sna.type.vz) fclose(fpvz);
	if (sna.type.p) fclose(fpp);
	if (sna.type.txx) fclose(fptxx);
	if (sna.type.tzz) fclose(fptzz);
	if (sna.type.txz) fclose(fptxz);
	if (sna.type.pp) fclose(fppp);
	if (sna.type.ss) fclose(fpss);

	return 0;
}
Beispiel #18
0
void kwZoMigr(float *data, int nx, int nt, float dt, float *velmod, int nxm, int nzm, int ixa, int ixb, float fmin, float fmax, float *xrcv, int izrcv, float ox, float dxm, float dz, int ntap, int conjg, int ndepth, float *image, int verbose, float *exrcv, int ndepthex)
{
    int     iomin, iomax, iom, ix, d, i, j;
    int     nfreq, optn, nkx, sign, endkx;
    int     ixrcv, ixmin, ixmax, ixo, ixn, ikx;
    float   k, k2, kz2, kx, kx2;
    float   dom, om, c, dkx, df, sr;
    float   *taper, scl, scl2, *pdata;
    float 	*trace;
    complex *ctrace;
    float   t0, t1;
    complex *cdata, tmp, ez;
    complex wa, *ctmp, da, *locdat;
    complex *cexrcv=(complex *) exrcv;

    /* define some constants */

    optn  = optncr(nt);
    nfreq = optn/2 + 1;
    df    = 1.0/(optn*dt);
    dom   = 2.0*M_PI*df;
    iomin = (int)MIN((fmin*dt*optn), (nfreq-1));
    iomin = MAX(iomin, 1);
    iomax = MIN((int)(fmax*dt*optn), (nfreq-1));

    /* transformation of shot record to frequency domain  */

    trace  = (float *)calloc(optn,sizeof(float));
    ctrace = (complex *)malloc(optn*sizeof(complex));
    cdata  = (complex *)calloc(nxm*nfreq, sizeof(complex));

    if (conjg) scl = -1.0;
    else scl = 1.0;

    sign  = -1;
    for (ix = 0; ix < nx; ix++) {

        memcpy(trace,&data[ix*nt],nt*sizeof(float));
        if (optn > nt)
            memset( &trace[nt], 0, sizeof(float)*(optn-nt) );

        rc1fft(trace,ctrace,optn,sign);

        ixrcv = NINT((xrcv[ix]-ox)/dxm);
        if (ixrcv < 0 || ixrcv > nxm-1) {
            fprintf(stderr,"kwZoMigr: ixrcv %f (%d) outside model\n", xrcv[ix], ixrcv);
            continue;
        }
        for (iom=0; iom<nfreq; iom++) {
            /* positioning of shot record into velocity model */
            cdata[iom*nxm+ixrcv].r = ctrace[iom].r;
            cdata[iom*nxm+ixrcv].i = ctrace[iom].i*scl;
        }
    }

    /* determine aperture to be calculated */

    ixo = nxm;
    ixn = 0;
    for (ix = 0; ix < nx; ix++) {
        ixrcv = NINT((xrcv[ix]-ox)/dxm);
        if (ixrcv < ixo) ixo = ixrcv;
        if (ixrcv > ixn) ixn = ixrcv;
    }
    ixmin = MAX(0, ixo-ixb-1);
    ixmax = MIN(ixn+ixa+1, nxm-1);
    nx    = (ixmax-ixmin)+1;

    if (verbose>=2) {
        vmess("kwZoMigr: calculation aperture: %.2f (%d) <--> %.2f (%d) (%d positions)", ixmin*dxm+ox, ixmin, ixmax*dxm+ox, ixmax, nx);
    }

    /* define some constants */

    scl   = 2.0/nfreq;
    scl   = 1.0/(dt*dxm*dxm);
    nkx   = optncc(2*ntap+nxm);
    ntap  = (nkx-nxm)/2;
    scl2  = 1.0/nkx;
    dkx   = 2.0*M_PI/(nkx*dxm);

    taper = (float *)malloc(ntap*sizeof(float));
    for (ix = 0; ix < ntap; ix++) {
        taper[ix] = exp(-1.0*(pow((0.4*(ntap-ix)/ntap), 2)));
    }

    /* calculate image at depth = 0 */

    if(izrcv==0 ) {
        for (ix = ixmin; ix <= ixmax; ix++) {
            for (iom = iomin; iom <= iomax; iom++) {
                image[ix*nzm+0] += scl*cdata[iom*nxm+ix].r;
            }
        }
    }

    t0 = wallclock_time();

    locdat  = (complex *)malloc(nkx*sizeof(complex));

    /* start extrapolation for all frequencies, depths and x-positions */

    for (iom = iomin; iom <= iomax; iom++) {
        memset(locdat,0,nkx*sizeof(complex));

        for (ix = ixmin; ix <= ixmax; ix++) {
            locdat[ntap+ix] = cdata[iom*nxm+ix];
        }
        om = iom*dom;
        d  = izrcv;

        /* start extrapolation of receiver arrays */

        for (; d < ndepth; d++) {

            /* transform to wavenumber domain */

            cc1fft(locdat, nkx, 1);

            /* Extrapolation of data */

            c = 0.0;
            for (ix = ixmin; ix <= ixmax; ix++) c += velmod[d*nxm+ix];
            k = nx*om/c;
            k2 = k*k;

            /* kx = 0 */
            ez.r = cos(k*dz);
            ez.i = -sin(k*dz);

            tmp.r  = ez.r*locdat[0].r;
            tmp.r += ez.i*locdat[0].i;
            tmp.i  = ez.r*locdat[0].i;
            tmp.i -= ez.i*locdat[0].r;
            locdat[0] = tmp;

            /* kx != 0 */
            endkx = MIN((int)(k/dkx),nkx/2);
            for (ikx = 1; ikx <= endkx; ikx++) {
                kx  = ikx*dkx;
                kx2 = kx*kx;
                kz2 = k2 - kx2;

                ez.r = cos(sqrt(kz2)*dz);
                ez.i = -sin(sqrt(kz2)*dz);

                tmp.r  = ez.r*locdat[ikx].r;
                tmp.r += ez.i*locdat[ikx].i;
                tmp.i  = ez.r*locdat[ikx].i;
                tmp.i -= ez.i*locdat[ikx].r;
                locdat[ikx] = tmp;

                tmp.r  = ez.r*locdat[nkx-ikx].r;
                tmp.r += ez.i*locdat[nkx-ikx].i;
                tmp.i  = ez.r*locdat[nkx-ikx].i;
                tmp.i -= ez.i*locdat[nkx-ikx].r;
                locdat[nkx-ikx] = tmp;
            }

            /* transform data back to space domain */

            cc1fft(locdat, nkx, -1);

            for (j = 0; j < nkx; j++) {
                locdat[j].r *= scl2;
                locdat[j].i *= scl2;
            }

            /* imaging condition */

            for (ix = ixmin; ix <= ixmax; ix++) {
                image[ix*nzm+d+1]+= scl*locdat[ntap+ix].r;
            }

            /* save extrapolated field at requested depth */

            if	(d==ndepthex-1) {
                if ( cexrcv != NULL ) {
                    for (ix = 0; ix < nxm; ix++) {
                        cexrcv[iom*nxm+ix] = locdat[ntap+ix];
                    }
                }
            }

            /* taper extrapolated data at edges */

            for (j = 0; j < ntap; j++) {
                locdat[j].r *= taper[j];
                locdat[j].i *= taper[j];
                locdat[nkx-j-1].r *= taper[j];
                locdat[nkx-j-1].i *= taper[j];
            }

        } /* end of depth loop */

    } /* end of iom loop */


    if(exrcv) wx2xt(cexrcv, exrcv, optn, nxm, nxm, optn);

    free(locdat);
    free(cdata);
    free(taper);

    t1 = wallclock_time();
    vmess("kwZoMigr took: %f seconds", t1-t0);

    return;
}
Beispiel #19
0
int main (int argc, char **argv)
{
	FILE	*fp_in1, *fp_in2, *fp_out, *fp_chk, *fp_psline1, *fp_psline2;
	int		verbose, shift, k, nx1, nt1, nx2, nt2;
	int     ntmax, nxmax, ret, i, j, jmax, imax, above, check;
	int     size, ntraces, ngath, *maxval, hw, smooth;
    int     tstart, tend, scale, *xrcv;
	float   dt, d2, f1, f2, t0, t1, f1b, f2b, d1, d1b, d2b;
	float	w1, w2, dxrcv;
	float 	*tmpdata, *tmpdata2, *costaper;
	char 	*file_mute, *file_shot, *file_out;
	float   scl, sclsxgx, sclshot, xmin, xmax, tmax, lmax;
	segy	*hdrs_in1, *hdrs_in2;

	t0 = wallclock_time();
	initargs(argc, argv);
	requestdoc(1);

	if(!getparstring("file_mute", &file_mute)) file_mute=NULL;
	if(!getparstring("file_shot", &file_shot)) file_shot=NULL;
	if(!getparstring("file_out", &file_out)) file_out=NULL;
	if(!getparint("ntmax", &ntmax)) ntmax = 1024;
	if(!getparint("nxmax", &nxmax)) nxmax = 512;
	if(!getparint("above", &above)) above = 0;
    if(!getparint("check", &check)) check = 0;
    if(!getparint("scale", &scale)) scale = 0;
    if(!getparint("hw", &hw)) hw = 15;
    if(!getparint("smooth", &smooth)) smooth = 0;
	if(!getparfloat("w1", &w1)) w1=1.0;
	if(!getparfloat("w2", &w2)) w2=1.0;
	if(!getparint("shift", &shift)) shift=0;
	if(!getparint("verbose", &verbose)) verbose=0;

/* Reading input data for file_mute */

    if (file_mute != NULL) {
        ngath = 1;
        getFileInfo(file_mute, &nt1, &nx1, &ngath, &d1, &d2, &f1, &f2, &xmin, &xmax, &sclsxgx, &ntraces);

        if (!getparint("ntmax", &ntmax)) ntmax = nt1;
        if (!getparint("nxmax", &nxmax)) nxmax = nx1;
        if (verbose>=2 && (ntmax!=nt1 || nxmax!=nx1))
            vmess("dimensions overruled: %d x %d",ntmax,nxmax);
		if(!getparfloat("dt", &dt)) dt=d1;

        fp_in1 = fopen(file_mute, "r");
        if (fp_in1 == NULL) verr("error on opening input file_mute=%s", file_mute);

        size = ntmax * nxmax;
        tmpdata = (float *)malloc(size*sizeof(float));
        hdrs_in1 = (segy *) calloc(nxmax,sizeof(segy));

        nx1 = readData(fp_in1, tmpdata, hdrs_in1, nt1);
        if (nx1 == 0) {
            fclose(fp_in1);
            if (verbose) vmess("end of file_mute data reached");
        }

        if (verbose) {
            disp_fileinfo(file_mute, nt1, nx1, f1,  f2,  dt,  d2, hdrs_in1);
        }
    }

/* Reading input data for file_shot */

	ngath = 1;
	getFileInfo(file_shot, &nt2, &nx2, &ngath, &d1b, &d2b, &f1b, &f2b, &xmin, &xmax, &sclshot, &ntraces);

	if (!getparint("ntmax", &ntmax)) ntmax = nt2;
	if (!getparint("nxmax", &nxmax)) nxmax = nx2;

	size = ntmax * nxmax;
	tmpdata2 = (float *)malloc(size*sizeof(float));
	hdrs_in2 = (segy *) calloc(nxmax,sizeof(segy));

	if (file_shot != NULL) fp_in2 = fopen(file_shot, "r");
	else fp_in2=stdin;
	if (fp_in2 == NULL) verr("error on opening input file_shot=%s", file_shot);

	nx2 = readData(fp_in2, tmpdata2, hdrs_in2, nt2);
	if (nx2 == 0) {
		fclose(fp_in2);
		if (verbose) vmess("end of file_shot data reached");
	}
	nt2 = hdrs_in2[0].ns;
	f1b = hdrs_in2[0].f1;
	f2b = hdrs_in2[0].f2;
	d1b = (float)hdrs_in2[0].dt*1e-6;
		
	if (verbose) {
		disp_fileinfo(file_shot, nt2, nx2, f1b,  f2b,  d1b,  d2b, hdrs_in2);
	}
    
    /* file_shot will be used as well to define the mute window */
    if (file_mute == NULL) {
        nx1=nx2;
        nt1=nt2;
        dt=d1b;
        f1=f1b;
        f2=f2b;
        tmpdata = tmpdata2;
        hdrs_in1 = hdrs_in2;
    }

	if (verbose) vmess("sampling file_mute=%d, file_shot=%d", nt1, nt2);

/*================ initializations ================*/

	maxval = (int *)calloc(nx1,sizeof(int));
	xrcv   = (int *)calloc(nx1,sizeof(int));
	
	if (file_out==NULL) fp_out = stdout;
	else {
		fp_out = fopen(file_out, "w+");
		if (fp_out==NULL) verr("error on ceating output file");
	}
    if (check!=0){
        fp_chk = fopen("check.su", "w+");
		if (fp_chk==NULL) verr("error on ceating output file");
        fp_psline1 = fopen("pslinepos.asci", "w+");
		if (fp_psline1==NULL) verr("error on ceating output file");
        fp_psline2 = fopen("pslineneg.asci", "w+");
		if (fp_psline2==NULL) verr("error on ceating output file");
        
    }
	if (smooth) {
		costaper = (float *)malloc(smooth*sizeof(float));
		scl = M_PI/((float)smooth);
		for (i=0; i<smooth; i++) {
			costaper[i] = 0.5*(1.0+cos((i+1)*scl));
/*			fprintf(stderr,"costaper[%d]=%f\n",i,costaper[i]);*/
		}
	}

/*================ loop over all shot records ================*/

	k=1;
	while (nx1 > 0) {
		if (verbose) vmess("processing input gather %d", k);

/*================ loop over all shot records ================*/

        /* find consistent (one event) maximum related to maximum value */
        
        /* find global maximum 
        xmax=0.0;
		for (i = 0; i < nx1; i++) {
			tmax=0.0;
			jmax = 0;
			for (j = 0; j < nt1; j++) {
                lmax = fabs(tmpdata[i*nt1+j]);
				if (lmax > tmax) {
					jmax = j;
					tmax = lmax;
                    if (lmax > xmax) {
                        imax = i;
                        xmax=lmax;
                    }
				}
			}
			maxval[i] = jmax;
		}
		*/

		/* alternative find maximum at source position */
        dxrcv = (hdrs_in1[nx1-1].gx - hdrs_in1[0].gx)*sclsxgx/(float)(nx1-1);
		imax = NINT(((hdrs_in1[0].sx-hdrs_in1[0].gx)*sclsxgx)/dxrcv);
		tmax=0.0;
		jmax = 0;
		for (j = 0; j < nt1; j++) {
            lmax = fabs(tmpdata[imax*nt1+j]);
			if (lmax > tmax) {
				jmax = j;
				tmax = lmax;
                   if (lmax > xmax) {
                       xmax=lmax;
                   }
			}
		}
		maxval[imax] = jmax;
		if (verbose >= 3) vmess("Mute max at src-trace %d is sample %d", imax, maxval[imax]);

        /* search forward */
        for (i = imax+1; i < nx1; i++) {
            tstart = MAX(0, (maxval[i-1]-hw));
            tend   = MIN(nt1-1, (maxval[i-1]+hw));
            jmax=tstart;
            tmax=0.0;
            for(j = tstart; j <= tend; j++) {
                lmax = fabs(tmpdata[i*nt1+j]);
                if (lmax > tmax) {
                    jmax = j;
                    tmax = lmax;
                }
            }
            maxval[i] = jmax;
        }
        /* search backward */
        for (i = imax-1; i >=0; i--) {
            tstart = MAX(0, (maxval[i+1]-hw));
            tend   = MIN(nt1-1, (maxval[i+1]+hw));
            jmax=tstart;
            tmax=0.0;
            for(j = tstart; j <= tend; j++) {
                lmax = fabs(tmpdata[i*nt1+j]);
                if (lmax > tmax) {
                    jmax = j;
                    tmax = lmax;
                }
            }
            maxval[i] = jmax;
        }

/* scale with maximum ampltiude */

		if (scale==1) {
			for (i = 0; i < nx2; i++) {
				lmax = fabs(tmpdata2[i*nt2+maxval[i]]);
				xrcv[i] = i;
				for (j = 0; j < nt2; j++) {
					tmpdata2[i*nt2+j] = tmpdata2[i*nt2+j]/lmax;
				}
			}
		}

/*================ apply mute window ================*/

		applyMute(tmpdata2, maxval, smooth, above, 1, nx2, nt2, xrcv, nx2, shift);

/*================ write result to output file ================*/

		ret = writeData(fp_out, tmpdata2, hdrs_in2, nt2, nx2);
		if (ret < 0 ) verr("error on writing output file.");

        /* put mute window in file to check correctness of mute */
        if (check !=0) {
            for (i = 0; i < nx1; i++) {
                jmax = maxval[i]-shift;
                tmpdata[i*nt1+jmax] = 2*xmax;
			}
			if (above==0){
            	for (i = 0; i < nx1; i++) {
                	jmax = nt2-maxval[i]+shift;
                	tmpdata[i*nt1+jmax] = 2*xmax;
				}
			}
            ret = writeData(fp_chk, tmpdata, hdrs_in1, nt1, nx1);
            if (ret < 0 ) verr("error on writing check file.");
			for (i=0; i<nx1; i++) {
				jmax = maxval[i]-shift;
            	ret = fprintf(fp_psline1, "%.5f %.5f \n",jmax*dt,hdrs_in1[i].gx*sclshot);
				jmax =-maxval[i]+shift;
            	ret = fprintf(fp_psline2, "%.5f %.5f \n",jmax*dt,hdrs_in1[i].gx*sclshot);
			}
        }

/*================ Read next record for muting ================*/

        if (file_mute != NULL) {    
            nx1 = readData(fp_in1, tmpdata, hdrs_in1, nt1);
            if (nx1 == 0) {
                fclose(fp_in1);
                if (verbose) vmess("end of file_mute data reached");
                fclose(fp_in2);
                if (fp_out!=stdout) fclose(fp_out);
                if (check!=0) fclose(fp_chk);
                if (check!=0) {
					fclose(fp_psline1);
					fclose(fp_psline2);
				}
                break;
            }
            nt1 = (int)hdrs_in1[0].ns;
            if (nt1 > ntmax) verr("n_samples (%d) greater than ntmax", nt1);
            if (nx1 > nxmax) verr("n_traces  (%d) greater than nxmax", nx1);
        }

/*================ Read next shot record(s) ================*/

        nx2 = readData(fp_in2, tmpdata2, hdrs_in2, nt2);
        if (nx2 == 0) {
            if (verbose) vmess("end of file_shot data reached");
            fclose(fp_in2);
            break;
        }
        nt2 = (int)hdrs_in2[0].ns;
        if (nt2 > ntmax) verr("n_samples (%d) greater than ntmax", nt2);
        if (nx2 > nxmax) verr("n_traces  (%d) greater than nxmax", nx2);

        if (file_mute == NULL) {
            nx1=nx2;
            nt1=nt2;
        	hdrs_in1 = hdrs_in2;
            tmpdata = tmpdata2;
        }

		k++;
	}

	t1 = wallclock_time();
	if (verbose) vmess("Total CPU-time = %f",t1-t0);
	

	return 0;
}
int main (int argc, char **argv)
{
	FILE *fp_in, *fp_shot, *fp_out;
	char *fin, *fshot, *fout, *ptr, fbegin[100], fend[100], fins[100], fin2[100];
	float *indata, *Ghom, *shotdata, *rtrace, *costaper, scl, rho, *taper;
	float dt, dx, t0, x0, xmin, xmax1, sclsxgx, f1, f2, dxrcv, dzrcv, dxpos, dw;
	int nshots, nt, nw, nx, ntraces, ret, ix, it, is, ir, pos, ifile, file_det, nxs, nzs, sxmin, sxmax;
	int pos1, xcount, zcount, npos, zmax, file_cl, ht, inx, numb, dnumb, indrcv, shift;
	int rmt, smooth, *tol, tolside, tolset, mode, i, j, ntap;
	segy *hdr_in, *hdr_out, *hdr_shot;

	initargs(argc, argv);
	requestdoc(1);

	if (!getparstring("fin", &fin)) fin = NULL;
	if (!getparstring("fshot", &fshot)) fshot = NULL;
    if (!getparstring("fout", &fout)) fout = "out.su";
	if (!getparint("zmax", &zmax)) zmax = 0;
	if (!getparint("inx", &inx)) inx = 0;
	if (!getparfloat("zrcv", &f1)) f1 = 0;
    if (!getparfloat("xrcv", &f2)) f2 = 0;
	if (!getparfloat("dzrcv", &dzrcv)) dzrcv = -1;
    if (!getparfloat("dxrcv", &dxrcv)) dxrcv = -1;
	if (!getparfloat("rho", &rho)) rho=1000.0;
	if (!getparint("numb", &numb)) numb=0;
    if (!getparint("dnumb", &dnumb)) dnumb=1;
	if (!getparint("tolset", &tolset)) tolset=10;
	if (!getparint("mode", &mode)) mode=0;
	if (!getparint("ntap", &ntap)) ntap=0;
	if (fin == NULL) verr("Incorrect f2 input");
	if (fshot == NULL) verr("Incorrect Green input");

	if (dnumb == 0) dnumb = 1;

	ptr  = strstr(fin,"z0");
	pos1 = ptr - fin + 1;

   	sprintf(fbegin,"%*.*s", pos1-1, pos1-1, fin);
   	sprintf(fend,"%s", fin+pos1+1);

	file_det = 1;
	zcount=0;
	nzs=0;

	while (file_det) {
        sprintf(fins,"z%d",nzs*dnumb+numb);
        sprintf(fin,"%s%s%s",fbegin,fins,fend);
        fp_in = fopen(fin, "r");
        if (fp_in == NULL) {
            if (nzs == 0) {
                verr("error on opening basefile=%s", fin);
            }
            else if (nzs == 1) {
                vmess("1 file detected");
				file_det = 0;
         		break;
            }
            else {
                vmess("%d files detected",nzs);
                file_det = 0;
                break;
            }
        }
        fclose(fp_in);
        nzs++;
    }

	if (inx < 1) { 
		inx = 1;
	}

	if (zmax < 1) zmax=1;
	if (zmax < nzs) nzs=zmax;

	nxs = inx;
	
	npos = nxs*nzs;

	vmess("nxs: %d, nzs: %d",nxs,nzs);

	nshots = 0;
    getFileInfo(fshot, &nt, &nx, &nshots, &dt, &dx, &t0, &x0, &xmin, &xmax1, &sclsxgx, &ntraces);

	if (dxrcv < 0) dxrcv=dx;
	if (dzrcv < 0) dzrcv=dx;

	// ngath zijn het aantal schoten
	shotdata	= (float *)malloc(nt*nx*nshots*sizeof(float));
	hdr_shot	= (segy *)calloc(nx*nshots,sizeof(segy));

	fp_shot = fopen(fshot,"r");
	if (fp_shot == NULL) {
		verr("Could not open file");
	}
	vmess("nt: %d nx: %d nshots: %d",nt,nx,nshots);
	nx = readData(fp_shot, shotdata, hdr_shot, nt);
	fclose(fp_shot);

	hdr_out     = (segy *)calloc(nxs,sizeof(segy));	
	Ghom		= (float *)calloc(nt*npos,sizeof(float));
	ht			= (int)ceil(nt/2);
	nw 			= ht+1;
	dw			= 2.0*(M_PI)/(dt*nt);
	tol			= (int *)malloc(nxs*sizeof(float));
	taper		= (float *)malloc(nx*sizeof(float));

	for (i=0; i<nx; i++) {
		taper[i] = 1.0;
	}
	if (ntap > 0) {//Create taper
		for (i=0; i<ntap; i++) {
			taper[i] = (cos((M_PI)*(i-ntap)/ntap)+1)/2.0;
			taper[nx-1-i] = (cos((M_PI)*(i-ntap)/ntap)+1)/2.0;
		}
	}

#pragma omp parallel default(shared) \
  private(rtrace,ix,it,is) \
  private(indata, hdr_in,fins,fin2,fp_in)
{
    rtrace		= (float *)malloc(nt*nx*sizeof(float));
	indata		= (float *)malloc(nt*nx*nxs*sizeof(float));
    hdr_in 		= (segy *)calloc(nx*nxs,sizeof(segy));
#pragma omp for 
	for (ir = 0; ir < nzs; ir++) {
        sprintf(fins,"z%d",ir*dnumb+numb);
		sprintf(fin2,"%s%s%s",fbegin,fins,fend);
        fp_in = fopen(fin2, "r");
		if (fp_in == NULL) {
			verr("Danger Will Robinson");
		}
		fclose(fp_in);
		readSnapData(fin2, &indata[0], &hdr_in[0], nxs, nx, nt, 0, nx, 0, nt);
		for (is = 0; is < nxs; is++) {
			convolhom(shotdata, &indata[is*nx*nt], rtrace, nx, nt, dt, 0, rho, mode);
			if (mode==0) {//single source
				for (i=0; i<nx; i++) {
            		j=0;
            		Ghom[(j+nt/2)*nxs] += (rtrace[i*nt+j] + rtrace[i*nt+j])*taper[i];
            		for (j=1; j<nt/2; j++) {
                		Ghom[(j+nt/2)*nxs] += (rtrace[i*nt+j] + rtrace[i*nt+nt-j])*taper[i];
                		Ghom[j*nxs] += (rtrace[i*nt+(j+nt/2)] + rtrace[i*nt+nt-(j+nt/2)])*taper[i];
					}
            	}
			}
			else {//multiple sources
				for (i=0; i<nx; i++) {
                    j=0;
                    Ghom[(j+nt/2)*nxs] += (rtrace[i*nt+j])*taper[i];
                    for (j=1; j<nt/2; j++) {
                        Ghom[(j+nt/2)*nxs] += (rtrace[i*nt+j])*taper[i];
                        Ghom[j*nxs] += (rtrace[i*nt+(j+nt/2)])*taper[i];
                    }
                }
			}
		}
		vmess("Creating Homogeneous Green's function at depth %d from %d depths",ir+1,nzs);
	}
    free(rtrace);
	free(indata);free(hdr_in);
}
	free(shotdata);

	vmess("nxs: %d nxz: %d f1: %.7f",nxs,nzs,f1);

	fp_out = fopen(fout, "w+");
	
	for (ir	= 0; ir < nt; ir++) {
		for (ix = 0; ix < nxs; ix++) {
            	hdr_out[ix].fldr	= ir+1;
            	hdr_out[ix].tracl	= ir*nxs+ix+1;
            	hdr_out[ix].tracf	= ix+1;
				hdr_out[ix].scalco  = hdr_shot[0].scalco;
    			hdr_out[ix].scalel	= hdr_shot[0].scalel;
				hdr_out[ix].sdepth	= hdr_shot[0].sdepth;
				hdr_out[ix].trid	= 1;
				hdr_out[ix].ns		= nzs;
				hdr_out[ix].trwf	= nxs;
				hdr_out[ix].ntr		= hdr_out[ix].fldr*hdr_out[ix].trwf;
				hdr_out[ix].f1		= f1;
				hdr_out[ix].f2		= f2/1000;
				hdr_out[ix].dt      = dt*(1E6);
				hdr_out[ix].d1      = dzrcv;
            	hdr_out[ix].d2      = dxrcv;
				hdr_out[ix].sx      = hdr_shot[0].sx;
				hdr_out[ix].gx      = (int)roundf(f2 + (ix*hdr_out[ix].d2)*1000.0);
            	hdr_out[ix].offset	= (hdr_out[ix].gx - hdr_out[ix].sx)/1000.0;
		}
		ret = writeData(fp_out, &Ghom[ir*nxs*nzs], hdr_out, nzs, nxs);
		if (ret < 0 ) verr("error on writing output file.");
	}
	
	fclose(fp_out);
	return 0;
}
void synthesisPosistions(int nx, int nt, int nxs, int nts, float dt, float *xsyn, int Nsyn, float *xrcv, float *xsrc, float fxs2, float fxs, float dxs, float dxsrc, float dx, int ixa, int ixb,  int reci, int nshots, int *ixpossyn, int *npossyn, int verbose)
{
    int     iox, inx;
    int     i, l, ixsrc, ix, dosrc, k;
    float   x0, x1;


/*================ SYNTHESIS ================*/

    for (l = 0; l < 1; l++) { /* assuming all synthesis operators cover the same lateral area */
//    for (l = 0; l < Nsyn; l++) {
        *npossyn=0;

        for (k=0; k<nshots; k++) {

            ixsrc = NINT((xsrc[k] - fxs)/dxs);
            if (verbose>=3) {
                vmess("source position:     %.2f in operator %d", xsrc[k], ixsrc);
                vmess("receiver positions:  %.2f <--> %.2f", xrcv[k*nx+0], xrcv[k*nx+nx-1]);
            }
    
            if ((NINT(xsrc[k]-fxs2) > 0) || (NINT(xrcv[k*nx+nx-1]-fxs2) > 0) ||
                (NINT(xrcv[k*nx+nx-1]-fxs) < 0) || (NINT(xsrc[k]-fxs) < 0) || 
                (NINT(xrcv[k*nx+0]-fxs) < 0) || (NINT(xrcv[k*nx+0]-fxs2) > 0) ) {
                vwarn("source/receiver positions are outside synthesis model");
                vwarn("integration calculation is stopped at gather %d", k);
                vmess("xsrc = %.2f xrcv_1 = %.2f xrvc_N = %.2f", xsrc[k], xrcv[k*nx+0], xrcv[k*nx+nx-1]);
                break;
            }
   
			iox = 0; inx = nx; 
    
            if (ixa || ixb) { 
                if (reci == 0) {
                    x0 = xsyn[l]-ixb*dxsrc; 
                    x1 = xsyn[l]+ixa*dxsrc; 
                    if ((xsrc[k] < x0) || (xsrc[k] > x1)) continue;
                    ix = NINT((xsrc[k]-x0)/dxsrc);
                    dosrc = 1;
                }
                else if (reci == 1) {
                    x0 = xsyn[l]-ixb*dxs; 
                    x1 = xsyn[l]+ixa*dxs; 
                    if (((xsrc[k] < x0) || (xsrc[k] > x1)) && 
                        (xrcv[k*nx+0] < x0) && (xrcv[k*nx+nx-1] < x0)) continue;
                    if (((xsrc[k] < x0) || (xsrc[k] > x1)) && 
                        (xrcv[k*nx+0] > x1) && (xrcv[k*nx+nx-1] > x1)) continue;
                    if ((xsrc[k] < x0) || (xsrc[k] > x1)) dosrc = 0;
                    else dosrc = 1;
                    ix = NINT((xsrc[k]-x0)/dxs);
                }
                else if (reci == 2) {
                    if (NINT(dxsrc/dx)*dx != NINT(dxsrc)) dx = dxs;
                    x0 = xsyn[l]-ixb*dx; 
                    x1 = xsyn[l]+ixa*dx; 
                    if ((xrcv[k*nx+0] < x0) && (xrcv[k*nx+nx-1] < x0)) continue;
                    if ((xrcv[k*nx+0] > x1) && (xrcv[k*nx+nx-1] > x1)) continue;
                }
            }
            else { 
                ix = k; 
                x0 = fxs; 
                x1 = fxs+dxs*nxs;
                dosrc = 1;
            }
            if (reci == 1 && dosrc) ix = NINT((xsrc[k]-x0)/dxs);
    
            if (reci < 2 && dosrc) {
                ixpossyn[*npossyn]=ixsrc;
                *npossyn += 1;
            }
            if (verbose>=3) {
            	vmess("ixpossyn[%d] = %d ixsrc=%d ix=%d", *npossyn-1, ixpossyn[*npossyn-1], ixsrc, ix);
            }
    
            if (reci == 1 || reci == 2) {
                for (i = iox; i < inx; i++) {
                    if ((xrcv[k*nx+i] < x0) || (xrcv[k*nx+i] > x1)) continue;
                    if (reci == 1) ix = NINT((xrcv[k*nx+i]-x0)/dxs);
                    else ix = NINT((xrcv[k*nx+i]-x0)/dx);
    
                    ixpossyn[*npossyn]=ix;
                    *npossyn += 1;
    
                }
            }
    
        } /* end of Nsyn loop */

    } /* end of nshots (k) loop */

    return;
}
Beispiel #22
0
int main(int argc, char **argv)
{
	modPar mod;
	recPar rec;
	srcPar src;
	shotPar shot;
	rayPar ray;
    float *velocity, *slowness, *smooth, *trueslow, **inter;
	double t0, t1, t2, tinit, tray, tio;
	size_t size;
	int nw, n1, ix, iz, ir, ixshot, izshot;
	int nt, ntfft, nfreq, ig;
	int irec, sbox, ipos, nrx, nrz, nr;
    fcoord coordsx, coordgx, Time;
    icoord grid, isrc; 
    float Jr, *ampl, *time, *ttime, *ttime_p, cp_average, *wavelet, dw, dt;
	float dxrcv, dzrcv, rdelay, tr, dt_tmp;
    segy hdr;
    char filetime[1024], fileamp[1024], *method, *file_rcvtime, *file_src;
    size_t  nwrite, nread;
	int verbose;
    complex *cmute, *cwav;
    FILE *fpt, *fpa, *fpwav, *fprcv;

	t0= wallclock_time();
	initargs(argc,argv);
	requestdoc(0);

	if(!getparint("verbose",&verbose)) verbose=0;
    if(!getparint("sbox", &sbox)) sbox = 1;
    if(!getparstring("method", &method)) method="jesper";
	if (!getparfloat("rec_delay",&rdelay)) rdelay=0.0;

	getParameters(&mod, &rec, &src, &shot, &ray, verbose);

    /* read file_src if file_rcvtime is defined */

    if (!getparstring("file_rcvtime",&file_rcvtime)) file_rcvtime=NULL;

	if (file_rcvtime != NULL) {
    	if (!getparstring("file_src",&file_src)) file_src=NULL;
		if (!getparfloat("dt",&dt)) dt=0.004;
		if (file_src != NULL ) {
        	fpwav = fopen( file_src, "r" );
        	assert( fpwav != NULL);
        	nread = fread( &hdr, 1, TRCBYTES, fpwav );
        	assert(nread == TRCBYTES);
			ntfft = optncr(MAX(hdr.ns, rec.nt));
 			wavelet = (float *)calloc(ntfft,sizeof(float));
			/* read first trace */
        	nread = fread(wavelet, sizeof(float), hdr.ns, fpwav);
        	assert (nread == hdr.ns);
        	fclose(fpwav);
		}
		else {
			ntfft = optncr(rec.nt);
 			wavelet = (float *)calloc(ntfft,sizeof(float));
			wavelet[0] = 1.0;
		}
    	nfreq = ntfft/2+1;
    	cwav    = (complex *)calloc(nfreq,sizeof(complex));
    	cmute   = (complex *)calloc(nfreq,sizeof(complex));
        rc1fft(wavelet,cwav,ntfft,-1);
    	dw      = 2*M_PI/(ntfft*dt);
	}

	/* allocate arrays for model parameters: the different schemes use different arrays */

	n1 = mod.nz;
    if(!strcmp(method,"fd")) nw = 0;
    else nw = ray.smoothwindow;

	velocity = (float *)calloc(mod.nx*mod.nz,sizeof(float));
	slowness = (float *)calloc((mod.nx+2*nw)*(mod.nz+2*nw),sizeof(float));
    trueslow = (float *)calloc(mod.nx*mod.nz,sizeof(float));

    if(!strcmp(method,"fd")) {
		ttime = (float *)calloc(mod.nx*mod.nz,sizeof(float));
	}

	/* read velocity and density files */
	readModel(mod, velocity, slowness, nw);

	/* allocate arrays for wavefield and receiver arrays */

	size = shot.n*rec.n;
    time = (float *)calloc(size,sizeof(float));
    ampl = (float *)calloc(size,sizeof(float));

	/* Sinking source and receiver arrays: 
	   If P-velocity==0 the source and receiver 
	   postions are placed deeper until the P-velocity changes. 
	   Setting the option rec.sinkvel only sinks the receiver position 
       (not the source) and uses the velocity 
	   of the first receiver to sink through to the next layer. */

/* sink receivers to value different than sinkvel */
	for (ir=0; ir<rec.n; ir++) {
		iz = rec.z[ir];
		ix = rec.x[ir];
		while(velocity[(ix)*n1+iz] == rec.sinkvel) iz++;
		rec.z[ir]=iz+rec.sinkdepth;
		rec.zr[ir]=rec.zr[ir]+(rec.z[ir]-iz)*mod.dz;
//		rec.zr[ir]=rec.z[ir]*mod.dz;
		if (verbose>3) vmess("receiver position %d at grid[ix=%d, iz=%d] = (x=%f z=%f)", ir, ix, rec.z[ir], rec.xr[ir]+mod.x0, rec.zr[ir]+mod.z0);
	}
		vmess("   - method for ray-tracing       = %s", method);
/*
*/

/* sink sources to value different than zero */
	for (izshot=0; izshot<shot.nz; izshot++) {
		for (ixshot=0; ixshot<shot.nx; ixshot++) {
			iz = shot.z[izshot];
			ix = shot.x[ixshot];
			while(velocity[(ix)*n1+iz] == 0.0) iz++;
			shot.z[izshot]=iz+src.sinkdepth; 
		}
	}

	if (verbose>3) writeSrcRecPos(&mod, &rec, &src, &shot);

    /* smooth slowness grid */
    grid.x = mod.nx;
    grid.z = mod.nz;
    grid.y = 1;
    if ( nw != 0 ) { /* smooth slowness */ 
        smooth = (float *)calloc(grid.x*grid.z,sizeof(float));
        applyMovingAverageFilter(slowness, grid, nw, 2, smooth);
        memcpy(slowness,smooth,grid.x*grid.z*sizeof(float));
        free(smooth);
	}

    /* prepare output file and headers */
    strcpy(filetime, rec.file_rcv);
    name_ext(filetime, "_time");
    fpt = fopen(filetime, "w");
    assert(fpt != NULL);

	if (ray.geomspread) {
        strcpy(fileamp, rec.file_rcv);
        name_ext(fileamp, "_amp");
        fpa = fopen(fileamp, "w");
        assert(fpa != NULL);
	}
	if (file_rcvtime != NULL) {
        fprcv = fopen(file_rcvtime, "w");
        assert(fprcv != NULL);
	}

    memset(&hdr,0,sizeof(hdr));
    hdr.scalco = -1000;
    hdr.scalel = -1000;
    hdr.trid   = 0;

	t1=wallclock_time();
	tinit = t1-t0;
    tray=0.0;
    tio=0.0;

	/* Outer loop over number of shots */
	for (izshot=0; izshot<shot.nz; izshot++) {
		for (ixshot=0; ixshot<shot.nx; ixshot++) {

	        t2=wallclock_time();
        	if (verbose) {
            	vmess("Modeling source %d at gridpoints ix=%d iz=%d", (izshot*shot.n)+ixshot, shot.x[ixshot], shot.z[izshot]);
            	vmess(" which are actual positions x=%.2f z=%.2f", mod.x0+mod.dx*shot.x[ixshot], mod.z0+mod.dz*shot.z[izshot]);
            	vmess("Receivers at gridpoint x-range ix=%d - %d", rec.x[0], rec.x[rec.n-1]);
            	vmess(" which are actual positions x=%.2f - %.2f", mod.x0+rec.xr[0], mod.x0+rec.xr[rec.n-1]);
            	vmess("Receivers at gridpoint z-range iz=%d - %d", rec.z[0], rec.z[rec.n-1]);
            	vmess(" which are actual positions z=%.2f - %.2f", mod.z0+rec.zr[0], mod.z0+rec.zr[rec.n-1]);
        	}

        	coordsx.x = shot.x[ixshot]*mod.dx;
        	coordsx.z = shot.z[izshot]*mod.dz;
        	coordsx.y = 0;

	        t1=wallclock_time();
            tio += t1-t2;

            if (!strcmp(method,"jesper")) {
#pragma omp parallel for default(shared) \
private (coordgx,irec,Time,Jr) 
        		for (irec=0; irec<rec.n; irec++) {
            		coordgx.x=rec.xr[irec];
            		coordgx.z=rec.zr[irec];
            		coordgx.y = 0;
		
            		getWaveParameter(slowness, grid, mod.dx, coordsx, coordgx, ray, &Time, &Jr);
	
            		time[((izshot*shot.nx)+ixshot)*rec.n + irec] = Time.x + Time.y + 0.5*Time.z;
            		ampl[((izshot*shot.nx)+ixshot)*rec.n + irec] = Jr;
            		if (verbose>4) vmess("JS: shot=%f,%f receiver at %f,%f T0=%f T1=%f T2=%f Jr=%f",coordsx.x, coordsx.z, coordgx.x, coordgx.z, Time.x, Time.y, Time.z, Jr); 
        		}
			}
            else if(!strcmp(method,"fd")) {
	            int mzrcv;

                isrc.x = shot.x[ixshot];
                isrc.y = 0;
                isrc.z = shot.z[izshot];

                mzrcv = 0;
                for (irec = 0; irec < rec.n; irec++) mzrcv = MAX(rec.z[irec], mzrcv);

                vidale(ttime,slowness,&isrc,grid,mod.dx,sbox, mzrcv);
        		for (irec=0; irec<rec.n; irec++) {
            		coordgx.x=mod.x0+rec.xr[irec];
            		coordgx.z=mod.z0+rec.zr[irec];
            		coordgx.y = 0;
					ipos = ((izshot*shot.nx)+ixshot)*rec.n + irec;
	
            		time[ipos] = ttime[rec.z[irec]*mod.nx+rec.x[irec]];
					/* compute average velocity between source and receiver */
 					nrx = (rec.x[irec]-isrc.x);
 					nrz = (rec.z[irec]-isrc.z);
 					nr = abs(nrx) + abs(nrz);
					cp_average = 0.0;
        			for (ir=0; ir<nr; ir++) {
						ix = isrc.x + floor((ir*nrx)/nr);
						iz = isrc.z + floor((ir*nrz)/nr);
						//fprintf(stderr,"ir=%d ix=%d iz=%d velocity=%f\n", ir, ix, iz, velocity[ix*mod.nz+iz]);
						cp_average += velocity[ix*mod.nz+iz];
					}
					cp_average = cp_average/((float)nr);
            		ampl[ipos] = sqrt(time[ipos]*cp_average);
            		if (verbose>4) vmess("FD: shot=%f,%f receiver at %f(%d),%f(%d) T=%e V=%f Ampl=%f",coordsx.x, coordsx.z, coordgx.x, rec.x[irec], coordgx.z, rec.z[irec], time[ipos], cp_average, ampl[ipos]); 
        		}
            }
	        t2=wallclock_time();
            tray += t2-t1;

        	hdr.sx     = 1000*(mod.x0+mod.dx*shot.x[ixshot]);
        	hdr.sdepth = 1000*(mod.z0+mod.dz*shot.z[izshot]);
        	hdr.selev  = (int)(-1000.0*(mod.z0+mod.dz*shot.z[izshot]));
        	hdr.fldr   = ((izshot*shot.nx)+ixshot)+1;
        	hdr.tracl  = ((izshot*shot.nx)+ixshot)+1;
        	hdr.tracf  = ((izshot*shot.nx)+ixshot)+1;
        	hdr.ntr    = shot.n;
    		hdr.dt     = (unsigned short)1;
    		hdr.trwf   = shot.n;
    		hdr.ns     = rec.n;
        	//hdr.d1     = (rec.x[1]-rec.x[0])*mod.dx; // discrete
        	hdr.d1     = (rec.xr[1]-rec.xr[0]);
        	hdr.f1     = mod.x0+rec.x[0]*mod.dx;
        	hdr.d2     = (shot.x[MIN(shot.n-1,1)]-shot.x[0])*mod.dx;
        	hdr.f2     = mod.x0+shot.x[0]*mod.dx;
			dt_tmp = (fabs(hdr.d1*((float)hdr.scalco)));
			hdr.dt	   = (unsigned short)dt_tmp;
    
        	nwrite = fwrite( &hdr, 1, TRCBYTES, fpt);
        	assert(nwrite == TRCBYTES);
        	nwrite = fwrite( &time[((izshot*shot.nx)+ixshot)*rec.n], sizeof(float), rec.n, fpt);
        	assert(nwrite == rec.n);
	    	fflush(fpt);
	    	if (ray.geomspread) {
            	nwrite = fwrite( &hdr, 1, TRCBYTES, fpa);
            	assert(nwrite == TRCBYTES);
            	nwrite = fwrite( &ampl[((izshot*shot.nx)+ixshot)*rec.n], sizeof(float), rec.n, fpa);
            	assert(nwrite == rec.n);
	        	fflush(fpa);
        	}
			if (file_rcvtime != NULL) {
    			hdr.ns     = rec.nt;
    			hdr.trwf   = rec.n;
    			hdr.ntr    = ((izshot*shot.nx)+ixshot+1)*rec.n;
    			hdr.dt     = dt*1000000;
    			hdr.d1     = dt;
        		hdr.f1     = 0.0;
        		hdr.d2     = (rec.xr[1]-rec.xr[0]);
    			hdr.f2     = mod.x0+rec.x[0]*mod.dx;

        		for (irec=0; irec<rec.n; irec++) {
					ipos = ((izshot*shot.nx)+ixshot)*rec.n + irec;
        			hdr.tracf  = irec+1;
        			hdr.tracl  = ((izshot*shot.nx)+ixshot*shot.nz)+irec+1;
        			hdr.gx     = 1000*(mod.x0+rec.xr[irec]);
        			hdr.offset = (rec.xr[irec]-shot.x[ixshot]*mod.dx);
        			hdr.gelev  = (int)(-1000*(mod.z0+rec.zr[irec]));

					tr = time[ipos]+rdelay;
                    for (ig=0; ig<nfreq; ig++) {
                        cmute[ig].r = (cwav[ig].r*cos(ig*dw*tr-M_PI/4.0)-cwav[ig].i*sin(ig*dw*tr-M_PI/4.0))/(ntfft*ampl[ipos]);
                        cmute[ig].i = (cwav[ig].i*cos(ig*dw*tr-M_PI/4.0)+cwav[ig].r*sin(ig*dw*tr-M_PI/4.0))/(ntfft*ampl[ipos]);
                    }
                	cr1fft(cmute,wavelet,ntfft,-1);
        			nwrite = fwrite( &hdr, 1, TRCBYTES, fprcv);
        			nwrite = fwrite( wavelet, sizeof(float), rec.nt, fprcv );
				}
			}
	        t1=wallclock_time();
            tio += t1-t2;
    	} /* end of ixshot loop */
	} /* end of loop over number of shots */
	fclose(fpt);
	if (file_rcvtime != NULL) fclose(fprcv);
	if (ray.geomspread) fclose(fpa);

	t1= wallclock_time();
	if (verbose) {
		vmess("*******************************************");
		vmess("************* runtime info ****************");
		vmess("*******************************************");
		vmess("Total compute time ray-tracing    = %.2f s.", t1-t0);
		vmess("   - intializing arrays and model = %.3f", tinit);
		vmess("   - ray tracing                  = %.3f", tray);
		vmess("   - writing data to file         = %.3f", tio);
	}

	/* free arrays */

	initargs(argc,argv); /* this will free the arg arrays declared */
	free(velocity);
	free(slowness);
	
	return 0;
}
int main (int argc, char **argv)
{
	FILE *fp_in, *fp_out;
	char *fin, *fout, *ptr, fbegin[100], fend[100], fins[100], fin2[100], numb1[100];
	float *indata, *outdata, *rtrace, fz, fx;
	float dt, dx, t0, x0, xmin, xmax, sclsxgx, dt2, dx2, t02, x02, xmin2, xmax2, sclsxgx2, dxrcv, dzrcv;
	int nshots, nt, nx, ntraces, nshots2, nt2, nx2, ntraces2, ix, it, is, ir, pos, ifile, file_det, nxs, nzs;
	int xcount, numb, dnumb, ret, nzmax, verbose;
	segy *hdr_in, *hdr_out;

	initargs(argc, argv);
	requestdoc(1);

	if (!getparstring("file_in", &fin)) fin = NULL;
    if (!getparstring("file_out", &fout)) fout = "out.su";
	if (!getparint("numb", &numb)) numb=0;
	if (!getparint("dnumb", &dnumb)) dnumb=0;
	if (!getparint("nzmax", &nzmax)) nzmax=0;
	if (!getparint("verbose", &verbose)) verbose=0;
	if (fin == NULL) verr("Incorrect downgoing input");

	if (dnumb < 1) dnumb = 1;

	sprintf(numb1,"%d",numb);

	ptr  = strstr(fin,numb1);
    pos = ptr - fin + 1;

    sprintf(fbegin,"%*.*s", pos-1, pos-1, fin);
   	sprintf(fend,"%s", fin+pos);

	file_det = 1;
	nzs=0;

	while (file_det) {
        sprintf(fins,"%d",nzs*dnumb+numb);
        sprintf(fin,"%s%s%s",fbegin,fins,fend);
        fp_in = fopen(fin, "r");
        if (fp_in == NULL) {
            if (nzs == 0) {
                verr("error on opening basefile=%s", fin);
            }
            else if (nzs == 1) {
                vmess("1 file detected");
            }
            else {
                vmess("%d files detected",nzs);
                file_det = 0;
                break;
            }
        }
        fclose(fp_in);
        nzs++;
		if (nzmax!=0 && nzs == nzmax) {
			vmess("%d files detected",nzs);
            file_det = 0;
            break;
		}
    }

	sprintf(fins,"%d",numb);
    sprintf(fin2,"%s%s%s",fbegin,fins,fend);
	nshots = 0;
    getFileInfo(fin2, &nt, &nx, &nshots, &dt, &dx, &t0, &x0, &xmin, &xmax, &sclsxgx, &ntraces);

	sprintf(fins,"%d",numb+dnumb);
    sprintf(fin2,"%s%s%s",fbegin,fins,fend);
    nshots = 0;
    getFileInfo(fin2, &nt2, &nx2, &nshots2, &dt2, &dx2, &t02, &x02, &xmin2, &xmax2, &sclsxgx2, &ntraces2);

	dxrcv=dx*1000;
	dzrcv=t02-t0;

	if (nshots==0) nshots=1;
	nxs = ntraces;

	// ngath zijn het aantal schoten
	hdr_out     = (segy *)calloc(nxs,sizeof(segy));	
	outdata		= (float *)calloc(nxs*nzs,sizeof(float));
	hdr_in      = (segy *)calloc(nxs,sizeof(segy));
    indata    	= (float *)calloc(nxs,sizeof(float));

	readSnapData(fin2, &indata[0], &hdr_in[0], nshots, nxs, nt, 0, nxs, 0, nt);
	nshots 	= hdr_in[nxs-1].fldr;
	nxs		= hdr_in[nxs-1].tracf;

	for (ir = 0; ir < nzs; ir++) {
		if (verbose) vmess("Depth:%d out of %d",ir+1,nzs);
		sprintf(fins,"%d",ir*dnumb+numb);
        sprintf(fin2,"%s%s%s",fbegin,fins,fend);
        fp_in = fopen(fin2, "r");
		if (fp_in == NULL) {
			verr("Error opening file");
		}
		fclose(fp_in);
		readSnapData(fin2, &indata[0], &hdr_in[0], nshots, nxs, nt, 0, nxs, 0, nt);
		if (ir==0) fz=hdr_in[0].f1; fx=hdr_in[0].f2;
		if (ir==1) dzrcv=hdr_in[0].f1-fz;
		for (is = 0; is < nxs; is++) {
			for (it = 0; it < nshots; it++) {
				outdata[it*nxs*nzs+is*nzs+ir] = indata[it*nxs+is];
			}
		}
	}
	free(indata);

	fp_out = fopen(fout, "w+");

	for (is = 0; is < nshots; is++) {
		for (ix = 0; ix < nxs; ix++) {
           	hdr_out[ix].fldr	= is+1;
           	hdr_out[ix].tracl	= is*nxs+ix+1;
           	hdr_out[ix].tracf	= ix+1;
			hdr_out[ix].scalco  = -1000;
   			hdr_out[ix].scalel	= -1000;
			hdr_out[ix].sdepth	= hdr_in[0].sdepth;
			hdr_out[ix].trid	= 1;
			hdr_out[ix].ns		= nzs;
			hdr_out[ix].trwf	= nxs;
			hdr_out[ix].ntr		= hdr_out[ix].fldr*hdr_out[ix].trwf;
			hdr_out[ix].f1		= fz;
			hdr_out[ix].f2		= fx;
			hdr_out[ix].dt      = dt*(1E6);
			hdr_out[ix].d1      = dzrcv;
           	hdr_out[ix].d2      = dxrcv;
			hdr_out[ix].sx      = (int)roundf(fx + (ix*hdr_out[ix].d2));
			hdr_out[ix].gx      = (int)roundf(fx + (ix*hdr_out[ix].d2));
           	hdr_out[ix].offset	= (hdr_out[ix].gx - hdr_out[ix].sx)/1000.0;
		}
		ret = writeData(fp_out, &outdata[is*nxs*nzs], hdr_out, nzs, nxs);
		if (ret < 0 ) verr("error on writing output file.");
	}
	
	fclose(fp_out);
	return 0;
}
Beispiel #24
0
int main(int argc, char **argv)
{
	FILE	*fp_in, *fp_out;
	int     ret, verbose, i, j;
	size_t  size, nread;
    int     n1, n2, n3, stype, num;
	float   d1, d2, d3; 
    double  *ddata;
    float   *data;
	char  	*file_in, *file_out;
	segy	*hdrs;

	initargs(argc, argv);
	requestdoc(1);

	if(!getparint("verbose", &verbose)) verbose = 0;

	if(!getparstring("file_in", &file_in)) {
		if (verbose) vwarn("parameter file_in not found, assume pipe");
		file_in = NULL;
	}
	if(!getparstring("file_out", &file_out)){
		if (verbose) vwarn("parameter file_out not found, assume pipe");
		file_out = NULL;
	}
	if(!getparint("stype", &stype)) stype=1;
	if(!getparint("n1", &n1)) verr("n1 must be given");
	if(!getparint("n2", &n2)) verr("n2 must be given");
	if(!getparint("n3", &n3)) n3=1;
	if(!getparfloat("d1", &d1)) d1=1.0;
	if(!getparfloat("d2", &d2)) d2=1.0;
	if(!getparfloat("d3", &d3)) d3=1.0;

/* Opening input file */
	if (file_in != NULL) fp_in = fopen(file_in, "r");
	else fp_in=stdin;
	if (fp_in == NULL) verr("error on opening input file_in=%s", file_in);
    
	size = n1 * n2;
	ddata = (double *)malloc(size*sizeof(double)*stype);
	hdrs = (segy *) calloc(n2,sizeof(segy));
	if (ddata == NULL || hdrs==NULL )
		verr("memory allocation error for input data");

    for (i = 0; i < n2; i++) {
        hdrs[i].ns = stype*n1;
        hdrs[i].f1 = 0.0;
        hdrs[i].f2 = 0.0;
        hdrs[i].d1 = d1;
        hdrs[i].d2 = d2;
        hdrs[i].dt = d1*1e+6;
        hdrs[i].tracl = i+1;
        if (stype==2) {
            hdrs[i].trid=FUNPACKNYQ;
        }
        else {
            hdrs[i].trid=TREAL;
        }
    }

    nread = fread(&ddata[0], sizeof(double), size*stype, fp_in);
    if (nread == 0) {
		fclose(fp_in);
		if (verbose) verr("error in reading data of file %s", file_in);
	}
    

	/* allocate data array */
	data = (float *)malloc(stype*size*sizeof(float));
	if (data == NULL) verr("memory allocation error for data");

	/* create output file */
	if (file_out==NULL) fp_out = stdout;
	else {
		fp_out = fopen(file_out, "w+");
		if (fp_out==NULL) verr("error on creating output file");
	}
	

	/* loop for processing all data gathers */
	num   = 1;
	while (n3 > 0) {

        /* convert data and fill headers */
        for (i = 0; i < n2; i++) {
            hdrs[i].fldr = num;
            for (j = 0; j < stype*n1; j++) {
                data[i*stype*n1+j] = (float)ddata[i*stype*n1+j];
            }
        }

/* write result to output file */

		ret = writeData(fp_out, data, hdrs, stype*n1, n2);
		if (ret < 0 ) verr("error on writing output file.");

        nread = fread(ddata, sizeof(double), size*stype, fp_in);

		if (nread == 0) {
			fclose(fp_in);
			fclose(fp_out);
			if (verbose) vmess("end of data reached");
			free(hdrs);
			free(data);
            free(ddata);
			return 0;
		}
        n3--;
        num++;
	}
	return 0;
}
long defineSource3D(wavPar wav, srcPar src, modPar mod, recPar rec, float **src_nwav, long reverse, long verbose)
{
    FILE    *fp;
    size_t  nread;
    long optn, nfreq, i, j, k, iwmax, tracesToDo;
    long iw, n1, namp, optnscale, nfreqscale;
    float scl, d1, df, deltom, om, tshift;
    float amp1, amp2, amp3;
    float *trace, maxampl, scale;
    complex *ctrace, tmp;
    segy hdr;
    
	scale = 1.0;
    n1 = wav.ns;
    if (wav.random) { /* initialize random sequence */
        srand48(wav.seed+1);
        seedCMWC4096();
        for (i=0; i<8192; i++) {
            amp1 = dcmwc4096();
        }
    }
    else {

/* read first header and last byte to get file size */

        fp = fopen( wav.file_src, "r" );
        assert( fp != NULL);
        nread = fread( &hdr, 1, TRCBYTES, fp );
        assert(nread == TRCBYTES);
    
/* read all traces */

        tracesToDo = wav.nx;
        i = 0;
        while (tracesToDo) {
            memset(&src_nwav[i][0],0,wav.nt*sizeof(float));
            nread = fread(&src_nwav[i][0], sizeof(float), hdr.ns, fp);
            assert (nread == hdr.ns);
    
            nread = fread( &hdr, 1, TRCBYTES, fp );
            if (nread==0) break;
            tracesToDo--;
            i++;
        }
        fclose(fp);
    }

    optn = loptncr(n1);
    nfreq = optn/2 + 1;
    if (wav.nt != wav.ns) {
		vmess("Sampling in wavelet is %e while for modeling is set to %e", wav.ds, mod.dt);
		vmess("Wavelet sampling will be FFT-interpolated to sampling of modeling");
		vmess("file_src Nt=%li sampling after interpolation=%li", wav.ns, wav.nt);
		optnscale  = wav.nt;
		nfreqscale = optnscale/2 + 1;
	}
	else {
		optnscale  = optn;
		nfreqscale = optnscale/2 + 1;
	}
//	fprintf(stderr,"define S optn=%li ns=%li %e nt=%li %e\n", optn, wav.ns, wav.ds, optnscale, wav.dt);

    ctrace = (complex *)calloc(nfreqscale,sizeof(complex));
    trace = (float *)calloc(optnscale,sizeof(float));

    df     = 1.0/(optn*wav.ds);
    deltom = 2.*M_PI*df;
    scl    = 1.0/optn;
    iwmax = nfreq;

    for (i=0; i<wav.nx; i++) {
        if (wav.random) {
            randomWavelet3D(wav, src, &src_nwav[i][0], src.tbeg[i], src.tend[i], verbose);
        }
        else {
            memset(&ctrace[0].r,0,nfreqscale*sizeof(complex));
            memset(&trace[0],0,optnscale*sizeof(float));
            memcpy(&trace[0],&src_nwav[i][0],n1*sizeof(float));
            rc1fft(trace,ctrace,optn,-1);
            /* Scale source from file with -j/w (=1/(jw)) for volume source injections
                no scaling is applied for volume source injection rates */
            if (src.injectionrate==0) {
                for (iw=1;iw<iwmax;iw++) {
                    om = 1.0/(deltom*iw);
                    tmp.r = om*ctrace[iw].i;
                    tmp.i = -om*ctrace[iw].r;
                    ctrace[iw].r = tmp.r;
                    ctrace[iw].i = tmp.i;
                }
            }

            if (src.type < 6) { // shift wavelet with +1/2 DeltaT due to staggered in time 
                tshift=-(0.5*rec.skipdt+1.5)*wav.dt;
                for (iw=1;iw<iwmax;iw++) {
                    om = deltom*iw*tshift;
                    tmp.r = ctrace[iw].r*cos(-om) - ctrace[iw].i*sin(-om);
                    tmp.i = ctrace[iw].i*cos(-om) + ctrace[iw].r*sin(-om);
                    ctrace[iw].r = tmp.r;
                    ctrace[iw].i = tmp.i;
                }
            }

            /* zero frequency iw=0 set to 0 if the next sample has amplitude==0*/
            amp1 = sqrt(ctrace[1].r*ctrace[1].r+ctrace[1].i*ctrace[1].i);
            if (amp1 == 0.0) {
                ctrace[0].r = ctrace[0].i = 0.0;
            }
            else { /* stabilization for w=0: extrapolate amplitudes to 0 */
                amp2 = sqrt(ctrace[2].r*ctrace[2].r+ctrace[2].i*ctrace[2].i);
                amp3 = sqrt(ctrace[3].r*ctrace[3].r+ctrace[3].i*ctrace[3].i);
                ctrace[0].r = amp1+(2.0*(amp1-amp2)-(amp2-amp3));
                ctrace[0].i = 0.0;
                if (ctrace[1].r < 0.0) {
                    ctrace[0].r *= -1.0;
                }
            }
            for (iw=iwmax;iw<nfreqscale;iw++) {
                ctrace[iw].r = 0.0;
                ctrace[iw].i = 0.0;
            }

            memset(&trace[0],0,optnscale*sizeof(float));
            cr1fft(ctrace,trace,optnscale,1);
            /* avoid a (small) spike in the last sample 
               this is done to avoid diffraction from last wavelet sample
               which will act as a pulse */
    		maxampl=0.0;
            if (reverse) {
                for (j=0; j<wav.nt; j++) {
					src_nwav[i][j] = scl*(trace[wav.nt-j-1]-trace[0]);
					maxampl = MAX(maxampl,fabs(src_nwav[i][j]));
				}
            }
            else {
                for (j=0; j<wav.nt; j++) {
					src_nwav[i][j] = scl*(trace[j]-trace[wav.nt-1]);
					maxampl = MAX(maxampl,fabs(src_nwav[i][j]));
				}
            }
			if (verbose > 3) vmess("Wavelet sampling (FFT-interpolated) done for trace %li", i);
        }
    }
	/* set values smaller than 1e-5 maxampl to zero */
	maxampl *= 1e-5;
    for (i=0; i<wav.nx; i++) {
        for (j=0; j<wav.nt; j++) {
	        if (fabs(src_nwav[i][j]) < maxampl) src_nwav[i][j] = 0.0;
	    }
	}
    free(ctrace);
    free(trace);

/* use random amplitude gain factor for each source */
    if (src.amplitude > 0.0) {
        namp=wav.nx*10;
        trace = (float *)calloc(2*namp,sizeof(float));
        for (i=0; i<wav.nx; i++) {
            if (src.distribution) {
                scl = gaussGen()*src.amplitude;
                k = (long)MAX(MIN(namp*(scl+5*src.amplitude)/(10*src.amplitude),namp-1),0);
                d1 = 10.0*src.amplitude/(namp-1);
            }
            else {
                scl = (float)(drand48()-0.5)*src.amplitude;
                k = (long)MAX(MIN(namp*(scl+1*src.amplitude)/(2*src.amplitude),namp-1),0);
                d1 = 2.0*src.amplitude/(namp-1);
            }

            trace[k] += 1.0;
/*            trace[i] = scl; */
            if (wav.random) n1 = wav.nsamp[i];
            else n1 = wav.nt;
            for (j=0; j<n1; j++) {
                src_nwav[i][j] *= scl;
            }
        }
        if (verbose>2) writesufile3D("src_ampl.su", trace, namp, 1, -5*src.amplitude, 0.0, d1, 1);

        free(trace);
    }

    if (verbose>3) writesufilesrcnwav3D("src_nwav.su", src_nwav, wav, wav.nt, wav.nx, 0.0, 0.0, wav.dt, 1);

    return 0;
}
Beispiel #26
0
int main(int argc, char **argv)
{
	modPar mod;
	recPar rec;
	snaPar sna;
	wavPar wav;
	srcPar src;
	bndPar bnd;
	shotPar shot;
	float **src_nwav;
	float *rox, *roz, *l2m, *lam, *mul;
	float *tss, *tes, *tep, *p, *q, *r;
	float *vx, *vz, *tzz, *txz, *txx;
	float *rec_vx, *rec_vz, *rec_p;
	float *rec_txx, *rec_tzz, *rec_txz;
	float *rec_pp, *rec_ss;
	float *rec_udp, *rec_udvz;
	float *beam_vx, *beam_vz, *beam_p;
	float *beam_txx, *beam_tzz, *beam_txz;
	float *beam_pp, *beam_ss;	
	float sinkvel;
	double t0, t1, t2, t3, tt, tinit;
	size_t size, sizem, nsamp, memsize;
	int n1, ix, iz, ir, ishot, i;
	int ioPx, ioPz;
	int it0, it1, its, it, fileno, isam;
	int ixsrc, izsrc;
	int verbose;

	t0= wallclock_time();
	initargs(argc,argv);
	requestdoc(0);

	if (!getparint("verbose",&verbose)) verbose=0;
	getParameters(&mod, &rec, &sna, &wav, &src, &shot, &bnd, verbose);

	/* allocate arrays for model parameters: the different schemes use different arrays */

	n1 = mod.naz;
	sizem=mod.nax*mod.naz;

	rox = (float *)calloc(sizem,sizeof(float));
	roz = (float *)calloc(sizem,sizeof(float));
	l2m = (float *)calloc(sizem,sizeof(float));
	if (mod.ischeme==2) {
		tss = (float *)calloc(sizem,sizeof(float));
		tep = (float *)calloc(sizem,sizeof(float));
		q = (float *)calloc(sizem,sizeof(float));
	}
	if (mod.ischeme>2) {
		lam = (float *)calloc(sizem,sizeof(float));
		mul = (float *)calloc(sizem,sizeof(float));
	}
	if (mod.ischeme==4) {
		tss = (float *)calloc(sizem,sizeof(float));
		tes = (float *)calloc(sizem,sizeof(float));
		tep = (float *)calloc(sizem,sizeof(float));
		r = (float *)calloc(sizem,sizeof(float));
		p = (float *)calloc(sizem,sizeof(float));
		q = (float *)calloc(sizem,sizeof(float));
	}

	/* read velocity and density files */

	readModel(mod, bnd, rox, roz, l2m, lam, mul, tss, tes, tep);

	/* read and/or define source wavelet(s) */

	/* Using a random source, which can have a random length 
	   for each source position, a pointer array with variable 
	   length (wav.nsamp[i]) is used.
	   The total length of all the source lengths together is wav.nst */
	
	if (wav.random) {
		src_nwav = (float **)calloc(wav.nx,sizeof(float *));
		src_nwav[0] = (float *)calloc(wav.nst,sizeof(float));
		assert(src_nwav[0] != NULL);
		nsamp = 0;
		for (i=0; i<wav.nx; i++) {
			src_nwav[i] = (float *)(src_nwav[0] + nsamp);
			nsamp += wav.nsamp[i];
		}
	}
	else {
		src_nwav = (float **)calloc(wav.nx,sizeof(float *));
		src_nwav[0] = (float *)calloc(wav.nt*wav.nx,sizeof(float));
		assert(src_nwav[0] != NULL);
		for (i=0; i<wav.nx; i++) {
			src_nwav[i] = (float *)(src_nwav[0] + wav.nt*i);
		}
	}

	defineSource(wav, src, src_nwav, mod.grid_dir, verbose);

	/* allocate arrays for wavefield and receiver arrays */

	vx  = (float *)calloc(sizem,sizeof(float));
	vz  = (float *)calloc(sizem,sizeof(float));
	tzz = (float *)calloc(sizem,sizeof(float)); /* =P field for acoustic */
	if (mod.ischeme>2) {
		txz = (float *)calloc(sizem,sizeof(float));
		txx = (float *)calloc(sizem,sizeof(float));
	}
	if (rec.type.vz)  rec_vz  = (float *)calloc(size,sizeof(float));
	
	size = rec.n*rec.nt;
	if (rec.type.vz)  rec_vz  = (float *)calloc(size,sizeof(float));
	if (rec.type.vx)  rec_vx  = (float *)calloc(size,sizeof(float));
	if (rec.type.p)   rec_p   = (float *)calloc(size,sizeof(float));
	if (rec.type.txx) rec_txx = (float *)calloc(size,sizeof(float));
	if (rec.type.tzz) rec_tzz = (float *)calloc(size,sizeof(float));
	if (rec.type.txz) rec_txz = (float *)calloc(size,sizeof(float));
	if (rec.type.pp)  rec_pp  = (float *)calloc(size,sizeof(float));
	if (rec.type.ss)  rec_ss  = (float *)calloc(size,sizeof(float));
    if (rec.type.ud) { /* get velcity and density at first receiver location */
		ir = mod.ioZz + rec.z[0]+(rec.x[0]+mod.ioZx)*n1;
		rec.rho = mod.dt/(mod.dx*roz[ir]);
		rec.cp  = sqrt(l2m[ir]*(roz[ir]))*mod.dx/mod.dt;
		rec_udvz  = (float *)calloc(mod.nax*rec.nt,sizeof(float));
		rec_udp   = (float *)calloc(mod.nax*rec.nt,sizeof(float));
	}
	
	if(sna.beam) {
		size = sna.nz*sna.nx;
		if (sna.type.vz)  beam_vz  = (float *)calloc(size,sizeof(float));
		if (sna.type.vx)  beam_vx  = (float *)calloc(size,sizeof(float));
		if (sna.type.p)   beam_p   = (float *)calloc(size,sizeof(float));
		if (sna.type.txx) beam_txx = (float *)calloc(size,sizeof(float));
		if (sna.type.tzz) beam_tzz = (float *)calloc(size,sizeof(float));
		if (sna.type.txz) beam_txz = (float *)calloc(size,sizeof(float));
		if (sna.type.pp)  beam_pp  = (float *)calloc(size,sizeof(float));
		if (sna.type.ss)  beam_ss  = (float *)calloc(size,sizeof(float));
	}

	t1= wallclock_time();
	if (verbose) {
		tinit = t1-t0;
		vmess("*******************************************");
		vmess("************* runtime info ****************");
		vmess("*******************************************");
		vmess("CPU time for intializing arrays and model = %f", tinit);
	}

	/* Sinking source and receiver arrays: 
	   If P-velocity==0 the source and receiver 
	   postions are placed deeper until the P-velocity changes. 
	   The free-surface position is stored in bnd.surface[ix].
	   Setting the option rec.sinkvel only sinks the receiver position 
       (not the source) and uses the velocity 
	   of the first receiver to sink through to the next layer. */

    ioPx=mod.ioPx;
    ioPz=mod.ioPz;
    if (bnd.lef==4 || bnd.lef==2) ioPx += bnd.ntap;
    if (bnd.top==4 || bnd.top==2) ioPz += bnd.ntap;
	if (rec.sinkvel) sinkvel=l2m[(rec.x[0]+ioPx)*n1+rec.z[0]+ioPz];
	else sinkvel = 0.0;

/* sink receivers to value different than sinkvel */
	for (ir=0; ir<rec.n; ir++) {
		iz = rec.z[ir];
		ix = rec.x[ir];
		while(l2m[(ix+ioPx)*n1+iz+ioPz] == sinkvel) iz++;
		rec.z[ir]=iz+rec.sinkdepth;
		rec.zr[ir]=rec.zr[ir]+(rec.z[ir]-iz)*mod.dz;
//		rec.zr[ir]=rec.z[ir]*mod.dz;
		if (verbose>3) vmess("receiver position %d at grid[ix=%d, iz=%d] = (x=%f z=%f)", ir, ix+ioPx, rec.z[ir]+ioPz, rec.xr[ir]+mod.x0, rec.zr[ir]+mod.z0);
	}

/* sink sources to value different than zero */
	for (ishot=0; ishot<shot.n; ishot++) {
		iz = shot.z[ishot];
		ix = shot.x[ishot];
		while(l2m[(ix+ioPx)*n1+iz+ioPz] == 0.0) iz++;
		shot.z[ishot]=iz+src.sinkdepth; 
	}

	/* scan for free surface boundary in case it has a topography */
	for (ix=0; ix<mod.nx; ix++) {
		iz = ioPz;
		while(l2m[(ix+ioPx)*n1+iz] == 0.0) iz++;
		bnd.surface[ix+ioPx] = iz;
		if ((verbose>3) && (iz != ioPz)) vmess("Topgraphy surface x=%.2f z=%.2f", mod.x0+mod.dx*ix, mod.z0+mod.dz*(iz-ioPz));
	}
	for (ix=0; ix<ioPx; ix++) {
		bnd.surface[ix] = bnd.surface[ioPx];
	}
	for (ix=ioPx+mod.nx; ix<mod.iePx; ix++) {
		bnd.surface[ix] = bnd.surface[mod.iePx-1];
	}
	if (verbose>3) writeSrcRecPos(&mod, &rec, &src, &shot);

	/* Outer loop over number of shots */
	for (ishot=0; ishot<shot.n; ishot++) {

		izsrc = shot.z[ishot];
		ixsrc = shot.x[ishot];
		fileno= 0;

		memset(vx,0,sizem*sizeof(float));
		memset(vz,0,sizem*sizeof(float));
		memset(tzz,0,sizem*sizeof(float));
		if (mod.ischeme==2) {
			memset(q,0,sizem*sizeof(float));
		}
		if (mod.ischeme>2) {
			memset(txz,0,sizem*sizeof(float));
			memset(txx,0,sizem*sizeof(float));
		}
		if (mod.ischeme==4) {
			memset(r,0,sizem*sizeof(float));
			memset(p,0,sizem*sizeof(float));
			memset(q,0,sizem*sizeof(float));
		}
		if (verbose) {
			if (!src.random) {
				vmess("Modeling source %d at gridpoints ix=%d iz=%d", ishot, shot.x[ishot], shot.z[ishot]);
				vmess(" which are actual positions x=%.2f z=%.2f", mod.x0+mod.dx*shot.x[ishot], mod.z0+mod.dz*shot.z[ishot]);
			}
			vmess("Receivers at gridpoint x-range ix=%d - %d", rec.x[0], rec.x[rec.n-1]);
			vmess(" which are actual positions x=%.2f - %.2f", mod.x0+rec.xr[0], mod.x0+rec.xr[rec.n-1]);
			vmess("Receivers at gridpoint z-range iz=%d - %d", rec.z[0], rec.z[rec.n-1]);
			vmess(" which are actual positions z=%.2f - %.2f", mod.z0+rec.zr[0], mod.z0+rec.zr[rec.n-1]);
		}

		if (mod.grid_dir) { /* reverse time modeling */
			it0=-mod.nt+1;
			it1=0;
			its=-1;

			it0=0;
			it1=mod.nt;
			its=1;
		}
		else {
			it0=0;
			it1=mod.nt;
			its=1;
		}

		/* Main loop over the number of time steps */
		for (it=it0; it<it1; it++) {

#pragma omp parallel default (shared) \
shared (rox, roz, l2m, lam, mul, txx, txz, tzz, vx, vz) \
shared (tss, tep, tes, r, q, p) \
shared (tinit, it0, it1, its) \
shared(beam_vx, beam_vz, beam_txx, beam_tzz, beam_txz, beam_p, beam_pp, beam_ss) \
shared(rec_vx, rec_vz, rec_txx, rec_tzz, rec_txz, rec_p, rec_pp, rec_ss) \
private (tt, t2, t3) \
shared (shot, bnd, mod, src, wav, rec, ixsrc, izsrc, it, src_nwav, verbose)
{
			switch ( mod.ischeme ) {
				case 1 : /* Acoustic FD kernel */
					if (mod.iorder==2) {
						acoustic2(mod, src, wav, bnd, it, ixsrc, izsrc, src_nwav, 
							vx, vz, tzz, rox, roz, l2m, verbose);
					}
					else if (mod.iorder==4) {
                        if (mod.sh) {
                            acousticSH4(mod, src, wav, bnd, it, ixsrc, izsrc, src_nwav, 
                                  vx, vz, tzz, rox, roz, l2m, verbose);
                        }
                        else {
                            acoustic4(mod, src, wav, bnd, it, ixsrc, izsrc, src_nwav, 
                                      vx, vz, tzz, rox, roz, l2m, verbose);
                        }
					}
					else if (mod.iorder==6) {
						acoustic6(mod, src, wav, bnd, it, ixsrc, izsrc, src_nwav, 
							vx, vz, tzz, rox, roz, l2m, verbose);
					}
					break;
				case 2 : /* Visco-Acoustic FD kernel */
					viscoacoustic4(mod, src, wav, bnd, it, ixsrc, izsrc, src_nwav, 
							vx, vz, tzz, rox, roz, l2m, tss, tep, q, verbose);
					break;
				case 3 : /* Elastic FD kernel */
                    if (mod.iorder==4) {
                        elastic4(mod, src, wav, bnd, it, ixsrc, izsrc, src_nwav, 
                            vx, vz, tzz, txx, txz, rox, roz, l2m, lam, mul, verbose);
					}
					else if (mod.iorder==6) {
                        elastic6(mod, src, wav, bnd, it, ixsrc, izsrc, src_nwav, 
							vx, vz, tzz, txx, txz, rox, roz, l2m, lam, mul, verbose);
                    }
					break;
				case 4 : /* Visco-Elastic FD kernel */
					viscoelastic4(mod, src, wav, bnd, it, ixsrc, izsrc, src_nwav, 
						vx, vz, tzz, txx, txz, rox, roz, l2m, lam, mul, 
						tss, tep, tes, r, q, p, verbose);
					break;
			}

			/* write samples to file if rec.nt samples are calculated */

#pragma omp master
{
			if ( (((it-rec.delay) % rec.skipdt)==0) && (it >= rec.delay) ) {
				int writeToFile, itwritten;

				writeToFile = ! ( (((it-rec.delay)/rec.skipdt)+1)%rec.nt );
				itwritten   = fileno*(rec.nt)*rec.skipdt;
				isam        = (it-rec.delay-itwritten)/rec.skipdt;

				/* store time at receiver positions */
				getRecTimes(mod, rec, bnd, it, isam, vx, vz, tzz, txx, txz, 
					rec_vx, rec_vz, rec_txx, rec_tzz, rec_txz, 
					rec_p, rec_pp, rec_ss, rec_udp, rec_udvz, verbose);
			
				/* at the end of modeling a shot, write receiver array to output file(s) */
				if (writeToFile && (it+rec.skipdt <= it1-1) ) {
					fileno = ( ((it-rec.delay)/rec.skipdt)+1)/rec.nt;
					writeRec(rec, mod, bnd, wav, ixsrc, izsrc, isam+1, ishot, fileno,
						rec_vx, rec_vz, rec_txx, rec_tzz, rec_txz, 
						rec_p, rec_pp, rec_ss, rec_udp, rec_udvz, verbose);
				}
			}

			/* write snapshots to output file(s) */
			if (sna.nsnap) {
				writeSnapTimes(mod, sna, ixsrc, izsrc, it, vx, vz, tzz, txx, txz, verbose);
			}

			/* calculate beams */
			if(sna.beam) {
				getBeamTimes(mod, sna, vx, vz, tzz, txx,  txz, 
					beam_vx, beam_vz, beam_txx, beam_tzz, beam_txz, 
					beam_p, beam_pp, beam_ss, verbose);
			}
}
					
			/* taper the edges of the model */
//			taperEdges(mod, bnd, vx, vz, verbose);

#pragma omp master
{
			if (verbose) {
				if (it==it0+100*its) t2=wallclock_time();
				if (it==(it0+500*its)) {
					t3=wallclock_time();
					tt=(t3-t2)*(((it1-it0)*its)/400.0);
					vmess("Estimated compute time = %.2f s. per shot.",tt);
					vmess("Estimated total compute time = %.2f s.",tinit+shot.n*tt);
				}
			}
}
} /* end of OpenMP parallel section */

		} /* end of loop over time steps it */

		/* write output files: receivers and or beams */
		if (fileno) fileno++;
		
		if (rec.scale==1) { /* scale receiver with distance src-rcv */
			float xsrc, zsrc, Rrec, rdx, rdz;
			int irec;
			xsrc=mod.x0+mod.dx*ixsrc;
			zsrc=mod.z0+mod.dz*izsrc;
			for (irec=0; irec<rec.n; irec++) {
				rdx=mod.x0+rec.xr[irec]-xsrc;
				rdz=mod.z0+rec.zr[irec]-zsrc;
				Rrec = sqrt(rdx*rdx+rdz*rdz);
				fprintf(stderr,"Rec %d is scaled with distance %f R=%.2f,%.2f S=%.2f,%.2f\n", irec, Rrec,rdx,rdz,xsrc,zsrc);
				for (it=0; it<rec.nt; it++) {
					rec_p[irec*rec.nt+it] *= sqrt(Rrec);
				}
			}
		}
		writeRec(rec, mod, bnd, wav, ixsrc, izsrc, isam+1, ishot, fileno,
			rec_vx, rec_vz, rec_txx, rec_tzz, rec_txz, 
			rec_p, rec_pp, rec_ss, rec_udp, rec_udvz, verbose);
		
		writeBeams(mod, sna, ixsrc, izsrc, ishot, fileno, 
				   beam_vx, beam_vz, beam_txx, beam_tzz, beam_txz, 
				   beam_p, beam_pp, beam_ss, verbose);
		

	} /* end of loop over number of shots */


	t1= wallclock_time();
	if (verbose) {
		vmess("Total compute time FD modelling = %.2f s.", t1-t0);
	}

	/* free arrays */
	
	free(rox);
	free(roz);
	free(l2m);
	free(src_nwav[0]);
	free(src_nwav);
	free(vx);
	free(vz);
	free(tzz);
	if (rec.type.vz)  free(rec_vz);
	if (rec.type.vx)  free(rec_vx);
	if (rec.type.p)   free(rec_p);
	if (rec.type.txx) free(rec_txx);
	if (rec.type.tzz) free(rec_tzz);
	if (rec.type.txz) free(rec_txz);
	if (rec.type.pp)  free(rec_pp);
	if (rec.type.ss)  free(rec_ss);
	if (rec.type.ud)  {
		free(rec_udvz);
		free(rec_udp);
	}
	if(sna.beam) {
		if (sna.type.vz)  free(beam_vz);
		if (sna.type.vx)  free(beam_vx);
		if (sna.type.p)   free(beam_p);
		if (sna.type.txx) free(beam_txx);
		if (sna.type.tzz) free(beam_tzz);
		if (sna.type.txz) free(beam_txz);
		if (sna.type.pp)  free(beam_pp);
		if (sna.type.ss)  free(beam_ss);
	}
	
	if (mod.ischeme==2) {
		free(tss);
		free(tep);
		free(q);
	}
	if (mod.ischeme>2) {
		free(lam);
		free(mul);
		free(txz);
		free(txx);
	}
	if (mod.ischeme==4) {
		free(tss);
		free(tes);
		free(tep);
		free(r);
		free(p);
		free(q);
	}


	return 0;
}
int getWaveletInfo(char *file_src, int *n1, int *n2, float *d1, float *d2, float *f1, float *f2, float *fmax, int *nxm, int verbose)
{
    FILE    *fp;
    size_t  nread, trace_sz;
    off_t   bytes;
    int     ret, one_shot, ntraces;
    int     optn, nfreq, i, iwmax;
    float   *trace;
	float   ampl, amplmax, tampl, tamplmax; 
    complex *ctrace;
    segy hdr;
    
    if (file_src == NULL) return 0; /* Input pipe can not be handled */
    else fp = fopen( file_src, "r" );
    assert( fp != NULL);
    nread = fread( &hdr, 1, TRCBYTES, fp );
    assert(nread == TRCBYTES);
    ret = fseeko( fp, 0, SEEK_END );
	if (ret<0) perror("fseeko");
    bytes = ftello( fp );

    *n1 = hdr.ns;
    if (hdr.trid == 1 || hdr.dt != 0) {
        *d1 = ((float) hdr.dt)*1.e-6;
        *f1 = ((float) hdr.delrt)/1000.;
		if (*d1 == 0.0) *d1 = hdr.d1;
    }
    else {
        *d1 = hdr.d1;
        *f1 = hdr.f1;
    }
    *f2 = hdr.f2;

    trace_sz = (size_t)(sizeof(float)*(*n1)+TRCBYTES);
    ntraces  = (int) (bytes/trace_sz);
	*n2 = ntraces;

    /* check to find out number of traces in shot gather */

	optn = optncr(*n1);
	nfreq = optn/2 + 1;
	ctrace = (complex *)malloc(nfreq*sizeof(complex));
    one_shot = 1;
    trace = (float *)malloc(optn*sizeof(float));
    fseeko( fp, TRCBYTES, SEEK_SET );

    while (one_shot) {
		memset(trace,0,optn*sizeof(float));
        nread = fread( trace, sizeof(float), *n1, fp );
        assert (nread == *n1);
		tamplmax = 0.0;
		for (i=0;i<(*n1);i++) {
			tampl = fabsf(trace[i]);
			if (tampl > tamplmax) tamplmax = tampl;
		}
		if (trace[0]*1e-3 > tamplmax) {
			fprintf(stderr,"WARNING: file_src has a large amplitude %f at t=0\n", trace[0]);
			fprintf(stderr,"This will introduce high frequencies and can cause dispersion.\n");
		}

		/* estimate maximum frequency assuming amplitude spectrum is smooth */
		rc1fft(trace,ctrace,optn,1);

		/* find maximum amplitude */
		amplmax = 0.0;
		iwmax = 0;
		for (i=0;i<nfreq;i++) {
			ampl = sqrt(ctrace[i].r*ctrace[i].r+ctrace[i].i*ctrace[i].i);
			if (ampl > amplmax) {
				amplmax = ampl;
				iwmax = i;
			}
		}
		/* from the maximum amplitude position look for the largest frequency
         * which has an amplitude 400 times weaker than the maximum amplitude */
		for (i=iwmax;i<nfreq;i++) {
			ampl = sqrt(ctrace[i].r*ctrace[i].r+ctrace[i].i*ctrace[i].i);
			if (400*ampl < amplmax) {
				*fmax = (i-1)*(1.0/(optn*(*d1)));
				break;
			}
		}

        nread = fread( &hdr, 1, TRCBYTES, fp );
        if (nread==0) break;
    }
	*nxm = (int)ntraces;

	if (verbose>2) {
		vmess("For file %s", file_src);
		vmess("nt=%d nx=%d", *n1, *n2);
		vmess("dt=%f dx=%f", *d1, *d2);
		vmess("fmax=%f", *fmax);
		vmess("tstart=%f", *f1);
	}

    fclose(fp);
    free(trace);
    free(ctrace);

    return 0;
}
Beispiel #28
0
int writeSnapTimes(modPar mod, snaPar sna, bndPar bnd, int ixsrc, int izsrc, int itime, float *vx, float *vz, float *tzz, float *txx, float *txz, int verbose)
{
	FILE    *fpvx, *fpvz, *fptxx, *fptzz, *fptxz, *fpp, *fppp, *fpss;
	int append, isnap;
	static int first=1;
	int n1, ibndx, ibndz, ixs, izs, ize, i, j;
	int ix, iz, ix2;
	float *snap, sdx;
	segy hdr;

	if (sna.nsnap==0) return 0;

    ibndx = mod.ioXx;
    ibndz = mod.ioXz;
	n1    = mod.naz;
	sdx   = 1.0/mod.dx;

	if (sna.withbnd) {
		sna.nz=mod.naz;
		sna.z1=0;
		sna.z2=mod.naz-1;
		sna.skipdz=1;

		sna.nx=mod.nax;
		sna.x1=0;
		sna.x2=mod.nax-1;
		sna.skipdx=1;
	}

	/* check if this itime is a desired snapshot time */
	if ( (((itime-sna.delay) % sna.skipdt)==0) && 
		  (itime >= sna.delay) &&
		  (itime <= sna.delay+(sna.nsnap-1)*sna.skipdt) ) {

		isnap = NINT((itime-sna.delay)/sna.skipdt);
		if (verbose) vmess("Writing snapshot(%d) at time=%.4f", isnap+1, itime*mod.dt);
	
		if (first) {
			append=0;
			first=0;
		}
		else {
			append=1;
		}

		if (sna.type.vx)  fpvx  = fileOpen(sna.file_snap, "_svx", append);
		if (sna.type.vz)  fpvz  = fileOpen(sna.file_snap, "_svz", append);
		if (sna.type.p)   fpp   = fileOpen(sna.file_snap, "_sp", append);
		if (sna.type.txx) fptxx = fileOpen(sna.file_snap, "_stxx", append);
		if (sna.type.tzz) fptzz = fileOpen(sna.file_snap, "_stzz", append);
		if (sna.type.txz) fptxz = fileOpen(sna.file_snap, "_stxz", append);
		if (sna.type.pp)  fppp  = fileOpen(sna.file_snap, "_spp", append);
		if (sna.type.ss)  fpss  = fileOpen(sna.file_snap, "_sss", append);
	
		memset(&hdr,0,TRCBYTES);
		hdr.dt     = 1000000*(sna.skipdt*mod.dt);
		hdr.ungpow  = (sna.delay*mod.dt);
		hdr.scalco = -1000;
		hdr.scalel = -1000;
		hdr.sx     = 1000*(mod.x0+ixsrc*mod.dx);
		hdr.sdepth = 1000*(mod.z0+izsrc*mod.dz);
		hdr.fldr   = isnap+1;
		hdr.trid   = 1;
		hdr.ns     = sna.nz;
		hdr.trwf   = sna.nx;
		hdr.ntr    = (isnap+1)*sna.nx;
		hdr.f1     = sna.z1*mod.dz+mod.z0;
		hdr.f2     = sna.x1*mod.dx+mod.x0;
		hdr.d1     = mod.dz*sna.skipdz;
		hdr.d2     = mod.dx*sna.skipdx;
		if (sna.withbnd) {
        	if ( !ISODD(bnd.top)) hdr.f1 = mod.z0 - bnd.ntap*mod.dz;
        	if ( !ISODD(bnd.lef)) hdr.f2 = mod.x0 - bnd.ntap*mod.dx;
        	//if ( !ISODD(bnd.rig)) ;
        	//if ( !ISODD(bnd.bot)) store=1;
		}

/***********************************************************************
* vx velocities have one sample less in x-direction
* vz velocities have one sample less in z-direction
* txz stresses have one sample less in z-direction and x-direction
***********************************************************************/

		snap = (float *)malloc(sna.nz*sizeof(float));

		/* Decimate, with skipdx and skipdz, the number of gridpoints written to file 
		   and write to file. */
		for (ixs=sna.x1, i=0; ixs<=sna.x2; ixs+=sna.skipdx, i++) {
			hdr.tracf  = i+1;
			hdr.tracl  = isnap*sna.nx+i+1;
			hdr.gx     = 1000*(mod.x0+ixs*mod.dx);
			ix = ixs+ibndx;
			ix2 = ix+1;

			izs = sna.z1+ibndz;
			ize = sna.z2+ibndz;

			if (sna.withbnd) {
				izs = 0;
				ize = sna.z2;
				ix = ixs;
				ix2 = ix;
				if (sna.type.vz || sna.type.txz) izs = -1;
        		if ( !ISODD(bnd.lef)) hdr.gx = 1000*(mod.x0 - bnd.ntap*mod.dx);
			}

			if (sna.type.vx) {
				for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
					snap[j] = vx[ix2*n1+iz];
				}
				traceWrite(&hdr, snap, sna.nz, fpvx);
			}
			if (sna.type.vz) {
				for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
					snap[j] = vz[ix*n1+iz+1];
				}
				traceWrite(&hdr, snap, sna.nz, fpvz);
			}
			if (sna.type.p) {
				for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
					snap[j] = tzz[ix*n1+iz];
				}
				traceWrite(&hdr, snap, sna.nz, fpp);
			}
			if (sna.type.tzz) {
				for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
					snap[j] = tzz[ix*n1+iz];
				}
				traceWrite(&hdr, snap, sna.nz, fptzz);
			}
			if (sna.type.txx) {
				for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
					snap[j] = txx[ix*n1+iz];
				}
				traceWrite(&hdr, snap, sna.nz, fptxx);
			}
			if (sna.type.txz) {
				for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
					snap[j] = txz[ix2*n1+iz+1];
				}
				traceWrite(&hdr, snap, sna.nz, fptxz);
			}
			/* calculate divergence of velocity field */
			if (sna.type.pp) {
				for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
					snap[j] = sdx*((vx[(ix+1)*n1+iz]-vx[ix*n1+iz])+
									(vz[ix*n1+iz+1]-vz[ix*n1+iz]));
				}
				traceWrite(&hdr, snap, sna.nz, fppp);
			}
			/* calculate rotation of velocity field */
			if (sna.type.ss) {
				for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
					snap[j] = sdx*((vx[ix*n1+iz]-vx[ix*n1+iz-1])-
									(vz[ix*n1+iz]-vz[(ix-1)*n1+iz]));
				}
				traceWrite(&hdr, snap, sna.nz, fpss);
			}

		}

		if (sna.type.vx) fclose(fpvx);
		if (sna.type.vz) fclose(fpvz);
		if (sna.type.p) fclose(fpp);
		if (sna.type.txx) fclose(fptxx);
		if (sna.type.tzz) fclose(fptzz);
		if (sna.type.txz) fclose(fptxz);
		if (sna.type.pp) fclose(fppp);
		if (sna.type.ss) fclose(fpss);

		free(snap);
	}

	return 0;
}
Beispiel #29
0
long recvPar3D(recPar *rec, float sub_x0, float sub_y0, float sub_z0, 
	float dx, float dy, float dz, long nx, long ny, long nz)
{
	float   *xrcv1, *xrcv2, *yrcv1, *yrcv2, *zrcv1, *zrcv2;
	long    i, ix, iy, ir, verbose;
	float   dxrcv, dyrcv, dzrcv, *dxr, *dyr, *dzr;
	float   rrcv, dphi, oxrcv, oyrcv, ozrcv, arcv;
	double  circ, h, a, b, e, s, xr, yr, zr, dr, srun, phase;
	float   xrange, yrange, zrange, sub_x1, sub_y1, sub_z1;
	long    Nx1, Nx2, Ny1, Ny2, Nz1, Nz2, Ndx, Ndy, Ndz, iarray, nrec, nh;
	long    nxrcv, nyrcv, nzrcv, ncrcv, nrcv, ntrcv, *nlxrcv, *nlyrcv;
	float   *xrcva, *yrcva, *zrcva;
	char*   rcv_txt;
	FILE    *fp;

	if (!getparlong("verbose", &verbose)) verbose = 0;

    /* Calculate Model Dimensions */
    sub_x1=sub_x0+(nx-1)*dx;
    sub_y1=sub_y0+(ny-1)*dy;
    sub_z1=sub_z0+(nz-1)*dz;

/* Compute how many receivers are defined and then allocate the receiver arrays */

    /* Receiver Array */
    nxrcv=countparval("xrcva");
    nyrcv=countparval("yrcva");
    nzrcv=countparval("zrcva");
    if (nxrcv!=nzrcv) verr("Number of receivers in array xrcva (%li), yrcva (%li), zrcva(%li) are not equal",nxrcv,nyrcv,nzrcv);
    if (verbose&&nxrcv) vmess("Total number of array receivers: %li",nxrcv);

    /* Linear Receiver Arrays */
	Nx1 = countparval("xrcv1");
	Nx2 = countparval("xrcv2");
	Ny1 = countparval("yrcv1");
	Ny2 = countparval("yrcv2");
	Nz1 = countparval("zrcv1");
	Nz2 = countparval("zrcv2");
    if (Nx1!=Nx2) verr("Number of receivers starting points in 'xrcv1' (%li) and number of endpoint in 'xrcv2' (%li) are not equal",Nx1,Nx2);
    if (Ny1!=Ny2) verr("Number of receivers starting points in 'yrcv1' (%li) and number of endpoint in 'yrcv2' (%li) are not equal",Ny1,Ny2);
    if (Nz1!=Nz2) verr("Number of receivers starting points in 'zrcv1' (%li) and number of endpoint in 'zrcv2' (%li) are not equal",Nz1,Nz2);
    if (Nx1!=Ny2) verr("Number of receivers starting points in 'xrcv1' (%li) and number of endpoint in 'yrcv2' (%li) are not equal",Nx1,Ny2);
    if (Nx1!=Nz2) verr("Number of receivers starting points in 'xrcv1' (%li) and number of endpoint in 'zrcv2' (%li) are not equal",Nx1,Nz2);

    rec->max_nrec=nyrcv*nxrcv;

	/* no receivers are defined use default linear array of receivers on top of model */
    if (!rec->max_nrec && Nx1==0) Nx1=1; // Default is to use top of model to record data
    if (!rec->max_nrec && Ny1==0) Ny1=1;

    if (Nx1) {
        /* Allocate Start & End Points of Linear Arrays */
        xrcv1=(float *)malloc(Nx1*sizeof(float));
        xrcv2=(float *)malloc(Nx1*sizeof(float));
        yrcv1=(float *)malloc(Nx1*sizeof(float));
        yrcv2=(float *)malloc(Nx1*sizeof(float));
        zrcv1=(float *)malloc(Nx1*sizeof(float));
        zrcv2=(float *)malloc(Nx1*sizeof(float));
        if (!getparfloat("xrcv1",xrcv1)) xrcv1[0]=sub_x0;
        if (!getparfloat("xrcv2",xrcv2)) xrcv2[0]=sub_x1;
        if (!getparfloat("yrcv1",yrcv1)) yrcv1[0]=sub_y0;
        if (!getparfloat("yrcv2",yrcv2)) yrcv2[0]=sub_y1;
        if (!getparfloat("zrcv1",zrcv1)) zrcv1[0]=sub_z0;
        if (!getparfloat("zrcv2",zrcv2)) zrcv2[0]=zrcv1[0];

		/* check if receiver arrays fit into model */
		for (iarray=0; iarray<Nx1; iarray++) {
			xrcv1[iarray] = MAX(sub_x0,      xrcv1[iarray]);
			xrcv1[iarray] = MIN(sub_x0+nx*dx,xrcv1[iarray]);
			xrcv2[iarray] = MAX(sub_x0,      xrcv2[iarray]);
			xrcv2[iarray] = MIN(sub_x0+nx*dx,xrcv2[iarray]);
			
			yrcv1[iarray] = MAX(sub_y0,      yrcv1[iarray]);
			yrcv1[iarray] = MIN(sub_y0+ny*dy,yrcv1[iarray]);
			yrcv2[iarray] = MAX(sub_y0,      yrcv2[iarray]);
			yrcv2[iarray] = MIN(sub_y0+ny*dy,yrcv2[iarray]);

			zrcv1[iarray] = MAX(sub_z0,      zrcv1[iarray]);
			zrcv1[iarray] = MIN(sub_z0+nz*dz,zrcv1[iarray]);
			zrcv2[iarray] = MAX(sub_z0,      zrcv2[iarray]);
			zrcv2[iarray] = MIN(sub_z0+nz*dz,zrcv2[iarray]);
		}

        /* Crop to Fit Model */
/* Max's addtion still have to check if it has the same fucntionality */
        for (iarray=0;iarray<Nx1;iarray++) {
            if (xrcv1[iarray]<sub_x0) {
                if (xrcv2[iarray]<sub_x0) {
                    verr("Linear array %li outside model bounds",iarray);
                }
				else {
                    vwarn("Cropping element %li of 'xrcv1' (%f) to model bounds (%f)",iarray,xrcv1[iarray],sub_x0);
                    xrcv1[iarray]=sub_x0;
                }
            } 
			else if (xrcv1[iarray] > sub_x1) {
                verr("Linear array %li outside model bounds",iarray);
            }
            if ( (xrcv2[iarray] < xrcv1[iarray]) ) {
                verr("Ill defined linear array %li, 'xrcv1' (%f) greater than 'xrcv2' (%f)",iarray,xrcv1[iarray],xrcv2[iarray]);
            }
			else if (xrcv2[iarray]>sub_x1) {
                vwarn("Cropping element %li of 'xrcv2' (%f) to model bounds (%f)",iarray,xrcv2[iarray],sub_x1);
                xrcv2[iarray]=sub_x1;
            }

            if (yrcv1[iarray]<sub_y0) {
                if (yrcv2[iarray]<sub_y0) {
                    verr("Linear array %li outside model bounds",iarray);
                }
				else {
                    vwarn("Cropping element %li of 'yrcv1' (%f) to model bounds (%f)",iarray,yrcv1[iarray],sub_y0);
                    yrcv1[iarray]=sub_y0;
                }
            } 
			else if (yrcv1[iarray] > sub_y1) {
                verr("Linear array %li outside model bounds",iarray);
            }
            if ( (yrcv2[iarray] < yrcv1[iarray]) ) {
                verr("Ill defined linear array %li, 'yrcv1' (%f) greater than 'yrcv2' (%f)",iarray,yrcv1[iarray],yrcv2[iarray]);
            }
			else if (yrcv2[iarray]>sub_y1) {
                vwarn("Cropping element %li of 'yrcv2' (%f) to model bounds (%f)",iarray,yrcv2[iarray],sub_y1);
                yrcv2[iarray]=sub_y1;
            }

            if (zrcv1[iarray] < sub_z0) {
                if (zrcv2[iarray] < sub_z0) {
                    verr("Linear array %li outside model bounds",iarray);
                }
				else {
               		vwarn("Cropping element %li of 'zrcv1' (%f) to model bounds (%f)",iarray,zrcv1[iarray],sub_z0);
                	zrcv1[iarray]=sub_z0;
                }
            }
			else if (zrcv1[iarray] > sub_z1) {
                verr("Linear array %li outside model bounds",iarray);
            }
            if ( (zrcv2[iarray] < zrcv1[iarray]) ) {
                verr("Ill defined linear array %li, 'zrcv1' (%f) greater than 'zrcv2' (%f)",iarray,zrcv1[iarray],zrcv2[iarray]);
            }
			else if (zrcv2[iarray]>sub_z1) {
                vwarn("Cropping element %li of 'xrcv2' (%f) to model bounds (%f)",iarray,zrcv2[iarray],sub_z1);
                zrcv2[iarray]=sub_z1;
            }
        }

        /* Get Sampling Rates */
		Ndx = countparval("dxrcv");
		Ndy = countparval("dyrcv");
		Ndz = countparval("dzrcv");

		dxr = (float *)malloc(Nx1*sizeof(float));
		dyr = (float *)malloc(Nx1*sizeof(float));
		dzr = (float *)malloc(Nx1*sizeof(float));
		if(!getparfloat("dxrcv", dxr)) dxr[0]=dx;
		if(!getparfloat("dyrcv", dyr)) dyr[0]=dy;
		if(!getparfloat("dzrcv", dzr)) dzr[0]=0.0;
		if ( (Ndx<=1) && (Ndy<=1) && (Ndz==0) ){ /* default values are set */
			for (i=1; i<Nx1; i++) {
				dxr[i] = dxr[0];
				dyr[i] = dyr[0];
				dzr[i] = dzr[0];
			}
			Ndx=1;
            Ndy=1;
			Ndz=1;
		}
		else if ( (Ndz==1) && (Ndx==0) && (Ndy==0) ){ /* default values are set */
			for (i=1; i<Nx1; i++) {
				dxr[i] = dxr[0];
				dyr[i] = dyr[0];
				dzr[i] = dzr[0];
			}
			Ndz=1;
            Ndy=1;
			Ndx=1;
		}
		else { /* make sure that each array has dzrcv or dxrcv defined for each line or receivers */
			if (Ndx!=Ndz) {
				verr("Number of 'dxrcv' (%li) is not equal to number of 'dzrcv' (%li) or 1",Ndx,Ndz);
			}
			if (Ndx!=Ndy) {
				verr("Number of 'dxrcv' (%li) is not equal to number of 'dyrcv' (%li) or 1",Ndx,Ndy);
			}
			if (Ndx!=Nx1 && Ndx!=1) {
				verr("Number of 'dxrcv' (%li) is not equal to number of starting points in 'xrcv1' (%li) or 1",Ndx,Nx1);
			}
			if (Ndy!=Ny1 && Ndy!=1) {
				verr("Number of 'dyrcv' (%li) is not equal to number of starting points in 'yrcv1' (%li) or 1",Ndy,Ny1);
			}
		}

		/* check consistency of receiver steps */
        for (iarray=0; iarray<Ndx; iarray++) {
            if (dxr[iarray]<0) {
				dxr[i]=dx;
				vwarn("'dxrcv' element %li (%f) is less than zero, changing it to %f'",iarray,dxr[iarray],dx);
			}
        }
        for (iarray=0; iarray<Ndy; iarray++) {
            if (dyr[iarray]<0) {
				dyr[i]=dx;
				vwarn("'dyrcv' element %li (%f) is less than zero, changing it to %f'",iarray,dyr[iarray],dy);
			}
        }
        for (iarray=0;iarray<Ndz;iarray++) {
            if (dzr[iarray]<0) {
				dzr[iarray]=dz;
				vwarn("'dzrcv' element %li (%f) is less than zero, changing it to %f'",iarray,dzr[iarray],dz);
			}
        }
        for (iarray=0;iarray<Ndx;iarray++){
            if (dxr[iarray]==0 && dzr[iarray]==0) {
                xrcv2[iarray]=xrcv1[iarray];
				dxr[iarray]=1.;
                vwarn("'dxrcv' element %li & 'dzrcv' element 1 are both 0.",iarray+1);
                vmess("Placing 1 receiver at (%li,%li)",xrcv1[iarray],zrcv1[iarray]);
            }
        }
        for (iarray=0;iarray<Ndx;iarray++){
            if (xrcv1[iarray]==xrcv2[iarray] && dxr[iarray]!=0) {
                dxr[iarray]=0.;
                vwarn("Linear array %li: 'xrcv1'='xrcv2' and 'dxrcv' is not 0. Setting 'dxrcv'=0",iarray+1);
            }
        }
        for (iarray=0;iarray<Ndx;iarray++){
            if (yrcv1[iarray]==yrcv2[iarray] && dyr[iarray]!=0) {
                dyr[iarray]=0.;
                vwarn("Linear array %li: 'yrcv1'='yrcv2' and 'dyrcv' is not 0. Setting 'dyrcv'=0",iarray+1);
            }
        }
        for (iarray=0;iarray<Ndx;iarray++){
            if (zrcv1[iarray]==zrcv2[iarray] && dzr[iarray]!=0.){
                dzr[iarray]=0.;
                vwarn("Linear array %li: 'zrcv1'='zrcv2' and 'dzrcv' is not 0. Setting 'dzrcv'=0",iarray+1);
            }
        }

        /* Calculate Number of Receivers */
		nrcv = 0;
        nlxrcv=(long *)malloc(Nx1*sizeof(long));
        nlyrcv=(long *)malloc(Nx1*sizeof(long));
		for (iarray=0; iarray<Nx1; iarray++) {
			xrange = (xrcv2[iarray]-xrcv1[iarray]); 
			yrange = (yrcv2[iarray]-yrcv1[iarray]); 
			zrange = (zrcv2[iarray]-zrcv1[iarray]); 
			if (dxr[iarray] != 0.0 && dyr[iarray] != 0.0) {
				nlxrcv[iarray] = NINT(fabs(xrange/dxr[iarray]))+1;
				nlyrcv[iarray] = NINT(fabs(yrange/dyr[iarray]))+1;
			}
			else if (dxr[iarray] != 0.0) {
				nlxrcv[iarray] = NINT(fabs(xrange/dxr[iarray]))+1;
				nlyrcv[iarray] = 1;
			}
			else if (dyr[iarray] != 0.0) {
				nlxrcv[iarray] = 1;
				nlyrcv[iarray] = NINT(fabs(yrange/dyr[iarray]))+1;
			}
			else {
				if (dzr[iarray] == 0) {
					verr("For receiver array %li: receiver distance dzrcv is not given", iarray);
				}
				nlxrcv[iarray] = NINT(fabs(zrange/dzr[iarray]))+1;
				nlyrcv[iarray] = NINT(fabs(zrange/dzr[iarray]))+1;
			}
            nrcv+=nlyrcv[iarray]*nlxrcv[iarray];
		}

        /* Calculate Number of Receivers */
        if (verbose) vmess("Total number of linear array receivers: %li",nrcv);
        if (!nrcv) {
            free(xrcv1);
            free(xrcv2);
            free(yrcv1);
            free(yrcv2);
            free(zrcv1);
            free(zrcv2);
            free(dxr);
            free(dyr);
            free(dzr);
            free(nlxrcv);
            free(nlyrcv);
        }
        rec->max_nrec+=nrcv;
    } 
	else {
		nrcv=0;
	}

/* allocate the receiver arrays */

    /* Total Number of Receivers */
    if (verbose) vmess("Total number of receivers: %li",rec->max_nrec);

    /* Allocate Arrays */
    rec->x  = (long *)calloc(rec->max_nrec,sizeof(long));
    rec->y  = (long *)calloc(rec->max_nrec,sizeof(long));
    rec->z  = (long *)calloc(rec->max_nrec,sizeof(long));
    rec->xr = (float *)calloc(rec->max_nrec,sizeof(float));
    rec->yr = (float *)calloc(rec->max_nrec,sizeof(float));
    rec->zr = (float *)calloc(rec->max_nrec,sizeof(float));

/* read in the receiver postions */

	nrec=0;
    /* Receiver Array */
	if (nxrcv != 0 && nyrcv != 0) {
		/* receiver array is defined */
		xrcva = (float *)malloc(nxrcv*sizeof(float));
		yrcva = (float *)malloc(nxrcv*sizeof(float));
		zrcva = (float *)malloc(nxrcv*sizeof(float));
		getparfloat("xrcva", xrcva);
		getparfloat("yrcva", yrcva);
		getparfloat("zrcva", zrcva);
		for (iy=0; iy<nyrcv; iy++) {
            for (ix=0; ix<nxrcv; ix++) {
                rec->xr[nrec+iy*nxrcv+ix] = xrcva[ix]-sub_x0;
                rec->yr[nrec+iy*nxrcv+ix] = yrcva[iy]-sub_y0;
                rec->zr[nrec+iy*nxrcv+ix] = zrcva[ix]-sub_z0;
                rec->x[nrec+iy*nxrcv+ix] = NINT((xrcva[ix]-sub_x0)/dx);
                rec->y[nrec+iy*nxrcv+ix] = NINT((yrcva[iy]-sub_y0)/dy);
                rec->z[nrec+iy*nxrcv+ix] = NINT((zrcva[ix]-sub_z0)/dz);
                if (verbose>4) fprintf(stderr,"Receiver Array: xrcv[%li]=%f yrcv[%li]=%f zrcv=%f\n", ix, rec->xr[nrec+ix]+sub_x0, iy, rec->yr[nrec+ix]+sub_y0, rec->zr[nrec+ix]+sub_z0);
            }
        }
		free(xrcva);
		free(yrcva);
		free(zrcva);
		nrec += nyrcv*nxrcv;
	}

    /* Linear Receiver Arrays */
    if (nrcv!=0) {
		xrcv1 = (float *)malloc(Nx1*sizeof(float));
		xrcv2 = (float *)malloc(Nx1*sizeof(float));
		yrcv1 = (float *)malloc(Nx1*sizeof(float));
		yrcv2 = (float *)malloc(Nx1*sizeof(float));
		zrcv1 = (float *)malloc(Nx1*sizeof(float));
		zrcv2 = (float *)malloc(Nx1*sizeof(float));
		
		if(!getparfloat("xrcv1", xrcv1)) xrcv1[0]=sub_x0;
		if(!getparfloat("xrcv2", xrcv2)) xrcv2[0]=(nx-1)*dx+sub_x0;
		if(!getparfloat("yrcv1", yrcv1)) yrcv1[0]=sub_y0;
		if(!getparfloat("yrcv2", yrcv2)) yrcv2[0]=(ny-1)*dy+sub_y0;
		if(!getparfloat("zrcv1", zrcv1)) zrcv1[0]=sub_z0;
		if(!getparfloat("zrcv2", zrcv2)) zrcv2[0]=zrcv1[0];		
		
		Ndx = countparval("dxrcv");
		Ndy = countparval("dyrcv");
		Ndz = countparval("dzrcv");

		dxr = (float *)malloc(Nx1*sizeof(float));
		dyr = (float *)malloc(Nx1*sizeof(float));
		dzr = (float *)malloc(Nx1*sizeof(float));
		if(!getparfloat("dxrcv", dxr)) dxr[0]=dx;
		if(!getparfloat("dyrcv", dyr)) dyr[0]=dy;
		if(!getparfloat("dzrcv", dzr)) dzr[0]=0.0;
		if ( (Ndx<=1) && (Ndy<=1) && (Ndz==0) ){ /* default values are set */
			for (i=1; i<Nx1; i++) {
				dxr[i] = dxr[0];
				dyr[i] = dyr[0];
				dzr[i] = dzr[0];
			}
			Ndx=1;
            Ndy=1;
		}
        else if ( (Ndx<=1) && (Ndy==0) && (Ndz==0) ){ /* default values are set */
			for (i=1; i<Nx1; i++) {
				dxr[i] = dxr[0];
				dyr[i] = dyr[0];
				dzr[i] = dzr[0];
			}
			Ndx=1;
		}
        else if ( (Ndy<=1) && (Ndx==0) && (Ndz==0) ){ /* default values are set */
			for (i=1; i<Nx1; i++) {
				dxr[i] = dxr[0];
				dyr[i] = dyr[0];
				dzr[i] = dzr[0];
			}
			Ndy=1;
		}
		else if ( (Ndz==1) && (Ndy==0) && (Ndx==0) ){ /* default values are set */
			for (i=1; i<Nx1; i++) {
				dxr[i] = dxr[0];
				dyr[i] = dyr[0];
				dzr[i] = dzr[0];
			}
			Ndz=1;
		}
		else { /* make sure that each array has dzrcv or dxrcv defined for each line or receivers */
			if (Ndx>1) assert(Ndx==Nx1);
			if (Ndy>1) assert(Ndy==Ny1);
			if (Ndz>1) assert(Ndz==Nx1);
		}
		
		/* check if receiver arrays fit into model */
		for (iarray=0; iarray<Nx1; iarray++) {
			xrcv1[iarray] = MAX(sub_x0,      xrcv1[iarray]);
			xrcv1[iarray] = MIN(sub_x0+nx*dx,xrcv1[iarray]);
			xrcv2[iarray] = MAX(sub_x0,      xrcv2[iarray]);
			xrcv2[iarray] = MIN(sub_x0+nx*dx,xrcv2[iarray]);

			yrcv1[iarray] = MAX(sub_y0,      yrcv1[iarray]);
			yrcv1[iarray] = MIN(sub_y0+ny*dy,yrcv1[iarray]);
			yrcv2[iarray] = MAX(sub_y0,      yrcv2[iarray]);
			yrcv2[iarray] = MIN(sub_y0+ny*dy,yrcv2[iarray]);
			
			zrcv1[iarray] = MAX(sub_z0,      zrcv1[iarray]);
			zrcv1[iarray] = MIN(sub_z0+nz*dz,zrcv1[iarray]);
			zrcv2[iarray] = MAX(sub_z0,      zrcv2[iarray]);
			zrcv2[iarray] = MIN(sub_z0+nz*dz,zrcv2[iarray]);
		}

		/* calculate receiver array and store into rec->x,y,z */

		for (iarray=0; iarray<Nx1; iarray++) {
			xrange = (xrcv2[iarray]-xrcv1[iarray]); 
			yrange = (yrcv2[iarray]-yrcv1[iarray]); 
			zrange = (zrcv2[iarray]-zrcv1[iarray]); 
			if (dxr[iarray] != 0.0) {
				nrcv = nlyrcv[iarray]*nlxrcv[iarray];
				dxrcv = dxr[iarray];
				dyrcv = yrange/(nlyrcv[iarray]-1);
				dzrcv = zrange/(nlxrcv[iarray]-1);
				if (dyrcv != dyr[iarray]) {
					vwarn("For receiver array %li: calculated dyrcv=%f given=%f", iarray, dyrcv, dyr[iarray]);
					vwarn("The calculated receiver distance %f is used", dyrcv);
				}
				if (dzrcv != dzr[iarray]) {
					vwarn("For receiver array %li: calculated dzrcv=%f given=%f", iarray, dzrcv, dzr[iarray]);
					vwarn("The calculated receiver distance %f is used", dzrcv);
				}
			}
            else if (dyr[iarray] != 0.0) {
				nrcv = nlyrcv[iarray]*nlxrcv[iarray];
				dxrcv = xrange/(nlxrcv[iarray]-1);
				dyrcv = dyr[iarray];
				dzrcv = zrange/(nlxrcv[iarray]-1);
				if (dxrcv != dxr[iarray]) {
					vwarn("For receiver array %li: calculated dxrcv=%f given=%f", iarray, dxrcv, dxr[iarray]);
					vwarn("The calculated receiver distance %f is used", dxrcv);
				}
				if (dzrcv != dzr[iarray]) {
					vwarn("For receiver array %li: calculated dzrcv=%f given=%f", iarray, dzrcv, dzr[iarray]);
					vwarn("The calculated receiver distance %f is used", dzrcv);
				}
			}
			else {
				if (dzr[iarray] == 0) {
					verr("For receiver array %li: receiver distance dzrcv is not given", iarray);
				}
				nrcv = nlyrcv[iarray]*nlxrcv[iarray];
				dxrcv = xrange/(nrcv-1);
				dyrcv = yrange/(nrcv-1);
				dzrcv = dzr[iarray];
				if (dxrcv != dxr[iarray]) {
					vwarn("For receiver array %li: calculated dxrcv=%f given=%f", iarray, dxrcv, dxr[iarray]);
					vwarn("The calculated receiver distance %f is used", dxrcv);
				}
				if (dyrcv != dyr[iarray]) {
					vwarn("For receiver array %li: calculated dyrcv=%f given=%f", iarray, dyrcv, dyr[iarray]);
					vwarn("The calculated receiver distance %f is used", dyrcv);
				}
			}

			// calculate coordinates
			for (iy=0; iy<nlyrcv[iarray]; iy++) {
                for (ix=0; ix<nlxrcv[iarray]; ix++) {
                    rec->xr[nrec]=xrcv1[iarray]-sub_x0+ix*dxrcv;
                    rec->yr[nrec]=yrcv1[iarray]-sub_y0+iy*dyrcv;
                    rec->zr[nrec]=zrcv1[iarray]-sub_z0+ix*dzrcv;

                    rec->x[nrec]=NINT((rec->xr[nrec])/dx);
                    rec->y[nrec]=NINT((rec->yr[nrec])/dy);
                    rec->z[nrec]=NINT((rec->zr[nrec])/dz);
                    nrec++;
                }
            }
		}
		free(xrcv1);
		free(xrcv2);
		free(yrcv1);
		free(yrcv2);
		free(zrcv1);
		free(zrcv2);
		free(dxr);
		free(dyr);
		free(dzr);
        free(nlxrcv);
        free(nlyrcv);
	}

    rec->n=rec->max_nrec;
	return 0;
}
long writeSnapTimes3D(modPar mod, snaPar sna, bndPar bnd, wavPar wav,
	long ixsrc, long iysrc, long izsrc, long itime, float *vx, float *vy, float *vz,
	float *tzz, float *tyy, float *txx, float *txz, float *tyz, float *txy, long verbose)
{
	FILE    *fpvx, *fpvy, *fpvz, *fptxx, *fptyy, *fptzz, *fptxz, *fptyz, *fptxy, *fpp, *fppp, *fpss;
	long append, isnap;
	static long first=1;
	long n1, n2, ibndx, ibndy, ibndz, ixs, iys, izs, ize, i, j, l;
	long ix, iy, iz, ix2, iy2;
	float *snap, sdx, stime;
	segy hdr;

	if (sna.nsnap==0) return 0;

    ibndx = mod.ioXx;
    ibndy = mod.ioXy;
    ibndz = mod.ioXz;
	n1    = mod.naz;
	n2    = mod.nax;
	sdx   = 1.0/mod.dx;

	if (sna.withbnd) {
		sna.nz=mod.naz;
		sna.z1=0;
		sna.z2=mod.naz-1;
		sna.skipdz=1;

		sna.ny=mod.nax;
		sna.y1=0;
		sna.y2=mod.nay-1;
		sna.skipdy=1;

		sna.nx=mod.nax;
		sna.x1=0;
		sna.x2=mod.nax-1;
		sna.skipdx=1;
	}

	/* check if this itime is a desired snapshot time */
	if ( (((itime-sna.delay) % sna.skipdt)==0) && 
		  (itime >= sna.delay) &&
		  (itime <= sna.delay+(sna.nsnap-1)*sna.skipdt) ) {

		isnap = NINT((itime-sna.delay)/sna.skipdt);

        if (mod.grid_dir) stime = (-wav.nt+1+itime+1)*mod.dt;  /* reverse time modeling */
        else  stime = itime*mod.dt;
		if (verbose) vmess("Writing snapshot(%li) at time=%.4f", isnap+1, stime);
	
		if (first) {
			append=0;
			first=0;
		}
		else {
			append=1;
		}

		if (sna.type.vx)  fpvx  = fileOpen(sna.file_snap, "_svx", (int)append);
		if (sna.type.vy)  fpvy  = fileOpen(sna.file_snap, "_svy", (int)append);
		if (sna.type.vz)  fpvz  = fileOpen(sna.file_snap, "_svz", (int)append);
		if (sna.type.p)   fpp   = fileOpen(sna.file_snap, "_sp", (int)append);
		if (sna.type.txx) fptxx = fileOpen(sna.file_snap, "_stxx", (int)append);
		if (sna.type.tyy) fptyy = fileOpen(sna.file_snap, "_styy", (int)append);
		if (sna.type.tzz) fptzz = fileOpen(sna.file_snap, "_stzz", (int)append);
		if (sna.type.txz) fptxz = fileOpen(sna.file_snap, "_stxz", (int)append);
		if (sna.type.tyz) fptyz = fileOpen(sna.file_snap, "_styz", (int)append);
		if (sna.type.txy) fptxy = fileOpen(sna.file_snap, "_stxy", (int)append);
		if (sna.type.pp)  fppp  = fileOpen(sna.file_snap, "_spp", (int)append);
		if (sna.type.ss)  fpss  = fileOpen(sna.file_snap, "_sss", (int)append);
	
		memset(&hdr,0,TRCBYTES);
		hdr.dt     = 1000000*(sna.skipdt*mod.dt);
		hdr.ungpow  = (sna.delay*mod.dt);
		hdr.scalco = -1000;
		hdr.scalel = -1000;
		hdr.sx     = 1000*(mod.x0+ixsrc*mod.dx);
		hdr.sy     = 1000*(mod.y0+iysrc*mod.dy);
		hdr.sdepth = 1000*(mod.z0+izsrc*mod.dz);
		hdr.fldr   = isnap+1;
		hdr.trid   = 1;
		hdr.ns     = sna.nz;
		hdr.trwf   = sna.nx*sna.nx;
		hdr.ntr    = (isnap+1)*sna.nx;
		hdr.f1     = sna.z1*mod.dz+mod.z0;
		hdr.f2     = sna.x1*mod.dx+mod.x0;
		hdr.d1     = mod.dz*sna.skipdz;
		hdr.d2     = mod.dx*sna.skipdx;
		if (sna.withbnd) {
        	if ( !ISODD(bnd.top)) hdr.f1 = mod.z0 - bnd.ntap*mod.dz;
        	if ( !ISODD(bnd.lef)) hdr.f2 = mod.x0 - bnd.ntap*mod.dx;
        	//if ( !ISODD(bnd.rig)) ;
        	//if ( !ISODD(bnd.bot)) store=1;
		}

/***********************************************************************
* vx velocities have one sample less in x-direction
* vz velocities have one sample less in z-direction
* txz stresses have one sample less in z-direction and x-direction
***********************************************************************/

		snap = (float *)malloc(sna.nz*sizeof(float));

		/* Decimate, with skipdx and skipdz, the number of gridpoints written to file 
		   and write to file. */
		for (iys=sna.y1, l=0; iys<=sna.y2; iys+=sna.skipdy, l++) {
			for (ixs=sna.x1, i=0; ixs<=sna.x2; ixs+=sna.skipdx, i++) {
				hdr.tracf  = l*sna.nx+i+1;
				hdr.tracl  = isnap*sna.nx*sna.ny+l*sna.nx+i+1;
				hdr.gx     = 1000*(mod.x0+ixs*mod.dx);
				hdr.gy     = 1000*(mod.y0+iys*mod.dy);
				ix  = ixs+ibndx;
				ix2 = ix+1;
				iy  = iys+ibndy;
				iy2 = iy+1;

				izs = sna.z1+ibndz;
				ize = sna.z2+ibndz;

				if (sna.withbnd) {
					izs = 0;
					ize = sna.z2;
					ix  = ixs;
					ix2 = ix;
					iy  = iys;
					iy2 = iy;
					if (sna.type.vz || sna.type.txz || sna.type.tyz) izs = -1;
					if ( !ISODD(bnd.lef)) hdr.gx = 1000*(mod.x0 - bnd.ntap*mod.dx);
					if ( !ISODD(bnd.fro)) hdr.gy = 1000*(mod.y0 - bnd.ntap*mod.dy);
				}

				if (sna.type.vx) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] = vx[iy*n1*n2+ix2*n1+iz];
					}
					traceWrite(&hdr, snap, (int)sna.nz, fpvx);
				}
				if (sna.type.vy) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] = vy[iy2*n1*n2+ix*n1+iz];
					}
					traceWrite(&hdr, snap, (int)sna.nz, fpvy);
				}
				if (sna.type.vz) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] = vz[iy*n1*n2+ix*n1+iz+1];
					}
					traceWrite(&hdr, snap, (int)sna.nz, fpvz);
				}
				if (sna.type.p) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] = tzz[iy*n1*n2+ix*n1+iz];
					}
					traceWrite(&hdr, snap, (int)sna.nz, fpp);
				}
				if (sna.type.tzz) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] = tzz[iy*n1*n2+ix*n1+iz];
					}
					traceWrite(&hdr, snap, (int)sna.nz, fptzz);
				}
				if (sna.type.tyy) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] = tyy[iy*n1*n2+ix*n1+iz];
					}
					traceWrite(&hdr, snap, (int)sna.nz, fptyy);
				}
				if (sna.type.txx) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] = txx[iy*n1*n2+ix*n1+iz];
					}
					traceWrite(&hdr, snap, (int)sna.nz, fptxx);
				}
				if (sna.type.txz) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] = txz[iy*n1*n2+ix2*n1+iz+1];
					}
					traceWrite(&hdr, snap, (int)sna.nz, fptxz);
				}
				if (sna.type.txy) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] = txy[iy2*n1*n2+ix2*n1+iz];
					}
					traceWrite(&hdr, snap, (int)sna.nz, fptxy);
				}
				if (sna.type.tyz) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] = tyz[iy2*n1*n2+ix*n1+iz+1];
					}
					traceWrite(&hdr, snap, (int)sna.nz, fptyz);
				}
				/* calculate divergence of velocity field */
				if (sna.type.pp) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] =  sdx*((vx[iy*n1*n2+(ix+1)*n1+iz]-vx[iy*n1*n2+ix*n1+iz])+
										(vy[(iy+1)*n1*n2+ix*n1+iz]-vy[iy*n1*n2+ix*n1+iz])+
										(vz[iy*n1*n2+ix*n1+iz+1]-vz[iy*n1*n2+ix*n1+iz]));
					}
					traceWrite(&hdr, snap, (int)sna.nz, fppp);
				}
				/* calculate rotation of velocity field */
				if (sna.type.ss) {
					for (iz=izs, j=0; iz<=ize; iz+=sna.skipdz, j++) {
						snap[j] =  sdx*((vx[iy*n1*n2+ix*n1+iz]-vx[(iy-1)*n1*n2+ix*n1+iz-1])-
										(vy[iy*n1*n2+ix*n1+iz]-vy[iy*n1*n2+(ix-1)*n1+iz-1])-
										(vz[iy*n1*n2+ix*n1+iz]-vz[(iy-1)*n1*n2+(ix-1)*n1+iz]));
					}
					traceWrite(&hdr, snap, (int)sna.nz, fpss);
				}

			}
		}

		if (sna.type.vx) fclose(fpvx);
		if (sna.type.vy) fclose(fpvy);
		if (sna.type.vz) fclose(fpvz);
		if (sna.type.p) fclose(fpp);
		if (sna.type.txx) fclose(fptxx);
		if (sna.type.tyy) fclose(fptyy);
		if (sna.type.tzz) fclose(fptzz);
		if (sna.type.txz) fclose(fptxz);
		if (sna.type.tyz) fclose(fptyz);
		if (sna.type.txy) fclose(fptxy);
		if (sna.type.pp) fclose(fppp);
		if (sna.type.ss) fclose(fpss);

		free(snap);
	}

	return 0;
}