Ejemplo n.º 1
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;
}
Ejemplo n.º 2
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;
}